[llvm-branch-commits] [libc] [llvm] [libc] Use syscall wrappers for fork (PR #242253)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Nov 13 02:51:57 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Connector Switch (c8ef)
<details>
<summary>Changes</summary>
This PR move fork behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
Full diff: https://github.com/llvm/llvm-project/pull/242253.diff
6 Files Affected:
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+26)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/fork.h (+41)
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h (+31)
- (modified) libc/src/unistd/linux/CMakeLists.txt (+7-6)
- (modified) libc/src/unistd/linux/fork.cpp (+15-17)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+34-2)
``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index f1adb0ff19af9..cfd4d1e25aba1 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -87,6 +87,18 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ gettid
+ HDRS
+ gettid.h
+ DEPENDS
+ libc.hdr.types.pid_t
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.macros.config
+)
+
add_header_library(
getuid
HDRS
@@ -1261,6 +1273,20 @@ add_header_library(
libc.src.__support.macros.config
)
+add_header_library(
+ fork
+ HDRS
+ fork.h
+ DEPENDS
+ libc.hdr.signal_macros
+ libc.hdr.types.pid_t
+ libc.include.sys_syscall
+ libc.src.__support.OSUtil.osutil
+ libc.src.__support.common
+ libc.src.__support.error_or
+ libc.src.__support.macros.config
+)
+
add_header_library(
lchown
HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fork.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fork.h
new file mode 100644
index 0000000000000..d80de7c98c964
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fork.h
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for fork.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FORK_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FORK_H
+
+#include "hdr/signal_macros.h"
+#include "hdr/types/pid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<pid_t> fork() {
+#ifdef SYS_fork
+ return syscall_checked<pid_t>(SYS_fork);
+#elif defined(SYS_clone)
+ return syscall_checked<pid_t>(SYS_clone, SIGCHLD, 0);
+#else
+#error "fork and clone syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FORK_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h
new file mode 100644
index 0000000000000..42f7862326f6e
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for gettid.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETTID_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETTID_H
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE pid_t gettid() { return syscall_impl<pid_t>(SYS_gettid); }
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETTID_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index f179b86543e5d..84902b6b827f0 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -210,15 +210,16 @@ add_entrypoint_object(
../fork.h
DEPENDS
libc.hdr.types.pid_t
- libc.hdr.fcntl_macros
- libc.hdr.signal_macros
- libc.include.unistd
- libc.include.sys_syscall
+ libc.hdr.unistd_macros
+ libc.src.__support.OSUtil.linux.syscall_wrappers.fork
+ libc.src.__support.OSUtil.linux.syscall_wrappers.gettid
+ libc.src.__support.common
+ libc.src.__support.libc_errno
+ libc.src.__support.macros.config
libc.src.__support.threads.fork_callbacks
- libc.src.__support.OSUtil.osutil
+ libc.src.__support.threads.identifier
libc.src.__support.threads.thread
libc.src.errno.errno
- libc.src.__support.threads.identifier
)
add_entrypoint_object(
diff --git a/libc/src/unistd/linux/fork.cpp b/libc/src/unistd/linux/fork.cpp
index a7529415fa873..ff3b0244c0ab0 100644
--- a/libc/src/unistd/linux/fork.cpp
+++ b/libc/src/unistd/linux/fork.cpp
@@ -1,14 +1,19 @@
-//===-- Linux implementation of fork --------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of fork.
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/fork.h"
-#include "hdr/signal_macros.h" // For SIGCHLD
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/fork.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/gettid.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/__support/threads/fork_callbacks.h"
@@ -16,7 +21,6 @@
#include "src/__support/threads/thread.h" // For thread self object
#include "src/__support/libc_errno.h"
-#include <sys/syscall.h> // For syscall numbers.
namespace LIBC_NAMESPACE_DECL {
@@ -30,33 +34,27 @@ LLVM_LIBC_FUNCTION(pid_t, fork, (void)) {
// process because in the post-fork instruction windows, there may be a signal
// handler triggered which may get the wrong tid.
internal::force_set_tid(0);
-#ifdef SYS_fork
- pid_t ret = syscall_impl<pid_t>(SYS_fork);
-#elif defined(SYS_clone)
- pid_t ret = syscall_impl<pid_t>(SYS_clone, SIGCHLD, 0);
-#else
-#error "fork and clone syscalls not available."
-#endif
-
- if (ret == 0) {
+ auto result = linux_syscalls::fork();
+
+ if (result && result.value() == 0) {
// Return value is 0 in the child process.
// The child is created with a single thread whose self object will be a
// copy of parent process' thread which called fork. So, we have to fix up
// the child process' self object with the new process' tid.
- internal::force_set_tid(syscall_impl<pid_t>(SYS_gettid));
+ internal::force_set_tid(linux_syscalls::gettid());
invoke_child_callbacks();
return 0;
}
- if (ret < 0) {
+ if (!result) {
// Error case, a child process was not created.
- libc_errno = static_cast<int>(-ret);
+ libc_errno = result.error();
return -1;
}
// recover parent's tid.
internal::force_set_tid(parent_tid);
invoke_parent_callbacks();
- return ret;
+ return result.value();
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 22a1a659adeed..3be110a1c7da4 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2865,6 +2865,23 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_fork",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/fork.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_error_or",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ":hdr_signal_macros",
+ ":types_pid_t",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_ftruncate",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h"],
@@ -3592,6 +3609,21 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_syscall_wrappers_gettid",
+ hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/gettid.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_macros_config",
+ ":__support_osutil_syscall",
+ ":types_pid_t",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_linux_syscall_wrappers_getuid",
hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/getuid.h"],
@@ -19077,13 +19109,13 @@ libc_function(
":__support_common",
":__support_libc_errno",
":__support_macros_config",
- ":__support_osutil_syscall",
+ ":__support_osutil_linux_syscall_wrappers_fork",
+ ":__support_osutil_linux_syscall_wrappers_gettid",
":__support_threads_fork_callbacks",
":__support_threads_identifier",
":__support_threads_thread",
":__support_threads_thread_headers",
":errno",
- ":hdr_signal_macros",
":hdr_unistd_macros",
":types_pid_t",
],
``````````
</details>
https://github.com/llvm/llvm-project/pull/242253
More information about the llvm-branch-commits
mailing list