From f31afe1d20294d6ebcb5453f6a3972de05161e81 Mon Sep 17 00:00:00 2001 From: JonathanLichtnerAMD <195780826+JonathanLichtnerAMD@users.noreply.github.com> Date: Fri, 19 Sep 2025 12:53:41 -0600 Subject: [PATCH] [HIP CLR] Make hipMemPtrGetInfo consistent with malloc and hipMalloc (#1005) hipMemPtrGetInfo was returning the error hipErrorInvalidValue if it was called on a nullptr. However, this does not match the malloc convention where a nullptr has size zero; for example, malloc_usable_size() returns zero if called on a nullptr. This commit changes hipMemPtrGetInfo to set the size to zero and return hipSuccess when called with a nullptr. (This also fits with hipMalloc and hipFree usage, since hipMalloc of size zero results in a nullptr, and hipFree of a nullptr is successful.) --- projects/clr/hipamd/src/hip_memory.cpp | 7 ++++++- .../catch/unit/memory/hipMemPtrGetInfo.cc | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/projects/clr/hipamd/src/hip_memory.cpp b/projects/clr/hipamd/src/hip_memory.cpp index 0cfd93f77a..45244291ec 100644 --- a/projects/clr/hipamd/src/hip_memory.cpp +++ b/projects/clr/hipamd/src/hip_memory.cpp @@ -1,4 +1,4 @@ -/* Copyright (c) 2015 - 2024 Advanced Micro Devices, Inc. +/* Copyright (c) 2015 - 2025 Advanced Micro Devices, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -829,6 +829,11 @@ hipError_t hipMemcpyWithStream(void* dst, const void* src, size_t sizeBytes, hip hipError_t hipMemPtrGetInfo(void* ptr, size_t* size) { HIP_INIT_API(hipMemPtrGetInfo, ptr, size); + if (ptr == nullptr) { + *size = 0; + HIP_RETURN(hipSuccess); + } + size_t offset = 0; amd::Memory* svmMem = getMemoryObject(ptr, offset); diff --git a/projects/hip-tests/catch/unit/memory/hipMemPtrGetInfo.cc b/projects/hip-tests/catch/unit/memory/hipMemPtrGetInfo.cc index c09cdd3d73..5f9a574b35 100644 --- a/projects/hip-tests/catch/unit/memory/hipMemPtrGetInfo.cc +++ b/projects/hip-tests/catch/unit/memory/hipMemPtrGetInfo.cc @@ -1,5 +1,5 @@ /* -Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved. +Copyright (c) 2021 - 2025 Advanced Micro Devices, Inc. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -56,3 +56,17 @@ TEST_CASE("Unit_hipMemPtrGetInfo_Basic") { HIP_CHECK(hipFree(fPtr)); HIP_CHECK(hipFree(sPtr)); } + +/* +This testcase verifies the scenario of +hipMemPtrGetInfo API being called on a zero-sized allocation. +*/ +TEST_CASE("Unit_hipMemPtrGetInfo_SizeZeroAllocation") { + int* iPtr; + size_t sSetSize = 0, sGetSize; + HIP_CHECK(hipMalloc(&iPtr, sSetSize)); + HIP_CHECK(hipMemPtrGetInfo(iPtr, &sGetSize)); + REQUIRE(sGetSize == sSetSize); + + HIP_CHECK(hipFree(iPtr)); +}