Repository navigation
Qualify the borrowed header scan at the actual Zig ABI #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b658f46
Check borrowed header traversal through Idris contract and actual Zig…
hyperpolymath 1383aeb
Select the finite-list prefix operation in the borrow contract
hyperpolymath bef2a34
Document exact borrowed span and RefC call obligations
hyperpolymath eaec1a6
Refuse zero-exit compiler diagnostics and bind borrow checks to confi…
hyperpolymath 95a7df0
Invalidate old borrow-control evidence before every run
hyperpolymath ca8e1e0
Bind borrowed ABI evidence to immutable pre-run inputs and private sn…
hyperpolymath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/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) | ||
| echo INCOMPLETE > "$out/result.txt" | ||
| work=$(mktemp -d) | ||
| 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' | ||
| #!/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" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/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" | ||
| 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 | ||
| 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 "$work/header_borrow.zig" -lc -L "$work" -lproven -rpath "$work" -O "$mode" > "$out/native-$mode.txt" 2>&1 | ||
| done | ||
| (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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| -- SPDX-License-Identifier: MPL-2.0 | ||
| -- Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> | ||
| module Proven.FFI.HeaderBorrow | ||
| import Proven.SafeHeader.ByteKernel | ||
| import Data.List | ||
|
|
||
| %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 (Data.List.take count bytes) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> | ||
| 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); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.