Skip to content

Commit d82664f

Browse files
authored
[SWDEV-563823][Compiler-rt][ASan] Simplify API Logic 'asan_hsa_amd_ipc_memory_create'. (#475)
- Use reinterpret_cast<uptr> for pointer arithmetic. - Add sanitizer interception logic for api 'hsa_amd_pointer_info'. - Allow only valid values of ptr and len in non-ASan mode. - ptr == Actual agentBaseAddress && len == original_len_used_in_alloc - Allow only valid values of ptr and len in ASan mode. - Here pinfo is retrieved from external hsa_amd_pointer_info(not internal device allocator function AmdgpuMemFuncs::GetPointerInfo) - ptr == pinfo.agentBaseAddress && len == pinfo.sizeInBytes - ptr == original_ptr_returned_by_ASAN && len == original_len_used_in_alloc
1 parent aa47a98 commit d82664f

File tree

3 files changed

+64
-11
lines changed

3 files changed

+64
-11
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,7 +1399,10 @@ DECLARE_REAL(hsa_status_t, hsa_amd_ipc_memory_attach,
13991399
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)
1402-
DECLARE_REAL(hsa_status_t, hsa_amd_vmem_address_free, void* ptr, size_t size);
1402+
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

@@ -1452,18 +1455,22 @@ static struct AP64<LocalAddressSpaceView> AP_;
14521455
static struct AP32<LocalAddressSpaceView> AP_;
14531456
#endif
14541457

1455-
hsa_status_t asan_hsa_amd_ipc_memory_create(void *ptr, size_t len,
1456-
hsa_amd_ipc_memory_t * handle) {
1457-
void *ptr_;
1458-
size_t len_ = get_allocator().GetActuallyAllocatedSize(ptr);
1459-
if (len_) {
1458+
hsa_status_t asan_hsa_amd_ipc_memory_create(void* ptr, size_t len,
1459+
hsa_amd_ipc_memory_t* handle) {
1460+
void* ptr_ = get_allocator().GetBlockBegin(ptr);
1461+
AsanChunk* m = ptr_
1462+
? instance.GetAsanChunkByAddr(reinterpret_cast<uptr>(ptr_))
1463+
: nullptr;
1464+
if (ptr_ && m) {
14601465
static_assert(AP_.kMetadataSize == 0, "Expression below requires this");
1461-
ptr_ = reinterpret_cast<void *>(reinterpret_cast<uptr>(ptr) - kPageSize_);
1462-
} else {
1463-
ptr_ = ptr;
1464-
len_ = len;
1466+
uptr p = reinterpret_cast<uptr>(ptr);
1467+
uptr p_ = reinterpret_cast<uptr>(ptr_);
1468+
if (p == p_ + kPageSize_ && len == m->UsedSize()) {
1469+
size_t len_ = get_allocator().GetActuallyAllocatedSize(ptr_);
1470+
return REAL(hsa_amd_ipc_memory_create)(ptr_, len_, handle);
1471+
}
14651472
}
1466-
return REAL(hsa_amd_ipc_memory_create)(ptr_, len_, handle);
1473+
return REAL(hsa_amd_ipc_memory_create)(ptr, len, handle);
14671474
}
14681475

14691476
hsa_status_t asan_hsa_amd_ipc_memory_attach(const hsa_amd_ipc_memory_t *handle,
@@ -1540,5 +1547,36 @@ hsa_status_t asan_hsa_amd_vmem_address_free(void* ptr, size_t size,
15401547
}
15411548
return REAL(hsa_amd_vmem_address_free)(ptr, size);
15421549
}
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* ptr_ = get_allocator().GetBlockBegin(ptr);
1557+
AsanChunk* m = ptr_
1558+
? instance.GetAsanChunkByAddr(reinterpret_cast<uptr>(ptr_))
1559+
: nullptr;
1560+
if (ptr_ && m) {
1561+
hsa_status_t status = REAL(hsa_amd_pointer_info)(
1562+
ptr_, info, alloc, num_agents_accessible, accessible);
1563+
if (status == HSA_STATUS_SUCCESS && info) {
1564+
static_assert(AP_.kMetadataSize == 0, "Expression below requires this");
1565+
// Adjust base address of agent,host and sizeInBytes so as to return
1566+
// the actual pointer information of user allocation rather than asan
1567+
// allocation. Asan allocation pointer info can be acquired using internal
1568+
// 'GetPointerInfo'
1569+
info->agentBaseAddress = reinterpret_cast<void*>(
1570+
reinterpret_cast<uptr>(info->agentBaseAddress) + kPageSize_);
1571+
info->hostBaseAddress = reinterpret_cast<void*>(
1572+
reinterpret_cast<uptr>(info->hostBaseAddress) + kPageSize_);
1573+
info->sizeInBytes = m->UsedSize();
1574+
}
1575+
return status;
1576+
}
1577+
return REAL(hsa_amd_pointer_info)(ptr, info, alloc, num_agents_accessible,
1578+
accessible);
1579+
}
1580+
15431581
} // namespace __asan
15441582
#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)