Skip to content

Commit c94ff36

Browse files
authored
Merge pull request #379 from ntalpallikar/bug_matrixmulDynlinkJIT_crash
Fix null pointer reference issue with cuda driver API function pointer.
2 parents 3f1c509 + f6504f4 commit c94ff36

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

Samples/0_Introduction/matrixMulDynlinkJIT/cuda_drvapi_dynlink.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,23 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
242242

243243
if (*pInstance == NULL) {
244244
printf("LoadLibrary \"%s\" failed!\n", __CudaLibName);
245-
return CUDA_ERROR_UNKNOWN;
245+
exit(EXIT_FAILURE);
246246
}
247247

248248
return CUDA_SUCCESS;
249249
}
250250

251+
CUresult GET_DRIVER_HANDLE(CUDADRIVER *pInstance)
252+
{
253+
*pInstance = GetModuleHandle(__CudaLibName);
254+
if (*pInstance) {
255+
return CUDA_SUCCESS;
256+
}
257+
else {
258+
return CUDA_ERROR_UNKNOWN;
259+
}
260+
}
261+
251262
#define GET_PROC_EX(name, alias, required) \
252263
alias = (t##name *)GetProcAddress(CudaDrvLib, #name); \
253264
if (alias == NULL && required) { \
@@ -269,6 +280,13 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
269280
return CUDA_ERROR_UNKNOWN; \
270281
}
271282

283+
#define GET_PROC_ERROR_FUNCTIONS(name, alias, required) \
284+
alias = (t##name *)GetProcAddress(CudaDrvLib, #name); \
285+
if (alias == NULL && required) { \
286+
printf("Failed to find error function \"%s\" in %s\n", #name, __CudaLibName); \
287+
exit(EXIT_FAILURE); \
288+
}
289+
272290
#elif defined(__unix__) || defined(__QNX__) || defined(__APPLE__) || defined(__MACOSX)
273291

274292
#include <dlfcn.h>
@@ -293,12 +311,23 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
293311

294312
if (*pInstance == NULL) {
295313
printf("dlopen \"%s\" failed!\n", __CudaLibName);
296-
return CUDA_ERROR_UNKNOWN;
314+
exit(EXIT_FAILURE);
297315
}
298316

299317
return CUDA_SUCCESS;
300318
}
301319

320+
CUresult GET_DRIVER_HANDLE(CUDADRIVER *pInstance)
321+
{
322+
*pInstance = dlopen(__CudaLibName, RTLD_LAZY);
323+
if (*pInstance) {
324+
return CUDA_SUCCESS;
325+
}
326+
else {
327+
return CUDA_ERROR_UNKNOWN;
328+
}
329+
}
330+
302331
#define GET_PROC_EX(name, alias, required) \
303332
alias = (t##name *)dlsym(CudaDrvLib, #name); \
304333
if (alias == NULL && required) { \
@@ -320,6 +349,13 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
320349
return CUDA_ERROR_UNKNOWN; \
321350
}
322351

352+
#define GET_PROC_ERROR_FUNCTIONS(name, alias, required) \
353+
alias = (t##name *)dlsym(CudaDrvLib, #name); \
354+
if (alias == NULL && required) { \
355+
printf("Failed to find error function \"%s\" in %s\n", #name, __CudaLibName); \
356+
exit(EXIT_FAILURE); \
357+
}
358+
323359
#else
324360
#error unsupported platform
325361
#endif
@@ -338,11 +374,19 @@ static CUresult LOAD_LIBRARY(CUDADRIVER *pInstance)
338374
#define GET_PROC_V2(name) GET_PROC_EX_V2(name, name, 1)
339375
#define GET_PROC_V3(name) GET_PROC_EX_V3(name, name, 1)
340376

377+
CUresult INIT_ERROR_FUNCTIONS(void)
378+
{
379+
CUDADRIVER CudaDrvLib;
380+
CUresult result = CUDA_SUCCESS;
381+
result = GET_DRIVER_HANDLE(&CudaDrvLib);
382+
GET_PROC_ERROR_FUNCTIONS(cuGetErrorString, cuGetErrorString, 1);
383+
return result;
384+
}
385+
341386
CUresult CUDAAPI cuInit(unsigned int Flags, int cudaVersion)
342387
{
343388
CUDADRIVER CudaDrvLib;
344389
int driverVer = 1000;
345-
346390
CHECKED_CALL(LOAD_LIBRARY(&CudaDrvLib));
347391

348392
// cuInit is required; alias it to _cuInit
@@ -619,6 +663,5 @@ CUresult CUDAAPI cuInit(unsigned int Flags, int cudaVersion)
619663
GET_PROC(cuGraphicsD3D9RegisterResource);
620664
#endif
621665
}
622-
623666
return CUDA_SUCCESS;
624667
}

Samples/0_Introduction/matrixMulDynlinkJIT/helper_cuda_drvapi.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@ inline int ftoi(float value) { return (value >= 0 ? static_cast<int>(value + 0.5
4242
#ifndef checkCudaErrors
4343
#define checkCudaErrors(err) __checkCudaErrors(err, __FILE__, __LINE__)
4444

45+
extern "C" CUresult INIT_ERROR_FUNCTIONS(void);
46+
4547
// These are the inline versions for all of the SDK helper functions
4648
inline void __checkCudaErrors(CUresult err, const char *file, const int line)
4749
{
4850
if (CUDA_SUCCESS != err) {
4951
const char *errorStr = NULL;
52+
53+
if (!cuGetErrorString) {
54+
CUresult result = INIT_ERROR_FUNCTIONS();
55+
if (result != CUDA_SUCCESS) {
56+
printf("CUDA driver API failed");
57+
exit(EXIT_FAILURE);
58+
}
59+
}
5060
cuGetErrorString(err, &errorStr);
5161
fprintf(stderr,
5262
"checkCudaErrors() Driver API error = %04d \"%s\" from file <%s>, "

0 commit comments

Comments
 (0)