| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <unistd.h> |
| 25 |
#include <sys/mman.h> |
| 26 |
#include <sys/ptrace.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/wait.h> |
| 29 |
|
| 30 |
#include <hugetlbfs.h> |
| 31 |
|
| 32 |
#include "hugetests.h" |
| 33 |
|
| 34 |
#define CONST 0xdeadbeefL |
| 35 |
|
| 36 |
static long hpage_size; |
| 37 |
static volatile int ready_to_trace = 0; |
| 38 |
|
| 39 |
static void sigchld_handler(int signum, siginfo_t *si, void *uc) |
| 40 |
{ |
| 41 |
int status; |
| 42 |
|
| 43 |
wait(&status); |
| 44 |
if (WIFEXITED(status)) |
| 45 |
exit(WEXITSTATUS(status)); |
| 46 |
else if (WIFSIGNALED(status)) |
| 47 |
exit(status); |
| 48 |
|
| 49 |
ready_to_trace = 1; |
| 50 |
} |
| 51 |
|
| 52 |
static void child(int hugefd, int pipefd) |
| 53 |
{ |
| 54 |
void *p; |
| 55 |
int err; |
| 56 |
|
| 57 |
p = mmap(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, |
| 58 |
hugefd, 0); |
| 59 |
if (p == MAP_FAILED) |
| 60 |
FAIL("mmap(): %s", strerror(errno)); |
| 61 |
|
| 62 |
memset(p, 0, hpage_size); |
| 63 |
|
| 64 |
verbose_printf("Child mapped data at %p\n", p); |
| 65 |
|
| 66 |
err = write(pipefd, &p, sizeof(p)); |
| 67 |
if (err == -1) |
| 68 |
FAIL("Writing to pipe: %s", strerror(errno)); |
| 69 |
if (err != sizeof(p)) |
| 70 |
FAIL("Short write to pipe"); |
| 71 |
|
| 72 |
pause(); |
| 73 |
} |
| 74 |
|
| 75 |
static void do_poke(pid_t pid, void *p) |
| 76 |
{ |
| 77 |
long err; |
| 78 |
|
| 79 |
verbose_printf("Poking..."); |
| 80 |
err = ptrace(PTRACE_POKEDATA, pid, p, (void *)CONST); |
| 81 |
if (err) |
| 82 |
FAIL("ptrace(POKEDATA): %s", strerror(errno)); |
| 83 |
verbose_printf("done\n"); |
| 84 |
|
| 85 |
verbose_printf("Peeking..."); |
| 86 |
err = ptrace(PTRACE_PEEKDATA, pid, p, NULL); |
| 87 |
if (err == -1) |
| 88 |
FAIL("ptrace(PEEKDATA): %s", strerror(errno)); |
| 89 |
|
| 90 |
if (err != CONST) |
| 91 |
FAIL("mismatch (%lx instead of %lx)", err, CONST); |
| 92 |
verbose_printf("done\n"); |
| 93 |
} |
| 94 |
|
| 95 |
int main(int argc, char *argv[]) |
| 96 |
{ |
| 97 |
int fd; |
| 98 |
int pipefd[2]; |
| 99 |
long err; |
| 100 |
pid_t cpid; |
| 101 |
void *p; |
| 102 |
struct sigaction sa = { |
| 103 |
.sa_sigaction = sigchld_handler, |
| 104 |
.sa_flags = SA_SIGINFO, |
| 105 |
}; |
| 106 |
struct sigaction old_sa; |
| 107 |
|
| 108 |
|
| 109 |
test_init(argc, argv); |
| 110 |
|
| 111 |
hpage_size = check_hugepagesize(); |
| 112 |
|
| 113 |
fd = hugetlbfs_unlinked_fd(); |
| 114 |
if (fd < 0) |
| 115 |
FAIL("hugetlbfs_unlinked_fd()"); |
| 116 |
|
| 117 |
err = sigaction(SIGCHLD, &sa, &old_sa); |
| 118 |
if (err) |
| 119 |
FAIL("Can't install SIGCHLD handler: %s", strerror(errno)); |
| 120 |
|
| 121 |
err = pipe(pipefd); |
| 122 |
if (err) |
| 123 |
FAIL("pipe(): %s", strerror(errno)); |
| 124 |
|
| 125 |
cpid = fork(); |
| 126 |
if (cpid < 0) |
| 127 |
FAIL("fork(): %s", strerror(errno)); |
| 128 |
|
| 129 |
|
| 130 |
if (cpid == 0) { |
| 131 |
child(fd, pipefd[1]); |
| 132 |
exit(0); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
err = read(pipefd[0], &p, sizeof(p)); |
| 137 |
if (err == -1) |
| 138 |
FAIL("Reading pipe: %s\n", strerror(errno)); |
| 139 |
if (err != sizeof(p)) |
| 140 |
FAIL("Short read over pipe"); |
| 141 |
|
| 142 |
verbose_printf("Parent received address %p\n", p); |
| 143 |
|
| 144 |
err = ptrace(PTRACE_ATTACH, cpid, NULL, NULL); |
| 145 |
if (err) |
| 146 |
FAIL("ptrace(ATTACH): %s", strerror(errno)); |
| 147 |
|
| 148 |
while (! ready_to_trace) |
| 149 |
; |
| 150 |
|
| 151 |
do_poke(cpid, p); |
| 152 |
do_poke(cpid, p + getpagesize()); |
| 153 |
|
| 154 |
err = sigaction(SIGCHLD, &old_sa, NULL); |
| 155 |
if (err) |
| 156 |
FAIL("Clearing SIGCHLD handler: %s", strerror(errno)); |
| 157 |
|
| 158 |
ptrace(PTRACE_KILL, cpid, NULL, NULL); |
| 159 |
|
| 160 |
PASS(); |
| 161 |
} |