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 / omega / platform / kernel-repro.sh

140 lines · 6063 bytes Raw Blame History
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.
18set -eu
19export LC_ALL=C
20# cd prints the directory when CDPATH supplies it, which would corrupt $(cd ...).
21unset CDPATH
22
23die() { printf 'PCIE COOLING REPRO FAIL: %s\n' "$*" >&2; exit 1; }
24
25[ "$#" -eq 1 ] || die "usage: kernel-repro.sh OUT_DIR"
26here=$(cd "$(dirname "$0")" && pwd -P)
27mkdir -p "$1"
28out=$(cd "$1" && pwd -P)
29
30ref=$here/pcie-cooling-ref.c
31proposal=$here/pcie-cooling-proposal.patch
32[ -f "$ref" ] && [ -f "$proposal" ] || die "missing reference source or proposal patch"
33got=$(sha256sum < "$ref" | cut -d' ' -f1)
34[ "$got" = fff34da417c7f8cdb642a8b8ac20b7a5d1010392a2a7b8662178078abd1824c9 ] || die "reference source pin mismatch (got $got)"
35
36tree=$out/patch-tree/drivers/thermal
37mkdir -p "$tree"
38cp "$ref" "$tree/pcie_cooling.c"
39rc=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)"; }
42fixed=$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).
48functions() { # 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}
67functions "$ref" > "$out/old.functions" || die "reference function extraction"
68functions "$fixed" > "$out/fixed.functions" || die "patched function extraction"
69
70cat > "$out/header.c" <<'EOF'
71#include <stdio.h>
72#include <errno.h>
73#define READ_ONCE(x) (x)
74enum pci_bus_speed { PCIE_SPEED_2_5GT=0x14, PCIE_SPEED_64_0GT=0x19, PCI_SPEED_UNKNOWN=0xff };
75struct pci_bus { unsigned char max_bus_speed, cur_bus_speed; };
76struct pci_dev { struct pci_bus *subordinate; };
77struct thermal_cooling_device { void *devdata; unsigned long max_state; };
78EOF
79
80cat > "$out/bug-main.c" <<'EOF'
81int main(void) {
82struct pci_bus bus={0x18,0xff}; struct pci_dev dev={&bus};
83struct thermal_cooling_device c={&dev,4}; unsigned long s=999;
84if (pcie_cooling_get_max_level(&c,&s) || s!=4) return 7;
85s=999;
86int r=pcie_cooling_get_cur_level(&c,&s);
87if (r || (long)s != -231) return 3;
88printf("REPRODUCED unknown=0xff max_state=4 cur_state=%ld unsigned=%lu\n",(long)s,s);
89return 0; }
90EOF
91
92cat > "$out/test-main.c" <<'EOF'
93int main(void) {
94struct pci_bus bus={0x18,0x14}; struct pci_dev dev={&bus};
95struct thermal_cooling_device c={&dev,4}; unsigned long s;
96for (unsigned int i=0x14; i<=0x19; i++) {
97bus.max_bus_speed=i; s=999;
98if (pcie_cooling_get_max_level(&c,&s) || s!=i-0x14) return 1;
99c.max_state=i-0x14;
100for (unsigned int j=0x14; j<=i; j++) {
101bus.cur_bus_speed=j; s=999;
102if (pcie_cooling_get_cur_level(&c,&s) || s!=i-j) return 2;
103}}
104unsigned int bad[]={0,0x13,0x1a,0xff};
105for (unsigned int i=0;i<4;i++) {
106bus.cur_bus_speed=bad[i]; s=999;
107if (pcie_cooling_get_cur_level(&c,&s)!=-EAGAIN || s!=999) return 4;
108bus.max_bus_speed=bad[i]; s=999;
109if (pcie_cooling_get_max_level(&c,&s)!=-EINVAL || s!=999) return 5;
110}
111c.max_state=4; bus.cur_bus_speed=0x19; s=999;
112if (pcie_cooling_get_cur_level(&c,&s)!=-EAGAIN || s!=999) return 6;
113printf("FIX PASS all enum speeds, unknown/non-PCIe, stale-above-max; outputs unchanged on errors\n");
114return 0; }
115EOF
116
117results=''
118build() { # 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}
134build old-repro "$out/old.functions" "$out/bug-main.c" 0
135build fixed-repro-negative "$out/fixed.functions" "$out/bug-main.c" 3
136build fixed-boundaries "$out/fixed.functions" "$out/test-main.c" 0
137build old-boundaries-negative "$out/old.functions" "$out/test-main.c" 4
138
139printf '[\n%s\n]\n' "$results" > "$out/results.json"
140printf 'PCIE COOLING REPRO PASS: old reproduces -231; proposed fix refuses; full boundary oracle passes and kills old code\n'