From 17d23cbd78a1353dc84ea7a53516933424d572cc Mon Sep 17 00:00:00 2001 From: Kent Russell Date: Mon, 16 Sep 2024 12:15:56 -0400 Subject: [PATCH] rocrtst: Various codeql fixes Fix some potentially unreleased memory, null value checks, files not closed, and other such issues reported by codeql Change-Id: Ia679aff97a773a642d8c8cbadeae30955554a62e Signed-off-by: Kent Russell [ROCm/ROCR-Runtime commit: d64e33520f9cea34d9c17507233e527147316a52] --- .../rocrtst/samples/binary_search/binary_search.cc | 2 +- .../suites/functional/deallocation_notifier.cc | 2 +- .../rocrtst/suites/functional/memory_access.cc | 2 ++ .../rocrtst/suites/functional/signal_concurrent.cc | 14 ++++++++++++++ .../rocrtst/suites/functional/virtual_memory.cc | 5 +++++ .../rocrtst/suites/performance/dispatch_time.cc | 2 ++ .../rocrtst/suites/performance/enqueueLatency.cc | 2 ++ 7 files changed, 27 insertions(+), 2 deletions(-) diff --git a/projects/rocr-runtime/rocrtst/samples/binary_search/binary_search.cc b/projects/rocr-runtime/rocrtst/samples/binary_search/binary_search.cc index f84e3fd5b2..08aa3a9d76 100755 --- a/projects/rocr-runtime/rocrtst/samples/binary_search/binary_search.cc +++ b/projects/rocr-runtime/rocrtst/samples/binary_search/binary_search.cc @@ -393,8 +393,8 @@ hsa_status_t LoadKernelFromObjFile(BinarySearch* bs) { } err = hsa_code_object_reader_create_from_file(file_handle, &code_obj_rdr); - RET_IF_HSA_ERR(err); close(file_handle); + RET_IF_HSA_ERR(err); err = hsa_executable_create_alt(HSA_PROFILE_FULL, HSA_DEFAULT_FLOAT_ROUNDING_MODE_DEFAULT, NULL, &executable); diff --git a/projects/rocr-runtime/rocrtst/suites/functional/deallocation_notifier.cc b/projects/rocr-runtime/rocrtst/suites/functional/deallocation_notifier.cc index d245c42392..6a7ab22e96 100644 --- a/projects/rocr-runtime/rocrtst/suites/functional/deallocation_notifier.cc +++ b/projects/rocr-runtime/rocrtst/suites/functional/deallocation_notifier.cc @@ -177,8 +177,8 @@ void DeallocationNotifierTest::TestDeallocationNotifier(void) { // Attempt register on bad address (ie one not known to ROCr). Should fail. ptr = malloc(4096); status = hsa_amd_register_deallocation_callback(ptr, call, (void*)0xDEADBEEF); - ASSERT_EQ(HSA_STATUS_ERROR_INVALID_ALLOCATION, status) << "Register deallocation callback error."; free(ptr); + ASSERT_EQ(HSA_STATUS_ERROR_INVALID_ALLOCATION, status) << "Register deallocation callback error."; // Allocate, register and free. Callback should complete before free returns. status = hsa_amd_memory_pool_allocate(pool, 4096, 0, &ptr); diff --git a/projects/rocr-runtime/rocrtst/suites/functional/memory_access.cc b/projects/rocr-runtime/rocrtst/suites/functional/memory_access.cc index 6abe0dad95..9ebb36b26f 100755 --- a/projects/rocr-runtime/rocrtst/suites/functional/memory_access.cc +++ b/projects/rocr-runtime/rocrtst/suites/functional/memory_access.cc @@ -404,6 +404,8 @@ void MemoryAccessTest::CPUAccessToGPUMemoryTest(hsa_agent_t cpuAgent, unsigned int *sys_data; sys_data = (unsigned int*)malloc(max_alloc_size); + ASSERT_NE(sys_data, nullptr); + for (unsigned int i = 0; i < max_element; ++i) { sys_data[i] = i; } diff --git a/projects/rocr-runtime/rocrtst/suites/functional/signal_concurrent.cc b/projects/rocr-runtime/rocrtst/suites/functional/signal_concurrent.cc index 68dfe7148d..4997067a31 100644 --- a/projects/rocr-runtime/rocrtst/suites/functional/signal_concurrent.cc +++ b/projects/rocr-runtime/rocrtst/suites/functional/signal_concurrent.cc @@ -191,9 +191,17 @@ void SignalConcurrentTest::TestSignalCreateConcurrent(void) { hsa_status_t status; signals = reinterpret_cast(malloc(sizeof(hsa_signal_t) * N * M)); + ASSERT_NE(signals, nullptr); + struct rocrtst::test_group* tg_sg_create = rocrtst::TestGroupCreate(N); int* offset = reinterpret_cast(malloc(sizeof(int) * N)); + EXPECT_NE(offset, nullptr); + if (!offset) { + free(signals); + return; + } + for (i = 0; i < N; ++i) { offset[i] = i * M; rocrtst::TestGroupAdd(tg_sg_create, &TestSignalCreateFunction, offset + i, 1); @@ -269,9 +277,15 @@ void SignalConcurrentTest::TestSignalDestroyConcurrent(void) { signals = reinterpret_cast(malloc(sizeof(hsa_signal_t) * N * M)); + ASSERT_NE(signals, nullptr); + struct rocrtst::test_group *tg_sg_destroy = rocrtst::TestGroupCreate(N); int *offset = reinterpret_cast(malloc(sizeof(int) * N)); + EXPECT_NE(offset, nullptr); + if (!offset) + return; + for (i = 0; i < N; ++i) { int j; offset[i] = i * M; diff --git a/projects/rocr-runtime/rocrtst/suites/functional/virtual_memory.cc b/projects/rocr-runtime/rocrtst/suites/functional/virtual_memory.cc index b77a04cd7e..f37310c6cd 100644 --- a/projects/rocr-runtime/rocrtst/suites/functional/virtual_memory.cc +++ b/projects/rocr-runtime/rocrtst/suites/functional/virtual_memory.cc @@ -467,6 +467,8 @@ void VirtMemoryTestBasic::CPUAccessToGPUMemoryTest(hsa_agent_t cpuAgent, hsa_age unsigned int* host_data = NULL; host_data = (unsigned int*)malloc(max_alloc_size); + ASSERT_NE(host_data, nullptr); + for (unsigned int i = 0; i < max_element; ++i) { host_data[i] = i; } @@ -477,6 +479,9 @@ void VirtMemoryTestBasic::CPUAccessToGPUMemoryTest(hsa_agent_t cpuAgent, hsa_age hsa_amd_vmem_alloc_handle_t mem_handle_host, mem_handle_dev; ASSERT_SUCCESS( hsa_amd_vmem_address_reserve(reinterpret_cast(&dev_data), max_alloc_size, 0, 0)); + + ASSERT_NE(dev_data, nullptr); + ASSERT_SUCCESS(hsa_amd_vmem_handle_create(device_pool, max_alloc_size, MEMORY_TYPE_NONE, 0, &mem_handle_dev)); ASSERT_SUCCESS( diff --git a/projects/rocr-runtime/rocrtst/suites/performance/dispatch_time.cc b/projects/rocr-runtime/rocrtst/suites/performance/dispatch_time.cc index bfccb5ec27..950c16ae99 100755 --- a/projects/rocr-runtime/rocrtst/suites/performance/dispatch_time.cc +++ b/projects/rocr-runtime/rocrtst/suites/performance/dispatch_time.cc @@ -255,6 +255,8 @@ void DispatchTime::RunMulti() { uint64_t* index = reinterpret_cast(malloc(sizeof(uint64_t) * num_batch_)); + ASSERT_NE(index, nullptr); + hsa_signal_store_screlease(aql().completion_signal, num_batch_); for (uint32_t j = 0; j < num_batch_; j++) { diff --git a/projects/rocr-runtime/rocrtst/suites/performance/enqueueLatency.cc b/projects/rocr-runtime/rocrtst/suites/performance/enqueueLatency.cc index a2b2e3834e..b19a03ecde 100755 --- a/projects/rocr-runtime/rocrtst/suites/performance/enqueueLatency.cc +++ b/projects/rocr-runtime/rocrtst/suites/performance/enqueueLatency.cc @@ -262,6 +262,8 @@ void EnqueueLatency::EnqueueMultiPackets() { uint64_t* index = reinterpret_cast(malloc(sizeof(uint64_t) * num_of_pkts_)); + ASSERT_NE(index, nullptr); + hsa_signal_store_screlease(aql().completion_signal, num_of_pkts_); for (uint32_t j = 0; j < num_of_pkts_; j++) {