From 7e20e8ec13d43e5bf78e2471e6abd5d6fcd8163d Mon Sep 17 00:00:00 2001 From: marandje Date: Fri, 24 Oct 2025 16:27:48 +0200 Subject: [PATCH] SWDEV-548500 - Resolve memory leaks in memory tests (#1093) --- .../catch/unit/memory/hipDrvPtrGetAttributes.cc | 17 +++++++++++------ projects/hip-tests/catch/unit/memory/hipFree.cc | 1 + .../hip-tests/catch/unit/memory/hipFreeHost.cc | 2 +- .../hip-tests/catch/unit/memory/hipHostAlloc.cc | 8 ++++---- .../hip-tests/catch/unit/memory/hipHostFree.cc | 9 +++++---- .../catch/unit/memory/hipHostMalloc.cc | 5 +++++ .../catch/unit/memory/hipMemAdvise_old.cc | 3 +++ .../catch/unit/memory/hipMemPoolCreate.cc | 1 + .../unit/memory/hipMemRangeGetAttributes_old.cc | 8 +------- .../catch/unit/memory/hipMemcpyFromSymbol.cc | 4 ++++ .../catch/unit/memory/hipPointerGetAttribute.cc | 4 ++-- .../catch/unit/memory/hipPtrGetAttribute.cc | 4 ++++ 12 files changed, 42 insertions(+), 24 deletions(-) diff --git a/projects/hip-tests/catch/unit/memory/hipDrvPtrGetAttributes.cc b/projects/hip-tests/catch/unit/memory/hipDrvPtrGetAttributes.cc index dea43bbe72..0c3740731b 100644 --- a/projects/hip-tests/catch/unit/memory/hipDrvPtrGetAttributes.cc +++ b/projects/hip-tests/catch/unit/memory/hipDrvPtrGetAttributes.cc @@ -49,10 +49,8 @@ TEST_CASE("Unit_hipDrvPtrGetAttributes_Negative") { int numDevices = 0; HIP_CHECK(hipGetDeviceCount(&numDevices)); int* A_d; - int* A_Pinned_h; HIP_CHECK(hipMalloc(&A_d, Nbytes)); - HIP_CHECK(hipHostMalloc(reinterpret_cast(&A_Pinned_h), Nbytes, hipHostMallocDefault)); HIP_CHECK(hipGetDevice(&deviceId)); unsigned int device_ordinal; int* dev_ptr{nullptr}; @@ -94,6 +92,8 @@ TEST_CASE("Unit_hipDrvPtrGetAttributes_Negative") { hipErrorInvalidValue); } #endif + + HIP_CHECK(hipFree(A_d)); } // Testcase verifies functional scenarios of hipDrvPointerGetAttributes API @@ -104,10 +104,8 @@ TEST_CASE("Unit_hipDrvPtrGetAttributes_Functional") { int numDevices = 0; HIP_CHECK(hipGetDeviceCount(&numDevices)); int* A_d; - int* A_Pinned_h; HIP_CHECK(hipMalloc(&A_d, Nbytes)); - HIP_CHECK(hipHostMalloc(reinterpret_cast(&A_Pinned_h), Nbytes, hipHostMallocDefault)); HIP_CHECK(hipGetDevice(&deviceId)); SECTION("Passing device attributes to device pointer") { @@ -165,13 +163,20 @@ TEST_CASE("Unit_hipDrvPtrGetAttributes_Functional") { int device_ordinal; void* data[2]; int* host_ptr; + int* pinned_host_ptr; data[0] = (&host_ptr); data[1] = (&device_ordinal); + + HIP_CHECK(hipHostMalloc(reinterpret_cast(&pinned_host_ptr), Nbytes, hipHostMallocDefault)); + hipPointer_attribute attributes[] = {HIP_POINTER_ATTRIBUTE_HOST_POINTER, HIP_POINTER_ATTRIBUTE_DEVICE_ORDINAL}; HIP_CHECK(hipDrvPointerGetAttributes(2, attributes, data, - reinterpret_cast(A_Pinned_h))); - REQUIRE(host_ptr == A_Pinned_h); + reinterpret_cast(pinned_host_ptr))); + REQUIRE(host_ptr == pinned_host_ptr); REQUIRE(device_ordinal == deviceId); + HIP_CHECK(hipFreeHost(pinned_host_ptr)); } + + HIP_CHECK(hipFree(A_d)); } diff --git a/projects/hip-tests/catch/unit/memory/hipFree.cc b/projects/hip-tests/catch/unit/memory/hipFree.cc index 10a3c6b542..4992767fad 100644 --- a/projects/hip-tests/catch/unit/memory/hipFree.cc +++ b/projects/hip-tests/catch/unit/memory/hipFree.cc @@ -176,6 +176,7 @@ TEST_CASE("Unit_hipFreeNegativeHost") { auto flag = GENERATE(hipHostRegisterDefault, hipHostRegisterPortable, hipHostRegisterMapped); HIP_CHECK(hipHostRegister((void*)hostPtr, sizeof(char), flag)); HIP_CHECK_ERROR(hipHostFree(hostPtr), hipErrorInvalidValue); + HIP_CHECK(hipHostUnregister(hostPtr)); delete hostPtr; } #if (HT_AMD == 1) && (HT_LINUX == 1) diff --git a/projects/hip-tests/catch/unit/memory/hipFreeHost.cc b/projects/hip-tests/catch/unit/memory/hipFreeHost.cc index 91cad1b613..d12f17472e 100644 --- a/projects/hip-tests/catch/unit/memory/hipFreeHost.cc +++ b/projects/hip-tests/catch/unit/memory/hipFreeHost.cc @@ -96,7 +96,7 @@ TEST_CASE("Unit_hipFreeHost_Multithreading") { std::vector ptrs(10); size_t ptr_size = 1024; - for (auto ptr : ptrs) { + for (auto& ptr : ptrs) { HIP_CHECK(hipHostMalloc(&ptr, ptr_size)); } diff --git a/projects/hip-tests/catch/unit/memory/hipHostAlloc.cc b/projects/hip-tests/catch/unit/memory/hipHostAlloc.cc index 893444db19..55d53f3d08 100644 --- a/projects/hip-tests/catch/unit/memory/hipHostAlloc.cc +++ b/projects/hip-tests/catch/unit/memory/hipHostAlloc.cc @@ -240,10 +240,10 @@ TEST_CASE("Unit_hipHostAlloc_Basic") { TEST_CASE("Unit_hipHostAlloc_Default") { int* A = nullptr; HIP_CHECK(hipHostAlloc(reinterpret_cast(&A), SIZEBYTES, hipHostMallocDefault)); - const char* ptrType = "default"; - CheckHostPointer(NUMELEMENTS, A, 0, SYNC_DEVICE, ptrType); - CheckHostPointer(NUMELEMENTS, A, 0, SYNC_STREAM, ptrType); - CheckHostPointer(NUMELEMENTS, A, 0, SYNC_EVENT, ptrType); + std::string kPtrType{"default"}; + CheckHostPointer(NUMELEMENTS, A, 0, SYNC_DEVICE, kPtrType); + CheckHostPointer(NUMELEMENTS, A, 0, SYNC_STREAM, kPtrType); + CheckHostPointer(NUMELEMENTS, A, 0, SYNC_EVENT, kPtrType); HIP_CHECK(hipHostFree(A)); } diff --git a/projects/hip-tests/catch/unit/memory/hipHostFree.cc b/projects/hip-tests/catch/unit/memory/hipHostFree.cc index dec64b9a8e..c918c2fc32 100644 --- a/projects/hip-tests/catch/unit/memory/hipHostFree.cc +++ b/projects/hip-tests/catch/unit/memory/hipHostFree.cc @@ -42,12 +42,13 @@ TEST_CASE("Unit_hipHostFree_InvalidMemory") { } SECTION("Host registered memory") { - const size_t ptr_size = 1024; - char* ptr = new char[ptr_size]; + constexpr size_t kPtrSize = 1024; + auto ptr = std::make_unique(kPtrSize); auto flag = GENERATE(hipHostRegisterDefault, hipHostRegisterPortable, hipHostRegisterMapped); - HIP_CHECK(hipHostRegister(ptr, ptr_size, flag)); - HIP_CHECK_ERROR(hipHostFree(ptr), hipErrorInvalidValue); + HIP_CHECK(hipHostRegister(ptr.get(), kPtrSize, flag)); + HIP_CHECK_ERROR(hipHostFree(ptr.get()), hipErrorInvalidValue); + HIP_CHECK(hipHostUnregister(ptr.get())); } #if (HT_AMD == 1) && (HT_LINUX == 1) diff --git a/projects/hip-tests/catch/unit/memory/hipHostMalloc.cc b/projects/hip-tests/catch/unit/memory/hipHostMalloc.cc index 386e97e535..8b9e9318d6 100644 --- a/projects/hip-tests/catch/unit/memory/hipHostMalloc.cc +++ b/projects/hip-tests/catch/unit/memory/hipHostMalloc.cc @@ -156,6 +156,7 @@ TEST_CASE("Unit_hipHostMalloc_Basic") { HIP_CHECK(hipHostFree(A_h)); HIP_CHECK(hipHostFree(B_h)); HIP_CHECK(hipHostFree(C_h)); + HIP_CHECK(hipFree(B_d)); } } /* @@ -188,6 +189,7 @@ TEST_CASE("Unit_hipHostMalloc_NonCoherent") { CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_DEVICE, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_STREAM, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_EVENT, ptrType); + HIP_CHECK(hipFreeHost(A)); } /* @@ -209,6 +211,8 @@ TEST_CASE("Unit_hipHostMalloc_Coherent") { CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_DEVICE, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_STREAM, ptrType); CheckHostPointer(numElements, A, hipEventReleaseToSystem, SYNC_EVENT, ptrType); + + HIP_CHECK(hipFreeHost(A)); } else { SUCCEED("Coherence memory allocation failed. Is SVM atomic supported?"); } @@ -229,6 +233,7 @@ TEST_CASE("Unit_hipHostMalloc_Default") { CheckHostPointer(numElements, A, 0, SYNC_DEVICE, ptrType); CheckHostPointer(numElements, A, 0, SYNC_STREAM, ptrType); CheckHostPointer(numElements, A, 0, SYNC_EVENT, ptrType); + HIP_CHECK(hipFreeHost(A)); } TEST_CASE("Unit_hipHostGetDevicePointer_NullCheck") { diff --git a/projects/hip-tests/catch/unit/memory/hipMemAdvise_old.cc b/projects/hip-tests/catch/unit/memory/hipMemAdvise_old.cc index 9b5361489f..ece1ad9598 100644 --- a/projects/hip-tests/catch/unit/memory/hipMemAdvise_old.cc +++ b/projects/hip-tests/catch/unit/memory/hipMemAdvise_old.cc @@ -493,6 +493,7 @@ TEST_CASE("Unit_hipMemAdvise_TstAccessedByFlg") { HIP_CHECK(hipMemAdvise(Hmm, 2 * 4096, hipMemAdviseSetAccessedBy, 0)); HIP_CHECK(hipMemRangeGetAttribute(&data, sizeof(int), hipMemRangeAttributeLastPrefetchLocation, Hmm, 2 * 4096)); + HIP_CHECK(hipFree(Hmm)); if (data != -2) { WARN("Didnt get expected value!!\n"); REQUIRE(false); @@ -899,6 +900,8 @@ TEST_CASE("Unit_hipMemAdvise_TstSetUnsetPrfrdLoc") { WARN("Didnt receive expected value!!"); REQUIRE(false); } + + HIP_CHECK(hipFree(Hmm)); } else { SUCCEED( "GPU 0 doesn't support hipDeviceAttributeManagedMemory " diff --git a/projects/hip-tests/catch/unit/memory/hipMemPoolCreate.cc b/projects/hip-tests/catch/unit/memory/hipMemPoolCreate.cc index b835b9b8b3..5b309990a9 100644 --- a/projects/hip-tests/catch/unit/memory/hipMemPoolCreate.cc +++ b/projects/hip-tests/catch/unit/memory/hipMemPoolCreate.cc @@ -114,6 +114,7 @@ TEST_CASE("Unit_hipMemPoolCreate_With_maxSize") { HIP_CHECK( hipMallocFromPoolAsync(reinterpret_cast(&B), 1024 * 1024 * 513, mem_pool, stream)); #endif + HIP_CHECK(hipFreeAsync(A, stream)); HIP_CHECK(hipMemPoolDestroy(mem_pool)); HIP_CHECK(hipStreamDestroy(stream)); } diff --git a/projects/hip-tests/catch/unit/memory/hipMemRangeGetAttributes_old.cc b/projects/hip-tests/catch/unit/memory/hipMemRangeGetAttributes_old.cc index 501df6c39c..214060c696 100644 --- a/projects/hip-tests/catch/unit/memory/hipMemRangeGetAttributes_old.cc +++ b/projects/hip-tests/catch/unit/memory/hipMemRangeGetAttributes_old.cc @@ -291,13 +291,7 @@ TEST_CASE("Unit_hipMemRangeGetAttributes_NegativeTst") { } REQUIRE(IfTestPassed); - // The following scenarios have been removed considering the nature of the - // api. With Consultation with Maneesh Gupta, the following scenarios - // have been removed. - // passing numAttributes as 4 while the attributes array has only 2 members - // passing numAttributes as 10 while the attributes array has only 2 members - // length of the list of dataSizes less than the number of - // attributes being probed + HIP_CHECK(hipFree(Hmm)); } else { SUCCEED( "GPU 0 doesn't support hipDeviceAttributeManagedMemory " diff --git a/projects/hip-tests/catch/unit/memory/hipMemcpyFromSymbol.cc b/projects/hip-tests/catch/unit/memory/hipMemcpyFromSymbol.cc index 5320880ed0..88a30bbfee 100644 --- a/projects/hip-tests/catch/unit/memory/hipMemcpyFromSymbol.cc +++ b/projects/hip-tests/catch/unit/memory/hipMemcpyFromSymbol.cc @@ -209,6 +209,10 @@ TEST_CASE("Unit_hipMemcpyToFromSymbol_SyncAndAsync") { } } } + + if (streamType == StreamTestType::CreatedStream) { + HIP_CHECK(hipStreamDestroy(stream)); + } } /** diff --git a/projects/hip-tests/catch/unit/memory/hipPointerGetAttribute.cc b/projects/hip-tests/catch/unit/memory/hipPointerGetAttribute.cc index 8c1031ff3c..1e8336a25f 100644 --- a/projects/hip-tests/catch/unit/memory/hipPointerGetAttribute.cc +++ b/projects/hip-tests/catch/unit/memory/hipPointerGetAttribute.cc @@ -189,6 +189,7 @@ TEST_CASE("Unit_hipPointerGetAttribute_BufferID") { HIP_CHECK(hipPointerGetAttribute(&bufid2, HIP_POINTER_ATTRIBUTE_BUFFER_ID, reinterpret_cast(A_d))); REQUIRE(bufid1 != bufid2); + HIP_CHECK(hipFree(A_d)); } /* Allocate host memory and get the device ordinal by calling @@ -240,6 +241,7 @@ TEST_CASE("Unit_hipPointerGetAttribute_MappedMem") { REQUIRE(mallocManaged == 1); HIP_CHECK(hipFree(A_d)); HIP_CHECK(hipHostFree(ptr1)); + HIP_CHECK(hipFree(ptr2)); free(A_h); } @@ -276,8 +278,6 @@ TEST_CASE("Unit_hipPointerGetAttribute_Negative") { reinterpret_cast(B_d)) == hipErrorInvalidValue); } SECTION("Get Start address of host pointer") { - char* A_h; - A_h = reinterpret_cast(malloc(Nbytes)); REQUIRE(hipPointerGetAttribute(&data, HIP_POINTER_ATTRIBUTE_RANGE_START_ADDR, reinterpret_cast(A_h)) == hipErrorInvalidValue); } diff --git a/projects/hip-tests/catch/unit/memory/hipPtrGetAttribute.cc b/projects/hip-tests/catch/unit/memory/hipPtrGetAttribute.cc index 0bff78a0dc..3ab24f8ec4 100644 --- a/projects/hip-tests/catch/unit/memory/hipPtrGetAttribute.cc +++ b/projects/hip-tests/catch/unit/memory/hipPtrGetAttribute.cc @@ -147,4 +147,8 @@ TEST_CASE("Unit_hipPtrGetAttribute_Simple") { HIP_CHECK(hipPointerGetAttribute(&bufId2, HIP_POINTER_ATTRIBUTE_BUFFER_ID, reinterpret_cast(A_Pinned_h))); REQUIRE(bufId1 != bufId2); + + HIP_CHECK(hipFree(A_d)); + HIP_CHECK(hipHostFree(A_Pinned_h)); + HIP_CHECK(hipFree(A_Hmm)); }