Merge pull request #379 from ntalpallikar/bug_matrixmulDynlinkJIT_crash

Fix null pointer reference issue with cuda driver API function pointer.
This commit is contained in:
Rob Armstrong 2025-09-05 09:29:17 -07:00 committed by GitHub
commit c94ff366ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 4 deletions

View File

@ -242,12 +242,23 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
if (*pInstance == NULL) { if (*pInstance == NULL) {
printf("LoadLibrary \"%s\" failed!\n", __CudaLibName); printf("LoadLibrary \"%s\" failed!\n", __CudaLibName);
return CUDA_ERROR_UNKNOWN; exit(EXIT_FAILURE);
} }
return CUDA_SUCCESS; return CUDA_SUCCESS;
} }
CUresult GET_DRIVER_HANDLE(CUDADRIVER *pInstance)
{
*pInstance = GetModuleHandle(__CudaLibName);
if (*pInstance) {
return CUDA_SUCCESS;
}
else {
return CUDA_ERROR_UNKNOWN;
}
}
#define GET_PROC_EX(name, alias, required) \ #define GET_PROC_EX(name, alias, required) \
alias = (t##name *)GetProcAddress(CudaDrvLib, #name); \ alias = (t##name *)GetProcAddress(CudaDrvLib, #name); \
if (alias == NULL && required) { \ if (alias == NULL && required) { \
@ -269,6 +280,13 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
return CUDA_ERROR_UNKNOWN; \ return CUDA_ERROR_UNKNOWN; \
} }
#define GET_PROC_ERROR_FUNCTIONS(name, alias, required) \
alias = (t##name *)GetProcAddress(CudaDrvLib, #name); \
if (alias == NULL && required) { \
printf("Failed to find error function \"%s\" in %s\n", #name, __CudaLibName); \
exit(EXIT_FAILURE); \
}
#elif defined(__unix__) || defined(__QNX__) || defined(__APPLE__) || defined(__MACOSX) #elif defined(__unix__) || defined(__QNX__) || defined(__APPLE__) || defined(__MACOSX)
#include <dlfcn.h> #include <dlfcn.h>
@ -293,12 +311,23 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
if (*pInstance == NULL) { if (*pInstance == NULL) {
printf("dlopen \"%s\" failed!\n", __CudaLibName); printf("dlopen \"%s\" failed!\n", __CudaLibName);
return CUDA_ERROR_UNKNOWN; exit(EXIT_FAILURE);
} }
return CUDA_SUCCESS; return CUDA_SUCCESS;
} }
CUresult GET_DRIVER_HANDLE(CUDADRIVER *pInstance)
{
*pInstance = dlopen(__CudaLibName, RTLD_LAZY);
if (*pInstance) {
return CUDA_SUCCESS;
}
else {
return CUDA_ERROR_UNKNOWN;
}
}
#define GET_PROC_EX(name, alias, required) \ #define GET_PROC_EX(name, alias, required) \
alias = (t##name *)dlsym(CudaDrvLib, #name); \ alias = (t##name *)dlsym(CudaDrvLib, #name); \
if (alias == NULL && required) { \ if (alias == NULL && required) { \
@ -320,6 +349,13 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
return CUDA_ERROR_UNKNOWN; \ return CUDA_ERROR_UNKNOWN; \
} }
#define GET_PROC_ERROR_FUNCTIONS(name, alias, required) \
alias = (t##name *)dlsym(CudaDrvLib, #name); \
if (alias == NULL && required) { \
printf("Failed to find error function \"%s\" in %s\n", #name, __CudaLibName); \
exit(EXIT_FAILURE); \
}
#else #else
#error unsupported platform #error unsupported platform
#endif #endif
@ -338,11 +374,19 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
#define GET_PROC_V2(name) GET_PROC_EX_V2(name, name, 1) #define GET_PROC_V2(name) GET_PROC_EX_V2(name, name, 1)
#define GET_PROC_V3(name) GET_PROC_EX_V3(name, name, 1) #define GET_PROC_V3(name) GET_PROC_EX_V3(name, name, 1)
CUresult INIT_ERROR_FUNCTIONS(void)
{
CUDADRIVER CudaDrvLib;
CUresult result = CUDA_SUCCESS;
result = GET_DRIVER_HANDLE(&CudaDrvLib);
GET_PROC_ERROR_FUNCTIONS(cuGetErrorString, cuGetErrorString, 1);
return result;
}
CUresult CUDAAPI cuInit(unsigned int Flags, int cudaVersion) CUresult CUDAAPI cuInit(unsigned int Flags, int cudaVersion)
{ {
CUDADRIVER CudaDrvLib; CUDADRIVER CudaDrvLib;
int driverVer = 1000; int driverVer = 1000;
CHECKED_CALL(LOAD_LIBRARY(&CudaDrvLib)); CHECKED_CALL(LOAD_LIBRARY(&CudaDrvLib));
// cuInit is required; alias it to _cuInit // cuInit is required; alias it to _cuInit
@ -619,6 +663,5 @@ CUresult CUDAAPI cuInit(unsigned int Flags, int cudaVersion)
GET_PROC(cuGraphicsD3D9RegisterResource); GET_PROC(cuGraphicsD3D9RegisterResource);
#endif #endif
} }
return CUDA_SUCCESS; return CUDA_SUCCESS;
} }

View File

@ -42,11 +42,21 @@ inline int ftoi(float value) { return (value >= 0 ? static_cast<int>(value + 0.5
#ifndef checkCudaErrors #ifndef checkCudaErrors
#define checkCudaErrors(err) __checkCudaErrors(err, __FILE__, __LINE__) #define checkCudaErrors(err) __checkCudaErrors(err, __FILE__, __LINE__)
extern "C" CUresult INIT_ERROR_FUNCTIONS(void);
// These are the inline versions for all of the SDK helper functions // These are the inline versions for all of the SDK helper functions
inline void __checkCudaErrors(CUresult err, const char *file, const int line) inline void __checkCudaErrors(CUresult err, const char *file, const int line)
{ {
if (CUDA_SUCCESS != err) { if (CUDA_SUCCESS != err) {
const char *errorStr = NULL; const char *errorStr = NULL;
if (!cuGetErrorString) {
CUresult result = INIT_ERROR_FUNCTIONS();
if (result != CUDA_SUCCESS) {
printf("CUDA driver API failed");
exit(EXIT_FAILURE);
}
}
cuGetErrorString(err, &errorStr); cuGetErrorString(err, &errorStr);
fprintf(stderr, fprintf(stderr,
"checkCudaErrors() Driver API error = %04d \"%s\" from file <%s>, " "checkCudaErrors() Driver API error = %04d \"%s\" from file <%s>, "