This is an archive of the discontinued LLVM Phabricator instance.

tsan: fix deadlock/crash in signal handling
ClosedPublic

Authored by dvyukov on Jun 15 2022, 4:20 AM.

Details

Summary

We set in_blocking_func around some blocking C functions so that we don't
delay signal infinitely (if in_blocking_func is set we deliver signals
synchronously).

However, pthread_join is blocking but also call munmap/free to free thread
resources. If we are inside the munmap/free interceptors called from
pthread_join and deliver a signal synchronously, it can lead to deadlocks
and crashes since we re-enter runtime and try to lock the same mutexes
or use the same per-thread data structures.

If we re-enter runtime via an interceptor when in_blocking_func is set,
temporary reset in_blocking_func around the interceptor and restore it back
when we return from the recursive interceptor.

Also move in_blocking_func from ThreadSignalContext to ThreadContext
so that we can CHECK that it's not set in SlotLocker ctor.

Fixes https://github.com/google/sanitizers/issues/1540

Diff Detail

Event Timeline

dvyukov created this revision.Jun 15 2022, 4:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 4:20 AM
Herald added a subscriber: Enna1. · View Herald Transcript
dvyukov requested review of this revision.Jun 15 2022, 4:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 4:20 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
melver accepted this revision.Jun 15 2022, 5:50 AM
melver added inline comments.
compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
179

typo -> synchronously

compiler-rt/lib/tsan/rtl/tsan_rtl.h
595

synchronously

This revision is now accepted and ready to land.Jun 15 2022, 5:50 AM
dvyukov updated this revision to Diff 437197.Jun 15 2022, 8:51 AM

fixed typos in comments

melver accepted this revision.Sep 30 2022, 4:50 AM
This revision was landed with ongoing or failed builds.Sep 30 2022, 5:24 AM
This revision was automatically updated to reflect the committed changes.