[libc-commits] [libc] [libc] reland mincore (PR #79309)

Schrodinger ZHU Yifan via libc-commits libc-commits at lists.llvm.org
Wed Jan 24 09:30:40 PST 2024


https://github.com/SchrodingerZhu updated https://github.com/llvm/llvm-project/pull/79309

>From 517af124e4880ab90657dc8e4107707bb741fd91 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Thu, 30 Nov 2023 14:22:36 -0500
Subject: [PATCH 1/3] [libc][SysMMan] implement mincore (#73704)

Implement `mincore` as specified in
https://man7.org/linux/man-pages/man2/mincore.2.html
---
 libc/config/linux/aarch64/entrypoints.txt     |   1 +
 libc/config/linux/riscv/entrypoints.txt       |   1 +
 libc/config/linux/x86_64/entrypoints.txt      |   1 +
 libc/spec/linux.td                            |  15 ++-
 libc/spec/spec.td                             |   2 +
 libc/src/sys/mman/CMakeLists.txt              |   7 ++
 libc/src/sys/mman/linux/CMakeLists.txt        |  13 ++
 libc/src/sys/mman/linux/mincore.cpp           |  28 +++++
 libc/src/sys/mman/mincore.h                   |  20 +++
 libc/test/src/sys/mman/linux/CMakeLists.txt   |  16 +++
 libc/test/src/sys/mman/linux/mincore_test.cpp | 115 ++++++++++++++++++
 11 files changed, 218 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/sys/mman/linux/mincore.cpp
 create mode 100644 libc/src/sys/mman/mincore.h
 create mode 100644 libc/test/src/sys/mman/linux/mincore_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 3f66a582f5e3ee..52f1d2bce34f4c 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -136,6 +136,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.mman.mprotect
     libc.src.sys.mman.munmap
     libc.src.sys.mman.posix_madvise
+    libc.src.sys.mman.mincore
 
     # sys/random.h entrypoints
     libc.src.sys.random.getrandom
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 0331ef782cf74a..a257f3f8d64ab9 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -142,6 +142,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.mman.mprotect
     libc.src.sys.mman.munmap
     libc.src.sys.mman.posix_madvise
+    libc.src.sys.mman.mincore
 
     # sys/random.h entrypoints
     libc.src.sys.random.getrandom
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index d5ab891674a2d8..8c4c25b4bb2098 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -142,6 +142,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.mman.mprotect
     libc.src.sys.mman.munmap
     libc.src.sys.mman.posix_madvise
+    libc.src.sys.mman.mincore
 
     # sys/random.h entrypoints
     libc.src.sys.random.getrandom
diff --git a/libc/spec/linux.td b/libc/spec/linux.td
index ba5f99c12ecd11..eab0a987b920cd 100644
--- a/libc/spec/linux.td
+++ b/libc/spec/linux.td
@@ -76,7 +76,20 @@ def Linux : StandardSpec<"Linux"> {
 
   HeaderSpec SysMMan = HeaderSpec<
       "sys/mman.h",
-      [Macro<"MAP_ANONYMOUS">]
+      [Macro<"MAP_ANONYMOUS">],
+      [], // Types
+      [], // Enumerations
+      [
+        FunctionSpec<
+            "mincore",
+            RetValSpec<IntType>,
+            [
+              ArgSpec<VoidPtr>,
+              ArgSpec<SizeTType>,
+              ArgSpec<UnsignedCharPtr>,
+            ]
+        >,
+      ]  // Functions
   >;
 
 
diff --git a/libc/spec/spec.td b/libc/spec/spec.td
index 9b689b5eb502a9..818cfaee6b61c2 100644
--- a/libc/spec/spec.td
+++ b/libc/spec/spec.td
@@ -49,6 +49,7 @@ def FloatType : NamedType<"float">;
 def DoubleType : NamedType<"double">;
 def LongDoubleType : NamedType<"long double">;
 def CharType : NamedType<"char">;
+def UnsignedCharType : NamedType<"unsigned char">;
 
 // TODO: Add compatibility layer to use C23 type _Float128 if possible.
 def Float128Type : NamedType<"__float128">;
@@ -109,6 +110,7 @@ def IntPtr : PtrType<IntType>;
 def RestrictedIntPtr : RestrictedPtrType<IntType>;
 def FloatPtr : PtrType<FloatType>;
 def DoublePtr : PtrType<DoubleType>;
+def UnsignedCharPtr : PtrType<UnsignedCharType>;
 
 def SigHandlerT : NamedType<"__sighandler_t">;
 
diff --git a/libc/src/sys/mman/CMakeLists.txt b/libc/src/sys/mman/CMakeLists.txt
index e336bfd5d6dbc6..2d17429a26b457 100644
--- a/libc/src/sys/mman/CMakeLists.txt
+++ b/libc/src/sys/mman/CMakeLists.txt
@@ -36,3 +36,10 @@ add_entrypoint_object(
   DEPENDS
     .${LIBC_TARGET_OS}.posix_madvise
 )
+
+add_entrypoint_object(
+  mincore
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.mincore
+)
diff --git a/libc/src/sys/mman/linux/CMakeLists.txt b/libc/src/sys/mman/linux/CMakeLists.txt
index 163e7dead8887a..ce0cda7f222770 100644
--- a/libc/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/src/sys/mman/linux/CMakeLists.txt
@@ -61,3 +61,16 @@ add_entrypoint_object(
     libc.include.sys_syscall
     libc.src.__support.OSUtil.osutil
 )
+
+add_entrypoint_object(
+  mincore
+  SRCS
+    mincore.cpp
+  HDRS
+    ../mincore.h
+  DEPENDS
+    libc.include.sys_mman
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.errno.errno
+)
diff --git a/libc/src/sys/mman/linux/mincore.cpp b/libc/src/sys/mman/linux/mincore.cpp
new file mode 100644
index 00000000000000..8220c69ef2cb77
--- /dev/null
+++ b/libc/src/sys/mman/linux/mincore.cpp
@@ -0,0 +1,28 @@
+//===---------- Linux implementation of the mincore 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/mman/mincore.h"
+
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+
+#include "src/errno/libc_errno.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, mincore, (void *addr, size_t len, unsigned char *vec)) {
+  long ret = syscall_impl(SYS_mincore, reinterpret_cast<long>(addr), len,
+                          reinterpret_cast<long>(vec));
+  if (ret < 0) {
+    libc_errno = static_cast<int>(-ret);
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/sys/mman/mincore.h b/libc/src/sys/mman/mincore.h
new file mode 100644
index 00000000000000..403afaeb6af970
--- /dev/null
+++ b/libc/src/sys/mman/mincore.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for mincore function --------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_MMAN_MINCORE_H
+#define LLVM_LIBC_SRC_SYS_MMAN_MINCORE_H
+
+#include <sys/mman.h> // For size_t
+
+namespace LIBC_NAMESPACE {
+
+int mincore(void *addr, size_t len, unsigned char *vec);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_SYS_MMAN_MINCORE_H
diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 66743be175fed1..5402ae030b345c 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -62,3 +62,19 @@ add_libc_unittest(
     libc.src.sys.mman.posix_madvise
     libc.test.UnitTest.ErrnoSetterMatcher
 )
+
+add_libc_unittest(
+  mincore_test
+  SUITE
+    libc_sys_mman_unittests
+  SRCS
+    mincore_test.cpp
+  DEPENDS
+    libc.include.sys_mman
+    libc.src.errno.errno
+    libc.src.sys.mman.mmap
+    libc.src.sys.mman.munmap
+    libc.src.sys.mman.madvise
+    libc.src.sys.mman.mincore
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
diff --git a/libc/test/src/sys/mman/linux/mincore_test.cpp b/libc/test/src/sys/mman/linux/mincore_test.cpp
new file mode 100644
index 00000000000000..7c8ca3b4ffa4cc
--- /dev/null
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -0,0 +1,115 @@
+//===-- Unittests for mincore ---------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/errno/libc_errno.h"
+#include "src/sys/mman/madvise.h"
+#include "src/sys/mman/mincore.h"
+#include "src/sys/mman/mmap.h"
+#include "src/sys/mman/munmap.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/LibcTest.h"
+#include "test/UnitTest/Test.h"
+
+#include <linux/param.h> // For EXEC_PAGESIZE
+#include <sys/mman.h>
+
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
+using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
+
+TEST(LlvmLibcMincoreTest, UnMappedMemory) {
+  libc_errno = 0;
+  unsigned char vec;
+  int res = LIBC_NAMESPACE::mincore(nullptr, 1, &vec);
+  EXPECT_THAT(res, Fails(ENOMEM, -1));
+}
+
+TEST(LlvmLibcMincoreTest, InvalidVec) {
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, 4 * EXEC_PAGESIZE, PROT_READ,
+                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  EXPECT_NE(addr, MAP_FAILED);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  libc_errno = 0;
+  int res = LIBC_NAMESPACE::mincore(addr, 1, nullptr);
+  EXPECT_THAT(res, Fails(EFAULT, -1));
+  void *area =
+      LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ | PROT_WRITE,
+                           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  EXPECT_NE(area, MAP_FAILED);
+  unsigned char *ptr = static_cast<unsigned char *>(area) + EXEC_PAGESIZE - 3;
+  res = LIBC_NAMESPACE::mincore(addr, 4 * EXEC_PAGESIZE, ptr);
+  EXPECT_THAT(res, Fails(EFAULT, -1));
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(area, 2), Succeeds());
+}
+
+TEST(LlvmLibcMincoreTest, UnalignedAddr) {
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ,
+                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  EXPECT_NE(addr, MAP_FAILED);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  libc_errno = 0;
+  int res = LIBC_NAMESPACE::mincore(static_cast<char *>(addr) + 1, 1, nullptr);
+  EXPECT_THAT(res, Fails(EINVAL, -1));
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+}
+
+TEST(LlvmLibcMincoreTest, NoError) {
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ,
+                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  EXPECT_NE(addr, MAP_FAILED);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  unsigned char vec;
+  libc_errno = 0;
+  int res = LIBC_NAMESPACE::mincore(addr, 1, &vec);
+  EXPECT_THAT(res, Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+}
+
+TEST(LlvmLibcMincoreTest, NegativeLength) {
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ,
+                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  EXPECT_NE(addr, MAP_FAILED);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  unsigned char vec;
+  libc_errno = 0;
+  int res = LIBC_NAMESPACE::mincore(addr, -1, &vec);
+  EXPECT_THAT(res, Fails(ENOMEM, -1));
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+}
+
+TEST(LlvmLibcMincoreTest, PageOut) {
+  unsigned char vec;
+  void *addr =
+      LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ | PROT_WRITE,
+                           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  EXPECT_NE(addr, MAP_FAILED);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+
+  // touch the page
+  {
+    static_cast<char *>(addr)[0] = 0;
+    libc_errno = 0;
+    int res = LIBC_NAMESPACE::mincore(addr, 1, &vec);
+    EXPECT_EQ(vec & 1u, 1u);
+    EXPECT_THAT(res, Succeeds());
+  }
+
+  // page out the memory
+  {
+    libc_errno = 0;
+    EXPECT_THAT(LIBC_NAMESPACE::madvise(addr, EXEC_PAGESIZE, MADV_DONTNEED),
+                Succeeds());
+
+    libc_errno = 0;
+    int res = LIBC_NAMESPACE::mincore(addr, EXEC_PAGESIZE, &vec);
+    EXPECT_EQ(vec & 1u, 0u);
+    EXPECT_THAT(res, Succeeds());
+  }
+
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+}

>From 612d00533375dbd44e984b9b75ce074144ea13af Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Wed, 24 Jan 2024 10:37:37 -0500
Subject: [PATCH 2/3] additional fixes

---
 libc/test/src/sys/mman/linux/CMakeLists.txt   |  2 +
 libc/test/src/sys/mman/linux/mincore_test.cpp | 76 +++++++++++--------
 2 files changed, 45 insertions(+), 33 deletions(-)

diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 5402ae030b345c..36557695e503c4 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -71,10 +71,12 @@ add_libc_unittest(
     mincore_test.cpp
   DEPENDS
     libc.include.sys_mman
+    libc.include.unistd
     libc.src.errno.errno
     libc.src.sys.mman.mmap
     libc.src.sys.mman.munmap
     libc.src.sys.mman.madvise
     libc.src.sys.mman.mincore
+    libc.src.unistd.sysconf
     libc.test.UnitTest.ErrnoSetterMatcher
 )
diff --git a/libc/test/src/sys/mman/linux/mincore_test.cpp b/libc/test/src/sys/mman/linux/mincore_test.cpp
index 7c8ca3b4ffa4cc..e9e57d3dd5412c 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -6,17 +6,20 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/errno/libc_errno.h"
 #include "src/sys/mman/madvise.h"
 #include "src/sys/mman/mincore.h"
 #include "src/sys/mman/mmap.h"
 #include "src/sys/mman/munmap.h"
+#include "src/unistd/sysconf.h"
 #include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/LibcTest.h"
 #include "test/UnitTest/Test.h"
 
-#include <linux/param.h> // For EXEC_PAGESIZE
 #include <sys/mman.h>
+#include <sys/syscall.h>
+#include <unistd.h>
 
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
 using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
@@ -28,88 +31,95 @@ TEST(LlvmLibcMincoreTest, UnMappedMemory) {
   EXPECT_THAT(res, Fails(ENOMEM, -1));
 }
 
+TEST(LlvmLibcMincoreTest, UnalignedAddr) {
+  unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ,
+                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  EXPECT_NE(addr, MAP_FAILED);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
+  libc_errno = 0;
+  int res = LIBC_NAMESPACE::mincore(static_cast<char *>(addr) + 1, 1, nullptr);
+  EXPECT_THAT(res, Fails(EINVAL, -1));
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
+}
+
 TEST(LlvmLibcMincoreTest, InvalidVec) {
-  void *addr = LIBC_NAMESPACE::mmap(nullptr, 4 * EXEC_PAGESIZE, PROT_READ,
+  unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, 4 * page_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
-  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
   libc_errno = 0;
   int res = LIBC_NAMESPACE::mincore(addr, 1, nullptr);
   EXPECT_THAT(res, Fails(EFAULT, -1));
-  void *area =
-      LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ | PROT_WRITE,
-                           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  void *area = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ | PROT_WRITE,
+                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(area, MAP_FAILED);
-  unsigned char *ptr = static_cast<unsigned char *>(area) + EXEC_PAGESIZE - 3;
-  res = LIBC_NAMESPACE::mincore(addr, 4 * EXEC_PAGESIZE, ptr);
+  unsigned char *ptr = static_cast<unsigned char *>(area) + page_size - 3;
+  res = LIBC_NAMESPACE::mincore(addr, 4 * page_size, ptr);
   EXPECT_THAT(res, Fails(EFAULT, -1));
-  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
   EXPECT_THAT(LIBC_NAMESPACE::munmap(area, 2), Succeeds());
 }
 
-TEST(LlvmLibcMincoreTest, UnalignedAddr) {
-  void *addr = LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ,
-                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
-  EXPECT_NE(addr, MAP_FAILED);
-  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
-  libc_errno = 0;
-  int res = LIBC_NAMESPACE::mincore(static_cast<char *>(addr) + 1, 1, nullptr);
-  EXPECT_THAT(res, Fails(EINVAL, -1));
-  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
-}
-
 TEST(LlvmLibcMincoreTest, NoError) {
-  void *addr = LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ,
+  unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
-  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
   unsigned char vec;
   libc_errno = 0;
   int res = LIBC_NAMESPACE::mincore(addr, 1, &vec);
   EXPECT_THAT(res, Succeeds());
-  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
 }
 
 TEST(LlvmLibcMincoreTest, NegativeLength) {
-  void *addr = LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ,
+  unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ,
                                     MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
-  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
   unsigned char vec;
   libc_errno = 0;
   int res = LIBC_NAMESPACE::mincore(addr, -1, &vec);
   EXPECT_THAT(res, Fails(ENOMEM, -1));
-  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
 }
 
 TEST(LlvmLibcMincoreTest, PageOut) {
+  unsigned long page_size = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
   unsigned char vec;
-  void *addr =
-      LIBC_NAMESPACE::mmap(nullptr, EXEC_PAGESIZE, PROT_READ | PROT_WRITE,
-                           MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+  void *addr = LIBC_NAMESPACE::mmap(nullptr, page_size, PROT_READ | PROT_WRITE,
+                                    MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
   EXPECT_NE(addr, MAP_FAILED);
-  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % EXEC_PAGESIZE, 0ul);
+  EXPECT_EQ(reinterpret_cast<unsigned long>(addr) % page_size, 0ul);
 
   // touch the page
   {
     static_cast<char *>(addr)[0] = 0;
+    LIBC_NAMESPACE::syscall_impl(
+        SYS_mlock, reinterpret_cast<unsigned long>(addr), page_size);
     libc_errno = 0;
     int res = LIBC_NAMESPACE::mincore(addr, 1, &vec);
     EXPECT_EQ(vec & 1u, 1u);
     EXPECT_THAT(res, Succeeds());
+    LIBC_NAMESPACE::syscall_impl(
+        SYS_munlock, reinterpret_cast<unsigned long>(addr), page_size);
   }
 
   // page out the memory
   {
     libc_errno = 0;
-    EXPECT_THAT(LIBC_NAMESPACE::madvise(addr, EXEC_PAGESIZE, MADV_DONTNEED),
+    EXPECT_THAT(LIBC_NAMESPACE::madvise(addr, page_size, MADV_DONTNEED),
                 Succeeds());
 
     libc_errno = 0;
-    int res = LIBC_NAMESPACE::mincore(addr, EXEC_PAGESIZE, &vec);
+    int res = LIBC_NAMESPACE::mincore(addr, page_size, &vec);
     EXPECT_EQ(vec & 1u, 0u);
     EXPECT_THAT(res, Succeeds());
   }
 
-  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, EXEC_PAGESIZE), Succeeds());
+  EXPECT_THAT(LIBC_NAMESPACE::munmap(addr, page_size), Succeeds());
 }

>From a8c88b4540cb7686cf0f22575ae3a1b01b88c3a3 Mon Sep 17 00:00:00 2001
From: Schrodinger ZHU Yifan <yifanzhu at rochester.edu>
Date: Wed, 24 Jan 2024 12:30:27 -0500
Subject: [PATCH 3/3] add TODO

---
 libc/test/src/sys/mman/linux/mincore_test.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libc/test/src/sys/mman/linux/mincore_test.cpp b/libc/test/src/sys/mman/linux/mincore_test.cpp
index e9e57d3dd5412c..3cb37a42cd0ec6 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -99,6 +99,7 @@ TEST(LlvmLibcMincoreTest, PageOut) {
   // touch the page
   {
     static_cast<char *>(addr)[0] = 0;
+    // TODO: use wrapper functions for mlock/munlock once implemented.
     LIBC_NAMESPACE::syscall_impl(
         SYS_mlock, reinterpret_cast<unsigned long>(addr), page_size);
     libc_errno = 0;



More information about the libc-commits mailing list