From: Avinesh Kumar <akumar@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH] syscalls/mmap22: Add new test for validating mmap's EINVAL scenarios
Date: Thu, 31 Aug 2023 16:58:48 +0530 [thread overview]
Message-ID: <20230831112849.22126-1-akumar@suse.de> (raw)
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
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next reply other threads:[~2023-08-31 11:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-31 11:28 Avinesh Kumar [this message]
2023-08-31 12:45 ` [LTP] [PATCH] syscalls/mmap22: Add new test for validating mmap's EINVAL scenarios Cyril Hrubis
2023-09-01 5:57 ` [LTP] [PATCH] syscalls/mmap06: Add testcases for " Avinesh Kumar
2023-09-08 14:35 ` Cyril Hrubis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230831112849.22126-1-akumar@suse.de \
--to=akumar@suse.de \
--cc=ltp@lists.linux.it \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox