Skip to contentSkip to conversation

syscalls/mmap22: Add new test for validating mmap's EINVAL scenarios

PATCH In review Avinesh Kumar 2 participants 4 messages Aug 31, 2023 – Sep 8, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 syscalls/mmap22: Add new test for validating mmap's EINVAL scenarios

---
 testcases/kernel/syscalls/mmap/mmap22.c | 73 +++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap22.c
testcases/kernel/syscalls/mmap/mmap22.c new file +73 -0
new file mode 100644
index 000000000..98e8b05e2
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that, mmap() call fails with errno EINVAL when
+ *
+ * - length argument is 0.
+ * - flags contains none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE.
+ */
+
+#include <stdlib.h>
+#include "tst_test.h"
+
+#define TEMPFILE "mmapfile"
+#define MMAPSIZE 1024
+static size_t page_sz;
+static int fd;
+
+static struct tcase {
+	size_t length;
+	int prot;
+	int flags;
+} tcases[] = {
+	{0, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED},
+	{MMAPSIZE, PROT_READ | PROT_WRITE, MAP_FILE},
+};
+
+static void setup(void)
+{
+	char *buf;
+
+	page_sz = getpagesize();
+	buf = SAFE_MALLOC(page_sz);
+	memset(buf, 'A', page_sz);
+
+	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
+	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, page_sz);
+	free(buf);
+}
+
+static void run(unsigned int i)
+{
+	struct tcase *tc = &tcases[i];
+
+	TESTPTR(mmap(0, tc->length, tc->prot, tc->flags, fd, 0));
+
+	if (TST_RET_PTR != MAP_FAILED) {
+		tst_res(TFAIL | TERRNO, "mmap() was successful unexpectedly");
+		SAFE_MUNMAP(TST_RET_PTR, page_sz);
+	} else if (TST_ERR == EINVAL) {
+		tst_res(TPASS, "mmap() failed with errno EINVAL");
+	} else {
+		tst_res(TFAIL | TERRNO, "mmap() failed but unexpected errno");
+	}
+}
+
+static void cleanup(void)
+{
+	if (fd > 0)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases),
+	.needs_tmpdir = 1
+};
Signature · 5 lines
2.41.0


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

Hi!
If we add exp_errno to the struct tcase we can as well put these two EINVAL testcasese into mmap06.c instead.

Signature · 5 lines
Cyril Hrubis
[email protected]

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

PATCH 1/1 syscalls/mmap06: Add testcases for EINVAL scenarios

---
 testcases/kernel/syscalls/mmap/mmap06.c | 32 +++++++++++++++----------
 1 file changed, 20 insertions(+), 12 deletions(-)
testcases/kernel/syscalls/mmap/mmap06.c +20 -12
index 012e7de41..1a0c8e3cf 100644
@@ -8,27 +8,35 @@
 /*\
  * [Description]
  *
- * Verify that, mmap() call fails when a file mapping is requested but the
- * file descriptor is not open for reading, and errno is set to EACCES.
+ * Verify that, mmap() call fails with errno:
+ *
+ * - EACCES, when a file mapping is requested but the file descriptor is not open for reading.
+ * - EINVAL, when length argument is 0.
+ * - EINVAL, when flags contains none of MAP_PRIVATE, MAP_SHARED, or MAP_SHARED_VALIDATE.
  */
 
 #include <stdlib.h>
 #include "tst_test.h"
 
+#define MMAPSIZE 1024
 #define TEMPFILE "mmapfile"
 static size_t page_sz;
 static int fd;
 
 static struct tcase {
+	size_t length;
 	int prot;
 	int flags;
+	int exp_errno;
 } tcases[] = {
-	{PROT_WRITE, MAP_FILE | MAP_PRIVATE},
-	{PROT_WRITE, MAP_FILE | MAP_SHARED},
-	{PROT_READ, MAP_FILE | MAP_PRIVATE},
-	{PROT_READ, MAP_FILE | MAP_SHARED},
-	{PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE},
-	{PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED}
+	{MMAPSIZE, PROT_WRITE, MAP_FILE | MAP_PRIVATE, EACCES},
+	{MMAPSIZE, PROT_WRITE, MAP_FILE | MAP_SHARED, EACCES},
+	{MMAPSIZE, PROT_READ, MAP_FILE | MAP_PRIVATE, EACCES},
+	{MMAPSIZE, PROT_READ, MAP_FILE | MAP_SHARED, EACCES},
+	{MMAPSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, EACCES},
+	{MMAPSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, EACCES},
+	{0, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, EINVAL},
+	{MMAPSIZE, PROT_READ | PROT_WRITE, MAP_FILE, EINVAL}
 };
 
 static void setup(void)
@@ -48,13 +56,13 @@ static void run(unsigned int i)
 {
 	struct tcase *tc = &tcases[i];
 
-	TESTPTR(mmap(NULL, page_sz, tc->prot, tc->flags, fd, 0));
+	TESTPTR(mmap(NULL, tc->length, tc->prot, tc->flags, fd, 0));
 
 	if (TST_RET_PTR != MAP_FAILED) {
 		tst_res(TFAIL, "mmap() was successful unexpectedly");
-		SAFE_MUNMAP(TST_RET_PTR, page_sz);
-	} else if (TST_ERR == EACCES) {
-		tst_res(TPASS, "mmap() failed with errno EACCES");
+		SAFE_MUNMAP(TST_RET_PTR, MMAPSIZE);
+	} else if (TST_ERR == tc->exp_errno) {
+		tst_res(TPASS, "mmap() failed with expected errno");
 	} else {
 		tst_res(TFAIL | TERRNO, "mmap() failed but unexpected errno");
 	}
Signature · 5 lines
2.41.0


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

Re: [LTP] [PATCH] syscalls/mmap06: Add testcases for EINVAL scenarios

Hi!
Pushed with a minor change, thanks.

I did change the messages a bit so that we have sligtly nicer output with:

testcases/kernel/syscalls/mmap/mmap06.c +2 -2
index 1a0c8e3cf..615743fa7 100644
@@ -62,9 +62,9 @@ static void run(unsigned int i)
 		tst_res(TFAIL, "mmap() was successful unexpectedly");
 		SAFE_MUNMAP(TST_RET_PTR, MMAPSIZE);
 	} else if (TST_ERR == tc->exp_errno) {
-		tst_res(TPASS, "mmap() failed with expected errno");
+		tst_res(TPASS | TERRNO, "mmap() failed with");
 	} else {
-		tst_res(TFAIL | TERRNO, "mmap() failed but unexpected errno");
+		tst_res(TFAIL | TERRNO, "mmap() failed unexpectedly");
 	}
 }
 
Signature · 5 lines
Cyril Hrubis
[email protected]

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