From: Cyril H. <su...@li...> - 2013-06-04 16:37:59
|
The branch, master, has been updated via beb0b7d667c912611d7ba32ae6ca464d486b13b2 (commit) via b9f92622f9ac492e6bcf32f875712c8435d593d7 (commit) via 7ff5ebfc44300e915b0a4623d19837a5dc0bb662 (commit) via 7e40c4e9d1d83816259d8558488f60f28e69479d (commit) via 197c1c348366c69ad0090e0054507b7594283636 (commit) from 0546c995eb9e5e8ba76a9781dd54a940d22b6865 (commit) - Log ----------------------------------------------------------------- commit beb0b7d667c912611d7ba32ae6ca464d486b13b2 Author: Cyril Hrubis <ch...@su...> Date: Tue Jun 4 17:47:40 2013 +0200 syscalls/mprotect03: Make use of SAFE_WRITE(). Signed-off-by: Cyril Hrubis <ch...@su...> commit b9f92622f9ac492e6bcf32f875712c8435d593d7 Author: Cyril Hrubis <ch...@su...> Date: Tue Jun 4 17:20:04 2013 +0200 syscalls/mprotect01: Straighten up the logic. Pass the pointer to the current testcase to the setup function instead of passing the values via global variables + various small fixes. Signed-off-by: Cyril Hrubis <ch...@su...> commit 7ff5ebfc44300e915b0a4623d19837a5dc0bb662 Author: Cyril Hrubis <ch...@su...> Date: Tue Jun 4 16:32:39 2013 +0200 syscalls/mprotect: Fix mmap() error checks. The addr < 0 is always true. Make use of proper MAP_FAILED constant instead. Signed-off-by: Cyril Hrubis <ch...@su...> commit 7e40c4e9d1d83816259d8558488f60f28e69479d Author: Cyril Hrubis <ch...@su...> Date: Tue Jun 4 16:12:37 2013 +0200 syscalls/mprotect: Cleanup. Signed-off-by: Cyril Hrubis <ch...@su...> commit 197c1c348366c69ad0090e0054507b7594283636 Author: Cyril Hrubis <ch...@su...> Date: Tue Jun 4 14:41:46 2013 +0200 syscalls/mprotect: Cleanup UCLINUX ifdefs. Add mprotect directory to FILTER_OUT_DIRS on UCLINUX. Remove the UCLINUX ifdefs from the code. Signed-off-by: Cyril Hrubis <ch...@su...> ----------------------------------------------------------------------- Summary of changes: testcases/kernel/syscalls/Makefile | 2 +- testcases/kernel/syscalls/mprotect/mprotect01.c | 193 ++++++++--------------- testcases/kernel/syscalls/mprotect/mprotect02.c | 67 +++------ testcases/kernel/syscalls/mprotect/mprotect03.c | 103 ++++--------- 4 files changed, 114 insertions(+), 251 deletions(-) diff --git a/testcases/kernel/syscalls/Makefile b/testcases/kernel/syscalls/Makefile index da7c7fa..e70758d 100644 --- a/testcases/kernel/syscalls/Makefile +++ b/testcases/kernel/syscalls/Makefile @@ -26,7 +26,7 @@ include $(top_srcdir)/include/mk/env_pre.mk ifeq ($(UCLINUX),1) FILTER_OUT_DIRS += capget capset chmod chown clone fork getcontext llseek \ - nftw profil remap_file_pages mincore + nftw profil remap_file_pages mincore mprotect endif include $(top_srcdir)/include/mk/generic_trunk_target.mk diff --git a/testcases/kernel/syscalls/mprotect/mprotect01.c b/testcases/kernel/syscalls/mprotect/mprotect01.c index ccf4cab..411b335 100644 --- a/testcases/kernel/syscalls/mprotect/mprotect01.c +++ b/testcases/kernel/syscalls/mprotect/mprotect01.c @@ -1,26 +1,22 @@ /* + * Copyright (c) International Business Machines Corp., 2001 * - * Copyright (c) International Business Machines Corp., 2001 + * 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 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. * - * 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 + * 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 */ /* - * NAME - * mprotect01.c - * * DESCRIPTION * Testcase to check the error conditions for mprotect(2) * @@ -36,81 +32,55 @@ * Try to set write permission (PROT_WRITE) using mprotect(2). * Check that error is set to EACCES. * - * USAGE: <for command-line> - * mprotect01 [-c n] [-e] [-i n] [-I x] [-P x] [-t] - * where, -c n : Run n copies concurrently. - * -e : Turn on errno logging. - * -i n : Execute test n times. - * -I x : Execute test for x seconds. - * -P x : Pause for x seconds between iterations. - * -t : Turn on syscall timing. - * * HISTORY * 07/2001 Ported by Wayne Boyer * 03/2002 Paul Larson: case 1 should expect ENOMEM not EFAULT - * - * RESTRICTIONS - * None */ #include <fcntl.h> #include <errno.h> #include <sys/mman.h> #include <stdlib.h> -#include <limits.h> /* for PAGESIZE */ #include <unistd.h> #include "test.h" #include "usctest.h" -#ifndef PAGESIZE -#define PAGESIZE 4096 -#endif - -void cleanup(void); -void setup(void); -void setup1(void); -void setup2(void); -void setup3(void); - char *TCID = "mprotect01"; int TST_TOTAL = 3; -void *addr1, *addr2, *addr3; -int fd; - -int exp_enos[] = { ENOMEM, EINVAL, EACCES, 0 }; - -struct test_case_t { - void **addr; +struct test_case { + void *addr; int len; int prot; int error; - void (*setupfunc) (); -} TC[] = { -#ifdef __ia64__ - /* Check for ENOMEM passing memory that cannot be accessed. */ - { - &addr1, 1024, PROT_READ, ENOMEM, setup1}, -#else - /* Check for ENOMEM passing memory that cannot be accessed. */ - { - &addr1, 1024, PROT_READ, ENOMEM, NULL}, -#endif - /* - * Check for EINVAL by passing a pointer which is not a - * multiple of PAGESIZE. - */ - { - &addr2, 1024, PROT_READ, EINVAL, setup2}, - /* - * Check for EACCES by trying to mark a section of memory - * which has been mmap'ed as read-only, as PROT_WRITE - */ - { - &addr3, PAGESIZE, PROT_WRITE, EACCES, setup3} + void (*setupfunc) (struct test_case *self); }; -#ifndef UCLINUX +static void cleanup(void); +static void setup(void); +static void setup1(struct test_case *self); +static void setup2(struct test_case *self); +static void setup3(struct test_case *self); + +static int exp_enos[] = { ENOMEM, EINVAL, EACCES, 0 }; + +static int fd; + +struct test_case TC[] = { + /* Check for ENOMEM passing memory that cannot be accessed. */ + {NULL, 0, PROT_READ, ENOMEM, setup1}, + + /* + * Check for EINVAL by passing a pointer which is not a + * multiple of PAGESIZE. + */ + {NULL, 1024, PROT_READ, EINVAL, setup2}, + /* + * Check for EACCES by trying to mark a section of memory + * which has been mmap'ed as read-only, as PROT_WRITE + */ + {NULL, 0, PROT_WRITE, EACCES, setup3} +}; int main(int ac, char **av) { @@ -118,30 +88,23 @@ int main(int ac, char **av) int i; char *msg; - if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { + if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); - } - setup(); /* global setup */ + setup(); /* set up the expected errnos */ TEST_EXP_ENOS(exp_enos); - /* The following loop checks looping state if -i option given */ for (lc = 0; TEST_LOOPING(lc); lc++) { - - /* reset tst_count in case we are looping */ tst_count = 0; - /* loop through the test cases */ for (i = 0; i < TST_TOTAL; i++) { - /* perform test specific setup */ - if (TC[i].setupfunc != NULL) { - TC[i].setupfunc(); - } + if (TC[i].setupfunc != NULL) + TC[i].setupfunc(&TC[i]); - TEST(mprotect(*(TC[i].addr), TC[i].len, TC[i].prot)); + TEST(mprotect(TC[i].addr, TC[i].len, TC[i].prot)); if (TEST_RETURN != -1) { tst_resm(TFAIL, "call succeeded unexpectedly"); @@ -160,83 +123,53 @@ int main(int ac, char **av) strerror(TEST_ERRNO), TC[i].error); } } - close(fd); } cleanup(); tst_exit(); } -#else - -int main() -{ - tst_resm(TINFO, "Ignore this test on uClinux"); - tst_exit(); -} - -#endif /* UCLINUX */ - -/* - * setup1() - sets up conditions for the first test - */ -void setup1() +static void setup1(struct test_case *self) { - TC[0].len = getpagesize() + 1; + self->len = getpagesize() + 1; } -/* - * setup2() - sets up conditions for the second test - */ -void setup2() +static void setup2(struct test_case *self) { + self->addr = malloc(getpagesize()); - addr2 = (char *)malloc(PAGESIZE); - if (addr2 == NULL) { + if (self->addr == NULL) tst_brkm(TINFO, cleanup, "malloc failed"); - } - addr2++; /* Ensure addr2 is not page aligned */ + + /* Ensure addr2 is not page aligned */ + self->addr++; } -/* - * setup3() - sets up conditions for the third test - */ -void setup3() +static void setup3(struct test_case *self) { fd = open("/etc/passwd", O_RDONLY); - if (fd < 0) { + if (fd < 0) tst_brkm(TBROK, cleanup, "open failed"); - } + + self->len = getpagesize(); /* * mmap the PAGESIZE bytes as read only. */ - addr3 = mmap(0, PAGESIZE, PROT_READ, MAP_SHARED, fd, 0); - if (addr3 < 0) { + self->addr = mmap(0, self->len, PROT_READ, MAP_SHARED, fd, 0); + if (self->addr == MAP_FAILED) tst_brkm(TBROK, cleanup, "mmap failed"); - } + } -/* - * setup() - performs all ONE TIME setup for this test - */ -void setup() +static void setup(void) { - tst_sig(FORK, DEF_HANDLER, cleanup); TEST_PAUSE; } -/* - * cleanup() - performs all the ONE TIME cleanup for this test at completion - * or premature exit. - */ -void cleanup() +static void cleanup(void) { - /* - * print timing status if that option was specified. - * print errno log if that option was specified - */ + close(fd); TEST_CLEANUP; - } diff --git a/testcases/kernel/syscalls/mprotect/mprotect02.c b/testcases/kernel/syscalls/mprotect/mprotect02.c index 0a9492f..afaa5b8 100644 --- a/testcases/kernel/syscalls/mprotect/mprotect02.c +++ b/testcases/kernel/syscalls/mprotect/mprotect02.c @@ -1,26 +1,22 @@ /* + * Copyright (c) International Business Machines Corp., 2001 * - * Copyright (c) International Business Machines Corp., 2001 + * 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 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. * - * 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 + * 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 */ /* - * NAME - * mprotect02.c - * * DESCRIPTION * Testcase to check the mprotect(2) system call. * @@ -32,21 +28,9 @@ * Again try to write into the mapped region. * Verify that no SIGSEGV is generated. * - * USAGE: <for command-line> - * mprotect02 [-c n] [-f] [-i n] [-I x] [-P x] [-t] - * where, -c n : Run n copies concurrently. - * -f : Turn off functionality Testing. - * -i n : Execute test n times. - * -I x : Execute test for x seconds. - * -P x : Pause for x seconds between iterations. - * -t : Turn on syscall timing. - * * HISTORY * 07/2001 Ported by Wayne Boyer * 05/2002 changed over to use tst_sig instead of sigaction - * - * RESTRICTIONS - * None */ #include <sys/mman.h> @@ -66,13 +50,11 @@ static void setup(void); char *TCID = "mprotect02"; int TST_TOTAL = 1; -int fd, status; -char file1[BUFSIZ]; - -char *addr = MAP_FAILED; -char buf[] = "abcdefghijklmnopqrstuvwxyz"; +static int fd, status; +static char file1[BUFSIZ]; -#ifndef UCLINUX +static char *addr = MAP_FAILED; +static char buf[] = "abcdefghijklmnopqrstuvwxyz"; int main(int ac, char **av) { @@ -115,7 +97,7 @@ int main(int ac, char **av) tst_brkm(TBROK | TERRNO, cleanup, "fork #1 failed"); if (pid == 0) { - (void)memcpy(addr, buf, strlen(buf)); + memcpy(addr, buf, strlen(buf)); exit(255); } @@ -149,7 +131,7 @@ int main(int ac, char **av) "fork #2 failed"); if (pid == 0) { - (void)memcpy(addr, buf, strlen(buf)); + memcpy(addr, buf, strlen(buf)); exit(0); } @@ -185,21 +167,12 @@ int main(int ac, char **av) tst_exit(); } -#else - -int main() -{ - tst_brkm(TCONF, NULL, "test not runnable on uClinux"); -} - -#endif /* UCLINUX */ - static void sighandler(int sig) { _exit((sig == SIGSEGV) ? 0 : sig); } -static void setup() +static void setup(void) { tst_sig(FORK, sighandler, cleanup); @@ -210,7 +183,7 @@ static void setup() sprintf(file1, "mprotect02.tmp.%d", getpid()); } -static void cleanup() +static void cleanup(void) { TEST_CLEANUP; diff --git a/testcases/kernel/syscalls/mprotect/mprotect03.c b/testcases/kernel/syscalls/mprotect/mprotect03.c index 0174418..c5898d5 100644 --- a/testcases/kernel/syscalls/mprotect/mprotect03.c +++ b/testcases/kernel/syscalls/mprotect/mprotect03.c @@ -1,20 +1,19 @@ /* + * Copyright (c) International Business Machines Corp., 2001 * - * Copyright (c) International Business Machines Corp., 2001 + * 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 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. * - * 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 + * 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 */ /* @@ -34,47 +33,35 @@ * succeeds only when this sigsegv is generated while attempting to * memcpy() on a shared region with only read permission. * - * USAGE: <for command-line> - * mprotect03 [-c n] [-f] [-i n] [-I x] [-P x] [-t] - * where, -c n : Run n copies concurrently. - * -f : Turn off functionality Testing. - * -i n : Execute test n times. - * -I x : Execute test for x seconds. - * -P x : Pause for x seconds between iterations. - * -t : Turn on syscall timing. - * * HISTORY * 07/2001 Ported by Wayne Boyer * 05/2002 changed over to use tst_sig instead of sigaction - * - * RESTRICTIONS - * None */ #include <errno.h> #include <fcntl.h> #include <sys/mman.h> -#include <limits.h> /* for PAGESIZE */ +#include <limits.h> #include <signal.h> #include <wait.h> #include "test.h" #include "usctest.h" +#include "safe_macros.h" + #ifndef PAGESIZE #define PAGESIZE 4096 #endif #define FAILED 1 -void cleanup(void); -void setup(void); +static void cleanup(void); +static void setup(void); char *TCID = "mprotect03"; int TST_TOTAL = 1; int status; char file1[BUFSIZ]; -#ifndef UCLINUX - int main(int ac, char **av) { int lc; @@ -84,32 +71,26 @@ int main(int ac, char **av) int fd, pid; char *buf = "abcdefghijklmnopqrstuvwxyz"; - if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { + if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); - } - setup(); /* global setup */ + setup(); - /* The following loop checks looping state if -i option given */ for (lc = 0; TEST_LOOPING(lc); lc++) { - - /* reset tst_count in case we are looping */ tst_count = 0; - if ((fd = open(file1, O_RDWR | O_CREAT, 0777)) < 0) { //mode must be specified when O_CREAT is in the flag + if ((fd = open(file1, O_RDWR | O_CREAT, 0777)) < 0) tst_brkm(TBROK, cleanup, "open failed"); - } - (void)write(fd, buf, strlen(buf)); + SAFE_WRITE(cleanup, 1, fd, buf, strlen(buf)); /* * mmap the PAGESIZE bytes as read only. */ addr = mmap(0, strlen(buf), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (addr < 0) { + if (addr == MAP_FAILED) tst_brkm(TBROK, cleanup, "mmap failed"); - } /* * Try to change the protection to WRITE. @@ -122,16 +103,14 @@ int main(int ac, char **av) tst_brkm(TBROK, cleanup, "fork failed"); } - if (pid == 0) { /* child */ - (void)memcpy((void *)addr, (void *)buf, - strlen(buf)); + if (pid == 0) { + memcpy(addr, buf, strlen(buf)); tst_resm(TINFO, "memcpy() did " "not generate SIGSEGV"); exit(1); } - /* parent */ - (void)waitpid(pid, &status, 0); + waitpid(pid, &status, 0); if (WEXITSTATUS(status) != 0) { tst_resm(TFAIL, "child returned " "unexpected status"); @@ -158,22 +137,12 @@ int main(int ac, char **av) tst_brkm(TBROK, cleanup, "unlink failed"); } } + cleanup(); tst_exit(); - } -#else - -int main() -{ - tst_resm(TINFO, "Ignore this test on uClinux"); - tst_exit(); -} - -#endif /* UCLINUX */ - -void sighandler(int sig) +static void sighandler(int sig) { if (sig == SIGSEGV) { tst_resm(TINFO, "received signal: SIGSEGV"); @@ -182,32 +151,20 @@ void sighandler(int sig) tst_brkm(TBROK, 0, "Unexpected signal %d received.", sig); } -/* - * setup() - performs all ONE TIME setup for this test - */ -void setup() +static void setup(void) { tst_sig(FORK, sighandler, NULL); TEST_PAUSE; - tst_tmpdir(); /* create a temporary directory, cd to it */ + tst_tmpdir(); sprintf(file1, "mprotect03.tmp.%d", getpid()); } -/* - * cleanup() - performs all the ONE TIME cleanup for this test at completion - * or premature exit. - */ -void cleanup() +static void cleanup(void) { - /* - * print timing status if that option was specified. - * print errno log if that option was specified - */ TEST_CLEANUP; tst_rmdir(); - } hooks/post-receive -- ltp |