[LTP] [PATCH] syscalls/mmap22: Add new test for validating mmap's EINVAL scenarios

Avinesh Kumar akumar@suse.de
Thu Aug 31 13:28:48 CEST 2023


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

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
+};
-- 
2.41.0



More information about the ltp mailing list