[PATCH] D101881: [hwasan] Fix missing synchronization in AllocThread.

Evgenii Stepanov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 4 19:51:59 PDT 2021


eugenis created this revision.
eugenis added reviewers: vitalybuka, pcc, hctim.
eugenis requested review of this revision.
Herald added a project: Sanitizers.
Herald added a subscriber: Sanitizers.

The problem was introduced in D100348 <https://reviews.llvm.org/D100348>.

It's really hard to trigger the bug in a stress test - the race is just too
narrow - but the new checks in Thread::Init should at least provide usable
diagnostic if the problem ever returns.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D101881

Files:
  compiler-rt/lib/hwasan/hwasan_thread.cpp
  compiler-rt/lib/hwasan/hwasan_thread_list.h


Index: compiler-rt/lib/hwasan/hwasan_thread_list.h
===================================================================
--- compiler-rt/lib/hwasan/hwasan_thread_list.h
+++ compiler-rt/lib/hwasan/hwasan_thread_list.h
@@ -173,6 +173,7 @@
 
  private:
   Thread *AllocThread() {
+    SpinMutexLock l(&free_space_mutex_);
     uptr align = ring_buffer_size_ * 2;
     CHECK(IsAligned(free_space_, align));
     Thread *t = (Thread *)(free_space_ + ring_buffer_size_);
@@ -181,6 +182,7 @@
     return t;
   }
 
+  SpinMutex free_space_mutex_;
   uptr free_space_;
   uptr free_space_end_;
   uptr ring_buffer_size_;
Index: compiler-rt/lib/hwasan/hwasan_thread.cpp
===================================================================
--- compiler-rt/lib/hwasan/hwasan_thread.cpp
+++ compiler-rt/lib/hwasan/hwasan_thread.cpp
@@ -35,6 +35,10 @@
 }
 
 void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size) {
+  CHECK_EQ(0, unique_id_);  // try to catch bad stack reuse
+  CHECK_EQ(0, stack_top_);
+  CHECK_EQ(0, stack_bottom_);
+
   static u64 unique_id;
   unique_id_ = unique_id++;
   if (auto sz = flags()->heap_history_size)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D101881.342932.patch
Type: text/x-patch
Size: 1131 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210505/8f67282a/attachment.bin>


More information about the llvm-commits mailing list