Skip to content

Commit 4ccd2b0

Browse files
authored
Reland "[rtsan] Intercept aligned_alloc on all versions of OSX if available on the build machine" (#114153)
This commit reverts commit 7c55426 (relands commit 97fb21a) With the additional step of ignoring a warning that we don't care about. (-Wunguarded-availability-new) > We know that aligned_alloc will never be called on systems that do not have aligned_alloc defined because the client OSX won't provide it. This function is actually guarded on OS's lower than 10.15 because aligned_alloc declaration won't exist on that version. There will be no way to call this function from code.
1 parent 7e877fc commit 4ccd2b0

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

compiler-rt/lib/rtsan/rtsan_interceptors_posix.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,19 @@ INTERCEPTOR(void *, valloc, SIZE_T size) {
467467
}
468468

469469
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
470+
471+
// In some cases, when targeting older Darwin versions, this warning may pop up.
472+
// Because we are providing a wrapper, the client is responsible to check
473+
// whether aligned_alloc is available, not us. We still succeed linking on an
474+
// old OS, because we are using a weak symbol (see aligned_alloc in
475+
// sanitizer_platform_interceptors.h)
476+
#pragma clang diagnostic push
477+
#pragma clang diagnostic ignored "-Wunguarded-availability-new"
470478
INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
471479
__rtsan_notify_intercepted_call("aligned_alloc");
472480
return REAL(aligned_alloc)(alignment, size);
473481
}
482+
#pragma clang diagnostic pop
474483
#define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
475484
#else
476485
#define RTSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC

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)