static void test_mprotect(int fd, char *testname, unsigned long len1, int prot1, unsigned long len2, int prot2) { void *p; int err; verbose_printf("Testing %s\n", testname); verbose_printf("Mapping with prot=%x\n", prot1); p = mmap(NULL, len1, prot1, MAP_SHARED, fd, 0); if (p == MAP_FAILED) FAIL("%s: mmap(prot=%x): %s", testname, prot1, strerror(errno)); test_prot(p, prot1); verbose_printf("mprotect()ing to prot=%x\n", prot2); err = mprotect(p, len2, prot2); if (err != 0) FAIL("%s: mprotect(prot=%x): %s", testname, prot2, strerror(errno)); test_prot(p, prot2); if (len2 < len1) test_prot(p + len2, prot1); munmap(p, len1); }
void tests_prot(void) { int n = init_test_prot(); int i, j; /* Try each combination of protection. */ for (i = 0; i < n; i++) { for (j = 0; j < n; j++) test_prot(SIG1, i, j, SIGSETSIZE); } /* Try some random values. */ for (i = 0; i < 1000; i++) { test_prot(rand() % (SIGMAX + 2), rand() % n, rand() % n, SIGSETSIZE + (rand() % 10 == 0 ? 1 : 0)); } }