| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#define _GNU_SOURCE |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <stdlib.h> |
| 23 |
#include <string.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <time.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <limits.h> |
| 28 |
#include <string.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <sys/mman.h> |
| 31 |
#include <sys/shm.h> |
| 32 |
#include <sys/wait.h> |
| 33 |
|
| 34 |
#include "hugetests.h" |
| 35 |
|
| 36 |
#define BLOCK_SIZE 16384 |
| 37 |
#define CONST 0xdeadbeef |
| 38 |
#define SHM_KEY 0xdeadcab |
| 39 |
#define NUM_CHILDREN 2 |
| 40 |
|
| 41 |
#define BIG_INIT { \ |
| 42 |
[0] = CONST, [17] = CONST, [BLOCK_SIZE-1] = CONST, \ |
| 43 |
} |
| 44 |
static int small_data = 1; |
| 45 |
static int big_data[BLOCK_SIZE] = BIG_INIT; |
| 46 |
|
| 47 |
static int small_bss; |
| 48 |
static int big_bss[BLOCK_SIZE]; |
| 49 |
|
| 50 |
const int small_const = CONST; |
| 51 |
const int big_const[BLOCK_SIZE] = BIG_INIT; |
| 52 |
|
| 53 |
static int static_func(int x) |
| 54 |
{ |
| 55 |
return x; |
| 56 |
} |
| 57 |
|
| 58 |
int global_func(int x) |
| 59 |
{ |
| 60 |
return x; |
| 61 |
} |
| 62 |
|
| 63 |
static struct test_entry { |
| 64 |
const char *name; |
| 65 |
void *data; |
| 66 |
int size; |
| 67 |
char linkchar; |
| 68 |
int writable, execable; |
| 69 |
int is_huge; |
| 70 |
} testtab[] = { |
| 71 |
#define RWENT(name, linkchar) { #name, &name, sizeof(name), linkchar, 1, 0, } |
| 72 |
#define ROENT(name, linkchar) { #name, (void *)&name, sizeof(name), linkchar, 0, 0, } |
| 73 |
#define RXENT(name, linkchar) { #name, &name, sizeof(name), linkchar, 0, 1, } |
| 74 |
RWENT(small_data, 'D'), |
| 75 |
RWENT(big_data, 'D'), |
| 76 |
RWENT(small_bss, 'B'), |
| 77 |
RWENT(big_bss, 'B'), |
| 78 |
ROENT(small_const, 'T'), |
| 79 |
ROENT(big_const, 'T'), |
| 80 |
RXENT(static_func, 'T'), |
| 81 |
RXENT(global_func, 'T'), |
| 82 |
}; |
| 83 |
|
| 84 |
#define NUM_TESTS (sizeof(testtab) / sizeof(testtab[0])) |
| 85 |
|
| 86 |
static int sharing; |
| 87 |
static int elfmap_off; |
| 88 |
static int shmid; |
| 89 |
static ino_t *shm; |
| 90 |
|
| 91 |
static char link_string[32]; |
| 92 |
|
| 93 |
static void get_link_string(const char *argv0) |
| 94 |
{ |
| 95 |
const char *p, *q; |
| 96 |
|
| 97 |
|
| 98 |
p = strrchr(argv0, '/'); |
| 99 |
if (p) |
| 100 |
p++; |
| 101 |
else |
| 102 |
p = argv0; |
| 103 |
|
| 104 |
if (*p != 'x') |
| 105 |
return; |
| 106 |
|
| 107 |
q = strchr(p, '.'); |
| 108 |
if (!q) |
| 109 |
|
| 110 |
return; |
| 111 |
|
| 112 |
memcpy(link_string, p, q-p); |
| 113 |
} |
| 114 |
|
| 115 |
static ino_t do_test(struct test_entry *te) |
| 116 |
{ |
| 117 |
int i; |
| 118 |
volatile int *p = te->data; |
| 119 |
|
| 120 |
if (te->writable) { |
| 121 |
for (i = 0; i < (te->size / sizeof(*p)); i++) |
| 122 |
p[i] = CONST ^ i; |
| 123 |
|
| 124 |
barrier(); |
| 125 |
|
| 126 |
for (i = 0; i < (te->size / sizeof(*p)); i++) { |
| 127 |
if (p[i] != (CONST ^ i)) { |
| 128 |
verbose_printf("mismatch on %s", te->name); |
| 129 |
exit(RC_FAIL); |
| 130 |
} |
| 131 |
} |
| 132 |
} else if (te->execable) { |
| 133 |
int (*pf)(int) = te->data; |
| 134 |
|
| 135 |
if ((*pf)(CONST) != CONST) { |
| 136 |
verbose_printf("%s returns incorrect results", te->name); |
| 137 |
exit(RC_FAIL); |
| 138 |
} |
| 139 |
} else { |
| 140 |
|
| 141 |
for (i = 0; i < (te->size / sizeof(*p)); i++) |
| 142 |
p[i]; |
| 143 |
} |
| 144 |
|
| 145 |
te->is_huge = (test_addr_huge(te->data) == 1); |
| 146 |
|
| 147 |
return get_addr_inode(te->data); |
| 148 |
} |
| 149 |
|
| 150 |
static void parse_env(void) |
| 151 |
{ |
| 152 |
char *env; |
| 153 |
|
| 154 |
env = getenv("HUGETLB_ELFMAP"); |
| 155 |
if (env && (strcasecmp(env, "no") == 0)) { |
| 156 |
verbose_printf("Segment remapping disabled\n"); |
| 157 |
elfmap_off = 1; |
| 158 |
} else { |
| 159 |
env = getenv("HUGETLB_SHARE"); |
| 160 |
if (env) |
| 161 |
sharing = atoi(env); |
| 162 |
verbose_printf("Segment remapping enabled, " |
| 163 |
"sharing = %d\n", sharing); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
static pid_t spawn_child(char *self, int index) |
| 168 |
{ |
| 169 |
int ret; |
| 170 |
char execarg1[5]; |
| 171 |
|
| 172 |
ret = snprintf(execarg1, 5, "%d", index); |
| 173 |
if (ret < 0) |
| 174 |
FAIL("snprintf failed: %s", strerror(errno)); |
| 175 |
|
| 176 |
ret = fork(); |
| 177 |
if (ret) { |
| 178 |
if (ret < 0) { |
| 179 |
shmctl(shmid, IPC_RMID, NULL); |
| 180 |
shmdt(shm); |
| 181 |
FAIL("fork failed: %s", |
| 182 |
strerror(errno)); |
| 183 |
} |
| 184 |
} else { |
| 185 |
ret = execlp(self, self, execarg1, NULL); |
| 186 |
if (ret) { |
| 187 |
shmctl(shmid, IPC_RMID, NULL); |
| 188 |
shmdt(shm); |
| 189 |
FAIL("execl(%s, %s, %s failed: %s", |
| 190 |
self, self, execarg1, |
| 191 |
strerror(errno)); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
return ret; |
| 196 |
} |
| 197 |
|
| 198 |
static int child_process(char *self, int index) |
| 199 |
{ |
| 200 |
int i; |
| 201 |
ino_t ino; |
| 202 |
|
| 203 |
get_link_string(self); |
| 204 |
|
| 205 |
shmid = shmget(SHM_KEY, NUM_CHILDREN * NUM_TESTS * |
| 206 |
sizeof(ino_t), 0666); |
| 207 |
if (shmid < 0) { |
| 208 |
verbose_printf("Child's shmget failed: %s", strerror(errno)); |
| 209 |
exit(RC_FAIL); |
| 210 |
} |
| 211 |
|
| 212 |
shm = shmat(shmid, NULL, 0); |
| 213 |
if (shm == (void *)-1) { |
| 214 |
verbose_printf("Child's shmat failed: %s", strerror(errno)); |
| 215 |
exit(RC_FAIL); |
| 216 |
} |
| 217 |
|
| 218 |
for (i = 0; i < NUM_TESTS; i++) { |
| 219 |
if (!test_addr_huge(testtab + i)) { |
| 220 |
|
| 221 |
shm[index * NUM_TESTS + i] = 0; |
| 222 |
} else { |
| 223 |
ino = do_test(testtab + i); |
| 224 |
if ((int)ino < 0) { |
| 225 |
shmdt(shm); |
| 226 |
exit(RC_FAIL); |
| 227 |
} |
| 228 |
shm[index * NUM_TESTS + i] = ino; |
| 229 |
} |
| 230 |
} |
| 231 |
shmdt(shm); |
| 232 |
return 0; |
| 233 |
} |
| 234 |
|
| 235 |
static void verify_inodes() |
| 236 |
{ |
| 237 |
int i, j; |
| 238 |
|
| 239 |
for (i = 0; i < NUM_TESTS; i++) { |
| 240 |
ino_t base = shm[i]; |
| 241 |
for (j = 1; j < NUM_CHILDREN; j++) { |
| 242 |
ino_t comp = shm[j * NUM_TESTS + i]; |
| 243 |
if (base != comp) { |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
if (sharing == 1 && testtab[i].writable == 0) { |
| 250 |
shmctl(shmid, IPC_RMID, NULL); |
| 251 |
shmdt(shm); |
| 252 |
FAIL("Inodes do not match " |
| 253 |
"(%u != %u)", |
| 254 |
(int)base, (int)comp); |
| 255 |
} |
| 256 |
} else { |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
if (base == 0) |
| 267 |
continue; |
| 268 |
|
| 269 |
if (elfmap_off == 1 || sharing == 0 || |
| 270 |
(sharing == 1 && testtab[i].writable == 1)) { |
| 271 |
shmctl(shmid, IPC_RMID, NULL); |
| 272 |
shmdt(shm); |
| 273 |
if (sharing == 1 && testtab[i].writable == 1) |
| 274 |
verbose_printf("Incorrectly sharing a writable segment...\n"); |
| 275 |
FAIL("Inodes match, but we should not be " |
| 276 |
"sharing this segment (%d == %d)", |
| 277 |
(int)base, (int)comp); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
static void sigsegv_handler(int signum, siginfo_t *si, void *context) |
| 285 |
{ |
| 286 |
FAIL("Segmentation fault in parent at address %p", si->si_addr); |
| 287 |
} |
| 288 |
|
| 289 |
int main(int argc, char *argv[], char *envp[]) |
| 290 |
{ |
| 291 |
test_init(argc, argv); |
| 292 |
|
| 293 |
if (argc == 1) { |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
pid_t children_pids[NUM_CHILDREN]; |
| 298 |
int ret, i; |
| 299 |
int status; |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
struct sigaction sa_seg = { |
| 308 |
.sa_sigaction = sigsegv_handler, |
| 309 |
.sa_flags = SA_SIGINFO, |
| 310 |
}; |
| 311 |
|
| 312 |
parse_env(); |
| 313 |
|
| 314 |
ret = sigaction(SIGSEGV, &sa_seg, NULL); |
| 315 |
if (ret < 0) |
| 316 |
FAIL("Installing SIGSEGV handler failed: %s", |
| 317 |
strerror(errno)); |
| 318 |
|
| 319 |
shmid = shmget(SHM_KEY, NUM_CHILDREN * NUM_TESTS * |
| 320 |
sizeof(ino_t), IPC_CREAT | IPC_EXCL | |
| 321 |
0666); |
| 322 |
if (shmid < 0) |
| 323 |
FAIL("Parent's shmget failed: %s", strerror(errno)); |
| 324 |
|
| 325 |
shm = shmat(shmid, NULL, 0); |
| 326 |
if (shm == (void *)-1) |
| 327 |
FAIL("Parent's shmat failed: %s", strerror(errno)); |
| 328 |
|
| 329 |
for (i = 0; i < NUM_CHILDREN; i++) |
| 330 |
children_pids[i] = spawn_child(argv[0], i); |
| 331 |
|
| 332 |
for (i = 0; i < NUM_CHILDREN; i++) { |
| 333 |
ret = waitpid(children_pids[i], &status, 0); |
| 334 |
if (ret < 0) { |
| 335 |
shmctl(shmid, IPC_RMID, NULL); |
| 336 |
shmdt(shm); |
| 337 |
FAIL("waitpid failed: %s", strerror(errno)); |
| 338 |
} |
| 339 |
|
| 340 |
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) { |
| 341 |
shmctl(shmid, IPC_RMID, NULL); |
| 342 |
shmdt(shm); |
| 343 |
FAIL("Child %d exited with non-zero status: %d", |
| 344 |
i + 1, WEXITSTATUS(status)); |
| 345 |
} |
| 346 |
|
| 347 |
if (WIFSIGNALED(status)) { |
| 348 |
shmctl(shmid, IPC_RMID, NULL); |
| 349 |
shmdt(shm); |
| 350 |
FAIL("Child %d killed by signal: %s", i + 1, |
| 351 |
strsignal(WTERMSIG(status))); |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
verify_inodes(); |
| 356 |
|
| 357 |
shmctl(shmid, IPC_RMID, NULL); |
| 358 |
shmdt(shm); |
| 359 |
PASS(); |
| 360 |
} else { |
| 361 |
if (argc == 2) { |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
child_process(argv[0], atoi(argv[1])); |
| 367 |
} else { |
| 368 |
FAIL("Invalid arguments\n"); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
return 0; |
| 373 |
} |