diff --git a/.dockerignore b/.dockerignore index b4a9f65..9ef536f 100644 --- a/.dockerignore +++ b/.dockerignore @@ -4,6 +4,10 @@ research docs scripts +# The one script the build needs inside the image: it is the gate that asks the +# AOT binary whether the link is static (Dockerfile.aot-probe, and the +# aot-probe.yml job runs it with `docker run`). +!scripts/aot-link-report.sh dotnet/**/bin dotnet/**/obj *.db diff --git a/.github/workflows/aot-probe.yml b/.github/workflows/aot-probe.yml index a2d67cd..60cea03 100644 --- a/.github/workflows/aot-probe.yml +++ b/.github/workflows/aot-probe.yml @@ -55,19 +55,20 @@ jobs: fi [ -S /var/run/docker.sock ] || { echo "no /var/run/docker.sock on this runner"; exit 1; } - - name: Build the AOT image and keep the link report + # Two builds, one cache. The image build produces the artifact; the + # `--target build` build produces the stage that HOLDS the binary, and a + # `docker run` of the report script against it always executes — which is + # what the gate needs, since a cached build layer prints nothing (run 2503 + # failed on exactly that). + - name: Build the AOT image and ask the binary whether it is static run: | set -eu - docker build -f Dockerfile.aot-probe -t "$IMAGE" --progress=plain . 2>&1 | tee /tmp/aot-build.log + docker build -f Dockerfile.aot-probe -t "$IMAGE" --progress=plain . 2>&1 \ + | tail -20 printf 'image size: %s\n' "$(docker image ls --format '{{.Size}}' "$IMAGE")" - # The gate: the two lines the build stage prints about the binary itself. - grep -E 'program headers naming an interpreter: 0$' /tmp/aot-build.log >/dev/null \ - || { echo 'the AOT binary names an interpreter — the link is not static'; exit 1; } - grep -E 'NEEDED shared objects: 0$' /tmp/aot-build.log >/dev/null \ - || { echo 'the AOT binary needs a shared object — the link is not static'; exit 1; } - grep -E 'static-pie linked' /tmp/aot-build.log >/dev/null \ - || { echo 'file(1) does not call this a static-pie executable'; exit 1; } - grep -E 'binary bytes: [0-9]+' /tmp/aot-build.log || true + docker build -f Dockerfile.aot-probe --target build -t openwaf-aot-build:ci . 2>&1 \ + | tail -5 + docker run --rm --entrypoint sh openwaf-aot-build:ci /report.sh # Reported, not gated: what the binary does once it is running. It is # expected to die, and the log line it dies with is the finding. diff --git a/Dockerfile.aot-probe b/Dockerfile.aot-probe index ac10973..09b3f1b 100644 --- a/Dockerfile.aot-probe +++ b/Dockerfile.aot-probe @@ -65,16 +65,12 @@ RUN . /rid && dotnet publish OpenWaf.Edge/OpenWaf.Edge.csproj \ -o /publish/api \ && mkdir -p /publish/data -# The static-link claim, checked at build time rather than asserted in a comment: -# a fully static executable has no PT_INTERP and `file` calls it static. -RUN apk add --no-cache file binutils \ - && BIN=/publish/api/OpenWaf.Edge \ - && printf 'binary bytes: %s\n' "$(wc -c < "$BIN" | tr -d ' ')" \ - && printf 'file says: %s\n' "$(file -b "$BIN")" \ - && printf 'program headers naming an interpreter: %s\n' \ - "$(readelf -l "$BIN" 2>/dev/null | grep -c 'program interpreter' )" \ - && printf 'NEEDED shared objects: %s\n' \ - "$(readelf -d "$BIN" 2>/dev/null | grep -c '(NEEDED)')" +# The static-link claim, checked against the binary rather than asserted in a +# comment. The same script is what CI runs — see scripts/aot-link-report.sh for +# why the gate does not grep this build's log (a cached layer prints nothing, and +# run 2503 turned a good artifact red that way). +COPY scripts/aot-link-report.sh /report.sh +RUN sh /report.sh /publish/api/OpenWaf.Edge FROM curlimages/curl:8.10.1 AS geoip ARG DBIP_VERSION diff --git a/scripts/aot-link-report.sh b/scripts/aot-link-report.sh new file mode 100755 index 0000000..9d6cedf --- /dev/null +++ b/scripts/aot-link-report.sh @@ -0,0 +1,53 @@ +#!/bin/sh +# The static-link report for the NativeAOT probe image, run INSIDE the build +# stage against the binary it just produced. +# +# Why a script and not a RUN line in the Dockerfile: a cached layer prints +# nothing. Measured on Gitea Actions run 2503 — the publish layer was cached, the +# report layer was cached with it, the build log carried no report lines, and the +# CI gate that greps the log failed on a perfectly good artifact. A `docker run` +# of this script always executes, so the gate asks the binary rather than asking +# the cache. +# +# Exits non-zero when the link is NOT fully static, because that is the one claim +# Dockerfile.aot-probe makes and the claim a scratch layer depends on: an +# interpreter or a NEEDED shared object means the image would be silently +# depending on a libc it does not contain. +set -eu + +BIN=${1:-/publish/api/OpenWaf.Edge} + +if [ ! -f "$BIN" ]; then + printf 'no binary at %s: cannot report\n' "$BIN" >&2 + exit 2 +fi + +# file(1) and readelf(1) are not in the SDK image; the AOT toolchain stage has +# binutils already, but this runs in any stage that holds the binary. +if ! command -v readelf >/dev/null 2>&1; then + apk add --no-cache binutils file >/dev/null 2>&1 || { + printf 'neither readelf nor a way to install it: cannot report (exit 2), not a pass\n' >&2 + exit 2 + } +fi + +printf 'binary: %s\n' "$BIN" +printf 'binary bytes: %s\n' "$(wc -c < "$BIN" | tr -d ' ')" +if command -v file >/dev/null 2>&1; then + printf 'file says: %s\n' "$(file -b "$BIN")" +fi + +interpreters=$(readelf -l "$BIN" 2>/dev/null | grep -c 'program interpreter' || true) +needed=$(readelf -d "$BIN" 2>/dev/null | grep -c '(NEEDED)' || true) +printf 'program headers naming an interpreter: %s\n' "$interpreters" +printf 'NEEDED shared objects: %s\n' "$needed" + +if [ "$interpreters" != "0" ]; then + printf 'NOT static: the binary names a dynamic loader\n' >&2 + exit 1 +fi +if [ "$needed" != "0" ]; then + printf 'NOT static: the binary needs a shared object at run time\n' >&2 + exit 1 +fi +printf 'static: no interpreter, no NEEDED shared object\n'