Skip to content

Commit 5aa2f8c

Browse files
authored
[NFC][ASAN] Replace AsanInitIsRunning with TryAsanInitFromRtl
Reviewers: zacklj89 Reviewed By: zacklj89 Pull Request: #74171
1 parent 6066530 commit 5aa2f8c

File tree

4 files changed

+30
-23
lines changed

4 files changed

+30
-23
lines changed

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ 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 (AsanInitIsRunning()) \
103-
return REAL(func)(__VA_ARGS__); \
104-
if (SANITIZER_APPLE && UNLIKELY(!AsanInited())) \
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 constexpr (SANITIZER_APPLE) { \
103+
if (UNLIKELY(!AsanInited())) \
104+
return REAL(func)(__VA_ARGS__); \
105+
} else { \
106+
if (!TryAsanInitFromRtl()) \
107+
return REAL(func)(__VA_ARGS__); \
108+
} \
107109
} while (false)
108110
#define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \
109111
do { \
@@ -534,16 +536,16 @@ INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) {
534536
INTERCEPTOR(char *, strcpy, char *to, const char *from) {
535537
void *ctx;
536538
ASAN_INTERCEPTOR_ENTER(ctx, strcpy);
537-
#if SANITIZER_APPLE
538-
if (UNLIKELY(!AsanInited()))
539-
return REAL(strcpy)(to, from);
540-
#endif
541-
// strcpy is called from malloc_default_purgeable_zone()
542-
// in __asan::ReplaceSystemAlloc() on Mac.
543-
if (AsanInitIsRunning()) {
544-
return REAL(strcpy)(to, from);
539+
if constexpr (SANITIZER_APPLE) {
540+
// strcpy is called from malloc_default_purgeable_zone()
541+
// in __asan::ReplaceSystemAlloc() on Mac.
542+
if (UNLIKELY(!AsanInited()))
543+
return REAL(strcpy)(to, from);
544+
} else {
545+
if (!TryAsanInitFromRtl())
546+
return REAL(strcpy)(to, from);
545547
}
546-
ENSURE_ASAN_INITED();
548+
547549
if (flags()->replace_str) {
548550
uptr from_size = internal_strlen(from) + 1;
549551
CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size);

compiler-rt/lib/asan/asan_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class AsanThread;
6060
using __sanitizer::StackTrace;
6161

6262
void AsanInitFromRtl();
63+
bool TryAsanInitFromRtl();
6364

6465
// asan_win.cpp
6566
void InitializePlatformExceptionHandlers();
@@ -131,7 +132,6 @@ void InstallAtExitCheckLeaks();
131132
__asan_on_error()
132133

133134
bool AsanInited();
134-
bool AsanInitIsRunning(); // Used to avoid infinite recursion in __asan_init().
135135
extern bool replace_intrin_cached;
136136
extern void (*death_callback)(void);
137137
// These magic values are written to shadow for better error

compiler-rt/lib/asan/asan_malloc_linux.cpp

Lines changed: 1 addition & 4 deletions
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 AsanInitIsRunning(); }
34+
static bool UseImpl() { return !TryAsanInitFromRtl(); }
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
@@ -65,23 +65,20 @@ INTERCEPTOR(void, cfree, void *ptr) {
6565
INTERCEPTOR(void*, malloc, uptr size) {
6666
if (DlsymAlloc::Use())
6767
return DlsymAlloc::Allocate(size);
68-
ENSURE_ASAN_INITED();
6968
GET_STACK_TRACE_MALLOC;
7069
return asan_malloc(size, &stack);
7170
}
7271

7372
INTERCEPTOR(void*, calloc, uptr nmemb, uptr size) {
7473
if (DlsymAlloc::Use())
7574
return DlsymAlloc::Callocate(nmemb, size);
76-
ENSURE_ASAN_INITED();
7775
GET_STACK_TRACE_MALLOC;
7876
return asan_calloc(nmemb, size, &stack);
7977
}
8078

8179
INTERCEPTOR(void*, realloc, void *ptr, uptr size) {
8280
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
8381
return DlsymAlloc::Realloc(ptr, size);
84-
ENSURE_ASAN_INITED();
8582
GET_STACK_TRACE_MALLOC;
8683
return asan_realloc(ptr, size, &stack);
8784
}

compiler-rt/lib/asan/asan_rtl.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static void SetAsanInitIsRunning(u32 val) { asan_init_is_running = val; }
8080

8181
bool AsanInited() { return asan_inited == 1; }
8282

83-
bool AsanInitIsRunning() { return asan_init_is_running == 1; }
83+
static bool AsanInitIsRunning() { return asan_init_is_running == 1; }
8484

8585
bool replace_intrin_cached;
8686

@@ -525,6 +525,14 @@ void AsanInitFromRtl() {
525525
AsanInitInternal();
526526
}
527527

528+
bool TryAsanInitFromRtl() {
529+
if (UNLIKELY(AsanInitIsRunning()))
530+
return false;
531+
if (UNLIKELY(!AsanInited()))
532+
AsanInitInternal();
533+
return true;
534+
}
535+
528536
#if ASAN_DYNAMIC
529537
// Initialize runtime in case it's LD_PRELOAD-ed into unsanitized executable
530538
// (and thus normal initializers from .preinit_array or modules haven't run).

0 commit comments

Comments
 (0)