https://github.com/c8ef updated https://github.com/llvm/llvm-project/pull/239195
>From e606eb959dd09e4994b39a87ffa74678c504ca31 Mon Sep 17 00:00:00 2001 From: c8ef <[email protected]> Date: Thu, 5 Nov 2026 19:35:23 +0800 Subject: [PATCH] [libc] Use a syscall wrapper for mremap This PR move mremap behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md. --- .../linux/syscall_wrappers/CMakeLists.txt | 13 +++++++ .../OSUtil/linux/syscall_wrappers/mremap.h | 38 +++++++++++++++++++ libc/src/sys/mman/linux/CMakeLists.txt | 6 ++- libc/src/sys/mman/linux/mremap.cpp | 25 ++++++------ .../llvm-project-overlay/libc/BUILD.bazel | 18 ++++++++- 5 files changed, 84 insertions(+), 16 deletions(-) create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/mremap.h diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt index 1b6da18cd6d50c..4054e31dd4ef86 100644 --- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt @@ -896,6 +896,19 @@ add_header_library( libc.include.sys_syscall ) +add_header_library( + mremap + HDRS + mremap.h + DEPENDS + libc.hdr.types.size_t + 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( munmap HDRS diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/mremap.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/mremap.h new file mode 100644 index 00000000000000..53e103aa76a53a --- /dev/null +++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/mremap.h @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// 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 mremap. +/// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MREMAP_H +#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MREMAP_H + +#include "hdr/types/size_t.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<void *> mremap(void *old_address, size_t old_size, + size_t new_size, int flags, + void *new_address) { + return syscall_checked<void *>(SYS_mremap, old_address, old_size, new_size, + flags, new_address); +} + +} // namespace linux_syscalls +} // namespace LIBC_NAMESPACE_DECL + +#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MREMAP_H diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt index b923eb42d15029..b8ad15c1e149e3 100644 --- a/libc/src/sys/mman/linux/CMakeLists.txt +++ b/libc/src/sys/mman/linux/CMakeLists.txt @@ -54,8 +54,10 @@ add_entrypoint_object( DEPENDS libc.hdr.sys_mman_macros libc.hdr.types.size_t - libc.include.sys_syscall - libc.src.__support.OSUtil.osutil + libc.src.__support.OSUtil.linux.syscall_wrappers.mremap + 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/mremap.cpp b/libc/src/sys/mman/linux/mremap.cpp index 1b4052c83094e5..cdfee79be6431f 100644 --- a/libc/src/sys/mman/linux/mremap.cpp +++ b/libc/src/sys/mman/linux/mremap.cpp @@ -1,21 +1,23 @@ -//===---------- Linux implementation of the POSIX mremap 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 mremap. +/// +//===----------------------------------------------------------------------===// #include "src/sys/mman/mremap.h" -#include "src/__support/OSUtil/syscall.h" // For internal syscall function. +#include "src/__support/OSUtil/linux/syscall_wrappers/mremap.h" #include "src/__support/common.h" - #include "src/__support/libc_errno.h" #include "src/__support/macros/config.h" -#include <linux/param.h> // For EXEC_PAGESIZE. #include <stdarg.h> -#include <sys/syscall.h> // For syscall numbers. #include "hdr/sys_mman_macros.h" // For MREMAP_FIXED, MAP_FAILED. @@ -25,7 +27,6 @@ LLVM_LIBC_FUNCTION(void *, mremap, (void *old_address, size_t old_size, size_t new_size, int flags, ... /* void *new_address */)) { - long ret = 0; void *new_address = nullptr; if (flags & MREMAP_FIXED) { va_list varargs; @@ -33,15 +34,13 @@ LLVM_LIBC_FUNCTION(void *, mremap, new_address = va_arg(varargs, void *); va_end(varargs); } - ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_mremap, old_address, old_size, - new_size, flags, new_address); - - if (ret < 0 && ret > -EXEC_PAGESIZE) { - libc_errno = static_cast<int>(-ret); + auto result = linux_syscalls::mremap(old_address, old_size, new_size, flags, + new_address); + if (!result) { + libc_errno = result.error(); return MAP_FAILED; } - - return reinterpret_cast<void *>(ret); + 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 df91f401bb5a4e..33bf57c2726d29 100644 --- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel +++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel @@ -4551,6 +4551,22 @@ libc_support_library( ], ) +libc_support_library( + name = "__support_osutil_linux_syscall_wrappers_mremap", + hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/mremap.h"], + target_compatible_with = select({ + "@platforms//os:linux": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + deps = [ + ":__support_common", + ":__support_error_or", + ":__support_macros_config", + ":__support_osutil_syscall", + ":types_size_t", + ], +) + libc_support_library( name = "__support_osutil_linux_syscall_wrappers_munmap", hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/munmap.h"], @@ -19739,7 +19755,7 @@ libc_function( ":__support_common", ":__support_libc_errno", ":__support_macros_config", - ":__support_osutil_syscall", + ":__support_osutil_linux_syscall_wrappers_mremap", ":errno", ":hdr_sys_mman_macros", ":types_size_t", _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
