From b658f46795350ee3da724c05a47b0bfd2f0bf948 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:56:35 +0000 Subject: [PATCH 1/6] Check borrowed header traversal through Idris contract and actual Zig ABI --- scripts/test-header-borrow.sh | 25 +++++++++++++++++++++ scripts/test-header-bridge.sh | 1 + src/Proven/FFI/HeaderBorrow.idr | 40 +++++++++++++++++++++++++++++++++ tests/ffi/header_borrow.zig | 23 +++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 scripts/test-header-borrow.sh create mode 100644 src/Proven/FFI/HeaderBorrow.idr create mode 100644 tests/ffi/header_borrow.zig diff --git a/scripts/test-header-borrow.sh b/scripts/test-header-borrow.sh new file mode 100644 index 00000000..864ec522 --- /dev/null +++ b/scripts/test-header-borrow.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +set -euo pipefail +[[ $# == 1 && -f "$1/libproven.so" ]] +library=$(cd "$1" && pwd) +root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +out=${EVIDENCE_DIR:-"$root/build/header-evidence/ownership"} +mkdir -p "$out"; out=$(cd "$out" && pwd) +echo INCOMPLETE > "$out/result.txt" +work=$(mktemp -d) +trap 'status=$?; if ((status)); then echo FAIL > "$out/result.txt"; tail -n 40 "$out"/*.txt >&2; fi; rm -rf "$work"' EXIT +mkdir -p "$work/Proven/FFI" "$work/Proven/SafeHeader" +cp "$root/src/Proven/FFI/HeaderBorrow.idr" "$work/Proven/FFI/" +cp "$root/src/Proven/SafeHeader/ByteKernel.idr" "$work/Proven/SafeHeader/" +(cd "$work"; idris2 --total -Werror --check Proven/FFI/HeaderBorrow.idr) > "$out/idris.txt" 2>&1 +! grep -q '^Error:' "$out/idris.txt" +(cd "$work"; printf ':m\n:q\n' | idris2 --no-banner Proven/FFI/HeaderBorrow.idr) > "$out/holes.txt" 2>&1 +grep -q 'No holes' "$out/holes.txt" +! grep -q '^Error:' "$out/holes.txt" +for mode in Debug ReleaseSafe ReleaseFast ReleaseSmall; do + "${ZIG:-zig}" test "$root/tests/ffi/header_borrow.zig" -lc -L "$library" -lproven -rpath "$library" -O "$mode" > "$out/native-$mode.txt" 2>&1 +done +sha256sum "$root/src/Proven/FFI/HeaderBorrow.idr" "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/tests/ffi/header_borrow.zig" "$library/libproven.so" > "$out/inputs.sha256" +echo PASS > "$out/result.txt" +echo 'HEADER BORROW PASS: checked Idris fold and actual exported ABI fixtures; readable immutable span remains a premise' diff --git a/scripts/test-header-bridge.sh b/scripts/test-header-bridge.sh index d65562dc..b95dcbbb 100644 --- a/scripts/test-header-bridge.sh +++ b/scripts/test-header-bridge.sh @@ -27,6 +27,7 @@ cd "$root/ffi/zig" "$zig" build --build-file build_standalone.zig -Dshared=true -Dheader-refc="$work/bundle" --prefix "$work/shared" > "$evidence/shared-build.log" 2>&1 cc -Wall -Wextra -Werror -I "$root/bindings/c/include" "$root/tests/ffi/header_byte_bridge.c" "$work/shared/lib/libproven.so" -Wl,-rpath,"$work/shared/lib" -o "$work/c-abi" > "$evidence/c-build.log" 2>&1 "$work/c-abi" > "$evidence/c-abi.log" 2>&1 +EVIDENCE_DIR="$evidence/ownership" ZIG="$zig" bash "$root/scripts/test-header-borrow.sh" "$work/shared/lib" # Every proven.h function and every library export must have a ledger row, and # the qualified library must define exactly the rows that claim presence. diff --git a/src/Proven/FFI/HeaderBorrow.idr b/src/Proven/FFI/HeaderBorrow.idr new file mode 100644 index 00000000..cefc43f4 --- /dev/null +++ b/src/Proven/FFI/HeaderBorrow.idr @@ -0,0 +1,40 @@ +-- SPDX-License-Identifier: MPL-2.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +module Proven.FFI.HeaderBorrow +import Proven.SafeHeader.ByteKernel + +%default total + +||| Contract of the checked state conversion used by the Zig borrowed scan. +||| The actual RefC word decoder has a separate source-bound proof. +public export +acceptState : Bits8 -> Maybe Bool +acceptState value = if value == 0 then Just False else + if value == 1 then Just True else Nothing + +public export +acceptEncoded : (state : Bool) -> acceptState (encodeState state) = Just state +acceptEncoded False = Refl +acceptEncoded True = Refl + +public export +checkedScan : Bool -> List Bits8 -> Maybe Bool +checkedScan state [] = Just state +checkedScan state (b :: bs) = case acceptState (headerStep (encodeState state) b) of + Nothing => Nothing + Just next => checkedScan next bs + +public export +checkedScanCorrect : (state : Bool) -> (bytes : List Bits8) -> + checkedScan state bytes = Just (scanTyped state bytes) +checkedScanCorrect state [] = Refl +checkedScanCorrect state (b :: bs) = + rewrite stepEncodes state b in + rewrite acceptEncoded (nextState state b) in + checkedScanCorrect (nextState state b) bs + +||| Readability, immutable lifetime and non-aliasing with foreign writes are +||| premises of the machine span. This pure list theorem does not prove them. +public export +borrowedPrefix : Nat -> List Bits8 -> Maybe Bool +borrowedPrefix count bytes = checkedScan False (take count bytes) diff --git a/tests/ffi/header_borrow.zig b/tests/ffi/header_borrow.zig new file mode 100644 index 00000000..9cefaa7b --- /dev/null +++ b/tests/ffi/header_borrow.zig @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2026 Jonathan D.A. Jewell +const std = @import("std"); +const BoolResult = extern struct { status: c_int, value: bool }; +extern fn proven_header_has_crlf(?[*]const u8, usize) callconv(.c) BoolResult; + +test "actual ABI borrows exact spans without modifying caller storage" { + var bytes = [_]u8{ 255, 0, 128, 13, 10, 7 }; + const original = bytes; + for (0..bytes.len + 1) |len| { + const result = proven_header_has_crlf(&bytes, len); + try std.testing.expectEqual(@as(c_int, 0), result.status); + try std.testing.expectEqual(len >= 4, result.value); + try std.testing.expectEqualSlices(u8, &original, &bytes); + } + const null_result = proven_header_has_crlf(null, 0); + try std.testing.expectEqual(@as(c_int, -1), null_result.status); + // Borrow is synchronous: the owner may modify and reuse storage afterwards. + bytes[0] = 10; + const changed = proven_header_has_crlf(&bytes, 1); + try std.testing.expectEqual(@as(c_int, 0), changed.status); + try std.testing.expect(changed.value); +} From 1383aebd7c284e0187f56513e1dc5ebdca92e5b7 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 07:59:12 +0000 Subject: [PATCH 2/6] Select the finite-list prefix operation in the borrow contract --- src/Proven/FFI/HeaderBorrow.idr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Proven/FFI/HeaderBorrow.idr b/src/Proven/FFI/HeaderBorrow.idr index cefc43f4..70bc5f80 100644 --- a/src/Proven/FFI/HeaderBorrow.idr +++ b/src/Proven/FFI/HeaderBorrow.idr @@ -2,6 +2,7 @@ -- Copyright (c) 2026 Jonathan D.A. Jewell module Proven.FFI.HeaderBorrow import Proven.SafeHeader.ByteKernel +import Data.List %default total @@ -37,4 +38,4 @@ checkedScanCorrect state (b :: bs) = ||| premises of the machine span. This pure list theorem does not prove them. public export borrowedPrefix : Nat -> List Bits8 -> Maybe Bool -borrowedPrefix count bytes = checkedScan False (take count bytes) +borrowedPrefix count bytes = checkedScan False (Data.List.take count bytes) From bef2a347f9f963ea4a22f107d64ff44d3eebf999 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:08:21 +0000 Subject: [PATCH 3/6] Document exact borrowed span and RefC call obligations --- docs/adr/2026-11-25-header-borrow.adoc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 docs/adr/2026-11-25-header-borrow.adoc diff --git a/docs/adr/2026-11-25-header-borrow.adoc b/docs/adr/2026-11-25-header-borrow.adoc new file mode 100644 index 00000000..2a37b221 --- /dev/null +++ b/docs/adr/2026-11-25-header-borrow.adoc @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: CC-BY-SA-4.0 += Borrowed header loop and foreign-call contract + +`Proven.FFI.HeaderBorrow` proves that checked canonical Boolean-state conversion +and scanning agree with the authoritative ByteKernel fold. It is checked with +totality, warnings as errors and a no-holes audit in the actual header ABI gate. + +The native fixture is a Zig caller of the real exported RefC-backed library. +It checks exact prefix lengths, unchanged input bytes, null refusal, and owner +reuse after return. The caller is compiled in all four Zig optimisation modes; +this is not a claim of four independently qualified library builds. + +The caller owns a live readable immutable span for the synchronous call. The +RefC transition and trampoline must return normally without modifying that span. +Tagged Bits8 carriers are immediate words, not allocated pointees; their +canonical-word proof does not establish machine-pointer provenance. + +Atoma's companion source grammar covers the actual `header_refc.detect` loop, +checked state guard and final Boolean comparison. Its fold theorem explicitly +assumes the foreign-step contract; source/compiler refinement, the public +pointer-to-slice entry and arbitrary pointer validity remain open. The existing +kernel-mutation and staging controls remain required. ABI contracts stay in +Idris2; the foreign-facing implementation stays in Zig. From eaec1a684d9141d54482aad12e8ea35fea591fc7 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:16:19 +0000 Subject: [PATCH 4/6] Refuse zero-exit compiler diagnostics and bind borrow checks to configured tools --- scripts/test-header-borrow-controls.sh | 25 +++++++++++++++++++++++++ scripts/test-header-borrow.sh | 16 +++++++++++----- scripts/test-header-bridge.sh | 1 + 3 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 scripts/test-header-borrow-controls.sh diff --git a/scripts/test-header-borrow-controls.sh b/scripts/test-header-borrow-controls.sh new file mode 100644 index 00000000..94c80333 --- /dev/null +++ b/scripts/test-header-borrow-controls.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +set -euo pipefail +[[ $# == 1 ]] +root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) +out=${EVIDENCE_DIR:-"$root/build/header-evidence/ownership-controls"} +mkdir -p "$out"; out=$(cd "$out" && pwd) +work=$(mktemp -d) +trap 'rm -rf "$work"' EXIT +real_idris=$(command -v "${IDRIS2:-idris2}") +real_idris=$(cd "$(dirname "$real_idris")" && pwd)/$(basename "$real_idris") +cat > "$work/diagnostic-compiler" <<'SH' +#!/usr/bin/env bash +if [[ $1 == --version ]]; then exec "$REAL_IDRIS" "$@"; fi +printf 'Error: planted zero-exit compiler failure\n' +exit 0 +SH +chmod +x "$work/diagnostic-compiler" +if REAL_IDRIS="$real_idris" IDRIS2="$work/diagnostic-compiler" EVIDENCE_DIR="$out/refused" \ + bash "$root/scripts/test-header-borrow.sh" "$1" > "$out/reject-diagnostic.txt" 2>&1; then + echo 'FAIL: zero-exit compiler diagnostic accepted' >&2; exit 1 +fi +grep -q 'FAIL: borrow compiler diagnostics' "$out/reject-diagnostic.txt" +[[ $(cat "$out/refused/result.txt") == FAIL ]] +echo PASS > "$out/result.txt" diff --git a/scripts/test-header-borrow.sh b/scripts/test-header-borrow.sh index 864ec522..3af8e132 100644 --- a/scripts/test-header-borrow.sh +++ b/scripts/test-header-borrow.sh @@ -7,18 +7,24 @@ root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) out=${EVIDENCE_DIR:-"$root/build/header-evidence/ownership"} mkdir -p "$out"; out=$(cd "$out" && pwd) echo INCOMPLETE > "$out/result.txt" +idris=$(command -v "${IDRIS2:-idris2}") +idris=$(cd "$(dirname "$idris")" && pwd)/$(basename "$idris") +zig=$(command -v "${ZIG:-zig}") +zig=$(cd "$(dirname "$zig")" && pwd)/$(basename "$zig") +[[ $("$idris" --version) == 'Idris 2, version 0.8.0-b6cbc25d5' ]] +[[ $("$zig" version) == 0.15.2 ]] work=$(mktemp -d) trap 'status=$?; if ((status)); then echo FAIL > "$out/result.txt"; tail -n 40 "$out"/*.txt >&2; fi; rm -rf "$work"' EXIT mkdir -p "$work/Proven/FFI" "$work/Proven/SafeHeader" cp "$root/src/Proven/FFI/HeaderBorrow.idr" "$work/Proven/FFI/" cp "$root/src/Proven/SafeHeader/ByteKernel.idr" "$work/Proven/SafeHeader/" -(cd "$work"; idris2 --total -Werror --check Proven/FFI/HeaderBorrow.idr) > "$out/idris.txt" 2>&1 -! grep -q '^Error:' "$out/idris.txt" -(cd "$work"; printf ':m\n:q\n' | idris2 --no-banner Proven/FFI/HeaderBorrow.idr) > "$out/holes.txt" 2>&1 +(cd "$work"; "$idris" --total -Werror --check Proven/FFI/HeaderBorrow.idr) > "$out/idris.txt" 2>&1 +if grep -q '^Error:' "$out/idris.txt"; then echo 'FAIL: borrow compiler diagnostics' >&2; exit 1; fi +(cd "$work"; printf ':m\n:q\n' | "$idris" --no-banner Proven/FFI/HeaderBorrow.idr) > "$out/holes.txt" 2>&1 grep -q 'No holes' "$out/holes.txt" -! grep -q '^Error:' "$out/holes.txt" +if grep -q '^Error:' "$out/holes.txt"; then echo 'FAIL: borrow hole-check diagnostics' >&2; exit 1; fi for mode in Debug ReleaseSafe ReleaseFast ReleaseSmall; do - "${ZIG:-zig}" test "$root/tests/ffi/header_borrow.zig" -lc -L "$library" -lproven -rpath "$library" -O "$mode" > "$out/native-$mode.txt" 2>&1 + "$zig" test "$root/tests/ffi/header_borrow.zig" -lc -L "$library" -lproven -rpath "$library" -O "$mode" > "$out/native-$mode.txt" 2>&1 done sha256sum "$root/src/Proven/FFI/HeaderBorrow.idr" "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/tests/ffi/header_borrow.zig" "$library/libproven.so" > "$out/inputs.sha256" echo PASS > "$out/result.txt" diff --git a/scripts/test-header-bridge.sh b/scripts/test-header-bridge.sh index b95dcbbb..cb4ab28b 100644 --- a/scripts/test-header-bridge.sh +++ b/scripts/test-header-bridge.sh @@ -28,6 +28,7 @@ cd "$root/ffi/zig" cc -Wall -Wextra -Werror -I "$root/bindings/c/include" "$root/tests/ffi/header_byte_bridge.c" "$work/shared/lib/libproven.so" -Wl,-rpath,"$work/shared/lib" -o "$work/c-abi" > "$evidence/c-build.log" 2>&1 "$work/c-abi" > "$evidence/c-abi.log" 2>&1 EVIDENCE_DIR="$evidence/ownership" ZIG="$zig" bash "$root/scripts/test-header-borrow.sh" "$work/shared/lib" +EVIDENCE_DIR="$evidence/ownership-controls" ZIG="$zig" bash "$root/scripts/test-header-borrow-controls.sh" "$work/shared/lib" # Every proven.h function and every library export must have a ledger row, and # the qualified library must define exactly the rows that claim presence. From 95a7df0c2d094c8d3636d9287afc30014c1aa261 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:31:38 +0000 Subject: [PATCH 5/6] Invalidate old borrow-control evidence before every run --- scripts/test-header-borrow-controls.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/test-header-borrow-controls.sh b/scripts/test-header-borrow-controls.sh index 94c80333..924c94c2 100644 --- a/scripts/test-header-borrow-controls.sh +++ b/scripts/test-header-borrow-controls.sh @@ -5,8 +5,9 @@ set -euo pipefail root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd) out=${EVIDENCE_DIR:-"$root/build/header-evidence/ownership-controls"} mkdir -p "$out"; out=$(cd "$out" && pwd) +echo INCOMPLETE > "$out/result.txt" work=$(mktemp -d) -trap 'rm -rf "$work"' EXIT +trap 'status=$?; if ((status)); then echo FAIL > "$out/result.txt"; fi; rm -rf "$work"' EXIT real_idris=$(command -v "${IDRIS2:-idris2}") real_idris=$(cd "$(dirname "$real_idris")" && pwd)/$(basename "$real_idris") cat > "$work/diagnostic-compiler" <<'SH' From ca8e1e07ad0777b723f15d2c59fdcd759d505c36 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 25 Nov 2026 08:44:03 +0000 Subject: [PATCH 6/6] Bind borrowed ABI evidence to immutable pre-run inputs and private snapshots --- scripts/test-header-borrow.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/scripts/test-header-borrow.sh b/scripts/test-header-borrow.sh index 3af8e132..e363e6c7 100644 --- a/scripts/test-header-borrow.sh +++ b/scripts/test-header-borrow.sh @@ -15,17 +15,29 @@ zig=$(cd "$(dirname "$zig")" && pwd)/$(basename "$zig") [[ $("$zig" version) == 0.15.2 ]] work=$(mktemp -d) trap 'status=$?; if ((status)); then echo FAIL > "$out/result.txt"; tail -n 40 "$out"/*.txt >&2; fi; rm -rf "$work"' EXIT +sha256sum "$root/src/Proven/FFI/HeaderBorrow.idr" "$root/src/Proven/SafeHeader/ByteKernel.idr" \ + "$root/tests/ffi/header_borrow.zig" "$root/scripts/test-header-borrow.sh" "$library/libproven.so" > "$out/inputs.sha256" mkdir -p "$work/Proven/FFI" "$work/Proven/SafeHeader" cp "$root/src/Proven/FFI/HeaderBorrow.idr" "$work/Proven/FFI/" cp "$root/src/Proven/SafeHeader/ByteKernel.idr" "$work/Proven/SafeHeader/" +cp "$root/tests/ffi/header_borrow.zig" "$work/header_borrow.zig" +cp "$library/libproven.so" "$work/libproven.so" +cmp "$root/src/Proven/FFI/HeaderBorrow.idr" "$work/Proven/FFI/HeaderBorrow.idr" +cmp "$root/src/Proven/SafeHeader/ByteKernel.idr" "$work/Proven/SafeHeader/ByteKernel.idr" +cmp "$root/tests/ffi/header_borrow.zig" "$work/header_borrow.zig" +cmp "$library/libproven.so" "$work/libproven.so" +sha256sum -c "$out/inputs.sha256" > "$out/snapshot-recheck.txt" +(cd "$work"; sha256sum Proven/FFI/HeaderBorrow.idr Proven/SafeHeader/ByteKernel.idr header_borrow.zig libproven.so) > "$out/checked-inputs.sha256" (cd "$work"; "$idris" --total -Werror --check Proven/FFI/HeaderBorrow.idr) > "$out/idris.txt" 2>&1 if grep -q '^Error:' "$out/idris.txt"; then echo 'FAIL: borrow compiler diagnostics' >&2; exit 1; fi (cd "$work"; printf ':m\n:q\n' | "$idris" --no-banner Proven/FFI/HeaderBorrow.idr) > "$out/holes.txt" 2>&1 grep -q 'No holes' "$out/holes.txt" if grep -q '^Error:' "$out/holes.txt"; then echo 'FAIL: borrow hole-check diagnostics' >&2; exit 1; fi for mode in Debug ReleaseSafe ReleaseFast ReleaseSmall; do - "$zig" test "$root/tests/ffi/header_borrow.zig" -lc -L "$library" -lproven -rpath "$library" -O "$mode" > "$out/native-$mode.txt" 2>&1 + "$zig" test "$work/header_borrow.zig" -lc -L "$work" -lproven -rpath "$work" -O "$mode" > "$out/native-$mode.txt" 2>&1 done -sha256sum "$root/src/Proven/FFI/HeaderBorrow.idr" "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/tests/ffi/header_borrow.zig" "$library/libproven.so" > "$out/inputs.sha256" +(cd "$work"; sha256sum -c "$out/checked-inputs.sha256") > "$out/checked-input-recheck.txt" +sha256sum -c "$out/inputs.sha256" > "$out/input-recheck.txt" +cp "$work/header_borrow.zig" "$work/Proven/FFI/HeaderBorrow.idr" "$work/Proven/SafeHeader/ByteKernel.idr" "$out/" echo PASS > "$out/result.txt" echo 'HEADER BORROW PASS: checked Idris fold and actual exported ABI fixtures; readable immutable span remains a premise'