[libc-commits] [libc] Syscall wrappers (PR #204176)
Jeff Bailey via libc-commits
libc-commits at lists.llvm.org
Tue Jun 16 08:01:06 PDT 2026
https://github.com/kaladron created https://github.com/llvm/llvm-project/pull/204176
None
>From 515292f4c7f3d3b9b1054d0e1a14046e543d8727 Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 16 Jun 2026 15:51:54 +0100
Subject: [PATCH 1/3] [libc][NFC] Use link syscall wrapper
Updated the link entrypoint and its CMake dependencies to use the
linux_syscalls::link wrapper.
Assisted-by: Automated tooling, human reviewed.
---
libc/src/unistd/linux/CMakeLists.txt | 5 +----
libc/src/unistd/linux/link.cpp | 20 +++++---------------
2 files changed, 6 insertions(+), 19 deletions(-)
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index be057b4f80972..c5f6875901473 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -359,10 +359,7 @@ add_entrypoint_object(
HDRS
../link.h
DEPENDS
- libc.hdr.fcntl_macros
- libc.include.unistd
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.src.__support.OSUtil.linux.syscall_wrappers.link
libc.src.errno.errno
)
diff --git a/libc/src/unistd/linux/link.cpp b/libc/src/unistd/linux/link.cpp
index 205cf8a84a5cb..d75c02eed0775 100644
--- a/libc/src/unistd/linux/link.cpp
+++ b/libc/src/unistd/linux/link.cpp
@@ -8,30 +8,20 @@
#include "src/unistd/link.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/link.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include "hdr/fcntl_macros.h"
-#include <sys/syscall.h> // For syscall numbers.
-
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, link, (const char *path1, const char *path2)) {
-#ifdef SYS_link
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_link, path1, path2);
-#elif defined(SYS_linkat)
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_linkat, AT_FDCWD, path1,
- AT_FDCWD, path2, 0);
-#else
-#error "link or linkat syscalls not available."
-#endif
- if (ret < 0) {
- libc_errno = -ret;
+ auto result = linux_syscalls::link(path1, path2);
+ if (!result) {
+ libc_errno = result.error();
return -1;
}
- return ret;
+ return result.value();
}
} // namespace LIBC_NAMESPACE_DECL
>From 3492af7d97aca26aff7c66368a08f961de819464 Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 16 Jun 2026 15:53:24 +0100
Subject: [PATCH 2/3] [libc][NFC] Use ftruncate syscall wrapper
Updated the ftruncate entrypoint and its CMake dependencies to use the
linux_syscalls::ftruncate wrapper.
Assisted-by: Automated tooling, human reviewed.
---
libc/src/unistd/linux/CMakeLists.txt | 6 +-----
libc/src/unistd/linux/ftruncate.cpp | 22 ++++------------------
2 files changed, 5 insertions(+), 23 deletions(-)
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index c5f6875901473..ce4cfe04803bb 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -207,11 +207,7 @@ add_entrypoint_object(
../ftruncate.h
DEPENDS
libc.hdr.types.off_t
- libc.hdr.fcntl_macros
- libc.hdr.stdint_proxy
- libc.include.unistd
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.src.__support.OSUtil.linux.syscall_wrappers.ftruncate
libc.src.errno.errno
)
diff --git a/libc/src/unistd/linux/ftruncate.cpp b/libc/src/unistd/linux/ftruncate.cpp
index b4729f8d50d73..2540fff999de3 100644
--- a/libc/src/unistd/linux/ftruncate.cpp
+++ b/libc/src/unistd/linux/ftruncate.cpp
@@ -8,31 +8,17 @@
#include "src/unistd/ftruncate.h"
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h"
#include "src/__support/common.h"
-
-#include "hdr/stdint_proxy.h" // For uint64_t.
-#include "hdr/unistd_macros.h"
#include "src/__support/libc_errno.h"
#include "src/__support/macros/config.h"
-#include <sys/syscall.h> // For syscall numbers.
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, ftruncate, (int fd, off_t len)) {
-#ifdef SYS_ftruncate
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ftruncate, fd, len);
-#elif defined(SYS_ftruncate64)
- // Same as ftruncate but can handle large offsets
- static_assert(sizeof(off_t) == 8);
- int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_ftruncate64, fd, (long)len,
- (long)(((uint64_t)(len)) >> 32));
-#else
-#error "ftruncate and ftruncate64 syscalls not available."
-#endif
-
- if (ret < 0) {
- libc_errno = -ret;
+ auto result = linux_syscalls::ftruncate(fd, len);
+ if (!result) {
+ libc_errno = result.error();
return -1;
}
return 0;
>From 0d416e5f3d5b4a736793e8c20e942ea0bdc5e15b Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 16 Jun 2026 15:55:00 +0100
Subject: [PATCH 3/3] [libc][NFC] Use getrandom syscall wrapper in getentropy
Updated the getentropy entrypoint and its CMake dependencies to use the
linux_syscalls::getrandom wrapper.
Assisted-by: Automated tooling, human reviewed.
---
libc/src/unistd/linux/CMakeLists.txt | 3 +--
libc/src/unistd/linux/getentropy.cpp | 14 ++++++--------
2 files changed, 7 insertions(+), 10 deletions(-)
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index ce4cfe04803bb..77d6f718c304d 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -659,7 +659,6 @@ add_entrypoint_object(
libc.hdr.types.size_t
libc.hdr.types.ssize_t
libc.hdr.errno_macros
- libc.include.sys_syscall
- libc.src.__support.OSUtil.osutil
+ libc.src.__support.OSUtil.linux.syscall_wrappers.getrandom
libc.src.errno.errno
)
diff --git a/libc/src/unistd/linux/getentropy.cpp b/libc/src/unistd/linux/getentropy.cpp
index 65bcbf27601da..7f398ccf21b7d 100644
--- a/libc/src/unistd/linux/getentropy.cpp
+++ b/libc/src/unistd/linux/getentropy.cpp
@@ -8,12 +8,10 @@
#include "src/unistd/getentropy.h"
#include "hdr/errno_macros.h"
-#include "src/__support/OSUtil/syscall.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getrandom.h"
#include "src/__support/common.h"
#include "src/__support/libc_errno.h"
-#include <sys/syscall.h> // For syscall numbers.
-
namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(int, getentropy, (void *buffer, size_t length)) {
// check the length limit
@@ -26,16 +24,16 @@ LLVM_LIBC_FUNCTION(int, getentropy, (void *buffer, size_t length)) {
while (length != 0) {
// 0 flag means urandom and blocking, which meets the assumption of
// getentropy
- auto ret = syscall_impl<long>(SYS_getrandom, cursor, length, 0);
+ auto result = linux_syscalls::getrandom(cursor, length, 0);
// on success, advance the buffer pointer
- if (ret >= 0) {
- length -= static_cast<size_t>(ret);
- cursor += ret;
+ if (result) {
+ length -= static_cast<size_t>(result.value());
+ cursor += result.value();
continue;
}
- auto error = -static_cast<int>(ret);
+ auto error = result.error();
// on EINTR, try again
if (error == EINTR)
More information about the libc-commits
mailing list