Skip to content

Commit d5b090a

Browse files
committed
[rtsan] Intercept aligned_alloc on all versions of OSX if available on the build machine
1 parent 8f8d5f0 commit d5b090a

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,17 @@ INTERCEPTOR(void *, valloc, SIZE_T size) {
461461
return REAL(valloc)(size);
462462
}
463463

464-
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
464+
// aligned_alloc was introduced in OSX 10.15
465+
// Linking will fail when using an older SDK
466+
#if defined(__MAC_10_15)
467+
// macOS 10.15 is greater than our minimal deployment target. To ensure we
468+
// generate a weak reference so the dylib continues to work on older
469+
// systems, we need to forward declare the intercepted function as "weak
470+
// imports".
471+
SANITIZER_WEAK_IMPORT void *aligned_alloc(SIZE_T __alignment, SIZE_T __size);
472+
#endif // defined(__MAC_10_15)
473+
474+
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC || SANITIZER_APPLE
465475
INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
466476
__rtsan_notify_intercepted_call("aligned_alloc");
467477
return REAL(aligned_alloc)(alignment, size);

compiler-rt/lib/rtsan/tests/rtsan_test_interceptors.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@ TEST(TestRtsanInterceptors, VallocDiesWhenRealtime) {
120120
ExpectNonRealtimeSurvival(Func);
121121
}
122122

123-
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
124123
TEST(TestRtsanInterceptors, AlignedAllocDiesWhenRealtime) {
125-
auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
126-
ExpectRealtimeDeath(Func, "aligned_alloc");
127-
ExpectNonRealtimeSurvival(Func);
128-
}
124+
#if SANITIZER_APPLE
125+
if (__builtin_available(macOS 10.15, *)) {
126+
#endif // SANITIZER_APPLE
127+
auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
128+
ExpectRealtimeDeath(Func, "aligned_alloc");
129+
ExpectNonRealtimeSurvival(Func);
130+
#if SANITIZER_APPLE
131+
}
129132
#endif
133+
}
130134

131135
// free_sized and free_aligned_sized (both C23) are not yet supported
132136
TEST(TestRtsanInterceptors, FreeDiesWhenRealtime) {

0 commit comments

Comments
 (0)