Skip to contentSkip to conversation

mmap/mmap13.c: new case to test SIGBUS error of mmap

PATCH In review DAN LI 3 participants 3 messages Jun 20, 2013

lore
Share & tools
lore

DAN LI

author
#0 lore raw

PATCH 1/1 mmap/mmap13.c: new case to test SIGBUS error of mmap

Test SIGBUS error by accessing to a portion of the buffer that does not correspond to the file.

---
 runtest/syscalls                        |   1 +
 testcases/kernel/syscalls/mmap/mmap13.c | 155 ++++++++++++++++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap13.c
runtest/syscalls +1 -0
index e6ce29c..eba0200 100644
@@ -561,6 +561,7 @@ mmap06 mmap06
 mmap07 mmap07
 mmap08 mmap08
 mmap09 mmap09
+mmap13 mmap13
 # test is broken, mask it for now.
 #mmap11 mmap11 -i 30000
 
testcases/kernel/syscalls/mmap/mmap13.c new file +155 -0

155 lines not on this page · raw message

Signature · 12 lines
1.8.1

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#1 lore raw

Hi!

Quoted 71 lines

Test SIGBUS error by accessing to a portion of the buffer that does not correspond to the file.

---
 runtest/syscalls                        |   1 +
 testcases/kernel/syscalls/mmap/mmap13.c | 155 ++++++++++++++++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap13.c
runtest/syscalls +1 -0
index e6ce29c..eba0200 100644
@@ -561,6 +561,7 @@ mmap06 mmap06
 mmap07 mmap07
 mmap08 mmap08
 mmap09 mmap09
+mmap13 mmap13
 # test is broken, mask it for now.
 #mmap11 mmap11 -i 30000
 
testcases/kernel/syscalls/mmap/mmap13.c new file +43 -0
new file mode 100644
index 0000000..6aaa8ad
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) 2013 FNST, DAN LI <[email protected]>
+ *
+ * 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.
+ *
+ * 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."
+ *
+ * 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.
+ */
+#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 "usctest.h"

testcases/kernel/syscalls/mmap/mmap13.c line 53

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap13.c new file +10 -0
+
+#define TEMPFILE	"mmapfile"
+
+char *TCID = "mmap13";
+int TST_TOTAL = 1;
+
+static size_t page_sz;
+static char *addr;
+static int fildes;
+static int pass;

This should really be volatile or even better 'static volatile sig_atomic_t pass' because otherwise the compiler may optimize out the if (pass) in the main loop.

Quoted 80 lines
testcases/kernel/syscalls/mmap/mmap13.c new file +80 -0
+static sigjmp_buf env;
+
+static void setup(void);
+static void cleanup(void);
+static void sig_handler(int sig);
+
+int main(int argc, char *argv[])
+{
+	int lc;
+	char *ch;
+	char *msg;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		tst_count = 0;
+
+		addr = mmap(0, 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 (STD_FUNCTIONAL_TEST) {
+
+			if (sigsetjmp(env, 1) == 0) {
+				ch = addr + page_sz + 1;
+				*ch = 0;
+
+				if (pass)
+					tst_resm(TPASS, "Got SIGSEGV"
+							"as expected");
+				else
+					tst_resm(TFAIL, "Invalid access not"
+							"rise SIGSEGV");
+			}
+
+		} else {
+			tst_resm(TPASS, "call succeeded");
+		}
+
+		if (munmap(addr, page_sz * 2) != 0)
+			tst_brkm(TFAIL | TERRNO, cleanup,
+				 "failed to unmap the mmapped pages");
+
+		pass = 0;
+	}
+
+	cleanup();
+	tst_exit();
+
+}
+
+static void setup(void)
+{
+	tst_sig(NOFORK, sig_handler, cleanup);
+
+	TEST_PAUSE;
+
+	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);
+}
+

testcases/kernel/syscalls/mmap/mmap13.c line 143

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap13.c new file +10 -0
+/*
+ *   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)
+{
+	if (sig == SIGBUS) {
+		pass = 1;
+		tst_resm(TPASS, "Got SIGBUS as expected");

Calling the tst_resm() interface may not be save from signal context. We should rather resort only to setting the variable here.

Quoted 12 lines
testcases/kernel/syscalls/mmap/mmap13.c new file +12 -0
+		siglongjmp(env, 1);
+	} else {
+		tst_brkm(TBROK, cleanup, "received an unexpected signal");
+	}
+}
+
+static void cleanup(void)
+{
+	close(fildes);
+	TEST_CLEANUP;
+	tst_rmdir();
+}
Signature · 13 lines
Cyril Hrubis
[email protected]

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list
#2 lore raw
In reply to DAN LI

----- Original Message -----

Quoted 124 lines

From: "DAN LI" <[email protected]>
To: "LTP list" <[email protected]>
Sent: Thursday, 20 June, 2013 12:15:31 PM
Subject: [LTP] [PATCH] mmap/mmap13.c: new case to test SIGBUS error of mmap

Test SIGBUS error by accessing to a portion of the buffer that does not correspond to the file.

---
 runtest/syscalls                        |   1 +
 testcases/kernel/syscalls/mmap/mmap13.c | 155
 ++++++++++++++++++++++++++++++++
 2 files changed, 156 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mmap/mmap13.c
runtest/syscalls +1 -0
index e6ce29c..eba0200 100644
@@ -561,6 +561,7 @@ mmap06 mmap06
 mmap07 mmap07
 mmap08 mmap08
 mmap09 mmap09
+mmap13 mmap13
 # test is broken, mask it for now.
 #mmap11 mmap11 -i 30000
 
testcases/kernel/syscalls/mmap/mmap13.c new file +16 -0
new file mode 100644
index 0000000..6aaa8ad
@@ -0,0 +1,155 @@
+/*
+ * Copyright (c) 2013 FNST, DAN LI <[email protected]>
+ *
+ * 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.
+ *
+ * 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
+ */

testcases/kernel/syscalls/mmap/mmap13.c new file +10 -0
+
+/*
+ * Test Description:
+ *  Verify error signal SIGBUS.
+ *  "Attempted access to a portion of the buffer that does not correspond
+ *   to the file."
+ *
+ * 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.
+ */

testcases/kernel/syscalls/mmap/mmap13.c new file +57 -0
+#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 "usctest.h"
+
+#define TEMPFILE	"mmapfile"
+
+char *TCID = "mmap13";
+int TST_TOTAL = 1;
+
+static size_t page_sz;
+static char *addr;
+static int fildes;
+static int 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[])
+{
+	int lc;
+	char *ch;
+	char *msg;
+
+	msg = parse_opts(argc, argv, NULL, NULL);
+	if (msg != NULL)
+		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); lc++) {
+		tst_count = 0;
+
+		addr = mmap(0, 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 (STD_FUNCTIONAL_TEST) {
+
+			if (sigsetjmp(env, 1) == 0) {

testcases/kernel/syscalls/mmap/mmap13.c line 96

Quoted 10 lines
testcases/kernel/syscalls/mmap/mmap13.c new file +10 -0
+				ch = addr + page_sz + 1;
+				*ch = 0;
+
+				if (pass)
+					tst_resm(TPASS, "Got SIGSEGV"
+							"as expected");
+				else
+					tst_resm(TFAIL, "Invalid access not"
+							"rise SIGSEGV");
+			}
                        ^^

Shouldn't this block end 7 lines above?
I assume you want to check that condition only after return from siglongjmp. Then as Cyril pointed out you don't need same message in sig_handler.

Quoted 65 lines
testcases/kernel/syscalls/mmap/mmap13.c new file +59 -1
+
+		} else {
+			tst_resm(TPASS, "call succeeded");
+		}
+
+		if (munmap(addr, page_sz * 2) != 0)
+			tst_brkm(TFAIL | TERRNO, cleanup,
+				 "failed to unmap the mmapped pages");
+
+		pass = 0;
+	}
+
+	cleanup();
+	tst_exit();
+
+}
+
+static void setup(void)
+{
+	tst_sig(NOFORK, sig_handler, cleanup);
+
+	TEST_PAUSE;
+
+	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);
+}
+
+/*
+ *   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)
+{
+	if (sig == SIGBUS) {
+		pass = 1;
+		tst_resm(TPASS, "Got SIGBUS as expected");
+		siglongjmp(env, 1);
+	} else {
+		tst_brkm(TBROK, cleanup, "received an unexpected signal");
+	}
+}
+
+static void cleanup(void)
+{
+	close(fildes);
+	TEST_CLEANUP;
+	tst_rmdir();
+}
--

1.8.1

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Quoted 8 lines

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ltp-list mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ltp-list