[libc-commits] [libc] Create a poor-developer's msan for libc wide read functions. (PR #170586)

via libc-commits libc-commits at lists.llvm.org
Thu Dec 4 10:15:18 PST 2025


================
@@ -0,0 +1,98 @@
+// For performance, some vector-based libc functions read data outside of, but
+// adjacent to, the input address. For example, string_length can read both
+// before and after the data in its src parameter. As part of the
+// implementation, it is allowed to do this. However, the code must take care
+// to avoid address errors. The sanitizers can't distinguish between "the
+// implementation" and user-code, and so report an error. Therefore we can't use
+// them to check if functions like thees have memory errors.
+//
+// This test uses mprotect to simulate address sanitization. Tests that read too
+// far outside data will segfault.
+//
+// It creates three adjacent pages in memory. The outer two are mprotected
+// unreadable, the middle usable normally. By placing test data at the edges
+// between the middle page and the others, we can test for bad accesses.
+
+#include <cstddef>
+#include <type_traits>
+#include <vector>
+
+#include <assert.h>
+#include <sys/mman.h>
+#include <unistd.h>
----------------
lntue wrote:

You'll need to replace these with our internal implementations / headers. I see we already have `mprotect`, `mmap`, `munmap`.  You might need to add `getpagesize` entrypoint (in a separate PR).  Also you can use our `inline_memcpy` instead of `BasicMemCopy`.

One good thing with using those is that with correct dependency, this test will be skipped automatically for any build configurations where those syscalls are not available / implemented.

https://github.com/llvm/llvm-project/pull/170586


More information about the libc-commits mailing list