diff mbox series

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

Message ID 20230831112849.22126-1-akumar@suse.de
State Handled Elsewhere
Headers show
Series syscalls/mmap22: Add new test for validating mmap's EINVAL scenarios | expand

Commit Message

Avinesh Kumar Aug. 31, 2023, 11:28 a.m. UTC
Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
 testcases/kernel/syscalls/mmap/mmap22.c | 73 +++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap22.c

Comments

Cyril Hrubis Aug. 31, 2023, 12:45 p.m. UTC | #1
Hi!
If we add exp_errno to the struct tcase we can as well put these two
EINVAL testcasese into mmap06.c instead.
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/mmap/mmap22.c b/testcases/kernel/syscalls/mmap/mmap22.c
new file mode 100644
index 000000000..98e8b05e2
--- /dev/null
+++ b/testcases/kernel/syscalls/mmap/mmap22.c
@@ -0,0 +1,73 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
+ */
+
+/*\
+ * [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
+};