[compiler-rt] 7bf43fe - [ASan] Ensure Symbolize Flag setting on Windows through __asan_default_options() is maintained throughout runtime (#132811)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 9 16:24:16 PDT 2025


Author: MacGyver Codilla
Date: 2025-08-09T16:24:12-07:00
New Revision: 7bf43fec3aa1c7bb64749efefe0f5fd1e377c1fd

URL: https://github.com/llvm/llvm-project/commit/7bf43fec3aa1c7bb64749efefe0f5fd1e377c1fd
DIFF: https://github.com/llvm/llvm-project/commit/7bf43fec3aa1c7bb64749efefe0f5fd1e377c1fd.diff

LOG: [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](https://github.com/llvm/llvm-project/issues/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 <mcodilla at microsoft.com>

Added: 
    compiler-rt/test/asan/TestCases/Windows/symbolize.cpp

Modified: 
    compiler-rt/lib/asan/asan_flags.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
    compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/asan/asan_flags.cpp b/compiler-rt/lib/asan/asan_flags.cpp
index 190a89345dd18..f222ec04a7728 100644
--- a/compiler-rt/lib/asan/asan_flags.cpp
+++ b/compiler-rt/lib/asan/asan_flags.cpp
@@ -241,6 +241,8 @@ void InitializeFlags() {
         InitializeDefaultFlags();
         ProcessFlags();
         ApplyFlags();
+        if (!common_flags()->symbolize)
+          Symbolizer::ClearTools();
       });
 
 #  if CAN_SANITIZE_UB

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
index bd89dc4e302fc..a98194c81d157 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer.h
@@ -136,6 +136,8 @@ class Symbolizer final {
   /// (if it wasn't already initialized).
   static Symbolizer *GetOrInit();
   static void LateInitialize();
+  static void ClearTools();
+
   // Returns a list of symbolized frames for a given address (containing
   // all inlined functions, if necessary).
   SymbolizedStack *SymbolizePC(uptr address);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cpp
index 565701c85d978..ce3890e06e64d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_symbolizer_libcdep.cpp
@@ -26,6 +26,16 @@ Symbolizer *Symbolizer::GetOrInit() {
   return symbolizer_;
 }
 
+// If the 'symbolize' flag is set to 0, it clears the tools
+// associated with the symbolizer to prevent unnecessary symbolization and
+// resource usage. This is necessary because of the late binding of the
+// overridden method, __asan_default_options().
+void Symbolizer::ClearTools() {
+  SpinMutexLock l(&init_mu_);
+  if (symbolizer_)
+    symbolizer_->tools_.clear();
+}
+
 // See sanitizer_symbolizer_markup.cpp.
 #if !SANITIZER_SYMBOLIZER_MARKUP
 

diff  --git a/compiler-rt/test/asan/TestCases/Windows/symbolize.cpp b/compiler-rt/test/asan/TestCases/Windows/symbolize.cpp
new file mode 100644
index 0000000000000..5e0b1a4c40dd5
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/Windows/symbolize.cpp
@@ -0,0 +1,38 @@
+// RUN: %clangxx_asan -O0 %s -o %t
+// RUN: %env_asan_opts=symbolize=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
+// RUN: %env_asan_opts=symbolize=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
+
+// RUN: %clangxx_asan -O0 %s -o %t -DUSER_FUNCTION_OFF
+// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
+// RUN: %env_asan_opts=symbolize=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
+// RUN: %env_asan_opts=symbolize=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
+
+// RUN: %clangxx_asan -O0 %s -o %t -DUSER_FUNCTION_ON
+// RUN: not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
+// RUN: %env_asan_opts=symbolize=0 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-OFF
+// RUN: %env_asan_opts=symbolize=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-SYMBOLIZE-ON
+#if USER_FUNCTION_OFF
+
+extern "C" __declspec(dllexport) extern const char *__asan_default_options() {
+  return "symbolize=0";
+}
+
+#endif
+
+#if USER_FUNCTION_ON
+
+extern "C" __declspec(dllexport) extern const char *__asan_default_options() {
+  return "symbolize=1";
+}
+
+#endif
+
+#include <cstdio>
+#include <cstdlib>
+
+volatile static int heapBufferOverflowValue = 10;
+int main() {
+  int *array = new int[10];
+  heapBufferOverflowValue = array[10]; // CHECK-SYMBOLIZE-ON: symbolize.cpp:36
+  return 0; // CHECK-SYMBOLIZE-OFF: symbolize.cpp.tmp+0x
+}


        


More information about the llvm-commits mailing list