Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/adr/2026-11-25-header-borrow.adoc
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.
26 changes: 26 additions & 0 deletions scripts/test-header-borrow-controls.sh
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)
Comment thread
hyperpolymath marked this conversation as resolved.
Resolved
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"
43 changes: 43 additions & 0 deletions scripts/test-header-borrow.sh
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'
2 changes: 2 additions & 0 deletions scripts/test-header-bridge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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"
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.
Expand Down
41 changes: 41 additions & 0 deletions src/Proven/FFI/HeaderBorrow.idr
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)
23 changes: 23 additions & 0 deletions tests/ffi/header_borrow.zig
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);
}
Loading