Skip to content

[asan] Install pthread_atfork #75290

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 6 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
2 changes: 2 additions & 0 deletions compiler-rt/lib/asan/asan_fuchsia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ void FlushUnneededASanShadowMemory(uptr p, uptr size) {
// So this doesn't install any atexit hook like on other platforms.
void InstallAtExitCheckLeaks() {}

void InstallAtForkHandler() {}

} // namespace __asan

namespace __lsan {
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/asan/asan_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void *AsanDlSymNext(const char *sym);
bool HandleDlopenInit();

void InstallAtExitCheckLeaks();
void InstallAtForkHandler();

#define ASAN_ON_ERROR() \
if (&__asan_on_error) \
Expand Down
24 changes: 24 additions & 0 deletions compiler-rt/lib/asan/asan_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,30 @@ void PlatformTSDDtor(void *tsd) {
}
#endif

void InstallAtForkHandler() {
auto before = []() {
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();
// `_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);
}

void InstallAtExitCheckLeaks() {
if (CAN_SANITIZE_LEAKS) {
if (common_flags()->detect_leaks && common_flags()->leak_check_at_exit) {
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/asan/asan_rtl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ static bool AsanInitInternal() {
InstallAtExitCheckLeaks();
}

InstallAtForkHandler();

#if CAN_SANITIZE_UB
__ubsan::InitAsPlugin();
#endif
Expand Down
2 changes: 2 additions & 0 deletions compiler-rt/lib/asan/asan_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ void InitializePlatformInterceptors() {

void InstallAtExitCheckLeaks() {}

void InstallAtForkHandler() {}

void AsanApplyToGlobals(globals_op_fptr op, const void *needle) {
UNIMPLEMENTED();
}
Expand Down
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, hwasan
// UNSUPPORTED: hwasan

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