Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/net/tcp/gate.sh # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / net / tcp / gate.sh` 150 lines ยท 7903 bytes Raw Blame History 1 #!/bin/sh 2 # gate.sh: L232 QEMU gate for the Omega TCP stack (verification rig, not product code). 3 # Usage: gate.sh OMC IMAGE BUSYBOX OUTDIR [PROFILES] [SCENARIOS] 4 # OMC pinned seeded omc (sha256 checked against the repository PIN file) 5 # IMAGE arm64 Linux Image for the guest (needs virtio-net, AF_PACKET) 6 # BUSYBOX static aarch64 busybox (guest init, sha256sum, and the g2h payload) 7 # PROFILES default "clean loss reorder hostile"; SCENARIOS default "echo h2g g2h" 8 # Each run boots a fresh guest whose only TCP/IP stack is tcpd (tcp.om + tcpd.om), 9 # routes its NIC through link.om (seeded drop / reorder / duplicate / hostile 10 # injection) to QEMU slirp, and talks to peer.om on the host. Exit 0 only when 11 # every run passes every check. Requires qemu-system-aarch64 with KVM. 12 set -u 13 OMC=$1 IMAGE=$2 BUSYBOX=$3 OUT=$4 14 PROFILES=${5:-clean loss reorder hostile} 15 SCEN=${6:-echo h2g g2h} 16 HERE=$(cd "$(dirname "$0")" && pwd) 17 # The compiler pin is the repository's one PIN file (moved by pin-follow.sh). 18 PIN=$(sed -n 's/^omc-sha256 //p' "$HERE/../../../PIN") 19 [ -n "$PIN" ] || { echo "GATE_FAIL no omc-sha256 line in $HERE/../../../PIN"; exit 2; } 20 mkdir -p "$OUT" || exit 2 21 OUT=$(cd "$OUT" && pwd) 22 have=$(sha256sum "$OMC" | cut -d' ' -f1) 23 if [ "$have" != "$PIN" ]; then echo "GATE_FAIL omc sha256 $have is not the pin $PIN"; exit 2; fi 24 B=$OUT/bin; mkdir -p "$B" 25 cat "$HERE/tcp.om" "$HERE/tcpd.om" | "$OMC" > "$B/tcpd" || { echo "GATE_FAIL build tcpd"; exit 2; } 26 "$OMC" < "$HERE/link.om" > "$B/link" || { echo "GATE_FAIL build link"; exit 2; } 27 "$OMC" < "$HERE/peer.om" > "$B/peer" || { echo "GATE_FAIL build peer"; exit 2; } 28 cat "$HERE/tcp.om" "$HERE/corpus.om" | "$OMC" > "$B/corpus" || { echo "GATE_FAIL build corpus"; exit 2; } 29 chmod +x "$B/tcpd" "$B/link" "$B/peer" "$B/corpus" 30 sha256sum "$OMC" "$IMAGE" "$BUSYBOX" "$B/tcpd" "$B/link" "$B/peer" "$B/corpus" > "$OUT/inputs.sha256" 31 # Hosted hostile corpus first: exact-state cases plus FUZZ seeded mutated frames. 32 FUZZ=${FUZZ:-`2000000`} 33 "$B/corpus" "$FUZZ" > "$OUT/corpus.log" 2>&1 34 CS=$? 35 if [ $CS -ne 0 ] || ! grep -q '^CORPUS_PASS' "$OUT/corpus.log"; then 36 echo "CORPUS_FAIL rc=$CS $(head -3 "$OUT/corpus.log" | tr '\n' ' ')"; corpus_ok=0 37 else 38 echo "CORPUS_OK $(grep '^CORPUS_PASS' "$OUT/corpus.log")"; corpus_ok=1 39 fi 40 qemu-system-aarch64 --version | head -1 >> "$OUT/inputs.sha256" 41 # Payloads: a fixed pseudo-random file (derived from the busybox bytes, so it is 42 # reproducible) for echo and host-to-guest, the busybox binary for guest-to-host. 43 P1=$OUT/payload1 44 { cat "$BUSYBOX"; cat "$BUSYBOX"; } | gzip -9 -n | head -c `1048576` > "$P1" 45 cat "$P1" "$P1" | head -c `2000000` > "$OUT/payload2" 46 P2=$OUT/payload2 47 # Initramfs 48 R=$OUT/rd; rm -rf "$R"; mkdir -p "$R/bin" "$R/proc" "$R/sys" "$R/dev" "$R/tmp" 49 cp "$BUSYBOX" "$R/bin/busybox"; cp "$B/tcpd" "$R/tcpd" 50 cat > "$R/init" <<'EOF' 51 #!/bin/busybox sh 52 B=/bin/busybox 53 $B mount -t proc p /proc 54 $B mount -t sysfs s /sys 55 $B mount -t devtmpfs d /dev 56 A=$($B sed -n 's/.*tcpdargs=\([^ ]*\).*/\1/p' /proc/cmdline | $B tr , ' ') 57 /tcpd $A 58 echo "TCPD_EXIT=$?" 59 [ -f /tmp/rx ] && $B sha256sum /tmp/rx 60 # Give the serial console time to drain; poweroff -f can cut the last lines. 61 $B sleep 1 62 $B poweroff -f 63 EOF 64 chmod +x "$R/init" 65 (cd "$R" && find . | cpio -o -H newc 2>/dev/null | gzip -1 > "$OUT/rd.cpio.gz") 66 fails=0; runs=0; n=0 67 num() { v=$(grep -a "^$1 " "$2" | head -1 | cut -d' ' -f"$3"); echo "${v:-0}"; } 68 # Host ports sit below the Linux ephemeral range (32768-60999) so an unrelated 69 # outgoing connection cannot already hold one when slirp binds its forward. 70 PORTBASE=${PORTBASE:-21100} 71 for prof in $PROFILES; do 72 case $prof in 73 clean) RATES="0 0 0 0" ;; 74 loss) RATES="30 0 0 0" ;; 75 reorder) RATES="0 150 0 0" ;; 76 hostile) RATES="20 100 20 200" ;; 77 *) echo "GATE_FAIL unknown profile $prof"; exit 2 ;; 78 esac 79 for sc in $SCEN; do 80 n=$((n + 1)); runs=$((runs + 1)) 81 HP=$((PORTBASE + n)); SEED=$((7919 * n + 17)) 82 D=$OUT/run-$prof-$sc; rm -rf "$D"; mkdir -p "$D"; : > "$D/console.log" 83 T=$(mktemp -d /tmp/l232g.XXXXXX) 84 case $sc in 85 echo) ARGS="e,7,-,-,150"; FWD=",hostfwd=tcp:127.0.0.1:$HP-10.0.2.15:7"; WANT=$P1 ;; 86 h2g) ARGS="r,9,-,/tmp/rx,150"; FWD=",hostfwd=tcp:127.0.0.1:$HP-10.0.2.15:9"; WANT=$P2 ;; 87 g2h) ARGS="s,$HP,10.0.2.2,/bin/busybox,150"; FWD=""; WANT=$BUSYBOX ;; 88 esac 89 # link.om stops on its own when the guest goes quiet; timeout bounds it if traffic never stops. 90 timeout 230 "$B/link" "$T/l1" "$T/g" "$T/l2" "$T/s" $SEED $RATES 8 > "$D/link.log" 2>&1 & 91 LP=$! 92 if [ "$sc" = g2h ]; then 93 "$B/peer" r $HP "$WANT" 170 "$D/received" > "$D/peer.log" 2>&1 & 94 PP=$! 95 fi 96 sleep 0.2 97 timeout 200 qemu-system-aarch64 -M virt -accel kvm -cpu host -m 512 -smp 1 -nographic -no-reboot \ 98 -kernel "$IMAGE" -initrd "$OUT/rd.cpio.gz" \ 99 -append "console=ttyAMA0 quiet panic=-1 ipv6.disable=1 tcpdargs=$ARGS" \ 100 -netdev dgram,id=n0,local.type=unix,local.path=$T/g,remote.type=unix,remote.path=$T/l1 \ 101 -device virtio-net-pci,netdev=n0,romfile= \ 102 -netdev dgram,id=d1,local.type=unix,local.path=$T/s,remote.type=unix,remote.path=$T/l2 \ 103 -netdev user,id=u0$FWD \ 104 -netdev hubport,id=hp0,hubid=1,netdev=u0 -netdev hubport,id=hp1,hubid=1,netdev=d1 \ 105 > "$D/console.log" 2>&1 & 106 QP=$! 107 if [ "$sc" != g2h ]; then 108 i=0; while [ $i -lt 300 ] && ! grep -aq TCPD_READY "$D/console.log"; do sleep 0.1; i=$((i + 1)); done 109 MODE=e; [ "$sc" = h2g ] && MODE=s 110 "$B/peer" $MODE $HP "$WANT" 170 > "$D/peer.log" 2>&1 111 PS=$? 112 else 113 wait $PP; PS=$? 114 fi 115 wait $QP; QS=$? 116 wait $LP; LS=$? 117 rm -rf "$T" 118 ok=1; why="" 119 [ $PS -eq 0 ] && grep -aq '^PEER_PASS' "$D/peer.log" || { ok=0; why="$why peer"; } 120 grep -aq '^TCPD_PASS' "$D/console.log" && grep -aq 'TCPD_EXIT=0' "$D/console.log" || { ok=0; why="$why tcpd"; } 121 [ $LS -eq 0 ] || { ok=0; why="$why link"; } 122 [ $QS -eq 0 ] || { ok=0; why="$why qemu"; } 123 want=$(sha256sum "$WANT" | cut -d' ' -f1) 124 # Echo bytes are compared one by one by peer.om; the two transfers are compared by digest. 125 case $sc in 126 h2g) got=$(grep -a ' /tmp/rx' "$D/console.log" | cut -d' ' -f1) ;; 127 g2h) got=$(sha256sum "$D/received" | cut -d' ' -f1) ;; 128 echo) got=peer-compared ;; 129 esac 130 [ "$sc" = echo ] || [ "$got" = "$want" ] || { ok=0; why="$why digest"; } 131 # The profile must really have impaired this run (LINK fields: 2 in 3 out 4 drop 5 held 6 dup, +8 for B to A; 18 hostile). 132 drop=$(( $(num LINK "$D/link.log" 4) + $(num LINK "$D/link.log" 12) )) 133 held=$(( $(num LINK "$D/link.log" 5) + $(num LINK "$D/link.log" 13) )) 134 dup=$(( $(num LINK "$D/link.log" 6) + $(num LINK "$D/link.log" 14) )) 135 host=$(num LINK "$D/link.log" 18) 136 set -- $RATES 137 [ "$1" -eq 0 ] || [ $drop -gt 0 ] || { ok=0; why="$why nodrop"; } 138 [ "$2" -eq 0 ] || [ $held -gt 0 ] || { ok=0; why="$why noreorder"; } 139 [ "$3" -eq 0 ] || [ $dup -gt 0 ] || { ok=0; why="$why nodup"; } 140 [ "$4" -eq 0 ] || [ $host -gt 0 ] || { ok=0; why="$why nohostile"; } 141 # Every one of the 15 hostile kinds (LINKH fields 2..16) must have been injected. 142 kinds=0; k=2; while [ $k -le 16 ]; do [ $(num LINKH "$D/link.log" $k) -gt 0 ] && kinds=$((kinds + 1)); k=$((k + 1)); done 143 [ "$4" -eq 0 ] || [ $kinds -eq 15 ] || { ok=0; why="$why kinds=$kinds"; } 144 [ "$1$2$3$4" != 0000 ] || [ $((drop + held + dup + host)) -eq 0 ] || { ok=0; why="$why notclean"; } 145 line="$prof $sc sha=$got drop=$drop held=$held dup=$dup hostile=$host qemu=$QS $(grep -a '^TCPD_PASS\|^TCPD_FAIL' "$D/console.log" | head -1)" 146 if [ $ok -eq 1 ]; then echo "RUN_PASS $line"; else echo "RUN_FAIL$why $line"; fails=$((fails + 1)); fi 147 done 148 done 149 if [ $fails -eq 0 ] && [ $corpus_ok -eq 1 ]; then echo "GATE_PASS runs=$runs corpus=pass"; exit 0; fi 150 echo "GATE_FAIL failed=$fails runs=$runs corpus_ok=$corpus_ok"; exit 1