This is an archive of the discontinued LLVM Phabricator instance.

Handle NetBSD specific indirection of libpthread functions
ClosedPublic

Authored by krytarowski on Nov 20 2017, 5:07 AM.

Details

Summary

Correct handling of libpthread(3) functions in TSan/NetBSD:

  • pthread_cond_init(3),
  • pthread_cond_signal(3),
  • pthread_cond_broadcast(3),
  • pthread_cond_wait(3),
  • pthread_cond_destroy(3),
  • pthread_mutex_init(3),
  • pthread_mutex_destroy(3),
  • pthread_mutex_trylock(3),
  • pthread_rwlock_init(3),
  • pthread_rwlock_destroy(3),
  • pthread_rwlock_rdlock(3),
  • pthread_rwlock_tryrdlock(3),
  • pthread_rwlock_wrlock(3),
  • pthread_rwlock_trywrlock(3),
  • pthread_rwlock_unlock(3),
  • pthread_once(3).

Code out of the libpthread(3) context uses the libc symbols
that are prefixed with libc_, for example: libc_cond_init.

This caused that these functions were invisible to sanitizers on NetBSD.
Intercept the libc-specific ones and add them as NetBSD-specific aliases
for the common pthread(3) ones.

NetBSD needs to intercept both functions, as the regularly named ones
are used internally in libpthread(3).

Sponsored by <The NetBSD Foundation>

Diff Detail

Repository
rL LLVM

Event Timeline

krytarowski created this revision.Nov 20 2017, 5:07 AM

There is an open question what to do with tests. They report results with the mangled name in stack trace, instead of the regular libpthread(3) ones.

$ ./mutex                                                                    
==================
WARNING: ThreadSanitizer: data race (pid=13274)
  Write of size 4 at 0x0000014f9ec0 by thread T1 (mutexes: write M76):
    #0 Thread1(void*) /public/llvm-build/mutex.cc:36:9 (mutex+0x46e5ec)

  Previous write of size 4 at 0x0000014f9ec0 by thread T2:
    #0 Thread2(void*) /public/llvm-build/mutex.cc:42:9 (mutex+0x46e6b7)

  Location is global 'Global' of size 4 at 0x0000014f9ec0 (mutex+0x0000014f9ec0)

  Mutex M76 (0x0000014f9ec8) created at:
    #0 __libc_mutex_init /public/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:1137:3 (mutex+0x413ef3)
    #1 main /public/llvm-build/mutex.cc:66:3 (mutex+0x46e796)

  Thread T1 (tid=2, running) created by main thread at:
    #0 pthread_create /public/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:921:3 (mutex+0x412cf0)
    #1 main /public/llvm-build/mutex.cc:71:3 (mutex+0x46e7b5)

  Thread T2 (tid=3, finished) created by main thread at:
    #0 pthread_create /public/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:921:3 (mutex+0x412cf0)
    #1 main /public/llvm-build/mutex.cc:72:3 (mutex+0x46e7d4)

SUMMARY: ThreadSanitizer: data race /public/llvm-build/mutex.cc:36:9 in Thread1(void*)
==================
ThreadSanitizer: reported 1 warnings

__libc_mutex_init printed instead of pthread_mutex_init. This breaks tests that check for exact strings.

I will implement fix for tests (demangle names or change tests) in a dedicated review.

dvyukov edited edge metadata.Nov 20 2017, 5:36 AM

There is an open question what to do with tests. They report results with the mangled name in stack trace, instead of the regular libpthread(3) ones.

$ ./mutex                                                                    
==================
WARNING: ThreadSanitizer: data race (pid=13274)
  Write of size 4 at 0x0000014f9ec0 by thread T1 (mutexes: write M76):
    #0 Thread1(void*) /public/llvm-build/mutex.cc:36:9 (mutex+0x46e5ec)

  Previous write of size 4 at 0x0000014f9ec0 by thread T2:
    #0 Thread2(void*) /public/llvm-build/mutex.cc:42:9 (mutex+0x46e6b7)

  Location is global 'Global' of size 4 at 0x0000014f9ec0 (mutex+0x0000014f9ec0)

  Mutex M76 (0x0000014f9ec8) created at:
    #0 __libc_mutex_init /public/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:1137:3 (mutex+0x413ef3)
    #1 main /public/llvm-build/mutex.cc:66:3 (mutex+0x46e796)

  Thread T1 (tid=2, running) created by main thread at:
    #0 pthread_create /public/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:921:3 (mutex+0x412cf0)
    #1 main /public/llvm-build/mutex.cc:71:3 (mutex+0x46e7b5)

  Thread T2 (tid=3, finished) created by main thread at:
    #0 pthread_create /public/llvm/projects/compiler-rt/lib/tsan/rtl/tsan_interceptors.cc:921:3 (mutex+0x412cf0)
    #1 main /public/llvm-build/mutex.cc:72:3 (mutex+0x46e7d4)

SUMMARY: ThreadSanitizer: data race /public/llvm-build/mutex.cc:36:9 in Thread1(void*)
==================
ThreadSanitizer: reported 1 warnings

__libc_mutex_init printed instead of pthread_mutex_init. This breaks tests that check for exact strings.

I will implement fix for tests (demangle names or change tests) in a dedicated review.

Check out PrintStack in tsan_report.cc. We already mess with interceptor names there (e.g. with interceptors they would like as __interceptor_pthread_mutex_lock even on linux). I think it's the right place to patch function names.

lib/tsan/rtl/tsan_interceptors.cc
2480

Perhaps we need some helper macro so this can be spelled along the lines of:

NETBSD_ALIAS(cond_init);

I think we could declare all alias functions as taking no arguments.

Add helper macros to make the code prettier and reduce copy-and-paste.

krytarowski added inline comments.Nov 20 2017, 6:26 AM
lib/tsan/rtl/tsan_interceptors.cc
2480

Done. I've decided to keep arguments.

dvyukov accepted this revision.Nov 20 2017, 8:12 AM
This revision is now accepted and ready to land.Nov 20 2017, 8:12 AM
krytarowski closed this revision.Nov 20 2017, 10:08 AM