[libc-commits] [libc] [llvm] [libc] Use syscall wrappers for fork (PR #242253)
Connector Switch via libc-commits
libc-commits at lists.llvm.org
Fri Nov 13 23:41:11 PST 2026
https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/242253
>From b45761ecc766e4e6da2744539893bb26613c37fc Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Fri, 13 Nov 2026 18:46:22 +0800
Subject: [PATCH] [libc] Implement fork with the clone3 syscall
---
.../linux/syscall_wrappers/CMakeLists.txt | 12 ++++++
.../OSUtil/linux/syscall_wrappers/gettid.h | 31 +++++++++++++++
libc/src/unistd/linux/CMakeLists.txt | 14 ++++---
libc/src/unistd/linux/fork.cpp | 38 ++++++++++---------
.../OSUtil/linux/syscall_wrappers/BUILD.bazel | 31 +++++++++++++++
.../libc/src/unistd/BUILD.bazel | 3 +-
6 files changed, 105 insertions(+), 24 deletions(-)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 4a417280c4c568..322795b34ab298 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -100,6 +100,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
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 00000000000000..42f7862326f6ee
--- /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 f179b86543e5d3..c4807abb0a19a6 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -209,16 +209,18 @@ add_entrypoint_object(
HDRS
../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.types.pid_t
+ libc.hdr.unistd_macros
+ libc.src.__support.OSUtil.linux.syscall_wrappers.clone3
+ 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 a7529415fa873e..2ae82c25271f6e 100644
--- a/libc/src/unistd/linux/fork.cpp
+++ b/libc/src/unistd/linux/fork.cpp
@@ -1,14 +1,21 @@
-//===-- 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 "hdr/signal_macros.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/clone3.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 +23,8 @@
#include "src/__support/threads/thread.h" // For thread self object
#include "src/__support/libc_errno.h"
-#include <sys/syscall.h> // For syscall numbers.
+
+#include <linux/sched.h> // For struct clone_args
namespace LIBC_NAMESPACE_DECL {
@@ -30,33 +38,29 @@ 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) {
+ struct clone_args args{};
+ args.exit_signal = SIGCHLD;
+ auto result = linux_syscalls::clone3(&args, sizeof(args));
+
+ 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/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
index 482e9e4bf11f38..fba8f752f47ef3 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
@@ -990,6 +990,21 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "gettid",
+ hdrs = ["gettid.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ "//libc/hdr/types:pid_t",
+ "//libc/src/__support:common",
+ "//libc/src/__support/OSUtil:syscall",
+ "//libc/src/__support/macros:config",
+ ],
+)
+
libc_support_library(
name = "getuid",
hdrs = ["getuid.h"],
@@ -1333,6 +1348,22 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "clone3",
+ hdrs = ["clone3.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ "//libc/hdr/types:pid_t",
+ "//libc/src/__support:common",
+ "//libc/src/__support:error_or",
+ "//libc/src/__support/OSUtil:syscall",
+ "//libc/src/__support/macros:config",
+ ],
+)
+
libc_support_library(
name = "clock_gettime",
hdrs = ["clock_gettime.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel
index 572162a076bcdf..86b476d4b41c09 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel
@@ -303,7 +303,8 @@ libc_function(
"//libc/hdr/types:pid_t",
"//libc/src/__support:common",
"//libc/src/__support:libc_errno",
- "//libc/src/__support/OSUtil:syscall",
+ "//libc/src/__support/OSUtil/linux/syscall_wrappers:clone3",
+ "//libc/src/__support/OSUtil/linux/syscall_wrappers:gettid",
"//libc/src/__support/macros:config",
"//libc/src/__support/threads:fork_callbacks",
"//libc/src/__support/threads:identifier",
More information about the libc-commits
mailing list