Skip to content

Commit 993bce9

Browse files
committed
[HIP] Support ASAN with malloc/free
Device side malloc/free needs special implementation for ASAN. Reviewed by: Artem Belevich, Matt Arsenault Differential Revision: https://reviews.llvm.org/D143111
1 parent 1fdf06d commit 993bce9

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

clang/lib/Headers/__clang_hip_runtime_wrapper.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,25 @@ extern "C" {
8080
#if HIP_VERSION_MAJOR * 100 + HIP_VERSION_MINOR >= 405
8181
extern "C" __device__ unsigned long long __ockl_dm_alloc(unsigned long long __size);
8282
extern "C" __device__ void __ockl_dm_dealloc(unsigned long long __addr);
83+
#if __has_feature(address_sanitizer)
84+
extern "C" __device__ unsigned long long __asan_malloc_impl(unsigned long long __size, unsigned long long __pc);
85+
extern "C" __device__ void __asan_free_impl(unsigned long long __addr, unsigned long long __pc);
86+
__attribute__((noinline, weak)) __device__ void *malloc(__hip_size_t __size) {
87+
unsigned long long __pc = (unsigned long long)__builtin_return_address(0);
88+
return (void *)__asan_malloc_impl(__size, __pc);
89+
}
90+
__attribute__((noinline, weak)) __device__ void free(void *__ptr) {
91+
unsigned long long __pc = (unsigned long long)__builtin_return_address(0);
92+
__asan_free_impl((unsigned long long)__ptr, __pc);
93+
}
94+
#else
8395
__attribute__((weak)) inline __device__ void *malloc(__hip_size_t __size) {
8496
return (void *) __ockl_dm_alloc(__size);
8597
}
8698
__attribute__((weak)) inline __device__ void free(void *__ptr) {
8799
__ockl_dm_dealloc((unsigned long long)__ptr);
88100
}
101+
#endif // __has_feature(address_sanitizer)
89102
#else // HIP version check
90103
#if __HIP_ENABLE_DEVICE_MALLOC__
91104
__device__ void *__hip_malloc(__hip_size_t __size);

clang/test/Headers/hip-header.hip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
// RUN: -target-cpu gfx906 -emit-llvm %s -fcuda-is-device -o - \
3333
// RUN: -DHIP_VERSION_MAJOR=4 -DHIP_VERSION_MINOR=5 \
3434
// RUN: -D__HIPCC_RTC__ | FileCheck -check-prefixes=CHECK,MALLOC %s
35+
// RUN: %clang_cc1 -no-opaque-pointers -include __clang_hip_runtime_wrapper.h \
36+
// RUN: -internal-isystem %S/../../lib/Headers/cuda_wrappers \
37+
// RUN: -internal-isystem %S/Inputs/include \
38+
// RUN: -triple amdgcn-amd-amdhsa -aux-triple x86_64-unknown-unknown \
39+
// RUN: -target-cpu gfx906 -emit-llvm %s -fcuda-is-device -o - \
40+
// RUN: -DHIP_VERSION_MAJOR=4 -DHIP_VERSION_MINOR=5 \
41+
// RUN: -fsanitize=address -disable-llvm-passes -D__HIPCC_RTC__ \
42+
// RUN: | FileCheck -check-prefixes=MALLOC-ASAN %s
3543

3644
// expected-no-diagnostics
3745

@@ -130,6 +138,9 @@ __device__ double test_isnan() {
130138
// CHECK-LABEL: define weak {{.*}}i8* @malloc(i64
131139
// MALLOC: call i64 @__ockl_dm_alloc
132140
// NOMALLOC: call void @llvm.trap
141+
// MALLOC-ASAN-LABEL: define weak {{.*}}i8* @malloc(i64
142+
// MALLOC-ASAN: call i8* @llvm.returnaddress(i32 0)
143+
// MALLOC-ASAN: call i64 @__asan_malloc_impl(i64 {{.*}}, i64 {{.*}})
133144
__device__ void test_malloc(void *a) {
134145
a = malloc(42);
135146
}
@@ -139,6 +150,9 @@ __device__ void test_malloc(void *a) {
139150
// CHECK-LABEL: define weak {{.*}}void @free(i8*
140151
// MALLOC: call void @__ockl_dm_dealloc
141152
// NOMALLOC: call void @llvm.trap
153+
// MALLOC-ASAN-LABEL: define weak {{.*}}void @free(i8*
154+
// MALLOC-ASAN: call i8* @llvm.returnaddress(i32 0)
155+
// MALLOC-ASAN: call void @__asan_free_impl(i64 {{.*}}, i64 {{.*}})
142156
__device__ void test_free(void *a) {
143157
free(a);
144158
}

0 commit comments

Comments
 (0)