Skip to content

Commit 7d1e85f

Browse files
39otsumacgyvercodilla
authored andcommitted
Automerge: [ASan] Ensure Symbolize Flag setting on Windows through __asan_default_options() is maintained throughout runtime (#132811)
As a consequence of the ASAN DLL's initialization process on Windows, some flags defined by the user through overriding the __asan_default_options() method will not be honored. More information here: [#117925](llvm/llvm-project#117925) This PR aims to alleviate this for the symbolize flag in relation to this user's concern [here.](https://developercommunity.visualstudio.com/t/Overloading-of-__asan_default_options-/10688871) 1. Declared `Symbolizer::ClearTools()`. 2. Defined `Symbolizer::ClearTools()`. Upon invocation of the weak function callback of `__asan_default_options()`, `Symbolizer::tools_` will be cleared if the user specifies `symbolize=0`. 3. Added tests. --------- Co-authored-by: MacGyver Codilla <[email protected]>
2 parents 45b1e37 + 7bf43fe commit 7d1e85f

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

compiler-rt/lib/asan/asan_flags.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ void InitializeFlags() {
241241
InitializeDefaultFlags();
242242
ProcessFlags();
243243
ApplyFlags();
244+
if (!common_flags()->symbolize)
245+
Symbolizer::ClearTools();
244246
});
245247

246248
# if CAN_SANITIZE_UB

compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class Symbolizer final {
136136
/// (if it wasn't already initialized).
137137
static Symbolizer *GetOrInit();
138138
static void LateInitialize();
139+
static void ClearTools();
140+
139141
// Returns a list of symbolized frames for a given address (containing
140142
// all inlined functions, if necessary).
141143
SymbolizedStack *SymbolizePC(uptr address);

compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ Symbolizer *Symbolizer::GetOrInit() {
2626
return symbolizer_;
2727
}
2828

29+
// If the 'symbolize' flag is set to 0, it clears the tools
30+
// associated with the symbolizer to prevent unnecessary symbolization and
31+
// resource usage. This is necessary because of the late binding of the
32+
// overridden method, __asan_default_options().
33+
void Symbolizer::ClearTools() {
34+
SpinMutexLock l(&init_mu_);
35+
if (symbolizer_)
36+
symbolizer_->tools_.clear();
37+
}
38+
2939
// See sanitizer_symbolizer_markup.cpp.
3040
#if !SANITIZER_SYMBOLIZER_MARKUP
3141

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// RUN: %clangxx_asan -O0 %s -o %t
2+
// RUN: %env_asan_opts=symbolize=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
3+
// RUN: %env_asan_opts=symbolize=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
4+
5+
// RUN: %clangxx_asan -O0 %s -o %t -DUSER_FUNCTION_OFF
6+
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
7+
// RUN: %env_asan_opts=symbolize=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
8+
// RUN: %env_asan_opts=symbolize=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
9+
10+
// RUN: %clangxx_asan -O0 %s -o %t -DUSER_FUNCTION_ON
11+
// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
12+
// RUN: %env_asan_opts=symbolize=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
13+
// RUN: %env_asan_opts=symbolize=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
14+
#if USER_FUNCTION_OFF
15+
16+
extern "C" __declspec(dllexport) extern const char *__asan_default_options() {
17+
return "symbolize=0";
18+
}
19+
20+
#endif
21+
22+
#if USER_FUNCTION_ON
23+
24+
extern "C" __declspec(dllexport) extern const char *__asan_default_options() {
25+
return "symbolize=1";
26+
}
27+
28+
#endif
29+
30+
#include <cstdio>
31+
#include <cstdlib>
32+
33+
volatile static int heapBufferOverflowValue = 10;
34+
int main() {
35+
int *array = new int[10];
36+
heapBufferOverflowValue = array[10]; // CHECK-SYMBOLIZE-ON: symbolize.cpp:36
37+
return 0; // CHECK-SYMBOLIZE-OFF: symbolize.cpp.tmp+0x
38+
}

0 commit comments

Comments
 (0)