[libc-commits] [libc] [libc] Make <sys/mman.h> tests hermetic (PR #210697)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Mon Jul 20 05:03:24 PDT 2026


https://github.com/labath created https://github.com/llvm/llvm-project/pull/210697

The only issue was in pkey_test, which uses function-local static to do lazy initialization. This requires the c++ runtime (__cxa_guard_acquire/release).

There are no threads in this test, so we can just use the (non-thread-safe) hand-rolled version of that. This is currently our only test with such a pattern.

This would be something that in gtest would go into a SetUpTestSuite method, but our test framework currently doesn't have an equivalent.

>From 0922bff5806720296332a250d5c14fa6d9af3b79 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Mon, 20 Jul 2026 11:52:44 +0000
Subject: [PATCH] [libc] Make <sys/mman.h> tests hermetic

The only issue was in pkey_test, which uses function-local static to do
lazy initialization. This requires the c++ runtime
(__cxa_guard_acquire/release).

There are no threads in this test, so we can just use the
(non-thread-safe) hand-rolled version of that. This is currently our
only test with such a pattern.

This would be something that in gtest would go into a SetUpTestSuite
method, but our test framework currently doesn't have an equivalent.
---
 libc/test/src/sys/mman/linux/CMakeLists.txt | 24 ++++++++++-----------
 libc/test/src/sys/mman/linux/pkey_test.cpp  |  8 ++++++-
 2 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 627952f654d5f..e46bdb59a42f0 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -1,6 +1,6 @@
 add_custom_target(libc_sys_mman_unittests)
 
-add_libc_unittest(
+add_libc_test(
   mmap_test
   SUITE
     libc_sys_mman_unittests
@@ -18,7 +18,7 @@ add_libc_unittest(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-add_libc_unittest(
+add_libc_test(
   mremap_test
   SUITE
     libc_sys_mman_unittests
@@ -36,7 +36,7 @@ add_libc_unittest(
 )
 
 if (NOT LLVM_USE_SANITIZER)
-  add_libc_unittest(
+  add_libc_test(
     mprotect_test
     SUITE
       libc_sys_mman_unittests
@@ -54,7 +54,7 @@ if (NOT LLVM_USE_SANITIZER)
   )
 endif()
 
-add_libc_unittest(
+add_libc_test(
   madvise_test
   SUITE
     libc_sys_mman_unittests
@@ -75,7 +75,7 @@ add_libc_unittest(
 # This test intentionally triggers segfaults to verify pkey_mprotect behavior,
 # and sanitizers register signal handlers that interfere with death testing.
 if (NOT LLVM_USE_SANITIZER)
-  add_libc_unittest(
+  add_libc_test(
     pkey_test
     SUITE
       libc_sys_mman_unittests
@@ -98,7 +98,7 @@ if (NOT LLVM_USE_SANITIZER)
   )
 endif()
 
-add_libc_unittest(
+add_libc_test(
   posix_madvise_test
   SUITE
     libc_sys_mman_unittests
@@ -115,7 +115,7 @@ add_libc_unittest(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-add_libc_unittest(
+add_libc_test(
   mincore_test
   SUITE
     libc_sys_mman_unittests
@@ -137,7 +137,7 @@ add_libc_unittest(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-add_libc_unittest(
+add_libc_test(
   mlock_test
   SUITE
     libc_sys_mman_unittests
@@ -165,7 +165,7 @@ add_libc_unittest(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-add_libc_unittest(
+add_libc_test(
   msync_test
   SUITE
     libc_sys_mman_unittests
@@ -192,7 +192,7 @@ if(CMAKE_CROSSCOMPILING_EMULATOR)
     message(STATUS "Skipping remap_file_pages_test under emulator.")
   endif()
 else()
-add_libc_unittest(
+add_libc_test(
   remap_file_pages_test
   SUITE
     libc_sys_mman_unittests
@@ -213,7 +213,7 @@ add_libc_unittest(
 )
 endif()
 
-add_libc_unittest(
+add_libc_test(
   shm_test
   SUITE
     libc_sys_mman_unittests
@@ -237,7 +237,7 @@ add_libc_unittest(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-add_libc_unittest(
+add_libc_test(
   memfd_create_test
   SUITE
     libc_sys_mman_unittests
diff --git a/libc/test/src/sys/mman/linux/pkey_test.cpp b/libc/test/src/sys/mman/linux/pkey_test.cpp
index 73945df0d4177..d81df4057501f 100644
--- a/libc/test/src/sys/mman/linux/pkey_test.cpp
+++ b/libc/test/src/sys/mman/linux/pkey_test.cpp
@@ -73,7 +73,13 @@ class MMapPageGuard {
 };
 
 bool protection_keys_supported() {
-  static bool supported = []() {
+  static bool checked = false;
+  static bool supported;
+  if (checked)
+    return supported;
+
+  checked = true;
+  supported = []() {
     PKeyGuard pkey(LIBC_NAMESPACE::pkey_alloc(0, 0));
     int err = libc_errno;
     libc_errno = 0;



More information about the libc-commits mailing list