Skip to content

[NFC][sanitizer] Rename Lock{Before,After}Fork suffixes locking StackDepotBase #76279

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
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
48 changes: 26 additions & 22 deletions compiler-rt/lib/asan/asan_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,33 +146,37 @@ void PlatformTSDDtor(void *tsd) {
# endif
AsanThread::TSDDtor(tsd);
}
#endif
# endif

static void BeforeFork() {
if (CAN_SANITIZE_LEAKS) {
__lsan::LockGlobal();
}
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and lock the
// stuff we need.
__lsan::LockThreads();
__lsan::LockAllocator();
StackDepotLockBeforeFork();
}

static void AfterFork(bool fork_child) {
StackDepotUnlockAfterFork(fork_child);
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and unlock
// the stuff we need.
__lsan::UnlockAllocator();
__lsan::UnlockThreads();
if (CAN_SANITIZE_LEAKS) {
__lsan::UnlockGlobal();
}
}

void InstallAtForkHandler() {
# if SANITIZER_SOLARIS || SANITIZER_NETBSD || SANITIZER_APPLE
return; // FIXME: Implement FutexWait.
# endif
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);
pthread_atfork(
&BeforeFork, []() { AfterFork(/* fork_child= */ false); },
[]() { AfterFork(/* fork_child= */ true); });
}

void InstallAtExitCheckLeaks() {
Expand Down
6 changes: 6 additions & 0 deletions compiler-rt/lib/dfsan/dfsan_chained_origin_depot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ static ChainedOriginDepot chainedOriginDepot;

ChainedOriginDepot* GetChainedOriginDepot() { return &chainedOriginDepot; }

void ChainedOriginDepotLockBeforeFork() { chainedOriginDepot.LockAll(); }

void ChainedOriginDepotUnlockAfterFork(bool fork_child) {
chainedOriginDepot.UnlockAll();
}

} // namespace __dfsan
3 changes: 3 additions & 0 deletions compiler-rt/lib/dfsan/dfsan_chained_origin_depot.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ namespace __dfsan {

ChainedOriginDepot* GetChainedOriginDepot();

void ChainedOriginDepotLockBeforeFork();
void ChainedOriginDepotUnlockAfterFork(bool fork_child);

} // namespace __dfsan

#endif // DFSAN_CHAINED_ORIGIN_DEPOT_H
12 changes: 6 additions & 6 deletions compiler-rt/lib/dfsan/dfsan_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2893,13 +2893,13 @@ int __dfso___isoc99_sscanf(char *str, const char *format, dfsan_label str_label,
}

static void BeforeFork() {
StackDepotLockAll();
GetChainedOriginDepot()->LockAll();
StackDepotLockBeforeFork();
ChainedOriginDepotLockBeforeFork();
}

static void AfterFork() {
GetChainedOriginDepot()->UnlockAll();
StackDepotUnlockAll();
static void AfterFork(bool fork_child) {
ChainedOriginDepotUnlockAfterFork(fork_child);
StackDepotUnlockAfterFork(fork_child);
}

SANITIZER_INTERFACE_ATTRIBUTE
Expand All @@ -2913,7 +2913,7 @@ SANITIZER_INTERFACE_ATTRIBUTE
pid_t __dfso_fork(dfsan_label *ret_label, dfsan_origin *ret_origin) {
BeforeFork();
pid_t pid = __dfsw_fork(ret_label);
AfterFork();
AfterFork(/* fork_child= */ pid == 0);
return pid;
}

Expand Down
46 changes: 25 additions & 21 deletions compiler-rt/lib/hwasan/hwasan_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,28 +521,32 @@ uptr TagMemoryAligned(uptr p, uptr size, tag_t tag) {
return AddTagToPointer(p, tag);
}

static void BeforeFork() {
if (CAN_SANITIZE_LEAKS) {
__lsan::LockGlobal();
}
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and lock the
// stuff we need.
__lsan::LockThreads();
__lsan::LockAllocator();
StackDepotLockBeforeFork();
}

static void AfterFork(bool fork_child) {
StackDepotUnlockAfterFork(fork_child);
// `_lsan` functions defined regardless of `CAN_SANITIZE_LEAKS` and unlock
// the stuff we need.
__lsan::UnlockAllocator();
__lsan::UnlockThreads();
if (CAN_SANITIZE_LEAKS) {
__lsan::UnlockGlobal();
}
}

void HwasanInstallAtForkHandler() {
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);
pthread_atfork(
&BeforeFork, []() { AfterFork(/* fork_child= */ false); },
[]() { AfterFork(/* fork_child= */ true); });
}

void InstallAtExitCheckLeaks() {
Expand Down
30 changes: 17 additions & 13 deletions compiler-rt/lib/lsan/lsan_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,27 @@ void InstallAtExitCheckLeaks() {
Atexit(DoLeakCheck);
}

static void BeforeFork() {
LockGlobal();
LockThreads();
LockAllocator();
StackDepotLockBeforeFork();
}

static void AfterFork(bool fork_child) {
StackDepotUnlockAfterFork(fork_child);
UnlockAllocator();
UnlockThreads();
UnlockGlobal();
}

void InstallAtForkHandler() {
# if SANITIZER_SOLARIS || SANITIZER_NETBSD || SANITIZER_APPLE
return; // FIXME: Implement FutexWait.
# endif
auto before = []() {
LockGlobal();
LockThreads();
LockAllocator();
StackDepotLockAll();
};
auto after = []() {
StackDepotUnlockAll();
UnlockAllocator();
UnlockThreads();
UnlockGlobal();
};
pthread_atfork(before, after, after);
pthread_atfork(
&BeforeFork, []() { AfterFork(/* fork_child= */ false); },
[]() { AfterFork(/* fork_child= */ true); });
}

} // namespace __lsan
Expand Down
6 changes: 2 additions & 4 deletions compiler-rt/lib/msan/msan_chained_origin_depot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ u32 ChainedOriginDepotGet(u32 id, u32 *other) {
return chainedOriginDepot.Get(id, other);
}

void ChainedOriginDepotLockAll() {
chainedOriginDepot.LockAll();
}
void ChainedOriginDepotBeforeFork() { chainedOriginDepot.LockAll(); }

void ChainedOriginDepotUnlockAll() {
void ChainedOriginDepotAfterFork(bool fork_child) {
chainedOriginDepot.UnlockAll();
}

Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/msan/msan_chained_origin_depot.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ bool ChainedOriginDepotPut(u32 here_id, u32 prev_id, u32 *new_id);
// Retrieves the stored StackDepot ID for the given origin ID.
u32 ChainedOriginDepotGet(u32 id, u32 *other);

void ChainedOriginDepotLockAll();
void ChainedOriginDepotUnlockAll();
void ChainedOriginDepotBeforeFork();
void ChainedOriginDepotAfterFork(bool fork_child);

} // namespace __msan

Expand Down
32 changes: 18 additions & 14 deletions compiler-rt/lib/msan/msan_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,26 @@ void MsanTSDDtor(void *tsd) {
atomic_signal_fence(memory_order_seq_cst);
MsanThread::TSDDtor(tsd);
}
#endif
# endif

static void BeforeFork() {
// Usually we lock ThreadRegistry, but msan does not have one.
LockAllocator();
StackDepotLockBeforeFork();
ChainedOriginDepotBeforeFork();
}

static void AfterFork(bool fork_child) {
ChainedOriginDepotAfterFork(fork_child);
StackDepotUnlockAfterFork(fork_child);
UnlockAllocator();
// Usually we unlock ThreadRegistry, but msan does not have one.
}

void InstallAtForkHandler() {
auto before = []() {
// Usually we lock ThreadRegistry, but msan does not have one.
LockAllocator();
StackDepotLockAll();
ChainedOriginDepotLockAll();
};
auto after = []() {
ChainedOriginDepotUnlockAll();
StackDepotUnlockAll();
UnlockAllocator();
// Usually we unlock ThreadRegistry, but msan does not have one.
};
pthread_atfork(before, after, after);
pthread_atfork(
&BeforeFork, []() { AfterFork(/* fork_child= */ false); },
[]() { AfterFork(/* fork_child= */ true); });
}

} // namespace __msan
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,13 @@ StackTrace StackDepotGet(u32 id) {
return theDepot.Get(id);
}

void StackDepotLockAll() {
void StackDepotLockBeforeFork() {
theDepot.LockAll();
compress_thread.LockAndStop();
stackStore.LockAll();
}

void StackDepotUnlockAll() {
void StackDepotUnlockAfterFork(bool fork_child) {
stackStore.UnlockAll();
compress_thread.Unlock();
theDepot.UnlockAll();
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/sanitizer_common/sanitizer_stackdepot.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ StackDepotHandle StackDepotPut_WithHandle(StackTrace stack);
// Retrieves a stored stack trace by the id.
StackTrace StackDepotGet(u32 id);

void StackDepotLockAll();
void StackDepotUnlockAll();
void StackDepotLockBeforeFork();
void StackDepotUnlockAfterFork(bool fork_child);
void StackDepotPrintAll();
void StackDepotStopBackgroundThread();

Expand Down