https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/239196
>From 51f9073c8e85edef6506c9d50e3bae05cbcf15bd Mon Sep 17 00:00:00 2001 From: c8ef <[email protected]> Date: Thu, 5 Nov 2026 19:35:47 +0800 Subject: [PATCH] [libc] Use a syscall wrapper for pkey_alloc This PR move pkey_alloc behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. --- .../linux/syscall_wrappers/CMakeLists.txt | 13 ++++++ .../linux/syscall_wrappers/pkey_alloc.h | 40 +++++++++++++++++++ libc/src/sys/mman/linux/CMakeLists.txt | 7 ++-- libc/src/sys/mman/linux/pkey_alloc.cpp | 26 +++++------- .../llvm-project-overlay/libc/BUILD.bazel | 19 ++++++++- 5 files changed, 85 insertions(+), 20 deletions(-) create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/pkey_alloc.h diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt index 4054e31dd4ef86..5f1cca5bf0ed62 100644 --- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt @@ -933,6 +933,19 @@ add_header_library( libc.include.sys_syscall ) +add_header_library( + pkey_alloc + HDRS + pkey_alloc.h + DEPENDS + libc.hdr.errno_macros + libc.include.sys_syscall + libc.src.__support.OSUtil.osutil + libc.src.__support.common + libc.src.__support.error_or + libc.src.__support.macros.config +) + add_header_library( pkey_mprotect HDRS diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/pkey_alloc.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/pkey_alloc.h new file mode 100644 index 00000000000000..297908393ce200 --- /dev/null +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/pkey_alloc.h @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// 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 pkey_alloc. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PKEY_ALLOC_H +#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PKEY_ALLOC_H + +#include "hdr/errno_macros.h" +#include "src/__support/OSUtil/linux/syscall.h" +#include "src/__support/common.h" +#include "src/__support/error_or.h" +#include "src/__support/macros/config.h" + +#include <sys/syscall.h> // For syscall numbers. + +namespace LIBC_NAMESPACE_DECL { +namespace linux_syscalls { + +LIBC_INLINE ErrorOr<int> pkey_alloc(unsigned int flags, + unsigned int access_rights) { +#if defined(SYS_pkey_alloc) + return syscall_checked<int>(SYS_pkey_alloc, flags, access_rights); +#else + return Error(ENOSYS); +#endif +} + +} // namespace linux_syscalls +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_PKEY_ALLOC_H diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt index b8ad15c1e149e3..e4eda48e4f9702 100644 --- a/libc/src/sys/mman/linux/CMakeLists.txt +++ b/libc/src/sys/mman/linux/CMakeLists.txt @@ -196,9 +196,10 @@ add_entrypoint_object( HDRS ../pkey_alloc.h DEPENDS - libc.include.sys_mman - libc.include.sys_syscall - libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.pkey_alloc + libc.src.__support.common + libc.src.__support.libc_errno + libc.src.__support.macros.config libc.src.errno.errno ) diff --git a/libc/src/sys/mman/linux/pkey_alloc.cpp b/libc/src/sys/mman/linux/pkey_alloc.cpp index 6ad65f342eb5eb..19a3cef249c25e 100644 --- a/libc/src/sys/mman/linux/pkey_alloc.cpp +++ b/libc/src/sys/mman/linux/pkey_alloc.cpp @@ -1,37 +1,33 @@ -//===---------- Linux implementation of the Linux pkey_alloc function -----===// +//===----------------------------------------------------------------------===// // // 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 pkey_alloc. +/// +//===----------------------------------------------------------------------===// #include "src/sys/mman/pkey_alloc.h" -#include "hdr/errno_macros.h" // For ENOSYS -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/OSUtil/linux/syscall_wrappers/pkey_alloc.h" #include "src/__support/common.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, pkey_alloc, (unsigned int flags, unsigned int access_rights)) { -#if !defined(SYS_pkey_alloc) - libc_errno = ENOSYS; - return -1; -#else - int ret = - LIBC_NAMESPACE::syscall_impl<int>(SYS_pkey_alloc, flags, access_rights); - if (ret < 0) { - libc_errno = -ret; + auto result = linux_syscalls::pkey_alloc(flags, access_rights); + if (!result) { + libc_errno = result.error(); return -1; } - return static_cast<int>(ret); -#endif + return result.value(); } } // namespace LIBC_NAMESPACE_DECL diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel index 33bf57c2726d29..f3a3a3ad118f70 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -4597,6 +4597,22 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_osutil_linux_syscall_wrappers_pkey_alloc", + hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/pkey_alloc.h"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + deps = [ + ":__support_common", + ":__support_error_or", + ":__support_macros_config", + ":__support_osutil_syscall", + ":hdr_errno_macros", + ], +) + libc_support_library( name = "__support_osutil_linux_syscall_wrappers_pkey_mprotect", hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/pkey_mprotect.h"], @@ -19842,9 +19858,8 @@ libc_function( ":__support_common", ":__support_libc_errno", ":__support_macros_config", - ":__support_osutil_syscall", + ":__support_osutil_linux_syscall_wrappers_pkey_alloc", ":errno", - ":hdr_errno_macros", ], ) _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
