This is an archive of the discontinued LLVM Phabricator instance.

Defer StartBackgroundThread() and StopBackgroundThread() in TSan
ClosedPublic

Authored by krytarowski on Nov 28 2017, 3:10 PM.

Details

Summary

NetBSD cannot spawn new POSIX thread entities in early
libc and libpthread initialization stage. Defer this to the point
of intercepting the first pthread_create(3) call.

This is the last change that makes Thread Sanitizer functional
on NetBSD/amd64 without downstream patches.


Testing Time: 64.91s


Failing Tests (5):

  ThreadSanitizer-x86_64 :: dtls.c
  ThreadSanitizer-x86_64 :: ignore_lib5.cc
  ThreadSanitizer-x86_64 :: ignored-interceptors-mmap.cc
  ThreadSanitizer-x86_64 :: mutex_lock_destroyed.cc
  ThreadSanitizer-x86_64 :: vfork.cc

Expected Passes    : 290
Expected Failures  : 1
Unsupported Tests  : 83
Unexpected Failures: 5

Sponsored by <The NetBSD Foundation>

Diff Detail

Repository
rL LLVM

Event Timeline

krytarowski created this revision.Nov 28 2017, 3:10 PM

Try to make the code prettier.

dvyukov accepted this revision.Nov 28 2017, 11:51 PM

LGTM with a nit

lib/tsan/rtl/tsan_rtl.cc
423

atomic_exchange will be slightly simpler than CAS:

static atomic_uint32_t bg_thread = {};
if (atomic_load(&bg_thread, memory_order_relaxed) == 0 &&
    atomic_exchange(&bg_thread, 1, memory_order_relaxed) == 0) {
  StartBackgroundThread();
  SetSandboxingCallback(StopBackgroundThread);
}
This revision is now accepted and ready to land.Nov 28 2017, 11:51 PM
dvyukov added inline comments.Nov 28 2017, 11:52 PM
lib/tsan/rtl/tsan_rtl.h
703

Another nit: please don't prefix function in tsan with tsan. Everything here is tsan-something. It does not add any value to add tsan everywhere.

dvyukov added inline comments.Nov 28 2017, 11:53 PM
lib/tsan/rtl/tsan_rtl.h
703

And this all is already in __tsan namespace. So this is actually tsan's __tsan::TSan...

  • rename TSanMaybeSpawnBackgroundThread() to MaybeSpawnBackgroundThread ()
  • simplify atomic check in MaybeSpawnBackgroundThread()
krytarowski closed this revision.Nov 29 2017, 2:24 AM

Thank you again for the great support!

Next MSan.

Status:

MSan - 1/3 tests pass, but needs more work
Scudo - I got it to build and pass some tests locally, however a lot of them are designed for <malloc.h> specific internals from GLIBC
XRay - it does not look to be far away, waiting for Darwin patches to get merged
LSan - still no functional StopTheWorld(),
KAsan - frontend/llvm part is done, but kernel part needs porting
KUBsan - same as KAsan
ESan - not started, appears to require more work to handle FILE*.

eugenis edited edge metadata.Nov 29 2017, 11:33 AM

Interesting. Did you see the same problem with ASan spawning its background thread (the one that does RSS limiting) too early as well? If not, then what's different between that and TSan?

Interesting. Did you see the same problem with ASan spawning its background thread (the one that does RSS limiting) too early as well? If not, then what's different between that and TSan?

Do you mean the code in sanitizer_common_libcdep.cc?

void MaybeStartBackgroudThread() {
#if SANITIZER_LINUX && \
    !SANITIZER_GO  // Need to implement/test on other platforms.
  // Start the background thread if one of the rss limits is given.
  if (!common_flags()->hard_rss_limit_mb &&
      !common_flags()->soft_rss_limit_mb &&
      !common_flags()->heap_profile) return;
  if (!&real_pthread_create) return;  // Can't spawn the thread anyway.
  internal_start_thread(BackgroundThread, nullptr);
#endif
}

It's Linux-only as of today.

Yes, that's it. Thanks.