Skip to content

Commit 0182c5d

Browse files
committed
[PR] move to atomic variables for initialization
1 parent 8b41126 commit 0182c5d

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,43 @@
1212
#include <rtsan/rtsan_context.h>
1313
#include <rtsan/rtsan_interceptors.h>
1414

15+
#include "sanitizer_common/sanitizer_atomic.h"
16+
1517
using namespace __rtsan;
18+
using namespace __sanitizer;
19+
20+
static atomic_uint8_t rtsan_initialized{0};
21+
static atomic_uint8_t rtsan_init_is_running{0};
22+
23+
static void SetInitIsRunning(bool is_running) {
24+
atomic_store(&rtsan_init_is_running, is_running, memory_order_release);
25+
}
26+
27+
static bool IsInitRunning() {
28+
return atomic_load(&rtsan_init_is_running, memory_order_acquire) == 1;
29+
}
1630

17-
bool __rtsan::rtsan_initialized;
18-
bool __rtsan::rtsan_init_is_running;
31+
static void SetInitialized() {
32+
atomic_store(&rtsan_initialized, 1, memory_order_release);
33+
}
1934

2035
extern "C" {
2136

2237
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
23-
CHECK(!rtsan_init_is_running);
24-
if (rtsan_initialized)
38+
CHECK(!IsInitRunning());
39+
if (__rtsan_is_initialized())
2540
return;
26-
rtsan_init_is_running = true;
41+
42+
SetInitIsRunning(true);
2743

2844
InitializeInterceptors();
2945

30-
rtsan_init_is_running = false;
31-
rtsan_initialized = true;
46+
SetInitIsRunning(false);
47+
SetInitialized();
48+
}
49+
50+
SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized() {
51+
return atomic_load(&rtsan_initialized, memory_order_acquire) == 1;
3252
}
3353

3454
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_realtime_enter() {

compiler-rt/lib/rtsan/rtsan.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@
1414

1515
extern "C" {
1616

17-
namespace __rtsan {
18-
19-
extern bool rtsan_initialized;
20-
extern bool rtsan_init_is_running;
21-
22-
} // namespace __rtsan
23-
2417
// Initialise rtsan interceptors.
2518
// A call to this method is added to the preinit array on Linux systems.
2619
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init();
2720

21+
SANITIZER_INTERFACE_ATTRIBUTE bool __rtsan_is_initialized();
22+
2823
// Enter real-time context.
2924
// When in a real-time context, RTSan interceptors will error if realtime
3025
// violations are detected. Calls to this method are injected at the code

compiler-rt/lib/rtsan/rtsan_interceptors.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@
3939

4040
using namespace __sanitizer;
4141

42-
using __rtsan::rtsan_initialized;
43-
4442
namespace {
4543
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
46-
static bool UseImpl() { return !rtsan_initialized; }
44+
static bool UseImpl() { return !__rtsan_is_initialized(); }
4745
};
4846
} // namespace
4947

5048
void ExpectNotRealtime(const char *intercepted_function_name) {
51-
if (!rtsan_initialized)
49+
if (!__rtsan_is_initialized())
5250
__rtsan_init();
5351

5452
__rtsan::GetContextForThisThread().ExpectNotRealtime(

0 commit comments

Comments
 (0)