interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

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.
12set -u
13OMC=$1 IMAGE=$2 BUSYBOX=$3 OUT=$4
14PROFILES=${5:-clean loss reorder hostile}
15SCEN=${6:-echo h2g g2h}
16HERE=$(cd "$(dirname "$0")" && pwd)
17# The compiler pin is the repository's one PIN file (moved by pin-follow.sh).
18PIN=$(sed -n 's/^omc-sha256 //p' "$HERE/../../../PIN")
19[ -n "$PIN" ] || { echo "GATE_FAIL no omc-sha256 line in $HERE/../../../PIN"; exit 2; }
20mkdir -p "$OUT" || exit 2
21OUT=$(cd "$OUT" && pwd)
22have=$(sha256sum "$OMC" | cut -d' ' -f1)
23if [ "$have" != "$PIN" ]; then echo "GATE_FAIL omc sha256 $have is not the pin $PIN"; exit 2; fi
24B=$OUT/bin; mkdir -p "$B"
25cat "$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; }
28cat "$HERE/tcp.om" "$HERE/corpus.om" | "$OMC" > "$B/corpus" || { echo "GATE_FAIL build corpus"; exit 2; }
29chmod +x "$B/tcpd" "$B/link" "$B/peer" "$B/corpus"
30sha256sum "$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.
32FUZZ=${FUZZ:-2000000}
33"$B/corpus" "$FUZZ" > "$OUT/corpus.log" 2>&1
34CS=$?
35if [ $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
37else
38 echo "CORPUS_OK $(grep '^CORPUS_PASS' "$OUT/corpus.log")"; corpus_ok=1
39fi
40qemu-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.
43P1=$OUT/payload1
44{ cat "$BUSYBOX"; cat "$BUSYBOX"; } | gzip -9 -n | head -c 1048576 > "$P1"
45cat "$P1" "$P1" | head -c 2000000 > "$OUT/payload2"
46P2=$OUT/payload2
47# Initramfs
48R=$OUT/rd; rm -rf "$R"; mkdir -p "$R/bin" "$R/proc" "$R/sys" "$R/dev" "$R/tmp"
49cp "$BUSYBOX" "$R/bin/busybox"; cp "$B/tcpd" "$R/tcpd"
50cat > "$R/init" <<'EOF'
51#!/bin/busybox sh
52B=/bin/busybox
53$B mount -t proc p /proc
54$B mount -t sysfs s /sys
55$B mount -t devtmpfs d /dev
56A=$($B sed -n 's/.*tcpdargs=\([^ ]*\).*/\1/p' /proc/cmdline | $B tr , ' ')
57/tcpd $A
58echo "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
63EOF
64chmod +x "$R/init"
65(cd "$R" && find . | cpio -o -H newc 2>/dev/null | gzip -1 > "$OUT/rd.cpio.gz")
66fails=0; runs=0; n=0
67num() { 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.
70PORTBASE=${PORTBASE:-21100}
71for 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
148done
149if [ $fails -eq 0 ] && [ $corpus_ok -eq 1 ]; then echo "GATE_PASS runs=$runs corpus=pass"; exit 0; fi
150echo "GATE_FAIL failed=$fails runs=$runs corpus_ok=$corpus_ok"; exit 1