#!/usr/bin/env bash # What a NativeAOT edge actually does on this tree, measured rather than argued. # # This is not a gate. It is the reproduction of a verdict: the AOT build compiles, # starts, and creates its own schema, and then every EF query it makes fails, # because EF cannot compile a LINQ query at run time in a NativeAOT binary and the # only alternative — precompiling them at build time — does not produce compilable # code for this model. Run it when the EF version changes, before deciding again # whether AOT is worth the price. # # scripts/aot-probe.sh # publish + start + curl # scripts/aot-probe.sh --regen # regenerate the EF compiled model first # # Needs the NativeAOT toolchain (clang/lld) and takes a few minutes: the ILC step # alone is over a minute. It is deliberately not in `ci-gates.sh all` — a machine # without clang would then report a build failure as if the product were broken. # # The compiled model is generated for sqlite, the provider the shipped image uses, # and is compiled into the binary only under -p:OpenWafCompiledModel=true; the # framework-dependent build still builds its model at start-up and serves both # providers. See dotnet/OpenWaf.Infrastructure/OpenWaf.Infrastructure.csproj. set -uo pipefail cd "$(dirname "$0")/.." || exit 2 RUNTIME=$(dotnet --info 2>/dev/null | sed -n 's/^ *RID: *//p' | head -1) [ -n "$RUNTIME" ] || { printf 'cannot work out this RID from the dotnet --info output\n' >&2; exit 2; } PUBLISH=dotnet/OpenWaf.Edge/bin/Release/net10.0/$RUNTIME/publish BIN=$PUBLISH/OpenWaf.Edge if [ "${1:-}" = "--regen" ]; then printf '\n=== regenerate the EF compiled model (sqlite) ===\n' rm -rf dotnet/OpenWaf.Infrastructure/Persistence/CompiledModel # OPENWAF_MIGRATE_PROVIDER=sqlite: the design-time factory defaults to postgres, # and a postgres compiled model in a sqlite image is a different kind of broken. (cd dotnet && OPENWAF_MIGRATE_PROVIDER=sqlite dotnet ef dbcontext optimize \ --project OpenWaf.Infrastructure --startup-project OpenWaf.Infrastructure \ --context OpenWafDbContext --namespace OpenWaf.Infrastructure.Persistence.CompiledModel \ --output-dir Persistence/CompiledModel) || { printf 'compiled model generation failed\n' >&2; exit 1; } fi printf '\n=== publish NativeAOT (%s) ===\n' "$RUNTIME" # IlcTreatWarningsAsErrors=false and the two NoWarn codes are what let the build # finish at all: EF's assemblies produce IL2104/IL3053 trim warnings and the # schema step still calls three design-time APIs. Those are recorded, not hidden — # the counts are printed below from the same log. AOT_LOG=/tmp/openwaf-aot-publish.log # OpenWafAot, not PublishAot: the latter as a command-line property is global, and # it then reaches the referenced Roslyn analyzer (netstandard2.0) and the publish # dies with NETSDK1207. OpenWaf.Edge.csproj turns this switch into PublishAot for # itself only. (cd dotnet && dotnet publish OpenWaf.Edge/OpenWaf.Edge.csproj -c Release -r "$RUNTIME" \ -p:OpenWafAot=true \ -p:OpenWafCompiledModel=true -p:NoWarn=IL2026%3BIL3050 \ -p:IlcTreatWarningsAsErrors=false) >"$AOT_LOG" 2>&1 publish_status=$? # The warning count only means something if the ILC actually ran. An up-to-date # publish skips "Generating native code" and would otherwise report zero warnings # for a build that produced no output at all. generated=no grep -q 'Generating native code' "$AOT_LOG" && generated=yes printf 'publish exit %s; native code generated: %s; IL warnings: %s; errors: %s\n' \ "$publish_status" "$generated" "$(grep -c ' warning IL[0-9]' "$AOT_LOG")" \ "$(grep -c ' error ' "$AOT_LOG")" [ "$publish_status" -eq 0 ] || { tail -20 "$AOT_LOG"; exit 1; } [ -x "$BIN" ] || { printf 'no native binary at %s\n' "$BIN" >&2; exit 1; } # Absolute, because --contentRoot is resolved against the binary's own directory # rather than the caller's: measured, a relative one came back doubled # (…/publish/dotnet/OpenWaf.Edge/…/publish/) and the host died in # DirectoryNotFoundException before it could say anything useful. PUBLISH="$(cd "$PUBLISH" && pwd)" printf 'native binary: %s bytes\n' "$(wc -c < "$BIN" | tr -d ' ')" EDGE_PORT=${EDGE_PORT:-18077} UP_PORT=${UP_PORT:-18078} DB=/tmp/openwaf-aot-probe-$$.db LOG=/tmp/openwaf-aot-probe-edge.log TOKEN=aot-probe-token rm -f "$DB" "$DB-shm" "$DB-wal" cleanup() { [ -n "${EDGE_PID:-}" ] && kill "$EDGE_PID" 2>/dev/null [ -n "${UP_PID:-}" ] && kill "$UP_PID" 2>/dev/null rm -f "$DB" "$DB-shm" "$DB-wal" } trap cleanup EXIT mkdir -p /tmp/openwaf-upstream && printf 'aot probe upstream\n' > /tmp/openwaf-upstream/body.txt python3 -m http.server "$UP_PORT" --bind 127.0.0.1 -d /tmp/openwaf-upstream >/dev/null 2>&1 & UP_PID=$! # OPENWAF_SCHEMA=script is the point of the exercise: EnsureCreated and # GenerateCreateScript both go through EF's design-time model, which a NativeAOT # build refuses outright, so the DDL is generated at build time and read out of # the binary. The schema-script gate keeps that file equal to what the model # generates. ASPNETCORE_URLS="http://127.0.0.1:$EDGE_PORT" \ OPENWAF_ADMIN_TOKEN="$TOKEN" \ OPENWAF_SCHEMA=script \ ConnectionStrings__OpenWaf="Data Source=$DB" \ Database__Provider=sqlite \ OpenWaf__Policy__Mode=Enforce \ OpenWaf__Policy__TrustProxyHeaders=true \ OpenWaf__Policy__StreakThreshold=3 \ ReverseProxy__Clusters__upstream__Destinations__upstream1__Address="http://127.0.0.1:$UP_PORT/" \ "$BIN" --contentRoot "$PUBLISH" >"$LOG" 2>&1 & EDGE_PID=$! for _ in $(seq 1 60); do curl -fsS "http://127.0.0.1:$EDGE_PORT/healthz" >/dev/null 2>&1 && break sleep 0.25 done pass=0; fail=0 check() { if [ "$2" = "$3" ]; then pass=$((pass+1)); printf ' ok %-52s %s\n' "$1" "$3" else fail=$((fail+1)); printf ' FAIL %-52s expected %s, got %s\n' "$1" "$2" "$3"; fi } code() { curl -s -o /dev/null -m 5 -w '%{http_code}' "$@"; } printf '\n=== the schema step ===\n' tables=$(python3 -c "import sqlite3;print(len([r for r in sqlite3.connect('$DB').execute(\"select name from sqlite_master where type='table' and name != 'sqlite_sequence'\")]))" 2>/dev/null || echo 0) check 'tables the shipped DDL created' 12 "$tables" # Two halves, and the split is the finding, not a convenience. Measured 2026-10-07 # on this tree (osx-arm64, the same binary): the AOT edge reaches "Now listening # on:", creates its 12 tables from the DDL it carries and proxies a request to the # upstream for 200 — everything below in the first half passes. Every assertion in # the second half fails, and fails with the SAME sentence 130 times in the log: # "Query wasn't precompiled and dynamic code isn't supported with NativeAOT". They # are recorded rather than checked because a check that fails for a reason this # project already knows, and has a ticket for, stops being a signal; and they are # recorded rather than deleted because the day EF compiles a query again, one of # these answers changes and says so. printf '\n=== the half that needs no query (gated) ===\n' check 'healthz answers' 200 "$(code "http://127.0.0.1:$EDGE_PORT/healthz")" check 'a request reaches the upstream' 200 "$(code "http://127.0.0.1:$EDGE_PORT/body.txt")" HEALTHCHECK_PORT="$EDGE_PORT" "$BIN" --healthcheck >/dev/null 2>&1 check 'the healthcheck mode answers 0' 0 "$?" # The header the edge actually reads. The probe used to send "Authorization: # Bearer", which the admin surface does not look at, so three of these assertions # were measuring a 401 rather than the query wall. Measured 2026-10-07: AdminAuth # reads X-Admin-Token (or ?token=, which it names out loud). printf '\n=== the half behind the query wall (recorded, not gated) ===\n' recorded=0; drifted=0 record() { if [ "$2" = "$3" ]; then drifted=$((drifted+1)); printf ' MOVED %-50s %s — the query wall moved here, promote it back to a check\n' "$1" "$3" else recorded=$((recorded+1)); printf ' %-56s got %s, a working edge answers %s\n' "$1" "$3" "$2" fi } record 'an operator block is accepted' 204 "$(code -X POST -H "X-Admin-Token: $TOKEN" \ -H 'Content-Type: application/json' \ -d '{"ip":"198.51.100.7","reason":"aot probe","redirectTo":"https://honeypot.example.org/"}' \ "http://127.0.0.1:$EDGE_PORT/admin-api/blocks")" record 'the blocked source is redirected' 301 "$(code -H 'X-Admin-Token: $TOKEN' -H 'X-Forwarded-For: 198.51.100.7' \ "http://127.0.0.1:$EDGE_PORT/body.txt")" ban="" for _ in 1 2 3 4; do ban="$ban $(code -H 'X-Forwarded-For: 203.0.113.66' "http://127.0.0.1:$EDGE_PORT/probe")"; done record 'four probes draw the streak ban' " 403 403 403 403" "$ban" record 'the state surface answers' 200 "$(code -H "X-Admin-Token: $TOKEN" \ "http://127.0.0.1:$EDGE_PORT/admin-api/state")" record 'readiness answers' 200 "$(code "http://127.0.0.1:$EDGE_PORT/readyz")" # The body, not just the code. Before ProblemDetails had a contract this endpoint # answered 500 and said nothing; the point of registering it is that the 503 # names the failure an operator would otherwise have to find in the log. printf ' what /readyz says about itself: %s\n' \ "$(curl -s -m 5 "http://127.0.0.1:$EDGE_PORT/readyz" | tr -d '\n\r' | head -c 220)" printf '\npassed %s, failed %s; recorded behind the wall %s, moved %s\n' \ "$pass" "$fail" "$recorded" "$drifted" printf '\n=== the two walls, counted from the edge log ===\n' listening=$(grep -c 'Now listening on:' "$LOG") json_refusals=$(grep -c 'JsonTypeInfo metadata for type' "$LOG") queries=$(grep -c "Query wasn't precompiled and dynamic code isn't supported with NativeAOT" "$LOG") printf ' lines where the edge started listening: %s\n' "$listening" printf ' json metadata refusals: %s\n' "$json_refusals" { [ "$json_refusals" -gt 0 ] && grep -m1 -o "JsonTypeInfo metadata for type '[^']*'" "$LOG" | sed 's/^/ first type without a contract: /'; } printf ' queries refused at run time: %s\n' "$queries" grep -c "Application startup exception" "$LOG" | sed 's/^/ startup exceptions: /' printf ' full log: %s\n' "$LOG" # The API half is a claim now, so it is gated: the host must be listening, and no # response may be refused for want of a compiled contract. Before 2026-10-06 the # first number was 0 (the host threw before Kestrel opened a port); on 2026-10-07 # the second was 55, all of them one anonymous /healthz literal. [ "$listening" -ge 1 ] || { printf 'the edge never listened: the API half regressed\n' >&2; exit 1; } [ "$json_refusals" -eq 0 ] || { printf 'a response shape still has no compiled JSON contract\n' >&2; exit 1; } [ "$fail" -eq 0 ] || exit 1