From 8b9275bb367dcf770a887c6c7149296253e073aa Mon Sep 17 00:00:00 2001 From: David Yat Sin Date: Thu, 13 Oct 2022 23:09:03 +0000 Subject: [PATCH] Early return for invalid pointer queries If a user queries the pointer info on an invalid pointer, hsaKmtQueryPointerInfo will return error or unknown pointer. The other fields in HsaPointerInfo are invalid, so we do not return them to the user. Also removing the assert and returning unknown pointer instead. As the assert will not trigger in release builds. hsaKmtQueryPointerInfo may also return unknown pointer for userptrs as they are not always tracked by thunk. Adjusting code to still treat these pointers as valid in this case. Change-Id: Idf5cd8b61cd532d31b072f449839d223369bb138 [ROCm/ROCR-Runtime commit: 18547173e9b33bb1b579e531de7523b1fb487971] --- .../runtime/hsa-runtime/core/runtime/amd_memory_region.cpp | 5 ++++- .../runtime/hsa-runtime/core/runtime/runtime.cpp | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_memory_region.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_memory_region.cpp index a27e90eada..d97d0e5856 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_memory_region.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/amd_memory_region.cpp @@ -536,10 +536,13 @@ hsa_status_t MemoryRegion::AllowAccess(uint32_t num_agents, info.size = sizeof(info); ScopedAcquire lock(&access_lock_); + if (core::Runtime::runtime_singleton_->PtrInfo(const_cast(ptr), &info, malloc, &agent_count, &accessible, &blockInfo) == HSA_STATUS_SUCCESS) { - if (blockInfo.length != size || info.sizeInBytes != size) { + /* Thunk may return type = HSA_EXT_POINTER_TYPE_UNKNOWN for userptrs */ + if (info.type != HSA_EXT_POINTER_TYPE_UNKNOWN && + (blockInfo.length != size || info.sizeInBytes != size)) { for (int i = 0; i < num_agents; i++) union_agents.push_back(agents[i].handle); for (int i = 0; i < agent_count; i++) union_agents.push_back(accessible[i].handle); std::sort(union_agents.begin(), union_agents.end()); diff --git a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp index e85a224883..a4cb90280f 100644 --- a/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp +++ b/projects/rocr-runtime/runtime/hsa-runtime/core/runtime/runtime.cpp @@ -777,8 +777,11 @@ hsa_status_t Runtime::PtrInfo(const void* ptr, hsa_amd_pointer_info_t* info, voi // We don't care if this returns an error code. // The type will be HSA_EXT_POINTER_TYPE_UNKNOWN if so. auto err = hsaKmtQueryPointerInfo(ptr, &thunkInfo); - assert(((err == HSAKMT_STATUS_SUCCESS) || (thunkInfo.Type == HSA_POINTER_UNKNOWN)) && - "Thunk ptr info error and not type HSA_POINTER_UNKNOWN."); + if (err != HSAKMT_STATUS_SUCCESS || thunkInfo.Type == HSA_POINTER_UNKNOWN) { + memset(info, 0, sizeof(*info)); + info->type = HSA_EXT_POINTER_TYPE_UNKNOWN; + return HSA_STATUS_SUCCESS; + } if (returnListData) { assert(thunkInfo.NMappedNodes <= agents_by_node_.size() &&