Add mangling code
[hashcat.git] / src / hashcat.c
index 492e52d..d11921f 100644 (file)
@@ -1387,13 +1387,13 @@ void status_display ()
 
             #ifdef WIN
 
-            __time64_t runtime_left = data.proc_start + data.runtime - runtime_cur;
+            __time64_t runtime_left = data.proc_start + data.runtime + data.prepare_time - runtime_cur;
 
             tmp = _gmtime64 (&runtime_left);
 
             #else
 
-            time_t runtime_left = data.proc_start + data.runtime - runtime_cur;
+            time_t runtime_left = data.proc_start + data.runtime + data.prepare_time - runtime_cur;
 
             tmp = gmtime (&runtime_left);
 
@@ -3592,7 +3592,7 @@ static int autotune (hc_device_param_t *device_param)
   {
     for (int i = 0; i < STEPS_CNT; i++)
     {
-      const u32 kernel_accel_try = 1 << i;
+      const u32 kernel_accel_try = 1u << i;
 
       if (kernel_accel_try < kernel_accel_min) continue;
       if (kernel_accel_try > kernel_accel_max) break;
@@ -4692,7 +4692,7 @@ static void *thread_monitor (void *p)
 
       time (&runtime_cur);
 
-      int runtime_left = data.proc_start + data.runtime - runtime_cur;
+      int runtime_left = data.proc_start + data.runtime + data.prepare_time - runtime_cur;
 
       if (runtime_left <= 0)
       {
@@ -6147,6 +6147,22 @@ int main (int argc, char **argv)
 
   umask (077);
 
+  /**
+   * There's some buggy OpenCL runtime that do not support -I.
+   * A workaround is to chdir() to the OpenCL folder,
+   * then compile the kernels,
+   * then chdir() back to where we came from so we need to save it first
+   */
+
+  char cwd[1024];
+
+  if (getcwd (cwd, sizeof (cwd) - 1) == NULL)
+  {
+    log_error ("ERROR: getcwd(): %s", strerror (errno));
+
+    return -1;
+  }
+
   /**
    * Real init
    */
@@ -6159,6 +6175,10 @@ int main (int argc, char **argv)
 
   data.proc_start = proc_start;
 
+  time_t prepare_start;
+
+  time (&prepare_start);
+
   int    myargc = argc;
   char **myargv = argv;
 
@@ -6547,6 +6567,66 @@ int main (int argc, char **argv)
 
   myfree (exec_path);
 
+  /**
+   * There's alot of problem related to bad support -I parameters when building the kernel.
+   * Each OpenCL runtime handles it slightly different.
+   * The most problematic is with new AMD drivers on Windows, which can not handle quote characters!
+   * The best workaround found so far is to modify the TMP variable (only inside hashcat process) before the runtime is load
+   */
+
+  char cpath[1024] = { 0 };
+
+  #if _WIN
+
+  snprintf (cpath, sizeof (cpath) - 1, "%s\\OpenCL\\", shared_dir);
+
+  char *cpath_real = mymalloc (MAX_PATH);
+
+  if (GetFullPathName (cpath, MAX_PATH, cpath_real, NULL) == 0)
+  {
+    log_error ("ERROR: %s: %s", cpath, "GetFullPathName()");
+
+    return -1;
+  }
+
+  #else
+
+  snprintf (cpath, sizeof (cpath) - 1, "%s/OpenCL/", shared_dir);
+
+  char *cpath_real = mymalloc (PATH_MAX);
+
+  if (realpath (cpath, cpath_real) == NULL)
+  {
+    log_error ("ERROR: %s: %s", cpath, strerror (errno));
+
+    return -1;
+  }
+
+  #endif
+
+  //if (getenv ("TMP") == NULL)
+  if (1)
+  {
+    char tmp[1000];
+
+    snprintf (tmp, sizeof (tmp) - 1, "TMP=%s", cpath_real);
+
+    putenv (tmp);
+  }
+
+  #if _WIN
+
+  naive_replace (cpath_real, '\\', '/');
+
+  // not escaping here, windows using quotes later
+  // naive_escape (cpath_real, PATH_MAX,  ' ', '\\');
+
+  #else
+
+  naive_escape (cpath_real, PATH_MAX,  ' ', '\\');
+
+  #endif
+
   /**
    * kernel cache, we need to make sure folder exist
    */
@@ -13765,14 +13845,14 @@ int main (int argc, char **argv)
 
     if (bitmap_max < bitmap_min) bitmap_max = bitmap_min;
 
-    uint *bitmap_s1_a = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
-    uint *bitmap_s1_b = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
-    uint *bitmap_s1_c = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
-    uint *bitmap_s1_d = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
-    uint *bitmap_s2_a = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
-    uint *bitmap_s2_b = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
-    uint *bitmap_s2_c = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
-    uint *bitmap_s2_d = (uint *) mymalloc ((1 << bitmap_max) * sizeof (uint));
+    uint *bitmap_s1_a = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
+    uint *bitmap_s1_b = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
+    uint *bitmap_s1_c = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
+    uint *bitmap_s1_d = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
+    uint *bitmap_s2_a = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
+    uint *bitmap_s2_b = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
+    uint *bitmap_s2_c = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
+    uint *bitmap_s2_d = (uint *) mymalloc ((1u << bitmap_max) * sizeof (uint));
 
     uint bitmap_bits;
     uint bitmap_nums;
@@ -13783,7 +13863,7 @@ int main (int argc, char **argv)
     {
       if (data.quiet == 0) log_info_nn ("Generating bitmap tables with %u bits...", bitmap_bits);
 
-      bitmap_nums = 1 << bitmap_bits;
+      bitmap_nums = 1u << bitmap_bits;
 
       bitmap_mask = bitmap_nums - 1;
 
@@ -13797,7 +13877,7 @@ int main (int argc, char **argv)
       break;
     }
 
-    bitmap_nums = 1 << bitmap_bits;
+    bitmap_nums = 1u << bitmap_bits;
 
     bitmap_mask = bitmap_nums - 1;
 
@@ -14087,7 +14167,7 @@ int main (int argc, char **argv)
 
       for (uint platform_id = 0; platform_id < platforms_cnt; platform_id++)
       {
-        if ((opencl_platforms_filter & (1 << platform_id)) == 0) continue;
+        if ((opencl_platforms_filter & (1u << platform_id)) == 0) continue;
 
         cl_platform_id platform = platforms[platform_id];
 
@@ -14095,9 +14175,13 @@ int main (int argc, char **argv)
 
         if (CL_err != CL_SUCCESS)
         {
-          log_error ("ERROR: clGetDeviceIDs(): %s\n", val2cstr_cl (CL_err));
+          //log_error ("ERROR: clGetDeviceIDs(): %s\n", val2cstr_cl (CL_err));
 
-          return -1;
+          //return -1;
+
+          // Silently ignore at this point, it will be reused later and create a note for the user at that point
+
+          continue;
         }
 
         for (uint platform_devices_id = 0; platform_devices_id < platform_devices_cnt; platform_devices_id++)
@@ -14161,15 +14245,6 @@ int main (int argc, char **argv)
 
       cl_platform_id platform = platforms[platform_id];
 
-      CL_err = hc_clGetDeviceIDs (data.ocl, platform, CL_DEVICE_TYPE_ALL, DEVICES_MAX, platform_devices, &platform_devices_cnt);
-
-      if (CL_err != CL_SUCCESS)
-      {
-        log_error ("ERROR: clGetDeviceIDs(): %s\n", val2cstr_cl (CL_err));
-
-        return -1;
-      }
-
       char platform_vendor[INFOSZ] = { 0 };
 
       CL_err = hc_clGetPlatformInfo (data.ocl, platform, CL_PLATFORM_VENDOR, sizeof (platform_vendor), platform_vendor, NULL);
@@ -14224,7 +14299,18 @@ int main (int argc, char **argv)
         platform_vendor_id = VENDOR_ID_GENERIC;
       }
 
-      const uint platform_skipped = ((opencl_platforms_filter & (1 << platform_id)) == 0);
+      uint platform_skipped = ((opencl_platforms_filter & (1u << platform_id)) == 0);
+
+      CL_err = hc_clGetDeviceIDs (data.ocl, platform, CL_DEVICE_TYPE_ALL, DEVICES_MAX, platform_devices, &platform_devices_cnt);
+
+      if (CL_err != CL_SUCCESS)
+      {
+        //log_error ("ERROR: clGetDeviceIDs(): %s\n", val2cstr_cl (CL_err));
+
+        //return -1;
+
+        platform_skipped = 2;
+      }
 
       if ((benchmark == 1 || quiet == 0) && (algorithm_pos == 0))
       {
@@ -14240,15 +14326,21 @@ int main (int argc, char **argv)
 
             log_info (line);
           }
-          else
+          else if (platform_skipped == 1)
           {
             log_info ("OpenCL Platform #%u: %s, skipped", platform_id + 1, platform_vendor);
             log_info ("");
           }
+          else if (platform_skipped == 2)
+          {
+            log_info ("OpenCL Platform #%u: %s, skipped! No OpenCL compatible devices found", platform_id + 1, platform_vendor);
+            log_info ("");
+          }
         }
       }
 
       if (platform_skipped == 1) continue;
+      if (platform_skipped == 2) continue;
 
       for (uint platform_devices_id = 0; platform_devices_id < platform_devices_cnt; platform_devices_id++)
       {
@@ -14714,7 +14806,7 @@ int main (int argc, char **argv)
 
         // skipped
 
-        device_param->skipped |= ((devices_filter      & (1 << device_id)) == 0);
+        device_param->skipped |= ((devices_filter      & (1u << device_id)) == 0);
         device_param->skipped |= ((device_types_filter & (device_type))    == 0);
 
         // driver_version
@@ -15757,7 +15849,7 @@ int main (int argc, char **argv)
         {
           size_scrypt = (128 * scrypt_r) * scrypt_N;
 
-          size_scrypt /= 1 << tmto;
+          size_scrypt /= 1u << tmto;
 
           size_scrypt *= device_param->device_processors * device_param->kernel_threads * device_param->kernel_accel_max;
 
@@ -16062,46 +16154,19 @@ int main (int argc, char **argv)
        * default building options
        */
 
-      char cpath[1024] = { 0 };
-
-      char build_opts[1024] = { 0 };
-
-      #if _WIN
-
-      snprintf (cpath, sizeof (cpath) - 1, "%s\\OpenCL\\", shared_dir);
-
-      char *cpath_real = mymalloc (MAX_PATH);
-
-      if (GetFullPathName (cpath, MAX_PATH, cpath_real, NULL) == 0)
+      if (chdir (cpath_real) == -1)
       {
-        log_error ("ERROR: %s: %s", cpath, "GetFullPathName()");
+        log_error ("ERROR: %s: %s", cpath_real, strerror (errno));
 
         return -1;
       }
 
-      naive_replace (cpath_real, '\\', '/');
-
-      // not escaping here, windows has quotes
+      char build_opts[1024] = { 0 };
 
+      #if _WIN
       snprintf (build_opts, sizeof (build_opts) - 1, "-I \"%s\"", cpath_real);
-
       #else
-
-      snprintf (cpath, sizeof (cpath) - 1, "%s/OpenCL/", shared_dir);
-
-      char *cpath_real = mymalloc (PATH_MAX);
-
-      if (realpath (cpath, cpath_real) == NULL)
-      {
-        log_error ("ERROR: %s: %s", cpath, strerror (errno));
-
-        return -1;
-      }
-
-      naive_escape (cpath_real, PATH_MAX,  ' ', '\\');
-
       snprintf (build_opts, sizeof (build_opts) - 1, "-I %s", cpath_real);
-
       #endif
 
       // include check
@@ -16131,15 +16196,11 @@ int main (int argc, char **argv)
 
       for (int i = 0; i < files_cnt; i++)
       {
-        char path[1024] = { 0 };
-
-        snprintf (path, sizeof (path) - 1, "%s/%s", cpath_real, files_names[i]);
-
-        FILE *fd = fopen (path, "r");
+        FILE *fd = fopen (files_names[i], "r");
 
         if (fd == NULL)
         {
-          log_error ("ERROR: %s: fopen(): %s", path, strerror (errno));
+          log_error ("ERROR: %s: fopen(): %s", files_names[i], strerror (errno));
 
           return -1;
         }
@@ -16150,7 +16211,7 @@ int main (int argc, char **argv)
 
         if (n != 1)
         {
-          log_error ("ERROR: %s: fread(): %s", path, strerror (errno));
+          log_error ("ERROR: %s: fread(): %s", files_names[i], strerror (errno));
 
           return -1;
         }
@@ -16158,8 +16219,6 @@ int main (int argc, char **argv)
         fclose (fd);
       }
 
-      myfree (cpath_real);
-
       // we don't have sm_* on vendors not NV but it doesn't matter
 
       char build_opts_new[1024] = { 0 };
@@ -16707,6 +16766,15 @@ int main (int argc, char **argv)
         local_free (kernel_sources);
       }
 
+      // return back to the folder we came from initially (workaround)
+
+      if (chdir (cwd) == -1)
+      {
+        log_error ("ERROR: %s: %s", cwd, strerror (errno));
+
+        return -1;
+      }
+
       // some algorithm collide too fast, make that impossible
 
       if (benchmark == 1)
@@ -19372,6 +19440,8 @@ int main (int argc, char **argv)
 
         data.runtime_start = runtime_start;
 
+        data.prepare_time += runtime_start - prepare_start;
+
         for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
         {
           hc_device_param_t *device_param = &devices_param[device_id];
@@ -19461,6 +19531,8 @@ int main (int argc, char **argv)
         logfile_sub_uint (runtime_start);
         logfile_sub_uint (runtime_stop);
 
+        time (&prepare_start);
+
         logfile_sub_msg ("STOP");
 
         global_free (subid);