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 / seed / challenge / run.sh
| 1 | #!/bin/sh |
| 2 | # run.sh -- the seed-chain bootstrap challenge (seed/challenge/CHALLENGE.md). |
| 3 | # Rebuilds omc from the 408-byte seed0 (seed0 -> hex1 -> asm2 -> om4 -> omc -> omc -> omc) out of the |
| 4 | # sources committed at the pin named in expected.txt, and checks every input and every rung against the |
| 5 | # published sha256 and byte size there. Nothing in the working tree is written except the output dir. |
| 6 | # |
| 7 | # sh seed/challenge/run.sh # from the repo root of a full clone (needs the pin commit) |
| 8 | # sh seed/challenge/run.sh --tree DIR # check an already extracted tree instead (negative controls) |
| 9 | # options: --expect FILE (default seed/challenge/expected.txt, version 2; expected-v1.txt is version 1), --out DIR (default build/seed/challenge) |
| 10 | # |
| 11 | # Exit 0 = CHALLENGE PASS, 1 = CHALLENGE FAIL, 2 = usage, 3 = NOT_RUN (not AArch64 Linux, or pin missing). |
| 12 | # The hash log is <out>/challenge.log. |
| 13 | # |
| 14 | # Host glue by design, like load0.sh and omega-from-seed.sh: it only runs the seed programs and compares |
| 15 | # with coreutils sha256sum, an outside oracle. It has no .om successor on purpose: a checker compiled by |
| 16 | # the chain it checks would be circular. The bytes of every rung are written by seed programs, not by C. |
| 17 | set -u |
| 18 | R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) |
| 19 | EXP=$R/seed/challenge/expected.txt |
| 20 | TREE= |
| 21 | OUT=$R/build/seed/challenge |
| 22 | while [ $# -gt 0 ]; do |
| 23 | case $1 in |
| 24 | --expect) [ $# -ge 2 ] || exit 2; EXP=$2; shift 2 ;; |
| 25 | --tree) [ $# -ge 2 ] || exit 2; TREE=$2; shift 2 ;; |
| 26 | --out) [ $# -ge 2 ] || exit 2; OUT=$2; shift 2 ;; |
| 27 | *) echo "usage: run.sh [--expect FILE] [--tree DIR] [--out DIR]" >&2; exit 2 ;; |
| 28 | esac |
| 29 | done |
| 30 | |
| 31 | if [ "$(uname -s)/$(uname -m)" != Linux/aarch64 ]; then |
| 32 | echo "CHALLENGE NOT_RUN: the seed programs are AArch64 Linux ELF files; this host is $(uname -s)/$(uname -m)" |
| 33 | exit 3 |
| 34 | fi |
| 35 | [ -r "$EXP" ] || { echo "CHALLENGE NOT_RUN: no expected table at $EXP"; exit 3; } |
| 36 | pin=$(awk '$1=="pin"{print $2}' "$EXP") |
| 37 | [ -n "$pin" ] || { echo "CHALLENGE NOT_RUN: expected table names no pin"; exit 3; } |
| 38 | |
| 39 | rm -rf "$OUT" |
| 40 | mkdir -p "$OUT/b" || exit 3 |
| 41 | B=$OUT/b |
| 42 | LOG=$OUT/challenge.log |
| 43 | : > "$LOG" |
| 44 | fails=0 |
| 45 | say() { printf '%s\n' "$*" | tee -a "$LOG"; } |
| 46 | # Milliseconds; a date without %N (busybox) falls back to whole seconds. |
| 47 | ms() { v=$(date +%s%3N 2>/dev/null); case $v in ''|*[!0-9]*) echo $(( $(date +%s) * 1000 )) ;; *) echo "$v" ;; esac; } |
| 48 | sha() { sha256sum "$1" | cut -d' ' -f1; } |
| 49 | |
| 50 | if [ -z "$TREE" ]; then |
| 51 | TREE=$OUT/src |
| 52 | mkdir -p "$TREE" |
| 53 | if ! git -C "$R" cat-file -e "$pin^{commit}" 2>/dev/null; then |
| 54 | echo "CHALLENGE NOT_RUN: pin $pin is not in this clone (shallow?); run: git fetch origin $pin" |
| 55 | exit 3 |
| 56 | fi |
| 57 | git -C "$R" archive "$pin" seed/rung0 seed/rung1 seed/rung2 seed/rung3 seed/rung4 seed/omc \ |
| 58 | | tar -x -C "$TREE" || { echo "CHALLENGE NOT_RUN: git archive of $pin failed"; exit 3; } |
| 59 | src="git archive $pin" |
| 60 | else |
| 61 | TREE=$(CDPATH= cd -- "$TREE" && pwd) || exit 2 |
| 62 | src="--tree $TREE" |
| 63 | fi |
| 64 | S=$TREE/seed |
| 65 | |
| 66 | say "omega seed-chain bootstrap challenge" |
| 67 | say "date $(date -u +%FT%TZ)" |
| 68 | say "host $(uname -srm)" |
| 69 | say "cpu $(awk -F': ' '/^(model name|CPU part)/{print $2; exit}' /proc/cpuinfo 2>/dev/null)" |
| 70 | say "checker_head $(git -C "$R" rev-parse HEAD 2>/dev/null || echo none)" |
| 71 | say "checker_sha256 $(sha "$0")" |
| 72 | say "expected_sha256 $(sha "$EXP")" |
| 73 | say "pin $pin" |
| 74 | say "source $src" |
| 75 | |
| 76 | # check KIND NAME FILE MS: compare FILE with the table line "KIND NAME sha bytes". |
| 77 | check() { |
| 78 | want=$(awk -v k="$1" -v n="$2" '$1==k && $2==n {print $3" "$4}' "$EXP") |
| 79 | if [ -z "$want" ]; then say "FAIL $1 $2: no published value"; fails=$((fails+1)); return; fi |
| 80 | if [ ! -f "$3" ]; then say "NOT_RUN $1 $2: not built"; fails=$((fails+1)); return; fi |
| 81 | got="$(sha "$3") $(wc -c < "$3" | tr -d ' ')" |
| 82 | if [ "$got" = "$want" ]; then say "PASS $1 $2 $got${4:+ ${4}ms}" |
| 83 | else say "FAIL $1 $2 got $got want $want"; fails=$((fails+1)); fi |
| 84 | } |
| 85 | bad() { say "FAIL $*"; fails=$((fails+1)); } |
| 86 | # run OUTFILE INFILE PROG...: PROG < INFILE > OUTFILE; a nonzero status removes OUTFILE. |
| 87 | run() { |
| 88 | o=$1; i=$2; shift 2 |
| 89 | if "$@" < "$i" > "$o" 2> "$o.err"; then chmod +x "$o" |
| 90 | else bad "$(basename "$1") on $(basename "$i") exited $?: $(head -c 200 "$o.err")"; rm -f "$o"; fi |
| 91 | } |
| 92 | need() { [ -x "$1" ] || { say "NOT_RUN $2: $(basename "$1") missing"; fails=$((fails+1)); return 1; }; } |
| 93 | |
| 94 | for f in $(awk '$1=="input"{print $2}' "$EXP"); do check input "$f" "$TREE/$f"; done |
| 95 | |
| 96 | T0=$(ms) |
| 97 | # rung 0: seed0, the 408 hand-written bytes, loaded by POSIX printf (load0.sh), then rebuilding itself. |
| 98 | t=$(ms) |
| 99 | if sh "$S/rung0/load0.sh" "$S/rung0/seed0.hex" "$B/seed0" 2> "$B/load0.err"; then chmod +x "$B/seed0" |
| 100 | else bad "load0.sh: $(head -c 200 "$B/load0.err")"; rm -f "$B/seed0"; fi |
| 101 | check rung seed0 "$B/seed0" $(( $(ms) - t )) |
| 102 | if need "$B/seed0" seed0.self; then |
| 103 | run "$B/seed0.self" "$S/rung0/seed0.hex" "$B/seed0" |
| 104 | cmp -s "$B/seed0" "$B/seed0.self" && say "PASS seed0 rebuilds itself" || bad "seed0 did not rebuild itself" |
| 105 | fi |
| 106 | # rung 1: hex1, written by seed0, then rebuilding itself. |
| 107 | t=$(ms) |
| 108 | need "$B/seed0" hex1 && run "$B/hex1" "$S/rung1/hex1.hex" "$B/seed0" |
| 109 | check rung hex1 "$B/hex1" $(( $(ms) - t )) |
| 110 | if need "$B/hex1" hex1.self; then |
| 111 | run "$B/hex1.self" "$S/rung1/hex1.hex" "$B/hex1" |
| 112 | cmp -s "$B/hex1" "$B/hex1.self" && say "PASS hex1 rebuilds itself" || bad "hex1 did not rebuild itself" |
| 113 | fi |
| 114 | # rung 2: asm2, written by hex1. |
| 115 | t=$(ms) |
| 116 | need "$B/hex1" asm2 && run "$B/asm2" "$S/rung2/asm2.hex1" "$B/hex1" |
| 117 | check rung asm2 "$B/asm2" $(( $(ms) - t )) |
| 118 | # rung 4: om4, written by asm2 (as seed/rung4/mk4.sh does, on a copy: the p_filesz line of om4.s must |
| 119 | # already carry the size of the image it describes). |
| 120 | t=$(ms) |
| 121 | if need "$B/asm2" om4; then |
| 122 | cat "$S/rung2/table.s2" "$S/rung3/table3.s2" "$S/rung4/table4.s2" "$S/rung4/om4.s" > "$B/om4.in" |
| 123 | run "$B/om4.first" "$B/om4.in" "$B/asm2" |
| 124 | if [ -f "$B/om4.first" ]; then |
| 125 | n=$(wc -c < "$B/om4.first" | tr -d ' ') |
| 126 | sed "s/^ \.w 0x[0-9a-f]* ; p_filesz = the size of om4.bin.*/ .w $(printf 0x%08x "$n") ; p_filesz = the size of om4.bin, $n bytes (asm2 has no size arithmetic; gate4.sh checks it)/" \ |
| 127 | "$S/rung4/om4.s" > "$B/om4.s.sized" |
| 128 | cmp -s "$S/rung4/om4.s" "$B/om4.s.sized" && say "PASS om4.s p_filesz already $n" || bad "om4.s p_filesz is not $n" |
| 129 | cat "$S/rung2/table.s2" "$S/rung3/table3.s2" "$S/rung4/table4.s2" "$B/om4.s.sized" > "$B/om4.in2" |
| 130 | run "$B/om4" "$B/om4.in2" "$B/asm2" |
| 131 | fi |
| 132 | fi |
| 133 | check rung om4 "$B/om4" $(( $(ms) - t )) |
| 134 | # rung 5: omc, written by om4 (stage1), then by itself twice (stage2, stage3). All three must be equal. |
| 135 | for st in 1 2 3; do |
| 136 | case $st in 1) by=$B/om4 ;; 2) by=$B/stage1 ;; 3) by=$B/stage2 ;; esac |
| 137 | t=$(ms) |
| 138 | need "$by" "omc stage$st" && run "$B/stage$st" "$S/omc/omc.om" "$by" |
| 139 | d=$(( $(ms) - t )) |
| 140 | if [ -f "$B/stage$st" ]; then |
| 141 | cp "$B/stage$st" "$B/omc" |
| 142 | printf 'stage%s ' "$st" >> "$LOG"; printf 'stage%s ' "$st" |
| 143 | check rung omc "$B/omc" "$d" |
| 144 | else say "NOT_RUN rung omc stage$st"; fi |
| 145 | done |
| 146 | # The seed-born compiler compiles and runs a program (seed/rung4/tests/r5b_fwd.om returns 7). |
| 147 | if need "$B/stage3" probe; then |
| 148 | run "$B/fwd" "$S/rung4/tests/r5b_fwd.om" "$B/stage3" |
| 149 | if [ -x "$B/fwd" ]; then |
| 150 | "$B/fwd" > /dev/null; rc=$? |
| 151 | [ "$rc" = 7 ] && say "PASS stage3 compiles r5b_fwd.om; it runs and exits 7" || bad "r5b_fwd ran as $rc, wanted 7" |
| 152 | fi |
| 153 | fi |
| 154 | say "chain_ms $(( $(ms) - T0 ))" |
| 155 | if [ "$fails" = 0 ]; then say "CHALLENGE PASS pin=$pin omc=$(sha "$B/stage3")"; exit 0; fi |
| 156 | say "CHALLENGE FAIL fails=$fails pin=$pin" |
| 157 | exit 1 |