[PATCH v3 7/7] mremap09: Add test for expanding over unmapped gap

Andrea Cervesato <[email protected]>
Newsgroups gmane.linux.ltp
Message-ID <[email protected]>
From: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>

Add a test case for mremap() without MREMAP_MAYMOVE where we map
three pages, unmap the middle one to create a hole, and then
attempt to expand the first page to cover all three. The mremap()
call must fail with ENOMEM since the third page is occupied.

Signed-off-by: Andrea Cervesato <andrea.cervesato-IBi9RG/[email protected]>
---
 runtest/syscalls                            |  1 +
 testcases/kernel/syscalls/mremap/.gitignore |  1 +
 testcases/kernel/syscalls/mremap/mremap09.c | 59 +++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+)

diff --git a/runtest/syscalls b/runtest/syscalls
index ede795a48..5166886c4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -947,6 +947,7 @@ mremap05 mremap05
 mremap06 mremap06
 mremap07 mremap07
 mremap08 mremap08
+mremap09 mremap09
 
 mseal01 mseal01
 mseal02 mseal02
diff --git a/testcases/kernel/syscalls/mremap/.gitignore b/testcases/kernel/syscalls/mremap/.gitignore
index dd4bf4e8e..a7ae3b643 100644
--- a/testcases/kernel/syscalls/mremap/.gitignore
+++ b/testcases/kernel/syscalls/mremap/.gitignore
@@ -6,3 +6,4 @@
 /mremap06
 /mremap07
 /mremap08
+/mremap09
diff --git a/testcases/kernel/syscalls/mremap/mremap09.c b/testcases/kernel/syscalls/mremap/mremap09.c
new file mode 100644
index 000000000..28ab9e310
--- /dev/null
+++ b/testcases/kernel/syscalls/mremap/mremap09.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Linux Test Project
+ */
+
+/*\
+ * Verify that :manpage:`mremap(2)` fails with errno ``ENOMEM`` when it is used
+ * to expand an existing mapping in place, if the region cannot be expanded at
+ * the current virtual address and the ``MREMAP_MAYMOVE`` flag is not set.
+ *
+ * [Algorithm]
+ *
+ * - map an anonymous region of three pages
+ * - unmap the middle page to create a gap
+ * - call mremap() on the first page to grow it to three pages with ``flags``
+ *   set to 0
+ * - expect the call to fail with ``MAP_FAILED`` and ``ENOMEM`` since the
+ *   third page is occupied.
+ */
+
+#define _GNU_SOURCE
+#include <sys/mman.h>
+#include "tst_test.h"
+
+static size_t pagesize;
+static void *addr = MAP_FAILED;
+
+static void setup(void)
+{
+	pagesize = getpagesize();
+}
+
+static void run(void)
+{
+	size_t map_size = 3 * pagesize;
+	size_t old_size = 1 * pagesize;
+	size_t new_size = 3 * pagesize;
+
+	addr = SAFE_MMAP(NULL, map_size, PROT_READ | PROT_WRITE,
+			 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+
+	/* unmap the middle page to create a gap */
+	SAFE_MUNMAP((char *)addr + pagesize, pagesize);
+
+	TST_EXP_FAIL_PTR_VOID(mremap(addr, old_size, new_size, 0), ENOMEM);
+
+	if (TST_RET_PTR != MAP_FAILED) {
+		SAFE_MUNMAP(TST_RET_PTR, new_size);
+		return;
+	}
+
+	SAFE_MUNMAP(addr, pagesize);
+	SAFE_MUNMAP((char *)addr + 2 * pagesize, pagesize);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+};

-- 
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.