[compiler-rt] r285600 - [ASan] Initialize cloned dynamic runtime global variables earlier

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 31 10:13:02 PDT 2016


Author: rnk
Date: Mon Oct 31 12:13:02 2016
New Revision: 285600

URL: http://llvm.org/viewvc/llvm-project?rev=285600&view=rev
Log:
[ASan] Initialize cloned dynamic runtime global variables earlier

Otherwise __asan_dynamic_memory_address will be zero during static
initialization and instrumented code will crash immediately.

Fixes PR30810

Patch by David Major

Modified:
    compiler-rt/trunk/lib/asan/asan_win_dynamic_runtime_thunk.cc

Modified: compiler-rt/trunk/lib/asan/asan_win_dynamic_runtime_thunk.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win_dynamic_runtime_thunk.cc?rev=285600&r1=285599&r2=285600&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_win_dynamic_runtime_thunk.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_win_dynamic_runtime_thunk.cc Mon Oct 31 12:13:02 2016
@@ -28,6 +28,7 @@
 #include <windows.h>
 
 // First, declare CRT sections we'll be using in this file
+#pragma section(".CRT$XIB", long, read)  // NOLINT
 #pragma section(".CRT$XID", long, read)  // NOLINT
 #pragma section(".CRT$XCAB", long, read)  // NOLINT
 #pragma section(".CRT$XTW", long, read)  // NOLINT
@@ -46,14 +47,24 @@
 // after initialization anyways.
 extern "C" {
 __declspec(dllimport) int __asan_should_detect_stack_use_after_return();
-int __asan_option_detect_stack_use_after_return =
-    __asan_should_detect_stack_use_after_return();
+int __asan_option_detect_stack_use_after_return;
 
 __declspec(dllimport) void* __asan_get_shadow_memory_dynamic_address();
-void* __asan_shadow_memory_dynamic_address =
+void* __asan_shadow_memory_dynamic_address;
+}
+
+static int InitializeClonedVariables() {
+  __asan_option_detect_stack_use_after_return =
+    __asan_should_detect_stack_use_after_return();
+  __asan_shadow_memory_dynamic_address =
     __asan_get_shadow_memory_dynamic_address();
+  return 0;
 }
 
+// Our cloned variables must be initialized before C/C++ constructors.
+__declspec(allocate(".CRT$XIB"))
+int (*__asan_initialize_cloned_variables)() = InitializeClonedVariables;
+
 ////////////////////////////////////////////////////////////////////////////////
 // For some reason, the MD CRT doesn't call the C/C++ terminators during on DLL
 // unload or on exit.  ASan relies on LLVM global_dtors to call




More information about the llvm-commits mailing list