Skip to content

Commit 5189f45

Browse files
Merge pull request #4 from cjappl/alignedalloc_2
Protect aligned_alloc and shared_mutex and tests behind ifdefs
2 parents b4bbc41 + d6b6a42 commit 5189f45

File tree

8 files changed

+50
-21
lines changed

8 files changed

+50
-21
lines changed

compiler-rt/cmake/config-ix.cmake

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,25 +446,28 @@ if(APPLE)
446446

447447
# Note: In order to target x86_64h on OS X the minimum deployment target must
448448
# be 10.8 or higher.
449+
set(SANITIZER_MIN_OSX_VERSION "" CACHE STRING
450+
"Minimum OS X version to target (e.g. 10.15) for sanitizers.")
451+
449452
set(DEFAULT_SANITIZER_MIN_OSX_VERSION 10.10)
450453
set(DARWIN_osx_MIN_VER_FLAG "-mmacosx-version-min")
451-
if(NOT SANITIZER_MIN_OSX_VERSION)
454+
if(SANITIZER_MIN_OSX_VERSION STREQUAL "")
452455
string(REGEX MATCH "${DARWIN_osx_MIN_VER_FLAG}=([.0-9]+)"
453456
MACOSX_VERSION_MIN_FLAG "${CMAKE_CXX_FLAGS}")
454457
if(MACOSX_VERSION_MIN_FLAG)
455-
set(SANITIZER_MIN_OSX_VERSION "${CMAKE_MATCH_1}")
458+
set(MIN_OSX_VERSION "${CMAKE_MATCH_1}")
456459
elseif(CMAKE_OSX_DEPLOYMENT_TARGET)
457-
set(SANITIZER_MIN_OSX_VERSION ${CMAKE_OSX_DEPLOYMENT_TARGET})
460+
set(MIN_OSX_VERSION ${CMAKE_OSX_DEPLOYMENT_TARGET})
458461
else()
459-
set(SANITIZER_MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
462+
set(MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
460463
endif()
461-
if(SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.7")
464+
465+
if(MIN_OSX_VERSION VERSION_LESS "10.7")
462466
message(FATAL_ERROR "macOS deployment target '${SANITIZER_MIN_OSX_VERSION}' is too old.")
463467
endif()
464-
if(SANITIZER_MIN_OSX_VERSION VERSION_GREATER ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
465-
message(WARNING "macOS deployment target '${SANITIZER_MIN_OSX_VERSION}' is too new, setting to '${DEFAULT_SANITIZER_MIN_OSX_VERSION}' instead.")
466-
set(SANITIZER_MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
467-
endif()
468+
469+
set(SANITIZER_MIN_OSX_VERSION "${MIN_OSX_VERSION}" CACHE STRING
470+
"Minimum OS X version to target (e.g. 10.15) for sanitizers." FORCE)
468471
endif()
469472

470473
# We're setting the flag manually for each target OS

compiler-rt/lib/radsan/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ set(RADSAN_LINK_LIBS
2727
${COMPILER_RT_CXX_LINK_LIBS})
2828

2929
if(APPLE)
30-
list(APPEND RADSAN_CFLAGS -mmacosx-version-min=10.15)
3130
add_compiler_rt_object_libraries(RTRadsan
3231
OS ${SANITIZER_COMMON_SUPPORTED_OS}
3332
ARCHS ${RADSAN_SUPPORTED_ARCH}

compiler-rt/lib/radsan/radsan_interceptors.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "radsan/radsan_interceptors.h"
1010

1111
#include "sanitizer_common/sanitizer_platform.h"
12+
#include "sanitizer_common/sanitizer_platform_interceptors.h"
1213

1314
#include "interception/interception.h"
1415
#include "radsan/radsan_context.h"
@@ -261,10 +262,15 @@ INTERCEPTOR(void *, valloc, SIZE_T size) {
261262
return REAL(valloc)(size);
262263
}
263264

265+
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
264266
INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
265267
radsan::expectNotRealtime("aligned_alloc");
266268
return REAL(aligned_alloc)(alignment, size);
267269
}
270+
#define RADSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
271+
#else
272+
#define RADSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
273+
#endif
268274

269275
INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
270276
radsan::expectNotRealtime("posix_memalign");
@@ -330,7 +336,7 @@ void initialiseInterceptors() {
330336
INTERCEPT_FUNCTION(realloc);
331337
INTERCEPT_FUNCTION(reallocf);
332338
INTERCEPT_FUNCTION(valloc);
333-
INTERCEPT_FUNCTION(aligned_alloc);
339+
RADSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
334340
INTERCEPT_FUNCTION(posix_memalign);
335341

336342
INTERCEPT_FUNCTION(open);

compiler-rt/lib/radsan/tests/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ if (APPLE)
4141
list(APPEND RADSAN_UNITTEST_LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS})
4242
list(APPEND RADSAN_UNITTEST_LINK_FLAGS ${DARWIN_osx_LINK_FLAGS})
4343
list(APPEND RADSAN_UNITTEST_CFLAGS ${DARWIN_osx_CFLAGS})
44-
# aligned_alloc is only available in macOS 10.15 and later. This is a temporary
45-
# solution that we're running with until we have a full understanding of what
46-
# macOS versions we wish to support.
47-
list(APPEND RADSAN_UNITTEST_CFLAGS -mmacosx-version-min=10.15)
4844
else()
4945
#append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RADSAN_UNITTEST_LINK_FLAGS)
5046
list(APPEND RADSAN_UNITTEST_LINK_FLAGS -latomic)

compiler-rt/lib/radsan/tests/radsan_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "radsan_test_utilities.h"
1212
#include <radsan.h>
1313
#include <sanitizer_common/sanitizer_platform.h>
14+
#include <sanitizer_common/sanitizer_platform_interceptors.h>
1415

1516
#include <array>
1617
#include <atomic>
@@ -20,6 +21,15 @@
2021
#include <shared_mutex>
2122
#include <thread>
2223

24+
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
25+
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
26+
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_12 1
27+
#else
28+
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_12 0
29+
#endif
30+
31+
#define RADSAN_TEST_SHARED_MUTEX (!(SI_MAC) || SI_MAC_DEPLOYMENT_AT_LEAST_10_12)
32+
2333
using namespace testing;
2434
using namespace radsan_testing;
2535
using namespace std::chrono_literals;
@@ -98,6 +108,9 @@ TEST(TestRadsan, unlockingAMutexDiesWhenRealtime) {
98108
expectNonrealtimeSurvival(func);
99109
}
100110

111+
112+
#if RADSAN_TEST_SHARED_MUTEX
113+
101114
TEST(TestRadsan, lockingASharedMutexDiesWhenRealtime) {
102115
auto mutex = std::shared_mutex();
103116
auto func = [&]() { mutex.lock(); };
@@ -128,6 +141,8 @@ TEST(TestRadsan, sharedUnlockingASharedMutexDiesWhenRealtime) {
128141
expectNonrealtimeSurvival(func);
129142
}
130143

144+
#endif // RADSAN_TEST_SHARED_MUTEX
145+
131146
TEST(TestRadsan, launchingAThreadDiesWhenRealtime) {
132147
auto func = [&]() {
133148
auto t = std::thread([]() {});

compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "gtest/gtest.h"
1010

1111
#include <sanitizer_common/sanitizer_platform.h>
12+
#include <sanitizer_common/sanitizer_platform_interceptors.h>
1213

1314
#include "radsan_test_utilities.h"
1415

@@ -66,11 +67,13 @@ TEST(TestRadsanInterceptors, vallocDiesWhenRealtime) {
6667
expectNonrealtimeSurvival(func);
6768
}
6869

70+
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
6971
TEST(TestRadsanInterceptors, alignedAllocDiesWhenRealtime) {
7072
auto func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
7173
expectRealtimeDeath(func, "aligned_alloc");
7274
expectNonrealtimeSurvival(func);
7375
}
76+
#endif
7477

7578
// free_sized and free_aligned_sized (both C23) are not yet supported
7679
TEST(TestRadsanInterceptors, freeDiesWhenRealtime) {

compiler-rt/lib/radsan/tests/radsan_test_utilities.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#pragma once
1010

1111
#include "gmock/gmock.h"
12-
13-
#include <optional>
12+
#include <string>
1413

1514
namespace radsan_testing {
1615

@@ -21,15 +20,15 @@ template <typename Function>
2120

2221
template <typename Function>
2322
void expectRealtimeDeath(
24-
Function &&func, std::optional<std::string> intercepted_method_name = {}) {
23+
Function &&func, const char* intercepted_method_name = nullptr) {
2524

2625
using namespace testing;
2726

2827
auto expected_error_substr = [&]() -> std::string {
29-
return intercepted_method_name.has_value()
28+
return intercepted_method_name != nullptr
3029
? "Real-time violation: intercepted call to real-time unsafe "
3130
"function `" +
32-
intercepted_method_name.value() + "`"
31+
std::string(intercepted_method_name) + "`"
3332
: "";
3433
};
3534

compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@
492492
#define SANITIZER_INTERCEPT_PVALLOC (SI_GLIBC || SI_ANDROID)
493493
#define SANITIZER_INTERCEPT_CFREE (SI_GLIBC && !SANITIZER_RISCV64)
494494
#define SANITIZER_INTERCEPT_REALLOCARRAY SI_POSIX
495-
#define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!SI_MAC)
495+
#define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!(SI_MAC) || SI_MAC_DEPLOYMENT_AT_LEAST_10_15)
496496
#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
497497
#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
498498
#define SANITIZER_INTERCEPT_WCSLEN 1
@@ -523,6 +523,14 @@
523523
#else
524524
#define SI_MAC_DEPLOYMENT_BELOW_10_10 0
525525
#endif
526+
527+
#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
528+
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500
529+
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_15 1
530+
#else
531+
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_15 0
532+
#endif
533+
526534
#define SANITIZER_INTERCEPT_READLINKAT \
527535
(SI_POSIX && !SI_MAC_DEPLOYMENT_BELOW_10_10)
528536

0 commit comments

Comments
 (0)