[libc-commits] [libc] 3c67042 - [libc] Move syscall to va_args (#243647)
via libc-commits
libc-commits at lists.llvm.org
Wed Nov 18 14:00:33 PST 2026
Author: Michael Jones
Date: 2026-11-18T14:00:27-08:00
New Revision: 3c67042a157a3b92902ed7d28bc9dec81ddc8dea
URL: https://github.com/llvm/llvm-project/commit/3c67042a157a3b92902ed7d28bc9dec81ddc8dea
DIFF: https://github.com/llvm/llvm-project/commit/3c67042a157a3b92902ed7d28bc9dec81ddc8dea.diff
LOG: [libc] Move syscall to va_args (#243647)
The `syscall` function takes 1-7 arguments. Previously this was
implemented as a macro that filled in the arguments so that the internal
function could always take 7 arguments. The kernel ignores extra
arguments anyways, so it worked fine. Alas, having a macro named
`syscall` caused problems when people defined variables named `syscall`,
and it's not a reserved name.
This PR moves our syscall to match other implementations, reading all 6
optional arguments from va_args.
Assisted-by: Automated tooling, human reviewed.
Added:
Modified:
libc/config/linux/aarch64/entrypoints.txt
libc/config/linux/arm/entrypoints.txt
libc/config/linux/riscv/entrypoints.txt
libc/config/linux/x86_64/entrypoints.txt
libc/include/llvm-libc-macros/linux/unistd-macros.h
libc/include/unistd.yaml
libc/src/unistd/CMakeLists.txt
libc/src/unistd/linux/CMakeLists.txt
libc/src/unistd/linux/syscall.cpp
libc/src/unistd/syscall.h
libc/test/integration/scudo/CMakeLists.txt
libc/test/src/unistd/CMakeLists.txt
libc/test/src/unistd/syscall_test.cpp
Removed:
################################################################################
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index b43c7c670db822..6c940c17656feb 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -529,6 +529,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.symlinkat
libc.src.unistd.sync
libc.src.unistd.syncfs
+ libc.src.unistd.syscall
libc.src.unistd.tcgetpgrp
libc.src.unistd.tcsetpgrp
libc.src.unistd.truncate
@@ -1534,7 +1535,6 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.time.timer_settime
# unistd.h entrypoints
- libc.src.unistd.__llvm_libc_syscall
libc.src.unistd._exit
libc.src.unistd.confstr
libc.src.unistd.environ
diff --git a/libc/config/linux/arm/entrypoints.txt b/libc/config/linux/arm/entrypoints.txt
index 956a154cf0d5ec..a7ed46d851ca74 100644
--- a/libc/config/linux/arm/entrypoints.txt
+++ b/libc/config/linux/arm/entrypoints.txt
@@ -309,6 +309,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.symlinkat
libc.src.unistd.sync
libc.src.unistd.syncfs
+ libc.src.unistd.syscall
libc.src.unistd.tcgetpgrp
libc.src.unistd.tcsetpgrp
libc.src.unistd.truncate
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 6354889fa0edd5..1c7870fcfb9f48 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -575,6 +575,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.symlinkat
libc.src.unistd.sync
libc.src.unistd.syncfs
+ libc.src.unistd.syscall
libc.src.unistd.tcgetpgrp
libc.src.unistd.tcsetpgrp
libc.src.unistd.truncate
@@ -1767,7 +1768,6 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.locale.uselocale
# unistd.h entrypoints
- libc.src.unistd.__llvm_libc_syscall
libc.src.unistd._exit
libc.src.unistd.confstr
libc.src.unistd.environ
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 0e7ae76c11457c..ff18f8417cc8dc 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -575,6 +575,7 @@ set(TARGET_LIBC_ENTRYPOINTS
libc.src.unistd.symlinkat
libc.src.unistd.sync
libc.src.unistd.syncfs
+ libc.src.unistd.syscall
libc.src.unistd.tcgetpgrp
libc.src.unistd.tcsetpgrp
libc.src.unistd.truncate
@@ -1771,7 +1772,6 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.locale.uselocale
# unistd.h entrypoints
- libc.src.unistd.__llvm_libc_syscall
libc.src.unistd._exit
libc.src.unistd.confstr
libc.src.unistd.environ
diff --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
index e2c6c55817bfb6..38c3601a22beac 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -96,15 +96,6 @@
#define _POSIX_THREAD_ROBUST_PRIO_PROTECT -1
#define _POSIX_THREAD_SPORADIC_SERVER -1
-// Macro to set up the call to the __llvm_libc_syscall function
-// This is to prevent the call from having fewer than 6 arguments, since six
-// arguments are always passed to the syscall. Unnecessary arguments are
-// ignored.
-#define __syscall_helper(sysno, arg1, arg2, arg3, arg4, arg5, arg6, ...) \
- __llvm_libc_syscall((long)(sysno), (long)(arg1), (long)(arg2), (long)(arg3), \
- (long)(arg4), (long)(arg5), (long)(arg6))
-#define syscall(...) __syscall_helper(__VA_ARGS__, 0, 1, 2, 3, 4, 5, 6)
-
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(expression) \
(__extension__({ \
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 334fee67f09f4e..84fb02fd097aa8 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -126,18 +126,6 @@ objects:
- object_name: optopt
object_type: int
functions:
- - name: __llvm_libc_syscall
- standards:
- - posix
- return_type: long
- arguments:
- - type: long
- - type: long
- - type: long
- - type: long
- - type: long
- - type: long
- - type: long
- name: _exit
standards:
- posix
@@ -721,6 +709,13 @@ functions:
return_type: int
arguments:
- type: int
+ - name: syscall
+ standards:
+ - gnu
+ return_type: long
+ arguments:
+ - type: long
+ - type: '...'
- name: sysconf
standards:
- posix
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index dbf1155741bd25..c57833ef1ba2ec 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -554,10 +554,10 @@ add_entrypoint_object(
)
add_entrypoint_object(
- __llvm_libc_syscall
+ syscall
ALIAS
DEPENDS
- .${LIBC_TARGET_OS}.__llvm_libc_syscall
+ .${LIBC_TARGET_OS}.syscall
)
add_entrypoint_object(
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index 67313890cf1565..f0777b23764c33 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -1043,14 +1043,15 @@ add_entrypoint_object(
)
add_entrypoint_object(
- __llvm_libc_syscall
+ syscall
SRCS
syscall.cpp
HDRS
../syscall.h
DEPENDS
- libc.include.unistd
libc.src.__support.OSUtil.osutil
+ libc.src.__support.arg_list
+ libc.src.__support.error_or
libc.src.errno.errno
)
diff --git a/libc/src/unistd/linux/syscall.cpp b/libc/src/unistd/linux/syscall.cpp
index 0f7b3da88d6270..b98ec3c79cebc0 100644
--- a/libc/src/unistd/linux/syscall.cpp
+++ b/libc/src/unistd/linux/syscall.cpp
@@ -1,34 +1,56 @@
-//===-- Linux implementation of syscall -----------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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 syscall.
+///
+//===----------------------------------------------------------------------===//
#include "src/unistd/syscall.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/arg_list.h"
#include "src/__support/common.h"
-
+#include "src/__support/error_or.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
+
#include <stdarg.h>
namespace LIBC_NAMESPACE_DECL {
-LLVM_LIBC_FUNCTION(long, __llvm_libc_syscall,
- (long number, long arg1, long arg2, long arg3, long arg4,
- long arg5, long arg6)) {
- long ret = LIBC_NAMESPACE::syscall_impl<long>(number, arg1, arg2, arg3, arg4,
- arg5, arg6);
- // Syscalls may return large positive values that overflow, but will never
- // return values between -4096 and -1
- if (static_cast<unsigned long>(ret) > -4096UL) {
- libc_errno = static_cast<int>(-ret);
+LLVM_LIBC_FUNCTION(long, syscall, (long number, ...)) {
+ va_list vlist;
+ va_start(vlist, number);
+ internal::ArgList args(vlist);
+ va_end(vlist);
+
+ // Like other libcs, this always reads six long-sized arguments, even when
+ // the caller passed fewer. Reading past the end of a va_list is technically
+ // UB (FIXME: this could interact badly with LTO / ExpandVariadics if enabled
+ // on more targets), though on the ABIs we support the extra values are just
+ // harmless garbage ignored by the kernel past a syscall's arity. Callers must
+ // pass every argument as a long-sized value (pointers, long, or a cast) and
+ // split 64-bit arguments themselves on 32-bit targets.
+ long arg1 = args.next_var<long>();
+ long arg2 = args.next_var<long>();
+ long arg3 = args.next_var<long>();
+ long arg4 = args.next_var<long>();
+ long arg5 = args.next_var<long>();
+ long arg6 = args.next_var<long>();
+
+ ErrorOr<long> syscall_result = linux_syscalls::syscall_checked<long>(
+ number, arg1, arg2, arg3, arg4, arg5, arg6);
+ if (!syscall_result.has_value()) {
+ libc_errno = syscall_result.error();
return -1;
}
- return ret;
+ return syscall_result.value();
}
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/syscall.h b/libc/src/unistd/syscall.h
index 7f82bd8a452f62..6b981d8d0ba4f6 100644
--- a/libc/src/unistd/syscall.h
+++ b/libc/src/unistd/syscall.h
@@ -1,22 +1,24 @@
-//===-- Implementation header for syscall -----------------------*- C++ -*-===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Implementation header for syscall.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC_UNISTD_SYSCALL_H
#define LLVM_LIBC_SRC_UNISTD_SYSCALL_H
-#include "hdr/unistd_macros.h"
#include "src/__support/macros/config.h"
-#include <stdarg.h>
namespace LIBC_NAMESPACE_DECL {
-long __llvm_libc_syscall(long number, long arg1, long arg2, long arg3,
- long arg4, long arg5, long arg6);
+long syscall(long number, ...);
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/integration/scudo/CMakeLists.txt b/libc/test/integration/scudo/CMakeLists.txt
index 74709803972cdb..c51dcf4315d1d5 100644
--- a/libc/test/integration/scudo/CMakeLists.txt
+++ b/libc/test/integration/scudo/CMakeLists.txt
@@ -25,9 +25,9 @@ add_entrypoint_library(
libc.src.sys.mman.mmap
libc.src.sys.mman.munmap
libc.src.sys.prctl.prctl
- libc.src.unistd.__llvm_libc_syscall
libc.src.unistd.close
libc.src.unistd.read
+ libc.src.unistd.syscall
libc.src.unistd.sysconf
libc.src.unistd.write
)
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 3ce09ee110e0df..63ee7b17882076 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -1051,11 +1051,13 @@ add_libc_test(
SRCS
syscall_test.cpp
DEPENDS
+ libc.hdr.errno_macros
libc.hdr.sys_stat_macros
libc.include.unistd
libc.hdr.fcntl_macros
libc.include.sys_syscall
- libc.src.unistd.__llvm_libc_syscall
+ libc.src.unistd.getpid
+ libc.src.unistd.syscall
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
diff --git a/libc/test/src/unistd/syscall_test.cpp b/libc/test/src/unistd/syscall_test.cpp
index 1c696dd7b2ea12..e601765bb43b12 100644
--- a/libc/test/src/unistd/syscall_test.cpp
+++ b/libc/test/src/unistd/syscall_test.cpp
@@ -1,37 +1,51 @@
-//===-- Unittests for syscalls --------------------------------------------===//
+//===----------------------------------------------------------------------===//
//
// 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
+/// Unittests for syscall.
+///
+//===----------------------------------------------------------------------===//
+#include "src/unistd/getpid.h"
#include "src/unistd/syscall.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
+#include "hdr/errno_macros.h"
#include "hdr/fcntl_macros.h"
#include "hdr/sys_stat_macros.h" // For S_* flags.
-#include <sys/syscall.h> // For syscall numbers.
+#include <sys/syscall.h> // For syscall numbers.
#include <unistd.h>
-using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LlvmLibcSyscallTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
// We only do a smoke test here. Actual functionality tests are
// done by the unit tests of the syscall wrappers like mmap.
// The goal is to test syscalls with a wide number of args.
-// There is no function named "syscall" in llvm-libc, we instead use a macro to
-// set up the arguments properly. We still need to specify the namespace though
-// because the macro generates a call to the actual internal function
-// (__llvm_libc_syscall) which is inside the namespace.
-TEST_F(LlvmLibcSyscallTest, TrivialCall) {
- ASSERT_GE(LIBC_NAMESPACE::syscall(SYS_gettid), 0l);
+TEST_F(LlvmLibcSyscallTest, GetPid) {
+ ASSERT_EQ(LIBC_NAMESPACE::syscall(SYS_getpid),
+ static_cast<long>(LIBC_NAMESPACE::getpid()));
ASSERT_ERRNO_SUCCESS();
}
+TEST_F(LlvmLibcSyscallTest, InvalidSyscallNumber) {
+ // Use an out-of-range positive syscall number below 0x0f0000
+ // (__ARM_NR_BASE). On 32-bit ARM, syscall numbers >= 0x0f0000 (including -1,
+ // which wraps to 0xffffffff) are routed to the ARM-private syscall handler
+ // and raise SIGILL instead of returning ENOSYS.
+ constexpr long INVALID_SYSCALL_NUMBER = 0xffff;
+ ASSERT_THAT(LIBC_NAMESPACE::syscall(INVALID_SYSCALL_NUMBER),
+ Fails(ENOSYS, -1L));
+}
+
TEST_F(LlvmLibcSyscallTest, SymlinkCreateDestroy) {
constexpr const char LINK_VAL[] = "syscall_readlink_test_value";
constexpr const char LINK[] = "testdata/syscall_readlink.test.link";
@@ -85,8 +99,12 @@ TEST_F(LlvmLibcSyscallTest, FileReadWrite) {
ASSERT_GT(fd, 0l);
ASSERT_ERRNO_SUCCESS();
- ASSERT_GE(LIBC_NAMESPACE::syscall(SYS_pwrite64, fd, HELLO, HELLO_SIZE, 0),
- 0l);
+ // Pass three zero args for the offset: 64-bit targets use arg4 and ignore the
+ // rest, 32-bit x86/RISC-V use arg4/arg5 for the split 64-bit offset, and
+ // 32-bit ARM EABI requires a padding register in arg4 before arg5/arg6.
+ ASSERT_GE(
+ LIBC_NAMESPACE::syscall(SYS_pwrite64, fd, HELLO, HELLO_SIZE, 0L, 0L, 0L),
+ 0l);
ASSERT_ERRNO_SUCCESS();
ASSERT_GE(LIBC_NAMESPACE::syscall(SYS_fsync, fd), 0l);
More information about the libc-commits
mailing list