[compiler-rt] r342652 - [winasan] Unpoison the stack in NtTerminateThread

David Major via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 20 07:59:33 PDT 2018


Author: dmajor
Date: Thu Sep 20 07:59:33 2018
New Revision: 342652

URL: http://llvm.org/viewvc/llvm-project?rev=342652&view=rev
Log:
[winasan] Unpoison the stack in NtTerminateThread

In long-running builds we've seen some ASan complaints during thread creation that we suspect are due to leftover poisoning from previous threads whose stacks occupied that memory. This patch adds a hook that unpoisons the stack just before the NtTerminateThread syscall.

Differential Revision: https://reviews.llvm.org/D52091


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

Modified: compiler-rt/trunk/lib/asan/asan_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_win.cc?rev=342652&r1=342651&r2=342652&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_win.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_win.cc Thu Sep 20 07:59:33 2018
@@ -154,6 +154,14 @@ INTERCEPTOR_WINAPI(DWORD, CreateThread,
                             asan_thread_start, t, thr_flags, tid);
 }
 
+INTERCEPTOR_WINAPI(void, NtTerminateThread, void *rcx) {
+  // Unpoison the terminating thread's stack because the memory may be re-used.
+  NT_TIB *tib = (NT_TIB *)NtCurrentTeb();
+  uptr stackSize = (uptr)tib->StackBase - (uptr)tib->StackLimit;
+  __asan_unpoison_memory_region(tib->StackLimit, stackSize);
+  return REAL(NtTerminateThread(rcx));
+}
+
 // }}}
 
 namespace __asan {
@@ -161,7 +169,9 @@ namespace __asan {
 void InitializePlatformInterceptors() {
   ASAN_INTERCEPT_FUNC(CreateThread);
   ASAN_INTERCEPT_FUNC(SetUnhandledExceptionFilter);
-
+  CHECK(::__interception::OverrideFunction("NtTerminateThread",
+                                           (uptr)WRAP(NtTerminateThread),
+                                           (uptr *)&REAL(NtTerminateThread)));
 #ifdef _WIN64
   ASAN_INTERCEPT_FUNC(__C_specific_handler);
 #else




More information about the llvm-commits mailing list