build/build-clang/r342652-unpoison-thread-stacks.patch
author Narcis Beleuzu <nbeleuzu@mozilla.com>
Fri, 21 Dec 2018 05:59:24 +0200
changeset 451643 644731a71be81b31a049deee70170af3ca36efa1
parent 451257 585f7d2135eeaddf35d239c909ef43ffa37fa860
permissions -rw-r--r--
Backed out 2 changesets (bug 1485016, bug 1512921) as requested by tjr. Backed out changeset 61ae84746b34 (bug 1485016) Backed out changeset 585f7d2135ee (bug 1512921)

[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

--- a/compiler-rt/lib/asan/asan_win.cc	(revision 342651)
+++ b/compiler-rt/lib/asan/asan_win.cc	(revision 342652)
@@ -154,6 +154,14 @@
                             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 @@
 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