aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / omega / platform / kernel-repro.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Exact distro C reference oracle. Never opens sysfs or a PCI device. |
| 6 | # POSIX sh, awk, sha256sum, patch, nice and cc. |
| 7 | # |
| 8 | # Usage: sh native/omega/platform/kernel-repro.sh OUT_DIR |
| 9 | # |
| 10 | # Applies the actual proposed patch to a disposable copy of the pinned |
| 11 | # reference source (not a hand-coded fixed oracle), extracts the original and |
| 12 | # patched pcie_cooling_get_* functions, and builds four programs with small |
| 13 | # reference structs: the bug reproduction and the full boundary suite, each |
| 14 | # against the old and the patched code. Every program must compile cleanly |
| 15 | # under -Wall -Wextra -Werror and exit with its expected status, so the red and |
| 16 | # green controls run in both directions. Writes the programs, their outputs |
| 17 | # and results.json into OUT_DIR. |
| 18 | set -eu |
| 19 | export LC_ALL=C |
| 20 | # cd prints the directory when CDPATH supplies it, which would corrupt $(cd ...). |
| 21 | unset CDPATH |
| 22 | |
| 23 | die() { printf 'PCIE COOLING REPRO FAIL: %s\n' "$*" >&2; exit 1; } |
| 24 | |
| 25 | [ "$#" -eq 1 ] || die "usage: kernel-repro.sh OUT_DIR" |
| 26 | here=$(cd "$(dirname "$0")" && pwd -P) |
| 27 | mkdir -p "$1" |
| 28 | out=$(cd "$1" && pwd -P) |
| 29 | |
| 30 | ref=$here/pcie-cooling-ref.c |
| 31 | proposal=$here/pcie-cooling-proposal.patch |
| 32 | [ -f "$ref" ] && [ -f "$proposal" ] || die "missing reference source or proposal patch" |
| 33 | got=$(sha256sum < "$ref" | cut -d' ' -f1) |
| 34 | [ "$got" = fff34da417c7f8cdb642a8b8ac20b7a5d1010392a2a7b8662178078abd1824c9 ] || die "reference source pin mismatch (got $got)" |
| 35 | |
| 36 | tree=$out/patch-tree/drivers/thermal |
| 37 | mkdir -p "$tree" |
| 38 | cp "$ref" "$tree/pcie_cooling.c" |
| 39 | rc=0 |
| 40 | (cd "$out/patch-tree" && patch --batch -p1 -i "$proposal") > "$out/patch.stdout" 2> "$out/patch.stderr" || rc=$? |
| 41 | [ "$rc" -eq 0 ] || { cat "$out/patch.stderr" >&2; die "proposal patch does not apply (exit $rc)"; } |
| 42 | fixed=$tree/pcie_cooling.c |
| 43 | |
| 44 | # Every match of: "static int pcie_cooling_get_" word "(" then one or more |
| 45 | # characters other than "}" up to the first "}" that starts a line. Matches |
| 46 | # are joined with one newline and no trailing newline. Requires exactly two |
| 47 | # (get_max_level and get_cur_level). |
| 48 | functions() { # file -> stdout |
| 49 | awk ' |
| 50 | { text = text $0 "\n" } |
| 51 | END { |
| 52 | key = "static int pcie_cooling_get_"; s = text; first = 1; found = 0 |
| 53 | while ((i = index(s, key)) > 0) { |
| 54 | rest = substr(s, i + length(key)) |
| 55 | if (!match(rest, /^[A-Za-z0-9_]+\(/)) { s = substr(s, i + 1); continue } |
| 56 | body = substr(rest, RLENGTH + 1) |
| 57 | j = index(body, "}") |
| 58 | if (j < 3 || substr(body, j - 1, 1) != "\n") { s = substr(s, i + 1); continue } |
| 59 | m = key substr(rest, 1, RLENGTH) substr(body, 1, j) |
| 60 | printf "%s%s", (first ? "" : "\n"), m |
| 61 | first = 0; found++ |
| 62 | s = substr(body, j + 1) |
| 63 | } |
| 64 | if (found != 2) { printf "PCIE COOLING REPRO FAIL: expected 2 get functions, found %d\n", found > "/dev/stderr"; exit 1 } |
| 65 | }' "$1" |
| 66 | } |
| 67 | functions "$ref" > "$out/old.functions" || die "reference function extraction" |
| 68 | functions "$fixed" > "$out/fixed.functions" || die "patched function extraction" |
| 69 | |
| 70 | cat > "$out/header.c" <<'EOF' |
| 71 | #include <stdio.h> |
| 72 | #include <errno.h> |
| 73 | #define READ_ONCE(x) (x) |
| 74 | enum pci_bus_speed { PCIE_SPEED_2_5GT=0x14, PCIE_SPEED_64_0GT=0x19, PCI_SPEED_UNKNOWN=0xff }; |
| 75 | struct pci_bus { unsigned char max_bus_speed, cur_bus_speed; }; |
| 76 | struct pci_dev { struct pci_bus *subordinate; }; |
| 77 | struct thermal_cooling_device { void *devdata; unsigned long max_state; }; |
| 78 | EOF |
| 79 | |
| 80 | cat > "$out/bug-main.c" <<'EOF' |
| 81 | int main(void) { |
| 82 | struct pci_bus bus={0x18,0xff}; struct pci_dev dev={&bus}; |
| 83 | struct thermal_cooling_device c={&dev,4}; unsigned long s=999; |
| 84 | if (pcie_cooling_get_max_level(&c,&s) || s!=4) return 7; |
| 85 | s=999; |
| 86 | int r=pcie_cooling_get_cur_level(&c,&s); |
| 87 | if (r || (long)s != -231) return 3; |
| 88 | printf("REPRODUCED unknown=0xff max_state=4 cur_state=%ld unsigned=%lu\n",(long)s,s); |
| 89 | return 0; } |
| 90 | EOF |
| 91 | |
| 92 | cat > "$out/test-main.c" <<'EOF' |
| 93 | int main(void) { |
| 94 | struct pci_bus bus={0x18,0x14}; struct pci_dev dev={&bus}; |
| 95 | struct thermal_cooling_device c={&dev,4}; unsigned long s; |
| 96 | for (unsigned int i=0x14; i<=0x19; i++) { |
| 97 | bus.max_bus_speed=i; s=999; |
| 98 | if (pcie_cooling_get_max_level(&c,&s) || s!=i-0x14) return 1; |
| 99 | c.max_state=i-0x14; |
| 100 | for (unsigned int j=0x14; j<=i; j++) { |
| 101 | bus.cur_bus_speed=j; s=999; |
| 102 | if (pcie_cooling_get_cur_level(&c,&s) || s!=i-j) return 2; |
| 103 | }} |
| 104 | unsigned int bad[]={0,0x13,0x1a,0xff}; |
| 105 | for (unsigned int i=0;i<4;i++) { |
| 106 | bus.cur_bus_speed=bad[i]; s=999; |
| 107 | if (pcie_cooling_get_cur_level(&c,&s)!=-EAGAIN || s!=999) return 4; |
| 108 | bus.max_bus_speed=bad[i]; s=999; |
| 109 | if (pcie_cooling_get_max_level(&c,&s)!=-EINVAL || s!=999) return 5; |
| 110 | } |
| 111 | c.max_state=4; bus.cur_bus_speed=0x19; s=999; |
| 112 | if (pcie_cooling_get_cur_level(&c,&s)!=-EAGAIN || s!=999) return 6; |
| 113 | printf("FIX PASS all enum speeds, unknown/non-PCIe, stale-above-max; outputs unchanged on errors\n"); |
| 114 | return 0; } |
| 115 | EOF |
| 116 | |
| 117 | results='' |
| 118 | build() { # name functions main expected_exit |
| 119 | name=$1 fns=$2 main=$3 want=$4 |
| 120 | src=$out/$name.c |
| 121 | bin=$out/$name |
| 122 | cat "$out/header.c" "$fns" "$main" > "$src" |
| 123 | rc=0 |
| 124 | nice -n 15 cc -std=c11 -Wall -Wextra -Werror -o "$bin" "$src" > /dev/null 2> "$out/$name.compile.stderr" || rc=$? |
| 125 | [ "$rc" -eq 0 ] || { cat "$out/$name.compile.stderr" >&2; die "$name: compile failed (exit $rc)"; } |
| 126 | rc=0 |
| 127 | "$bin" > "$out/$name.stdout" 2> /dev/null || rc=$? |
| 128 | [ "$rc" -eq "$want" ] || die "$name: exit $rc, expected $want" |
| 129 | sum=$(sha256sum < "$bin" | cut -d' ' -f1) |
| 130 | entry=$(printf ' {\n "name": "%s",\n "compile_exit": 0,\n "run_exit": %d,\n "expected": %d,\n "binary_sha256": "%s"\n }' "$name" "$rc" "$want" "$sum") |
| 131 | if [ -n "$results" ]; then results="$results, |
| 132 | $entry"; else results=$entry; fi |
| 133 | } |
| 134 | build old-repro "$out/old.functions" "$out/bug-main.c" 0 |
| 135 | build fixed-repro-negative "$out/fixed.functions" "$out/bug-main.c" 3 |
| 136 | build fixed-boundaries "$out/fixed.functions" "$out/test-main.c" 0 |
| 137 | build old-boundaries-negative "$out/old.functions" "$out/test-main.c" 4 |
| 138 | |
| 139 | printf '[\n%s\n]\n' "$results" > "$out/results.json" |
| 140 | printf 'PCIE COOLING REPRO PASS: old reproduces -231; proposed fix refuses; full boundary oracle passes and kills old code\n' |