1
0
mirror of https://github.com/hashcat/hashcat.git synced 2025-07-09 08:08:15 +00:00

Merge pull request #4278 from matrix/hwmon_ctx_init_split

Refactoring Hardware Monitor
This commit is contained in:
Jens Steube 2025-07-01 20:24:20 +02:00 committed by GitHub
commit 9aa7c94ede
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 259 additions and 224 deletions

View File

@ -92,6 +92,7 @@
- Fixed bug in grep out-of-memory workaround on Unit Test - Fixed bug in grep out-of-memory workaround on Unit Test
- Fixed bug in input_tokenizer when TOKEN_ATTR_FIXED_LENGTH is used and refactor modules - Fixed bug in input_tokenizer when TOKEN_ATTR_FIXED_LENGTH is used and refactor modules
- Fixed bug in --stdout that caused certain rules to malfunction - Fixed bug in --stdout that caused certain rules to malfunction
- Fixed bug in Hardware Monitor: prevent disable if ADL fail
- Fixed race condition in selftest_init on OpenCL with non-blocking write - Fixed race condition in selftest_init on OpenCL with non-blocking write
- Fixed build failed for 10700 optimized with Apple Metal - Fixed build failed for 10700 optimized with Apple Metal
- Fixed build failed for 13772 and 13773 with Apple Metal - Fixed build failed for 13772 and 13773 with Apple Metal
@ -156,6 +157,7 @@
- Modules: Check UnpackSize to raise false positive with hc_decompress_rar - Modules: Check UnpackSize to raise false positive with hc_decompress_rar
- User Options: added --metal-compiler-runtime option - User Options: added --metal-compiler-runtime option
- Hardware Monitor: avoid sprintf in src/ext_iokit.c - Hardware Monitor: avoid sprintf in src/ext_iokit.c
- Hardware Monitor: Splitting hwmon_ctx_init function into smaller library-specific functions
- Help: show supported hash-modes only with -hh - Help: show supported hash-modes only with -hh
- Makefile: prevent make failure with Apple Silicon in case of partial rebuild - Makefile: prevent make failure with Apple Silicon in case of partial rebuild
- Rules: Rename best64.rule to best66.rule and remove the unknown section from it - Rules: Rename best64.rule to best66.rule and remove the unknown section from it

View File

@ -1268,142 +1268,10 @@ u64 hm_get_memoryused_with_devices_idx (hashcat_ctx_t *hashcat_ctx, const int ba
return 0; return 0;
} }
int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) static void hwmon_ctx_init_nvml (hashcat_ctx_t *hashcat_ctx, hm_attrs_t *hm_adapters_nvml, int backend_devices_cnt)
{ {
bridge_ctx_t *bridge_ctx = hashcat_ctx->bridge_ctx;
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx; backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
user_options_t *user_options = hashcat_ctx->user_options; hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
hwmon_ctx->enabled = false;
int backend_devices_cnt = backend_ctx->backend_devices_cnt;
if (bridge_ctx->enabled == true) backend_devices_cnt = 1;
//#if !defined (WITH_HWMON)
//return 0;
//#endif // WITH_HWMON
if (user_options->usage > 0) return 0;
//if (user_options->backend_info > 0) return 0;
if (user_options->hash_info == true) return 0;
if (user_options->keyspace == true) return 0;
if (user_options->left == true) return 0;
if (user_options->show == true) return 0;
if (user_options->stdout_flag == true) return 0;
if (user_options->version == true) return 0;
if (user_options->identify == true) return 0;
//we need hwmon support to get free memory per device support
//its a joke, but there's no way around
//if (user_options->hwmon == false) return 0;
hwmon_ctx->hm_device = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
/**
* Initialize shared libraries
*/
hm_attrs_t *hm_adapters_adl = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_nvapi = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_nvml = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_sysfs_amdgpu = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_sysfs_cpu = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_iokit = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
#define FREE_ADAPTERS \
do { \
hcfree (hm_adapters_adl); \
hcfree (hm_adapters_nvapi); \
hcfree (hm_adapters_nvml); \
hcfree (hm_adapters_sysfs_amdgpu); \
hcfree (hm_adapters_sysfs_cpu); \
hcfree (hm_adapters_iokit); \
} while (0)
if (backend_ctx->need_nvml == true)
{
hwmon_ctx->hm_nvml = (NVML_PTR *) hcmalloc (sizeof (NVML_PTR));
if (nvml_init (hashcat_ctx) == -1)
{
hcfree (hwmon_ctx->hm_nvml);
hwmon_ctx->hm_nvml = NULL;
}
}
if ((backend_ctx->need_nvapi == true) && (hwmon_ctx->hm_nvml)) // nvapi can't work alone, we need nvml, too
{
hwmon_ctx->hm_nvapi = (NVAPI_PTR *) hcmalloc (sizeof (NVAPI_PTR));
if (nvapi_init (hashcat_ctx) == -1)
{
hcfree (hwmon_ctx->hm_nvapi);
hwmon_ctx->hm_nvapi = NULL;
}
}
if (backend_ctx->need_adl == true)
{
hwmon_ctx->hm_adl = (ADL_PTR *) hcmalloc (sizeof (ADL_PTR));
if (adl_init (hashcat_ctx) == -1)
{
hcfree (hwmon_ctx->hm_adl);
hwmon_ctx->hm_adl = NULL;
}
}
if (backend_ctx->need_sysfs_amdgpu == true)
{
hwmon_ctx->hm_sysfs_amdgpu = (SYSFS_AMDGPU_PTR *) hcmalloc (sizeof (SYSFS_AMDGPU_PTR));
if (sysfs_amdgpu_init (hashcat_ctx) == false)
{
hcfree (hwmon_ctx->hm_sysfs_amdgpu);
hwmon_ctx->hm_sysfs_amdgpu = NULL;
}
// also if there's ADL, we don't need sysfs_amdgpu
if (hwmon_ctx->hm_adl)
{
hcfree (hwmon_ctx->hm_sysfs_amdgpu);
hwmon_ctx->hm_sysfs_amdgpu = NULL;
}
}
if (backend_ctx->need_sysfs_cpu == true)
{
hwmon_ctx->hm_sysfs_cpu = (SYSFS_CPU_PTR *) hcmalloc (sizeof (SYSFS_CPU_PTR));
if (sysfs_cpu_init (hashcat_ctx) == false)
{
hcfree (hwmon_ctx->hm_sysfs_cpu);
hwmon_ctx->hm_sysfs_cpu = NULL;
}
}
#if defined(__APPLE__)
if (backend_ctx->need_iokit == true)
{
hwmon_ctx->hm_iokit = (IOKIT_PTR *) hcmalloc (sizeof (IOKIT_PTR));
if (iokit_init (hashcat_ctx) == false)
{
hcfree (hwmon_ctx->hm_iokit);
hwmon_ctx->hm_iokit = NULL;
}
}
#endif
if (hwmon_ctx->hm_nvml) if (hwmon_ctx->hm_nvml)
{ {
@ -1485,6 +1353,12 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
hcfree (nvmlGPUHandle); hcfree (nvmlGPUHandle);
} }
} }
}
static void hwmon_ctx_init_nvapi (hashcat_ctx_t *hashcat_ctx, hm_attrs_t *hm_adapters_nvapi, int backend_devices_cnt)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
if (hwmon_ctx->hm_nvapi) if (hwmon_ctx->hm_nvapi)
{ {
@ -1558,6 +1432,12 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
hcfree (nvGPUHandle); hcfree (nvGPUHandle);
} }
} }
}
static int hwmon_ctx_init_adl (hashcat_ctx_t *hashcat_ctx, hm_attrs_t *hm_adapters_adl, int backend_devices_cnt)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
if (hwmon_ctx->hm_adl) if (hwmon_ctx->hm_adl)
{ {
@ -1567,23 +1447,13 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
int tmp_in; int tmp_in;
if (get_adapters_num_adl (hashcat_ctx, &tmp_in) == -1) if (get_adapters_num_adl (hashcat_ctx, &tmp_in) == -1) return -1;
{
FREE_ADAPTERS;
return -1;
}
// adapter info // adapter info
LPAdapterInfo lpAdapterInfo = (LPAdapterInfo) hccalloc (tmp_in, sizeof (AdapterInfo)); LPAdapterInfo lpAdapterInfo = (LPAdapterInfo) hccalloc (tmp_in, sizeof (AdapterInfo));
if (hm_ADL_Adapter_AdapterInfo_Get (hashcat_ctx, lpAdapterInfo, tmp_in * sizeof (AdapterInfo)) == -1) if (hm_ADL_Adapter_AdapterInfo_Get (hashcat_ctx, lpAdapterInfo, tmp_in * sizeof (AdapterInfo)) == -1) return -1;
{
FREE_ADAPTERS;
return -1;
}
for (int backend_devices_idx = 0; backend_devices_idx < backend_devices_cnt; backend_devices_idx++) for (int backend_devices_idx = 0; backend_devices_idx < backend_devices_cnt; backend_devices_idx++)
{ {
@ -1639,9 +1509,15 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
} }
} }
return 0;
}
static void hwmon_ctx_init_sysfs_amdgpu_iokit (hashcat_ctx_t *hashcat_ctx, hm_attrs_t *hm_adapters_sysfs_amdgpu, hm_attrs_t *hm_adapters_iokit, int backend_devices_cnt)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
if (hwmon_ctx->hm_sysfs_amdgpu || hwmon_ctx->hm_iokit) if (hwmon_ctx->hm_sysfs_amdgpu || hwmon_ctx->hm_iokit)
{
if (true)
{ {
for (int backend_devices_idx = 0; backend_devices_idx < backend_devices_cnt; backend_devices_idx++) for (int backend_devices_idx = 0; backend_devices_idx < backend_devices_cnt; backend_devices_idx++)
{ {
@ -1703,11 +1579,14 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
} }
} }
} }
} }
static void hwmon_ctx_init_sysfs_cpu (hashcat_ctx_t *hashcat_ctx, hm_attrs_t *hm_adapters_sysfs_cpu, int backend_devices_cnt)
{
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
if (hwmon_ctx->hm_sysfs_cpu) if (hwmon_ctx->hm_sysfs_cpu)
{
if (true)
{ {
for (int backend_devices_idx = 0; backend_devices_idx < backend_devices_cnt; backend_devices_idx++) for (int backend_devices_idx = 0; backend_devices_idx < backend_devices_cnt; backend_devices_idx++)
{ {
@ -1739,7 +1618,151 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
} }
} }
} }
}
int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
bridge_ctx_t *bridge_ctx = hashcat_ctx->bridge_ctx;
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx;
user_options_t *user_options = hashcat_ctx->user_options;
hwmon_ctx->enabled = false;
int backend_devices_cnt = backend_ctx->backend_devices_cnt;
if (bridge_ctx->enabled == true) backend_devices_cnt = 1;
//#if !defined (WITH_HWMON)
//return 0;
//#endif // WITH_HWMON
if (user_options->usage > 0) return 0;
//if (user_options->backend_info > 0) return 0;
if (user_options->hash_info == true) return 0;
if (user_options->keyspace == true) return 0;
if (user_options->left == true) return 0;
if (user_options->show == true) return 0;
if (user_options->stdout_flag == true) return 0;
if (user_options->version == true) return 0;
if (user_options->identify == true) return 0;
//we need hwmon support to get free memory per device support
//its a joke, but there's no way around
//if (user_options->hwmon == false) return 0;
hwmon_ctx->hm_device = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
/**
* Initialize shared libraries
*/
hm_attrs_t *hm_adapters_adl = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_nvapi = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_nvml = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_sysfs_amdgpu = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_sysfs_cpu = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
hm_attrs_t *hm_adapters_iokit = (hm_attrs_t *) hccalloc (DEVICES_MAX, sizeof (hm_attrs_t));
if (backend_ctx->need_nvml == true)
{
hwmon_ctx->hm_nvml = (NVML_PTR *) hcmalloc (sizeof (NVML_PTR));
if (nvml_init (hashcat_ctx) == -1)
{
hcfree (hwmon_ctx->hm_nvml);
hwmon_ctx->hm_nvml = NULL;
} }
}
if ((backend_ctx->need_nvapi == true) && (hwmon_ctx->hm_nvml)) // nvapi can't work alone, we need nvml, too
{
hwmon_ctx->hm_nvapi = (NVAPI_PTR *) hcmalloc (sizeof (NVAPI_PTR));
if (nvapi_init (hashcat_ctx) == -1)
{
hcfree (hwmon_ctx->hm_nvapi);
hwmon_ctx->hm_nvapi = NULL;
}
}
if (backend_ctx->need_adl == true)
{
hwmon_ctx->hm_adl = (ADL_PTR *) hcmalloc (sizeof (ADL_PTR));
if (adl_init (hashcat_ctx) == -1)
{
hcfree (hwmon_ctx->hm_adl);
hwmon_ctx->hm_adl = NULL;
}
}
if (backend_ctx->need_sysfs_amdgpu == true)
{
hwmon_ctx->hm_sysfs_amdgpu = (SYSFS_AMDGPU_PTR *) hcmalloc (sizeof (SYSFS_AMDGPU_PTR));
if (sysfs_amdgpu_init (hashcat_ctx) == false)
{
hcfree (hwmon_ctx->hm_sysfs_amdgpu);
hwmon_ctx->hm_sysfs_amdgpu = NULL;
}
}
if (backend_ctx->need_sysfs_cpu == true)
{
hwmon_ctx->hm_sysfs_cpu = (SYSFS_CPU_PTR *) hcmalloc (sizeof (SYSFS_CPU_PTR));
if (sysfs_cpu_init (hashcat_ctx) == false)
{
hcfree (hwmon_ctx->hm_sysfs_cpu);
hwmon_ctx->hm_sysfs_cpu = NULL;
}
}
#if defined(__APPLE__)
if (backend_ctx->need_iokit == true)
{
hwmon_ctx->hm_iokit = (IOKIT_PTR *) hcmalloc (sizeof (IOKIT_PTR));
if (iokit_init (hashcat_ctx) == false)
{
hcfree (hwmon_ctx->hm_iokit);
hwmon_ctx->hm_iokit = NULL;
}
}
#endif
hwmon_ctx_init_nvml (hashcat_ctx, hm_adapters_nvml, backend_devices_cnt);
hwmon_ctx_init_nvapi (hashcat_ctx, hm_adapters_nvapi, backend_devices_cnt);
// if ADL init fail, disable
if (hwmon_ctx_init_adl (hashcat_ctx, hm_adapters_adl, backend_devices_cnt) == -1)
{
hcfree (hwmon_ctx->hm_adl);
hwmon_ctx->hm_adl = NULL;
}
// if there's ADL, we don't need sysfs_amdgpu
if (hwmon_ctx->hm_adl)
{
hcfree (hwmon_ctx->hm_sysfs_amdgpu);
hwmon_ctx->hm_sysfs_amdgpu = NULL;
}
hwmon_ctx_init_sysfs_amdgpu_iokit (hashcat_ctx, hm_adapters_sysfs_amdgpu, hm_adapters_iokit, backend_devices_cnt);
hwmon_ctx_init_sysfs_cpu (hashcat_ctx, hm_adapters_sysfs_cpu, backend_devices_cnt);
#if defined(__APPLE__) #if defined(__APPLE__)
if (backend_ctx->need_iokit == true) if (backend_ctx->need_iokit == true)
@ -1757,7 +1780,12 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_sysfs_amdgpu == NULL && hwmon_ctx->hm_sysfs_cpu == NULL && hwmon_ctx->hm_iokit == NULL) if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_sysfs_amdgpu == NULL && hwmon_ctx->hm_sysfs_cpu == NULL && hwmon_ctx->hm_iokit == NULL)
{ {
FREE_ADAPTERS; hcfree (hm_adapters_adl);
hcfree (hm_adapters_nvapi);
hcfree (hm_adapters_nvml);
hcfree (hm_adapters_sysfs_amdgpu);
hcfree (hm_adapters_sysfs_cpu);
hcfree (hm_adapters_iokit);
return 0; return 0;
} }
@ -1992,7 +2020,12 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
hm_get_memoryused_with_devices_idx (hashcat_ctx, backend_devices_idx); hm_get_memoryused_with_devices_idx (hashcat_ctx, backend_devices_idx);
} }
FREE_ADAPTERS; hcfree (hm_adapters_adl);
hcfree (hm_adapters_nvapi);
hcfree (hm_adapters_nvml);
hcfree (hm_adapters_sysfs_amdgpu);
hcfree (hm_adapters_sysfs_cpu);
hcfree (hm_adapters_iokit);
return 0; return 0;
} }