From 0edab2b20fa6ade295eb5e59871a4e3baefadd60 Mon Sep 17 00:00:00 2001 From: Jukka Ojanen Date: Tue, 6 Jul 2021 16:47:18 +0300 Subject: [PATCH] Allow CPU affinity mask up to 64 processors in Windows. Remove call to SetThreadAffinityMask as SetProcessAffinityMask limits all threads in process. Report error code. pthread_setaffinity_np returns non-zero if failure; works also with OSX as it reports KERN_SUCCESS which is zero. --- src/affinity.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/affinity.c b/src/affinity.c index 0cf6d843a..3faa46638 100644 --- a/src/affinity.c +++ b/src/affinity.c @@ -56,8 +56,10 @@ int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx) #if defined (_WIN) DWORD_PTR aff_mask = 0; + const int cpu_id_max = 8 * sizeof (aff_mask); #else cpu_set_t cpuset; + const int cpu_id_max = 8 * sizeof (cpuset); CPU_ZERO (&cpuset); #endif @@ -84,7 +86,7 @@ int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx) break; } - if (cpu_id > 32) + if (cpu_id > cpu_id_max) { event_log_error (hashcat_ctx, "Invalid cpu_id %d specified.", cpu_id); @@ -94,7 +96,7 @@ int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx) } #if defined (_WIN) - aff_mask |= 1u << (cpu_id - 1); + aff_mask |= ((DWORD_PTR) 1) << (cpu_id - 1); #else CPU_SET ((cpu_id - 1), &cpuset); #endif @@ -105,11 +107,9 @@ int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx) #if defined (_WIN) - SetProcessAffinityMask (GetCurrentProcess (), aff_mask); - - if (SetThreadAffinityMask (GetCurrentThread (), aff_mask) == 0) + if (SetProcessAffinityMask (GetCurrentProcess (), aff_mask) == 0) { - event_log_error (hashcat_ctx, "%s", "SetThreadAffinityMask()."); + event_log_error (hashcat_ctx, "SetProcessAffinityMask() failed with error: %d", (int) GetLastError ()); return -1; } @@ -118,9 +118,11 @@ int set_cpu_affinity (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx) pthread_t thread = pthread_self (); - if (pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset) == -1) + const int rc = pthread_setaffinity_np (thread, sizeof (cpu_set_t), &cpuset); + + if (rc != 0) { - event_log_error (hashcat_ctx, "%s", "pthread_setaffinity_np()."); + event_log_error (hashcat_ctx, "pthread_setaffinity_np() failed with error: %d", rc); return -1; }