[PATCH] D26473: [asan/win] Delay load dbghelp.dll to delay ucrtbase.dll initialization

Reid Kleckner via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 9 14:05:34 PST 2016


rnk created this revision.
rnk added a reviewer: etienneb.
rnk added a subscriber: llvm-commits.
Herald added subscribers: mgorny, kubabrecka.

ASan needs to initialize before ucrtbase.dll so that it can intercept all of
its heap allocations. New versions of dbghelp.dll depend on ucrtbase.dll, which
means both of those DLLs will initialize before the dynamic ASan runtime. With
delayloading, we delay the ucrtbase initialization and can intercept its
allocations.

Eventually, I would like to remove our dbghelp.dll dependency in favor of
always using llvm-symbolizer.exe, but this seems like an acceptable interim
solution.

Fixes PR30903


https://reviews.llvm.org/D26473

Files:
  lib/asan/CMakeLists.txt
  test/asan/TestCases/Windows/Inputs/delay_dbghelp/dbghelp.dll
  test/asan/TestCases/Windows/Inputs/delay_dbghelp/ucrtbase.dll
  test/asan/TestCases/Windows/delay_dbghelp.cc


Index: test/asan/TestCases/Windows/delay_dbghelp.cc
===================================================================
--- /dev/null
+++ test/asan/TestCases/Windows/delay_dbghelp.cc
@@ -0,0 +1,19 @@
+// Make a directory, copy the problematic copies of dbg*.dll into that
+// directory, build an exe there, and force the loader to find dbghelp.dll there
+// instead of searching in C:/Windows/system32 first. Touching t.exe.local does
+// this, according to MSDN:
+// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682600(v=vs.85).aspx
+//
+// RUN: rm -rf %t.dir && mkdir %t.dir && cd %t.dir
+// RUN: find %/S/Inputs/delay_dbghelp -iname '*.dll' | xargs cp -t .
+// RUN: touch t.exe.local
+// RUN: %clang_cl_asan %s -Fet.exe
+// RUN: %run %t.dir/t.exe
+
+// REQUIRES: asan-64-bits
+
+extern "C" int puts(const char *);
+
+int main() {
+  puts("main");
+}
Index: lib/asan/CMakeLists.txt
===================================================================
--- lib/asan/CMakeLists.txt
+++ lib/asan/CMakeLists.txt
@@ -64,6 +64,14 @@
   -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
 
+# Delay load dbghelp. Newer versions of dbghelp depend on ucrtbase.dll. We
+# can't allow ucrtbase.dll to initialize before ASan, or some heap allocations
+# will not be intercepted (http://llvm.org/pr30903). The loader initializes
+# dependencies before dependent DLLs, so we can't have a direct dependence on
+# dbghelp.
+append_list_if(MSVC -delayload:dbghelp.dll ASAN_DYNAMIC_LINK_FLAGS)
+append_list_if(MSVC -defaultlib:delayimp.lib ASAN_DYNAMIC_LINK_FLAGS)
+
 append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D26473.77393.patch
Type: text/x-patch
Size: 1789 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161109/5f77ffa9/attachment.bin>


More information about the llvm-commits mailing list