aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDag-Erling Smørgrav <des@FreeBSD.org>2026-11-13 00:46:50 +0000
committerDag-Erling Smørgrav <des@FreeBSD.org>2026-11-14 15:46:33 +0000
commit488e9144c154904391fb224dfce0095e9edff0e6 (patch)
tree5fc675d803753f88af5ed6b89a1612d48ba0a6ef
parent61aa39815b9a44eeb00af6e212e3ef1d2569c1f8 (diff)
tests: Add test for vn_io_fault1 bug
This bug was fixed in b487aca48535, but I did not have a regression test ready for it at the time. The test allocates a large buffer, faults part of it in and leaves the rest unmapped, then tries to read a 3 GiB file into it. If the bug is present, the read succeeds, but the return value exceeds the requested amount by exactly one page less than the amount that was prefaulted. PR: 298159 MFC after: 1 week Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D61475 (cherry picked from commit ba02bbbe86aeca0c95c8e7d12c1be357ebb3ddeb) tests: Improve vn_io_fault1 test * Declare that the test needs 4 GB of memory so it can be automatically skipped on systems with low memory. * Switch back to mmap() as in my initial prototype. Using malloc() is simpler, but on systems with malloc debugging enabled, malloc() will prefault the entire buffer, resulting in a false positive. Using mmap() also guarantees that the buffer is page-aligned, which renders the test determinstic: we now only need to prefault exactly 2 GB, and the difference between ret and len will be exactly the amount that we prefaulted. * To prefault the buffer, instead of a simple loop that writes zeroes to it, which a compiler might recognize as dead stores and eliminate, read the first INT_MAX bytes from the file into it. PR: 298159 Fixes: ba02bbbe86ae ("tests: Add test for vn_io_fault1 bug") Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D61508 (cherry picked from commit c5cff3df96c35aa3613c083f151e853b532d88cb)
-rw-r--r--tests/sys/kern/Makefile3
-rw-r--r--tests/sys/kern/vn_io_fault1.c66
2 files changed, 69 insertions, 0 deletions
diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile
index 0defbd2541eb..99a623581ac3 100644
--- a/tests/sys/kern/Makefile
+++ b/tests/sys/kern/Makefile
@@ -71,6 +71,9 @@ ATF_TESTS_C+= waitpid_nohang
ATF_TESTS_C+= pdeathsig
ATF_TESTS_C+= sigsys
TEST_METADATA.sigsys+= is_exclusive="true"
+.if ${MACHINE_ARCH:M*64*}
+ATF_TESTS_C+= vn_io_fault1
+.endif
ATF_TESTS_SH+= coredump_phnum_test
ATF_TESTS_SH+= jailmeta
diff --git a/tests/sys/kern/vn_io_fault1.c b/tests/sys/kern/vn_io_fault1.c
new file mode 100644
index 000000000000..eabb35d2c9be
--- /dev/null
+++ b/tests/sys/kern/vn_io_fault1.c
@@ -0,0 +1,66 @@
+/*-
+ * Copyright (c) 2026 Dag-Erling Smørgrav
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/mman.h>
+
+#include <atf-c.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+ATF_TC(big_unmapped_read);
+ATF_TC_HEAD(big_unmapped_read, tc)
+{
+ atf_tc_set_md_var(tc, "descr", "Read a large amount of data "
+ "into a partially unmapped buffer");
+ atf_tc_set_md_var(tc, "require.memory", "4G");
+}
+ATF_TC_BODY(big_unmapped_read, tc)
+{
+ char *buf;
+ size_t len = 3LU << 30;
+ ssize_t ret;
+ int fd;
+
+ /* allocate a large buffer */
+ buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ if (buf == MAP_FAILED)
+ atf_tc_skip("failed to allocate buffer");
+
+ /* create a large file */
+ fd = open("big", O_RDWR | O_CREAT | O_TRUNC, 0600);
+ ATF_REQUIRE(fd >= 0);
+ ATF_REQUIRE_EQ_MSG(0, ftruncate(fd, len),
+ "failed to extend file: %m");
+
+ /* read part of the file into the buffer */
+ ATF_REQUIRE_EQ(0, lseek(fd, 0, SEEK_SET));
+ ret = read(fd, buf, INT_MAX);
+ ATF_REQUIRE_MSG(ret >= 0,
+ "failed to read from file: %m");
+ ATF_REQUIRE_EQ_MSG(INT_MAX, (size_t)ret,
+ "requested %zu bytes, got %zd", (size_t)INT_MAX, ret);
+
+ /* read the entire file into the buffer */
+ ATF_REQUIRE_EQ(0, lseek(fd, 0, SEEK_SET));
+ ret = read(fd, buf, len);
+ ATF_REQUIRE_MSG(ret >= 0,
+ "failed to read from file: %m");
+ ATF_REQUIRE_EQ_MSG(len, (size_t)ret,
+ "requested %zu bytes, got %zd", len, ret);
+
+ /* done */
+ ATF_CHECK_EQ(0, close(fd));
+ ATF_CHECK_EQ(0, munmap(buf, len));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, big_unmapped_read);
+ return (atf_no_error());
+}