git: de97544ca485 - releng/14.5 - tests: Add test for vn_io_fault1 bug

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 17:04:55 UTC
The branch releng/14.5 has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=de97544ca485fb64c44e45836b5c4022a1c04bdb

commit de97544ca485fb64c44e45836b5c4022a1c04bdb
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-13 00:46:50 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2026-11-15 15:40:22 +0000

    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.
    
    Approved by:    so
    Security:       FreeBSD-EN-26:30.vfs
    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)
    (cherry picked from commit 488e9144c154904391fb224dfce0095e9edff0e6)
---
 tests/sys/kern/Makefile       |  3 ++
 tests/sys/kern/vn_io_fault1.c | 66 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 69 insertions(+)

diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile
index ecb851644fb9..ef3c4613b682 100644
--- a/tests/sys/kern/Makefile
+++ b/tests/sys/kern/Makefile
@@ -65,6 +65,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());
+}