From f04bf4791ae6d56b822f9bca6eb82ce4adccb989 Mon Sep 17 00:00:00 2001 From: "Mikhail R. Gadelha" Date: Fri, 17 Jul 2026 14:23:39 -0300 Subject: [PATCH] [libc][NFC] Port raw syscall users to existing syscall_wrappers (#208039) Several call sites still invoked syscall_impl directly (or carried their own SYS_* fallback ladders) for syscalls that already have a wrapper in src/__support/OSUtil/linux/syscall_wrappers. This PR removes these syscalls in favor of the syscall_wrappers. --- libc/src/__support/File/linux/CMakeLists.txt | 6 ++-- libc/src/__support/File/linux/dir.cpp | 7 ++-- libc/src/__support/File/linux/file.cpp | 30 ++++++++-------- .../src/__support/OSUtil/linux/CMakeLists.txt | 3 ++ libc/src/__support/OSUtil/linux/auxv.h | 21 +++++++----- .../__support/threads/linux/CMakeLists.txt | 3 ++ libc/src/__support/threads/linux/thread.cpp | 26 ++++++++------ libc/src/sched/linux/CMakeLists.txt | 3 +- libc/src/sched/linux/sched_getaffinity.cpp | 14 ++++---- libc/src/spawn/linux/CMakeLists.txt | 1 + libc/src/spawn/linux/posix_spawn.cpp | 7 ++-- libc/src/stdio/linux/CMakeLists.txt | 4 +-- libc/src/stdio/linux/remove.cpp | 15 ++++---- libc/src/sys/ipc/linux/CMakeLists.txt | 15 ++------ libc/src/sys/ipc/linux/ftok.cpp | 11 +++--- libc/src/sys/ipc/linux/kernel_statx.h | 34 ------------------- libc/src/sys/mman/linux/CMakeLists.txt | 1 + libc/src/sys/mman/linux/shm_unlink.cpp | 23 ++++--------- libc/src/sys/time/linux/CMakeLists.txt | 2 -- libc/src/sys/time/linux/utimes.cpp | 8 ----- libc/startup/linux/x86_64/CMakeLists.txt | 1 + libc/startup/linux/x86_64/tls.cpp | 11 +++--- .../llvm-project-overlay/libc/BUILD.bazel | 13 ++++--- 23 files changed, 111 insertions(+), 148 deletions(-) delete mode 100644 libc/src/sys/ipc/linux/kernel_statx.h diff --git a/libc/src/__support/File/linux/CMakeLists.txt b/libc/src/__support/File/linux/CMakeLists.txt index 87ee7423a362..336854d4429d 100644 --- a/libc/src/__support/File/linux/CMakeLists.txt +++ b/libc/src/__support/File/linux/CMakeLists.txt @@ -11,12 +11,13 @@ add_object_library( libc.hdr.sys_stat_macros libc.hdr.types.FILE libc.hdr.types.off_t - libc.include.sys_syscall libc.src.__support.CPP.new - libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.close libc.src.__support.OSUtil.linux.syscall_wrappers.fcntl libc.src.__support.OSUtil.linux.syscall_wrappers.lseek libc.src.__support.OSUtil.linux.syscall_wrappers.open + libc.src.__support.OSUtil.linux.syscall_wrappers.read + libc.src.__support.OSUtil.linux.syscall_wrappers.write libc.src.__support.error_or libc.src.__support.File.file libc.src.errno.errno @@ -33,6 +34,7 @@ add_object_library( libc.hdr.sys_stat_macros libc.src.__support.OSUtil.linux.stat.kernel_statx_types libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.close libc.src.__support.OSUtil.linux.syscall_wrappers.open libc.src.__support.OSUtil.linux.syscall_wrappers.fcntl libc.src.__support.OSUtil.linux.syscall_wrappers.statx diff --git a/libc/src/__support/File/linux/dir.cpp b/libc/src/__support/File/linux/dir.cpp index 0f194d44de3d..2b6bb4b0b6c5 100644 --- a/libc/src/__support/File/linux/dir.cpp +++ b/libc/src/__support/File/linux/dir.cpp @@ -15,6 +15,7 @@ #include "hdr/fcntl_macros.h" // For open flags #include "hdr/sys_stat_macros.h" // For S_ISDIR #include "src/__support/OSUtil/linux/stat/kernel_statx_types.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/close.h" #include "src/__support/OSUtil/linux/syscall_wrappers/fcntl.h" #include "src/__support/OSUtil/linux/syscall_wrappers/open.h" #include "src/__support/OSUtil/linux/syscall_wrappers/statx.h" @@ -42,9 +43,9 @@ ErrorOr platform_fetch_dirents(int fd, cpp::span buffer) { } int platform_closedir(int fd) { - int ret = LIBC_NAMESPACE::syscall_impl(SYS_close, fd); - if (ret < 0) - return static_cast(-ret); + auto ret = linux_syscalls::close(fd); + if (!ret) + return ret.error(); return 0; } diff --git a/libc/src/__support/File/linux/file.cpp b/libc/src/__support/File/linux/file.cpp index 61735e153086..91f94d8f5ab9 100644 --- a/libc/src/__support/File/linux/file.cpp +++ b/libc/src/__support/File/linux/file.cpp @@ -14,36 +14,34 @@ #include "hdr/types/off_t.h" #include "src/__support/CPP/new.h" #include "src/__support/File/file.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/close.h" #include "src/__support/OSUtil/linux/syscall_wrappers/fcntl.h" #include "src/__support/OSUtil/linux/syscall_wrappers/lseek.h" #include "src/__support/OSUtil/linux/syscall_wrappers/open.h" -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/OSUtil/linux/syscall_wrappers/read.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/write.h" #include "src/__support/alloc-checker.h" #include "src/__support/libc_errno.h" // For error macros #include "src/__support/macros/config.h" -#include // For syscall numbers - namespace LIBC_NAMESPACE_DECL { FileIOResult linux_file_write(File *f, const void *data, size_t size) { auto *lf = reinterpret_cast(f); - int ret = - LIBC_NAMESPACE::syscall_impl(SYS_write, lf->get_fd(), data, size); - if (ret < 0) { - return {0, -ret}; + auto ret = linux_syscalls::write(lf->get_fd(), data, size); + if (!ret) { + return {0, ret.error()}; } - return ret; + return static_cast(ret.value()); } FileIOResult linux_file_read(File *f, void *buf, size_t size) { auto *lf = reinterpret_cast(f); - int ret = - LIBC_NAMESPACE::syscall_impl(SYS_read, lf->get_fd(), buf, size); - if (ret < 0) { - return {0, -ret}; + auto ret = linux_syscalls::read(lf->get_fd(), buf, size); + if (!ret) { + return {0, ret.error()}; } - return ret; + return static_cast(ret.value()); } ErrorOr linux_file_seek(File *f, off_t offset, int whence) { @@ -54,9 +52,9 @@ ErrorOr linux_file_seek(File *f, off_t offset, int whence) { int linux_file_close(File *f) { File::remove_file(f); auto *lf = reinterpret_cast(f); - int ret = LIBC_NAMESPACE::syscall_impl(SYS_close, lf->get_fd()); - if (ret < 0) { - return -ret; + auto ret = linux_syscalls::close(lf->get_fd()); + if (!ret) { + return ret.error(); } delete lf; return 0; diff --git a/libc/src/__support/OSUtil/linux/CMakeLists.txt b/libc/src/__support/OSUtil/linux/CMakeLists.txt index ad8bbe658c1b..87d0fb862fad 100644 --- a/libc/src/__support/OSUtil/linux/CMakeLists.txt +++ b/libc/src/__support/OSUtil/linux/CMakeLists.txt @@ -25,12 +25,15 @@ add_header_library( HDRS auxv.h DEPENDS + libc.hdr.errno_macros libc.hdr.fcntl_macros libc.hdr.sys_auxv_macros libc.hdr.sys_mman_macros + libc.src.__support.OSUtil.linux.syscall_wrappers.close libc.src.__support.OSUtil.linux.syscall_wrappers.mmap libc.src.__support.OSUtil.linux.syscall_wrappers.munmap libc.src.__support.OSUtil.linux.syscall_wrappers.open + libc.src.__support.OSUtil.linux.syscall_wrappers.read libc.src.__support.OSUtil.osutil libc.src.__support.common libc.src.__support.CPP.optional diff --git a/libc/src/__support/OSUtil/linux/auxv.h b/libc/src/__support/OSUtil/linux/auxv.h index b4c990f9721b..a6c28e078fdb 100644 --- a/libc/src/__support/OSUtil/linux/auxv.h +++ b/libc/src/__support/OSUtil/linux/auxv.h @@ -9,12 +9,15 @@ #ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_AUXV_H #define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_AUXV_H +#include "hdr/errno_macros.h" // For EINTR #include "hdr/fcntl_macros.h" // For open flags #include "hdr/sys_auxv_macros.h" // For AT_ macros #include "hdr/sys_mman_macros.h" // For mmap flags +#include "src/__support/OSUtil/linux/syscall_wrappers/close.h" #include "src/__support/OSUtil/linux/syscall_wrappers/mmap.h" #include "src/__support/OSUtil/linux/syscall_wrappers/munmap.h" #include "src/__support/OSUtil/linux/syscall_wrappers/open.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/read.h" #include "src/__support/OSUtil/syscall.h" #include "src/__support/common.h" #include "src/__support/threads/callonce.h" @@ -122,18 +125,20 @@ LIBC_INLINE void Vector::fallback_initialize_unsync() { uint8_t *cursor = reinterpret_cast(vector); bool has_error = false; while (avaiable_size != 0) { - long bytes_read = - syscall_impl(SYS_read, fd.value(), cursor, avaiable_size); - if (bytes_read <= 0) { - if (bytes_read == -EINTR) + ErrorOr bytes_read = + linux_syscalls::read(fd.value(), cursor, avaiable_size); + if (!bytes_read.has_value()) { + if (bytes_read.error() == EINTR) continue; - has_error = bytes_read < 0; + has_error = true; break; } - avaiable_size -= bytes_read; - cursor += bytes_read; + if (bytes_read.value() == 0) + break; + avaiable_size -= bytes_read.value(); + cursor += bytes_read.value(); } - syscall_impl(SYS_close, fd.value()); + linux_syscalls::close(fd.value()); if (has_error) { linux_syscalls::munmap(vector, AUXV_MMAP_SIZE); return; diff --git a/libc/src/__support/threads/linux/CMakeLists.txt b/libc/src/__support/threads/linux/CMakeLists.txt index 8344c6c0462a..f9f2a455cd7a 100644 --- a/libc/src/__support/threads/linux/CMakeLists.txt +++ b/libc/src/__support/threads/linux/CMakeLists.txt @@ -41,10 +41,13 @@ add_object_library( libc.src.__support.CPP.string_view libc.src.__support.common libc.src.__support.error_or + libc.src.__support.OSUtil.linux.syscall_wrappers.close libc.src.__support.OSUtil.linux.syscall_wrappers.mmap libc.src.__support.OSUtil.linux.syscall_wrappers.mprotect libc.src.__support.OSUtil.linux.syscall_wrappers.munmap libc.src.__support.OSUtil.linux.syscall_wrappers.open + libc.src.__support.OSUtil.linux.syscall_wrappers.read + libc.src.__support.OSUtil.linux.syscall_wrappers.write libc.src.__support.threads.thread_common COMPILE_OPTIONS ${libc_opt_high_flag} diff --git a/libc/src/__support/threads/linux/thread.cpp b/libc/src/__support/threads/linux/thread.cpp index 64e043862ff7..f094484b6406 100644 --- a/libc/src/__support/threads/linux/thread.cpp +++ b/libc/src/__support/threads/linux/thread.cpp @@ -11,10 +11,13 @@ #include "src/__support/CPP/atomic.h" #include "src/__support/CPP/string_view.h" #include "src/__support/CPP/stringstream.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/close.h" #include "src/__support/OSUtil/linux/syscall_wrappers/mmap.h" #include "src/__support/OSUtil/linux/syscall_wrappers/mprotect.h" #include "src/__support/OSUtil/linux/syscall_wrappers/munmap.h" #include "src/__support/OSUtil/linux/syscall_wrappers/open.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/read.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/write.h" #include "src/__support/OSUtil/syscall.h" // For syscall functions. #include "src/__support/common.h" #include "src/__support/error_or.h" @@ -435,13 +438,13 @@ int Thread::set_name(const cpp::string_view &name) { if (!fd) return fd.error(); - int retval = LIBC_NAMESPACE::syscall_impl(SYS_write, fd.value(), - name.data(), name.size()); - LIBC_NAMESPACE::syscall_impl(SYS_close, fd.value()); + auto write_result = + linux_syscalls::write(fd.value(), name.data(), name.size()); + linux_syscalls::close(fd.value()); - if (retval < 0) - return -retval; - else if (retval != int(name.size())) + if (!write_result) + return write_result.error(); + else if (write_result.value() != static_cast(name.size())) return EIO; else return 0; @@ -471,11 +474,12 @@ int Thread::get_name(cpp::StringStream &name) const { if (!fd) return fd.error(); - int retval = LIBC_NAMESPACE::syscall_impl(SYS_read, fd.value(), - name_buffer, NAME_SIZE_MAX); - LIBC_NAMESPACE::syscall_impl(SYS_close, fd.value()); - if (retval < 0) - return -retval; + auto read_result = + linux_syscalls::read(fd.value(), name_buffer, NAME_SIZE_MAX); + linux_syscalls::close(fd.value()); + if (!read_result) + return read_result.error(); + int retval = static_cast(read_result.value()); if (retval == NAME_SIZE_MAX) return ERANGE; if (name_buffer[retval - 1] == '\n') diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt index 9ffb59b8c0d0..85d298f311d3 100644 --- a/libc/src/sched/linux/CMakeLists.txt +++ b/libc/src/sched/linux/CMakeLists.txt @@ -20,7 +20,8 @@ add_entrypoint_object( libc.hdr.types.cpu_set_t libc.hdr.types.pid_t libc.hdr.types.size_t - libc.src.__support.OSUtil.osutil + libc.src.__support.CPP.span + libc.src.__support.OSUtil.linux.syscall_wrappers.sched_getaffinity libc.src.errno.errno ) diff --git a/libc/src/sched/linux/sched_getaffinity.cpp b/libc/src/sched/linux/sched_getaffinity.cpp index d652f7f7ddc3..ddb34aed5e3b 100644 --- a/libc/src/sched/linux/sched_getaffinity.cpp +++ b/libc/src/sched/linux/sched_getaffinity.cpp @@ -9,7 +9,8 @@ #include "src/sched/sched_getaffinity.h" #include "hdr/stdint_proxy.h" -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/CPP/span.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/sched_getaffinity.h" #include "src/__support/common.h" #include "src/__support/libc_errno.h" #include "src/__support/macros/config.h" @@ -17,18 +18,19 @@ #include "hdr/types/cpu_set_t.h" #include "hdr/types/pid_t.h" #include "hdr/types/size_t.h" -#include // For syscall numbers. namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(int, sched_getaffinity, (pid_t tid, size_t cpuset_size, cpu_set_t *mask)) { - int ret = LIBC_NAMESPACE::syscall_impl(SYS_sched_getaffinity, tid, - cpuset_size, mask); - if (ret < 0) { - libc_errno = -ret; + auto result = linux_syscalls::sched_getaffinity( + tid, cpp::span(reinterpret_cast(mask), + cpuset_size)); + if (!result) { + libc_errno = result.error(); return -1; } + int ret = result.value(); if (size_t(ret) < cpuset_size) { // This means that only |ret| bytes in |mask| have been set. We will have to // zero out the remaining bytes. diff --git a/libc/src/spawn/linux/CMakeLists.txt b/libc/src/spawn/linux/CMakeLists.txt index 5b3d09a82ff3..2293f8db9bf0 100644 --- a/libc/src/spawn/linux/CMakeLists.txt +++ b/libc/src/spawn/linux/CMakeLists.txt @@ -11,6 +11,7 @@ add_entrypoint_object( libc.include.spawn libc.include.sys_syscall libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.close libc.src.__support.OSUtil.linux.syscall_wrappers.dup2 libc.src.__support.OSUtil.linux.syscall_wrappers.open libc.src.spawn.file_actions diff --git a/libc/src/spawn/linux/posix_spawn.cpp b/libc/src/spawn/linux/posix_spawn.cpp index dcdb14d90d7a..00bfc8785c67 100644 --- a/libc/src/spawn/linux/posix_spawn.cpp +++ b/libc/src/spawn/linux/posix_spawn.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "src/spawn/posix_spawn.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/close.h" #include "src/__support/OSUtil/linux/syscall_wrappers/dup2.h" #include "src/__support/OSUtil/linux/syscall_wrappers/open.h" #include "src/__support/OSUtil/syscall.h" // For internal syscall function. @@ -40,8 +41,6 @@ pid_t fork() { #endif } -void close(int fd) { LIBC_NAMESPACE::syscall_impl(SYS_close, fd); } - // All exits from child_process are error exits. So, we use a simple // exit implementation which exits with code 127. void exit() { @@ -75,7 +74,7 @@ void child_process(const char *__restrict path, if (actual_fd != open_act->fd) { bool dup2_result = linux_syscalls::dup2(actual_fd, open_act->fd).has_value(); - close(actual_fd); // The old fd is not needed anymore. + linux_syscalls::close(actual_fd); // The old fd is not needed anymore. if (!dup2_result) exit(); } @@ -83,7 +82,7 @@ void child_process(const char *__restrict path, } case BaseSpawnFileAction::CLOSE: { auto *close_act = reinterpret_cast(act); - close(close_act->fd); + linux_syscalls::close(close_act->fd); break; } case BaseSpawnFileAction::DUP2: { diff --git a/libc/src/stdio/linux/CMakeLists.txt b/libc/src/stdio/linux/CMakeLists.txt index 1552060c5255..218875c78e24 100644 --- a/libc/src/stdio/linux/CMakeLists.txt +++ b/libc/src/stdio/linux/CMakeLists.txt @@ -5,10 +5,10 @@ add_entrypoint_object( HDRS ../remove.h DEPENDS + libc.hdr.errno_macros libc.hdr.fcntl_macros libc.include.unistd - libc.include.sys_syscall - libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.unlinkat libc.src.errno.errno ) diff --git a/libc/src/stdio/linux/remove.cpp b/libc/src/stdio/linux/remove.cpp index ac755db0bc78..2e1a58147435 100644 --- a/libc/src/stdio/linux/remove.cpp +++ b/libc/src/stdio/linux/remove.cpp @@ -8,26 +8,25 @@ #include "src/stdio/remove.h" -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/OSUtil/linux/syscall_wrappers/unlinkat.h" #include "src/__support/common.h" +#include "hdr/errno_macros.h" // For EISDIR. #include "hdr/fcntl_macros.h" // For AT_* macros. #include "src/__support/libc_errno.h" #include "src/__support/macros/config.h" -#include // For syscall numbers. namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(int, remove, (const char *path)) { // We first try unlinking it as a file. If it is ia file, it will succeed. If // it fails with EISDIR, we will try unlinking it as a directory. - int ret = LIBC_NAMESPACE::syscall_impl(SYS_unlinkat, AT_FDCWD, path, 0); - if (ret == -EISDIR) - ret = LIBC_NAMESPACE::syscall_impl(SYS_unlinkat, AT_FDCWD, path, - AT_REMOVEDIR); - if (ret >= 0) + auto ret = linux_syscalls::unlinkat(AT_FDCWD, path, 0); + if (!ret && ret.error() == EISDIR) + ret = linux_syscalls::unlinkat(AT_FDCWD, path, AT_REMOVEDIR); + if (ret) return 0; - libc_errno = -ret; + libc_errno = ret.error(); return -1; } diff --git a/libc/src/sys/ipc/linux/CMakeLists.txt b/libc/src/sys/ipc/linux/CMakeLists.txt index d5b2d1b8ab77..b237e998e6e2 100644 --- a/libc/src/sys/ipc/linux/CMakeLists.txt +++ b/libc/src/sys/ipc/linux/CMakeLists.txt @@ -1,14 +1,3 @@ -add_header_library( - kernel_statx - HDRS - kernel_statx.h - DEPENDS - libc.hdr.fcntl_macros - libc.include.sys_syscall - libc.src.__support.error_or - libc.src.__support.OSUtil.osutil -) - add_entrypoint_object( ftok SRCS @@ -16,8 +5,10 @@ add_entrypoint_object( HDRS ../ftok.h DEPENDS - .kernel_statx + libc.hdr.fcntl_macros libc.hdr.types.key_t + libc.src.__support.OSUtil.linux.stat.kernel_statx_types + libc.src.__support.OSUtil.linux.syscall_wrappers.statx libc.src.__support.common libc.src.errno.errno ) diff --git a/libc/src/sys/ipc/linux/ftok.cpp b/libc/src/sys/ipc/linux/ftok.cpp index 28762ac6e33f..f1e2965dddb5 100644 --- a/libc/src/sys/ipc/linux/ftok.cpp +++ b/libc/src/sys/ipc/linux/ftok.cpp @@ -7,18 +7,19 @@ //===----------------------------------------------------------------------===// #include "src/sys/ipc/ftok.h" +#include "hdr/fcntl_macros.h" // For AT_FDCWD. +#include "src/__support/OSUtil/linux/stat/kernel_statx_types.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/statx.h" #include "src/__support/common.h" -#include "src/__support/error_or.h" #include "src/__support/libc_errno.h" -#include "kernel_statx.h" - namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(key_t, ftok, (const char *path, int id)) { - struct statx xbuf; + internal::kernel_statx_buf xbuf; - ErrorOr err = statx_for_ftok(path, xbuf); + auto err = linux_syscalls::statx( + AT_FDCWD, path, 0, internal::KERNEL_STATX_BASIC_STATS_MASK, &xbuf); if (!err.has_value()) { libc_errno = err.error(); diff --git a/libc/src/sys/ipc/linux/kernel_statx.h b/libc/src/sys/ipc/linux/kernel_statx.h deleted file mode 100644 index 7613e1b50f20..000000000000 --- a/libc/src/sys/ipc/linux/kernel_statx.h +++ /dev/null @@ -1,34 +0,0 @@ -//===-- Wrapper over SYS_statx syscall for ftok ---------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_LIBC_SRC_SYS_IPC_LINUX_KERNEL_STATX_H -#define LLVM_LIBC_SRC_SYS_IPC_LINUX_KERNEL_STATX_H - -#include "hdr/fcntl_macros.h" -#include "src/__support/OSUtil/syscall.h" -#include "src/__support/error_or.h" -#include "sys/syscall.h" - -#include - -namespace LIBC_NAMESPACE_DECL { - -LIBC_INLINE ErrorOr statx_for_ftok(const char *path, struct statx &xbuf) { - - // store the file stats metadata into xbuf - int ret = LIBC_NAMESPACE::syscall_impl(SYS_statx, AT_FDCWD, path, 0, - STATX_BASIC_STATS, &xbuf); - - if (ret < 0) - return Error(-ret); - return 0; -} - -} // namespace LIBC_NAMESPACE_DECL - -#endif // LLVM_LIBC_SRC_SYS_IPC_LINUX_KERNEL_STATX_H diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt index e8d9ed384c9b..6987261e53e0 100644 --- a/libc/src/sys/mman/linux/CMakeLists.txt +++ b/libc/src/sys/mman/linux/CMakeLists.txt @@ -306,6 +306,7 @@ add_entrypoint_object( HDRS ../shm_unlink.h DEPENDS + libc.src.__support.OSUtil.linux.syscall_wrappers.unlink libc.src.errno.errno .shm_common ) diff --git a/libc/src/sys/mman/linux/shm_unlink.cpp b/libc/src/sys/mman/linux/shm_unlink.cpp index 7671b1918b83..70eab7e9b4bf 100644 --- a/libc/src/sys/mman/linux/shm_unlink.cpp +++ b/libc/src/sys/mman/linux/shm_unlink.cpp @@ -8,37 +8,26 @@ #include "src/sys/mman/shm_unlink.h" -#include "hdr/fcntl_macros.h" -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. -#include "src/__support/libc_errno.h" // For internal errno. +#include "src/__support/OSUtil/linux/syscall_wrappers/unlink.h" +#include "src/__support/libc_errno.h" // For internal errno. #include "src/__support/macros/config.h" #include "src/sys/mman/linux/shm_common.h" -#include // For SYS_unlink, SYS_unlinkat namespace LIBC_NAMESPACE_DECL { -// TODO: move the unlink syscall to a shared utility. - LLVM_LIBC_FUNCTION(int, shm_unlink, (const char *name)) { auto path_result = shm_common::translate_name(name); if (!path_result.has_value()) { libc_errno = path_result.error(); return -1; } -#ifdef SYS_unlink - int ret = LIBC_NAMESPACE::syscall_impl(SYS_unlink, path_result->data()); -#elif defined(SYS_unlinkat) - int ret = LIBC_NAMESPACE::syscall_impl(SYS_unlinkat, AT_FDCWD, - path_result->data(), 0); -#else -#error "unlink and unlinkat syscalls not available." -#endif - if (ret < 0) { - libc_errno = -ret; + auto result = linux_syscalls::unlink(path_result->data()); + if (!result) { + libc_errno = result.error(); return -1; } - return ret; + return 0; } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/sys/time/linux/CMakeLists.txt b/libc/src/sys/time/linux/CMakeLists.txt index 2dbd3115ea8f..9e5b92cece04 100644 --- a/libc/src/sys/time/linux/CMakeLists.txt +++ b/libc/src/sys/time/linux/CMakeLists.txt @@ -8,10 +8,8 @@ add_entrypoint_object( libc.hdr.types.struct_timespec libc.hdr.types.struct_timeval libc.hdr.fcntl_macros - libc.src.__support.OSUtil.osutil libc.src.__support.OSUtil.linux.syscall_wrappers.utimensat libc.include.sys_stat - libc.include.sys_syscall libc.include.fcntl libc.src.__support.libc_errno ) diff --git a/libc/src/sys/time/linux/utimes.cpp b/libc/src/sys/time/linux/utimes.cpp index abb56183cf7c..02101c3e3831 100644 --- a/libc/src/sys/time/linux/utimes.cpp +++ b/libc/src/sys/time/linux/utimes.cpp @@ -13,17 +13,12 @@ #include "hdr/types/struct_timeval.h" #include "src/__support/OSUtil/linux/syscall_wrappers/utimensat.h" -#include "src/__support/OSUtil/syscall.h" #include "src/__support/libc_errno.h" -#include - namespace LIBC_NAMESPACE_DECL { LLVM_LIBC_FUNCTION(int, utimes, (const char *path, const struct timeval times[2])) { -#if defined(SYS_utimensat) || defined(SYS_utimensat_time64) - // the utimensat syscall requires a timespec struct, not timeval. struct timespec ts[2]; struct timespec *ts_ptr = nullptr; // default value if times is nullptr @@ -64,8 +59,5 @@ LLVM_LIBC_FUNCTION(int, utimes, } return 0; -#else -#error "utimensat or utimensat_time64 syscalls not available." -#endif // SYS_utimensat || SYS_utimensat_time64 } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/startup/linux/x86_64/CMakeLists.txt b/libc/startup/linux/x86_64/CMakeLists.txt index 12dc0eeef42e..19417b345d7c 100644 --- a/libc/startup/linux/x86_64/CMakeLists.txt +++ b/libc/startup/linux/x86_64/CMakeLists.txt @@ -7,6 +7,7 @@ add_startup_object( libc.hdr.sys_mman_macros libc.include.sys_syscall libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.getrandom libc.src.__support.OSUtil.linux.syscall_wrappers.mmap libc.src.__support.OSUtil.linux.syscall_wrappers.munmap libc.src.string.memory_utils.inline_memcpy diff --git a/libc/startup/linux/x86_64/tls.cpp b/libc/startup/linux/x86_64/tls.cpp index 7856bdd14daa..98c5fbeb9d84 100644 --- a/libc/startup/linux/x86_64/tls.cpp +++ b/libc/startup/linux/x86_64/tls.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "hdr/sys_mman_macros.h" +#include "src/__support/OSUtil/linux/syscall_wrappers/getrandom.h" #include "src/__support/OSUtil/linux/syscall_wrappers/mmap.h" #include "src/__support/OSUtil/linux/syscall_wrappers/munmap.h" #include "src/__support/macros/config.h" @@ -57,11 +58,11 @@ void init_tls(TLSDescriptor &tls_descriptor) { // Setting the stack guard to a random value. // We cannot call the get_random function here as the function sets errno on // failure. Since errno is implemented via a thread local variable, we cannot - // use errno before TLS is setup. - long stack_guard_retval = - syscall_impl(SYS_getrandom, reinterpret_cast(stack_guard_addr), - sizeof(uint64_t), 0); - if (stack_guard_retval < 0) + // use errno before TLS is setup. The linux_syscalls wrapper is safe as it + // reports errors via ErrorOr instead of errno. + ErrorOr stack_guard_retval = + linux_syscalls::getrandom(stack_guard_addr, sizeof(uint64_t), 0); + if (!stack_guard_retval.has_value()) syscall_impl(SYS_exit, 1); tls_descriptor = {tls_size_with_addr, reinterpret_cast(tls_addr), diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index 0c4b759e331d..5254288ba60b 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -1603,10 +1603,12 @@ libc_support_library( ":__support_file_file", ":__support_libc_errno", ":__support_macros_config", + ":__support_osutil_linux_syscall_wrappers_close", ":__support_osutil_linux_syscall_wrappers_fcntl", ":__support_osutil_linux_syscall_wrappers_lseek", ":__support_osutil_linux_syscall_wrappers_open", - ":__support_osutil_syscall", + ":__support_osutil_linux_syscall_wrappers_read", + ":__support_osutil_linux_syscall_wrappers_write", ":hdr_fcntl_macros", ":hdr_stdint_proxy", ":hdr_stdio_macros", @@ -2932,11 +2934,14 @@ libc_support_library( }), deps = [ ":__support_common", + ":__support_osutil_linux_syscall_wrappers_close", ":__support_osutil_linux_syscall_wrappers_mmap", ":__support_osutil_linux_syscall_wrappers_munmap", ":__support_osutil_linux_syscall_wrappers_open", + ":__support_osutil_linux_syscall_wrappers_read", ":__support_osutil_syscall", ":__support_threads_callonce", + ":hdr_errno_macros", ":hdr_fcntl_macros", ":hdr_sys_auxv_macros", ":hdr_sys_mman_macros", @@ -15702,8 +15707,9 @@ libc_function( ":__support_common", ":__support_libc_errno", ":__support_macros_config", - ":__support_osutil_syscall", + ":__support_osutil_linux_syscall_wrappers_unlinkat", ":errno", + ":hdr_errno_macros", ":hdr_fcntl_macros", ":hdr_stdio_overlay", ":types_FILE", @@ -16090,9 +16096,8 @@ libc_function( ":__support_common", ":__support_libc_errno", ":__support_macros_config", - ":__support_osutil_syscall", + ":__support_osutil_linux_syscall_wrappers_unlink", ":errno", - ":hdr_fcntl_macros", ":shm_common", ":types_mode_t", ],