Skip to content

Commit 2606a58

Browse files
authored
compiler-rt: sanitizer_common: use close_range() instead of looping (#114442)
_SC_OPEN_MAX is quite high on FreeBSD, which makes this close() loop a quite obvious problem when attempting to do any kind of debugging in a process that uses StartSubprocess. Switch to using close_range(2) instead to close them all in a single syscall and dramatically reduce the runtime and syscall trace noise Linux has an equivalent syscall, but I do not have the capacity to test that it works there, so this is limited to SANITIZER_FREEBSD for the time being.
1 parent 0019d06 commit 2606a58

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ int internal_madvise(uptr addr, uptr length, int advice) {
256256
return internal_syscall(SYSCALL(madvise), addr, length, advice);
257257
}
258258

259+
# if SANITIZER_FREEBSD
260+
uptr internal_close_range(fd_t lowfd, fd_t highfd, int flags) {
261+
return internal_syscall(SYSCALL(close_range), lowfd, highfd, flags);
262+
}
263+
# endif
259264
uptr internal_close(fd_t fd) { return internal_syscall(SYSCALL(close), fd); }
260265

261266
uptr internal_open(const char *filename, int flags) {

compiler-rt/lib/sanitizer_common/sanitizer_posix.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ namespace __sanitizer {
2828
// Don't use directly, use __sanitizer::OpenFile() instead.
2929
uptr internal_open(const char *filename, int flags);
3030
uptr internal_open(const char *filename, int flags, u32 mode);
31+
# if SANITIZER_FREEBSD
32+
uptr internal_close_range(fd_t lowfd, fd_t highfd, int flags);
33+
# endif
3134
uptr internal_close(fd_t fd);
3235

3336
uptr internal_read(fd_t fd, void *buf, uptr count);

compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,11 @@ pid_t StartSubprocess(const char *program, const char *const argv[],
543543
internal_close(stderr_fd);
544544
}
545545

546+
# if SANITIZER_FREEBSD
547+
internal_close_range(3, ~static_cast<fd_t>(0), 0);
548+
# else
546549
for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd);
550+
# endif
547551

548552
internal_execve(program, const_cast<char **>(&argv[0]),
549553
const_cast<char *const *>(envp));

0 commit comments

Comments
 (0)