From f274af17de3d208a9a26517d2964c79ff70d539b Mon Sep 17 00:00:00 2001 From: Chen Chengjun Date: Fri, 27 Jun 2025 07:42:07 +0000 Subject: [PATCH] Add a regression test --- test/apps/mmap/mmap_beyond_the_file.c | 29 +++++++++++++++++++++++++++ test/apps/scripts/process.sh | 1 + 2 files changed, 30 insertions(+) create mode 100644 test/apps/mmap/mmap_beyond_the_file.c diff --git a/test/apps/mmap/mmap_beyond_the_file.c b/test/apps/mmap/mmap_beyond_the_file.c new file mode 100644 index 000000000..241d19908 --- /dev/null +++ b/test/apps/mmap/mmap_beyond_the_file.c @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MPL-2.0 + +#include +#include +#include + +#include "../test.h" + +#define PAGE_SIZE 4096 + +FN_TEST(mmap_beyond_the_file) +{ + const char *filename = "mmap_test_file"; + int fd = TEST_SUCC(open(filename, O_CREAT | O_RDWR, 0600)); + TEST_SUCC(ftruncate(fd, 2 * PAGE_SIZE)); + + char *addr = CHECK_WITH(mmap(NULL, 4 * PAGE_SIZE, + PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0), + _ret != MAP_FAILED); + TEST_RES(addr[1 * PAGE_SIZE], _ret == 0); + // The following operation (if uncommented) would cause a segmentation fault. + // TEST_RES(addr[3 * PAGE_SIZE], _ret == 0); + + TEST_SUCC(munmap(addr, 4 * PAGE_SIZE)); + + TEST_SUCC(close(fd)); + TEST_SUCC(unlink(filename)); +} +END_TEST() diff --git a/test/apps/scripts/process.sh b/test/apps/scripts/process.sh index 606cea4bf..669a7a5d1 100755 --- a/test/apps/scripts/process.sh +++ b/test/apps/scripts/process.sh @@ -29,6 +29,7 @@ itimer/setitimer itimer/timer_create mmap/mmap_and_fork mmap/mmap_and_mremap +mmap/mmap_beyond_the_file mmap/mmap_shared_filebacked mmap/mmap_readahead mmap/mmap_vmrss