[libc] [llvm] Add support for getpagesize (PR #171713)

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 13:53:25 PST 2025


https://github.com/Sterling-Augustine created https://github.com/llvm/llvm-project/pull/171713

As in the description.



>From 04d3cd82b1ee7336d3312426cf2d66a19f108cd3 Mon Sep 17 00:00:00 2001
From: Sterling Augustine <saugustine at google.com>
Date: Wed, 10 Dec 2025 10:32:58 -0800
Subject: [PATCH 1/2] Add support for getpagesize

---
 libc/config/linux/aarch64/entrypoints.txt |  1 +
 libc/config/linux/riscv/entrypoints.txt   |  1 +
 libc/config/linux/x86_64/entrypoints.txt  |  1 +
 libc/include/sys/syscall.h.def            |  2 +-
 libc/include/unistd.yaml                  |  6 ++++++
 libc/src/unistd/CMakeLists.txt            |  7 +++++++
 libc/src/unistd/getpagesize.cpp           | 16 +++++++++++++++
 libc/src/unistd/getpagesize.h             | 20 +++++++++++++++++++
 libc/src/unistd/linux/CMakeLists.txt      | 10 ++++++++++
 libc/src/unistd/linux/getpagesize.cpp     | 21 ++++++++++++++++++++
 libc/test/src/unistd/CMakeLists.txt       | 11 +++++++++++
 libc/test/src/unistd/getpagesize_test.cpp | 24 +++++++++++++++++++++++
 libc/utils/docgen/unistd.yaml             |  2 ++
 13 files changed, 121 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/unistd/getpagesize.cpp
 create mode 100644 libc/src/unistd/getpagesize.h
 create mode 100644 libc/src/unistd/linux/getpagesize.cpp
 create mode 100644 libc/test/src/unistd/getpagesize_test.cpp

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index acfd4c8a14acb..970c825bbfc96 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -335,6 +335,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.getentropy
     libc.src.unistd.geteuid
     libc.src.unistd.gethostname
+    libc.src.unistd.getpagesize
     libc.src.unistd.getpid
     libc.src.unistd.getppid
     libc.src.unistd.getsid
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 83d5c42816632..7baf4de9d8a5b 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -338,6 +338,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.getentropy
     libc.src.unistd.geteuid
     libc.src.unistd.gethostname
+    libc.src.unistd.getpagesize
     libc.src.unistd.getpid
     libc.src.unistd.getppid
     libc.src.unistd.getsid
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 4b6c10917c315..9399b284fa2da 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -347,6 +347,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.unistd.getentropy
     libc.src.unistd.geteuid
     libc.src.unistd.gethostname
+    libc.src.unistd.getpagesize
     libc.src.unistd.getpid
     libc.src.unistd.getppid
     libc.src.unistd.getsid
diff --git a/libc/include/sys/syscall.h.def b/libc/include/sys/syscall.h.def
index f7e53cc4942d5..21e42ad4abbf2 100644
--- a/libc/include/sys/syscall.h.def
+++ b/libc/include/sys/syscall.h.def
@@ -532,7 +532,7 @@
 #ifdef __NR_getitimer
 #define SYS_getitimer __NR_getitimer
 #endif
-
+alskdfjlaskdjflkasjdf
 #ifdef __NR_getpagesize
 #define SYS_getpagesize __NR_getpagesize
 #endif
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index 3f5e957768533..a576188bdee8f 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -174,6 +174,12 @@ functions:
     arguments:
       - type: char *
       - type: size_t
+  - name: getpagesize
+    standards:
+      - GNUExtensions
+    return_type: int
+    arguments:
+      - type: void
   - name: getopt
     standards:
       - POSIX
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index b7444a4722b0d..097d75b3f7bf5 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -139,6 +139,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.gethostname
 )
 
+add_entrypoint_object(
+  getpagesize
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.getpagesize
+)
+
 add_entrypoint_object(
   getpid
   ALIAS
diff --git a/libc/src/unistd/getpagesize.cpp b/libc/src/unistd/getpagesize.cpp
new file mode 100644
index 0000000000000..68dcdd7f94b4f
--- /dev/null
+++ b/libc/src/unistd/getpagesize.cpp
@@ -0,0 +1,16 @@
+//===-- Implementation file for getpagesize ---------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/getpagesize.h"
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, getpagesize, ()) { return internal::getpagesize(); }
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/unistd/getpagesize.h b/libc/src/unistd/getpagesize.h
new file mode 100644
index 0000000000000..4ecafa78ef502
--- /dev/null
+++ b/libc/src/unistd/getpagesize.h
@@ -0,0 +1,20 @@
+//===-- Implementation header for getpagesize -------------------*- 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_UNISTD_GETPAGESIZE_H
+#define LLVM_LIBC_SRC_UNISTD_GETPAGESIZE_H
+
+#include "src/__support/common.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int getpagesize();
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_UNISTD_GETPAGESIZE_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index c45b6ef1c5d80..1501b43b45ef7 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -249,6 +249,16 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  getpagesize
+  SRCS
+    getpagesize.cpp
+  HDRS
+    ../getpagesize.h
+  DEPENDS
+    libc.src.unistd.sysconf
+)
+
 add_entrypoint_object(
   geteuid
   SRCS
diff --git a/libc/src/unistd/linux/getpagesize.cpp b/libc/src/unistd/linux/getpagesize.cpp
new file mode 100644
index 0000000000000..22637672985f6
--- /dev/null
+++ b/libc/src/unistd/linux/getpagesize.cpp
@@ -0,0 +1,21 @@
+//===-- Linux implementation of getpagesize -------------------------------===//
+//
+// 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/unistd/getpagesize.h"
+
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/unistd/sysconf.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, getpagesize, ()) {
+  return static_cast<int>(LIBC_NAMESPACE::sysconf(_SC_PAGESIZE));
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 3012ea9a466f4..4f748746706a3 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -477,6 +477,17 @@ add_libc_unittest(
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
+add_libc_unittest(
+  getpagesize_test
+  SUITE
+    libc_unistd_unittests
+  SRCS
+    getpagesize_test.cpp
+  DEPENDS
+    libc.src.unistd.getpagesize
+    libc.test.UnitTest.ErrnoCheckingTest
+)
+
 add_libc_unittest(
   getgid_test
   SUITE
diff --git a/libc/test/src/unistd/getpagesize_test.cpp b/libc/test/src/unistd/getpagesize_test.cpp
new file mode 100644
index 0000000000000..21bf491231dcc
--- /dev/null
+++ b/libc/test/src/unistd/getpagesize_test.cpp
@@ -0,0 +1,24 @@
+//===-- Unittests for getpagesize -----------------------------------------===//
+//
+// 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/unistd/getpagesize.h"
+
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcGetPageSizeTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST(LlvmLibcGetPageSizeTest, GetPageSize) {
+  // getpagesize doesn't modify errno
+  int ret = LIBC_NAMESPACE::getpagesize();
+  ASSERT_NE(ret, -1);
+  ASSERT_NE(ret, 0);
+  // Correct page size depends on the hardware mode, but will be a modulus of
+  // 4096
+  ASSERT_EQ(ret % 4096, 0);
+}
diff --git a/libc/utils/docgen/unistd.yaml b/libc/utils/docgen/unistd.yaml
index 5cdb1351322e0..d5c742e42a298 100644
--- a/libc/utils/docgen/unistd.yaml
+++ b/libc/utils/docgen/unistd.yaml
@@ -577,6 +577,8 @@ functions:
     in-latest-posix: ""
   gethostname:
     in-latest-posix: ""
+  getpagesize:
+    in-latest-posix: ""
   getlogin:
     in-latest-posix: ""
   getlogin_r:

>From f6e113a824ba7baa1241a6669f3d7b677e91a1a0 Mon Sep 17 00:00:00 2001
From: Sterling Augustine <saugustine at google.com>
Date: Wed, 10 Dec 2025 13:25:42 -0800
Subject: [PATCH 2/2] Add bazel support for getpagesize.

---
 utils/bazel/llvm-project-overlay/libc/BUILD.bazel        | 9 +++++++++
 .../libc/test/src/unistd/BUILD.bazel                     | 8 ++++++++
 2 files changed, 17 insertions(+)

diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 17185dfe20fac..edd1ce50e4760 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -6092,6 +6092,15 @@ libc_function(
     ],
 )
 
+libc_function(
+    name = "getpagesize",
+    srcs = ["src/unistd/linux/getpagesize.cpp"],
+    hdrs = ["src/unistd/getpagesize.h"],
+    deps = [
+        ":sysconf",
+    ],
+)
+
 libc_function(
     name = "getppid",
     srcs = ["src/unistd/linux/getppid.cpp"],
diff --git a/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
index ac74513c8c4af..bfec523bfb501 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/src/unistd/BUILD.bazel
@@ -74,6 +74,14 @@ libc_test(
     ],
 )
 
+libc_test(
+    name = "getpagesize_test",
+    srcs = ["getpagesize_test.cpp"],
+    deps = [
+        "//libc:getpagesize",
+    ],
+)
+
 libc_test(
     name = "pread_pwrite_test",
     srcs = ["pread_pwrite_test.cpp"],



More information about the llvm-commits mailing list