[libc-commits] [libc] [llvm] libc: Introduce calls to sysconf to get page size. (PR #163462)
Peter Collingbourne via libc-commits
libc-commits at lists.llvm.org
Wed Oct 15 11:23:13 PDT 2025
https://github.com/pcc updated https://github.com/llvm/llvm-project/pull/163462
>From 1ed982952c3a1b615736e2dcccf7b8541b1d6949 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <pcc at google.com>
Date: Tue, 14 Oct 2025 14:55:56 -0700
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
=?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.6-beta.1
---
libc/test/src/sys/mman/linux/mincore_test.cpp | 5 +++--
libc/test/src/sys/mman/linux/mlock_test.cpp | 4 ++--
libc/test/src/sys/mman/linux/msync_test.cpp | 5 +++--
libc/test/src/sys/mman/linux/remap_file_pages_test.cpp | 4 ++--
4 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/libc/test/src/sys/mman/linux/mincore_test.cpp b/libc/test/src/sys/mman/linux/mincore_test.cpp
index fb86252992def..7d470fc4b08b1 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -16,12 +16,13 @@
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
+#include <unistd.h>
+
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
using LlvmLibcMincoreTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
-// TODO: Replace with sysconf call once the function is properly implemented.
-constexpr size_t PAGE_SIZE = 4096;
+const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
TEST_F(LlvmLibcMincoreTest, UnMappedMemory) {
unsigned char vec;
diff --git a/libc/test/src/sys/mman/linux/mlock_test.cpp b/libc/test/src/sys/mman/linux/mlock_test.cpp
index f4a072ec18a31..834c513fdda0b 100644
--- a/libc/test/src/sys/mman/linux/mlock_test.cpp
+++ b/libc/test/src/sys/mman/linux/mlock_test.cpp
@@ -27,9 +27,9 @@
#include "test/UnitTest/Test.h"
#include <sys/syscall.h>
+#include <unistd.h>
-// TODO: Replace with sysconf call once the function is properly implemented.
-constexpr size_t PAGE_SIZE = 4096;
+const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
using LlvmLibcMlockTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
diff --git a/libc/test/src/sys/mman/linux/msync_test.cpp b/libc/test/src/sys/mman/linux/msync_test.cpp
index 764a67d02e3b1..e99328992800b 100644
--- a/libc/test/src/sys/mman/linux/msync_test.cpp
+++ b/libc/test/src/sys/mman/linux/msync_test.cpp
@@ -15,8 +15,9 @@
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
-// TODO: Replace with sysconf call once the function is properly implemented.
-constexpr size_t PAGE_SIZE = 4096;
+#include <unistd.h>
+
+const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
using LlvmLibcMsyncTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
diff --git a/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp b/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
index 094bcb2c71427..f10c8d4211631 100644
--- a/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
+++ b/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
@@ -17,9 +17,9 @@
#include <sys/mman.h>
#include <sys/stat.h> // For S_IRWXU
+#include <unistd.h>
-// TODO: Replace with sysconf call once the function is properly implemented.
-constexpr size_t PAGE_SIZE = 4096;
+const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
>From 4e70bf8b08187edc4af098afab194687596998d3 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <pcc at google.com>
Date: Tue, 14 Oct 2025 17:04:02 -0700
Subject: [PATCH 2/3] Address review comments
Created using spr 1.3.6-beta.1
---
libc/test/src/sys/mman/linux/CMakeLists.txt | 4 ++++
libc/test/src/sys/mman/linux/mincore_test.cpp | 5 ++---
libc/test/src/sys/mman/linux/mlock_test.cpp | 4 ++--
libc/test/src/sys/mman/linux/msync_test.cpp | 5 ++---
libc/test/src/sys/mman/linux/remap_file_pages_test.cpp | 4 ++--
5 files changed, 12 insertions(+), 10 deletions(-)
diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index a362c1cf61cbc..32fee920321c0 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -99,6 +99,7 @@ add_libc_unittest(
libc.src.sys.mman.mincore
libc.src.sys.mman.mlock
libc.src.sys.mman.munlock
+ libc.src.unistd.sysconf
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
@@ -123,6 +124,7 @@ add_libc_unittest(
libc.src.sys.mman.mlockall
libc.src.sys.mman.munlockall
libc.src.sys.resource.getrlimit
+ libc.src.unistd.sysconf
libc.src.__support.OSUtil.osutil
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
@@ -144,6 +146,7 @@ add_libc_unittest(
libc.src.sys.mman.mincore
libc.src.sys.mman.mlock
libc.src.sys.mman.munlock
+ libc.src.unistd.sysconf
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
@@ -165,6 +168,7 @@ add_libc_unittest(
libc.src.sys.mman.munmap
libc.src.fcntl.open
libc.src.unistd.close
+ libc.src.unistd.sysconf
)
add_libc_unittest(
diff --git a/libc/test/src/sys/mman/linux/mincore_test.cpp b/libc/test/src/sys/mman/linux/mincore_test.cpp
index 7d470fc4b08b1..1806bb1d671a2 100644
--- a/libc/test/src/sys/mman/linux/mincore_test.cpp
+++ b/libc/test/src/sys/mman/linux/mincore_test.cpp
@@ -12,17 +12,16 @@
#include "src/sys/mman/mmap.h"
#include "src/sys/mman/munlock.h"
#include "src/sys/mman/munmap.h"
+#include "src/unistd/sysconf.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
-#include <unistd.h>
-
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
using LlvmLibcMincoreTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
-const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
+const size_t PAGE_SIZE = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
TEST_F(LlvmLibcMincoreTest, UnMappedMemory) {
unsigned char vec;
diff --git a/libc/test/src/sys/mman/linux/mlock_test.cpp b/libc/test/src/sys/mman/linux/mlock_test.cpp
index 834c513fdda0b..0056ccf7f0b3d 100644
--- a/libc/test/src/sys/mman/linux/mlock_test.cpp
+++ b/libc/test/src/sys/mman/linux/mlock_test.cpp
@@ -22,14 +22,14 @@
#include "src/sys/mman/munlockall.h"
#include "src/sys/mman/munmap.h"
#include "src/sys/resource/getrlimit.h"
+#include "src/unistd/sysconf.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include <sys/syscall.h>
-#include <unistd.h>
-const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
+const size_t PAGE_SIZE = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
using LlvmLibcMlockTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
diff --git a/libc/test/src/sys/mman/linux/msync_test.cpp b/libc/test/src/sys/mman/linux/msync_test.cpp
index e99328992800b..bf9640d39ebd6 100644
--- a/libc/test/src/sys/mman/linux/msync_test.cpp
+++ b/libc/test/src/sys/mman/linux/msync_test.cpp
@@ -11,13 +11,12 @@
#include "src/sys/mman/msync.h"
#include "src/sys/mman/munlock.h"
#include "src/sys/mman/munmap.h"
+#include "src/unistd/sysconf.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
-#include <unistd.h>
-
-const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
+const size_t PAGE_SIZE = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
using LlvmLibcMsyncTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
diff --git a/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp b/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
index f10c8d4211631..08ffffe35ab2d 100644
--- a/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
+++ b/libc/test/src/sys/mman/linux/remap_file_pages_test.cpp
@@ -11,15 +11,15 @@
#include "src/sys/mman/munmap.h"
#include "src/sys/mman/remap_file_pages.h"
#include "src/unistd/close.h"
+#include "src/unistd/sysconf.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"
#include <sys/mman.h>
#include <sys/stat.h> // For S_IRWXU
-#include <unistd.h>
-const size_t PAGE_SIZE = sysconf(_SC_PAGESIZE);
+const size_t PAGE_SIZE = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;
>From eb4904ddeec76db96e0878e5adab4ac9f5f8f723 Mon Sep 17 00:00:00 2001
From: Peter Collingbourne <pcc at google.com>
Date: Wed, 15 Oct 2025 11:22:56 -0700
Subject: [PATCH 3/3] Update bazel files
Created using spr 1.3.6-beta.1
---
.../llvm-project-overlay/libc/BUILD.bazel | 28 +++++++++++++++++++
.../libc/test/src/sys/mman/BUILD.bazel | 4 +++
2 files changed, 32 insertions(+)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 936bc1248b6ef..c7e3aa692b1fb 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -1501,6 +1501,21 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__support_osutil_linux_auxv",
+ hdrs = ["src/__support/OSUtil/linux/auxv.h"],
+ target_compatible_with = select({
+ "@platforms//os:linux": [],
+ "//conditions:default": ["@platforms//:incompatible"],
+ }),
+ deps = [
+ ":__support_common",
+ ":__support_osutil_syscall",
+ ":__support_threads_callonce",
+ ":hdr_fcntl_macros",
+ ],
+)
+
libc_support_library(
name = "__support_osutil_vdso",
hdrs = [
@@ -6336,6 +6351,19 @@ libc_function(
],
)
+# WARNING: NOT FULLY IMPLEMENTED, FOR TESTING USE ONLY
+libc_function(
+ name = "sysconf",
+ srcs = ["src/unistd/linux/sysconf.cpp"],
+ hdrs = ["src/unistd/sysconf.h"],
+ deps = [
+ ":__support_common",
+ ":__support_osutil_linux_auxv",
+ ":errno",
+ ":hdr_unistd_macros",
+ ],
+)
+
libc_function(
name = "write",
srcs = ["src/unistd/linux/write.cpp"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/sys/mman/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/sys/mman/BUILD.bazel
index e2c7f7a8bf60b..6de76e2357b70 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/sys/mman/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/sys/mman/BUILD.bazel
@@ -30,6 +30,7 @@ libc_test(
"//libc:mmap",
"//libc:munlock",
"//libc:munmap",
+ "//libc:sysconf",
],
)
@@ -48,6 +49,7 @@ libc_test(
"//libc:munlock",
"//libc:munlockall",
"//libc:munmap",
+ "//libc:sysconf",
],
)
@@ -89,6 +91,7 @@ libc_test(
"//libc:msync",
"//libc:munlock",
"//libc:munmap",
+ "//libc:sysconf",
],
)
@@ -111,6 +114,7 @@ libc_test(
"//libc:munmap",
"//libc:open",
"//libc:remap_file_pages",
+ "//libc:sysconf",
],
)
More information about the libc-commits
mailing list