diff --git a/source/adapters/cuda/adapter.cpp b/source/adapters/cuda/adapter.cpp index c8949cd9a8..dba88e1ee7 100644 --- a/source/adapters/cuda/adapter.cpp +++ b/source/adapters/cuda/adapter.cpp @@ -91,7 +91,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) { UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetLastError( ur_adapter_handle_t, const char **ppMessage, int32_t *pError) { - std::ignore = pError; + *pError = ErrorAdapterNativeCode; *ppMessage = ErrorMessage; return ErrorMessageCode; } diff --git a/source/adapters/cuda/common.cpp b/source/adapters/cuda/common.cpp index f2b6dae841..3a2f3a2efb 100644 --- a/source/adapters/cuda/common.cpp +++ b/source/adapters/cuda/common.cpp @@ -10,10 +10,12 @@ #include "common.hpp" #include "logger/ur_logger.hpp" +#include "ur_api.h" #include #include +#include ur_result_t mapErrorUR(CUresult Result) { switch (Result) { @@ -105,6 +107,7 @@ void detail::ur::assertion(bool Condition, const char *Message) { // Global variables for ZER_EXT_RESULT_ADAPTER_SPECIFIC_ERROR thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS; thread_local char ErrorMessage[MaxMessageSize]; +thread_local int32_t ErrorAdapterNativeCode = 0; // Utility function for setting a message and warning [[maybe_unused]] void setErrorMessage(const char *pMessage, @@ -114,16 +117,24 @@ thread_local char ErrorMessage[MaxMessageSize]; ErrorMessageCode = ErrorCode; } -void setPluginSpecificMessage(CUresult cu_res) { +[[maybe_unused]] void setAdapterSpecificMessage(CUresult cu_res) { + ErrorAdapterNativeCode = static_cast(cu_res); + // according to the documentation of the cuGetErrorName and cuGetErrorString + // CUDA driver APIs, both error_name and error_string are null-terminated. const char *error_string; const char *error_name; cuGetErrorName(cu_res, &error_name); cuGetErrorString(cu_res, &error_string); - char *message = (char *)malloc(strlen(error_string) + strlen(error_name) + 2); - strcpy(message, error_name); - strcat(message, "\n"); - strcat(message, error_string); + static constexpr char new_line[] = "\n"; + // non-null-terminated sizes + const size_t error_string_size = std::strlen(error_string); + const size_t error_name_size = std::strlen(error_name); + char *message = reinterpret_cast( + std::malloc(error_string_size + error_name_size + sizeof(new_line))); + std::strcpy(message, error_name); + std::strcat(message, new_line); + std::strncat(message, error_string, error_string_size); setErrorMessage(message, UR_RESULT_ERROR_ADAPTER_SPECIFIC); - free(message); + std::free(message); } diff --git a/source/adapters/cuda/common.hpp b/source/adapters/cuda/common.hpp index 67223c45bc..5ee9d774a5 100644 --- a/source/adapters/cuda/common.hpp +++ b/source/adapters/cuda/common.hpp @@ -35,12 +35,13 @@ std::string getCudaVersionString(); constexpr size_t MaxMessageSize = 256; extern thread_local ur_result_t ErrorMessageCode; extern thread_local char ErrorMessage[MaxMessageSize]; +extern thread_local int32_t ErrorAdapterNativeCode; // Utility function for setting a message and warning [[maybe_unused]] void setErrorMessage(const char *pMessage, ur_result_t ErrorCode); -void setPluginSpecificMessage(CUresult cu_res); +void setAdapterSpecificMessage(CUresult cu_res); /// ------ Error handling, matching OpenCL plugin semantics. namespace detail {