1 [winasan] Unpoison the stack in NtTerminateThread
3 In long-running builds we've seen some ASan complaints during thread creation
4 that we suspect are due to leftover poisoning from previous threads whose stacks
5 occupied that memory. This patch adds a hook that unpoisons the stack just
6 before the NtTerminateThread syscall.
8 Differential Revision: https://reviews.llvm.org/D52091
10 ** Update for clang 9 ** : After some backouts, this patch eventually landed
11 upstream in a different form, as the TLS handler `asan_thread_exit`, but that
12 variant causes failures in our test suite, so revert the TLS handler in favor of
13 the interceptor approach from the first patch.
15 --- a/compiler-rt/lib/asan/asan_win.cc
16 +++ b/compiler-rt/lib/asan/asan_win.cc
21 +INTERCEPTOR_WINAPI(void, NtTerminateThread, void *rcx) {
22 + // Unpoison the terminating thread's stack because the memory may be re-used.
23 + NT_TIB *tib = (NT_TIB *)NtCurrentTeb();
24 + uptr stackSize = (uptr)tib->StackBase - (uptr)tib->StackLimit;
25 + __asan_unpoison_memory_region(tib->StackLimit, stackSize);
26 + return REAL(NtTerminateThread(rcx));
34 ASAN_INTERCEPT_FUNC(CreateThread);
35 ASAN_INTERCEPT_FUNC(SetUnhandledExceptionFilter);
37 + CHECK(::__interception::OverrideFunction("NtTerminateThread",
38 + (uptr)WRAP(NtTerminateThread),
39 + (uptr *)&REAL(NtTerminateThread)));
41 ASAN_INTERCEPT_FUNC(__C_specific_handler);
44 void *, unsigned long, void *) = asan_thread_init;
47 -static void NTAPI asan_thread_exit(void *module, DWORD reason, void *reserved) {
48 - if (reason == DLL_THREAD_DETACH) {
49 - // Unpoison the thread's stack because the memory may be re-used.
50 - NT_TIB *tib = (NT_TIB *)NtCurrentTeb();
51 - uptr stackSize = (uptr)tib->StackBase - (uptr)tib->StackLimit;
52 - __asan_unpoison_memory_region(tib->StackLimit, stackSize);
56 -#pragma section(".CRT$XLY", long, read) // NOLINT
57 -__declspec(allocate(".CRT$XLY")) void(NTAPI *__asan_tls_exit)(
58 - void *, unsigned long, void *) = asan_thread_exit;
60 WIN_FORCE_LINK(__asan_dso_reg_hook)