Skip to content

Commit 596d534

Browse files
committed
[ASan] Stop blocking child thread progress from parent thread in pthread_create interceptor.
Previously in ASan's `pthread_create` interceptor we would block in the `pthread_create` interceptor waiting for the child thread to start. Unfortunately this has bad performance characteristics because the OS scheduler doesn't know the relationship between the parent and child thread (i.e. the parent thread cannot make progress until the child thread makes progress) and may make the wrong scheduling decision which stalls progress. It turns out that ASan didn't use to block in this interceptor but was changed to do so to try to address http://llvm.org/bugs/show_bug.cgi?id=21621/. In that bug the problem being addressed was a LeakSanitizer false positive. That bug concerns a heap object being passed as `arg` to `pthread_create`. If: * The calling thread loses a live reference to the object (e.g. `pthread_create` finishes and the thread no longer has a live reference to the object). * Leak checking is triggered. * The child thread has not yet started (once it starts it will have a live reference). then the heap object will incorrectly appear to be leaked. This bug is covered by the `lsan/TestCases/leak_check_before_thread_started.cpp` test case. In b029c51 ASan was changed to block in `pthread_create()` until the child thread starts so that `arg` is kept alive for the purposes of leaking check. While this change "works" its problematic due to the performance problems it causes. The change is also completely unnecessary if leak checking is disabled (via detect_leaks runtime option or CAN_SANITIZE_LEAKS compile time config). This patch does two things: 1. Takes a different approach to solving the leak false positive by making LSan's leak checking mechanism treat the `arg` pointer of created but not started threads as reachable. This is done by implementing the `ForEachRegisteredThreadContextCb` callback for ASan. 2. Removes the blocking behaviour in the ASan `pthread_create` interceptor. rdar://problem/63537240 Differential Revision: https://reviews.llvm.org/D95184
1 parent 49231c1 commit 596d534

4 files changed

Lines changed: 42 additions & 42 deletions

File tree

compiler-rt/lib/asan/asan_allocator.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,30 @@ IgnoreObjectResult IgnoreObjectLocked(const void *p) {
11851185
}
11861186

11871187
void GetAdditionalThreadContextPtrs(ThreadContextBase *tctx, void *ptrs) {
1188-
// This function can be used to treat memory reachable from `tctx` as live.
1189-
// This is useful for threads that have been created but not yet started.
1190-
1191-
// This is currently a no-op because the ASan `pthread_create()` interceptor
1192-
// blocks until the child thread starts which keeps the thread's `arg` pointer
1193-
// live.
1188+
// Look for the arg pointer of threads that have been created or are running.
1189+
// This is necessary to prevent false positive leaks due to the AsanThread
1190+
// holding the only live reference to a heap object. This can happen because
1191+
// the `pthread_create()` interceptor doesn't wait for the child thread to
1192+
// start before returning and thus loosing the the only live reference to the
1193+
// heap object on the stack.
1194+
1195+
__asan::AsanThreadContext *atctx =
1196+
reinterpret_cast<__asan::AsanThreadContext *>(tctx);
1197+
__asan::AsanThread *asan_thread = atctx->thread;
1198+
1199+
// Note ThreadStatusRunning is required because there is a small window where
1200+
// the thread status switches to `ThreadStatusRunning` but the `arg` pointer
1201+
// still isn't on the stack yet.
1202+
if (atctx->status != ThreadStatusCreated &&
1203+
atctx->status != ThreadStatusRunning)
1204+
return;
1205+
1206+
uptr thread_arg = reinterpret_cast<uptr>(asan_thread->get_arg());
1207+
if (!thread_arg)
1208+
return;
1209+
1210+
auto ptrsVec = reinterpret_cast<InternalMmapVector<uptr> *>(ptrs);
1211+
ptrsVec->push_back(thread_arg);
11941212
}
11951213

11961214
} // namespace __lsan

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -189,20 +189,11 @@ DECLARE_REAL_AND_INTERCEPTOR(void, free, void *)
189189
#include "sanitizer_common/sanitizer_common_syscalls.inc"
190190
#include "sanitizer_common/sanitizer_syscalls_netbsd.inc"
191191

192-
struct ThreadStartParam {
193-
atomic_uintptr_t t;
194-
atomic_uintptr_t is_registered;
195-
};
196-
197192
#if ASAN_INTERCEPT_PTHREAD_CREATE
198193
static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
199-
ThreadStartParam *param = reinterpret_cast<ThreadStartParam *>(arg);
200-
AsanThread *t = nullptr;
201-
while ((t = reinterpret_cast<AsanThread *>(
202-
atomic_load(&param->t, memory_order_acquire))) == nullptr)
203-
internal_sched_yield();
194+
AsanThread *t = (AsanThread *)arg;
204195
SetCurrentThread(t);
205-
return t->ThreadStart(GetTid(), &param->is_registered);
196+
return t->ThreadStart(GetTid());
206197
}
207198

208199
INTERCEPTOR(int, pthread_create, void *thread,
@@ -215,9 +206,11 @@ INTERCEPTOR(int, pthread_create, void *thread,
215206
int detached = 0;
216207
if (attr)
217208
REAL(pthread_attr_getdetachstate)(attr, &detached);
218-
ThreadStartParam param;
219-
atomic_store(&param.t, 0, memory_order_relaxed);
220-
atomic_store(&param.is_registered, 0, memory_order_relaxed);
209+
210+
u32 current_tid = GetCurrentTidOrInvalid();
211+
AsanThread *t =
212+
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
213+
221214
int result;
222215
{
223216
// Ignore all allocations made by pthread_create: thread stack/TLS may be
@@ -227,21 +220,13 @@ INTERCEPTOR(int, pthread_create, void *thread,
227220
#if CAN_SANITIZE_LEAKS
228221
__lsan::ScopedInterceptorDisabler disabler;
229222
#endif
230-
result = REAL(pthread_create)(thread, attr, asan_thread_start, &param);
223+
result = REAL(pthread_create)(thread, attr, asan_thread_start, t);
231224
}
232-
if (result == 0) {
233-
u32 current_tid = GetCurrentTidOrInvalid();
234-
AsanThread *t =
235-
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
236-
atomic_store(&param.t, reinterpret_cast<uptr>(t), memory_order_release);
237-
// Wait until the AsanThread object is initialized and the ThreadRegistry
238-
// entry is in "started" state. One reason for this is that after this
239-
// interceptor exits, the child thread's stack may be the only thing holding
240-
// the |arg| pointer. This may cause LSan to report a leak if leak checking
241-
// happens at a point when the interceptor has already exited, but the stack
242-
// range for the child thread is not yet known.
243-
while (atomic_load(&param.is_registered, memory_order_acquire) == 0)
244-
internal_sched_yield();
225+
if (result != 0) {
226+
// If the thread didn't start delete the AsanThread to avoid leaking it.
227+
// Note AsanThreadContexts never get destroyed so the AsanThreadContext
228+
// that was just created for the AsanThread is wasted.
229+
t->Destroy();
245230
}
246231
return result;
247232
}

compiler-rt/lib/asan/asan_thread.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,9 @@ void AsanThread::Init(const InitOptions *options) {
253253
// SetThreadStackAndTls.
254254
#if !SANITIZER_FUCHSIA && !SANITIZER_RTEMS
255255

256-
thread_return_t AsanThread::ThreadStart(
257-
tid_t os_id, atomic_uintptr_t *signal_thread_is_registered) {
256+
thread_return_t AsanThread::ThreadStart(tid_t os_id) {
258257
Init();
259258
asanThreadRegistry().StartThread(tid(), os_id, ThreadType::Regular, nullptr);
260-
if (signal_thread_is_registered)
261-
atomic_store(signal_thread_is_registered, 1, memory_order_release);
262259

263260
if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
264261

@@ -288,8 +285,7 @@ AsanThread *CreateMainThread() {
288285
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ 0,
289286
/* stack */ nullptr, /* detached */ true);
290287
SetCurrentThread(main_thread);
291-
main_thread->ThreadStart(internal_getpid(),
292-
/* signal_thread_is_registered */ nullptr);
288+
main_thread->ThreadStart(internal_getpid());
293289
return main_thread;
294290
}
295291

compiler-rt/lib/asan/asan_thread.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ class AsanThread {
6969
struct InitOptions;
7070
void Init(const InitOptions *options = nullptr);
7171

72-
thread_return_t ThreadStart(tid_t os_id,
73-
atomic_uintptr_t *signal_thread_is_registered);
72+
thread_return_t ThreadStart(tid_t os_id);
7473

7574
uptr stack_top();
7675
uptr stack_bottom();
@@ -132,6 +131,8 @@ class AsanThread {
132131

133132
void *extra_spill_area() { return &extra_spill_area_; }
134133

134+
void *get_arg() { return arg_; }
135+
135136
private:
136137
// NOTE: There is no AsanThread constructor. It is allocated
137138
// via mmap() and *must* be valid in zero-initialized state.

0 commit comments

Comments
 (0)