[libc-commits] [libc] [llvm] [libc][test] Bound mlockall memory locking and skip under sanitizers (PR #242736)
via libc-commits
libc-commits at lists.llvm.org
Sat Nov 14 18:31:02 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Roland McGrath (frobtech)
<details>
<summary>Changes</summary>
Shadow-memory sanitizers map terabytes of shadow memory, which is why
sanitizer_common intercepts mlockall as a no-op. Because
LIBC_NAMESPACE::mlockall invokes SYS_mlockall directly, it bypasses
sanitizer interceptors; calling mlockall(MCL_CURRENT) when
RLIMIT_MEMLOCK is unlimited or CAP_IPC_LOCK is held attempts to
populate and wire-lock the shadow region, exhausting physical RAM.
Define LIBC_HAS_SANITIZER in sanitizer.h for shadow-memory sanitizers
(ASan, MSan, TSan, and HWASan) and skip LlvmLibcMlockTest.MLockAll
under sanitizers. For unprivileged non-sanitizer builds (without
CAP_IPC_LOCK), also cap soft RLIMIT_MEMLOCK to 64 MiB inside MLockAll
so mlockall fails fast with ENOMEM if multi-gigabyte virtual arenas are
mapped on hosts with RLIM_INFINITY.
Assisted-by: Gemini
---
Full diff: https://github.com/llvm/llvm-project/pull/242736.diff
4 Files Affected:
- (modified) libc/src/__support/macros/sanitizer.h (+16)
- (modified) libc/test/src/sys/mman/linux/CMakeLists.txt (+4)
- (modified) libc/test/src/sys/mman/linux/mlock_test.cpp (+42)
- (modified) utils/bazel/llvm-project-overlay/libc/test/src/sys/mman/BUILD.bazel (+4)
``````````diff
diff --git a/libc/src/__support/macros/sanitizer.h b/libc/src/__support/macros/sanitizer.h
index f92088ac245f3c..6218705198fcd4 100644
--- a/libc/src/__support/macros/sanitizer.h
+++ b/libc/src/__support/macros/sanitizer.h
@@ -23,6 +23,22 @@
#define LIBC_HAS_MEMORY_SANITIZER
#endif
+#if LIBC_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__)
+#define LIBC_HAS_THREAD_SANITIZER
+#endif
+
+#if LIBC_HAS_FEATURE(hwaddress_sanitizer) || defined(__SANITIZE_HWADDRESS__)
+#define LIBC_HAS_HWADDRESS_SANITIZER
+#endif
+
+// Shadow-memory sanitizers (ASan, MSan, TSan, HWASan).
+#if defined(LIBC_HAS_ADDRESS_SANITIZER) || \
+ defined(LIBC_HAS_MEMORY_SANITIZER) || \
+ defined(LIBC_HAS_THREAD_SANITIZER) || \
+ defined(LIBC_HAS_HWADDRESS_SANITIZER)
+#define LIBC_HAS_SANITIZER
+#endif
+
#ifdef LIBC_HAS_MEMORY_SANITIZER
// Only perform MSAN unpoison in non-constexpr context.
#include <sanitizer/msan_interface.h>
diff --git a/libc/test/src/sys/mman/linux/CMakeLists.txt b/libc/test/src/sys/mman/linux/CMakeLists.txt
index 072d7225a76fc6..89f73b32dccb18 100644
--- a/libc/test/src/sys/mman/linux/CMakeLists.txt
+++ b/libc/test/src/sys/mman/linux/CMakeLists.txt
@@ -144,9 +144,13 @@ add_libc_test(
libc.src.sys.mman.mlockall
libc.src.sys.mman.munlockall
libc.hdr.sys_resource_macros
+ libc.hdr.types.struct_rlimit
libc.src.sys.resource.getrlimit
+ libc.src.sys.resource.setrlimit
libc.src.unistd.getpagesize
+ libc.src.__support.CPP.scope
libc.src.__support.OSUtil.osutil
+ libc.src.__support.macros.sanitizer
libc.test.UnitTest.ErrnoCheckingTest
libc.test.UnitTest.ErrnoSetterMatcher
)
diff --git a/libc/test/src/sys/mman/linux/mlock_test.cpp b/libc/test/src/sys/mman/linux/mlock_test.cpp
index 526fd36b518270..dc35c45f1227c3 100644
--- a/libc/test/src/sys/mman/linux/mlock_test.cpp
+++ b/libc/test/src/sys/mman/linux/mlock_test.cpp
@@ -12,8 +12,11 @@
#include "hdr/sys_mman_macros.h"
#include "hdr/sys_resource_macros.h"
+#include "hdr/types/struct_rlimit.h"
+#include "src/__support/CPP/scope.h"
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
#include "src/__support/libc_errno.h"
+#include "src/__support/macros/sanitizer.h"
#include "src/sys/mman/madvise.h"
#include "src/sys/mman/mincore.h"
#include "src/sys/mman/mlock.h"
@@ -24,6 +27,7 @@
#include "src/sys/mman/munlockall.h"
#include "src/sys/mman/munmap.h"
#include "src/sys/resource/getrlimit.h"
+#include "src/sys/resource/setrlimit.h"
#include "src/unistd/getpagesize.h"
#include "test/UnitTest/ErrnoCheckingTest.h"
#include "test/UnitTest/ErrnoSetterMatcher.h"
@@ -131,7 +135,44 @@ TEST_F(LlvmLibcMlockTest, InvalidFlag) {
}
#endif // LIBC_TEST_UNDER_EMULATOR
+// Shadow-memory sanitizers reserve terabytes of shadow mappings (which is why
+// sanitizer_common intercepts mlockall as a no-op). Because LIBC_NAMESPACE::
+// mlockall invokes SYS_mlockall directly, MCL_CURRENT attempts to populate and
+// pin the shadow region into physical RAM when RLIMIT_MEMLOCK is unlimited or
+// CAP_IPC_LOCK is held.
+#ifndef LIBC_HAS_SANITIZER
TEST_F(LlvmLibcMlockTest, MLockAll) {
+ // For unprivileged processes (without CAP_IPC_LOCK), cap soft RLIMIT_MEMLOCK
+ // so mlockall fails fast with ENOMEM on RLIM_INFINITY hosts if multi-gigabyte
+ // virtual arenas (such as allocator regions or undetected shadow tools) are
+ // mapped.
+ constexpr rlim_t MAX_TEST_MEMLOCK = 64 * 1024 * 1024;
+ rlimit orig_limits{};
+ ASSERT_THAT(LIBC_NAMESPACE::getrlimit(RLIMIT_MEMLOCK, &orig_limits),
+ Succeeds());
+ bool lowered_rlimit = false;
+ if (orig_limits.rlim_cur > MAX_TEST_MEMLOCK) {
+ rlimit capped_limits = orig_limits;
+ capped_limits.rlim_cur = MAX_TEST_MEMLOCK;
+ if (LIBC_NAMESPACE::setrlimit(RLIMIT_MEMLOCK, &capped_limits) == 0) {
+ lowered_rlimit = true;
+ } else {
+ EXPECT_TRUE(libc_errno == EPERM || libc_errno == ENOSYS);
+ libc_errno = 0;
+ }
+ }
+ LIBC_NAMESPACE::cpp::scope_exit restore_limits([&] {
+ const int saved_errno = libc_errno;
+ LIBC_NAMESPACE::munlockall();
+ libc_errno = 0;
+ if (lowered_rlimit) {
+ EXPECT_THAT(LIBC_NAMESPACE::setrlimit(RLIMIT_MEMLOCK, &orig_limits),
+ Succeeds());
+ libc_errno = 0;
+ }
+ libc_errno = saved_errno;
+ });
+
{
PageHolder holder;
EXPECT_TRUE(holder.is_valid());
@@ -187,3 +228,4 @@ TEST_F(LlvmLibcMlockTest, MLockAll) {
}
#endif
}
+#endif // !LIBC_HAS_SANITIZER
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 415a7581635314..b1bfcbd181e375 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
@@ -42,7 +42,10 @@ libc_test(
deps = [
"//libc/hdr:sys_mman_macros",
"//libc/hdr:sys_resource_macros",
+ "//libc/hdr/types:struct_rlimit",
+ "//libc/src/__support/CPP:scope",
"//libc/src/__support/OSUtil:syscall",
+ "//libc/src/__support/macros:sanitizer",
"//libc/src/sys/mman:madvise",
"//libc/src/sys/mman:mincore",
"//libc/src/sys/mman:mlock",
@@ -53,6 +56,7 @@ libc_test(
"//libc/src/sys/mman:munlockall",
"//libc/src/sys/mman:munmap",
"//libc/src/sys/resource:getrlimit",
+ "//libc/src/sys/resource:setrlimit",
"//libc/src/unistd:getpagesize",
],
)
``````````
</details>
https://github.com/llvm/llvm-project/pull/242736
More information about the libc-commits
mailing list