diff --git a/source/adapters/cuda/common.cpp b/source/adapters/cuda/common.cpp index f2b6dae841..89500d1a1c 100644 --- a/source/adapters/cuda/common.cpp +++ b/source/adapters/cuda/common.cpp @@ -104,13 +104,15 @@ 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 char ErrorMessage[MaxMessageSize]{}; // Utility function for setting a message and warning [[maybe_unused]] void setErrorMessage(const char *pMessage, ur_result_t ErrorCode) { - assert(strlen(pMessage) <= MaxMessageSize); - strcpy(ErrorMessage, pMessage); + assert(strlen(pMessage) < MaxMessageSize); + // Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is + // always null terminated. + strncpy(ErrorMessage, pMessage, MaxMessageSize - 1); ErrorMessageCode = ErrorCode; } diff --git a/source/adapters/hip/common.cpp b/source/adapters/hip/common.cpp index da8cc2c765..23ef1d3301 100644 --- a/source/adapters/hip/common.cpp +++ b/source/adapters/hip/common.cpp @@ -164,12 +164,14 @@ void detail::ur::assertion(bool Condition, const char *pMessage) { // Global variables for UR_RESULT_ADAPTER_SPECIFIC_ERROR thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS; -thread_local char ErrorMessage[MaxMessageSize]; +thread_local char ErrorMessage[MaxMessageSize]{}; // Utility function for setting a message and warning [[maybe_unused]] void setErrorMessage(const char *pMessage, ur_result_t ErrorCode) { assert(strlen(pMessage) < MaxMessageSize); + // Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is + // always null terminated. strncpy(ErrorMessage, pMessage, MaxMessageSize - 1); ErrorMessageCode = ErrorCode; } diff --git a/source/adapters/level_zero/common.cpp b/source/adapters/level_zero/common.cpp index f5d8b20014..371ed78aeb 100644 --- a/source/adapters/level_zero/common.cpp +++ b/source/adapters/level_zero/common.cpp @@ -332,15 +332,17 @@ template <> zes_structure_type_t getZesStructureType() { // 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 char ErrorMessage[MaxMessageSize]{}; thread_local int32_t ErrorAdapterNativeCode; // Utility function for setting a message and warning [[maybe_unused]] void setErrorMessage(const char *pMessage, ur_result_t ErrorCode, int32_t AdapterErrorCode) { - assert(strlen(pMessage) <= MaxMessageSize); - strcpy(ErrorMessage, pMessage); + assert(strlen(pMessage) < MaxMessageSize); + // Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is + // always null terminated. + strncpy(ErrorMessage, pMessage, MaxMessageSize - 1); ErrorMessageCode = ErrorCode; ErrorAdapterNativeCode = AdapterErrorCode; } diff --git a/source/adapters/native_cpu/common.cpp b/source/adapters/native_cpu/common.cpp index b956fc8c7a..ab7c7a07ea 100644 --- a/source/adapters/native_cpu/common.cpp +++ b/source/adapters/native_cpu/common.cpp @@ -13,13 +13,15 @@ // Global variables for UR_RESULT_ADAPTER_SPECIFIC_ERROR // See urGetLastResult thread_local ur_result_t ErrorMessageCode = UR_RESULT_SUCCESS; -thread_local char ErrorMessage[MaxMessageSize]; +thread_local char ErrorMessage[MaxMessageSize]{}; // Utility function for setting a message and warning [[maybe_unused]] void setErrorMessage(const char *pMessage, ur_result_t ErrorCode) { - assert(strlen(pMessage) <= MaxMessageSize); - strcpy(ErrorMessage, pMessage); + assert(strlen(pMessage) < MaxMessageSize); + // Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is + // always null terminated. + strncpy(ErrorMessage, pMessage, MaxMessageSize - 1); ErrorMessageCode = ErrorCode; } diff --git a/source/adapters/opencl/common.cpp b/source/adapters/opencl/common.cpp index d6e934c68b..33da43a182 100644 --- a/source/adapters/opencl/common.cpp +++ b/source/adapters/opencl/common.cpp @@ -14,11 +14,14 @@ namespace cl_adapter { /* Global variables for urAdapterGetLastError() */ thread_local int32_t ErrorMessageCode = 0; -thread_local char ErrorMessage[MaxMessageSize]; +thread_local char ErrorMessage[MaxMessageSize]{}; [[maybe_unused]] void setErrorMessage(const char *Message, int32_t ErrorCode) { - assert(strlen(Message) <= cl_adapter::MaxMessageSize); - strcpy(cl_adapter::ErrorMessage, Message); + assert(strlen(Message) < cl_adapter::MaxMessageSize); + // Copy at most MaxMessageSize - 1 bytes to ensure the resultant string is + // always null terminated. + strncpy(cl_adapter::ErrorMessage, Message, MaxMessageSize - 1); + ErrorMessageCode = ErrorCode; } } // namespace cl_adapter diff --git a/source/loader/layers/sanitizer/asan/asan_report.cpp b/source/loader/layers/sanitizer/asan/asan_report.cpp index fe7c1e0f87..6300e63e2b 100644 --- a/source/loader/layers/sanitizer/asan/asan_report.cpp +++ b/source/loader/layers/sanitizer/asan/asan_report.cpp @@ -47,11 +47,10 @@ void ReportBadFree(uptr Addr, const StackTrace &stack, if (!AI) { getContext()->logger.always("{} may be allocated on Host Memory", (void *)Addr); + } else { + assert(!AI->IsReleased && "Chunk must be not released"); + PrintAllocateInfo(Addr, AI.get()); } - - assert(AI && !AI->IsReleased && "Chunk must be not released"); - - PrintAllocateInfo(Addr, AI.get()); } void ReportBadContext(uptr Addr, const StackTrace &stack, diff --git a/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp index 357eff4b77..65b0e318dc 100644 --- a/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp +++ b/source/loader/layers/sanitizer/sanitizer_common/sanitizer_stacktrace.cpp @@ -31,7 +31,7 @@ bool Contains(const std::string &s, const char *p) { // Parse back trace information in the following formats: // ([function_name]+function_offset) [offset] -void ParseBacktraceInfo(BacktraceInfo BI, std::string &ModuleName, +void ParseBacktraceInfo(const BacktraceInfo &BI, std::string &ModuleName, uptr &Offset) { // Parse module name size_t End = BI.find_first_of('('); diff --git a/test/conformance/source/environment.cpp b/test/conformance/source/environment.cpp index 006ea09b8b..cc64ab11ea 100644 --- a/test/conformance/source/environment.cpp +++ b/test/conformance/source/environment.cpp @@ -222,7 +222,7 @@ void uur::PlatformEnvironment::selectPlatformFromOptions() { std::stringstream errstr; errstr << "Multiple possible platforms found; please select one of the " "following or set --platforms_count=1:\n"; - for (auto p : platforms_filtered) { + for (const auto &p : platforms_filtered) { errstr << " --backend=" << backend_to_str(p.backend) << " --platform=\"" << p.name << "\"\n"; } @@ -287,7 +287,7 @@ PlatformEnvironment::parsePlatformOptions(int argc, char **argv) { } else if (std::strncmp(arg, "--backend=", sizeof("--backend=") - 1) == 0) { std::string backend_string{&arg[std::strlen("--backend=")]}; - if (!parse_backend(backend_string)) { + if (!parse_backend(std::move(backend_string))) { return options; } } else if (std::strncmp(arg, "--platforms_count=", diff --git a/test/conformance/usm/urUSMFree.cpp b/test/conformance/usm/urUSMFree.cpp index f5502c89a6..a670c26f4b 100644 --- a/test/conformance/usm/urUSMFree.cpp +++ b/test/conformance/usm/urUSMFree.cpp @@ -19,16 +19,16 @@ TEST_P(urUSMFreeTest, SuccessDeviceAlloc) { size_t allocation_size = sizeof(int); ASSERT_SUCCESS(urUSMDeviceAlloc(context, device, nullptr, nullptr, allocation_size, &ptr)); + ASSERT_NE(ptr, nullptr); ur_event_handle_t event = nullptr; uint8_t pattern = 0; - ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, + EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, allocation_size, 0, nullptr, &event)); EXPECT_SUCCESS(urQueueFlush(queue)); - ASSERT_SUCCESS(urEventWait(1, &event)); + EXPECT_SUCCESS(urEventWait(1, &event)); - ASSERT_NE(ptr, nullptr); ASSERT_SUCCESS(urUSMFree(context, ptr)); ASSERT_SUCCESS(urEventRelease(event)); } @@ -43,15 +43,15 @@ TEST_P(urUSMFreeTest, SuccessHostAlloc) { size_t allocation_size = sizeof(int); ASSERT_SUCCESS( urUSMHostAlloc(context, nullptr, nullptr, allocation_size, &ptr)); + ASSERT_NE(ptr, nullptr); ur_event_handle_t event = nullptr; uint8_t pattern = 0; - ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, + EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, allocation_size, 0, nullptr, &event)); EXPECT_SUCCESS(urQueueFlush(queue)); - ASSERT_SUCCESS(urEventWait(1, &event)); + EXPECT_SUCCESS(urEventWait(1, &event)); - ASSERT_NE(ptr, nullptr); ASSERT_SUCCESS(urUSMFree(context, ptr)); ASSERT_SUCCESS(urEventRelease(event)); } @@ -73,15 +73,15 @@ TEST_P(urUSMFreeTest, SuccessSharedAlloc) { size_t allocation_size = sizeof(int); ASSERT_SUCCESS(urUSMSharedAlloc(context, device, nullptr, nullptr, allocation_size, &ptr)); + ASSERT_NE(ptr, nullptr); ur_event_handle_t event = nullptr; uint8_t pattern = 0; - ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, + EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, allocation_size, 0, nullptr, &event)); EXPECT_SUCCESS(urQueueFlush(queue)); - ASSERT_SUCCESS(urEventWait(1, &event)); + EXPECT_SUCCESS(urEventWait(1, &event)); - ASSERT_NE(ptr, nullptr); ASSERT_SUCCESS(urUSMFree(context, ptr)); ASSERT_SUCCESS(urEventRelease(event)); } diff --git a/test/conformance/usm/urUSMSharedAlloc.cpp b/test/conformance/usm/urUSMSharedAlloc.cpp index e543602fbc..021fa6368e 100644 --- a/test/conformance/usm/urUSMSharedAlloc.cpp +++ b/test/conformance/usm/urUSMSharedAlloc.cpp @@ -44,6 +44,7 @@ struct urUSMSharedAllocTest ur_usm_pool_handle_t pool = nullptr; bool usePool = std::get<0>(getParam()).value; + void *ptr = nullptr; }; // The 0 value parameters are not relevant for urUSMSharedAllocTest tests, they @@ -57,7 +58,6 @@ UUR_TEST_SUITE_P( uur::printUSMAllocTestString); TEST_P(urUSMSharedAllocTest, Success) { - void *ptr = nullptr; size_t allocation_size = sizeof(int); ASSERT_SUCCESS(urUSMSharedAlloc(context, device, nullptr, pool, allocation_size, &ptr)); @@ -65,9 +65,9 @@ TEST_P(urUSMSharedAllocTest, Success) { ur_event_handle_t event = nullptr; uint8_t pattern = 0; - ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, + EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, allocation_size, 0, nullptr, &event)); - ASSERT_SUCCESS(urEventWait(1, &event)); + EXPECT_SUCCESS(urEventWait(1, &event)); ASSERT_SUCCESS(urUSMFree(context, ptr)); EXPECT_SUCCESS(urEventRelease(event)); @@ -85,7 +85,6 @@ TEST_P(urUSMSharedAllocTest, SuccessWithDescriptors) { ur_usm_desc_t usm_desc{UR_STRUCTURE_TYPE_USM_DESC, &usm_host_desc, /* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT, /* alignment */ 0}; - void *ptr = nullptr; size_t allocation_size = sizeof(int); ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool, allocation_size, &ptr)); @@ -93,9 +92,9 @@ TEST_P(urUSMSharedAllocTest, SuccessWithDescriptors) { ur_event_handle_t event = nullptr; uint8_t pattern = 0; - ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, + EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, allocation_size, 0, nullptr, &event)); - ASSERT_SUCCESS(urEventWait(1, &event)); + EXPECT_SUCCESS(urEventWait(1, &event)); ASSERT_SUCCESS(urUSMFree(context, ptr)); EXPECT_SUCCESS(urEventRelease(event)); @@ -107,7 +106,6 @@ TEST_P(urUSMSharedAllocTest, SuccessWithMultipleAdvices) { /* mem advice flags */ UR_USM_ADVICE_FLAG_SET_READ_MOSTLY | UR_USM_ADVICE_FLAG_BIAS_CACHED, /* alignment */ 0}; - void *ptr = nullptr; size_t allocation_size = sizeof(int); ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool, allocation_size, &ptr)); @@ -115,23 +113,21 @@ TEST_P(urUSMSharedAllocTest, SuccessWithMultipleAdvices) { ur_event_handle_t event = nullptr; uint8_t pattern = 0; - ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, + EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, allocation_size, 0, nullptr, &event)); - ASSERT_SUCCESS(urEventWait(1, &event)); + EXPECT_SUCCESS(urEventWait(1, &event)); ASSERT_SUCCESS(urUSMFree(context, ptr)); EXPECT_SUCCESS(urEventRelease(event)); } TEST_P(urUSMSharedAllocTest, InvalidNullHandleContext) { - void *ptr = nullptr; ASSERT_EQ_RESULT( UR_RESULT_ERROR_INVALID_NULL_HANDLE, urUSMSharedAlloc(nullptr, device, nullptr, pool, sizeof(int), &ptr)); } TEST_P(urUSMSharedAllocTest, InvalidNullHandleDevice) { - void *ptr = nullptr; ASSERT_EQ_RESULT( UR_RESULT_ERROR_INVALID_NULL_HANDLE, urUSMSharedAlloc(context, nullptr, nullptr, pool, sizeof(int), &ptr)); @@ -144,14 +140,12 @@ TEST_P(urUSMSharedAllocTest, InvalidNullPtrMem) { } TEST_P(urUSMSharedAllocTest, InvalidUSMSize) { - void *ptr = nullptr; ASSERT_EQ_RESULT( UR_RESULT_ERROR_INVALID_USM_SIZE, urUSMSharedAlloc(context, device, nullptr, pool, -1, &ptr)); } TEST_P(urUSMSharedAllocTest, InvalidValueAlignPowerOfTwo) { - void *ptr = nullptr; ur_usm_desc_t desc = {}; desc.stype = UR_STRUCTURE_TYPE_USM_DESC; desc.align = 5; @@ -185,16 +179,15 @@ TEST_P(urUSMSharedAllocAlignmentTest, SuccessAlignedAllocations) { /* mem advice flags */ UR_USM_ADVICE_FLAG_DEFAULT, alignment}; - void *ptr = nullptr; ASSERT_SUCCESS(urUSMSharedAlloc(context, device, &usm_desc, pool, allocation_size, &ptr)); ASSERT_NE(ptr, nullptr); ur_event_handle_t event = nullptr; uint8_t pattern = 0; - ASSERT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, + EXPECT_SUCCESS(urEnqueueUSMFill(queue, ptr, sizeof(pattern), &pattern, allocation_size, 0, nullptr, &event)); - ASSERT_SUCCESS(urEventWait(1, &event)); + EXPECT_SUCCESS(urEventWait(1, &event)); ASSERT_SUCCESS(urUSMFree(context, ptr)); EXPECT_SUCCESS(urEventRelease(event));