Skip to content

Commit 69a471e

Browse files
committed
Improve ASan implementations of hsa api calls based on @bing-ma
suggestions. This commit again adds the interception of 'hsa_amd_pointer_info' call.
1 parent 339e178 commit 69a471e

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,6 +1400,9 @@ DECLARE_REAL(hsa_status_t, hsa_amd_ipc_memory_detach, void *mapped_ptr)
14001400
DECLARE_REAL(hsa_status_t, hsa_amd_vmem_address_reserve_align, void** ptr,
14011401
size_t size, uint64_t address, uint64_t alignment, uint64_t flags)
14021402
DECLARE_REAL(hsa_status_t, hsa_amd_vmem_address_free, void* ptr, size_t size)
1403+
DECLARE_REAL(hsa_status_t, hsa_amd_pointer_info, const void* ptr,
1404+
hsa_amd_pointer_info_t* info, void* (*alloc)(size_t),
1405+
uint32_t* num_agents_accessible, hsa_agent_t** accessible)
14031406

14041407
namespace __asan {
14051408

@@ -1454,21 +1457,18 @@ static struct AP32<LocalAddressSpaceView> AP_;
14541457

14551458
hsa_status_t asan_hsa_amd_ipc_memory_create(void* ptr, size_t len,
14561459
hsa_amd_ipc_memory_t* handle) {
1457-
static_assert(AP_.kMetadataSize == 0, "Expression below requires this");
14581460
void* ptr_ = get_allocator().GetBlockBegin(ptr);
1459-
size_t len_ = get_allocator().GetActuallyAllocatedSize(ptr);
1460-
1461-
uptr p = reinterpret_cast<uptr>(ptr);
1462-
uptr p_ = reinterpret_cast<uptr>(ptr_);
1463-
1464-
if (p == p_)
1461+
AsanChunk* m = ptr_
1462+
? instance.GetAsanChunkByAddr(reinterpret_cast<uptr>(ptr_))
1463+
: nullptr;
1464+
if (ptr_ && m) {
1465+
static_assert(AP_.kMetadataSize == 0, "Expression below requires this");
1466+
uptr p = reinterpret_cast<uptr>(ptr);
1467+
uptr p_ = reinterpret_cast<uptr>(ptr_);
1468+
size_t len_ = len;
1469+
if (p == p_ + kPageSize_ && len == m->UsedSize())
1470+
len_ = get_allocator().GetActuallyAllocatedSize(ptr);
14651471
return REAL(hsa_amd_ipc_memory_create)(ptr_, len_, handle);
1466-
1467-
if (p == p_ + kPageSize_) {
1468-
AsanChunk* m = instance.GetAsanChunkByAddr(p_);
1469-
if (m && len == m->UsedSize())
1470-
return REAL(hsa_amd_ipc_memory_create)(ptr_, len_, handle);
1471-
return REAL(hsa_amd_ipc_memory_create)(ptr_, len, handle);
14721472
}
14731473
return REAL(hsa_amd_ipc_memory_create)(ptr, len, handle);
14741474
}
@@ -1547,5 +1547,28 @@ hsa_status_t asan_hsa_amd_vmem_address_free(void* ptr, size_t size,
15471547
}
15481548
return REAL(hsa_amd_vmem_address_free)(ptr, size);
15491549
}
1550+
1551+
hsa_status_t asan_hsa_amd_pointer_info(const void* ptr,
1552+
hsa_amd_pointer_info_t* info,
1553+
void* (*alloc)(size_t),
1554+
uint32_t* num_agents_accessible,
1555+
hsa_agent_t** accessible) {
1556+
void* p = get_allocator().GetBlockBegin(ptr);
1557+
AsanChunk* m = instance.GetAsanChunkByAddr(reinterpret_cast<uptr>(p));
1558+
hsa_status_t status;
1559+
if (p && m)
1560+
status = REAL(hsa_amd_pointer_info)(ptr, info, alloc, num_agents_accessible,
1561+
accessible);
1562+
if (status == HSA_STATUS_SUCCESS && info && p && m) {
1563+
static_assert(AP_.kMetadataSize == 0, "Expression below requires this");
1564+
info->agentBaseAddress = reinterpret_cast<void*>(
1565+
reinterpret_cast<uptr>(info->agentBaseAddress) + kPageSize_);
1566+
info->hostBaseAddress = reinterpret_cast<void*>(
1567+
reinterpret_cast<uptr>(info->hostBaseAddress) + kPageSize_);
1568+
info->sizeInBytes = m->UsedSize();
1569+
}
1570+
return status;
1571+
}
1572+
15501573
} // namespace __asan
15511574
#endif

compiler-rt/lib/asan/asan_allocator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ hsa_status_t asan_hsa_amd_vmem_address_reserve_align(void** ptr, size_t size,
341341
BufferedStackTrace* stack);
342342
hsa_status_t asan_hsa_amd_vmem_address_free(void* ptr, size_t size,
343343
BufferedStackTrace* stack);
344+
hsa_status_t asan_hsa_amd_pointer_info(const void* ptr,
345+
hsa_amd_pointer_info_t* info,
346+
void* (*alloc)(size_t),
347+
uint32_t* num_agents_accessible,
348+
hsa_agent_t** accessible);
344349
} // namespace __asan
345350
#endif
346351

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,15 @@ INTERCEPTOR(hsa_status_t, hsa_amd_vmem_address_free, void* ptr, size_t size) {
948948
return asan_hsa_amd_vmem_address_free(ptr, size, &stack);
949949
}
950950

951+
INTERCEPTOR(hsa_status_t, hsa_amd_pointer_info, const void* ptr,
952+
hsa_amd_pointer_info_t* info, void* (*alloc)(size_t),
953+
uint32_t* num_agents_accessible, hsa_agent_t** accessible) {
954+
AsanInitFromRtl();
955+
ENSURE_HSA_INITED();
956+
return asan_hsa_amd_pointer_info(ptr, info, alloc, num_agents_accessible,
957+
accessible);
958+
}
959+
951960
void InitializeAmdgpuInterceptors() {
952961
ASAN_INTERCEPT_FUNC(hsa_memory_copy);
953962
ASAN_INTERCEPT_FUNC(hsa_amd_memory_pool_allocate);
@@ -962,6 +971,7 @@ void InitializeAmdgpuInterceptors() {
962971
ASAN_INTERCEPT_FUNC(hsa_amd_ipc_memory_detach);
963972
ASAN_INTERCEPT_FUNC(hsa_amd_vmem_address_reserve_align);
964973
ASAN_INTERCEPT_FUNC(hsa_amd_vmem_address_free);
974+
ASAN_INTERCEPT_FUNC(hsa_amd_pointer_info);
965975
}
966976

967977
void ENSURE_HSA_INITED() {

0 commit comments

Comments
 (0)