Skip to content

Commit

Permalink
[hwasan] Improve support of forking with threads (llvm#75291)
Browse files Browse the repository at this point in the history
Lock Lsan and Thread related date at_fork.

Clean shadow before thread starts, forked process may reuse already
mapped stack of 'lost' parent thread for new threads.
  • Loading branch information
vitalybuka authored Dec 13, 2023
1 parent 1928401 commit 8d300e6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
16 changes: 14 additions & 2 deletions compiler-rt/lib/hwasan/hwasan_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,24 @@ uptr TagMemoryAligned(uptr p, uptr size, tag_t tag) {

void HwasanInstallAtForkHandler() {
auto before = []() {
HwasanAllocatorLock();
if (CAN_SANITIZE_LEAKS) {
__lsan::LockGlobal();
}
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and lock the
// stuff we need.
__lsan::LockThreads();
__lsan::LockAllocator();
StackDepotLockAll();
};
auto after = []() {
StackDepotUnlockAll();
HwasanAllocatorUnlock();
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and unlock
// the stuff we need.
__lsan::UnlockAllocator();
__lsan::UnlockThreads();
if (CAN_SANITIZE_LEAKS) {
__lsan::UnlockGlobal();
}
};
pthread_atfork(before, after, after);
}
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/hwasan/hwasan_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,
}
Print("Creating : ");
}
ClearShadowForThreadStackAndTLS();
}

void Thread::InitStackRingBuffer(uptr stack_buffer_start,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// RUN: %clang -O0 %s -o %t && %env_tool_opts=die_after_fork=0 %run %t

// UNSUPPORTED: hwasan

// The test uses pthread barriers which are not available on Darwin.
// UNSUPPORTED: darwin

Expand Down

0 comments on commit 8d300e6

Please sign in to comment.