[compiler-rt] f9dd3ea - [hwasan] Fix data race between ReleaseThread() and VisitAllLiveThreads()

via llvm-commits llvm-commits at lists.llvm.org
Fri May 5 03:39:26 PDT 2023


Author: Enna1
Date: 2023-05-05T18:39:00+08:00
New Revision: f9dd3ea475e467d42bd3a3ff28c9aa384fe75549

URL: https://github.com/llvm/llvm-project/commit/f9dd3ea475e467d42bd3a3ff28c9aa384fe75549
DIFF: https://github.com/llvm/llvm-project/commit/f9dd3ea475e467d42bd3a3ff28c9aa384fe75549.diff

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

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     |
```

Reviewed By: vitalybuka

Differential Revision: https://reviews.llvm.org/D148909

Added: 
    

Modified: 
    compiler-rt/lib/hwasan/hwasan_thread_list.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/hwasan/hwasan_thread_list.h b/compiler-rt/lib/hwasan/hwasan_thread_list.h
index 99d2d460261fc..52290b5a9b464 100644
--- a/compiler-rt/lib/hwasan/hwasan_thread_list.h
+++ b/compiler-rt/lib/hwasan/hwasan_thread_list.h
@@ -131,9 +131,9 @@ class SANITIZER_MUTEX HwasanThreadList {
 
   void ReleaseThread(Thread *t) SANITIZER_EXCLUDES(free_list_mutex_) {
     RemoveThreadStats(t);
+    RemoveThreadFromLiveList(t);
     t->Destroy();
     DontNeedThread(t);
-    RemoveThreadFromLiveList(t);
     SpinMutexLock l(&free_list_mutex_);
     free_list_.push_back(t);
   }


        


More information about the llvm-commits mailing list