Skip to content

Commit 1594fa8

Browse files
authored
[asan][win] Fix CreateThread leak (llvm#126738)
Fix llvm#126541 Since ```t->Destroy``` cannot be called after ```start_routine```(When calling standard thread_start in crt) Intercept `ExitThread` and free the memory created by `VirtualAlloc'
1 parent 9e257b0 commit 1594fa8

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

compiler-rt/lib/asan/asan_win.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
145145
t->GetStartData(params);
146146

147147
auto res = (*params.start_routine)(params.arg);
148-
t->Destroy(); // POSIX calls this from TSD destructor.
149148
return res;
150149
}
151150

@@ -166,6 +165,13 @@ INTERCEPTOR_WINAPI(HANDLE, CreateThread, LPSECURITY_ATTRIBUTES security,
166165
thr_flags, tid);
167166
}
168167

168+
INTERCEPTOR_WINAPI(void, ExitThread, DWORD dwExitCode) {
169+
AsanThread *t = (AsanThread *)__asan::GetCurrentThread();
170+
if (t)
171+
t->Destroy();
172+
REAL(ExitThread)(dwExitCode);
173+
}
174+
169175
// }}}
170176

171177
namespace __asan {
@@ -181,6 +187,7 @@ void InitializePlatformInterceptors() {
181187
(LPCWSTR)&InitializePlatformInterceptors, &pinned));
182188

183189
ASAN_INTERCEPT_FUNC(CreateThread);
190+
ASAN_INTERCEPT_FUNC(ExitThread);
184191
ASAN_INTERCEPT_FUNC(SetUnhandledExceptionFilter);
185192

186193
#ifdef _WIN64

0 commit comments

Comments
 (0)