Skip to content

Commit 97fb21a

Browse files
authored
[rtsan] Intercept aligned_alloc on all versions of OSX if available on build machine (#112780)
1 parent 98e3075 commit 97fb21a

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,20 @@ TEST(TestRtsanInterceptors, VallocDiesWhenRealtime) {
122122
ExpectNonRealtimeSurvival(Func);
123123
}
124124

125-
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
125+
#if __has_builtin(__builtin_available) && SANITIZER_APPLE
126+
#define ALIGNED_ALLOC_AVAILABLE() (__builtin_available(macOS 10.15, *))
127+
#else
128+
// We are going to assume this is true until we hit systems where it isn't
129+
#define ALIGNED_ALLOC_AVAILABLE() (true)
130+
#endif
131+
126132
TEST(TestRtsanInterceptors, AlignedAllocDiesWhenRealtime) {
127-
auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
128-
ExpectRealtimeDeath(Func, "aligned_alloc");
129-
ExpectNonRealtimeSurvival(Func);
133+
if (ALIGNED_ALLOC_AVAILABLE()) {
134+
auto Func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
135+
ExpectRealtimeDeath(Func, "aligned_alloc");
136+
ExpectNonRealtimeSurvival(Func);
137+
}
130138
}
131-
#endif
132139

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

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,25 @@
8484
#define SI_NOT_MAC 1
8585
#endif
8686

87+
#if SANITIZER_APPLE
88+
# include <Availability.h>
89+
90+
// aligned_alloc was introduced in OSX 10.15
91+
// Linking will fail when using an older SDK
92+
# if defined(__MAC_10_15)
93+
// macOS 10.15 is greater than our minimal deployment target. To ensure we
94+
// generate a weak reference so the dylib continues to work on older
95+
// systems, we need to forward declare the intercepted function as "weak
96+
// imports".
97+
SANITIZER_WEAK_IMPORT void *aligned_alloc(__sanitizer::usize __alignment,
98+
__sanitizer::usize __size);
99+
# define SI_MAC_SDK_10_15_AVAILABLE 1
100+
# else
101+
# define SI_MAC_SDK_10_15_AVAILABLE 0
102+
# endif // defined(__MAC_10_15)
103+
104+
#endif // SANITIZER_APPLE
105+
87106
#if SANITIZER_IOS
88107
#define SI_IOS 1
89108
#else
@@ -500,7 +519,8 @@
500519
#define SANITIZER_INTERCEPT_PVALLOC (SI_GLIBC || SI_ANDROID)
501520
#define SANITIZER_INTERCEPT_CFREE (SI_GLIBC && !SANITIZER_RISCV64)
502521
#define SANITIZER_INTERCEPT_REALLOCARRAY SI_POSIX
503-
#define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!SI_MAC)
522+
#define SANITIZER_INTERCEPT_ALIGNED_ALLOC \
523+
(!SI_MAC || SI_MAC_SDK_10_15_AVAILABLE)
504524
#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
505525
#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
506526
#define SANITIZER_INTERCEPT_WCSLEN 1

0 commit comments

Comments
 (0)