Skip to contentSkip to conversation

mmap04.c: Avoid vma merging

PATCH Superseded Avinesh Kumar 3 participants 8 messages Jan 23, 2024 – Jan 25, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 testcases/kernel/syscalls/mmap/mmap04.c | 49 +++++++++++++++----------
 1 file changed, 30 insertions(+), 19 deletions(-)
testcases/kernel/syscalls/mmap/mmap04.c +30 -19
index f6f4f7c98..f0f87b7f5 100644
@@ -17,28 +17,28 @@
 #include "tst_test.h"
 #include <stdio.h>
 
-#define MMAPSIZE 1024
-static char *addr;
+static char *addr1;
+static char *addr2;
 
 static struct tcase {
 	int prot;
 	int flags;
 	char *exp_perms;
 } tcases[] = {
-	{PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, "---p"},
-	{PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, "---s"},
-	{PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, "r--p"},
-	{PROT_READ, MAP_ANONYMOUS | MAP_SHARED, "r--s"},
-	{PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, "-w-p"},
-	{PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, "-w-s"},
-	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, "rw-p"},
-	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, "rw-s"},
-	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "r-xp"},
-	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "r-xs"},
-	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "-wxp"},
-	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "-wxs"},
-	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "rwxp"},
-	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "rwxs"}
+	{PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "---p"},
+	{PROT_NONE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "---s"},
+	{PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "r--p"},
+	{PROT_READ, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "r--s"},
+	{PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "-w-p"},
+	{PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "-w-s"},
+	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "rw-p"},
+	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "rw-s"},
+	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "r-xp"},
+	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "r-xs"},
+	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "-wxp"},
+	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "-wxs"},
+	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "rwxp"},
+	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "rwxs"}
 };
 
 static void run(unsigned int i)
@@ -47,10 +47,21 @@ static void run(unsigned int i)
 	char addr_str[20];
 	char perms[8];
 	char fmt[1024];
+	unsigned int pagesize;
 
-	addr = SAFE_MMAP(NULL, MMAPSIZE, tc->prot, tc->flags, -1, 0);
+	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
 
-	sprintf(addr_str, "%" PRIxPTR, (uintptr_t)addr);
+	/* 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. */
+	if ((tc->prot == PROT_NONE) && (tc->flags == (MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED)))
+		addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_READ, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+	else
+		addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
+
+	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags, -1, 0);
+
+	sprintf(addr_str, "%" PRIxPTR, (uintptr_t)addr2);
 	sprintf(fmt, "%s-%%*x %%s", addr_str);
 	SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
 
@@ -61,7 +72,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 = {
Signature · 5 lines
2.43.0


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

Hi,
some comments below.

Quoted 48 lines · Avinesh Kumar

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.

---
  testcases/kernel/syscalls/mmap/mmap04.c | 49 +++++++++++++++----------
  1 file changed, 30 insertions(+), 19 deletions(-)
testcases/kernel/syscalls/mmap/mmap04.c +6 -16
index f6f4f7c98..f0f87b7f5 100644
@@ -17,28 +17,28 @@
  #include "tst_test.h"
  #include <stdio.h>
 
-#define MMAPSIZE 1024
-static char *addr;
+static char *addr1;
+static char *addr2;
 
  static struct tcase {
  	int prot;
  	int flags;
  	char *exp_perms;
  } tcases[] = {
-	{PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, "---p"},
-	{PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, "---s"},
-	{PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, "r--p"},
-	{PROT_READ, MAP_ANONYMOUS | MAP_SHARED, "r--s"},
-	{PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, "-w-p"},
-	{PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, "-w-s"},
-	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, "rw-p"},
-	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, "rw-s"},
-	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "r-xp"},
-	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "r-xs"},
-	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "-wxp"},
-	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "-wxs"},
-	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "rwxp"},
-	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "rwxs"}
+	{PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "---p"},
+	{PROT_NONE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "---s"},
+	{PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "r--p"},
+	{PROT_READ, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "r--s"},

testcases/kernel/syscalls/mmap/mmap04.c line 41

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap04.c +10 -0
+	{PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "-w-p"},
+	{PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "-w-s"},
+	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "rw-p"},
+	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "rw-s"},
+	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "r-xp"},
+	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "r-xs"},
+	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "-wxp"},
+	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "-wxs"},
+	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "rwxp"},
+	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "rwxs"}

The MAP_FIXED flag doesn't belong in the testcases, it should be added in the mmap() call instead: SAFE_MMAP(..., tc->flags | MAP_FIXED, ...);
It's an implementation detail not related to the testcases themselves. You don't want to rewrite all the test cases again if we decide to not use MAP_FIXED for whatever reason in the future.

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap04.c +1 -1
  };
 
  static void run(unsigned int i)
@@ -47,10 +47,21 @@ static void run(unsigned int i)
  	char addr_str[20];
  	char perms[8];
  	char fmt[1024];
+	unsigned int pagesize;
 
-	addr = SAFE_MMAP(NULL, MMAPSIZE, tc->prot, tc->flags, -1, 0);

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i) · line 60

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap04.c +8 -1
+	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
 
-	sprintf(addr_str, "%" PRIxPTR, (uintptr_t)addr);
+	/* 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. */
+	if ((tc->prot == PROT_NONE) && (tc->flags == (MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED)))
+		addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_READ, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
+	else
+		addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);

This would be cleaner (just invert the shared/private flag):
int flags = (tc->flags & MAP_PRIVATE) ? MAP_SHARED : MAP_PRIVATE; addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_NONE, MAP_ANONYMOUS | flags, -1, 0);

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i) · line 64

Quoted 4 lines
testcases/kernel/syscalls/mmap/mmap04.c +4 -0
+
+	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags, -1, 0);
+
+	sprintf(addr_str, "%" PRIxPTR, (uintptr_t)addr2);

Why not merge the two sprintf()s into one? sprintf(fmt, "%" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);

Quoted 12 lines
testcases/kernel/syscalls/mmap/mmap04.c +1 -1
  	sprintf(fmt, "%s-%%*x %%s", addr_str);
  	SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
 
@@ -61,7 +72,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 = {
Signature · 11 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


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

PATCH v2 1/1 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.

---
 testcases/kernel/syscalls/mmap/mmap04.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/mmap/mmap04.c +16 -7
index f6f4f7c98..fa85deed1 100644
@@ -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 = {
Signature · 5 lines
2.43.0


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

Hi,

Quoted 65 lines · Avinesh Kumar

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.

---
  testcases/kernel/syscalls/mmap/mmap04.c | 23 ++++++++++++++++-------
  1 file changed, 16 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/mmap/mmap04.c +16 -7
index f6f4f7c98..fa85deed1 100644
@@ -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 = {
Signature · 11 lines
Martin Doucha   [email protected]
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


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

Hi Avinesh, Martin,

Quoted 52 lines

Hi,

Quoted 48 lines · Avinesh Kumar

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.

---
  testcases/kernel/syscalls/mmap/mmap04.c | 23 ++++++++++++++++-------
  1 file changed, 16 insertions(+), 7 deletions(-)
testcases/kernel/syscalls/mmap/mmap04.c +14 -6
index f6f4f7c98..fa85deed1 100644
@@ -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);
+
Quoted 8 lines

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i) · line 72

Quoted 8 lines
testcases/kernel/syscalls/mmap/mmap04.c +2 -1
+	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);

Shouldn't there be also second munmap()?
SAFE_MUNMAP(addr2, pagesize);

Kind regards,
Petr

Quoted 2 lines
Quoted 2 lines
testcases/kernel/syscalls/mmap/mmap04.c +0 -0
  }
  static struct tst_test test = {
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Hi Petr,

Quoted 51 lines · CET Petr Vorel

Hi Avinesh, Martin,

Quoted 49 lines

Hi,

Quoted 45 lines · Avinesh Kumar

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.

---

  testcases/kernel/syscalls/mmap/mmap04.c | 23 ++++++++++++++++-------
  1 file changed, 16 insertions(+), 7 deletions(-)
(quoted diff) +0 -0

b/testcases/kernel/syscalls/mmap/mmap04.c index f6f4f7c98..fa85deed1 100644

testcases/kernel/syscalls/mmap/mmap04.c +2 -2
@@ -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;

testcases/kernel/syscalls/mmap/mmap04.c +0 -1
@@ -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];
 
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
testcases/kernel/syscalls/mmap/mmap04.c +3 -3
+	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);
Quoted 8 lines

testcases/kernel/syscalls/mmap/mmap04.c static struct tcase {

Quoted 8 lines
testcases/kernel/syscalls/mmap/mmap04.c +1 -0
+	/* To avoid new mapping getting merged with existing mappings, we

first

testcases/kernel/syscalls/mmap/mmap04.c +1 -0
+	 * create a 2-page mapping with the different permissions, and then

remap

testcases/kernel/syscalls/mmap/mmap04.c +4 -0
+	 * 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,

Quoted 13 lines
Quoted 11 lines

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i) · line 71

Quoted 11 lines

-1, 0); +

testcases/kernel/syscalls/mmap/mmap04.c +1 -0
+	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags |

MAP_FIXED, -1, 0); +

testcases/kernel/syscalls/mmap/mmap04.c +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);

Quoted 1 line
Quoted 1 line
Quoted 7 lines
Quoted 4 lines

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i) · line 73

Quoted 4 lines
  	}
testcases/kernel/syscalls/mmap/mmap04.c +1 -1
-	SAFE_MUNMAP(addr, MMAPSIZE);
+	SAFE_MUNMAP(addr1, pagesize * 2);

Shouldn't there be also second munmap()?
SAFE_MUNMAP(addr2, pagesize);

No, we are unmapping both the mappings ( 2 pages ) together.

Regards,
Avinesh

Quoted 6 lines

Kind regards,
Petr

Quoted 2 lines
Quoted 2 lines
  }
  static struct tst_test test = {
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Hi Avinesh, Martin,

Quoted 3 lines
Quoted 2 lines

Shouldn't there be also second munmap()?
SAFE_MUNMAP(addr2, pagesize);

No, we are unmapping both the mappings ( 2 pages ) together.

Ah, thanks!
Merged.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw
In reply to Martin Doucha

Hi Martin,

Quoted 58 lines · CET Martin Doucha

Hi,
some comments below.

Quoted 54 lines · Avinesh Kumar

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.

---

  testcases/kernel/syscalls/mmap/mmap04.c | 49 +++++++++++++++----------
  1 file changed, 30 insertions(+), 19 deletions(-)
(quoted diff) +0 -0

b/testcases/kernel/syscalls/mmap/mmap04.c index f6f4f7c98..f0f87b7f5 100644

testcases/kernel/syscalls/mmap/mmap04.c +2 -15
@@ -17,28 +17,28 @@
 
  #include "tst_test.h"
  #include <stdio.h>
 
-#define MMAPSIZE 1024
-static char *addr;
+static char *addr1;
+static char *addr2;
 
  static struct tcase {
 
  	int prot;
  	int flags;
  	char *exp_perms;
 
  } tcases[] = {
 
-	{PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE, "---p"},
-	{PROT_NONE, MAP_ANONYMOUS | MAP_SHARED, "---s"},
-	{PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, "r--p"},
-	{PROT_READ, MAP_ANONYMOUS | MAP_SHARED, "r--s"},
-	{PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, "-w-p"},
-	{PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, "-w-s"},
-	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, "rw-p"},
-	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, "rw-s"},
-	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "r-xp"},
-	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "r-xs"},
-	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, "-wxp"},
-	{PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED, "-wxs"},
-	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE,

"rwxp"}, - {PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS |
MAP_SHARED, "rwxs"} + {PROT_NONE, MAP_ANONYMOUS | MAP_PRIVATE |
MAP_FIXED, "---p"},

testcases/kernel/syscalls/mmap/mmap04.c +2 -0
+	{PROT_NONE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "---s"},
+	{PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "r--p"},
Quoted 8 lines
Quoted 8 lines
testcases/kernel/syscalls/mmap/mmap04.c +4 -0
+	{PROT_READ, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "r--s"},
+	{PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "-w-p"},
+	{PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "-w-s"},
+	{PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED,

"rw-p"}, + {PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED |
MAP_FIXED, "rw-s"}, + {PROT_READ | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE

| MAP_FIXED, "r-xp"}, +	{PROT_READ | PROT_EXEC, MAP_ANONYMOUS |
MAP_SHARED | MAP_FIXED, "r-xs"}, +	{PROT_WRITE | PROT_EXEC, 

MAP_ANONYMOUS

Quoted 11 lines
Quoted 5 lines
| MAP_PRIVATE | MAP_FIXED, "-wxp"}, +	{PROT_WRITE | PROT_EXEC,
MAP_ANONYMOUS | MAP_SHARED | MAP_FIXED, "-wxs"}, +	{PROT_READ |
PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, "rwxp"},
testcases/kernel/syscalls/mmap/mmap04.c +1 -0
+	{PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_SHARED |

MAP_FIXED, "rwxs"}

The MAP_FIXED flag doesn't belong in the testcases, it should be added in the mmap() call instead: SAFE_MMAP(..., tc->flags | MAP_FIXED, ...); It's an implementation detail not related to the testcases themselves. You don't want to rewrite all the test cases again if we decide to not use MAP_FIXED for whatever reason in the future.

Thank you for review and all the corrections/suggestions. I have send the updated patch.

Quoted 10 lines
Quoted 10 lines
  };

static void run(unsigned int i)

testcases/kernel/syscalls/mmap/mmap04.c +0 -0
@@ -47,10 +47,21 @@ static void run(unsigned int i)
 
  	char addr_str[20];
  	char perms[8];
  	char fmt[1024];
 
Quoted 7 lines

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i)

Quoted 7 lines
testcases/kernel/syscalls/mmap/mmap04.c +3 -2
+	unsigned int pagesize;
 
-	addr = SAFE_MMAP(NULL, MMAPSIZE, tc->prot, tc->flags, -1, 0);
+	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
 
-	sprintf(addr_str, "%" PRIxPTR, (uintptr_t)addr);
+	/* To avoid new mapping getting merged with existing mappings, we 

first

Quoted 5 lines
Quoted 5 lines
testcases/kernel/syscalls/mmap/mmap04.c +1 -0
+	   create a 2-page mapping with the different permissions, and then

remap

testcases/kernel/syscalls/mmap/mmap04.c +2 -0
+	   the 2nd page with the perms being tested. */
+	if ((tc->prot == PROT_NONE) && (tc->flags == (MAP_ANONYMOUS |

MAP_PRIVATE | MAP_FIXED))) + addr1 = SAFE_MMAP(NULL,

pagesize * 2,

Quoted 2 lines

testcases/kernel/syscalls/mmap/mmap04.c

Quoted 2 lines

PROT_READ, MAP_ANONYMOUS | MAP_SHARED, -1, 0); + else

testcases/kernel/syscalls/mmap/mmap04.c +1 -0
+		addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_NONE, 

MAP_ANONYMOUS |

Quoted 8 lines
Quoted 1 line

MAP_PRIVATE, -1, 0);

This would be cleaner (just invert the shared/private flag):
int flags = (tc->flags & MAP_PRIVATE) ? MAP_SHARED : MAP_PRIVATE; addr1 = SAFE_MMAP(NULL, pagesize * 2, PROT_NONE, MAP_ANONYMOUS | flags, -1, 0);

testcases/kernel/syscalls/mmap/mmap04.c

Quoted 2 lines
testcases/kernel/syscalls/mmap/mmap04.c +2 -0
+
+	addr2 = SAFE_MMAP(addr1 + pagesize, pagesize, tc->prot, tc->flags, 

-1,

Quoted 22 lines

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i) · line 64

Quoted 2 lines

0); +

testcases/kernel/syscalls/mmap/mmap04.c +1 -0
+	sprintf(addr_str, "%" PRIxPTR, (uintptr_t)addr2);

Why not merge the two sprintf()s into one? sprintf(fmt, "%" PRIxPTR "-%%*x %%s", (uintptr_t)addr2);

testcases/kernel/syscalls/mmap/mmap04.c static void run(unsigned int i) · line 73

Quoted 6 lines
  	sprintf(fmt, "%s-%%*x %%s", addr_str);
  	SAFE_FILE_LINES_SCANF("/proc/self/maps", fmt, perms);
testcases/kernel/syscalls/mmap/mmap04.c +0 -0
@@ -61,7 +72,7 @@ static void run(unsigned int i)
 
  						tc-

exp_perms, perms);

Quoted 9 lines
testcases/kernel/syscalls/mmap/mmap04.c +1 -1
  	}
 
-	SAFE_MUNMAP(addr, MMAPSIZE);
+	SAFE_MUNMAP(addr1, pagesize * 2);
 
  }
  
  static struct tst_test test = {
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp