[libc-commits] [libc] [libc] Migrate file I/O syscall wrappers to syscall_checked (PR #226477)
via libc-commits
libc-commits at lists.llvm.org
Fri Sep 25 06:04:25 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Pavel Labath (labath)
<details>
<summary>Changes</summary>
Migrate a batch of related file descriptor and core I/O syscall wrappers to use syscall_checked instead of manual syscall_impl error checking.
While in there:
- update file headers to the new style
- reformat includes into a single block so clang-format can sort them
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/226477.diff
11 Files Affected:
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/close.h (+8-6)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h (+2-5)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h (+3-6)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h (+2-5)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h (+9-15)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h (+2-5)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h (+9-7)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h (+9-11)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h (+2-5)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/read.h (+8-6)
- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/write.h (+8-6)
``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h
index fe305af5051c0..88c71d9d5af74 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/close.h
@@ -1,15 +1,20 @@
-//===-- Implementation header for close -------------------------*- 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
+/// Syscall wrapper for close.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOSE_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOSE_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -19,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> close(int fd) {
- int ret = syscall_impl<int>(SYS_close, fd);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<int>(SYS_close, fd);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h
index 3d54b684bd66e..9d61414993072 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup.h
@@ -14,7 +14,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> dup(int fd) {
- int ret = syscall_impl<int>(SYS_dup, fd);
- if (ret < 0)
- return Error(-ret);
- return ret;
+ return syscall_checked<int>(SYS_dup, fd);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h
index 78da9801895ab..55687c3dad9c7 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup2.h
@@ -15,7 +15,7 @@
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP2_H
#include "hdr/fcntl_macros.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
#include "src/__support/OSUtil/linux/syscall_wrappers/fcntl.h"
#include "src/__support/common.h"
#include "src/__support/error_or.h"
@@ -27,7 +27,7 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> dup2(int oldfd, int newfd) {
#ifdef SYS_dup2
- int ret = syscall_impl<int>(SYS_dup2, oldfd, newfd);
+ return syscall_checked<int>(SYS_dup2, oldfd, newfd);
#elif defined(SYS_dup3)
if (oldfd == newfd) {
auto ret = fcntl(oldfd, F_GETFD);
@@ -35,13 +35,10 @@ LIBC_INLINE ErrorOr<int> dup2(int oldfd, int newfd) {
return Error(ret.error());
return oldfd;
}
- int ret = syscall_impl<int>(SYS_dup3, oldfd, newfd, 0);
+ return syscall_checked<int>(SYS_dup3, oldfd, newfd, 0);
#else
#error "dup2 and dup3 syscalls not available."
#endif
- if (ret < 0)
- return Error(-ret);
- return ret;
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h
index 3be0ad4526e90..e112cb85d8f64 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/dup3.h
@@ -14,7 +14,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP3_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_DUP3_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> dup3(int oldfd, int newfd, int flags) {
- int ret = syscall_impl<int>(SYS_dup3, oldfd, newfd, flags);
- if (ret < 0)
- return Error(-ret);
- return ret;
+ return syscall_checked<int>(SYS_dup3, oldfd, newfd, flags);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h
index 0581693f9612c..5bc3482bfe192 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fcntl.h
@@ -20,7 +20,7 @@
#include "hdr/types/struct_f_owner_ex.h"
#include "hdr/types/struct_flock.h"
#include "hdr/types/struct_flock64.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -49,10 +49,7 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
flk64.l_len = flk->l_len;
flk64.l_pid = flk->l_pid;
// create a syscall
- int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
- if (ret < 0)
- return Error(-ret);
- return ret;
+ return syscall_checked<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
}
case F_OFD_GETLK:
case F_OFD_SETLK: {
@@ -65,10 +62,10 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
flk64.l_len = flk->l_len;
flk64.l_pid = flk->l_pid;
// create a syscall
- int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
+ auto ret = syscall_checked<int>(FCNTL_SYSCALL_ID, fd, cmd, &flk64);
// On failure, return
- if (ret < 0)
- return Error(-ret);
+ if (!ret)
+ return ret;
// Check for overflow, i.e. the offsets are not the same when cast
// to off_t from off64_t.
if (static_cast<off_t>(flk64.l_len) != flk64.l_len ||
@@ -85,9 +82,9 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
}
case F_GETOWN: {
struct f_owner_ex fex;
- int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, F_GETOWN_EX, &fex);
- if (ret < 0)
- return Error(-ret);
+ auto ret = syscall_checked<int>(FCNTL_SYSCALL_ID, fd, F_GETOWN_EX, &fex);
+ if (!ret)
+ return ret;
return fex.type == F_OWNER_PGRP ? -fex.pid : fex.pid;
}
#ifdef SYS_fcntl64
@@ -112,10 +109,7 @@ LIBC_INLINE ErrorOr<int> fcntl(int fd, int cmd, void *arg = nullptr) {
// Plain passthrough for all other commands. When only SYS_fcntl64 is
// available, F_GETLK/F_SETLK/F_SETLKW have been rewritten to their 64-bit
// variants by the cases above.
- int ret = syscall_impl<int>(FCNTL_SYSCALL_ID, fd, cmd, arg);
- if (ret < 0)
- return Error(-ret);
- return ret;
+ return syscall_checked<int>(FCNTL_SYSCALL_ID, fd, cmd, arg);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h
index 1ebbca23f3019..4b0a864b6992e 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/fsync.h
@@ -14,7 +14,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FSYNC_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_FSYNC_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> fsync(int fd) {
- int ret = syscall_impl<int>(SYS_fsync, fd);
- if (ret < 0)
- return Error(-ret);
- return ret;
+ return syscall_checked<int>(SYS_fsync, fd);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h
index 6835de85d3321..76d692d589ec4 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/ftruncate.h
@@ -1,16 +1,21 @@
-//===-- Implementation header for ftruncate ---------------------*- 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
+/// Syscall wrapper for ftruncate.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_FTRUNCATE_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_FTRUNCATE_H
#include "hdr/types/off_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -21,18 +26,15 @@ namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> ftruncate(int fd, off_t len) {
#ifdef SYS_ftruncate
- int ret = syscall_impl<int>(SYS_ftruncate, fd, len);
+ return syscall_checked<int>(SYS_ftruncate, fd, len);
#elif defined(SYS_ftruncate64)
// Same as ftruncate but can handle large offsets on 32-bit systems.
static_assert(sizeof(off_t) == 8);
- int ret = syscall_impl<int>(SYS_ftruncate64, fd, (long)len,
+ return syscall_checked<int>(SYS_ftruncate64, fd, (long)len,
(long)(((uint64_t)(len)) >> 32));
#else
#error "ftruncate and ftruncate64 syscalls not available."
#endif
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return 0;
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h
index 861a1174d367f..30b17328a8f17 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/lseek.h
@@ -16,7 +16,7 @@
#include "hdr/stdint_proxy.h"
#include "hdr/types/off_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -26,27 +26,25 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<off_t> lseek(int fd, off_t offset, int whence) {
- off_t result;
#ifdef SYS_lseek
- result = syscall_impl<off_t>(SYS_lseek, fd, offset, whence);
- if (result < 0)
- return Error(-static_cast<int>(result));
+ return syscall_checked<off_t>(SYS_lseek, fd, offset, whence);
#elif defined(SYS_llseek) || defined(SYS__llseek)
#ifdef SYS_llseek
constexpr long LLSEEK_SYSCALL_NO = SYS_llseek;
#elif defined(SYS__llseek)
constexpr long LLSEEK_SYSCALL_NO = SYS__llseek;
#endif
+ off_t result;
uint64_t offset_64 = static_cast<uint64_t>(offset);
- int ret = syscall_impl<int>(LLSEEK_SYSCALL_NO, fd,
- static_cast<long>(offset_64 >> 32),
- static_cast<long>(offset_64), &result, whence);
- if (ret < 0)
- return Error(-ret);
+ auto ret = syscall_checked<int>(
+ LLSEEK_SYSCALL_NO, fd, static_cast<long>(offset_64 >> 32),
+ static_cast<long>(offset_64), &result, whence);
+ if (!ret)
+ return Error(ret.error());
+ return result;
#else
#error "lseek, llseek and _llseek syscalls not available."
#endif
- return result;
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h
index e748c3c6fb80d..b54ffbbf6e8fc 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/memfd_create.h
@@ -14,7 +14,7 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MEMFD_CREATE_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MEMFD_CREATE_H
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -24,10 +24,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<int> memfd_create(const char *name, unsigned int flags) {
- int ret = syscall_impl<int>(SYS_memfd_create, name, flags);
- if (ret < 0)
- return Error(-ret);
- return ret;
+ return syscall_checked<int>(SYS_memfd_create, name, flags);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h
index 86123904fc6b2..c30a0bb3480e9 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/read.h
@@ -1,16 +1,21 @@
-//===-- Implementation header for read --------------------------*- 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
+/// Syscall wrapper for read.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_READ_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_READ_H
#include "hdr/types/ssize_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -20,10 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<ssize_t> read(int fd, void *buf, size_t count) {
- ssize_t ret = syscall_impl<ssize_t>(SYS_read, fd, buf, count);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<ssize_t>(SYS_read, fd, buf, count);
}
} // namespace linux_syscalls
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h
index e57986d357a21..d253ee68d764c 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/write.h
@@ -1,16 +1,21 @@
-//===-- Implementation header for write -------------------------*- 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
+/// Syscall wrapper for write.
+///
+//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_WRITE_H
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_WRITE_H
#include "hdr/types/ssize_t.h"
-#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#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"
@@ -20,10 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
namespace linux_syscalls {
LIBC_INLINE ErrorOr<ssize_t> write(int fd, const void *buf, size_t count) {
- ssize_t ret = syscall_impl<ssize_t>(SYS_write, fd, buf, count);
- if (ret < 0)
- return Error(-static_cast<int>(ret));
- return ret;
+ return syscall_checked<ssize_t>(SYS_write, fd, buf, count);
}
} // namespace linux_syscalls
``````````
</details>
https://github.com/llvm/llvm-project/pull/226477
More information about the libc-commits
mailing list