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 / om / tls / test / fuzz / run.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # om/tls/test/fuzz/run.sh: orchestration for the om/tls fuzz targets (fz_*.om). The targets are |
| 4 | # Omega; this script only builds, replays and drives AFL++ (qemu_mode, binary-only coverage). |
| 5 | # A trap in a target exits 100+code (code 1..14); exit >= 100 on any input is a crash, because |
| 6 | # om/tls/README.md requires that untrusted input never traps. |
| 7 | # |
| 8 | # sh run.sh [STAGE...] stages: build replay plant fuzz cmin triage service (default: build replay plant) |
| 9 | # |
| 10 | # Environment: |
| 11 | # OMC pinned compiler (default seed/out/omc) |
| 12 | # AFLPP AFL++ v4.40c tree with aflpp-exitrange.patch and qemu_mode built (sh aflpp.sh) |
| 13 | # OUT scratch dir (default /tmp/omtls-fuzz) |
| 14 | # FUZZ_HOURS one-shot campaign length over all targets (default 0 = skip the fuzz stage) |
| 15 | # FUZZ_TARGETS targets (default: all eight + fz_plant for plant) |
| 16 | # FUZZ_CPUS worker cpus, at most 2 (default "10 11"; never the X925 cores 5-9, 15-19) |
| 17 | # PLANT_S blind planted-control budget, seconds (default 1800) |
| 18 | # FUZZ_CORPUS / FUZZ_CRASHES / FUZZ_BOARD service + triage locations |
| 19 | # FUZZ_SLICE_S service time per target before rotating (default 3600) |
| 20 | set -u |
| 21 | D=$(cd -P "$(dirname "$0")" && pwd -P) |
| 22 | R=$(cd "$D/../../../.." && pwd) |
| 23 | OMC=${OMC:-$R/seed/out/omc} |
| 24 | AFLPP=${AFLPP:-$HOME/.local/lib/aflpp-4.40c} |
| 25 | OUT=${OUT:-/tmp/omtls-fuzz} |
| 26 | HOURS=${FUZZ_HOURS:-0} |
| 27 | TARGETS=${FUZZ_TARGETS:-fz_flight fz_hs fz_x509 fz_pem fz_dns fz_http fz_https} |
| 28 | CPUS=${FUZZ_CPUS:-10 11} |
| 29 | H=${HOME:-/home/drakestapleton} |
| 30 | CORPUS=${FUZZ_CORPUS:-$H/handoffs/TlsProof/fuzz/corpus} |
| 31 | CRASHES=${FUZZ_CRASHES:-$H/handoffs/TlsProof/crashes} |
| 32 | BOARD=${FUZZ_BOARD:-$H/handoffs/BOARD.md} |
| 33 | S=$D/seeds |
| 34 | NICE="nice -n 19 ionice -c3" |
| 35 | fails=0 |
| 36 | say() { echo "$*"; } |
| 37 | bad() { echo "FAIL $*"; fails=$((fails+1)); } |
| 38 | |
| 39 | for c in $CPUS; do case $c in 5|6|7|8|9|15|16|17|18|19) echo "refused: cpu $c is an X925 core" >&2; exit 2;; esac; done |
| 40 | [ "$(echo $CPUS | wc -w)" -le 2 ] || { echo "refused: at most 2 workers" >&2; exit 2; } |
| 41 | |
| 42 | # target argument: chain-verifying targets read the fuzz trust store, fz_pem a chain file |
| 43 | targ() { case $1 in fz_flight|fz_hs|fz_x509) echo "$S/fzstore.pem";; fz_pem) echo "$S/chain.bin";; *) echo "";; esac; } |
| 44 | |
| 45 | afl_env() { |
| 46 | export AFL_PATH=$AFLPP AFL_CRASH_EXITCODE=101-114 AFL_NO_UI=1 AFL_SKIP_CPUFREQ=1 \ |
| 47 | AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1 AFL_AUTORESUME=1 |
| 48 | } |
| 49 | |
| 50 | build() { |
| 51 | mkdir -p "$OUT/bin" |
| 52 | say "omc sha256 $(sha256sum "$OMC" | cut -c1-64)" |
| 53 | for t in $TARGETS fz_plant; do |
| 54 | cat "$R/seed/net/http.om" "$R/seed/net/https.om" "$D/common.om" "$D/$t.om" > "$OUT/bin/$t.prog.om" |
| 55 | if $NICE sh "$R/om/tls/build.sh" "$OMC" "$OUT/bin/$t.prog.om" "$OUT/bin/$t" > "$OUT/bin/$t.build.log" 2>&1 |
| 56 | then say "built $t $(sha256sum "$OUT/bin/$t" | cut -c1-16)"; else bad "build $t (see $OUT/bin/$t.build.log)"; fi |
| 57 | done |
| 58 | } |
| 59 | |
| 60 | # run1 TARGET FILE -> sets ex (exit status) and rc (the fz_say depth code, if any) |
| 61 | run1() { |
| 62 | a=$(targ "$1"); timeout 20 "$OUT/bin/$1" $a < "$2" > /dev/null 2> "$OUT/err"; ex=$? |
| 63 | rc=$(sed -n 's/^rc \([0-9]*\)$/\1/p' "$OUT/err" | tail -1) |
| 64 | } |
| 65 | |
| 66 | # replay: every seed and every regression input must exit < 100; depth codes must match seeds/EXPECT |
| 67 | replay() { |
| 68 | exp=${1:-$S/EXPECT}; n=0 |
| 69 | for t in $TARGETS; do |
| 70 | for f in "$S/$t"/* "$D/regress/$t"/*; do |
| 71 | [ -f "$f" ] || continue; run1 "$t" "$f"; n=$((n+1)) |
| 72 | [ "$ex" -ge 100 ] && bad "replay $t $(basename "$f") exit $ex (trap $((ex-100)))" |
| 73 | [ "$ex" -eq 124 ] && bad "replay $t $(basename "$f") timeout" |
| 74 | w=$(awk -v t="$t" -v s="$(basename "$f")" '$1==t && $2==s {print $3}' "$exp") |
| 75 | [ -n "$w" ] && [ "$w" != "$rc" ] && bad "replay $t $(basename "$f") rc $rc expected $w" |
| 76 | done |
| 77 | done |
| 78 | say "replayed $n inputs" |
| 79 | } |
| 80 | |
| 81 | # plant: the planted defect must trap natively, and AFL must find it blind from \0abcd |
| 82 | plant() { |
| 83 | printf '\0OMFZ' > "$OUT/hit"; run1 fz_plant "$OUT/hit" |
| 84 | [ "$ex" -eq 103 ] && say "PASS plant native exit 103" || bad "plant native exit $ex, want 103" |
| 85 | printf '\0OMFY' > "$OUT/miss"; run1 fz_plant "$OUT/miss" |
| 86 | [ "$ex" -eq 0 ] && say "PASS plant near-miss exit 0" || bad "plant near-miss exit $ex, want 0" |
| 87 | # negative control for replay: a corrupted expectation must FAIL |
| 88 | awk 'NR==1 {$3 = ($3 == "000") ? "999" : "000"} {print}' "$S/EXPECT" > "$OUT/EXPECT.bad" |
| 89 | f0=$fails; replay "$OUT/EXPECT.bad" > "$OUT/negctl.log"; f1=$fails; fails=$f0 |
| 90 | [ "$f1" -gt "$f0" ] && say "PASS replay negative control (corrupted EXPECT line 1 fails)" || bad "replay negative control did not fail" |
| 91 | afl_env; c=$(echo $CPUS | cut -d' ' -f1); rm -rf "$OUT/plant" |
| 92 | AFL_BENCH_UNTIL_CRASH=1 $NICE timeout "${PLANT_S:-1800}" "$AFLPP/afl-fuzz" -Q -c 0 -b "$c" \ |
| 93 | -i "$S/fz_plant" -o "$OUT/plant" -t 2000 -- "$OUT/bin/fz_plant" > "$OUT/plant.log" 2>&1 |
| 94 | stats fz_plant "$OUT/plant/default" |
| 95 | k=$(ls "$OUT/plant/default/crashes" 2>/dev/null | grep -c '^id') |
| 96 | [ "$k" -ge 1 ] && say "PASS plant found blind ($k crash)" || bad "plant not found in ${PLANT_S:-1800} s" |
| 97 | } |
| 98 | |
| 99 | # stats TARGET DIR: one tsv line from fuzzer_stats |
| 100 | stats() { |
| 101 | f=$2/fuzzer_stats; [ -f "$f" ] || { say "stats $1 NOT_RUN"; return; } |
| 102 | g() { sed -n "s/^$1 *: //p" "$f"; } |
| 103 | printf '%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n' "$1" "$(g run_time)" "$(g execs_done)" "$(g execs_per_sec)" \ |
| 104 | "$(g saved_crashes)" "$(g saved_hangs)" "$(g edges_found)" "$(g bitmap_cvg)" "$(g corpus_count)" | tee -a "$OUT/stats.tsv" |
| 105 | } |
| 106 | |
| 107 | fuzz() { |
| 108 | [ "$HOURS" = 0 ] && { say "fuzz NOT_RUN (FUZZ_HOURS=0)"; return; } |
| 109 | afl_env; nt=$(echo $TARGETS | wc -w); nc=$(echo $CPUS | wc -w) |
| 110 | secs=$(awk -v h="$HOURS" -v n="$nt" -v c="$nc" 'BEGIN{printf "%d", h*3600*c/n}') |
| 111 | printf 'target\trun_time\texecs\texecs_per_sec\tcrashes\thangs\tedges\tbitmap\tcorpus\n' > "$OUT/stats.tsv" |
| 112 | set -- $CPUS; i=0 |
| 113 | for t in $TARGETS; do |
| 114 | c=$1; [ $((i % nc)) -eq 1 ] && c=${2:-$1}; i=$((i+1)) |
| 115 | ( $NICE "$AFLPP/afl-fuzz" -Q -c 0 -b "$c" -V "$secs" -i "$S/$t" -o "$OUT/camp/$t" -t 5000 -- \ |
| 116 | "$OUT/bin/$t" $(targ "$t") > "$OUT/camp-$t.log" 2>&1 ) & |
| 117 | [ $((i % nc)) -eq 0 ] && wait |
| 118 | done |
| 119 | wait |
| 120 | for t in $TARGETS; do stats "$t" "$OUT/camp/$t/default"; done |
| 121 | } |
| 122 | |
| 123 | cmin() { |
| 124 | afl_env |
| 125 | for t in $TARGETS; do |
| 126 | q=$OUT/camp/$t/default/queue; [ -d "$q" ] || continue |
| 127 | rm -rf "$OUT/cmin/$t"; a=$(targ "$t") |
| 128 | $NICE "$AFLPP/afl-cmin" -Q -T 2 -i "$q" -o "$OUT/cmin/$t" -t 5000 -- "$OUT/bin/$t" $a > "$OUT/cmin-$t.log" 2>&1 |
| 129 | say "cmin $t $(ls "$q" | grep -c '^id') -> $(ls "$OUT/cmin/$t" 2>/dev/null | wc -l)" |
| 130 | done |
| 131 | } |
| 132 | |
| 133 | # triage: reproduce every saved crash natively, dedupe by (target, exit, sha256), copy, one BOARD line |
| 134 | triage() { |
| 135 | mkdir -p "$CRASHES"; new=0; tot=0 |
| 136 | for t in $TARGETS; do |
| 137 | for f in "$OUT"/camp/"$t"/default/crashes/id* "$CORPUS"/"$t"/default/crashes/id*; do |
| 138 | [ -f "$f" ] || continue; tot=$((tot+1)); run1 "$t" "$f" |
| 139 | if [ "$ex" -lt 100 ] || [ "$ex" -eq 124 ]; then say "triage $t $(basename "$f") does not reproduce natively (exit $ex)"; continue; fi |
| 140 | h=$(sha256sum "$f" | cut -c1-12); n=$CRASHES/$t-exit$ex-$h |
| 141 | [ -e "$n" ] || { cp "$f" "$n"; new=$((new+1)); say "triage NEW $n"; } |
| 142 | done |
| 143 | done |
| 144 | line="$(date -u +%Y-%m-%dT%H:%MZ) TlsProof.TlsFuzz: om/tls fuzz triage: $tot saved crashes, $new new unique reproduced (MEASURED $CRASHES)" |
| 145 | say "$line"; [ "${FUZZ_NO_BOARD:-0}" = 1 ] || echo "$line" >> "$BOARD" |
| 146 | } |
| 147 | |
| 148 | # service: persistent workers (one per cpu) rotating targets with AFL resume; the watchdog |
| 149 | # (service/fuzz-watchdog.sh) pauses/stops this unit |
| 150 | service() { |
| 151 | afl_env; mkdir -p "$CORPUS"; set -- $TARGETS; nt=$# |
| 152 | k=0 |
| 153 | for c in $CPUS; do |
| 154 | ( j=$k; while :; do |
| 155 | t=$(echo $TARGETS | cut -d' ' -f$((j % nt + 1))); j=$((j + 2)) |
| 156 | "$AFLPP/afl-fuzz" -Q -c 0 -b "$c" -V "${FUZZ_SLICE_S:-3600}" -i "$S/$t" -o "$CORPUS/$t" -t 5000 -- \ |
| 157 | "$OUT/bin/$t" $(targ "$t") >> "$CORPUS/$t.log" 2>&1 |
| 158 | done ) & |
| 159 | k=$((k+1)) |
| 160 | done |
| 161 | wait |
| 162 | } |
| 163 | |
| 164 | [ $# -eq 0 ] && set -- build replay plant |
| 165 | for st in "$@"; do case $st in build|replay|plant|fuzz|cmin|triage|service) $st ;; *) echo "unknown stage $st" >&2; exit 2;; esac; done |
| 166 | say "fails=$fails"; [ $fails -eq 0 ] |