All of lore.kernel.org
 help / color / mirror / Atom feed
From: Avinesh Kumar <akumar@suse.de>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API
Date: Fri, 25 Aug 2023 12:08:40 +0530	[thread overview]
Message-ID: <20230825063932.30875-3-akumar@suse.de> (raw)
In-Reply-To: <20230825063932.30875-1-akumar@suse.de>

Signed-off-by: Avinesh Kumar <akumar@suse.de>
---
 testcases/kernel/syscalls/mmap/mmap13.c | 174 +++++++++---------------
 1 file changed, 65 insertions(+), 109 deletions(-)

diff --git a/testcases/kernel/syscalls/mmap/mmap13.c b/testcases/kernel/syscalls/mmap/mmap13.c
index c5a2058e9..5e18f24fe 100644
--- a/testcases/kernel/syscalls/mmap/mmap13.c
+++ b/testcases/kernel/syscalls/mmap/mmap13.c
@@ -1,142 +1,98 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) 2013 FNST, DAN LI <li.dan@cn.fujitsu.com>
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
+ */
+
+/*\
+ * [Description]
  *
- * This program is free software;  you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * Verify that, mmap() call succeeds to create a file mapping with length
+ * argument greater than the file size but any attempt to reference the
+ * memory region which does not correspond to the file causes SIGBUS signal.
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY;  without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
- * the GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program;  if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-/*
- * Test Description:
- *  Verify error signal SIGBUS.
- *  "Attempted access to a portion of the buffer that does not correspond
- *   to the file."
+ *                  mmap(0, 8192, prot, MAP_FILE | MAP_SHARED, fd, 0);
  *
- * Expected Result:
- *  mmap() should succeed returning the address of the mapped region,
- *  and an attempt to access the memory which does not correspond to the file
- *  should rise the signal SIGBUS.
+ *  byte offset:    0        2047 2048    4095 4096                8191 8192
+ *                  +------------+------------+------------------------+
+ *  Memory          |            |  remainder |                        |
+ *  region          |            | of page(0s)|                        |
+ *                  +------------+------------+------------------------+
+ *                  |<---------->|<---------->|<---------------------->|<--------------->
+ *                  | accessible,| accessible,        references            references
+ *                  |   mapped   | not mapped        yield SIGBUS          yield SIGSEGV
+ *                  |  to file   |  to file
+ *                  |            |
+ *                  +------------+
+ *  Mapped file     |            |
+ * (2048 bytes)     |            |
+ *                  +------------+
+ *  file offset:    0         2047
  */
-#include <stdio.h>
+
 #include <stdlib.h>
-#include <sys/types.h>
-#include <errno.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
 #include <setjmp.h>
-
-#include "test.h"
+#include "tst_test.h"
 
 #define TEMPFILE	"mmapfile"
-
-char *TCID = "mmap13";
-int TST_TOTAL = 1;
-
 static size_t page_sz;
 static char *addr;
-static int fildes;
+static int fd;
 static volatile sig_atomic_t pass;
 static sigjmp_buf env;
 
-static void setup(void);
-static void cleanup(void);
-static void sig_handler(int sig);
-
-int main(int argc, char *argv[])
+static void sig_handler(int sig)
 {
-	int lc;
-	char *ch;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-
-		addr = mmap(NULL, page_sz * 2, PROT_READ | PROT_WRITE,
-			    MAP_FILE | MAP_SHARED, fildes, 0);
-
-		if (addr == MAP_FAILED) {
-			tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
-				 TEMPFILE);
-			continue;
-		}
-
-		if (sigsetjmp(env, 1) == 0) {
-			ch = addr + page_sz + 1;
-			*ch = 0;
-		}
-
-		if (pass)
-			tst_resm(TPASS, "Got SIGBUS "
-					"as expected");
-		else
-			tst_resm(TFAIL, "Invalid access not "
-						"rise SIGBUS");
-
-		if (munmap(addr, page_sz * 2) != 0)
-			tst_brkm(TFAIL | TERRNO, cleanup,
-				 "failed to unmap the mmapped pages");
-
-		pass = 0;
-	}
-
-	cleanup();
-	tst_exit();
+	if (sig == SIGBUS) {
+		pass = 1;
+		siglongjmp(env, 1);
+	} else
+		tst_brk(TBROK, "received an unexpected signal");
 }
 
 static void setup(void)
 {
-	tst_sig(NOFORK, sig_handler, cleanup);
-
-	TEST_PAUSE;
+	SAFE_SIGNAL(SIGBUS, sig_handler);
 
 	page_sz = getpagesize();
 
-	tst_tmpdir();
-
-	fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0766);
-	if (fildes < 0)
-		tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
-			 TEMPFILE);
-
-	if (ftruncate(fildes, page_sz / 2) == -1)
-		tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate %s failed",
-			 TEMPFILE);
+	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT, 0666);
+	SAFE_FTRUNCATE(fd, page_sz / 2);
 }
 
-/*
- *   This function gets executed when the test process receives
- *   the signal SIGBUS while trying to access the memory which
- *   does not correspond to the file.
- */
-static void sig_handler(int sig)
+static void run(void)
 {
-	if (sig == SIGBUS) {
-		pass = 1;
-		siglongjmp(env, 1);
-	} else {
-		tst_brkm(TBROK, cleanup, "received an unexpected signal");
+	char *ch;
+
+	addr = mmap(0, page_sz * 2, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
+	if (addr == MAP_FAILED) {
+		tst_res(TFAIL | TERRNO, "mmap() of %s failed", TEMPFILE);
+		return;
 	}
+
+	if (sigsetjmp(env, 1) == 0) {
+		ch = addr + page_sz + 1;
+		*ch = 0;
+	}
+
+	if (pass)
+		tst_res(TPASS, "Received SIGBUS signal as expected");
+	else
+		tst_res(TFAIL, "SIGBUS signal not received");
+
+	SAFE_MUNMAP(addr, page_sz * 2);
 }
 
 static void cleanup(void)
 {
-	close(fildes);
-	tst_rmdir();
+	if (fd > 0)
+		SAFE_CLOSE(fd);
 }
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.needs_tmpdir = 1
+};
-- 
2.41.0


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

  parent reply	other threads:[~2023-08-25  6:40 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-25  6:38 [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the test using new LTP API Avinesh Kumar
2023-08-25  6:38 ` [LTP] [PATCH 2/5] syscalls/mmap09: " Avinesh Kumar
2023-09-01  8:27   ` Richard Palethorpe
2023-09-01  9:04     ` Cyril Hrubis
2023-09-01  9:11       ` Richard Palethorpe
2023-08-25  6:38 ` Avinesh Kumar [this message]
2023-09-04  8:54   ` [LTP] [PATCH 3/5] syscalls/mmap13: Rewrite the test using new API Richard Palethorpe
2023-08-25  6:38 ` [LTP] [PATCH 4/5] syscalls/mmap14: Rewrite test using new LTP API Avinesh Kumar
2023-09-01  9:23   ` Richard Palethorpe
2023-09-05 13:28   ` [LTP] [PATCH v2] " Avinesh Kumar
2023-10-09 10:59     ` Richard Palethorpe
2023-10-31 15:39       ` Cyril Hrubis
2023-08-25  6:38 ` [LTP] [PATCH 5/5] syscalls/mmap15: " Avinesh Kumar
2023-09-04  9:23   ` Richard Palethorpe
2023-09-05 16:01     ` [LTP] [PATCH v2] " Avinesh Kumar
2023-10-31 15:31       ` Cyril Hrubis
2023-12-11 20:49         ` [LTP] [PATCH v3] " Avinesh Kumar
2024-01-09 17:15           ` Petr Vorel
2024-01-10  9:41             ` Avinesh Kumar
2023-08-30 12:51 ` [LTP] [PATCH 1/5] syscalls/mmap08: Rewrite the " 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=20230825063932.30875-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.