aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAvinesh Kumar <akumar@suse.de>2024-01-24 14:26:02 +0100
committerPetr Vorel <pvorel@suse.cz>2024-01-25 09:24:40 +0100
commit6c4bf3a75f4ba17503bc2d8ba91a7514fdae995e (patch)
tree4a13df04c13e9e81f059d4a751b7f4bef69d0fbd
parent9062824a70b8da74aa5b1db08710d0018b48072e (diff)
downloadltp-6c4bf3a75f4ba17503bc2d8ba91a7514fdae995e.tar.gz
mmap04.c: Avoid vma merging
We hit a scenario where new mapping was merged with existing mapping of same permission and the return address from the mmap was hidden in the merged mapping in /proc/self/maps, causing the test to fail. To avoid this, we first create a 2-page mapping with the different permissions, and then remap the 2nd page with the perms being tested. Link: https://lore.kernel.org/ltp/20240124132603.16199-1-akumar@suse.de/ Reported-by: Martin Doucha <mdoucha@suse.cz> Reviewed-by: Martin Doucha <mdoucha@suse.cz> Reviewed-by: Petr Vorel <pvorel@suse.cz> Signed-off-by: Avinesh Kumar <akumar@suse.de>
-rw-r--r--testcases/kernel/syscalls/mmap/mmap04.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/testcases/kernel/syscalls/mmap/mmap04.c b/testcases/kernel/syscalls/mmap/mmap04.c
index f6f4f7c98..fa85deed1 100644
--- a/testcases/kernel/syscalls/mmap/mmap04.c
+++ b/testcases/kernel/syscalls/mmap/mmap04.c
@@ -17,8 +17,8 @@
#include "tst_test.h"
#include <stdio.h>
-#define MMAPSIZE 1024
-static char *addr;
+static char *addr1;
+static char *addr2;
static struct tcase {
int prot;
@@ -44,14 +44,23 @@ static struct tcase {
static void run(unsigned int i)
{
struct tcase *tc = &tcases[i];
- char addr_str[20];
char perms[8];
char fmt[1024];
+ unsigned int pagesize;
+ int flag;
- addr = SAFE_MMAP(NULL, MMAPSIZE, tc->prot, tc->flags, -1, 0);
+ pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
- sprintf(addr_str, "%" PRIxPTR, (uintptr_t)addr);
- sprintf(fmt, "%s-%%*x %%s", addr_str);
+ /* To avoid new mapping getting merged with existing mappings, we first
+ * create a 2-page mapping with the different permissions, and then remap
+ * the 2nd page with the perms being tested.
+ */
+ flag = (tc->flags & MAP_PRIVATE) ? MAP_SHARED : MAP_PRIVATE;
+ addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_NONE, MAP_ANONYMOUS | flag, -1, 0);
+
+ addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags | MAP_FIXED, -1, 0);
+
+ sprintf(fmt, "%" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);
SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
if (!strcmp(perms, tc->exp_perms)) {
@@ -61,7 +70,7 @@ static void run(unsigned int i)
tc->exp_perms, perms);
}
- SAFE_MUNMAP(addr, MMAPSIZE);
+ SAFE_MUNMAP(addr1, pagesize * 2);
}
static struct tst_test test = {