diff mbox series

[v3,21/29] misc: Add tst-getpagesize

Message ID a4d305f48d27c5fc07cf8c9ddbf85d43d6e22386.1727624528.git.fweimer@redhat.com
State New
Headers show
Series Teach glibc about possible page sizes and handle gaps in ld.so | expand

Commit Message

Florian Weimer Sept. 29, 2024, 4:34 p.m. UTC
This test checks the system page size against the hard-coded range
in <bits/pagesize.h>.
---
 misc/Makefile          |  1 +
 misc/tst-getpagesize.c | 65 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)
 create mode 100644 misc/tst-getpagesize.c
diff mbox series

Patch

diff --git a/misc/Makefile b/misc/Makefile
index a932b1aab4..4250b789d1 100644
--- a/misc/Makefile
+++ b/misc/Makefile
@@ -243,6 +243,7 @@  tests := \
   tst-empty \
   tst-error1 \
   tst-fdset \
+  tst-getpagesize \
   tst-hsearch \
   tst-insremque \
   tst-ioctl \
diff --git a/misc/tst-getpagesize.c b/misc/tst-getpagesize.c
new file mode 100644
index 0000000000..d8bcdc6dc5
--- /dev/null
+++ b/misc/tst-getpagesize.c
@@ -0,0 +1,65 @@ 
+/* Run-time tests for page size.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <support/check.h>
+#include <support/xunistd.h>
+#include <sys/mman.h>
+#include <sys/pagesize.h>
+#include <unistd.h>
+
+#ifdef __linux__
+# define HAVE_AUXV 1
+# include <sys/auxv.h>
+#else
+# define HAVE_AUXV 0
+#endif
+
+static int
+do_test (void)
+{
+  printf ("info: getpagesize (): %d\n", getpagesize ());
+  TEST_COMPARE (getpagesize (), sysconf (_SC_PAGE_SIZE));
+  TEST_COMPARE (_SC_PAGE_SIZE, _SC_PAGESIZE);
+
+  /* The page size in the auxiliary vector is not optimized.  */
+#ifdef HAVE_AUX
+  TEST_COMPARE (getpagesize (), getauxval (AT_PAGESZ));
+#endif
+
+  /* Test against the page size ranges from <sys/pagesize.h>.  */
+  TEST_VERIFY (getpagesize () >= PAGE_SIZE_MIN);
+  TEST_VERIFY (getpagesize () <= PAGE_SIZE_MAX);
+  _Static_assert (PAGE_SHIFT_MIN == __GLIBC_PAGE_SHIFT_MIN, "PAGE_SHIFT_MIN");
+  _Static_assert (PAGE_SHIFT_MAX == __GLIBC_PAGE_SHIFT_MAX, "PAGE_SHIFT_MAX");
+  _Static_assert (PAGE_SIZE_MIN == (1UL << PAGE_SHIFT_MIN), "PAGE_SIZE_MIN");
+  _Static_assert (PAGE_SIZE_MAX == (1UL << PAGE_SHIFT_MAX), "PAGE_SIZE_MAX");
+  _Static_assert (PAGE_SIZE_MIN <= PAGE_SIZE_MAX, "page size ordering");
+
+  /* Create a few mappings to verify that the address is a multiple of
+     the page size.  */
+  for (int i = 0; i < 1000; i++)
+    TEST_COMPARE ((uintptr_t) xmmap (NULL, 1, PROT_READ | PROT_WRITE,
+                                     MAP_PRIVATE | MAP_ANONYMOUS, -1)
+                  % 4096, 0);
+
+  return 0;
+}
+
+#include <support/test-driver.c>