[compiler-rt] [sanitizer] Use close_range on Linux to close FDs in StartSubprocess (PR #191450)
Paweł Bylica via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 10 08:58:44 PDT 2026
https://github.com/chfast created https://github.com/llvm/llvm-project/pull/191450
StartSubprocess() loops close(fd) from sysconf(_SC_OPEN_MAX) down to 3. With high RLIMIT_NOFILE (e.g. 1B in Docker environments), this takes minutes.
Use close_range(3, ~0U, 0) on Linux 5.9+, with fallback to the loop if the syscall is not supported or the kernel headers don't define __NR_close_range. FreeBSD already used close_range.
Fixes #63297.
Fixes #152459.
>From a4542a8f999169285355e021b30cd273728c6cd4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Fri, 10 Apr 2026 15:07:39 +0200
Subject: [PATCH] [sanitizer] Use close_range on Linux to close FDs in
StartSubprocess
StartSubprocess() loops close(fd) from sysconf(_SC_OPEN_MAX) down
to 3. With high RLIMIT_NOFILE (e.g. 1B in Docker environments),
this takes minutes.
Use close_range(3, ~0U, 0) on Linux 5.9+, with fallback to the
loop if the syscall is not supported or the kernel headers don't
define __NR_close_range. FreeBSD already used close_range.
Fixes #63297.
---
.../lib/sanitizer_common/sanitizer_linux.cpp | 13 ++++++++++++-
compiler-rt/lib/sanitizer_common/sanitizer_posix.h | 2 +-
.../sanitizer_common/sanitizer_posix_libcdep.cpp | 12 ++++++++----
3 files changed, 21 insertions(+), 6 deletions(-)
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index 16b94ea901bad..7708689d3e6e9 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -169,6 +169,13 @@ const int FUTEX_WAKE_PRIVATE = FUTEX_WAKE | FUTEX_PRIVATE_FLAG;
# define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
# endif
+// close_range is available on FreeBSD 13+ and Linux 5.9+.
+# if SANITIZER_FREEBSD || (SANITIZER_LINUX && defined(__NR_close_range))
+# define SANITIZER_USE_CLOSE_RANGE 1
+# else
+# define SANITIZER_USE_CLOSE_RANGE 0
+# endif
+
// Note : FreeBSD implemented both Linux and OpenBSD apis.
# if SANITIZER_LINUX && defined(__NR_getrandom)
# if !defined(GRND_NONBLOCK)
@@ -306,9 +313,13 @@ int internal_madvise(uptr addr, uptr length, int advice) {
return internal_syscall(SYSCALL(madvise), addr, length, advice);
}
-# if SANITIZER_FREEBSD
+# if SANITIZER_FREEBSD || SANITIZER_LINUX
uptr internal_close_range(fd_t lowfd, fd_t highfd, int flags) {
+# if SANITIZER_USE_CLOSE_RANGE
return internal_syscall(SYSCALL(close_range), lowfd, highfd, flags);
+# else
+ return -1;
+# endif
}
# endif
uptr internal_close(fd_t fd) { return internal_syscall(SYSCALL(close), fd); }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
index dc9c3b8822d30..5a5556eefc711 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
@@ -28,7 +28,7 @@ namespace __sanitizer {
// Don't use directly, use __sanitizer::OpenFile() instead.
uptr internal_open(const char *filename, int flags);
uptr internal_open(const char *filename, int flags, u32 mode);
-# if SANITIZER_FREEBSD
+# if SANITIZER_FREEBSD || SANITIZER_LINUX
uptr internal_close_range(fd_t lowfd, fd_t highfd, int flags);
# endif
uptr internal_close(fd_t fd);
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
index 8e5e87938c372..5f224b7ee1b79 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
@@ -562,11 +562,15 @@ pid_t StartSubprocess(const char *program, const char *const argv[],
internal_close(stderr_fd);
}
-# if SANITIZER_FREEBSD
- internal_close_range(3, ~static_cast<fd_t>(0), 0);
-# else
- for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd);
+# if SANITIZER_FREEBSD || SANITIZER_LINUX
+ // Use close_range (FreeBSD 13+, Linux 5.9+) to avoid a slow loop
+ // when RLIMIT_NOFILE is high (e.g. 1B in Docker environments).
+ // Falls back to the loop if the syscall is not available.
+ if (internal_close_range(3, ~static_cast<fd_t>(0), 0) != 0)
# endif
+ {
+ for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd);
+ }
internal_execve(program, const_cast<char **>(&argv[0]),
const_cast<char *const *>(envp));
More information about the llvm-commits
mailing list