@@ -1228,6 +1228,10 @@ int __asan_update_allocation_context(void* addr) {
1228
1228
#if SANITIZER_AMDGPU
1229
1229
DECLARE_REAL (hsa_status_t , hsa_amd_agents_allow_access, uint32_t num_agents,
1230
1230
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)
1231
1235
1232
1236
namespace __asan {
1233
1237
@@ -1237,22 +1241,37 @@ static const size_t kPageSize_ = 4096;
1237
1241
hsa_status_t asan_hsa_amd_memory_pool_allocate (
1238
1242
hsa_amd_memory_pool_t memory_pool, size_t size, uint32_t flags, void **ptr,
1239
1243
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
+ }
1249
1263
}
1250
1264
1251
1265
hsa_status_t asan_hsa_amd_memory_pool_free (
1252
1266
void *ptr,
1253
1267
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
+ }
1256
1275
}
1257
1276
1258
1277
hsa_status_t asan_hsa_amd_agents_allow_access (
@@ -1263,7 +1282,7 @@ hsa_status_t asan_hsa_amd_agents_allow_access(
1263
1282
if (p) {
1264
1283
return REAL (hsa_amd_agents_allow_access)(num_agents, agents, flags, p);
1265
1284
} else {
1266
- return HSA_STATUS_ERROR_FATAL ;
1285
+ return REAL (hsa_amd_agents_allow_access)(num_agents, agents, flags, ptr) ;
1267
1286
}
1268
1287
}
1269
1288
} // namespace __asan
0 commit comments