Skip to contentSkip to conversation

mmap testing suite to cover MAP_32BIT

PATCH In review Andrea Cervesato 5 participants 10 messages Sep 11 – Sep 14

lore
Share & tools
lore
#0 lore raw

v3 cover mmap testing suite to cover MAP_32BIT

Create two new tests to verify MAP_32BIT functionality:

- mmap23: verify if it fails with invalid parameters
- mmap24: verify bondaries of the allocation

---
Changes in v3:
- switch to PROT_NONE and drop memory writes to avoid physical allocation and commit limits
- add unrestricted control mmap() on ENOMEM to detect generic memory exhaustion
- fix format warnings on 32-bit builds by replacing %zu with %lu
- use TESTPTR macro instead of bare mmap() calls

Changes in v2:
- fix description
- Link to v1: https://lore.kernel.org/[email protected]

---
Andrea Cervesato (3):
lapi/mmap: add MAP_32BIT fallback
mmap23: add test for MAP_32BIT oversized mapping
mmap24: add test for MAP_32BIT address limit

 include/lapi/mmap.h                       |   4 ++
 runtest/syscalls                          |   2 +
 testcases/kernel/syscalls/mmap/.gitignore |   2 +
 testcases/kernel/syscalls/mmap/mmap23.c   |  36 ++++++++++
 testcases/kernel/syscalls/mmap/mmap24.c   | 112 ++++++++++++++++++++++++++++++
 5 files changed, 156 insertions(+)

---
base-commit: f6933a16f304f36045ec39300c6496dae452933d
change-id: 20260827-mmap_32bit_coverage-09a22407aa39

Best regards,

Signature · 5 lines
Andrea Cervesato <[email protected]>


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v3 1/3 lapi/mmap: add MAP_32BIT fallback

From: Andrea Cervesato <[email protected]>

---
 include/lapi/mmap.h | 4 ++++
 1 file changed, 4 insertions(+)
include/lapi/mmap.h +4 -0
index 3908310f4..f20eb3bd5 100644
@@ -91,6 +91,10 @@
 # define MAP_DROPPABLE 0x08
 #endif
 
+#ifndef MAP_32BIT
+# define MAP_32BIT 0x40
+#endif
+
 #ifndef MREMAP_DONTUNMAP
 # define MREMAP_DONTUNMAP 4
 #endif
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] lapi/mmap: add MAP_32BIT fallback

Hi Andrea,

Quoted 1 line · Andrea Cervesato

lapi/mmap: add MAP_32BIT fallback

Verdict - Reviewed

---
Note:

The agent can sometimes produce false positives although often its findings are genuine. If you find issues with the review, please comment this email or ignore the suggestions.

Regards,
LTP AI Reviewer

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

PATCH v3 2/3 mmap23: add test for MAP_32BIT oversized mapping

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Add a test verifying that mmap() with the MAP_32BIT flag fails with ENOMEM when requesting a mapping of size >= 2GB, since it cannot fit within the first 2GB of address space.

---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/mmap/.gitignore |  1 +
 testcases/kernel/syscalls/mmap/mmap23.c   | 36 +++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)
runtest/syscalls +1 -0
index ca190dd97..56f88c0b6 100644
@@ -890,6 +890,7 @@ mmap20 mmap20
 mmap21_01 mmap21 -m 1
 mmap21_02 mmap21
 mmap22 mmap22
+mmap23 mmap23
 
 modify_ldt01 modify_ldt01
 modify_ldt02 modify_ldt02
testcases/kernel/syscalls/mmap/.gitignore +1 -0
index 075be933d..dd332e9a1 100644
@@ -19,3 +19,4 @@
 /mmap20
 /mmap21
 /mmap22
+/mmap23
testcases/kernel/syscalls/mmap/mmap23.c new file +36 -0
new file mode 100644
index 000000000..4eb7bf6e9
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2026 SUSE LLC Andrea Cervesato <[email protected]>
+ */
+
+/*\
+ * Verify that :manpage:`mmap(2)` with the MAP_32BIT flag fails with ENOMEM
+ * when requesting a mapping of size >= 2GB, since it cannot fit within the
+ * first 2GB of address space.
+ *
+ * MAP_32BIT is supported only on x86-64 for 64-bit programs.
+ */
+
+#include "tst_test.h"
+#include "lapi/mmap.h"
+
+#define MAP_SZ (2UL * TST_GB)
+
+static void run(void)
+{
+	TST_EXP_FAIL_PTR_VOID(mmap(NULL, MAP_SZ, PROT_READ | PROT_WRITE,
+				   MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0),
+			      ENOMEM,
+			      "mmap(2GB) with MAP_32BIT");
+
+	if (TST_RET_PTR != MAP_FAILED)
+		SAFE_MUNMAP(TST_RET_PTR, MAP_SZ);
+}
+
+static struct tst_test test = {
+	.test_all = run,
+	.supported_archs = (const char *const []){
+		"x86_64",
+		NULL
+	},
+};
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v3 2/3
#5 lore raw

Re: [LTP] [PATCH v3 2/3] mmap23: add test for MAP_32BIT oversized mapping

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

PATCH v3 3/3 mmap24: add test for MAP_32BIT address limit

In reply to Andrea Cervesato

From: Andrea Cervesato <[email protected]>

Add a test verifying that mmap() with the MAP_32BIT flag restricts mappings to the first 2GB of address space and fails with ENOMEM when the 32-bit address space is exhausted without falling back to higher addresses.

---
 runtest/syscalls                          |   1 +
 testcases/kernel/syscalls/mmap/.gitignore |   1 +
 testcases/kernel/syscalls/mmap/mmap24.c   | 112 ++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
runtest/syscalls +1 -0
index 56f88c0b6..418e8f64d 100644
@@ -891,6 +891,7 @@ mmap21_01 mmap21 -m 1
 mmap21_02 mmap21
 mmap22 mmap22
 mmap23 mmap23
+mmap24 mmap24
 
 modify_ldt01 modify_ldt01
 modify_ldt02 modify_ldt02
testcases/kernel/syscalls/mmap/.gitignore +1 -0
index dd332e9a1..f072e7f64 100644
@@ -20,3 +20,4 @@
 /mmap21
 /mmap22
 /mmap23
+/mmap24
testcases/kernel/syscalls/mmap/mmap24.c new file +112 -0

112 lines not on this page · raw message

Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v3 3/3
#7 lore raw

Re: [LTP] [PATCH v3 3/3] mmap24: add test for MAP_32BIT address limit

Quoted 8 lines

+/*\
+ * Verify that :manpage:mmap(2) with the MAP_32BIT flag restricts all mappings
+ * to the first 2GB of the process address space (< 0x80000000).
+ *
+ * MAP_32BIT is supported only on x86-64 for 64-bit programs.
+ *
+ * Use PROT_NONE to test address placement without allocating data pages
+ * or charging private writable memory against the commit limit.

This test itself looks good, but unlike the address placement way, maybe we can create a new test (mmap25.c) that allocates real read-write anonymous memory, writes a pattern, reads it back, and checks the result.

This verifies that read-write memory mapped with MAP_32BIT functions correctly.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp