Skip to content

Commit 19aba45

Browse files
committed
Fix segfault in cu_memory_provider_get_last_native_error()
Fix segfault in cu_memory_provider_get_last_native_error() when it is called after a CUDA device was destroyed. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 7727ad1 commit 19aba45

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/provider/provider_cuda.c

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -539,22 +539,41 @@ static void cu_memory_provider_get_last_native_error(void *provider,
539539
return;
540540
}
541541

542-
const char *error_name = 0;
543-
const char *error_string = 0;
544-
g_cu_ops.cuGetErrorName(TLS_last_native_error.native_error, &error_name);
545-
g_cu_ops.cuGetErrorString(TLS_last_native_error.native_error,
546-
&error_string);
547-
542+
CUresult result;
548543
size_t buf_size = 0;
549-
strncpy(TLS_last_native_error.msg_buff, error_name, TLS_MSG_BUF_LEN - 1);
550-
buf_size = strlen(TLS_last_native_error.msg_buff);
544+
const char *error_name = NULL;
545+
const char *error_string = NULL;
546+
547+
// If the error code is not recognized,
548+
// CUDA_ERROR_INVALID_VALUE will be returned
549+
// and error_name will be set to the NULL address.
550+
result = g_cu_ops.cuGetErrorName(TLS_last_native_error.native_error,
551+
&error_name);
552+
if (result == CUDA_SUCCESS && error_name != NULL) {
553+
strncpy(TLS_last_native_error.msg_buff, error_name,
554+
TLS_MSG_BUF_LEN - 1);
555+
} else {
556+
strncpy(TLS_last_native_error.msg_buff, "cuGetErrorName() failed",
557+
TLS_MSG_BUF_LEN - 1);
558+
}
551559

560+
buf_size = strlen(TLS_last_native_error.msg_buff);
552561
strncat(TLS_last_native_error.msg_buff, " - ",
553562
TLS_MSG_BUF_LEN - buf_size - 1);
554563
buf_size = strlen(TLS_last_native_error.msg_buff);
555564

556-
strncat(TLS_last_native_error.msg_buff, error_string,
557-
TLS_MSG_BUF_LEN - buf_size - 1);
565+
// If the error code is not recognized,
566+
// CUDA_ERROR_INVALID_VALUE will be returned
567+
// and error_string will be set to the NULL address.
568+
result = g_cu_ops.cuGetErrorString(TLS_last_native_error.native_error,
569+
&error_string);
570+
if (result == CUDA_SUCCESS && error_string != NULL) {
571+
strncat(TLS_last_native_error.msg_buff, error_string,
572+
TLS_MSG_BUF_LEN - buf_size - 1);
573+
} else {
574+
strncat(TLS_last_native_error.msg_buff, "cuGetErrorString() failed",
575+
TLS_MSG_BUF_LEN - buf_size - 1);
576+
}
558577

559578
*pError = TLS_last_native_error.native_error;
560579
*ppMessage = TLS_last_native_error.msg_buff;

0 commit comments

Comments
 (0)