Skip to content

Commit d24d73a

Browse files
committed
[SANITIZER_AMDGPU] Add a workaround to solve the 2MB alignment requirement by device memory manager
Change-Id: Id7f07e3b868a5d4d4bb69d689f14c945eaab8f8a
1 parent 4daf980 commit d24d73a

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,10 @@ int __asan_update_allocation_context(void* addr) {
12281228
#if SANITIZER_AMDGPU
12291229
DECLARE_REAL(hsa_status_t, hsa_amd_agents_allow_access, uint32_t num_agents,
12301230
const hsa_agent_t *agents, const uint32_t *flags, const void *ptr)
1231+
DECLARE_REAL(hsa_status_t, hsa_amd_memory_pool_allocate,
1232+
hsa_amd_memory_pool_t memory_pool, size_t size, uint32_t flags,
1233+
void **ptr)
1234+
DECLARE_REAL(hsa_status_t, hsa_amd_memory_pool_free, void *ptr)
12311235

12321236
namespace __asan {
12331237

@@ -1237,22 +1241,37 @@ static const size_t kPageSize_ = 4096;
12371241
hsa_status_t asan_hsa_amd_memory_pool_allocate(
12381242
hsa_amd_memory_pool_t memory_pool, size_t size, uint32_t flags, void **ptr,
12391243
BufferedStackTrace *stack) {
1240-
AmdgpuAllocationInfo aa_info;
1241-
aa_info.alloc_func = reinterpret_cast<void *>(asan_hsa_amd_memory_pool_allocate);
1242-
aa_info.memory_pool = memory_pool;
1243-
aa_info.size = size;
1244-
aa_info.flags = flags;
1245-
aa_info.ptr = nullptr;
1246-
SetErrnoOnNull(*ptr = instance.Allocate(size, kPageSize_, stack, FROM_MALLOC,
1247-
false, &aa_info));
1248-
return aa_info.status;
1244+
// Device memory manager will allocate 2MB slabs which have to be 2MB
1245+
// aligned. This is hard to achieve with added redzone but not wasting big
1246+
// chunks of device memory. The current workaround is to bypass the
1247+
// allocation call to HSA if the allocation size is 2MB
1248+
static const size_t kSlabSize_ = 2 * 1024 * 1024;
1249+
if (size != kSlabSize_) {
1250+
AmdgpuAllocationInfo aa_info;
1251+
aa_info.alloc_func =
1252+
reinterpret_cast<void *>(asan_hsa_amd_memory_pool_allocate);
1253+
aa_info.memory_pool = memory_pool;
1254+
aa_info.size = size;
1255+
aa_info.flags = flags;
1256+
aa_info.ptr = nullptr;
1257+
SetErrnoOnNull(*ptr = instance.Allocate(size, kPageSize_, stack,
1258+
FROM_MALLOC, false, &aa_info));
1259+
return aa_info.status;
1260+
} else {
1261+
return REAL(hsa_amd_memory_pool_allocate)(memory_pool, size, flags, ptr);
1262+
}
12491263
}
12501264

12511265
hsa_status_t asan_hsa_amd_memory_pool_free(
12521266
void *ptr,
12531267
BufferedStackTrace *stack) {
1254-
instance.Deallocate(ptr, 0, 0, stack, FROM_MALLOC);
1255-
return HSA_STATUS_SUCCESS;
1268+
void *p = get_allocator().GetBlockBegin(ptr);
1269+
if (p) {
1270+
instance.Deallocate(ptr, 0, 0, stack, FROM_MALLOC);
1271+
return HSA_STATUS_SUCCESS;
1272+
} else {
1273+
return REAL(hsa_amd_memory_pool_free)(ptr);
1274+
}
12561275
}
12571276

12581277
hsa_status_t asan_hsa_amd_agents_allow_access(
@@ -1263,7 +1282,7 @@ hsa_status_t asan_hsa_amd_agents_allow_access(
12631282
if (p) {
12641283
return REAL(hsa_amd_agents_allow_access)(num_agents, agents, flags, p);
12651284
} else {
1266-
return HSA_STATUS_ERROR_FATAL;
1285+
return REAL(hsa_amd_agents_allow_access)(num_agents, agents, flags, ptr);
12671286
}
12681287
}
12691288
} // namespace __asan

0 commit comments

Comments
 (0)