[compiler-rt] r210030 - [ASan Win] Manually call __asan_init early in the DLL initialization process to avoid a null function call in cout/cerr constructors

Timur Iskhodzhanov timurrrr at google.com
Mon Jun 2 07:40:07 PDT 2014


Author: timurrrr
Date: Mon Jun  2 09:40:07 2014
New Revision: 210030

URL: http://llvm.org/viewvc/llvm-project?rev=210030&view=rev
Log:
[ASan Win] Manually call __asan_init early in the DLL initialization process to avoid a null function call in cout/cerr constructors

Added:
    compiler-rt/trunk/test/asan/TestCases/Windows/dll_cerr.cc
Modified:
    compiler-rt/trunk/lib/asan/asan_dll_thunk.cc

Modified: compiler-rt/trunk/lib/asan/asan_dll_thunk.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_dll_thunk.cc?rev=210030&r1=210029&r2=210030&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_dll_thunk.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_dll_thunk.cc Mon Jun  2 09:40:07 2014
@@ -325,9 +325,24 @@ INTERCEPT_LIBRARY_FUNCTION(strnlen);
 INTERCEPT_LIBRARY_FUNCTION(strtol);
 INTERCEPT_LIBRARY_FUNCTION(wcslen);
 
-// Must be at the end of the file due to the way INTERCEPT_HOOKS is defined.
+// Must be after all the interceptor declarations due to the way INTERCEPT_HOOKS
+// is defined.
 void InterceptHooks() {
   INTERCEPT_HOOKS();
 }
 
+// We want to call __asan_init before C/C++ initializers/constructors are
+// executed, otherwise functions like memset might be invoked.
+// For some strange reason, merely linking in asan_preinit.cc doesn't work
+// as the callback is never called...  Is link.exe doing something too smart?
+
+// In DLLs, the callbacks are expected to return 0,
+// otherwise CRT initialization fails.
+static int call_asan_init() {
+  __asan_init_v3();
+  return 0;
+}
+#pragma section(".CRT$XIB", long, read)  // NOLINT
+__declspec(allocate(".CRT$XIB")) int (*__asan_preinit)() = call_asan_init;
+
 #endif // ASAN_DLL_THUNK

Added: compiler-rt/trunk/test/asan/TestCases/Windows/dll_cerr.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/Windows/dll_cerr.cc?rev=210030&view=auto
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/Windows/dll_cerr.cc (added)
+++ compiler-rt/trunk/test/asan/TestCases/Windows/dll_cerr.cc Mon Jun  2 09:40:07 2014
@@ -0,0 +1,23 @@
+// RUN: %clang_cl_asan -O0 %p/dll_host.cc -Fe%t
+// RUN: %clang_cl_asan -LD -O0 %s -Fe%t.dll
+// RUN: %run %t %t.dll 2>&1 | FileCheck %s
+
+// Test that it works correctly even with ICF enabled.
+// RUN: %clang_cl_asan -LD -O0 %s -Fe%t.dll -link /OPT:REF /OPT:ICF
+// RUN: %run %t %t.dll 2>&1 | FileCheck %s
+
+#include <iostream>
+
+extern "C" __declspec(dllexport)
+int test_function() {
+  // Just make sure we can use cout.
+  std::cout << "All ok\n";
+// CHECK: All ok
+
+  // This line forces a declaration of some global basic_ostream internal object that
+  // calls memcpy() in its constructor.  This doesn't work if __asan_init is not
+  // called early enough.
+  std::cout << 42;
+// CHECK: 42
+  return 0;
+}





More information about the llvm-commits mailing list