omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / osh / host / test_osh_host.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * test_osh_host.c -- tests for the osh Linux host adapter execution service (aien-architecture#158). |
| 19 | * Records are hand-built (no parser exists here). Everything happens in a disposable temp dir under $TMPDIR. |
| 20 | * The test binary is also its own helper program: `test_osh_host --helper <op> ...` (cat, sum, gen, fdlist, env, |
| 21 | * touch, kill, sleep, echo) so no external tool except yes/head/printf (used by name for the comparisons) is needed. |
| 22 | */ |
| 23 | #ifndef _GNU_SOURCE |
| 24 | #define _GNU_SOURCE |
| 25 | #endif |
| 26 | #include "osh_host.h" |
| 27 | |
| 28 | #include <dirent.h> |
| 29 | #include <errno.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <ftw.h> |
| 32 | #include <signal.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <sys/resource.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/wait.h> |
| 40 | #include <time.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | /* ================= helper mode ================= */ |
| 44 | |
| 45 | static uint32_t fnv(uint32_t h, const unsigned char *p, size_t n) |
| 46 | { |
| 47 | for (size_t i = 0; i < n; i++) h = (h ^ p[i]) * 16777619u; |
| 48 | return h; |
| 49 | } |
| 50 | |
| 51 | static unsigned char gen_byte(unsigned long i) { return (unsigned char)((i * 2654435761u) >> 13); } |
| 52 | |
| 53 | static int helper_main(int argc, char **argv) |
| 54 | { |
| 55 | const char *op = argc > 2 ? argv[2] : ""; |
| 56 | if (!strcmp(op, "die")) { /* die of a signal (omega#344 RISK-8 tests) */ |
| 57 | int sig = argc > 3 ? atoi(argv[3]) : SIGTERM; |
| 58 | signal(sig, SIG_DFL); |
| 59 | raise(sig); |
| 60 | return 99; |
| 61 | } |
| 62 | if (!strcmp(op, "echo")) { |
| 63 | for (int i = 3; i < argc; i++) printf("%s%s", i > 3 ? " " : "", argv[i]); |
| 64 | printf("\n"); |
| 65 | return 0; |
| 66 | } |
| 67 | if (!strcmp(op, "cat")) { |
| 68 | char b[65536]; |
| 69 | ssize_t n; |
| 70 | while ((n = read(0, b, sizeof b)) > 0) |
| 71 | if (write(1, b, (size_t)n) != n) return 1; |
| 72 | return 0; |
| 73 | } |
| 74 | if (!strcmp(op, "sum")) { |
| 75 | unsigned char b[65536]; |
| 76 | ssize_t n; |
| 77 | uint32_t h = 2166136261u; |
| 78 | unsigned long tot = 0; |
| 79 | while ((n = read(0, b, sizeof b)) > 0) { h = fnv(h, b, (size_t)n); tot += (unsigned long)n; } |
| 80 | printf("%lu %08x\n", tot, h); |
| 81 | return 0; |
| 82 | } |
| 83 | if (!strcmp(op, "gen")) { |
| 84 | unsigned long n = strtoul(argv[3], NULL, 10); |
| 85 | unsigned char b[65536]; |
| 86 | for (unsigned long off = 0; off < n;) { |
| 87 | size_t k = n - off < sizeof b ? (size_t)(n - off) : sizeof b; |
| 88 | for (size_t i = 0; i < k; i++) b[i] = gen_byte(off + i); |
| 89 | if (write(1, b, k) != (ssize_t)k) return 1; |
| 90 | off += k; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | if (!strcmp(op, "fdlist")) { /* open descriptors above 2, excluding the directory handle used to list them */ |
| 95 | DIR *d = opendir("/proc/self/fd"); |
| 96 | int dfd = dirfd(d), cnt = 0; |
| 97 | struct dirent *e; |
| 98 | while ((e = readdir(d))) |
| 99 | if (e->d_name[0] != '.' && atoi(e->d_name) > 2 && atoi(e->d_name) != dfd) { |
| 100 | char l[320], t[256]; |
| 101 | snprintf(l, sizeof l, "/proc/self/fd/%s", e->d_name); |
| 102 | ssize_t n = readlink(l, t, sizeof t - 1); |
| 103 | t[n > 0 ? n : 0] = 0; |
| 104 | printf("leak fd %s -> %s\n", e->d_name, t); |
| 105 | cnt++; |
| 106 | } |
| 107 | closedir(d); |
| 108 | printf("fdcount %d\n", cnt); |
| 109 | return 0; |
| 110 | } |
| 111 | if (!strcmp(op, "env")) { |
| 112 | const char *v = getenv(argv[3]); |
| 113 | printf("%s\n", v ? v : "<unset>"); |
| 114 | return 0; |
| 115 | } |
| 116 | if (!strcmp(op, "envcount")) { |
| 117 | extern char **environ; |
| 118 | int n = 0; |
| 119 | for (char **e = environ; *e; e++) n++; |
| 120 | printf("%d\n", n); |
| 121 | return 0; |
| 122 | } |
| 123 | if (!strcmp(op, "touch")) { |
| 124 | int fd = open(argv[3], O_WRONLY | O_CREAT, 0666); |
| 125 | return fd < 0 ? 1 : 0; |
| 126 | } |
| 127 | if (!strcmp(op, "kill")) { raise(atoi(argv[3])); return 0; } |
| 128 | if (!strcmp(op, "sleep")) { |
| 129 | struct timespec ts = {atoi(argv[3]) / 1000, (atoi(argv[3]) % 1000) * 1000000L}; |
| 130 | nanosleep(&ts, NULL); |
| 131 | return 0; |
| 132 | } |
| 133 | if (!strcmp(op, "exit")) return atoi(argv[3]); |
| 134 | if (!strcmp(op, "pgid")) { printf("%d\n", (int)getpgrp()); return 0; } |
| 135 | if (!strcmp(op, "sigdisp")) { /* how SIGINT / SIGQUIT arrive in this process */ |
| 136 | struct sigaction a, q; |
| 137 | sigaction(SIGINT, NULL, &a); |
| 138 | sigaction(SIGQUIT, NULL, &q); |
| 139 | printf("INT %s QUIT %s\n", a.sa_handler == SIG_IGN ? "ign" : a.sa_handler == SIG_DFL ? "dfl" : "handler", q.sa_handler == SIG_IGN ? "ign" : q.sa_handler == SIG_DFL ? "dfl" : "handler"); |
| 140 | return 0; |
| 141 | } |
| 142 | if (!strcmp(op, "sigblk")) { /* number of signals TERM/USR1/INT/QUIT blocked here */ |
| 143 | sigset_t m; |
| 144 | sigprocmask(SIG_BLOCK, NULL, &m); |
| 145 | printf("blocked %d\n", sigismember(&m, SIGTERM) + sigismember(&m, SIGUSR1) + sigismember(&m, SIGINT) + sigismember(&m, SIGQUIT)); |
| 146 | return 0; |
| 147 | } |
| 148 | if (!strcmp(op, "both")) { /* write to stdout and stderr */ |
| 149 | printf("out\n"); fflush(stdout); |
| 150 | fprintf(stderr, "err\n"); |
| 151 | return 0; |
| 152 | } |
| 153 | return 99; |
| 154 | } |
| 155 | |
| 156 | /* ================= harness ================= */ |
| 157 | |
| 158 | static int g_fail, g_pass; |
| 159 | static char g_self[4096], g_tmp[4096]; |
| 160 | |
| 161 | #define CHECK(cond, ...) do { if (cond) g_pass++; else { g_fail++; printf("FAIL %s:%d: %s -- ", __FILE__, __LINE__, #cond); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 162 | |
| 163 | static size_t g_slurp_len; |
| 164 | |
| 165 | static char *slurp(const char *path) |
| 166 | { |
| 167 | g_slurp_len = 0; |
| 168 | FILE *f = fopen(path, "rb"); |
| 169 | char *b = calloc(1, 1 << 20); |
| 170 | if (f) { size_t n = fread(b, 1, (1 << 20) - 1, f); b[n] = 0; g_slurp_len = n; fclose(f); } |
| 171 | return b; |
| 172 | } |
| 173 | |
| 174 | static void wfile(const char *path, const char *s, mode_t mode) |
| 175 | { |
| 176 | int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); |
| 177 | if (fd >= 0) { if (write(fd, s, strlen(s)) < 0) {} close(fd); } |
| 178 | chmod(path, mode); |
| 179 | } |
| 180 | |
| 181 | static char *P(const char *rel) /* path in the temp dir (static ring) */ |
| 182 | { |
| 183 | static char ring[16][4200]; |
| 184 | static int k; |
| 185 | char *r = ring[k++ & 15]; |
| 186 | snprintf(r, 4200, "%s/%s", g_tmp, rel); |
| 187 | return r; |
| 188 | } |
| 189 | |
| 190 | typedef struct { |
| 191 | OshSession s; |
| 192 | char op[4200], ep[4200]; |
| 193 | int ofd, efd; |
| 194 | } T; |
| 195 | |
| 196 | static int count_fds(void) |
| 197 | { |
| 198 | DIR *d = opendir("/proc/self/fd"); |
| 199 | int n = 0; |
| 200 | struct dirent *e; |
| 201 | while ((e = readdir(d))) if (e->d_name[0] != '.') n++; |
| 202 | closedir(d); |
| 203 | return n - 1; /* the DIR itself */ |
| 204 | } |
| 205 | |
| 206 | static void t_begin_env(T *t, const char *path, char *const *env) |
| 207 | { |
| 208 | osh_session_init(&t->s, env); |
| 209 | osh_var_export(&t->s, "PATH", path); |
| 210 | snprintf(t->op, sizeof t->op, "%s/cap.out", g_tmp); |
| 211 | snprintf(t->ep, sizeof t->ep, "%s/cap.err", g_tmp); |
| 212 | t->ofd = open(t->op, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); |
| 213 | t->efd = open(t->ep, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC, 0600); |
| 214 | t->s.fd[1] = t->ofd; |
| 215 | t->s.fd[2] = t->efd; |
| 216 | t->s.binding.valid = 1; |
| 217 | t->s.binding.domain = 2; |
| 218 | t->s.binding.cap_generation = 1; |
| 219 | } |
| 220 | |
| 221 | static void t_begin(T *t, const char *path) |
| 222 | { |
| 223 | char *env[] = {"HOME=/nonexistent-home", "LANG=C", NULL}; |
| 224 | t_begin_env(t, path, env); |
| 225 | } |
| 226 | |
| 227 | static void t_end(T *t) |
| 228 | { |
| 229 | close(t->ofd); |
| 230 | close(t->efd); |
| 231 | osh_session_free(&t->s); |
| 232 | } |
| 233 | |
| 234 | static char *t_out(T *t) { return slurp(t->op); } |
| 235 | static char *t_err(T *t) { return slurp(t->ep); } |
| 236 | |
| 237 | static void t_clear(T *t) |
| 238 | { |
| 239 | if (ftruncate(t->ofd, 0)) {} |
| 240 | if (ftruncate(t->efd, 0)) {} |
| 241 | lseek(t->ofd, 0, SEEK_SET); |
| 242 | lseek(t->efd, 0, SEEK_SET); |
| 243 | } |
| 244 | |
| 245 | /* build a command: cmd(b, builtin_id, "a", "b", NULL) */ |
| 246 | static void cmd(OshBuilder *b, int bi, ...) |
| 247 | { |
| 248 | osh_rb_cmd(b, bi); |
| 249 | va_list ap; |
| 250 | va_start(ap, bi); |
| 251 | for (const char *a; (a = va_arg(ap, const char *));) osh_rb_arg(b, a); |
| 252 | va_end(ap); |
| 253 | } |
| 254 | |
| 255 | static void helper(OshBuilder *b, ...) /* a command that runs this binary as a helper */ |
| 256 | { |
| 257 | osh_rb_cmd(b, 0); |
| 258 | osh_rb_arg(b, g_self); |
| 259 | osh_rb_arg(b, "--helper"); |
| 260 | va_list ap; |
| 261 | va_start(ap, b); |
| 262 | for (const char *a; (a = va_arg(ap, const char *));) osh_rb_arg(b, a); |
| 263 | va_end(ap); |
| 264 | } |
| 265 | |
| 266 | static int go(T *t, OshBuilder *b, OshResult *res) |
| 267 | { |
| 268 | osh_rb_seal(b); |
| 269 | CHECK(b->err == 0, "builder error %d", b->err); |
| 270 | return osh_exec_record(&t->s, b->rec, osh_rb_cells(b), b->out, b->out_used, res); |
| 271 | } |
| 272 | |
| 273 | static OshBuilder *nb(unsigned flags, int conn) |
| 274 | { |
| 275 | static OshBuilder b; |
| 276 | osh_rb_init(&b, flags, conn); |
| 277 | return &b; |
| 278 | } |
| 279 | |
| 280 | static double now(void) |
| 281 | { |
| 282 | struct timespec ts; |
| 283 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 284 | return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; |
| 285 | } |
| 286 | |
| 287 | static int rm_cb(const char *p, const struct stat *sb, int tf, struct FTW *w) |
| 288 | { |
| 289 | (void)sb; (void)tf; (void)w; |
| 290 | return remove(p); |
| 291 | } |
| 292 | |
| 293 | /* ================= tests ================= */ |
| 294 | |
| 295 | static void test_simple_and_path(void) |
| 296 | { |
| 297 | T t; |
| 298 | t_begin(&t, "/nonexistent-dir:/usr/bin:/bin"); |
| 299 | OshResult r; |
| 300 | OshBuilder *b = nb(0, 0); |
| 301 | cmd(b, 0, "printf", "%s\n", "via-path", NULL); |
| 302 | CHECK(go(&t, b, &r) == 0 && r.cmd[0].outcome == OSH_OUT_COMPLETED, "status %d", r.status); |
| 303 | char *o = t_out(&t); |
| 304 | CHECK(strcmp(o, "via-path\n") == 0, "got '%s'", o); |
| 305 | free(o); |
| 306 | CHECK(t.s.last_status == 0, "last_status"); |
| 307 | |
| 308 | t_clear(&t); |
| 309 | b = nb(0, 0); |
| 310 | cmd(b, 0, "definitely-not-a-command-xyz", NULL); |
| 311 | CHECK(go(&t, b, &r) == 127 && r.cmd[0].outcome == OSH_OUT_FAILED_NO_EFFECT && r.cmd[0].pid == 0, "PATH miss -> 127"); |
| 312 | char *e = t_err(&t); |
| 313 | CHECK(strstr(e, "not found") != NULL, "diag '%s'", e); |
| 314 | free(e); |
| 315 | |
| 316 | /* non-executable file: 126, found before a later dir that has an executable of that name? (first usable wins) */ |
| 317 | mkdir(P("bin1"), 0700); |
| 318 | mkdir(P("bin2"), 0700); |
| 319 | wfile(P("bin1/tool"), "#!/bin/true\n", 0644); |
| 320 | CHECK(osh_var_set(&t.s, "PATH", P("bin1")) == 0, "set PATH"); |
| 321 | b = nb(0, 0); |
| 322 | cmd(b, 0, "tool", NULL); |
| 323 | CHECK(go(&t, b, &r) == 126, "non-exec -> 126, got %d", r.status); |
| 324 | b = nb(0, 0); |
| 325 | cmd(b, 0, P("bin1/tool"), NULL); |
| 326 | CHECK(go(&t, b, &r) == 126, "path with slash non-exec -> 126"); |
| 327 | b = nb(0, 0); |
| 328 | cmd(b, 0, P("bin1"), NULL); |
| 329 | CHECK(go(&t, b, &r) == 126, "directory -> 126"); |
| 330 | b = nb(0, 0); |
| 331 | cmd(b, 0, P("bin1/absent"), NULL); |
| 332 | CHECK(go(&t, b, &r) == 127, "absent path with slash -> 127"); |
| 333 | |
| 334 | /* a non-exec file in an earlier dir does not hide an executable in a later one (bash/dash behaviour) */ |
| 335 | { |
| 336 | int in = open("/bin/true", O_RDONLY), out = open(P("bin2/tool"), O_WRONLY | O_CREAT | O_TRUNC, 0755); |
| 337 | char buf[65536]; ssize_t n; |
| 338 | while (in >= 0 && out >= 0 && (n = read(in, buf, sizeof buf)) > 0) if (write(out, buf, (size_t)n) < 0) break; |
| 339 | if (in >= 0) close(in); |
| 340 | if (out >= 0) close(out); |
| 341 | chmod(P("bin2/tool"), 0755); |
| 342 | } |
| 343 | char pp[9000]; |
| 344 | snprintf(pp, sizeof pp, "%s:%s", P("bin1"), P("bin2")); |
| 345 | osh_var_set(&t.s, "PATH", pp); |
| 346 | b = nb(0, 0); |
| 347 | cmd(b, 0, "tool", NULL); |
| 348 | CHECK(go(&t, b, &r) == 0, "later executable found past a non-exec, got %d", r.status); |
| 349 | |
| 350 | /* a name containing '/' is never searched: bin2 is on PATH but "bin2/tool" relative to cwd is not found there */ |
| 351 | osh_var_set(&t.s, "PATH", g_tmp); |
| 352 | b = nb(0, 0); |
| 353 | cmd(b, 0, "bin2/tool", NULL); |
| 354 | char *cwd0 = getcwd(NULL, 0); |
| 355 | CHECK(chdir("/") == 0, "chdir /"); |
| 356 | CHECK(go(&t, b, &r) == 127, "slash names are not PATH-searched, got %d", r.status); |
| 357 | |
| 358 | /* empty PATH element = cwd (POSIX); also leading, trailing and "::" */ |
| 359 | CHECK(chdir(P("bin2")) == 0, "chdir bin2"); |
| 360 | const char *empties[] = {":/nonexistent", "/nonexistent:", "/nonexistent::/nonexistent2", ""}; |
| 361 | for (int i = 0; i < 4; i++) { |
| 362 | osh_var_set(&t.s, "PATH", empties[i]); |
| 363 | b = nb(0, 0); |
| 364 | cmd(b, 0, "tool", NULL); |
| 365 | CHECK(go(&t, b, &r) == 0, "empty PATH element means cwd: '%s' -> %d", empties[i], r.status); |
| 366 | } |
| 367 | /* PATH unset: bare names not found, no fallback to the process environment */ |
| 368 | osh_var_unset(&t.s, "PATH"); |
| 369 | b = nb(0, 0); |
| 370 | cmd(b, 0, "tool", NULL); |
| 371 | CHECK(go(&t, b, &r) == 127, "PATH unset -> 127, got %d", r.status); |
| 372 | /* a prefix assignment PATH=... applies to the lookup of that command */ |
| 373 | b = nb(0, 0); |
| 374 | cmd(b, 0, "tool", NULL); |
| 375 | osh_rb_assign(b, "PATH", "."); |
| 376 | CHECK(go(&t, b, &r) == 0, "PATH= prefix assignment used for lookup, got %d", r.status); |
| 377 | CHECK(chdir(cwd0) == 0, "chdir back"); |
| 378 | free(cwd0); |
| 379 | t_end(&t); |
| 380 | } |
| 381 | |
| 382 | static void test_builtins(void) |
| 383 | { |
| 384 | T t; |
| 385 | t_begin(&t, "/usr/bin:/bin"); |
| 386 | mkdir(P("d1"), 0700); |
| 387 | mkdir(P("d1/sub"), 0700); |
| 388 | OshResult r; |
| 389 | char *cwd0 = getcwd(NULL, 0); |
| 390 | osh_var_set(&t.s, "PWD", cwd0); |
| 391 | |
| 392 | /* cd with explicit dir, PWD/OLDPWD, pwd logical */ |
| 393 | OshBuilder *b = nb(0, 0); |
| 394 | cmd(b, OSH_B_CD, "cd", P("d1"), NULL); |
| 395 | CHECK(go(&t, b, &r) == 0, "cd ok"); |
| 396 | CHECK(osh_var_get(&t.s, "PWD") && strcmp(osh_var_get(&t.s, "PWD"), P("d1")) == 0, "PWD=%s", osh_var_get(&t.s, "PWD")); |
| 397 | CHECK(osh_var_get(&t.s, "OLDPWD") && strcmp(osh_var_get(&t.s, "OLDPWD"), cwd0) == 0, "OLDPWD"); |
| 398 | { /* bash exports OLDPWD, so a child sees the previous directory */ |
| 399 | t_clear(&t); |
| 400 | OshBuilder *b2 = nb(0, 0); |
| 401 | helper(b2, "env", "OLDPWD", NULL); |
| 402 | go(&t, b2, &r); |
| 403 | char *o2 = t_out(&t), w2[PATH_MAX + 2]; |
| 404 | snprintf(w2, sizeof w2, "%s\n", cwd0); |
| 405 | CHECK(strcmp(o2, w2) == 0, "OLDPWD reaches children, got '%s'", o2); |
| 406 | free(o2); |
| 407 | t_clear(&t); |
| 408 | } |
| 409 | b = nb(0, 0); |
| 410 | cmd(b, OSH_B_CD, "cd", "sub/../sub/.", NULL); |
| 411 | CHECK(go(&t, b, &r) == 0, "relative cd"); |
| 412 | CHECK(strcmp(osh_var_get(&t.s, "PWD"), P("d1/sub")) == 0, "lexical PWD got %s", osh_var_get(&t.s, "PWD")); |
| 413 | b = nb(0, 0); |
| 414 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 415 | CHECK(go(&t, b, &r) == 0, "pwd"); |
| 416 | char *o = t_out(&t); |
| 417 | char want[4300]; |
| 418 | snprintf(want, sizeof want, "%s\n", P("d1/sub")); |
| 419 | CHECK(strcmp(o, want) == 0, "pwd got '%s'", o); |
| 420 | free(o); |
| 421 | char *cw = getcwd(NULL, 0); |
| 422 | CHECK(strcmp(cw, P("d1/sub")) == 0 || strstr(cw, "d1/sub"), "process cwd moved: %s", cw); |
| 423 | free(cw); |
| 424 | |
| 425 | /* logical pwd through a symlink */ |
| 426 | if (symlink(P("d1"), P("link"))) g_fail++; |
| 427 | b = nb(0, 0); |
| 428 | cmd(b, OSH_B_CD, "cd", P("link"), NULL); |
| 429 | go(&t, b, &r); |
| 430 | t_clear(&t); |
| 431 | b = nb(0, 0); |
| 432 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 433 | go(&t, b, &r); |
| 434 | o = t_out(&t); |
| 435 | snprintf(want, sizeof want, "%s\n", P("link")); |
| 436 | CHECK(strcmp(o, want) == 0, "pwd is logical through symlink, got '%s'", o); |
| 437 | free(o); |
| 438 | t_clear(&t); |
| 439 | b = nb(0, 0); |
| 440 | cmd(b, OSH_B_PWD, "pwd", "-P", NULL); |
| 441 | go(&t, b, &r); |
| 442 | o = t_out(&t); |
| 443 | CHECK(strstr(o, "d1\n") != NULL && strstr(o, "link") == NULL, "pwd -P physical, got '%s'", o); |
| 444 | free(o); |
| 445 | |
| 446 | /* cd with HOME default, cd -, errors */ |
| 447 | osh_var_set(&t.s, "HOME", P("d1/sub")); |
| 448 | b = nb(0, 0); |
| 449 | cmd(b, OSH_B_CD, "cd", P("d1"), NULL); |
| 450 | go(&t, b, &r); |
| 451 | b = nb(0, 0); |
| 452 | cmd(b, OSH_B_CD, "cd", NULL); |
| 453 | CHECK(go(&t, b, &r) == 0 && strcmp(osh_var_get(&t.s, "PWD"), P("d1/sub")) == 0, "cd with no arg goes to HOME"); |
| 454 | t_clear(&t); |
| 455 | b = nb(0, 0); |
| 456 | cmd(b, OSH_B_CD, "cd", "-", NULL); |
| 457 | CHECK(go(&t, b, &r) == 0 && strcmp(osh_var_get(&t.s, "PWD"), P("d1")) == 0, "cd - returns"); |
| 458 | o = t_out(&t); |
| 459 | snprintf(want, sizeof want, "%s\n", P("d1")); |
| 460 | CHECK(strcmp(o, want) == 0, "cd - prints the directory, got '%s'", o); |
| 461 | free(o); |
| 462 | osh_var_unset(&t.s, "HOME"); |
| 463 | b = nb(0, 0); |
| 464 | cmd(b, OSH_B_CD, "cd", NULL); |
| 465 | CHECK(go(&t, b, &r) == 1, "cd without HOME fails"); |
| 466 | b = nb(0, 0); |
| 467 | cmd(b, OSH_B_CD, "cd", P("absent-dir"), NULL); |
| 468 | CHECK(go(&t, b, &r) == 1 && strcmp(osh_var_get(&t.s, "PWD"), P("d1")) == 0, "failed cd keeps PWD"); |
| 469 | |
| 470 | /* omega#344 RISK-3 and NITs: cd -L, -P, --, invalid option, cd "", CDPATH refusal */ |
| 471 | b = nb(0, 0); |
| 472 | cmd(b, OSH_B_CD, "cd", "-L", P("d1/sub"), NULL); |
| 473 | CHECK(go(&t, b, &r) == 0 && strcmp(osh_var_get(&t.s, "PWD"), P("d1/sub")) == 0, "cd -L DIR"); |
| 474 | b = nb(0, 0); |
| 475 | cmd(b, OSH_B_CD, "cd", "--", P("d1"), NULL); |
| 476 | CHECK(go(&t, b, &r) == 0 && strcmp(osh_var_get(&t.s, "PWD"), P("d1")) == 0, "cd -- DIR"); |
| 477 | b = nb(0, 0); |
| 478 | cmd(b, OSH_B_CD, "cd", "-P", P("d1/sub"), NULL); |
| 479 | { |
| 480 | char phys[PATH_MAX]; |
| 481 | CHECK(go(&t, b, &r) == 0 && getcwd(phys, sizeof phys) && strcmp(osh_var_get(&t.s, "PWD"), phys) == 0, "cd -P sets PWD to the physical directory"); |
| 482 | } |
| 483 | t_clear(&t); |
| 484 | b = nb(0, 0); |
| 485 | cmd(b, OSH_B_CD, "cd", "-x", P("d1"), NULL); |
| 486 | CHECK(go(&t, b, &r) == 2, "cd -x is an invalid option, status 2"); |
| 487 | o = t_err(&t); |
| 488 | CHECK(strstr(o, "cd: -x: invalid option") != NULL, "cd -x diagnostic, got '%s'", o); |
| 489 | free(o); |
| 490 | { |
| 491 | char keep[PATH_MAX]; |
| 492 | snprintf(keep, sizeof keep, "%s", osh_var_get(&t.s, "PWD")); |
| 493 | b = nb(0, 0); |
| 494 | cmd(b, OSH_B_CD, "cd", "", NULL); |
| 495 | CHECK(go(&t, b, &r) == 0 && strcmp(osh_var_get(&t.s, "PWD"), keep) == 0, "cd \"\" is a no-op with status 0"); |
| 496 | } |
| 497 | b = nb(0, 0); |
| 498 | cmd(b, OSH_B_CD, "cd", P("d1"), NULL); |
| 499 | go(&t, b, &r); |
| 500 | osh_var_set(&t.s, "CDPATH", P("absent-dir")); |
| 501 | t_clear(&t); |
| 502 | b = nb(0, 0); |
| 503 | cmd(b, OSH_B_CD, "cd", "sub", NULL); |
| 504 | CHECK(go(&t, b, &r) == 1 && strcmp(osh_var_get(&t.s, "PWD"), P("d1")) == 0, "relative cd refused while CDPATH is set"); |
| 505 | o = t_err(&t); |
| 506 | CHECK(strstr(o, "CDPATH is set") != NULL, "CDPATH diagnostic, got '%s'", o); |
| 507 | free(o); |
| 508 | b = nb(0, 0); |
| 509 | cmd(b, OSH_B_CD, "cd", "./sub", NULL); |
| 510 | CHECK(go(&t, b, &r) == 0 && strcmp(osh_var_get(&t.s, "PWD"), P("d1/sub")) == 0, "anchored ./sub still works with CDPATH set"); |
| 511 | osh_var_unset(&t.s, "CDPATH"); |
| 512 | b = nb(0, 0); |
| 513 | cmd(b, OSH_B_CD, "cd", P("d1"), NULL); |
| 514 | go(&t, b, &r); |
| 515 | |
| 516 | /* omega#344 NIT: bash 5.2 returns 0 and says nothing for unset of a non-identifier */ |
| 517 | t_clear(&t); |
| 518 | b = nb(0, 0); |
| 519 | cmd(b, OSH_B_UNSET, "unset", "1x", NULL); |
| 520 | CHECK(go(&t, b, &r) == 0, "unset 1x succeeds as in bash"); |
| 521 | o = t_err(&t); |
| 522 | CHECK(o[0] == 0, "unset 1x is silent, got '%s'", o); |
| 523 | free(o); |
| 524 | |
| 525 | /* omega#344 RISK-8: a member killed by a signal gets a message, except SIGINT and SIGPIPE */ |
| 526 | t_clear(&t); |
| 527 | b = nb(0, 0); |
| 528 | helper(b, "die", "15", NULL); |
| 529 | CHECK(go(&t, b, &r) == 143 && r.cmd[0].termsig == SIGTERM, "SIGTERM death is status 143, got %d", r.status); |
| 530 | o = t_err(&t); |
| 531 | CHECK(strstr(o, "Terminated (signal 15)") != NULL, "Terminated message, got '%s'", o); |
| 532 | free(o); |
| 533 | t_clear(&t); |
| 534 | b = nb(0, 0); |
| 535 | helper(b, "die", "13", NULL); |
| 536 | CHECK(go(&t, b, &r) == 141, "SIGPIPE death is status 141, got %d", r.status); |
| 537 | o = t_err(&t); |
| 538 | CHECK(o[0] == 0, "SIGPIPE death stays silent, got '%s'", o); |
| 539 | free(o); |
| 540 | |
| 541 | /* export / unset in parent; exported reaches children, unexported does not */ |
| 542 | b = nb(0, 0); |
| 543 | cmd(b, OSH_B_EXPORT, "export", "XA=1", "XB", NULL); |
| 544 | CHECK(go(&t, b, &r) == 0, "export"); |
| 545 | osh_var_set(&t.s, "XC", "3"); /* shell variable, not exported */ |
| 546 | t_clear(&t); |
| 547 | b = nb(0, 0); |
| 548 | helper(b, "env", "XA", NULL); |
| 549 | go(&t, b, &r); |
| 550 | b = nb(0, 0); |
| 551 | helper(b, "env", "XC", NULL); |
| 552 | go(&t, b, &r); |
| 553 | b = nb(0, 0); |
| 554 | helper(b, "env", "XB", NULL); |
| 555 | go(&t, b, &r); |
| 556 | o = t_out(&t); |
| 557 | CHECK(strcmp(o, "1\n<unset>\n<unset>\n") == 0, "export visibility got '%s'", o); |
| 558 | free(o); |
| 559 | b = nb(0, 0); |
| 560 | cmd(b, OSH_B_EXPORT, "export", "XC", NULL); /* now export an existing shell var */ |
| 561 | go(&t, b, &r); |
| 562 | t_clear(&t); |
| 563 | b = nb(0, 0); |
| 564 | helper(b, "env", "XC", NULL); |
| 565 | go(&t, b, &r); |
| 566 | o = t_out(&t); |
| 567 | CHECK(strcmp(o, "3\n") == 0, "exported later, got '%s'", o); |
| 568 | free(o); |
| 569 | b = nb(0, 0); |
| 570 | cmd(b, OSH_B_EXPORT, "export", "1bad", NULL); |
| 571 | CHECK(go(&t, b, &r) == 1, "export invalid identifier"); |
| 572 | b = nb(0, 0); |
| 573 | cmd(b, OSH_B_UNSET, "unset", "XA", NULL); |
| 574 | CHECK(go(&t, b, &r) == 0 && osh_var_get(&t.s, "XA") == NULL, "unset"); |
| 575 | t_clear(&t); |
| 576 | b = nb(0, 0); |
| 577 | helper(b, "env", "XA", NULL); |
| 578 | go(&t, b, &r); |
| 579 | o = t_out(&t); |
| 580 | CHECK(strcmp(o, "<unset>\n") == 0, "unset reaches children, got '%s'", o); |
| 581 | free(o); |
| 582 | |
| 583 | /* export -p / -n / -f / bad option, measured against bash 5.2.21 (#344 RISK-6) */ |
| 584 | osh_var_export(&t.s, "XQ", "a$b\"c\\d`e"); /* double-quote form escapes \" \\ $ ` */ |
| 585 | osh_var_export(&t.s, "XT", "x\ty'z\001\177\200"); /* control or non-ASCII bytes: $'...' form */ |
| 586 | osh_var_export(&t.s, "XE", ""); |
| 587 | t_clear(&t); |
| 588 | b = nb(0, 0); |
| 589 | cmd(b, OSH_B_EXPORT, "export", "-p", NULL); |
| 590 | CHECK(go(&t, b, &r) == 0, "export -p status"); |
| 591 | o = t_out(&t); |
| 592 | CHECK(strstr(o, "declare -x XB\n") && strstr(o, "declare -x XC=\"3\"\n") && !strstr(o, "export "), "export -p uses declare -x lines, got '%s'", o); |
| 593 | CHECK(strstr(o, "declare -x XQ=\"a\\$b\\\"c\\\\d\\`e\"\n") != NULL, "export -p double-quote escaping, got '%s'", o); |
| 594 | CHECK(strstr(o, "declare -x XT=$'x\\ty\\'z\\001\\177\\200'\n") != NULL, "export -p $'...' form for control bytes, got '%s'", o); |
| 595 | CHECK(strstr(o, "declare -x XE=\"\"\n") != NULL, "export -p empty value, got '%s'", o); |
| 596 | free(o); |
| 597 | t_clear(&t); |
| 598 | b = nb(0, 0); |
| 599 | cmd(b, OSH_B_EXPORT, "export", "-pn", NULL); /* options with no names still list */ |
| 600 | CHECK(go(&t, b, &r) == 0, "export -pn status"); |
| 601 | o = t_out(&t); |
| 602 | CHECK(strstr(o, "declare -x XC=\"3\"\n") != NULL, "export -pn lists, got '%s'", o); |
| 603 | free(o); |
| 604 | t_clear(&t); |
| 605 | b = nb(0, 0); |
| 606 | cmd(b, OSH_B_EXPORT, "export", "-n", "XC", "XN=7", "NOPE", NULL); /* -n keeps the variable, drops the export mark; assigns first */ |
| 607 | CHECK(go(&t, b, &r) == 0 && osh_var_get(&t.s, "XC") && strcmp(osh_var_get(&t.s, "XC"), "3") == 0, "export -n keeps value"); |
| 608 | CHECK(osh_var_get(&t.s, "XN") && strcmp(osh_var_get(&t.s, "XN"), "7") == 0, "export -n NAME=value assigns"); |
| 609 | t_clear(&t); |
| 610 | b = nb(0, 0); |
| 611 | helper(b, "env", "XC", NULL); |
| 612 | go(&t, b, &r); |
| 613 | b = nb(0, 0); |
| 614 | helper(b, "env", "XN", NULL); |
| 615 | go(&t, b, &r); |
| 616 | o = t_out(&t); |
| 617 | CHECK(strcmp(o, "<unset>\n<unset>\n") == 0, "export -n: children no longer see them, got '%s'", o); |
| 618 | free(o); |
| 619 | t_clear(&t); |
| 620 | b = nb(0, 0); |
| 621 | cmd(b, OSH_B_EXPORT, "export", "-p", "XC", NULL); /* -p with names is a plain export, prints nothing */ |
| 622 | CHECK(go(&t, b, &r) == 0, "export -p NAME status"); |
| 623 | o = t_out(&t); |
| 624 | CHECK(*o == 0, "export -p NAME prints nothing, got '%s'", o); |
| 625 | free(o); |
| 626 | t_clear(&t); |
| 627 | b = nb(0, 0); |
| 628 | helper(b, "env", "XC", NULL); |
| 629 | go(&t, b, &r); |
| 630 | o = t_out(&t); |
| 631 | CHECK(strcmp(o, "3\n") == 0, "export -p NAME re-exported, got '%s'", o); |
| 632 | free(o); |
| 633 | t_clear(&t); |
| 634 | b = nb(0, 0); |
| 635 | cmd(b, OSH_B_EXPORT, "export", "-q", "XC", NULL); |
| 636 | CHECK(go(&t, b, &r) == 2, "export bad option status 2"); |
| 637 | o = t_err(&t); |
| 638 | CHECK(strstr(o, "export: -q: invalid option") && strstr(o, "export: usage: export [-fn] [name[=value] ...] or export -p"), "export bad option diagnostic, got '%s'", o); |
| 639 | free(o); |
| 640 | t_clear(&t); |
| 641 | b = nb(0, 0); |
| 642 | cmd(b, OSH_B_EXPORT, "export", "-f", "zz", NULL); /* osh has no functions */ |
| 643 | CHECK(go(&t, b, &r) == 1, "export -f status 1"); |
| 644 | o = t_err(&t); |
| 645 | CHECK(strstr(o, "export: zz: not a function") != NULL, "export -f diagnostic, got '%s'", o); |
| 646 | free(o); |
| 647 | t_clear(&t); |
| 648 | b = nb(0, 0); |
| 649 | cmd(b, OSH_B_EXPORT, "export", "-", NULL); |
| 650 | CHECK(go(&t, b, &r) == 1, "export - is an invalid identifier"); |
| 651 | b = nb(0, 0); |
| 652 | cmd(b, OSH_B_EXPORT, "export", "--", "-n", NULL); |
| 653 | CHECK(go(&t, b, &r) == 1 && osh_var_get(&t.s, "XN") && strcmp(osh_var_get(&t.s, "XN"), "7") == 0, "export -- -n: -n is a (bad) name, not an option"); |
| 654 | t_clear(&t); |
| 655 | |
| 656 | /* exit: parent only when alone */ |
| 657 | |
| 658 | b = nb(0, 0); |
| 659 | cmd(b, OSH_B_EXIT, "exit", "7", NULL); |
| 660 | CHECK(go(&t, b, &r) == 7 && t.s.exit_requested && t.s.exit_status == 7 && r.exit_requested, "exit 7 in parent"); |
| 661 | t.s.exit_requested = 0; |
| 662 | t.s.last_status = 5; |
| 663 | b = nb(0, 0); |
| 664 | cmd(b, OSH_B_EXIT, "exit", NULL); |
| 665 | CHECK(go(&t, b, &r) == 5 && t.s.exit_status == 5, "exit with no arg uses $?"); |
| 666 | t.s.exit_requested = 0; |
| 667 | b = nb(0, 0); |
| 668 | cmd(b, OSH_B_EXIT, "exit", "abc", NULL); |
| 669 | CHECK(go(&t, b, &r) == 2, "exit non-numeric -> 2"); |
| 670 | t.s.exit_requested = 0; |
| 671 | |
| 672 | /* inside a pipeline builtins run in a child: parent state is unchanged */ |
| 673 | const char *before_pwd = strdup(osh_var_get(&t.s, "PWD")); |
| 674 | char *cw0 = getcwd(NULL, 0); |
| 675 | t_clear(&t); |
| 676 | b = nb(0, 0); |
| 677 | cmd(b, OSH_B_CD, "cd", P("d1/sub"), NULL); |
| 678 | helper(b, "cat", NULL); |
| 679 | t.s.fd[0] = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 680 | CHECK(go(&t, b, &r) == 0, "cd | cat"); |
| 681 | char *cw1 = getcwd(NULL, 0); |
| 682 | CHECK(strcmp(cw0, cw1) == 0 && strcmp(osh_var_get(&t.s, "PWD"), before_pwd) == 0, "cd in pipeline does not move the parent"); |
| 683 | free(cw0); free(cw1); |
| 684 | b = nb(0, 0); |
| 685 | cmd(b, OSH_B_EXPORT, "export", "PIPEVAR=1", NULL); |
| 686 | helper(b, "cat", NULL); |
| 687 | go(&t, b, &r); |
| 688 | CHECK(osh_var_get(&t.s, "PIPEVAR") == NULL, "export in pipeline does not touch parent"); |
| 689 | b = nb(0, 0); |
| 690 | cmd(b, OSH_B_UNSET, "unset", "XC", NULL); |
| 691 | helper(b, "cat", NULL); |
| 692 | go(&t, b, &r); |
| 693 | CHECK(osh_var_get(&t.s, "XC") != NULL, "unset in pipeline does not touch parent"); |
| 694 | b = nb(0, 0); |
| 695 | cmd(b, OSH_B_EXIT, "exit", "9", NULL); |
| 696 | helper(b, "cat", NULL); |
| 697 | int st = go(&t, b, &r); |
| 698 | CHECK(!t.s.exit_requested && st == 0 && r.cmd[0].status == 9, "exit in pipeline only ends its child (cmd0 status %d)", r.cmd[0].status); |
| 699 | /* last command a builtin in a pipeline: its status is the pipeline status, output goes through the pipe path */ |
| 700 | t_clear(&t); |
| 701 | b = nb(0, 0); |
| 702 | helper(b, "echo", "x", NULL); |
| 703 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 704 | CHECK(go(&t, b, &r) == 0, "echo | pwd"); |
| 705 | o = t_out(&t); |
| 706 | CHECK(o[0] == '/', "pwd inside pipeline wrote to stdout '%s'", o); |
| 707 | free(o); |
| 708 | close(t.s.fd[0]); |
| 709 | t.s.fd[0] = 0; |
| 710 | |
| 711 | /* standalone assignment: shell variable, not exported; with argv: per command environment only */ |
| 712 | b = nb(0, 0); |
| 713 | osh_rb_cmd(b, 0); |
| 714 | osh_rb_assign(b, "SA", "one"); |
| 715 | CHECK(go(&t, b, &r) == 0 && strcmp(osh_var_get(&t.s, "SA"), "one") == 0, "standalone assignment sets shell var"); |
| 716 | t_clear(&t); |
| 717 | b = nb(0, 0); |
| 718 | helper(b, "env", "SA", NULL); |
| 719 | go(&t, b, &r); |
| 720 | o = t_out(&t); |
| 721 | CHECK(strcmp(o, "<unset>\n") == 0, "standalone assignment is not exported, got '%s'", o); |
| 722 | free(o); |
| 723 | t_clear(&t); |
| 724 | b = nb(0, 0); |
| 725 | helper(b, "env", "PA", NULL); |
| 726 | osh_rb_assign(b, "PA", "temp"); |
| 727 | go(&t, b, &r); |
| 728 | o = t_out(&t); |
| 729 | CHECK(strcmp(o, "temp\n") == 0 && osh_var_get(&t.s, "PA") == NULL, "prefix assignment is per-command, got '%s'", o); |
| 730 | free(o); |
| 731 | t_clear(&t); |
| 732 | b = nb(0, 0); |
| 733 | helper(b, "env", "SA", NULL); |
| 734 | osh_rb_assign(b, "SA", "over"); /* overrides, and does not change the shell variable */ |
| 735 | go(&t, b, &r); |
| 736 | o = t_out(&t); |
| 737 | CHECK(strcmp(o, "over\n") == 0 && strcmp(osh_var_get(&t.s, "SA"), "one") == 0, "prefix over shell var '%s'", o); |
| 738 | free(o); |
| 739 | /* env is built fresh: exactly the exported table plus the override plus _ (omega#344), nothing from the test process environ */ |
| 740 | t_clear(&t); |
| 741 | int nexp = 1; |
| 742 | for (size_t i = 0; i < t.s.nvars; i++) nexp += t.s.vars[i].exported && t.s.vars[i].value; |
| 743 | setenv("LEAK_FROM_PROCESS_ENV", "1", 1); |
| 744 | b = nb(0, 0); |
| 745 | helper(b, "envcount", NULL); |
| 746 | go(&t, b, &r); |
| 747 | o = t_out(&t); |
| 748 | CHECK(atoi(o) == nexp, "child env has %d entries, expected %d", atoi(o), nexp); |
| 749 | free(o); |
| 750 | unsetenv("LEAK_FROM_PROCESS_ENV"); |
| 751 | |
| 752 | CHECK(chdir(cwd0) == 0, "restore cwd"); |
| 753 | free(cwd0); |
| 754 | free((void *)before_pwd); |
| 755 | t_end(&t); |
| 756 | } |
| 757 | |
| 758 | static void printf_case(T *t, const char *fmt, const char *a1, const char *a2, const char *a3, const char *a4, const char *a5, const char *a6) |
| 759 | { |
| 760 | OshResult r; |
| 761 | const char *args[6] = {a1, a2, a3, a4, a5, a6}; |
| 762 | char *res[2]; |
| 763 | int st[2]; |
| 764 | for (int k = 0; k < 2; k++) { |
| 765 | t_clear(t); |
| 766 | OshBuilder *b = nb(0, 0); |
| 767 | if (k == 0) cmd(b, OSH_B_PRINTF, "printf", fmt, NULL); |
| 768 | else cmd(b, 0, "/usr/bin/printf", fmt, NULL); |
| 769 | for (int i = 0; i < 6 && args[i]; i++) osh_rb_arg(b, args[i]); |
| 770 | st[k] = go(t, b, &r); |
| 771 | res[k] = t_out(t); |
| 772 | } |
| 773 | CHECK(strcmp(res[0], res[1]) == 0 && st[0] == st[1], "printf '%s': builtin '%s' (%d) vs /usr/bin/printf '%s' (%d)", fmt, res[0], st[0], res[1], st[1]); |
| 774 | free(res[0]); |
| 775 | free(res[1]); |
| 776 | } |
| 777 | |
| 778 | static void test_printf(void) |
| 779 | { |
| 780 | T t; |
| 781 | t_begin(&t, "/usr/bin:/bin"); |
| 782 | printf_case(&t, "%s\n", "hello", NULL, NULL, NULL, NULL, NULL); |
| 783 | printf_case(&t, "%d %i %u %x %o %c|%%\n", "42", "-7", "7", "255", "8", "xyz"); |
| 784 | printf_case(&t, "%s %s\n", "a", "b", "c", "d", NULL, NULL); |
| 785 | printf_case(&t, "%s %s\n", "a", "b", "c", NULL, NULL, NULL); |
| 786 | printf_case(&t, "\\t|\\101|\\\\|\\n|\\a\\b\\f\\r\\v|\\\"|\n", NULL, NULL, NULL, NULL, NULL, NULL); |
| 787 | printf_case(&t, "no conversion\n", "ignored", NULL, NULL, NULL, NULL, NULL); |
| 788 | printf_case(&t, "%s\n", NULL, NULL, NULL, NULL, NULL, NULL); |
| 789 | printf_case(&t, "%d|%u\n", NULL, NULL, NULL, NULL, NULL, NULL); |
| 790 | printf_case(&t, "%d %d %d\n", "0x1f", "010", "99", NULL, NULL, NULL); |
| 791 | printf_case(&t, "%d\n", "'A", NULL, NULL, NULL, NULL, NULL); |
| 792 | printf_case(&t, "%s\n", "a\\nb", NULL, NULL, NULL, NULL, NULL); |
| 793 | printf_case(&t, "%x %o\n", "3735928559", "511", NULL, NULL, NULL, NULL); |
| 794 | printf_case(&t, "%d\n", "abc", NULL, NULL, NULL, NULL, NULL); |
| 795 | printf_case(&t, "%s", "x", "y", "z", NULL, NULL, NULL); |
| 796 | printf_case(&t, "100%%\n", NULL, NULL, NULL, NULL, NULL, NULL); |
| 797 | printf_case(&t, "\\0101\n", NULL, NULL, NULL, NULL, NULL, NULL); |
| 798 | /* widths, flags, precision, * from arguments, length modifiers, floats, %b and %q, measured against bash 5.2.21 |
| 799 | * (#344 RISK-4). bash pads %s with spaces even under the 0 flag. */ |
| 800 | OshResult r; |
| 801 | t_clear(&t); |
| 802 | OshBuilder *b = nb(0, 0); |
| 803 | cmd(b, OSH_B_PRINTF, "printf", "[%5d][%-4s][%.2s][%05d][%+d][% d][%#x][%#o][%*d][%.*s][%05s][%X][%ld %lld %hd]\n", |
| 804 | "3", "ab", "xyz", "42", "7", "7", "255", "8", "4", "9", "2", "hello", "cd", "255", "1", "2", "3", NULL); |
| 805 | CHECK(go(&t, b, &r) == 0, "widths status"); |
| 806 | char *o = t_out(&t); |
| 807 | CHECK(strcmp(o, "[ 3][ab ][xy][00042][+7][ 7][0xff][010][ 9][he][ cd][FF][1 2 3]\n") == 0, "widths/flags/precision as bash, got '%s'", o); |
| 808 | free(o); |
| 809 | t_clear(&t); |
| 810 | b = nb(0, 0); |
| 811 | cmd(b, OSH_B_PRINTF, "printf", "%5.2f|%e|%g|%08.3f|%G|%.0d|%+.2d|%5c|%-3c|\n", "3.14159", "2.5", "0.0001", "3.14159", "4", "0", "3", "x", "y", NULL); |
| 812 | CHECK(go(&t, b, &r) == 0, "floats status"); |
| 813 | o = t_out(&t); |
| 814 | CHECK(strcmp(o, " 3.14|2.500000e+00|0.0001|0003.142|4||+03| x|y |\n") == 0, "floats and char widths as bash, got '%s'", o); |
| 815 | free(o); |
| 816 | t_clear(&t); |
| 817 | b = nb(0, 0); |
| 818 | cmd(b, OSH_B_PRINTF, "printf", "%b|%5.3b|%-4b|%b|%b\n", "a\\tb\\0101\\x41\\u00e9", "abcdef", "xy", "\\e\\E", "\\q\\\\", NULL); |
| 819 | CHECK(go(&t, b, &r) == 0, "%%b status"); |
| 820 | o = t_out(&t); |
| 821 | CHECK(strcmp(o, "a\tbAA\xc3\xa9| abc|xy |\033\033|\\q\\\n") == 0, "%%b escapes, width and precision as bash, got '%s'", o); |
| 822 | free(o); |
| 823 | t_clear(&t); |
| 824 | b = nb(0, 0); |
| 825 | cmd(b, OSH_B_PRINTF, "printf", "a%bz\n", "x\\cy", NULL); |
| 826 | CHECK(go(&t, b, &r) == 0, "%%b \\c status"); |
| 827 | o = t_out(&t); |
| 828 | CHECK(strcmp(o, "ax") == 0, "%%b \\c stops all output, got '%s'", o); |
| 829 | free(o); |
| 830 | t_clear(&t); |
| 831 | b = nb(0, 0); |
| 832 | cmd(b, OSH_B_PRINTF, "printf", "%q|%q|%q|%q|%q|%q|%q\n", "a b", "it's", "", "#x~", "a\tb", "x#y,z{w}", "~p", NULL); |
| 833 | CHECK(go(&t, b, &r) == 0, "%%q status"); |
| 834 | o = t_out(&t); |
| 835 | CHECK(strcmp(o, "a\\ b|it\\'s|''|\\#x~|$'a\\tb'|x#y\\,z\\{w\\}|\\~p\n") == 0, "%%q quotes as bash, got '%s'", o); |
| 836 | free(o); |
| 837 | t_clear(&t); |
| 838 | b = nb(0, 0); |
| 839 | cmd(b, OSH_B_PRINTF, "printf", "%*d|\n", "x", "5", NULL); /* bad * width: diagnostic, width 0, status 1 */ |
| 840 | CHECK(go(&t, b, &r) == 1, "bad * width status 1"); |
| 841 | o = t_out(&t); |
| 842 | char *e = t_err(&t); |
| 843 | CHECK(strcmp(o, "5|\n") == 0 && strstr(e, "invalid number"), "bad * width prints with width 0, got '%s' / '%s'", o, e); |
| 844 | free(o); |
| 845 | free(e); |
| 846 | t_clear(&t); |
| 847 | b = nb(0, 0); |
| 848 | cmd(b, OSH_B_PRINTF, "printf", "%5%\n", NULL); /* bash: `%': invalid format character */ |
| 849 | CHECK(go(&t, b, &r) == 1, "%%5%% refused"); |
| 850 | o = t_out(&t); |
| 851 | e = t_err(&t); |
| 852 | CHECK(o[0] == 0 && strstr(e, "`%': invalid format character"), "%%5%% nothing printed, bash's diagnostic, got '%s' / '%s'", o, e); |
| 853 | free(o); |
| 854 | free(e); |
| 855 | t_clear(&t); |
| 856 | b = nb(0, 0); |
| 857 | cmd(b, OSH_B_PRINTF, "printf", "%5", "x", NULL); |
| 858 | CHECK(go(&t, b, &r) == 1, "%%5 at the end refused"); |
| 859 | e = t_err(&t); |
| 860 | CHECK(strstr(e, "missing format character") != NULL, "%%5 at the end: bash's diagnostic, got '%s'", e); |
| 861 | free(e); |
| 862 | t_clear(&t); |
| 863 | b = nb(0, 0); |
| 864 | cmd(b, OSH_B_PRINTF, "printf", "%2000000d\n", "1", NULL); /* osh limit, documented in the builtin header */ |
| 865 | CHECK(go(&t, b, &r) == 1, "absurd width refused"); |
| 866 | t_clear(&t); |
| 867 | b = nb(0, 0); |
| 868 | cmd(b, OSH_B_PRINTF, "printf", "a\\qb\n", NULL); |
| 869 | go(&t, b, &r); |
| 870 | o = t_out(&t); |
| 871 | CHECK(strcmp(o, "a\\qb\n") == 0, "unknown escape kept verbatim, got '%s'", o); |
| 872 | free(o); |
| 873 | b = nb(0, 0); |
| 874 | cmd(b, OSH_B_PRINTF, "printf", NULL); |
| 875 | CHECK(go(&t, b, &r) == 2, "printf with no format (bash: 2)"); |
| 876 | t_end(&t); |
| 877 | } |
| 878 | |
| 879 | static void test_redirs(void) |
| 880 | { |
| 881 | T t; |
| 882 | t_begin(&t, "/usr/bin:/bin"); |
| 883 | OshResult r; |
| 884 | OshBuilder *b; |
| 885 | char *o; |
| 886 | |
| 887 | /* > and < */ |
| 888 | b = nb(0, 0); |
| 889 | helper(b, "echo", "hello", NULL); |
| 890 | osh_rb_redir(b, OSH_R_OUT, 1, P("o1"), 0); |
| 891 | CHECK(go(&t, b, &r) == 0, "> file"); |
| 892 | o = slurp(P("o1")); |
| 893 | CHECK(strcmp(o, "hello\n") == 0, "> content '%s'", o); |
| 894 | free(o); |
| 895 | b = nb(0, 0); |
| 896 | helper(b, "cat", NULL); |
| 897 | osh_rb_redir(b, OSH_R_IN, 0, P("o1"), 0); |
| 898 | osh_rb_redir(b, OSH_R_OUT, 1, P("o2"), 0); |
| 899 | CHECK(go(&t, b, &r) == 0, "< file > file"); |
| 900 | o = slurp(P("o2")); |
| 901 | CHECK(strcmp(o, "hello\n") == 0, "< content '%s'", o); |
| 902 | free(o); |
| 903 | /* >> appends, > truncates */ |
| 904 | b = nb(0, 0); |
| 905 | helper(b, "echo", "more", NULL); |
| 906 | osh_rb_redir(b, OSH_R_APPEND, 1, P("o1"), 0); |
| 907 | go(&t, b, &r); |
| 908 | o = slurp(P("o1")); |
| 909 | CHECK(strcmp(o, "hello\nmore\n") == 0, ">> content '%s'", o); |
| 910 | free(o); |
| 911 | b = nb(0, 0); |
| 912 | helper(b, "echo", "new", NULL); |
| 913 | osh_rb_redir(b, OSH_R_OUT, 1, P("o1"), 0); |
| 914 | go(&t, b, &r); |
| 915 | o = slurp(P("o1")); |
| 916 | CHECK(strcmp(o, "new\n") == 0, "> truncates '%s'", o); |
| 917 | free(o); |
| 918 | /* N> and N>> on stderr */ |
| 919 | b = nb(0, 0); |
| 920 | helper(b, "both", NULL); |
| 921 | osh_rb_redir(b, OSH_R_OUT, 2, P("e1"), 0); |
| 922 | go(&t, b, &r); |
| 923 | o = slurp(P("e1")); |
| 924 | CHECK(strcmp(o, "err\n") == 0, "2> content '%s'", o); |
| 925 | free(o); |
| 926 | b = nb(0, 0); |
| 927 | helper(b, "both", NULL); |
| 928 | osh_rb_redir(b, OSH_R_APPEND, 2, P("e1"), 0); |
| 929 | go(&t, b, &r); |
| 930 | o = slurp(P("e1")); |
| 931 | CHECK(strcmp(o, "err\nerr\n") == 0, "2>> content '%s'", o); |
| 932 | free(o); |
| 933 | |
| 934 | /* order: `>o 2>&1` sends both to the file; `2>&1 >o` sends stderr to the OLD stdout (the capture), stdout to file */ |
| 935 | t_clear(&t); |
| 936 | b = nb(0, 0); |
| 937 | helper(b, "both", NULL); |
| 938 | osh_rb_redir(b, OSH_R_OUT, 1, P("o3"), 0); |
| 939 | osh_rb_redir(b, OSH_R_DUP, 2, NULL, 1); |
| 940 | CHECK(go(&t, b, &r) == 0, ">o 2>&1"); |
| 941 | o = slurp(P("o3")); |
| 942 | CHECK(strcmp(o, "out\nerr\n") == 0, ">o 2>&1: file '%s'", o); |
| 943 | free(o); |
| 944 | o = t_out(&t); |
| 945 | CHECK(o[0] == 0, ">o 2>&1: capture empty, got '%s'", o); |
| 946 | free(o); |
| 947 | t_clear(&t); |
| 948 | b = nb(0, 0); |
| 949 | helper(b, "both", NULL); |
| 950 | osh_rb_redir(b, OSH_R_DUP, 2, NULL, 1); |
| 951 | osh_rb_redir(b, OSH_R_OUT, 1, P("o4"), 0); |
| 952 | CHECK(go(&t, b, &r) == 0, "2>&1 >o"); |
| 953 | o = slurp(P("o4")); |
| 954 | CHECK(strcmp(o, "out\n") == 0, "2>&1 >o: file has only stdout '%s'", o); |
| 955 | free(o); |
| 956 | o = t_out(&t); |
| 957 | CHECK(strcmp(o, "err\n") == 0, "2>&1 >o: stderr went to the old stdout '%s'", o); |
| 958 | free(o); |
| 959 | /* N>&M with N != 1: 0<&... and 1>&2 */ |
| 960 | t_clear(&t); |
| 961 | b = nb(0, 0); |
| 962 | helper(b, "echo", "to-stderr", NULL); |
| 963 | osh_rb_redir(b, OSH_R_DUP, 1, NULL, 2); |
| 964 | go(&t, b, &r); |
| 965 | o = t_err(&t); |
| 966 | CHECK(strcmp(o, "to-stderr\n") == 0, "1>&2 '%s'", o); |
| 967 | free(o); |
| 968 | |
| 969 | /* failed redirection: diagnostic, status 1, command not run */ |
| 970 | unlink(P("marker")); |
| 971 | t_clear(&t); |
| 972 | b = nb(0, 0); |
| 973 | helper(b, "touch", P("marker"), NULL); |
| 974 | osh_rb_redir(b, OSH_R_IN, 0, P("no-such-input"), 0); |
| 975 | CHECK(go(&t, b, &r) == 1, "failed < gives 1, got %d", r.status); |
| 976 | CHECK(access(P("marker"), F_OK) != 0, "command did not run after failed redirection"); |
| 977 | o = t_err(&t); |
| 978 | CHECK(strstr(o, "no-such-input") != NULL, "diagnostic names target: '%s'", o); |
| 979 | free(o); |
| 980 | b = nb(0, 0); |
| 981 | helper(b, "touch", P("marker"), NULL); |
| 982 | osh_rb_redir(b, OSH_R_OUT, 1, P("no-dir/x"), 0); |
| 983 | CHECK(go(&t, b, &r) == 1 && access(P("marker"), F_OK) != 0, "failed > into missing dir"); |
| 984 | /* later redirection fails after an earlier one succeeded: earlier file created, no leak, command not run */ |
| 985 | int fds0 = count_fds(); |
| 986 | b = nb(0, 0); |
| 987 | helper(b, "touch", P("marker"), NULL); |
| 988 | osh_rb_redir(b, OSH_R_OUT, 2, P("partial"), 0); |
| 989 | osh_rb_redir(b, OSH_R_IN, 0, P("no-such-input"), 0); |
| 990 | CHECK(go(&t, b, &r) == 1 && access(P("marker"), F_OK) != 0, "second redirection fails"); |
| 991 | CHECK(count_fds() == fds0, "no descriptors leaked in the parent after failed redirections (%d vs %d)", count_fds(), fds0); |
| 992 | /* failure inside a pipeline: the failed command's status is 1; neighbours still finish (reader sees EOF) */ |
| 993 | t_clear(&t); |
| 994 | b = nb(0, 0); |
| 995 | helper(b, "echo", "data", NULL); |
| 996 | helper(b, "cat", NULL); |
| 997 | osh_rb_redir(b, OSH_R_IN, 0, P("no-such-input"), 0); |
| 998 | CHECK(go(&t, b, &r) == 1 && r.cmd[1].status == 1, "pipeline with failed redirect on last command"); |
| 999 | /* builtin with redirection: applied then restored in the parent; the shell's own fds are untouched */ |
| 1000 | t_clear(&t); |
| 1001 | b = nb(0, 0); |
| 1002 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 1003 | osh_rb_redir(b, OSH_R_OUT, 1, P("pwdout"), 0); |
| 1004 | CHECK(go(&t, b, &r) == 0, "pwd > file"); |
| 1005 | o = slurp(P("pwdout")); |
| 1006 | CHECK(o[0] == '/', "builtin output went to file '%s'", o); |
| 1007 | free(o); |
| 1008 | o = t_out(&t); |
| 1009 | CHECK(o[0] == 0, "and not to the shell's stdout, got '%s'", o); |
| 1010 | free(o); |
| 1011 | b = nb(0, 0); |
| 1012 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 1013 | CHECK(go(&t, b, &r) == 0, "pwd again"); |
| 1014 | o = t_out(&t); |
| 1015 | CHECK(o[0] == '/', "stdout restored for next builtin '%s'", o); |
| 1016 | free(o); |
| 1017 | b = nb(0, 0); |
| 1018 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 1019 | osh_rb_redir(b, OSH_R_IN, 0, P("no-such-input"), 0); |
| 1020 | CHECK(go(&t, b, &r) == 1, "builtin with failed redirection -> 1"); |
| 1021 | /* redirection-only command */ |
| 1022 | b = nb(0, 0); |
| 1023 | osh_rb_cmd(b, 0); |
| 1024 | osh_rb_redir(b, OSH_R_OUT, 1, P("created-by-redir-only"), 0); |
| 1025 | CHECK(go(&t, b, &r) == 0 && access(P("created-by-redir-only"), F_OK) == 0, "> f alone creates the file"); |
| 1026 | t_end(&t); |
| 1027 | } |
| 1028 | |
| 1029 | static void test_pipelines(void) |
| 1030 | { |
| 1031 | T t; |
| 1032 | t_begin(&t, "/usr/bin:/bin"); |
| 1033 | OshResult r; |
| 1034 | OshBuilder *b; |
| 1035 | char *o; |
| 1036 | |
| 1037 | /* pipeline of 3, status = last */ |
| 1038 | b = nb(0, 0); |
| 1039 | helper(b, "gen", "100000", NULL); |
| 1040 | helper(b, "cat", NULL); |
| 1041 | helper(b, "sum", NULL); |
| 1042 | CHECK(go(&t, b, &r) == 0 && r.ncmds == 3, "gen | cat | sum"); |
| 1043 | unsigned char *buf = malloc(100000); |
| 1044 | for (unsigned long i = 0; i < 100000; i++) buf[i] = gen_byte(i); |
| 1045 | char want[64]; |
| 1046 | snprintf(want, sizeof want, "100000 %08x\n", fnv(2166136261u, buf, 100000)); |
| 1047 | free(buf); |
| 1048 | o = t_out(&t); |
| 1049 | CHECK(strcmp(o, want) == 0, "3-stage checksum '%s' want '%s'", o, want); |
| 1050 | free(o); |
| 1051 | /* status is the last command's, not the first's */ |
| 1052 | b = nb(0, 0); |
| 1053 | helper(b, "exit", "5", NULL); |
| 1054 | helper(b, "exit", "0", NULL); |
| 1055 | CHECK(go(&t, b, &r) == 0 && r.cmd[0].status == 5, "status is last command"); |
| 1056 | b = nb(0, 0); |
| 1057 | helper(b, "exit", "0", NULL); |
| 1058 | helper(b, "exit", "3", NULL); |
| 1059 | CHECK(go(&t, b, &r) == 3, "last failing"); |
| 1060 | |
| 1061 | /* yes | head -1 terminates within 2 s (SIGPIPE default in children) */ |
| 1062 | t_clear(&t); |
| 1063 | b = nb(0, 0); |
| 1064 | cmd(b, 0, "yes", NULL); |
| 1065 | cmd(b, 0, "head", "-1", NULL); |
| 1066 | double t0 = now(); |
| 1067 | int st = go(&t, b, &r); |
| 1068 | double dt = now() - t0; |
| 1069 | o = t_out(&t); |
| 1070 | CHECK(st == 0 && strcmp(o, "y\n") == 0 && dt < 2.0, "yes | head -1: status %d out '%s' %.3fs", st, o, dt); |
| 1071 | CHECK(r.cmd[0].termsig == SIGPIPE && r.cmd[0].status == 128 + SIGPIPE, "writer died of SIGPIPE (sig %d)", r.cmd[0].termsig); |
| 1072 | free(o); |
| 1073 | |
| 1074 | /* 10 MB with backpressure */ |
| 1075 | t_clear(&t); |
| 1076 | b = nb(0, 0); |
| 1077 | helper(b, "gen", "10485760", NULL); |
| 1078 | helper(b, "cat", NULL); |
| 1079 | helper(b, "cat", NULL); |
| 1080 | helper(b, "sum", NULL); |
| 1081 | t0 = now(); |
| 1082 | st = go(&t, b, &r); |
| 1083 | dt = now() - t0; |
| 1084 | buf = malloc(10485760); |
| 1085 | for (unsigned long i = 0; i < 10485760; i++) buf[i] = gen_byte(i); |
| 1086 | snprintf(want, sizeof want, "10485760 %08x\n", fnv(2166136261u, buf, 10485760)); |
| 1087 | free(buf); |
| 1088 | o = t_out(&t); |
| 1089 | CHECK(st == 0 && strcmp(o, want) == 0, "10 MB through 4 stages: '%s' want '%s' (%.2fs)", o, want, dt); |
| 1090 | free(o); |
| 1091 | |
| 1092 | /* signal status 128+n and abort */ |
| 1093 | b = nb(0, 0); |
| 1094 | helper(b, "kill", "15", NULL); |
| 1095 | CHECK(go(&t, b, &r) == 128 + 15 && r.cmd[0].termsig == 15, "SIGTERM -> 143, got %d", r.status); |
| 1096 | b = nb(0, 0); |
| 1097 | helper(b, "kill", "9", NULL); |
| 1098 | CHECK(go(&t, b, &r) == 137, "SIGKILL -> 137"); |
| 1099 | b = nb(0, 0); |
| 1100 | helper(b, "kill", "6", NULL); |
| 1101 | CHECK(go(&t, b, &r) == 134, "SIGABRT -> 134"); |
| 1102 | /* SIGINT death: killed_by_int stops list execution */ |
| 1103 | b = nb(0, 0); |
| 1104 | helper(b, "kill", "2", NULL); |
| 1105 | CHECK(go(&t, b, &r) == 130 && r.killed_by_int, "SIGINT -> 130 and killed_by_int"); |
| 1106 | /* a stopped child (SIGTSTP) is ended, not parked: no job control */ |
| 1107 | b = nb(0, 0); |
| 1108 | helper(b, "kill", "20", NULL); |
| 1109 | st = go(&t, b, &r); |
| 1110 | CHECK(st == 128 + 20 && r.cmd[0].outcome == OSH_OUT_CANCELLED && r.err == OSH_E_INTERRUPTED && waitpid(r.cmd[0].pid, NULL, WNOHANG) < 0, |
| 1111 | "SIGTSTP child cancelled and reaped: status %d outcome %d", st, r.cmd[0].outcome); |
| 1112 | |
| 1113 | t_end(&t); |
| 1114 | } |
| 1115 | |
| 1116 | static void raise_int(void *ctx) { (void)ctx; kill(getpid(), SIGINT); } |
| 1117 | |
| 1118 | static void test_partial_launch(void) |
| 1119 | { |
| 1120 | T t; |
| 1121 | t_begin(&t, "/usr/bin:/bin"); |
| 1122 | OshResult r; |
| 1123 | OshBuilder *b; |
| 1124 | char *o; |
| 1125 | t.s.fd[0] = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 1126 | |
| 1127 | /* 2nd command not found: others still run and are reaped, 127 for it, status = last */ |
| 1128 | unlink(P("m1")); |
| 1129 | b = nb(0, 0); |
| 1130 | helper(b, "touch", P("m1"), NULL); |
| 1131 | cmd(b, 0, "no-such-command-xyz", NULL); |
| 1132 | int st = go(&t, b, &r); |
| 1133 | CHECK(st == 127 && r.cmd[1].status == 127 && r.cmd[0].status == 0 && access(P("m1"), F_OK) == 0, "partial: status %d", st); |
| 1134 | CHECK(waitpid(r.cmd[0].pid, NULL, WNOHANG) < 0 && errno == ECHILD, "first command reaped"); |
| 1135 | /* not-found in the middle: the writer gets EOF/SIGPIPE, the reader gets EOF */ |
| 1136 | t_clear(&t); |
| 1137 | b = nb(0, 0); |
| 1138 | helper(b, "gen", "1000000", NULL); |
| 1139 | cmd(b, 0, "no-such-command-xyz", NULL); |
| 1140 | helper(b, "sum", NULL); |
| 1141 | st = go(&t, b, &r); |
| 1142 | o = t_out(&t); |
| 1143 | CHECK(st == 0 && strcmp(o, "0 811c9dc5\n") == 0 && r.cmd[1].status == 127 && r.cmd[0].termsig == SIGPIPE, "middle not found: '%s' st %d", o, st); |
| 1144 | free(o); |
| 1145 | /* not found as the first command, last still runs with EOF input */ |
| 1146 | t_clear(&t); |
| 1147 | b = nb(0, 0); |
| 1148 | cmd(b, 0, "no-such-command-xyz", NULL); |
| 1149 | helper(b, "sum", NULL); |
| 1150 | st = go(&t, b, &r); |
| 1151 | o = t_out(&t); |
| 1152 | CHECK(st == 0 && strcmp(o, "0 811c9dc5\n") == 0, "first not found '%s'", o); |
| 1153 | free(o); |
| 1154 | |
| 1155 | /* real launch failure (fork fails at the 2nd command): PARTIAL_LAUNCH, started ones cancelled and reaped */ |
| 1156 | int fds0 = count_fds(); |
| 1157 | b = nb(0, 0); |
| 1158 | helper(b, "sleep", "30000", NULL); |
| 1159 | helper(b, "sleep", "30000", NULL); |
| 1160 | helper(b, "sleep", "30000", NULL); |
| 1161 | t.s.fail_fork_at = 2; |
| 1162 | double t0 = now(); |
| 1163 | st = go(&t, b, &r); |
| 1164 | double dt = now() - t0; |
| 1165 | t.s.fail_fork_at = 0; |
| 1166 | CHECK(r.err == OSH_E_PARTIAL_LAUNCH && st == 1 && dt < 5.0, "partial launch err %d status %d in %.2fs", r.err, st, dt); |
| 1167 | CHECK(r.cmd[0].outcome == OSH_OUT_CANCELLED && r.cmd[1].outcome == OSH_OUT_NOT_STARTED && r.cmd[2].outcome == OSH_OUT_NOT_STARTED, |
| 1168 | "outcomes %d %d %d", r.cmd[0].outcome, r.cmd[1].outcome, r.cmd[2].outcome); |
| 1169 | CHECK(r.cmd[0].pid > 0 && kill(r.cmd[0].pid, 0) < 0 && errno == ESRCH, "started command is gone (reaped, no zombie)"); |
| 1170 | CHECK(count_fds() == fds0, "no descriptors left in the parent (%d vs %d)", count_fds(), fds0); |
| 1171 | close(t.s.fd[0]); |
| 1172 | t_end(&t); |
| 1173 | } |
| 1174 | |
| 1175 | static void test_fd_leaks(void) |
| 1176 | { |
| 1177 | T t; |
| 1178 | t_begin(&t, "/usr/bin:/bin"); |
| 1179 | OshResult r; |
| 1180 | OshBuilder *b; |
| 1181 | char *o; |
| 1182 | t.s.fd[0] = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 1183 | int fds0 = count_fds(); |
| 1184 | |
| 1185 | /* 4-stage pipeline where every stage lists its descriptors: only 0,1,2 may exist */ |
| 1186 | b = nb(0, 0); |
| 1187 | helper(b, "echo", "x", NULL); |
| 1188 | helper(b, "fdlist", NULL); |
| 1189 | osh_rb_redir(b, OSH_R_OUT, 1, P("fd1"), 0); |
| 1190 | helper(b, "fdlist", NULL); |
| 1191 | osh_rb_redir(b, OSH_R_OUT, 1, P("fd2"), 0); |
| 1192 | helper(b, "fdlist", NULL); |
| 1193 | CHECK(go(&t, b, &r) == 0, "fdlist pipeline"); |
| 1194 | o = t_out(&t); |
| 1195 | CHECK(strcmp(o, "fdcount 0\n") == 0, "last stage fd list: '%s'", o); |
| 1196 | free(o); |
| 1197 | o = slurp(P("fd1")); |
| 1198 | CHECK(strcmp(o, "fdcount 0\n") == 0, "stage 2 (with > redirect) fd list: '%s'", o); |
| 1199 | free(o); |
| 1200 | o = slurp(P("fd2")); |
| 1201 | CHECK(strcmp(o, "fdcount 0\n") == 0, "stage 3 fd list: '%s'", o); |
| 1202 | free(o); |
| 1203 | /* single command with every redirection kind */ |
| 1204 | t_clear(&t); |
| 1205 | b = nb(0, 0); |
| 1206 | helper(b, "fdlist", NULL); |
| 1207 | osh_rb_redir(b, OSH_R_IN, 0, P("fd1"), 0); |
| 1208 | osh_rb_redir(b, OSH_R_APPEND, 2, P("fd3"), 0); |
| 1209 | osh_rb_redir(b, OSH_R_DUP, 2, NULL, 1); |
| 1210 | CHECK(go(&t, b, &r) == 0, "fdlist with redirs"); |
| 1211 | o = t_out(&t); |
| 1212 | CHECK(strcmp(o, "fdcount 0\n") == 0, "redirected single command fd list: '%s'", o); |
| 1213 | free(o); |
| 1214 | /* builtin in a pipeline child also sheds descriptors: builtin cannot show them, but exec'd neighbour proves pipes */ |
| 1215 | CHECK(count_fds() == fds0, "parent descriptor count unchanged after all runs (%d vs %d)", count_fds(), fds0); |
| 1216 | /* parent builtin with redirects leaves nothing behind either */ |
| 1217 | b = nb(0, 0); |
| 1218 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 1219 | osh_rb_redir(b, OSH_R_OUT, 1, P("fd4"), 0); |
| 1220 | osh_rb_redir(b, OSH_R_DUP, 2, NULL, 1); |
| 1221 | go(&t, b, &r); |
| 1222 | CHECK(count_fds() == fds0, "parent builtin redirection closes its descriptors (%d vs %d)", count_fds(), fds0); |
| 1223 | close(t.s.fd[0]); |
| 1224 | t_end(&t); |
| 1225 | } |
| 1226 | |
| 1227 | static void test_connectors(void) |
| 1228 | { |
| 1229 | T t; |
| 1230 | t_begin(&t, "/usr/bin:/bin"); |
| 1231 | OshRequest *rq[4]; |
| 1232 | static OshBuilder bs[4]; |
| 1233 | OshResult res[4]; |
| 1234 | const int conns[4] = {OSH_CONN_AND, OSH_CONN_OR, OSH_CONN_SEMI, OSH_CONN_NONE}; |
| 1235 | const char *marks[4] = {"c0", "c1", "c2", "c3"}; |
| 1236 | |
| 1237 | /* false && X ; ... X is skipped; `|| Y` then runs because $? is still 1; ; Z always runs */ |
| 1238 | for (int i = 0; i < 4; i++) { |
| 1239 | osh_rb_init(&bs[i], 0, conns[i]); |
| 1240 | if (i == 0) helper(&bs[i], "exit", "1", NULL); |
| 1241 | else helper(&bs[i], "touch", P(marks[i]), NULL); |
| 1242 | osh_rb_seal(&bs[i]); |
| 1243 | rq[i] = malloc(sizeof(OshRequest)); |
| 1244 | CHECK(osh_req_decode(bs[i].rec, osh_rb_cells(&bs[i]), bs[i].out, bs[i].out_used, rq[i]) == 0, "decode"); |
| 1245 | unlink(P(marks[i])); |
| 1246 | } |
| 1247 | /* list: false && c1 || c2 ; c3 */ |
| 1248 | int last = osh_run_list(&t.s, (const OshRequest *const *)rq, 4, res); |
| 1249 | CHECK(access(P("c1"), F_OK) != 0, "&& after failure skipped"); |
| 1250 | CHECK(access(P("c2"), F_OK) == 0, "|| after skipped && still sees $?=1 and runs"); |
| 1251 | CHECK(access(P("c3"), F_OK) == 0, "; always runs"); |
| 1252 | CHECK(last == 0, "final $? = %d", last); |
| 1253 | for (int i = 0; i < 4; i++) free(rq[i]); |
| 1254 | |
| 1255 | /* $? tracking: true || X skipped; true && X runs */ |
| 1256 | int cs[3] = {OSH_CONN_OR, OSH_CONN_AND, OSH_CONN_NONE}; |
| 1257 | OshRequest *q[3]; |
| 1258 | static OshBuilder b3[3]; |
| 1259 | for (int i = 0; i < 3; i++) { |
| 1260 | osh_rb_init(&b3[i], 0, cs[i]); |
| 1261 | if (i == 0) helper(&b3[i], "exit", "0", NULL); |
| 1262 | else helper(&b3[i], "touch", P(i == 1 ? "k1" : "k2"), NULL); |
| 1263 | osh_rb_seal(&b3[i]); |
| 1264 | q[i] = malloc(sizeof(OshRequest)); |
| 1265 | osh_req_decode(b3[i].rec, osh_rb_cells(&b3[i]), b3[i].out, b3[i].out_used, q[i]); |
| 1266 | } |
| 1267 | unlink(P("k1")); unlink(P("k2")); |
| 1268 | osh_run_list(&t.s, (const OshRequest *const *)q, 3, res); |
| 1269 | CHECK(access(P("k1"), F_OK) != 0 && access(P("k2"), F_OK) == 0, "true || X skipped, then && runs"); |
| 1270 | for (int i = 0; i < 3; i++) free(q[i]); |
| 1271 | CHECK(osh_conn_should_run(OSH_CONN_AND, 0) && !osh_conn_should_run(OSH_CONN_AND, 1) && !osh_conn_should_run(OSH_CONN_OR, 0) && |
| 1272 | osh_conn_should_run(OSH_CONN_OR, 3) && osh_conn_should_run(OSH_CONN_SEMI, 9) && osh_conn_should_run(OSH_CONN_NONE, 9), |
| 1273 | "connector truth table"); |
| 1274 | |
| 1275 | /* exit in a list stops it */ |
| 1276 | OshRequest *e[2]; |
| 1277 | static OshBuilder be[2]; |
| 1278 | for (int i = 0; i < 2; i++) { |
| 1279 | osh_rb_init(&be[i], 0, i == 0 ? OSH_CONN_SEMI : 0); |
| 1280 | if (i == 0) cmd(&be[i], OSH_B_EXIT, "exit", "4", NULL); |
| 1281 | else helper(&be[i], "touch", P("after-exit"), NULL); |
| 1282 | osh_rb_seal(&be[i]); |
| 1283 | e[i] = malloc(sizeof(OshRequest)); |
| 1284 | osh_req_decode(be[i].rec, osh_rb_cells(&be[i]), be[i].out, be[i].out_used, e[i]); |
| 1285 | } |
| 1286 | unlink(P("after-exit")); |
| 1287 | int st = osh_run_list(&t.s, (const OshRequest *const *)e, 2, res); |
| 1288 | CHECK(st == 4 && access(P("after-exit"), F_OK) != 0, "exit 4 ends the list (st %d)", st); |
| 1289 | free(e[0]); free(e[1]); |
| 1290 | t_end(&t); |
| 1291 | } |
| 1292 | |
| 1293 | static void test_refusals(void) |
| 1294 | { |
| 1295 | T t; |
| 1296 | t_begin(&t, "/usr/bin:/bin"); |
| 1297 | OshBuilder *b; |
| 1298 | OshRequest *rq = malloc(sizeof *rq); |
| 1299 | |
| 1300 | b = nb(0, 0); |
| 1301 | helper(b, "echo", "x", NULL); |
| 1302 | osh_rb_seal(b); |
| 1303 | size_t nc = osh_rb_cells(b); |
| 1304 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == 0, "baseline decodes"); |
| 1305 | uint64_t save; |
| 1306 | #define MUT(idx, val, want, what) do { save = b->rec[idx]; b->rec[idx] = (val); \ |
| 1307 | int rc_ = osh_req_decode(b->rec, nc, b->out, b->out_used, rq); CHECK(rc_ == (want), what ": got %d want %d", rc_, (want)); \ |
| 1308 | OshResult rr_; int st_ = osh_exec_record(&t.s, b->rec, nc, b->out, b->out_used, &rr_); CHECK(st_ == 2 && rr_.refusal == (want), what " not run"); \ |
| 1309 | b->rec[idx] = save; } while (0) |
| 1310 | MUT(0, 0x4F524552, OSH_ABI_MAGIC, "bad magic"); |
| 1311 | MUT(5, 2, OSH_ABI_VERSION, "version 2"); |
| 1312 | MUT(5, 0, OSH_ABI_VERSION, "version 0"); |
| 1313 | MUT(5, 0x100000001ull, OSH_ABI_VERSION, "version high bits"); |
| 1314 | MUT(6, 1, OSH_ABI_RESERVED, "reserved cell 6"); |
| 1315 | MUT(7, 1, OSH_ABI_RESERVED, "reserved cell 7"); |
| 1316 | MUT(3, 2, OSH_ABI_RESERVED, "unknown flag bit"); |
| 1317 | MUT(1, 0, OSH_REQ_FIELD, "ncmds 0"); |
| 1318 | MUT(1, 9, OSH_REQ_FIELD, "ncmds 9"); |
| 1319 | MUT(1, 2, OSH_ABI_LENGTH, "ncmds beyond supplied cells"); |
| 1320 | MUT(2, 4, OSH_REQ_FIELD, "bad connector"); |
| 1321 | MUT(4, 9000, OSH_REQ_FIELD, "out_used over cap"); |
| 1322 | MUT(4, 1, OSH_REQ_FIELD, "out_used smaller than the offsets used"); |
| 1323 | #undef MUT |
| 1324 | CHECK(osh_req_decode(b->rec, 3, b->out, b->out_used, rq) == OSH_ABI_LENGTH, "header too short"); |
| 1325 | CHECK(osh_req_decode(NULL, 0, NULL, 0, rq) == OSH_ABI_LENGTH, "NULL record"); |
| 1326 | /* field-level malformations */ |
| 1327 | uint64_t c0 = OSH_REQ_HDR_CELLS; |
| 1328 | save = b->rec[c0 + 0]; b->rec[c0 + 0] = 33; |
| 1329 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "nargv 33"); |
| 1330 | b->rec[c0 + 0] = save; |
| 1331 | save = b->rec[c0 + 3]; b->rec[c0 + 3] = 7; |
| 1332 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "unknown builtin id"); |
| 1333 | b->rec[c0 + 3] = save; |
| 1334 | save = b->rec[c0 + 4]; b->rec[c0 + 4] = 1000000; |
| 1335 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "argv offset out of range"); |
| 1336 | b->rec[c0 + 4] = save; |
| 1337 | save = b->rec[c0 + 5]; b->rec[c0 + 5] = 1000000; |
| 1338 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "argv length out of range"); |
| 1339 | b->rec[c0 + 5] = save; |
| 1340 | save = b->rec[c0 + 4 + 2 * 5]; b->rec[c0 + 4 + 2 * 5] = 1; /* entry beyond nargv must be zero */ |
| 1341 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_ABI_RESERVED, "stray entry beyond nargv"); |
| 1342 | b->rec[c0 + 4 + 2 * 5] = save; |
| 1343 | uint64_t sv = b->out[0]; b->out[0] = 0; /* NUL inside an argument */ |
| 1344 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "NUL byte in argument"); |
| 1345 | b->out[0] = sv; |
| 1346 | sv = b->out[0]; b->out[0] = 256; /* a cell is a byte */ |
| 1347 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "cell above 255"); |
| 1348 | b->out[0] = sv; |
| 1349 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == 0, "restored record decodes"); |
| 1350 | /* redirection fields */ |
| 1351 | b = nb(0, 0); |
| 1352 | helper(b, "echo", "x", NULL); |
| 1353 | osh_rb_redir(b, OSH_R_DUP, 2, NULL, 1); |
| 1354 | osh_rb_seal(b); |
| 1355 | nc = osh_rb_cells(b); |
| 1356 | uint64_t rbase = c0 + 4 + 64 + 64; |
| 1357 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == 0, "dup redir decodes"); |
| 1358 | b->rec[rbase + 2] = 3; |
| 1359 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "N>&3 refused (M in 0..2)"); |
| 1360 | b->rec[rbase + 2] = 1; b->rec[rbase + 1] = 3; |
| 1361 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "fd 3 refused"); |
| 1362 | b->rec[rbase + 1] = 2; b->rec[rbase + 0] = 9; |
| 1363 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "redir kind 9"); |
| 1364 | b->rec[rbase + 0] = 4; b->rec[rbase + 3] = 1; |
| 1365 | CHECK(osh_req_decode(b->rec, nc, b->out, b->out_used, rq) == OSH_REQ_FIELD, "dup with nonzero length"); |
| 1366 | /* direct records may not carry builtins */ |
| 1367 | b = nb(OSH_REQ_FLAG_DIRECT, 0); |
| 1368 | cmd(b, OSH_B_PWD, "pwd", NULL); |
| 1369 | osh_rb_seal(b); |
| 1370 | CHECK(osh_req_decode(b->rec, osh_rb_cells(b), b->out, b->out_used, rq) == OSH_REQ_FIELD, "DIRECT with builtin_id refused"); |
| 1371 | /* the wire-form check on a request with a runnable command produced nothing */ |
| 1372 | free(rq); |
| 1373 | t_end(&t); |
| 1374 | } |
| 1375 | |
| 1376 | static int cls_hook(void *ctx, const char *name) |
| 1377 | { |
| 1378 | (void)ctx; |
| 1379 | if (!strcmp(name, "native-op")) return OSH_CLASS_OMEGA_NATIVE_OP; |
| 1380 | if (!strcmp(name, "artifact")) return OSH_CLASS_AIENOS_ARTIFACT; |
| 1381 | if (!strcmp(name, "service")) return OSH_CLASS_AIENOS_SERVICE; |
| 1382 | return OSH_CLASS_LINUX; |
| 1383 | } |
| 1384 | |
| 1385 | static int perm_hook(void *ctx, const OshBinding *b, int op) |
| 1386 | { |
| 1387 | (void)b; |
| 1388 | return *(int *)ctx == op ? OSH_E_REVOKED : OSH_E_OK; |
| 1389 | } |
| 1390 | |
| 1391 | static void test_destinations_and_authority(void) |
| 1392 | { |
| 1393 | T t; |
| 1394 | t_begin(&t, "/usr/bin:/bin"); |
| 1395 | OshResult r; |
| 1396 | OshBuilder *b; |
| 1397 | t.s.class_hook = cls_hook; |
| 1398 | const char *names[3] = {"native-op", "artifact", "service"}; |
| 1399 | for (int i = 0; i < 3; i++) { |
| 1400 | unlink(P("m2")); |
| 1401 | b = nb(0, 0); |
| 1402 | helper(b, "touch", P("m2"), NULL); /* the first command would run if the pipeline were not refused as a whole */ |
| 1403 | cmd(b, 0, names[i], NULL); |
| 1404 | CHECK(go(&t, b, &r) == 126 && r.err == OSH_E_UNAVAILABLE && r.cmd[0].outcome == OSH_OUT_FAILED_NO_EFFECT, "%s refused (st %d err %d)", names[i], r.status, r.err); |
| 1405 | CHECK(access(P("m2"), F_OK) != 0, "%s: nothing in the pipeline ran, never forwarded", names[i]); |
| 1406 | CHECK(r.cmd[0].pid == 0 && r.cmd[1].pid == 0, "no process started"); |
| 1407 | } |
| 1408 | /* even if a Linux file of that name exists on PATH, the class hook wins: no fallback between classes */ |
| 1409 | wfile(P("artifact"), "#!/bin/true\n", 0755); |
| 1410 | osh_var_set(&t.s, "PATH", g_tmp); |
| 1411 | b = nb(0, 0); |
| 1412 | cmd(b, 0, "artifact", NULL); |
| 1413 | CHECK(go(&t, b, &r) == 126 && r.err == OSH_E_UNAVAILABLE, "no fallback to a same-named Linux executable"); |
| 1414 | t.s.class_hook = NULL; |
| 1415 | |
| 1416 | /* authority: no binding, bad domain, narrowing, hook denial */ |
| 1417 | osh_var_set(&t.s, "PATH", "/usr/bin:/bin"); |
| 1418 | t.s.binding.valid = 0; |
| 1419 | unlink(P("m3")); |
| 1420 | b = nb(0, 0); |
| 1421 | helper(b, "touch", P("m3"), NULL); |
| 1422 | CHECK(go(&t, b, &r) == 126 && r.err == OSH_E_DENIED && access(P("m3"), F_OK) != 0, "no binding -> DENIED, nothing runs"); |
| 1423 | t.s.binding.valid = 1; |
| 1424 | t.s.binding.domain = 3; |
| 1425 | b = nb(0, 0); |
| 1426 | helper(b, "touch", P("m3"), NULL); |
| 1427 | CHECK(go(&t, b, &r) == 126 && r.err == OSH_E_CAP_DOMAIN_MISMATCH, "domain mismatch"); |
| 1428 | t.s.binding.domain = 1; |
| 1429 | t.s.binding.cap_generation = 0x100000000ull; |
| 1430 | b = nb(0, 0); |
| 1431 | helper(b, "touch", P("m3"), NULL); |
| 1432 | CHECK(go(&t, b, &r) == 126 && r.err == OSH_E_CAP_GEN_NARROW, "domain-1 generation above 32 bits"); |
| 1433 | t.s.binding.domain = 2; |
| 1434 | int deny = OSH_OP_OPEN_WRITE; |
| 1435 | t.s.perm_hook = perm_hook; |
| 1436 | t.s.hook_ctx = &deny; |
| 1437 | b = nb(0, 0); |
| 1438 | helper(b, "touch", P("m3"), NULL); |
| 1439 | CHECK(go(&t, b, &r) == 0 && access(P("m3"), F_OK) == 0, "spawn allowed when only writes are revoked"); |
| 1440 | b = nb(0, 0); |
| 1441 | helper(b, "echo", "x", NULL); |
| 1442 | osh_rb_redir(b, OSH_R_OUT, 1, P("denied-out"), 0); |
| 1443 | CHECK(go(&t, b, &r) == 126 && r.err == OSH_E_REVOKED && access(P("denied-out"), F_OK) != 0, "revoked write: effect does not happen"); |
| 1444 | deny = OSH_OP_SPAWN; |
| 1445 | b = nb(0, 0); |
| 1446 | helper(b, "touch", P("m4"), NULL); |
| 1447 | CHECK(go(&t, b, &r) == 126 && r.err == OSH_E_REVOKED && access(P("m4"), F_OK) != 0, "revoked spawn"); |
| 1448 | t_end(&t); |
| 1449 | } |
| 1450 | |
| 1451 | static void test_direct_equals_record(void) |
| 1452 | { |
| 1453 | T t; |
| 1454 | t_begin(&t, "/usr/bin:/bin"); |
| 1455 | OshResult r1, r2; |
| 1456 | |
| 1457 | /* direct-argv submit builds the same record: same decoded content, same behaviour */ |
| 1458 | const char *argv[] = {g_self, "--helper", "both"}; |
| 1459 | OshRedir rd[2] = {{.kind = OSH_R_OUT, .fd = 1, .path = P("dout")}, {.kind = OSH_R_DUP, .fd = 2, .src_fd = 1}}; |
| 1460 | OshAssign ov[1] = {{"DIRECT_VAR", "v"}}; |
| 1461 | int s1 = osh_submit_argv(&t.s, argv, 3, ov, 1, rd, 2, &r1); |
| 1462 | char *o1 = slurp(P("dout")); |
| 1463 | |
| 1464 | OshBuilder *b = nb(0, 0); |
| 1465 | helper(b, "both", NULL); |
| 1466 | osh_rb_assign(b, "DIRECT_VAR", "v"); |
| 1467 | osh_rb_redir(b, OSH_R_OUT, 1, P("rout"), 0); |
| 1468 | osh_rb_redir(b, OSH_R_DUP, 2, NULL, 1); |
| 1469 | int s2 = go(&t, b, &r2); |
| 1470 | char *o2 = slurp(P("rout")); |
| 1471 | CHECK(s1 == s2 && s1 == 0 && strcmp(o1, o2) == 0 && strcmp(o1, "out\nerr\n") == 0, "direct vs record: %d/%d '%s' '%s'", s1, s2, o1, o2); |
| 1472 | free(o1); free(o2); |
| 1473 | |
| 1474 | /* identical decoded structs (apart from the flag) */ |
| 1475 | OshBuilder bd, br; |
| 1476 | osh_rb_init(&bd, OSH_REQ_FLAG_DIRECT, 0); |
| 1477 | osh_rb_cmd(&bd, 0); |
| 1478 | osh_rb_arg(&bd, "a"); osh_rb_arg(&bd, ""); osh_rb_arg(&bd, "c d"); |
| 1479 | osh_rb_assign(&bd, "K", "v"); |
| 1480 | osh_rb_redir(&bd, OSH_R_APPEND, 1, "f", 0); |
| 1481 | osh_rb_seal(&bd); |
| 1482 | osh_rb_init(&br, 0, 0); |
| 1483 | osh_rb_cmd(&br, 0); |
| 1484 | osh_rb_arg(&br, "a"); osh_rb_arg(&br, ""); osh_rb_arg(&br, "c d"); |
| 1485 | osh_rb_assign(&br, "K", "v"); |
| 1486 | osh_rb_redir(&br, OSH_R_APPEND, 1, "f", 0); |
| 1487 | osh_rb_seal(&br); |
| 1488 | OshRequest *a = malloc(sizeof *a), *c = malloc(sizeof *c); |
| 1489 | CHECK(osh_req_decode(bd.rec, osh_rb_cells(&bd), bd.out, bd.out_used, a) == 0 && osh_req_decode(br.rec, osh_rb_cells(&br), br.out, br.out_used, c) == 0, "decode both"); |
| 1490 | CHECK(a->cmd[0].nargv == 3 && c->cmd[0].nargv == 3 && strcmp(a->cmd[0].argv[2], c->cmd[0].argv[2]) == 0 && a->cmd[0].argv[1][0] == 0 && |
| 1491 | a->cmd[0].nassign == 1 && a->cmd[0].redir[0].kind == c->cmd[0].redir[0].kind && a->flags == 1 && c->flags == 0, |
| 1492 | "decoded content equal (empty argument preserved)"); |
| 1493 | free(a); free(c); |
| 1494 | |
| 1495 | /* the direct path obeys the same resolution, refusals and authority checks */ |
| 1496 | const char *bad[] = {"no-such-command-xyz"}; |
| 1497 | CHECK(osh_submit_argv(&t.s, bad, 1, NULL, 0, NULL, 0, &r1) == 127, "direct: PATH miss 127"); |
| 1498 | const char *pwdv[] = {"pwd"}; |
| 1499 | CHECK(osh_submit_argv(&t.s, pwdv, 1, NULL, 0, NULL, 0, &r1) == 0, "direct: 'pwd' is an external /usr/bin/pwd, not the builtin"); |
| 1500 | CHECK(r1.cmd[0].pid > 0, "direct: a process was started for pwd (builtin_id must be 0)"); |
| 1501 | t.s.binding.valid = 0; |
| 1502 | CHECK(osh_submit_argv(&t.s, argv, 3, NULL, 0, NULL, 0, &r1) == 126 && r1.err == OSH_E_DENIED, "direct path honours the binding check"); |
| 1503 | t.s.binding.valid = 1; |
| 1504 | CHECK(osh_submit_argv(&t.s, argv, 0, NULL, 0, NULL, 0, &r1) == 0, "direct: empty argv with no assignments is a no-op"); |
| 1505 | OshAssign badname[1] = {{"1x", "v"}}; |
| 1506 | CHECK(osh_submit_argv(&t.s, argv, 3, badname, 1, NULL, 0, &r1) == 2 && r1.refusal == OSH_REQ_FIELD, "direct: bad assignment name refused as REQ_FIELD"); |
| 1507 | const char *many[33]; |
| 1508 | for (int i = 0; i < 33; i++) many[i] = "x"; |
| 1509 | CHECK(osh_submit_argv(&t.s, many, 33, NULL, 0, NULL, 0, &r1) == 2 && r1.refusal == OSH_REQ_FIELD, "direct: 33 args refused"); |
| 1510 | OshRedir badr[1] = {{.kind = OSH_R_DUP, .fd = 2, .src_fd = 5}}; |
| 1511 | CHECK(osh_submit_argv(&t.s, argv, 3, NULL, 0, badr, 1, &r1) == 2 && r1.refusal == OSH_REQ_FIELD, "direct: N>&5 refused"); |
| 1512 | t_end(&t); |
| 1513 | } |
| 1514 | |
| 1515 | /* bash 5.2: `env 'A B=1' bash -c env` still shows "A B=1"; the entry is not a variable but every child gets it (#344 RISK-7) */ |
| 1516 | static void test_raw_env(void) |
| 1517 | { |
| 1518 | T t; OshBuilder *b; OshResult r; |
| 1519 | char *env[] = {"HOME=/nonexistent-home", "LANG=C", "A B=1", "1X=2", NULL}; |
| 1520 | t_begin_env(&t, "/usr/bin:/bin", env); |
| 1521 | b = nb(0, 0); |
| 1522 | helper(b, "env", "A B", NULL); |
| 1523 | go(&t, b, &r); |
| 1524 | char *o = t_out(&t); |
| 1525 | CHECK(strcmp(o, "1\n") == 0, "child sees A B=1 (got %s)", o); |
| 1526 | free(o); |
| 1527 | t_clear(&t); |
| 1528 | b = nb(0, 0); |
| 1529 | helper(b, "env", "1X", NULL); |
| 1530 | go(&t, b, &r); |
| 1531 | o = t_out(&t); |
| 1532 | CHECK(strcmp(o, "2\n") == 0, "child sees 1X=2 (got %s)", o); |
| 1533 | free(o); |
| 1534 | CHECK(osh_var_get(&t.s, "A B") == NULL && osh_var_get(&t.s, "1X") == NULL, "not shell variables"); |
| 1535 | t_end(&t); |
| 1536 | } |
| 1537 | |
| 1538 | static void test_session_env(void) |
| 1539 | { |
| 1540 | OshSession s; |
| 1541 | char *env[] = {"A=1", "B=two=2", "=bad", "NOEQ", "9x=1", "C=", "A B=1", "BASH_FUNC_f%%=() { echo hi; }", NULL}; |
| 1542 | CHECK(osh_session_init(&s, env) == 0, "init"); |
| 1543 | CHECK(strcmp(osh_var_get(&s, "A"), "1") == 0 && strcmp(osh_var_get(&s, "B"), "two=2") == 0 && osh_var_get(&s, "C") && !*osh_var_get(&s, "C"), "imported"); |
| 1544 | CHECK(osh_var_get(&s, "NOEQ") == NULL && osh_var_get(&s, "9x") == NULL && osh_var_get(&s, "A B") == NULL, "malformed entries are not variables"); |
| 1545 | CHECK(osh_var_get(&s, "PWD") != NULL, "PWD set"); |
| 1546 | OshAssign ov[2] = {{"A", "9"}, {"Z", "z"}}; |
| 1547 | char **e = osh_build_envp(&s, ov, 2, NULL); |
| 1548 | int a = 0, z = 0, n = 0, raw = 0; |
| 1549 | for (char **p = e; *p; p++) { n++; a += !strcmp(*p, "A=9"); z += !strcmp(*p, "Z=z"); |
| 1550 | raw += !strcmp(*p, "9x=1") + !strcmp(*p, "A B=1") + !strcmp(*p, "BASH_FUNC_f%%=() { echo hi; }"); } |
| 1551 | CHECK(a == 1 && z == 1 && n == 9, "fresh envp with override plus SHLVL (n=%d)", n); |
| 1552 | CHECK(raw == 3, "non-identifier entries reach children verbatim, as bash does (%d/3)", raw); |
| 1553 | /* bash 5.2: SHLVL is the inherited level plus one; OLDPWD is exported, valueless unless inherited naming a directory */ |
| 1554 | int shl = 0, oldpwd_valueless = 0; |
| 1555 | for (char **p = e; *p; p++) shl += !strcmp(*p, "SHLVL=1"); |
| 1556 | for (size_t k = 0; k < s.nvars; k++) |
| 1557 | if (!strcmp(s.vars[k].name, "OLDPWD")) oldpwd_valueless = s.vars[k].exported && !s.vars[k].value; |
| 1558 | CHECK(shl == 1 && oldpwd_valueless, "no SHLVL inherited: SHLVL=1 exported; OLDPWD exported without a value (%d %d)", shl, oldpwd_valueless); |
| 1559 | osh_envp_free(e); |
| 1560 | CHECK(osh_var_set(&s, "bad name", "x") < 0 && osh_var_export(&s, "", NULL) < 0, "invalid names refused"); |
| 1561 | osh_var_set(&s, "U", "u"); |
| 1562 | e = osh_build_envp(&s, NULL, 0, NULL); |
| 1563 | int found = 0; |
| 1564 | for (char **p = e; *p; p++) found += !strncmp(*p, "U=", 2) + !strncmp(*p, "_=", 2); |
| 1565 | CHECK(found == 0, "unexported variable not in envp; no _ without a program"); |
| 1566 | osh_envp_free(e); |
| 1567 | /* omega#344: bash gives every program _=<the path it executes>, over an assigned or inherited _ */ |
| 1568 | OshAssign us[1] = {{"_", "/x/y"}}; |
| 1569 | e = osh_build_envp(&s, us, 1, "/usr/bin/env"); |
| 1570 | int u = 0, uok = 0; |
| 1571 | for (char **p = e; *p; p++) { u += !strncmp(*p, "_=", 2); uok += !strcmp(*p, "_=/usr/bin/env"); } |
| 1572 | CHECK(u == 1 && uok == 1, "_ is the executed path, once (%d %d)", u, uok); |
| 1573 | osh_envp_free(e); |
| 1574 | osh_session_free(&s); |
| 1575 | } |
| 1576 | |
| 1577 | /* bash 5.2.21 (measured): SHLVL=3 -> 4; abc -> 1; -2 -> 0; 999 -> 1 (too high, reset); inherited OLDPWD kept only when it |
| 1578 | * names a directory. */ |
| 1579 | static void test_session_levels(void) |
| 1580 | { |
| 1581 | static const struct { const char *in, *want; } lv[] = {{"SHLVL=3", "4"}, {"SHLVL=abc", "1"}, {"SHLVL=-2", "0"}, {"SHLVL=999", "1"}, {"SHLVL=0", "1"}}; |
| 1582 | for (size_t k = 0; k < sizeof lv / sizeof lv[0]; k++) { |
| 1583 | OshSession s; |
| 1584 | char in[32]; |
| 1585 | snprintf(in, sizeof in, "%s", lv[k].in); |
| 1586 | char *env[] = {in, NULL}; |
| 1587 | CHECK(osh_session_init(&s, env) == 0, "init %s", lv[k].in); |
| 1588 | const char *v = osh_var_get(&s, "SHLVL"); |
| 1589 | CHECK(v && strcmp(v, lv[k].want) == 0, "%s -> SHLVL=%s (got %s)", lv[k].in, lv[k].want, v ? v : "(null)"); |
| 1590 | osh_session_free(&s); |
| 1591 | } |
| 1592 | OshSession s; |
| 1593 | char *env1[] = {"OLDPWD=/usr", NULL}; |
| 1594 | CHECK(osh_session_init(&s, env1) == 0, "init oldpwd dir"); |
| 1595 | CHECK(osh_var_get(&s, "OLDPWD") && strcmp(osh_var_get(&s, "OLDPWD"), "/usr") == 0, "inherited OLDPWD naming a directory is kept"); |
| 1596 | osh_session_free(&s); |
| 1597 | char *env2[] = {"OLDPWD=/nonexistent-dir-osh", NULL}; |
| 1598 | CHECK(osh_session_init(&s, env2) == 0, "init oldpwd bogus"); |
| 1599 | int valueless = 0; |
| 1600 | for (size_t k = 0; k < s.nvars; k++) |
| 1601 | if (!strcmp(s.vars[k].name, "OLDPWD")) valueless = s.vars[k].exported && !s.vars[k].value; |
| 1602 | CHECK(valueless, "inherited OLDPWD not naming a directory becomes a valueless export"); |
| 1603 | osh_session_free(&s); |
| 1604 | } |
| 1605 | |
| 1606 | /* ================= review fixes (PR 337): isolation helper ================= */ |
| 1607 | |
| 1608 | /* Results of a test that ran in its own process (so a hang or a signal storm cannot take the suite with it). */ |
| 1609 | typedef struct { |
| 1610 | int status, err, outcome0, outcome1, outcome2, termsig0, termsig1, sigint_seen, killed_by_int, exit_requested, exit_status; |
| 1611 | int extra[6]; |
| 1612 | double dt; |
| 1613 | char text[256]; |
| 1614 | } Rep; |
| 1615 | |
| 1616 | static int g_only_set; |
| 1617 | static const char *g_only; |
| 1618 | static int want(const char *name) { return !g_only_set || strstr(name, g_only) != NULL; } |
| 1619 | |
| 1620 | /* Run fn in a forked process (own process group if new_group, with a SIGALRM watchdog). Returns 0 when it reported, |
| 1621 | * -1 when it hung or crashed (then any members of its group are killed). */ |
| 1622 | static int run_isolated(void (*fn)(Rep *), Rep *out, int new_group, unsigned secs) |
| 1623 | { |
| 1624 | int pf[2]; |
| 1625 | memset(out, 0, sizeof *out); |
| 1626 | if (pipe(pf)) return -1; |
| 1627 | fflush(stdout); |
| 1628 | pid_t pid = fork(); |
| 1629 | if (pid < 0) return -1; |
| 1630 | if (pid == 0) { |
| 1631 | if (new_group) setpgid(0, 0); |
| 1632 | close(pf[0]); |
| 1633 | alarm(secs); |
| 1634 | Rep r; |
| 1635 | memset(&r, 0, sizeof r); |
| 1636 | fn(&r); |
| 1637 | if (write(pf[1], &r, sizeof r) < 0) {} |
| 1638 | _exit(0); |
| 1639 | } |
| 1640 | close(pf[1]); |
| 1641 | if (new_group) setpgid(pid, pid); |
| 1642 | size_t got = 0; |
| 1643 | while (got < sizeof *out) { |
| 1644 | ssize_t n = read(pf[0], (char *)out + got, sizeof *out - got); |
| 1645 | if (n <= 0) break; |
| 1646 | got += (size_t)n; |
| 1647 | } |
| 1648 | close(pf[0]); |
| 1649 | int st; |
| 1650 | waitpid(pid, &st, 0); |
| 1651 | if (new_group) kill(-pid, SIGKILL); /* leftovers of a failed run */ |
| 1652 | if (got != sizeof *out) { memset(out, 0, sizeof *out); return -1; } |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | static void rep_fill(Rep *rp, const OshResult *r, int st, double dt) |
| 1657 | { |
| 1658 | rp->status = st; |
| 1659 | rp->err = r->err; |
| 1660 | rp->outcome0 = r->cmd[0].outcome; |
| 1661 | rp->outcome1 = r->ncmds > 1 ? r->cmd[1].outcome : 0; |
| 1662 | rp->outcome2 = r->ncmds > 2 ? r->cmd[2].outcome : 0; |
| 1663 | rp->termsig0 = r->cmd[0].termsig; |
| 1664 | rp->termsig1 = r->ncmds > 1 ? r->cmd[1].termsig : 0; |
| 1665 | rp->sigint_seen = r->sigint_seen; |
| 1666 | rp->killed_by_int = r->killed_by_int; |
| 1667 | rp->exit_requested = r->exit_requested; |
| 1668 | rp->dt = dt; |
| 1669 | } |
| 1670 | |
| 1671 | static void raise_int_group(void *ctx) { (void)ctx; kill(0, SIGINT); } /* what the terminal does on ^C: the whole group */ |
| 1672 | |
| 1673 | /* ---- BUG1: non-interactive session ---- */ |
| 1674 | |
| 1675 | static void iso_sigint_group(Rep *rp) |
| 1676 | { |
| 1677 | T t; |
| 1678 | t_begin(&t, "/usr/bin:/bin"); |
| 1679 | OshResult r; |
| 1680 | OshBuilder *b = nb(0, 0); |
| 1681 | helper(b, "pgid", NULL); |
| 1682 | helper(b, "sleep", "20000", NULL); |
| 1683 | helper(b, "cat", NULL); |
| 1684 | t.s.after_launch_hook = raise_int_group; |
| 1685 | double t0 = now(); |
| 1686 | int st = go(&t, b, &r); |
| 1687 | rep_fill(rp, &r, st, now() - t0); |
| 1688 | rp->exit_status = t.s.exit_status; |
| 1689 | rp->exit_requested = t.s.exit_requested; |
| 1690 | rp->termsig1 = r.cmd[1].termsig; |
| 1691 | t.s.after_launch_hook = NULL; |
| 1692 | t_clear(&t); |
| 1693 | b = nb(0, 0); |
| 1694 | helper(b, "pgid", NULL); |
| 1695 | t.s.exit_requested = 0; |
| 1696 | go(&t, b, &r); |
| 1697 | char *o = t_out(&t); |
| 1698 | rp->extra[0] = atoi(o) == (int)getpgrp(); /* child stays in the shell's own process group */ |
| 1699 | free(o); |
| 1700 | t_end(&t); |
| 1701 | } |
| 1702 | |
| 1703 | static void test_sigint_noninteractive(void) |
| 1704 | { |
| 1705 | Rep rp; |
| 1706 | int rc = run_isolated(iso_sigint_group, &rp, 1, 15); |
| 1707 | CHECK(rc == 0, "group SIGINT test hung or crashed"); |
| 1708 | CHECK(rc == 0 && rp.dt < 3.0, "pipeline with a terminal-style SIGINT to the group ended in %.2fs (hung until the child finished?)", rp.dt); |
| 1709 | CHECK(rc == 0 && rp.termsig1 == SIGINT && rp.killed_by_int == 1 && rp.sigint_seen == 1, |
| 1710 | "sleeping child died of SIGINT (termsig %d) and the shell saw it (seen %d, killed_by_int %d)", rp.termsig1, rp.sigint_seen, rp.killed_by_int); |
| 1711 | CHECK(rc == 0 && rp.exit_requested == 1 && rp.exit_status == 130, |
| 1712 | "non-interactive shell exits 128+2 after the pipeline whose foreground child died of SIGINT (requested %d status %d)", rp.exit_requested, rp.exit_status); |
| 1713 | CHECK(rc == 0 && rp.extra[0] == 1, "non-interactive children stay in the shell's process group"); |
| 1714 | } |
| 1715 | |
| 1716 | static void iso_sigint_direct(Rep *rp) |
| 1717 | { |
| 1718 | T t; |
| 1719 | t_begin(&t, "/usr/bin:/bin"); |
| 1720 | OshResult r; |
| 1721 | OshBuilder *b = nb(0, 0); |
| 1722 | helper(b, "sleep", "400", NULL); |
| 1723 | t.s.after_launch_hook = raise_int; |
| 1724 | double t0 = now(); |
| 1725 | int st = go(&t, b, &r); |
| 1726 | rep_fill(rp, &r, st, now() - t0); |
| 1727 | rp->exit_requested = t.s.exit_requested; |
| 1728 | t_end(&t); |
| 1729 | } |
| 1730 | |
| 1731 | static void test_sigint_direct_noninteractive(void) |
| 1732 | { |
| 1733 | Rep rp; |
| 1734 | int rc = run_isolated(iso_sigint_direct, &rp, 1, 15); |
| 1735 | CHECK(rc == 0 && rp.sigint_seen == 1 && rp.status == 0 && rp.termsig0 == 0 && rp.dt >= 0.3 && !rp.killed_by_int && !rp.exit_requested, |
| 1736 | "kill -INT <shell pid> while waiting (non-interactive): nothing forwarded, child finished (status %d termsig %d dt %.2f exit %d)", |
| 1737 | rp.status, rp.termsig0, rp.dt, rp.exit_requested); |
| 1738 | } |
| 1739 | |
| 1740 | static void iso_ign_inherited(Rep *rp) |
| 1741 | { |
| 1742 | signal(SIGINT, SIG_IGN); |
| 1743 | signal(SIGQUIT, SIG_IGN); |
| 1744 | T t; |
| 1745 | t_begin(&t, "/usr/bin:/bin"); |
| 1746 | OshResult r; |
| 1747 | OshBuilder *b = nb(0, 0); |
| 1748 | helper(b, "sigdisp", NULL); |
| 1749 | int st = go(&t, b, &r); |
| 1750 | char *o = t_out(&t); |
| 1751 | rp->extra[0] = strcmp(o, "INT ign QUIT ign\n") == 0; |
| 1752 | snprintf(rp->text, sizeof rp->text, "%s", o); |
| 1753 | free(o); |
| 1754 | struct sigaction sa; |
| 1755 | sigaction(SIGINT, NULL, &sa); |
| 1756 | rp->extra[1] = sa.sa_handler == SIG_IGN; /* the embedder's own disposition is restored */ |
| 1757 | rp->status = st; |
| 1758 | t_end(&t); |
| 1759 | } |
| 1760 | |
| 1761 | static void iso_ign_not_inherited(Rep *rp) |
| 1762 | { |
| 1763 | T t; |
| 1764 | t_begin(&t, "/usr/bin:/bin"); |
| 1765 | OshResult r; |
| 1766 | OshBuilder *b = nb(0, 0); |
| 1767 | helper(b, "sigdisp", NULL); |
| 1768 | go(&t, b, &r); |
| 1769 | char *o = t_out(&t); |
| 1770 | rp->extra[0] = strcmp(o, "INT dfl QUIT dfl\n") == 0; |
| 1771 | snprintf(rp->text, sizeof rp->text, "%s", o); |
| 1772 | free(o); |
| 1773 | t_end(&t); |
| 1774 | } |
| 1775 | |
| 1776 | static void test_inherited_ignore(void) |
| 1777 | { |
| 1778 | Rep rp; |
| 1779 | int rc = run_isolated(iso_ign_inherited, &rp, 0, 15); |
| 1780 | CHECK(rc == 0 && rp.extra[0] == 1, "SIGINT/SIGQUIT ignored at entry stay ignored in children (POSIX); child saw: %s", rp.text); |
| 1781 | CHECK(rc == 0 && rp.extra[1] == 1, "embedder's ignore disposition restored after the run"); |
| 1782 | rc = run_isolated(iso_ign_not_inherited, &rp, 0, 15); |
| 1783 | CHECK(rc == 0 && rp.extra[0] == 1, "children get default SIGINT/SIGQUIT when the shell did not have them ignored; child saw: %s", rp.text); |
| 1784 | } |
| 1785 | |
| 1786 | /* ---- BUG3: a stopped member anywhere ends the pipeline ---- */ |
| 1787 | |
| 1788 | static void iso_stop_tail(Rep *rp) |
| 1789 | { |
| 1790 | T t; |
| 1791 | t_begin(&t, "/usr/bin:/bin"); |
| 1792 | OshResult r; |
| 1793 | OshBuilder *b = nb(0, 0); |
| 1794 | cmd(b, 0, "yes", NULL); |
| 1795 | cmd(b, 0, "sh", "-c", "kill -STOP $$", NULL); |
| 1796 | double t0 = now(); |
| 1797 | int st = go(&t, b, &r); |
| 1798 | rep_fill(rp, &r, st, now() - t0); |
| 1799 | rp->extra[0] = waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD; /* nothing left unreaped */ |
| 1800 | char *e = t_err(&t); |
| 1801 | rp->extra[1] = strstr(e, "stopped") != NULL; |
| 1802 | free(e); |
| 1803 | t_end(&t); |
| 1804 | } |
| 1805 | |
| 1806 | static void iso_stop_head(Rep *rp) |
| 1807 | { |
| 1808 | T t; |
| 1809 | t_begin(&t, "/usr/bin:/bin"); |
| 1810 | OshResult r; |
| 1811 | OshBuilder *b = nb(0, 0); |
| 1812 | helper(b, "kill", "19", NULL); |
| 1813 | helper(b, "sleep", "20000", NULL); |
| 1814 | helper(b, "cat", NULL); |
| 1815 | double t0 = now(); |
| 1816 | int st = go(&t, b, &r); |
| 1817 | rep_fill(rp, &r, st, now() - t0); |
| 1818 | rp->extra[0] = waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD; |
| 1819 | t_end(&t); |
| 1820 | } |
| 1821 | |
| 1822 | static void iso_stop_middle(Rep *rp) |
| 1823 | { |
| 1824 | T t; |
| 1825 | t_begin(&t, "/usr/bin:/bin"); |
| 1826 | OshResult r; |
| 1827 | OshBuilder *b = nb(0, 0); |
| 1828 | helper(b, "sleep", "20000", NULL); |
| 1829 | helper(b, "kill", "20", NULL); |
| 1830 | helper(b, "cat", NULL); |
| 1831 | double t0 = now(); |
| 1832 | int st = go(&t, b, &r); |
| 1833 | rep_fill(rp, &r, st, now() - t0); |
| 1834 | rp->extra[0] = waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD; |
| 1835 | t_end(&t); |
| 1836 | } |
| 1837 | |
| 1838 | static void test_stopped_member(void) |
| 1839 | { |
| 1840 | Rep rp; |
| 1841 | int rc = run_isolated(iso_stop_tail, &rp, 1, 10); |
| 1842 | CHECK(rc == 0 && rp.dt < 2.0, "yes | sh -c 'kill -STOP $$' finishes within 2 s (took %.2f, rc %d)", rp.dt, rc); |
| 1843 | CHECK(rc == 0 && rp.status == 128 + SIGSTOP && rp.outcome1 == OSH_OUT_CANCELLED && rp.outcome0 == OSH_OUT_CANCELLED && rp.err == OSH_E_INTERRUPTED, |
| 1844 | "stopped last member: status %d (want 147), outcomes %d/%d, err %d", rp.status, rp.outcome0, rp.outcome1, rp.err); |
| 1845 | CHECK(rc == 0 && rp.extra[0] == 1 && rp.extra[1] == 1, "no child left unreaped (%d) and a diagnostic mentions 'stopped' (%d)", rp.extra[0], rp.extra[1]); |
| 1846 | rc = run_isolated(iso_stop_head, &rp, 1, 10); |
| 1847 | CHECK(rc == 0 && rp.dt < 2.0 && rp.status == 128 + SIGSTOP && rp.extra[0] == 1, |
| 1848 | "stopped FIRST member of a 3-stage pipeline ends it: dt %.2f status %d reaped %d", rp.dt, rp.status, rp.extra[0]); |
| 1849 | rc = run_isolated(iso_stop_middle, &rp, 1, 10); |
| 1850 | CHECK(rc == 0 && rp.dt < 2.0 && rp.status == 128 + SIGTSTP && rp.extra[0] == 1, |
| 1851 | "stopped MIDDLE member (SIGTSTP) ends it: dt %.2f status %d reaped %d", rp.dt, rp.status, rp.extra[0]); |
| 1852 | } |
| 1853 | |
| 1854 | /* ---- BUG1: interactive session on a pseudo-terminal ---- */ |
| 1855 | |
| 1856 | static void iso_pty_common(Rep *rp, int mode) |
| 1857 | { |
| 1858 | /* mode 0: ^C typed on the terminal; 1: kill -INT <shell pid>; 2: stopped member */ |
| 1859 | setsid(); |
| 1860 | int master = posix_openpt(O_RDWR | O_NOCTTY); |
| 1861 | if (master < 0 || grantpt(master) || unlockpt(master)) { rp->extra[5] = -1; return; } |
| 1862 | char *sn = ptsname(master); |
| 1863 | int slave = sn ? open(sn, O_RDWR) : -1; /* the session leader without a terminal acquires it */ |
| 1864 | if (slave < 0) { rp->extra[5] = -1; return; } |
| 1865 | rp->extra[2] = tcgetpgrp(slave) == getpgrp(); /* shell owns the terminal before */ |
| 1866 | T t; |
| 1867 | t_begin(&t, "/usr/bin:/bin"); |
| 1868 | t.s.interactive = 1; |
| 1869 | t.s.tty_fd = slave; |
| 1870 | OshResult r; |
| 1871 | OshBuilder *b = nb(0, 0); |
| 1872 | if (mode == 2) { |
| 1873 | cmd(b, 0, "yes", NULL); |
| 1874 | cmd(b, 0, "sh", "-c", "kill -STOP $$", NULL); |
| 1875 | } else { |
| 1876 | helper(b, "sleep", "8000", NULL); |
| 1877 | helper(b, "cat", NULL); |
| 1878 | } |
| 1879 | t.s.after_launch_hook = mode == 1 ? raise_int : NULL; |
| 1880 | if (mode == 0) { |
| 1881 | /* the terminal driver sends ^C to the FOREGROUND group: type it once the pipeline owns the terminal */ |
| 1882 | pid_t kid = fork(); |
| 1883 | if (kid == 0) { |
| 1884 | usleep(400000); |
| 1885 | if (write(master, "\003", 1) < 0) {} |
| 1886 | _exit(0); |
| 1887 | } |
| 1888 | double t0 = now(); |
| 1889 | int st = go(&t, b, &r); |
| 1890 | rep_fill(rp, &r, st, now() - t0); |
| 1891 | waitpid(kid, NULL, 0); |
| 1892 | } else { |
| 1893 | double t0 = now(); |
| 1894 | int st = go(&t, b, &r); |
| 1895 | rep_fill(rp, &r, st, now() - t0); |
| 1896 | } |
| 1897 | rp->extra[3] = tcgetpgrp(slave) == getpgrp(); /* the shell took the terminal back */ |
| 1898 | rp->exit_requested = t.s.exit_requested; |
| 1899 | rp->extra[4] = waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD; |
| 1900 | t_end(&t); |
| 1901 | } |
| 1902 | |
| 1903 | static void iso_pty_ctrl_c(Rep *rp) { iso_pty_common(rp, 0); } |
| 1904 | static void iso_pty_kill_shell(Rep *rp) { iso_pty_common(rp, 1); } |
| 1905 | static void iso_pty_stop(Rep *rp) { iso_pty_common(rp, 2); } |
| 1906 | |
| 1907 | static void iso_pty_pgid(Rep *rp) |
| 1908 | { |
| 1909 | setsid(); |
| 1910 | int master = posix_openpt(O_RDWR | O_NOCTTY); |
| 1911 | if (master < 0 || grantpt(master) || unlockpt(master)) { rp->extra[5] = -1; return; } |
| 1912 | char *sn = ptsname(master); |
| 1913 | int slave = sn ? open(sn, O_RDWR) : -1; |
| 1914 | if (slave < 0) { rp->extra[5] = -1; return; } |
| 1915 | T t; |
| 1916 | t_begin(&t, "/usr/bin:/bin"); |
| 1917 | t.s.interactive = 1; |
| 1918 | t.s.tty_fd = slave; |
| 1919 | OshResult r; |
| 1920 | OshBuilder *b = nb(0, 0); |
| 1921 | helper(b, "pgid", NULL); |
| 1922 | helper(b, "cat", NULL); |
| 1923 | go(&t, b, &r); |
| 1924 | char *o = t_out(&t); |
| 1925 | int pg = atoi(o); |
| 1926 | free(o); |
| 1927 | rp->extra[0] = pg > 0 && pg != (int)getpgrp(); /* its own group, not the shell's */ |
| 1928 | /* SIGINT delivered to a child in an interactive session is back to default: even if ignored at entry */ |
| 1929 | signal(SIGINT, SIG_IGN); |
| 1930 | t_clear(&t); |
| 1931 | b = nb(0, 0); |
| 1932 | helper(b, "sigdisp", NULL); |
| 1933 | go(&t, b, &r); |
| 1934 | o = t_out(&t); |
| 1935 | rp->extra[1] = strcmp(o, "INT dfl QUIT dfl\n") == 0; |
| 1936 | free(o); |
| 1937 | t_end(&t); |
| 1938 | } |
| 1939 | |
| 1940 | static void test_interactive_pty(void) |
| 1941 | { |
| 1942 | Rep rp; |
| 1943 | int rc = run_isolated(iso_pty_pgid, &rp, 0, 15); |
| 1944 | if (rc == 0 && rp.extra[5] == -1) { printf("note: no pseudo-terminal available, interactive tests skipped\n"); g_pass++; return; } |
| 1945 | CHECK(rc == 0 && rp.extra[0] == 1, "interactive: pipeline runs in its own process group"); |
| 1946 | CHECK(rc == 0 && rp.extra[1] == 1, "interactive: children get default SIGINT/SIGQUIT"); |
| 1947 | rc = run_isolated(iso_pty_ctrl_c, &rp, 0, 20); |
| 1948 | CHECK(rc == 0 && rp.extra[2] == 1, "interactive: shell owns the terminal before the pipeline"); |
| 1949 | CHECK(rc == 0 && rp.dt < 4.0 && rp.termsig0 == SIGINT && rp.termsig1 == SIGINT && rp.killed_by_int == 1, |
| 1950 | "^C on the terminal reaches the pipeline's group (dt %.2f termsig %d/%d)", rp.dt, rp.termsig0, rp.termsig1); |
| 1951 | CHECK(rc == 0 && rp.sigint_seen == 0 && rp.exit_requested == 0, "^C does not reach the interactive shell (seen %d) and the shell does not exit", rp.sigint_seen); |
| 1952 | CHECK(rc == 0 && rp.extra[3] == 1 && rp.extra[4] == 1, "shell takes the terminal back after the pipeline (%d) and nothing is left unreaped (%d)", rp.extra[3], rp.extra[4]); |
| 1953 | rc = run_isolated(iso_pty_kill_shell, &rp, 0, 20); |
| 1954 | CHECK(rc == 0 && rp.dt < 4.0 && rp.termsig0 == SIGINT && rp.sigint_seen == 1, |
| 1955 | "kill -INT <shell pid> while waiting (interactive): forwarded to the pipeline's group (dt %.2f termsig %d seen %d)", rp.dt, rp.termsig0, rp.sigint_seen); |
| 1956 | CHECK(rc == 0 && rp.extra[3] == 1 && rp.exit_requested == 0, "terminal returned, interactive shell does not exit"); |
| 1957 | rc = run_isolated(iso_pty_stop, &rp, 0, 20); |
| 1958 | CHECK(rc == 0 && rp.dt < 2.0 && rp.status == 128 + SIGSTOP && rp.outcome1 == OSH_OUT_CANCELLED && rp.extra[3] == 1 && rp.extra[4] == 1, |
| 1959 | "interactive stopped member: cancelled within 2 s (dt %.2f status %d), terminal returned %d, reaped %d", rp.dt, rp.status, rp.extra[3], rp.extra[4]); |
| 1960 | } |
| 1961 | |
| 1962 | /* ================= review fixes (PR 337): printf table and wired adversarial checks ================= */ |
| 1963 | |
| 1964 | typedef struct { |
| 1965 | const char *fmt, *a1, *a2, *a3; |
| 1966 | } PfRow; |
| 1967 | |
| 1968 | /* SAME: stdout and status equal /usr/bin/printf (its stderr is not compared). */ |
| 1969 | static const PfRow k_pf_same[] = { |
| 1970 | {"\\%q|\\n", "5", NULL, NULL}, /* review B2: backslash hides % from the old validator */ |
| 1971 | {"\\%", "x", NULL, NULL}, /* review B2: % at the very end after a backslash, extra arg follows */ |
| 1972 | {"\\%d\\n", "7", NULL, NULL}, |
| 1973 | {"\\\\%d\\n", "7", NULL, NULL}, |
| 1974 | {"a\\", NULL, NULL, NULL}, /* lone trailing backslash */ |
| 1975 | {"\\", "x", NULL, NULL}, |
| 1976 | {"\\%s|\\n", "a", NULL, NULL}, |
| 1977 | {"%%%%\\n", NULL, NULL, NULL}, |
| 1978 | {"%%d\\n", "5", NULL, NULL}, |
| 1979 | {"%s%%%s\\n", "a", "b", NULL}, |
| 1980 | {"%s\\n", "", NULL, NULL}, |
| 1981 | {"%d\\n", "-5", NULL, NULL}, |
| 1982 | {"%d\\n", "9223372036854775807", NULL, NULL}, |
| 1983 | {"%d\\n", "9223372036854775808", NULL, NULL}, |
| 1984 | {"%u\\n", "-1", NULL, NULL}, |
| 1985 | {"%x\\n", "-1", NULL, NULL}, |
| 1986 | {"%o\\n", "0777", NULL, NULL}, |
| 1987 | {"%c|\\n", "", NULL, NULL}, |
| 1988 | {"%c|\\n", "hello", NULL, NULL}, |
| 1989 | {"%s %s %s\\n", "a", "b", NULL}, |
| 1990 | {"%s\\n", "a", "b", "c"}, |
| 1991 | {"%d\\n", "1", "2", "3"}, |
| 1992 | {"%i\\n", "0x10", NULL, NULL}, |
| 1993 | {"%d\\n", "'x", NULL, NULL}, |
| 1994 | {"%d\\n", "\"ab", NULL, NULL}, |
| 1995 | {"\\101\\102\\n", NULL, NULL, NULL}, |
| 1996 | {"\\1012\\n", NULL, NULL, NULL}, |
| 1997 | {"\\8\\n", NULL, NULL, NULL}, |
| 1998 | {"\\\"q\\\"\\n", NULL, NULL, NULL}, |
| 1999 | {"x\\ty\\n", NULL, NULL, NULL}, |
| 2000 | {"%s\\\\n", "a", NULL, NULL}, |
| 2001 | {"%d%d\\n", "1", "2", NULL}, |
| 2002 | {"%s", "-", NULL, NULL}, |
| 2003 | {"\\a\\b\\f\\r\\v", NULL, NULL, NULL}, |
| 2004 | {"\\q\\z\\n", NULL, NULL, NULL}, |
| 2005 | {"%5d\\n", "3", NULL, NULL}, |
| 2006 | {"%-3s|\\n", "a", NULL, NULL}, |
| 2007 | {"%.2s\\n", "abc", NULL, NULL}, |
| 2008 | {"%ld\\n", "5", NULL, NULL}, |
| 2009 | {"%X\\n", "255", NULL, NULL}, |
| 2010 | {"%b\\n", "a", NULL, NULL}, |
| 2011 | {"%e\\n", "1", NULL, NULL}, |
| 2012 | {"%q\\n", "5", NULL, NULL}, |
| 2013 | {"ab%q", "5", NULL, NULL}, |
| 2014 | {"%s%q\\n", "x", "y", NULL}, |
| 2015 | {"%5.2f|%-6s|%+.3d|%08.3f\\n", "3.14159", "ab", "5"}, |
| 2016 | {"%b|%b\\n", "a\\tb\\0101", "x\\\\y", NULL}, |
| 2017 | {"%*d|%-*d|\\n", "3", "7", "2"}, |
| 2018 | {"%#x %#o % d\\n", "255", "8", "4"}, |
| 2019 | }; |
| 2020 | |
| 2021 | /* REFUSE: outside the documented subset or malformed. The builtin refuses BEFORE printing anything and returns 1; |
| 2022 | * /usr/bin/printf prints what precedes the bad conversion, then fails (also 1). Intentional difference. */ |
| 2023 | static const PfRow k_pf_refuse[] = { |
| 2024 | {"%", NULL, NULL, NULL}, |
| 2025 | {"a%", "x", NULL, NULL}, |
| 2026 | {"%\\n", NULL, NULL, NULL}, |
| 2027 | {"\\%%\\n", NULL, NULL, NULL}, |
| 2028 | {"%s %z\\n", "a", NULL, NULL}, |
| 2029 | }; |
| 2030 | |
| 2031 | /* DIFF: accepted by the builtin but intentionally not like coreutils (documented in OSH_PLATFORM_ABI / PR body). */ |
| 2032 | static const struct { const char *fmt, *want; } k_pf_diff[] = { |
| 2033 | {"\\x41\\n", "\\x41\n"}, |
| 2034 | {"\\u0041\\n", "\\u0041\n"}, |
| 2035 | {"a\\cb\\n", "a\\cb\n"}, |
| 2036 | }; |
| 2037 | |
| 2038 | static void test_printf_review(void) |
| 2039 | { |
| 2040 | T t; |
| 2041 | t_begin(&t, "/usr/bin:/bin"); |
| 2042 | OshResult r; |
| 2043 | int nsame = (int)(sizeof k_pf_same / sizeof k_pf_same[0]); |
| 2044 | for (int i = 0; i < nsame; i++) { |
| 2045 | const PfRow *w = &k_pf_same[i]; |
| 2046 | t_clear(&t); |
| 2047 | char *res[2]; |
| 2048 | size_t len[2] = {0, 0}; |
| 2049 | int st[2]; |
| 2050 | for (int k = 0; k < 2; k++) { |
| 2051 | t_clear(&t); |
| 2052 | OshBuilder *b = nb(0, 0); |
| 2053 | if (k == 0) cmd(b, OSH_B_PRINTF, "printf", w->fmt, NULL); |
| 2054 | else cmd(b, 0, "/usr/bin/printf", w->fmt, NULL); |
| 2055 | if (w->a1) osh_rb_arg(b, w->a1); |
| 2056 | if (w->a2) osh_rb_arg(b, w->a2); |
| 2057 | if (w->a3) osh_rb_arg(b, w->a3); |
| 2058 | st[k] = go(&t, b, &r); |
| 2059 | res[k] = t_out(&t); |
| 2060 | len[k] = g_slurp_len; |
| 2061 | } |
| 2062 | CHECK(len[0] == len[1] && memcmp(res[0], res[1], len[0]) == 0 && st[0] == st[1], "printf table #%d fmt '%s': builtin '%s' (%d) vs /usr/bin/printf '%s' (%d)", i, w->fmt, res[0], st[0], res[1], st[1]); |
| 2063 | free(res[0]); |
| 2064 | free(res[1]); |
| 2065 | } |
| 2066 | int nref = (int)(sizeof k_pf_refuse / sizeof k_pf_refuse[0]); |
| 2067 | for (int i = 0; i < nref; i++) { |
| 2068 | const PfRow *w = &k_pf_refuse[i]; |
| 2069 | t_clear(&t); |
| 2070 | OshBuilder *b = nb(0, 0); |
| 2071 | cmd(b, OSH_B_PRINTF, "printf", w->fmt, NULL); |
| 2072 | if (w->a1) osh_rb_arg(b, w->a1); |
| 2073 | if (w->a2) osh_rb_arg(b, w->a2); |
| 2074 | int st = go(&t, b, &r); |
| 2075 | char *o = t_out(&t), *e = t_err(&t); |
| 2076 | CHECK(st == 1 && o[0] == 0 && e[0] != 0, "printf refuse #%d fmt '%s': status %d, stdout '%s', stderr '%s'", i, w->fmt, st, o, e); |
| 2077 | free(o); |
| 2078 | free(e); |
| 2079 | } |
| 2080 | int ndiff = (int)(sizeof k_pf_diff / sizeof k_pf_diff[0]); |
| 2081 | for (int i = 0; i < ndiff; i++) { |
| 2082 | t_clear(&t); |
| 2083 | OshBuilder *b = nb(0, 0); |
| 2084 | cmd(b, OSH_B_PRINTF, "printf", k_pf_diff[i].fmt, NULL); |
| 2085 | int st = go(&t, b, &r); |
| 2086 | char *o = t_out(&t); |
| 2087 | CHECK(st == 0 && strcmp(o, k_pf_diff[i].want) == 0, "printf documented difference '%s': got '%s'", k_pf_diff[i].fmt, o); |
| 2088 | free(o); |
| 2089 | } |
| 2090 | /* never read past the NUL: the argument after a trailing % or \ must never show up as format text */ |
| 2091 | t_clear(&t); |
| 2092 | OshBuilder *b = nb(0, 0); |
| 2093 | cmd(b, OSH_B_PRINTF, "printf", "\\%", "SECRET", NULL); |
| 2094 | go(&t, b, &r); |
| 2095 | char *o = t_out(&t); |
| 2096 | CHECK(strstr(o, "SECRET") == NULL, "format scan stopped at the terminator, got '%s'", o); |
| 2097 | free(o); |
| 2098 | t_end(&t); |
| 2099 | } |
| 2100 | |
| 2101 | static void test_adversarial(void) |
| 2102 | { |
| 2103 | T t; |
| 2104 | t_begin(&t, "/usr/bin:/bin"); |
| 2105 | t.s.fd[0] = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 2106 | OshResult r; |
| 2107 | OshBuilder *b; |
| 2108 | /* fd and zombie hygiene on six error-path shapes, repeated (review R1) */ |
| 2109 | int base = count_fds(), zomb = 0; |
| 2110 | for (int it = 0; it < 150; it++) { |
| 2111 | b = nb(0, 0); cmd(b, 0, "true", NULL); cmd(b, 0, "true", NULL); cmd(b, 0, "true", NULL); |
| 2112 | t.s.fail_fork_at = 3; go(&t, b, &r); t.s.fail_fork_at = 0; |
| 2113 | b = nb(0, 0); cmd(b, 0, "true", NULL); cmd(b, 0, "cat", NULL); osh_rb_redir(b, OSH_R_IN, 0, "/nonexistent/x", 0); go(&t, b, &r); |
| 2114 | b = nb(0, 0); cmd(b, 0, "true", NULL); cmd(b, 0, "nope-zz", NULL); cmd(b, 0, "cat", NULL); go(&t, b, &r); |
| 2115 | b = nb(0, 0); cmd(b, OSH_B_PWD, "pwd", NULL); osh_rb_redir(b, OSH_R_OUT, 1, "/dev/null", 0); osh_rb_redir(b, OSH_R_IN, 0, "/nonexistent/x", 0); go(&t, b, &r); |
| 2116 | b = nb(0, 0); cmd(b, OSH_B_PWD, "pwd", NULL); osh_rb_redir(b, OSH_R_OUT, 1, "/dev/null", 0); osh_rb_redir(b, OSH_R_DUP, 2, NULL, 1); go(&t, b, &r); |
| 2117 | b = nb(0, 0); cmd(b, OSH_B_PWD, "pwd", NULL); cmd(b, 0, "cat", NULL); go(&t, b, &r); |
| 2118 | } |
| 2119 | if (!(waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD)) zomb = 1; |
| 2120 | CHECK(!zomb, "no unreaped child after 900 error-path runs"); |
| 2121 | CHECK(count_fds() == base, "no descriptor leak after 900 error-path runs (%d vs %d)", count_fds(), base); |
| 2122 | /* exit / cd / export inside a pipeline do not touch the parent */ |
| 2123 | b = nb(0, 0); cmd(b, OSH_B_EXIT, "exit", "3", NULL); cmd(b, 0, "cat", NULL); |
| 2124 | go(&t, b, &r); |
| 2125 | CHECK(!r.exit_requested && !t.s.exit_requested, "exit in a pipeline does not exit the shell"); |
| 2126 | char *cwd0 = getcwd(NULL, 0); |
| 2127 | b = nb(0, 0); cmd(b, OSH_B_CD, "cd", "/", NULL); cmd(b, OSH_B_EXPORT, "export", "QQ=1", NULL); go(&t, b, &r); |
| 2128 | char *cwd1 = getcwd(NULL, 0); |
| 2129 | CHECK(strcmp(cwd0, cwd1) == 0 && osh_var_get(&t.s, "QQ") == NULL, "cd/export in a pipeline do not touch the shell"); |
| 2130 | free(cwd0); |
| 2131 | free(cwd1); |
| 2132 | /* PATH edge cases */ |
| 2133 | char longn[5000], longp[5000], out[PATH_MAX]; |
| 2134 | memset(longn, 'a', 4999); longn[4999] = 0; |
| 2135 | b = nb(0, 0); cmd(b, 0, longn, NULL); |
| 2136 | CHECK(go(&t, b, &r) == 127, "over-long command name is not found"); |
| 2137 | longp[0] = '/'; memset(longp + 1, 'a', 4990); longp[4991] = 0; |
| 2138 | b = nb(0, 0); cmd(b, 0, longp, NULL); |
| 2139 | int st = go(&t, b, &r); |
| 2140 | CHECK(st == 127 || st == 126, "over-long path status %d", st); |
| 2141 | osh_var_unset(&t.s, "PATH"); |
| 2142 | CHECK(osh_resolve_linux(&t.s, "true", out, sizeof out) == 127, "PATH unset: not found"); |
| 2143 | osh_var_export(&t.s, "PATH", "/usr/bin:/bin"); |
| 2144 | CHECK(osh_resolve_linux(&t.s, "true", out, sizeof out) == 0 && osh_resolve_linux(&t.s, "", out, sizeof out) == 127, "PATH resolution and empty name"); |
| 2145 | /* duplicate prefix assignments keep the last one, nothing leaks into the session, unexported stays private */ |
| 2146 | t_clear(&t); |
| 2147 | b = nb(0, 0); cmd(b, 0, "printenv", "ZZV", NULL); osh_rb_assign(b, "ZZV", "1"); osh_rb_assign(b, "ZZV", "2"); |
| 2148 | go(&t, b, &r); |
| 2149 | char *o = t_out(&t); |
| 2150 | CHECK(strcmp(o, "2\n") == 0 && osh_var_get(&t.s, "ZZV") == NULL, "duplicate prefix assignment: '%s'", o); |
| 2151 | free(o); |
| 2152 | osh_var_set(&t.s, "UNEXP", "v"); |
| 2153 | b = nb(0, 0); cmd(b, 0, "printenv", "UNEXP", NULL); |
| 2154 | CHECK(go(&t, b, &r) == 1, "unexported variable is not passed to children"); |
| 2155 | /* printf to a full device reports a write error */ |
| 2156 | b = nb(0, 0); cmd(b, OSH_B_PRINTF, "printf", "%s\n", "x", NULL); osh_rb_redir(b, OSH_R_OUT, 1, "/dev/full", 0); |
| 2157 | CHECK(go(&t, b, &r) == 1, "printf to /dev/full fails with status 1"); |
| 2158 | /* a script without a shebang: DECISION (review N1): the adapter does not fall back to sh; execve's ENOEXEC is |
| 2159 | * reported as 126, like a file that is found but cannot be executed. */ |
| 2160 | wfile(P("noshebang.sh"), "echo hi\n", 0755); |
| 2161 | b = nb(0, 0); cmd(b, 0, P("noshebang.sh"), NULL); |
| 2162 | CHECK(go(&t, b, &r) == 126, "no-shebang script is 126 (documented: no sh fallback)"); |
| 2163 | close(t.s.fd[0]); |
| 2164 | t_end(&t); |
| 2165 | } |
| 2166 | |
| 2167 | static void iso_low_nofile(Rep *rp) |
| 2168 | { |
| 2169 | struct rlimit rl; |
| 2170 | getrlimit(RLIMIT_NOFILE, &rl); |
| 2171 | rl.rlim_cur = 12; |
| 2172 | setrlimit(RLIMIT_NOFILE, &rl); |
| 2173 | T t; |
| 2174 | t_begin(&t, "/usr/bin:/bin"); |
| 2175 | t.s.fd[0] = open("/dev/null", O_RDONLY | O_CLOEXEC); |
| 2176 | int base = count_fds(); |
| 2177 | OshResult r; |
| 2178 | OshBuilder *b = nb(0, 0); |
| 2179 | for (int i = 0; i < 5; i++) cmd(b, 0, "cat", NULL); |
| 2180 | int st = go(&t, b, &r); |
| 2181 | rp->status = st; |
| 2182 | rp->extra[0] = waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD; |
| 2183 | rp->extra[1] = count_fds() == base; |
| 2184 | for (int i = 0; i < 100; i++) { |
| 2185 | b = nb(0, 0); cmd(b, 0, "true", NULL); osh_rb_redir(b, OSH_R_OUT, 1, "/dev/null", 0); go(&t, b, &r); |
| 2186 | b = nb(0, 0); cmd(b, 0, "true", NULL); cmd(b, 0, "cat", NULL); cmd(b, 0, "cat", NULL); cmd(b, 0, "cat", NULL); cmd(b, 0, "cat", NULL); go(&t, b, &r); |
| 2187 | } |
| 2188 | rp->extra[2] = waitpid(-1, NULL, WNOHANG) == -1 && errno == ECHILD && count_fds() == base; |
| 2189 | close(t.s.fd[0]); |
| 2190 | t_end(&t); |
| 2191 | } |
| 2192 | |
| 2193 | static void iso_child_mask(Rep *rp) |
| 2194 | { |
| 2195 | sigset_t m; |
| 2196 | sigemptyset(&m); |
| 2197 | sigaddset(&m, SIGTERM); |
| 2198 | sigaddset(&m, SIGUSR1); |
| 2199 | sigprocmask(SIG_BLOCK, &m, NULL); |
| 2200 | T t; |
| 2201 | t_begin(&t, "/usr/bin:/bin"); |
| 2202 | OshResult r; |
| 2203 | OshBuilder *b = nb(0, 0); |
| 2204 | helper(b, "sigblk", NULL); |
| 2205 | go(&t, b, &r); |
| 2206 | char *o = t_out(&t); |
| 2207 | rp->extra[0] = strcmp(o, "blocked 0\n") == 0; |
| 2208 | free(o); |
| 2209 | sigset_t cur; |
| 2210 | sigprocmask(SIG_BLOCK, NULL, &cur); |
| 2211 | rp->extra[1] = sigismember(&cur, SIGTERM) && sigismember(&cur, SIGUSR1); /* embedder's mask restored */ |
| 2212 | t_end(&t); |
| 2213 | } |
| 2214 | |
| 2215 | static void test_adversarial_isolated(void) |
| 2216 | { |
| 2217 | Rep rp; |
| 2218 | int rc = run_isolated(iso_low_nofile, &rp, 1, 60); |
| 2219 | CHECK(rc == 0 && rp.extra[0] && rp.extra[1] && rp.extra[2], "RLIMIT_NOFILE=12: 5-stage pipeline status %d, no zombie %d, no leak %d/%d", rp.status, rp.extra[0], rp.extra[1], rp.extra[2]); |
| 2220 | rc = run_isolated(iso_child_mask, &rp, 0, 15); |
| 2221 | CHECK(rc == 0 && rp.extra[0] == 1 && rp.extra[1] == 1, "children start with an empty signal mask; the embedder's mask is restored"); |
| 2222 | /* decoder fuzz: mutated records are accepted or refused, never crash, accepted pointers stay inside the arena */ |
| 2223 | OshBuilder g; |
| 2224 | osh_rb_init(&g, 0, 0); |
| 2225 | osh_rb_cmd(&g, 0); osh_rb_arg(&g, "true"); |
| 2226 | osh_rb_cmd(&g, 0); osh_rb_arg(&g, "cat"); osh_rb_assign(&g, "A", "b"); osh_rb_redir(&g, OSH_R_OUT, 1, "/dev/null", 0); |
| 2227 | osh_rb_seal(&g); |
| 2228 | static OshRequest rq; |
| 2229 | static uint64_t rec[OSH_REQ_HDR_CELLS + OSH_MAX_CMDS * OSH_CMD_CELLS], out[OSH_OUT_CAP]; |
| 2230 | unsigned seed = 1; |
| 2231 | int accepted = 0, refused = 0, badptr = 0; |
| 2232 | for (int i = 0; i < 100000; i++) { |
| 2233 | memcpy(rec, g.rec, sizeof rec); |
| 2234 | memcpy(out, g.out, sizeof out); |
| 2235 | int k = 1 + rand_r(&seed) % 3; |
| 2236 | for (int j = 0; j < k; j++) { |
| 2237 | size_t idx = rand_r(&seed) % (OSH_REQ_HDR_CELLS + 2 * OSH_CMD_CELLS + 4); |
| 2238 | uint64_t v; |
| 2239 | switch (rand_r(&seed) % 5) { |
| 2240 | case 0: v = ~0ull; break; |
| 2241 | case 1: v = (uint64_t)rand_r(&seed); break; |
| 2242 | case 2: v = 1ull << 63; break; |
| 2243 | case 3: v = 0xFFFFFFFFull; break; |
| 2244 | default: v = (uint64_t)(rand_r(&seed) % 9000); |
| 2245 | } |
| 2246 | rec[idx] = v; |
| 2247 | } |
| 2248 | size_t nrec = OSH_REQ_HDR_CELLS + 2 * OSH_CMD_CELLS; |
| 2249 | if (rand_r(&seed) % 8 == 0) nrec = (size_t)rand_r(&seed) % (nrec + 1); |
| 2250 | int dc = osh_req_decode(rec, nrec, out, rand_r(&seed) % 4 ? OSH_OUT_CAP : (size_t)(rand_r(&seed) % 100), &rq); |
| 2251 | if (dc) { refused++; continue; } |
| 2252 | accepted++; |
| 2253 | for (int c = 0; c < rq.ncmds; c++) |
| 2254 | for (int a = 0; a < rq.cmd[c].nargv; a++) |
| 2255 | if (!rq.cmd[c].argv[a] || rq.cmd[c].argv[a] < rq.arena || rq.cmd[c].argv[a] > rq.arena + sizeof rq.arena) badptr++; |
| 2256 | } |
| 2257 | CHECK(badptr == 0 && accepted > 0 && refused > 0, "decoder fuzz: %d accepted, %d refused, %d pointers outside the arena", accepted, refused, badptr); |
| 2258 | } |
| 2259 | |
| 2260 | /* Issue #347: the kernel silently discards SIGTSTP/SIGTTIN/SIGTTOU sent to a member of an ORPHANED process group (one where no |
| 2261 | * member has a parent in another group of the same session; man 7 signal, setpgid(2)). Run from a detached background loop or |
| 2262 | * under `setsid`, the test's own group is orphaned, so the "stopped child" checks saw the helper exit 0 instead of stopping. |
| 2263 | * The product is correct (it only observes stops the kernel delivers). The test must own its conditions: run the whole suite in |
| 2264 | * a child that leads a fresh group whose parent (this process) sits in the caller's group, so the group is never orphaned. */ |
| 2265 | static void anchor_pgrp(void) |
| 2266 | { |
| 2267 | pid_t kid = fork(); |
| 2268 | if (kid < 0) { printf("fork failed\n"); exit(2); } |
| 2269 | if (kid == 0) { setpgid(0, 0); return; } |
| 2270 | setpgid(kid, kid); |
| 2271 | int tty_fg = isatty(0) && tcgetpgrp(0) == getpgrp(); |
| 2272 | struct sigaction ign = {.sa_handler = SIG_IGN}, old; |
| 2273 | sigaction(SIGTTOU, &ign, &old); |
| 2274 | if (tty_fg) tcsetpgrp(0, kid); |
| 2275 | int st = 0; |
| 2276 | while (waitpid(kid, &st, 0) < 0 && errno == EINTR) {} |
| 2277 | if (tty_fg) tcsetpgrp(0, getpgrp()); |
| 2278 | sigaction(SIGTTOU, &old, NULL); |
| 2279 | if (WIFSIGNALED(st)) { printf("test run died of signal %d\n", WTERMSIG(st)); exit(128 + WTERMSIG(st)); } |
| 2280 | exit(WEXITSTATUS(st)); |
| 2281 | } |
| 2282 | |
| 2283 | int main(int argc, char **argv) |
| 2284 | { |
| 2285 | if (argc > 2 && !strcmp(argv[1], "--helper")) return helper_main(argc, argv); |
| 2286 | anchor_pgrp(); |
| 2287 | ssize_t n = readlink("/proc/self/exe", g_self, sizeof g_self - 1); |
| 2288 | if (n <= 0) { printf("cannot find own path\n"); return 2; } |
| 2289 | g_self[n] = 0; |
| 2290 | const char *base = getenv("TMPDIR"); |
| 2291 | snprintf(g_tmp, sizeof g_tmp, "%s/osh-host-XXXXXX", base && *base ? base : "/tmp"); |
| 2292 | if (!mkdtemp(g_tmp)) { printf("mkdtemp failed\n"); return 2; } |
| 2293 | char *cwd0 = getcwd(NULL, 0); |
| 2294 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 2295 | |
| 2296 | if (argc > 2 && !strcmp(argv[1], "--only")) { g_only_set = 1; g_only = argv[2]; } |
| 2297 | alarm(300); /* watchdog: a hang must fail the run, not stall it */ |
| 2298 | if (want("env")) { test_session_env(); test_raw_env(); test_session_levels(); } |
| 2299 | if (want("simple")) test_simple_and_path(); |
| 2300 | if (want("builtins")) test_builtins(); |
| 2301 | if (want("printf")) { test_printf(); test_printf_review(); } |
| 2302 | if (want("redirs")) test_redirs(); |
| 2303 | if (want("pipelines")) test_pipelines(); |
| 2304 | if (want("sigint")) { test_sigint_noninteractive(); test_sigint_direct_noninteractive(); test_inherited_ignore(); } |
| 2305 | if (want("stop")) test_stopped_member(); |
| 2306 | if (want("pty")) test_interactive_pty(); |
| 2307 | if (want("adversarial")) { test_adversarial(); test_adversarial_isolated(); } |
| 2308 | if (want("partial")) test_partial_launch(); |
| 2309 | if (want("fdleaks")) test_fd_leaks(); |
| 2310 | if (want("connectors")) test_connectors(); |
| 2311 | if (want("refusals")) test_refusals(); |
| 2312 | if (want("destinations")) test_destinations_and_authority(); |
| 2313 | if (want("direct")) test_direct_equals_record(); |
| 2314 | |
| 2315 | if (chdir(cwd0)) {} |
| 2316 | free(cwd0); |
| 2317 | nftw(g_tmp, rm_cb, 16, FTW_DEPTH | FTW_PHYS); |
| 2318 | printf("checks passed %d failed %d\n", g_pass, g_fail); |
| 2319 | printf(g_fail ? "OSH_HOST_FAIL\n" : "OSH_HOST_PASS\n"); |
| 2320 | return g_fail != 0; |
| 2321 | } |