[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for msync (PR #243784)

via libc-commits libc-commits at lists.llvm.org
Wed Nov 18 02:27:30 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

This PR move msync behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.


---
Full diff: https://github.com/llvm/llvm-project/pull/243784.diff


6 Files Affected:

- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+13) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/msync.h (+34) 
- (modified) libc/src/sys/mman/linux/CMakeLists.txt (+5-3) 
- (modified) libc/src/sys/mman/linux/msync.cpp (+13-7) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel (+16) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel (+1-2) 


``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index cd2f92ce59e71..c332cf2cb6582 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1137,6 +1137,19 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  msync
+  HDRS
+    msync.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/msync.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/msync.h
new file mode 100644
index 0000000000000..3a53a2367abb6
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/msync.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 msync.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MSYNC_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MSYNC_H
+
+#include "hdr/types/size_t.h"
+#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"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> msync(void *addr, size_t len, int flags) {
+  return syscall_checked<int>(SYS_msync, addr, len, flags);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_MSYNC_H
diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index bfdfa3ee6c55d..4180ba24ce7aa 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -192,9 +192,11 @@ add_entrypoint_object(
   HDRS
     ../msync.h
   DEPENDS
-    libc.include.sys_mman
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.hdr.types.size_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.msync
+    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/msync.cpp b/libc/src/sys/mman/linux/msync.cpp
index 650678bcb36e0..465f7273c86df 100644
--- a/libc/src/sys/mman/linux/msync.cpp
+++ b/libc/src/sys/mman/linux/msync.cpp
@@ -1,26 +1,32 @@
-//===---------- Linux implementation of the msync 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 msync.
+///
+//===----------------------------------------------------------------------===//
 
 #include "src/sys/mman/msync.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
-
+#include "src/__support/OSUtil/linux/syscall_wrappers/msync.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, msync, (void *addr, size_t len, int flags)) {
-  long ret = syscall_impl(SYS_msync, cpp::bit_cast<long>(addr), len, flags);
-  if (ret < 0) {
-    libc_errno = static_cast<int>(-ret);
+  auto result = linux_syscalls::msync(addr, len, flags);
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
   return 0;
 }
+
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
index 5e1c3cd4fb86d..b9cb62558e129 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
@@ -1610,6 +1610,22 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "msync",
+    hdrs = ["msync.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        "//libc/hdr/types:size_t",
+        "//libc/src/__support:common",
+        "//libc/src/__support:error_or",
+        "//libc/src/__support/OSUtil:syscall",
+        "//libc/src/__support/macros:config",
+    ],
+)
+
 libc_support_library(
     name = "munmap",
     hdrs = ["munmap.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel
index 63475f9e27e55..4fa25b630ed9b 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/sys/mman/BUILD.bazel
@@ -140,11 +140,10 @@ libc_function(
     srcs = ["linux/msync.cpp"],
     hdrs = ["msync.h"],
     deps = [
-        "//libc/hdr:sys_mman_macros",
         "//libc/hdr/types:size_t",
         "//libc/src/__support:common",
         "//libc/src/__support:libc_errno",
-        "//libc/src/__support/OSUtil:syscall",
+        "//libc/src/__support/OSUtil/linux/syscall_wrappers:msync",
         "//libc/src/__support/macros:config",
         "//libc/src/errno",
     ],

``````````

</details>


https://github.com/llvm/llvm-project/pull/243784


More information about the libc-commits mailing list