Skip to content

Commit fcce843

Browse files
authored
[msan] Use pthread_atfork instead of interceptor (#75398)
This is done for consistency with other sanitizers. Also lock the allocator.
1 parent c155269 commit fcce843

File tree

6 files changed

+29
-19
lines changed

6 files changed

+29
-19
lines changed

compiler-rt/lib/msan/msan.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ void __msan_init() {
449449
__sanitizer_set_report_path(common_flags()->log_path);
450450

451451
InitializeInterceptors();
452+
InstallAtForkHandler();
452453
CheckASLR();
453454
InitTlsSize();
454455
InstallDeadlySignalHandlers(MsanOnDeadlySignal);

compiler-rt/lib/msan/msan.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,8 @@ void *MsanTSDGet();
336336
void MsanTSDSet(void *tsd);
337337
void MsanTSDDtor(void *tsd);
338338

339+
void InstallAtForkHandler();
340+
339341
} // namespace __msan
340342

341343
#endif // MSAN_H

compiler-rt/lib/msan/msan_allocator.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ void MsanAllocatorInit() {
159159
max_malloc_size = kMaxAllowedMallocSize;
160160
}
161161

162+
void LockAllocator() { allocator.ForceLock(); }
163+
164+
void UnlockAllocator() { allocator.ForceUnlock(); }
165+
162166
AllocatorCache *GetAllocatorCache(MsanThreadLocalMallocStorage *ms) {
163167
CHECK(ms);
164168
CHECK_LE(sizeof(AllocatorCache), sizeof(ms->allocator_cache));

compiler-rt/lib/msan/msan_allocator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ struct MsanThreadLocalMallocStorage {
2828
MsanThreadLocalMallocStorage() {}
2929
};
3030

31+
void LockAllocator();
32+
void UnlockAllocator();
33+
3134
} // namespace __msan
3235
#endif // MSAN_ALLOCATOR_H

compiler-rt/lib/msan/msan_interceptors.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,24 +1326,6 @@ static int setup_at_exit_wrapper(void(*f)(), void *arg, void *dso) {
13261326
return res;
13271327
}
13281328

1329-
static void BeforeFork() {
1330-
StackDepotLockAll();
1331-
ChainedOriginDepotLockAll();
1332-
}
1333-
1334-
static void AfterFork() {
1335-
ChainedOriginDepotUnlockAll();
1336-
StackDepotUnlockAll();
1337-
}
1338-
1339-
INTERCEPTOR(int, fork, void) {
1340-
ENSURE_MSAN_INITED();
1341-
BeforeFork();
1342-
int pid = REAL(fork)();
1343-
AfterFork();
1344-
return pid;
1345-
}
1346-
13471329
// NetBSD ships with openpty(3) in -lutil, that needs to be prebuilt explicitly
13481330
// with MSan.
13491331
#if SANITIZER_LINUX
@@ -1933,7 +1915,6 @@ void InitializeInterceptors() {
19331915
INTERCEPT_FUNCTION(atexit);
19341916
INTERCEPT_FUNCTION(__cxa_atexit);
19351917
INTERCEPT_FUNCTION(shmat);
1936-
INTERCEPT_FUNCTION(fork);
19371918
MSAN_MAYBE_INTERCEPT_OPENPTY;
19381919
MSAN_MAYBE_INTERCEPT_FORKPTY;
19391920

compiler-rt/lib/msan/msan_linux.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
# include <unwind.h>
2727

2828
# include "msan.h"
29+
# include "msan_allocator.h"
30+
# include "msan_chained_origin_depot.h"
2931
# include "msan_report.h"
3032
# include "msan_thread.h"
3133
# include "sanitizer_common/sanitizer_common.h"
3234
# include "sanitizer_common/sanitizer_procmaps.h"
35+
# include "sanitizer_common/sanitizer_stackdepot.h"
3336

3437
namespace __msan {
3538

@@ -255,6 +258,22 @@ void MsanTSDDtor(void *tsd) {
255258
}
256259
#endif
257260

261+
void InstallAtForkHandler() {
262+
auto before = []() {
263+
// Usually we lock ThreadRegistry, but msan does not have one.
264+
LockAllocator();
265+
StackDepotLockAll();
266+
ChainedOriginDepotLockAll();
267+
};
268+
auto after = []() {
269+
ChainedOriginDepotUnlockAll();
270+
StackDepotUnlockAll();
271+
UnlockAllocator();
272+
// Usually we unlock ThreadRegistry, but msan does not have one.
273+
};
274+
pthread_atfork(before, after, after);
275+
}
276+
258277
} // namespace __msan
259278

260279
#endif // SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD

0 commit comments

Comments
 (0)