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 / bench / inference_cost / audit.sh
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # Sourced only by driver.sh --self-test; all candidates below are disposable fixtures. |
| 3 | summary_status() { awk '$1=="status"{v=$2} END{print v}' "$1"; } |
| 4 | |
| 5 | scorer_probe() { # label expected_rc version-2 wire; challenge both wire versions |
| 6 | sp_label=$1 sp_want=$2 sp_wire=$3 |
| 7 | for sp_version in 1 2; do |
| 8 | sp_rc=0 |
| 9 | if [ "$sp_version" -eq 1 ]; then |
| 10 | printf '%s\n' "$sp_wire" | awk 'NR==1{$2=1} NR>2{$2=""; $3=""} {print}' | |
| 11 | "$BIN_DIR/scorer" > "$BUILD_DIR/selftest/probe.txt" 2>/dev/null || sp_rc=$? |
| 12 | else |
| 13 | printf '%s\n' "$sp_wire" | "$BIN_DIR/scorer" > "$BUILD_DIR/selftest/probe.txt" 2>/dev/null || sp_rc=$? |
| 14 | fi |
| 15 | st_expect "$sp_label v$sp_version rc" "$sp_want" "$sp_rc" |
| 16 | done |
| 17 | } |
| 18 | |
| 19 | audit_checks() { |
| 20 | say '== audit regressions' |
| 21 | scorer_probe 'trailing row' 2 'version 2 |
| 22 | attempts 1 |
| 23 | task01 om plain 1 1 0 1 10 111 37 148 |
| 24 | task02 om plain 1 1 0 0 10 111 37 148' |
| 25 | scorer_probe 'refused compile marked verified' 2 'version 2 |
| 26 | attempts 1 |
| 27 | task01 om plain 1 1 1 1 10 111 37 148' |
| 28 | scorer_probe 'duplicate verified task' 2 'version 2 |
| 29 | attempts 3 |
| 30 | task01 om feedback 1 1 0 1 10 111 37 148 |
| 31 | task01 om feedback 2 0 0 1 10 111 37 148 |
| 32 | task02 om feedback 1 1 0 0 10 111 37 148' |
| 33 | scorer_probe 'nonboolean verified flag' 2 'version 2 |
| 34 | attempts 1 |
| 35 | task01 om plain 1 1 0 2 10 111 37 148' |
| 36 | scorer_probe 'nonboolean first flag' 2 'version 2 |
| 37 | attempts 1 |
| 38 | task01 om plain 1 2 0 0 10 111 37 148' |
| 39 | scorer_probe 'skipped attempt' 2 'version 2 |
| 40 | attempts 1 |
| 41 | task01 om plain 2 0 0 0 10 111 37 148' |
| 42 | scorer_probe 'distinct interleaved tasks' 1 'version 2 |
| 43 | attempts 3 |
| 44 | task01 om feedback 1 1 0 0 10 111 37 148 |
| 45 | task02 om feedback 1 1 0 0 10 111 37 148 |
| 46 | task01 om feedback 2 0 0 1 10 111 37 148' |
| 47 | st_expect 'distinct tasks counted' 2 "$(meta tasks_total "$BUILD_DIR/selftest/probe.txt")" |
| 48 | st_expect 'distinct verified counted' 1 "$(meta verified_count "$BUILD_DIR/selftest/probe.txt")" |
| 49 | |
| 50 | ac_dir="$BUILD_DIR/selftest/security" |
| 51 | mkdir -p "$ac_dir" |
| 52 | cat > "$ac_dir/attack.py" <<EOF |
| 53 | from pathlib import Path |
| 54 | import sys |
| 55 | try: |
| 56 | Path("$ac_dir/host-marker").write_text("candidate wrote host file") |
| 57 | except OSError: |
| 58 | print("host write denied", file=sys.stderr) |
| 59 | sys.stdout.write("".join(Path("$TASKS_DIR/task01/expected.txt").read_text().splitlines(True)[2:])) |
| 60 | sys.exit(0) |
| 61 | EOF |
| 62 | evaluate py "$TASKS_DIR/task01" "$ac_dir/attack.py" "$ac_dir/attack" |
| 63 | st_expect 'oracle attack compiled' 0 "$COMP_RC" |
| 64 | st_expect 'oracle attack unverified' 0 "$VERIFIED" |
| 65 | if [ ! -e "$ac_dir/host-marker" ] && grep -q 'expected.txt' "$ac_dir/attack/run.err"; then |
| 66 | st_ok 'sandbox denies host write and oracle read' |
| 67 | else |
| 68 | st_bad 'sandbox allowed host write or did not exercise oracle read' |
| 69 | fi |
| 70 | evaluate py "$TASKS_DIR/task01" "$TASKS_DIR/task01/reference.py" "$ac_dir/reference" |
| 71 | st_expect 'sandboxed candidate verified' 1 "$VERIFIED" |
| 72 | say 'sandboxed candidate output:' |
| 73 | cat "$ac_dir/reference/out.txt" |
| 74 | # Compile-time oracle reads must fail too (neither runtime-only isolation nor |
| 75 | # Python -I prevents C/Rust's compile-time file reads). |
| 76 | { |
| 77 | printf '#if __has_include("%s")\n#error ORACLE_VISIBLE\n#endif\n' "$TASKS_DIR/task01/expected.txt" |
| 78 | cat "$TASKS_DIR/task01/reference.c" |
| 79 | } > "$ac_dir/include.c" |
| 80 | evaluate c "$TASKS_DIR/task01" "$ac_dir/include.c" "$ac_dir/c-include" |
| 81 | st_expect 'C compiler cannot see oracle' 0 "$COMP_RC" |
| 82 | st_expect 'C oracle-invisibility control verified' 1 "$VERIFIED" |
| 83 | printf 'const ORACLE: &str = include_str!("%s");\n' "$TASKS_DIR/task01/expected.txt" > "$ac_dir/include.rs" |
| 84 | evaluate rs "$TASKS_DIR/task01" "$ac_dir/include.rs" "$ac_dir/rs-include" |
| 85 | st_expect 'Rust compile-time oracle unverified' 0 "$VERIFIED" |
| 86 | if [ "$COMP_RC" -ne 0 ] && grep -q 'No such file or directory' "$ac_dir/rs-include/err.txt"; then |
| 87 | st_ok 'Rust compiler cannot read oracle' |
| 88 | else |
| 89 | st_bad 'Rust oracle include did not fail at the filesystem boundary' |
| 90 | fi |
| 91 | # bwrap emits namespace JSON before mount setup; only an in-sandbox handshake |
| 92 | # distinguishes setup failure from a genuine compiler/candidate exit status. |
| 93 | mkdir -p "$ac_dir/setup-tools" "$ac_dir/empty-sysroot" |
| 94 | cat > "$ac_dir/setup-tools/rustc" <<EOF |
| 95 | #!/bin/sh |
| 96 | printf '%s\n' "$ac_dir/empty-sysroot" |
| 97 | EOF |
| 98 | chmod +x "$ac_dir/setup-tools/rustc" |
| 99 | ac_rc=0 |
| 100 | ac_out=$(PATH="$ac_dir/setup-tools:$PATH" sh "$SCRIPT_DIR/driver.sh" --evaluate-probe rs \ |
| 101 | "$TASKS_DIR/task01/reference.rs" "$ac_dir/compile-setup" 2>&1) || ac_rc=$? |
| 102 | st_expect 'compiler mount setup failure NOT_RUN' 3 "$ac_rc" |
| 103 | if [ -s "$ac_dir/compile-setup/compile.sandbox" ] && [ ! -s "$ac_dir/compile-setup/compile.ready" ]; then |
| 104 | st_ok 'namespace JSON is not compiler readiness' |
| 105 | else |
| 106 | st_bad 'compiler setup negative control did not separate JSON and readiness' |
| 107 | fi |
| 108 | ac_bwrap=$(command -v bwrap) |
| 109 | cat > "$ac_dir/setup-tools/bwrap" <<EOF |
| 110 | #!/bin/sh |
| 111 | for arg do :; done |
| 112 | if [ "\$arg" = /work/candidate ]; then |
| 113 | exec "$ac_bwrap" --ro-bind "$ac_dir/nonexistent-mount" /unavailable "\$@" |
| 114 | fi |
| 115 | exec "$ac_bwrap" "\$@" |
| 116 | EOF |
| 117 | chmod +x "$ac_dir/setup-tools/bwrap" |
| 118 | ac_rc=0 |
| 119 | ac_out=$(PATH="$ac_dir/setup-tools:$PATH" sh "$SCRIPT_DIR/driver.sh" --evaluate-probe py \ |
| 120 | "$TASKS_DIR/task01/reference.py" "$ac_dir/run-setup" 2>&1) || ac_rc=$? |
| 121 | st_expect 'runtime mount setup failure NOT_RUN' 3 "$ac_rc" |
| 122 | if [ -s "$ac_dir/run-setup/run.sandbox" ] && [ ! -s "$ac_dir/run-setup/run.ready" ]; then |
| 123 | st_ok 'namespace JSON is not candidate readiness' |
| 124 | else |
| 125 | st_bad 'runtime setup negative control did not separate JSON and readiness' |
| 126 | fi |
| 127 | { |
| 128 | printf 'import os\ntry:\n os.write(3, b"forged")\nexcept OSError:\n pass\nelse:\n raise RuntimeError("readiness fd leaked")\n' |
| 129 | cat "$TASKS_DIR/task01/reference.py" |
| 130 | } > "$ac_dir/readiness-fd.py" |
| 131 | evaluate py "$TASKS_DIR/task01" "$ac_dir/readiness-fd.py" "$ac_dir/readiness-fd" |
| 132 | st_expect 'candidate cannot inherit readiness fd' 1 "$VERIFIED" |
| 133 | # A caller may hold host files on arbitrary inheritable descriptors. Both |
| 134 | # compiler includes and runtime /proc/self/fd reads must lose that access. |
| 135 | { |
| 136 | printf 'import os\ntry:\n os.read(99, 1)\nexcept OSError:\n pass\nelse:\n raise RuntimeError("inherited host descriptor leaked")\n' |
| 137 | cat "$TASKS_DIR/task01/reference.py" |
| 138 | } > "$ac_dir/inherited-fd.py" |
| 139 | ac_rc=0 |
| 140 | ac_out=$(bash -c 'exec 99< "$1"; shift; exec "$@"' _ "$TASKS_DIR/task01/expected.txt" \ |
| 141 | sh "$SCRIPT_DIR/driver.sh" --evaluate-probe py "$ac_dir/inherited-fd.py" "$ac_dir/inherited-py" 2>&1) || ac_rc=$? |
| 142 | st_expect 'inherited descriptor runtime control rc' 0 "$ac_rc" |
| 143 | case $ac_out in *'COMP_RC=0 RUN_RC=0 VERIFIED=1'*) st_ok 'runtime cannot read inherited host descriptor' ;; |
| 144 | *) st_bad 'runtime inherited descriptor was not closed' ;; esac |
| 145 | { |
| 146 | printf 'const SECRET: &str = include_str!("/proc/self/fd/99");\n' |
| 147 | cat "$TASKS_DIR/task01/reference.rs" |
| 148 | } > "$ac_dir/inherited-fd.rs" |
| 149 | ac_rc=0 |
| 150 | ac_out=$(bash -c 'exec 99< "$1"; shift; exec "$@"' _ "$TASKS_DIR/task01/expected.txt" \ |
| 151 | sh "$SCRIPT_DIR/driver.sh" --evaluate-probe rs "$ac_dir/inherited-fd.rs" "$ac_dir/inherited-rs" 2>&1) || ac_rc=$? |
| 152 | st_expect 'inherited descriptor compiler control rc' 0 "$ac_rc" |
| 153 | if grep -q 'No such file or directory' "$ac_dir/inherited-rs/err.txt"; then |
| 154 | st_ok 'compiler cannot read inherited host descriptor' |
| 155 | else |
| 156 | st_bad 'compiler inherited descriptor was not closed' |
| 157 | fi |
| 158 | |
| 159 | cat > "$ac_dir/ignore-term.py" <<'EOF' |
| 160 | import os |
| 161 | import signal |
| 162 | import time |
| 163 | signal.signal(signal.SIGTERM, signal.SIG_IGN) |
| 164 | if os.fork() == 0: |
| 165 | while True: |
| 166 | time.sleep(1) |
| 167 | def clamp_add_u16(a, b): |
| 168 | while True: |
| 169 | time.sleep(1) |
| 170 | EOF |
| 171 | ac_rc=0 |
| 172 | timeout --kill-after=1s 13s sh "$SCRIPT_DIR/driver.sh" --deadline-probe \ |
| 173 | "$ac_dir/ignore-term.py" "$ac_dir/deadline" || ac_rc=$? |
| 174 | st_expect 'TERM-ignoring process group deadline' 0 "$ac_rc" |
| 175 | ac_pid=$(awk '/"child-pid"/{gsub(/[^0-9]/, ""); print}' "$ac_dir/deadline/run.sandbox") |
| 176 | if [ -n "$ac_pid" ] && ! kill -0 "$ac_pid" 2>/dev/null; then |
| 177 | st_ok 'timed-out PID namespace is gone' |
| 178 | else |
| 179 | st_bad 'timed-out sandbox process is still alive' |
| 180 | fi |
| 181 | # Control for the driver's own teardown wait: a namespace init that never exits is NOT_RUN. |
| 182 | sleep 30 & |
| 183 | ac_live=$! |
| 184 | printf '{\n "child-pid": %s\n}\n' "$ac_live" > "$ac_dir/live.sandbox" |
| 185 | ac_rc=0 |
| 186 | ac_out=$(sandbox_gone "$ac_dir/live.sandbox") || ac_rc=$? |
| 187 | kill "$ac_live" 2>/dev/null || true |
| 188 | wait "$ac_live" 2>/dev/null || true |
| 189 | st_expect 'live sandbox namespace after a deadline is NOT_RUN' 3 "$ac_rc" |
| 190 | case $ac_out in *"still alive 5 s after"*) st_ok 'live sandbox namespace is named' ;; *) st_bad "live sandbox namespace: $ac_out" ;; esac |
| 191 | |
| 192 | mkdir -p "$ac_dir/tools" |
| 193 | printf '#!/bin/sh\nexit 1\n' > "$ac_dir/tools/bwrap" |
| 194 | chmod +x "$ac_dir/tools/bwrap" |
| 195 | ac_rc=0 |
| 196 | ac_out=$(PATH="$ac_dir/tools:$PATH" sh "$SCRIPT_DIR/driver.sh" --run-model \ |
| 197 | --endpoint http://127.0.0.1:1 --model fake --langs py --arms plain \ |
| 198 | --out "$ac_dir/unavailable" 2>&1) || ac_rc=$? |
| 199 | st_expect 'unavailable sandbox NOT_RUN' 3 "$ac_rc" |
| 200 | case $ac_out in *'bubblewrap sandbox is unavailable'*) st_ok 'sandbox failure is closed' ;; |
| 201 | *) st_bad 'sandbox failure did not stop at sandbox preflight' ;; esac |
| 202 | st_expect 'sandbox failure creates no run' no "$([ -e "$ac_dir/unavailable" ] && say yes || say no)" |
| 203 | for ac_option in --langs --arms; do |
| 204 | for ac_value in '' ','; do |
| 205 | ac_rc=0 |
| 206 | sh "$SCRIPT_DIR/driver.sh" --run-model --endpoint http://127.0.0.1:1 \ |
| 207 | --model fake "$ac_option" "$ac_value" --out "$ac_dir/empty" >/dev/null 2>&1 || ac_rc=$? |
| 208 | st_expect "empty $ac_option '$ac_value' rejected" 2 "$ac_rc" |
| 209 | done |
| 210 | done |
| 211 | mkdir "$ac_dir/empty-output" |
| 212 | ac_rc=0 |
| 213 | sh "$SCRIPT_DIR/driver.sh" --output-probe "$ac_dir/empty-output" || ac_rc=$? |
| 214 | st_expect 'existing empty output can be claimed' 0 "$ac_rc" |
| 215 | mkdir "$ac_dir/link-target" |
| 216 | ln -s "$ac_dir/link-target" "$ac_dir/output-link" |
| 217 | for ac_link in "$ac_dir/output-link" "$ac_dir/output-link/"; do |
| 218 | ac_rc=0 |
| 219 | sh "$SCRIPT_DIR/driver.sh" --output-probe "$ac_link" >/dev/null 2>&1 || ac_rc=$? |
| 220 | st_expect 'symlink output rejected, including trailing slash' 2 "$ac_rc" |
| 221 | done |
| 222 | st_expect 'symlink target left empty' '' "$(find "$ac_dir/link-target" -mindepth 1 -print -quit)" |
| 223 | # Force both processes past the initial emptiness check before either tries |
| 224 | # mkdir on the ownership marker; this exercises the actual acquisition race. |
| 225 | mkdir -p "$ac_dir/claim-tools" "$ac_dir/claim-barrier" |
| 226 | ac_mkdir=$(command -v mkdir) |
| 227 | cat > "$ac_dir/claim-tools/mkdir" <<EOF |
| 228 | #!/bin/sh |
| 229 | for arg do :; done |
| 230 | if [ "\$arg" = "$ac_dir/raced-output/.inference-cost-owner" ]; then |
| 231 | : > "$ac_dir/claim-barrier/\$\$" |
| 232 | while [ "\$(find "$ac_dir/claim-barrier" -type f | wc -l)" -lt 2 ]; do |
| 233 | sleep 0.01 |
| 234 | done |
| 235 | fi |
| 236 | exec "$ac_mkdir" "\$@" |
| 237 | EOF |
| 238 | chmod +x "$ac_dir/claim-tools/mkdir" |
| 239 | ( |
| 240 | ac_rc=0 |
| 241 | PATH="$ac_dir/claim-tools:$PATH" timeout --kill-after=1s 5s sh "$SCRIPT_DIR/driver.sh" \ |
| 242 | --output-probe "$ac_dir/raced-output" >"$ac_dir/race1.log" 2>&1 || ac_rc=$? |
| 243 | say "$ac_rc" > "$ac_dir/race1.rc" |
| 244 | ) & |
| 245 | ac_pid1=$! |
| 246 | ( |
| 247 | ac_rc=0 |
| 248 | PATH="$ac_dir/claim-tools:$PATH" timeout --kill-after=1s 5s sh "$SCRIPT_DIR/driver.sh" \ |
| 249 | --output-probe "$ac_dir/raced-output" >"$ac_dir/race2.log" 2>&1 || ac_rc=$? |
| 250 | say "$ac_rc" > "$ac_dir/race2.rc" |
| 251 | ) & |
| 252 | ac_pid2=$! |
| 253 | wait "$ac_pid1" |
| 254 | wait "$ac_pid2" |
| 255 | case $(cat "$ac_dir/race1.rc"):$(cat "$ac_dir/race2.rc") in |
| 256 | 0:2|2:0) st_ok 'only one concurrent output claimant succeeds' ;; |
| 257 | *) st_bad 'concurrent output claim did not have exactly one owner' ;; |
| 258 | esac |
| 259 | } |
| 260 | |
| 261 | # ---- memory ceiling: task01 candidates that are correct but first touch BYTES of memory |
| 262 | |
| 263 | hog_src() { # lang bytes -> a task01 candidate on stdout |
| 264 | case $1 in |
| 265 | c) printf 'uint32_t clamp_add_u16(uint32_t a, uint32_t b) {\n static volatile unsigned char *hog;\n if (!hog) {\n size_t n = %sull;\n hog = malloc(n);\n if (!hog) return 0;\n for (size_t i = 0; i < n; i += 4096) hog[i] = 1;\n }\n uint32_t s = a + b;\n return s > 65535u ? 65535u : s;\n}\n' "$2" ;; |
| 266 | py) printf '_hog = []\ndef clamp_add_u16(a, b):\n if not _hog:\n _hog.append(b"\\x01" * %s)\n s = a + b\n return 65535 if s > 65535 else s\n' "$2" ;; |
| 267 | rs) printf 'static HOG: std::sync::Once = std::sync::Once::new();\nfn clamp_add_u16(a: u32, b: u32) -> u32 {\n HOG.call_once(|| {\n let n: usize = %s;\n let mut v: Vec<u8> = Vec::with_capacity(n);\n let p = v.as_mut_ptr();\n let mut i = 0usize;\n while i < n {\n unsafe { std::ptr::write_volatile(p.add(i), 1u8); }\n i += 4096;\n }\n std::mem::forget(v);\n });\n let s = a.wrapping_add(b);\n if s > 65535 { 65535 } else { s }\n}\n' "$2" ;; |
| 268 | om) # globals of 16777216 ulong (128 MiB) each, every 4096-byte page written once |
| 269 | hg_n=$(( ($2 + 134217727) / 134217728 )) |
| 270 | printf 'global hogk ulong 1\n' |
| 271 | hg_i=0 |
| 272 | while [ "$hg_i" -lt "$hg_n" ]; do printf 'global hog%d ulong 16777216\n' "$hg_i"; hg_i=$((hg_i + 1)); done |
| 273 | printf 'fn clamp_add_u16 a uint b uint to uint\nif eq get hogk 0 0\nput hogk 0 1\n' |
| 274 | hg_i=0 |
| 275 | while [ "$hg_i" -lt "$hg_n" ]; do printf 'for i ulong 32768\nput hog%d mul i 512 1\nend\n' "$hg_i"; hg_i=$((hg_i + 1)); done |
| 276 | printf 'end\ns = add a b\nif gt s 65535\nret 65535\nend\nret s\nend\n' ;; |
| 277 | esac |
| 278 | } |
| 279 | |
| 280 | mem_probe() { # driver lang candidate work -> the --evaluate-probe line |
| 281 | sh "$1" --evaluate-probe "$2" "$3" "$4" 2>&1 | tail -n 1 |
| 282 | } |
| 283 | |
| 284 | memory_checks() { |
| 285 | mc_dir="$BUILD_DIR/selftest/memory" |
| 286 | mkdir -p "$mc_dir" |
| 287 | say "== memory ceiling (memory_max_bytes $MEM_MAX)" |
| 288 | # Over the ceiling by 256 MiB: killed by the kernel and scored wrong. Half the ceiling: the same |
| 289 | # code verifies, so the kill above is the ceiling's doing, not a defect of the candidate. |
| 290 | mc_over=$((MEM_MAX + 268435456)) |
| 291 | mc_half=$((MEM_MAX / 2)) |
| 292 | for l in $ALL_LANGS; do |
| 293 | hog_src "$l" "$mc_over" > "$mc_dir/over.$l" |
| 294 | hog_src "$l" "$mc_half" > "$mc_dir/half.$l" |
| 295 | mc_out=$(mem_probe "$SCRIPT_DIR/driver.sh" "$l" "$mc_dir/over.$l" "$mc_dir/over-$l") |
| 296 | case $mc_out in |
| 297 | "COMP_RC=0 RUN_RC="*" VERIFIED=0 MEM_KILL=run") st_ok "$l candidate over the ceiling is killed and scored wrong ($mc_out)" ;; |
| 298 | *) st_bad "$l candidate over the ceiling: $mc_out" ;; |
| 299 | esac |
| 300 | mc_out=$(mem_probe "$SCRIPT_DIR/driver.sh" "$l" "$mc_dir/half.$l" "$mc_dir/half-$l") |
| 301 | st_expect "$l candidate at half the ceiling verifies" "COMP_RC=0 RUN_RC=0 VERIFIED=1 MEM_KILL=none" "$mc_out" |
| 302 | done |
| 303 | # Mutant: the driver with the ceiling line removed must fail the check above (the over-ceiling |
| 304 | # candidate survives and verifies). |
| 305 | mc_mut="$mc_dir/no-ceiling/bench/inference_cost" |
| 306 | mkdir -p "$mc_mut" |
| 307 | cp -R "$TASKS_DIR" "$mc_mut/tasks" |
| 308 | grep -v ' # memory ceiling$' "$SCRIPT_DIR/driver.sh" > "$mc_mut/driver.sh" |
| 309 | st_expect "no-ceiling mutant removes exactly one line" 1 \ |
| 310 | "$(( $(wc -l < "$SCRIPT_DIR/driver.sh") - $(wc -l < "$mc_mut/driver.sh") ))" |
| 311 | mc_out=$(mem_probe "$mc_mut/driver.sh" c "$mc_dir/over.c" "$mc_dir/mutant-over-c") |
| 312 | case $mc_out in |
| 313 | "COMP_RC=0 RUN_RC="*" VERIFIED=0 MEM_KILL=run") st_bad "no-ceiling mutant still killed the candidate ($mc_out)" ;; |
| 314 | *) st_ok "no-ceiling mutant fails the memory check ($mc_out)" ;; |
| 315 | esac |
| 316 | # Fail closed: a systemd-run that runs the command outside any new scope leaves the ceiling |
| 317 | # unapplied, and one that cannot reach the user manager fails; both are NOT_RUN, never unbounded. |
| 318 | mkdir -p "$mc_dir/noscope" "$mc_dir/nobus" |
| 319 | printf '#!/bin/sh\nwhile [ "$1" != -- ]; do shift; done\nshift\nexec "$@"\n' > "$mc_dir/noscope/systemd-run" |
| 320 | printf '#!/bin/sh\necho "Failed to connect to bus" >&2\nexit 1\n' > "$mc_dir/nobus/systemd-run" |
| 321 | chmod +x "$mc_dir/noscope/systemd-run" "$mc_dir/nobus/systemd-run" |
| 322 | for mc_fake in noscope nobus; do |
| 323 | mc_rc=0 |
| 324 | mc_out=$(PATH="$mc_dir/$mc_fake:$PATH" sh "$SCRIPT_DIR/driver.sh" --run-model --endpoint http://127.0.0.1:1 \ |
| 325 | --model fake --langs py --arms plain --out "$mc_dir/run-$mc_fake" 2>&1) || mc_rc=$? |
| 326 | st_expect "$mc_fake systemd-run: run-model NOT_RUN" 3 "$mc_rc" |
| 327 | case $mc_out in |
| 328 | *'memory ceiling (cgroup scope MemoryMax='*'cannot be applied'*) st_ok "$mc_fake systemd-run: stopped at the ceiling preflight" ;; |
| 329 | *) st_bad "$mc_fake systemd-run: $mc_out" ;; |
| 330 | esac |
| 331 | st_expect "$mc_fake systemd-run: no run directory" no "$([ -e "$mc_dir/run-$mc_fake" ] && say yes || say no)" |
| 332 | mc_rc=0 |
| 333 | mc_out=$(PATH="$mc_dir/$mc_fake:$PATH" sh "$SCRIPT_DIR/driver.sh" --evaluate-probe py \ |
| 334 | "$TASKS_DIR/task01/reference.py" "$mc_dir/probe-$mc_fake" 2>&1) || mc_rc=$? |
| 335 | st_expect "$mc_fake systemd-run: evaluate NOT_RUN" 3 "$mc_rc" |
| 336 | done |
| 337 | case $(PATH="$mc_dir/noscope:$PATH" sh "$SCRIPT_DIR/driver.sh" --evaluate-probe py \ |
| 338 | "$TASKS_DIR/task01/reference.py" "$mc_dir/probe-noscope2" 2>&1) in |
| 339 | *'memory ceiling could not be applied to the compiler sandbox'*) st_ok 'unapplied ceiling is named at the compiler sandbox' ;; |
| 340 | *) st_bad 'unapplied ceiling was not named at the compiler sandbox' ;; |
| 341 | esac |
| 342 | # A systemd-run that fails once before any scope exists (the user bus dropping a connection) is |
| 343 | # retried: the reference still verifies, inside a scope, and the failure stays out of its stderr. |
| 344 | mkdir -p "$mc_dir/flaky" |
| 345 | printf '#!/bin/sh\nif [ ! -e "%s" ]; then\n : > "%s"\n echo "Failed to start transient scope unit: Transport endpoint is not connected" >&2\n exit 1\nfi\nexec %s "$@"\n' \ |
| 346 | "$mc_dir/flaky/failed-once" "$mc_dir/flaky/failed-once" "$(command -v systemd-run)" > "$mc_dir/flaky/systemd-run" |
| 347 | chmod +x "$mc_dir/flaky/systemd-run" |
| 348 | mc_out=$(PATH="$mc_dir/flaky:$PATH" sh "$SCRIPT_DIR/driver.sh" --evaluate-probe py \ |
| 349 | "$TASKS_DIR/task01/reference.py" "$mc_dir/probe-flaky" 2>&1 | tail -n 1) |
| 350 | st_expect 'systemd-run failing once before the scope is retried' "COMP_RC=0 RUN_RC=0 VERIFIED=1 MEM_KILL=none" "$mc_out" |
| 351 | st_expect 'the retried failure happened' yes "$([ -e "$mc_dir/flaky/failed-once" ] && say yes || say no)" |
| 352 | st_expect 'the retried compile ran in the scope' 0 "$(sandbox_note memory_oom_kills "$mc_dir/probe-flaky/compile.sandbox")" |
| 353 | st_expect 'the retried failure is not in the compiler stderr' 0 "$(grep -c 'Transport endpoint' "$mc_dir/probe-flaky/err.txt" || true)" |
| 354 | for mc_bad in 0 67108863 4294967297 1G; do |
| 355 | mc_rc=0 |
| 356 | BENCH_MEM_MAX_BYTES=$mc_bad sh "$SCRIPT_DIR/driver.sh" --run-model --endpoint http://127.0.0.1:1 \ |
| 357 | --model fake --langs py --arms plain --out "$mc_dir/run-bad-$mc_bad" > /dev/null 2>&1 || mc_rc=$? |
| 358 | st_expect "BENCH_MEM_MAX_BYTES=$mc_bad refused" 2 "$mc_rc" |
| 359 | done |
| 360 | } |
| 361 | |
| 362 | audit_reuse_check() { |
| 363 | find "$BUILD_DIR/selftest/good" -type f -exec sha256sum {} \; | sort > "$BUILD_DIR/selftest/before.sha256" |
| 364 | ac_rc=0 |
| 365 | ac_out=$(sh "$SCRIPT_DIR/driver.sh" --run-model --endpoint http://127.0.0.1:1 --model fake \ |
| 366 | --langs py --arms feedback --out "$BUILD_DIR/selftest/good" 2>&1) || ac_rc=$? |
| 367 | st_expect 'nonempty output rejected' 2 "$ac_rc" |
| 368 | case $ac_out in *'--out must be empty'*) st_ok 'output refused before HTTP' ;; *) st_bad 'wrong output refusal' ;; esac |
| 369 | ln -s "$BUILD_DIR/selftest/good" "$BUILD_DIR/selftest/good-link" |
| 370 | for ac_link in "$BUILD_DIR/selftest/good-link" "$BUILD_DIR/selftest/good-link/"; do |
| 371 | ac_rc=0 |
| 372 | sh "$SCRIPT_DIR/driver.sh" --run-model --endpoint http://127.0.0.1:1 --model fake \ |
| 373 | --langs py --arms feedback --out "$ac_link" >/dev/null 2>&1 || ac_rc=$? |
| 374 | st_expect 'run-model refuses symlink to completed run' 2 "$ac_rc" |
| 375 | done |
| 376 | find "$BUILD_DIR/selftest/good" -type f -exec sha256sum {} \; | sort > "$BUILD_DIR/selftest/after.sha256" |
| 377 | if cmp -s "$BUILD_DIR/selftest/before.sha256" "$BUILD_DIR/selftest/after.sha256"; then |
| 378 | st_ok 'reused output remains byte-identical' |
| 379 | else |
| 380 | st_bad 'reused output changed' |
| 381 | fi |
| 382 | } |