Skip to content

Commit 5d6304f

Browse files
zacklj89vitalybuka
authored andcommitted
[NFC][asan] Change asan_init and asan_init_is_running; add setters/getters
For #71833
1 parent 08771c4 commit 5d6304f

11 files changed

+57
-49
lines changed

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ struct Allocator {
527527
// -------------------- Allocation/Deallocation routines ---------------
528528
void *Allocate(uptr size, uptr alignment, BufferedStackTrace *stack,
529529
AllocType alloc_type, bool can_fill) {
530-
if (UNLIKELY(!asan_inited))
530+
if (UNLIKELY(!AsanInited()))
531531
AsanInitFromRtl();
532532
if (UNLIKELY(IsRssLimitExceeded())) {
533533
if (AllocatorMayReturnNull())

compiler-rt/lib/asan/asan_globals.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ static inline bool UseODRIndicator(const Global *g) {
196196
// This function may be called more than once for every global
197197
// so we store the globals in a map.
198198
static void RegisterGlobal(const Global *g) {
199-
CHECK(asan_inited);
199+
CHECK(AsanInited());
200200
if (flags()->report_globals >= 2)
201201
ReportGlobal(*g, "Added");
202202
CHECK(flags()->report_globals);
@@ -237,7 +237,7 @@ static void RegisterGlobal(const Global *g) {
237237
}
238238

239239
static void UnregisterGlobal(const Global *g) {
240-
CHECK(asan_inited);
240+
CHECK(AsanInited());
241241
if (flags()->report_globals >= 2)
242242
ReportGlobal(*g, "Removed");
243243
CHECK(flags()->report_globals);
@@ -427,7 +427,7 @@ void __asan_before_dynamic_init(const char *module_name) {
427427
return;
428428
bool strict_init_order = flags()->strict_init_order;
429429
CHECK(module_name);
430-
CHECK(asan_inited);
430+
CHECK(AsanInited());
431431
Lock lock(&mu_for_globals);
432432
if (flags()->report_globals >= 3)
433433
Printf("DynInitPoison module: %s\n", module_name);
@@ -451,7 +451,7 @@ void __asan_after_dynamic_init() {
451451
!CanPoisonMemory() ||
452452
!dynamic_init_globals)
453453
return;
454-
CHECK(asan_inited);
454+
CHECK(AsanInited());
455455
Lock lock(&mu_for_globals);
456456
// FIXME: Optionally report that we're unpoisoning globals from a module.
457457
for (uptr i = 0, n = dynamic_init_globals->size(); i < n; ++i) {

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
9696
ASAN_WRITE_RANGE(ctx, ptr, size)
9797
#define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \
9898
ASAN_READ_RANGE(ctx, ptr, size)
99-
# define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
100-
ASAN_INTERCEPTOR_ENTER(ctx, func); \
101-
do { \
102-
if (asan_init_is_running) \
103-
return REAL(func)(__VA_ARGS__); \
104-
if (SANITIZER_APPLE && UNLIKELY(!asan_inited)) \
105-
return REAL(func)(__VA_ARGS__); \
106-
ENSURE_ASAN_INITED(); \
99+
# define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \
100+
ASAN_INTERCEPTOR_ENTER(ctx, func); \
101+
do { \
102+
if (AsanInitIsRunning()) \
103+
return REAL(func)(__VA_ARGS__); \
104+
if (SANITIZER_APPLE && UNLIKELY(!AsanInited())) \
105+
return REAL(func)(__VA_ARGS__); \
106+
ENSURE_ASAN_INITED(); \
107107
} while (false)
108108
#define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
109109
do { \
@@ -138,7 +138,7 @@ DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
138138
# define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit()
139139
# define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle)
140140
# define COMMON_INTERCEPTOR_LIBRARY_UNLOADED()
141-
# define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited)
141+
# define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!AsanInited())
142142
# define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \
143143
if (AsanThread *t = GetCurrentThread()) { \
144144
*begin = t->tls_begin(); \
@@ -535,12 +535,12 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
535535
void *ctx;
536536
ASAN_INTERCEPTOR_ENTER(ctx, strcpy);
537537
#if SANITIZER_APPLE
538-
if (UNLIKELY(!asan_inited))
538+
if (UNLIKELY(!AsanInited()))
539539
return REAL(strcpy)(to, from);
540540
#endif
541541
// strcpy is called from malloc_default_purgeable_zone()
542542
// in __asan::ReplaceSystemAlloc() on Mac.
543-
if (asan_init_is_running) {
543+
if (AsanInitIsRunning()) {
544544
return REAL(strcpy)(to, from);
545545
}
546546
ENSURE_ASAN_INITED();
@@ -556,7 +556,7 @@ INTERCEPTOR(char *, strcpy, char *to, const char *from) {
556556
INTERCEPTOR(char*, strdup, const char *s) {
557557
void *ctx;
558558
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
559-
if (UNLIKELY(!asan_inited))
559+
if (UNLIKELY(!AsanInited()))
560560
return internal_strdup(s);
561561
ENSURE_ASAN_INITED();
562562
uptr length = internal_strlen(s);
@@ -575,7 +575,7 @@ INTERCEPTOR(char*, strdup, const char *s) {
575575
INTERCEPTOR(char*, __strdup, const char *s) {
576576
void *ctx;
577577
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
578-
if (UNLIKELY(!asan_inited))
578+
if (UNLIKELY(!AsanInited()))
579579
return internal_strdup(s);
580580
ENSURE_ASAN_INITED();
581581
uptr length = internal_strlen(s);
@@ -636,7 +636,7 @@ INTERCEPTOR(int, atoi, const char *nptr) {
636636
void *ctx;
637637
ASAN_INTERCEPTOR_ENTER(ctx, atoi);
638638
#if SANITIZER_APPLE
639-
if (UNLIKELY(!asan_inited))
639+
if (UNLIKELY(!AsanInited()))
640640
return REAL(atoi)(nptr);
641641
# endif
642642
ENSURE_ASAN_INITED();
@@ -658,7 +658,7 @@ INTERCEPTOR(long, atol, const char *nptr) {
658658
void *ctx;
659659
ASAN_INTERCEPTOR_ENTER(ctx, atol);
660660
#if SANITIZER_APPLE
661-
if (UNLIKELY(!asan_inited))
661+
if (UNLIKELY(!AsanInited()))
662662
return REAL(atol)(nptr);
663663
# endif
664664
ENSURE_ASAN_INITED();
@@ -697,7 +697,7 @@ static void AtCxaAtexit(void *unused) {
697697
INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg,
698698
void *dso_handle) {
699699
#if SANITIZER_APPLE
700-
if (UNLIKELY(!asan_inited))
700+
if (UNLIKELY(!AsanInited()))
701701
return REAL(__cxa_atexit)(func, arg, dso_handle);
702702
# endif
703703
ENSURE_ASAN_INITED();

compiler-rt/lib/asan/asan_interceptors.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ namespace __asan {
2424
void InitializeAsanInterceptors();
2525
void InitializePlatformInterceptors();
2626

27-
#define ENSURE_ASAN_INITED() \
28-
do { \
29-
CHECK(!asan_init_is_running); \
30-
if (UNLIKELY(!asan_inited)) { \
31-
AsanInitFromRtl(); \
32-
} \
27+
#define ENSURE_ASAN_INITED() \
28+
do { \
29+
CHECK(!AsanInitIsRunning()); \
30+
if (UNLIKELY(!AsanInited())) { \
31+
AsanInitFromRtl(); \
32+
} \
3333
} while (0)
3434

3535
} // namespace __asan

compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ using namespace __asan;
3333
} \
3434
ASAN_READ_RANGE(ctx, from, size); \
3535
ASAN_WRITE_RANGE(ctx, to, size); \
36-
} else if (UNLIKELY(!asan_inited)) { \
36+
} else if (UNLIKELY(!AsanInited())) { \
3737
return internal_memcpy(to, from, size); \
3838
} \
3939
return REAL(memcpy)(to, from, size); \
@@ -44,7 +44,7 @@ using namespace __asan;
4444
do { \
4545
if (LIKELY(replace_intrin_cached)) { \
4646
ASAN_WRITE_RANGE(ctx, block, size); \
47-
} else if (UNLIKELY(!asan_inited)) { \
47+
} else if (UNLIKELY(!AsanInited())) { \
4848
return internal_memset(block, c, size); \
4949
} \
5050
return REAL(memset)(block, c, size); \

compiler-rt/lib/asan/asan_internal.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,8 @@ void InstallAtExitCheckLeaks();
130130
if (&__asan_on_error) \
131131
__asan_on_error()
132132

133-
extern int asan_inited;
134-
// Used to avoid infinite recursion in __asan_init().
135-
extern bool asan_init_is_running;
133+
bool AsanInited();
134+
bool AsanInitIsRunning(); // Used to avoid infinite recursion in __asan_init().
136135
extern bool replace_intrin_cached;
137136
extern void (*death_callback)(void);
138137
// These magic values are written to shadow for better error

compiler-rt/lib/asan/asan_malloc_linux.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
using namespace __asan;
3232

3333
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
34-
static bool UseImpl() { return asan_init_is_running; }
34+
static bool UseImpl() { return AsanInitIsRunning(); }
3535
static void OnAllocate(const void *ptr, uptr size) {
3636
# if CAN_SANITIZE_LEAKS
3737
// Suppress leaks from dlerror(). Previously dlsym hack on global array was

compiler-rt/lib/asan/asan_malloc_mac.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
using namespace __asan;
2424
#define COMMON_MALLOC_ZONE_NAME "asan"
2525
#define COMMON_MALLOC_ENTER() ENSURE_ASAN_INITED()
26-
# define COMMON_MALLOC_SANITIZER_INITIALIZED asan_inited
26+
# define COMMON_MALLOC_SANITIZER_INITIALIZED AsanInited()
2727
# define COMMON_MALLOC_FORCE_LOCK() asan_mz_force_lock()
2828
# define COMMON_MALLOC_FORCE_UNLOCK() asan_mz_force_unlock()
2929
# define COMMON_MALLOC_MEMALIGN(alignment, size) \

compiler-rt/lib/asan/asan_malloc_win.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ INTERCEPTOR_WINAPI(size_t, HeapSize, HANDLE hHeap, DWORD dwFlags,
211211
// interception takes place, so if it is not owned by the RTL heap we can
212212
// pass it to the ASAN heap for inspection.
213213
if (flags()->windows_hook_rtl_allocators) {
214-
if (!asan_inited || OWNED_BY_RTL(hHeap, lpMem))
214+
if (!AsanInited() || OWNED_BY_RTL(hHeap, lpMem))
215215
return REAL(HeapSize)(hHeap, dwFlags, lpMem);
216216
} else {
217217
CHECK(dwFlags == 0 && "unsupported heap flags");
@@ -226,7 +226,7 @@ INTERCEPTOR_WINAPI(LPVOID, HeapAlloc, HANDLE hHeap, DWORD dwFlags,
226226
// If the ASAN runtime is not initialized, or we encounter an unsupported
227227
// flag, fall back to the original allocator.
228228
if (flags()->windows_hook_rtl_allocators) {
229-
if (UNLIKELY(!asan_inited ||
229+
if (UNLIKELY(!AsanInited() ||
230230
(dwFlags & HEAP_ALLOCATE_UNSUPPORTED_FLAGS) != 0)) {
231231
return REAL(HeapAlloc)(hHeap, dwFlags, dwBytes);
232232
}
@@ -297,7 +297,7 @@ void *SharedReAlloc(ReAllocFunction reallocFunc, SizeFunction heapSizeFunc,
297297

298298
// If this heap block which was allocated before the ASAN
299299
// runtime came up, use the real HeapFree function.
300-
if (UNLIKELY(!asan_inited)) {
300+
if (UNLIKELY(!AsanInited())) {
301301
return reallocFunc(hHeap, dwFlags, lpMem, dwBytes);
302302
}
303303
bool only_asan_supported_flags =
@@ -420,7 +420,7 @@ size_t RtlSizeHeap(void* HeapHandle, DWORD Flags, void* BaseAddress);
420420
INTERCEPTOR_WINAPI(size_t, RtlSizeHeap, HANDLE HeapHandle, DWORD Flags,
421421
void* BaseAddress) {
422422
if (!flags()->windows_hook_rtl_allocators ||
423-
UNLIKELY(!asan_inited || OWNED_BY_RTL(HeapHandle, BaseAddress))) {
423+
UNLIKELY(!AsanInited() || OWNED_BY_RTL(HeapHandle, BaseAddress))) {
424424
return REAL(RtlSizeHeap)(HeapHandle, Flags, BaseAddress);
425425
}
426426
GET_CURRENT_PC_BP_SP;
@@ -448,7 +448,7 @@ INTERCEPTOR_WINAPI(void*, RtlAllocateHeap, HANDLE HeapHandle, DWORD Flags,
448448
// If the ASAN runtime is not initialized, or we encounter an unsupported
449449
// flag, fall back to the original allocator.
450450
if (!flags()->windows_hook_rtl_allocators ||
451-
UNLIKELY(!asan_inited ||
451+
UNLIKELY(!AsanInited() ||
452452
(Flags & HEAP_ALLOCATE_UNSUPPORTED_FLAGS) != 0)) {
453453
return REAL(RtlAllocateHeap)(HeapHandle, Flags, Size);
454454
}

compiler-rt/lib/asan/asan_rtl.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,17 @@ static void CheckUnwind() {
7171
}
7272

7373
// -------------------------- Globals --------------------- {{{1
74-
int asan_inited;
75-
bool asan_init_is_running;
74+
static int asan_inited = 0;
75+
static int asan_init_is_running = 0;
76+
77+
void SetAsanInited(u32 val) { asan_inited = val; }
78+
79+
void SetAsanInitIsRunning(u32 val) { asan_init_is_running = val; }
80+
81+
bool AsanInited() { return asan_inited == 1; }
82+
83+
bool AsanInitIsRunning() { return asan_init_is_running == 1; }
84+
7685
bool replace_intrin_cached;
7786

7887
#if !ASAN_FIXED_MAPPING
@@ -382,11 +391,11 @@ void PrintAddressSpaceLayout() {
382391
}
383392

384393
static void AsanInitInternal() {
385-
if (LIKELY(asan_inited))
394+
if (LIKELY(AsanInited()))
386395
return;
387396
SanitizerToolName = "AddressSanitizer";
388-
CHECK(!asan_init_is_running && "ASan init calls itself!");
389-
asan_init_is_running = true;
397+
CHECK(!AsanInitIsRunning() && "ASan init calls itself!");
398+
SetAsanInitIsRunning(1);
390399

391400
CacheBinaryName();
392401

@@ -399,7 +408,7 @@ static void AsanInitInternal() {
399408
// Stop performing init at this point if we are being loaded via
400409
// dlopen() and the platform supports it.
401410
if (SANITIZER_SUPPORTS_INIT_FOR_DLOPEN && UNLIKELY(HandleDlopenInit())) {
402-
asan_init_is_running = false;
411+
SetAsanInitIsRunning(0);
403412
VReport(1, "AddressSanitizer init is being performed for dlopen().\n");
404413
return;
405414
}
@@ -461,8 +470,8 @@ static void AsanInitInternal() {
461470
// On Linux AsanThread::ThreadStart() calls malloc() that's why asan_inited
462471
// should be set to 1 prior to initializing the threads.
463472
replace_intrin_cached = flags()->replace_intrin;
464-
asan_inited = 1;
465-
asan_init_is_running = false;
473+
SetAsanInited(1);
474+
SetAsanInitIsRunning(0);
466475

467476
if (flags()->atexit)
468477
Atexit(asan_atexit);
@@ -584,7 +593,7 @@ static void UnpoisonFakeStack() {
584593
using namespace __asan;
585594

586595
void NOINLINE __asan_handle_no_return() {
587-
if (asan_init_is_running)
596+
if (AsanInitIsRunning())
588597
return;
589598

590599
if (!PlatformUnpoisonStacks())

compiler-rt/lib/asan/asan_stack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void __sanitizer::BufferedStackTrace::UnwindImpl(
5757
uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
5858
using namespace __asan;
5959
size = 0;
60-
if (UNLIKELY(!asan_inited))
60+
if (UNLIKELY(!AsanInited()))
6161
return;
6262
request_fast = StackTrace::WillUseFastUnwind(request_fast);
6363
AsanThread *t = GetCurrentThread();

0 commit comments

Comments
 (0)