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

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 12:02:23 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Paweł Bylica (chfast)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/192114.diff


1 Files Affected:

- (modified) compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp (+4-5) 


``````````diff
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));

``````````

</details>


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


More information about the llvm-commits mailing list