From 4045e600215036957c4f657ef528bb6da6ca53ef Mon Sep 17 00:00:00 2001 From: Jens Steube Date: Fri, 26 Apr 2019 10:03:16 +0200 Subject: [PATCH] Add nvrtc wrapper for later use --- OpenCL/inc_platform.h | 6 +-- docs/changes.txt | 1 + docs/readme.txt | 2 +- include/ext_nvrtc.h | 81 +++++++++++++++++++++++++++++++++++ include/types.h | 2 + src/Makefile | 2 +- src/backend.c | 99 ++++++++++++++++++++++++++++++++++++++++++- src/ext_nvrtc.c | 8 ++++ 8 files changed, 193 insertions(+), 8 deletions(-) create mode 100644 include/ext_nvrtc.h create mode 100644 src/ext_nvrtc.c diff --git a/OpenCL/inc_platform.h b/OpenCL/inc_platform.h index fd3d310d1..7d27852d9 100644 --- a/OpenCL/inc_platform.h +++ b/OpenCL/inc_platform.h @@ -4,6 +4,7 @@ */ #ifndef _INC_PLATFORM_H +#define _INC_PLATFORM_H #ifdef IS_CUDA DECLSPEC u32 atomic_dec (u32 *p); @@ -11,11 +12,6 @@ DECLSPEC u32 atomic_inc (u32 *p); DECLSPEC size_t get_global_id (const u32 dimindx __attribute__((unused))); DECLSPEC size_t get_local_id (const u32 dimindx __attribute__((unused))); DECLSPEC size_t get_local_size (const u32 dimindx __attribute__((unused))); -DECLSPEC uint4 uint4_init (const u32 a); -DECLSPEC uint4 uint4_init (const u32 a, const u32 b, const u32 c, const u32 d); -DECLSPEC __inline__ u8 rotate (const u8 v, const int i); -DECLSPEC __inline__ u32 rotate (const u32 v, const int i); -DECLSPEC __inline__ u64 rotate (const u64 v, const int i); #define rotate(a,n) (((a) << (n)) | ((a) >> (32 - (n)))) #define bitselect(a,b,c) ((a) ^ ((c) & ((b) ^ (a)))) diff --git a/docs/changes.txt b/docs/changes.txt index 6f6b68b91..d18141fc4 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -73,6 +73,7 @@ - Kernel Compile: Removed -cl-std= from all kernel build options since we're compatible to all OpenCL versions - Mode 16800/16801 hash format: Changed separator character from '*' to ':' - Requirements: Update runtime check for minimum NVIDIA driver version from 367.x to 418.56 or later +- Requirements: Add new requirement for NVIDIA GPU: CUDA Toolkit (10.1 or later) * changes v5.0.0 -> v5.1.0 diff --git a/docs/readme.txt b/docs/readme.txt index 2614bdf0b..ae10eb231 100644 --- a/docs/readme.txt +++ b/docs/readme.txt @@ -6,7 +6,7 @@ AMD GPUs on Windows require "AMD Radeon Software Crimson Edition" (15.12 or late Intel CPUs require "OpenCL Runtime for Intel Core and Intel Xeon Processors" (16.1.1 or later) Intel GPUs on Linux require "OpenCL 2.0 GPU Driver Package for Linux" (2.0 or later) Intel GPUs on Windows require "OpenCL Driver for Intel Iris and Intel HD Graphics" -NVIDIA GPUs require "NVIDIA Driver" (418.56 or later) +NVIDIA GPUs require "NVIDIA Driver" (418.56 or later) and "CUDA Toolkit" (10.1 or later) ## ## Features diff --git a/include/ext_nvrtc.h b/include/ext_nvrtc.h new file mode 100644 index 000000000..407170c16 --- /dev/null +++ b/include/ext_nvrtc.h @@ -0,0 +1,81 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#ifndef _EXT_NVRTC_H +#define _EXT_NVRTC_H + +/** + * from cuda.h (/usr/local/cuda-10.1/targets/x86_64-linux/include/nvrtc.h) + */ + +/** + * \ingroup error + * \brief The enumerated type nvrtcResult defines API call result codes. + * NVRTC API functions return nvrtcResult to indicate the call + * result. + */ +typedef enum { + NVRTC_SUCCESS = 0, + NVRTC_ERROR_OUT_OF_MEMORY = 1, + NVRTC_ERROR_PROGRAM_CREATION_FAILURE = 2, + NVRTC_ERROR_INVALID_INPUT = 3, + NVRTC_ERROR_INVALID_PROGRAM = 4, + NVRTC_ERROR_INVALID_OPTION = 5, + NVRTC_ERROR_COMPILATION = 6, + NVRTC_ERROR_BUILTIN_OPERATION_FAILURE = 7, + NVRTC_ERROR_NO_NAME_EXPRESSIONS_AFTER_COMPILATION = 8, + NVRTC_ERROR_NO_LOWERED_NAMES_BEFORE_COMPILATION = 9, + NVRTC_ERROR_NAME_EXPRESSION_NOT_VALID = 10, + NVRTC_ERROR_INTERNAL_ERROR = 11 +} nvrtcResult; + +/** + * \ingroup compilation + * \brief nvrtcProgram is the unit of compilation, and an opaque handle for + * a program. + * + * To compile a CUDA program string, an instance of nvrtcProgram must be + * created first with ::nvrtcCreateProgram, then compiled with + * ::nvrtcCompileProgram. + */ +typedef struct _nvrtcProgram *nvrtcProgram; + +#ifdef _WIN32 +#define NVRTCAPI __stdcall +#else +#define NVRTCAPI +#endif + +#define NVRTC_API_CALL NVRTCAPI + +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCADDNAMEEXPRESSION) (nvrtcProgram, const char *); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCCOMPILEPROGRAM) (nvrtcProgram, int, const char **); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCCREATEPROGRAM) (nvrtcProgram *, const char *, const char *, int, const char **, const char **); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCDESTROYPROGRAM) (nvrtcProgram *); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCGETLOWEREDNAME) (nvrtcProgram, const char *, const char **); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCGETPTX) (nvrtcProgram, char *); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCGETPTXSIZE) (nvrtcProgram, size_t *); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCGETPROGRAMLOG) (nvrtcProgram, char *); +typedef nvrtcResult (NVRTC_API_CALL *NVRTC_NVRTCGETPROGRAMLOGSIZE) (nvrtcProgram, size_t *); + +typedef struct hc_nvrtc_lib +{ + hc_dynlib_t lib; + + NVRTC_NVRTCADDNAMEEXPRESSION nvrtcAddNameExpression; + NVRTC_NVRTCCOMPILEPROGRAM nvrtcCompileProgram; + NVRTC_NVRTCCREATEPROGRAM nvrtcCreateProgram; + NVRTC_NVRTCDESTROYPROGRAM nvrtcDestroyProgram; + NVRTC_NVRTCGETLOWEREDNAME nvrtcGetLoweredName; + NVRTC_NVRTCGETPTX nvrtcGetPTX; + NVRTC_NVRTCGETPTXSIZE nvrtcGetPTXSize; + NVRTC_NVRTCGETPROGRAMLOG nvrtcGetProgramLog; + NVRTC_NVRTCGETPROGRAMLOGSIZE nvrtcGetProgramLogSize; + +} hc_nvrtc_lib_t; + +typedef hc_nvrtc_lib_t NVRTC_PTR; + +#endif // _EXT_NVRTC_H diff --git a/include/types.h b/include/types.h index f35eca3f7..0f0ecf1b4 100644 --- a/include/types.h +++ b/include/types.h @@ -989,6 +989,7 @@ typedef struct link_speed } link_speed_t; +#include "ext_nvrtc.h" #include "ext_cuda.h" #include "ext_OpenCL.h" @@ -1335,6 +1336,7 @@ typedef struct backend_ctx void *ocl; void *cuda; + void *nvrtc; cl_uint platforms_cnt; cl_platform_id *platforms; diff --git a/src/Makefile b/src/Makefile index 196ad6c89..3f90fafe0 100644 --- a/src/Makefile +++ b/src/Makefile @@ -280,7 +280,7 @@ EMU_OBJS_ALL += emu_inc_truecrypt_crc32 emu_inc_truecrypt_keyfile emu EMU_OBJS_ALL += emu_inc_hash_md4 emu_inc_hash_md5 emu_inc_hash_ripemd160 emu_inc_hash_sha1 emu_inc_hash_sha256 emu_inc_hash_sha384 emu_inc_hash_sha512 emu_inc_hash_streebog256 emu_inc_hash_streebog512 EMU_OBJS_ALL += emu_inc_cipher_aes emu_inc_cipher_camellia emu_inc_cipher_des emu_inc_cipher_kuznyechik emu_inc_cipher_serpent emu_inc_cipher_twofish -OBJS_ALL := affinity autotune backend benchmark bitmap bitops combinator common convert cpt cpu_crc32 debugfile dictstat dispatch dynloader event ext_ADL ext_cuda ext_nvapi ext_nvml ext_OpenCL ext_sysfs ext_lzma filehandling folder hashcat hashes hlfmt hwmon induct interface keyboard_layout locking logfile loopback memory monitor mpsp outfile_check outfile pidfile potfile restore rp rp_cpu selftest slow_candidates shared status stdout straight terminal thread timer tuningdb usage user_options wordlist $(EMU_OBJS_ALL) +OBJS_ALL := affinity autotune backend benchmark bitmap bitops combinator common convert cpt cpu_crc32 debugfile dictstat dispatch dynloader event ext_ADL ext_cuda ext_nvapi ext_nvml ext_nvrtc ext_OpenCL ext_sysfs ext_lzma filehandling folder hashcat hashes hlfmt hwmon induct interface keyboard_layout locking logfile loopback memory monitor mpsp outfile_check outfile pidfile potfile restore rp rp_cpu selftest slow_candidates shared status stdout straight terminal thread timer tuningdb usage user_options wordlist $(EMU_OBJS_ALL) ifeq ($(ENABLE_BRAIN),1) OBJS_ALL += brain diff --git a/src/backend.c b/src/backend.c index 41cf2b645..e7187c94d 100644 --- a/src/backend.c +++ b/src/backend.c @@ -543,6 +543,73 @@ void generate_cached_kernel_amp_filename (const u32 attack_kern, char *profile_d snprintf (cached_file, 255, "%s/kernels/amp_a%u.%s.kernel", profile_dir, attack_kern, device_name_chksum_amp_mp); } +int nvrtc_init (hashcat_ctx_t *hashcat_ctx) +{ + backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx; + + NVRTC_PTR *nvrtc = backend_ctx->nvrtc; + + memset (nvrtc, 0, sizeof (NVRTC_PTR)); + + #if defined (_WIN) + nvrtc->lib = hc_dlopen ("nvrtc"); + #elif defined (__APPLE__) + nvrtc->lib = hc_dlopen ("/System/Library/Frameworks/NVRTC.framework/NVRTC"); + #elif defined (__CYGWIN__) + nvrtc->lib = hc_dlopen ("nvrtc.dll"); + + if (nvrtc->lib == NULL) nvrtc->lib = hc_dlopen ("cygnvrtc-1.dll"); + #else + nvrtc->lib = hc_dlopen ("libnvrtc.so"); + + if (nvrtc->lib == NULL) nvrtc->lib = hc_dlopen ("libnvrtc.so.1"); + #endif + + if (nvrtc->lib == NULL) + { + event_log_error (hashcat_ctx, "Cannot find NVRTC library."); + + event_log_warning (hashcat_ctx, "You are probably missing the native CUDA SDK and/or driver for your platform."); + event_log_warning (hashcat_ctx, "NVIDIA GPUs require this runtime and/or driver:"); + event_log_warning (hashcat_ctx, " \"NVIDIA Driver\" (418.56 or later)"); + event_log_warning (hashcat_ctx, " \"CUDA Toolkit\" (10.1 or later)"); + event_log_warning (hashcat_ctx, NULL); + + return -1; + } + + HC_LOAD_FUNC (nvrtc, nvrtcAddNameExpression, NVRTC_NVRTCADDNAMEEXPRESSION, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcCompileProgram, NVRTC_NVRTCCOMPILEPROGRAM, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcCreateProgram, NVRTC_NVRTCCREATEPROGRAM, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcDestroyProgram, NVRTC_NVRTCDESTROYPROGRAM, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcGetLoweredName, NVRTC_NVRTCGETLOWEREDNAME, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcGetPTX, NVRTC_NVRTCGETPTX, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcGetPTXSize, NVRTC_NVRTCGETPTXSIZE, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcGetProgramLog, NVRTC_NVRTCGETPROGRAMLOG, NVRTC, 1); + HC_LOAD_FUNC (nvrtc, nvrtcGetProgramLogSize, NVRTC_NVRTCGETPROGRAMLOGSIZE, NVRTC, 1); + + return 0; +} + +void nvrtc_close (hashcat_ctx_t *hashcat_ctx) +{ + backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx; + + NVRTC_PTR *nvrtc = backend_ctx->nvrtc; + + if (nvrtc) + { + if (nvrtc->lib) + { + hc_dlclose (nvrtc->lib); + } + + hcfree (backend_ctx->nvrtc); + + backend_ctx->nvrtc = NULL; + } +} + int cuda_init (hashcat_ctx_t *hashcat_ctx) { backend_ctx_t *backend_ctx = hashcat_ctx->backend_ctx; @@ -572,6 +639,7 @@ int cuda_init (hashcat_ctx_t *hashcat_ctx) event_log_warning (hashcat_ctx, "You are probably missing the native CUDA runtime or driver for your platform."); event_log_warning (hashcat_ctx, "NVIDIA GPUs require this runtime and/or driver:"); event_log_warning (hashcat_ctx, " \"NVIDIA Driver\" (418.56 or later)"); + event_log_warning (hashcat_ctx, " \"CUDA Toolkit\" (10.1 or later)"); event_log_warning (hashcat_ctx, NULL); return -1; @@ -702,6 +770,7 @@ int ocl_init (hashcat_ctx_t *hashcat_ctx) event_log_warning (hashcat_ctx, "* NVIDIA GPUs require this runtime and/or driver:"); event_log_warning (hashcat_ctx, " \"NVIDIA Driver\" (418.56 or later)"); + event_log_warning (hashcat_ctx, " \"CUDA Toolkit\" (10.1 or later)"); event_log_warning (hashcat_ctx, NULL); return -1; @@ -3071,6 +3140,31 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx) cuda_close (hashcat_ctx); } + /** + * Load and map NVRTC library calls + */ + + NVRTC_PTR *nvrtc = (NVRTC_PTR *) hcmalloc (sizeof (NVRTC_PTR)); + + backend_ctx->nvrtc = nvrtc; + + const int rc_nvrtc_init = nvrtc_init (hashcat_ctx); + + if (rc_nvrtc_init == -1) + { + nvrtc_close (hashcat_ctx); + } + + /** + * Check if both CUDA and NVRTC were load successful + */ + + if ((rc_cuda_init == -1) || (rc_nvrtc_init == -1)) + { + cuda_close (hashcat_ctx); + nvrtc_close (hashcat_ctx); + } + /** * Load and map OpenCL library calls */ @@ -3086,6 +3180,7 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx) ocl_close (hashcat_ctx); } + /** * return if both CUDA and OpenCL initialization failed */ @@ -3199,6 +3294,7 @@ int backend_ctx_init (hashcat_ctx_t *hashcat_ctx) event_log_warning (hashcat_ctx, "* NVIDIA GPUs require this runtime and/or driver:"); event_log_warning (hashcat_ctx, " \"NVIDIA Driver\" (418.56 or later)"); + event_log_warning (hashcat_ctx, " \"CUDA Toolkit\" (10.1 or later)"); event_log_warning (hashcat_ctx, NULL); FREE_OPENCL_CTX_ON_ERROR; @@ -3300,7 +3396,8 @@ void backend_ctx_destroy (hashcat_ctx_t *hashcat_ctx) if (backend_ctx->enabled == false) return; - ocl_close (hashcat_ctx); + cuda_close (hashcat_ctx); + ocl_close (hashcat_ctx); hcfree (backend_ctx->devices_param); diff --git a/src/ext_nvrtc.c b/src/ext_nvrtc.c new file mode 100644 index 000000000..17e6ff03b --- /dev/null +++ b/src/ext_nvrtc.c @@ -0,0 +1,8 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#include "common.h" +#include "types.h" +#include "ext_nvrtc.h"