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
6 changes: 4 additions & 2 deletions .github/workflows/main-estate-audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@ jobs:
test -f LICENSE.txt || test -f LICENSE || test -f LICENSE.md || test -f LICENSE.adoc

echo "=== cicd-suite: idris2-abi & zig-unified-api-adapter ==="
# git grep exits 1 when no tracked .zig file exists, so no guard is needed.
if git grep -E 'std\.fs|std\.io' -- '*.zig'; then
# Enforce the effect boundary on runtime FFI modules. Standalone
# verification runners outside this tree must emit oracle witnesses.
if git grep -E 'std\.fs|std\.io' -- 'src/interface/ffi/src/*.zig'; then
echo "::error::Zig FFI modules contain prohibited side-effects (std.fs/std.io)"
exit 1
fi
echo "Central Estate CI/CD Audit passed."

51 changes: 51 additions & 0 deletions scripts/check-registry-state.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: MPL-2.0
set -euo pipefail
root=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
out=${EVIDENCE_DIR:-"$root/build/runtime-registry-evidence/state"}
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 35 "$out"/*.txt >&2; fi; rm -rf "$work"' EXIT
[[ $(zig version) == 0.15.2 ]]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor the caller-selected compiler paths

When check-runtime-registry.sh is run with ZIG=/path/to/zig (or IDRIS2=/path/to/idris2), its existing checks use those configured binaries, but this newly invoked script switches back to bare zig and idris2 commands throughout. Consequently, environments where the selected compiler is not on PATH, or where PATH contains a different Zig version, now fail after the original runtime checks succeed or generate evidence with a different toolchain. Resolve the compiler paths once from ZIG/IDRIS2 and use them for every command in this script.

Useful? React with 👍 / 👎.

cp -R "$root/src/interface/Abi" "$work/"
cp "$root/src/interface/RegistryStateOracle.idr" "$work/Main.idr"
cp "$root/src/interface/RegistryStateRunner.zig" "$work/runner.zig"
cp "$root/src/interface/ffi/src/registry_state.zig" "$work/"
(cd "$work"; sha256sum Main.idr Abi/*.idr *.zig) > "$out/inputs.sha256"

Check warning on line 16 in scripts/check-registry-state.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Prefix files and paths with "./" or "--" when using glob.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_unified-api-adapter&issues=AaIQWCdFJ12bmP9Ngmh-&open=AaIQWCdFJ12bmP9Ngmh-&pullRequest=23
(cd "$work"; idris2 --total -Werror --codegen refc -o oracle Main.idr) > "$out/idris-build.txt" 2>&1
if grep -q '^Error:' "$out/idris-build.txt"; then exit 1; fi
(cd "$work"; printf ':m\n:q\n' | idris2 --no-banner Main.idr) > "$out/idris-holes.txt" 2>&1
grep -q 'No holes' "$out/idris-holes.txt"
if grep -q '^Error:' "$out/idris-holes.txt"; then exit 1; fi
"$work/build/exec/oracle" > "$out/contract.tsv"
[[ $(wc -l < "$out/contract.tsv") == 917 ]]
for mode in Debug ReleaseSafe ReleaseFast ReleaseSmall; do
zig build-exe "$work/runner.zig" -O "$mode" -femit-bin="$work/native" > "$out/build-$mode.txt" 2>&1
"$work/native" > "$out/native-$mode.tsv"
cmp "$out/contract.tsv" "$out/native-$mode.tsv"
done
cp "$work/registry_state.zig" "$work/original"
for mutation in frame busy restoration identity status; do
case "$mutation" in

Check failure on line 31 in scripts/check-registry-state.sh

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Add a default case (*) to handle unexpected values.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_unified-api-adapter&issues=AaIQWCdFJ12bmP9Ngmh_&open=AaIQWCdFJ12bmP9Ngmh_&pullRequest=23
frame) sed 's/.rest = s.rest, .mcp = endpoint/.rest = endpoint, .mcp = endpoint/' "$work/original" ;;
busy) sed '/if (s.active)/d' "$work/original" ;;
restoration) sed 's/return mark(E, s, false)/return mark(E, s, true)/' "$work/original" ;;
identity) sed 's/.selected = endpoint/.selected = null/;s/const endpoint = get/_ = get/' "$work/original" ;;
status) sed 's/status <= 11/status <= 12/' "$work/original" ;;
esac > "$work/registry_state.zig"
! cmp -s "$work/original" "$work/registry_state.zig"
zig build-exe "$work/runner.zig" -O ReleaseSafe -femit-bin="$work/mutant" > "$out/mutant-$mutation-build.txt" 2>&1
"$work/mutant" > "$out/mutant-$mutation.tsv"
if cmp -s "$out/contract.tsv" "$out/mutant-$mutation.tsv"; then echo "FAIL $mutation escaped"; exit 1; fi
done
cp "$work/original" "$work/registry_state.zig"
(cd "$work"; sha256sum -c "$out/inputs.sha256") > "$out/recheck.txt"
cp "$work/"*.zig "$out/"
cp "$work/Abi/RegistryState.idr" "$out/"
idris2 --version > "$out/idris-version.txt"
zig version > "$out/zig-version.txt"
echo PASS > "$out/result.txt"
echo 'REGISTRY PASS: 900 two-operation traces, 17 signed statuses, four modes, five compiled mutants'

2 changes: 2 additions & 0 deletions scripts/check-runtime-registry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ cp -R "$work/idris-evidence" "$evidence/"
mkdir -p "$work/src" "$work/include" "$work/test"
cp "$root/src/interface/ffi/build.zig" "$work/"
cp "$root/src/interface/ffi/src/registry.zig" "$work/src/"
cp "$root/src/interface/ffi/src/registry_state.zig" "$work/src/"
cp "$root/src/interface/ffi/src/protocol_tag.zig" "$work/src/"
cp "$root/src/interface/ffi/src/main.zig" "$work/src/"
cp "$root/src/interface/ffi/include/uaa_registry.h" "$work/include/"
Expand All @@ -53,4 +54,5 @@ if "$work/caller" > "$evidence/mutant.csv" 2> "$evidence/mutant.log"; then
fi
fi
echo PASS > "$evidence/result.txt"
EVIDENCE_DIR="$evidence/state" bash "$root/scripts/check-registry-state.sh"
echo 'PASS: real C ABI, Idris differential matrix, lifetime/refusal regressions and rejecting Zig mutant'
54 changes: 54 additions & 0 deletions src/interface/RegistryStateOracle.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- SPDX-License-Identifier: MPL-2.0
module Main
import Abi.Registry
import Abi.RegistryState
import Abi.Status
%default total

record Observation where
constructor Observe
code : Bits8
state : State Nat
chosen : Maybe Nat

fromReply : Reply Nat -> Observation
fromReply r = Observe (statusTag (status r)) (next r) Nothing

step : Nat -> Slot -> State Nat -> Observation
step 0 k s = fromReply (register k 11 s)
step 1 k s = fromReply (register k 22 s)
step 2 k s = fromReply (unregister k s)
step 3 k s = case beginCall k s of
Left r => fromReply r
Right call => Observe 0 (duringCall call) (Just (selected call))
step _ k s = Observe 0 (normalReturn (MkInvocation (entries s) 0)) Nothing

endpoint : Nat -> Maybe Nat
endpoint 0 = Nothing
endpoint 1 = Just 11
endpoint _ = Just 22

number : Maybe Nat -> String
number Nothing = "0"
number (Just n) = show n

render : Observation -> String
render (Observe code s chosen) =
show code ++ " " ++ number (restEndpoint (entries s)) ++ " " ++
number (mcpEndpoint (entries s)) ++ " " ++
(if busy s then "1" else "0") ++ " " ++ number chosen

main : IO ()
main = do
for_ [0,1,2] $ \r =>
for_ [0,1,2] $ \m =>
for_ [False,True] $ \b =>
for_ [RESTSlot,MCPSlot] $ \k =>
for_ [0,1,2,3,4] $ \a =>
for_ [0,1,2,3,4] $ \z => do
let first = step a k (MkState (MkRegistry (endpoint r) (endpoint m)) b)
let second = step z k (state first)
putStrLn (render first ++ " " ++ render second)
for_ [-2147483648,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,256,2147483647] $ \z =>
putStrLn ("status " ++ show z ++ " " ++ show (statusTag (sanitize z)))

41 changes: 41 additions & 0 deletions src/interface/RegistryStateRunner.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MPL-2.0
const std = @import("std");
const source = @import("registry_state.zig");
const E = u32;
fn step(op: u8, slot: source.Slot, s: source.State(E)) source.Reply(E) {
return switch (op) {
0 => source.register(E, s, slot, 11),
1 => source.register(E, s, slot, 22),
2 => source.unregister(E, s, slot),
3 => source.begin(E, s, slot),
else => .{ .status = 0, .next = source.finish(E, s) },
};
}
pub fn main() !void {
var buf: [256]u8 = undefined;
const endpoints = [_]?E{ null, 11, 22 };
for (endpoints) |r| {
for (endpoints) |m| {
for ([_]bool{ false, true }) |busy| {
for ([_]source.Slot{ .rest, .mcp }) |slot| {
for (0..5) |a| {
for (0..5) |z| {
const first = step(@intCast(a), slot, .{ .rest = r, .mcp = m, .active = busy });
const second = step(@intCast(z), slot, first.next);
const line = try std.fmt.bufPrint(&buf, "{d} {d} {d} {d} {d} {d} {d} {d} {d} {d}\n", .{
first.status, first.next.rest orelse 0, first.next.mcp orelse 0, @intFromBool(first.next.active), first.selected orelse 0,
second.status, second.next.rest orelse 0, second.next.mcp orelse 0, @intFromBool(second.next.active), second.selected orelse 0,
});
try std.fs.File.stdout().writeAll(line);
}
}
}
}
}
}
for ([_]c_int{ -2147483648, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 256, 2147483647 }) |z| {
const line = try std.fmt.bufPrint(&buf, "status {d} {d}\n", .{ z, source.sanitize(z) });
try std.fs.File.stdout().writeAll(line);
}
}

50 changes: 25 additions & 25 deletions src/interface/ffi/src/registry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@
// not a source/compiler refinement proof or a built-in network transport.
const std = @import("std");
const known = @import("protocol_tag.zig").known;
const state = @import("registry_state.zig");

pub const Callback = *const fn (?*anyopaque, ?[*]const u8, usize) callconv(.c) c_int;
const Endpoint = struct { callback: Callback, context: ?*anyopaque };
pub const Registry = struct {
rest: ?Endpoint = null,
mcp: ?Endpoint = null,
active: bool = false,
};
pub const Registry = state.State(Endpoint);

// All calls on one live registry must be serialized by its owner. Callback
// code/context and input remain borrowed for the synchronous call only.
Expand All @@ -32,44 +29,47 @@ pub export fn uaa_registry_register(r: ?*Registry, tag: u8, callback: ?Callback,
const registry = r orelse return 4;
if (registry.active) return 6;
if (!known(tag)) return 2;
const slot = switch (tag) {
1 => &registry.mcp,
9 => &registry.rest,
const slot: state.Slot = switch (tag) {
1 => .mcp,
9 => .rest,
else => return 11,
};
const cb = callback orelse return 4;
if (slot.* != null) return 9;
slot.* = .{ .callback = cb, .context = context };
return 0;
const reply = state.register(Endpoint, registry.*, slot, .{ .callback = cb, .context = context });
registry.* = reply.next;
return reply.status;
}

pub export fn uaa_registry_unregister(r: ?*Registry, tag: u8) c_int {
const registry = r orelse return 4;
if (registry.active) return 6;
if (!known(tag)) return 2;
const slot = switch (tag) {
1 => &registry.mcp,
9 => &registry.rest,
const slot: state.Slot = switch (tag) {
1 => .mcp,
9 => .rest,
else => return 11,
};
if (slot.* == null) return 8;
slot.* = null;
return 0;
const reply = state.unregister(Endpoint, registry.*, slot);
registry.* = reply.next;
return reply.status;
}

pub export fn uaa_registry_call(r: ?*Registry, tag: u8, bytes: ?[*]const u8, len: usize) c_int {
const registry = r orelse return 4;
if (registry.active) return 6;
if (!known(tag)) return 2;
const endpoint = switch (tag) {
1 => registry.mcp,
9 => registry.rest,
else => null,
} orelse return 11;
const slot: state.Slot = switch (tag) {
1 => .mcp,
9 => .rest,
else => return 11,
};
const reply = state.begin(Endpoint, registry.*, slot);
if (reply.status != 0) return reply.status;
const endpoint = reply.selected orelse return 11;
registry.* = reply.next;
defer registry.* = state.finish(Endpoint, registry.*);
if (bytes == null and len != 0) return 4;
registry.active = true;
defer registry.active = false;
const status = endpoint.callback(endpoint.context, bytes, len);
// A foreign callback cannot inject an unknown ABI status.
return if (status >= 0 and status <= 11) status else 6;
return state.sanitize(status);
}
45 changes: 45 additions & 0 deletions src/interface/ffi/src/registry_state.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk>
// Pure state transitions used by the actual registry FFI. Endpoint values
// retain callback/context identity; ownership of their pointees is external.
pub const Slot = enum { mcp, rest };

pub fn State(comptime E: type) type {
return struct { rest: ?E = null, mcp: ?E = null, active: bool = false };
}
pub fn Reply(comptime E: type) type {
return struct { status: u8, next: State(E), selected: ?E = null };
}
pub fn get(comptime E: type, s: State(E), slot: Slot) ?E {
return switch (slot) { .mcp => s.mcp, .rest => s.rest };
}
pub fn put(comptime E: type, s: State(E), slot: Slot, endpoint: ?E) State(E) {
return switch (slot) {
.mcp => .{ .rest = s.rest, .mcp = endpoint, .active = s.active },
.rest => .{ .rest = endpoint, .mcp = s.mcp, .active = s.active },
};
}
pub fn mark(comptime E: type, s: State(E), active: bool) State(E) {
return .{ .rest = s.rest, .mcp = s.mcp, .active = active };
}
pub fn register(comptime E: type, s: State(E), slot: Slot, endpoint: E) Reply(E) {
if (s.active) return .{ .status = 6, .next = s };
if (get(E, s, slot) != null) return .{ .status = 9, .next = s };
return .{ .status = 0, .next = put(E, s, slot, endpoint) };
}
pub fn unregister(comptime E: type, s: State(E), slot: Slot) Reply(E) {
if (s.active) return .{ .status = 6, .next = s };
if (get(E, s, slot) == null) return .{ .status = 8, .next = s };
return .{ .status = 0, .next = put(E, s, slot, null) };
}
pub fn begin(comptime E: type, s: State(E), slot: Slot) Reply(E) {
if (s.active) return .{ .status = 6, .next = s };
const endpoint = get(E, s, slot) orelse return .{ .status = 11, .next = s };
return .{ .status = 0, .next = mark(E, s, true), .selected = endpoint };
}
pub fn finish(comptime E: type, s: State(E)) State(E) {
return mark(E, s, false);
}
pub fn sanitize(status: c_int) c_int {
return if (status >= 0 and status <= 11) status else 6;
}
Loading