| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <unistd.h> |
| 27 |
#include <sys/mman.h> |
| 28 |
#include <setjmp.h> |
| 29 |
#include <signal.h> |
| 30 |
|
| 31 |
#include <hugetlbfs.h> |
| 32 |
|
| 33 |
#include "hugetests.h" |
| 34 |
|
| 35 |
static sigjmp_buf sig_escape; |
| 36 |
static void *sig_expected = MAP_FAILED; |
| 37 |
static long hpage_size; |
| 38 |
|
| 39 |
static void sig_handler(int signum, siginfo_t *si, void *uc) |
| 40 |
{ |
| 41 |
if (signum == SIGSEGV) { |
| 42 |
verbose_printf("SIGSEGV at %p (sig_expected=%p)\n", si->si_addr, |
| 43 |
sig_expected); |
| 44 |
if (si->si_addr == sig_expected) { |
| 45 |
siglongjmp(sig_escape, 1); |
| 46 |
} |
| 47 |
FAIL("SIGSEGV somewhere unexpected"); |
| 48 |
} |
| 49 |
FAIL("Unexpected signal %s", strsignal(signum)); |
| 50 |
} |
| 51 |
|
| 52 |
static int test_read(void *p) |
| 53 |
{ |
| 54 |
volatile unsigned long *pl = p; |
| 55 |
unsigned long x; |
| 56 |
|
| 57 |
if (sigsetjmp(sig_escape, 1)) { |
| 58 |
|
| 59 |
sig_expected = MAP_FAILED; |
| 60 |
return -1; |
| 61 |
} |
| 62 |
|
| 63 |
sig_expected = p; |
| 64 |
barrier(); |
| 65 |
x = *pl; |
| 66 |
verbose_printf("Read back %lu\n", x); |
| 67 |
barrier(); |
| 68 |
sig_expected = MAP_FAILED; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
pl = (unsigned long *)x; |
| 74 |
|
| 75 |
return 0; |
| 76 |
} |
| 77 |
|
| 78 |
static int test_write(void *p, unsigned long val) |
| 79 |
{ |
| 80 |
volatile unsigned long *pl = p; |
| 81 |
unsigned long x; |
| 82 |
|
| 83 |
if (sigsetjmp(sig_escape, 1)) { |
| 84 |
|
| 85 |
sig_expected = MAP_FAILED; |
| 86 |
return -1; |
| 87 |
} |
| 88 |
|
| 89 |
sig_expected = p; |
| 90 |
barrier(); |
| 91 |
*pl = val; |
| 92 |
x = *pl; |
| 93 |
barrier(); |
| 94 |
sig_expected = MAP_FAILED; |
| 95 |
|
| 96 |
return (x != val); |
| 97 |
} |
| 98 |
|
| 99 |
#define RANDOM_CONSTANT 0x1234ABCD |
| 100 |
|
| 101 |
static void test_prot(void *p, int prot) |
| 102 |
{ |
| 103 |
int r, w; |
| 104 |
|
| 105 |
verbose_printf("Reading.."); |
| 106 |
r = test_read(p); |
| 107 |
verbose_printf("%d\n", r); |
| 108 |
verbose_printf("Writing.."); |
| 109 |
w = test_write(p, RANDOM_CONSTANT); |
| 110 |
verbose_printf("%d\n", w); |
| 111 |
|
| 112 |
if (prot & PROT_READ) { |
| 113 |
if (r != 0) |
| 114 |
FAIL("read failed on mmap(prot=%x)", prot); |
| 115 |
} else { |
| 116 |
if (r != -1) |
| 117 |
FAIL("read succeeded on mmap(prot=%x)", prot); |
| 118 |
} |
| 119 |
|
| 120 |
if (prot & PROT_WRITE) { |
| 121 |
switch (w) { |
| 122 |
case -1: |
| 123 |
FAIL("write failed on mmap(prot=%x)", prot); |
| 124 |
break; |
| 125 |
case 0: |
| 126 |
break; |
| 127 |
case 1: |
| 128 |
FAIL("write mismatch on mmap(prot=%x)", prot); |
| 129 |
break; |
| 130 |
default: |
| 131 |
TEST_BUG(); |
| 132 |
} |
| 133 |
} else { |
| 134 |
switch (w) { |
| 135 |
case -1: |
| 136 |
break; |
| 137 |
case 0: |
| 138 |
FAIL("write succeeded on mmap(prot=%x)", prot); |
| 139 |
break; |
| 140 |
case 1: |
| 141 |
FAIL("write mismatch on mmap(prot=%x)", prot); |
| 142 |
break; |
| 143 |
default: |
| 144 |
TEST_BUG(); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
static void test_mprotect(int fd, char *testname, |
| 150 |
unsigned long len1, int prot1, |
| 151 |
unsigned long len2, int prot2) |
| 152 |
{ |
| 153 |
void *p; |
| 154 |
int err; |
| 155 |
|
| 156 |
verbose_printf("Testing %s\n", testname); |
| 157 |
verbose_printf("Mapping with prot=%x\n", prot1); |
| 158 |
p = mmap(NULL, len1, prot1, MAP_SHARED, fd, 0); |
| 159 |
if (p == MAP_FAILED) |
| 160 |
FAIL("%s: mmap(prot=%x): %s", testname, prot1, |
| 161 |
strerror(errno)); |
| 162 |
|
| 163 |
test_prot(p, prot1); |
| 164 |
|
| 165 |
verbose_printf("mprotect()ing to prot=%x\n", prot2); |
| 166 |
err = mprotect(p, len2, prot2); |
| 167 |
if (err != 0) |
| 168 |
FAIL("%s: mprotect(prot=%x): %s", testname, prot2, |
| 169 |
strerror(errno)); |
| 170 |
|
| 171 |
test_prot(p, prot2); |
| 172 |
|
| 173 |
if (len2 < len1) |
| 174 |
test_prot(p + len2, prot1); |
| 175 |
|
| 176 |
munmap(p, len1); |
| 177 |
} |
| 178 |
|
| 179 |
int main(int argc, char *argv[]) |
| 180 |
{ |
| 181 |
int err; |
| 182 |
int fd; |
| 183 |
void *p; |
| 184 |
|
| 185 |
test_init(argc, argv); |
| 186 |
|
| 187 |
struct sigaction sa = { |
| 188 |
.sa_sigaction = sig_handler, |
| 189 |
.sa_flags = SA_SIGINFO, |
| 190 |
}; |
| 191 |
|
| 192 |
err = sigaction(SIGSEGV, &sa, NULL); |
| 193 |
if (err) |
| 194 |
FAIL("Can't install SIGSEGV handler: %s", strerror(errno)); |
| 195 |
|
| 196 |
hpage_size = check_hugepagesize(); |
| 197 |
|
| 198 |
fd = hugetlbfs_unlinked_fd(); |
| 199 |
if (fd < 0) |
| 200 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 201 |
|
| 202 |
verbose_printf("instantiating page\n"); |
| 203 |
|
| 204 |
p = mmap(NULL, 2*hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 205 |
if (p == MAP_FAILED) |
| 206 |
FAIL("mmap(): %s", strerror(errno)); |
| 207 |
memset(p, 0, hpage_size); |
| 208 |
munmap(p, hpage_size); |
| 209 |
|
| 210 |
|
| 211 |
test_mprotect(fd, "R->RW", hpage_size, PROT_READ, |
| 212 |
hpage_size, PROT_READ|PROT_WRITE); |
| 213 |
test_mprotect(fd, "RW->R", hpage_size, PROT_READ|PROT_WRITE, |
| 214 |
hpage_size, PROT_READ); |
| 215 |
|
| 216 |
|
| 217 |
test_mprotect(fd, "R->RW 1/2", 2*hpage_size, PROT_READ, |
| 218 |
hpage_size, PROT_READ|PROT_WRITE); |
| 219 |
test_mprotect(fd, "RW->R 1/2", 2*hpage_size, PROT_READ|PROT_WRITE, |
| 220 |
hpage_size, PROT_READ); |
| 221 |
|
| 222 |
|
| 223 |
test_mprotect(fd, "NONE->R", hpage_size, PROT_NONE, |
| 224 |
hpage_size, PROT_READ); |
| 225 |
test_mprotect(fd, "NONE->RW", hpage_size, PROT_NONE, |
| 226 |
hpage_size, PROT_READ|PROT_WRITE); |
| 227 |
|
| 228 |
PASS(); |
| 229 |
} |