[libc-commits] [libc] [libc] Move syscall to va_args (PR #243647)
Michael Jones via libc-commits
libc-commits at lists.llvm.org
Tue Nov 17 14:59:14 PST 2026
https://github.com/michaelrj-google created https://github.com/llvm/llvm-project/pull/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.
>From 7d91ba7aa009fbb27ea061dd071643239d1000b0 Mon Sep 17 00:00:00 2001
From: Michael Jones <michaelrj at google.com>
Date: Tue, 17 Nov 2026 22:42:34 +0000
Subject: [PATCH] [libc] Move syscall to va_args
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.
---
libc/config/linux/aarch64/entrypoints.txt | 2 +-
libc/config/linux/arm/entrypoints.txt | 1 +
libc/config/linux/riscv/entrypoints.txt | 2 +-
libc/config/linux/x86_64/entrypoints.txt | 2 +-
.../llvm-libc-macros/linux/unistd-macros.h | 9 ----
libc/include/unistd.yaml | 19 +++-----
libc/src/unistd/CMakeLists.txt | 4 +-
libc/src/unistd/linux/CMakeLists.txt | 5 +-
libc/src/unistd/linux/syscall.cpp | 46 +++++++++++++------
libc/src/unistd/syscall.h | 12 +++--
libc/test/integration/scudo/CMakeLists.txt | 2 +-
libc/test/src/unistd/CMakeLists.txt | 4 +-
libc/test/src/unistd/syscall_test.cpp | 28 +++++++----
13 files changed, 79 insertions(+), 57 deletions(-)
diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 291b11da09d6ad..9dd7b3f7fc6599 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -523,6 +523,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
@@ -1517,7 +1518,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 2927e66c631d0b..791aaab1f5657c 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 b8082355d8c5af..d32872933d464a 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -569,6 +569,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
@@ -1748,7 +1749,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 59d5a4ab376732..0e0c27c5b23eb8 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -569,6 +569,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
@@ -1752,7 +1753,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 85f3e441ed2060..5fc6e5c0479196 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 f179b86543e5d3..196bceacff5252 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -1042,14 +1042,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..7fea0042919f0f 100644
--- a/libc/src/unistd/linux/syscall.cpp
+++ b/libc/src/unistd/linux/syscall.cpp
@@ -1,34 +1,54 @@
-//===-- 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. The extra values are garbage, but the kernel
+ // ignores registers 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..b9e0536b771047 100644
--- a/libc/test/src/unistd/syscall_test.cpp
+++ b/libc/test/src/unistd/syscall_test.cpp
@@ -1,37 +1,47 @@
-//===-- 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) {
+ constexpr long INVALID_SYSCALL_NUMBER = -1;
+ 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";
More information about the libc-commits
mailing list