Skip to content

Commit 8d300e6

Browse files
authored
[hwasan] Improve support of forking with threads (#75291)
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.
1 parent 1928401 commit 8d300e6

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

compiler-rt/lib/hwasan/hwasan_linux.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,24 @@ uptr TagMemoryAligned(uptr p, uptr size, tag_t tag) {
523523

524524
void HwasanInstallAtForkHandler() {
525525
auto before = []() {
526-
HwasanAllocatorLock();
526+
if (CAN_SANITIZE_LEAKS) {
527+
__lsan::LockGlobal();
528+
}
529+
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and lock the
530+
// stuff we need.
531+
__lsan::LockThreads();
532+
__lsan::LockAllocator();
527533
StackDepotLockAll();
528534
};
529535
auto after = []() {
530536
StackDepotUnlockAll();
531-
HwasanAllocatorUnlock();
537+
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and unlock
538+
// the stuff we need.
539+
__lsan::UnlockAllocator();
540+
__lsan::UnlockThreads();
541+
if (CAN_SANITIZE_LEAKS) {
542+
__lsan::UnlockGlobal();
543+
}
532544
};
533545
pthread_atfork(before, after, after);
534546
}

compiler-rt/lib/hwasan/hwasan_thread.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ void Thread::Init(uptr stack_buffer_start, uptr stack_buffer_size,
6868
}
6969
Print("Creating : ");
7070
}
71+
ClearShadowForThreadStackAndTLS();
7172
}
7273

7374
void Thread::InitStackRingBuffer(uptr stack_buffer_start,

compiler-rt/test/sanitizer_common/TestCases/Posix/fork_threaded.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %clang -O0 %s -o %t && %env_tool_opts=die_after_fork=0 %run %t
22

3-
// UNSUPPORTED: hwasan
4-
53
// The test uses pthread barriers which are not available on Darwin.
64
// UNSUPPORTED: darwin
75

0 commit comments

Comments
 (0)