Skip to content

[rtsan] Make sure rtsan gets initialized on mac #100188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 11, 2024
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
35 changes: 27 additions & 8 deletions compiler-rt/lib/rtsan/rtsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,42 @@
#include <rtsan/rtsan_context.h>
#include <rtsan/rtsan_interceptors.h>

#include "sanitizer_common/sanitizer_atomic.h"
#include "sanitizer_common/sanitizer_mutex.h"

using namespace __rtsan;
using namespace __sanitizer;

static StaticSpinMutex rtsan_inited_mutex;
static atomic_uint8_t rtsan_initialized = {0};

bool __rtsan::rtsan_initialized;
bool __rtsan::rtsan_init_is_running;
static void SetInitialized() {
atomic_store(&rtsan_initialized, 1, memory_order_release);
}

extern "C" {

SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
CHECK(!rtsan_init_is_running);
if (rtsan_initialized)
CHECK(!__rtsan_is_initialized());
InitializeInterceptors();
SetInitialized();
}

SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_ensure_initialized() {
if (LIKELY(__rtsan_is_initialized()))
return;
rtsan_init_is_running = true;

InitializeInterceptors();
SpinMutexLock lock(&rtsan_inited_mutex);

// Someone may have initialized us while we were waiting for the lock
if (__rtsan_is_initialized())
return;

__rtsan_init();
}

rtsan_init_is_running = false;
rtsan_initialized = true;
SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized() {
return atomic_load(&rtsan_initialized, memory_order_acquire) == 1;
}

SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_realtime_enter() {
Expand Down
14 changes: 7 additions & 7 deletions compiler-rt/lib/rtsan/rtsan.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

extern "C" {

namespace __rtsan {

extern bool rtsan_initialized;
extern bool rtsan_init_is_running;

} // namespace __rtsan

// Initialise rtsan interceptors.
// A call to this method is added to the preinit array on Linux systems.
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init();

// Initializes rtsan if it has not been initialized yet.
// Used by the RTSan runtime to ensure that rtsan is initialized before any
// other rtsan functions are called.
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_ensure_initialized();

SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized();

// Enter real-time context.
// When in a real-time context, RTSan interceptors will error if realtime
// violations are detected. Calls to this method are injected at the code
Expand Down
7 changes: 3 additions & 4 deletions compiler-rt/lib/rtsan/rtsan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ void OSSpinLockLock(volatile OSSpinLock *__lock);

using namespace __sanitizer;

using __rtsan::rtsan_init_is_running;
using __rtsan::rtsan_initialized;

namespace {
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
static bool UseImpl() { return !rtsan_initialized; }
static bool UseImpl() { return !__rtsan_is_initialized(); }
};
} // namespace

void ExpectNotRealtime(const char *intercepted_function_name) {
__rtsan_ensure_initialized();

__rtsan::GetContextForThisThread().ExpectNotRealtime(
intercepted_function_name);
}
Expand Down
Loading