[PATCH] D95184: [ASan] Stop blocking child thread progress from parent thread in `pthread_create` interceptor.

Dan Liew via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 21 16:19:15 PST 2021


delcypher created this revision.
delcypher added reviewers: kubamracek, yln, kcc, dvyukov, eugenis, vitalybuka, cryptoad, earthdok.
Herald added a subscriber: jfb.
delcypher requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

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 b029c5101fb49b3577a1c322f42ef9fc616f25bf 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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95184

Files:
  compiler-rt/lib/asan/asan_allocator.cpp
  compiler-rt/lib/asan/asan_interceptors.cpp
  compiler-rt/lib/asan/asan_thread.cpp
  compiler-rt/lib/asan/asan_thread.h

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95184.318349.patch
Type: text/x-patch
Size: 6659 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210122/c40d7311/attachment.bin>


More information about the llvm-commits mailing list