Skip to content

[lsan] Install pthread_atfork #75281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler-rt/lib/lsan/lsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ extern "C" void __lsan_init() {
InstallDeadlySignalHandlers(LsanOnDeadlySignal);
InitializeMainThread();
InstallAtExitCheckLeaks();
InstallAtForkHandler();

InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);

Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/lsan/lsan.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void InitializeInterceptors();
void ReplaceSystemMalloc();
void LsanOnDeadlySignal(int signo, void *siginfo, void *context);
void InstallAtExitCheckLeaks();
void InstallAtForkHandler();

#define ENSURE_LSAN_INITED \
do { \
Expand Down
3 changes: 3 additions & 0 deletions compiler-rt/lib/lsan/lsan_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ namespace __lsan {
// also to protect the global list of root regions.
static Mutex global_mutex;

void LockGlobal() SANITIZER_ACQUIRE(global_mutex) { global_mutex.Lock(); }
void UnlockGlobal() SANITIZER_RELEASE(global_mutex) { global_mutex.Unlock(); }

Flags lsan_flags;

void DisableCounterUnderflow() {
Expand Down
4 changes: 4 additions & 0 deletions compiler-rt/lib/lsan/lsan_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ void GetRunningThreadsLocked(InternalMmapVector<tid_t> *threads);
void LockAllocator();
void UnlockAllocator();

// Lock/unlock global mutext.
void LockGlobal();
void UnlockGlobal();

// Returns the address range occupied by the global allocator object.
void GetAllocatorGlobalRange(uptr *begin, uptr *end);
// If p points into a chunk that has been allocated to the user, returns its
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/lsan/lsan_fuchsia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void GetAllThreadAllocatorCachesLocked(InternalMmapVector<uptr> *caches) {
// On Fuchsia, leak detection is done by a special hook after atexit hooks.
// So this doesn't install any atexit hook like on other platforms.
void InstallAtExitCheckLeaks() {}
void InstallAtForkHandler() {}

// ASan defines this to check its `halt_on_error` flag.
bool UseExitcodeOnLeak() { return true; }
Expand Down
18 changes: 18 additions & 0 deletions compiler-rt/lib/lsan/lsan_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "sanitizer_common/sanitizer_platform.h"

#if SANITIZER_POSIX
# include <pthread.h>

# include "lsan.h"
# include "lsan_allocator.h"
# include "lsan_thread.h"
Expand Down Expand Up @@ -98,6 +100,22 @@ void InstallAtExitCheckLeaks() {
Atexit(DoLeakCheck);
}

void InstallAtForkHandler() {
auto before = []() {
LockGlobal();
LockThreads();
LockAllocator();
StackDepotLockAll();
};
auto after = []() {
StackDepotUnlockAll();
UnlockAllocator();
UnlockThreads();
UnlockGlobal();
};
pthread_atfork(before, after, after);
}

} // namespace __lsan

#endif // SANITIZER_POSIX
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %clang -O0 %s -o %t && %env_tool_opts=die_after_fork=0 %run %t

// UNSUPPORTED: asan, lsan, hwasan
// UNSUPPORTED: asan, hwasan

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