This is an archive of the discontinued LLVM Phabricator instance.

[hwasan] Fix data race between ReleaseThread() and VisitAllLiveThreads()
ClosedPublic

Authored by Enna1 on Apr 21 2023, 2:26 AM.

Details

Summary

Data race scenario:

Thread 1                                | Thread 2
ReportTagMismatch()                     |
Call VisitAllLiveThreads() to scan all  |
threads' ring buffers to find           |
if it's a heap-use-after-free.          |
Lock live_list_mutex_                   |
                                        | Thread 2 exit
                                        | ReleaseThread() calls Thread::Destroy() for Thread 2,
                                        | which frees heap alloctions ring buffer
                                        | RemoveThreadFromLiveList() tries to take live_list_mutex_ again
Iterate the heap alloctions ring buffer |
of Thread 2, which is already freed     |

Diff Detail

Event Timeline

Enna1 created this revision.Apr 21 2023, 2:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2023, 2:26 AM
Enna1 edited the summary of this revision. (Show Details)Apr 21 2023, 2:28 AM
Enna1 edited the summary of this revision. (Show Details)
Enna1 added a subscriber: MTC.
Enna1 published this revision for review.Apr 21 2023, 2:43 AM

We encountered a crash due to this data race during the process of enabling hwasan for our server-side applications on AArch64/Linux. With this change, the crash will be fixed.

But can't figure out how to write a test case for this, still trying.

Herald added a project: Restricted Project. · View Herald TranscriptApr 21 2023, 2:43 AM
Herald added a subscriber: Restricted Project. · View Herald Transcript
vitalybuka accepted this revision.Apr 26 2023, 1:13 PM

Would be nice to have a simple reproducer as a test, but LGTM as-is if that's hard.

This revision is now accepted and ready to land.Apr 26 2023, 1:13 PM