| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | |
| 3 | /* |
| 4 | * Copyright (C) 2019 Joe Korty <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Have a tracer do a bunch of PTRACE_SINGLESTEPs against |
| 9 | * a tracee as fast as possible. Create several of these |
| 10 | * tracer/tracee pairs and see if they can be made to |
| 11 | * interfere with each other. |
| 12 | * |
| 13 | * Usage: |
| 14 | * ssdd nforks niters |
| 15 | * Where: |
| 16 | * nforks - number of tracer/tracee pairs to fork off. |
| 17 | * default 10. |
| 18 | * niters - number of PTRACE_SINGLESTEP iterations to |
| 19 | * do before declaring success, for each tracer/ |
| 20 | * tracee pair set up. Default 10,000. |
| 21 | * |
| 22 | * The tracer waits on each PTRACE_SINGLESTEP with a waitpid(2) |
| 23 | * and checks that waitpid's return values for correctness. |
| 24 | * |
| 25 | */ |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <stddef.h> |
| 29 | #include <unistd.h> |
| 30 | #include <getopt.h> |
| 31 | #include <string.h> |
| 32 | #include <signal.h> |
| 33 | #include <time.h> |
| 34 | #include <errno.h> |
| 35 | |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/wait.h> |
| 38 | #include <sys/ptrace.h> |
| 39 | |
| 40 | #include "rt-utils.h" |
| 41 | |
| 42 | /* do_wait return values */ |
| 43 | #define STATE_EXITED 1 |
| 44 | #define STATE_STOPPED 2 |
| 45 | #define STATE_SIGNALED 3 |
| 46 | #define STATE_UNKNOWN 4 |
| 47 | #define STATE_ECHILD 5 |
| 48 | #define STATE_EXITED_TSIG 6 /* exited with termination signal */ |
| 49 | #define STATE_EXITED_ERRSTAT 7 /* exited with non-zero status */ |
| 50 | |
| 51 | static char *state_name[] = { |
| 52 | [STATE_EXITED] = "STATE_EXITED", |
| 53 | [STATE_STOPPED] = "STATE_STOPPED", |
| 54 | [STATE_SIGNALED] = "STATE_SIGNALED", |
| 55 | [STATE_UNKNOWN] = "STATE_UNKNOWN", |
| 56 | [STATE_ECHILD] = "STATE_ECHILD", |
| 57 | [STATE_EXITED_TSIG] = "STATE_EXITED_TSIG", |
| 58 | [STATE_EXITED_ERRSTAT] = "STATE_EXITED_ERRSTAT" |
| 59 | }; |
| 60 | |
| 61 | static const char *get_state_name(int state) |
| 62 | { |
| 63 | if (state < STATE_EXITED || state > STATE_EXITED_ERRSTAT) |
| 64 | return "?"; |
| 65 | return state_name[state]; |
| 66 | } |
| 67 | |
| 68 | static int quiet; |
| 69 | static char jsonfile[MAX_PATH]; |
| 70 | |
| 71 | volatile int got_sigchld; |
| 72 | |
| 73 | enum option_value { OPT_NFORKS=1, OPT_NITERS, OPT_HELP, OPT_JSON, OPT_QUIET }; |
| 74 | |
| 75 | static void usage(int error) |
| 76 | { |
| 77 | printf("ssdd V %1.2f\n", VERSION); |
| 78 | printf("Usage:\n" |
| 79 | "ssdd <options>\n\n" |
| 80 | "-f --forks=NUM number of forks\n" |
| 81 | "-h --help print this message\n" |
| 82 | " --json=FILENAME write final results into FILENAME, JSON formatted\n" |
| 83 | "-q --quiet suppress running output\n" |
| 84 | "-i --iters=NUM number of iterations, NUM must be at least 1\n" |
| 85 | ); |
| 86 | exit(error); |
| 87 | } |
| 88 | |
| 89 | static int do_wait(pid_t *wait_pid, int *ret_sig) |
| 90 | { |
| 91 | int status, child_status; |
| 92 | |
| 93 | *ret_sig = -1; /* initially mark 'nothing returned' */ |
| 94 | |
| 95 | while (1) { |
| 96 | status = waitpid(-1, &child_status, WUNTRACED | __WALL); |
| 97 | if (status == -1) { |
| 98 | if (errno == EINTR) |
| 99 | continue; |
| 100 | if (errno == ECHILD) { |
| 101 | *wait_pid = (pid_t)0; |
| 102 | return STATE_ECHILD; |
| 103 | } |
| 104 | printf("do_wait/%d: EXITING, ERROR: " |
| 105 | "waitpid() returned errno %d\n", |
| 106 | getpid(), errno); |
| 107 | exit(1); |
| 108 | } |
| 109 | break; |
| 110 | } |
| 111 | *wait_pid = (pid_t)status; |
| 112 | |
| 113 | if (WIFEXITED(child_status)) { |
| 114 | if (WIFSIGNALED(child_status)) |
| 115 | return STATE_EXITED_TSIG; |
| 116 | if (WEXITSTATUS(child_status)) |
| 117 | return STATE_EXITED_ERRSTAT; |
| 118 | return STATE_EXITED; |
| 119 | } |
| 120 | if (WIFSTOPPED(child_status)) { |
| 121 | *ret_sig = WSTOPSIG(child_status); |
| 122 | return STATE_STOPPED; |
| 123 | } |
| 124 | if (WIFSIGNALED(child_status)) { |
| 125 | *ret_sig = WTERMSIG(child_status); |
| 126 | return STATE_SIGNALED; |
| 127 | } |
| 128 | return STATE_UNKNOWN; |
| 129 | } |
| 130 | |
| 131 | static int check_sigchld(sigset_t *set) |
| 132 | { |
| 133 | |
| 134 | struct timespec timeout; |
| 135 | |
| 136 | timeout.tv_sec = 10; |
| 137 | timeout.tv_nsec = 0; |
| 138 | int recv_sig = 0; |
| 139 | |
| 140 | /* |
| 141 | * Check the handler flag, then if need be, wait for the signal to |
| 142 | * arrive |
| 143 | */ |
| 144 | if (!got_sigchld) |
| 145 | recv_sig = sigtimedwait(set, NULL, &timeout); |
| 146 | |
| 147 | if (sigprocmask(SIG_UNBLOCK, set, NULL) == -1) { |
| 148 | printf("EXITING, ERROR: unable to mask signal set\n"); |
| 149 | exit(1); |
| 150 | } |
| 151 | |
| 152 | if (recv_sig == -1) { |
| 153 | printf("EXITING, ERROR: Timeout: no signal received in 10 seconds\n"); |
| 154 | exit(1); |
| 155 | } else if (recv_sig == SIGCHLD) { |
| 156 | got_sigchld = 1; |
| 157 | } |
| 158 | |
| 159 | return got_sigchld; |
| 160 | } |
| 161 | |
| 162 | static pid_t parent; |
| 163 | static int nforks = 10; |
| 164 | static int nsteps = 10000; |
| 165 | |
| 166 | static void sigchld(int sig, siginfo_t *info, void *arg) |
| 167 | { |
| 168 | got_sigchld = 1; |
| 169 | } |
| 170 | |
| 171 | static void child_process(void) |
| 172 | { |
| 173 | while (1) |
| 174 | ; |
| 175 | } |
| 176 | |
| 177 | static int forktests(int testid) |
| 178 | { |
| 179 | int i, status, ret_sig; |
| 180 | long pstatus; |
| 181 | pid_t child, wait_pid; |
| 182 | struct sigaction act, oact; |
| 183 | |
| 184 | parent = getpid(); |
| 185 | |
| 186 | child = fork(); |
| 187 | if (child == -1) { |
| 188 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 189 | "fork returned errno %d\n", testid, parent, errno); |
| 190 | exit(1); |
| 191 | } |
| 192 | if (!child) |
| 193 | child_process(); |
| 194 | |
| 195 | if (!quiet) |
| 196 | printf("forktest#%d/%d/%d: STARTING\n", testid, parent, child); |
| 197 | |
| 198 | act.sa_sigaction = sigchld; |
| 199 | sigemptyset(&act.sa_mask); |
| 200 | act.sa_flags = SA_SIGINFO; |
| 201 | status = sigaction(SIGCHLD, &act, &oact); |
| 202 | if (status) { |
| 203 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 204 | "sigaction returned %d, errno %d\n", |
| 205 | testid, parent, status, errno); |
| 206 | exit(1); |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Block the signal before it is generated |
| 211 | * Ensures we can synchronously wait for it. |
| 212 | */ |
| 213 | sigset_t set; |
| 214 | |
| 215 | sigemptyset(&set); |
| 216 | sigaddset(&set, SIGCHLD); |
| 217 | |
| 218 | if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { |
| 219 | printf("EXITING, ERROR: unable to mask signal set\n"); |
| 220 | exit(1); |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Attach to the child. |
| 225 | */ |
| 226 | pstatus = ptrace(PTRACE_ATTACH, child, NULL, NULL); |
| 227 | if (pstatus == ~0l) { |
| 228 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 229 | "attach failed. errno %d\n", |
| 230 | testid, getpid(), errno); |
| 231 | exit(1); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * The attach should cause the child to receive a signal. |
| 236 | */ |
| 237 | status = do_wait(&wait_pid, &ret_sig); |
| 238 | if (wait_pid != child) { |
| 239 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 240 | "attach: Unexpected wait pid %d\n", |
| 241 | testid, getpid(), wait_pid); |
| 242 | exit(1); |
| 243 | } |
| 244 | if (status != STATE_STOPPED) { |
| 245 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 246 | "attach: wait on PTRACE_ATTACH returned %d " |
| 247 | "[%s, wanted STATE_STOPPED], signo %d\n", |
| 248 | testid, getpid(), status, get_state_name(status), |
| 249 | ret_sig); |
| 250 | exit(1); |
| 251 | } |
| 252 | if (!check_sigchld(&set)) { |
| 253 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 254 | "wait on PTRACE_ATTACH saw a SIGCHLD count of %d, should be 1\n", |
| 255 | testid, getpid(), got_sigchld); |
| 256 | exit(1); |
| 257 | } |
| 258 | got_sigchld = 0; |
| 259 | |
| 260 | |
| 261 | /* |
| 262 | * Generate 'nsteps' PTRACE_SINGLESTEPs, make sure they all actually |
| 263 | * step the tracee. |
| 264 | */ |
| 265 | for (i = 0; i < nsteps; i++) { |
| 266 | |
| 267 | if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { |
| 268 | printf("EXITING, ERROR: unable to mask signal set\n"); |
| 269 | exit(1); |
| 270 | } |
| 271 | |
| 272 | pstatus = ptrace(PTRACE_SINGLESTEP, child, NULL, NULL); |
| 273 | |
| 274 | if (pstatus) { |
| 275 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 276 | "PTRACE_SINGLESTEP #%d: returned status %ld, " |
| 277 | "errno %d, signo %d\n", |
| 278 | testid, getpid(), i, pstatus, errno, ret_sig); |
| 279 | exit(1); |
| 280 | } |
| 281 | |
| 282 | status = do_wait(&wait_pid, &ret_sig); |
| 283 | if (wait_pid != child) { |
| 284 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 285 | "wait on PTRACE_SINGLESTEP #%d: returned wrong pid %d, " |
| 286 | "expected %d\n", |
| 287 | testid, getpid(), i, wait_pid, child); |
| 288 | exit(1); |
| 289 | } |
| 290 | if (status != STATE_STOPPED) { |
| 291 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 292 | "wait on PTRACE_SINGLESTEP #%d: wanted STATE_STOPPED, " |
| 293 | "saw %s instead (and saw signo %d too)\n", |
| 294 | testid, getpid(), i, |
| 295 | get_state_name(status), ret_sig); |
| 296 | exit(1); |
| 297 | } |
| 298 | if (ret_sig != SIGTRAP) { |
| 299 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 300 | "wait on PTRACE_SINGLESTEP #%d: returned signal %d, " |
| 301 | "wanted SIGTRAP\n", |
| 302 | testid, getpid(), i, ret_sig); |
| 303 | exit(1); |
| 304 | } |
| 305 | if (!check_sigchld(&set)) { |
| 306 | printf("forktest#%d/%d: EXITING, ERROR: " |
| 307 | "wait on PTRACE_SINGLESTEP #%d: no SIGCHLD seen " |
| 308 | "(signal count == 0), signo %d\n", |
| 309 | testid, getpid(), i, ret_sig); |
| 310 | exit(1); |
| 311 | } |
| 312 | got_sigchld = 0; |
| 313 | } |
| 314 | |
| 315 | /* There is no need for the tracer to kill the tracee. It will |
| 316 | * automatically exit when its owner, ie, us, exits. |
| 317 | */ |
| 318 | if (!quiet) |
| 319 | printf("forktest#%d/%d: EXITING, no error\n", testid, parent); |
| 320 | exit(0); |
| 321 | } |
| 322 | |
| 323 | int main(int argc, char **argv) |
| 324 | { |
| 325 | int i, ret_sig, status; |
| 326 | pid_t child = 0, wait_pid; |
| 327 | int error = 0; |
| 328 | |
| 329 | setbuf(stdout, NULL); |
| 330 | |
| 331 | rt_init(argc, argv); |
| 332 | for (;;) { |
| 333 | int option_index = 0; |
| 334 | |
| 335 | static struct option long_options[] = { |
| 336 | {"forks", required_argument, NULL, OPT_NFORKS}, |
| 337 | {"help", no_argument, NULL, OPT_HELP}, |
| 338 | {"json", required_argument, NULL, OPT_JSON}, |
| 339 | {"quiet", no_argument, NULL, OPT_QUIET}, |
| 340 | {"iters", required_argument, NULL, OPT_NITERS}, |
| 341 | {NULL, 0, NULL, 0}, |
| 342 | }; |
| 343 | int c = getopt_long(argc, argv, "f:hqi:", long_options, &option_index); |
| 344 | if (c == -1) |
| 345 | break; |
| 346 | switch(c) { |
| 347 | case 'f': |
| 348 | case OPT_NFORKS: |
| 349 | nforks = atoi(optarg); |
| 350 | break; |
| 351 | case 'h': |
| 352 | case OPT_HELP: |
| 353 | usage(0); |
| 354 | break; |
| 355 | case OPT_JSON: |
| 356 | strncpy(jsonfile, optarg, strnlen(optarg, MAX_PATH-1)); |
| 357 | break; |
| 358 | case OPT_QUIET: |
| 359 | case 'q': |
| 360 | quiet = 1; |
| 361 | break; |
| 362 | case 'i': |
| 363 | case OPT_NITERS: |
| 364 | nsteps = atoi(optarg); |
| 365 | if (nsteps < 1) { |
| 366 | usage(0); |
| 367 | } |
| 368 | break; |
| 369 | default: |
| 370 | usage(1); |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (!quiet) { |
| 376 | printf("#main : %d\n", getpid()); |
| 377 | printf("#forks: %d\n", nforks); |
| 378 | printf("#steps: %d\n", nsteps); |
| 379 | printf("\n"); |
| 380 | } |
| 381 | |
| 382 | for (i = 0; i < nforks; i++) { |
| 383 | child = fork(); |
| 384 | if (child == -1) { |
| 385 | printf("main: fork returned errno %d\n", errno); |
| 386 | exit(1); |
| 387 | } |
| 388 | if (!child) |
| 389 | forktests(i); |
| 390 | } |
| 391 | |
| 392 | for (i = 0; i < nforks; i++) { |
| 393 | status = do_wait(&wait_pid, &ret_sig); |
| 394 | if (status != STATE_EXITED) { |
| 395 | if (0) printf("main/%d: ERROR: " |
| 396 | "forktest#%d unexpected do_wait status %d " |
| 397 | "[%s, wanted STATE_EXITED]\n", |
| 398 | getpid(), wait_pid, status, |
| 399 | get_state_name(status)); |
| 400 | error = 1; |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | printf("%s.\n", error ? |
| 405 | "One or more tests FAILED" : |
| 406 | "All tests PASSED"); |
| 407 | |
| 408 | if (strlen(jsonfile) != 0) |
| 409 | rt_write_json(jsonfile, error, NULL, NULL); |
| 410 | |
| 411 | exit(error); |
| 412 | } |