This is an archive of the discontinued LLVM Phabricator instance.

tsan: fix a race when assigning ThreadSignalContext
ClosedPublic

Authored by ridiculousfish on Dec 22 2022, 12:43 PM.

Details

Summary

The SigCtx function lazily allocates a ThreadSignalContext, and stores it
in the ThreadState. This function may be called by various interceptors and
the signal handler itself.

If SigCtx itself is interrupted by a signal, then (prior to this fix) there
was a possibility of allocating two ThreadSignalContexts. This not only
leaks, it fails to deliver the signal to the program's signal handler, as
the recorded signal is overwritten by the new ThreadSignalContext.

Fix this by using a CAS to swap in the ThreadSignalContext, preventing the
race. Add a test for this case.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptDec 22 2022, 12:43 PM
Herald added a subscriber: Enna1. · View Herald Transcript
ridiculousfish requested review of this revision.Dec 22 2022, 12:43 PM
Herald added a subscriber: Restricted Project. · View Herald TranscriptDec 22 2022, 12:43 PM
melver added inline comments.Jan 2 2023, 1:27 AM
compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
264

A failed atomic_compare_exchange will load current value into ctx, so this additional atomic_load is unnecessary?

Address review feedback: we don't need to loop on CAS and we can elide the atomic_load.

ridiculousfish marked an inline comment as done.Jan 2 2023, 12:50 PM
ridiculousfish added inline comments.
compiler-rt/lib/tsan/rtl/tsan_interceptors_posix.cpp
264

Good call, fixed.

melver accepted this revision.Jan 3 2023, 12:54 AM

LGTM, but please wait for Dmitry's review as well (he'll likely respond next week).

Thanks!

This revision is now accepted and ready to land.Jan 3 2023, 12:54 AM
dvyukov accepted this revision.Jan 9 2023, 7:02 AM
melver added a subscriber: Peter.EditedJan 10 2023, 9:42 AM

@ Peter, are you able to commit this yourself, or should we commit it for you?

Peter added a comment.Jan 10 2023, 9:56 AM

@ Peter, are you able to commit this yourself, or should we commit it for you?

Sorry but I think you got the wrong Peter. I never worked on tsan before:)

@ Peter, are you able to commit this yourself, or should we commit it for you?

Sorry but I think you got the wrong Peter. I never worked on tsan before:)

Yes, I meant @ridiculousfish - sorry about that, I didn't realize @ auto-Ccs.

ridiculousfish marked an inline comment as done.Jan 10 2023, 10:19 AM

Thank you for the review! Please merge on my behalf.

This revision was automatically updated to reflect the committed changes.