[compiler-rt] [sanitizer] Generalize FD closing in StartSubprocess (PR #192114)

Paweł Bylica via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 12:01:37 PDT 2026


https://github.com/chfast created https://github.com/llvm/llvm-project/pull/192114

Use internal_close_range with a fallback to the sysconf(_SC_OPEN_MAX) loop. This removes the platform-specific #if and lets all platforms benefit from close_range when supported.

Follow-up to #191971.

>From 98b421e5db22544369c82e1ba789acebf111cc82 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pawe=C5=82=20Bylica?= <pawel at hepcolgum.band>
Date: Tue, 14 Apr 2026 20:35:34 +0200
Subject: [PATCH] [sanitizer] Generalize FD closing in StartSubprocess

Use internal_close_range with a fallback to the sysconf(_SC_OPEN_MAX)
loop. This removes the platform-specific #if and lets all platforms
benefit from close_range when supported.

Follow-up to #191971.
---
 .../lib/sanitizer_common/sanitizer_posix_libcdep.cpp     | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
index 8e5e87938c372..35b596de30fff 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp
@@ -562,11 +562,10 @@ 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);
-#  endif
+    // Close all fds except stdin/stdout/stderr before exec.
+    // Fallback to the loop if close_range is not supported.
+    if (internal_close_range(3, ~static_cast<fd_t>(0), 0) != 0)
+      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