git: ba02bbbe86ae - main - tests: Add test for vn_io_fault1 bug

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Fri, 13 Nov 2026 00:47:15 UTC
The branch main has been updated by des:

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

commit ba02bbbe86aeca0c95c8e7d12c1be357ebb3ddeb
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-11-13 00:46:50 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-11-13 00:46:50 +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.
    
    PR:             298159
    MFC after:      1 week
    Reviewed by:    kib
    Differential Revision:  https://reviews.freebsd.org/D61475
---
 tests/sys/kern/Makefile       |  3 +++
 tests/sys/kern/vn_io_fault1.c | 56 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile
index a59168094c58..0a8615fad24e 100644
--- a/tests/sys/kern/Makefile
+++ b/tests/sys/kern/Makefile
@@ -82,6 +82,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+=	logsigexit_test
diff --git a/tests/sys/kern/vn_io_fault1.c b/tests/sys/kern/vn_io_fault1.c
new file mode 100644
index 000000000000..d393d9d6b089
--- /dev/null
+++ b/tests/sys/kern/vn_io_fault1.c
@@ -0,0 +1,56 @@
+/*-
+ * Copyright (c) 2026 Dag-Erling Smørgrav
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <atf-c.h>
+#include <fcntl.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_BODY(big_unmapped_read, tc)
+{
+	char *buf;
+	size_t len = 3LU << 30;
+	ssize_t ret;
+	int fd;
+
+	/* allocate a large buffer */
+	if ((buf = malloc(len)) == NULL)
+		atf_tc_skip("failed to allocate buffer");
+
+	/* fault in just a tad more than the first 2 GB */
+	for (size_t i = 0; i <= 2LU << 30; i += 4096)
+		buf[i] = 0;
+
+	/* 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 the file into the buffer */
+	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));
+	free(buf);
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, big_unmapped_read);
+	return (atf_no_error());
+}