Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions compiler-rt/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -446,25 +446,28 @@ if(APPLE)

# Note: In order to target x86_64h on OS X the minimum deployment target must
# be 10.8 or higher.
set(SANITIZER_MIN_OSX_VERSION "" CACHE STRING
"Minimum OS X version to target (e.g. 10.15) for sanitizers.")

set(DEFAULT_SANITIZER_MIN_OSX_VERSION 10.10)
set(DARWIN_osx_MIN_VER_FLAG "-mmacosx-version-min")
if(NOT SANITIZER_MIN_OSX_VERSION)
if(SANITIZER_MIN_OSX_VERSION STREQUAL "")
string(REGEX MATCH "${DARWIN_osx_MIN_VER_FLAG}=([.0-9]+)"
MACOSX_VERSION_MIN_FLAG "${CMAKE_CXX_FLAGS}")
if(MACOSX_VERSION_MIN_FLAG)
set(SANITIZER_MIN_OSX_VERSION "${CMAKE_MATCH_1}")
set(MIN_OSX_VERSION "${CMAKE_MATCH_1}")
elseif(CMAKE_OSX_DEPLOYMENT_TARGET)
set(SANITIZER_MIN_OSX_VERSION ${CMAKE_OSX_DEPLOYMENT_TARGET})
set(MIN_OSX_VERSION ${CMAKE_OSX_DEPLOYMENT_TARGET})
else()
set(SANITIZER_MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
set(MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
endif()
if(SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.7")

if(MIN_OSX_VERSION VERSION_LESS "10.7")
message(FATAL_ERROR "macOS deployment target '${SANITIZER_MIN_OSX_VERSION}' is too old.")
endif()
if(SANITIZER_MIN_OSX_VERSION VERSION_GREATER ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
message(WARNING "macOS deployment target '${SANITIZER_MIN_OSX_VERSION}' is too new, setting to '${DEFAULT_SANITIZER_MIN_OSX_VERSION}' instead.")
set(SANITIZER_MIN_OSX_VERSION ${DEFAULT_SANITIZER_MIN_OSX_VERSION})
endif()

set(SANITIZER_MIN_OSX_VERSION "${MIN_OSX_VERSION}" CACHE STRING
"Minimum OS X version to target (e.g. 10.15) for sanitizers." FORCE)
endif()

# We're setting the flag manually for each target OS
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/lib/radsan/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ set(RADSAN_LINK_LIBS
${COMPILER_RT_CXX_LINK_LIBS})

if(APPLE)
list(APPEND RADSAN_CFLAGS -mmacosx-version-min=10.15)
add_compiler_rt_object_libraries(RTRadsan
OS ${SANITIZER_COMMON_SUPPORTED_OS}
ARCHS ${RADSAN_SUPPORTED_ARCH}
Expand Down
8 changes: 7 additions & 1 deletion compiler-rt/lib/radsan/radsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "radsan/radsan_interceptors.h"

#include "sanitizer_common/sanitizer_platform.h"
#include "sanitizer_common/sanitizer_platform_interceptors.h"

#include "interception/interception.h"
#include "radsan/radsan_context.h"
Expand Down Expand Up @@ -261,10 +262,15 @@ INTERCEPTOR(void *, valloc, SIZE_T size) {
return REAL(valloc)(size);
}

#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
radsan::expectNotRealtime("aligned_alloc");
return REAL(aligned_alloc)(alignment, size);
}
#define RADSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
#else
#define RADSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
#endif

INTERCEPTOR(int, posix_memalign, void **memptr, size_t alignment, size_t size) {
radsan::expectNotRealtime("posix_memalign");
Expand Down Expand Up @@ -330,7 +336,7 @@ void initialiseInterceptors() {
INTERCEPT_FUNCTION(realloc);
INTERCEPT_FUNCTION(reallocf);
INTERCEPT_FUNCTION(valloc);
INTERCEPT_FUNCTION(aligned_alloc);
RADSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC;
INTERCEPT_FUNCTION(posix_memalign);

INTERCEPT_FUNCTION(open);
Expand Down
4 changes: 0 additions & 4 deletions compiler-rt/lib/radsan/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ if (APPLE)
list(APPEND RADSAN_UNITTEST_LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS})
list(APPEND RADSAN_UNITTEST_LINK_FLAGS ${DARWIN_osx_LINK_FLAGS})
list(APPEND RADSAN_UNITTEST_CFLAGS ${DARWIN_osx_CFLAGS})
# aligned_alloc is only available in macOS 10.15 and later. This is a temporary
# solution that we're running with until we have a full understanding of what
# macOS versions we wish to support.
list(APPEND RADSAN_UNITTEST_CFLAGS -mmacosx-version-min=10.15)
else()
#append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic RADSAN_UNITTEST_LINK_FLAGS)
list(APPEND RADSAN_UNITTEST_LINK_FLAGS -latomic)
Expand Down
15 changes: 15 additions & 0 deletions compiler-rt/lib/radsan/tests/radsan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "radsan_test_utilities.h"
#include <radsan.h>
#include <sanitizer_common/sanitizer_platform.h>
#include <sanitizer_common/sanitizer_platform_interceptors.h>

#include <array>
#include <atomic>
Expand All @@ -20,6 +21,15 @@
#include <shared_mutex>
#include <thread>

#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101200
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_12 1
#else
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_12 0
#endif

#define RADSAN_TEST_SHARED_MUTEX (!(SI_MAC) || SI_MAC_DEPLOYMENT_AT_LEAST_10_12)

using namespace testing;
using namespace radsan_testing;
using namespace std::chrono_literals;
Expand Down Expand Up @@ -98,6 +108,9 @@ TEST(TestRadsan, unlockingAMutexDiesWhenRealtime) {
expectNonrealtimeSurvival(func);
}


#if RADSAN_TEST_SHARED_MUTEX

TEST(TestRadsan, lockingASharedMutexDiesWhenRealtime) {
auto mutex = std::shared_mutex();
auto func = [&]() { mutex.lock(); };
Expand Down Expand Up @@ -128,6 +141,8 @@ TEST(TestRadsan, sharedUnlockingASharedMutexDiesWhenRealtime) {
expectNonrealtimeSurvival(func);
}

#endif // RADSAN_TEST_SHARED_MUTEX

TEST(TestRadsan, launchingAThreadDiesWhenRealtime) {
auto func = [&]() {
auto t = std::thread([]() {});
Expand Down
3 changes: 3 additions & 0 deletions compiler-rt/lib/radsan/tests/radsan_test_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "gtest/gtest.h"

#include <sanitizer_common/sanitizer_platform.h>
#include <sanitizer_common/sanitizer_platform_interceptors.h>

#include "radsan_test_utilities.h"

Expand Down Expand Up @@ -66,11 +67,13 @@ TEST(TestRadsanInterceptors, vallocDiesWhenRealtime) {
expectNonrealtimeSurvival(func);
}

#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
TEST(TestRadsanInterceptors, alignedAllocDiesWhenRealtime) {
auto func = []() { EXPECT_NE(nullptr, aligned_alloc(16, 32)); };
expectRealtimeDeath(func, "aligned_alloc");
expectNonrealtimeSurvival(func);
}
#endif

// free_sized and free_aligned_sized (both C23) are not yet supported
TEST(TestRadsanInterceptors, freeDiesWhenRealtime) {
Expand Down
9 changes: 4 additions & 5 deletions compiler-rt/lib/radsan/tests/radsan_test_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
#pragma once

#include "gmock/gmock.h"

#include <optional>
#include <string>

namespace radsan_testing {

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

template <typename Function>
void expectRealtimeDeath(
Function &&func, std::optional<std::string> intercepted_method_name = {}) {
Function &&func, const char* intercepted_method_name = nullptr) {

using namespace testing;

auto expected_error_substr = [&]() -> std::string {
return intercepted_method_name.has_value()
return intercepted_method_name != nullptr
? "Real-time violation: intercepted call to real-time unsafe "
"function `" +
intercepted_method_name.value() + "`"
std::string(intercepted_method_name) + "`"
: "";
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@
#define SANITIZER_INTERCEPT_PVALLOC (SI_GLIBC || SI_ANDROID)
#define SANITIZER_INTERCEPT_CFREE (SI_GLIBC && !SANITIZER_RISCV64)
#define SANITIZER_INTERCEPT_REALLOCARRAY SI_POSIX
#define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!SI_MAC)
#define SANITIZER_INTERCEPT_ALIGNED_ALLOC (!(SI_MAC) || SI_MAC_DEPLOYMENT_AT_LEAST_10_15)
#define SANITIZER_INTERCEPT_MALLOC_USABLE_SIZE (!SI_MAC && !SI_NETBSD)
#define SANITIZER_INTERCEPT_MCHECK_MPROBE SI_LINUX_NOT_ANDROID
#define SANITIZER_INTERCEPT_WCSLEN 1
Expand Down Expand Up @@ -523,6 +523,14 @@
#else
#define SI_MAC_DEPLOYMENT_BELOW_10_10 0
#endif

#if defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__) && \
__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 101500
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_15 1
#else
#define SI_MAC_DEPLOYMENT_AT_LEAST_10_15 0
#endif

#define SANITIZER_INTERCEPT_READLINKAT \
(SI_POSIX && !SI_MAC_DEPLOYMENT_BELOW_10_10)

Expand Down