From e2843fda8883dea51025cbffa160870b2155c199 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:53:46 +0000 Subject: [PATCH 01/24] Route exact-byte header checks through Idris RefC with replayable state proofs --- .github/workflows/e2e.yml | 11 +- .github/workflows/header-refc.yml | 56 +++ .github/workflows/header-theory.yml | 65 +++ bindings/c/include/proven.h | 6 +- docs/adr/2026-11-16-header-refc-boundary.adoc | 91 ++++ docs/ffi-ci-build.adoc | 22 +- ffi/zig/build.zig | 7 +- ffi/zig/build_standalone.zig | 51 +- ffi/zig/src/core.zig | 396 +++++++++++++++ ffi/zig/src/header_refc.zig | 28 ++ ffi/zig/src/header_unavailable.zig | 5 + ffi/zig/src/main.zig | 451 +----------------- ffi/zig/src/main_refc.zig | 80 ++++ proven-ffi.ipkg | 1 + proven.ipkg | 1 + scripts/build-header-refc.sh | 50 ++ scripts/check-header-proofs.sh | 59 +++ scripts/test-header-bridge.sh | 41 ++ src/Proven/FFI/HeaderKernelMain.idr | 12 + src/Proven/FFI/Main.idr | 3 + src/Proven/SafeHeader/ByteKernel.idr | 75 +++ verification/header/.gitignore | 9 + verification/header/agda/ProvenHeader.agda | 55 +++ verification/header/coq/ProvenHeader.v | 31 ++ .../header/cubical/ProvenHeaderCubical.agda | 12 + verification/header/lean4/ProvenHeader.lean | 41 ++ 26 files changed, 1196 insertions(+), 463 deletions(-) create mode 100644 .github/workflows/header-refc.yml create mode 100644 .github/workflows/header-theory.yml create mode 100644 docs/adr/2026-11-16-header-refc-boundary.adoc create mode 100644 ffi/zig/src/core.zig create mode 100644 ffi/zig/src/header_refc.zig create mode 100644 ffi/zig/src/header_unavailable.zig create mode 100644 ffi/zig/src/main_refc.zig create mode 100644 scripts/build-header-refc.sh create mode 100644 scripts/check-header-proofs.sh create mode 100644 scripts/test-header-bridge.sh create mode 100644 src/Proven/FFI/HeaderKernelMain.idr create mode 100644 src/Proven/SafeHeader/ByteKernel.idr create mode 100644 verification/header/.gitignore create mode 100644 verification/header/agda/ProvenHeader.agda create mode 100644 verification/header/coq/ProvenHeader.v create mode 100644 verification/header/cubical/ProvenHeaderCubical.agda create mode 100644 verification/header/lean4/ProvenHeader.lean diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 75a45e36..f35daf20 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -3,7 +3,8 @@ # # proven — E2E + Safety Aspect + Bench # -# Tests the full proof chain: Idris2 → Zig FFI → integration. +# Tests the standalone surface, including explicit absent-backend refusal. +# header-refc.yml executes the real Idris -> RefC -> Zig header boundary. # Safety aspects verify no dangerous patterns escape into production. name: E2E + Safety + Bench @@ -42,16 +43,14 @@ jobs: uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: version: '0.15.2' - # Build the standalone pure-Zig FFI surface (src/main.zig only) -- no - # Idris2 RefC / idris2_zig_ffi dependency. The full Idris->RefC->Zig - # pipeline needs the private nextgen-languages/language-bridges repo, - # which CI cannot fetch as a path dependency; see PR #149. + # The standalone header function must return -99 without RefC. + # The public header-refc.yml gate supplies and exercises the real backend. - name: Build FFI (standalone Zig surface) run: cd ffi/zig && zig build --build-file build_standalone.zig - name: Run FFI tests (standalone) run: cd ffi/zig && zig build test --build-file build_standalone.zig - name: Verify library output - run: ls -la ffi/zig/zig-out/lib/libproven_ffi.* || echo "Library output check" + run: test -s ffi/zig/zig-out/lib/libproven_ffi.a safety-aspects: name: Aspect — Safety Invariants runs-on: ubuntu-latest diff --git a/.github/workflows/header-refc.yml b/.github/workflows/header-refc.yml new file mode 100644 index 00000000..eba79673 --- /dev/null +++ b/.github/workflows/header-refc.yml @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +name: Header RefC soundness +on: + pull_request: + push: + branches: [main] + workflow_dispatch: +permissions: + contents: read +concurrency: + group: header-refc-${{ github.ref }} + cancel-in-progress: true +jobs: + header-refc: + name: Real Idris header ABI + runs-on: ubuntu-24.04 + timeout-minutes: 45 + env: + IDRIS_REF: b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b + steps: + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + with: + persist-credentials: false + - name: Install compiler dependencies + run: sudo apt-get update && sudo apt-get install -y chezscheme libgmp-dev + - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + id: idris-cache + with: + path: ~/.idris2 + key: header-refc-${{ runner.os }}-${{ runner.arch }}-${{ env.IDRIS_REF }} + - name: Build pinned Idris compiler and matching RefC SDK + if: steps.idris-cache.outputs.cache-hit != 'true' + run: | + set -euo pipefail + git init "$RUNNER_TEMP/idris2" + git -C "$RUNNER_TEMP/idris2" remote add origin https://github.com/idris-lang/Idris2.git + git -C "$RUNNER_TEMP/idris2" fetch --depth 1 origin "$IDRIS_REF" + git -C "$RUNNER_TEMP/idris2" checkout --detach FETCH_HEAD + test "$(git -C "$RUNNER_TEMP/idris2" rev-parse HEAD)" = "$IDRIS_REF" + cd "$RUNNER_TEMP/idris2" + make bootstrap SCHEME=chezscheme + make install + - name: Add Idris to PATH + run: echo "$HOME/.idris2/bin" >> "$GITHUB_PATH" + - uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 + with: + version: '0.15.2' + - name: Total Idris kernel, actual ABI, exhaustive pairs and rejecting mutant + run: bash scripts/test-header-bridge.sh + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + if: always() + with: + name: header-refc-evidence + path: build/header-evidence/ + if-no-files-found: error diff --git a/.github/workflows/header-theory.yml b/.github/workflows/header-theory.yml new file mode 100644 index 00000000..7ee88583 --- /dev/null +++ b/.github/workflows/header-theory.yml @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +name: Header theory replay +on: + pull_request: + push: + branches: [main] + workflow_dispatch: +permissions: + contents: read +concurrency: + group: header-theory-${{ github.ref }} + cancel-in-progress: true +jobs: + theory: + name: Agda Cubical Lean Rocq replay + runs-on: ubuntu-24.04 + timeout-minutes: 90 + env: + OPAMYES: '1' + OPAMJOBS: '2' + steps: + - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4 + with: + persist-credentials: false + - name: Install proof tool dependencies + run: sudo apt-get update && sudo apt-get install -y ghc cabal-install opam libgmp-dev zstd + - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + id: proof-cache + with: + path: | + ~/.cabal + ~/.opam + ~/.local/header-proof-tools + key: header-theory-ubuntu2404-agda2643-lean4150-rocq920-ocaml511-v1 + - name: Build pinned proof tools + if: steps.proof-cache.outputs.cache-hit != 'true' + run: | + set -euo pipefail + mkdir -p "$HOME/.local/header-proof-tools/bin" + cabal update + cabal v2-install Agda-2.6.4.3 --index-state=2026-11-16T00:00:00Z --jobs=2 --install-method=copy --installdir="$HOME/.local/header-proof-tools/bin" + opam init --bare --disable-sandboxing -y + opam switch create header-proof ocaml-base-compiler.5.1.1 -y + opam install --switch=header-proof coq.9.2.0 rocq-stdlib.9.2.0 -y + curl --fail --location --proto '=https' --tlsv1.2 https://github.com/leanprover/lean4/releases/download/v4.15.0/lean-4.15.0-linux.tar.zst -o "$RUNNER_TEMP/lean.tar.zst" + tar --zstd -xf "$RUNNER_TEMP/lean.tar.zst" -C "$HOME/.local/header-proof-tools" + - name: Fetch the exact Sholes proof source + run: | + set -euo pipefail + git init "$RUNNER_TEMP/sholes" + git -C "$RUNNER_TEMP/sholes" remote add origin https://github.com/hyperpolymath/sholes.git + git -C "$RUNNER_TEMP/sholes" fetch --depth 1 origin c28993a73d1c78830c6d92f563fae638d22bb7f7 + - name: Replay proofs, audit assumptions and reject false witnesses + run: | + set -euo pipefail + eval "$(opam env --switch=header-proof --set-switch)" + export PATH="$HOME/.local/header-proof-tools/bin:$HOME/.local/header-proof-tools/lean-4.15.0-linux/bin:$PATH" + bash scripts/check-header-proofs.sh "$RUNNER_TEMP/sholes" + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + if: always() + with: + name: header-theory-evidence + path: build/header-proof-evidence/ + if-no-files-found: error diff --git a/bindings/c/include/proven.h b/bindings/c/include/proven.h index b5e09f9c..e68555e0 100644 --- a/bindings/c/include/proven.h +++ b/bindings/c/include/proven.h @@ -393,8 +393,10 @@ bool proven_network_ipv4_is_loopback(ProvenIPv4Address addr); * ============================================================================ */ /** - * @brief Check for CRLF injection characters in header value. - * @return Result with detection status (true = CRLF found). + * @brief Detect either CR (13) or LF (10) in the exact byte span. + * Embedded NUL and non-UTF-8 bytes are data. ptr must name len readable bytes. + * @return Status 0 and true if either byte occurs; -1 for null ptr (even len=0), + * -99 if built without the Idris backend, -7 for an invalid backend result. */ ProvenBoolResult proven_header_has_crlf(const uint8_t* ptr, size_t len); diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc new file mode 100644 index 00000000..24863ac2 --- /dev/null +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -0,0 +1,91 @@ +// SPDX-License-Identifier: CC-BY-SA-4.0 +// Copyright (c) 2026 Jonathan D.A. Jewell += Exact-byte Idris header boundary + +== Repaired behavior + +`proven_header_has_crlf` detects either CR (13) or LF (10), including lone +characters. The previous Zig implementation detected only adjacent CRLF and +disagreed with `Proven.SafeHeader.Types.hasCRLF` on CR, LF and embedded CR. +The repair invokes compiled Idris for each byte transition. Zig only walks +the caller's slice, marshals state/byte values, and validates the returned state. +It does not duplicate the predicate. + +The byte span is authoritative. NUL is data, invalid UTF-8 is data, and no +String conversion or character truncation occurs. A null pointer returns -1 +even at length zero. Callers must supply a readable span; arbitrary pointer +validity is not established by this API. Success returns status 0; an absent +Idris backend returns -99; a noncanonical RefC state returns -7. Consumers must +check status before interpreting the Boolean. + +== Source and ABI decision + +`Proven.SafeHeader.ByteKernel.headerStep` owns the concrete transition. +State 0 is clean and state 1 means a forbidden byte has been seen. +`stepEncodes` and `scanEncodes` relate the Bits8 transition to the typed Boolean +state machine; `scanAppend` proves arbitrary chunk splitting for the Idris fold. +`HeaderKernelMain` is a dynamic code-generation root that retains the named +`%noinline` symbol. It is not the library entry point and is never called by +the Zig API. A normal Idris module export alone does not establish C reachability. + +The qualified profile is native Linux x86_64 GNU, Zig 0.15.2 and Idris2 commit +`b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b`. The build copies the compiler's own +RefC SDK and imports `idris2_vp_int_shift` from its actual header. Bits8 values +are immediate tagged words. The bridge accepts only canonical 0/1 results and +uses the runtime trampoline. No borrowed Idris String or heap list crosses +this boundary. Generated C `main` is renamed when linked into the library. + +`scripts/build-header-refc.sh` needs no private `language-bridges` dependency. +The standalone library without its bundle deliberately refuses header checks. +The whole-library RefC build still has a private dependency and is not qualified +by this header-only result. Other legacy exports are not repaired or verified +by this work. + +== Reproduction and evidence + +[source,shell] +---- +bash scripts/test-header-bridge.sh +bash scripts/check-header-proofs.sh /path/to/sholes +---- + +The first command requires the qualified Idris and Zig on PATH (or `IDRIS2` +and `ZIG`). It runs the actual C ABI for all 256 single bytes and 65,536 byte +pairs, null/empty spans, embedded NUL, non-UTF-8, explicit prefix lengths and +an 8,192-byte boundary case. An ABI-compatible always-clean generated-C mutant +must fail assertions, not merely fail compilation. The unavailable profile +must return -99. `header-refc.yml` runs this gate on every PR and main push. +Evidence in `build/header-evidence` records versions, source/SDK hashes and +positive/negative logs. + +The second command imports only committed proof sources from Sholes +`c28993a73d1c78830c6d92f563fae638d22bb7f7` into a clean scratch directory. +`verification/header` proves the numeric-byte classifier, the unbounded state +fold, chunk composition and preservation by Sholes canonicalization. Required +profiles are Agda 2.6.4.3 with `--safe --cubical-compatible --double-check`, +the separate full Cubical consumer, Lean 4.15.0 and Rocq 9.2 plus `coqchk`. +The Lean/Rocq sources are hand re-proofs of the Agda statement. The checker +script audits named theorem assumptions and requires three false witnesses +to be rejected. `header-theory.yml` runs this separate theory gate. The RefC +job checks the Idris implementation and executable ABI; neither job proves +the remaining source/compiler refinement theorem. + +== Remaining proof obligations + +. Relate primitive Idris Bits8 equality and its compiled representation to the + structurally finite Agda model for all values, with a source-bound witness. +. Prove the actual Zig loop implements the Idris fold, including bounds, + state preservation, pointer validity preconditions and ABI correspondence. +. Establish compiler/lowering refinement or document the precise trusted + compilation boundary. Tests and model theorems do not establish this theorem. +. Transport and audit statement identity across proof assistants. Matching + hand re-proofs are not automatic proof-object translation. +. Add the matching alternate checker and qualify additional target/compiler + profiles independently. Full Cubical is a stronger profile of Agda, not an + independent kernel; no successful Lean4Lean run is claimed here. + +Atoma's original five-case diagnostic now links the real RefC library and +can confirm that the observed mismatch is repaired. Atoma's current closed +u8 source subset does not cover this loop; its source-to-proof milestone +remains INCOMPLETE. The model proof and executable evidence must not be +promoted to a whole-library or v1 verification claim. diff --git a/docs/ffi-ci-build.adoc b/docs/ffi-ci-build.adoc index 872e40e3..7ccd8782 100644 --- a/docs/ffi-ci-build.adoc +++ b/docs/ffi-ci-build.adoc @@ -1,5 +1,15 @@ == FFI CI build: standalone surface and full Idris→Zig pipeline +=== Header boundary qualification (2026-11-16) + +The standalone build now has an explicit header backend choice. Without +`-Dheader-refc`, header detection returns status -99. With the native SDK bundle +from `scripts/build-header-refc.sh`, it links the actual Idris byte kernel. +`header-refc.yml` checks this public path without the private bridge dependency. +The older whole-library descriptions below do not qualify every export. +See link:adr/2026-11-16-header-refc-boundary.adoc[the header boundary decision] +for supported versions, executable evidence and outstanding proof obligations. + `+proven+`’s FFI layer compiles Idris 2 to C via the RefC backend and wraps it in a stable C ABI with Zig (`+Idris2 --codegen refc → .c → zig build → libproven+`). There are @@ -13,8 +23,9 @@ wraps it in a stable C ABI with Zig + generated RefC C |local dev, full integration CI |`+ffi/zig/build_standalone.zig+` |`+libproven_ffi.a+` from -`+src/main.zig+` only (pure-Zig C-ABI surface) |none |*CI* (`+e2e.yml+`, -`+zig-ffi.yml+`) +the common C ABI surface; header unavailable unless a RefC bundle is supplied +|none for unavailable profile; pinned native RefC SDK for header profile +|*CI* (`+e2e.yml+`, `+zig-ffi.yml+`, `+header-refc.yml+`) |=== === Why E2E uses the standalone build (ADR-003) @@ -28,10 +39,9 @@ wraps it in a stable C ABI with Zig That points at a *private, cross-owner bridge repo*. `+e2e.yml+` stays on `+build_standalone.zig+` so the broad E2E and benchmark jobs keep -running without private credentials. `+build_standalone.zig+` compiles -`+src/main.zig+` alone — and that file imports only `+std+`/`+builtin+` -— so it needs neither the bridge nor the generated RefC, giving CI a -real (if narrower) Zig FFI build + unit-test signal. +running without private credentials. Its unavailable-backend profile needs +neither the private bridge nor generated RefC. The separate public header +gate compiles the Idris kernel and exercises the exported ABI. `+e2e.yml+` and `+zig-ffi.yml+` therefore run, with Zig provisioned by `+mlugg/setup-zig@v1+` at `+0.15.2+` (the `+build.zig.zon+` minimum): diff --git a/ffi/zig/build.zig b/ffi/zig/build.zig index af988ddc..eb77bbb3 100644 --- a/ffi/zig/build.zig +++ b/ffi/zig/build.zig @@ -56,11 +56,12 @@ fn configureRefC( "-std=c11", "-fno-strict-aliasing", "-D_GNU_SOURCE", + "-Dmain=proven_refc_codegen_main", }, }); } } else |_| { - // Directory doesn't exist yet (pre-RefC build) + @panic("missing RefC source directory; build Idris before linking"); } } @@ -124,6 +125,8 @@ pub fn build(b: *std.Build) void { "idris-support-lib", "Path to libidris2_support.{a,so} directory", ); + if (refc_path == null or refc_runtime_path == null or c_support_path == null or support_lib_path == null) + @panic("the full FFI build requires all four Idris RefC paths; use build_standalone.zig for explicit unavailable-backend behavior"); // ====================================================================== // Get idris2-zig-ffi dependency @@ -140,7 +143,7 @@ pub fn build(b: *std.Build) void { // ====================================================================== const proven_mod = b.addModule("proven", .{ - .root_source_file = b.path("src/main.zig"), + .root_source_file = b.path("src/main_refc.zig"), .target = target, .optimize = optimize, .imports = &.{ diff --git a/ffi/zig/build_standalone.zig b/ffi/zig/build_standalone.zig index 083d5f06..6c3916a9 100644 --- a/ffi/zig/build_standalone.zig +++ b/ffi/zig/build_standalone.zig @@ -4,19 +4,38 @@ // build_standalone.zig — standalone build for proven FFI without the Idris2 RefC // pipeline dependency (idris2_zig_ffi from nextgen-languages/language-bridges). // -// This build produces libproven_ffi.a from src/main.zig only, exposing the -// pure-Zig C ABI symbols (proven_path_has_traversal, proven_init, etc.) that -// consumers such as developer-ecosystem/zig-api can link against. +// Without -Dheader-refc the header export refuses with -99. Passing a native +// SDK bundle from scripts/build-header-refc.sh selects the real Idris kernel. +// Other legacy exports are unchanged; this is not a whole-library proof claim. // // Usage: // zig build --build-file build_standalone.zig -// # Outputs: zig-out-standalone/lib/libproven_ffi.a +// # Outputs: zig-out/lib/libproven_ffi.a const std = @import("std"); +fn attachHeader(b: *std.Build, mod: *std.Build.Module, path: []const u8) void { + if (!std.fs.path.isAbsolute(path)) @panic("-Dheader-refc must be an absolute SDK bundle path"); + mod.addIncludePath(.{ .cwd_relative = b.fmt("{s}/refc", .{path}) }); + mod.addIncludePath(.{ .cwd_relative = b.fmt("{s}/support", .{path}) }); + mod.addCSourceFile(.{ + .file = .{ .cwd_relative = b.fmt("{s}/kernel.c", .{path}) }, + .flags = &.{ "-std=c11", "-D_GNU_SOURCE", "-Dmain=proven_header_codegen_main", "-fno-strict-aliasing" }, + }); + mod.addObjectFile(.{ .cwd_relative = b.fmt("{s}/libidris2_refc.a", .{path}) }); + mod.addObjectFile(.{ .cwd_relative = b.fmt("{s}/libidris2_support.a", .{path}) }); + mod.linkSystemLibrary("gmp", .{}); + mod.linkSystemLibrary("m", .{}); + mod.linkSystemLibrary("pthread", .{}); +} + pub fn build(b: *std.Build) void { - const target = b.standardTargetOptions(.{}); + const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const header = b.option([]const u8, "header-refc", "Absolute bundle from scripts/build-header-refc.sh; absent means header operations return -99"); + if (header != null and (!target.query.isNative() or target.result.os.tag != .linux or target.result.cpu.arch != .x86_64 or target.result.abi != .gnu)) + @panic("header RefC profile currently supports native Linux x86_64 GNU only"); + const source = if (header != null) "src/main_refc.zig" else "src/main.zig"; // ------------------------------------------------------------------------- // Static library — libproven_ffi.a @@ -24,16 +43,17 @@ pub fn build(b: *std.Build) void { // proven_path_has_traversal and the lifecycle functions. // ------------------------------------------------------------------------- const proven_mod = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - .link_libc = true, + .root_source_file = b.path(source), + .target = target, + .optimize = optimize, + .link_libc = true, }); + if (header) |path| attachHeader(b, proven_mod, path); const lib = b.addLibrary(.{ - .name = "proven_ffi", + .name = "proven_ffi", .root_module = proven_mod, - .linkage = .static, + .linkage = .static, }); b.installArtifact(lib); @@ -41,11 +61,12 @@ pub fn build(b: *std.Build) void { // Unit tests — run with `zig build test --build-file build_standalone.zig` // ------------------------------------------------------------------------- const test_mod = b.createModule(.{ - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - .link_libc = true, + .root_source_file = b.path(source), + .target = target, + .optimize = optimize, + .link_libc = true, }); + if (header) |path| attachHeader(b, test_mod, path); const unit_tests = b.addTest(.{ .root_module = test_mod, }); diff --git a/ffi/zig/src/core.zig b/ffi/zig/src/core.zig new file mode 100644 index 00000000..eb0013f9 --- /dev/null +++ b/ffi/zig/src/core.zig @@ -0,0 +1,396 @@ +// PROVEN FFI Implementation +// +// This module implements the C-compatible FFI declared in src/abi/Foreign.idr +// All types and layouts must match the Idris2 ABI definitions. +// +// SPDX-License-Identifier: MPL-2.0 + +const std = @import("std"); + +// Version information (keep in sync with project) +const VERSION = "0.1.0"; +const BUILD_INFO = "PROVEN built with Zig " ++ @import("builtin").zig_version_string; + +/// Thread-local error storage +threadlocal var last_error: ?[]const u8 = null; + +/// Set the last error message +fn setError(msg: []const u8) void { + last_error = msg; +} + +/// Clear the last error +fn clearError() void { + last_error = null; +} + +//============================================================================== +// Core Types (must match src/abi/Types.idr) +//============================================================================== + +/// Result codes (must match Idris2 Result type) +pub const Result = enum(c_int) { + ok = 0, + @"error" = 1, + invalid_param = 2, + out_of_memory = 3, + null_pointer = 4, +}; + +/// Library handle. C consumers receive only a pointer, so C code treats it +/// as opaque. Zig code accesses fields directly via the pointer. +pub const Handle = struct { + allocator: std.mem.Allocator, + initialized: bool, +}; + +//============================================================================== +// Library Lifecycle +//============================================================================== + +/// Initialize the library +/// Returns a handle, or null on failure +export fn proven_init() ?*Handle { + const allocator = std.heap.c_allocator; + + const handle = allocator.create(Handle) catch { + setError("Failed to allocate handle"); + return null; + }; + + // Initialize handle + handle.* = .{ + .allocator = allocator, + .initialized = true, + }; + + clearError(); + return handle; +} + +/// Free the library handle +export fn proven_free(handle: ?*Handle) void { + const h = handle orelse return; + const allocator = h.allocator; + + // Clean up resources + h.initialized = false; + + allocator.destroy(h); + clearError(); +} + +/// Return the library version triple +export fn proven_version_major() u32 { + return 1; +} +export fn proven_version_minor() u32 { + return 1; +} +export fn proven_version_patch() u32 { + return 0; +} + +/// Return the FFI ABI version +export fn proven_ffi_abi_version() u32 { + return 1; +} + +//============================================================================== +// Core Operations +//============================================================================== + +/// Process data (example operation) +export fn proven_process(handle: ?*Handle, input: u32) Result { + const h = handle orelse { + setError("Null handle"); + return .null_pointer; + }; + + if (!h.initialized) { + setError("Handle not initialized"); + return .@"error"; + } + + // Example processing logic + _ = input; + + clearError(); + return .ok; +} + +//============================================================================== +// String Operations +//============================================================================== + +/// Get a string result (example) +/// Caller must free the returned string +export fn proven_get_string(handle: ?*Handle) ?[*:0]const u8 { + const h = handle orelse { + setError("Null handle"); + return null; + }; + + if (!h.initialized) { + setError("Handle not initialized"); + return null; + } + + // Example: allocate and return a string + const result = h.allocator.dupeZ(u8, "Example result") catch { + setError("Failed to allocate string"); + return null; + }; + + clearError(); + return result.ptr; +} + +/// Free a string allocated by the library +export fn proven_free_string(str: ?[*:0]const u8) void { + const s = str orelse return; + const allocator = std.heap.c_allocator; + + const slice = std.mem.span(s); + allocator.free(slice); +} + +//============================================================================== +// Array/Buffer Operations +//============================================================================== + +/// Process an array of data +export fn proven_process_array( + handle: ?*Handle, + buffer: ?[*]const u8, + len: u32, +) Result { + const h = handle orelse { + setError("Null handle"); + return .null_pointer; + }; + + const buf = buffer orelse { + setError("Null buffer"); + return .null_pointer; + }; + + if (!h.initialized) { + setError("Handle not initialized"); + return .@"error"; + } + + // Access the buffer + const data = buf[0..len]; + _ = data; + + // Process data here + + clearError(); + return .ok; +} + +//============================================================================== +// Error Handling +//============================================================================== + +/// Get the last error message +/// Returns null if no error +export fn proven_last_error() ?[*:0]const u8 { + const err = last_error orelse return null; + + // Return C string (static storage, no need to free) + const allocator = std.heap.c_allocator; + const c_str = allocator.dupeZ(u8, err) catch return null; + return c_str.ptr; +} + +//============================================================================== +// Version Information +//============================================================================== + +/// Get the library version +export fn proven_version() [*:0]const u8 { + return VERSION.ptr; +} + +/// Get build information +export fn proven_build_info() [*:0]const u8 { + return BUILD_INFO.ptr; +} + +//============================================================================== +// Callback Support +//============================================================================== + +/// Callback function type (C ABI) +pub const Callback = *const fn (u64, u32) callconv(.c) u32; + +/// Register a callback +export fn proven_register_callback( + handle: ?*Handle, + callback: ?Callback, +) Result { + const h = handle orelse { + setError("Null handle"); + return .null_pointer; + }; + + const cb = callback orelse { + setError("Null callback"); + return .null_pointer; + }; + + if (!h.initialized) { + setError("Handle not initialized"); + return .@"error"; + } + + // Store callback for later use + _ = cb; + + clearError(); + return .ok; +} + +//============================================================================== +// SafePath — Filesystem traversal prevention +// Matches the declarations in proven.h and src/abi/Foreign.idr. +//============================================================================== + +/// C-ABI result type for boolean operations (matches ProvenBoolResult in proven.h). +/// Layout: { status: i32, value: bool } (4 + 1 bytes; compiler pads as needed). +pub const BoolResult = extern struct { + status: c_int, + value: bool, +}; + +/// Status codes matching ProvenStatus in proven.h. +const PROVEN_OK: c_int = 0; +const PROVEN_ERR_NULL_POINTER: c_int = -1; + +/// Check if a byte slice contains a directory traversal sequence (".."). +/// +/// Matches: ProvenBoolResult proven_path_has_traversal(const uint8_t* ptr, size_t len) +/// +/// Returns BoolResult{ .status = PROVEN_OK, .value = true } when a ".." component +/// is present in the path, and { .status = PROVEN_OK, .value = false } otherwise. +/// Returns { .status = PROVEN_ERR_NULL_POINTER } when ptr is null. +/// +/// Detection rule: look for the two-byte sequence ".." bounded by path separators +/// ('/'), a NUL byte, or the start/end of the slice. This matches what the +/// Idris2 backend will formally verify once the RefC pipeline is available. +export fn proven_path_has_traversal(ptr: ?[*]const u8, len: usize) callconv(.c) BoolResult { + const p = ptr orelse return BoolResult{ .status = PROVEN_ERR_NULL_POINTER, .value = false }; + if (len == 0) return BoolResult{ .status = PROVEN_OK, .value = false }; + + const bytes = p[0..len]; + + // Walk the byte slice and check every ".." component. + var i: usize = 0; + while (i < len) { + // Find next component boundary: advance past leading separators. + while (i < len and bytes[i] == '/') : (i += 1) {} + if (i >= len) break; + + // Measure the component. + const comp_start = i; + while (i < len and bytes[i] != '/') : (i += 1) {} + const component = bytes[comp_start..i]; + + if (std.mem.eql(u8, component, "..")) { + return BoolResult{ .status = PROVEN_OK, .value = true }; + } + } + + return BoolResult{ .status = PROVEN_OK, .value = false }; +} + +//============================================================================== +// SafeHeader — HTTP header safety +// Matches the declarations in proven.h and src/abi/Foreign.idr. +//============================================================================== + +/// Check if a byte slice contains CRLF injection characters ("\r\n"). +/// +/// Matches: ProvenBoolResult proven_header_has_crlf(const uint8_t* ptr, size_t len) +/// +/// Returns success/true when either CR or LF is found by the Idris byte kernel, +/// and { .status = PROVEN_OK, .value = false } otherwise. +/// Returns { .status = PROVEN_ERR_NULL_POINTER } when ptr is null. +/// +/// The exact byte range is passed to Idris; embedded NUL is data. +/// Without a linked Idris backend, returns PROVEN_ERR_NOT_IMPLEMENTED (-99). +pub fn headerHasCrlf(comptime header_backend: type, ptr: ?[*]const u8, len: usize) BoolResult { + const p = ptr orelse return BoolResult{ .status = PROVEN_ERR_NULL_POINTER, .value = false }; + const detected = header_backend.detect(p[0..len]) catch |err| { + setError(@errorName(err)); + return .{ .status = if (@as(anyerror, err) == error.BackendUnavailable) -99 else -7, .value = false }; + }; + return .{ .status = PROVEN_OK, .value = detected }; +} + +//============================================================================== +// Utility Functions +//============================================================================== + +/// Check if handle is initialized +export fn proven_is_initialized(handle: ?*Handle) u32 { + const h = handle orelse return 0; + return if (h.initialized) 1 else 0; +} + +//============================================================================== +// Tests +//============================================================================== + +test "lifecycle" { + const handle = proven_init() orelse return error.InitFailed; + defer proven_free(handle); + + try std.testing.expect(proven_is_initialized(handle) == 1); +} + +test "error handling" { + const result = proven_process(null, 0); + try std.testing.expectEqual(Result.null_pointer, result); + + const err = proven_last_error(); + try std.testing.expect(err != null); +} + +test "version" { + const ver = proven_version(); + const ver_str = std.mem.span(ver); + try std.testing.expectEqualStrings(VERSION, ver_str); +} + +test "proven_path_has_traversal detects traversal" { + // Classic attacks. + const r1 = proven_path_has_traversal("../etc/passwd".ptr, "../etc/passwd".len); + try std.testing.expectEqual(@as(c_int, PROVEN_OK), r1.status); + try std.testing.expect(r1.value); + + const r2 = proven_path_has_traversal("/tmp/../../root".ptr, "/tmp/../../root".len); + try std.testing.expectEqual(@as(c_int, PROVEN_OK), r2.status); + try std.testing.expect(r2.value); +} + +test "proven_path_has_traversal allows safe paths" { + const r1 = proven_path_has_traversal("/var/mnt/eclipse/repos/myrepo/file.scm".ptr, "/var/mnt/eclipse/repos/myrepo/file.scm".len); + try std.testing.expectEqual(@as(c_int, PROVEN_OK), r1.status); + try std.testing.expect(!r1.value); + + const r2 = proven_path_has_traversal("/tmp/scratch.scm".ptr, "/tmp/scratch.scm".len); + try std.testing.expectEqual(@as(c_int, PROVEN_OK), r2.status); + try std.testing.expect(!r2.value); +} + +test "proven_path_has_traversal handles null and empty" { + const r_null = proven_path_has_traversal(null, 0); + try std.testing.expectEqual(@as(c_int, PROVEN_ERR_NULL_POINTER), r_null.status); + + const r_empty = proven_path_has_traversal("".ptr, 0); + try std.testing.expectEqual(@as(c_int, PROVEN_OK), r_empty.status); + try std.testing.expect(!r_empty.value); +} diff --git a/ffi/zig/src/header_refc.zig b/ffi/zig/src/header_refc.zig new file mode 100644 index 00000000..87af65d6 --- /dev/null +++ b/ffi/zig/src/header_refc.zig @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2026 Jonathan D.A. Jewell +//! RefC Bits8 marshaling. The predicate and transition live in Idris. +const c = @cImport({ + @cInclude("_datatypes.h"); +}); +extern fn Proven_SafeHeader_ByteKernel_headerStep(?*anyopaque, ?*anyopaque) callconv(.c) ?*anyopaque; +extern fn idris2_trampoline(?*anyopaque) callconv(.c) ?*anyopaque; +const shift = c.idris2_vp_int_shift; + +fn box(b: u8) ?*anyopaque { + return @ptrFromInt((@as(usize, b) << shift) | 1); +} + +pub fn detect(bytes: []const u8) error{InvalidRefcResult}!bool { + var state: u8 = 0; + for (bytes) |b| { + const result = idris2_trampoline(Proven_SafeHeader_ByteKernel_headerStep(box(state), box(b))); + const raw = @intFromPtr(result); + // Accept only the two canonical RefC Bits8 state encodings. + if (raw == @intFromPtr(box(0))) { + state = 0; + } else if (raw == @intFromPtr(box(1))) { + state = 1; + } else return error.InvalidRefcResult; + } + return state == 1; +} diff --git a/ffi/zig/src/header_unavailable.zig b/ffi/zig/src/header_unavailable.zig new file mode 100644 index 00000000..4d8e356c --- /dev/null +++ b/ffi/zig/src/header_unavailable.zig @@ -0,0 +1,5 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2026 Jonathan D.A. Jewell +pub fn detect(_: []const u8) error{BackendUnavailable}!bool { + return error.BackendUnavailable; +} diff --git a/ffi/zig/src/main.zig b/ffi/zig/src/main.zig index 3e669243..d05e84bf 100644 --- a/ffi/zig/src/main.zig +++ b/ffi/zig/src/main.zig @@ -1,436 +1,23 @@ -// PROVEN FFI Implementation -// -// This module implements the C-compatible FFI declared in src/abi/Foreign.idr -// All types and layouts must match the Idris2 ABI definitions. -// // SPDX-License-Identifier: MPL-2.0 - +// Copyright (c) 2026 Jonathan D.A. Jewell +//! Standalone surface: header checks explicitly require the RefC build. const std = @import("std"); - -// Version information (keep in sync with project) -const VERSION = "0.1.0"; -const BUILD_INFO = "PROVEN built with Zig " ++ @import("builtin").zig_version_string; - -/// Thread-local error storage -threadlocal var last_error: ?[]const u8 = null; - -/// Set the last error message -fn setError(msg: []const u8) void { - last_error = msg; -} - -/// Clear the last error -fn clearError() void { - last_error = null; -} - -//============================================================================== -// Core Types (must match src/abi/Types.idr) -//============================================================================== - -/// Result codes (must match Idris2 Result type) -pub const Result = enum(c_int) { - ok = 0, - @"error" = 1, - invalid_param = 2, - out_of_memory = 3, - null_pointer = 4, -}; - -/// Library handle. C consumers receive only a pointer, so C code treats it -/// as opaque. Zig code accesses fields directly via the pointer. -pub const Handle = struct { - allocator: std.mem.Allocator, - initialized: bool, -}; - -//============================================================================== -// Library Lifecycle -//============================================================================== - -/// Initialize the library -/// Returns a handle, or null on failure -export fn proven_init() ?*Handle { - const allocator = std.heap.c_allocator; - - const handle = allocator.create(Handle) catch { - setError("Failed to allocate handle"); - return null; - }; - - // Initialize handle - handle.* = .{ - .allocator = allocator, - .initialized = true, - }; - - clearError(); - return handle; -} - -/// Free the library handle -export fn proven_free(handle: ?*Handle) void { - const h = handle orelse return; - const allocator = h.allocator; - - // Clean up resources - h.initialized = false; - - allocator.destroy(h); - clearError(); -} - -/// Return the library version triple -export fn proven_version_major() u32 { return 1; } -export fn proven_version_minor() u32 { return 1; } -export fn proven_version_patch() u32 { return 0; } - -/// Return the FFI ABI version -export fn proven_ffi_abi_version() u32 { return 1; } - -//============================================================================== -// Core Operations -//============================================================================== - -/// Process data (example operation) -export fn proven_process(handle: ?*Handle, input: u32) Result { - const h = handle orelse { - setError("Null handle"); - return .null_pointer; - }; - - if (!h.initialized) { - setError("Handle not initialized"); - return .@"error"; - } - - // Example processing logic - _ = input; - - clearError(); - return .ok; -} - -//============================================================================== -// String Operations -//============================================================================== - -/// Get a string result (example) -/// Caller must free the returned string -export fn proven_get_string(handle: ?*Handle) ?[*:0]const u8 { - const h = handle orelse { - setError("Null handle"); - return null; - }; - - if (!h.initialized) { - setError("Handle not initialized"); - return null; - } - - // Example: allocate and return a string - const result = h.allocator.dupeZ(u8, "Example result") catch { - setError("Failed to allocate string"); - return null; - }; - - clearError(); - return result.ptr; -} - -/// Free a string allocated by the library -export fn proven_free_string(str: ?[*:0]const u8) void { - const s = str orelse return; - const allocator = std.heap.c_allocator; - - const slice = std.mem.span(s); - allocator.free(slice); -} - -//============================================================================== -// Array/Buffer Operations -//============================================================================== - -/// Process an array of data -export fn proven_process_array( - handle: ?*Handle, - buffer: ?[*]const u8, - len: u32, -) Result { - const h = handle orelse { - setError("Null handle"); - return .null_pointer; - }; - - const buf = buffer orelse { - setError("Null buffer"); - return .null_pointer; - }; - - if (!h.initialized) { - setError("Handle not initialized"); - return .@"error"; - } - - // Access the buffer - const data = buf[0..len]; - _ = data; - - // Process data here - - clearError(); - return .ok; -} - -//============================================================================== -// Error Handling -//============================================================================== - -/// Get the last error message -/// Returns null if no error -export fn proven_last_error() ?[*:0]const u8 { - const err = last_error orelse return null; - - // Return C string (static storage, no need to free) - const allocator = std.heap.c_allocator; - const c_str = allocator.dupeZ(u8, err) catch return null; - return c_str.ptr; -} - -//============================================================================== -// Version Information -//============================================================================== - -/// Get the library version -export fn proven_version() [*:0]const u8 { - return VERSION.ptr; -} - -/// Get build information -export fn proven_build_info() [*:0]const u8 { - return BUILD_INFO.ptr; -} - -//============================================================================== -// Callback Support -//============================================================================== - -/// Callback function type (C ABI) -pub const Callback = *const fn (u64, u32) callconv(.c) u32; - -/// Register a callback -export fn proven_register_callback( - handle: ?*Handle, - callback: ?Callback, -) Result { - const h = handle orelse { - setError("Null handle"); - return .null_pointer; - }; - - const cb = callback orelse { - setError("Null callback"); - return .null_pointer; - }; - - if (!h.initialized) { - setError("Handle not initialized"); - return .@"error"; +const core = @import("core.zig"); +pub const Result = core.Result; +pub const Handle = core.Handle; +pub const Callback = core.Callback; +pub const BoolResult = core.BoolResult; +comptime { + _ = core; +} +pub export fn proven_header_has_crlf(ptr: ?[*]const u8, len: usize) callconv(.c) BoolResult { + return core.headerHasCrlf(@import("header_unavailable.zig"), ptr, len); +} +test "header operation explicitly refuses an unavailable Idris backend" { + for ([_][]const u8{ "", "safe", "\r", "\n", "\r\n" }) |bytes| { + const result = proven_header_has_crlf(bytes.ptr, bytes.len); + try std.testing.expectEqual(@as(c_int, -99), result.status); + try std.testing.expect(!result.value); } - - // Store callback for later use - _ = cb; - - clearError(); - return .ok; -} - -//============================================================================== -// SafePath — Filesystem traversal prevention -// Matches the declarations in proven.h and src/abi/Foreign.idr. -//============================================================================== - -/// C-ABI result type for boolean operations (matches ProvenBoolResult in proven.h). -/// Layout: { status: i32, value: bool } (4 + 1 bytes; compiler pads as needed). -pub const BoolResult = extern struct { - status: c_int, - value: bool, -}; - -/// Status codes matching ProvenStatus in proven.h. -const PROVEN_OK: c_int = 0; -const PROVEN_ERR_NULL_POINTER: c_int = -1; - -/// Check if a byte slice contains a directory traversal sequence (".."). -/// -/// Matches: ProvenBoolResult proven_path_has_traversal(const uint8_t* ptr, size_t len) -/// -/// Returns BoolResult{ .status = PROVEN_OK, .value = true } when a ".." component -/// is present in the path, and { .status = PROVEN_OK, .value = false } otherwise. -/// Returns { .status = PROVEN_ERR_NULL_POINTER } when ptr is null. -/// -/// Detection rule: look for the two-byte sequence ".." bounded by path separators -/// ('/'), a NUL byte, or the start/end of the slice. This matches what the -/// Idris2 backend will formally verify once the RefC pipeline is available. -export fn proven_path_has_traversal(ptr: ?[*]const u8, len: usize) callconv(.c) BoolResult { - const p = ptr orelse return BoolResult{ .status = PROVEN_ERR_NULL_POINTER, .value = false }; - if (len == 0) return BoolResult{ .status = PROVEN_OK, .value = false }; - - const bytes = p[0..len]; - - // Walk the byte slice and check every ".." component. - var i: usize = 0; - while (i < len) { - // Find next component boundary: advance past leading separators. - while (i < len and bytes[i] == '/') : (i += 1) {} - if (i >= len) break; - - // Measure the component. - const comp_start = i; - while (i < len and bytes[i] != '/') : (i += 1) {} - const component = bytes[comp_start..i]; - - if (std.mem.eql(u8, component, "..")) { - return BoolResult{ .status = PROVEN_OK, .value = true }; - } - } - - return BoolResult{ .status = PROVEN_OK, .value = false }; -} - -//============================================================================== -// SafeHeader — HTTP header safety -// Matches the declarations in proven.h and src/abi/Foreign.idr. -//============================================================================== - -/// Check if a byte slice contains CRLF injection characters ("\r\n"). -/// -/// Matches: ProvenBoolResult proven_header_has_crlf(const uint8_t* ptr, size_t len) -/// -/// Returns BoolResult{ .status = PROVEN_OK, .value = true } when CRLF is found, -/// and { .status = PROVEN_OK, .value = false } otherwise. -/// Returns { .status = PROVEN_ERR_NULL_POINTER } when ptr is null. -/// -/// Detection rule: scan for the byte sequence CR (0x0D) followed by LF (0x0A). -/// This prevents CRLF injection attacks in HTTP headers. -export fn proven_header_has_crlf(ptr: ?[*]const u8, len: usize) callconv(.c) BoolResult { - const p = ptr orelse return BoolResult{ .status = PROVEN_ERR_NULL_POINTER, .value = false }; - if (len == 0) return BoolResult{ .status = PROVEN_OK, .value = false }; - - const bytes = p[0..len]; - - // Scan for the CRLF sequence: 0x0D (CR) followed by 0x0A (LF). - var i: usize = 0; - while (i < len - 1) { - if (bytes[i] == '\r' and bytes[i + 1] == '\n') { - return BoolResult{ .status = PROVEN_OK, .value = true }; - } - i += 1; - } - - return BoolResult{ .status = PROVEN_OK, .value = false }; -} - -//============================================================================== -// Utility Functions -//============================================================================== - -/// Check if handle is initialized -export fn proven_is_initialized(handle: ?*Handle) u32 { - const h = handle orelse return 0; - return if (h.initialized) 1 else 0; -} - -//============================================================================== -// Tests -//============================================================================== - -test "lifecycle" { - const handle = proven_init() orelse return error.InitFailed; - defer proven_free(handle); - - try std.testing.expect(proven_is_initialized(handle) == 1); -} - -test "error handling" { - const result = proven_process(null, 0); - try std.testing.expectEqual(Result.null_pointer, result); - - const err = proven_last_error(); - try std.testing.expect(err != null); -} - -test "version" { - const ver = proven_version(); - const ver_str = std.mem.span(ver); - try std.testing.expectEqualStrings(VERSION, ver_str); -} - -test "proven_path_has_traversal detects traversal" { - // Classic attacks. - const r1 = proven_path_has_traversal("../etc/passwd".ptr, "../etc/passwd".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r1.status); - try std.testing.expect(r1.value); - - const r2 = proven_path_has_traversal("/tmp/../../root".ptr, "/tmp/../../root".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r2.status); - try std.testing.expect(r2.value); -} - -test "proven_path_has_traversal allows safe paths" { - const r1 = proven_path_has_traversal("/var/mnt/eclipse/repos/myrepo/file.scm".ptr, - "/var/mnt/eclipse/repos/myrepo/file.scm".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r1.status); - try std.testing.expect(!r1.value); - - const r2 = proven_path_has_traversal("/tmp/scratch.scm".ptr, "/tmp/scratch.scm".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r2.status); - try std.testing.expect(!r2.value); -} - -test "proven_path_has_traversal handles null and empty" { - const r_null = proven_path_has_traversal(null, 0); - try std.testing.expectEqual(@as(c_int, PROVEN_ERR_NULL_POINTER), r_null.status); - - const r_empty = proven_path_has_traversal("".ptr, 0); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r_empty.status); - try std.testing.expect(!r_empty.value); -} - -test "proven_header_has_crlf detects CRLF" { - // Classic CRLF injection. - const r1 = proven_header_has_crlf("Set-Cookie: foo=bar\r\nSet-Cookie: admin=true".ptr, - "Set-Cookie: foo=bar\r\nSet-Cookie: admin=true".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r1.status); - try std.testing.expect(r1.value); - - const r2 = proven_header_has_crlf("X-Custom\r\n".ptr, "X-Custom\r\n".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r2.status); - try std.testing.expect(r2.value); -} - -test "proven_header_has_crlf allows safe headers" { - const r1 = proven_header_has_crlf("Content-Type: application/json".ptr, - "Content-Type: application/json".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r1.status); - try std.testing.expect(!r1.value); - - const r2 = proven_header_has_crlf("Authorization: Bearer token123".ptr, - "Authorization: Bearer token123".len); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r2.status); - try std.testing.expect(!r2.value); -} - -test "proven_header_has_crlf handles null and empty" { - const r_null = proven_header_has_crlf(null, 0); - try std.testing.expectEqual(@as(c_int, PROVEN_ERR_NULL_POINTER), r_null.status); - - const r_empty = proven_header_has_crlf("".ptr, 0); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r_empty.status); - try std.testing.expect(!r_empty.value); - - // Single byte should not crash. - const r_single = proven_header_has_crlf("X".ptr, 1); - try std.testing.expectEqual(@as(c_int, PROVEN_OK), r_single.status); - try std.testing.expect(!r_single.value); + try std.testing.expectEqual(@as(c_int, -1), proven_header_has_crlf(null, 0).status); } diff --git a/ffi/zig/src/main_refc.zig b/ffi/zig/src/main_refc.zig new file mode 100644 index 00000000..5c6738f4 --- /dev/null +++ b/ffi/zig/src/main_refc.zig @@ -0,0 +1,80 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2026 Jonathan D.A. Jewell +const core = @import("core.zig"); +pub const Result = core.Result; +pub const Handle = core.Handle; +pub const Callback = core.Callback; +pub const BoolResult = core.BoolResult; +comptime { + _ = core; +} +pub const proven = @This(); +pub export fn proven_header_has_crlf(ptr: ?[*]const u8, len: usize) callconv(.c) core.BoolResult { + return core.headerHasCrlf(@import("header_refc.zig"), ptr, len); +} + +const std = @import("std"); +test "header ABI uses Idris for every one-byte and two-byte input" { + var bytes: [2]u8 = undefined; + for (0..256) |a| { + bytes[0] = @intCast(a); + const single = proven.proven_header_has_crlf(&bytes, 1); + try std.testing.expectEqual(@as(c_int, 0), single.status); + try std.testing.expectEqual(a == 10 or a == 13, single.value); + for (0..256) |b| { + bytes[1] = @intCast(b); + const r = proven.proven_header_has_crlf(&bytes, 2); + try std.testing.expectEqual(@as(c_int, 0), r.status); + try std.testing.expectEqual(a == 10 or a == 13 or b == 10 or b == 13, r.value); + } + } +} +test "header ABI preserves null, length, NUL and non-UTF8 semantics" { + try std.testing.expectEqual(@as(c_int, -1), proven.proven_header_has_crlf(null, 0).status); + try std.testing.expectEqual(@as(c_int, -1), proven.proven_header_has_crlf(null, 99).status); + const bytes = [_]u8{ 0xff, 0, 13, 0x80 }; + for (0..bytes.len + 1) |len| { + const r = proven.proven_header_has_crlf(&bytes, len); + try std.testing.expectEqual(@as(c_int, 0), r.status); + try std.testing.expectEqual(len >= 3, r.value); + } + var long = [_]u8{0} ** 8192; + long[long.len - 1] = 10; + try std.testing.expect(proven.proven_header_has_crlf(&long, long.len).value); + try std.testing.expect(!proven.proven_header_has_crlf(&long, long.len - 1).value); +} + +test "proven_header_has_crlf detects CRLF" { + // Classic CRLF injection. + const r1 = proven_header_has_crlf("Set-Cookie: foo=bar\r\nSet-Cookie: admin=true".ptr, "Set-Cookie: foo=bar\r\nSet-Cookie: admin=true".len); + try std.testing.expectEqual(@as(c_int, 0), r1.status); + try std.testing.expect(r1.value); + + const r2 = proven_header_has_crlf("X-Custom\r\n".ptr, "X-Custom\r\n".len); + try std.testing.expectEqual(@as(c_int, 0), r2.status); + try std.testing.expect(r2.value); +} + +test "proven_header_has_crlf allows safe headers" { + const r1 = proven_header_has_crlf("Content-Type: application/json".ptr, "Content-Type: application/json".len); + try std.testing.expectEqual(@as(c_int, 0), r1.status); + try std.testing.expect(!r1.value); + + const r2 = proven_header_has_crlf("Authorization: Bearer token123".ptr, "Authorization: Bearer token123".len); + try std.testing.expectEqual(@as(c_int, 0), r2.status); + try std.testing.expect(!r2.value); +} + +test "proven_header_has_crlf handles null and empty" { + const r_null = proven_header_has_crlf(null, 0); + try std.testing.expectEqual(@as(c_int, -1), r_null.status); + + const r_empty = proven_header_has_crlf("".ptr, 0); + try std.testing.expectEqual(@as(c_int, 0), r_empty.status); + try std.testing.expect(!r_empty.value); + + // Single byte should not crash. + const r_single = proven_header_has_crlf("X".ptr, 1); + try std.testing.expectEqual(@as(c_int, 0), r_single.status); + try std.testing.expect(!r_single.value); +} diff --git a/proven-ffi.ipkg b/proven-ffi.ipkg index ad3cddea..ca572157 100644 --- a/proven-ffi.ipkg +++ b/proven-ffi.ipkg @@ -23,4 +23,5 @@ executable = proven-ffi modules = Proven.Core , Proven.SafeUrl.Parser , Proven.FFI.SafeUrl + , Proven.SafeHeader.ByteKernel , Proven.FFI.Main diff --git a/proven.ipkg b/proven.ipkg index 6456dbec..c80e168c 100644 --- a/proven.ipkg +++ b/proven.ipkg @@ -138,6 +138,7 @@ modules = Proven -- SafeHeader , Proven.SafeHeader , Proven.SafeHeader.Types + , Proven.SafeHeader.ByteKernel , Proven.SafeHeader.Parser , Proven.SafeHeader.Proofs -- SafeCookie diff --git a/scripts/build-header-refc.sh b/scripts/build-header-refc.sh new file mode 100644 index 00000000..ae251c97 --- /dev/null +++ b/scripts/build-header-refc.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +# Build the small header kernel with its actual RefC SDK. No private dependency. +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +out=${1:-"$root/build/header-refc"} +idris=${IDRIS2:-idris2} +command -v "$idris" >/dev/null +[[ $("$idris" --version) == 'Idris 2, version 0.8.0-b6cbc25d5' ]] || { + echo 'FAIL: header ABI profile requires Idris2 b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b' >&2; exit 1; +} +[[ $(uname -sm) == 'Linux x86_64' ]] || { echo 'FAIL: only native Linux x86_64 is qualified' >&2; exit 1; } +mkdir -p "$out" +out=$(cd "$out" && pwd) +work=$(mktemp -d -t proven-header-refc.XXXXXXXX) +trap 'rm -rf -- "$work"' EXIT +mkdir -p "$work/src/Proven/SafeHeader" "$work/src/Proven/FFI" +cp "$root/src/Proven/SafeHeader/ByteKernel.idr" "$work/src/Proven/SafeHeader/" +cp "$root/src/Proven/FFI/HeaderKernelMain.idr" "$work/src/Proven/FFI/" +if ! (cd "$work"; "$idris" -Werror --total --source-dir src --codegen refc -o header-kernel src/Proven/FFI/HeaderKernelMain.idr) > "$out/build.log" 2>&1; then + cat "$out/build.log" >&2; exit 1 +fi +if grep -q '^Error:' "$out/build.log" || [[ ! -s "$work/build/exec/header-kernel.c" ]]; then + cat "$out/build.log" >&2; echo 'FAIL: no valid RefC output' >&2; exit 1 +fi +grep -q 'Proven_SafeHeader_ByteKernel_headerStep' "$work/build/exec/header-kernel.c" +(cd "$work"; printf ':m\n:q\n' | "$idris" --no-banner --source-dir src src/Proven/SafeHeader/ByteKernel.idr) > "$out/holes.log" 2>&1 +grep -q 'No holes' "$out/holes.log" +! grep -q '^Error:' "$out/holes.log" +prefix=$("$idris" --prefix) +mapfile -t runtimes < <(find "$prefix" -path '*/support/refc/runtime.h' -type f) +mapfile -t supports < <(find "$prefix" -path '*/support/c/idris_support.h' -type f) +mapfile -t support_libs < <(find "$prefix" -name libidris2_support.a -type f) +[[ ${#runtimes[@]} == 1 && ${#supports[@]} == 1 && ${#support_libs[@]} == 1 ]] || { + echo 'FAIL: expected one matching installed RefC SDK; ambiguous or missing SDK' >&2; exit 1; +} +runtime=$(dirname "${runtimes[0]}") +support=$(dirname "${supports[0]}") +test -s "$runtime/libidris2_refc.a" +mkdir -p "$out/refc" "$out/support" +cp "$runtime/"*.h "$out/refc/" +cp "$support/"*.h "$out/support/" +cp "$runtime/libidris2_refc.a" "${support_libs[0]}" "$out/" +cp "$work/build/exec/header-kernel.c" "$out/kernel.c" +{ + "$idris" --version + sha256sum "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/src/Proven/FFI/HeaderKernelMain.idr" "$out/kernel.c" "$out/"*.a "$out/refc/"*.h +} > "$out/identity.log" +printf 'RefC header kernel: %s\n' "$out" diff --git a/scripts/check-header-proofs.sh b/scripts/check-header-proofs.sh new file mode 100644 index 00000000..3cfa0dd9 --- /dev/null +++ b/scripts/check-header-proofs.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +sholes=${1:?usage: check-header-proofs.sh /path/to/sholes-git-checkout} +pin=c28993a73d1c78830c6d92f563fae638d22bb7f7 +for tool in agda lean coqc coqchk git; do command -v "$tool" >/dev/null; done +[[ $(agda --version) == 'Agda version 2.6.4.3' ]] +lean --version | grep -q 'version 4.15.0,' +[[ $(coqc --version | head -1) == 'The Rocq Prover, version 9.2' ]] +evidence=${EVIDENCE_DIR:-"$root/build/header-proof-evidence"} +mkdir -p "$evidence" +evidence=$(cd "$evidence" && pwd) +rm -f "$evidence/result.txt" +work=$(mktemp -d -t proven-header-proof.XXXXXXXX) +trap 'rm -rf -- "$work"' EXIT +# Only committed, pinned upstream proof sources enter the proof environment. +git -C "$sholes" archive "$pin" verification/proofs | tar -x -C "$work" +cp -R "$root/verification/header" "$work/header" +find "$work/header" -type f \( -name '*.agdai' -o -name '*.olean' -o -name '*.vo' \) -delete +base="$work/verification/proofs" +agda --safe --without-K --cubical-compatible --double-check --warning=error -i "$base/agda" -i "$work/header/agda" "$work/header/agda/ProvenHeader.agda" > "$evidence/agda.log" 2>&1 +agda --safe --cubical --double-check --warning=error -i "$base/agda" -i "$work/header/agda" -i "$work/header/cubical" "$work/header/cubical/ProvenHeaderCubical.agda" > "$evidence/cubical.log" 2>&1 +cp "$work/header/lean4/ProvenHeader.lean" "$base/lean4/" +(cd "$base/lean4"; lean -o Sholes/Core.olean Sholes/Core.lean) > "$evidence/lean-upstream.log" 2>&1 +(cd "$base/lean4"; LEAN_PATH="$base/lean4" lean -o ProvenHeader.olean ProvenHeader.lean) > "$evidence/lean.log" 2>&1 +[[ $(grep -c 'does not depend on any axioms' "$evidence/lean.log") == 4 ]] +! grep -Eq 'sorry|warning:|error:' "$evidence/lean.log" +cp "$work/header/coq/ProvenHeader.v" "$base/coq/" +(cd "$base/coq"; coqc Sholes_Core.v; coqc Sholes_Wire.v) > "$evidence/coq-upstream.log" 2>&1 +(cd "$base/coq"; coqc ProvenHeader.v) > "$evidence/coq.log" 2>&1 +[[ $(grep -c 'Closed under the global context' "$evidence/coq.log") == 4 ]] +(cd "$base/coq"; coqchk -silent ProvenHeader) > "$evidence/coqchk.log" 2>&1 + +# Explicitly false witnesses must be rejected, not skipped or admitted. +cat > "$work/Reject.agda" <<'AGDA' +{-# OPTIONS --safe --without-K --cubical-compatible #-} +module Reject where +open import Agda.Builtin.Bool +open import Agda.Builtin.Equality +bad : true ≡ false +bad = refl +AGDA +if agda --safe --cubical-compatible --double-check -i "$work" "$work/Reject.agda" > "$evidence/agda-reject.log" 2>&1; then exit 1; fi +grep -q 'true != false' "$evidence/agda-reject.log" +echo 'example : True = False := rfl' > "$work/Reject.lean" +if lean "$work/Reject.lean" > "$evidence/lean-reject.log" 2>&1; then exit 1; fi +grep -q 'error:' "$evidence/lean-reject.log" +echo 'Goal true = false. Proof. reflexivity. Qed.' > "$work/Reject.v" +if coqc "$work/Reject.v" > "$evidence/coq-reject.log" 2>&1; then exit 1; fi +grep -q 'Unable to unify' "$evidence/coq-reject.log" +{ + printf 'Sholes: %s\n' "$pin" + agda --version; lean --version; coqc --version + (cd "$work/header"; find . -type f \( -name '*.agda' -o -name '*.lean' -o -name '*.v' \) -exec sha256sum {} +) +} > "$evidence/identity.log" +echo PASS > "$evidence/result.txt" +printf 'PASS: compatible Agda; full Cubical; Lean axiom audit; Rocq assumption audit and coqchk; three rejecting controls.\nEvidence: %s\n' "$evidence" diff --git a/scripts/test-header-bridge.sh b/scripts/test-header-bridge.sh new file mode 100644 index 00000000..79daa850 --- /dev/null +++ b/scripts/test-header-bridge.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +zig=${ZIG:-zig} +command -v "$zig" >/dev/null +evidence=${EVIDENCE_DIR:-"$root/build/header-evidence"} +mkdir -p "$evidence" +evidence=$(cd "$evidence" && pwd) +rm -f "$evidence/result.txt" +find "$root/ffi/zig/src" -maxdepth 1 -name '*.zig' -type f -exec sha256sum {} + > "$evidence/inputs.sha256" +sha256sum "$root/ffi/zig/build_standalone.zig" "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/src/Proven/FFI/HeaderKernelMain.idr" >> "$evidence/inputs.sha256" +work=$(mktemp -d -t proven-header-check.XXXXXXXX) +trap 'rm -rf -- "$work"' EXIT +bash "$root/scripts/build-header-refc.sh" "$work/bundle" +cp "$work/bundle/identity.log" "$work/bundle/holes.log" "$evidence/" +cp "$work/bundle/build.log" "$evidence/kernel-build.log" +"$zig" version > "$evidence/zig-version.log" +cd "$root/ffi/zig" +"$zig" build test --build-file build_standalone.zig --summary all > "$evidence/unavailable.log" 2>&1 +"$zig" build test --build-file build_standalone.zig -Dheader-refc="$work/bundle" --summary all > "$evidence/refc.log" 2>&1 +"$zig" build --build-file build_standalone.zig -Dheader-refc="$work/bundle" > "$evidence/build.log" 2>&1 + +# A valid but wrong implementation must fail the exported-ABI tests. +# Rename only within this private generated bundle and supply an ABI-compatible +# mutant that always keeps the old state. Production sources remain untouched. +sed -i 's/Proven_SafeHeader_ByteKernel_headerStep/proven_original_headerStep/g' "$work/bundle/kernel.c" +cat >> "$work/bundle/kernel.c" <<'C' +void *Proven_SafeHeader_ByteKernel_headerStep(void *state, void *byte) { + (void)byte; + return state; +} +C +if "$zig" build test --build-file build_standalone.zig -Dheader-refc="$work/bundle" > "$evidence/mutant.log" 2>&1; then + echo 'FAIL: ABI tests accepted the always-clean kernel' >&2; exit 1 +fi +grep -q 'TestExpectedEqual' "$evidence/mutant.log" +sha256sum -c "$evidence/inputs.sha256" > "$evidence/input-recheck.log" +echo PASS > "$evidence/result.txt" +printf 'PASS: Idris totality/holes; unavailable backend refusal; actual RefC ABI; 256 single bytes; 65536 pairs; boundary corpus; rejecting mutant.\nEvidence: %s\n' "$evidence" diff --git a/src/Proven/FFI/HeaderKernelMain.idr b/src/Proven/FFI/HeaderKernelMain.idr new file mode 100644 index 00000000..bbe77b99 --- /dev/null +++ b/src/Proven/FFI/HeaderKernelMain.idr @@ -0,0 +1,12 @@ +-- SPDX-License-Identifier: MPL-2.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) +module Proven.FFI.HeaderKernelMain +import Proven.SafeHeader.ByteKernel + +-- Dynamic inputs retain the noinline, two-argument headerStep in generated C. +-- This executable is a code-generation root; the library never invokes main. +main : IO () +main = do + a <- getLine + b <- getLine + printLn (headerStep (cast (length a)) (cast (length b))) diff --git a/src/Proven/FFI/Main.idr b/src/Proven/FFI/Main.idr index 2a5fa86f..b8087666 100644 --- a/src/Proven/FFI/Main.idr +++ b/src/Proven/FFI/Main.idr @@ -6,6 +6,7 @@ module Proven.FFI.Main import Proven.FFI.SafeUrl +import Proven.SafeHeader.ByteKernel main : IO () main = do @@ -15,6 +16,8 @@ main = do -- Validation let v = proven_idris_url_is_valid "http://test.com" putStrLn (show v) + -- Retain the actual header kernel in the full RefC build as well. + putStrLn (show (headerStep (cast s1) (cast v))) -- Components let (_, _) = proven_idris_url_get_scheme "http://x" let (_, _) = proven_idris_url_get_host "http://x" diff --git a/src/Proven/SafeHeader/ByteKernel.idr b/src/Proven/SafeHeader/ByteKernel.idr new file mode 100644 index 00000000..e07617a5 --- /dev/null +++ b/src/Proven/SafeHeader/ByteKernel.idr @@ -0,0 +1,75 @@ +-- SPDX-License-Identifier: MPL-2.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) +module Proven.SafeHeader.ByteKernel + +%default total + +public export +isHeaderBreak : Bits8 -> Bool +isHeaderBreak b = b == 10 || b == 13 + +public export +encodeState : Bool -> Bits8 +encodeState False = 0 +encodeState True = 1 + +public export +nextState : Bool -> Bits8 -> Bool +nextState False b = isHeaderBreak b +nextState True _ = True + +||| A byte transition. State 0 means no forbidden byte has occurred; +||| state 1 means one has occurred. The ABI starts at 0 and only accepts 0/1. +||| Keep a callable RefC symbol; exporting a module name alone is not C export. +public export +%noinline +headerStep : Bits8 -> Bits8 -> Bits8 +headerStep state b = if state == 0 then encodeState (isHeaderBreak b) else state + +||| The concrete Bits8 kernel implements the typed Boolean state transition. +public export +stepEncodes : (state : Bool) -> (b : Bits8) -> + headerStep (encodeState state) b = encodeState (nextState state b) +stepEncodes False b = Refl +stepEncodes True b = Refl + +public export +scanTyped : Bool -> List Bits8 -> Bool +scanTyped state [] = state +scanTyped state (b :: bs) = scanTyped (nextState state b) bs + +public export +scanBytes : Bits8 -> List Bits8 -> Bits8 +scanBytes state [] = state +scanBytes state (b :: bs) = scanBytes (headerStep state b) bs + +public export +scanEncodes : (state : Bool) -> (bs : List Bits8) -> + scanBytes (encodeState state) bs = encodeState (scanTyped state bs) +scanEncodes state [] = Refl +scanEncodes state (b :: bs) = rewrite stepEncodes state b in scanEncodes (nextState state b) bs + +public export +seenStaysSeen : (bs : List Bits8) -> scanTyped True bs = True +seenStaysSeen [] = Refl +seenStaysSeen (b :: bs) = seenStaysSeen bs + +||| The streaming loop can split anywhere, including around NUL and CR/LF. +public export +scanAppend : (state : Bits8) -> (xs, ys : List Bits8) -> + scanBytes state (xs ++ ys) = scanBytes (scanBytes state xs) ys +scanAppend state [] ys = Refl +scanAppend state (x :: xs) ys = scanAppend (headerStep state x) xs ys + +public export +carriageReturnDetected : headerStep 0 13 = 1 +carriageReturnDetected = Refl +public export +lineFeedDetected : headerStep 0 10 = 1 +lineFeedDetected = Refl +public export +nulDoesNotTerminate : scanBytes 0 [0, 13] = 1 +nulDoesNotTerminate = Refl +public export +highByteIsData : scanBytes 0 [255, 0, 128] = 0 +highByteIsData = Refl diff --git a/verification/header/.gitignore b/verification/header/.gitignore new file mode 100644 index 00000000..59e1c7a7 --- /dev/null +++ b/verification/header/.gitignore @@ -0,0 +1,9 @@ +# Generated prover artifacts are never proof sources. +*.agdai +*.olean +*.ilean +*.vo +*.vos +*.vok +*.glob +.*.aux diff --git a/verification/header/agda/ProvenHeader.agda b/verification/header/agda/ProvenHeader.agda new file mode 100644 index 00000000..f9bca8e7 --- /dev/null +++ b/verification/header/agda/ProvenHeader.agda @@ -0,0 +1,55 @@ +{-# OPTIONS --safe --without-K --cubical-compatible #-} +-- SPDX-License-Identifier: CC-BY-SA-4.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +module ProvenHeader where +open import Sholes.Core public +open import Sholes.Wire public +open import Sholes.Streaming using (_++_) + +_∨_ : Bool → Bool → Bool +true ∨ b = true +false ∨ b = b + +boolEqual : Bool → Bool → Bool +boolEqual true true = true +boolEqual false false = true +boolEqual _ _ = false +boolEqual-sound : ∀ a b → boolEqual a b ≡ true → a ≡ b +boolEqual-sound true true p = refl +boolEqual-sound true false () +boolEqual-sound false true () +boolEqual-sound false false p = refl + +-- Numeric octets, with CR=13 and LF=10; no text decoding or sentinel. +isBreak : Octet → Bool +isBreak b = equal {256} b (at 10 {245}) ∨ equal {256} b (at 13 {242}) +classifier-check : every 256 (λ b → boolEqual (isBreak b) (hasBreak (decode b ∷ []))) ≡ true +classifier-check = refl +classifies : (b : Octet) → isBreak b ≡ hasBreak (decode b ∷ []) +classifies b = boolEqual-sound _ _ + (every-sound 256 (λ x → boolEqual (isBreak x) (hasBreak (decode x ∷ []))) classifier-check b) + +step : Bool → Octet → Bool +step true _ = true +step false b = isBreak b +scan : Bool → List Octet → Bool +scan s [] = s +scan s (b ∷ bs) = scan (step s b) bs + +-- This is an unbounded list proof, not the executable finite test corpus. +scan-correct : (s : Bool) (bs : List Octet) → scan s bs ≡ (s ∨ hasBreak (map decode bs)) +scan-correct true [] = refl +scan-correct true (b ∷ bs) = scan-correct true bs +scan-correct false [] = refl +scan-correct false (b ∷ bs) rewrite classifies b with decode b +... | cr = scan-correct true bs +... | lf = scan-correct true bs +... | plain _ = scan-correct false bs + +scan-append : (s : Bool) (xs ys : List Octet) → scan s (xs ++ ys) ≡ scan (scan s xs) ys +scan-append s [] ys = refl +scan-append s (x ∷ xs) ys = scan-append (step s x) xs ys + +scan-canon : (bs : List Octet) → scan false (canonWire bs) ≡ scan false bs +scan-canon bs = trans (scan-correct false (canonWire bs)) + (trans (wire-header-preserved bs) (sym (scan-correct false bs))) diff --git a/verification/header/coq/ProvenHeader.v b/verification/header/coq/ProvenHeader.v new file mode 100644 index 00000000..753de4b5 --- /dev/null +++ b/verification/header/coq/ProvenHeader.v @@ -0,0 +1,31 @@ +(* SPDX-License-Identifier: CC-BY-SA-4.0 + Copyright (c) 2026 Jonathan D.A. Jewell + Hand re-proof of ProvenHeader.agda. *) +From Stdlib Require Import List Bool. +Require Import Sholes_Core Sholes_Wire. +Import ListNotations Wire. +Module HeaderState. +Definition isBreak (b : Octet) : bool := equal 256 b (indexAt 10 245) || equal 256 b (indexAt 13 242). +Lemma classifier_check : every 256 (fun b => Bool.eqb (isBreak b) (hasBreak [decode b])) = true. +Proof. reflexivity. Qed. +Lemma classifies b : isBreak b = hasBreak [decode b]. +Proof. apply Bool.eqb_prop. exact (every_sound 256 _ classifier_check b). Qed. +Definition step (s : bool) (b : Octet) : bool := if s then true else isBreak b. +Fixpoint scan (s : bool) (bs : list Octet) : bool := + match bs with [] => s | b :: bs => scan (step s b) bs end. +Lemma scan_correct s bs : scan s bs = (s || hasBreak (Wire.map decode bs)). +Proof. revert s. induction bs as [|b bs IH]; intro s. + - destruct s; reflexivity. + - destruct s; simpl. + + exact (IH true). + + rewrite IH, classifies. destruct (decode b); reflexivity. +Qed. +Lemma scan_append s xs ys : scan s (xs ++ ys) = scan (scan s xs) ys. +Proof. revert s. induction xs as [|x xs IH]; intro s; simpl; [reflexivity|apply IH]. Qed. +Lemma scan_canon bs : scan false (canonWire bs) = scan false bs. +Proof. rewrite !scan_correct, wire_header_preserved. reflexivity. Qed. +Print Assumptions classifies. +Print Assumptions scan_correct. +Print Assumptions scan_append. +Print Assumptions scan_canon. +End HeaderState. diff --git a/verification/header/cubical/ProvenHeaderCubical.agda b/verification/header/cubical/ProvenHeaderCubical.agda new file mode 100644 index 00000000..4dfd1eda --- /dev/null +++ b/verification/header/cubical/ProvenHeaderCubical.agda @@ -0,0 +1,12 @@ +{-# OPTIONS --safe --cubical #-} +-- SPDX-License-Identifier: CC-BY-SA-4.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +module ProvenHeaderCubical where +open import ProvenHeader +open import Agda.Builtin.Cubical.Path renaming (_≡_ to _≡ᵖ_) +equality-path : ∀ {A : Set} {x y : A} → x ≡ y → x ≡ᵖ y +equality-path {x = x} refl i = x +scan-path : (s : Bool) (bs : List Octet) → scan s bs ≡ᵖ (s ∨ hasBreak (map decode bs)) +scan-path s bs = equality-path (scan-correct s bs) +canon-path : (bs : List Octet) → scan false (canonWire bs) ≡ᵖ scan false bs +canon-path bs = equality-path (scan-canon bs) diff --git a/verification/header/lean4/ProvenHeader.lean b/verification/header/lean4/ProvenHeader.lean new file mode 100644 index 00000000..be232aa3 --- /dev/null +++ b/verification/header/lean4/ProvenHeader.lean @@ -0,0 +1,41 @@ +-- SPDX-License-Identifier: CC-BY-SA-4.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +-- Hand re-proof of ProvenHeader.agda; no automatic witness transport claim. +import Sholes.Core +set_option warningAsError true +set_option maxRecDepth 8192 +set_option maxHeartbeats 8000000 +namespace ProvenHeader +open Sholes Sholes.Byte Sholes.Wire +def isBreak (b : Octet) : Bool := equal 256 b (indexAt 10 (extra := 245)) || equal 256 b (indexAt 13 (extra := 242)) +def boolEqual : Bool → Bool → Bool + | true, true => true | false, false => true | _, _ => false +theorem boolEqual_sound (a b : Bool) : boolEqual a b = true → a = b := by + cases a <;> cases b <;> intro h <;> first | rfl | cases h +theorem classifier_check : every 256 (fun b => boolEqual (isBreak b) (hasBreak [decode b])) = true := rfl +theorem classifies (b : Octet) : isBreak b = hasBreak [decode b] := + boolEqual_sound _ _ (every_sound 256 _ classifier_check b) +def step : Bool → Octet → Bool + | true, _ => true | false, b => isBreak b +def scan : Bool → List Octet → Bool + | s, [] => s | s, b :: bs => scan (step s b) bs +theorem scan_correct (s : Bool) (bs : List Octet) : scan s bs = (s || hasBreak (Sholes.Wire.map decode bs)) := by + induction bs generalizing s with + | nil => cases s <;> rfl + | cons b bs ih => + cases s with + | true => exact ih true + | false => + simp only [scan, step, ih, classifies, Sholes.Wire.map] + cases decode b <;> rfl +theorem scan_append (s : Bool) (xs ys : List Octet) : scan s (xs ++ ys) = scan (scan s xs) ys := by + induction xs generalizing s with + | nil => rfl + | cons x xs ih => exact ih (step s x) +theorem scan_canon (bs : List Octet) : scan false (canonWire bs) = scan false bs := by + rw [scan_correct, scan_correct, wire_header_preserved] +end ProvenHeader +#print axioms ProvenHeader.classifies +#print axioms ProvenHeader.scan_correct +#print axioms ProvenHeader.scan_append +#print axioms ProvenHeader.scan_canon From f807d91f84007ca1578941717d4086a137e5b5fa Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:11:49 +0000 Subject: [PATCH 02/24] Select compatible GHC explicitly in the theory replay runner --- .github/workflows/header-theory.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/header-theory.yml b/.github/workflows/header-theory.yml index 7ee88583..d40d5e77 100644 --- a/.github/workflows/header-theory.yml +++ b/.github/workflows/header-theory.yml @@ -32,14 +32,16 @@ jobs: ~/.cabal ~/.opam ~/.local/header-proof-tools - key: header-theory-ubuntu2404-agda2643-lean4150-rocq920-ocaml511-v1 + key: header-theory-ubuntu2404-systemghc-agda2643-lean4150-rocq920-ocaml511-${{ hashFiles('.github/workflows/header-theory.yml') }} - name: Build pinned proof tools if: steps.proof-cache.outputs.cache-hit != 'true' run: | set -euo pipefail mkdir -p "$HOME/.local/header-proof-tools/bin" cabal update - cabal v2-install Agda-2.6.4.3 --index-state=2026-11-16T00:00:00Z --jobs=2 --install-method=copy --installdir="$HOME/.local/header-proof-tools/bin" + # Hosted images also put a newer, incompatible GHC on PATH. + # Use the distribution compiler installed in the dependency step. + cabal v2-install Agda-2.6.4.3 --with-compiler=/usr/bin/ghc --with-hc-pkg=/usr/bin/ghc-pkg --index-state=2026-11-16T00:00:00Z --jobs=2 --install-method=copy --installdir="$HOME/.local/header-proof-tools/bin" opam init --bare --disable-sandboxing -y opam switch create header-proof ocaml-base-compiler.5.1.1 -y opam install --switch=header-proof coq.9.2.0 rocq-stdlib.9.2.0 -y @@ -57,6 +59,7 @@ jobs: eval "$(opam env --switch=header-proof --set-switch)" export PATH="$HOME/.local/header-proof-tools/bin:$HOME/.local/header-proof-tools/lean-4.15.0-linux/bin:$PATH" bash scripts/check-header-proofs.sh "$RUNNER_TEMP/sholes" + bash scripts/test-header-proof-diagnostics.sh "$RUNNER_TEMP/sholes" - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 if: always() with: From 418fce9fc31d41ccffbee7a456b3568a3c313651 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:12:36 +0000 Subject: [PATCH 03/24] Wire binding consumers to real RefC and enforce diagnostic failures --- .github/workflows/chapel-ci.yml | 30 ++++--------------- .github/workflows/d-ci.yml | 18 ++++------- .github/workflows/ocaml-ci.yml | 24 +++++---------- bindings/d/source/proven/safe_uuid.d | 4 +-- bindings/d/source/proven/safe_version.d | 4 +-- docs/adr/2026-11-16-header-refc-boundary.adoc | 4 +++ ffi/zig/build_standalone.zig | 5 ++-- scripts/build-header-refc.sh | 2 +- scripts/check-header-proofs.sh | 20 +++++++++---- scripts/test-header-bridge.sh | 4 +++ 10 files changed, 49 insertions(+), 66 deletions(-) diff --git a/.github/workflows/chapel-ci.yml b/.github/workflows/chapel-ci.yml index 55ea0697..61305126 100644 --- a/.github/workflows/chapel-ci.yml +++ b/.github/workflows/chapel-ci.yml @@ -119,7 +119,7 @@ jobs: chapel-build: runs-on: ubuntu-22.04 needs: detachability-guard - timeout-minutes: 20 + timeout-minutes: 45 steps: - name: Checkout proven uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 @@ -129,33 +129,13 @@ jobs: uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: version: ${{ env.ZIG_VERSION }} - - name: Build libproven.so (WIRED-subset fixture) + - name: Build libproven.so with the actual Idris header backend + uses: ./proven/ffi/zig/actions/refc-library + - name: Audit the library symbols working-directory: proven/ffi/zig - # NOTE: ffi/zig/build.zig depends on `idris2_zig_ffi` (relative - # path to a sibling estate repo). That dep is not resolvable - # from a fresh clone in CI (the same issue blocks zig-ffi.yml on - # main), and resolving it is out of scope for the Chapel pilot. - # All exports in src/main.zig are pure-Zig — none consume - # idris2_zig_ffi. We compile main.zig directly with - # `zig build-lib -dynamic -lc`, producing a libproven.so with - # the WIRED 5 symbols (proven_path_has_traversal, - # proven_header_has_crlf, proven_free_string, proven_version, - # proven_build_info). When proven#88 lands the real - # Idris-backed exports, this step swaps to - # `zig build -Dtarget=...`. run: | set -euo pipefail - mkdir -p zig-out/lib - zig build-lib -dynamic -O ReleaseSafe \ - -fPIC \ - --name proven \ - -femit-bin=zig-out/lib/libproven.so \ - src/main.zig -lc - ls -la zig-out/lib/ - nm -D --defined-only zig-out/lib/libproven.so \ - | awk '$2 ~ /^[TWV]$/ { print $3 }' \ - | grep -E '^(proven_path_has_traversal|proven_header_has_crlf|proven_free_string|proven_version|proven_build_info)$' \ - | sort -u | tee /tmp/wired-exports.txt + nm -D --defined-only zig-out/lib/libproven.so | awk '$2 ~ /^[TWV]$/ { print $3 }' | grep -E '^(proven_path_has_traversal|proven_header_has_crlf|proven_free_string|proven_version|proven_build_info)$' | sort -u | tee /tmp/wired-exports.txt test "$(wc -l < /tmp/wired-exports.txt)" -eq 5 - name: Install Chapel ${{ env.CHAPEL_VERSION }} (SHA-pinned .deb) run: | diff --git a/.github/workflows/d-ci.yml b/.github/workflows/d-ci.yml index a1021189..72738063 100644 --- a/.github/workflows/d-ci.yml +++ b/.github/workflows/d-ci.yml @@ -68,7 +68,7 @@ jobs: d-build: runs-on: ubuntu-22.04 needs: detachability-guard - timeout-minutes: 20 + timeout-minutes: 45 steps: - name: Checkout proven uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 @@ -78,20 +78,14 @@ jobs: uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: version: ${{ env.ZIG_VERSION }} - - name: Build libproven.so (WIRED-subset fixture) + - name: Build libproven.so with the actual Idris header backend + uses: ./proven/ffi/zig/actions/refc-library + - name: Audit the library symbols working-directory: proven/ffi/zig run: | set -euo pipefail - mkdir -p zig-out/lib - zig build-lib -dynamic -O ReleaseSafe \ - -fPIC \ - --name proven \ - -femit-bin=zig-out/lib/libproven.so \ - src/main.zig -lc - nm -D --defined-only zig-out/lib/libproven.so \ - | awk '$2 ~ /^[TWV]$/ { print $3 }' \ - | grep -E '^(proven_path_has_traversal|proven_header_has_crlf|proven_free_string|proven_version|proven_build_info)$' \ - | sort -u | tee /tmp/wired-exports.txt + nm -D --defined-only zig-out/lib/libproven.so | awk '$2 ~ /^[TWV]$/ { print $3 }' | grep -E '^(proven_path_has_traversal|proven_header_has_crlf|proven_free_string|proven_version|proven_build_info)$' | sort -u | tee /tmp/wired-exports.txt + test "$(wc -l < /tmp/wired-exports.txt)" -eq 5 - name: Setup D compiler (LDC) uses: dlang-community/setup-dlang@0a7469b93f791d83f30932c6fd105796c6966e20 # v2.0.0 with: diff --git a/.github/workflows/ocaml-ci.yml b/.github/workflows/ocaml-ci.yml index 7a46fab6..372f918c 100644 --- a/.github/workflows/ocaml-ci.yml +++ b/.github/workflows/ocaml-ci.yml @@ -69,7 +69,7 @@ jobs: ocaml-build: runs-on: ubuntu-22.04 needs: detachability-guard - timeout-minutes: 20 + timeout-minutes: 45 steps: - name: Checkout proven uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 @@ -79,24 +79,16 @@ jobs: uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: version: ${{ env.ZIG_VERSION }} - - name: Build libproven.so (WIRED-subset fixture) + - name: Build libproven.so with the actual Idris header backend + uses: ./proven/ffi/zig/actions/refc-library + - name: Audit the library symbols working-directory: proven/ffi/zig run: | set -euo pipefail - mkdir -p zig-out/lib - zig build-lib -dynamic -O ReleaseSafe \ - -fPIC \ - --name proven \ - -femit-bin=zig-out/lib/libproven.so \ - src/main.zig -lc - nm -D --defined-only zig-out/lib/libproven.so \ - | awk '$2 ~ /^[TWV]$/ { print $3 }' \ - | grep -E '^(proven_path_has_traversal|proven_header_has_crlf|proven_free_string|proven_version|proven_build_info)$' \ - | sort -u | tee /tmp/wired-exports.txt - # Wait! OCaml might need more for the full lib integration - # but for smoke we just need these. + nm -D --defined-only zig-out/lib/libproven.so | awk '$2 ~ /^[TWV]$/ { print $3 }' | grep -E '^(proven_path_has_traversal|proven_header_has_crlf|proven_free_string|proven_version|proven_build_info)$' | sort -u | tee /tmp/wired-exports.txt + test "$(wc -l < /tmp/wired-exports.txt)" -eq 5 - name: Setup OCaml ${{ env.OCAML_VERSION }} - uses: ocaml/setup-ocaml@4462cc269f8d5e869a19c676450637119e71ec60 # v3 + uses: ocaml/setup-ocaml@ff66428f72b64472e749b22e0ad4dded4b2ddca6 # v3 with: ocaml-compiler: ${{ env.OCAML_VERSION }} dune-cache: true @@ -151,7 +143,7 @@ jobs: .github/workflows/ocaml-ci.yml sparse-checkout-cone-mode: false - name: Setup OCaml ${{ env.OCAML_VERSION }} - uses: ocaml/setup-ocaml@4462cc269f8d5e869a19c676450637119e71ec60 # v3 + uses: ocaml/setup-ocaml@ff66428f72b64472e749b22e0ad4dded4b2ddca6 # v3 with: ocaml-compiler: ${{ env.OCAML_VERSION }} - name: Install dependencies diff --git a/bindings/d/source/proven/safe_uuid.d b/bindings/d/source/proven/safe_uuid.d index fd884f15..64904ec2 100644 --- a/bindings/d/source/proven/safe_uuid.d +++ b/bindings/d/source/proven/safe_uuid.d @@ -67,12 +67,12 @@ struct UuidResult string error; bool ok; - static UuidResult success(UUID uuid) + static UuidResult success(UUID uuid) nothrow { return UuidResult(uuid, "", true); } - static UuidResult failure(string error) + static UuidResult failure(string error) nothrow { return UuidResult(UUID.nil, error, false); } diff --git a/bindings/d/source/proven/safe_version.d b/bindings/d/source/proven/safe_version.d index 5c71e612..0dd97143 100644 --- a/bindings/d/source/proven/safe_version.d +++ b/bindings/d/source/proven/safe_version.d @@ -59,12 +59,12 @@ struct VersionResult string error; bool ok; - static VersionResult success(SemVer v) + static VersionResult success(SemVer v) nothrow { return VersionResult(v, "", true); } - static VersionResult failure(string error) + static VersionResult failure(string error) nothrow { return VersionResult(SemVer.init, error, false); } diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc index 24863ac2..ba36b20b 100644 --- a/docs/adr/2026-11-16-header-refc-boundary.adoc +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -55,6 +55,10 @@ pairs, null/empty spans, embedded NUL, non-UTF-8, explicit prefix lengths and an 8,192-byte boundary case. An ABI-compatible always-clean generated-C mutant must fail assertions, not merely fail compilation. The unavailable profile must return -99. `header-refc.yml` runs this gate on every PR and main push. +The gate also builds `libproven.so` and compiles a caller against the public C +header to exercise result layout, status codes and exact-byte spans. Chapel, +D and OCaml CI obtain this real shared-library profile through +`ffi/zig/actions/refc-library`; their detached jobs consume that artifact. Evidence in `build/header-evidence` records versions, source/SDK hashes and positive/negative logs. diff --git a/ffi/zig/build_standalone.zig b/ffi/zig/build_standalone.zig index 6c3916a9..e24fc367 100644 --- a/ffi/zig/build_standalone.zig +++ b/ffi/zig/build_standalone.zig @@ -32,6 +32,7 @@ fn attachHeader(b: *std.Build, mod: *std.Build.Module, path: []const u8) void { pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const shared = b.option(bool, "shared", "Build libproven.so for C ABI consumers") orelse false; const header = b.option([]const u8, "header-refc", "Absolute bundle from scripts/build-header-refc.sh; absent means header operations return -99"); if (header != null and (!target.query.isNative() or target.result.os.tag != .linux or target.result.cpu.arch != .x86_64 or target.result.abi != .gnu)) @panic("header RefC profile currently supports native Linux x86_64 GNU only"); @@ -51,9 +52,9 @@ pub fn build(b: *std.Build) void { if (header) |path| attachHeader(b, proven_mod, path); const lib = b.addLibrary(.{ - .name = "proven_ffi", + .name = if (shared) "proven" else "proven_ffi", .root_module = proven_mod, - .linkage = .static, + .linkage = if (shared) .dynamic else .static, }); b.installArtifact(lib); diff --git a/scripts/build-header-refc.sh b/scripts/build-header-refc.sh index ae251c97..848d7071 100644 --- a/scripts/build-header-refc.sh +++ b/scripts/build-header-refc.sh @@ -27,7 +27,7 @@ fi grep -q 'Proven_SafeHeader_ByteKernel_headerStep' "$work/build/exec/header-kernel.c" (cd "$work"; printf ':m\n:q\n' | "$idris" --no-banner --source-dir src src/Proven/SafeHeader/ByteKernel.idr) > "$out/holes.log" 2>&1 grep -q 'No holes' "$out/holes.log" -! grep -q '^Error:' "$out/holes.log" +if grep -q '^Error:' "$out/holes.log"; then cat "$out/holes.log" >&2; exit 1; fi prefix=$("$idris" --prefix) mapfile -t runtimes < <(find "$prefix" -path '*/support/refc/runtime.h' -type f) mapfile -t supports < <(find "$prefix" -path '*/support/c/idris_support.h' -type f) diff --git a/scripts/check-header-proofs.sh b/scripts/check-header-proofs.sh index 3cfa0dd9..8d790cfa 100644 --- a/scripts/check-header-proofs.sh +++ b/scripts/check-header-proofs.sh @@ -5,16 +5,22 @@ set -euo pipefail root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) sholes=${1:?usage: check-header-proofs.sh /path/to/sholes-git-checkout} pin=c28993a73d1c78830c6d92f563fae638d22bb7f7 -for tool in agda lean coqc coqchk git; do command -v "$tool" >/dev/null; done -[[ $(agda --version) == 'Agda version 2.6.4.3' ]] -lean --version | grep -q 'version 4.15.0,' -[[ $(coqc --version | head -1) == 'The Rocq Prover, version 9.2' ]] evidence=${EVIDENCE_DIR:-"$root/build/header-proof-evidence"} mkdir -p "$evidence" evidence=$(cd "$evidence" && pwd) rm -f "$evidence/result.txt" +work='' +finish() { + result=$? + if [[ $result -ne 0 ]]; then printf 'FAIL: exit %s\n' "$result" > "$evidence/result.txt"; fi + if [[ -n "$work" ]]; then rm -rf -- "$work"; fi +} +trap finish EXIT +for tool in agda lean coqc coqchk git; do command -v "$tool" >/dev/null; done +[[ $(agda --version) == 'Agda version 2.6.4.3' ]] +lean --version | grep -q 'version 4.15.0,' +[[ $(coqc --version | head -1) == 'The Rocq Prover, version 9.2' ]] work=$(mktemp -d -t proven-header-proof.XXXXXXXX) -trap 'rm -rf -- "$work"' EXIT # Only committed, pinned upstream proof sources enter the proof environment. git -C "$sholes" archive "$pin" verification/proofs | tar -x -C "$work" cp -R "$root/verification/header" "$work/header" @@ -26,7 +32,9 @@ cp "$work/header/lean4/ProvenHeader.lean" "$base/lean4/" (cd "$base/lean4"; lean -o Sholes/Core.olean Sholes/Core.lean) > "$evidence/lean-upstream.log" 2>&1 (cd "$base/lean4"; LEAN_PATH="$base/lean4" lean -o ProvenHeader.olean ProvenHeader.lean) > "$evidence/lean.log" 2>&1 [[ $(grep -c 'does not depend on any axioms' "$evidence/lean.log") == 4 ]] -! grep -Eq 'sorry|warning:|error:' "$evidence/lean.log" +if grep -Eq 'sorry|warning:|error:' "$evidence/lean.log"; then + echo 'FAIL: Lean diagnostic audit' >&2; exit 1 +fi cp "$work/header/coq/ProvenHeader.v" "$base/coq/" (cd "$base/coq"; coqc Sholes_Core.v; coqc Sholes_Wire.v) > "$evidence/coq-upstream.log" 2>&1 (cd "$base/coq"; coqc ProvenHeader.v) > "$evidence/coq.log" 2>&1 diff --git a/scripts/test-header-bridge.sh b/scripts/test-header-bridge.sh index 79daa850..70895453 100644 --- a/scripts/test-header-bridge.sh +++ b/scripts/test-header-bridge.sh @@ -11,6 +11,7 @@ evidence=$(cd "$evidence" && pwd) rm -f "$evidence/result.txt" find "$root/ffi/zig/src" -maxdepth 1 -name '*.zig' -type f -exec sha256sum {} + > "$evidence/inputs.sha256" sha256sum "$root/ffi/zig/build_standalone.zig" "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/src/Proven/FFI/HeaderKernelMain.idr" >> "$evidence/inputs.sha256" +sha256sum "$root/tests/ffi/header_byte_bridge.c" "$root/bindings/c/include/proven.h" >> "$evidence/inputs.sha256" work=$(mktemp -d -t proven-header-check.XXXXXXXX) trap 'rm -rf -- "$work"' EXIT bash "$root/scripts/build-header-refc.sh" "$work/bundle" @@ -21,6 +22,9 @@ cd "$root/ffi/zig" "$zig" build test --build-file build_standalone.zig --summary all > "$evidence/unavailable.log" 2>&1 "$zig" build test --build-file build_standalone.zig -Dheader-refc="$work/bundle" --summary all > "$evidence/refc.log" 2>&1 "$zig" build --build-file build_standalone.zig -Dheader-refc="$work/bundle" > "$evidence/build.log" 2>&1 +"$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 # A valid but wrong implementation must fail the exported-ABI tests. # Rename only within this private generated bundle and supply an ABI-compatible From 377a4d59162df0520b347057875c8e9658aee099 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:12:59 +0000 Subject: [PATCH 04/24] Add the RefC consumer build action and diagnostic regression controls --- ffi/zig/actions/refc-library/action.yml | 38 ++++++++++++++++++++++++ scripts/test-header-proof-diagnostics.sh | 37 +++++++++++++++++++++++ tests/ffi/header_byte_bridge.c | 19 ++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 ffi/zig/actions/refc-library/action.yml create mode 100644 scripts/test-header-proof-diagnostics.sh create mode 100644 tests/ffi/header_byte_bridge.c diff --git a/ffi/zig/actions/refc-library/action.yml b/ffi/zig/actions/refc-library/action.yml new file mode 100644 index 00000000..ea15ea4d --- /dev/null +++ b/ffi/zig/actions/refc-library/action.yml @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +name: Build the real header RefC library +description: Build native libproven.so with the Idris header backend; other legacy exports remain outside this qualification. +runs: + using: composite + steps: + - name: Install RefC prerequisites + shell: bash + run: sudo apt-get update && sudo apt-get install -y chezscheme libgmp-dev + - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 + id: refc-cache + with: + path: ~/.idris2 + key: bindings-header-refc-ubuntu2204-${{ runner.arch }}-b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b + - name: Build pinned compiler and matching SDK + if: steps.refc-cache.outputs.cache-hit != 'true' + shell: bash + run: | + set -euo pipefail + ref=b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b + git init "$RUNNER_TEMP/idris-header" + git -C "$RUNNER_TEMP/idris-header" remote add origin https://github.com/idris-lang/Idris2.git + git -C "$RUNNER_TEMP/idris-header" fetch --depth 1 origin "$ref" + git -C "$RUNNER_TEMP/idris-header" checkout --detach FETCH_HEAD + test "$(git -C "$RUNNER_TEMP/idris-header" rev-parse HEAD)" = "$ref" + cd "$RUNNER_TEMP/idris-header" + make bootstrap SCHEME=chezscheme + make install + - name: Build the library consumed by detached binding tests + shell: bash + run: | + set -euo pipefail + cd "$GITHUB_ACTION_PATH/../../../.." + IDRIS2="$HOME/.idris2/bin/idris2" bash scripts/build-header-refc.sh "$RUNNER_TEMP/header-refc" + cd ffi/zig + zig build --build-file build_standalone.zig -Dshared=true -Doptimize=ReleaseSafe -Dheader-refc="$RUNNER_TEMP/header-refc" + test -s zig-out/lib/libproven.so diff --git a/scripts/test-header-proof-diagnostics.sh b/scripts/test-header-proof-diagnostics.sh new file mode 100644 index 00000000..60dc7a98 --- /dev/null +++ b/scripts/test-header-proof-diagnostics.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +# Exercise the gate with a real compiler that emits an extra bad diagnostic. +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +sholes=${1:?usage: test-header-proof-diagnostics.sh /path/to/sholes} +real_lean=$(command -v lean) +evidence=${EVIDENCE_DIR:-"$root/build/header-proof-evidence/diagnostics"} +mkdir -p "$evidence" +evidence=$(cd "$evidence" && pwd) +work=$(mktemp -d -t proven-header-diagnostics.XXXXXXXX) +trap 'rm -rf -- "$work"' EXIT +cat > "$work/lean" <<'WRAPPER' +#!/usr/bin/env bash +set -euo pipefail +if [[ ${HEADER_BAD_VERSION:-0} == 1 && ${1:-} == --version ]]; then + echo 'Lean incompatible test version'; exit 0 +fi +"$HEADER_REAL_LEAN" "$@" +for arg in "$@"; do + if [[ $arg == ProvenHeader.lean ]]; then echo 'warning: injected gate regression control'; fi +done +WRAPPER +chmod +x "$work/lean" +if PATH="$work:$PATH" HEADER_REAL_LEAN="$real_lean" EVIDENCE_DIR="$evidence/warning" \ + bash "$root/scripts/check-header-proofs.sh" "$sholes" > "$evidence/warning-driver.log" 2>&1; then + echo 'FAIL: gate accepted a compiler warning' >&2; exit 1 +fi +grep -q 'FAIL: Lean diagnostic audit' "$evidence/warning-driver.log" +grep -q '^FAIL:' "$evidence/warning/result.txt" +if PATH="$work:$PATH" HEADER_REAL_LEAN="$real_lean" HEADER_BAD_VERSION=1 EVIDENCE_DIR="$evidence/version" \ + bash "$root/scripts/check-header-proofs.sh" "$sholes" > "$evidence/version-driver.log" 2>&1; then + echo 'FAIL: gate accepted an incompatible compiler' >&2; exit 1 +fi +grep -q '^FAIL:' "$evidence/version/result.txt" +echo 'PASS: successful compilation with a warning is rejected; early tool-profile failure leaves a failure receipt' diff --git a/tests/ffi/header_byte_bridge.c b/tests/ffi/header_byte_bridge.c new file mode 100644 index 00000000..bfaf5bda --- /dev/null +++ b/tests/ffi/header_byte_bridge.c @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: MPL-2.0 + * Copyright (c) 2026 Jonathan D.A. Jewell + * Compile against the public C header and call the real shared-library ABI. + */ +#include "proven.h" + +int main(void) { + const uint8_t bytes[] = {255, 0, 13, 128}; + for (size_t len = 0; len <= sizeof bytes; ++len) { + ProvenBoolResult r = proven_header_has_crlf(bytes, len); + if (r.status != 0 || r.value != (len >= 3)) return 1; + } + const uint8_t lf[] = {10}; + ProvenBoolResult r = proven_header_has_crlf(lf, sizeof lf); + if (r.status != 0 || !r.value) return 2; + r = proven_header_has_crlf(NULL, 0); + if (r.status != -1 || r.value) return 3; + return 0; +} From 5c9a0aa556969951d208a729f70b9c54e5d65464 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:47:20 +0000 Subject: [PATCH 05/24] Exercise raw header spans in Chapel and D consumers --- .github/workflows/d-ci.yml | 1 + bindings/chapel/smoke/hello_smoke.chpl | 13 +++++++++++++ bindings/chapel/src/SafeHeader.chpl | 11 +++++------ bindings/chapel/tests/TestFfiContract.chpl | 7 +++++-- bindings/chapel/tests/TestSafeHeader.chpl | 12 ++++++------ bindings/d/smoke/hello_smoke.d | 14 ++++++++++++++ 6 files changed, 44 insertions(+), 14 deletions(-) diff --git a/.github/workflows/d-ci.yml b/.github/workflows/d-ci.yml index 72738063..943f9a38 100644 --- a/.github/workflows/d-ci.yml +++ b/.github/workflows/d-ci.yml @@ -195,6 +195,7 @@ jobs: PROVEN_LIB_PATH: ${{ github.workspace }}/lib PROVEN_INCLUDE_PATH: ${{ github.workspace }}/bindings/c/include LD_LIBRARY_PATH: ${{ github.workspace }}/lib + LIBRARY_PATH: ${{ github.workspace }}/lib run: dub test d-detachable-build: runs-on: ubuntu-22.04 diff --git a/bindings/chapel/smoke/hello_smoke.chpl b/bindings/chapel/smoke/hello_smoke.chpl index 1e44297b..f68f4f51 100644 --- a/bindings/chapel/smoke/hello_smoke.chpl +++ b/bindings/chapel/smoke/hello_smoke.chpl @@ -38,6 +38,19 @@ proc main(): int { writeln("FAIL: SafeHeader.hasCrlf() did not return some(true)"); return 1; } + // Lone breaks and embedded NUL must survive Chapel's pointer/length bridge. + for value in ["\r", "\n", "a\x00\rZ"] { + const r = SafeHeader.hasCrlf(value); + if !r.present || !r.value { + writeln("FAIL: header byte-span counterexample was accepted"); + return 1; + } + } + const noBreak = SafeHeader.hasCrlf("a\x00Z"); + if !noBreak.present || noBreak.value { + writeln("FAIL: embedded NUL without a break was rejected"); + return 1; + } // Library identification (read-only, no allocation; verifies symbol table). const ver = libraryVersion(); diff --git a/bindings/chapel/src/SafeHeader.chpl b/bindings/chapel/src/SafeHeader.chpl index f0ce7a5f..f97ad6c0 100644 --- a/bindings/chapel/src/SafeHeader.chpl +++ b/bindings/chapel/src/SafeHeader.chpl @@ -3,10 +3,9 @@ // // SafeHeader - HTTP header safety via libproven FFI. // -// All computation is performed in the libproven C ABI (currently a Zig -// implementation under ffi/zig/src/main.zig; the Idris2 backing for this -// primitive lands as part of the proven#88 export ramp). This module is -// a thin wrapper; it does NOT reimplement any logic. +// The qualified libproven profile calls the Idris byte kernel through RefC. +// A library built without that backend returns -99, mapped to absent below. +// This module only marshals the byte span and status. // // Status: WIRED. proven_header_has_crlf is exported by libproven 0.9.0 // and verified by chapel-symbol-audit. @@ -16,14 +15,14 @@ module SafeHeader { public use LibProven; /** - * Check whether a header value contains a CRLF injection sequence ("\r\n"). + * Check whether a header value contains either CR or LF, including lone bytes. * * CRLF injection allows attackers to inject additional HTTP headers or * begin a response body when user input is reflected into a header. * * :arg value: Candidate header value. * :returns: ``absent(bool)`` on FFI error; otherwise ``some(true)`` if - * CRLF found, ``some(false)`` if safe. + * CR or LF found, ``some(false)`` if neither occurs. */ proc hasCrlf(value: string): Maybe(bool) { var (ptr, len) = toCBytes(value); diff --git a/bindings/chapel/tests/TestFfiContract.chpl b/bindings/chapel/tests/TestFfiContract.chpl index de87d1a7..93ac14d3 100644 --- a/bindings/chapel/tests/TestFfiContract.chpl +++ b/bindings/chapel/tests/TestFfiContract.chpl @@ -73,8 +73,11 @@ proc main(): int { checkHeader("Bearer abc.def", false); checkHeader("value\r\nX-Admin: true", true); checkHeader("trailing\r\n", true); - checkHeader("bare-CR\r", false); - checkHeader("bare-LF\n", false); + checkHeader("bare-CR\r", true); + checkHeader("bare-LF\n", true); + checkHeader("reversed\n\r", true); + checkHeader("a\x00\rZ", true); + checkHeader("a\x00Z", false); writeln("TestFfiContract: OK"); return 0; diff --git a/bindings/chapel/tests/TestSafeHeader.chpl b/bindings/chapel/tests/TestSafeHeader.chpl index 6711bf52..74302377 100644 --- a/bindings/chapel/tests/TestSafeHeader.chpl +++ b/bindings/chapel/tests/TestSafeHeader.chpl @@ -34,20 +34,20 @@ proc main(): int { assert(r6.value == true, "hasCrlf(leading-CRLF): expected true"); // ------- hasCrlf: boundary cases -------------------------------------- - // CR alone (no LF) is NOT a CRLF injection. + // The Idris contract detects either CR or LF, even without a pair. const r7 = SafeHeader.hasCrlf("bare-CR\r"); assert(r7.present, "hasCrlf(bare-CR): FFI error"); - assert(r7.value == false, "hasCrlf(bare-CR): expected false"); + assert(r7.value == true, "hasCrlf(bare-CR): expected true"); - // LF alone (no CR) is also not the CRLF sequence. + // LF alone is a forbidden header line break too. const r8 = SafeHeader.hasCrlf("bare-LF\n"); assert(r8.present, "hasCrlf(bare-LF): FFI error"); - assert(r8.value == false, "hasCrlf(bare-LF): expected false"); + assert(r8.value == true, "hasCrlf(bare-LF): expected true"); - // Reversed (LF then CR) is not CRLF. + // Order is immaterial: both bytes are header line breaks. const r9 = SafeHeader.hasCrlf("reversed\n\r"); assert(r9.present, "hasCrlf(reversed-LFCR): FFI error"); - assert(r9.value == false, "hasCrlf(reversed-LFCR): expected false"); + assert(r9.value == true, "hasCrlf(reversed-LFCR): expected true"); writeln("TestSafeHeader: OK"); return 0; diff --git a/bindings/d/smoke/hello_smoke.d b/bindings/d/smoke/hello_smoke.d index 4335416a..ff7122c2 100644 --- a/bindings/d/smoke/hello_smoke.d +++ b/bindings/d/smoke/hello_smoke.d @@ -7,6 +7,11 @@ import std.stdio; import std.process; import proven; +import proven.ffi : ProvenBoolResult; + +// The D package has no high-level SafeHeader wrapper yet. Exercise its C ABI +// directly using the package's actual result layout; do not duplicate policy. +extern(C) ProvenBoolResult proven_header_has_crlf(const(ubyte)* ptr, size_t len) nothrow; int main() { // SafePath.hasTraversal - happy + detection paths. @@ -22,6 +27,15 @@ int main() { return 1; } + const string[] headers = ["application/json", "\r", "\n", "a\0\rZ", "\xFF\0\x80"]; + const bool[] expected = [false, true, true, true, false]; + foreach (i, value; headers) { + auto r = proven_header_has_crlf(cast(const(ubyte)*) value.ptr, value.length); + if (r.status != 0 || r.value != expected[i]) { + writeln("FAIL: header C ABI byte-span case ", i); + return 1; + } + } writeln("hello_smoke: OK (libproven ", VERSION, ")"); return 0; } From 24fd48e954b7c124a195412a35c9c0be6f54990b Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 09:52:07 +0000 Subject: [PATCH 06/24] Cache Agda runtime data and expose actual OCaml ABI blockers --- .github/workflows/header-theory.yml | 2 ++ .github/workflows/ocaml-ci.yml | 8 ++++---- bindings/ocaml/lib/dune | 2 +- bindings/ocaml/lib/proven.ml | 2 +- bindings/ocaml/lib/proven_ffi.ml | 13 +++---------- bindings/ocaml/smoke/dune | 6 ++++++ bindings/ocaml/smoke/hello_smoke.ml | 9 +++++++++ 7 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 bindings/ocaml/smoke/dune diff --git a/.github/workflows/header-theory.yml b/.github/workflows/header-theory.yml index d40d5e77..e3b54836 100644 --- a/.github/workflows/header-theory.yml +++ b/.github/workflows/header-theory.yml @@ -30,6 +30,8 @@ jobs: with: path: | ~/.cabal + ~/.local/state/cabal + ~/.cache/cabal ~/.opam ~/.local/header-proof-tools key: header-theory-ubuntu2404-systemghc-agda2643-lean4150-rocq920-ocaml511-${{ hashFiles('.github/workflows/header-theory.yml') }} diff --git a/.github/workflows/ocaml-ci.yml b/.github/workflows/ocaml-ci.yml index 372f918c..fda8060b 100644 --- a/.github/workflows/ocaml-ci.yml +++ b/.github/workflows/ocaml-ci.yml @@ -93,14 +93,14 @@ jobs: ocaml-compiler: ${{ env.OCAML_VERSION }} dune-cache: true - name: Install dependencies - run: opam install dune ctypes ctypes-foreign + run: opam install --yes dune ctypes.0.24.0 ctypes-foreign.0.24.0 alcotest - name: OCaml build working-directory: proven/bindings/ocaml env: PROVEN_LIB_PATH: ${{ github.workspace }}/proven/ffi/zig/zig-out/lib run: | set -euo pipefail - dune build + opam exec -- dune build - name: Upload libproven.so artefact uses: actions/upload-artifact@6f51ac03b9356f520e9adb1b1b7802705f340c2b # v4 with: @@ -147,7 +147,7 @@ jobs: with: ocaml-compiler: ${{ env.OCAML_VERSION }} - name: Install dependencies - run: opam install dune ctypes ctypes-foreign + run: opam install --yes dune ctypes.0.24.0 ctypes-foreign.0.24.0 alcotest - name: Install just uses: extractions/setup-just@dd310ad5a97d8e7b41793f8ef055398d51ad4de6 # v2 with: @@ -164,7 +164,7 @@ jobs: PROVEN_INCLUDE_PATH: ${{ github.workspace }}/bindings/c/include LD_LIBRARY_PATH: ${{ github.workspace }}/lib run: | - dune build smoke/hello_smoke.exe + opam exec -- dune build smoke/hello_smoke.exe ./_build/default/smoke/hello_smoke.exe ocaml-detachable-build: runs-on: ubuntu-22.04 diff --git a/bindings/ocaml/lib/dune b/bindings/ocaml/lib/dune index 16473e73..57a7bb72 100644 --- a/bindings/ocaml/lib/dune +++ b/bindings/ocaml/lib/dune @@ -4,5 +4,5 @@ (library (name proven) (public_name proven) - (libraries ctypes ctypes.foreign) + (libraries ctypes ctypes-foreign) (modules proven proven_ffi)) diff --git a/bindings/ocaml/lib/proven.ml b/bindings/ocaml/lib/proven.ml index 69bd36fe..09cad8b3 100644 --- a/bindings/ocaml/lib/proven.ml +++ b/bindings/ocaml/lib/proven.ml @@ -195,7 +195,7 @@ module SafeString = struct let p = cstring_of_string s in unwrap_string_result (F.proven_string_escape_sql p (Unsigned.Size_t.of_int (String.length s))) - (** Escape a string for HTML (< > & " '). *) + (** Escape HTML angle brackets, ampersands and quotation marks. *) let escape_html (s : string) : string option = let p = cstring_of_string s in unwrap_string_result (F.proven_string_escape_html p (Unsigned.Size_t.of_int (String.length s))) diff --git a/bindings/ocaml/lib/proven_ffi.ml b/bindings/ocaml/lib/proven_ffi.ml index dc7fe0f0..1d859520 100644 --- a/bindings/ocaml/lib/proven_ffi.ml +++ b/bindings/ocaml/lib/proven_ffi.ml @@ -280,55 +280,48 @@ let () = seal retry_config (* Opaque pointer types for stateful objects *) (* -------------------------------------------------------------------------- *) +(* Keep pointee types incomplete. Sealing an empty C struct is unsupported; + these declarations are used only behind pointers, never allocated here. *) + (** Opaque handle for BoundedBuffer. *) type bounded_buffer let bounded_buffer : bounded_buffer structure typ = structure "BoundedBuffer" -let () = seal bounded_buffer (** Opaque handle for RateLimiter. *) type rate_limiter let rate_limiter : rate_limiter structure typ = structure "RateLimiter" -let () = seal rate_limiter (** Opaque handle for CircuitBreaker. *) type circuit_breaker let circuit_breaker : circuit_breaker structure typ = structure "CircuitBreaker" -let () = seal circuit_breaker (** Opaque handle for MonotonicCounter. *) type monotonic_counter let monotonic_counter : monotonic_counter structure typ = structure "MonotonicCounter" -let () = seal monotonic_counter (** Opaque handle for StateMachine. *) type state_machine let state_machine : state_machine structure typ = structure "StateMachine" -let () = seal state_machine (** Opaque handle for Tensor2D. *) type tensor_2d let tensor_2d : tensor_2d structure typ = structure "Tensor2D" -let () = seal tensor_2d (** Opaque handle for LRUCache. *) type lru_cache let lru_cache : lru_cache structure typ = structure "LRUCache" -let () = seal lru_cache (** Opaque handle for Graph. *) type graph let graph : graph structure typ = structure "Graph" -let () = seal graph (** Opaque handle for BoundedQueue. *) type bounded_queue let bounded_queue : bounded_queue structure typ = structure "BoundedQueue" -let () = seal bounded_queue (** Opaque handle for BloomFilter. *) type bloom_filter let bloom_filter : bloom_filter structure typ = structure "BloomFilter" -let () = seal bloom_filter (* -------------------------------------------------------------------------- *) (* Struct: BufferResult { status: i32, buffer: *BoundedBuffer } *) diff --git a/bindings/ocaml/smoke/dune b/bindings/ocaml/smoke/dune new file mode 100644 index 00000000..b94841b9 --- /dev/null +++ b/bindings/ocaml/smoke/dune @@ -0,0 +1,6 @@ +; SPDX-License-Identifier: MPL-2.0 +; Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) + +(executable + (name hello_smoke) + (libraries proven)) diff --git a/bindings/ocaml/smoke/hello_smoke.ml b/bindings/ocaml/smoke/hello_smoke.ml index fefc591a..01a26e8e 100644 --- a/bindings/ocaml/smoke/hello_smoke.ml +++ b/bindings/ocaml/smoke/hello_smoke.ml @@ -19,6 +19,15 @@ let () = exit 1 end; + (* Exact bytes, including lone breaks, embedded NUL and non-UTF-8. *) + List.iter (fun (value, expected) -> + if SafeHeader.has_crlf value <> expected then begin + print_endline "FAIL: SafeHeader byte-span counterexample"; + exit 1 + end + ) ["application/json", false; "\r", true; "\n", true; + "a\000\rZ", true; "\255\000\128", false]; + (* Library identification. *) let major = Version.major () in let minor = Version.minor () in From 47dd78de0520b063e9a824a1eb02285de7c164a5 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:25:35 +0000 Subject: [PATCH 07/24] Own RefC marshaling locally and build fresh package output without private dependencies --- .github/workflows/ffi-full-integration.yml | 113 ++------ ffi/zig/build.zig | 20 +- ffi/zig/build.zig.zon | 6 +- ffi/zig/build_standalone.zig | 4 +- ffi/zig/src/header_refc.zig | 24 +- ffi/zig/src/refc/README.adoc | 25 ++ ffi/zig/src/refc/bits8.zig | 35 +++ ffi/zig/test/integration_test.zig | 1 - proven-ffi.ipkg | 1 + scripts/build-header-refc.sh | 3 +- scripts/build-refc.sh | 284 +++++---------------- scripts/test-header-bridge.sh | 5 +- 12 files changed, 172 insertions(+), 349 deletions(-) create mode 100644 ffi/zig/src/refc/README.adoc create mode 100644 ffi/zig/src/refc/bits8.zig diff --git a/.github/workflows/ffi-full-integration.yml b/.github/workflows/ffi-full-integration.yml index e8487f65..2b95c05c 100644 --- a/.github/workflows/ffi-full-integration.yml +++ b/.github/workflows/ffi-full-integration.yml @@ -1,11 +1,9 @@ # SPDX-License-Identifier: MPL-2.0 # Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) - name: FFI Full Integration env: FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true permissions: - actions: read contents: read on: push: @@ -17,7 +15,6 @@ on: - 'scripts/build-refc.sh' - '.github/workflows/ffi-full-integration.yml' pull_request: - branches: [main] paths: - 'src/**' - 'ffi/zig/**' @@ -31,98 +28,42 @@ concurrency: jobs: idris-refc-zig: name: Idris -> RefC -> Zig - runs-on: ubuntu-latest + runs-on: ubuntu-22.04 timeout-minutes: 45 - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository - env: - IDRIS2_VERSION: '0.8.0' - ZIG_VERSION: '0.15.2' steps: - name: Checkout proven uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 with: persist-credentials: false - - name: Check language-bridges credential - id: bridge-token - env: - LANGUAGE_BRIDGES_TOKEN: ${{ secrets.LANGUAGE_BRIDGES_TOKEN }} - run: | - set -euo pipefail - if [ -z "$LANGUAGE_BRIDGES_TOKEN" ]; then - echo "available=false" >> "$GITHUB_OUTPUT" - echo "::notice::LANGUAGE_BRIDGES_TOKEN is not configured; skipping private full FFI integration." - else - echo "available=true" >> "$GITHUB_OUTPUT" - fi - - name: Checkout language-bridges - if: steps.bridge-token.outputs.available == 'true' - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 - with: - repository: nextgen-languages/language-bridges - path: nextgen-languages/language-bridges - token: ${{ secrets.LANGUAGE_BRIDGES_TOKEN }} - persist-credentials: false - - name: Install Idris 2 dependencies - if: steps.bridge-token.outputs.available == 'true' - run: | - set -euo pipefail - sudo apt-get update - sudo apt-get install -y chezscheme libgmp-dev - - name: Cache Idris 2 - if: steps.bridge-token.outputs.available == 'true' - id: cache-idris2 - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4.3.0 - with: - path: | - ~/.idris2 - ~/idris2-${{ env.IDRIS2_VERSION }} - key: idris2-refc-${{ env.IDRIS2_VERSION }}-${{ runner.os }} - - name: Install Idris 2 - if: steps.bridge-token.outputs.available == 'true' && steps.cache-idris2.outputs.cache-hit != 'true' - run: | - set -euo pipefail - git clone --depth 1 --branch v${{ env.IDRIS2_VERSION }} https://github.com/idris-lang/Idris2.git ~/idris2-${{ env.IDRIS2_VERSION }} - cd ~/idris2-${{ env.IDRIS2_VERSION }} - make bootstrap SCHEME=chezscheme - make install - - name: Add Idris 2 to PATH - if: steps.bridge-token.outputs.available == 'true' - run: echo "$HOME/.idris2/bin" >> "$GITHUB_PATH" - name: Setup Zig - if: steps.bridge-token.outputs.available == 'true' uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: - version: ${{ env.ZIG_VERSION }} - - name: Build Idris RefC output - if: steps.bridge-token.outputs.available == 'true' - run: scripts/build-refc.sh --verbose - - name: Build and test libproven - if: steps.bridge-token.outputs.available == 'true' + version: '0.15.2' + - name: Provision the pinned Idris compiler and check the header profile + uses: ./ffi/zig/actions/refc-library + - name: Generate the full FFI package from a clean source tree + env: + IDRIS2: /home/runner/.idris2/bin/idris2 + run: bash scripts/build-refc.sh --verbose + - name: Build and test libproven through the default build entry point run: | set -euo pipefail - IDRIS2_PREFIX="$(idris2 --prefix)" - REFC_RUNTIME="$(find "$IDRIS2_PREFIX" -path '*/support/refc' -type d 2>/dev/null | head -1)" - C_SUPPORT="$(find "$IDRIS2_PREFIX" -path '*/support/c' -type d 2>/dev/null | head -1)" - SUPPORT_LIB_FILE="$(find "$IDRIS2_PREFIX" -path '*/lib/libidris2_support.*' 2>/dev/null | head -1)" - - test -n "$REFC_RUNTIME" - test -n "$C_SUPPORT" - test -n "$SUPPORT_LIB_FILE" - SUPPORT_LIB="$(dirname "$SUPPORT_LIB_FILE")" - test -d "$GITHUB_WORKSPACE/build/refc" - test -d "$REFC_RUNTIME" - test -d "$C_SUPPORT" - test -d "$SUPPORT_LIB" - + source build/refc/sdk.env cd ffi/zig - zig build \ - -Didris-refc="$GITHUB_WORKSPACE/build/refc" \ - -Didris-refc-runtime="$REFC_RUNTIME" \ - -Didris-c-support="$C_SUPPORT" \ - -Didris-support-lib="$SUPPORT_LIB" - zig build test \ - -Didris-refc="$GITHUB_WORKSPACE/build/refc" \ - -Didris-refc-runtime="$REFC_RUNTIME" \ - -Didris-c-support="$C_SUPPORT" \ - -Didris-support-lib="$SUPPORT_LIB" - ls -la zig-out/lib/libproven.* + zig build --summary all \ + -Didris-refc="$REFC_OUTPUT_DIR" \ + -Didris-refc-runtime="$IDRIS2_REFC_RUNTIME" \ + -Didris-c-support="$IDRIS2_C_SUPPORT" \ + -Didris-support-lib="$IDRIS2_SUPPORT_LIB" 2>&1 | tee ../../build/refc/zig-build.log + zig build test --summary all \ + -Didris-refc="$REFC_OUTPUT_DIR" \ + -Didris-refc-runtime="$IDRIS2_REFC_RUNTIME" \ + -Didris-c-support="$IDRIS2_C_SUPPORT" \ + -Didris-support-lib="$IDRIS2_SUPPORT_LIB" 2>&1 | tee ../../build/refc/zig-tests.log + test -s zig-out/lib/libproven.a + - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + if: always() + with: + name: full-refc-build-evidence + path: build/refc/ + if-no-files-found: error diff --git a/ffi/zig/build.zig b/ffi/zig/build.zig index eb77bbb3..2267f0a7 100644 --- a/ffi/zig/build.zig +++ b/ffi/zig/build.zig @@ -14,7 +14,7 @@ //! -Didris-c-support= Path to Idris2 C support headers (idris_support.h, etc.) //! -Didris-support-lib= Path to libidris2_support.{a,so} //! -//! Uses idris2-zig-ffi for generic Idris 2 FFI infrastructure. +//! RefC marshaling lives in src/refc and uses the compiler's actual SDK. const std = @import("std"); @@ -40,7 +40,7 @@ fn configureRefC( defer dir.close(); var c_files: std.ArrayListUnmanaged([]const u8) = .empty; var iter = dir.iterate(); - while (iter.next() catch null) |entry| { + while (iter.next() catch @panic("cannot enumerate RefC translation units")) |entry| { if (entry.kind == .file) { const name = entry.name; if (std.mem.endsWith(u8, name, ".c")) { @@ -48,7 +48,8 @@ fn configureRefC( } } } - if (c_files.items.len > 0) { + if (c_files.items.len == 0) @panic("empty RefC source directory; refusing an unwired library"); + { mod.addCSourceFiles(.{ .root = .{ .cwd_relative = rp }, .files = c_files.items, @@ -128,16 +129,6 @@ pub fn build(b: *std.Build) void { if (refc_path == null or refc_runtime_path == null or c_support_path == null or support_lib_path == null) @panic("the full FFI build requires all four Idris RefC paths; use build_standalone.zig for explicit unavailable-backend behavior"); - // ====================================================================== - // Get idris2-zig-ffi dependency - // ====================================================================== - - const idris2_zig_ffi_dep = b.dependency("idris2_zig_ffi", .{ - .target = target, - .optimize = optimize, - }); - const idris2_zig_ffi_mod = idris2_zig_ffi_dep.module("idris2_zig_ffi"); - // ====================================================================== // Main library module // ====================================================================== @@ -146,9 +137,6 @@ pub fn build(b: *std.Build) void { .root_source_file = b.path("src/main_refc.zig"), .target = target, .optimize = optimize, - .imports = &.{ - .{ .name = "idris2_zig_ffi", .module = idris2_zig_ffi_mod }, - }, }); proven_mod.addIncludePath(b.path("include")); diff --git a/ffi/zig/build.zig.zon b/ffi/zig/build.zig.zon index f1f26321..51f3abeb 100644 --- a/ffi/zig/build.zig.zon +++ b/ffi/zig/build.zig.zon @@ -4,11 +4,7 @@ .fingerprint = 0xeb23bbc5c6685c93, .minimum_zig_version = "0.15.2", - .dependencies = .{ - .idris2_zig_ffi = .{ - .path = "../../nextgen-languages/language-bridges/bridges/idris2", - }, - }, + .dependencies = .{}, .paths = .{ "build.zig", diff --git a/ffi/zig/build_standalone.zig b/ffi/zig/build_standalone.zig index e24fc367..dc162910 100644 --- a/ffi/zig/build_standalone.zig +++ b/ffi/zig/build_standalone.zig @@ -1,8 +1,8 @@ // SPDX-License-Identifier: MPL-2.0 // Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) // -// build_standalone.zig — standalone build for proven FFI without the Idris2 RefC -// pipeline dependency (idris2_zig_ffi from nextgen-languages/language-bridges). +// build_standalone.zig — build the legacy surface with an optional qualified +// Idris header bundle, without requiring whole-package RefC code generation. // // Without -Dheader-refc the header export refuses with -99. Passing a native // SDK bundle from scripts/build-header-refc.sh selects the real Idris kernel. diff --git a/ffi/zig/src/header_refc.zig b/ffi/zig/src/header_refc.zig index 87af65d6..30ea9844 100644 --- a/ffi/zig/src/header_refc.zig +++ b/ffi/zig/src/header_refc.zig @@ -1,28 +1,22 @@ // SPDX-License-Identifier: MPL-2.0 // Copyright (c) 2026 Jonathan D.A. Jewell //! RefC Bits8 marshaling. The predicate and transition live in Idris. -const c = @cImport({ - @cInclude("_datatypes.h"); -}); +const bits8 = @import("refc/bits8.zig"); extern fn Proven_SafeHeader_ByteKernel_headerStep(?*anyopaque, ?*anyopaque) callconv(.c) ?*anyopaque; extern fn idris2_trampoline(?*anyopaque) callconv(.c) ?*anyopaque; -const shift = c.idris2_vp_int_shift; - -fn box(b: u8) ?*anyopaque { - return @ptrFromInt((@as(usize, b) << shift) | 1); -} pub fn detect(bytes: []const u8) error{InvalidRefcResult}!bool { var state: u8 = 0; for (bytes) |b| { - const result = idris2_trampoline(Proven_SafeHeader_ByteKernel_headerStep(box(state), box(b))); - const raw = @intFromPtr(result); + const result = idris2_trampoline(Proven_SafeHeader_ByteKernel_headerStep(bits8.encode(state), bits8.encode(b))); // Accept only the two canonical RefC Bits8 state encodings. - if (raw == @intFromPtr(box(0))) { - state = 0; - } else if (raw == @intFromPtr(box(1))) { - state = 1; - } else return error.InvalidRefcResult; + const next = try bits8.decode(result); + if (next > 1) return error.InvalidRefcResult; + state = next; } return state == 1; } + +test { + _ = bits8; +} diff --git a/ffi/zig/src/refc/README.adoc b/ffi/zig/src/refc/README.adoc new file mode 100644 index 00000000..b9acf5ee --- /dev/null +++ b/ffi/zig/src/refc/README.adoc @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: CC-BY-SA-4.0 +// Copyright (c) 2026 Jonathan D.A. Jewell += Proven RefC ABI marshaling + +This directory owns the small ABI helpers actually used by Proven's Zig +bridge. `bits8.zig` encodes and validates immediate Bits8 words using the +matched Idris compiler's `_datatypes.h`. It does not implement a safety +predicate, allocate Idris objects, or invent an alternative runtime. + +The declared `nextgen-languages/language-bridges/bridges/idris2` dependency +could not be located. No production source called that dependency; the +legacy integration sketch imported it without using it. This directory is +a new, scoped implementation, not a recovery of the missing repository. +Keeping it beside its actual consumer avoids a private checkout or token. + +Current qualification: Idris2 b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b, +Zig 0.15.2, native Linux x86_64 GNU. `scripts/test-header-bridge.sh` tests +all 256 marshaled values and rejects noncanonical tags/ranges, then runs +the real generated Idris function through the public byte-span ABI. +The full package integration gate remains separate from that qualification. + +Only extract a separate shared repository when a second real consumer +needs this same ABI profile. Strings, heap objects, closures, ownership, +threading and other compiler/target profiles need their own contracts +and executable evidence before being added here. diff --git a/ffi/zig/src/refc/bits8.zig b/ffi/zig/src/refc/bits8.zig new file mode 100644 index 00000000..a12c9fdf --- /dev/null +++ b/ffi/zig/src/refc/bits8.zig @@ -0,0 +1,35 @@ +// SPDX-License-Identifier: MPL-2.0 +// Copyright (c) 2026 Jonathan D.A. Jewell +//! Marshaling for the pinned Idris RefC immediate Bits8 representation. +//! Values are tagged words, not pointers to allocated objects. +const std = @import("std"); +const c = @cImport({ + @cInclude("_datatypes.h"); +}); +const shift = c.idris2_vp_int_shift; + +pub fn encode(value: u8) ?*anyopaque { + return @ptrFromInt((@as(usize, value) << shift) | 1); +} + +pub fn decode(value: ?*anyopaque) error{InvalidRefcResult}!u8 { + const raw = @intFromPtr(value); + const payload = raw >> shift; + if (payload > std.math.maxInt(u8)) return error.InvalidRefcResult; + const byte: u8 = @intCast(payload); + if (raw != @intFromPtr(encode(byte))) return error.InvalidRefcResult; + return byte; +} + +test "all canonical Bits8 words round trip through the actual SDK layout" { + for (0..256) |n| { + const byte: u8 = @intCast(n); + try std.testing.expectEqual(byte, try decode(encode(byte))); + } +} + +test "null, noncanonical tags and out-of-range payloads are rejected" { + try std.testing.expectError(error.InvalidRefcResult, decode(null)); + try std.testing.expectError(error.InvalidRefcResult, decode(@ptrFromInt(2))); + try std.testing.expectError(error.InvalidRefcResult, decode(@ptrFromInt((@as(usize, 256) << shift) | 1))); +} diff --git a/ffi/zig/test/integration_test.zig b/ffi/zig/test/integration_test.zig index 4490bd71..da69703e 100644 --- a/ffi/zig/test/integration_test.zig +++ b/ffi/zig/test/integration_test.zig @@ -7,7 +7,6 @@ // and verifies that the FFI bridge works correctly. const std = @import("std"); -const ffi = @import("idris2_zig_ffi"); // Import the main proven FFI const proven = @import("../src/main.zig"); diff --git a/proven-ffi.ipkg b/proven-ffi.ipkg index ca572157..a96c16d7 100644 --- a/proven-ffi.ipkg +++ b/proven-ffi.ipkg @@ -12,6 +12,7 @@ sourceloc = "https://github.com/hyperpolymath/proven" bugtracker = "https://github.com/hyperpolymath/proven/issues" depends = contrib +opts = "--total -Werror" sourcedir = "src" builddir = "build" diff --git a/scripts/build-header-refc.sh b/scripts/build-header-refc.sh index 848d7071..0a141276 100644 --- a/scripts/build-header-refc.sh +++ b/scripts/build-header-refc.sh @@ -6,7 +6,8 @@ set -euo pipefail root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) out=${1:-"$root/build/header-refc"} idris=${IDRIS2:-idris2} -command -v "$idris" >/dev/null +idris=$(command -v "$idris") +idris=$(cd "$(dirname "$idris")" && printf '%s/%s' "$PWD" "$(basename "$idris")") [[ $("$idris" --version) == 'Idris 2, version 0.8.0-b6cbc25d5' ]] || { echo 'FAIL: header ABI profile requires Idris2 b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b' >&2; exit 1; } diff --git a/scripts/build-refc.sh b/scripts/build-refc.sh index a16fe8a4..e574c660 100755 --- a/scripts/build-refc.sh +++ b/scripts/build-refc.sh @@ -1,230 +1,72 @@ #!/usr/bin/env bash # SPDX-License-Identifier: MPL-2.0 # Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) -# -# build-refc.sh - Compile Idris2 proven-ffi package to C via RefC backend -# -# This script is Step 1 of the Idris2 -> RefC -> C -> Zig compilation pipeline. -# It invokes the Idris2 RefC code generator on proven-ffi.ipkg, producing .c and .h -# files that the Zig build system then compiles and links into libproven.{so,dylib,dll}. -# -# Usage: -# ./scripts/build-refc.sh [--clean] [--verbose] -# -# Environment variables: -# IDRIS2 - Path to idris2 binary (default: idris2 on PATH) -# PROVEN_ROOT - Repository root (default: parent of scripts/) -# IPKG - Package file to build (default: proven-ffi.ipkg) -# -# Output: -# build/refc/ - Generated C source files -# The script prints REFC_OUTPUT_DIR= for downstream consumption. - +# Generate the full FFI package in a clean scratch tree, never from stale build/. set -euo pipefail - -# --------------------------------------------------------------------------- -# Configuration -# --------------------------------------------------------------------------- - -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -PROVEN_ROOT="${PROVEN_ROOT:-$(cd "$SCRIPT_DIR/.." && pwd)}" -IDRIS2="${IDRIS2:-idris2}" -IPKG="${IPKG:-proven-ffi.ipkg}" -VERBOSE="${VERBOSE:-0}" -CLEAN=0 - -# Parse arguments +root=${PROVEN_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)} +root=$(cd "$root" && pwd) +idris=$(command -v "${IDRIS2:-idris2}") +idris=$(cd "$(dirname "$idris")" && printf '%s/%s' "$PWD" "$(basename "$idris")") +ipkg=${IPKG:-proven-ffi.ipkg} for arg in "$@"; do - case "$arg" in - --clean) CLEAN=1 ;; - --verbose) VERBOSE=1 ;; - --help|-h) - echo "Usage: $0 [--clean] [--verbose]" - echo "" - echo "Compile Idris2 proven-ffi package to C via RefC backend." - echo "" - echo "Options:" - echo " --clean Remove build/refc/ before compiling" - echo " --verbose Show detailed compilation output" - echo " --help Show this help message" - exit 0 - ;; - *) - echo "Error: Unknown argument: $arg" >&2 - exit 1 - ;; - esac + case "$arg" in + --clean|--verbose) ;; # Every build is clean; diagnostics are always retained. + --help|-h) echo 'Usage: build-refc.sh [--clean] [--verbose]'; exit 0 ;; + *) echo "FAIL: unknown argument: $arg" >&2; exit 1 ;; + esac done - -# --------------------------------------------------------------------------- -# Helpers -# --------------------------------------------------------------------------- - -log() { - echo "[build-refc] $*" +[[ $("$idris" --version) == 'Idris 2, version 0.8.0-b6cbc25d5' ]] || { + echo 'FAIL: full RefC profile requires Idris2 b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b' >&2; exit 1; } - -verbose() { - if [ "$VERBOSE" -eq 1 ]; then - echo "[build-refc:debug] $*" - fi +[[ $(uname -sm) == 'Linux x86_64' ]] || { echo 'FAIL: native Linux x86_64 required' >&2; exit 1; } +test -f "$root/$ipkg" +out="$root/build/refc" +mkdir -p "$out" +rm -f "$out/result.txt" "$out/sdk.env" +# This directory is the script-owned generated staging area. Remove only its +# generated C/header files so failed builds cannot leave an accepted old bundle. +find "$out" -maxdepth 1 -type f \( -name '*.c' -o -name '*.h' \) -delete +work=$(mktemp -d -t proven-full-refc.XXXXXXXX) +finish() { + result=$? + if [[ $result -ne 0 ]]; then printf 'FAIL: exit %s\n' "$result" > "$out/result.txt"; fi + rm -rf -- "$work" } - -die() { - echo "[build-refc] ERROR: $*" >&2 - exit 1 -} - -# --------------------------------------------------------------------------- -# Preflight checks -# --------------------------------------------------------------------------- - -command -v "$IDRIS2" >/dev/null 2>&1 || die "idris2 not found. Set IDRIS2 env var or add to PATH." - -IDRIS2_VERSION=$("$IDRIS2" --version 2>/dev/null || echo "unknown") -verbose "Idris2 version: $IDRIS2_VERSION" - -IDRIS2_PREFIX=$("$IDRIS2" --prefix 2>/dev/null || die "Cannot determine Idris2 prefix") -verbose "Idris2 prefix: $IDRIS2_PREFIX" - -# Verify ipkg exists -if [ ! -f "$PROVEN_ROOT/$IPKG" ]; then - die "Package file not found: $PROVEN_ROOT/$IPKG" -fi - -# Verify RefC runtime is installed -IDRIS2_REFC_DIR="$IDRIS2_PREFIX/idris2-0.8.0/support/refc" -if [ ! -d "$IDRIS2_REFC_DIR" ]; then - # Try without version suffix (for non-standard installations) - IDRIS2_REFC_DIR=$(find "$IDRIS2_PREFIX" -path "*/support/refc" -type d 2>/dev/null | head -1) - if [ -z "$IDRIS2_REFC_DIR" ]; then - die "Idris2 RefC support directory not found under $IDRIS2_PREFIX" - fi -fi - -if [ ! -f "$IDRIS2_REFC_DIR/libidris2_refc.a" ]; then - die "libidris2_refc.a not found in $IDRIS2_REFC_DIR. Install Idris2 with RefC support." -fi - -verbose "RefC runtime dir: $IDRIS2_REFC_DIR" - -# --------------------------------------------------------------------------- -# Build directory setup -# --------------------------------------------------------------------------- - -REFC_OUTPUT_DIR="$PROVEN_ROOT/build/refc" - -if [ "$CLEAN" -eq 1 ] && [ -d "$REFC_OUTPUT_DIR" ]; then - log "Cleaning $REFC_OUTPUT_DIR" - rm -rf "$REFC_OUTPUT_DIR" -fi - -mkdir -p "$REFC_OUTPUT_DIR" - -# --------------------------------------------------------------------------- -# Step 1: Compile Idris2 to C via RefC backend -# --------------------------------------------------------------------------- - -log "Compiling $IPKG with RefC code generator..." -verbose "Working directory: $PROVEN_ROOT" -verbose "Command: $IDRIS2 --codegen refc --build $IPKG" - -cd "$PROVEN_ROOT" - -if [ "$VERBOSE" -eq 1 ]; then - "$IDRIS2" --codegen refc --build "$IPKG" -else - "$IDRIS2" --codegen refc --build "$IPKG" 2>&1 -fi - -# --------------------------------------------------------------------------- -# Step 2: Locate generated C files -# --------------------------------------------------------------------------- - -# Idris2 RefC output goes to build/exec/ (per ipkg outputdir) by default. -# The generated files are typically: -# build/exec/ (directory or single C file) -# For library builds, RefC generates .c files in build/exec/ or build/ttc/ - -# Search for generated C files in the build tree -EXEC_DIR="$PROVEN_ROOT/build/exec" -BUILD_DIR="$PROVEN_ROOT/build" - -# Find all generated .c files from the RefC compilation -GENERATED_C_FILES=() -GENERATED_H_FILES=() - -# RefC puts generated C code in the output directory -for dir in "$EXEC_DIR" "$BUILD_DIR"; do - if [ -d "$dir" ]; then - while IFS= read -r -d '' f; do - GENERATED_C_FILES+=("$f") - done < <(find "$dir" -name "*.c" -print0 2>/dev/null) - - while IFS= read -r -d '' f; do - GENERATED_H_FILES+=("$f") - done < <(find "$dir" -name "*.h" -print0 2>/dev/null) - fi -done - -if [ ${#GENERATED_C_FILES[@]} -eq 0 ]; then - log "WARNING: No .c files found in build directory." - log "The RefC backend may have placed output in a different location." - log "Checking alternative locations..." - - # Check if RefC created a single output binary instead of .c files - if [ -f "$EXEC_DIR/proven-ffi" ]; then - log "Found compiled binary at $EXEC_DIR/proven-ffi" - log "Note: RefC may have compiled directly. Re-run with --codegen refc --cg-opt --no-compile for .c output." - fi -else - log "Found ${#GENERATED_C_FILES[@]} generated C file(s):" - for f in "${GENERATED_C_FILES[@]}"; do - verbose " $(realpath --relative-to="$PROVEN_ROOT" "$f")" - done -fi - -if [ ${#GENERATED_H_FILES[@]} -gt 0 ]; then - log "Found ${#GENERATED_H_FILES[@]} generated header file(s):" - for f in "${GENERATED_H_FILES[@]}"; do - verbose " $(realpath --relative-to="$PROVEN_ROOT" "$f")" - done +trap finish EXIT +cp -R "$root/src" "$work/src" +cp "$root/$ipkg" "$work/package.ipkg" +if ! (cd "$work"; "$idris" --codegen refc --build package.ipkg) > "$out/build.log" 2>&1; then + cat "$out/build.log" >&2; exit 1 fi - -# --------------------------------------------------------------------------- -# Step 3: Copy/symlink generated files to build/refc/ for Zig consumption -# --------------------------------------------------------------------------- - -# Copy generated C files into a clean staging directory that Zig can reference -for f in "${GENERATED_C_FILES[@]}"; do - cp -v "$f" "$REFC_OUTPUT_DIR/" 2>/dev/null || cp "$f" "$REFC_OUTPUT_DIR/" -done - -for f in "${GENERATED_H_FILES[@]}"; do - cp -v "$f" "$REFC_OUTPUT_DIR/" 2>/dev/null || cp "$f" "$REFC_OUTPUT_DIR/" -done - -# --------------------------------------------------------------------------- -# Step 4: Report results for downstream pipeline -# --------------------------------------------------------------------------- - -log "" -log "=========================================" -log " RefC Compilation Complete" -log "=========================================" -log "" -log "REFC_OUTPUT_DIR=$REFC_OUTPUT_DIR" -log "IDRIS2_REFC_RUNTIME=$IDRIS2_REFC_DIR" -log "IDRIS2_SUPPORT_LIB=$IDRIS2_PREFIX/idris2-0.8.0/lib" -log "IDRIS2_C_SUPPORT=$IDRIS2_PREFIX/idris2-0.8.0/support/c" -log "" -log "C files staged in: $REFC_OUTPUT_DIR" -ls -la "$REFC_OUTPUT_DIR/" 2>/dev/null || true -log "" -log "Next step: Run 'just build-ffi' or 'zig build -Didris-refc=$REFC_OUTPUT_DIR'" - -# Export for CI/CD and pipeline scripts -echo "REFC_OUTPUT_DIR=$REFC_OUTPUT_DIR" -echo "IDRIS2_REFC_RUNTIME=$IDRIS2_REFC_DIR" -echo "IDRIS2_SUPPORT_LIB=$IDRIS2_PREFIX/idris2-0.8.0/lib" -echo "IDRIS2_C_SUPPORT=$IDRIS2_PREFIX/idris2-0.8.0/support/c" +if grep -q '^Error:' "$out/build.log"; then cat "$out/build.log" >&2; exit 1; fi +mapfile -d '' -t generated < <(find "$work/build/exec" -type f -name '*.c' -print0) +[[ ${#generated[@]} == 1 ]] || { echo 'FAIL: expected exactly one fresh RefC translation unit' >&2; exit 1; } +grep -q 'Proven_SafeHeader_ByteKernel_headerStep' "${generated[0]}" || { + echo 'FAIL: full package did not retain the header kernel symbol' >&2; exit 1; +} +prefix=$("$idris" --prefix) +mapfile -t runtimes < <(find "$prefix" -path '*/support/refc/runtime.h' -type f) +mapfile -t supports < <(find "$prefix" -path '*/support/c/idris_support.h' -type f) +mapfile -t libraries < <(find "$prefix" -name libidris2_support.a -type f) +[[ ${#runtimes[@]} == 1 && ${#supports[@]} == 1 && ${#libraries[@]} == 1 ]] || { + echo 'FAIL: missing or ambiguous matching RefC SDK' >&2; exit 1; +} +runtime=$(dirname "${runtimes[0]}") +support=$(dirname "${supports[0]}") +libdir=$(dirname "${libraries[0]}") +test -s "$runtime/libidris2_refc.a" +cp "${generated[0]}" "$out/proven-ffi.c" +{ + printf 'REFC_OUTPUT_DIR=%q\n' "$out" + printf 'IDRIS2_REFC_RUNTIME=%q\n' "$runtime" + printf 'IDRIS2_C_SUPPORT=%q\n' "$support" + printf 'IDRIS2_SUPPORT_LIB=%q\n' "$libdir" +} > "$out/sdk.env" +{ + "$idris" --version + sha256sum "$work/package.ipkg" "$out/proven-ffi.c" "$runtime/"*.h "$runtime/libidris2_refc.a" "$support/"*.h "${libraries[0]}" + (cd "$work"; find src -type f -name '*.idr' -exec sha256sum {} +) +} > "$out/identity.log" +echo PASS > "$out/result.txt" +printf 'PASS: fresh full-package RefC output; staging: %s\n' "$out" +printf 'Source %s/sdk.env before the Zig build. This is build evidence, not whole-library verification.\n' "$out" diff --git a/scripts/test-header-bridge.sh b/scripts/test-header-bridge.sh index 70895453..f0b23bc6 100644 --- a/scripts/test-header-bridge.sh +++ b/scripts/test-header-bridge.sh @@ -4,12 +4,13 @@ set -euo pipefail root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) zig=${ZIG:-zig} -command -v "$zig" >/dev/null +zig=$(command -v "$zig") +zig=$(cd "$(dirname "$zig")" && printf '%s/%s' "$PWD" "$(basename "$zig")") evidence=${EVIDENCE_DIR:-"$root/build/header-evidence"} mkdir -p "$evidence" evidence=$(cd "$evidence" && pwd) rm -f "$evidence/result.txt" -find "$root/ffi/zig/src" -maxdepth 1 -name '*.zig' -type f -exec sha256sum {} + > "$evidence/inputs.sha256" +find "$root/ffi/zig/src" -name '*.zig' -type f -exec sha256sum {} + > "$evidence/inputs.sha256" sha256sum "$root/ffi/zig/build_standalone.zig" "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/src/Proven/FFI/HeaderKernelMain.idr" >> "$evidence/inputs.sha256" sha256sum "$root/tests/ffi/header_byte_bridge.c" "$root/bindings/c/include/proven.h" >> "$evidence/inputs.sha256" work=$(mktemp -d -t proven-header-check.XXXXXXXX) From dfea88353020c530a711bd8a64978ba7106a9b99 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:28:30 +0000 Subject: [PATCH 08/24] Reject stale RefC evidence and document the qualified consumer boundary --- .github/workflows/ffi-full-integration.yml | 6 +++ .machine_readable/6a2/META.a2ml | 3 +- docs/adr/2026-11-16-header-refc-boundary.adoc | 48 +++++++++++++++++-- scripts/build-refc.sh | 14 +++--- scripts/test-refc-staging.sh | 40 ++++++++++++++++ 5 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 scripts/test-refc-staging.sh diff --git a/.github/workflows/ffi-full-integration.yml b/.github/workflows/ffi-full-integration.yml index 2b95c05c..66a4cf4f 100644 --- a/.github/workflows/ffi-full-integration.yml +++ b/.github/workflows/ffi-full-integration.yml @@ -13,6 +13,8 @@ on: - 'ffi/zig/**' - 'proven-ffi.ipkg' - 'scripts/build-refc.sh' + - 'scripts/build-header-refc.sh' + - 'scripts/test-refc-staging.sh' - '.github/workflows/ffi-full-integration.yml' pull_request: paths: @@ -20,6 +22,8 @@ on: - 'ffi/zig/**' - 'proven-ffi.ipkg' - 'scripts/build-refc.sh' + - 'scripts/build-header-refc.sh' + - 'scripts/test-refc-staging.sh' - '.github/workflows/ffi-full-integration.yml' workflow_dispatch: concurrency: @@ -61,6 +65,8 @@ jobs: -Didris-c-support="$IDRIS2_C_SUPPORT" \ -Didris-support-lib="$IDRIS2_SUPPORT_LIB" 2>&1 | tee ../../build/refc/zig-tests.log test -s zig-out/lib/libproven.a + - name: Reject stale output and misleading compiler success + run: bash scripts/test-refc-staging.sh - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 if: always() with: diff --git a/.machine_readable/6a2/META.a2ml b/.machine_readable/6a2/META.a2ml index 1e534d87..cbbc10d7 100644 --- a/.machine_readable/6a2/META.a2ml +++ b/.machine_readable/6a2/META.a2ml @@ -6,10 +6,11 @@ project = "proven" author = "Jonathan D.A. Jewell " license = "MPL-2.0" standard = "RSR 2026" -last-updated = "2026-06-01" +last-updated = "2026-11-16" [architecture-decisions] decisions = [ + { id = "ADR-004", title = "Qualified exact-byte Idris RefC header bridge with repo-local ABI marshaling", status = "proposed", date = "2026-11-16", body = "The header transition belongs to the total Idris Bits8 kernel; Zig walks the caller's span and uses the compiler's actual SDK representation. Missing backends and invalid states refuse explicitly. ABI marshaling lives in ffi/zig/src/refc; the unavailable language-bridges dependency had no production call sites and is removed, superseding ADR-003 for build provisioning. Full-package generation uses an isolated source tree and rejects stale C, misleading compiler success and missing output. Model proofs replay in compatible Agda, full Cubical, Lean and Rocq/coqchk; source/compiler refinement remains open. See docs/adr/2026-11-16-header-refc-boundary.adoc for qualification, the remaining OCaml lifecycle mismatch and repository-wide proof debt." }, { id = "ADR-001", title = "OWED-with-justification convention for bodyless declarations in Proofs.idr files", status = "accepted", date = "2026-05-20", body = "Bodyless type signatures (implicit postulates) are made discoverable via triple-pipe doc-comment naming the claim + Idris2 0.8.0 blocker + discharge condition, plus a leading `0 ` erased-multiplicity marker, plus the original bare signature. The `postulate` keyword is NOT used (zero Proofs.idr files use it). Canonical example: src/Proven/SafeChecksum/Proofs.idr. Reference: hyperpolymath/standards#158 Fork A campaign, PROOF-NEEDS.md." }, { id = "ADR-002", title = "Fast proof-check CI gate over the proven.ipkg proof modules", status = "accepted", date = "2026-06-01", body = "A required proof-check job runs idris2 --check on each *.Proofs module listed in proven.ipkg (the 29 build-contract proof modules), from the src/ sourcedir with module-relative paths, on a clean idris2-0.8.0 + contrib toolchain (build/ removed so a stale TTC cannot mask a break). This is the fast cone-check recommended in #145 after #127/#145 showed DISCHARGED-but-unbuilt proofs reaching main while the slow full build sat queued. Scope is the ipkg list, not every on-disk Proofs.idr (64 of 93 are intentionally env-blocked or WIP). A corollary of ADR-001: a theorem that can only be closed by consuming an erased 0-multiplicity OWED premise, or an irreducible top-level-constant projection, is itself reverted to OWED rather than given a body that does not type-check. Origin: #147 (gate) and #148 (gate correctness + 5 proof repairs)." }, { id = "ADR-003", title = "CI builds the standalone Zig FFI surface; full Idris->RefC->Zig integration deferred", status = "accepted", date = "2026-06-01", body = "The e2e.yml E2E and Bench jobs build ffi/zig/build_standalone.zig (src/main.zig only, the pure-Zig C-ABI surface) instead of the default build.zig, because build.zig's idris2_zig_ffi is a Zig .path dependency on the private, sibling-checkout-only repo nextgen-languages/language-bridges, which CI cannot resolve. The Zig toolchain is provisioned via mlugg/setup-zig@v1 at the build.zig.zon minimum (0.15.2). Full Idris->RefC->Zig integration (idris2 --codegen refc + scripts/build-refc.sh + zig build -Didris-*) is documented in docs/ffi-ci-build.md and deferred until language-bridges is provisioned as a submodule plus a CI read credential. Origin: #149; tracked in #151." }, diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc index ba36b20b..30a709f4 100644 --- a/docs/adr/2026-11-16-header-refc-boundary.adoc +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -37,9 +37,21 @@ this boundary. Generated C `main` is renamed when linked into the library. `scripts/build-header-refc.sh` needs no private `language-bridges` dependency. The standalone library without its bundle deliberately refuses header checks. -The whole-library RefC build still has a private dependency and is not qualified -by this header-only result. Other legacy exports are not repaired or verified -by this work. +The default build now uses local `ffi/zig/src/refc` ABI helpers and the actual +compiler SDK. The old private dependency was declared but had no production +call sites; it has been removed. The local directory is a new scoped +implementation, not a reconstruction of the missing repository's contents. +Other legacy exports are not repaired or verified by this work. + +`scripts/build-refc.sh` builds `proven-ffi.ipkg` in an isolated source tree, +with totality and warnings-as-errors enabled by the package. It accepts one +fresh generated C translation unit, requires the header symbol, and records +the exact SDK paths and hashes. It never scans the caller's existing build +tree for C files. The `FFI Full Integration` job now runs without a token, +including on fork PRs. "Full" here means the declared FFI package and its +import cone, not every library module or every declared public C function. +Three controls reject zero-exit compiler errors, absent output and a wrong +compiler even when an old C file and a PASS receipt have been planted. == Reproduction and evidence @@ -62,6 +74,22 @@ D and OCaml CI obtain this real shared-library profile through Evidence in `build/header-evidence` records versions, source/SDK hashes and positive/negative logs. +The RefC ABI now has 14 passing Zig tests, including all 256 immediate-word +round trips and rejection of invalid tags/ranges. Chapel's smoke, contract, +unit and detached tests pass with either-CR-or-LF semantics and embedded NUL. +D's smoke calls the header C ABI directly using its real result struct; +its build, tests, symbol audit and detached checks pass. These are executable +binding checks, not new source-refinement proofs. + +OCaml's package now compiles: the CI uses its opam environment, the smoke +executable has a Dune target, and opaque pointee types remain incomplete. +Its full smoke still fails honestly on the missing `proven_deinit` symbol. +It eagerly binds the entire API, and its global lifecycle signatures also +disagree with the legacy handle-based Zig exports. Do not add dummy exports +or enable unchecked lazy calls to hide this. A subsequent capability-scoped +binding or a real, consistently specified lifecycle implementation is needed +before claiming OCaml compatibility. No OCaml runtime pass is claimed here. + The second command imports only committed proof sources from Sholes `c28993a73d1c78830c6d92f563fae638d22bb7f7` into a clean scratch directory. `verification/header` proves the numeric-byte classifier, the unbounded state @@ -93,3 +121,17 @@ can confirm that the observed mismatch is repaired. Atoma's current closed u8 source subset does not cover this loop; its source-to-proof milestone remains INCOMPLETE. The model proof and executable evidence must not be promoted to a whole-library or v1 verification claim. + +== Repository-wide blockers outside the repaired profile + +The safety gate still reports 20 existing `assert_total` uses. The licence +gate rejects five existing `Palimpsest-MPL-1.0` headers in SafeYAML/Parser, +SafeRegex/Parser and Matcher, SafeSQL/Builder and SafeXML/Parser. Those files +are unchanged by this repair; licence changes require the owner and are not +part of this patch. The older Idris property suite also references unavailable +SafeMath operations and incompatible result types. These failures remain +visible and prevent a whole-repository green claim. + +The repository instructions name `./hypatia-v2`, but that executable is not +present in this checkout. No successful local scan with that command is +claimed. Existing hosted security checks remain enabled. diff --git a/scripts/build-refc.sh b/scripts/build-refc.sh index e574c660..9d7036cc 100755 --- a/scripts/build-refc.sh +++ b/scripts/build-refc.sh @@ -5,8 +5,6 @@ set -euo pipefail root=${PROVEN_ROOT:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)} root=$(cd "$root" && pwd) -idris=$(command -v "${IDRIS2:-idris2}") -idris=$(cd "$(dirname "$idris")" && printf '%s/%s' "$PWD" "$(basename "$idris")") ipkg=${IPKG:-proven-ffi.ipkg} for arg in "$@"; do case "$arg" in @@ -15,11 +13,6 @@ for arg in "$@"; do *) echo "FAIL: unknown argument: $arg" >&2; exit 1 ;; esac done -[[ $("$idris" --version) == 'Idris 2, version 0.8.0-b6cbc25d5' ]] || { - echo 'FAIL: full RefC profile requires Idris2 b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b' >&2; exit 1; -} -[[ $(uname -sm) == 'Linux x86_64' ]] || { echo 'FAIL: native Linux x86_64 required' >&2; exit 1; } -test -f "$root/$ipkg" out="$root/build/refc" mkdir -p "$out" rm -f "$out/result.txt" "$out/sdk.env" @@ -33,6 +26,13 @@ finish() { rm -rf -- "$work" } trap finish EXIT +idris=$(command -v "${IDRIS2:-idris2}") +idris=$(cd "$(dirname "$idris")" && printf '%s/%s' "$PWD" "$(basename "$idris")") +[[ $("$idris" --version) == 'Idris 2, version 0.8.0-b6cbc25d5' ]] || { + echo 'FAIL: full RefC profile requires Idris2 b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b' >&2; exit 1; +} +[[ $(uname -sm) == 'Linux x86_64' ]] || { echo 'FAIL: native Linux x86_64 required' >&2; exit 1; } +test -f "$root/$ipkg" cp -R "$root/src" "$work/src" cp "$root/$ipkg" "$work/package.ipkg" if ! (cd "$work"; "$idris" --codegen refc --build package.ipkg) > "$out/build.log" 2>&1; then diff --git a/scripts/test-refc-staging.sh b/scripts/test-refc-staging.sh new file mode 100644 index 00000000..0a4cd26c --- /dev/null +++ b/scripts/test-refc-staging.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +# Adversarial compiler controls: stale output must never rescue a failed build. +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +evidence=${EVIDENCE_DIR:-"$root/build/refc/staging-controls"} +mkdir -p "$evidence" +evidence=$(cd "$evidence" && pwd) +rm -f "$evidence/result.txt" +work=$(mktemp -d -t proven-refc-staging.XXXXXXXX) +trap 'rm -rf -- "$work"' EXIT +mkdir -p "$work/fixture/src" "$work/fixture/build/refc" +cp "$root/proven-ffi.ipkg" "$work/fixture/" +cat > "$work/idris2" <<'COMPILER' +#!/usr/bin/env bash +if [[ $1 == --version ]]; then + if [[ $PROVEN_REFC_TEST_MODE == version ]]; then echo 'wrong compiler'; + else echo 'Idris 2, version 0.8.0-b6cbc25d5'; fi +elif [[ $PROVEN_REFC_TEST_MODE == diagnostic ]]; then + echo 'Error: injected compiler diagnostic with successful exit' +fi +exit 0 +COMPILER +chmod +x "$work/idris2" +for mode in diagnostic empty version; do + printf '/* stale */ void Proven_SafeHeader_ByteKernel_headerStep(void) {}\n' > "$work/fixture/build/refc/stale.c" + echo PASS > "$work/fixture/build/refc/result.txt" + if PROVEN_REFC_TEST_MODE="$mode" PROVEN_ROOT="$work/fixture" IDRIS2="$work/idris2" bash "$root/scripts/build-refc.sh" > "$evidence/$mode.log" 2>&1; then + echo "FAIL: accepted $mode compiler with stale C output" >&2; exit 1 + fi + grep -q '^FAIL:' "$work/fixture/build/refc/result.txt" + test ! -e "$work/fixture/build/refc/stale.c" + test ! -e "$work/fixture/build/refc/sdk.env" +done +grep -q 'Error: injected compiler diagnostic' "$evidence/diagnostic.log" +grep -q 'expected exactly one fresh RefC translation unit' "$evidence/empty.log" +grep -q 'requires Idris2' "$evidence/version.log" +echo PASS > "$evidence/result.txt" +echo 'PASS: zero-exit diagnostics, missing output and wrong compiler cannot reuse stale C or a PASS receipt' From 16598dd9875e99f1c52e6448438ead8a93eb3317 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:41:46 +0000 Subject: [PATCH 09/24] Add an independently loadable five-symbol OCaml binding --- bindings/ocaml/smoke/dune | 6 ++++ bindings/ocaml/smoke/full_api_probe.ml | 5 ++++ bindings/ocaml/smoke/hello_smoke.ml | 28 +++++++++++------- bindings/ocaml/wired/README.adoc | 24 +++++++++++++++ bindings/ocaml/wired/dune | 6 ++++ bindings/ocaml/wired/proven_wired.ml | 41 ++++++++++++++++++++++++++ bindings/ocaml/wired/proven_wired.mli | 16 ++++++++++ 7 files changed, 116 insertions(+), 10 deletions(-) create mode 100644 bindings/ocaml/smoke/full_api_probe.ml create mode 100644 bindings/ocaml/wired/README.adoc create mode 100644 bindings/ocaml/wired/dune create mode 100644 bindings/ocaml/wired/proven_wired.ml create mode 100644 bindings/ocaml/wired/proven_wired.mli diff --git a/bindings/ocaml/smoke/dune b/bindings/ocaml/smoke/dune index b94841b9..8dec740e 100644 --- a/bindings/ocaml/smoke/dune +++ b/bindings/ocaml/smoke/dune @@ -3,4 +3,10 @@ (executable (name hello_smoke) + (modules hello_smoke) + (libraries proven.wired)) + +(executable + (name full_api_probe) + (modules full_api_probe) (libraries proven)) diff --git a/bindings/ocaml/smoke/full_api_probe.ml b/bindings/ocaml/smoke/full_api_probe.ml new file mode 100644 index 00000000..e044143b --- /dev/null +++ b/bindings/ocaml/smoke/full_api_probe.ml @@ -0,0 +1,5 @@ +(* SPDX-License-Identifier: MPL-2.0 *) +(* Copyright (c) 2026 Jonathan D.A. Jewell *) +(* Keep the full eager-ABI availability gate separate from the wired smoke. + This must fail while declared symbols are absent; it is not a mock. *) +let () = Printf.printf "Full API loaded: %d\n" (Proven.Version.major ()) diff --git a/bindings/ocaml/smoke/hello_smoke.ml b/bindings/ocaml/smoke/hello_smoke.ml index 01a26e8e..85cee57c 100644 --- a/bindings/ocaml/smoke/hello_smoke.ml +++ b/bindings/ocaml/smoke/hello_smoke.ml @@ -3,35 +3,43 @@ (** hello_smoke.ml - End-to-end "does the binding link and run" smoke test. *) -open Proven +open Proven_wired let () = (* SafePath.has_traversal — happy + detection paths. *) let ok_safe = SafePath.has_traversal "/var/www/html/index.html" in let ok_evil = SafePath.has_traversal "../../etc/passwd" in - if ok_safe <> false then begin + if ok_safe <> Some false then begin print_endline "FAIL: SafePath.has_traversal('/var/www/html/index.html') did not return false"; exit 1 end; - if ok_evil <> true then begin + if ok_evil <> Some true then begin print_endline "FAIL: SafePath.has_traversal('../../etc/passwd') did not return true"; exit 1 end; (* Exact bytes, including lone breaks, embedded NUL and non-UTF-8. *) List.iter (fun (value, expected) -> - if SafeHeader.has_crlf value <> expected then begin + if SafeHeader.has_crlf value <> Some expected then begin print_endline "FAIL: SafeHeader byte-span counterexample"; exit 1 end - ) ["application/json", false; "\r", true; "\n", true; + ) ["", false; "application/json", false; "\r", true; "\n", true; "a\000\rZ", true; "\255\000\128", false]; - (* Library identification. *) - let major = Version.major () in - let minor = Version.minor () in - let patch = Version.patch () in + (* Check the binding's byte conversion across every octet and pair. *) + for a = 0 to 255 do + for b = 0 to 255 do + let bytes = String.init 2 (function 0 -> Char.chr a | _ -> Char.chr b) in + let expected = a = 10 || a = 13 || b = 10 || b = 13 in + if SafeHeader.has_crlf bytes <> Some expected then begin + Printf.eprintf "FAIL: header pair %d,%d\n" a b; exit 1 + end + done + done; - Printf.printf "hello_smoke: OK (libproven %d.%d.%d)\n" major minor patch; + (* Library identification. *) + Printf.printf "hello_smoke: OK (libproven %s; %s)\n" + (Version.version ()) (Version.build_info ()); exit 0 diff --git a/bindings/ocaml/wired/README.adoc b/bindings/ocaml/wired/README.adoc new file mode 100644 index 00000000..63754d0a --- /dev/null +++ b/bindings/ocaml/wired/README.adoc @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: CC-BY-SA-4.0 +// Copyright (c) 2026 Jonathan D.A. Jewell += Available-symbol OCaml binding + +Link Dune library `proven.wired` and use `Proven_wired`. This small module +loads only the five symbols in the binding's manifest. It is independent +of the full `Proven` module, which eagerly requires a much larger ABI. + +`SafeHeader.has_crlf` copies the exact OCaml bytes and passes the explicit +length to the real Idris RefC bridge. Embedded NUL and non-UTF-8 are data. +Success returns `Some detected`; a nonzero C status returns `None`. +No header predicate is reimplemented in OCaml. `SafePath` is a legacy Zig +export and remains outside the Idris header proof qualification. + +The wired smoke covers empty strings, lone CR/LF, embedded NUL, high bytes +and all 65,536 byte pairs through this production wrapper. The separate +`ocaml-full-api` job still attempts to load the full API and fails on its +missing lifecycle symbols. That gate has no continue-on-error or dummy +exports. A passing wired smoke is not full OCaml API compatibility. + +Build profile: OCaml 5.1.1 and ctypes/ctypes-foreign 0.24.0. In this release +the Dune library's public name is `ctypes-foreign`; `ctypes.foreign` is a +compatibility alias. The workflow installs the pinned packages and invokes +Dune through `opam exec`. diff --git a/bindings/ocaml/wired/dune b/bindings/ocaml/wired/dune new file mode 100644 index 00000000..26557a0b --- /dev/null +++ b/bindings/ocaml/wired/dune @@ -0,0 +1,6 @@ +; SPDX-License-Identifier: MPL-2.0 +; Copyright (c) 2026 Jonathan D.A. Jewell +(library + (name proven_wired) + (public_name proven.wired) + (libraries ctypes ctypes-foreign)) diff --git a/bindings/ocaml/wired/proven_wired.ml b/bindings/ocaml/wired/proven_wired.ml new file mode 100644 index 00000000..864e5084 --- /dev/null +++ b/bindings/ocaml/wired/proven_wired.ml @@ -0,0 +1,41 @@ +(* SPDX-License-Identifier: MPL-2.0 *) +(* Copyright (c) 2026 Jonathan D.A. Jewell *) +(* Only the five declared wired symbols; no global lifecycle or broad API. *) +open Ctypes + +let lib = Dl.dlopen ~filename:"libproven.so" ~flags:[Dl.RTLD_NOW; Dl.RTLD_LOCAL] +type bool_result +let bool_result : bool_result structure typ = structure "ProvenBoolResult" +let status = field bool_result "status" int32_t +let value = field bool_result "value" bool +let () = seal bool_result +let header = Foreign.foreign ~from:lib "proven_header_has_crlf" + (ptr char @-> size_t @-> returning bool_result) +let path = Foreign.foreign ~from:lib "proven_path_has_traversal" + (ptr char @-> size_t @-> returning bool_result) +let _free_string = Foreign.foreign ~from:lib "proven_free_string" + (ptr char @-> returning void) +let version = Foreign.foreign ~from:lib "proven_version" + (void @-> returning string) +let build_info = Foreign.foreign ~from:lib "proven_build_info" + (void @-> returning string) + +let check_bytes call bytes = + let len = String.length bytes in + (* Own a non-null span even for empty input. Embedded NUL is copied as data. *) + let buffer = allocate_n char ~count:(len + 1) in + for i = 0 to len - 1 do (buffer +@ i) <-@ bytes.[i] done; + (buffer +@ len) <-@ '\000'; + let result = call buffer (Unsigned.Size_t.of_int len) in + if Int32.equal (getf result status) 0l then Some (getf result value) else None + +module SafeHeader = struct + let has_crlf bytes = check_bytes header bytes +end +module SafePath = struct + let has_traversal bytes = check_bytes path bytes +end +module Version = struct + let version () = version () + let build_info () = build_info () +end diff --git a/bindings/ocaml/wired/proven_wired.mli b/bindings/ocaml/wired/proven_wired.mli new file mode 100644 index 00000000..77851d69 --- /dev/null +++ b/bindings/ocaml/wired/proven_wired.mli @@ -0,0 +1,16 @@ +(* SPDX-License-Identifier: MPL-2.0 *) +(* Copyright (c) 2026 Jonathan D.A. Jewell *) +(** The available five-symbol ABI profile, independent of the full Proven API. + Loading requires those actual exports. Nonzero call statuses return None. *) +module SafeHeader : sig + (** Detect either CR or LF over the exact string bytes via Idris RefC. *) + val has_crlf : string -> bool option +end +module SafePath : sig + (** Legacy Zig export; outside the Idris header proof qualification. *) + val has_traversal : string -> bool option +end +module Version : sig + val version : unit -> string + val build_info : unit -> string +end From a8bb252d16b02122d111bcb5a56f112eb7bf8c4c Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:42:00 +0000 Subject: [PATCH 10/24] Check producer source closure and keep the full OCaml ABI gate explicit --- .github/workflows/chapel-ci.yml | 46 ++++++------- .github/workflows/d-ci.yml | 30 +++++++-- .github/workflows/ocaml-ci.yml | 67 +++++++++++++++++-- bindings/chapel/README.adoc | 10 ++- docs/adr/0001-binding-ci-template.adoc | 23 +++++-- docs/adr/2026-11-16-header-refc-boundary.adoc | 10 ++- 6 files changed, 140 insertions(+), 46 deletions(-) diff --git a/.github/workflows/chapel-ci.yml b/.github/workflows/chapel-ci.yml index 61305126..e9a21bce 100644 --- a/.github/workflows/chapel-ci.yml +++ b/.github/workflows/chapel-ci.yml @@ -3,25 +3,11 @@ # # Chapel CI — proven binding-tier-1 pilot. # -# This workflow is the DETACHABLE-HARNESS TEMPLATE for proven binding CI. -# It is intentionally narrow: it references ONLY paths under -# bindings/chapel/, ffi/zig/, and bindings/c/include/proven.h. No -# root-level test fixtures, no sibling-binding tooling, no surprise -# dependencies. See docs/adr/0001-binding-ci-template.md for the full -# detachability contract. -# -# Replication template (search/replace for the next binding): -# chapel -> (e.g. crystal, nim, ocaml) -# Chapel -> (display name) -# bindings/chapel/ -> bindings// -# chapel-ci.yml -> -ci.yml -# Chapel 2.3.0 -> -# ubuntu22.amd64.deb -> -# chpl --version -> -# -# Quality posture: every job is strict (no continue-on-error). PRs that -# break Chapel are blocked from merge. - +# Library producer: a sparse checkout of this binding, ffi/zig, the C header, +# scripts/build-header-refc.sh, src/Proven/SafeHeader/ByteKernel.idr and +# src/Proven/FFI/HeaderKernelMain.idr. The actual sparse build checks this closure. +# Consumer harnesses detach with their binding, C header and supplied .so artifact. +# See docs/adr/0001-binding-ci-template.adoc; the producer is not a binding-only build. name: Chapel CI permissions: actions: read @@ -32,6 +18,9 @@ on: paths: - 'bindings/chapel/**' - 'ffi/zig/**' + - 'scripts/build-header-refc.sh' + - 'src/Proven/SafeHeader/ByteKernel.idr' + - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' - '.github/workflows/chapel-ci.yml' pull_request: @@ -39,6 +28,9 @@ on: paths: - 'bindings/chapel/**' - 'ffi/zig/**' + - 'scripts/build-header-refc.sh' + - 'src/Proven/SafeHeader/ByteKernel.idr' + - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' - '.github/workflows/chapel-ci.yml' workflow_dispatch: @@ -97,18 +89,18 @@ jobs: # from prose ("see bindings/c/include/proven.h."). p="${raw%[.,;:)]}" case "$p" in - bindings/chapel/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|.github/workflows/chapel-ci.yml) + bindings/chapel/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|.github/workflows/chapel-ci.yml|./scripts/symbol-audit.sh|scripts/build-header-refc.sh|src/Proven/SafeHeader/ByteKernel.idr|src/Proven/FFI/HeaderKernelMain.idr) ;; bindings/*/*) echo "::error file=.github/workflows/chapel-ci.yml::detachability violation: $p" forbidden=1 ;; - tests/*) - echo "::error file=.github/workflows/chapel-ci.yml::detachability violation (root-level tests/): $p" + tests/*|scripts/*|src/*) + echo "::error file=.github/workflows/chapel-ci.yml::undeclared producer or root test dependency: $p" forbidden=1 ;; esac - done < <(grep -oE '(bindings/[a-zA-Z0-9_-]+/[A-Za-z0-9_./-]*|tests/[A-Za-z0-9_./-]+)' \ + done < <(grep -oE '(\./scripts/[A-Za-z0-9_./-]+|bindings/[a-zA-Z0-9_-]+/[A-Za-z0-9_./-]*|tests/[A-Za-z0-9_./-]+|scripts/[A-Za-z0-9_./-]+|src/[A-Za-z0-9_./-]+)' \ .github/workflows/chapel-ci.yml | sort -u) [ "$forbidden" -eq 0 ] echo "detachability-guard: OK" @@ -125,6 +117,14 @@ jobs: uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 with: path: proven + sparse-checkout: | + bindings/chapel + bindings/c/include/proven.h + ffi/zig + scripts/build-header-refc.sh + src/Proven/SafeHeader/ByteKernel.idr + src/Proven/FFI/HeaderKernelMain.idr + sparse-checkout-cone-mode: false - name: Setup Zig ${{ env.ZIG_VERSION }} uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: diff --git a/.github/workflows/d-ci.yml b/.github/workflows/d-ci.yml index 943f9a38..9bf97527 100644 --- a/.github/workflows/d-ci.yml +++ b/.github/workflows/d-ci.yml @@ -3,9 +3,11 @@ # # D CI — proven binding-tier-1. # -# This workflow follows the DETACHABLE-HARNESS contract for proven binding CI. -# It references ONLY: bindings/d/, ffi/zig/, and bindings/c/include/proven.h. - +# Library producer: a sparse checkout of this binding, ffi/zig, the C header, +# scripts/build-header-refc.sh, src/Proven/SafeHeader/ByteKernel.idr and +# src/Proven/FFI/HeaderKernelMain.idr. The actual sparse build checks this closure. +# Consumer harnesses detach with their binding, C header and supplied .so artifact. +# See docs/adr/0001-binding-ci-template.adoc; the producer is not a binding-only build. name: D CI permissions: actions: read @@ -16,6 +18,9 @@ on: paths: - 'bindings/d/**' - 'ffi/zig/**' + - 'scripts/build-header-refc.sh' + - 'src/Proven/SafeHeader/ByteKernel.idr' + - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' - '.github/workflows/d-ci.yml' pull_request: @@ -23,6 +28,9 @@ on: paths: - 'bindings/d/**' - 'ffi/zig/**' + - 'scripts/build-header-refc.sh' + - 'src/Proven/SafeHeader/ByteKernel.idr' + - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' - '.github/workflows/d-ci.yml' workflow_dispatch: @@ -50,18 +58,18 @@ jobs: while IFS= read -r raw; do p="${raw%[.,;:)]}" case "$p" in - bindings/d/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|.github/workflows/d-ci.yml) + bindings/d/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|.github/workflows/d-ci.yml|./scripts/symbol-audit.sh|scripts/build-header-refc.sh|src/Proven/SafeHeader/ByteKernel.idr|src/Proven/FFI/HeaderKernelMain.idr) ;; bindings/*/*) echo "::error file=.github/workflows/d-ci.yml::detachability violation: $p" forbidden=1 ;; - tests/*) - echo "::error file=.github/workflows/d-ci.yml::detachability violation (root-level tests/): $p" + tests/*|scripts/*|src/*) + echo "::error file=.github/workflows/d-ci.yml::undeclared producer or root test dependency: $p" forbidden=1 ;; esac - done < <(grep -oE '(bindings/[a-zA-Z0-9_-]+/[A-Za-z0-9_./-]*|tests/[A-Za-z0-9_./-]+)' \ + done < <(grep -oE '(\./scripts/[A-Za-z0-9_./-]+|bindings/[a-zA-Z0-9_-]+/[A-Za-z0-9_./-]*|tests/[A-Za-z0-9_./-]+|scripts/[A-Za-z0-9_./-]+|src/[A-Za-z0-9_./-]+)' \ .github/workflows/d-ci.yml | sort -u) [ "$forbidden" -eq 0 ] echo "detachability-guard: OK" @@ -74,6 +82,14 @@ jobs: uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 with: path: proven + sparse-checkout: | + bindings/d + bindings/c/include/proven.h + ffi/zig + scripts/build-header-refc.sh + src/Proven/SafeHeader/ByteKernel.idr + src/Proven/FFI/HeaderKernelMain.idr + sparse-checkout-cone-mode: false - name: Setup Zig ${{ env.ZIG_VERSION }} uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: diff --git a/.github/workflows/ocaml-ci.yml b/.github/workflows/ocaml-ci.yml index fda8060b..05f54f24 100644 --- a/.github/workflows/ocaml-ci.yml +++ b/.github/workflows/ocaml-ci.yml @@ -3,9 +3,11 @@ # # OCaml CI — proven binding-tier-1. # -# This workflow follows the DETACHABLE-HARNESS contract for proven binding CI. -# It references ONLY: bindings/ocaml/, ffi/zig/, and bindings/c/include/proven.h. - +# Library producer: a sparse checkout of this binding, ffi/zig, the C header, +# scripts/build-header-refc.sh, src/Proven/SafeHeader/ByteKernel.idr and +# src/Proven/FFI/HeaderKernelMain.idr. The actual sparse build checks this closure. +# Consumer harnesses detach with their binding, C header and supplied .so artifact. +# See docs/adr/0001-binding-ci-template.adoc; the producer is not a binding-only build. name: OCaml CI permissions: actions: read @@ -16,6 +18,9 @@ on: paths: - 'bindings/ocaml/**' - 'ffi/zig/**' + - 'scripts/build-header-refc.sh' + - 'src/Proven/SafeHeader/ByteKernel.idr' + - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' - '.github/workflows/ocaml-ci.yml' pull_request: @@ -23,6 +28,9 @@ on: paths: - 'bindings/ocaml/**' - 'ffi/zig/**' + - 'scripts/build-header-refc.sh' + - 'src/Proven/SafeHeader/ByteKernel.idr' + - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' - '.github/workflows/ocaml-ci.yml' workflow_dispatch: @@ -51,18 +59,18 @@ jobs: while IFS= read -r raw; do p="${raw%[.,;:)]}" case "$p" in - bindings/ocaml/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|.github/workflows/ocaml-ci.yml) + bindings/ocaml/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|.github/workflows/ocaml-ci.yml|./scripts/symbol-audit.sh|scripts/build-header-refc.sh|src/Proven/SafeHeader/ByteKernel.idr|src/Proven/FFI/HeaderKernelMain.idr) ;; bindings/*/*) echo "::error file=.github/workflows/ocaml-ci.yml::detachability violation: $p" forbidden=1 ;; - tests/*) - echo "::error file=.github/workflows/ocaml-ci.yml::detachability violation (root-level tests/): $p" + tests/*|scripts/*|src/*) + echo "::error file=.github/workflows/ocaml-ci.yml::undeclared producer or root test dependency: $p" forbidden=1 ;; esac - done < <(grep -oE '(bindings/[a-zA-Z0-9_-]+/[A-Za-z0-9_./-]*|tests/[A-Za-z0-9_./-]+)' \ + done < <(grep -oE '(\./scripts/[A-Za-z0-9_./-]+|bindings/[a-zA-Z0-9_-]+/[A-Za-z0-9_./-]*|tests/[A-Za-z0-9_./-]+|scripts/[A-Za-z0-9_./-]+|src/[A-Za-z0-9_./-]+)' \ .github/workflows/ocaml-ci.yml | sort -u) [ "$forbidden" -eq 0 ] echo "detachability-guard: OK" @@ -75,6 +83,14 @@ jobs: uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 with: path: proven + sparse-checkout: | + bindings/ocaml + bindings/c/include/proven.h + ffi/zig + scripts/build-header-refc.sh + src/Proven/SafeHeader/ByteKernel.idr + src/Proven/FFI/HeaderKernelMain.idr + sparse-checkout-cone-mode: false - name: Setup Zig ${{ env.ZIG_VERSION }} uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: @@ -166,6 +182,43 @@ jobs: run: | opam exec -- dune build smoke/hello_smoke.exe ./_build/default/smoke/hello_smoke.exe + ocaml-full-api: + runs-on: ubuntu-22.04 + needs: ocaml-build + timeout-minutes: 10 + steps: + - name: Checkout (binding + C header only) + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + with: + sparse-checkout: | + bindings/ocaml + bindings/c/include + .github/workflows/ocaml-ci.yml + sparse-checkout-cone-mode: false + - name: Setup OCaml ${{ env.OCAML_VERSION }} + uses: ocaml/setup-ocaml@ff66428f72b64472e749b22e0ad4dded4b2ddca6 # v3 + with: + ocaml-compiler: ${{ env.OCAML_VERSION }} + - name: Install dependencies + run: opam install --yes dune ctypes.0.24.0 ctypes-foreign.0.24.0 alcotest + - name: Install just + uses: extractions/setup-just@dd310ad5a97d8e7b41793f8ef055398d51ad4de6 # v2 + with: + just-version: '1.36.0' + - name: Download libproven.so + uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4 + with: + name: libproven-x86_64-linux-gnu + path: lib/ + - name: Build + Run full_api_probe + working-directory: bindings/ocaml + env: + PROVEN_LIB_PATH: ${{ github.workspace }}/lib + PROVEN_INCLUDE_PATH: ${{ github.workspace }}/bindings/c/include + LD_LIBRARY_PATH: ${{ github.workspace }}/lib + run: | + opam exec -- dune build smoke/full_api_probe.exe + ./_build/default/smoke/full_api_probe.exe ocaml-detachable-build: runs-on: ubuntu-22.04 needs: ocaml-build diff --git a/bindings/chapel/README.adoc b/bindings/chapel/README.adoc index 4a854d31..8bb27cf1 100644 --- a/bindings/chapel/README.adoc +++ b/bindings/chapel/README.adoc @@ -176,7 +176,7 @@ set to `ffi/zig/zig-out/lib`. They contain no Chapel-specific logic. This binding is structured as a *self-contained package* so it can be extracted to a standalone `proven-chapel` repo without surgery. -The full detachable set is: +The detachable consumer set (given an external `libproven.so`) is: [source] ---- @@ -213,6 +213,14 @@ The `chapel-detachable-build` CI job in `chapel-ci.yml` runs this exact sparse-checkout pattern on every PR — if extraction would regress, CI goes red. +The same workflow also contains an upstream library producer. Running that +job in an extracted repository requires `ffi/zig/`, +`scripts/build-header-refc.sh`, `src/Proven/SafeHeader/ByteKernel.idr` and +`src/Proven/FFI/HeaderKernelMain.idr`, with the pinned Idris SDK. Its own +sparse checkout tests this dependency set. A consumer-only extraction must +instead supply the library artifact; the combined workflow is not runnable +from just the consumer files above. See `docs/adr/0001-binding-ci-template.adoc`. + == Architecture ---- diff --git a/docs/adr/0001-binding-ci-template.adoc b/docs/adr/0001-binding-ci-template.adoc index dd4f7e67..5865309b 100644 --- a/docs/adr/0001-binding-ci-template.adoc +++ b/docs/adr/0001-binding-ci-template.adoc @@ -44,10 +44,22 @@ A binding's CI harness consists of *exactly*: extraction). * `.github/workflows/-ci.yml` — workflow file. -The CI workflow's path triggers reference no other paths. The -workflow's checkout steps use `sparse-checkout` to pull only the -detachable set. The detachable-build CI job (see I-5) proves this -holds on every PR. +Consumer jobs use `sparse-checkout` to pull only that set and obtain +`libproven.so` as an external artifact. The detachable-build job checks +that no Idris source, Zig source or root test tree is present. + +Amendment (2026-11-16): producing that artifact is a distinct responsibility. +The actual Idris header library needs `ffi/zig/`, +`scripts/build-header-refc.sh`, `src/Proven/SafeHeader/ByteKernel.idr` and +`src/Proven/FFI/HeaderKernelMain.idr`, plus its pinned compiler SDK. +The producer job now sparse-checks out exactly these inputs alongside the +binding and C header; a transitive dependency outside this set breaks the +real build. Workflow triggers and the path guard include these explicit +producer inputs. The grep guard alone does not prove transitive closure. + +An extracted consumer harness supplies the library artifact externally or +retains this explicit producer source set. Copying the combined workflow +without the producer inputs does not create a standalone library build. === I-2. Self-contained binding Justfile @@ -92,7 +104,8 @@ actually exports. . *detachability-guard* — fails if the workflow references paths outside the allowed set (`bindings//`, `ffi/zig/`, - `bindings/c/include/proven.h`, self). + `bindings/c/include/proven.h`, self and the three explicit producer + source/script files in I-1). . *-build* — installs the pinned toolchain (URL + SHA-256), builds `libproven.so`, typechecks every binding source module. . *-symbol-audit* — runs `scripts/symbol-audit.sh`. diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc index 30a709f4..721ab624 100644 --- a/docs/adr/2026-11-16-header-refc-boundary.adoc +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -83,12 +83,16 @@ binding checks, not new source-refinement proofs. OCaml's package now compiles: the CI uses its opam environment, the smoke executable has a Dune target, and opaque pointee types remain incomplete. -Its full smoke still fails honestly on the missing `proven_deinit` symbol. -It eagerly binds the entire API, and its global lifecycle signatures also +The new `proven.wired` library binds only the five manifest symbols. Its +header wrapper preserves exact bytes and checks status; its independent +smoke exercises all byte pairs without loading the broad API. The separate +`ocaml-full-api` job still fails honestly on missing `proven_deinit`. +The broad module eagerly binds the entire API, and its lifecycle signatures also disagree with the legacy handle-based Zig exports. Do not add dummy exports or enable unchecked lazy calls to hide this. A subsequent capability-scoped binding or a real, consistently specified lifecycle implementation is needed -before claiming OCaml compatibility. No OCaml runtime pass is claimed here. +before claiming full OCaml compatibility. The wired module's runtime evidence +qualifies only its explicit profile. The second command imports only committed proof sources from Sholes `c28993a73d1c78830c6d92f563fae638d22bb7f7` into a clean scratch directory. From 0eeb1914bb0f8b2ded1318e36a9fca4b14443a5c Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 10:57:07 +0000 Subject: [PATCH 11/24] Invalidate failed header bundles and preserve complete boundary evidence --- .github/workflows/trustfile.yml | 22 +- bindings/ocaml/dune-project | 4 +- docs/adr/2026-11-16-header-refc-boundary.adoc | 10 + docs/ffi-ci-build.adoc | 197 +++++++++--------- ffi/zig/build_standalone.zig | 4 + scripts/build-header-refc.sh | 30 ++- scripts/test-header-bridge.sh | 2 + scripts/test-header-staging.sh | 48 +++++ 8 files changed, 206 insertions(+), 111 deletions(-) create mode 100644 scripts/test-header-staging.sh diff --git a/.github/workflows/trustfile.yml b/.github/workflows/trustfile.yml index 1366960e..a2b2e14c 100644 --- a/.github/workflows/trustfile.yml +++ b/.github/workflows/trustfile.yml @@ -110,11 +110,25 @@ jobs: echo "WARNING: believe_me count ($BELIEVE_ME_COUNT) exceeds threshold (300)" fi - name: Verify FFI bridge purity + if: always() run: | - # Zig FFI bridge must not contain safety logic - SAFETY_LOGIC=$(grep -c "if.*overflow\|if.*bounds\|if.*null\|panic\|unreachable" ffi/zig/src/main.zig 2>/dev/null || true) + set -euo pipefail + # Advisory pattern scan, not a purity proof. Include the extracted + # core and every RefC helper, rather than only the export wrapper. + test -d ffi/zig/src + mapfile -d '' -t SOURCES < <(find ffi/zig/src -type f -name '*.zig' -print0) + test "${#SOURCES[@]}" -gt 0 + mkdir -p build + if grep -nE 'if.*overflow|if.*bounds|if.*null|panic|unreachable' "${SOURCES[@]}" > build/ffi-purity.log; then + : + else + result=$? + test "$result" -eq 1 || exit "$result" + fi + SAFETY_LOGIC=$(wc -l < build/ffi-purity.log) + echo "FFI heuristic scanned ${#SOURCES[@]} source files; $SAFETY_LOGIC matching lines" if [ "$SAFETY_LOGIC" -gt 5 ]; then - echo "WARNING: Potential safety logic in FFI bridge ($SAFETY_LOGIC patterns)" + echo "WARNING: Potential safety logic in FFI bridge ($SAFETY_LOGIC patterns); review required" else - echo "FFI bridge purity: PASS" + echo "FFI heuristic threshold not exceeded; this does not establish purity" fi diff --git a/bindings/ocaml/dune-project b/bindings/ocaml/dune-project index 2b733a52..ea2d8bac 100644 --- a/bindings/ocaml/dune-project +++ b/bindings/ocaml/dune-project @@ -25,6 +25,6 @@ (depends (ocaml (>= 4.14.0)) (dune (>= 3.0)) - (ctypes (>= 0.20.0)) - (ctypes-foreign (>= 0.20.0)) + (ctypes (>= 0.24.0)) + (ctypes-foreign (>= 0.24.0)) (alcotest :with-test))) diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc index 721ab624..e6cc587f 100644 --- a/docs/adr/2026-11-16-header-refc-boundary.adoc +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -74,6 +74,16 @@ D and OCaml CI obtain this real shared-library profile through Evidence in `build/header-evidence` records versions, source/SDK hashes and positive/negative logs. +A header rebuild invalidates the previous kernel, archives, copied headers +and identity before checking the compiler. Every failure leaves a FAIL marker; +Zig accepts a header bundle only after its producer writes PASS. Three more +controls plant stale bundles and simulate a wrong compiler, zero-exit errors +and absent output; the producer must remove them and Zig must refuse them. +The identity hashes the copied Idris sources and both sets of SDK headers. +Use one producer per output directory; concurrent publication to a shared +bundle directory is not supported. The marker records build completion, +not a source-to-proof certificate. + The RefC ABI now has 14 passing Zig tests, including all 256 immediate-word round trips and rejection of invalid tags/ranges. Chapel's smoke, contract, unit and detached tests pass with either-CR-or-LF semantics and embedded NUL. diff --git a/docs/ffi-ci-build.adoc b/docs/ffi-ci-build.adoc index 7ccd8782..bcdbe2f7 100644 --- a/docs/ffi-ci-build.adoc +++ b/docs/ffi-ci-build.adoc @@ -1,106 +1,101 @@ -== FFI CI build: standalone surface and full Idris→Zig pipeline +== FFI CI build: explicit profiles and the repo-local RefC bridge -=== Header boundary qualification (2026-11-16) +The header bridge uses Idris2 commit +`b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b`, its matching RefC SDK, +Zig 0.15.2 and native Linux x86_64 GNU. The declared private +`language-bridges` dependency had no production callers and has been removed. +Actual ABI marshaling lives in `ffi/zig/src/refc/`. No private checkout or +`LANGUAGE_BRIDGES_TOKEN` is needed, including for fork pull requests. -The standalone build now has an explicit header backend choice. Without -`-Dheader-refc`, header detection returns status -99. With the native SDK bundle -from `scripts/build-header-refc.sh`, it links the actual Idris byte kernel. -`header-refc.yml` checks this public path without the private bridge dependency. -The older whole-library descriptions below do not qualify every export. -See link:adr/2026-11-16-header-refc-boundary.adoc[the header boundary decision] -for supported versions, executable evidence and outstanding proof obligations. - -`+proven+`’s FFI layer compiles Idris 2 to C via the RefC backend and -wraps it in a stable C ABI with Zig -(`+Idris2 --codegen refc → .c → zig build → libproven+`). There are -*two* Zig build entry points: - -[width="99%",cols="17%,21%,39%,23%",options="header",] +[cols="1,2,2",options="header"] |=== -|File |Builds |External deps |Used by -|`+ffi/zig/build.zig+` |full Idris→RefC→Zig `+libproven+` -|`+idris2_zig_ffi+` (path dep on `+nextgen-languages/language-bridges+`) -+ generated RefC C |local dev, full integration CI - -|`+ffi/zig/build_standalone.zig+` |`+libproven_ffi.a+` from -the common C ABI surface; header unavailable unless a RefC bundle is supplied -|none for unavailable profile; pinned native RefC SDK for header profile -|*CI* (`+e2e.yml+`, `+zig-ffi.yml+`, `+header-refc.yml+`) +|Build entry |Inputs |Result +|`ffi/zig/build.zig` +|Fresh C from `proven-ffi.ipkg` plus all four SDK/source path options +|`libproven.a`; the declared FFI package and its import cone are built +|`ffi/zig/build_standalone.zig` without `-Dheader-refc` +|Zig only +|Legacy ABI surface; header operations explicitly return -99 +|`ffi/zig/build_standalone.zig` with `-Dheader-refc` +|Completed native header bundle from `scripts/build-header-refc.sh` +|Actual Idris header backend; static library or `-Dshared=true` shared library |=== -=== Why E2E uses the standalone build (ADR-003) - -`+build.zig+` declares its `+idris2_zig_ffi+` dependency as a Zig -*`+.path+`* dependency: - -.... -.idris2_zig_ffi = .{ .path = "../../nextgen-languages/language-bridges/bridges/idris2" } -.... - -That points at a *private, cross-owner bridge repo*. `+e2e.yml+` stays -on `+build_standalone.zig+` so the broad E2E and benchmark jobs keep -running without private credentials. Its unavailable-backend profile needs -neither the private bridge nor generated RefC. The separate public header -gate compiles the Idris kernel and exercises the exported ABI. - -`+e2e.yml+` and `+zig-ffi.yml+` therefore run, with Zig provisioned by -`+mlugg/setup-zig@v1+` at `+0.15.2+` (the `+build.zig.zon+` minimum): - -.... -zig build --build-file build_standalone.zig # E2E build -zig build test --build-file build_standalone.zig # E2E tests -zig build bench --build-file build_standalone.zig || … # Bench (soft) -.... - -=== The full pipeline (recipe — Idris half verified locally) - -The canonical recipe is the root `+Justfile+` (`+build-refc+` + -`+build-ffi+`). Verified locally on idris2-0.8.0 + contrib: - -.... -# Step 1 — Idris → RefC C (scripts/build-refc.sh, IPKG=proven-ffi.ipkg) -idris2 --codegen refc --build proven-ffi.ipkg # → build/exec/proven-ffi.c -# staged to build/refc/ ; the script prints the four paths used below - -# Step 2 — Zig links the RefC C + Idris runtime/support into libproven -PFX=$(idris2 --prefix) -zig build \ - -Didris-refc="$PWD/build/refc" \ - -Didris-refc-runtime="$(find "$PFX" -path '*/support/refc' -type d | head -1)" \ - -Didris-c-support="$(find "$PFX" -path '*/support/c' -type d | head -1)" \ - -Didris-support-lib="$(find "$PFX" -path '*/lib/libidris2_support.*' | head -1 | xargs dirname)" -.... - -Requires `+libgmp+` (the RefC runtime links `+gmp+`/`+pthread+`/`+m+`). - -=== Full integration CI - -`+.github/workflows/ffi-full-integration.yml+` restores the full -pipeline: - -[arabic] -. Check out `+proven+`. -. Check out `+nextgen-languages/language-bridges+` into -`+nextgen-languages/language-bridges+`. -. Install Idris2 0.8.0, Zig 0.15.2, and `+libgmp+`. -. Run `+scripts/build-refc.sh --verbose+`. -. Run `+zig build+` and `+zig build test+` with the generated RefC and -Idris runtime/support paths. - -The workflow runs the private full integration only when a repository -secret named `+LANGUAGE_BRIDGES_TOKEN+` is configured with read access -to `+nextgen-languages/language-bridges+`. If that secret is absent, the -job emits a notice and skips the private bridge steps instead of turning -every unrelated PR red. Pull requests from forks are skipped because -that credential must not be exposed to untrusted branches. - -ClusterFuzzLite uses the standalone Zig FFI surface to build a dynamic -`+libproven.so+` fixture and fuzzes the Rust path traversal wrapper -currently backed by `+src/main.zig+`. Additional Rust fuzz targets -should be enabled in ClusterFuzzLite once their exported Zig symbols are -available without the private bridge. - -If `+language-bridges+` is later converted to a real git submodule, keep -the same checkout location so `+ffi/zig/build.zig.zon+` continues to -resolve the bridge via -`+../../nextgen-languages/language-bridges/bridges/idris2+`. +Other legacy exports are outside the header proof qualification. Full-package +build success does not establish that every public C declaration is available +or that all source/compiler refinement obligations are discharged. + +=== Header profile + +From the repository root, with the pinned tools on PATH: + +[source,bash] +---- +bash scripts/test-header-bridge.sh +---- + +This executes the real generated Idris function, the exported Zig ABI and a +caller compiled against the public C header. It checks all single bytes and +pairs, NUL, non-UTF-8, null/empty and length boundaries, and rejects a wrong +backend. Failed-producer controls also prove that old bundles cannot rescue +a failed rebuild. The builder invalidates its previous artifacts before any +tool checks, and the Zig build refuses a missing or non-PASS completion marker. +The marker is build state, not an Atoma proof certificate. + +For a shared library, choose a dedicated absolute bundle/output path: + +[source,bash] +---- +bash scripts/build-header-refc.sh "$PWD/build/header-refc" +bundle="$PWD/build/header-refc" +(cd ffi/zig; zig build --build-file build_standalone.zig -Dshared=true -Dheader-refc="$bundle") +---- + +Use one producer per bundle directory. To rebuild an old bundle predating the +completion marker, run the builder again; do not fabricate a marker. + +=== Full FFI package pipeline + +The generator copies the Idris sources and package into a clean scratch tree. +It enables totality and warnings-as-errors through `proven-ffi.ipkg`, requires +one fresh C translation unit and the reachable header symbol, and records +matching SDK paths in `build/refc/sdk.env`. It never collects arbitrary C files +from an existing build tree. + +[source,bash] +---- +bash scripts/build-refc.sh --verbose +source build/refc/sdk.env +(cd ffi/zig; zig build \ + -Didris-refc="$REFC_OUTPUT_DIR" \ + -Didris-refc-runtime="$IDRIS2_REFC_RUNTIME" \ + -Didris-c-support="$IDRIS2_C_SUPPORT" \ + -Didris-support-lib="$IDRIS2_SUPPORT_LIB") +(cd ffi/zig; zig build test \ + -Didris-refc="$REFC_OUTPUT_DIR" \ + -Didris-refc-runtime="$IDRIS2_REFC_RUNTIME" \ + -Didris-c-support="$IDRIS2_C_SUPPORT" \ + -Didris-support-lib="$IDRIS2_SUPPORT_LIB") +bash scripts/test-refc-staging.sh +---- + +The runtime also requires libc, libgmp, pthread and libm. The existing +`.github/workflows/ffi-full-integration.yml` runs this path without secrets, +including on fork PRs. It uploads the generated-source/SDK identity, build +logs and stale-output rejection results. An actual failure remains a failure; +there is no credential-based success-by-skipping path. + +=== Consumers and proof replay + +Chapel, D and OCaml producers sparse-check out their explicit source/script +inputs, build the real shared library and pass it to detached consumer jobs. +OCaml's `proven.wired` module exercises the five available symbols; the separate +full-API gate still exposes its unresolved lifecycle mismatch. See +link:adr/0001-binding-ci-template.adoc[the producer/consumer contract]. + +Broad E2E, cross-target and fuzzing jobs using the unavailable profile qualify +only the legacy operations they actually call. They do not establish a working +Idris header backend. The separate `header-refc.yml` and `header-theory.yml` +gates cover executable evidence and Agda/Cubical/Lean/Rocq replay respectively. +See link:adr/2026-11-16-header-refc-boundary.adoc[the header boundary decision] +for exact qualification and open source/compiler refinement obligations. diff --git a/ffi/zig/build_standalone.zig b/ffi/zig/build_standalone.zig index dc162910..9081a026 100644 --- a/ffi/zig/build_standalone.zig +++ b/ffi/zig/build_standalone.zig @@ -16,6 +16,10 @@ const std = @import("std"); fn attachHeader(b: *std.Build, mod: *std.Build.Module, path: []const u8) void { if (!std.fs.path.isAbsolute(path)) @panic("-Dheader-refc must be an absolute SDK bundle path"); + const result = std.fs.cwd().readFileAlloc(b.allocator, b.fmt("{s}/result.txt", .{path}), 16) catch + @panic("header RefC bundle is incomplete; rebuild it with scripts/build-header-refc.sh"); + if (!std.mem.eql(u8, result, "PASS\n")) + @panic("header RefC bundle is incomplete; rebuild it with scripts/build-header-refc.sh"); mod.addIncludePath(.{ .cwd_relative = b.fmt("{s}/refc", .{path}) }); mod.addIncludePath(.{ .cwd_relative = b.fmt("{s}/support", .{path}) }); mod.addCSourceFile(.{ diff --git a/scripts/build-header-refc.sh b/scripts/build-header-refc.sh index 0a141276..c14e9367 100644 --- a/scripts/build-header-refc.sh +++ b/scripts/build-header-refc.sh @@ -5,6 +5,29 @@ set -euo pipefail root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) out=${1:-"$root/build/header-refc"} +mkdir -p "$out" +out=$(cd "$out" && pwd) +# The bundle is script-owned. Rebuilding invalidates the old result before +# tool checks, so ignoring a failed rebuild cannot silently reuse its kernel. +rm -f "$out/result.txt" +printf 'INCOMPLETE\n' > "$out/result.txt" +invalidate() { + rm -f "$out/kernel.c" "$out/libidris2_refc.a" "$out/libidris2_support.a" "$out/identity.log" + for dir in "$out/refc" "$out/support"; do + if [[ -d "$dir" ]]; then find "$dir" -maxdepth 1 -type f -name '*.h' -delete; fi + done +} +work='' +finish() { + result=$? + if [[ $result -ne 0 ]]; then + invalidate + printf 'FAIL: exit %s\n' "$result" > "$out/result.txt" + fi + if [[ -n "$work" ]]; then rm -rf -- "$work"; fi +} +trap finish EXIT +invalidate idris=${IDRIS2:-idris2} idris=$(command -v "$idris") idris=$(cd "$(dirname "$idris")" && printf '%s/%s' "$PWD" "$(basename "$idris")") @@ -12,10 +35,7 @@ idris=$(cd "$(dirname "$idris")" && printf '%s/%s' "$PWD" "$(basename "$idris")" echo 'FAIL: header ABI profile requires Idris2 b6cbc25d5cd2ef2a250556266fd7b68dfc360f8b' >&2; exit 1; } [[ $(uname -sm) == 'Linux x86_64' ]] || { echo 'FAIL: only native Linux x86_64 is qualified' >&2; exit 1; } -mkdir -p "$out" -out=$(cd "$out" && pwd) work=$(mktemp -d -t proven-header-refc.XXXXXXXX) -trap 'rm -rf -- "$work"' EXIT mkdir -p "$work/src/Proven/SafeHeader" "$work/src/Proven/FFI" cp "$root/src/Proven/SafeHeader/ByteKernel.idr" "$work/src/Proven/SafeHeader/" cp "$root/src/Proven/FFI/HeaderKernelMain.idr" "$work/src/Proven/FFI/" @@ -46,6 +66,8 @@ cp "$runtime/libidris2_refc.a" "${support_libs[0]}" "$out/" cp "$work/build/exec/header-kernel.c" "$out/kernel.c" { "$idris" --version - sha256sum "$root/src/Proven/SafeHeader/ByteKernel.idr" "$root/src/Proven/FFI/HeaderKernelMain.idr" "$out/kernel.c" "$out/"*.a "$out/refc/"*.h + (cd "$work"; sha256sum src/Proven/SafeHeader/ByteKernel.idr src/Proven/FFI/HeaderKernelMain.idr) + sha256sum "$out/kernel.c" "$out/"*.a "$out/refc/"*.h "$out/support/"*.h } > "$out/identity.log" +echo PASS > "$out/result.txt" printf 'RefC header kernel: %s\n' "$out" diff --git a/scripts/test-header-bridge.sh b/scripts/test-header-bridge.sh index f0b23bc6..bd620d38 100644 --- a/scripts/test-header-bridge.sh +++ b/scripts/test-header-bridge.sh @@ -41,6 +41,8 @@ if "$zig" build test --build-file build_standalone.zig -Dheader-refc="$work/bund echo 'FAIL: ABI tests accepted the always-clean kernel' >&2; exit 1 fi grep -q 'TestExpectedEqual' "$evidence/mutant.log" +grep -q '/support/idris_support.h' "$evidence/identity.log" +EVIDENCE_DIR="$evidence/staging-controls" ZIG="$zig" bash "$root/scripts/test-header-staging.sh" sha256sum -c "$evidence/inputs.sha256" > "$evidence/input-recheck.log" echo PASS > "$evidence/result.txt" printf 'PASS: Idris totality/holes; unavailable backend refusal; actual RefC ABI; 256 single bytes; 65536 pairs; boundary corpus; rejecting mutant.\nEvidence: %s\n' "$evidence" diff --git a/scripts/test-header-staging.sh b/scripts/test-header-staging.sh new file mode 100644 index 00000000..f0ccbc44 --- /dev/null +++ b/scripts/test-header-staging.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +# Failed producers must invalidate old header bundles, including early failure. +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +zig=$(command -v "${ZIG:-zig}") +zig=$(cd "$(dirname "$zig")" && printf '%s/%s' "$PWD" "$(basename "$zig")") +evidence=${EVIDENCE_DIR:-"$root/build/header-evidence/staging-controls"} +mkdir -p "$evidence" +evidence=$(cd "$evidence" && pwd) +rm -f "$evidence/result.txt" +work=$(mktemp -d -t proven-header-staging.XXXXXXXX) +trap 'rm -rf -- "$work"' EXIT +mkdir -p "$work/bundle/refc" "$work/bundle/support" +cat > "$work/idris2" <<'COMPILER' +#!/usr/bin/env bash +if [[ $1 == --version ]]; then + if [[ $PROVEN_HEADER_TEST_MODE == version ]]; then echo 'wrong compiler'; + else echo 'Idris 2, version 0.8.0-b6cbc25d5'; fi +elif [[ $PROVEN_HEADER_TEST_MODE == diagnostic ]]; then + echo 'Error: injected header compiler diagnostic with successful exit' +fi +exit 0 +COMPILER +chmod +x "$work/idris2" +for mode in version diagnostic empty; do + for file in kernel.c libidris2_refc.a libidris2_support.a identity.log refc/stale.h support/stale.h; do + echo stale > "$work/bundle/$file" + done + echo PASS > "$work/bundle/result.txt" + if PROVEN_HEADER_TEST_MODE="$mode" IDRIS2="$work/idris2" bash "$root/scripts/build-header-refc.sh" "$work/bundle" > "$evidence/$mode.log" 2>&1; then + echo "FAIL: accepted $mode header compiler with stale output" >&2; exit 1 + fi + grep -q '^FAIL:' "$work/bundle/result.txt" + for file in kernel.c libidris2_refc.a libidris2_support.a identity.log refc/stale.h support/stale.h; do + test ! -e "$work/bundle/$file" + done + if (cd "$root/ffi/zig"; "$zig" build --build-file build_standalone.zig -Dheader-refc="$work/bundle") > "$evidence/$mode-consumer.log" 2>&1; then + echo 'FAIL: Zig accepted an incomplete header bundle' >&2; exit 1 + fi + grep -q 'header RefC bundle is incomplete' "$evidence/$mode-consumer.log" +done +grep -q 'requires Idris2' "$evidence/version.log" +grep -q 'Error: injected header compiler diagnostic' "$evidence/diagnostic.log" +grep -q 'no valid RefC output' "$evidence/empty.log" +echo PASS > "$evidence/result.txt" +echo 'PASS: failed header producers remove stale artifacts and Zig refuses their bundles' From b362bbfcbad4ad260b5c6e52c356314de5d8ecfb Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:29:17 +0000 Subject: [PATCH 12/24] Replace totality escapes with structural descent and explicit parser refusal --- src/Proven/SafeArgs/Parser.idr | 59 ++++++------- src/Proven/SafeRegex/Matcher.idr | 10 +-- src/Proven/SafeTree.idr | 23 +++--- src/Proven/SafeXML/Parser.idr | 40 +++++---- src/Proven/SafeYAML/Parser.idr | 84 +++++++++++-------- tests/properties/SafeMathProps.idr | 115 +++++++++++--------------- tests/totality/BoundaryRegression.idr | 75 +++++++++++++++++ 7 files changed, 236 insertions(+), 170 deletions(-) create mode 100644 tests/totality/BoundaryRegression.idr diff --git a/src/Proven/SafeArgs/Parser.idr b/src/Proven/SafeArgs/Parser.idr index a6ed742b..7eb86ad9 100644 --- a/src/Proven/SafeArgs/Parser.idr +++ b/src/Proven/SafeArgs/Parser.idr @@ -182,7 +182,7 @@ parseArgs opts specs args = do traverse_ (checkArgLength opts) args -- Parse let state = initialState args - finalState <- parseLoop opts specs state + finalState <- parseLoop opts specs args state -- Check required checkRequired specs finalState.parsed -- DEFECT FIXED 2026-08-27: the do-block previously ended on checkRequired, @@ -192,16 +192,9 @@ parseArgs opts specs args = do Ok finalState.parsed where mutual - parseLoop : ParserOptions -> List ArgSpec -> ParserState -> ArgResult ParserState - -- TRUSTED: totality; budgeted per TRUSTED-BASE-REDUCTION-POLICY.adoc. - -- Measure: length state.remaining, which strictly decreases on all 14 - -- self-recursive calls (each matches (arg :: rest) and recurses on rest). - -- Invisible to size-change termination because every recursive call threads - -- the decrease through a RECORD UPDATE ({ remaining := rest } state), which - -- is opaque to the structural checker: neither "equal" nor "smaller". - -- Production fix: hoist `remaining` out of ParserState into an explicit - -- List String argument so the decrease is structurally visible. - parseLoop opts specs state = assert_total $ case state.remaining of + -- Remaining input is explicit, so the checker sees every list descent. + parseLoop : ParserOptions -> List ArgSpec -> List String -> ParserState -> ArgResult ParserState + parseLoop opts specs remaining state = case remaining of [] => Ok state (arg :: rest) => if state.afterSeparator @@ -210,30 +203,30 @@ parseArgs opts specs args = do in Ok ({ remaining := [], parsed := newParsed } state) else case classifyArg opts arg of EndOfOpts => - parseLoop opts specs ({ remaining := rest, afterSeparator := True } state) + parseLoop opts specs rest ({ remaining := rest, afterSeparator := True } state) LongOpt name => case findByLong specs name of Nothing => if opts.allowUnknown - then parseLoop opts specs ({ remaining := rest } state) + then parseLoop opts specs rest ({ remaining := rest } state) else Err (UnknownOption ("--" ++ name)) Just spec => if spec.argType == Flag then let newParsed = (specName spec, FlagSet) :: state.parsed - in parseLoop opts specs ({ remaining := rest, parsed := newParsed } state) + in parseLoop opts specs rest ({ remaining := rest, parsed := newParsed } state) else case rest of [] => Err (MissingValue ("--" ++ name)) (val :: rest') => do validated <- validateAllowed spec val let newParsed = (specName spec, OptionValue validated) :: state.parsed - parseLoop opts specs ({ remaining := rest', parsed := newParsed } state) + parseLoop opts specs rest' ({ remaining := rest', parsed := newParsed } state) LongOptEq name value => case findByLong specs name of Nothing => if opts.allowUnknown - then parseLoop opts specs ({ remaining := rest } state) + then parseLoop opts specs rest ({ remaining := rest } state) else Err (UnknownOption ("--" ++ name)) Just spec => if spec.argType == Flag @@ -241,30 +234,30 @@ parseArgs opts specs args = do else do validated <- validateAllowed spec value let newParsed = (specName spec, OptionValue validated) :: state.parsed - parseLoop opts specs ({ remaining := rest, parsed := newParsed } state) + parseLoop opts specs rest ({ remaining := rest, parsed := newParsed } state) ShortOpt c => case findByShort specs c of Nothing => if opts.allowUnknown - then parseLoop opts specs ({ remaining := rest } state) + then parseLoop opts specs rest ({ remaining := rest } state) else Err (UnknownOption ("-" ++ singleton c)) Just spec => if spec.argType == Flag then let newParsed = (specName spec, FlagSet) :: state.parsed - in parseLoop opts specs ({ remaining := rest, parsed := newParsed } state) + in parseLoop opts specs rest ({ remaining := rest, parsed := newParsed } state) else case rest of [] => Err (MissingValue ("-" ++ singleton c)) (val :: rest') => do validated <- validateAllowed spec val let newParsed = (specName spec, OptionValue validated) :: state.parsed - parseLoop opts specs ({ remaining := rest', parsed := newParsed } state) + parseLoop opts specs rest' ({ remaining := rest', parsed := newParsed } state) ShortOptVal c value => case findByShort specs c of Nothing => if opts.allowUnknown - then parseLoop opts specs ({ remaining := rest } state) + then parseLoop opts specs rest ({ remaining := rest } state) else Err (UnknownOption ("-" ++ singleton c)) Just spec => if spec.argType == Flag @@ -272,10 +265,11 @@ parseArgs opts specs args = do else do validated <- validateAllowed spec value let newParsed = (specName spec, OptionValue validated) :: state.parsed - parseLoop opts specs ({ remaining := rest, parsed := newParsed } state) + parseLoop opts specs rest ({ remaining := rest, parsed := newParsed } state) BundledOpts chars => - parseBundled opts specs state rest chars + do bundled <- parseBundled opts specs state chars + parseLoop opts specs rest ({ remaining := rest } bundled) PositionalArg value => if opts.stopAtNonOption @@ -284,30 +278,27 @@ parseArgs opts specs args = do else let posSpecs = filter (\s => s.argType == Positional) specs in if state.positionalCount >= length posSpecs then if opts.allowUnknown - then parseLoop opts specs ({ remaining := rest } state) + then parseLoop opts specs rest ({ remaining := rest } state) else Err (TooManyPositional (S state.positionalCount) (length posSpecs)) else let newParsed = ("positional-" ++ show state.positionalCount, PositionalValue value) :: state.parsed - in parseLoop opts specs ({ remaining := rest + in parseLoop opts specs rest ({ remaining := rest , parsed := newParsed , positionalCount := S state.positionalCount } state) - parseBundled : ParserOptions -> List ArgSpec -> ParserState -> List String -> List Char -> ArgResult ParserState - -- TRUSTED: totality; assert_total on the parseBundled -> parseLoop edge. - -- Each cycle takes remaining from (arg :: rest) to rest; the decrease is threaded - -- through a record update so the structural checker cannot see it. - -- Production fix: hoist `remaining` out of ParserState into an explicit argument. - parseBundled opts specs state rest [] = assert_total (parseLoop opts specs ({ remaining := rest } state)) - parseBundled opts specs state rest (c :: cs) = + -- Bundles only consume characters; the outer loop resumes on its tail. + parseBundled : ParserOptions -> List ArgSpec -> ParserState -> List Char -> ArgResult ParserState + parseBundled opts specs state [] = Ok state + parseBundled opts specs state (c :: cs) = case findByShort specs c of Nothing => if opts.allowUnknown - then parseBundled opts specs state rest cs + then parseBundled opts specs state cs else Err (UnknownOption ("-" ++ singleton c)) Just spec => if spec.argType /= Flag then Err (InvalidFormat ("-" ++ pack (c :: cs)) "only flags can be bundled") else let newParsed = (specName spec, FlagSet) :: state.parsed - in parseBundled opts specs ({ parsed := newParsed } state) rest cs + in parseBundled opts specs ({ parsed := newParsed } state) cs checkRequired : List ArgSpec -> ParsedArgs -> ArgResult () checkRequired [] _ = Ok () diff --git a/src/Proven/SafeRegex/Matcher.idr b/src/Proven/SafeRegex/Matcher.idr index d6e67cdf..d891c2d9 100644 --- a/src/Proven/SafeRegex/Matcher.idr +++ b/src/Proven/SafeRegex/Matcher.idr @@ -68,7 +68,7 @@ currentChar : MatchState -> Maybe Char currentChar st = if st.position >= st.inputLen then Nothing - else Just $ assert_total $ strIndex st.input (cast st.position) + else head' (drop st.position (unpack st.input)) ||| Get character at specific position public export @@ -76,7 +76,7 @@ charAt : MatchState -> Nat -> Maybe Char charAt st pos = if pos >= st.inputLen then Nothing - else Just $ assert_total $ strIndex st.input (cast pos) + else head' (drop pos (unpack st.input)) ||| Check if at end of input public export @@ -442,9 +442,9 @@ replaceAll sr input replacement = go (S inputLen) 0 "" newPos = max (S pos) end in go fuel newPos (acc ++ before ++ replacement) _ => - if pos < inputLen - then go fuel (S pos) (acc ++ singleton (assert_total $ strIndex input (cast pos))) - else acc + case head' (drop pos (unpack input)) of + Just c => go fuel (S pos) (acc ++ singleton c) + Nothing => acc ||| Split string by regex public export diff --git a/src/Proven/SafeTree.idr b/src/Proven/SafeTree.idr index 3a97d5f3..e4c02293 100644 --- a/src/Proven/SafeTree.idr +++ b/src/Proven/SafeTree.idr @@ -114,19 +114,18 @@ postorder (Node v l r) = postorder l ++ postorder r ++ [v] ||| Level-order (breadth-first) traversal public export levelorder : BinaryTree a -> List a -levelorder tree = bfs [tree] +levelorder tree = concat (levels tree) where - bfs : List (BinaryTree a) -> List a - bfs [] = [] - bfs (Leaf :: rest) = bfs rest - -- TRUSTED: totality; budgeted per TRUSTED-BASE-REDUCTION-POLICY.adoc. - -- Breadth-first queue: `rest ++ [l, r]` GROWS the argument, so no structural - -- measure exists. Termination is on total remaining node count, which is not - -- an argument. A fuel parameter was REJECTED: the correct bound is 2n+1 queue - -- pops for n internal nodes, and an off-by-one fuel would silently TRUNCATE - -- the traversal -- a correctness bug no test would catch. Declared debt is - -- preferable to silent wrongness. - bfs (Node v l r :: rest) = v :: assert_total (bfs (rest ++ [l, r])) + -- Combine equal depths, preserving left-to-right order. Both recursive + -- functions descend on explicit data; no queue bound can truncate a tree. + mergeLevels : List (List a) -> List (List a) -> List (List a) + mergeLevels [] ys = ys + mergeLevels xs [] = xs + mergeLevels (x :: xs) (y :: ys) = (x ++ y) :: mergeLevels xs ys + + levels : BinaryTree a -> List (List a) + levels Leaf = [] + levels (Node v l r) = [v] :: mergeLevels (levels l) (levels r) -------------------------------------------------------------------------------- -- Binary Search Tree Operations diff --git a/src/Proven/SafeXML/Parser.idr b/src/Proven/SafeXML/Parser.idr index cf5c02d2..2e7a5036 100644 --- a/src/Proven/SafeXML/Parser.idr +++ b/src/Proven/SafeXML/Parser.idr @@ -189,8 +189,9 @@ parseAttribute st = do Ok (MkXMLAttr name value, st5) ||| Parse attributes until > or /> -parseAttributes : ParserState -> XMLResult (List XMLAttr, ParserState) -parseAttributes st = +parseAttributes : Nat -> ParserState -> XMLResult (List XMLAttr, ParserState) +parseAttributes Z st = Err (MalformedXML "Attribute parsing budget exhausted") +parseAttributes (S fuel) st = let st' = skipWhitespace st in case peek st' of Just '>' => Ok ([], st') @@ -209,7 +210,7 @@ parseAttributes st = -- after recursion, which is the per-element count `maxAttributesPerElement` -- names. `>=` on a pre-increment count becomes `>` on the total. (attr, st1) <- parseAttribute st' - (rest, st2) <- assert_total (parseAttributes st1) + (rest, st2) <- parseAttributes fuel st1 if length (attr :: rest) > st.options.maxAttributesPerElement then Err (MalformedXML "Too many attributes") else Ok (attr :: rest, st2) @@ -310,16 +311,20 @@ parsePI st = -- Element Parsing -------------------------------------------------------------------------------- +-- Each recursive edge spends one unit, including dispatch to a child element. +-- Input-sized fuel bounds call depth; exhaustion is an error, never partial +-- success. This is a termination mechanism, not a proved completeness bound. -- Parse element (recursive) -- NOTE: a ||| docstring cannot precede a mutual block in Idris2; demoted to a comment. mutual - parseElement : ParserState -> XMLResult (XMLNode, ParserState) - parseElement st = do + parseElement : Nat -> ParserState -> XMLResult (XMLNode, ParserState) + parseElement Z st = Err (MalformedXML "Element parsing budget exhausted") + parseElement (S fuel) st = do -- Check depth limit checkDepth st st1 <- expect "<" st (name, st2) <- parseName st1 - (attrs, st3) <- parseAttributes st2 + (attrs, st3) <- parseAttributes (S (length st2.input)) st2 -- Check for self-closing or start tag case peekN 2 st3 of ['/', '>'] => do @@ -328,7 +333,7 @@ mutual _ => do st4 <- expect ">" st3 let st5 = pushDepth st4 - (children, st6) <- assert_total (parseChildren st5) + (children, st6) <- parseChildren fuel st5 -- Expect closing tag st7 <- expect " XMLResult (List XMLNode, ParserState) - parseChildren st = + parseChildren : Nat -> ParserState -> XMLResult (List XMLNode, ParserState) + parseChildren Z st = Err (MalformedXML "Child parsing budget exhausted") + parseChildren (S fuel) st = let st' = skipWhitespace st in case peekN 2 st' of ['<', '/'] => Ok ([], st') -- End tag @@ -350,11 +356,11 @@ mutual case peekN 4 st' of ['<', '!', '-', '-'] => do (comment, st1) <- parseComment st' - (rest, st2) <- assert_total (parseChildren st1) + (rest, st2) <- parseChildren fuel st1 Ok (comment :: rest, st2) ['<', '!', '[', 'C'] => do (cdata, st1) <- parseCDATA st' - (rest, st2) <- assert_total (parseChildren st1) + (rest, st2) <- parseChildren fuel st1 Ok (cdata :: rest, st2) ['<', '!', 'D', 'O'] => -- DOCTYPE not allowed here @@ -362,11 +368,11 @@ mutual _ => Err (MalformedXML "Invalid markup declaration") ['<', '?'] => do (pi, st1) <- parsePI st' - (rest, st2) <- assert_total (parseChildren st1) + (rest, st2) <- parseChildren fuel st1 Ok (pi :: rest, st2) ['<', _] => do - (elem, st1) <- assert_total (parseElement st') - (rest, st2) <- assert_total (parseChildren st1) + (elem, st1) <- parseElement fuel st' + (rest, st2) <- parseChildren fuel st1 Ok (elem :: rest, st2) _ => if isEOF st' @@ -374,7 +380,7 @@ mutual else do -- Parse text content (text, st1) <- parseTextContent st' - (rest, st2) <- assert_total (parseChildren st1) + (rest, st2) <- parseChildren fuel st1 Ok (Text text :: rest, st2) -------------------------------------------------------------------------------- @@ -437,7 +443,7 @@ parseDocument opts input = do st3 <- skipDoctype st2 let st4 = skipWhitespace st3 -- Parse root element - (root, st5) <- parseElement st4 + (root, st5) <- parseElement (2 * S (length st4.input)) st4 let st6 = skipWhitespace st5 -- Ensure no trailing content if isEOF st6 @@ -477,5 +483,5 @@ public export parseFragment : String -> XMLResult XMLNode parseFragment input = do let st = initParser secureDefaults input - (node, _) <- parseElement st + (node, _) <- parseElement (2 * S (length st.input)) st Ok node diff --git a/src/Proven/SafeYAML/Parser.idr b/src/Proven/SafeYAML/Parser.idr index 3eb63e47..dedd3b04 100644 --- a/src/Proven/SafeYAML/Parser.idr +++ b/src/Proven/SafeYAML/Parser.idr @@ -202,11 +202,12 @@ parseQuotedString quote st = (Just c, st1) => if c /= quote then Err (SyntaxError ("Expected " ++ singleton quote) st.line st.col) - else parseContent [] st1 + else parseContent (S (length st1.input)) [] st1 (Nothing, _) => Err (SyntaxError "Unexpected end of input" st.line st.col) where - parseContent : List Char -> ParserState -> YAMLResult (String, ParserState) - parseContent acc st = assert_total $ + parseContent : Nat -> List Char -> ParserState -> YAMLResult (String, ParserState) + parseContent Z acc st = Err (SyntaxError "String parsing budget exhausted" st.line st.col) + parseContent (S fuel) acc st = case consume st of (Nothing, _) => Err (SyntaxError "Unclosed string" st.line st.col) (Just c, st') => @@ -223,9 +224,9 @@ parseQuotedString quote st = '"' => '"' '\'' => '\'' _ => escaped - in parseContent (unescaped :: acc) st'' + in parseContent fuel (unescaped :: acc) st'' (Nothing, _) => Err (SyntaxError "Unclosed escape" st.line st.col) - else parseContent (c :: acc) st' + else parseContent fuel (c :: acc) st' ||| Parse unquoted string (plain scalar) parsePlainScalar : ParserState -> YAMLResult (String, ParserState) @@ -253,6 +254,9 @@ pushDepth st = { depth := S st.depth } st popDepth : ParserState -> ParserState popDepth st = { depth := pred st.depth } st +-- Recursive parser edges consume explicit fuel. Exhaustion returns an error, +-- never a partial value. The input-derived allowance is tested below, not +-- claimed here as a machine-proved completeness bound. -- Forward declarations: parseFlowSequence/parseFlowMapping and parseValue -- form a mutually recursive cluster with parseAnchor, parseNumeric, -- parseAlias and parseTaggedValue. Idris2 resolves top-level @@ -260,10 +264,10 @@ popDepth st = { depth := pred st.depth } st -- defined in place below. (Split decl/def is legal at top level; a `mutual` -- block would work too but would re-indent ~130 lines for no gain.) ||| Parse any value -parseValue : ParserState -> YAMLResult (YAMLValue, ParserState) +parseValue : Nat -> ParserState -> YAMLResult (YAMLValue, ParserState) ||| Parse anchor definition &name -parseAnchor : ParserState -> YAMLResult (YAMLValue, ParserState) +parseAnchor : Nat -> ParserState -> YAMLResult (YAMLValue, ParserState) ||| Parse numeric value (int or float) parseNumeric : ParserState -> YAMLResult (YAMLValue, ParserState) @@ -272,39 +276,42 @@ parseNumeric : ParserState -> YAMLResult (YAMLValue, ParserState) parseAlias : ParserState -> YAMLResult (YAMLValue, ParserState) ||| Parse tagged value !tag value -parseTaggedValue : ParserState -> YAMLResult (YAMLValue, ParserState) +parseTaggedValue : Nat -> ParserState -> YAMLResult (YAMLValue, ParserState) ||| Parse flow sequence [item, item, ...] -parseFlowSequence : ParserState -> YAMLResult (YAMLValue, ParserState) -parseFlowSequence st = do +parseFlowSequence : Nat -> ParserState -> YAMLResult (YAMLValue, ParserState) +parseFlowSequence Z st = Err (SyntaxError "Value parsing budget exhausted" st.line st.col) +parseFlowSequence (S fuel) st = do checkDepth st case consume st of (Just '[', st1) => let st2 = pushDepth (skipSpaces st1) - in parseItems [] st2 + in parseItems fuel [] st2 _ => Err (SyntaxError "Expected '['" st.line st.col) where - parseItems : List YAMLValue -> ParserState -> YAMLResult (YAMLValue, ParserState) - parseItems acc st = assert_total $ + parseItems : Nat -> List YAMLValue -> ParserState -> YAMLResult (YAMLValue, ParserState) + parseItems Z acc st = Err (SyntaxError "Collection parsing budget exhausted" st.line st.col) + parseItems (S remaining) acc st = let st' = skipSpaces (skipComment st) in case peek st' of Just ']' => Ok (YArray (reverse acc), popDepth (snd (consume st'))) _ => do - (val, st1) <- parseValue st' + (val, st1) <- parseValue remaining st' let st2 = skipSpaces st1 case peek st2 of - Just ',' => parseItems (val :: acc) (skipSpaces (snd (consume st2))) + Just ',' => parseItems remaining (val :: acc) (skipSpaces (snd (consume st2))) Just ']' => Ok (YArray (reverse (val :: acc)), popDepth (snd (consume st2))) _ => Err (SyntaxError "Expected ',' or ']'" st2.line st2.col) ||| Parse flow mapping {key: value, ...} -parseFlowMapping : ParserState -> YAMLResult (YAMLValue, ParserState) -parseFlowMapping st = do +parseFlowMapping : Nat -> ParserState -> YAMLResult (YAMLValue, ParserState) +parseFlowMapping Z st = Err (SyntaxError "Value parsing budget exhausted" st.line st.col) +parseFlowMapping (S fuel) st = do checkDepth st case consume st of (Just '{', st1) => let st2 = pushDepth (skipSpaces st1) - in parsePairs [] st2 + in parsePairs fuel [] st2 _ => Err (SyntaxError "Expected '{'" st.line st.col) where parseKey : ParserState -> YAMLResult (String, ParserState) @@ -313,8 +320,9 @@ parseFlowMapping st = do Just '\'' => parseQuotedString '\'' st _ => parsePlainScalar st - parsePairs : List (String, YAMLValue) -> ParserState -> YAMLResult (YAMLValue, ParserState) - parsePairs acc st = assert_total $ + parsePairs : Nat -> List (String, YAMLValue) -> ParserState -> YAMLResult (YAMLValue, ParserState) + parsePairs Z acc st = Err (SyntaxError "Collection parsing budget exhausted" st.line st.col) + parsePairs (S remaining) acc st = let st' = skipSpaces (skipComment st) in case peek st' of Just '}' => Ok (YObject (reverse acc), popDepth (snd (consume st'))) @@ -324,21 +332,22 @@ parseFlowMapping st = do let st2 = skipSpaces st1 case consume st2 of (Just ':', st3) => do - (val, st4) <- parseValue (skipSpaces st3) + (val, st4) <- parseValue remaining (skipSpaces st3) let st5 = skipSpaces st4 case peek st5 of - Just ',' => parsePairs ((key, val) :: acc) (skipSpaces (snd (consume st5))) + Just ',' => parsePairs remaining ((key, val) :: acc) (skipSpaces (snd (consume st5))) Just '}' => Ok (YObject (reverse ((key, val) :: acc)), popDepth (snd (consume st5))) _ => Err (SyntaxError "Expected ',' or '}'" st5.line st5.col) _ => Err (SyntaxError "Expected ':'" st2.line st2.col) -- Parse any value (signature forward-declared above) -parseValue st = +parseValue Z st = Err (SyntaxError "Value parsing budget exhausted" st.line st.col) +parseValue (S fuel) st = let st' = skipSpaces (skipComment st) in case peek st' of Nothing => Err (SyntaxError "Unexpected end of input" st'.line st'.col) - Just '[' => parseFlowSequence st' - Just '{' => parseFlowMapping st' + Just '[' => parseFlowSequence fuel st' + Just '{' => parseFlowMapping fuel st' Just '"' => do (s, st1) <- parseQuotedString '"' st' checkValueSize s st @@ -347,9 +356,9 @@ parseValue st = (s, st1) <- parseQuotedString '\'' st' checkValueSize s st Ok (YString s, st1) - Just '&' => parseAnchor st' + Just '&' => parseAnchor fuel st' Just '*' => parseAlias st' - Just '!' => parseTaggedValue st' + Just '!' => parseTaggedValue fuel st' Just c => if isDigit c || c == '-' || c == '+' then parseNumeric st' @@ -386,7 +395,8 @@ parseNumeric st = in sign * foldl (\acc, c => acc * 10 + cast (ord c - ord '0')) 0 digits -- Parse anchor definition &name (signature forward-declared above) -parseAnchor st = assert_total $ +parseAnchor Z st = Err (SyntaxError "Value parsing budget exhausted" st.line st.col) +parseAnchor (S fuel) st = if not st.options.allowAnchors then Err (DangerousTag "anchor (disabled)") else do @@ -395,7 +405,7 @@ parseAnchor st = assert_total $ let (name, st2) = consumeWhile isAnchorChar st1 st3 = skipSpaces st2 in do - (val, st4) <- parseValue st3 + (val, st4) <- parseValue fuel st3 let st5 = { anchors := (name, val) :: st4.anchors } st4 Ok (val, st5) _ => Err (SyntaxError "Expected '&'" st.line st.col) @@ -423,7 +433,8 @@ parseAlias st = isAnchorChar c = isAlphaNum c || c == '_' || c == '-' -- Parse tagged value !tag value (signature forward-declared above) -parseTaggedValue st = assert_total $ do +parseTaggedValue Z st = Err (SyntaxError "Value parsing budget exhausted" st.line st.col) +parseTaggedValue (S fuel) st = do case consume st of (Just '!', st1) => let (tag, st2) = consumeWhile isTagChar st1 @@ -431,7 +442,7 @@ parseTaggedValue st = assert_total $ do in do checkTag fullTag st let st3 = skipSpaces st2 - (val, st4) <- parseValue st3 + (val, st4) <- parseValue fuel st3 -- Apply tag transformation Ok (applyTag fullTag val, st4) _ => Err (SyntaxError "Expected '!'" st.line st.col) @@ -508,7 +519,7 @@ parseDocument st = do -- Skip document start marker let st2 = parseDocStart (skipSpaces st1) -- Parse value - (val, st3) <- parseValue (skipSpaces st2) + (val, st3) <- parseValue (3 * S (length st2.input)) (skipSpaces st2) -- Skip document end marker let st4 = parseDocEnd (skipSpaces st3) Ok (MkYAMLDocument version [] val, st4) @@ -518,10 +529,11 @@ public export parseStream : YAMLSecurityOptions -> String -> YAMLResult YAMLStream parseStream opts input = do let st = initParser opts input - go 0 [] st + go (S (length st.input)) 0 [] st where - go : Nat -> List YAMLDocument -> ParserState -> YAMLResult YAMLStream - go count acc st = assert_total $ + go : Nat -> Nat -> List YAMLDocument -> ParserState -> YAMLResult YAMLStream + go Z count acc st = Err (SyntaxError "Stream parsing budget exhausted" st.line st.col) + go (S fuel) count acc st = let st' = skipSpaces (skipComment st) in if isEOF st' then Ok (reverse acc) @@ -530,7 +542,7 @@ parseStream opts input = do then Err (TooManyDocuments count opts.maxDocuments) else do (doc, st1) <- parseDocument st' - go (S count) (doc :: acc) st1 + go fuel (S count) (doc :: acc) st1 -------------------------------------------------------------------------------- -- Public API diff --git a/tests/properties/SafeMathProps.idr b/tests/properties/SafeMathProps.idr index e965e874..04b89e1f 100644 --- a/tests/properties/SafeMathProps.idr +++ b/tests/properties/SafeMathProps.idr @@ -4,80 +4,63 @@ module SafeMathProps import Proven.Core import Proven.SafeMath +import Data.List +import System %default total -||| Property: Safe addition is commutative -prop_addCommutative : (x, y : Integer) -> safeAdd x y = safeAdd y x -prop_addCommutative x y = Refl - -||| Property: Safe addition of zero is identity -prop_addIdentity : (x : Integer) -> safeAdd x 0 = Ok x -prop_addIdentity x = Refl - -||| Property: Safe subtraction of zero is identity -prop_subIdentity : (x : Integer) -> safeSub x 0 = Ok x -prop_subIdentity x = Refl - -||| Property: Safe division by 1 is identity -prop_divByOne : (x : Integer) -> x >= 0 -> safeDiv x 1 = Ok x -prop_divByOne x _ = Refl - -||| Property: Safe division by zero returns error -prop_divByZeroFails : (x : Integer) -> isErr (safeDiv x 0) = True +||| These are universal definitional proofs of the actual Maybe-returning API. +prop_divByZeroFails : (x : Integer) -> safeDiv x 0 = Nothing prop_divByZeroFails x = Refl -||| OWED: Bounded values stay within bounds -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_boundedInRange_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_boundedInRange : (b : Bounded min max) -> - (getValue b >= min) && (getValue b <= max) = True - -||| Property: NonEmpty list always has at least one element -prop_nonEmptyHasElements : (ne : NonEmpty a) -> length (toList ne) >= 1 = True -prop_nonEmptyHasElements (MkNonEmpty h t) = Refl - -||| OWED: Safe modulo never crashes -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_modNeverCrashes_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_modNeverCrashes : (x, y : Integer) -> y /= 0 -> - isOk (safeMod x y) = True - -||| OWED: Absolute value is always non-negative -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_absNonNegative_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_absNonNegative : (x : Integer) -> safeAbs x >= 0 = True +prop_modByZeroFails : (x : Integer) -> safeMod x 0 = Nothing +prop_modByZeroFails x = Refl -||| Property: Safe negate is involutive (negating twice returns original) -prop_negateInvolutive : (x : Integer) -> safeNegate (safeNegate x) = x -prop_negateInvolutive x = Refl +-- The former safeAdd/safeSub/safeNegate names and Boolean-as-Type statements +-- did not describe this API. General arithmetic and bounds theorems remain +-- proof obligations; the following finite executions do not discharge them. +samples : List Integer +samples = [minInt64, minInt64 + 1, -1024, -2, -1, 0, 1, 2, 1024, + maxInt64 - 1, maxInt64] -||| Property: Overflow detection catches MAX_INT + 1 -prop_overflowDetected : detectOverflow MaxInt 1 = True -prop_overflowDetected = Refl +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| Test runner for properties export runMathProperties : IO () runMathProperties = do - putStrLn "SafeMath Property Tests" - putStrLn "======================" - putStrLn "prop_addCommutative: PASS (proven by type)" - putStrLn "prop_addIdentity: PASS (proven by type)" - putStrLn "prop_subIdentity: PASS (proven by type)" - putStrLn "prop_divByOne: PASS (proven by type)" - putStrLn "prop_divByZeroFails: PASS (proven by type)" - putStrLn "prop_nonEmptyHasElements: PASS (proven by type)" - putStrLn "prop_negateInvolutive: PASS (proven by type)" - putStrLn "prop_overflowDetected: PASS (proven by type)" - putStrLn "" - putStrLn "All type-level proofs verified by Idris 2 compiler." + check "checked addition commutes on boundary corpus" + (all (\x => all (\y => addChecked x y == addChecked y x) samples) samples) + check "checked addition/subtraction zero" + (all (\x => addChecked x 0 == Just x && subChecked x 0 == Just x) samples) + check "division by one" + (all (\x => safeDiv x 1 == Just x) samples) + check "zero denominators" + (all (\x => safeDiv x 0 == Nothing && safeMod x 0 == Nothing) samples) + check "positive modulo denominator: quotient/remainder identity" + (all (\x => all (\d => case (safeDiv x d, safeMod x d) of + (Just q, Just r) => x == q * d + r && r >= 0 && r < d + _ => False) [1, 2, 7, 1024]) samples) + check "overflow and underflow refused" + (addChecked maxInt64 1 == Nothing && subChecked minInt64 1 == Nothing && + mulChecked maxInt64 2 == Nothing && mulChecked minInt64 (-1) == Nothing) + check "absolute value boundary" + (absSafe minInt64 == Nothing && absClamped minInt64 == maxInt64 && + all (\x => case absSafe x of + Nothing => x == minInt64 + Just n => n >= 0) samples) + check "checked negate boundary and involution" + (negateInt64 minInt64 == Nothing && + all (\x => case negateInt64 x of + Nothing => x == minInt64 + Just n => negateInt64 n == Just x) samples) + check "percentage construction rejects out-of-range input" + (case (toPercentage 0, toPercentage 100, toPercentage 101) of + (Just lo, Just hi, Nothing) => percentageValue lo == 0 && percentageValue hi == 100 + _ => False) + check "clamping boundaries" + (all (\x => let y = clamp (-10) 10 x in y >= -10 && y <= 10) samples) + putStrLn "Two definitional zero-denominator proofs typechecked; other checks are finite execution." diff --git a/tests/totality/BoundaryRegression.idr b/tests/totality/BoundaryRegression.idr new file mode 100644 index 00000000..33b4c833 --- /dev/null +++ b/tests/totality/BoundaryRegression.idr @@ -0,0 +1,75 @@ +-- SPDX-License-Identifier: MPL-2.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +module BoundaryRegression + +import Proven.Core +import Proven.SafeTree +import Proven.SafeArgs.Types +import Proven.SafeArgs.Parser +import Proven.SafeRegex.Types +import Proven.SafeRegex.Matcher +import Proven.SafeXML.Parser +import Proven.SafeXML.Types +import Proven.SafeYAML.Parser +import Proven.SafeYAML.Types +import SafeMathProps +import Data.List +import Data.String +import System + +%default total + +check : String -> Bool -> IO () +check label True = putStrLn ("PASS: " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure + +tree : BinaryTree Nat +tree = Node 1 (Node 2 (Node 4 Leaf Leaf) Leaf) + (Node 3 (Node 5 Leaf Leaf) (Node 6 Leaf Leaf)) + +nestedXML : Nat -> String +nestedXML Z = "text" +nestedXML (S n) = "" ++ nestedXML n ++ "" + +nestedYAML : Nat -> String +nestedYAML Z = "true" +nestedYAML (S n) = "[" ++ nestedYAML n ++ "]" + +main : IO () +main = do + check "tree order including asymmetric levels" (levelorder tree == [1,2,3,4,5,6]) + check "empty tree" (levelorder (the (BinaryTree Nat) Leaf) == []) + check "bundles resume outer argument loop" + (case parseArgs defaultParserOptions + [flag "alpha" 'a' "", flag "beta" 'b' "", option "value" 'v' ""] + ["-ab", "--value", "payload", "--", "-ignored"] of + Ok parsed => getFlag parsed "alpha" && getFlag parsed "beta" && + getOption parsed "value" == Just "payload" && + getRest parsed == ["-ignored"] + Err _ => False) + check "missing option value refused" + (isErr (parseArgs defaultParserOptions [option "value" 'v' ""] ["-v"])) + let state = initState "aλ" 100 defaultFlags + check "regex character and end lookup" + (currentChar state == Just 'a' && charAt state 1 == Just 'λ' && + charAt state 2 == Nothing) + check "forged length cannot reach a partial string primitive" + (charAt ({ inputLen := 100 } state) 50 == Nothing) + check "XML nested and sibling/attribute paths" + (isOk (parseXML (nestedXML 20)) && + isOk (parseXML "text")) + check "XML malformed and security refusals" + (isErr (parseXML "") && isErr (parseXML "") && + isErr (parseXML "")) + check "XML depth limit remains effective" (isErr (parseXML (nestedXML 105))) + check "YAML flow collections and quoted escape" + (isOk (parseYAML (nestedYAML 20)) && + isOk (parseYAML "{\"key\": [true, false, \"a\\n\"]}")) + check "YAML malformed/forbidden-anchor refusals" + (isErr (parseYAML "[true") && isErr (parseYAML "\"unclosed") && + isErr (parseYAML "&a true")) + check "YAML depth limit remains effective" (isErr (parseYAML (nestedYAML 55))) + runMathProperties + putStrLn "Finite regression execution passed; parser completeness/equivalence is not claimed." From 3563d6523e2a8293f9d2e150555a830bdccafcbe Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:30:04 +0000 Subject: [PATCH 13/24] Run source totality regressions and expose every legacy property failure in CI --- .github/workflows/idris2-ci.yml | 25 +++++++++++++ docs/adr/2026-11-16-header-refc-boundary.adoc | 21 +++++++++++ scripts/test-totality-regressions.sh | 36 +++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 scripts/test-totality-regressions.sh diff --git a/.github/workflows/idris2-ci.yml b/.github/workflows/idris2-ci.yml index b661ec5f..3843edeb 100644 --- a/.github/workflows/idris2-ci.yml +++ b/.github/workflows/idris2-ci.yml @@ -67,8 +67,33 @@ jobs: run: idris2 --build proven.ipkg - name: Install proven library (required for tests.ipkg) run: idris2 --install proven.ipkg + - name: Check totality repairs against actual source + run: bash scripts/test-totality-regressions.sh + - name: Preserve totality regression evidence + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: totality-regressions + path: build/totality-regressions/ + - name: Diagnose every property module + if: ${{ !cancelled() }} + run: | + set -uo pipefail + fail=0 + cd tests/properties + for source in *Props.idr; do + echo "::group::$source" + if ! idris2 -p proven --check "$source" 2>&1 | tee "/tmp/$source.log"; then + fail=1 + fi + if grep -q '^Error:' "/tmp/$source.log"; then fail=1; fi + echo "::endgroup::" + done + exit "$fail" - name: Build property-test suite (tests.ipkg) run: idris2 --build tests.ipkg + - name: Execute property-test suite + run: build/tests/exec/proven_tests - name: Type check all modules run: | idris2 --check src/Proven.idr diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc index e6cc587f..b1b03b1e 100644 --- a/docs/adr/2026-11-16-header-refc-boundary.adoc +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -149,3 +149,24 @@ visible and prevent a whole-repository green claim. The repository instructions name `./hypatia-v2`, but that executable is not present in this checkout. No successful local scan with that command is claimed. Existing hosted security checks remain enabled. + +== Follow-up: totality and executable property repairs + +The 20 production totality assertions are replaced by explicit list recursion +(argument parsing), total list lookup (regex), structural level merging (tree +traversal), and error-returning input-derived recursion budgets (XML/YAML). +The budgets establish termination; completeness and equivalence with an +unbounded reference parser remain separate proof obligations. Exhaustion never +returns a successful truncated parse. No full XML/YAML standards claim follows. + +The old arithmetic suite referred to nonexistent APIs and reported success +without execution. It now uses the actual Maybe-returning API, retains two +definitional zero-denominator proofs, and executes boundary arithmetic checks. +Its old general arithmetic, bounded-value and nonempty-list claims remain owed; +finite checks do not discharge them. Other legacy property modules are diagnosed +individually by remote CI without removing the full suite gate. + +The totality-regression script builds from a fresh source copy, requires +totality and no reported holes, executes malformed-input and semantic +regressions, and retains source identities, compiler output and a result marker. +These are regression receipts, not Atoma source-to-proof certificates. diff --git a/scripts/test-totality-regressions.sh b/scripts/test-totality-regressions.sh new file mode 100644 index 00000000..5c85bbd4 --- /dev/null +++ b/scripts/test-totality-regressions.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +set -euo pipefail +unset CDPATH +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +idris=$(command -v "${IDRIS2:-idris2}") +idris=$(cd "$(dirname "$idris")" && pwd)/$(basename "$idris") +out="$root/build/totality-regressions" +mkdir -p "$out" +printf 'INCOMPLETE\n' > "$out/result.txt" +work=$(mktemp -d -t proven-totality.XXXXXXXX) +trap 'status=$?; rm -rf -- "$work"; if ((status)); then printf "FAIL\n" > "$out/result.txt"; fi' EXIT +cp -R "$root/src" "$work/src" +cp "$root/tests/totality/BoundaryRegression.idr" "$work/src/" +cp "$root/tests/properties/SafeMathProps.idr" "$work/src/" +# Exclude copied compiler artefacts: only this fresh source may satisfy imports. +find "$work/src" -type d -name build -prune -exec rm -rf -- {} + +( + cd "$work" + find src -type f -name '*.idr' -print0 | sort -z | xargs -0 sha256sum +) > "$out/source-sha256.txt" +"$idris" --version > "$out/toolchain.txt" +if ! (cd "$work"; "$idris" --total -Werror -p contrib -p network --source-dir src \ + -o boundary-regression src/BoundaryRegression.idr) > "$out/build.log" 2>&1; then + cat "$out/build.log"; exit 1 +fi +if grep -q '^Error:' "$out/build.log" || [[ ! -x "$work/build/exec/boundary-regression" ]]; then + cat "$out/build.log"; exit 1 +fi +(cd "$work"; printf ':m\n:q\n' | "$idris" --no-banner -p contrib -p network \ + --source-dir src src/BoundaryRegression.idr) > "$out/holes.log" 2>&1 +grep -q 'No holes' "$out/holes.log" +if grep -q '^Error:' "$out/holes.log"; then cat "$out/holes.log"; exit 1; fi +"$work/build/exec/boundary-regression" | tee "$out/execution.log" +printf 'PASS\n' > "$out/result.txt" From 4a6c8e943168d5bc38a4d1c138abddc13b018456 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:41:31 +0000 Subject: [PATCH 14/24] Use the actual Idris 0.8 nonzero constructor in the arithmetic proof cone --- src/Proven/SafeMath/Proofs.idr | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Proven/SafeMath/Proofs.idr b/src/Proven/SafeMath/Proofs.idr index 5996e190..7fb1003e 100644 --- a/src/Proven/SafeMath/Proofs.idr +++ b/src/Proven/SafeMath/Proofs.idr @@ -12,10 +12,9 @@ ||| - `minus n 0` no longer reduces on abstract `n`; case-split required ||| - `div`/`mod`/`gcd` internal representations changed; some properties ||| are stated without proof pending upstream proof availability -||| - `SIsNonZero` was replaced by `ItIsSucc` here; REVERTED 2026-08-27. On the -||| toolchain that actually builds this repo (Idris 2 0.7.0), `NonZero` and -||| `IsSucc` are distinct types and `divNatNZ`/`modNatNZ` require `SIsNonZero`. -||| VERSION-SENSITIVE: re-adjudicate if the build ever runs on the 0.8.0 CI pins. +||| - The CI contract is Idris 2 0.8.0. Its Data.Nat defines NonZero = IsSucc +||| and deprecates SIsNonZero in favour of ItIsSucc. Use the constructor +||| directly so the proof cone also passes the warnings-as-errors gate. module Proven.SafeMath.Proofs import Proven.Core @@ -150,8 +149,8 @@ lteAntisym (LTESucc ab) (LTESucc ba) = cong S (lteAntisym ab ba) ||| Proved via Data.Nat.Division.DivisionTheoremUniqueness: ||| n = n * 1 + 0 is the unique decomposition, so divNatNZ n 1 = n. public export -divByOne : (n : Nat) -> divNatNZ n 1 SIsNonZero = n -divByOne n = fst $ DivisionTheoremUniqueness n 1 SIsNonZero n 0 (LTESucc LTEZero) (nTimesOnePlusZero n) +divByOne : (n : Nat) -> divNatNZ n 1 ItIsSucc = n +divByOne n = fst $ DivisionTheoremUniqueness n 1 ItIsSucc n 0 (LTESucc LTEZero) (nTimesOnePlusZero n) where nTimesOnePlusZero : (k : Nat) -> k = k * 1 + 0 nTimesOnePlusZero k = sym $ Calc $ @@ -164,7 +163,7 @@ divByOne n = fst $ DivisionTheoremUniqueness n 1 SIsNonZero n 0 (LTESucc LTEZero ||| fuel-based induction over mod''. public export modLtDivisor : (n, d : Nat) -> {auto 0 ok : NonZero d} -> LT (modNatNZ n d ok) d -modLtDivisor n (S d) = boundModNatNZ n (S d) SIsNonZero +modLtDivisor n (S d) = boundModNatNZ n (S d) ItIsSucc -------------------------------------------------------------------------------- -- GCD Properties From 72120c318e9bda080781736557abbe20d0abe57e Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:50:13 +0000 Subject: [PATCH 15/24] Keep GCD debt as propositions and audit the complete OCaml symbol surface --- .github/workflows/idris2-ci.yml | 2 +- .github/workflows/ocaml-ci.yml | 13 +++++++++++ .machine_readable/6a2/META.a2ml | 1 + bindings/ocaml/scripts/full-api-audit.sh | 28 ++++++++++++++++++++++++ src/Proven/SafeMath/Proofs.idr | 13 ++++++----- 5 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 bindings/ocaml/scripts/full-api-audit.sh diff --git a/.github/workflows/idris2-ci.yml b/.github/workflows/idris2-ci.yml index 3843edeb..d5f33b9d 100644 --- a/.github/workflows/idris2-ci.yml +++ b/.github/workflows/idris2-ci.yml @@ -83,7 +83,7 @@ jobs: cd tests/properties for source in *Props.idr; do echo "::group::$source" - if ! idris2 -p proven --check "$source" 2>&1 | tee "/tmp/$source.log"; then + if ! idris2 -p proven -p contrib -p network --check "$source" 2>&1 | tee "/tmp/$source.log"; then fail=1 fi if grep -q '^Error:' "/tmp/$source.log"; then fail=1; fi diff --git a/.github/workflows/ocaml-ci.yml b/.github/workflows/ocaml-ci.yml index 05f54f24..cc0c6c60 100644 --- a/.github/workflows/ocaml-ci.yml +++ b/.github/workflows/ocaml-ci.yml @@ -211,6 +211,7 @@ jobs: name: libproven-x86_64-linux-gnu path: lib/ - name: Build + Run full_api_probe + if: ${{ !cancelled() }} working-directory: bindings/ocaml env: PROVEN_LIB_PATH: ${{ github.workspace }}/lib @@ -219,6 +220,18 @@ jobs: run: | opam exec -- dune build smoke/full_api_probe.exe ./_build/default/smoke/full_api_probe.exe + - name: Audit every declared full-API symbol + if: ${{ !cancelled() }} + working-directory: bindings/ocaml + env: + PROVEN_LIB_PATH: ${{ github.workspace }}/lib + run: bash scripts/full-api-audit.sh + - name: Preserve full-API availability inventory + if: always() + uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 + with: + name: ocaml-full-api-audit + path: bindings/ocaml/_build/full-api-audit/ ocaml-detachable-build: runs-on: ubuntu-22.04 needs: ocaml-build diff --git a/.machine_readable/6a2/META.a2ml b/.machine_readable/6a2/META.a2ml index cbbc10d7..6562f353 100644 --- a/.machine_readable/6a2/META.a2ml +++ b/.machine_readable/6a2/META.a2ml @@ -10,6 +10,7 @@ last-updated = "2026-11-16" [architecture-decisions] decisions = [ + { id = "ADR-005", title = "Proof debt is a proposition, not an assumed witness", status = "proposed", date = "2026-11-16", body = "In the qualified arithmetic import cone, replace the two unused bodyless GCD witnesses with explicitly named obligation Types. The propositions remain unproved; no theorem is claimed discharged. This supersedes ADR-001 only for these two declarations and prevents consumers treating erased declarations as established lemmas. The strict regression gate requires no holes in its complete import cone. Other bodyless declarations remain recorded debt pending the same review." }, { id = "ADR-004", title = "Qualified exact-byte Idris RefC header bridge with repo-local ABI marshaling", status = "proposed", date = "2026-11-16", body = "The header transition belongs to the total Idris Bits8 kernel; Zig walks the caller's span and uses the compiler's actual SDK representation. Missing backends and invalid states refuse explicitly. ABI marshaling lives in ffi/zig/src/refc; the unavailable language-bridges dependency had no production call sites and is removed, superseding ADR-003 for build provisioning. Full-package generation uses an isolated source tree and rejects stale C, misleading compiler success and missing output. Model proofs replay in compatible Agda, full Cubical, Lean and Rocq/coqchk; source/compiler refinement remains open. See docs/adr/2026-11-16-header-refc-boundary.adoc for qualification, the remaining OCaml lifecycle mismatch and repository-wide proof debt." }, { id = "ADR-001", title = "OWED-with-justification convention for bodyless declarations in Proofs.idr files", status = "accepted", date = "2026-05-20", body = "Bodyless type signatures (implicit postulates) are made discoverable via triple-pipe doc-comment naming the claim + Idris2 0.8.0 blocker + discharge condition, plus a leading `0 ` erased-multiplicity marker, plus the original bare signature. The `postulate` keyword is NOT used (zero Proofs.idr files use it). Canonical example: src/Proven/SafeChecksum/Proofs.idr. Reference: hyperpolymath/standards#158 Fork A campaign, PROOF-NEEDS.md." }, { id = "ADR-002", title = "Fast proof-check CI gate over the proven.ipkg proof modules", status = "accepted", date = "2026-06-01", body = "A required proof-check job runs idris2 --check on each *.Proofs module listed in proven.ipkg (the 29 build-contract proof modules), from the src/ sourcedir with module-relative paths, on a clean idris2-0.8.0 + contrib toolchain (build/ removed so a stale TTC cannot mask a break). This is the fast cone-check recommended in #145 after #127/#145 showed DISCHARGED-but-unbuilt proofs reaching main while the slow full build sat queued. Scope is the ipkg list, not every on-disk Proofs.idr (64 of 93 are intentionally env-blocked or WIP). A corollary of ADR-001: a theorem that can only be closed by consuming an erased 0-multiplicity OWED premise, or an irreducible top-level-constant projection, is itself reverted to OWED rather than given a body that does not type-check. Origin: #147 (gate) and #148 (gate correctness + 5 proof repairs)." }, diff --git a/bindings/ocaml/scripts/full-api-audit.sh b/bindings/ocaml/scripts/full-api-audit.sh new file mode 100644 index 00000000..6efc7dc1 --- /dev/null +++ b/bindings/ocaml/scripts/full-api-audit.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +set -euo pipefail +unset CDPATH +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +: "${PROVEN_LIB_PATH:?directory containing the actual libproven.so required}" +library="$PROVEN_LIB_PATH/libproven.so" +test -s "$library" +out="$root/_build/full-api-audit" +mkdir -p "$out" +echo INCOMPLETE > "$out/result.txt" +trap 'status=$?; if ((status)); then echo FAIL > "$out/result.txt"; fi' EXIT +# This inventory is distinct from the five-symbol qualified wired manifest. +# Every eagerly bound symbol must exist. Presence alone does not prove a C +# signature, ownership contract, or Idris implementation; full_api_probe stays. +sed -nE 's/.*foreign ~from:lib "([^"]+)".*/\1/p' "$root/lib/proven_ffi.ml" | sort -u > "$out/required.txt" +test -s "$out/required.txt" +nm -D --defined-only "$library" | awk '$2 ~ /^[TW]$/ {print $3}' | sort -u > "$out/exported.txt" +comm -23 "$out/required.txt" "$out/exported.txt" > "$out/missing.txt" +comm -12 "$out/required.txt" "$out/exported.txt" > "$out/present.txt" +sha256sum "$root/lib/proven_ffi.ml" "$library" > "$out/identity.sha256" +printf 'Required=%s Present=%s Missing=%s\n' "$(wc -l < "$out/required.txt")" \ + "$(wc -l < "$out/present.txt")" "$(wc -l < "$out/missing.txt")" | tee "$out/summary.txt" +if [[ -s "$out/missing.txt" ]]; then + cat "$out/missing.txt"; exit 1 +fi +echo PASS > "$out/result.txt" diff --git a/src/Proven/SafeMath/Proofs.idr b/src/Proven/SafeMath/Proofs.idr index 7fb1003e..ef0c859c 100644 --- a/src/Proven/SafeMath/Proofs.idr +++ b/src/Proven/SafeMath/Proofs.idr @@ -2,9 +2,8 @@ -- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) ||| Mathematical proofs for SafeMath operations ||| -||| This module contains proofs that verify properties of our safe -||| arithmetic operations. These proofs are checked by the Idris 2 -||| compiler, guaranteeing correctness. +||| Checked arithmetic lemmas and explicitly named outstanding obligations. +||| An obligation is a Type, not a proof value that callers may assume. ||| ||| Updated for Idris 2 0.8.0 compatibility: ||| - Delegated to Data.Nat stdlib proofs where possible (stdlib now uses @@ -183,8 +182,10 @@ modLtDivisor n (S d) = boundModNatNZ n (S d) ItIsSucc ||| `covering`), or (b) we rewrite the signature against ||| `Data.Nat.Factor.gcdUnproven` (which IS total) and prove agreement ||| with `Data.Nat.gcd` on every input. +||| This names the outstanding proposition; it does not provide a witness. public export -0 gcdZeroRight : (n : Nat) -> {auto 0 ok : NotBothZero n 0} -> gcd n 0 @{ok} = n +GcdZeroRightObligation : Type +GcdZeroRightObligation = (n : Nat) -> {auto 0 ok : NotBothZero n 0} -> gcd n 0 @{ok} = n ||| OWED: GCD is commutative — `gcd a b = gcd b a`. The base cases ||| (`gcd 0 (S b)` vs `gcd (S b) 0`) would be trivially `Refl` if @@ -201,5 +202,7 @@ public export ||| through `Data.Nat.Factor.gcdUnproven` (which IS total) and prove ||| agreement with `Data.Nat.gcd` on every input — at which point ||| both `gcdZeroRight` and `gcdCommutative` discharge together. +||| This names the outstanding proposition; it does not provide a witness. public export -0 gcdCommutative : (a, b : Nat) -> {auto 0 ok1 : NotBothZero a b} -> {auto 0 ok2 : NotBothZero b a} -> gcd a b @{ok1} = gcd b a @{ok2} +GcdCommutativeObligation : Type +GcdCommutativeObligation = (a, b : Nat) -> {auto 0 ok1 : NotBothZero a b} -> {auto 0 ok2 : NotBothZero b a} -> gcd a b @{ok1} = gcd b a @{ok2} From 3424d6d2e5a11c5a99d48607a6bc0349275befba Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:54:48 +0000 Subject: [PATCH 16/24] Make obligation schemas total and retain both checked Idris profiles --- .github/workflows/ocaml-ci.yml | 3 +-- src/Proven/SafeMath/Proofs.idr | 28 ++++++++++++++++++---------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ocaml-ci.yml b/.github/workflows/ocaml-ci.yml index cc0c6c60..08eb0110 100644 --- a/.github/workflows/ocaml-ci.yml +++ b/.github/workflows/ocaml-ci.yml @@ -222,10 +222,9 @@ jobs: ./_build/default/smoke/full_api_probe.exe - name: Audit every declared full-API symbol if: ${{ !cancelled() }} - working-directory: bindings/ocaml env: PROVEN_LIB_PATH: ${{ github.workspace }}/lib - run: bash scripts/full-api-audit.sh + run: bash bindings/ocaml/scripts/full-api-audit.sh - name: Preserve full-API availability inventory if: always() uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 diff --git a/src/Proven/SafeMath/Proofs.idr b/src/Proven/SafeMath/Proofs.idr index ef0c859c..cf44b738 100644 --- a/src/Proven/SafeMath/Proofs.idr +++ b/src/Proven/SafeMath/Proofs.idr @@ -11,9 +11,8 @@ ||| - `minus n 0` no longer reduces on abstract `n`; case-split required ||| - `div`/`mod`/`gcd` internal representations changed; some properties ||| are stated without proof pending upstream proof availability -||| - The CI contract is Idris 2 0.8.0. Its Data.Nat defines NonZero = IsSucc -||| and deprecates SIsNonZero in favour of ItIsSucc. Use the constructor -||| directly so the proof cone also passes the warnings-as-errors gate. +||| - CI includes both 0.8.0 and a legacy 0.7 container. Search for the +||| required NonZero witness instead of naming a version-specific constructor. module Proven.SafeMath.Proofs import Proven.Core @@ -24,6 +23,9 @@ import Syntax.PreorderReasoning %default total +nonZeroSuccessor : {n : Nat} -> NonZero (S n) +nonZeroSuccessor = %search + -------------------------------------------------------------------------------- -- Properties of Natural Number Addition -------------------------------------------------------------------------------- @@ -148,8 +150,8 @@ lteAntisym (LTESucc ab) (LTESucc ba) = cong S (lteAntisym ab ba) ||| Proved via Data.Nat.Division.DivisionTheoremUniqueness: ||| n = n * 1 + 0 is the unique decomposition, so divNatNZ n 1 = n. public export -divByOne : (n : Nat) -> divNatNZ n 1 ItIsSucc = n -divByOne n = fst $ DivisionTheoremUniqueness n 1 ItIsSucc n 0 (LTESucc LTEZero) (nTimesOnePlusZero n) +divByOne : (n : Nat) -> divNatNZ n 1 nonZeroSuccessor = n +divByOne n = fst $ DivisionTheoremUniqueness n 1 nonZeroSuccessor n 0 (LTESucc LTEZero) (nTimesOnePlusZero n) where nTimesOnePlusZero : (k : Nat) -> k = k * 1 + 0 nTimesOnePlusZero k = sym $ Calc $ @@ -162,12 +164,18 @@ divByOne n = fst $ DivisionTheoremUniqueness n 1 ItIsSucc n 0 (LTESucc LTEZero) ||| fuel-based induction over mod''. public export modLtDivisor : (n, d : Nat) -> {auto 0 ok : NonZero d} -> LT (modNatNZ n d ok) d -modLtDivisor n (S d) = boundModNatNZ n (S d) ItIsSucc +modLtDivisor n (S d) = boundModNatNZ n (S d) nonZeroSuccessor -------------------------------------------------------------------------------- -- GCD Properties -------------------------------------------------------------------------------- +||| An operation to which GCD laws may be applied. The upstream covering +||| Data.Nat.gcd has no total witness here; even that instantiation is owed. +public export +GcdOperation : Type +GcdOperation = (a, b : Nat) -> {auto 0 ok : NotBothZero a b} -> Nat + ||| OWED: GCD of n and 0 is n. `Data.Nat.gcd` is declared `covering` ||| (not `total`) in Idris2 0.8.0, so the type-checker refuses to ||| reduce `gcd (S k) 0` even though `gcd a Z = a` is a direct @@ -184,8 +192,8 @@ modLtDivisor n (S d) = boundModNatNZ n (S d) ItIsSucc ||| with `Data.Nat.gcd` on every input. ||| This names the outstanding proposition; it does not provide a witness. public export -GcdZeroRightObligation : Type -GcdZeroRightObligation = (n : Nat) -> {auto 0 ok : NotBothZero n 0} -> gcd n 0 @{ok} = n +GcdZeroRightObligation : GcdOperation -> Type +GcdZeroRightObligation operation = (n : Nat) -> {auto 0 ok : NotBothZero n 0} -> operation n 0 @{ok} = n ||| OWED: GCD is commutative — `gcd a b = gcd b a`. The base cases ||| (`gcd 0 (S b)` vs `gcd (S b) 0`) would be trivially `Refl` if @@ -204,5 +212,5 @@ GcdZeroRightObligation = (n : Nat) -> {auto 0 ok : NotBothZero n 0} -> gcd n 0 @ ||| both `gcdZeroRight` and `gcdCommutative` discharge together. ||| This names the outstanding proposition; it does not provide a witness. public export -GcdCommutativeObligation : Type -GcdCommutativeObligation = (a, b : Nat) -> {auto 0 ok1 : NotBothZero a b} -> {auto 0 ok2 : NotBothZero b a} -> gcd a b @{ok1} = gcd b a @{ok2} +GcdCommutativeObligation : GcdOperation -> Type +GcdCommutativeObligation operation = (a, b : Nat) -> {auto 0 ok1 : NotBothZero a b} -> {auto 0 ok2 : NotBothZero b a} -> operation a b @{ok1} = operation b a @{ok2} From e5a0078a628a694700f6baebb4ca83a0768773fd Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:13:33 +0000 Subject: [PATCH 17/24] Add replayed proof obligations for canonical RefC Bits8 words --- ffi/zig/src/refc/bits8.zig | 17 +++++ scripts/check-header-proofs.sh | 17 +++++ verification/header/agda/RefCBits8.agda | 66 +++++++++++++++++++ verification/header/coq/RefCBits8.v | 42 ++++++++++++ .../header/cubical/RefCBits8Cubical.agda | 14 ++++ verification/header/lean4/RefCBits8.lean | 59 +++++++++++++++++ 6 files changed, 215 insertions(+) create mode 100644 verification/header/agda/RefCBits8.agda create mode 100644 verification/header/coq/RefCBits8.v create mode 100644 verification/header/cubical/RefCBits8Cubical.agda create mode 100644 verification/header/lean4/RefCBits8.lean diff --git a/ffi/zig/src/refc/bits8.zig b/ffi/zig/src/refc/bits8.zig index a12c9fdf..7e825053 100644 --- a/ffi/zig/src/refc/bits8.zig +++ b/ffi/zig/src/refc/bits8.zig @@ -7,6 +7,10 @@ const c = @cImport({ @cInclude("_datatypes.h"); }); const shift = c.idris2_vp_int_shift; +comptime { + if (@bitSizeOf(usize) != 64 or shift != 32) + @compileError("RefC Bits8 proof profile requires 64-bit words and SDK shift 32"); +} pub fn encode(value: u8) ?*anyopaque { return @ptrFromInt((@as(usize, value) << shift) | 1); @@ -33,3 +37,16 @@ test "null, noncanonical tags and out-of-range payloads are rejected" { try std.testing.expectError(error.InvalidRefcResult, decode(@ptrFromInt(2))); try std.testing.expectError(error.InvalidRefcResult, decode(@ptrFromInt((@as(usize, 256) << shift) | 1))); } + +test "every unused bit and missing tag is rejected for every byte" { + for (0..256) |n| { + const canonical = @intFromPtr(encode(@intCast(n))); + try std.testing.expectError(error.InvalidRefcResult, decode(@ptrFromInt(canonical ^ 1))); + for (1..32) |bit| { + try std.testing.expectError(error.InvalidRefcResult, decode(@ptrFromInt(canonical | (@as(usize, 1) << @intCast(bit))))); + } + for (40..64) |bit| { + try std.testing.expectError(error.InvalidRefcResult, decode(@ptrFromInt(canonical | (@as(usize, 1) << @intCast(bit))))); + } + } +} diff --git a/scripts/check-header-proofs.sh b/scripts/check-header-proofs.sh index 8d790cfa..023da9eb 100644 --- a/scripts/check-header-proofs.sh +++ b/scripts/check-header-proofs.sh @@ -41,6 +41,23 @@ cp "$work/header/coq/ProvenHeader.v" "$base/coq/" [[ $(grep -c 'Closed under the global context' "$evidence/coq.log") == 4 ]] (cd "$base/coq"; coqchk -silent ProvenHeader) > "$evidence/coqchk.log" 2>&1 +# RefC word partition: 1 tag + 31 low bits + 8 payload bits + 24 high bits. +# These are model theorems. Importer/compiler/pointer-carrier refinement is owed. +agda --safe --without-K --cubical-compatible --double-check --warning=error -i "$work/header/agda" "$work/header/agda/RefCBits8.agda" > "$evidence/bits8-agda.log" 2>&1 +agda --safe --cubical --double-check --warning=error -i "$work/header/agda" -i "$work/header/cubical" "$work/header/cubical/RefCBits8Cubical.agda" > "$evidence/bits8-cubical.log" 2>&1 +(cd "$work/header/lean4"; lean -o RefCBits8.olean RefCBits8.lean) > "$evidence/bits8-lean.log" 2>&1 +[[ $(grep -c 'does not depend on any axioms' "$evidence/bits8-lean.log") == 3 ]] +if grep -Eq 'sorry|warning:|error:' "$evidence/bits8-lean.log"; then exit 1; fi +(cd "$work/header/coq"; coqc RefCBits8.v) > "$evidence/bits8-coq.log" 2>&1 +[[ $(grep -c 'Closed under the global context' "$evidence/bits8-coq.log") == 3 ]] +(cd "$work/header/coq"; coqchk -silent RefCBits8) > "$evidence/bits8-coqchk.log" 2>&1 +# Mutating the tag in the encode definition must invalidate round-trip. +sed -e 's/module RefCBits8 where/module RefCBits8Mutant where/' \ + -e 's/encode b = word true/encode b = word false/' \ + "$work/header/agda/RefCBits8.agda" > "$work/RefCBits8Mutant.agda" +if agda --safe --without-K --cubical-compatible --double-check --warning=error -i "$work" "$work/RefCBits8Mutant.agda" > "$evidence/bits8-reject.log" 2>&1; then exit 1; fi +grep -q 'nothing != just' "$evidence/bits8-reject.log" + # Explicitly false witnesses must be rejected, not skipped or admitted. cat > "$work/Reject.agda" <<'AGDA' {-# OPTIONS --safe --without-K --cubical-compatible #-} diff --git a/verification/header/agda/RefCBits8.agda b/verification/header/agda/RefCBits8.agda new file mode 100644 index 00000000..c7c3aeec --- /dev/null +++ b/verification/header/agda/RefCBits8.agda @@ -0,0 +1,66 @@ +{-# OPTIONS --safe --without-K --cubical-compatible #-} +-- SPDX-License-Identifier: CC-BY-SA-4.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +-- Abstract 64-bit partition, little-endian by bit significance, not memory. +-- Source/compiler refinement and pointer-carrier validity are separate debts. +module RefCBits8 where +open import Agda.Builtin.Nat using (Nat; zero; suc) +open import Agda.Builtin.Bool using (Bool; true; false) +open import Agda.Builtin.Maybe using (Maybe; just; nothing) +open import Agda.Builtin.Equality using (_≡_; refl) + +data Bits : Nat → Set where + [] : Bits zero + _∷_ : ∀ {n} → Bool → Bits n → Bits (suc n) +infixr 5 _∷_ +zeros : (n : Nat) → Bits n +zeros zero = [] +zeros (suc n) = false ∷ zeros n +allZero : ∀ {n} → Bits n → Bool +allZero [] = true +allZero (true ∷ bs) = false +allZero (false ∷ bs) = allZero bs +zeros-test : (n : Nat) → allZero (zeros n) ≡ true +zeros-test zero = refl +zeros-test (suc n) = zeros-test n +allZero-sound : ∀ {n} (bs : Bits n) → allZero bs ≡ true → bs ≡ zeros n +allZero-sound [] p = refl +allZero-sound (true ∷ bs) () +allZero-sound (false ∷ bs) p rewrite allZero-sound bs p = refl + +Byte : Set +Byte = Bits 8 +record Word : Set where + constructor word + field + tag : Bool -- bit 0 + low : Bits 31 -- bits 1..31, must be zero + payload : Byte -- bits 32..39 + high : Bits 24 -- bits 40..63, must be zero +open Word +encode : Byte → Word +encode b = word true (zeros 31) b (zeros 24) +decode : Word → Maybe Byte +decode (word false lo b hi) = nothing +decode (word true lo b hi) with allZero lo | allZero hi +... | true | true = just b +... | true | false = nothing +... | false | _ = nothing + +nothing≠just : ∀ {A B : Set} {b : A} → nothing ≡ just b → B +nothing≠just () + +round-trip : (b : Byte) → decode (encode b) ≡ just b +round-trip b = refl +accepted-canonical : (w : Word) (b : Byte) → decode w ≡ just b → w ≡ encode b +accepted-canonical (word false lo p hi) b () +accepted-canonical (word true lo p hi) b ok with allZero lo in lo-ok | allZero hi in hi-ok +... | false | _ = nothing≠just ok +... | true | false = nothing≠just ok +... | true | true with ok +... | refl rewrite allZero-sound lo lo-ok | allZero-sound hi hi-ok = refl + +null-word : Word +null-word = word false (zeros 31) (zeros 8) (zeros 24) +null-rejected : decode null-word ≡ nothing +null-rejected = refl diff --git a/verification/header/coq/RefCBits8.v b/verification/header/coq/RefCBits8.v new file mode 100644 index 00000000..fee43318 --- /dev/null +++ b/verification/header/coq/RefCBits8.v @@ -0,0 +1,42 @@ +(* SPDX-License-Identifier: CC-BY-SA-4.0 + Copyright (c) 2026 Jonathan D.A. Jewell + Hand re-proof of RefCBits8.agda. No source refinement is claimed. *) +From Stdlib Require Import Bool Arith. +Module RefCBits8. +Inductive Bits : nat -> Type := +| bnil : Bits 0 +| bcons : forall {n}, bool -> Bits n -> Bits (S n). +Fixpoint zeros (n : nat) : Bits n := + match n with 0 => bnil | S k => bcons false (zeros k) end. +Fixpoint allZero {n} (bs : Bits n) : bool := + match bs with bnil => true | bcons b tail => if b then false else allZero tail end. +Lemma allZero_sound n (bs : Bits n) : allZero bs = true -> bs = zeros n. +Proof. + induction bs as [|n b bs IH]; simpl. + - reflexivity. + - destruct b; [discriminate|]. intro H. rewrite (IH H). reflexivity. +Qed. +Definition Byte := Bits 8. +Record Word := word { tag : bool; low : Bits 31; payload : Byte; high : Bits 24 }. +Definition encode (b : Byte) : Word := word true (zeros 31) b (zeros 24). +Definition decode (w : Word) : option Byte := + if tag w then if allZero (low w) then if allZero (high w) then Some (payload w) + else None else None else None. +Lemma round_trip b : decode (encode b) = Some b. +Proof. reflexivity. Qed. +Lemma accepted_canonical w b : decode w = Some b -> w = encode b. +Proof. + destruct w as [t lo p hi]. destruct t; [|discriminate]. + unfold decode; simpl. + destruct (allZero lo) eqn:L; [|discriminate]. + destruct (allZero hi) eqn:H; [|discriminate]. + intro E. injection E as E. subst p. + rewrite (allZero_sound _ lo L), (allZero_sound _ hi H). reflexivity. +Qed. +Definition nullWord : Word := word false (zeros 31) (zeros 8) (zeros 24). +Lemma null_rejected : decode nullWord = None. +Proof. reflexivity. Qed. +Print Assumptions round_trip. +Print Assumptions accepted_canonical. +Print Assumptions null_rejected. +End RefCBits8. diff --git a/verification/header/cubical/RefCBits8Cubical.agda b/verification/header/cubical/RefCBits8Cubical.agda new file mode 100644 index 00000000..29488714 --- /dev/null +++ b/verification/header/cubical/RefCBits8Cubical.agda @@ -0,0 +1,14 @@ +{-# OPTIONS --safe --cubical #-} +-- SPDX-License-Identifier: CC-BY-SA-4.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +module RefCBits8Cubical where +open import RefCBits8 +open import Agda.Builtin.Equality using (_≡_; refl) +open import Agda.Builtin.Maybe using (just) +open import Agda.Builtin.Cubical.Path renaming (_≡_ to _≡ᵖ_) +equality-path : ∀ {A : Set} {x y : A} → x ≡ y → x ≡ᵖ y +equality-path {x = x} refl i = x +round-trip-path : (b : Byte) → decode (encode b) ≡ᵖ just b +round-trip-path b = equality-path (round-trip b) +canonical-path : (w : Word) (b : Byte) → decode w ≡ just b → w ≡ᵖ encode b +canonical-path w b p = equality-path (accepted-canonical w b p) diff --git a/verification/header/lean4/RefCBits8.lean b/verification/header/lean4/RefCBits8.lean new file mode 100644 index 00000000..aa08e73f --- /dev/null +++ b/verification/header/lean4/RefCBits8.lean @@ -0,0 +1,59 @@ +-- SPDX-License-Identifier: CC-BY-SA-4.0 +-- Copyright (c) 2026 Jonathan D.A. Jewell +-- Hand re-proof of RefCBits8.agda, not automatic witness translation. +set_option warningAsError true +namespace RefCBits8 +inductive Bits : Nat → Type where + | nil : Bits 0 + | cons {n : Nat} : Bool → Bits n → Bits (n + 1) +def zeros : (n : Nat) → Bits n + | 0 => .nil + | n + 1 => .cons false (zeros n) +def allZero : {n : Nat} → Bits n → Bool + | _, .nil => true + | _, .cons true _ => false + | _, .cons false bs => allZero bs +theorem allZero_sound {n : Nat} (bs : Bits n) : allZero bs = true → bs = zeros n := by + induction bs with + | nil => intro _; rfl + | cons b bs ih => + cases b with + | true => intro h; cases h + | false => intro h; change allZero bs = true at h; cases ih h; rfl +abbrev Byte := Bits 8 +structure Word where + tag : Bool + low : Bits 31 + payload : Byte + high : Bits 24 +def encode (b : Byte) : Word := ⟨true, zeros 31, b, zeros 24⟩ +def decode (w : Word) : Option Byte := + match w.tag, allZero w.low, allZero w.high with + | true, true, true => some w.payload + | _, _, _ => none +theorem round_trip (b : Byte) : decode (encode b) = some b := rfl +theorem accepted_canonical (w : Word) (b : Byte) : decode w = some b → w = encode b := by + cases w with + | mk tag low payload high => + cases tag with + | false => intro h; cases h + | true => + cases hl : allZero low with + | false => intro h; simp only [decode, hl] at h; cases h + | true => + cases hh : allZero high with + | false => intro h; simp only [decode, hl, hh] at h; cases h + | true => + intro h + simp only [decode, hl, hh] at h + have hp : payload = b := Option.some.inj h + cases allZero_sound low hl + cases allZero_sound high hh + cases hp + rfl +def nullWord : Word := ⟨false, zeros 31, zeros 8, zeros 24⟩ +theorem null_rejected : decode nullWord = none := rfl +end RefCBits8 +#print axioms RefCBits8.round_trip +#print axioms RefCBits8.accepted_canonical +#print axioms RefCBits8.null_rejected From c285cf3b375a49c3a8cb1fb17b9c257f65b16b35 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:14:32 +0000 Subject: [PATCH 18/24] Frontload strict totality checks and report the remaining proof and ABI debts --- .github/workflows/idris2-ci.yml | 10 ++-- .machine_readable/6a2/META.a2ml | 2 +- docs/adr/2026-11-16-header-refc-boundary.adoc | 46 ++++++++++++++++--- src/Proven/SafeMath/Proofs.idr | 7 +-- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/.github/workflows/idris2-ci.yml b/.github/workflows/idris2-ci.yml index d5f33b9d..a32092d4 100644 --- a/.github/workflows/idris2-ci.yml +++ b/.github/workflows/idris2-ci.yml @@ -63,10 +63,6 @@ jobs: make install - name: Add Idris 2 to PATH run: echo "$HOME/.idris2/bin" >> $GITHUB_PATH - - name: Build proven library - run: idris2 --build proven.ipkg - - name: Install proven library (required for tests.ipkg) - run: idris2 --install proven.ipkg - name: Check totality repairs against actual source run: bash scripts/test-totality-regressions.sh - name: Preserve totality regression evidence @@ -75,6 +71,12 @@ jobs: with: name: totality-regressions path: build/totality-regressions/ + - name: Build proven library + if: ${{ !cancelled() }} + run: idris2 --build proven.ipkg + - name: Install proven library (required for tests.ipkg) + if: ${{ !cancelled() }} + run: idris2 --install proven.ipkg - name: Diagnose every property module if: ${{ !cancelled() }} run: | diff --git a/.machine_readable/6a2/META.a2ml b/.machine_readable/6a2/META.a2ml index 6562f353..5fd8fb9b 100644 --- a/.machine_readable/6a2/META.a2ml +++ b/.machine_readable/6a2/META.a2ml @@ -10,7 +10,7 @@ last-updated = "2026-11-16" [architecture-decisions] decisions = [ - { id = "ADR-005", title = "Proof debt is a proposition, not an assumed witness", status = "proposed", date = "2026-11-16", body = "In the qualified arithmetic import cone, replace the two unused bodyless GCD witnesses with explicitly named obligation Types. The propositions remain unproved; no theorem is claimed discharged. This supersedes ADR-001 only for these two declarations and prevents consumers treating erased declarations as established lemmas. The strict regression gate requires no holes in its complete import cone. Other bodyless declarations remain recorded debt pending the same review." }, + { id = "ADR-005", title = "Proof debt is a proposition, not an assumed witness", status = "proposed", date = "2026-11-16", body = "In the qualified arithmetic import cone, replace the two unused bodyless GCD witnesses with explicitly named obligation schemas parameterized by a total GCD operation. Instantiating them with the upstream covering operation is also owed. The propositions remain unproved; no theorem is claimed discharged. This supersedes ADR-001 only for these two declarations and prevents consumers treating erased declarations as established lemmas. The strict regression gate requires no holes in its complete import cone. Other bodyless declarations remain recorded debt pending the same review." }, { id = "ADR-004", title = "Qualified exact-byte Idris RefC header bridge with repo-local ABI marshaling", status = "proposed", date = "2026-11-16", body = "The header transition belongs to the total Idris Bits8 kernel; Zig walks the caller's span and uses the compiler's actual SDK representation. Missing backends and invalid states refuse explicitly. ABI marshaling lives in ffi/zig/src/refc; the unavailable language-bridges dependency had no production call sites and is removed, superseding ADR-003 for build provisioning. Full-package generation uses an isolated source tree and rejects stale C, misleading compiler success and missing output. Model proofs replay in compatible Agda, full Cubical, Lean and Rocq/coqchk; source/compiler refinement remains open. See docs/adr/2026-11-16-header-refc-boundary.adoc for qualification, the remaining OCaml lifecycle mismatch and repository-wide proof debt." }, { id = "ADR-001", title = "OWED-with-justification convention for bodyless declarations in Proofs.idr files", status = "accepted", date = "2026-05-20", body = "Bodyless type signatures (implicit postulates) are made discoverable via triple-pipe doc-comment naming the claim + Idris2 0.8.0 blocker + discharge condition, plus a leading `0 ` erased-multiplicity marker, plus the original bare signature. The `postulate` keyword is NOT used (zero Proofs.idr files use it). Canonical example: src/Proven/SafeChecksum/Proofs.idr. Reference: hyperpolymath/standards#158 Fork A campaign, PROOF-NEEDS.md." }, { id = "ADR-002", title = "Fast proof-check CI gate over the proven.ipkg proof modules", status = "accepted", date = "2026-06-01", body = "A required proof-check job runs idris2 --check on each *.Proofs module listed in proven.ipkg (the 29 build-contract proof modules), from the src/ sourcedir with module-relative paths, on a clean idris2-0.8.0 + contrib toolchain (build/ removed so a stale TTC cannot mask a break). This is the fast cone-check recommended in #145 after #127/#145 showed DISCHARGED-but-unbuilt proofs reaching main while the slow full build sat queued. Scope is the ipkg list, not every on-disk Proofs.idr (64 of 93 are intentionally env-blocked or WIP). A corollary of ADR-001: a theorem that can only be closed by consuming an erased 0-multiplicity OWED premise, or an irreducible top-level-constant projection, is itself reverted to OWED rather than given a body that does not type-check. Origin: #147 (gate) and #148 (gate correctness + 5 proof repairs)." }, diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc index b1b03b1e..419d7750 100644 --- a/docs/adr/2026-11-16-header-refc-boundary.adoc +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -138,13 +138,16 @@ promoted to a whole-library or v1 verification claim. == Repository-wide blockers outside the repaired profile -The safety gate still reports 20 existing `assert_total` uses. The licence -gate rejects five existing `Palimpsest-MPL-1.0` headers in SafeYAML/Parser, -SafeRegex/Parser and Matcher, SafeSQL/Builder and SafeXML/Parser. Those files -are unchanged by this repair; licence changes require the owner and are not -part of this patch. The older Idris property suite also references unavailable -SafeMath operations and incompatible result types. These failures remain -visible and prevent a whole-repository green claim. +The safety gate now passes after removing 20 production totality assertions. +The licence gate still rejects five existing `Palimpsest-MPL-1.0` headers in +SafeYAML/Parser, SafeRegex/Parser and Matcher, SafeSQL/Builder and SafeXML/Parser. +Their licence headers remain unchanged pending the owner's decision. The old +property suite contains further unavailable APIs, invalid propositions and +unproved declarations; repairing SafeMath does not qualify the other modules. +The full OCaml symbol audit at `3424d6d` required 146 distinct symbols: nine +were present and 137 missing. Presence alone does not establish signature +compatibility; the lifecycle signature conflict is also unresolved. These +failures prevent a whole-repository green claim and must not be hidden. The repository instructions name `./hypatia-v2`, but that executable is not present in this checkout. No successful local scan with that command is @@ -170,3 +173,32 @@ The totality-regression script builds from a fresh source copy, requires totality and no reported holes, executes malformed-input and semantic regressions, and retains source identities, compiler output and a result marker. These are regression receipts, not Atoma source-to-proof certificates. + +== Next source-to-proof step: canonical Bits8 word representation + +`verification/header/*/RefCBits8.*` defines a 64-bit word partition with tag +bit 0, zero bits 1..31, an eight-bit payload at 32..39, and zero bits 40..63. +The compatible Agda development states and proves byte round-trip, canonical +representation of every accepted word, and null rejection. Lean and Rocq are +separate hand re-proofs of those statements; their assumption audits and +`coqchk` are mandatory. Full Cubical checks paths obtained from the compatible +proofs. Changing the encoded tag is a required rejecting Agda control. +These claims require a successful current CI run; writing the files is not a +successful replay receipt. + +The actual Zig codec now refuses any target/SDK profile other than 64-bit +words with shift 32. Executable tests retain all 256 round-trips and reject +flipping the tag or setting each of the 55 unused bits for each byte. The +universal model theorem covers all bit combinations; these executable tests +are finite implementation evidence. Neither establishes source/model +correspondence, the pointer-carrier ABI assumption, generated RefC semantics, +or compiler correctness. In particular, the model's word partition does not +prove a C pointer points to an object and the carrier must never be dereferenced. + +Atoma must next import the actual codec AST and SDK constant into typed +bitvector semantics, reject changed widths/shifts/casts/guards, and produce a +receipt binding those obligations and theorem identities to the full source +closure. Then the header loop needs a prefix invariant, bounds and progress +proof, with an explicit RefC-call contract. UAA's real two-slot callback registry +is a second consumer for state transitions, refusal and borrowed lifetimes; +its differential execution is not yet an Atoma source-to-proof certificate. diff --git a/src/Proven/SafeMath/Proofs.idr b/src/Proven/SafeMath/Proofs.idr index cf44b738..f63f840e 100644 --- a/src/Proven/SafeMath/Proofs.idr +++ b/src/Proven/SafeMath/Proofs.idr @@ -23,6 +23,7 @@ import Syntax.PreorderReasoning %default total +public export nonZeroSuccessor : {n : Nat} -> NonZero (S n) nonZeroSuccessor = %search @@ -150,8 +151,8 @@ lteAntisym (LTESucc ab) (LTESucc ba) = cong S (lteAntisym ab ba) ||| Proved via Data.Nat.Division.DivisionTheoremUniqueness: ||| n = n * 1 + 0 is the unique decomposition, so divNatNZ n 1 = n. public export -divByOne : (n : Nat) -> divNatNZ n 1 nonZeroSuccessor = n -divByOne n = fst $ DivisionTheoremUniqueness n 1 nonZeroSuccessor n 0 (LTESucc LTEZero) (nTimesOnePlusZero n) +divByOne : (n : Nat) -> divNatNZ n 1 Proven.SafeMath.Proofs.nonZeroSuccessor = n +divByOne n = fst $ DivisionTheoremUniqueness n 1 Proven.SafeMath.Proofs.nonZeroSuccessor n 0 (LTESucc LTEZero) (nTimesOnePlusZero n) where nTimesOnePlusZero : (k : Nat) -> k = k * 1 + 0 nTimesOnePlusZero k = sym $ Calc $ @@ -164,7 +165,7 @@ divByOne n = fst $ DivisionTheoremUniqueness n 1 nonZeroSuccessor n 0 (LTESucc L ||| fuel-based induction over mod''. public export modLtDivisor : (n, d : Nat) -> {auto 0 ok : NonZero d} -> LT (modNatNZ n d ok) d -modLtDivisor n (S d) = boundModNatNZ n (S d) nonZeroSuccessor +modLtDivisor n (S d) = boundModNatNZ n (S d) Proven.SafeMath.Proofs.nonZeroSuccessor -------------------------------------------------------------------------------- -- GCD Properties From e5eadce09cdb8c6b114da2e32025ce0c9c470fd3 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:17:23 +0000 Subject: [PATCH 19/24] Use transport-compatible constructor injectivity in the Bits8 proof --- verification/header/agda/RefCBits8.agda | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/verification/header/agda/RefCBits8.agda b/verification/header/agda/RefCBits8.agda index c7c3aeec..1250df73 100644 --- a/verification/header/agda/RefCBits8.agda +++ b/verification/header/agda/RefCBits8.agda @@ -50,6 +50,14 @@ decode (word true lo b hi) with allZero lo | allZero hi nothing≠just : ∀ {A B : Set} {b : A} → nothing ≡ just b → B nothing≠just () +cong : ∀ {A B : Set} (f : A → B) {x y : A} → x ≡ y → f x ≡ f y +cong f refl = refl +fromMaybe : Byte → Maybe Byte → Byte +fromMaybe fallback nothing = fallback +fromMaybe fallback (just b) = b +just-injective : ∀ {a b : Byte} → just a ≡ just b → a ≡ b +just-injective {b = b} p = cong (fromMaybe b) p + round-trip : (b : Byte) → decode (encode b) ≡ just b round-trip b = refl accepted-canonical : (w : Word) (b : Byte) → decode w ≡ just b → w ≡ encode b @@ -57,8 +65,8 @@ accepted-canonical (word false lo p hi) b () accepted-canonical (word true lo p hi) b ok with allZero lo in lo-ok | allZero hi in hi-ok ... | false | _ = nothing≠just ok ... | true | false = nothing≠just ok -... | true | true with ok -... | refl rewrite allZero-sound lo lo-ok | allZero-sound hi hi-ok = refl +... | true | true rewrite allZero-sound lo lo-ok | allZero-sound hi hi-ok = + cong encode (just-injective ok) null-word : Word null-word = word false (zeros 31) (zeros 8) (zeros 24) From 8ebf5ad0f2b1942fb9f1d058d1f1ee708e0f0999 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:24:05 +0000 Subject: [PATCH 20/24] Preserve totality without repeated input scans --- .github/workflows/idris2-ci.yml | 2 ++ src/Proven/SafeRegex/Matcher.idr | 25 ++++++++------ src/Proven/SafeXML/Parser.idr | 29 ++++++---------- src/Proven/SafeYAML/Parser.idr | 49 ++++++++++++++------------- tests/totality/BoundaryRegression.idr | 15 ++++++++ verification/header/coq/RefCBits8.v | 4 +-- 6 files changed, 69 insertions(+), 55 deletions(-) diff --git a/.github/workflows/idris2-ci.yml b/.github/workflows/idris2-ci.yml index a32092d4..6cf8c6a1 100644 --- a/.github/workflows/idris2-ci.yml +++ b/.github/workflows/idris2-ci.yml @@ -11,6 +11,7 @@ on: paths: - 'src/**' - 'tests/**' + - 'scripts/test-totality-regressions.sh' - 'proven.ipkg' - 'tests.ipkg' - '.github/workflows/idris2-ci.yml' @@ -19,6 +20,7 @@ on: paths: - 'src/**' - 'tests/**' + - 'scripts/test-totality-regressions.sh' - 'proven.ipkg' - 'tests.ipkg' - '.github/workflows/idris2-ci.yml' diff --git a/src/Proven/SafeRegex/Matcher.idr b/src/Proven/SafeRegex/Matcher.idr index d891c2d9..0ea7ffff 100644 --- a/src/Proven/SafeRegex/Matcher.idr +++ b/src/Proven/SafeRegex/Matcher.idr @@ -5,7 +5,8 @@ ||| by tracking steps and enforcing configurable limits based on complexity. ||| ||| The matcher uses a backtracking algorithm but with strict step counting -||| to prevent catastrophic backtracking from causing denial of service. +||| to bound search transitions. List traversal and input decoding have their +||| own costs; a step limit alone is not a complete CPU or memory bound. module Proven.SafeRegex.Matcher import Proven.Core @@ -37,6 +38,8 @@ record MatchState where input : String ||| Input as list of characters (for efficient access) inputChars : List Char + ||| Erased witness: the cache describes the authoritative input String. + 0 charsMatchInput : inputChars = unpack input ||| Length of input inputLen : Nat ||| Regex flags @@ -47,7 +50,7 @@ public export initState : String -> Nat -> RegexFlags -> MatchState initState s maxSteps flags = let chars = unpack s - in MkMatchState 0 0 maxSteps [] s chars (length chars) flags + in MkMatchState 0 0 maxSteps [] s chars Refl (length chars) flags ||| Increment step counter public export @@ -68,7 +71,7 @@ currentChar : MatchState -> Maybe Char currentChar st = if st.position >= st.inputLen then Nothing - else head' (drop st.position (unpack st.input)) + else head' (drop st.position st.inputChars) ||| Get character at specific position public export @@ -76,7 +79,7 @@ charAt : MatchState -> Nat -> Maybe Char charAt st pos = if pos >= st.inputLen then Nothing - else head' (drop pos (unpack st.input)) + else head' (drop pos st.inputChars) ||| Check if at end of input public export @@ -426,24 +429,24 @@ replaceFirst sr input replacement = ||| Replace all matches public export replaceAll : SafeRegex -> String -> String -> String -replaceAll sr input replacement = go (S inputLen) 0 "" +replaceAll sr input replacement = go (unpack input) (S inputLen) 0 "" where inputLen : Nat inputLen = length input - go : (fuel : Nat) -> Nat -> String -> String - go Z pos acc = acc ++ substr pos (minus inputLen pos) input - go (S fuel) pos acc = + go : List Char -> (fuel : Nat) -> Nat -> String -> String + go chars Z pos acc = acc ++ substr pos (minus inputLen pos) input + go chars (S fuel) pos acc = if pos >= inputLen then acc ++ substr pos (minus inputLen pos) input else case matchAt sr input pos defaultFlags of MkMatchResult True (Just (start, end)) _ _ => let before = substr pos (minus start pos) input newPos = max (S pos) end - in go fuel newPos (acc ++ before ++ replacement) + in go chars fuel newPos (acc ++ before ++ replacement) _ => - case head' (drop pos (unpack input)) of - Just c => go fuel (S pos) (acc ++ singleton c) + case head' (drop pos chars) of + Just c => go chars fuel (S pos) (acc ++ singleton c) Nothing => acc ||| Split string by regex diff --git a/src/Proven/SafeXML/Parser.idr b/src/Proven/SafeXML/Parser.idr index 2e7a5036..386aa741 100644 --- a/src/Proven/SafeXML/Parser.idr +++ b/src/Proven/SafeXML/Parser.idr @@ -188,10 +188,11 @@ parseAttribute st = do (value, st5) <- parseAttrValue st4 Ok (MkXMLAttr name value, st5) -||| Parse attributes until > or /> +||| Parse attributes until > or />, spending one remaining attribute slot. +||| Delimiters are checked before the allowance, so a zero limit permits +||| an empty attribute list. This never recounts the remaining document. parseAttributes : Nat -> ParserState -> XMLResult (List XMLAttr, ParserState) -parseAttributes Z st = Err (MalformedXML "Attribute parsing budget exhausted") -parseAttributes (S fuel) st = +parseAttributes slots st = let st' = skipWhitespace st in case peek st' of Just '>' => Ok ([], st') @@ -200,20 +201,12 @@ parseAttributes (S fuel) st = _ => Err (MalformedXML "Expected '>' after '/'") Just c => if isXMLNameStartChar c - then do - -- Check attribute count limit. - -- REPAIRED 2026-08-27: was `if length [] >= st.options.maxAttributesPerElement`. - -- `length []` is 0, so the guard reduced to `0 >= n` and could NEVER fire: - -- the per-element attribute cap was declared but unenforced. `[]` also had - -- no determined element type, leaving an unsolved hole -- which is why this - -- module never compiled and never entered any .ipkg. The count is now taken - -- after recursion, which is the per-element count `maxAttributesPerElement` - -- names. `>=` on a pre-increment count becomes `>` on the total. - (attr, st1) <- parseAttribute st' - (rest, st2) <- parseAttributes fuel st1 - if length (attr :: rest) > st.options.maxAttributesPerElement - then Err (MalformedXML "Too many attributes") - else Ok (attr :: rest, st2) + then case slots of + Z => Err (MalformedXML "Too many attributes") + S remaining => do + (attr, st1) <- parseAttribute st' + (rest, st2) <- parseAttributes remaining st1 + Ok (attr :: rest, st2) else Err (MalformedXML ("Unexpected character in attributes: " ++ singleton c)) Nothing => Err (MalformedXML "Unexpected end of input in attributes") @@ -324,7 +317,7 @@ mutual checkDepth st st1 <- expect "<" st (name, st2) <- parseName st1 - (attrs, st3) <- parseAttributes (S (length st2.input)) st2 + (attrs, st3) <- parseAttributes st2.options.maxAttributesPerElement st2 -- Check for self-closing or start tag case peekN 2 st3 of ['/', '>'] => do diff --git a/src/Proven/SafeYAML/Parser.idr b/src/Proven/SafeYAML/Parser.idr index dedd3b04..7dfbb208 100644 --- a/src/Proven/SafeYAML/Parser.idr +++ b/src/Proven/SafeYAML/Parser.idr @@ -195,38 +195,39 @@ parseFloat st = parseDoubleMaybe : String -> Maybe Double parseDoubleMaybe s = Just 0.0 -- Simplified; real impl would parse -||| Parse quoted string +||| Parse quoted string by structural descent on its remaining characters. parseQuotedString : Char -> ParserState -> YAMLResult (String, ParserState) parseQuotedString quote st = case consume st of (Just c, st1) => if c /= quote then Err (SyntaxError ("Expected " ++ singleton quote) st.line st.col) - else parseContent (S (length st1.input)) [] st1 + else parseContent st1.input [] st1 (Nothing, _) => Err (SyntaxError "Unexpected end of input" st.line st.col) where - parseContent : Nat -> List Char -> ParserState -> YAMLResult (String, ParserState) - parseContent Z acc st = Err (SyntaxError "String parsing budget exhausted" st.line st.col) - parseContent (S fuel) acc st = - case consume st of - (Nothing, _) => Err (SyntaxError "Unclosed string" st.line st.col) - (Just c, st') => - if c == quote - then Ok (pack (reverse acc), st') - else if c == '\\' - then case consume st' of - (Just escaped, st'') => - let unescaped = case escaped of - 'n' => '\n' - 't' => '\t' - 'r' => '\r' - '\\' => '\\' - '"' => '"' - '\'' => '\'' - _ => escaped - in parseContent fuel (unescaped :: acc) st'' - (Nothing, _) => Err (SyntaxError "Unclosed escape" st.line st.col) - else parseContent fuel (c :: acc) st' + -- The explicit suffix and the private state start together and consume + -- one or two characters together. No full-suffix length scan is needed. + parseContent : List Char -> List Char -> ParserState -> YAMLResult (String, ParserState) + parseContent [] acc st = Err (SyntaxError "Unclosed string" st.line st.col) + parseContent (c :: cs) acc st = + let (_, st') = consume st + in if c == quote + then Ok (pack (reverse acc), st') + else if c == '\\' + then case cs of + [] => Err (SyntaxError "Unclosed escape" st.line st.col) + escaped :: rest => + let (_, st'') = consume st' + unescaped = case escaped of + 'n' => '\n' + 't' => '\t' + 'r' => '\r' + '\\' => '\\' + '"' => '"' + '\'' => '\'' + _ => escaped + in parseContent rest (unescaped :: acc) st'' + else parseContent cs (c :: acc) st' ||| Parse unquoted string (plain scalar) parsePlainScalar : ParserState -> YAMLResult (String, ParserState) diff --git a/tests/totality/BoundaryRegression.idr b/tests/totality/BoundaryRegression.idr index 33b4c833..92d2c74c 100644 --- a/tests/totality/BoundaryRegression.idr +++ b/tests/totality/BoundaryRegression.idr @@ -37,6 +37,13 @@ nestedYAML : Nat -> String nestedYAML Z = "true" nestedYAML (S n) = "[" ++ nestedYAML n ++ "]" +xmlSiblings : Nat -> String +xmlSiblings Z = "" +xmlSiblings (S n) = "" ++ xmlSiblings n +quotedItems : Nat -> String +quotedItems Z = "\"x\"" +quotedItems (S n) = "\"x\"," ++ quotedItems n + main : IO () main = do check "tree order including asymmetric levels" (levelorder tree == [1,2,3,4,5,6]) @@ -60,6 +67,12 @@ main = do check "XML nested and sibling/attribute paths" (isOk (parseXML (nestedXML 20)) && isOk (parseXML "text")) + check "XML flat elements and exact attribute limits" + (isOk (parseXML ("" ++ xmlSiblings 128 ++ "")) && + isOk (parseXMLWith ({ maxAttributesPerElement := 0 } Proven.SafeXML.Types.secureDefaults) "" ) && + isErr (parseXMLWith ({ maxAttributesPerElement := 0 } Proven.SafeXML.Types.secureDefaults) "" ) && + isOk (parseXMLWith ({ maxAttributesPerElement := 1 } Proven.SafeXML.Types.secureDefaults) "" ) && + isErr (parseXMLWith ({ maxAttributesPerElement := 1 } Proven.SafeXML.Types.secureDefaults) "" )) check "XML malformed and security refusals" (isErr (parseXML "") && isErr (parseXML "") && isErr (parseXML "")) @@ -67,6 +80,8 @@ main = do check "YAML flow collections and quoted escape" (isOk (parseYAML (nestedYAML 20)) && isOk (parseYAML "{\"key\": [true, false, \"a\\n\"]}")) + check "YAML flat quoted sequence" + (isOk (parseYAML ("[" ++ quotedItems 128 ++ "]"))) check "YAML malformed/forbidden-anchor refusals" (isErr (parseYAML "[true") && isErr (parseYAML "\"unclosed") && isErr (parseYAML "&a true")) diff --git a/verification/header/coq/RefCBits8.v b/verification/header/coq/RefCBits8.v index fee43318..99a6fcba 100644 --- a/verification/header/coq/RefCBits8.v +++ b/verification/header/coq/RefCBits8.v @@ -2,7 +2,7 @@ Copyright (c) 2026 Jonathan D.A. Jewell Hand re-proof of RefCBits8.agda. No source refinement is claimed. *) From Stdlib Require Import Bool Arith. -Module RefCBits8. +Module CodecBits8. Inductive Bits : nat -> Type := | bnil : Bits 0 | bcons : forall {n}, bool -> Bits n -> Bits (S n). @@ -39,4 +39,4 @@ Proof. reflexivity. Qed. Print Assumptions round_trip. Print Assumptions accepted_canonical. Print Assumptions null_rejected. -End RefCBits8. +End CodecBits8. From 35dae0ff3d65bc129a0713b637b4886d26893e83 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:24:24 +0000 Subject: [PATCH 21/24] Isolate diagnostic refusal and close detached dependency guard gaps --- .github/workflows/chapel-ci.yml | 2 +- .github/workflows/d-ci.yml | 2 +- .github/workflows/ocaml-ci.yml | 2 +- docs/adr/2026-11-16-header-refc-boundary.adoc | 13 +++++++++++++ scripts/build-header-refc.sh | 5 ++++- scripts/test-header-staging.sh | 4 +++- 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.github/workflows/chapel-ci.yml b/.github/workflows/chapel-ci.yml index e9a21bce..2002c0be 100644 --- a/.github/workflows/chapel-ci.yml +++ b/.github/workflows/chapel-ci.yml @@ -95,7 +95,7 @@ jobs: echo "::error file=.github/workflows/chapel-ci.yml::detachability violation: $p" forbidden=1 ;; - tests/*|scripts/*|src/*) + tests/*|scripts/*|./scripts/*|src/*) echo "::error file=.github/workflows/chapel-ci.yml::undeclared producer or root test dependency: $p" forbidden=1 ;; diff --git a/.github/workflows/d-ci.yml b/.github/workflows/d-ci.yml index 9bf97527..fe768fd9 100644 --- a/.github/workflows/d-ci.yml +++ b/.github/workflows/d-ci.yml @@ -64,7 +64,7 @@ jobs: echo "::error file=.github/workflows/d-ci.yml::detachability violation: $p" forbidden=1 ;; - tests/*|scripts/*|src/*) + tests/*|scripts/*|./scripts/*|src/*) echo "::error file=.github/workflows/d-ci.yml::undeclared producer or root test dependency: $p" forbidden=1 ;; diff --git a/.github/workflows/ocaml-ci.yml b/.github/workflows/ocaml-ci.yml index 08eb0110..0e35f052 100644 --- a/.github/workflows/ocaml-ci.yml +++ b/.github/workflows/ocaml-ci.yml @@ -65,7 +65,7 @@ jobs: echo "::error file=.github/workflows/ocaml-ci.yml::detachability violation: $p" forbidden=1 ;; - tests/*|scripts/*|src/*) + tests/*|scripts/*|./scripts/*|src/*) echo "::error file=.github/workflows/ocaml-ci.yml::undeclared producer or root test dependency: $p" forbidden=1 ;; diff --git a/docs/adr/2026-11-16-header-refc-boundary.adoc b/docs/adr/2026-11-16-header-refc-boundary.adoc index 419d7750..6121ab44 100644 --- a/docs/adr/2026-11-16-header-refc-boundary.adoc +++ b/docs/adr/2026-11-16-header-refc-boundary.adoc @@ -202,3 +202,16 @@ closure. Then the header loop needs a prefix invariant, bounds and progress proof, with an explicit RefC-call contract. UAA's real two-slot callback registry is a second consumer for state transitions, refusal and borrowed lifetimes; its differential execution is not yet an Atoma source-to-proof certificate. + +Review refinements remove repeated full-input scans: the regex state now +carries an erased equality tying its cached characters to the original String; +lookups use that cache. Constructing MatchState directly therefore requires the +new equality witness (initState supplies it). This is an internal Idris source +API change, with no C ABI layout change. List indexing still has linear cost; +no constant-time or complete resource bound is claimed. YAML quoted strings +now descend structurally through their suffix. XML attributes spend the declared +per-element attribute allowance, permitting zero attributes at a zero limit and +rejecting the first excess attribute without scanning the remaining document. +The strict executable corpus includes flat XML/YAML sequences and exact zero/ +one-attribute limits. The remaining element/collection fuel completeness debt +is unchanged. diff --git a/scripts/build-header-refc.sh b/scripts/build-header-refc.sh index c14e9367..be3b80da 100644 --- a/scripts/build-header-refc.sh +++ b/scripts/build-header-refc.sh @@ -42,7 +42,10 @@ cp "$root/src/Proven/FFI/HeaderKernelMain.idr" "$work/src/Proven/FFI/" if ! (cd "$work"; "$idris" -Werror --total --source-dir src --codegen refc -o header-kernel src/Proven/FFI/HeaderKernelMain.idr) > "$out/build.log" 2>&1; then cat "$out/build.log" >&2; exit 1 fi -if grep -q '^Error:' "$out/build.log" || [[ ! -s "$work/build/exec/header-kernel.c" ]]; then +if grep -q '^Error:' "$out/build.log"; then + cat "$out/build.log" >&2; echo 'FAIL: header compiler diagnostics' >&2; exit 1 +fi +if [[ ! -s "$work/build/exec/header-kernel.c" ]]; then cat "$out/build.log" >&2; echo 'FAIL: no valid RefC output' >&2; exit 1 fi grep -q 'Proven_SafeHeader_ByteKernel_headerStep' "$work/build/exec/header-kernel.c" diff --git a/scripts/test-header-staging.sh b/scripts/test-header-staging.sh index f0ccbc44..b8669899 100644 --- a/scripts/test-header-staging.sh +++ b/scripts/test-header-staging.sh @@ -19,6 +19,8 @@ if [[ $1 == --version ]]; then if [[ $PROVEN_HEADER_TEST_MODE == version ]]; then echo 'wrong compiler'; else echo 'Idris 2, version 0.8.0-b6cbc25d5'; fi elif [[ $PROVEN_HEADER_TEST_MODE == diagnostic ]]; then + mkdir -p build/exec + echo 'Proven_SafeHeader_ByteKernel_headerStep' > build/exec/header-kernel.c echo 'Error: injected header compiler diagnostic with successful exit' fi exit 0 @@ -42,7 +44,7 @@ for mode in version diagnostic empty; do grep -q 'header RefC bundle is incomplete' "$evidence/$mode-consumer.log" done grep -q 'requires Idris2' "$evidence/version.log" -grep -q 'Error: injected header compiler diagnostic' "$evidence/diagnostic.log" +grep -q '^FAIL: header compiler diagnostics' "$evidence/diagnostic.log" grep -q 'no valid RefC output' "$evidence/empty.log" echo PASS > "$evidence/result.txt" echo 'PASS: failed header producers remove stale artifacts and Zig refuses their bundles' From 50e54dc90d42671f21aa3a01f92de3adf3324b2c Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:26:44 +0000 Subject: [PATCH 22/24] Construct the character-cache equality before computing its length --- src/Proven/SafeRegex/Matcher.idr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Proven/SafeRegex/Matcher.idr b/src/Proven/SafeRegex/Matcher.idr index 0ea7ffff..9423c856 100644 --- a/src/Proven/SafeRegex/Matcher.idr +++ b/src/Proven/SafeRegex/Matcher.idr @@ -49,8 +49,8 @@ record MatchState where public export initState : String -> Nat -> RegexFlags -> MatchState initState s maxSteps flags = - let chars = unpack s - in MkMatchState 0 0 maxSteps [] s chars Refl (length chars) flags + let st = MkMatchState 0 0 maxSteps [] s (unpack s) Refl 0 flags + in { inputLen := length st.inputChars } st ||| Increment step counter public export From bb98fbc52454520daea08c813c56a303e7984548 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Mon, 16 Nov 2026 12:30:08 +0000 Subject: [PATCH 23/24] Move the ARM64 macOS check off the retired runner image --- .github/workflows/zig-ffi.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/zig-ffi.yml b/.github/workflows/zig-ffi.yml index 33c1ab21..a39d481b 100644 --- a/.github/workflows/zig-ffi.yml +++ b/.github/workflows/zig-ffi.yml @@ -69,7 +69,7 @@ jobs: include: - os: macos-15-intel target: x86_64-macos - - os: macos-14 + - os: macos-15 target: aarch64-macos runs-on: ${{ matrix.os }} timeout-minutes: 20 From 7a0b11c73b33435ac78c4aa95242b2ba353331de Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Wed, 18 Nov 2026 14:33:01 +0000 Subject: [PATCH 24/24] Make #237's build and OCaml full-API checks pass honestly: ABI status ledger, property rewrite, defect fixes (#239) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is stacked on #237 (`codex/real-header-refc-20261116`). It targets the two failing code checks there: `build (0.8.0)` and `ocaml-full-api`. Neither check was weakened, and no exports were stubbed. ## Reproduced first Both failures were reproduced locally at `bb98fbc` with the pinned toolchains (Idris2 0.8.0, Idris2 `b6cbc25d`, Zig 0.15.2, OCaml 5.1.1): - **Property diagnostics:** 28 of 29 modules fail, 241 diagnostics. This matches #237's own count. - **OCaml:** `Fatal error: Dl.DL_error(... undefined symbol: proven_deinit)`, and the full-API audit reports `Required=146 Present=9 Missing=137`. ## 1. C ABI status ledger (`bindings/c/include/proven-abi.tsv`) `proven.h` declares 162 functions. The qualified RefC library: - defines **9** of them with matching signatures; - exports **2** under conflicting signatures: `proven_init` (header `int32_t(void)`, Zig `?*Handle`) and `proven_is_initialized`; - exports **7** symbols that are not in the header at all. One is `proven_header_codegen_main`, the RefC `main`, which leaks out through `-Dmain=`. The new checker caught it on its first run. That leaves **151 owed**. Each row is classified as `exported`, `conflict`, `owed` or `undeclared`, and names its backend: `idris-refc` or `zig-legacy`. `scripts/check-abi-ledger.sh` checks the ledger in both directions against the header (after `cc -E`) and against `nm` of the real library: - An exported row that is missing from the library fails. - An owed row that appears in the library fails. - A library export with no ledger row fails. `test-header-bridge.sh` (the "Real Idris header ABI" job) runs the checker together with planted controls: 1 accept, 6 reject, 1 NOT RUN. The OCaml full binding now binds only the 7 `exported` symbols it previously declared. Before this change it could not load at all. `full-api-audit.sh` now checks that the binding ⊆ ledger-exported ⊆ `nm`, and that owed ∩ `nm` = ∅. Planted controls for this audit were run locally: - binding `proven_deinit` → FAIL - a library that exports `proven_deinit` → FAIL (`owed-present`) The recommendation is in `docs/adr/2026-11-17-c-abi-status-ledger.adoc`. In short: narrow first, as done here. Then implement owed rows only through the Idris core, in consumer-weighted slices, starting with SafeMath's checked arithmetic, which is declared by 84–87 of the 117 binding directories. After that, move the declaration into an Idris2 module that generates `proven.h`, the ledger and each binding's raw declarations. ## 2. Property suite rewritten against the real APIs All 28 failing modules were rewritten. Where a property holds definitionally it is now a proof. Closed cases that don't reduce are labelled finite execution checks. Properties that are true but unproved become named `…Obligation : Type` propositions, following the convention in #237's SafeMath work. Every bodyless `0 prop_…` declaration is gone; each one was acting as an unchecked postulate. Tautologies, and properties the API cannot express, are retired with a comment explaining why rather than deleted silently. `Main` no longer claims that compiling the suite proves every property. ## 3. Packaging `idris2 --install` installed neither `SafeJWT.Types` nor `SafeRegex.{Parser,Matcher}`, `SafeSQL.Builder`, `SafeXML.Parser` or `SafeYAML.Parser`. So for every package user, `import Proven.SafeJWT/SafeRegex/SafeSQL/SafeXML/SafeYAML` failed. All six are now listed in `proven.ipkg`. No file content or header changed. ## 4. Library defects the checks exposed **Fixed**, each with its check now passing and a regression case added: - **SafeHeader:** a header *name* containing CR/LF was accepted under the default options, so header injection worked through the name. NUL in a value and `:` in a name were also accepted. - **SafePassword:** every *accepted* password crashed at runtime on a bodyless `trustCheckPolicy` postulate. `MeetsPolicy` now carries erased evidence that `checkPolicy` returned `[]`. - **SafeJWT:** two-segment tokens were accepted. - **SafeJson:** trailing commas were accepted. The full parser also rejected `"1"` and `"[1]"` because its fuel ran out. - **SafePhone:** `+44…` was split as country code 4; letters were silently stripped; country code 0 was accepted. - **SafeBase64:** "UTF-8" encoding actually truncated each Char to its low byte. - **SafeUUID:** hex version nibbles were decoded wrongly. - **SafeEmail:** every dotted local part was rejected. - **SafeFile:** the empty path was accepted. - **SafeArgs:** required positionals could never be satisfied. **Strict known-defect ledger** (`tests/properties/KnownDefects.idr`). Each entry still reproduces. If one stops reproducing, the run **fails** until the entry is reclassified. - `toml-parser-stub`: the parser is a stub that returns `Ok []` for everything. - `yaml-block-collections-rejected` - `xml-doctype-unparsed`: billion-laughs and external-DTD input is rejected only by accident, and `allowDTD` has no effect. - `cookie-negative-max-age`: an owner call between rejecting ≤ 0 and rejecting < 0. ## 5. CI step fix The "Type check all modules" step ran from the repository root. From there, `src/Proven/Core.idr` is read as module `src.Proven.Core`, so the step could not pass for any module. It had never been reached before. It now runs from `src/`, the same way `proof-check` does. All 12 modules check. ## Remote evidence at head `9e4e542` `idris2-ci.yml` and `ocaml-ci.yml` only trigger for PRs targeting `main`, so they were dispatched on this branch with `workflow_dispatch`. - [Idris 2 CI run 44188825412](https://github.com/hyperpolymath/proven/actions/runs/44188825412): **`build (0.8.0)` success**, **`proof-check (0.8.0)` success**. - [OCaml CI run 44188830214](https://github.com/hyperpolymath/proven/actions/runs/44188830214): **`ocaml-full-api` success**. `ocaml-build`, `ocaml-symbol-audit`, `ocaml-smoke`, `ocaml-detachable-build` and `detachability-guard` also succeeded. - `pull_request` runs at the same head: - Real Idris header ABI, including the ledger check: success. - Agda/Cubical/Lean/Rocq replay: success. - Idris → RefC → Zig: success. - `verify-idris-build`, `enforce-idris-only`, `pr-fuzzing`, semgrep, Sonar and the manifest checks: success. `secret-scanner` and `spark-theatre-gate` ended with zero runtime. That is the known `startup_failure` pattern from the narrowed Actions allow-list, and the diff does not touch either workflow. ## Local evidence All runs are at the head of this branch: - **`build (0.8.0)`:** every step passes. - totality regressions: exit 0 - build: 0 errors - install - diagnose: 29/29 modules, 0 errors - `tests.ipkg` build - execute: 315 PASS, 20 ledgered expectations reproduce, exit 0 - type-check: 12/12 - **`proof-check (0.8.0)`:** 29/29 `*.Proofs` modules check. - **OCaml:** - `ocaml-build` passes. - `ocaml-symbol-audit`: 5/5. - `ocaml-smoke` passes. - `ocaml-full-api`: the probe loads, and the audit reports `Required=7 Present=7 Missing=0 LedgerOwed=151`. - Alcotest: 3/3. - Detachability guard: OK. ## Not addressed: owner decisions Both are reported on #230. - **Licence headers:** five `src/` files carry `SPDX-License-Identifier: Palimpsest-MPL-1.0`, and `Enforce Trustfile Policies` fails on them. They are untouched. - **Retired format and stray files:** `0-AI-MANIFEST.a2ml` and `Trustfile.a2ml` use the retired A2ML format, and there are two empty root files named `Off` and `On`. These are reported, not converted. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01J4QvDi2XA8N9NEXSvCspMr --------- Co-authored-by: Claude --- .github/workflows/idris2-ci.yml | 24 +- .github/workflows/ocaml-ci.yml | 4 +- bindings/c/include/proven-abi.tsv | 194 +++ bindings/ocaml/lib/proven.ml | 1351 +----------------- bindings/ocaml/lib/proven_ffi.ml | 954 +------------ bindings/ocaml/scripts/full-api-audit.sh | 37 +- bindings/ocaml/test/test_proven.ml | 235 +-- docs/adr/2026-11-17-c-abi-status-ledger.adoc | 133 ++ proven.ipkg | 6 + scripts/check-abi-ledger.sh | 85 ++ scripts/test-abi-ledger.sh | 62 + scripts/test-header-bridge.sh | 13 +- src/Proven/SafeArgs/Parser.idr | 24 +- src/Proven/SafeBase64/Decode.idr | 23 +- src/Proven/SafeBase64/Encode.idr | 16 +- src/Proven/SafeBase64/Proofs.idr | 9 +- src/Proven/SafeBase64/Types.idr | 6 + src/Proven/SafeCookie/Parser.idr | 12 +- src/Proven/SafeEmail/Parser.idr | 4 +- src/Proven/SafeFile/Operations.idr | 6 +- src/Proven/SafeHeader/Parser.idr | 43 +- src/Proven/SafeHeader/Proofs.idr | 71 +- src/Proven/SafeHeader/Types.idr | 20 +- src/Proven/SafeJWT/Decode.idr | 6 +- src/Proven/SafeJson.idr | 33 - src/Proven/SafeJson/Parser.idr | 64 +- src/Proven/SafePassword.idr | 11 +- src/Proven/SafePassword/Policy.idr | 24 +- src/Proven/SafePhone.idr | 101 +- src/Proven/SafeRegex/Matcher.idr | 2 +- src/Proven/SafeRegex/Parser.idr | 2 +- src/Proven/SafeSQL/Builder.idr | 2 +- src/Proven/SafeUUID.idr | 4 +- src/Proven/SafeXML/Parser.idr | 2 +- src/Proven/SafeYAML/Parser.idr | 2 +- tests.ipkg | 3 +- tests/properties/KnownDefects.idr | 63 + tests/properties/Main.idr | 23 +- tests/properties/SafeArgsProps.idr | 153 +- tests/properties/SafeBase64Props.idr | 211 ++- tests/properties/SafeCommandProps.idr | 160 ++- tests/properties/SafeContentTypeProps.idr | 151 +- tests/properties/SafeCookieProps.idr | 167 ++- tests/properties/SafeCryptoProps.idr | 172 ++- tests/properties/SafeCurrencyProps.idr | 148 +- tests/properties/SafeDateTimeProps.idr | 150 +- tests/properties/SafeEmailProps.idr | 124 +- tests/properties/SafeEnvProps.idr | 155 +- tests/properties/SafeFileProps.idr | 149 +- tests/properties/SafeHeaderProps.idr | 176 ++- tests/properties/SafeHexProps.idr | 151 +- tests/properties/SafeHtmlProps.idr | 178 ++- tests/properties/SafeJWTProps.idr | 162 ++- tests/properties/SafeJsonProps.idr | 171 ++- tests/properties/SafeNetworkProps.idr | 109 +- tests/properties/SafePasswordProps.idr | 146 +- tests/properties/SafePathProps.idr | 135 +- tests/properties/SafePhoneProps.idr | 171 ++- tests/properties/SafeRegexProps.idr | 122 +- tests/properties/SafeSQLProps.idr | 187 ++- tests/properties/SafeStringProps.idr | 180 ++- tests/properties/SafeTOMLProps.idr | 160 ++- tests/properties/SafeUUIDProps.idr | 188 ++- tests/properties/SafeUrlProps.idr | 131 +- tests/properties/SafeXMLProps.idr | 255 +++- tests/properties/SafeYAMLProps.idr | 276 +++- 66 files changed, 4079 insertions(+), 4233 deletions(-) create mode 100644 bindings/c/include/proven-abi.tsv create mode 100644 docs/adr/2026-11-17-c-abi-status-ledger.adoc create mode 100755 scripts/check-abi-ledger.sh create mode 100755 scripts/test-abi-ledger.sh create mode 100644 tests/properties/KnownDefects.idr diff --git a/.github/workflows/idris2-ci.yml b/.github/workflows/idris2-ci.yml index 6cf8c6a1..90b07bfb 100644 --- a/.github/workflows/idris2-ci.yml +++ b/.github/workflows/idris2-ci.yml @@ -99,19 +99,19 @@ jobs: - name: Execute property-test suite run: build/tests/exec/proven_tests - name: Type check all modules + # Run from the package source root with module-relative paths: from the + # repository root, `src/Proven/Core.idr` is read as module + # `src.Proven.Core` and every check fails on the module-name mismatch + # (the same pitfall documented in the proof-check job below). + working-directory: src run: | - idris2 --check src/Proven.idr - idris2 --check src/Proven/Core.idr - idris2 --check src/Proven/SafeMath.idr - idris2 --check src/Proven/SafeString.idr - idris2 --check src/Proven/SafeJson.idr - idris2 --check src/Proven/SafeUrl.idr - idris2 --check src/Proven/SafeEmail.idr - idris2 --check src/Proven/SafePath.idr - idris2 --check src/Proven/SafeCrypto.idr - idris2 --check src/Proven/SafePassword.idr - idris2 --check src/Proven/SafeDateTime.idr - idris2 --check src/Proven/SafeNetwork.idr + set -euo pipefail + for m in Proven.idr Proven/Core.idr Proven/SafeMath.idr Proven/SafeString.idr \ + Proven/SafeJson.idr Proven/SafeUrl.idr Proven/SafeEmail.idr \ + Proven/SafePath.idr Proven/SafeCrypto.idr Proven/SafePassword.idr \ + Proven/SafeDateTime.idr Proven/SafeNetwork.idr; do + idris2 -p contrib -p network --check "$m" + done # Fast, targeted gate over every Proofs.idr companion module. # # Rationale (issue #145): the swapped-arg `appendLengthInc` break (#127) diff --git a/.github/workflows/ocaml-ci.yml b/.github/workflows/ocaml-ci.yml index 0e35f052..5baaa6f9 100644 --- a/.github/workflows/ocaml-ci.yml +++ b/.github/workflows/ocaml-ci.yml @@ -22,6 +22,7 @@ on: - 'src/Proven/SafeHeader/ByteKernel.idr' - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' + - 'bindings/c/include/proven-abi.tsv' - '.github/workflows/ocaml-ci.yml' pull_request: branches: [main] @@ -32,6 +33,7 @@ on: - 'src/Proven/SafeHeader/ByteKernel.idr' - 'src/Proven/FFI/HeaderKernelMain.idr' - 'bindings/c/include/proven.h' + - 'bindings/c/include/proven-abi.tsv' - '.github/workflows/ocaml-ci.yml' workflow_dispatch: concurrency: @@ -59,7 +61,7 @@ jobs: while IFS= read -r raw; do p="${raw%[.,;:)]}" case "$p" in - bindings/ocaml/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|.github/workflows/ocaml-ci.yml|./scripts/symbol-audit.sh|scripts/build-header-refc.sh|src/Proven/SafeHeader/ByteKernel.idr|src/Proven/FFI/HeaderKernelMain.idr) + bindings/ocaml/*|ffi/zig/*|bindings/c/include|bindings/c/include/proven.h|bindings/c/include/proven-abi.tsv|.github/workflows/ocaml-ci.yml|./scripts/symbol-audit.sh|scripts/build-header-refc.sh|src/Proven/SafeHeader/ByteKernel.idr|src/Proven/FFI/HeaderKernelMain.idr) ;; bindings/*/*) echo "::error file=.github/workflows/ocaml-ci.yml::detachability violation: $p" diff --git a/bindings/c/include/proven-abi.tsv b/bindings/c/include/proven-abi.tsv new file mode 100644 index 00000000..058a04fe --- /dev/null +++ b/bindings/c/include/proven-abi.tsv @@ -0,0 +1,194 @@ +# SPDX-License-Identifier: MPL-2.0 +# Copyright (c) 2026 Jonathan D.A. Jewell +# +# libproven C ABI status ledger. One row per symbol declared in proven.h or +# exported by libproven. proven.h is the target ABI; this file says which part +# of it actually exists. Columns are tab-separated: +# +# symbol status backend note +# +# status exported in proven.h, exported, signature matches the header +# conflict exported under the header name with an incompatible signature +# owed in proven.h, not exported; an outstanding ABI obligation +# undeclared exported, absent from proven.h +# backend idris-refc computed by the compiled Idris core through RefC +# zig-legacy computed in Zig; not an Idris-verified implementation +# - no implementation +# +# Bindings may bind only `exported` rows. Checkers fail if an exported row is +# missing from the library, an owed row appears in it, or a header symbol or +# library export has no row. Move a row only together with the code change. +# +# Rows describe the qualified profile: build_standalone.zig -Dheader-refc on +# native Linux x86_64 GNU. Without -Dheader-refc, proven_header_has_crlf is +# still exported but refuses with status -99 (no Idris backend). +# Producer check: scripts/check-abi-ledger.sh (run by test-header-bridge.sh). +proven_angle_deg_to_rad owed - - +proven_angle_normalize_degrees owed - - +proven_angle_normalize_radians owed - - +proven_angle_rad_to_deg owed - - +proven_bloom_add owed - - +proven_bloom_contains owed - - +proven_bloom_create owed - - +proven_bloom_free owed - - +proven_buffer_append owed - - +proven_buffer_create owed - - +proven_buffer_free owed - - +proven_buffer_get owed - - +proven_build_info exported zig-legacy - +proven_calculator_eval owed - - +proven_callback_clear_all owed - - +proven_callback_count owed - - +proven_callback_fire owed - - +proven_callback_register owed - - +proven_callback_unregister owed - - +proven_checksum_crc32 owed - - +proven_checksum_verify_crc32 owed - - +proven_circuit_breaker_allow owed - - +proven_circuit_breaker_create owed - - +proven_circuit_breaker_failure owed - - +proven_circuit_breaker_free owed - - +proven_circuit_breaker_state owed - - +proven_circuit_breaker_success owed - - +proven_color_parse_hex owed - - +proven_color_rgb_to_hsl owed - - +proven_color_to_hex owed - - +proven_content_type_can_sniff_dangerous owed - - +proven_content_type_free owed - - +proven_content_type_is_json owed - - +proven_content_type_is_xml owed - - +proven_content_type_parse owed - - +proven_content_type_render owed - - +proven_cookie_build_delete owed - - +proven_cookie_build_set_cookie owed - - +proven_cookie_get_prefix owed - - +proven_cookie_has_injection owed - - +proven_cookie_validate_name owed - - +proven_cookie_validate_value owed - - +proven_crypto_constant_time_eq owed - - +proven_crypto_random_bytes owed - - +proven_currency_format owed - - +proven_currency_parse owed - - +proven_datetime_days_in_month owed - - +proven_datetime_format_iso8601 owed - - +proven_datetime_is_leap_year owed - - +proven_datetime_parse owed - - +proven_deinit owed - - +proven_digest_parse owed - - +proven_digest_to_string owed - - +proven_digest_verify owed - - +proven_email_is_valid owed - - +proven_ffi_abi_version exported zig-legacy - +proven_float_div owed - - +proven_float_is_finite owed - - +proven_float_is_nan owed - - +proven_float_ln owed - - +proven_float_sqrt owed - - +proven_free undeclared zig-legacy - +proven_free_string exported zig-legacy - +proven_geo_distance owed - - +proven_geo_in_bounds owed - - +proven_geo_validate owed - - +proven_get_string undeclared zig-legacy - +proven_graph_add_edge owed - - +proven_graph_create owed - - +proven_graph_free owed - - +proven_graph_has_edge owed - - +proven_header_build_csp owed - - +proven_header_build_hsts owed - - +proven_header_codegen_main undeclared idris-refc RefC program main renamed by -Dmain; not an API; should not be exported +proven_header_has_crlf exported idris-refc - +proven_header_is_dangerous owed - - +proven_header_is_valid_name owed - - +proven_header_render owed - - +proven_hex_decode owed - - +proven_hex_encode owed - - +proven_hex_free owed - - +proven_http_parse_www_authenticate owed - - +proven_http_url_decode owed - - +proven_http_url_encode owed - - +proven_init conflict zig-legacy header int32_t(void); export ?*Handle(void) +proven_is_initialized conflict zig-legacy header bool(void); export u32(?*Handle) +proven_json_get_type owed - - +proven_json_is_valid owed - - +proven_last_error undeclared zig-legacy - +proven_lru_create owed - - +proven_lru_free owed - - +proven_lru_get owed - - +proven_lru_put owed - - +proven_math_abs_safe owed - - +proven_math_add_checked owed - - +proven_math_clamp owed - - +proven_math_div owed - - +proven_math_mod owed - - +proven_math_mul_checked owed - - +proven_math_pow_checked owed - - +proven_math_sub_checked owed - - +proven_ml_clamp owed - - +proven_ml_leaky_relu owed - - +proven_ml_relu owed - - +proven_ml_sigmoid owed - - +proven_ml_softmax owed - - +proven_module_count owed - - +proven_monotonic_create owed - - +proven_monotonic_free owed - - +proven_monotonic_next owed - - +proven_network_ipv4_is_loopback owed - - +proven_network_ipv4_is_private owed - - +proven_network_parse_ipv4 owed - - +proven_password_is_common owed - - +proven_password_validate owed - - +proven_path_has_traversal exported zig-legacy - +proven_path_sanitize_filename owed - - +proven_phone_format_e164 owed - - +proven_phone_parse owed - - +proven_probability_and owed - - +proven_probability_create owed - - +proven_probability_not owed - - +proven_probability_or_exclusive owed - - +proven_process undeclared zig-legacy - +proven_process_array undeclared zig-legacy - +proven_queue_create owed - - +proven_queue_free owed - - +proven_queue_pop owed - - +proven_queue_push owed - - +proven_queue_size owed - - +proven_rate_limiter_create owed - - +proven_rate_limiter_free owed - - +proven_rate_limiter_try_acquire owed - - +proven_register_callback undeclared zig-legacy - +proven_registry_has_registry owed - - +proven_registry_parse owed - - +proven_registry_to_string owed - - +proven_retry_delay owed - - +proven_retry_should_retry owed - - +proven_state_machine_allow owed - - +proven_state_machine_create owed - - +proven_state_machine_free owed - - +proven_state_machine_state owed - - +proven_state_machine_transition owed - - +proven_string_escape_html owed - - +proven_string_escape_js owed - - +proven_string_escape_sql owed - - +proven_string_is_valid_utf8 owed - - +proven_tensor_create owed - - +proven_tensor_free owed - - +proven_tensor_get owed - - +proven_tensor_matmul owed - - +proven_tensor_set owed - - +proven_unit_convert_length owed - - +proven_unit_convert_temp owed - - +proven_url_free owed - - +proven_url_parse owed - - +proven_uuid_is_nil owed - - +proven_uuid_parse owed - - +proven_uuid_to_string owed - - +proven_uuid_v4 owed - - +proven_uuid_version owed - - +proven_version exported zig-legacy - +proven_version_compare owed - - +proven_version_free owed - - +proven_version_major exported zig-legacy - +proven_version_minor exported zig-legacy - +proven_version_parse owed - - +proven_version_patch exported zig-legacy - diff --git a/bindings/ocaml/lib/proven.ml b/bindings/ocaml/lib/proven.ml index 09cad8b3..f2142ff7 100644 --- a/bindings/ocaml/lib/proven.ml +++ b/bindings/ocaml/lib/proven.ml @@ -1,15 +1,19 @@ (* SPDX-License-Identifier: MPL-2.0 *) (* Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) *) -(** Proven: Formally verified safety primitives for OCaml. +(** Proven: safety primitives for OCaml, bound to libproven. - This library is a thin FFI wrapper around [libproven], which provides - formally verified (Idris 2) safety primitives. All computation is - performed inside the verified Idris 2 core -- this OCaml package does - data marshaling only. + This library marshals data only. It exposes exactly the libproven + functions whose C ABI status ledger row ([proven-abi.tsv] beside + [proven.h]) is [exported]. Each wrapper names its backend: - Every public function returns [option] or [result] instead of raising - exceptions. *) + - [SafeHeader.has_crlf] is computed by the compiled Idris core (RefC). + - [SafePath.has_traversal] and [Version] are computed in Zig and are not + Idris-verified implementations. + + The other [proven.h] functions are owed ABI obligations, not available + functions. Every public function returns a plain value or [option]; none + raises. *) open Ctypes module F = Proven_ffi @@ -18,7 +22,8 @@ module F = Proven_ffi (* Internal helpers *) (* ========================================================================== *) -(** Allocate a ctypes char pointer from an OCaml string. *) +(** Copy an OCaml string into a NUL-terminated buffer the caller owns. + Embedded NUL bytes are data: the length passed to C is [String.length]. *) let cstring_of_string (s : string) : char ptr = let len = String.length s in let buf = allocate_n char ~count:(len + 1) in @@ -28,74 +33,19 @@ let cstring_of_string (s : string) : char ptr = (buf +@ len) <-@ '\000'; buf -(** Read a C string (pointer + length) into an OCaml string, then free it - via [proven_free_string]. *) -let read_and_free_string (p : char ptr) (len : Unsigned.size_t) : string = - let n = Unsigned.Size_t.to_int len in - let s = Bytes.create n in - for i = 0 to n - 1 do - Bytes.set s i !@(p +@ i) - done; - F.proven_free_string p; - Bytes.to_string s - -(** Check if a status value is ok (== 0). *) -let status_ok (s : int32) : bool = - Int32.equal s F.proven_status_ok - -(** Unwrap an IntResult into [Some int64] if ok, [None] otherwise. *) -let unwrap_int_result (r : F.int_result structure) : int64 option = - if status_ok (getf r F.ir_status) then - Some (getf r F.ir_value) - else - None - (** Unwrap a BoolResult into [Some bool] if ok, [None] otherwise. *) let unwrap_bool_result (r : F.bool_result structure) : bool option = - if status_ok (getf r F.br_status) then + if Int32.equal (getf r F.br_status) F.proven_status_ok then Some (getf r F.br_value) else None -(** Unwrap a StringResult into [Some string] if ok, [None] otherwise. - The C string is freed after copying. *) -let unwrap_string_result (r : F.string_result structure) : string option = - if status_ok (getf r F.sr_status) then - let p = getf r F.sr_value in - let len = getf r F.sr_length in - if is_null p then None - else Some (read_and_free_string p len) - else - None - -(** Unwrap a FloatResult into [Some float] if ok, [None] otherwise. *) -let unwrap_float_result (r : F.float_result structure) : float option = - if status_ok (getf r F.fr_status) then - Some (getf r F.fr_value) - else - None - -(* ========================================================================== *) -(* Lifecycle *) -(* ========================================================================== *) - -module Lifecycle = struct - (** Initialize libproven. Returns [true] if initialization succeeded. *) - let init () : bool = - let rc = F.proven_init () in - Int32.equal rc 0l - - (** Deinitialize libproven. *) - let deinit () : unit = - F.proven_deinit () - - (** Check whether libproven has been initialized. *) - let is_initialized () : bool = - F.proven_is_initialized () -end +let check_span call (s : string) : bool option = + let p = cstring_of_string s in + unwrap_bool_result (call p (Unsigned.Size_t.of_int (String.length s))) (* ========================================================================== *) -(* Version *) +(* Version (zig-legacy) *) (* ========================================================================== *) module Version = struct @@ -114,1267 +64,34 @@ module Version = struct (** FFI ABI version. *) let abi_version () : int = Unsigned.UInt32.to_int (F.proven_ffi_abi_version ()) - - (** Number of modules in libproven. *) - let module_count () : int = - Unsigned.UInt32.to_int (F.proven_module_count ()) -end - -(* ========================================================================== *) -(* SafeMath *) -(* ========================================================================== *) - -module SafeMath = struct - (** Safe division. Returns [None] on division by zero. *) - let div (a : int) (b : int) : int option = - unwrap_int_result (F.proven_math_div (Int64.of_int a) (Int64.of_int b)) - |> Option.map Int64.to_int - - (** Safe modulo. Returns [None] on division by zero. *) - let modulo (a : int) (b : int) : int option = - unwrap_int_result (F.proven_math_mod (Int64.of_int a) (Int64.of_int b)) - |> Option.map Int64.to_int - - (** Checked addition. Returns [None] on overflow. *) - let add (a : int) (b : int) : int option = - unwrap_int_result (F.proven_math_add_checked (Int64.of_int a) (Int64.of_int b)) - |> Option.map Int64.to_int - - (** Checked subtraction. Returns [None] on underflow. *) - let sub (a : int) (b : int) : int option = - unwrap_int_result (F.proven_math_sub_checked (Int64.of_int a) (Int64.of_int b)) - |> Option.map Int64.to_int - - (** Checked multiplication. Returns [None] on overflow. *) - let mul (a : int) (b : int) : int option = - unwrap_int_result (F.proven_math_mul_checked (Int64.of_int a) (Int64.of_int b)) - |> Option.map Int64.to_int - - (** Safe absolute value. Handles MIN_INT correctly. *) - let abs (n : int) : int option = - unwrap_int_result (F.proven_math_abs_safe (Int64.of_int n)) - |> Option.map Int64.to_int - - (** Clamp value to range [lo, hi]. *) - let clamp ~lo ~hi (value : int) : int = - Int64.to_int (F.proven_math_clamp (Int64.of_int lo) (Int64.of_int hi) (Int64.of_int value)) - - (** Integer power with overflow checking. *) - let pow (base : int) (exp : int) : int option = - unwrap_int_result (F.proven_math_pow_checked (Int64.of_int base) (Unsigned.UInt32.of_int exp)) - |> Option.map Int64.to_int - - (** 64-bit variants for when OCaml [int] is too narrow. *) - - let div64 (a : int64) (b : int64) : int64 option = - unwrap_int_result (F.proven_math_div a b) - - let add64 (a : int64) (b : int64) : int64 option = - unwrap_int_result (F.proven_math_add_checked a b) - - let sub64 (a : int64) (b : int64) : int64 option = - unwrap_int_result (F.proven_math_sub_checked a b) - - let mul64 (a : int64) (b : int64) : int64 option = - unwrap_int_result (F.proven_math_mul_checked a b) end (* ========================================================================== *) -(* SafeString *) -(* ========================================================================== *) - -module SafeString = struct - (** Check whether a byte sequence is valid UTF-8. *) - let is_valid_utf8 (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_string_is_valid_utf8 p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> false - - (** Escape a string for SQL (single-quote doubling). *) - let escape_sql (s : string) : string option = - let p = cstring_of_string s in - unwrap_string_result (F.proven_string_escape_sql p (Unsigned.Size_t.of_int (String.length s))) - - (** Escape HTML angle brackets, ampersands and quotation marks. *) - let escape_html (s : string) : string option = - let p = cstring_of_string s in - unwrap_string_result (F.proven_string_escape_html p (Unsigned.Size_t.of_int (String.length s))) - - (** Escape a string for JavaScript (within quotes). *) - let escape_js (s : string) : string option = - let p = cstring_of_string s in - unwrap_string_result (F.proven_string_escape_js p (Unsigned.Size_t.of_int (String.length s))) -end - -(* ========================================================================== *) -(* SafePath *) +(* SafePath (zig-legacy) *) (* ========================================================================== *) module SafePath = struct - (** Check whether a path attempts directory traversal. *) - let has_traversal (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_path_has_traversal p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> true - - (** Sanitize a filename by removing dangerous characters. *) - let sanitize_filename (s : string) : string option = - let p = cstring_of_string s in - unwrap_string_result (F.proven_path_sanitize_filename p (Unsigned.Size_t.of_int (String.length s))) -end - -(* ========================================================================== *) -(* SafeCrypto *) -(* ========================================================================== *) - -module SafeCrypto = struct - (** Constant-time byte comparison (timing-safe). *) - let constant_time_eq (a : string) (b : string) : bool = - let pa = cstring_of_string a in - let pb = cstring_of_string b in - let r = F.proven_crypto_constant_time_eq - pa (Unsigned.Size_t.of_int (String.length a)) - pb (Unsigned.Size_t.of_int (String.length b)) in - match unwrap_bool_result r with Some v -> v | None -> false - - (** Fill a buffer with cryptographically secure random bytes. - Returns [None] on failure. *) - let random_bytes (n : int) : string option = - if n <= 0 then Some "" - else - let buf = allocate_n char ~count:n in - let rc = F.proven_crypto_random_bytes buf (Unsigned.Size_t.of_int n) in - if Int32.equal rc 0l then begin - let s = Bytes.create n in - for i = 0 to n - 1 do - Bytes.set s i !@(buf +@ i) - done; - Some (Bytes.to_string s) - end else - None -end - -(* ========================================================================== *) -(* SafeEmail *) -(* ========================================================================== *) - -module SafeEmail = struct - (** Check whether an email address is valid (RFC 5321 simplified). *) - let is_valid (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_email_is_valid p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> false -end - -(* ========================================================================== *) -(* SafeNetwork *) -(* ========================================================================== *) - -module SafeNetwork = struct - (** Parse an IPv4 address string. Returns the four octets on success. *) - let parse_ipv4 (s : string) : (int * int * int * int) option = - let p = cstring_of_string s in - let r = F.proven_network_parse_ipv4 p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.ipv4r_status) then - let addr = getf r F.ipv4r_address in - let octets = getf addr F.ipv4_octets in - Some ( - Unsigned.UInt8.to_int (CArray.get octets 0), - Unsigned.UInt8.to_int (CArray.get octets 1), - Unsigned.UInt8.to_int (CArray.get octets 2), - Unsigned.UInt8.to_int (CArray.get octets 3) - ) - else - None + (** Check whether a path attempts directory traversal. Fails closed: a + library error is reported as [true]. Use {!has_traversal_result} to + distinguish errors. *) + let has_traversal_result (s : string) : bool option = + check_span F.proven_path_has_traversal s - (** Check whether an IPv4 address is in a private range (RFC 1918). *) - let ipv4_is_private (a : int) (b : int) (c : int) (d : int) : bool = - let addr = make F.ipv4_address in - let octets = getf addr F.ipv4_octets in - CArray.set octets 0 (Unsigned.UInt8.of_int a); - CArray.set octets 1 (Unsigned.UInt8.of_int b); - CArray.set octets 2 (Unsigned.UInt8.of_int c); - CArray.set octets 3 (Unsigned.UInt8.of_int d); - F.proven_network_ipv4_is_private addr - - (** Check whether an IPv4 address is loopback (127.0.0.0/8). *) - let ipv4_is_loopback (a : int) (b : int) (c : int) (d : int) : bool = - let addr = make F.ipv4_address in - let octets = getf addr F.ipv4_octets in - CArray.set octets 0 (Unsigned.UInt8.of_int a); - CArray.set octets 1 (Unsigned.UInt8.of_int b); - CArray.set octets 2 (Unsigned.UInt8.of_int c); - CArray.set octets 3 (Unsigned.UInt8.of_int d); - F.proven_network_ipv4_is_loopback addr + let has_traversal (s : string) : bool = + match has_traversal_result s with Some v -> v | None -> true end (* ========================================================================== *) -(* SafeHeader *) +(* SafeHeader (idris-refc) *) (* ========================================================================== *) module SafeHeader = struct - (** Check for CRLF injection characters in a header value. *) - let has_crlf (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_header_has_crlf p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> true - - (** Check whether a header name is valid per RFC 7230. *) - let is_valid_name (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_header_is_valid_name p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> false - - (** Check whether a header name is in the dangerous-headers list. *) - let is_dangerous (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_header_is_dangerous p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> true - - (** Render a validated header string "Name: Value". *) - let render ~name ~value : string option = - let pn = cstring_of_string name in - let pv = cstring_of_string value in - unwrap_string_result (F.proven_header_render - pn (Unsigned.Size_t.of_int (String.length name)) - pv (Unsigned.Size_t.of_int (String.length value))) - - (** Build a Content-Security-Policy header value from JSON directives. *) - let build_csp (directives_json : string) : string option = - let p = cstring_of_string directives_json in - unwrap_string_result (F.proven_header_build_csp p (Unsigned.Size_t.of_int (String.length directives_json))) - - (** Build an HSTS header value. *) - let build_hsts ~max_age ~include_subdomains ~preload : string option = - unwrap_string_result (F.proven_header_build_hsts (Int64.of_int max_age) include_subdomains preload) -end - -(* ========================================================================== *) -(* SafeCookie *) -(* ========================================================================== *) - -module SafeCookie = struct - (** Check for cookie injection characters. *) - let has_injection (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_cookie_has_injection p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> true - - (** Validate a cookie name. *) - let validate_name (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_cookie_validate_name p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> false - - (** Validate a cookie value. *) - let validate_value (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_cookie_validate_value p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> false - - (** Get cookie prefix type: 0=none, 1=__Secure-, 2=__Host-. *) - let get_prefix (s : string) : int option = - let p = cstring_of_string s in - unwrap_int_result (F.proven_cookie_get_prefix p (Unsigned.Size_t.of_int (String.length s))) - |> Option.map Int64.to_int - - (** Build a delete-cookie header value. *) - let build_delete (name : string) : string option = - let p = cstring_of_string name in - unwrap_string_result (F.proven_cookie_build_delete p (Unsigned.Size_t.of_int (String.length name))) - - (* Note: build_set_cookie requires CookieAttributes struct marshaling. - Add if needed by extending Proven_ffi with the struct definition. *) -end - -(* ========================================================================== *) -(* SafeContentType *) -(* ========================================================================== *) - -module SafeContentType = struct - (** Check whether a content type could be sniffed to something dangerous. *) - let can_sniff_dangerous (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_content_type_can_sniff_dangerous p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> true - - (** Check whether a content type is JSON. *) - let is_json ~subtype ~suffix : bool = - let ps = cstring_of_string subtype in - let pf = cstring_of_string suffix in - let r = F.proven_content_type_is_json - ps (Unsigned.Size_t.of_int (String.length subtype)) - pf (Unsigned.Size_t.of_int (String.length suffix)) in - match unwrap_bool_result r with Some v -> v | None -> false - - (** Check whether a content type is XML. *) - let is_xml ~subtype ~suffix : bool = - let ps = cstring_of_string subtype in - let pf = cstring_of_string suffix in - let r = F.proven_content_type_is_xml - ps (Unsigned.Size_t.of_int (String.length subtype)) - pf (Unsigned.Size_t.of_int (String.length suffix)) in - match unwrap_bool_result r with Some v -> v | None -> false -end - -(* ========================================================================== *) -(* SafeUUID *) -(* ========================================================================== *) - -module SafeUUID = struct - (** Generate a random UUID v4. Returns the 36-character string. *) - let v4 () : string option = - let r = F.proven_uuid_v4 () in - if status_ok (getf r F.ur_status) then - let u = getf r F.ur_uuid in - let sr = F.proven_uuid_to_string u in - unwrap_string_result sr - else - None - - (** Parse a UUID from its string representation. Returns the 36-char string - if valid, [None] otherwise. *) - let parse (s : string) : string option = - let p = cstring_of_string s in - let r = F.proven_uuid_parse p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.ur_status) then - let u = getf r F.ur_uuid in - unwrap_string_result (F.proven_uuid_to_string u) - else - None - - (** Check whether a UUID is nil (all zeros). *) - let is_nil (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_uuid_parse p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.ur_status) then - F.proven_uuid_is_nil (getf r F.ur_uuid) - else - false - - (** Get the UUID version number (1-8). *) - let version (s : string) : int option = - let p = cstring_of_string s in - let r = F.proven_uuid_parse p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.ur_status) then - Some (Unsigned.UInt8.to_int (F.proven_uuid_version (getf r F.ur_uuid))) - else - None - - (** Check whether a string is a valid UUID. *) - let is_valid (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_uuid_parse p (Unsigned.Size_t.of_int (String.length s)) in - status_ok (getf r F.ur_status) -end - -(* ========================================================================== *) -(* SafeJson *) -(* ========================================================================== *) - -module SafeJson = struct - (** Check whether a string is valid JSON. *) - let is_valid (s : string) : bool = - let p = cstring_of_string s in - let r = F.proven_json_is_valid p (Unsigned.Size_t.of_int (String.length s)) in - match unwrap_bool_result r with Some v -> v | None -> false - - (** JSON value types. *) - type json_type = - | Null - | Bool - | Number - | String - | Array - | Object - | Invalid - - (** Get the root-level JSON value type. *) - let get_type (s : string) : json_type = - let p = cstring_of_string s in - let t = F.proven_json_get_type p (Unsigned.Size_t.of_int (String.length s)) in - match Int32.to_int t with - | 0 -> Null - | 1 -> Bool - | 2 -> Number - | 3 -> String - | 4 -> Array - | 5 -> Object - | _ -> Invalid -end - -(* ========================================================================== *) -(* SafeDateTime *) -(* ========================================================================== *) - -module SafeDateTime = struct - (** Parsed date-time components. *) - type t = { - year : int; - month : int; - day : int; - hour : int; - minute : int; - second : int; - nanosecond : int; - tz_offset_minutes : int; - } - - (** Parse an ISO 8601 date/time string. *) - let parse (s : string) : t option = - let p = cstring_of_string s in - let r = F.proven_datetime_parse p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.dtr_status) then - let dt = getf r F.dtr_datetime in - Some { - year = Int32.to_int (getf dt F.dt_year); - month = Unsigned.UInt8.to_int (getf dt F.dt_month); - day = Unsigned.UInt8.to_int (getf dt F.dt_day); - hour = Unsigned.UInt8.to_int (getf dt F.dt_hour); - minute = Unsigned.UInt8.to_int (getf dt F.dt_minute); - second = Unsigned.UInt8.to_int (getf dt F.dt_second); - nanosecond = Unsigned.UInt32.to_int (getf dt F.dt_nanosecond); - tz_offset_minutes = getf dt F.dt_tz_offset_minutes; - } - else - None - - (** Format a date-time as ISO 8601. *) - let format_iso8601 (t : t) : string option = - let dt = make F.datetime in - setf dt F.dt_year (Int32.of_int t.year); - setf dt F.dt_month (Unsigned.UInt8.of_int t.month); - setf dt F.dt_day (Unsigned.UInt8.of_int t.day); - setf dt F.dt_hour (Unsigned.UInt8.of_int t.hour); - setf dt F.dt_minute (Unsigned.UInt8.of_int t.minute); - setf dt F.dt_second (Unsigned.UInt8.of_int t.second); - setf dt F.dt_nanosecond (Unsigned.UInt32.of_int t.nanosecond); - setf dt F.dt_tz_offset_minutes t.tz_offset_minutes; - unwrap_string_result (F.proven_datetime_format_iso8601 dt) - - (** Check whether a year is a leap year. *) - let is_leap_year (year : int) : bool = - F.proven_datetime_is_leap_year (Int32.of_int year) - - (** Get the number of days in a month. *) - let days_in_month ~year ~month : int = - Unsigned.UInt8.to_int (F.proven_datetime_days_in_month (Int32.of_int year) (Unsigned.UInt8.of_int month)) -end - -(* ========================================================================== *) -(* SafeFloat *) -(* ========================================================================== *) - -module SafeFloat = struct - (** Safe floating-point division. *) - let div (a : float) (b : float) : float option = - unwrap_float_result (F.proven_float_div a b) - - (** Check whether a float is finite (not NaN or Inf). *) - let is_finite (x : float) : bool = - F.proven_float_is_finite x - - (** Check whether a float is NaN. *) - let is_nan (x : float) : bool = - F.proven_float_is_nan x - - (** Safe square root (returns [None] for negative input). *) - let sqrt (x : float) : float option = - unwrap_float_result (F.proven_float_sqrt x) - - (** Safe natural logarithm (returns [None] for non-positive input). *) - let ln (x : float) : float option = - unwrap_float_result (F.proven_float_ln x) -end - -(* ========================================================================== *) -(* SafePassword *) -(* ========================================================================== *) - -module SafePassword = struct - (** Password strength levels. *) - type strength = - | Very_weak - | Weak - | Fair - | Strong - | Very_strong - - (** Password validation result. *) - type validation = { - strength : strength; - has_lowercase : bool; - has_uppercase : bool; - has_digit : bool; - has_special : bool; - length : int; - } - - (** Validate password strength. *) - let validate (s : string) : validation = - let p = cstring_of_string s in - let r = F.proven_password_validate p (Unsigned.Size_t.of_int (String.length s)) in - let str = match Int32.to_int (getf r F.pw_strength) with - | 0 -> Very_weak - | 1 -> Weak - | 2 -> Fair - | 3 -> Strong - | 4 -> Very_strong - | _ -> Very_weak - in - { - strength = str; - has_lowercase = getf r F.pw_has_lowercase; - has_uppercase = getf r F.pw_has_uppercase; - has_digit = getf r F.pw_has_digit; - has_special = getf r F.pw_has_special; - length = Unsigned.Size_t.to_int (getf r F.pw_length); - } - - (** Check whether a password is in the common-passwords list. *) - let is_common (s : string) : bool = - let p = cstring_of_string s in - F.proven_password_is_common p (Unsigned.Size_t.of_int (String.length s)) -end - -(* ========================================================================== *) -(* SafeVersion (semver) *) -(* ========================================================================== *) - -module SafeVersion = struct - (** Parse a semantic version string. Returns (major, minor, patch) on - success, or [None] on parse failure. *) - let parse (s : string) : (int * int * int) option = - let p = cstring_of_string s in - let r = F.proven_version_parse_semver p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.vr_status) then begin - let v = getf r F.vr_version in - let major = Unsigned.UInt32.to_int (getf v F.sv_major) in - let minor = Unsigned.UInt32.to_int (getf v F.sv_minor) in - let patch = Unsigned.UInt32.to_int (getf v F.sv_patch) in - (* Free the version result (may contain prerelease string) *) - let vp = allocate F.semantic_version v in - F.proven_version_free vp; - Some (major, minor, patch) - end else - None - - (** Compare two version strings. Returns [Some n] where [n < 0] means - [a < b], [n = 0] means equal, [n > 0] means [a > b]. - Returns [None] if either string fails to parse. *) - let compare (a_str : string) (b_str : string) : int option = - let pa = cstring_of_string a_str in - let ra = F.proven_version_parse_semver pa (Unsigned.Size_t.of_int (String.length a_str)) in - if not (status_ok (getf ra F.vr_status)) then None - else begin - let pb = cstring_of_string b_str in - let rb = F.proven_version_parse_semver pb (Unsigned.Size_t.of_int (String.length b_str)) in - if not (status_ok (getf rb F.vr_status)) then begin - let vpa = allocate F.semantic_version (getf ra F.vr_version) in - F.proven_version_free vpa; - None - end else begin - let va = getf ra F.vr_version in - let vb = getf rb F.vr_version in - let result = Int32.to_int (F.proven_version_compare va vb) in - let vpa = allocate F.semantic_version va in - let vpb = allocate F.semantic_version vb in - F.proven_version_free vpa; - F.proven_version_free vpb; - Some result - end - end -end - -(* ========================================================================== *) -(* SafeGeo *) -(* ========================================================================== *) - -module SafeGeo = struct - (** Validate and normalize a geographic coordinate. - Returns [(latitude, longitude)] on success. *) - let validate ~lat ~lon : (float * float) option = - let r = F.proven_geo_validate lat lon in - if status_ok (getf r F.geor_status) then - let coord = getf r F.geor_coordinate in - Some (getf coord F.geo_latitude, getf coord F.geo_longitude) - else - None - - (** Calculate distance between two points in meters (Haversine). *) - let distance ~lat1 ~lon1 ~lat2 ~lon2 : float option = - let a = make F.geo_coordinate in - setf a F.geo_latitude lat1; - setf a F.geo_longitude lon1; - let b = make F.geo_coordinate in - setf b F.geo_latitude lat2; - setf b F.geo_longitude lon2; - unwrap_float_result (F.proven_geo_distance a b) - - (** Check whether a coordinate is within a bounding box. *) - let in_bounds ~lat ~lon ~min_lat ~max_lat ~min_lon ~max_lon : bool = - let coord = make F.geo_coordinate in - setf coord F.geo_latitude lat; - setf coord F.geo_longitude lon; - F.proven_geo_in_bounds coord min_lat max_lat min_lon max_lon -end - -(* ========================================================================== *) -(* SafeChecksum *) -(* ========================================================================== *) - -module SafeChecksum = struct - (** Calculate CRC32 of a byte string. *) - let crc32 (s : string) : int option = - let p = cstring_of_string s in - unwrap_int_result (F.proven_checksum_crc32 p (Unsigned.Size_t.of_int (String.length s))) - |> Option.map Int64.to_int - - (** Verify that CRC32 of data matches an expected value. *) - let verify_crc32 (s : string) (expected : int) : bool = - let p = cstring_of_string s in - let r = F.proven_checksum_verify_crc32 p - (Unsigned.Size_t.of_int (String.length s)) - (Unsigned.UInt32.of_int expected) in - match unwrap_bool_result r with Some v -> v | None -> false -end - -(* ========================================================================== *) -(* SafeProbability *) -(* ========================================================================== *) - -module SafeProbability = struct - (** Create a probability value, clamped to [0, 1]. *) - let create (v : float) : float = - F.proven_probability_create v - - (** Probability of independent events both occurring (a * b). *) - let both (a : float) (b : float) : float = - F.proven_probability_and a b - - (** Probability of mutually exclusive events (a + b). *) - let either_exclusive (a : float) (b : float) : float = - F.proven_probability_or_exclusive a b - - (** Complement probability (1 - p). *) - let complement (p : float) : float = - F.proven_probability_not p -end - -(* ========================================================================== *) -(* SafeCalculator *) -(* ========================================================================== *) - -module SafeCalculator = struct - (** Evaluate a mathematical expression string. *) - let eval (expr : string) : float option = - let p = cstring_of_string expr in - unwrap_float_result (F.proven_calculator_eval p (Unsigned.Size_t.of_int (String.length expr))) -end - -(* ========================================================================== *) -(* SafeHex *) -(* ========================================================================== *) + (** Check every byte of a header value for CR or LF. Fails closed: a + library error (including an unavailable Idris backend) is reported as + [true]. Use {!has_crlf_result} to distinguish errors. *) + let has_crlf_result (s : string) : bool option = + check_span F.proven_header_has_crlf s -module SafeHex = struct - (** Hex-encode bytes. [uppercase] selects A-F vs a-f. *) - let encode ?(uppercase = false) (s : string) : string option = - let p = cstring_of_string s in - unwrap_string_result (F.proven_hex_encode p (Unsigned.Size_t.of_int (String.length s)) uppercase) - - (** Hex-decode a string to bytes. *) - let decode (hex : string) : string option = - let p = cstring_of_string hex in - let r = F.proven_hex_decode p (Unsigned.Size_t.of_int (String.length hex)) in - if status_ok (getf r F.hdr_status) then begin - let data = getf r F.hdr_data in - let len = Unsigned.Size_t.to_int (getf r F.hdr_length) in - if is_null data then None - else begin - let s = Bytes.create len in - for i = 0 to len - 1 do - Bytes.set s i (Char.chr (Unsigned.UInt8.to_int !@(data +@ i))) - done; - let rp = allocate F.hex_decode_result r in - F.proven_hex_free rp; - Some (Bytes.to_string s) - end - end else - None -end - -(* ========================================================================== *) -(* SafeCurrency *) -(* ========================================================================== *) - -module SafeCurrency = struct - (** Parsed currency amount. *) - type t = { - amount_minor : int64; - currency_code : string; - decimal_places : int; - } - - (** Parse a currency string (e.g., "USD 123.45"). *) - let parse (s : string) : t option = - let p = cstring_of_string s in - let r = F.proven_currency_parse p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.cur_status) then begin - let code_arr = getf r F.cur_currency_code in - let code = Printf.sprintf "%c%c%c" - (Char.chr (Unsigned.UInt8.to_int (CArray.get code_arr 0))) - (Char.chr (Unsigned.UInt8.to_int (CArray.get code_arr 1))) - (Char.chr (Unsigned.UInt8.to_int (CArray.get code_arr 2))) in - Some { - amount_minor = getf r F.cur_amount_minor; - currency_code = code; - decimal_places = Unsigned.UInt8.to_int (getf r F.cur_decimal_places); - } - end else - None - - (** Format a currency amount to string. *) - let format ~amount_minor ~currency_code ~decimal_places : string option = - if String.length currency_code <> 3 then None - else begin - let code = CArray.make uint8_t 3 in - CArray.set code 0 (Unsigned.UInt8.of_int (Char.code currency_code.[0])); - CArray.set code 1 (Unsigned.UInt8.of_int (Char.code currency_code.[1])); - CArray.set code 2 (Unsigned.UInt8.of_int (Char.code currency_code.[2])); - unwrap_string_result (F.proven_currency_format amount_minor code (Unsigned.UInt8.of_int decimal_places)) - end -end - -(* ========================================================================== *) -(* SafePhone *) -(* ========================================================================== *) - -module SafePhone = struct - (** Parsed phone number. *) - type t = { - country_code : int; - national_number : int64; - is_valid : bool; - } - - (** Parse a phone number to E.164 components. *) - let parse (s : string) : t option = - let p = cstring_of_string s in - let r = F.proven_phone_parse p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.phr_status) then - Some { - country_code = Unsigned.UInt16.to_int (getf r F.phr_country_code); - national_number = Unsigned.UInt64.to_int64 (getf r F.phr_national_number); - is_valid = getf r F.phr_is_valid; - } - else - None - - (** Format a phone number as E.164 string. *) - let format_e164 ~country_code ~national_number : string option = - unwrap_string_result (F.proven_phone_format_e164 - (Unsigned.UInt16.of_int country_code) - (Unsigned.UInt64.of_int64 national_number)) -end - -(* ========================================================================== *) -(* SafeColor *) -(* ========================================================================== *) - -module SafeColor = struct - (** RGB color. *) - type rgb = { r : int; g : int; b : int } - - (** HSL color. *) - type hsl = { h : float; s : float; l : float } - - (** Parse a hex color string (#RRGGBB or #RGB). *) - let parse_hex (s : string) : rgb option = - let p = cstring_of_string s in - let r = F.proven_color_parse_hex p (Unsigned.Size_t.of_int (String.length s)) in - if status_ok (getf r F.cpr_status) then begin - let c = getf r F.cpr_color in - Some { - r = Unsigned.UInt8.to_int (getf c F.rgb_r); - g = Unsigned.UInt8.to_int (getf c F.rgb_g); - b = Unsigned.UInt8.to_int (getf c F.rgb_b); - } - end else - None - - (** Convert RGB to HSL. *) - let rgb_to_hsl (color : rgb) : hsl = - let c = make F.rgb_color in - setf c F.rgb_r (Unsigned.UInt8.of_int color.r); - setf c F.rgb_g (Unsigned.UInt8.of_int color.g); - setf c F.rgb_b (Unsigned.UInt8.of_int color.b); - let h = F.proven_color_rgb_to_hsl c in - { h = getf h F.hsl_h; s = getf h F.hsl_s; l = getf h F.hsl_l } - - (** Format RGB as hex string. *) - let to_hex (color : rgb) : string option = - let c = make F.rgb_color in - setf c F.rgb_r (Unsigned.UInt8.of_int color.r); - setf c F.rgb_g (Unsigned.UInt8.of_int color.g); - setf c F.rgb_b (Unsigned.UInt8.of_int color.b); - unwrap_string_result (F.proven_color_to_hex c) -end - -(* ========================================================================== *) -(* SafeAngle *) -(* ========================================================================== *) - -module SafeAngle = struct - (** Convert degrees to radians. *) - let deg_to_rad (degrees : float) : float = - F.proven_angle_deg_to_rad degrees - - (** Convert radians to degrees. *) - let rad_to_deg (radians : float) : float = - F.proven_angle_rad_to_deg radians - - (** Normalize angle to [0, 360) degrees. *) - let normalize_degrees (degrees : float) : float = - F.proven_angle_normalize_degrees degrees - - (** Normalize angle to [0, 2*pi) radians. *) - let normalize_radians (radians : float) : float = - F.proven_angle_normalize_radians radians -end - -(* ========================================================================== *) -(* SafeUnit *) -(* ========================================================================== *) - -module SafeUnit = struct - (** Length unit identifiers (matching libproven LengthUnit enum). *) - type length_unit = - | Meters | Kilometers | Centimeters | Millimeters - | Feet | Inches | Miles | Yards - - let length_unit_to_int = function - | Meters -> 0 | Kilometers -> 1 | Centimeters -> 2 | Millimeters -> 3 - | Feet -> 4 | Inches -> 5 | Miles -> 6 | Yards -> 7 - - (** Temperature unit identifiers (matching libproven TempUnit enum). *) - type temp_unit = Celsius | Fahrenheit | Kelvin - - let temp_unit_to_int = function - | Celsius -> 0 | Fahrenheit -> 1 | Kelvin -> 2 - - (** Convert a length value between units. *) - let convert_length ~from ~to_ (value : float) : float option = - unwrap_float_result (F.proven_unit_convert_length value - (Int32.of_int (length_unit_to_int from)) - (Int32.of_int (length_unit_to_int to_))) - - (** Convert a temperature value between units. *) - let convert_temp ~from ~to_ (value : float) : float option = - unwrap_float_result (F.proven_unit_convert_temp value - (Int32.of_int (temp_unit_to_int from)) - (Int32.of_int (temp_unit_to_int to_))) -end - -(* ========================================================================== *) -(* SafeHttp *) -(* ========================================================================== *) - -module SafeHttp = struct - (** URL-encode a string (RFC 3986 percent encoding). *) - let url_encode (s : string) : string option = - let p = cstring_of_string s in - unwrap_string_result (F.proven_http_url_encode p (Unsigned.Size_t.of_int (String.length s))) - - (** URL-decode a percent-encoded string. *) - let url_decode (s : string) : string option = - let p = cstring_of_string s in - unwrap_string_result (F.proven_http_url_decode p (Unsigned.Size_t.of_int (String.length s))) -end - -(* ========================================================================== *) -(* SafeBuffer *) -(* ========================================================================== *) - -module SafeBuffer = struct - (** Opaque buffer handle. *) - type t = F.bounded_buffer structure ptr - - (** Create a bounded buffer with the given capacity. *) - let create (capacity : int) : t option = - let r = F.proven_buffer_create (Unsigned.Size_t.of_int capacity) in - if status_ok (getf r F.bufr_status) then - let buf = getf r F.bufr_buffer in - if is_null buf then None - else Some buf - else - None - - (** Append data to the buffer. Returns [true] on success. *) - let append (buf : t) (data : string) : bool = - let p = cstring_of_string data in - let rc = F.proven_buffer_append buf p (Unsigned.Size_t.of_int (String.length data)) in - Int32.equal rc 0l - - (** Free the buffer. *) - let free (buf : t) : unit = - F.proven_buffer_free buf -end - -(* ========================================================================== *) -(* SafeRateLimiter *) -(* ========================================================================== *) - -module SafeRateLimiter = struct - (** Opaque rate limiter handle. *) - type t = F.rate_limiter structure ptr - - (** Create a token-bucket rate limiter. *) - let create ~capacity ~refill_rate : t option = - F.proven_rate_limiter_create capacity refill_rate - - (** Try to acquire [tokens] from the limiter. Returns [true] if allowed. *) - let try_acquire (rl : t) (tokens : float) : bool = - F.proven_rate_limiter_try_acquire rl tokens - - (** Free the rate limiter. *) - let free (rl : t) : unit = - F.proven_rate_limiter_free rl -end - -(* ========================================================================== *) -(* SafeCircuitBreaker *) -(* ========================================================================== *) - -module SafeCircuitBreaker = struct - (** Circuit breaker states. *) - type state = Closed | Open | Half_open - - (** Opaque circuit breaker handle. *) - type t = F.circuit_breaker structure ptr - - (** Create a circuit breaker. *) - let create ~failure_threshold ~success_threshold ~timeout_ms : t option = - F.proven_circuit_breaker_create - (Unsigned.UInt32.of_int failure_threshold) - (Unsigned.UInt32.of_int success_threshold) - (Int64.of_int timeout_ms) - - (** Check whether a request should be allowed. *) - let allow (cb : t) : bool = - F.proven_circuit_breaker_allow cb - - (** Record a successful request. *) - let success (cb : t) : unit = - F.proven_circuit_breaker_success cb - - (** Record a failed request. *) - let failure (cb : t) : unit = - F.proven_circuit_breaker_failure cb - - (** Get the current circuit breaker state. *) - let state (cb : t) : state = - match Int32.to_int (F.proven_circuit_breaker_state cb) with - | 0 -> Closed - | 1 -> Open - | 2 -> Half_open - | _ -> Open - - (** Free the circuit breaker. *) - let free (cb : t) : unit = - F.proven_circuit_breaker_free cb -end - -(* ========================================================================== *) -(* SafeRetry *) -(* ========================================================================== *) - -module SafeRetry = struct - (** Retry configuration. *) - type config = { - max_attempts : int; - base_delay_ms : int; - max_delay_ms : int; - multiplier : float; - } - - let make_config (c : config) : F.retry_config structure = - let rc = make F.retry_config in - setf rc F.rc_max_attempts (Unsigned.UInt32.of_int c.max_attempts); - setf rc F.rc_base_delay_ms (Unsigned.UInt64.of_int c.base_delay_ms); - setf rc F.rc_max_delay_ms (Unsigned.UInt64.of_int c.max_delay_ms); - setf rc F.rc_multiplier c.multiplier; - rc - - (** Calculate the delay for a given attempt number. *) - let delay (config : config) (attempt : int) : int = - Unsigned.UInt64.to_int (F.proven_retry_delay (make_config config) (Unsigned.UInt32.of_int attempt)) - - (** Check whether a retry should be attempted. *) - let should_retry (config : config) (attempt : int) : bool = - F.proven_retry_should_retry (make_config config) (Unsigned.UInt32.of_int attempt) -end - -(* ========================================================================== *) -(* SafeMonotonic *) -(* ========================================================================== *) - -module SafeMonotonic = struct - (** Opaque monotonic counter handle. *) - type t = F.monotonic_counter structure ptr - - (** Create a monotonic counter with initial value and maximum. *) - let create ~initial ~max_value : t option = - F.proven_monotonic_create - (Unsigned.UInt64.of_int initial) - (Unsigned.UInt64.of_int max_value) - - (** Get the next value from the counter. Returns [None] at overflow. *) - let next (c : t) : int64 option = - unwrap_int_result (F.proven_monotonic_next c) - - (** Free the counter. *) - let free (c : t) : unit = - F.proven_monotonic_free c -end - -(* ========================================================================== *) -(* SafeStateMachine *) -(* ========================================================================== *) - -module SafeStateMachine = struct - (** Opaque state machine handle. *) - type t = F.state_machine structure ptr - - (** Create a state machine with [state_count] states and an initial state. *) - let create ~state_count ~initial_state : t option = - F.proven_state_machine_create - (Unsigned.UInt32.of_int state_count) - (Unsigned.UInt32.of_int initial_state) - - (** Allow a transition from one state to another. Returns success. *) - let allow_transition (sm : t) ~from ~to_ : bool = - F.proven_state_machine_allow sm - (Unsigned.UInt32.of_int from) (Unsigned.UInt32.of_int to_) - - (** Try to transition to a new state. Returns [true] if the transition - was allowed and completed. *) - let transition (sm : t) ~to_ : bool = - F.proven_state_machine_transition sm (Unsigned.UInt32.of_int to_) - - (** Get the current state. *) - let current_state (sm : t) : int = - Unsigned.UInt32.to_int (F.proven_state_machine_state sm) - - (** Free the state machine. *) - let free (sm : t) : unit = - F.proven_state_machine_free sm -end - -(* ========================================================================== *) -(* SafeTensor *) -(* ========================================================================== *) - -module SafeTensor = struct - (** Opaque 2D tensor handle. *) - type t = F.tensor_2d structure ptr - - (** Create a [rows x cols] tensor (initialized to zero). *) - let create ~rows ~cols : t option = - F.proven_tensor_create - (Unsigned.Size_t.of_int rows) - (Unsigned.Size_t.of_int cols) - - (** Set a value at (row, col). Returns [true] on success. *) - let set (t : t) ~row ~col (value : float) : bool = - let rc = F.proven_tensor_set t - (Unsigned.Size_t.of_int row) - (Unsigned.Size_t.of_int col) value in - Int32.equal rc 0l - - (** Get a value at (row, col). *) - let get (t : t) ~row ~col : float option = - unwrap_float_result (F.proven_tensor_get t - (Unsigned.Size_t.of_int row) - (Unsigned.Size_t.of_int col)) - - (** Matrix multiplication. Returns [None] if dimensions are incompatible. *) - let matmul (a : t) (b : t) : t option = - F.proven_tensor_matmul a b - - (** Free the tensor. *) - let free (t : t) : unit = - F.proven_tensor_free t -end - -(* ========================================================================== *) -(* SafeMl *) -(* ========================================================================== *) - -module SafeMl = struct - (** Sigmoid activation function. *) - let sigmoid (x : float) : float = - F.proven_ml_sigmoid x - - (** ReLU activation function. *) - let relu (x : float) : float = - F.proven_ml_relu x - - (** Leaky ReLU activation function. *) - let leaky_relu ?(alpha = 0.01) (x : float) : float = - F.proven_ml_leaky_relu x alpha - - (** Clamp value to [min_val, max_val]. *) - let clamp ~min_val ~max_val (x : float) : float = - F.proven_ml_clamp x min_val max_val -end - -(* ========================================================================== *) -(* SafeLru *) -(* ========================================================================== *) - -module SafeLru = struct - (** Opaque LRU cache handle. *) - type t = F.lru_cache structure ptr - - (** Create an LRU cache with the given capacity. *) - let create (capacity : int) : t option = - F.proven_lru_create (Unsigned.Size_t.of_int capacity) - - (** Get a value by key. Returns [None] on miss. *) - let get (cache : t) (key : int) : int64 option = - unwrap_int_result (F.proven_lru_get cache (Unsigned.UInt64.of_int key)) - - (** Put a key-value pair. Returns [true] on success. *) - let put (cache : t) (key : int) (value : int64) : bool = - let rc = F.proven_lru_put cache (Unsigned.UInt64.of_int key) value in - Int32.equal rc 0l - - (** Free the cache. *) - let free (cache : t) : unit = - F.proven_lru_free cache -end - -(* ========================================================================== *) -(* SafeGraph *) -(* ========================================================================== *) - -module SafeGraph = struct - (** Opaque graph handle. *) - type t = F.graph structure ptr - - (** Create a graph with the given number of nodes. *) - let create (node_count : int) : t option = - F.proven_graph_create (Unsigned.Size_t.of_int node_count) - - (** Add a directed edge. Returns [true] on success. *) - let add_edge (g : t) ~from ~to_ : bool = - let rc = F.proven_graph_add_edge g - (Unsigned.Size_t.of_int from) - (Unsigned.Size_t.of_int to_) in - Int32.equal rc 0l - - (** Check whether a directed edge exists. *) - let has_edge (g : t) ~from ~to_ : bool = - F.proven_graph_has_edge g - (Unsigned.Size_t.of_int from) - (Unsigned.Size_t.of_int to_) - - (** Free the graph. *) - let free (g : t) : unit = - F.proven_graph_free g -end - -(* ========================================================================== *) -(* SafeQueue *) -(* ========================================================================== *) - -module SafeQueue = struct - (** Opaque bounded queue handle. *) - type t = F.bounded_queue structure ptr - - (** Create a bounded FIFO queue with the given capacity. *) - let create (capacity : int) : t option = - F.proven_queue_create (Unsigned.Size_t.of_int capacity) - - (** Push a value. Returns [true] on success, [false] if full. *) - let push (q : t) (value : int64) : bool = - F.proven_queue_push q value - - (** Pop a value. Returns [None] if empty. *) - let pop (q : t) : int64 option = - unwrap_int_result (F.proven_queue_pop q) - - (** Get current queue size. *) - let size (q : t) : int = - Unsigned.Size_t.to_int (F.proven_queue_size q) - - (** Free the queue. *) - let free (q : t) : unit = - F.proven_queue_free q -end - -(* ========================================================================== *) -(* SafeBloom *) -(* ========================================================================== *) - -module SafeBloom = struct - (** Opaque bloom filter handle. *) - type t = F.bloom_filter structure ptr - - (** Create a bloom filter. *) - let create ~expected_elements ~false_positive_rate : t option = - F.proven_bloom_create - (Unsigned.Size_t.of_int expected_elements) - false_positive_rate - - (** Add an element to the filter. *) - let add (bf : t) (s : string) : unit = - let p = cstring_of_string s in - F.proven_bloom_add bf p (Unsigned.Size_t.of_int (String.length s)) - - (** Check whether an element might be in the filter. *) - let contains (bf : t) (s : string) : bool = - let p = cstring_of_string s in - F.proven_bloom_contains bf p (Unsigned.Size_t.of_int (String.length s)) - - (** Free the bloom filter. *) - let free (bf : t) : unit = - F.proven_bloom_free bf -end - -(* ========================================================================== *) -(* Callbacks *) -(* ========================================================================== *) - -module Callbacks = struct - (** Event types that can trigger callbacks. *) - type event_type = - | Validation_failed - | Resource_acquired - | Resource_released - | Rate_limit_hit - | Circuit_state_change - | Signal_received - | Retry_attempt - | Crypto_operation - | Custom - - let event_type_to_int = function - | Validation_failed -> 1l - | Resource_acquired -> 2l - | Resource_released -> 3l - | Rate_limit_hit -> 4l - | Circuit_state_change -> 5l - | Signal_received -> 6l - | Retry_attempt -> 7l - | Crypto_operation -> 8l - | Custom -> 100l - - (** Query how many callbacks are registered for an event type. *) - let count (et : event_type) : int = - Unsigned.UInt32.to_int (F.proven_callback_count (event_type_to_int et)) - - (** Unregister all callbacks. Returns the number removed. *) - let clear_all () : int = - Unsigned.UInt32.to_int (F.proven_callback_clear_all ()) + let has_crlf (s : string) : bool = + match has_crlf_result s with Some v -> v | None -> true end diff --git a/bindings/ocaml/lib/proven_ffi.ml b/bindings/ocaml/lib/proven_ffi.ml index 1d859520..1f7df219 100644 --- a/bindings/ocaml/lib/proven_ffi.ml +++ b/bindings/ocaml/lib/proven_ffi.ml @@ -3,9 +3,12 @@ (** Low-level ctypes FFI bindings to libproven. - This module declares all C-ABI types and function bindings using - [Ctypes] and [Ctypes.Foreign]. Every function here is a direct - 1-to-1 mapping of a [proven_*] export from the shared library. + Binds only symbols whose row in the C ABI status ledger + ([bindings/c/include/proven-abi.tsv]) is [exported]: present in + libproven with the signature declared in [proven.h]. Every other + [proven.h] function is an [owed] or [conflict] row and is deliberately + not bound here; binding it eagerly would make this library fail to load. + [scripts/full-api-audit.sh] enforces this against the real library. Users should prefer the safe wrappers in {!Proven} instead. *) @@ -22,7 +25,7 @@ let lib = Dl.dlopen ~filename:"libproven.so" ~flags:[Dl.RTLD_NOW; Dl.RTLD_GLOBAL (* Enum: ProvenStatus *) (* -------------------------------------------------------------------------- *) -(** Status codes returned by most libproven functions. *) +(** Status codes returned by libproven functions. *) let proven_status_ok = 0l let proven_status_err_null_pointer = -1l let proven_status_err_invalid_arg = -2l @@ -35,16 +38,6 @@ let proven_status_err_out_of_bounds = -8l let proven_status_err_encoding = -9l let proven_status_err_alloc_failed = -10l -(* -------------------------------------------------------------------------- *) -(* Struct: IntResult { status: i32, value: i64 } *) -(* -------------------------------------------------------------------------- *) - -type int_result -let int_result : int_result structure typ = structure "IntResult" -let ir_status = field int_result "status" int32_t -let ir_value = field int_result "value" int64_t -let () = seal int_result - (* -------------------------------------------------------------------------- *) (* Struct: BoolResult { status: i32, value: bool } *) (* -------------------------------------------------------------------------- *) @@ -55,302 +48,10 @@ let br_status = field bool_result "status" int32_t let br_value = field bool_result "value" bool let () = seal bool_result -(* -------------------------------------------------------------------------- *) -(* Struct: StringResult { status: i32, value: *char, length: size_t } *) -(* -------------------------------------------------------------------------- *) - -type string_result -let string_result : string_result structure typ = structure "StringResult" -let sr_status = field string_result "status" int32_t -let sr_value = field string_result "value" (ptr char) -let sr_length = field string_result "length" size_t -let () = seal string_result - -(* -------------------------------------------------------------------------- *) -(* Struct: FloatResult { status: i32, value: f64 } *) -(* -------------------------------------------------------------------------- *) - -type float_result -let float_result : float_result structure typ = structure "FloatResult" -let fr_status = field float_result "status" int32_t -let fr_value = field float_result "value" double -let () = seal float_result - -(* -------------------------------------------------------------------------- *) -(* Struct: UUID { bytes: u8[16] } *) -(* -------------------------------------------------------------------------- *) - -type uuid -let uuid : uuid structure typ = structure "UUID" -let uuid_bytes = field uuid "bytes" (array 16 uint8_t) -let () = seal uuid - -(* -------------------------------------------------------------------------- *) -(* Struct: UUIDResult { status: i32, uuid: UUID } *) -(* -------------------------------------------------------------------------- *) - -type uuid_result -let uuid_result : uuid_result structure typ = structure "UUIDResult" -let ur_status = field uuid_result "status" int32_t -let ur_uuid = field uuid_result "uuid" uuid -let () = seal uuid_result - -(* -------------------------------------------------------------------------- *) -(* Struct: IPv4Address { octets: u8[4] } *) -(* -------------------------------------------------------------------------- *) - -type ipv4_address -let ipv4_address : ipv4_address structure typ = structure "IPv4Address" -let ipv4_octets = field ipv4_address "octets" (array 4 uint8_t) -let () = seal ipv4_address - -(* -------------------------------------------------------------------------- *) -(* Struct: IPv4Result { status: i32, address: IPv4Address } *) -(* -------------------------------------------------------------------------- *) - -type ipv4_result -let ipv4_result : ipv4_result structure typ = structure "IPv4Result" -let ipv4r_status = field ipv4_result "status" int32_t -let ipv4r_address = field ipv4_result "address" ipv4_address -let () = seal ipv4_result - -(* -------------------------------------------------------------------------- *) -(* Struct: CurrencyResult *) -(* -------------------------------------------------------------------------- *) - -type currency_result -let currency_result : currency_result structure typ = structure "CurrencyResult" -let cur_status = field currency_result "status" int32_t -let cur_amount_minor = field currency_result "amount_minor" int64_t -let cur_currency_code = field currency_result "currency_code" (array 3 uint8_t) -let cur_decimal_places = field currency_result "decimal_places" uint8_t -let () = seal currency_result - -(* -------------------------------------------------------------------------- *) -(* Struct: PhoneResult *) -(* -------------------------------------------------------------------------- *) - -type phone_result -let phone_result : phone_result structure typ = structure "PhoneResult" -let phr_status = field phone_result "status" int32_t -let phr_country_code = field phone_result "country_code" uint16_t -let phr_national_number = field phone_result "national_number" uint64_t -let phr_is_valid = field phone_result "is_valid" bool -let () = seal phone_result - -(* -------------------------------------------------------------------------- *) -(* Struct: DateTime *) -(* -------------------------------------------------------------------------- *) - -type datetime -let datetime : datetime structure typ = structure "DateTime" -let dt_year = field datetime "year" int32_t -let dt_month = field datetime "month" uint8_t -let dt_day = field datetime "day" uint8_t -let dt_hour = field datetime "hour" uint8_t -let dt_minute = field datetime "minute" uint8_t -let dt_second = field datetime "second" uint8_t -let dt_nanosecond = field datetime "nanosecond" uint32_t -let dt_tz_offset_minutes = field datetime "tz_offset_minutes" int16_t -let () = seal datetime - -(* -------------------------------------------------------------------------- *) -(* Struct: DateTimeResult *) -(* -------------------------------------------------------------------------- *) - -type datetime_result -let datetime_result : datetime_result structure typ = structure "DateTimeResult" -let dtr_status = field datetime_result "status" int32_t -let dtr_datetime = field datetime_result "datetime" datetime -let () = seal datetime_result - -(* -------------------------------------------------------------------------- *) -(* Struct: SemanticVersion *) -(* -------------------------------------------------------------------------- *) - -type semantic_version -let semantic_version : semantic_version structure typ = structure "SemanticVersion" -let sv_major = field semantic_version "major" uint32_t -let sv_minor = field semantic_version "minor" uint32_t -let sv_patch = field semantic_version "patch" uint32_t -let sv_prerelease_len = field semantic_version "prerelease_len" size_t -let sv_prerelease = field semantic_version "prerelease" (ptr char) -let () = seal semantic_version - -(* -------------------------------------------------------------------------- *) -(* Struct: VersionResult *) -(* -------------------------------------------------------------------------- *) - -type version_result -let version_result : version_result structure typ = structure "VersionResult" -let vr_status = field version_result "status" int32_t -let vr_version = field version_result "version" semantic_version -let () = seal version_result - -(* -------------------------------------------------------------------------- *) -(* Struct: GeoCoordinate *) -(* -------------------------------------------------------------------------- *) - -type geo_coordinate -let geo_coordinate : geo_coordinate structure typ = structure "GeoCoordinate" -let geo_latitude = field geo_coordinate "latitude" double -let geo_longitude = field geo_coordinate "longitude" double -let () = seal geo_coordinate - -(* -------------------------------------------------------------------------- *) -(* Struct: GeoResult *) -(* -------------------------------------------------------------------------- *) - -type geo_result -let geo_result : geo_result structure typ = structure "GeoResult" -let geor_status = field geo_result "status" int32_t -let geor_coordinate = field geo_result "coordinate" geo_coordinate -let () = seal geo_result - -(* -------------------------------------------------------------------------- *) -(* Struct: PasswordResult *) -(* -------------------------------------------------------------------------- *) - -type password_result -let password_result : password_result structure typ = structure "PasswordResult" -let pw_strength = field password_result "strength" int32_t -let pw_has_lowercase = field password_result "has_lowercase" bool -let pw_has_uppercase = field password_result "has_uppercase" bool -let pw_has_digit = field password_result "has_digit" bool -let pw_has_special = field password_result "has_special" bool -let pw_length = field password_result "length" size_t -let () = seal password_result - -(* -------------------------------------------------------------------------- *) -(* Struct: RGBColor *) -(* -------------------------------------------------------------------------- *) - -type rgb_color -let rgb_color : rgb_color structure typ = structure "RGBColor" -let rgb_r = field rgb_color "r" uint8_t -let rgb_g = field rgb_color "g" uint8_t -let rgb_b = field rgb_color "b" uint8_t -let () = seal rgb_color - -(* -------------------------------------------------------------------------- *) -(* Struct: HSLColor *) -(* -------------------------------------------------------------------------- *) - -type hsl_color -let hsl_color : hsl_color structure typ = structure "HSLColor" -let hsl_h = field hsl_color "h" double -let hsl_s = field hsl_color "s" double -let hsl_l = field hsl_color "l" double -let () = seal hsl_color - -(* -------------------------------------------------------------------------- *) -(* Struct: ColorParseResult *) -(* -------------------------------------------------------------------------- *) - -type color_parse_result -let color_parse_result : color_parse_result structure typ = structure "ColorParseResult" -let cpr_status = field color_parse_result "status" int32_t -let cpr_color = field color_parse_result "color" rgb_color -let () = seal color_parse_result - -(* -------------------------------------------------------------------------- *) -(* Struct: HexDecodeResult *) -(* -------------------------------------------------------------------------- *) - -type hex_decode_result -let hex_decode_result : hex_decode_result structure typ = structure "HexDecodeResult" -let hdr_status = field hex_decode_result "status" int32_t -let hdr_data = field hex_decode_result "data" (ptr uint8_t) -let hdr_length = field hex_decode_result "length" size_t -let () = seal hex_decode_result - -(* -------------------------------------------------------------------------- *) -(* Struct: RetryConfig *) -(* -------------------------------------------------------------------------- *) - -type retry_config -let retry_config : retry_config structure typ = structure "RetryConfig" -let rc_max_attempts = field retry_config "max_attempts" uint32_t -let rc_base_delay_ms = field retry_config "base_delay_ms" uint64_t -let rc_max_delay_ms = field retry_config "max_delay_ms" uint64_t -let rc_multiplier = field retry_config "multiplier" double -let () = seal retry_config - -(* -------------------------------------------------------------------------- *) -(* Opaque pointer types for stateful objects *) -(* -------------------------------------------------------------------------- *) - -(* Keep pointee types incomplete. Sealing an empty C struct is unsupported; - these declarations are used only behind pointers, never allocated here. *) - -(** Opaque handle for BoundedBuffer. *) -type bounded_buffer -let bounded_buffer : bounded_buffer structure typ = structure "BoundedBuffer" - -(** Opaque handle for RateLimiter. *) -type rate_limiter -let rate_limiter : rate_limiter structure typ = structure "RateLimiter" - -(** Opaque handle for CircuitBreaker. *) -type circuit_breaker -let circuit_breaker : circuit_breaker structure typ = structure "CircuitBreaker" - -(** Opaque handle for MonotonicCounter. *) -type monotonic_counter -let monotonic_counter : monotonic_counter structure typ = structure "MonotonicCounter" - -(** Opaque handle for StateMachine. *) -type state_machine -let state_machine : state_machine structure typ = structure "StateMachine" - -(** Opaque handle for Tensor2D. *) -type tensor_2d -let tensor_2d : tensor_2d structure typ = structure "Tensor2D" - -(** Opaque handle for LRUCache. *) -type lru_cache -let lru_cache : lru_cache structure typ = structure "LRUCache" - -(** Opaque handle for Graph. *) -type graph -let graph : graph structure typ = structure "Graph" - -(** Opaque handle for BoundedQueue. *) -type bounded_queue -let bounded_queue : bounded_queue structure typ = structure "BoundedQueue" - -(** Opaque handle for BloomFilter. *) -type bloom_filter -let bloom_filter : bloom_filter structure typ = structure "BloomFilter" - -(* -------------------------------------------------------------------------- *) -(* Struct: BufferResult { status: i32, buffer: *BoundedBuffer } *) -(* -------------------------------------------------------------------------- *) - -type buffer_result -let buffer_result : buffer_result structure typ = structure "BufferResult" -let bufr_status = field buffer_result "status" int32_t -let bufr_buffer = field buffer_result "buffer" (ptr bounded_buffer) -let () = seal buffer_result - (* ========================================================================== *) -(* Function bindings *) +(* Function bindings: ledger status `exported` *) (* ========================================================================== *) -(* -- Lifecycle ------------------------------------------------------------- *) - -let proven_init = - foreign ~from:lib "proven_init" - (void @-> returning int32_t) - -let proven_deinit = - foreign ~from:lib "proven_deinit" - (void @-> returning void) - -let proven_is_initialized = - foreign ~from:lib "proven_is_initialized" - (void @-> returning bool) - (* -- Memory management ----------------------------------------------------- *) let proven_free_string = @@ -375,649 +76,14 @@ let proven_ffi_abi_version = foreign ~from:lib "proven_ffi_abi_version" (void @-> returning uint32_t) -let proven_module_count = - foreign ~from:lib "proven_module_count" - (void @-> returning uint32_t) - -(* -- SafeMath -------------------------------------------------------------- *) - -let proven_math_div = - foreign ~from:lib "proven_math_div" - (int64_t @-> int64_t @-> returning int_result) - -let proven_math_mod = - foreign ~from:lib "proven_math_mod" - (int64_t @-> int64_t @-> returning int_result) - -let proven_math_add_checked = - foreign ~from:lib "proven_math_add_checked" - (int64_t @-> int64_t @-> returning int_result) - -let proven_math_sub_checked = - foreign ~from:lib "proven_math_sub_checked" - (int64_t @-> int64_t @-> returning int_result) - -let proven_math_mul_checked = - foreign ~from:lib "proven_math_mul_checked" - (int64_t @-> int64_t @-> returning int_result) - -let proven_math_abs_safe = - foreign ~from:lib "proven_math_abs_safe" - (int64_t @-> returning int_result) - -let proven_math_clamp = - foreign ~from:lib "proven_math_clamp" - (int64_t @-> int64_t @-> int64_t @-> returning int64_t) - -let proven_math_pow_checked = - foreign ~from:lib "proven_math_pow_checked" - (int64_t @-> uint32_t @-> returning int_result) - -(* -- SafeString ------------------------------------------------------------ *) - -let proven_string_is_valid_utf8 = - foreign ~from:lib "proven_string_is_valid_utf8" - (ptr char @-> size_t @-> returning bool_result) - -let proven_string_escape_sql = - foreign ~from:lib "proven_string_escape_sql" - (ptr char @-> size_t @-> returning string_result) - -let proven_string_escape_html = - foreign ~from:lib "proven_string_escape_html" - (ptr char @-> size_t @-> returning string_result) - -let proven_string_escape_js = - foreign ~from:lib "proven_string_escape_js" - (ptr char @-> size_t @-> returning string_result) - -(* -- SafePath -------------------------------------------------------------- *) +(* -- SafePath (zig-legacy backend) ----------------------------------------- *) let proven_path_has_traversal = foreign ~from:lib "proven_path_has_traversal" (ptr char @-> size_t @-> returning bool_result) -let proven_path_sanitize_filename = - foreign ~from:lib "proven_path_sanitize_filename" - (ptr char @-> size_t @-> returning string_result) - -(* -- SafeCrypto ------------------------------------------------------------ *) - -let proven_crypto_constant_time_eq = - foreign ~from:lib "proven_crypto_constant_time_eq" - (ptr char @-> size_t @-> ptr char @-> size_t @-> returning bool_result) - -let proven_crypto_random_bytes = - foreign ~from:lib "proven_crypto_random_bytes" - (ptr char @-> size_t @-> returning int32_t) - -(* -- SafeUrl --------------------------------------------------------------- *) - -(* Note: URL parsing uses a complex struct; we wrap only the simpler - helpers at this level. Full URL parsing is available through the - proven_url_parse / proven_url_free pair if needed. *) - -(* -- SafeEmail ------------------------------------------------------------- *) - -let proven_email_is_valid = - foreign ~from:lib "proven_email_is_valid" - (ptr char @-> size_t @-> returning bool_result) - -(* -- SafeNetwork ----------------------------------------------------------- *) - -let proven_network_parse_ipv4 = - foreign ~from:lib "proven_network_parse_ipv4" - (ptr char @-> size_t @-> returning ipv4_result) - -let proven_network_ipv4_is_private = - foreign ~from:lib "proven_network_ipv4_is_private" - (ipv4_address @-> returning bool) - -let proven_network_ipv4_is_loopback = - foreign ~from:lib "proven_network_ipv4_is_loopback" - (ipv4_address @-> returning bool) - -(* -- SafeHeader ------------------------------------------------------------ *) +(* -- SafeHeader (idris-refc backend) --------------------------------------- *) let proven_header_has_crlf = foreign ~from:lib "proven_header_has_crlf" (ptr char @-> size_t @-> returning bool_result) - -let proven_header_is_valid_name = - foreign ~from:lib "proven_header_is_valid_name" - (ptr char @-> size_t @-> returning bool_result) - -let proven_header_is_dangerous = - foreign ~from:lib "proven_header_is_dangerous" - (ptr char @-> size_t @-> returning bool_result) - -let proven_header_render = - foreign ~from:lib "proven_header_render" - (ptr char @-> size_t @-> ptr char @-> size_t @-> returning string_result) - -let proven_header_build_csp = - foreign ~from:lib "proven_header_build_csp" - (ptr char @-> size_t @-> returning string_result) - -let proven_header_build_hsts = - foreign ~from:lib "proven_header_build_hsts" - (int64_t @-> bool @-> bool @-> returning string_result) - -(* -- SafeCookie ------------------------------------------------------------ *) - -let proven_cookie_has_injection = - foreign ~from:lib "proven_cookie_has_injection" - (ptr char @-> size_t @-> returning bool_result) - -let proven_cookie_validate_name = - foreign ~from:lib "proven_cookie_validate_name" - (ptr char @-> size_t @-> returning bool_result) - -let proven_cookie_validate_value = - foreign ~from:lib "proven_cookie_validate_value" - (ptr char @-> size_t @-> returning bool_result) - -let proven_cookie_get_prefix = - foreign ~from:lib "proven_cookie_get_prefix" - (ptr char @-> size_t @-> returning int_result) - -let proven_cookie_build_delete = - foreign ~from:lib "proven_cookie_build_delete" - (ptr char @-> size_t @-> returning string_result) - -(* -- SafeContentType ------------------------------------------------------- *) - -let proven_content_type_can_sniff_dangerous = - foreign ~from:lib "proven_content_type_can_sniff_dangerous" - (ptr char @-> size_t @-> returning bool_result) - -let proven_content_type_is_json = - foreign ~from:lib "proven_content_type_is_json" - (ptr char @-> size_t @-> ptr char @-> size_t @-> returning bool_result) - -let proven_content_type_is_xml = - foreign ~from:lib "proven_content_type_is_xml" - (ptr char @-> size_t @-> ptr char @-> size_t @-> returning bool_result) - -(* -- SafeUUID -------------------------------------------------------------- *) - -let proven_uuid_v4 = - foreign ~from:lib "proven_uuid_v4" - (void @-> returning uuid_result) - -let proven_uuid_to_string = - foreign ~from:lib "proven_uuid_to_string" - (uuid @-> returning string_result) - -let proven_uuid_parse = - foreign ~from:lib "proven_uuid_parse" - (ptr char @-> size_t @-> returning uuid_result) - -let proven_uuid_is_nil = - foreign ~from:lib "proven_uuid_is_nil" - (uuid @-> returning bool) - -let proven_uuid_version = - foreign ~from:lib "proven_uuid_version" - (uuid @-> returning uint8_t) - -(* -- SafeJson -------------------------------------------------------------- *) - -let proven_json_is_valid = - foreign ~from:lib "proven_json_is_valid" - (ptr char @-> size_t @-> returning bool_result) - -let proven_json_get_type = - foreign ~from:lib "proven_json_get_type" - (ptr char @-> size_t @-> returning int32_t) - -(* -- SafeDateTime ---------------------------------------------------------- *) - -let proven_datetime_parse = - foreign ~from:lib "proven_datetime_parse" - (ptr char @-> size_t @-> returning datetime_result) - -let proven_datetime_format_iso8601 = - foreign ~from:lib "proven_datetime_format_iso8601" - (datetime @-> returning string_result) - -let proven_datetime_is_leap_year = - foreign ~from:lib "proven_datetime_is_leap_year" - (int32_t @-> returning bool) - -let proven_datetime_days_in_month = - foreign ~from:lib "proven_datetime_days_in_month" - (int32_t @-> uint8_t @-> returning uint8_t) - -(* -- SafeFloat ------------------------------------------------------------- *) - -let proven_float_div = - foreign ~from:lib "proven_float_div" - (double @-> double @-> returning float_result) - -let proven_float_is_finite = - foreign ~from:lib "proven_float_is_finite" - (double @-> returning bool) - -let proven_float_is_nan = - foreign ~from:lib "proven_float_is_nan" - (double @-> returning bool) - -let proven_float_sqrt = - foreign ~from:lib "proven_float_sqrt" - (double @-> returning float_result) - -let proven_float_ln = - foreign ~from:lib "proven_float_ln" - (double @-> returning float_result) - -(* -- SafePassword ---------------------------------------------------------- *) - -let proven_password_validate = - foreign ~from:lib "proven_password_validate" - (ptr char @-> size_t @-> returning password_result) - -let proven_password_is_common = - foreign ~from:lib "proven_password_is_common" - (ptr char @-> size_t @-> returning bool) - -(* -- SafeVersion ----------------------------------------------------------- *) - -let proven_version_parse_semver = - foreign ~from:lib "proven_version_parse" - (ptr char @-> size_t @-> returning version_result) - -let proven_version_compare = - foreign ~from:lib "proven_version_compare" - (semantic_version @-> semantic_version @-> returning int32_t) - -let proven_version_free = - foreign ~from:lib "proven_version_free" - (ptr semantic_version @-> returning void) - -(* -- SafeGeo --------------------------------------------------------------- *) - -let proven_geo_validate = - foreign ~from:lib "proven_geo_validate" - (double @-> double @-> returning geo_result) - -let proven_geo_distance = - foreign ~from:lib "proven_geo_distance" - (geo_coordinate @-> geo_coordinate @-> returning float_result) - -let proven_geo_in_bounds = - foreign ~from:lib "proven_geo_in_bounds" - (geo_coordinate @-> double @-> double @-> double @-> double @-> returning bool) - -(* -- SafeChecksum ---------------------------------------------------------- *) - -let proven_checksum_crc32 = - foreign ~from:lib "proven_checksum_crc32" - (ptr char @-> size_t @-> returning int_result) - -let proven_checksum_verify_crc32 = - foreign ~from:lib "proven_checksum_verify_crc32" - (ptr char @-> size_t @-> uint32_t @-> returning bool_result) - -(* -- SafeProbability ------------------------------------------------------- *) - -let proven_probability_create = - foreign ~from:lib "proven_probability_create" - (double @-> returning double) - -let proven_probability_and = - foreign ~from:lib "proven_probability_and" - (double @-> double @-> returning double) - -let proven_probability_or_exclusive = - foreign ~from:lib "proven_probability_or_exclusive" - (double @-> double @-> returning double) - -let proven_probability_not = - foreign ~from:lib "proven_probability_not" - (double @-> returning double) - -(* -- SafeCalculator -------------------------------------------------------- *) - -let proven_calculator_eval = - foreign ~from:lib "proven_calculator_eval" - (ptr char @-> size_t @-> returning float_result) - -(* -- SafeHex --------------------------------------------------------------- *) - -let proven_hex_encode = - foreign ~from:lib "proven_hex_encode" - (ptr char @-> size_t @-> bool @-> returning string_result) - -let proven_hex_decode = - foreign ~from:lib "proven_hex_decode" - (ptr char @-> size_t @-> returning hex_decode_result) - -let proven_hex_free = - foreign ~from:lib "proven_hex_free" - (ptr hex_decode_result @-> returning void) - -(* -- SafeCurrency ---------------------------------------------------------- *) - -let proven_currency_parse = - foreign ~from:lib "proven_currency_parse" - (ptr char @-> size_t @-> returning currency_result) - -let proven_currency_format = - foreign ~from:lib "proven_currency_format" - (int64_t @-> array 3 uint8_t @-> uint8_t @-> returning string_result) - -(* -- SafePhone ------------------------------------------------------------- *) - -let proven_phone_parse = - foreign ~from:lib "proven_phone_parse" - (ptr char @-> size_t @-> returning phone_result) - -let proven_phone_format_e164 = - foreign ~from:lib "proven_phone_format_e164" - (uint16_t @-> uint64_t @-> returning string_result) - -(* -- SafeColor ------------------------------------------------------------- *) - -let proven_color_parse_hex = - foreign ~from:lib "proven_color_parse_hex" - (ptr char @-> size_t @-> returning color_parse_result) - -let proven_color_rgb_to_hsl = - foreign ~from:lib "proven_color_rgb_to_hsl" - (rgb_color @-> returning hsl_color) - -let proven_color_to_hex = - foreign ~from:lib "proven_color_to_hex" - (rgb_color @-> returning string_result) - -(* -- SafeAngle ------------------------------------------------------------- *) - -let proven_angle_deg_to_rad = - foreign ~from:lib "proven_angle_deg_to_rad" - (double @-> returning double) - -let proven_angle_rad_to_deg = - foreign ~from:lib "proven_angle_rad_to_deg" - (double @-> returning double) - -let proven_angle_normalize_degrees = - foreign ~from:lib "proven_angle_normalize_degrees" - (double @-> returning double) - -let proven_angle_normalize_radians = - foreign ~from:lib "proven_angle_normalize_radians" - (double @-> returning double) - -(* -- SafeUnit -------------------------------------------------------------- *) - -let proven_unit_convert_length = - foreign ~from:lib "proven_unit_convert_length" - (double @-> int32_t @-> int32_t @-> returning float_result) - -let proven_unit_convert_temp = - foreign ~from:lib "proven_unit_convert_temp" - (double @-> int32_t @-> int32_t @-> returning float_result) - -(* -- SafeHttp -------------------------------------------------------------- *) - -let proven_http_url_encode = - foreign ~from:lib "proven_http_url_encode" - (ptr char @-> size_t @-> returning string_result) - -let proven_http_url_decode = - foreign ~from:lib "proven_http_url_decode" - (ptr char @-> size_t @-> returning string_result) - -(* -- SafeBuffer ------------------------------------------------------------ *) - -let proven_buffer_create = - foreign ~from:lib "proven_buffer_create" - (size_t @-> returning buffer_result) - -let proven_buffer_append = - foreign ~from:lib "proven_buffer_append" - (ptr bounded_buffer @-> ptr char @-> size_t @-> returning int32_t) - -let proven_buffer_get = - foreign ~from:lib "proven_buffer_get" - (ptr bounded_buffer @-> ptr (ptr char) @-> ptr size_t @-> returning int32_t) - -let proven_buffer_free = - foreign ~from:lib "proven_buffer_free" - (ptr bounded_buffer @-> returning void) - -(* -- SafeRateLimiter ------------------------------------------------------- *) - -let proven_rate_limiter_create = - foreign ~from:lib "proven_rate_limiter_create" - (double @-> double @-> returning (ptr_opt rate_limiter)) - -let proven_rate_limiter_try_acquire = - foreign ~from:lib "proven_rate_limiter_try_acquire" - (ptr rate_limiter @-> double @-> returning bool) - -let proven_rate_limiter_free = - foreign ~from:lib "proven_rate_limiter_free" - (ptr rate_limiter @-> returning void) - -(* -- SafeCircuitBreaker ---------------------------------------------------- *) - -let proven_circuit_breaker_create = - foreign ~from:lib "proven_circuit_breaker_create" - (uint32_t @-> uint32_t @-> int64_t @-> returning (ptr_opt circuit_breaker)) - -let proven_circuit_breaker_allow = - foreign ~from:lib "proven_circuit_breaker_allow" - (ptr circuit_breaker @-> returning bool) - -let proven_circuit_breaker_success = - foreign ~from:lib "proven_circuit_breaker_success" - (ptr circuit_breaker @-> returning void) - -let proven_circuit_breaker_failure = - foreign ~from:lib "proven_circuit_breaker_failure" - (ptr circuit_breaker @-> returning void) - -let proven_circuit_breaker_state = - foreign ~from:lib "proven_circuit_breaker_state" - (ptr circuit_breaker @-> returning int32_t) - -let proven_circuit_breaker_free = - foreign ~from:lib "proven_circuit_breaker_free" - (ptr circuit_breaker @-> returning void) - -(* -- SafeRetry ------------------------------------------------------------- *) - -let proven_retry_delay = - foreign ~from:lib "proven_retry_delay" - (retry_config @-> uint32_t @-> returning uint64_t) - -let proven_retry_should_retry = - foreign ~from:lib "proven_retry_should_retry" - (retry_config @-> uint32_t @-> returning bool) - -(* -- SafeMonotonic --------------------------------------------------------- *) - -let proven_monotonic_create = - foreign ~from:lib "proven_monotonic_create" - (uint64_t @-> uint64_t @-> returning (ptr_opt monotonic_counter)) - -let proven_monotonic_next = - foreign ~from:lib "proven_monotonic_next" - (ptr monotonic_counter @-> returning int_result) - -let proven_monotonic_free = - foreign ~from:lib "proven_monotonic_free" - (ptr monotonic_counter @-> returning void) - -(* -- SafeStateMachine ------------------------------------------------------ *) - -let proven_state_machine_create = - foreign ~from:lib "proven_state_machine_create" - (uint32_t @-> uint32_t @-> returning (ptr_opt state_machine)) - -let proven_state_machine_allow = - foreign ~from:lib "proven_state_machine_allow" - (ptr state_machine @-> uint32_t @-> uint32_t @-> returning bool) - -let proven_state_machine_transition = - foreign ~from:lib "proven_state_machine_transition" - (ptr state_machine @-> uint32_t @-> returning bool) - -let proven_state_machine_state = - foreign ~from:lib "proven_state_machine_state" - (ptr state_machine @-> returning uint32_t) - -let proven_state_machine_free = - foreign ~from:lib "proven_state_machine_free" - (ptr state_machine @-> returning void) - -(* -- SafeTensor ------------------------------------------------------------ *) - -let proven_tensor_create = - foreign ~from:lib "proven_tensor_create" - (size_t @-> size_t @-> returning (ptr_opt tensor_2d)) - -let proven_tensor_set = - foreign ~from:lib "proven_tensor_set" - (ptr tensor_2d @-> size_t @-> size_t @-> double @-> returning int32_t) - -let proven_tensor_get = - foreign ~from:lib "proven_tensor_get" - (ptr tensor_2d @-> size_t @-> size_t @-> returning float_result) - -let proven_tensor_matmul = - foreign ~from:lib "proven_tensor_matmul" - (ptr tensor_2d @-> ptr tensor_2d @-> returning (ptr_opt tensor_2d)) - -let proven_tensor_free = - foreign ~from:lib "proven_tensor_free" - (ptr tensor_2d @-> returning void) - -(* -- SafeML ---------------------------------------------------------------- *) - -let proven_ml_sigmoid = - foreign ~from:lib "proven_ml_sigmoid" - (double @-> returning double) - -let proven_ml_relu = - foreign ~from:lib "proven_ml_relu" - (double @-> returning double) - -let proven_ml_leaky_relu = - foreign ~from:lib "proven_ml_leaky_relu" - (double @-> double @-> returning double) - -let proven_ml_clamp = - foreign ~from:lib "proven_ml_clamp" - (double @-> double @-> double @-> returning double) - -(* -- SafeLRU --------------------------------------------------------------- *) - -let proven_lru_create = - foreign ~from:lib "proven_lru_create" - (size_t @-> returning (ptr_opt lru_cache)) - -let proven_lru_get = - foreign ~from:lib "proven_lru_get" - (ptr lru_cache @-> uint64_t @-> returning int_result) - -let proven_lru_put = - foreign ~from:lib "proven_lru_put" - (ptr lru_cache @-> uint64_t @-> int64_t @-> returning int32_t) - -let proven_lru_free = - foreign ~from:lib "proven_lru_free" - (ptr lru_cache @-> returning void) - -(* -- SafeGraph ------------------------------------------------------------- *) - -let proven_graph_create = - foreign ~from:lib "proven_graph_create" - (size_t @-> returning (ptr_opt graph)) - -let proven_graph_add_edge = - foreign ~from:lib "proven_graph_add_edge" - (ptr graph @-> size_t @-> size_t @-> returning int32_t) - -let proven_graph_has_edge = - foreign ~from:lib "proven_graph_has_edge" - (ptr graph @-> size_t @-> size_t @-> returning bool) - -let proven_graph_free = - foreign ~from:lib "proven_graph_free" - (ptr graph @-> returning void) - -(* -- SafeQueue ------------------------------------------------------------- *) - -let proven_queue_create = - foreign ~from:lib "proven_queue_create" - (size_t @-> returning (ptr_opt bounded_queue)) - -let proven_queue_push = - foreign ~from:lib "proven_queue_push" - (ptr bounded_queue @-> int64_t @-> returning bool) - -let proven_queue_pop = - foreign ~from:lib "proven_queue_pop" - (ptr bounded_queue @-> returning int_result) - -let proven_queue_size = - foreign ~from:lib "proven_queue_size" - (ptr bounded_queue @-> returning size_t) - -let proven_queue_free = - foreign ~from:lib "proven_queue_free" - (ptr bounded_queue @-> returning void) - -(* -- SafeBloom ------------------------------------------------------------- *) - -let proven_bloom_create = - foreign ~from:lib "proven_bloom_create" - (size_t @-> double @-> returning (ptr_opt bloom_filter)) - -let proven_bloom_add = - foreign ~from:lib "proven_bloom_add" - (ptr bloom_filter @-> ptr char @-> size_t @-> returning void) - -let proven_bloom_contains = - foreign ~from:lib "proven_bloom_contains" - (ptr bloom_filter @-> ptr char @-> size_t @-> returning bool) - -let proven_bloom_free = - foreign ~from:lib "proven_bloom_free" - (ptr bloom_filter @-> returning void) - -(* -- Callbacks ------------------------------------------------------------- *) - -let proven_callback_register = - foreign ~from:lib "proven_callback_register" - (int32_t @-> ptr void @-> ptr_opt void @-> returning uint32_t) - -let proven_callback_unregister = - foreign ~from:lib "proven_callback_unregister" - (uint32_t @-> returning int32_t) - -let proven_callback_fire = - foreign ~from:lib "proven_callback_fire" - (int32_t @-> ptr_opt char @-> size_t @-> int32_t @-> returning int32_t) - -let proven_callback_count = - foreign ~from:lib "proven_callback_count" - (int32_t @-> returning uint32_t) - -let proven_callback_clear_all = - foreign ~from:lib "proven_callback_clear_all" - (void @-> returning uint32_t) - -(* -- SafeRegistry ---------------------------------------------------------- *) - -(* Note: Registry and Digest use complex structs with internal pointers. - We provide the simpler interfaces here; full parsing can be added - by extending the struct definitions above. *) - -(* -- SafeDigest ------------------------------------------------------------ *) - -(* Digest structs with internal pointers require careful lifetime management. - Basic operations are exposed via the string-based API through SafeHex. *) diff --git a/bindings/ocaml/scripts/full-api-audit.sh b/bindings/ocaml/scripts/full-api-audit.sh index 6efc7dc1..11241759 100644 --- a/bindings/ocaml/scripts/full-api-audit.sh +++ b/bindings/ocaml/scripts/full-api-audit.sh @@ -1,28 +1,47 @@ #!/usr/bin/env bash # SPDX-License-Identifier: MPL-2.0 # Copyright (c) 2026 Jonathan D.A. Jewell +# +# Full-API audit for the eager OCaml binding against the C ABI status ledger +# (proven-abi.tsv beside proven.h) and a real libproven.so. +# +# Fails when the binding binds a symbol that is not a ledger `exported` row, +# a bound symbol is absent from the library, a ledger `exported` row is +# absent from the library, or a ledger `owed` row is present in it (the +# ledger is stale and must be reclassified together with the code). +# Presence alone does not prove a C signature, ownership contract or Idris +# implementation; full_api_probe stays as the load test. set -euo pipefail unset CDPATH root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) : "${PROVEN_LIB_PATH:?directory containing the actual libproven.so required}" +include=${PROVEN_INCLUDE_PATH:-"$root/../c/include"} library="$PROVEN_LIB_PATH/libproven.so" +ledger="$include/proven-abi.tsv" test -s "$library" +test -s "$ledger" out="$root/_build/full-api-audit" mkdir -p "$out" echo INCOMPLETE > "$out/result.txt" trap 'status=$?; if ((status)); then echo FAIL > "$out/result.txt"; fi' EXIT -# This inventory is distinct from the five-symbol qualified wired manifest. -# Every eagerly bound symbol must exist. Presence alone does not prove a C -# signature, ownership contract, or Idris implementation; full_api_probe stays. sed -nE 's/.*foreign ~from:lib "([^"]+)".*/\1/p' "$root/lib/proven_ffi.ml" | sort -u > "$out/required.txt" test -s "$out/required.txt" +grep -vE '^(#|[[:space:]]*$)' "$ledger" | awk -F'\t' '$2 == "exported" { print $1 }' | sort -u > "$out/ledger-exported.txt" +grep -vE '^(#|[[:space:]]*$)' "$ledger" | awk -F'\t' '$2 == "owed" { print $1 }' | sort -u > "$out/ledger-owed.txt" +test -s "$out/ledger-exported.txt" nm -D --defined-only "$library" | awk '$2 ~ /^[TW]$/ {print $3}' | sort -u > "$out/exported.txt" +comm -23 "$out/required.txt" "$out/ledger-exported.txt" > "$out/bound-not-ledger-exported.txt" comm -23 "$out/required.txt" "$out/exported.txt" > "$out/missing.txt" +comm -23 "$out/ledger-exported.txt" "$out/exported.txt" > "$out/ledger-exported-missing.txt" +comm -12 "$out/ledger-owed.txt" "$out/exported.txt" > "$out/owed-present.txt" comm -12 "$out/required.txt" "$out/exported.txt" > "$out/present.txt" -sha256sum "$root/lib/proven_ffi.ml" "$library" > "$out/identity.sha256" -printf 'Required=%s Present=%s Missing=%s\n' "$(wc -l < "$out/required.txt")" \ - "$(wc -l < "$out/present.txt")" "$(wc -l < "$out/missing.txt")" | tee "$out/summary.txt" -if [[ -s "$out/missing.txt" ]]; then - cat "$out/missing.txt"; exit 1 -fi +sha256sum "$root/lib/proven_ffi.ml" "$ledger" "$library" > "$out/identity.sha256" +printf 'Required=%s Present=%s Missing=%s LedgerExported=%s LedgerOwed=%s\n' \ + "$(wc -l < "$out/required.txt")" "$(wc -l < "$out/present.txt")" "$(wc -l < "$out/missing.txt")" \ + "$(wc -l < "$out/ledger-exported.txt")" "$(wc -l < "$out/ledger-owed.txt")" | tee "$out/summary.txt" +fail=0 +for f in bound-not-ledger-exported missing ledger-exported-missing owed-present; do + if [[ -s "$out/$f.txt" ]]; then echo "FAIL: $f"; sed 's/^/ /' "$out/$f.txt"; fail=1; fi +done +((fail == 0)) echo PASS > "$out/result.txt" diff --git a/bindings/ocaml/test/test_proven.ml b/bindings/ocaml/test/test_proven.ml index 9c5c6984..36cff516 100644 --- a/bindings/ocaml/test/test_proven.ml +++ b/bindings/ocaml/test/test_proven.ml @@ -4,75 +4,9 @@ (** Tests for the Proven OCaml FFI bindings. These tests call through to libproven via ctypes. They require - [libproven.so] to be loadable at runtime (e.g., via LD_LIBRARY_PATH). *) - -(* -------------------------------------------------------------------------- *) -(* Test helpers *) -(* -------------------------------------------------------------------------- *) - -let option_int = Alcotest.(option int) -let option_string = Alcotest.(option string) -let option_float = Alcotest.(option (float 1e-6)) - -(* -------------------------------------------------------------------------- *) -(* Lifecycle *) -(* -------------------------------------------------------------------------- *) - -let test_init () = - let ok = Proven.Lifecycle.init () in - Alcotest.(check bool) "init succeeds" true ok; - Alcotest.(check bool) "is_initialized" true (Proven.Lifecycle.is_initialized ()) - -(* -------------------------------------------------------------------------- *) -(* SafeMath *) -(* -------------------------------------------------------------------------- *) - -let test_math_div () = - Alcotest.(check option_int) "10/2=5" (Some 5) (Proven.SafeMath.div 10 2); - Alcotest.(check option_int) "10/0=None" None (Proven.SafeMath.div 10 0) - -let test_math_mod () = - Alcotest.(check option_int) "10%3=1" (Some 1) (Proven.SafeMath.modulo 10 3); - Alcotest.(check option_int) "10%0=None" None (Proven.SafeMath.modulo 10 0) - -let test_math_add () = - Alcotest.(check option_int) "1+2=3" (Some 3) (Proven.SafeMath.add 1 2) - -let test_math_sub () = - Alcotest.(check option_int) "5-3=2" (Some 2) (Proven.SafeMath.sub 5 3) - -let test_math_mul () = - Alcotest.(check option_int) "3*4=12" (Some 12) (Proven.SafeMath.mul 3 4) - -let test_math_abs () = - Alcotest.(check option_int) "abs(-7)=7" (Some 7) (Proven.SafeMath.abs (-7)); - Alcotest.(check option_int) "abs(0)=0" (Some 0) (Proven.SafeMath.abs 0) - -let test_math_clamp () = - Alcotest.(check int) "clamp 5 [0,10]" 5 (Proven.SafeMath.clamp ~lo:0 ~hi:10 5); - Alcotest.(check int) "clamp -1 [0,10]" 0 (Proven.SafeMath.clamp ~lo:0 ~hi:10 (-1)); - Alcotest.(check int) "clamp 20 [0,10]" 10 (Proven.SafeMath.clamp ~lo:0 ~hi:10 20) - -let test_math_pow () = - Alcotest.(check option_int) "2^10=1024" (Some 1024) (Proven.SafeMath.pow 2 10) - -(* -------------------------------------------------------------------------- *) -(* SafeString *) -(* -------------------------------------------------------------------------- *) - -let test_string_escape_html () = - Alcotest.(check option_string) "escape <" - (Some "<script>") - (Proven.SafeString.escape_html "") = False - -||| OWED: Event handlers are removed -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_eventHandlersRemoved_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_eventHandlersRemoved : containsEventHandler (sanitizeHtml "
text
") = False - -||| OWED: Escaped output doesn't contain raw HTML -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_escapeNoRawHtml_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_escapeNoRawHtml : (s : String) -> containsRawHtml (escapeHtml s) = False - -||| OWED: Escape is idempotent -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_escapeIdempotent_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_escapeIdempotent : (s : String) -> escapeHtml (escapeHtml s) = escapeHtml s - -||| Property: Less-than is escaped -prop_ltEscaped : escapeHtml "<" = "<" +-- Real API: `escapeHtmlContent : String -> String`, the total sanitisers +-- `sanitizeStandard`/`sanitizeStrict : String -> String` (documented as a +-- simplified implementation: strip every tag, then escape), `sanitizeUrl`, +-- `sanitizeAttrValue`, and the policy predicates `isTagAllowed`, +-- `isAttrAllowed`, `isDangerousAttr`, `isBlacklistedTag`. There is no +-- `sanitizeHtml` returning a Result. `Proven.SafeString.Escape` is imported +-- so that `escapeHtmlContent = escapeHTML` unfolds at the type level. +-- +-- The original `prop_escapeIdempotent` (escape (escape s) = escape s) is +-- false for any HTML escaper: escape "&" = "&" but escape "&" = +-- "&amp;". It is replaced by an execution check of that counterexample. + +-------------------------------------------------------------------------------- +-- Type-level proofs +-------------------------------------------------------------------------------- + +||| Less-than is escaped. +prop_ltEscaped : escapeHtmlContent "<" = "<" prop_ltEscaped = Refl -||| Property: Greater-than is escaped -prop_gtEscaped : escapeHtml ">" = ">" +||| Greater-than is escaped. +prop_gtEscaped : escapeHtmlContent ">" = ">" prop_gtEscaped = Refl -||| Property: Ampersand is escaped -prop_ampEscaped : escapeHtml "&" = "&" +||| Ampersand is escaped. +prop_ampEscaped : escapeHtmlContent "&" = "&" prop_ampEscaped = Refl -||| Property: Quote is escaped -prop_quoteEscaped : escapeHtml "\"" = """ +||| Double quote is escaped. +prop_quoteEscaped : escapeHtmlContent "\"" = """ prop_quoteEscaped = Refl -||| Property: Safe tags are preserved -prop_safeTagsPreserved : isOk (sanitizeHtml "

paragraph

") = True -prop_safeTagsPreserved = Refl +||| A whole script tag is neutralised by escaping. +prop_scriptEscaped : escapeHtmlContent "" + , "
text
" + , "
link" + , "" + , "<" == "alert('xss')") + check "event-handler attribute removed with its tag" + (sanitizeStandard "
text
" == "text") + check "javascript: link removed, link text kept" + (sanitizeStandard "link" == "link") + check "sanitised attack corpus has no raw < > \" and no dangerous tag" + (all (\s => noRaw (sanitizeStandard s) && + not (isInfixOf " \" (corpus)" + (all (noRaw . escapeHtmlContent) attackCorpus) + check "escaping is not idempotent (double escape of < is &lt;)" + (escapeHtmlContent (escapeHtmlContent "<") == "&lt;") + check "safe paragraph text preserved" (sanitizeStandard "

paragraph

" == "paragraph") + check "safe href attribute value preserved, event handler refused" + (sanitizeAttrValue "a" "href" "https://example.com" == Just "https://example.com" && + sanitizeAttrValue "div" "onclick" "evil()" == Nothing && + sanitizeAttrValue "a" "href" "javascript:alert(1)" == Nothing) putStrLn "" diff --git a/tests/properties/SafeJWTProps.idr b/tests/properties/SafeJWTProps.idr index 0ea0d6cc..244e6ff7 100644 --- a/tests/properties/SafeJWTProps.idr +++ b/tests/properties/SafeJWTProps.idr @@ -4,76 +4,85 @@ module SafeJWTProps import Proven.Core import Proven.SafeJWT +import System %default total -||| OWED: Valid JWT structure parses -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_validJWTParses_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_validJWTParses : isOk (parseJWT "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig") = True +-- The real API: token splitting (`splitToken`, total), creation +-- (`createToken`/`createHS256Token`, total; signing is a stub), and the +-- validation pipeline (`validateAlgorithm`, `validateExp`, `validateNbf`, +-- `validateDecoded`, `validate`, all total over a `DecodedJWT`). The +-- string-level entry points `decode`, `decodeClaims`, `verifyJWT`, +-- `validateToken` are `covering`, so this total module cannot call them; the +-- properties are therefore stated on the total layers they delegate to. +-- There is no `parseJWT`, `createJWT`, `validateJWT` or `getClaims`. -||| OWED: Invalid JWT structure fails -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_invalidJWTFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_invalidJWTFails : isErr (parseJWT "not.a.jwt.token") = True +||| Property: the 'none' algorithm is rejected by the default validation +||| pipeline, for every current time, header fields, claims and signature, +||| and hence by `validate` for every key. +prop_noneAlgRejected : (t : Integer) -> (key : SigningKey) -> + (raw : String) -> (typ, cty, kid, x5u, x5t, x5tS : Maybe String) -> + (claims : JWTClaims) -> (sig : List Bits8) -> (hs, ps : String) -> + validate Types.defaultValidation key t + (MkDecodedJWT raw (MkJWTHeader None typ cty kid x5u x5t x5tS) + claims sig hs ps) + = Err AlgorithmNoneNotAllowed +prop_noneAlgRejected t key raw typ cty kid x5u x5t x5tS claims sig hs ps = Refl -||| Property: JWT with missing parts fails -prop_missingPartsFails : isErr (parseJWT "header.payload") = True -prop_missingPartsFails = Refl +||| Property: whenever `isExpired` holds, `validateExp` fails, for every time, +||| clock skew and claim set. +prop_expiredRejected : (t : Integer) -> (skew : ClockSkew) -> (c : JWTClaims) -> + isExpired t skew c = True -> isOk (validateExp t skew c) = False +prop_expiredRejected t skew (MkJWTClaims _ _ _ Nothing _ _ _ _) prf = absurd prf +prop_expiredRejected t skew (MkJWTClaims _ _ _ (Just e) _ _ _ _) prf = rewrite prf in Refl -||| Property: None algorithm rejected -prop_noneAlgRejected : isErr (verifyJWT "eyJhbGciOiJub25lIn0.eyJzdWIiOiIxIn0." "secret") = True -prop_noneAlgRejected = Refl +||| Property: whenever `isNotYetValid` holds, `validateNbf` fails, for every +||| time, clock skew and claim set. +prop_nbfFutureRejected : (t : Integer) -> (skew : ClockSkew) -> (c : JWTClaims) -> + isNotYetValid t skew c = True -> isOk (validateNbf t skew c) = False +prop_nbfFutureRejected t skew (MkJWTClaims _ _ _ _ Nothing _ _ _) prf = absurd prf +prop_nbfFutureRejected t skew (MkJWTClaims _ _ _ _ (Just n) _ _ _) prf = rewrite prf in Refl -||| OWED: Expired JWT rejected -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_expiredRejected_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_expiredRejected : (jwt : JWT) -> isExpired jwt = True -> - isErr (validateJWT jwt) = True +||| Property: HS256 token creation succeeds for every secret and claim set +prop_hs256Supported : (secret : List Bits8) -> (claims : JWTClaims) -> + isOk (createToken (minimalHeader HS256) claims (SecretKey secret)) = True +prop_hs256Supported secret claims = Refl -||| OWED: Not-before future JWT rejected -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_nbfFutureRejected_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_nbfFutureRejected : (jwt : JWT) -> nbfInFuture jwt = True -> - isErr (validateJWT jwt) = True +||| Property: HS384 token creation succeeds for every secret and claim set +prop_hs384Supported : (secret : List Bits8) -> (claims : JWTClaims) -> + isOk (createToken (minimalHeader HS384) claims (SecretKey secret)) = True +prop_hs384Supported secret claims = Refl -||| Property: Signature verification deterministic -prop_verifyDeterministic : (token, secret : String) -> - verifyJWT token secret = verifyJWT token secret -prop_verifyDeterministic token secret = Refl +||| Property: HS512 token creation succeeds for every secret and claim set +prop_hs512Supported : (secret : List Bits8) -> (claims : JWTClaims) -> + isOk (createToken (minimalHeader HS512) claims (SecretKey secret)) = True +prop_hs512Supported secret claims = Refl -||| Property: HS256 algorithm supported -prop_hs256Supported : isOk (createJWT HS256 "secret" [("sub", "user")]) = True -prop_hs256Supported = Refl +-- The orphan's `prop_verifyDeterministic` (`verifyJWT t s = verifyJWT t s`) +-- holds by Refl for any function and says nothing; it is replaced by +-- `prop_noneAlgRejected` above, a real statement about the pipeline. -||| Property: HS384 algorithm supported -prop_hs384Supported : isOk (createJWT HS384 "secret" [("sub", "user")]) = True -prop_hs384Supported = Refl +||| OWED: claims survive a create/decode round trip, for `decode = +||| decodeClaims`. Blocked by: `decodeClaims` (and the JSON claim parser it +||| uses) is `covering`, so this total module can neither run nor mention it, +||| hence the family over the decoder; encoding and base64 also run over +||| `unpack`. This names the proposition; it is not a witness. +public export +ClaimsRoundTripObligation : (String -> Result JWTError JWTClaims) -> Type +ClaimsRoundTripObligation decode = + (secret : String) -> (claims : JWTClaims) -> (tok : String) -> + createHS256Token secret claims = Ok tok -> decode tok = Ok claims -||| Property: HS512 algorithm supported -prop_hs512Supported : isOk (createJWT HS512 "secret" [("sub", "user")]) = True -prop_hs512Supported = Refl +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| OWED: Claims are preserved in roundtrip -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_claimsPreserved_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_claimsPreserved : (claims : List (String, String)) -> (secret : String) -> - getClaims (createJWT HS256 secret claims) = claims +private +subUser : JWTClaims +subUser = { sub := Just "user" } emptyClaims ||| Test runner for JWT properties export @@ -81,10 +90,37 @@ runJWTProperties : IO () runJWTProperties = do putStrLn "SafeJWT Property Tests" putStrLn "======================" - putStrLn "prop_missingPartsFails: PASS (proven by type)" - putStrLn "prop_noneAlgRejected: PASS (proven by type)" - putStrLn "prop_verifyDeterministic: PASS (proven by type)" - putStrLn "prop_hs256Supported: PASS (proven by type)" - putStrLn "prop_hs384Supported: PASS (proven by type)" - putStrLn "prop_hs512Supported: PASS (proven by type)" + putStrLn "prop_noneAlgRejected: PASS (proven by type, all tokens/keys/times)" + putStrLn "prop_expiredRejected: PASS (proven by type, all claims/times)" + putStrLn "prop_nbfFutureRejected: PASS (proven by type, all claims/times)" + putStrLn "prop_hs256Supported: PASS (proven by type, all secrets/claims)" + putStrLn "prop_hs384Supported: PASS (proven by type, all secrets/claims)" + putStrLn "prop_hs512Supported: PASS (proven by type, all secrets/claims)" + check "valid three-part JWT structure splits" + (case splitToken "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.sig" of + Ok segs => segs.header == "eyJhbGciOiJIUzI1NiJ9" && + segs.payload == "eyJzdWIiOiIxIn0" && segs.signature == "sig" + Err _ => False) + check "four-part string rejected" + (isErr (splitToken "not.a.jwt.token")) + check "unsecured JWT (empty signature segment) splits" + (isOk (splitToken "eyJhbGciOiJub25lIn0.eyJzdWIiOiIxIn0.")) + check "created HS256 token has three segments and the encoded claims" + (case createHS256Token "secret" subUser of + Ok tok => case splitToken tok of + Ok segs => case base64UrlDecodeString segs.payload of + Ok json => json == "{\"sub\":\"user\"}" + Err _ => False + Err _ => False + Err _ => False) + check "concrete expiry: no exp accepted, exp 1000 rejected at t=2000 (default skew)" + (isOk (validateExp 2000 defaultClockSkew subUser) && + isErr (validateExp 2000 defaultClockSkew ({ exp := Just 1000 } subUser))) + check "concrete nbf: nbf 5000 rejected at t=1000, accepted at t=5000" + (isErr (validateNbf 1000 defaultClockSkew ({ nbf := Just 5000 } subUser)) && + isOk (validateNbf 5000 defaultClockSkew ({ nbf := Just 5000 } subUser))) + -- Kept last: compact serialisation (RFC 7515 s7.1 / RFC 7519 s6.1) always + -- has three segments; a FAIL here is a suspected library defect. + check "two-part string (missing signature segment) rejected" + (isErr (splitToken "header.payload")) putStrLn "" diff --git a/tests/properties/SafeJsonProps.idr b/tests/properties/SafeJsonProps.idr index b2b51034..62bce2f7 100644 --- a/tests/properties/SafeJsonProps.idr +++ b/tests/properties/SafeJsonProps.idr @@ -4,44 +4,114 @@ module SafeJsonProps import Proven.Core import Proven.SafeJson +import System %default total -||| Property: Parsing valid JSON returns Ok -prop_validJsonParses : parseJson "{}" = Ok (JsonObject []) +-- The real API: `parseJson : String -> Maybe JsonValue` (literal fast paths +-- for null/true/false/""/[]/{}, otherwise the full `parse` state machine), +-- `parse : String -> Either ParseError JsonValue`, `get` for object lookup, +-- `at` for array indexing. There is no `stringify`; `show` renders JSON. + +||| Property: parsing "{}" yields the empty object (holds via parseJson's +||| literal dispatch; the full parser is exercised at runtime below). +prop_validJsonParses : parseJson "{}" = Just (JsonObject []) prop_validJsonParses = Refl -||| Property: Parsing empty string fails -prop_emptyStringFails : isErr (parseJson "") = True +||| Property: parsing the empty string fails +prop_emptyStringFails : parseJson "" = Nothing prop_emptyStringFails = Refl -||| Property: JSON null is a valid value -prop_nullIsValid : parseJson "null" = Ok JsonNull +||| Property: JSON null parses +prop_nullIsValid : parseJson "null" = Just JsonNull prop_nullIsValid = Refl -||| Property: JSON boolean true parses correctly -prop_trueParses : parseJson "true" = Ok (JsonBool True) +||| Property: JSON true parses +prop_trueParses : parseJson "true" = Just (JsonBool True) prop_trueParses = Refl -||| Property: JSON boolean false parses correctly -prop_falseParses : parseJson "false" = Ok (JsonBool False) +||| Property: JSON false parses +prop_falseParses : parseJson "false" = Just (JsonBool False) prop_falseParses = Refl -||| Property: Getting from empty object returns Nothing -prop_emptyObjectGet : get "key" (JsonObject []) = Nothing -prop_emptyObjectGet = Refl +||| Property: getting any key from the empty object returns Nothing +prop_emptyObjectGet : (k : String) -> get k (JsonObject []) = Nothing +prop_emptyObjectGet k = Refl + +||| Property: any index into the empty array returns Nothing +prop_arrayOutOfBounds : (n : Nat) -> at n (JsonArray []) = Nothing +prop_arrayOutOfBounds n = Refl + +||| OWED: render/parse round trip for JsonNull, stated against the full +||| parser (`parse`, not `parseJson`'s literal fast path) on "null", the +||| rendering `show` produces for JsonNull. +||| Blocked by: `parse` runs the module-private, fuel-indexed `parseValue` +||| over `unpack s`, which does not reduce at the type level. A general +||| `parse (render v) = Right v` cannot be stated against the library: there +||| is no `stringify`, and `show` (now a single `Show JsonValue` instance, in +||| Proven.SafeJson.Parser) escapes strings with Idris `show String`, not +||| RFC 8259 §7 escapes, so it is not a JSON serialiser. This names the +||| proposition; it is not a witness. +public export +NullRoundTripObligation : Type +NullRoundTripObligation = parse "null" = Right JsonNull + +||| OWED: the `parseJson` literal fast paths agree with the full parser. +||| Blocked by: same non-reduction of `parse` as above. This names the +||| proposition; it is not a witness. +public export +FastPathAgreesObligation : Type +FastPathAgreesObligation = + (s : String) -> (v : JsonValue) -> parseJson s = Just v -> parse s = Right v + +-- Total structural equality (the library's `Eq JsonValue` is only `covering`). +mutual + private + jsonEq : JsonValue -> JsonValue -> Bool + jsonEq JsonNull JsonNull = True + jsonEq (JsonBool a) (JsonBool b) = a == b + jsonEq (JsonNumber a) (JsonNumber b) = a == b + jsonEq (JsonString a) (JsonString b) = a == b + jsonEq (JsonArray a) (JsonArray b) = listEq a b + jsonEq (JsonObject a) (JsonObject b) = pairsEq a b + jsonEq _ _ = False + + private + listEq : List JsonValue -> List JsonValue -> Bool + listEq [] [] = True + listEq (x :: xs) (y :: ys) = jsonEq x y && listEq xs ys + listEq _ _ = False + + private + pairsEq : List (String, JsonValue) -> List (String, JsonValue) -> Bool + pairsEq [] [] = True + pairsEq ((k, x) :: xs) ((l, y) :: ys) = k == l && jsonEq x y && pairsEq xs ys + pairsEq _ _ = False + +private +maybeEq : Maybe JsonValue -> Maybe JsonValue -> Bool +maybeEq Nothing Nothing = True +maybeEq (Just a) (Just b) = jsonEq a b +maybeEq _ _ = False + +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| Property: Array index out of bounds returns Nothing -prop_arrayOutOfBounds : getIndex 10 (JsonArray []) = Nothing -prop_arrayOutOfBounds = Refl +private +parsesTo : String -> JsonValue -> Bool +parsesTo s v = case parse s of + Right v' => jsonEq v' v + Left _ => False -||| OWED: Stringify then parse roundtrip for simple values -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_roundTripNull_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_roundTripNull : parseJson (stringify JsonNull) = Ok JsonNull +private +rejects : String -> Bool +rejects s = case parse s of + Right _ => False + Left _ => True ||| Test runner for JSON properties export @@ -54,6 +124,57 @@ runJsonProperties = do putStrLn "prop_nullIsValid: PASS (proven by type)" putStrLn "prop_trueParses: PASS (proven by type)" putStrLn "prop_falseParses: PASS (proven by type)" - putStrLn "prop_emptyObjectGet: PASS (proven by type)" - putStrLn "prop_arrayOutOfBounds: PASS (proven by type)" + putStrLn "prop_emptyObjectGet: PASS (proven by type, all keys)" + putStrLn "prop_arrayOutOfBounds: PASS (proven by type, all indices)" + -- The literal fast paths above bypass the real parser; check it directly. + check "full parser: {} / [] / null / true / false" + (parsesTo "{}" (JsonObject []) && parsesTo "[]" (JsonArray []) && + parsesTo "null" JsonNull && parsesTo "true" (JsonBool True) && + parsesTo "false" (JsonBool False)) + check "full parser: empty and whitespace-only input rejected" + (rejects "" && rejects " ") + check "full parser: whitespace-padded and nested values" + (parsesTo " { \"a\" : [1, true, null] } " + (JsonObject [("a", JsonArray [JsonNumber 1.0, JsonBool True, JsonNull])])) + check "full parser: trailing content and malformed input rejected" + (rejects "{} x" && rejects "{\"a\" 1}" && rejects "tru" && rejects "[1 2]") + check "get finds a present key and misses an absent one" + (maybeEq (get "k" (JsonObject [("k", JsonNull)])) (Just JsonNull) && + maybeEq (get "z" (JsonObject [("k", JsonNull)])) Nothing) + check "at: in-bounds index hits, out-of-bounds misses" + (maybeEq (at 0 (JsonArray [JsonBool True])) (Just (JsonBool True)) && + maybeEq (at 10 (JsonArray [JsonBool True])) Nothing) + check "full parser on \"null\" (instance of NullRoundTripObligation)" + (parsesTo "null" JsonNull) + check "fast paths agree with full parser (instances of FastPathAgreesObligation)" + (all (\t => case parseJson t of + Just v => parsesTo t v + Nothing => rejects t) + ["null", "true", "false", "", "[]", "{}"]) + -- RFC 8259 grammar has no trailing commas. Kept last so the other checks + -- above still report; a FAIL here is a suspected parser defect. + check "full parser: trailing commas rejected (RFC 8259)" + (rejects "[1,]" && rejects "{\"a\":1,}") + -- Regression cases for the trailing-comma fix: other comma misuse is + -- rejected, and empty / single / multi-element containers still parse. + check "full parser: empty elements and lone commas rejected" + (rejects "[,]" && rejects "[1,,2]" && rejects "[1 , ]" && rejects "{,}" && + rejects "{\"a\":1 , }" && rejects "{\"a\":1,,\"b\":2}") + check "full parser: empty, single and multi-member containers still parse" + (parsesTo "[ ]" (JsonArray []) && parsesTo "{ }" (JsonObject []) && + parsesTo "[1]" (JsonArray [JsonNumber 1.0]) && + parsesTo "[1, [2, []], {}]" + (JsonArray [JsonNumber 1.0, JsonArray [JsonNumber 2.0, JsonArray []], JsonObject []]) && + parsesTo "{\"a\":1, \"b\":{\"c\":[]}}" + (JsonObject [("a", JsonNumber 1.0), ("b", JsonObject [("c", JsonArray [])])])) + -- Regression: `parse` used to start with fuel = input length, which ran + -- out before the digits of a short number were read. + check "full parser: short and deeply nested numbers parse (fuel bound)" + (parsesTo "1" (JsonNumber 1.0) && parsesTo "-0" (JsonNumber 0.0) && + parsesTo "[[[[1]]]]" (JsonArray [JsonArray [JsonArray [JsonArray [JsonNumber 1.0]]]]) && + parsesTo "{\"a\":[1]}" (JsonObject [("a", JsonArray [JsonNumber 1.0])])) + -- Importers of Proven.SafeJson see a single Show JsonValue instance. + check "show on JsonValue resolves (single instance) and renders as before" + (show (JsonArray [JsonNull, JsonBool True, JsonObject [("k", JsonString "v")]]) + == "[null, true, {\"k\": \"v\"}]") putStrLn "" diff --git a/tests/properties/SafeNetworkProps.idr b/tests/properties/SafeNetworkProps.idr index 500f8d07..7cbcf77e 100644 --- a/tests/properties/SafeNetworkProps.idr +++ b/tests/properties/SafeNetworkProps.idr @@ -4,73 +4,100 @@ module SafeNetworkProps import Proven.Core import Proven.SafeNetwork +import Data.Maybe +import System %default total -||| Property: Valid IPv4 parses -prop_validIPv4 : isOk (parseIPv4 "192.168.1.1") = True -prop_validIPv4 = Refl +-- The real API is Maybe-returning: `parseIPv4`, `parseIPv6`, `parseCIDR` +-- (prefix validated by `mkPrefix`) and `mkPort : Nat -> Maybe Port`. +-- `isPrivateIP` takes the `IPAddress` union, so IPv4 values are wrapped +-- with `IPv4Addr`. The parsers go through `split`/`unpack`/`parsePositive` +-- on String, which do not reduce at the type level, so the parse +-- properties are execution checks in `runNetworkProperties`. -||| Property: Invalid IPv4 fails -prop_invalidIPv4 : isErr (parseIPv4 "256.1.1.1") = True -prop_invalidIPv4 = Refl - -||| Property: Localhost IPv4 parses -prop_localhost : isOk (parseIPv4 "127.0.0.1") = True -prop_localhost = Refl - -||| Property: Valid IPv6 parses -prop_validIPv6 : isOk (parseIPv6 "::1") = True -prop_validIPv6 = Refl - -||| Property: Valid CIDR parses -prop_validCIDR : isOk (parseCIDR "192.168.0.0/24") = True -prop_validCIDR = Refl - -||| Property: Invalid CIDR prefix fails -prop_invalidCIDRPrefix : isErr (parseCIDR "192.168.0.0/33") = True -prop_invalidCIDRPrefix = Refl - -||| Property: Valid port in range -prop_validPort : isOk (validatePort 8080) = True +||| Property: a port in range is accepted +prop_validPort : map (\p => portValue p) (mkPort 8080) = Just 8080 prop_validPort = Refl -||| Property: Port zero is valid -prop_portZero : isOk (validatePort 0) = True +||| Property: port zero is accepted +prop_portZero : map (\p => portValue p) (mkPort 0) = Just 0 prop_portZero = Refl -||| Property: Port max is valid (65535) -prop_portMax : isOk (validatePort 65535) = True +||| Property: the maximum port 65535 is accepted +prop_portMax : map (\p => portValue p) (mkPort 65535) = Just 65535 prop_portMax = Refl -||| Property: Port over max fails -prop_portOverMax : isErr (validatePort 65536) = True +||| Property: 65536 is rejected +prop_portOverMax : isJust (mkPort 65536) = False prop_portOverMax = Refl -||| Property: Private IP detection -prop_privateIP : isPrivateIP (MkIPv4 192 168 1 1) = True +||| Property: private IP detection (RFC 1918, 192.168.0.0/16) +prop_privateIP : isPrivateIP (IPv4Addr (MkIPv4 192 168 1 1)) = True prop_privateIP = Refl -||| Property: Public IP detection -prop_publicIP : isPrivateIP (MkIPv4 8 8 8 8) = False +||| Property: 8.8.8.8 is not private +prop_publicIP : isPrivateIP (IPv4Addr (MkIPv4 8 8 8 8)) = False prop_publicIP = Refl +||| Property: every address in 10.0.0.0/8 is private +prop_tenSlashEightPrivate : (b, c, d : Bits8) -> + isPrivateIP (IPv4Addr (MkIPv4 10 b c d)) = True +prop_tenSlashEightPrivate b c d = Refl + +||| Property: every address in 192.168.0.0/16 is private +prop_192168Private : (c, d : Bits8) -> + isPrivateIP (IPv4Addr (MkIPv4 192 168 c d)) = True +prop_192168Private c d = Refl + +||| Property: mkPrefix rejects 33 (CIDR prefix bound) +prop_prefix33Rejected : isJust (mkPrefix 33) = False +prop_prefix33Rejected = Refl + +||| Property: mkPrefix accepts 24 +prop_prefix24Accepted : map (\p => prefixValue p) (mkPrefix 24) = Just 24 +prop_prefix24Accepted = Refl + +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure + ||| Test runner for network properties export runNetworkProperties : IO () runNetworkProperties = do putStrLn "SafeNetwork Property Tests" putStrLn "==========================" - putStrLn "prop_validIPv4: PASS (proven by type)" - putStrLn "prop_invalidIPv4: PASS (proven by type)" - putStrLn "prop_localhost: PASS (proven by type)" - putStrLn "prop_validIPv6: PASS (proven by type)" - putStrLn "prop_validCIDR: PASS (proven by type)" - putStrLn "prop_invalidCIDRPrefix: PASS (proven by type)" putStrLn "prop_validPort: PASS (proven by type)" putStrLn "prop_portZero: PASS (proven by type)" putStrLn "prop_portMax: PASS (proven by type)" putStrLn "prop_portOverMax: PASS (proven by type)" putStrLn "prop_privateIP: PASS (proven by type)" putStrLn "prop_publicIP: PASS (proven by type)" + putStrLn "prop_tenSlashEightPrivate: PASS (proven by type, all addresses)" + putStrLn "prop_192168Private: PASS (proven by type, all addresses)" + putStrLn "prop_prefix33Rejected: PASS (proven by type)" + putStrLn "prop_prefix24Accepted: PASS (proven by type)" + check "valid IPv4 192.168.1.1 parses to its octets" + (parseIPv4 "192.168.1.1" == Just (MkIPv4 192 168 1 1)) + check "invalid IPv4 256.1.1.1 rejected" + (isNothing (parseIPv4 "256.1.1.1")) + check "malformed IPv4 rejected (too few/many octets, empty, letters)" + (isNothing (parseIPv4 "1.2.3") && isNothing (parseIPv4 "1.2.3.4.5") && + isNothing (parseIPv4 "") && isNothing (parseIPv4 "a.b.c.d")) + check "localhost 127.0.0.1 parses to localhost" + (parseIPv4 "127.0.0.1" == Just localhost) + check "IPv6 ::1 parses to the loopback address" + (map isLoopbackIPv6 (parseIPv6 "::1") == Just True) + check "valid CIDR 192.168.0.0/24 parses with prefix 24" + (map (prefixValue . pfxLen) (parseCIDR "192.168.0.0/24") == Just 24) + check "CIDR prefix /33 rejected" + (isNothing (parseCIDR "192.168.0.0/33")) + check "parsePort agrees with mkPort at the boundaries" + (map portValue (parsePort "8080") == Just 8080 && + map portValue (parsePort "65535") == Just 65535 && + isNothing (parsePort "65536")) putStrLn "" diff --git a/tests/properties/SafePasswordProps.idr b/tests/properties/SafePasswordProps.idr index a686a162..75b179c2 100644 --- a/tests/properties/SafePasswordProps.idr +++ b/tests/properties/SafePasswordProps.idr @@ -4,69 +4,77 @@ module SafePasswordProps import Proven.Core import Proven.SafePassword +import Data.List +import System %default total -||| Property: Strong password validates -prop_strongPasswordValid : isOk (validatePassword "Str0ng!Pass#2024") = True -prop_strongPasswordValid = Refl +-- The real API: `validatePassword : (policy : PasswordPolicy) -> RawPassword +-- -> Either (List PolicyViolation) (ValidPassword policy)` and the Bool +-- decision `isValidPassword policy s = null (checkPolicy policy s)`. +-- The uppercase/lowercase/digit/symbol requirements exist only in +-- `strictPolicy` (min length 12), so the properties use that policy. +-- `checkPolicy` works over `unpack`, which does not reduce at the type +-- level, so policy properties are execution checks. -||| Property: Weak password fails (too short) -prop_weakPasswordFails : isErr (validatePassword "weak") = True -prop_weakPasswordFails = Refl +||| OWED: under strictPolicy, every accepted password has at least Fair +||| strength, for `strength = quickStrengthCheck` (a 12+ char password using +||| all four character classes has ~78 bits of pool entropy before pattern +||| penalties; observed Strong/VeryStrong on sampled inputs). Blocked by: +||| `quickStrengthCheck`/`analyzeStrength` are `covering`, so this total +||| module can neither run them nor mention them even in a type; the +||| proposition is therefore named as a family over the strength function, +||| to be instantiated at `quickStrengthCheck`. `checkPolicy` also goes +||| through `unpack`. This names the proposition; it is not a witness. +public export +StrictValidHasMinStrengthObligation : (String -> StrengthLevel) -> Type +StrictValidHasMinStrengthObligation strength = + (s : String) -> isValidPassword strictPolicy s = True -> + (strength s >= Fair) = True -||| OWED: Password without uppercase fails -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_noUppercaseFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_noUppercaseFails : isErr (validatePassword "lowercase123!") = True +||| OWED: hashing never yields the plaintext, and hashing is deterministic +||| for a fixed salt. Blocked by: the library has no pure or salt-taking +||| hash entry point -- `hashPasswordArgon2id`/`Bcrypt`/`Scrypt` are bodyless +||| `export` FFI stubs returning `IO HashedPassword`, and the salt is chosen +||| inside them -- so neither property can be stated against the current +||| API. Named here as the specification a future pure +||| `hashWithSalt : String -> String -> String` (password, salt) must meet. +||| This names the proposition; it is not a witness. +public export +HashWithSaltSpecObligation : (String -> String -> String) -> Type +HashWithSaltSpecObligation hashWithSalt = + ((p, salt : String) -> Not (hashWithSalt p salt = p), + (p, salt : String) -> (q : String) -> p = q -> + hashWithSalt p salt = hashWithSalt q salt) -||| OWED: Password without lowercase fails -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_noLowercaseFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_noLowercaseFails : isErr (validatePassword "UPPERCASE123!") = True +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| OWED: Password without digit fails -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_noDigitFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_noDigitFails : isErr (validatePassword "NoDigitsHere!") = True +||| Run `validatePassword strictPolicy` and test the violation list. +||| Only used on inputs expected to be rejected (see the defect note below). +private +rejectedWith : (List PolicyViolation -> Bool) -> String -> Bool +rejectedWith p s = case validatePassword strictPolicy (MkRawPassword s) of + Left vs => p vs + Right _ => False -||| OWED: Password without special char fails -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_noSpecialFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_noSpecialFails : isErr (validatePassword "NoSpecial123") = True - -||| OWED: Hash is not the original password -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_hashNotOriginal_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_hashNotOriginal : (p : ValidPassword) -> hashPassword p /= getPasswordString p - -||| Property: Same password produces same hash (deterministic with same salt) -prop_hashDeterministic : (p : ValidPassword) -> (salt : String) -> - hashPasswordWithSalt p salt = hashPasswordWithSalt p salt -prop_hashDeterministic p salt = Refl - -||| OWED: Password strength is at least minimum when valid -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_minStrengthValid_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_minStrengthValid : (p : ValidPassword) -> getStrength p >= MinStrength +private +isMissingUpper, isMissingLower, isMissingDigit, isMissingSymbol, isTooShort + : PolicyViolation -> Bool +isMissingUpper MissingUppercase = True +isMissingUpper _ = False +isMissingLower MissingLowercase = True +isMissingLower _ = False +isMissingDigit MissingDigit = True +isMissingDigit _ = False +isMissingSymbol MissingSymbol = True +isMissingSymbol _ = False +isTooShort (TooShort _ _) = True +isTooShort _ = False ||| Test runner for password properties export @@ -74,7 +82,29 @@ runPasswordProperties : IO () runPasswordProperties = do putStrLn "SafePassword Property Tests" putStrLn "===========================" - putStrLn "prop_strongPasswordValid: PASS (proven by type)" - putStrLn "prop_weakPasswordFails: PASS (proven by type)" - putStrLn "prop_hashDeterministic: PASS (proven by type)" + check "strong password accepted by strictPolicy (isValidPassword)" + (isValidPassword strictPolicy "Str0ng!Pass#2024") + check "weak password rejected as too short" + (rejectedWith (any isTooShort) "weak") + check "password without uppercase rejected (MissingUppercase)" + (rejectedWith (any isMissingUpper) "lowercase123!") + check "password without lowercase rejected (MissingLowercase)" + (rejectedWith (any isMissingLower) "UPPERCASE123!") + check "password without digit rejected (MissingDigit)" + (rejectedWith (any isMissingDigit) "NoDigitsHere!") + check "password without symbol rejected (MissingSymbol)" + (rejectedWith (any isMissingSymbol) "NoSpecial123") + check "each single-class omission yields exactly that class violation" + (rejectedWith (\vs => length vs == 1) "lowercase123!" && + rejectedWith (\vs => length vs == 1) "UPPERCASE123!" && + rejectedWith (\vs => length vs == 1) "NoDigitsHere!" && + rejectedWith (\vs => length vs == 1) "NoSpecial123") + -- Regression (fixed): an accepted password used to evaluate a bodyless + -- `trustCheckPolicy` and abort at runtime; MeetsPolicy now carries the + -- erased evidence checkPolicy policy password = [] (decideMeetsPolicy). + putStrLn "running: validatePassword strictPolicy accepts \"Str0ng!Pass#2024\"" + check "strong password accepted by validatePassword" + (case validatePassword strictPolicy (MkRawPassword "Str0ng!Pass#2024") of + Right _ => True + Left _ => False) putStrLn "" diff --git a/tests/properties/SafePathProps.idr b/tests/properties/SafePathProps.idr index ff02a270..3499ac35 100644 --- a/tests/properties/SafePathProps.idr +++ b/tests/properties/SafePathProps.idr @@ -4,56 +4,76 @@ module SafePathProps import Proven.Core import Proven.SafePath +import Data.Maybe +import System %default total -||| Property: Simple path validates -prop_simplePathValid : isOk (validatePath "/home/user") = True -prop_simplePathValid = Refl +-- The real string API (Proven.SafePath.Operations): +-- validatePath : String -> Either String ValidatedPath (normalises; rejects +-- empty, NUL bytes, over-long segments/paths) +-- normalizePath, safeJoinPaths : String -> String -> Maybe String, +-- getExtension : String -> Maybe String +-- and the structured `Path` API (containsTraversal, safeJoin, ...). +-- There is no `joinPath`; the safe join is `safeJoinPaths`. `validatePath` +-- does not reject `..`: it resolves it. Traversal refusal lives in +-- `safeJoinPaths`/`safeJoin` and `containsTraversal`. -||| Property: Path with traversal fails -prop_traversalFails : isErr (validatePath "/home/../etc/passwd") = True -prop_traversalFails = Refl +||| Property: the empty path is rejected. (The original orphan expected it to +||| be valid as "current directory"; the library deliberately rejects it, in +||| line with POSIX, where an empty pathname is an error.) +prop_emptyPathRejected : validatePath "" = Left "Empty path" +prop_emptyPathRejected = Refl -||| OWED: Null byte in path fails -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_nullByteFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_nullByteFails : isErr (validatePath "/home/user\0malicious") = True +||| Property: any structured path whose first segment is ".." is flagged as +||| containing traversal, for every remaining segment list. +prop_leadingDotDotIsTraversal : (abs : Bool) -> (rest : List Segment) -> + containsTraversal (MkPath abs (".." :: rest)) = True +prop_leadingDotDotIsTraversal abs rest = foldlStaysTrue _ (\e => Refl) rest + where + -- A left fold whose step keeps True fixed returns True from True. + foldlStaysTrue : (f : Bool -> Segment -> Bool) -> + ((e : Segment) -> f True e = True) -> + (xs : List Segment) -> foldl f True xs = True + foldlStaysTrue f keep [] = Refl + foldlStaysTrue f keep (x :: xs) = rewrite keep x in foldlStaysTrue f keep xs -||| OWED: Empty path is valid (current directory) -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_emptyPathValid_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_emptyPathValid : isOk (validatePath "") = True +||| OWED: `safeJoinPaths base rel`, when it succeeds, yields a path whose +||| segments extend those of `normalizePath base` (no escape from base). +||| Blocked by: `splitPath`/`normalizePath` go through `unpack`/`split` on +||| String, which do not reduce on abstract input. This names the +||| proposition; it is not a witness. +public export +SafeJoinContainedObligation : Type +SafeJoinContainedObligation = + (base, rel, full : String) -> safeJoinPaths base rel = Just full -> + isPrefixOf (splitPath (normalizePath base)) (splitPath full) = True -||| Property: Relative path validates -prop_relativePathValid : isOk (validatePath "relative/path") = True -prop_relativePathValid = Refl +||| OWED: `normalizePath` is idempotent. Blocked by: same String +||| non-reduction. This names the proposition; it is not a witness. +public export +NormalizeIdempotentObligation : Type +NormalizeIdempotentObligation = + (s : String) -> normalizePath (normalizePath s) = normalizePath s -||| OWED: Path normalization removes double slashes -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_normalizeDoubleSlash_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_normalizeDoubleSlash : normalizePath "/home//user" = "/home/user" +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| Property: Join paths correctly -prop_joinPaths : joinPath "/home" "user" = "/home/user" -prop_joinPaths = Refl +private +validatesTo : String -> String -> Bool +validatesTo s expected = case validatePath s of + Right v => getPath v == expected + Left _ => False -||| Property: Get extension extracts correctly -prop_getExtension : getExtension "file.txt" = Just "txt" -prop_getExtension = Refl - -||| Property: No extension returns Nothing -prop_noExtension : getExtension "file" = Nothing -prop_noExtension = Refl +private +rejected : String -> Bool +rejected s = case validatePath s of + Right _ => False + Left _ => True ||| Test runner for path properties export @@ -61,10 +81,33 @@ runPathProperties : IO () runPathProperties = do putStrLn "SafePath Property Tests" putStrLn "=======================" - putStrLn "prop_simplePathValid: PASS (proven by type)" - putStrLn "prop_traversalFails: PASS (proven by type)" - putStrLn "prop_relativePathValid: PASS (proven by type)" - putStrLn "prop_joinPaths: PASS (proven by type)" - putStrLn "prop_getExtension: PASS (proven by type)" - putStrLn "prop_noExtension: PASS (proven by type)" + putStrLn "prop_emptyPathRejected: PASS (proven by type)" + putStrLn "prop_leadingDotDotIsTraversal: PASS (proven by type, all paths)" + check "simple absolute path validates unchanged" + (validatesTo "/home/user" "/home/user") + check "relative path validates unchanged" + (validatesTo "relative/path" "relative/path") + check "null byte in path rejected" + (rejected "/home/user\0malicious") + check "traversal escaping the base is refused by safeJoinPaths" + (isNothing (safeJoinPaths "/home" "../etc/passwd") && + isNothing (safeJoinPaths "/home/user" "a/../../../etc")) + check "validatePath resolves traversal rather than passing it through" + (validatesTo "/home/../etc/passwd" "/etc/passwd") + check "structured traversal detection (containsTraversal/safeJoin)" + (containsTraversal (parsePath "/home/../etc/passwd") && + not (containsTraversal (parsePath "/home/user")) && + isNothing (safeJoin (parsePath "/home") (parsePath "../etc/passwd"))) + check "normalisation removes double slashes" + (normalizePath "/home//user" == "/home/user") + check "normalisation idempotent on corpus (instances of NormalizeIdempotentObligation)" + (all (\s => normalizePath (normalizePath s) == normalizePath s) + ["/home//user", "a/./b/../c", "/../x", "../../y", "", "/"]) + check "joining paths" + (safeJoinPaths "/home" "user" == Just "/home/user") + check "extension extracted" + (getExtension "file.txt" == Just "txt" && + getExtension "/a/b/archive.tar.gz" == Just "gz") + check "no extension gives Nothing" + (isNothing (getExtension "file") && isNothing (getExtension "/a/b/file")) putStrLn "" diff --git a/tests/properties/SafePhoneProps.idr b/tests/properties/SafePhoneProps.idr index f235e417..adcb9f92 100644 --- a/tests/properties/SafePhoneProps.idr +++ b/tests/properties/SafePhoneProps.idr @@ -4,71 +4,74 @@ module SafePhoneProps import Proven.Core import Proven.SafePhone +import Data.Maybe +import System %default total -||| Property: E.164 format parses -prop_e164Parses : isOk (parsePhone "+14155551234") = True -prop_e164Parses = Refl +-- The real API is E.164-only: `parseE164 : String -> Maybe PhoneNumber` +-- (requires a leading '+', allows only digits and space/-/./()/ separators, +-- 7..15 digits, splits the country code by E.164 prefix match), +-- `formatE164 : PhoneNumber -> String`, and +-- `stripFormatting`. There is no `parsePhone`, no national-format parser and +-- no `Eq PhoneNumber`. Parsing goes through `unpack`/`substr`/ +-- `parsePositive`, which do not reduce at the type level, so parse +-- properties are execution checks. -||| Property: US format parses -prop_usFormatParses : isOk (parsePhone "(415) 555-1234") = True -prop_usFormatParses = Refl +||| Property: country-code equality is reflexive, for every code. +||| (Replaces the orphan's `p == p` on a phone type with no `Eq`.) +prop_countryCodeEqReflexive : (n : Nat) -> (MkCountryCode n == MkCountryCode n) = True +prop_countryCodeEqReflexive Z = Refl +prop_countryCodeEqReflexive (S k) = prop_countryCodeEqReflexive k -||| Property: International format parses -prop_internationalParses : isOk (parsePhone "+44 20 7946 0958") = True -prop_internationalParses = Refl +||| OWED: parsing is a fixed point of format-then-parse: whatever +||| `parseE164` returns re-parses from its own E.164 rendering to the same +||| country code and subscriber. Blocked by: `parseE164`/`formatE164` go +||| through `unpack`, `substr`, `show` and `parsePositive` on String, none of +||| which reduce on abstract input. This names the proposition; it is not a +||| witness. +public export +FormatE164RoundTripObligation : Type +FormatE164RoundTripObligation = + (s : String) -> (p : PhoneNumber) -> parseE164 s = Just p -> + parseE164 (formatE164 p) = Just p -||| Property: Too short fails -prop_tooShortFails : isErr (parsePhone "123") = True -prop_tooShortFails = Refl +||| OWED: a parsed number's country code has 1 to 3 digits and its +||| subscriber part at least 4. Blocked by: same String non-reduction. (The +||| `LTE 1 cc` part was refuted by "+0123456789" parsing with country code 0 +||| until parseE164 rejected a leading 0; checked at runtime below.) This +||| names the proposition; it is not a witness. +public export +ParsedPartsShapeObligation : Type +ParsedPartsShapeObligation = + (s : String) -> (cc : Nat) -> (sub : String) -> + parseE164 s = Just (MkPhoneNumber (MkCountryCode cc) sub) -> + (LTE 1 cc, LTE cc 999, LTE 4 (length sub)) -||| Property: Too long fails -prop_tooLongFails : isErr (parsePhone "+12345678901234567890") = True -prop_tooLongFails = Refl +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| Property: Letters fail -prop_lettersFail : isErr (parsePhone "+1ABCDEFGHIJ") = True -prop_lettersFail = Refl +private +parsesAs : String -> Nat -> String -> Bool +parsesAs s cc sub = case parseE164 s of + Just (MkPhoneNumber (MkCountryCode c) n) => c == cc && n == sub + Nothing => False -||| OWED: Formatting to E.164 is consistent -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_formatE164Consistent_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_formatE164Consistent : (p : ValidPhone) -> - parsePhone (formatE164 p) = Ok p +private +roundTrips : String -> Bool +roundTrips s = case parseE164 s of + Just p@(MkPhoneNumber (MkCountryCode c) n) => parsesAs (formatE164 p) c n + Nothing => False -||| OWED: Country code extractable -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_countryCodeExtractable_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_countryCodeExtractable : (p : ValidPhone) -> - length (getCountryCode p) >= 1 && length (getCountryCode p) <= 3 - -||| OWED: National number extractable -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_nationalNumberExtractable_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_nationalNumberExtractable : (p : ValidPhone) -> - length (getNationalNumber p) >= 4 - -||| Property: Phone comparison is reflexive -prop_phoneEqReflexive : (p : ValidPhone) -> p == p = True -prop_phoneEqReflexive p = Refl - -||| OWED: Different formats normalize to same -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_normalizationConsistent_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_normalizationConsistent : formatE164 (parsePhone "(415) 555-1234") = formatE164 (parsePhone "4155551234") +private +shapeOk : String -> Bool +shapeOk s = case parseE164 s of + Just (MkPhoneNumber (MkCountryCode c) n) => c >= 1 && c <= 999 && length n >= 4 + Nothing => False ||| Test runner for phone properties export @@ -76,11 +79,55 @@ runPhoneProperties : IO () runPhoneProperties = do putStrLn "SafePhone Property Tests" putStrLn "========================" - putStrLn "prop_e164Parses: PASS (proven by type)" - putStrLn "prop_usFormatParses: PASS (proven by type)" - putStrLn "prop_internationalParses: PASS (proven by type)" - putStrLn "prop_tooShortFails: PASS (proven by type)" - putStrLn "prop_tooLongFails: PASS (proven by type)" - putStrLn "prop_lettersFail: PASS (proven by type)" - putStrLn "prop_phoneEqReflexive: PASS (proven by type)" + putStrLn "prop_countryCodeEqReflexive: PASS (proven by type, all codes)" + check "E.164 +14155551234 parses as +1 / 4155551234" + (parsesAs "+14155551234" 1 "4155551234") + check "US national format without '+' is not E.164 (no national parser exists)" + (isNothing (parseE164 "(415) 555-1234")) + check "different formattings normalise to the same number" + (stripFormatting "(415) 555-1234" == stripFormatting "4155551234" && + parsesAs "+1 (415) 555-1234" 1 "4155551234" && + map formatE164 (parseE164 "+1 (415) 555-1234") == + map formatE164 (parseE164 "+14155551234")) + check "international number with spaces parses" + (isJust (parseE164 "+44 20 7946 0958")) + check "too short rejected" + (isNothing (parseE164 "123") && isNothing (parseE164 "+123")) + check "too long (over 15 digits) rejected" + (isNothing (parseE164 "+12345678901234567890")) + check "all-letter subscriber rejected" + (isNothing (parseE164 "+1ABCDEFGHIJ")) + check "format/parse round trip on corpus (instances of FormatE164RoundTripObligation)" + (all roundTrips ["+14155551234", "+44 20 7946 0958", "+81 3 1234 5678", + "+1 (415) 555-1234"]) + check "parsed part shapes on corpus (instances of ParsedPartsShapeObligation)" + (all shapeOk ["+14155551234", "+44 20 7946 0958", "+81 3 1234 5678"]) + -- Kept last: FAILs here are suspected library defects (see report). + check "UK number +44 20 7946 0958 has country code 44" + (parsesAs "+44 20 7946 0958" 44 "2079460958") + check "letters inside an otherwise valid-length number are rejected" + (isNothing (parseE164 "+1415ABC5551234")) + check "country code 0 is rejected (no E.164 code starts with 0)" + (isNothing (parseE164 "+0123456789")) + -- Regression cases for the prefix-match fix (1-, 2- and 3-digit codes). + check "country code split by prefix: 1, 7, 49, 86, 353, 420, 971, 880" + (parsesAs "+1 268 555 0100" 1 "2685550100" && + parsesAs "+7 495 123 4567" 7 "4951234567" && + parsesAs "+49 30 1234567" 49 "301234567" && + parsesAs "+86 10 1234 5678" 86 "1012345678" && + parsesAs "+353 1 234 5678" 353 "12345678" && + parsesAs "+420 123 456 789" 420 "123456789" && + parsesAs "+971 4 123 4567" 971 "41234567" && + parsesAs "+880 2 1234 5678" 880 "212345678") + check "format/parse round trip and shape on multi-digit-code corpus" + (all (\s => roundTrips s && shapeOk s) + ["+353 1 234 5678", "+420 123 456 789", "+971 4 123 4567", "+7 495 123 4567"]) + check "other non-separator characters rejected; separators still accepted" + (isNothing (parseE164 "+44 20 7946 0958x") && isNothing (parseE164 "+44_2079460958") && + isNothing (parseE164 "+") && isNothing (parseE164 "+ ") && + parsesAs "+44-20-7946-0958" 44 "2079460958" && + parsesAs "+44.20.7946.0958" 44 "2079460958") + check "isValidE164 agrees with parseE164 (letters, code 0, valid UK)" + (not (isValidE164 "+1415ABC5551234") && not (isValidE164 "+0123456789") && + isValidE164 "+44 20 7946 0958" && isValidE164 "+1 (415) 555-1234") putStrLn "" diff --git a/tests/properties/SafeRegexProps.idr b/tests/properties/SafeRegexProps.idr index 34bacabf..3bb96d8c 100644 --- a/tests/properties/SafeRegexProps.idr +++ b/tests/properties/SafeRegexProps.idr @@ -4,66 +4,59 @@ module SafeRegexProps import Proven.Core import Proven.SafeRegex +import Proven.SafeString.Escape +import Data.Either +import System %default total -||| Property: Valid simple regex compiles -prop_simpleRegexCompiles : isOk (compileRegex "hello") = True -prop_simpleRegexCompiles = Refl +-- The real API: `regex : String -> Either RegexError SafeRegex` (parse, then +-- reject patterns whose complexity analysis fails the normal safety level), +-- `match`/`test`/`testFull`, `hasRedosRisk`, and `escapeRegex` (which lives +-- in Proven.SafeString.Escape, not SafeRegex). There is no `compileRegex` +-- and no `ValidRegex`. The parser and matcher run over `unpack`, which does +-- not reduce at the type level, so compile/match properties are execution +-- checks. -||| Property: Valid character class regex compiles -prop_charClassCompiles : isOk (compileRegex "[a-z]+") = True -prop_charClassCompiles = Refl +||| Property: `test` agrees with `match`'s matched flag, for every regex and +||| input (replaces the orphan's `match r s = match r s`, which held for any +||| function). +prop_testAgreesWithMatch : (sr : SafeRegex) -> (s : String) -> + test sr s = (match sr s).matched +prop_testAgreesWithMatch sr s = Refl -||| Property: Unbalanced parentheses fails -prop_unbalancedParensFails : isErr (compileRegex "(hello") = True -prop_unbalancedParensFails = Refl +||| OWED: escaping never shortens a string. Blocked by: `escapeRegex` is +||| `pack . go . unpack`, and `length`/`pack`/`unpack` on String do not reduce +||| on abstract input. This names the proposition; it is not a witness. +public export +EscapePreservesLengthObligation : Type +EscapePreservesLengthObligation = + (s : String) -> LTE (length s) (length (escapeRegex s)) -||| Property: Unbalanced brackets fails -prop_unbalancedBracketsFails : isErr (compileRegex "[abc") = True -prop_unbalancedBracketsFails = Refl +||| OWED: an escaped string compiles and matches exactly the original string. +||| Blocked by: the parser and matcher are fuel-driven over `unpack`, which +||| does not reduce on abstract input. This names the proposition; it is not +||| a witness. +public export +EscapeMatchesLiterallyObligation : Type +EscapeMatchesLiterallyObligation = + (s : String) -> (sr : SafeRegex) -> regex (escapeRegex s) = Right sr -> + testFull sr s = True -||| OWED: Invalid escape fails -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_invalidEscapeFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_invalidEscapeFails : isErr (compileRegex "\\q") = True +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| OWED: ReDoS pattern detected -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_redosDetected_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_redosDetected : isErr (compileRegex "(a+)+$") = True +private +compiles : String -> Bool +compiles = isRight . regex -||| Property: Match returns correct result -prop_matchCorrect : (r : ValidRegex) -> (s : String) -> - match r s = match r s -prop_matchCorrect r s = Refl - -||| OWED: Empty pattern matches everything -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_emptyMatchesAll_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_emptyMatchesAll : isOk (compileRegex "") = True - -||| Property: Anchored regex works -prop_anchoredRegex : isOk (compileRegex "^hello$") = True -prop_anchoredRegex = Refl - -||| OWED: Escape special characters preserves length -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_escapePreservesLength_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_escapePreservesLength : (s : String) -> - length (escapeRegex s) >= length s +private +escapeCorpus : List String +escapeCorpus = ["", "abc", "a.b", "1+1=2", "(x)", "[a-z]*", "^$|\\", "{3}?"] ||| Test runner for regex properties export @@ -71,10 +64,25 @@ runRegexProperties : IO () runRegexProperties = do putStrLn "SafeRegex Property Tests" putStrLn "========================" - putStrLn "prop_simpleRegexCompiles: PASS (proven by type)" - putStrLn "prop_charClassCompiles: PASS (proven by type)" - putStrLn "prop_unbalancedParensFails: PASS (proven by type)" - putStrLn "prop_unbalancedBracketsFails: PASS (proven by type)" - putStrLn "prop_matchCorrect: PASS (proven by type)" - putStrLn "prop_anchoredRegex: PASS (proven by type)" + putStrLn "prop_testAgreesWithMatch: PASS (proven by type, all regexes/inputs)" + check "simple regex compiles" (compiles "hello") + check "character class regex compiles" (compiles "[a-z]+") + check "unbalanced parentheses rejected" (not (compiles "(hello")) + check "unbalanced brackets rejected" (not (compiles "[abc")) + check "invalid escape \\q rejected" (not (compiles "\\q")) + check "ReDoS pattern (a+)+$ rejected and flagged" + (not (compiles "(a+)+$") && hasRedosRisk "(a+)+$") + check "anchored regex compiles and matches only the whole word" + (compiles "^hello$" && quickTest "^hello$" "hello" && + not (quickTest "^hello$" "hello world")) + check "empty pattern compiles and matches every input" + (compiles "" && all (quickTest "") ["", "a", "hello world"]) + check "match: positive and negative cases" + (quickTest "[a-z]+" "123abc" && not (quickTest "[a-z]+" "123") && + quickTestFull "[a-z]+" "abc" && not (quickTestFull "[a-z]+" "abc1")) + check "escaping never shortens (instances of EscapePreservesLengthObligation)" + (all (\s => length (escapeRegex s) >= length s) escapeCorpus) + check "escaped string matches itself literally (instances of EscapeMatchesLiterallyObligation)" + (all (\s => quickTestFull (escapeRegex s) s) escapeCorpus && + not (quickTestFull (escapeRegex "a.b") "axb")) putStrLn "" diff --git a/tests/properties/SafeSQLProps.idr b/tests/properties/SafeSQLProps.idr index 82f3e73c..a74ff775 100644 --- a/tests/properties/SafeSQLProps.idr +++ b/tests/properties/SafeSQLProps.idr @@ -3,59 +3,95 @@ module SafeSQLProps import Proven.Core -import Proven.SafeSQL +-- `Proven.SafeSQL` re-exports `Proven.SafeSQL.Builder`, which proven.ipkg does +-- not list and so is not installed; import the installed sub-modules directly. +import Proven.SafeSQL.Types +import Proven.SafeSQL.Params +import Proven.SafeSQL.Proofs +import System %default total -||| Property: Parameterized query is safe -prop_parameterizedSafe : isOk (buildQuery "SELECT * FROM users WHERE id = ?" [IntParam 1]) = True -prop_parameterizedSafe = Refl - -||| OWED: SQL injection in string blocked -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_injectionBlocked_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_injectionBlocked : isErr (buildQuery "SELECT * FROM users WHERE name = '" []) = True - -||| Property: Escape quotes in strings -prop_escapeQuotes : escapeString "O'Brien" = "O''Brien" -prop_escapeQuotes = Refl - -||| Property: Identifier escaping works -prop_identifierEscape : escapeIdentifier "table name" = "\"table name\"" -prop_identifierEscape = Refl - -||| Property: Valid identifier passes -prop_validIdentifier : isOk (validateIdentifier "users") = True -prop_validIdentifier = Refl - -||| Property: Invalid identifier rejected (starts with number) -prop_invalidIdentifierNum : isErr (validateIdentifier "123users") = True -prop_invalidIdentifierNum = Refl - -||| Property: Reserved word rejected as identifier -prop_reservedWordRejected : isErr (validateIdentifier "SELECT") = True -prop_reservedWordRejected = Refl - -||| OWED: Parameter count matches placeholders -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_paramCountMatches_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_paramCountMatches : (q : String) -> (ps : List SQLParam) -> - countPlaceholders q = length ps -> - isOk (buildQuery q ps) = True - -||| Property: Null parameter handled -prop_nullParamHandled : isOk (buildQuery "SELECT * FROM users WHERE deleted_at IS ?" [NullParam]) = True -prop_nullParamHandled = Refl - -||| Property: LIKE wildcards escaped -prop_likeWildcardsEscaped : escapeLikePattern "100%" = "100\\%" -prop_likeWildcardsEscaped = Refl +-- Rewritten against the real API: queries are `ParameterizedQuery` values built +-- with `literal`/`appendParam`/`bindParams` and checked by `validateParams`; +-- escaping functions take an `SQLDialect`. (`buildQuery`, `IntParam`, +-- `validateIdentifier`, `countPlaceholders` never existed.) + +-------------------------------------------------------------------------------- +-- Type-level proofs +-------------------------------------------------------------------------------- + +||| One placeholder with one bound value validates, for every dialect, literal +||| prefix and value (was prop_parameterizedSafe). +prop_parameterizedSafe : (d : SQLDialect) -> (sql : String) -> (v : SQLValue) -> + isOk (validateParams (bindParams (appendParam (literal d sql)) [v])) = True +prop_parameterizedSafe d sql v = Refl + +||| A placeholder with no bound value is rejected. +prop_unboundParamRejected : (d : SQLDialect) -> (sql : String) -> + isErr (validateParams (appendParam (literal d sql))) = True +prop_unboundParamRejected d sql = Refl + +||| A query with no placeholders needs no bindings. +prop_literalNeedsNoParams : (d : SQLDialect) -> (sql : String) -> + isOk (validateParams (literal d sql)) = True +prop_literalNeedsNoParams d sql = Refl + +||| NULL renders as the keyword NULL in every dialect. +prop_nullRendersAsKeyword : (d : SQLDialect) -> renderValue d SQLNull = "NULL" +prop_nullRendersAsKeyword d = Refl + +||| A NULL parameter binds and validates in every dialect (was prop_nullParamHandled). +prop_nullParamHandled : (d : SQLDialect) -> (sql : String) -> + isOk (validateParams (bindParams (appendParam (literal d sql)) [SQLNull])) = True +prop_nullParamHandled d sql = Refl + +||| Integer values carry an escaping witness in every dialect. +prop_intValuesSafe : (d : SQLDialect) -> (i : Integer) -> IsEscapedValue d (SQLInt i) +prop_intValuesSafe d i = numericValuesCannotInject d i + +-------------------------------------------------------------------------------- +-- Outstanding obligations (named propositions, not witnesses) +-------------------------------------------------------------------------------- + +||| OWED: escaping leaves no unescaped quote that could end the literal early +||| (was prop_injectionBlocked). Blocked by: `unpack`/`++` over an abstract +||| String do not reduce at the type level; the library's `escapeStringQuotesSafe` +||| is itself an erased declaration without a body, not a proof. +||| This names the proposition; it is not a witness. +public export +EscapeStringQuotesObligation : Type +EscapeStringQuotesObligation = + (d : SQLDialect) -> (s : String) -> NoUnescapedQuotes (escapeString d s) + +||| OWED: a query whose positional placeholders are all bound and which has no +||| named placeholders validates (was prop_paramCountMatches). Blocked by: +||| `validateParams` goes through `nub`/`mapMaybe`/`filter` over an abstract +||| fragment list; needs list lemmas linking `countParams`/`getNamedParamNames` +||| to its branch conditions. This names the proposition; it is not a witness. +public export +ParamCountValidatesObligation : Type +ParamCountValidatesObligation = + (q : ParameterizedQuery) -> + LTE (countParams q) (length q.params) -> + getNamedParamNames q = [] -> + isOk (validateParams q) = True + +-------------------------------------------------------------------------------- +-- Execution checks +-------------------------------------------------------------------------------- + +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure + +private +rejected : Maybe a -> Bool +rejected Nothing = True +rejected (Just _) = False ||| Test runner for SQL properties export @@ -64,11 +100,50 @@ runSQLProperties = do putStrLn "SafeSQL Property Tests" putStrLn "======================" putStrLn "prop_parameterizedSafe: PASS (proven by type)" - putStrLn "prop_escapeQuotes: PASS (proven by type)" - putStrLn "prop_identifierEscape: PASS (proven by type)" - putStrLn "prop_validIdentifier: PASS (proven by type)" - putStrLn "prop_invalidIdentifierNum: PASS (proven by type)" - putStrLn "prop_reservedWordRejected: PASS (proven by type)" + putStrLn "prop_unboundParamRejected: PASS (proven by type)" + putStrLn "prop_literalNeedsNoParams: PASS (proven by type)" + putStrLn "prop_nullRendersAsKeyword: PASS (proven by type)" putStrLn "prop_nullParamHandled: PASS (proven by type)" - putStrLn "prop_likeWildcardsEscaped: PASS (proven by type)" + putStrLn "prop_intValuesSafe: PASS (proven by type)" + check "prop_escapeQuotes: escapeString doubles single quotes and wraps" + (escapeString PostgreSQL "O'Brien" == "'O''Brien'" && + escapeString MSSQL "O'Brien" == "'O''Brien'") + check "escapeString strips NUL bytes" + (escapeString SQLite "a\0b" == "'ab'") + check "prop_injectionBlocked: dangling quote in literal SQL is flagged" + (isErr (validateQuerySafety (literal PostgreSQL "SELECT * FROM users WHERE name = '"))) + check "prop_injectionBlocked: OR/comment payload flagged, plain word not" + (isErr (assertNoInjection "' OR 1=1 --") && isOk (assertNoInjection "alice")) + check "prop_injectionBlocked: payload bound as text stays inside one literal" + (escapeString MySQL "x'; DROP TABLE users; --" == "'x''; DROP TABLE users; --'") + check "prop_identifierEscape: identifiers quoted per dialect" + (quoteIdentifier PostgreSQL (MkSafeIdentifier "users") == "\"users\"" && + quoteIdentifier MySQL (MkSafeIdentifier "users") == "`users`" && + quoteIdentifier MSSQL (MkSafeIdentifier "users") == "[users]") + check "prop_identifierEscape: embedded quote characters doubled" + (quoteIdentifier PostgreSQL (MkSafeIdentifier "a\"b") == "\"a\"\"b\"" && + quoteIdentifier MSSQL (MkSafeIdentifier "a]b") == "[a]]b]") + check "prop_validIdentifier: users accepted" + (isOk (assertValidIdentifier "users") && + map identifierName (mkIdentifier "users") == Just "users") + check "prop_invalidIdentifierNum: leading digit rejected" + (rejected (mkIdentifier "123users") && isErr (assertValidIdentifier "123users")) + check "identifiers with space, quote, or empty rejected" + (rejected (mkIdentifier "table name") && rejected (mkIdentifier "a'b") && + rejected (mkIdentifier "")) + check "prop_reservedWordRejected: reserved words detected case-insensitively" + (isReservedWord "SELECT" && isReservedWord "select" && not (isReservedWord "users")) + check "prop_nullParamHandled: NULL interpolates as keyword" + (renderQueryInterpolated + (bindParams (appendParam (literal PostgreSQL "SELECT * FROM users WHERE deleted_at IS ")) [SQLNull]) + == "SELECT * FROM users WHERE deleted_at IS NULL") + check "prop_paramCountMatches: placeholders numbered per dialect" + (renderQuery (appendParam (appendLiteral " AND b = " (appendParam (literal PostgreSQL "a = ")))) + == "a = $1 AND b = $2") + check "prop_paramCountMatches: two placeholders with one value rejected" + (isErr (validateParams (bindParams (appendParam (appendParam (literal SQLite "x"))) [SQLInt 1]))) + check "prop_likeWildcardsEscaped: % and _ escaped" + (escapeLikePattern PostgreSQL "100%" == "100\\%" && + escapeLikePattern PostgreSQL "a_b" == "a\\_b" && + escapeLikePattern MSSQL "100%" == "100[%]") putStrLn "" diff --git a/tests/properties/SafeStringProps.idr b/tests/properties/SafeStringProps.idr index 57ad9ed7..cb7648ee 100644 --- a/tests/properties/SafeStringProps.idr +++ b/tests/properties/SafeStringProps.idr @@ -4,61 +4,123 @@ module SafeStringProps import Proven.Core import Proven.SafeString +import Data.List +import Data.String +import System %default total -||| OWED: Escaping then unescaping returns original (for valid strings) -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_escapeRoundTrip_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_escapeRoundTrip : (s : String) -> ValidUTF8 s -> - unescapeHtml (escapeHtml s) = s - -||| OWED: HTML escaping never returns empty for non-empty input -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_escapeNonEmpty_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_escapeNonEmpty : (s : String) -> length s > 0 -> - length (escapeHtml s) > 0 = True - -||| Property: SQL escaping doubles single quotes +-- Rewritten against the real API: the escapers are `escapeHTML`, `escapeSQL`, +-- `urlEncode`, `escapeShell`; `isValidUTF8` takes `List Bits8` (use +-- `stringToBytes`). There is no `unescapeHtml`, `ValidUTF8 s` predicate, +-- `AlreadyURLEncoded` or `IsASCII` in the library. + +-------------------------------------------------------------------------------- +-- Type-level proofs +-------------------------------------------------------------------------------- + +||| SQL escaping doubles a single quote. prop_sqlEscapeQuotes : escapeSQL "'" = "''" prop_sqlEscapeQuotes = Refl -||| OWED: URL encoding is idempotent for encoded strings -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_urlEncodeIdempotent_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_urlEncodeIdempotent : (s : String) -> AlreadyURLEncoded s -> - urlEncode s = s - -||| OWED: Shell escaping wraps in single quotes -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_shellEscapeWraps_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_shellEscapeWraps : (s : String) -> - head' (escapeShell s) = Just '\'' = True - -||| OWED: UTF-8 validation accepts ASCII -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_asciiIsValidUTF8_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_asciiIsValidUTF8 : (s : String) -> IsASCII s -> isValidUTF8 s = True - -||| Property: Empty string is always valid -prop_emptyStringValid : isValidUTF8 "" = True +||| The empty byte sequence is valid UTF-8 (was prop_emptyStringValid). +prop_emptyBytesValid : isValidUTF8 [] = True +prop_emptyBytesValid = Refl + +||| The empty string encodes to valid UTF-8. +prop_emptyStringValid : isValidUTF8 (stringToBytes "") = True prop_emptyStringValid = Refl +||| A concrete ASCII byte sequence ("Hi!") is valid UTF-8. +prop_asciiBytesValid : isValidUTF8 [0x48, 0x69, 0x21] = True +prop_asciiBytesValid = Refl + +||| A lone continuation byte is rejected. +prop_loneContinuationInvalid : isValidUTF8 [0x80] = False +prop_loneContinuationInvalid = Refl + +||| A truncated two-byte sequence is rejected. +prop_truncatedSequenceInvalid : isValidUTF8 [0xC3] = False +prop_truncatedSequenceInvalid = Refl + +||| A well-formed two-byte sequence (U+00E9) is accepted. +prop_twoByteSequenceValid : isValidUTF8 [0xC3, 0xA9] = True +prop_twoByteSequenceValid = Refl + +-------------------------------------------------------------------------------- +-- Outstanding obligations (named propositions, not witnesses) +-------------------------------------------------------------------------------- + +||| OWED: HTML escaping loses no information, i.e. is injective (the library has +||| no `unescapeHTML`, so the original round-trip is stated as injectivity). +||| Blocked by: `pack`/`unpack` over an abstract String do not reduce at the type +||| level and the per-character `go` is a private `where` helper. +||| This names the proposition; it is not a witness. +public export +EscapeHTMLInjectiveObligation : Type +EscapeHTMLInjectiveObligation = + (s1, s2 : String) -> escapeHTML s1 = escapeHTML s2 -> s1 = s2 + +||| OWED: HTML escaping never returns empty for non-empty input. +||| Blocked by: `length`/`pack`/`unpack` over an abstract String do not reduce. +||| This names the proposition; it is not a witness. +public export +EscapeHTMLNonEmptyObligation : Type +EscapeHTMLNonEmptyObligation = + (s : String) -> length s > 0 = True -> length (escapeHTML s) > 0 = True + +||| OWED: URL encoding is the identity on strings made only of RFC 3986 +||| unreserved characters (the original "idempotent on encoded strings" is false +||| as stated: `%` is itself re-encoded to `%25`). Blocked by: `concat`/`singleton` +||| over an abstract String do not reduce. This names the proposition; it is not +||| a witness. +public export +UrlEncodeUnreservedFixedObligation : Type +UrlEncodeUnreservedFixedObligation = + (s : String) -> all isURLSafe (unpack s) = True -> urlEncode s = s + +||| OWED: shell escaping always wraps its output in single quotes. +||| Blocked by: `++` over an abstract String does not reduce at the type level. +||| This names the proposition; it is not a witness. +public export +ShellEscapeWrapsObligation : Type +ShellEscapeWrapsObligation = + (s : String) -> + (startsWith "'" (escapeShell s) && endsWith "'" (escapeShell s)) = True + +||| OWED: every ASCII string encodes to valid UTF-8. +||| Blocked by: `unpack` over an abstract String does not reduce, and the +||| validator's fuelled loop is a private `where` helper. +||| This names the proposition; it is not a witness. +public export +AsciiIsValidUTF8Obligation : Type +AsciiIsValidUTF8Obligation = + (s : String) -> isAscii s = True -> isValidUTF8 (stringToBytes s) = True + +-------------------------------------------------------------------------------- +-- Execution checks +-------------------------------------------------------------------------------- + +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure + +private +corpus : List String +corpus = ["", "a", "plain text", "", "a & b", "\"q\"", + "100%", "it's", "x/y?z=1", "tab\there", "&"] + +private +noRawHtmlSpecials : String -> Bool +noRawHtmlSpecials s = all (\c => not (c `elem` ['<', '>', '"', '\''])) (unpack s) + +private +pairs : List a -> List (a, a) +pairs xs = [(x, y) | x <- xs, y <- xs] + ||| Test runner for string properties export runStringProperties : IO () @@ -66,6 +128,28 @@ runStringProperties = do putStrLn "SafeString Property Tests" putStrLn "=========================" putStrLn "prop_sqlEscapeQuotes: PASS (proven by type)" + putStrLn "prop_emptyBytesValid: PASS (proven by type)" putStrLn "prop_emptyStringValid: PASS (proven by type)" + putStrLn "prop_asciiBytesValid: PASS (proven by type)" + putStrLn "prop_loneContinuationInvalid: PASS (proven by type)" + putStrLn "prop_truncatedSequenceInvalid: PASS (proven by type)" + putStrLn "prop_twoByteSequenceValid: PASS (proven by type)" + check "prop_escapeRoundTrip: escapeHTML injective on corpus" + (all (\(a, b) => escapeHTML a /= escapeHTML b || a == b) (pairs corpus)) + check "escapeHTML leaves no raw < > \" ' on corpus" + (all (noRawHtmlSpecials . escapeHTML) corpus) + check "escapeHTML concrete output" + (escapeHTML "&" == "<a href="x">&</a>") + check "prop_escapeNonEmpty: non-empty input gives non-empty output on corpus" + (all (\s => length s == 0 || length (escapeHTML s) > 0) corpus) + check "prop_urlEncodeIdempotent: unreserved strings are fixed points" + (all (\s => urlEncode s == s) ["", "abc123", "A-Z_a.z~0"]) + check "urlEncode escapes reserved characters" + (urlEncode " " == "%20" && urlEncode "a&b=c" == "a%26b%3Dc" && urlEncode "%" == "%25") + check "prop_shellEscapeWraps: output starts and ends with a single quote" + (all (\s => startsWith "'" (escapeShell s) && endsWith "'" (escapeShell s)) corpus) + check "escapeShell neutralises embedded single quote" + (escapeShell "it's" == "'it'\\''s'") + check "prop_asciiIsValidUTF8: ASCII corpus encodes to valid UTF-8" + (all (\s => not (isAscii s) || isValidUTF8 (stringToBytes s)) corpus) putStrLn "" - putStrLn "String escape properties verified." diff --git a/tests/properties/SafeTOMLProps.idr b/tests/properties/SafeTOMLProps.idr index 2b8da2e6..ab0d7171 100644 --- a/tests/properties/SafeTOMLProps.idr +++ b/tests/properties/SafeTOMLProps.idr @@ -4,56 +4,76 @@ module SafeTOMLProps import Proven.Core import Proven.SafeTOML +import System +import KnownDefects %default total -||| Property: Valid TOML parses -prop_validTOMLParses : isOk (parseTOML "key = \"value\"") = True -prop_validTOMLParses = Refl +-- `parseTOML`/`parseValue` are `export` (not `public export`), so their results +-- cannot reduce at the type level; document-level properties are execution +-- checks. The original `isOk (parseTOML ...)` checks only asked whether the +-- parser returned *something*; they are strengthened here to check the parsed +-- content, which is what "parses" means. -||| Property: Section header parses -prop_sectionParses : isOk (parseTOML "[section]\nkey = \"value\"") = True -prop_sectionParses = Refl +-------------------------------------------------------------------------------- +-- Type-level proofs +-------------------------------------------------------------------------------- -||| Property: Nested section parses -prop_nestedSectionParses : isOk (parseTOML "[parent.child]\nkey = \"value\"") = True -prop_nestedSectionParses = Refl +mutual + ||| Mapping the identity over a TOML value is the identity (functor law). + prop_mapValuesIdentity : (v : TOMLValue) -> mapValues Prelude.id v = v + prop_mapValuesIdentity (TString _) = Refl + prop_mapValuesIdentity (TInt _) = Refl + prop_mapValuesIdentity (TFloat _) = Refl + prop_mapValuesIdentity (TBool _) = Refl + prop_mapValuesIdentity (TDateTime _) = Refl + prop_mapValuesIdentity (TDate _) = Refl + prop_mapValuesIdentity (TTime _) = Refl + prop_mapValuesIdentity (TArray xs) = cong TArray (mapItemsIdentity xs) + prop_mapValuesIdentity (TInlineTable kvs) = cong TInlineTable (mapPairsIdentity kvs) + prop_mapValuesIdentity (TTable kvs) = cong TTable (mapPairsIdentity kvs) -||| Property: Array parses -prop_arrayParses : isOk (parseTOML "arr = [1, 2, 3]") = True -prop_arrayParses = Refl + private + mapItemsIdentity : (xs : List TOMLValue) -> mapValuesItems Prelude.id xs = xs + mapItemsIdentity [] = Refl + mapItemsIdentity (x :: xs) = cong2 (::) (prop_mapValuesIdentity x) (mapItemsIdentity xs) -||| Property: Inline table parses -prop_inlineTableParses : isOk (parseTOML "point = { x = 1, y = 2 }") = True -prop_inlineTableParses = Refl + private + mapPairsIdentity : (kvs : List (String, TOMLValue)) -> mapValuesPairs Prelude.id kvs = kvs + mapPairsIdentity [] = Refl + mapPairsIdentity ((k, v) :: kvs) = + cong2 (::) (cong (MkPair k) (prop_mapValuesIdentity v)) (mapPairsIdentity kvs) -||| Property: Invalid TOML fails -prop_invalidTOMLFails : isErr (parseTOML "key = [unclosed") = True -prop_invalidTOMLFails = Refl +||| Typed coercion refuses a mismatched value: a string is never an integer. +prop_asIntRejectsString : (s : String) -> isErr (asInt (TString s)) = True +prop_asIntRejectsString s = Refl -||| Property: Duplicate key fails -prop_duplicateKeyFails : isErr (parseTOML "key = 1\nkey = 2") = True -prop_duplicateKeyFails = Refl +||| Typed coercion refuses a mismatched value: an integer is never a boolean. +prop_asBoolRejectsInt : (i : Integer) -> isErr (asBool (TInt i)) = True +prop_asBoolRejectsInt i = Refl -||| Property: Integer parses correctly -prop_integerParses : isOk (parseTOML "num = 42") = True -prop_integerParses = Refl +-------------------------------------------------------------------------------- +-- Execution checks +-------------------------------------------------------------------------------- -||| Property: Float parses correctly -prop_floatParses : isOk (parseTOML "num = 3.14") = True -prop_floatParses = Refl +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure -||| Property: Boolean parses correctly -prop_booleanParses : isOk (parseTOML "flag = true") = True -prop_booleanParses = Refl +private +resultIs : Eq a => a -> Result e a -> Bool +resultIs x (Ok y) = x == y +resultIs _ (Err _) = False -||| Property: DateTime parses correctly -prop_datetimeParses : isOk (parseTOML "dt = 2024-01-15T10:30:00Z") = True -prop_datetimeParses = Refl - -||| Property: Multiline string parses -prop_multilineStringParses : isOk (parseTOML "str = \"\"\"\nmulti\nline\n\"\"\"") = True -prop_multilineStringParses = Refl +||| Parse `src`, then apply an accessor to the document. +private +parsedThen : String -> (TOMLDocument -> Result TOMLError a) -> Result TOMLError a +parsedThen src f = case parseTOML src of + Ok doc => f doc + Err e => Err e ||| Test runner for TOML properties export @@ -61,16 +81,58 @@ runTOMLProperties : IO () runTOMLProperties = do putStrLn "SafeTOML Property Tests" putStrLn "=======================" - putStrLn "prop_validTOMLParses: PASS (proven by type)" - putStrLn "prop_sectionParses: PASS (proven by type)" - putStrLn "prop_nestedSectionParses: PASS (proven by type)" - putStrLn "prop_arrayParses: PASS (proven by type)" - putStrLn "prop_inlineTableParses: PASS (proven by type)" - putStrLn "prop_invalidTOMLFails: PASS (proven by type)" - putStrLn "prop_duplicateKeyFails: PASS (proven by type)" - putStrLn "prop_integerParses: PASS (proven by type)" - putStrLn "prop_floatParses: PASS (proven by type)" - putStrLn "prop_booleanParses: PASS (proven by type)" - putStrLn "prop_datetimeParses: PASS (proven by type)" - putStrLn "prop_multilineStringParses: PASS (proven by type)" + putStrLn "prop_mapValuesIdentity: PASS (proven by type)" + putStrLn "prop_asIntRejectsString: PASS (proven by type)" + putStrLn "prop_asBoolRejectsInt: PASS (proven by type)" + check "empty and comment-only documents parse to the empty document" + (resultIs [] (parseTOML "") && resultIs [] (parseTOML "# comment\n\n")) + check "parseValue: integer, float, boolean, basic and literal strings" + (resultIs (TInt 42) (parseValue "42") && + resultIs (TInt 1000) (parseValue "1_000") && + resultIs (TFloat 3.14) (parseValue "3.14") && + resultIs (TBool True) (parseValue "true") && + resultIs (TString "a\tb") (parseValue "\"a\\tb\"") && + resultIs (TString "C:\\x") (parseValue "'C:\\x'")) + check "parseValue rejects a bad escape and a bare word" + (isErr (parseValue "\"\\q\"") && isErr (parseValue "nope")) + check "accessors and dotted paths on a constructed document" + (let doc = mkDocument [("title", TString "t"), + ("server", mkTable [("port", TInt 8080)])] + in resultIs "t" (getString "title" doc) && + resultIs (TInt 8080) (getPath "server.port" doc) && + isErr (getPath "server.host" doc)) + check "mergeDocuments: second document wins on conflicts" + (mergeDocuments [("a", TInt 1), ("b", TInt 2)] [("b", TInt 3)] + == [("a", TInt 1), ("b", TInt 3)]) + -- Document-level parsing (original intent of every prop_*Parses / *Fails). + -- Known defect: parseTOMLWith is a stub (see KnownDefects). Expectations are + -- the correct ones; the run fails if any of them starts holding unrecorded. + knownDefect TomlParserStub "prop_validTOMLParses: key = \"value\" yields key -> \"value\"" + (resultIs "value" (parsedThen "key = \"value\"" (getString "key"))) + knownDefect TomlParserStub "prop_integerParses: num = 42" + (resultIs 42 (parsedThen "num = 42" (getInt "num"))) + knownDefect TomlParserStub "prop_floatParses: num = 3.14" + (resultIs 3.14 (parsedThen "num = 3.14" (getFloat "num"))) + knownDefect TomlParserStub "prop_booleanParses: flag = true" + (resultIs True (parsedThen "flag = true" (getBool "flag"))) + knownDefect TomlParserStub "prop_sectionParses: [section] key" + (resultIs (TString "value") (parsedThen "[section]\nkey = \"value\"" (getPath "section.key"))) + knownDefect TomlParserStub "prop_nestedSectionParses: [parent.child] key" + (resultIs (TString "value") + (parsedThen "[parent.child]\nkey = \"value\"" (getPath "parent.child.key"))) + knownDefect TomlParserStub "prop_arrayParses: arr = [1, 2, 3]" + (resultIs [TInt 1, TInt 2, TInt 3] (parsedThen "arr = [1, 2, 3]" (getArray "arr"))) + knownDefect TomlParserStub "prop_inlineTableParses: point = { x = 1, y = 2 }" + (resultIs (TInt 2) (parsedThen "point = { x = 1, y = 2 }" (getPath "point.y"))) + knownDefect TomlParserStub "prop_datetimeParses: dt = 2024-01-15T10:30:00Z" + (isOk (parsedThen "dt = 2024-01-15T10:30:00Z" (\d => getField "dt" d >>= asDateTime))) + knownDefect TomlParserStub "prop_multilineStringParses: str = \"\"\"multi line\"\"\"" + (resultIs "multi\nline\n" + (parsedThen "str = \"\"\"\nmulti\nline\n\"\"\"" (getString "str"))) + knownDefect TomlParserStub "prop_invalidTOMLFails: unclosed array rejected" + (isErr (parseTOML "key = [unclosed")) + knownDefect TomlParserStub "prop_duplicateKeyFails: duplicate key rejected" + (isErr (parseTOML "key = 1\nkey = 2")) + knownDefect TomlParserStub "parseValue: array literal [1, 2, 3] keeps its elements" + (resultIs (TArray [TInt 1, TInt 2, TInt 3]) (parseValue "[1, 2, 3]")) putStrLn "" diff --git a/tests/properties/SafeUUIDProps.idr b/tests/properties/SafeUUIDProps.idr index 096a686b..0f9b7e54 100644 --- a/tests/properties/SafeUUIDProps.idr +++ b/tests/properties/SafeUUIDProps.idr @@ -4,66 +4,112 @@ module SafeUUIDProps import Proven.Core import Proven.SafeUUID +import Data.Maybe +import Data.String +import System %default total -||| Property: Valid UUID v4 parses -prop_validUUIDv4Parses : isOk (parseUUID "550e8400-e29b-41d4-a716-446655440000") = True -prop_validUUIDv4Parses = Refl +-- Rewritten against the real API: `parse : String -> Maybe UUID`, +-- `isValid`, `isNil`, `format`, `formatCompact`, record fields `version` / +-- `variant`. There is no `parseUUID`, `ValidUUID`, `toString`, `getVersion`, +-- `generateUUIDv4`, `toBytes` or `fromBytes`; bytes are reached through the +-- re-exported `Proven.SafeHex` (`decode`/`encode`). -||| Property: Uppercase UUID parses -prop_uppercaseUUIDParses : isOk (parseUUID "550E8400-E29B-41D4-A716-446655440000") = True -prop_uppercaseUUIDParses = Refl +private +v4Sample : String +v4Sample = "550e8400-e29b-41d4-a716-446655440000" -||| Property: Invalid format fails -prop_invalidFormatFails : isErr (parseUUID "not-a-uuid") = True +-------------------------------------------------------------------------------- +-- Type-level proofs (closed computations that reduce). Accepting a UUID needs +-- the hex-character test in `isValidHex`, which does not reduce at the type +-- level, so the positive parses are execution checks below. +-------------------------------------------------------------------------------- + +||| A non-hex string is rejected. +prop_invalidFormatFails : isValid "not-a-uuid" = False prop_invalidFormatFails = Refl -||| Property: Wrong length fails -prop_wrongLengthFails : isErr (parseUUID "550e8400-e29b-41d4-a716") = True +||| A truncated UUID is rejected. +prop_wrongLengthFails : isValid "550e8400-e29b-41d4-a716" = False prop_wrongLengthFails = Refl -||| Property: Nil UUID parses -prop_nilUUIDParses : isOk (parseUUID "00000000-0000-0000-0000-000000000000") = True -prop_nilUUIDParses = Refl - -||| OWED: Generated UUID is valid -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_generatedUUIDValid_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_generatedUUIDValid : isOk (parseUUID (toString (generateUUIDv4 seed))) = True - -||| OWED: UUID version is extractable -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_versionExtractable_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_versionExtractable : (uuid : ValidUUID) -> getVersion uuid >= 1 && getVersion uuid <= 5 - -||| OWED: UUID formatting is consistent -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_formatConsistent_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_formatConsistent : (uuid : ValidUUID) -> - parseUUID (toString uuid) = Ok uuid - -||| Property: UUID comparison is reflexive -prop_uuidEqReflexive : (uuid : ValidUUID) -> uuid == uuid = True -prop_uuidEqReflexive uuid = Refl - -||| OWED: UUID bytes roundtrip -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_bytesRoundtrip_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_bytesRoundtrip : (uuid : ValidUUID) -> - fromBytes (toBytes uuid) = Ok uuid +-------------------------------------------------------------------------------- +-- Outstanding obligations (named propositions, not witnesses) +-------------------------------------------------------------------------------- + +||| OWED: formatting a parsed UUID and parsing it again gives the same UUID +||| (was prop_formatConsistent). Blocked by: `substr`/`++`/`unpack` over an +||| abstract String do not reduce at the type level. Holds only for parsed +||| UUIDs: `MkUUID` is public, so an arbitrary record need not be well-formed. +||| This names the proposition; it is not a witness. +public export +UuidFormatRoundTripObligation : Type +UuidFormatRoundTripObligation = + (s : String) -> (u : UUID) -> Proven.SafeUUID.parse s = Just u -> + Proven.SafeUUID.parse (format u) = Just u + +||| OWED: UUID equality is reflexive (was prop_uuidEqReflexive). Blocked by: +||| `==` compares `toLower` of the hex strings, and `toLower`/String equality do +||| not reduce for an abstract String. This names the proposition; it is not a +||| witness. +public export +UuidEqReflexiveObligation : Type +UuidEqReflexiveObligation = (u : UUID) -> (u == u) = True + +||| OWED: a parsed UUID decodes to 16 bytes that re-encode to an equal UUID +||| (was prop_bytesRoundtrip). Blocked by: String primitives in `decode`, +||| `encode` and `parse` do not reduce for an abstract String. +||| This names the proposition; it is not a witness. +public export +UuidBytesRoundTripObligation : Type +UuidBytesRoundTripObligation = + (s : String) -> (u : UUID) -> Proven.SafeUUID.parse s = Just u -> + (bs : List Nat ** (decode (formatCompact u) = Just bs, + length bs = 16, + map (== u) (Proven.SafeUUID.parse (encode bs)) = Just True)) + +-------------------------------------------------------------------------------- +-- Execution checks +-------------------------------------------------------------------------------- + +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure + +private +samples : List String +samples = [ v4Sample + , "550E8400-E29B-41D4-A716-446655440000" + , "00000000-0000-0000-0000-000000000000" + , "6ba7b810-9dad-11d1-80b4-00c04fd430c8" + , "6fa459ea-ee8a-3ca4-894e-db77e160355e" + , "886313e1-3b8a-5372-9b90-0c9aee199e5d" ] + +private +versionOf : String -> Maybe UUIDVersion +versionOf s = map (.version) (Proven.SafeUUID.parse s) + +private +roundTrips : String -> Bool +roundTrips s = case Proven.SafeUUID.parse s of + Nothing => False + Just u => case Proven.SafeUUID.parse (format u) of + Nothing => False + Just u' => u' == u && u'.hex == u.hex && u'.version == u.version && + u'.variant == u.variant + +private +bytesRoundTrip : String -> Bool +bytesRoundTrip s = case Proven.SafeUUID.parse s of + Nothing => False + Just u => case decode (formatCompact u) of + Nothing => False + Just bs => length bs == 16 && + map (== u) (Proven.SafeUUID.parse (encode bs)) == Just True ||| Test runner for UUID properties export @@ -71,10 +117,42 @@ runUUIDProperties : IO () runUUIDProperties = do putStrLn "SafeUUID Property Tests" putStrLn "=======================" - putStrLn "prop_validUUIDv4Parses: PASS (proven by type)" - putStrLn "prop_uppercaseUUIDParses: PASS (proven by type)" putStrLn "prop_invalidFormatFails: PASS (proven by type)" putStrLn "prop_wrongLengthFails: PASS (proven by type)" - putStrLn "prop_nilUUIDParses: PASS (proven by type)" - putStrLn "prop_uuidEqReflexive: PASS (proven by type)" + check "prop_validUUIDv4Parses: canonical v4 UUID accepted" + (isValid v4Sample) + check "prop_validUUIDv4Parses: version V4, variant RFC 4122" + (map (\u => u.version == V4 && u.variant == RFC4122) (Proven.SafeUUID.parse v4Sample) == Just True) + check "prop_uppercaseUUIDParses: uppercase hex accepted" + (isValid "550E8400-E29B-41D4-A716-446655440000") + check "prop_nilUUIDParses: nil UUID accepted" + (isValid "00000000-0000-0000-0000-000000000000") + check "nil UUID string is recognised as nil; v4 sample is not" + (map isNil (Proven.SafeUUID.parse "00000000-0000-0000-0000-000000000000") == Just True && + map isNil (Proven.SafeUUID.parse v4Sample) == Just False && isNil nilUUID) + check "prop_generatedUUIDValid: library-produced nilUUID formats to a valid UUID" + (isValid (format nilUUID) && format nilUUID == "00000000-0000-0000-0000-000000000000") + check "prop_versionExtractable: versions 1, 3, 4, 5 read from the version nibble" + (versionOf "6ba7b810-9dad-11d1-80b4-00c04fd430c8" == Just V1 && + versionOf "6fa459ea-ee8a-3ca4-894e-db77e160355e" == Just V3 && + versionOf v4Sample == Just V4 && + versionOf "886313e1-3b8a-5372-9b90-0c9aee199e5d" == Just V5) + check "prop_formatConsistent: format/parse round-trip on samples" + (all roundTrips samples) + check "format restores 8-4-4-4-12 hyphenation from compact input" + (map format (Proven.SafeUUID.parse "550e8400e29b41d4a716446655440000") == Just v4Sample) + check "prop_uuidEqReflexive: == reflexive and case-insensitive on samples" + (all (\s => map (\u => u == u) (Proven.SafeUUID.parse s) == Just True) samples && + Proven.SafeUUID.parse v4Sample == Proven.SafeUUID.parse "550E8400-E29B-41D4-A716-446655440000") + check "prop_bytesRoundtrip: 16 bytes that re-encode to an equal UUID" + (all bytesRoundTrip samples) + check "rejects non-hex characters and wrong lengths" + (not (isValid "550e8400-e29b-41d4-a716-44665544000g") && + not (isValid "550e8400-e29b-41d4-a716-4466554400000") && not (isValid "")) + check "prop_versionExtractable: hex version nibble 'a' is version 10" + (versionOf "550e8400-e29b-a1d4-a716-446655440000" == Just (Unknown 10)) + check "prop_versionExtractable: version nibble is case-insensitive hex ('F' and 'f' are 15)" + (versionOf "550e8400-e29b-F1d4-a716-446655440000" == Just (Unknown 15) && + versionOf "550e8400-e29b-f1d4-a716-446655440000" == Just (Unknown 15) && + versionOf "550e8400-e29b-91d4-a716-446655440000" == Just (Unknown 9)) putStrLn "" diff --git a/tests/properties/SafeUrlProps.idr b/tests/properties/SafeUrlProps.idr index 7c715735..e0a8981c 100644 --- a/tests/properties/SafeUrlProps.idr +++ b/tests/properties/SafeUrlProps.idr @@ -3,45 +3,82 @@ module SafeUrlProps import Proven.Core -import Proven.SafeUrl +-- The URL parser and query helpers live in the sub-modules; `Proven.SafeUrl` +-- itself does not re-export them. +import Proven.SafeUrl.Parser +import Proven.SafeUrl.Query +import Proven.SafeUrl.Proofs +import Data.Maybe +import System %default total -||| Property: Valid HTTP URL parses successfully -prop_httpUrlParses : isOk (parseUrl "http://example.com") = True -prop_httpUrlParses = Refl +-- Rewritten against the real API: `parseURL : String -> Either URLParseError +-- ParsedURL` (plus `parseURLMaybe`, `isValidURL`) and `urlEncode`/`urlDecode`. +-- There is no `parseUrl` or `encodeQueryValue`. -||| Property: Valid HTTPS URL parses successfully -prop_httpsUrlParses : isOk (parseUrl "https://example.com") = True -prop_httpsUrlParses = Refl +-------------------------------------------------------------------------------- +-- Type-level proofs +-------------------------------------------------------------------------------- -||| Property: Empty string fails to parse -prop_emptyUrlFails : isErr (parseUrl "") = True +||| The empty string is rejected (was prop_emptyUrlFails). +prop_emptyUrlFails : parseURLMaybe "" = Nothing prop_emptyUrlFails = Refl -||| Property: URL with path parses correctly -prop_urlWithPath : isOk (parseUrl "https://example.com/path") = True -prop_urlWithPath = Refl +||| The empty string is not a valid URL. +prop_emptyUrlInvalid : isValidURL "" = False +prop_emptyUrlInvalid = Refl -||| Property: URL with query parses correctly -prop_urlWithQuery : isOk (parseUrl "https://example.com?key=value") = True -prop_urlWithQuery = Refl +||| URL-encoding the empty string yields the empty string. +prop_encodeEmpty : urlEncode "" = "" +prop_encodeEmpty = Refl -||| OWED: Relative URL is rejected (requires scheme) -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_relativeUrlFails_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_relativeUrlFails : isErr (parseUrl "/path/to/resource") = True +||| Adding a query parameter increases the parameter count by one (library theorem). +prop_addParamCount : (k, v : String) -> (qb : QueryBuilder) -> + paramCount (addParam k v qb).params = S (paramCount qb.params) +prop_addParamCount = addParamIncreasesCount -||| Property: Query encoding preserves alphanumerics -prop_queryEncodeAlpha : encodeQueryValue "abc123" = "abc123" -prop_queryEncodeAlpha = Refl +-------------------------------------------------------------------------------- +-- Outstanding obligations (named propositions, not witnesses) +-------------------------------------------------------------------------------- -||| Property: Query encoding escapes special characters -prop_queryEncodeSpecial : encodeQueryValue " " = "%20" -prop_queryEncodeSpecial = Refl +||| OWED: encoding preserves unreserved alphanumerics (was prop_queryEncodeAlpha, +||| generalised). Blocked by: `isAlphaNum` is a Char primitive and `concat`/ +||| `unpack` do not reduce over an abstract String. This names the proposition; +||| it is not a witness. +public export +UrlEncodeAlnumFixedObligation : Type +UrlEncodeAlnumFixedObligation = + (s : String) -> all isAlphaNum (unpack s) = True -> urlEncode s = s + +||| OWED: decoding inverts encoding. Blocked by: String/Char primitives in +||| `urlEncode`/`urlDecode` do not reduce for an abstract String. +||| This names the proposition; it is not a witness. +public export +UrlEncodeDecodeObligation : Type +UrlEncodeDecodeObligation = (s : String) -> urlDecode (urlEncode s) = Just s + +-------------------------------------------------------------------------------- +-- Execution checks +-------------------------------------------------------------------------------- + +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure + +||| Apply a predicate to the parse result; a parse error fails the check. +private +parsedSatisfies : String -> (ParsedURL -> Bool) -> Bool +parsedSatisfies s p = case parseURL s of + Right u => p u + Left _ => False + +private +isHost : String -> ParsedURL -> Bool +isHost h u = u.host == Just (Domain h) ||| Test runner for URL properties export @@ -49,11 +86,37 @@ runUrlProperties : IO () runUrlProperties = do putStrLn "SafeUrl Property Tests" putStrLn "======================" - putStrLn "prop_httpUrlParses: PASS (proven by type)" - putStrLn "prop_httpsUrlParses: PASS (proven by type)" putStrLn "prop_emptyUrlFails: PASS (proven by type)" - putStrLn "prop_urlWithPath: PASS (proven by type)" - putStrLn "prop_urlWithQuery: PASS (proven by type)" - putStrLn "prop_queryEncodeAlpha: PASS (proven by type)" - putStrLn "prop_queryEncodeSpecial: PASS (proven by type)" + putStrLn "prop_emptyUrlInvalid: PASS (proven by type)" + putStrLn "prop_encodeEmpty: PASS (proven by type)" + putStrLn "prop_addParamCount: PASS (proven by type)" + check "prop_httpUrlParses: http scheme and host" + (parsedSatisfies "http://example.com" + (\u => u.scheme == Just HTTP && isHost "example.com" u && u.path == [])) + check "prop_httpsUrlParses: https scheme and host" + (parsedSatisfies "https://example.com" (\u => u.scheme == Just HTTPS && isHost "example.com" u)) + check "prop_urlWithPath: path segments" + (parsedSatisfies "https://example.com/path/to" (\u => u.path == ["path", "to"])) + check "prop_urlWithQuery: query pairs without a path" + (parsedSatisfies "https://example.com?key=value" + (\u => isHost "example.com" u && u.query == [("key", "value")])) + check "port, userinfo, fragment" + (parsedSatisfies "https://bob@example.com:8443/a?x=1#frag" + (\u => u.port == Just 8443 && isHost "example.com" u && + map (.username) u.userInfo == Just "bob" && + u.path == ["a"] && u.fragment == Just "frag")) + check "prop_relativeUrlFails: a relative reference carries no scheme or host" + (parsedSatisfies "/path/to/resource" + (\u => isNothing u.scheme && isNothing u.host && u.path == ["path", "to", "resource"])) + check "prop_queryEncodeAlpha: alphanumerics unchanged" + (urlEncode "abc123" == "abc123") + check "prop_queryEncodeSpecial: space and reserved characters escaped" + (urlEncode " " == "%20" && urlEncode "a&b=c" == "a%26b%3Dc") + check "urlDecode inverts urlEncode on samples" + (all (\s => urlDecode (urlEncode s) == Just s) ["", "abc", "a b&c=d/e?f", "100%"]) + check "urlDecode rejects a malformed escape" + (isNothing (urlDecode "%zz")) + check "query builder round-trips through parseQueryString" + (parseQueryString (buildQueryString (addParam "q" "a b" (addParam "n" "1" emptyQuery))) + == [("n", "1"), ("q", "a b")]) putStrLn "" diff --git a/tests/properties/SafeXMLProps.idr b/tests/properties/SafeXMLProps.idr index ad7ffa65..c5e62df4 100644 --- a/tests/properties/SafeXMLProps.idr +++ b/tests/properties/SafeXMLProps.idr @@ -3,63 +3,165 @@ module SafeXMLProps import Proven.Core -import Proven.SafeXML +-- Parser-level properties are stated as obligations indexed by the parser and +-- exercised on `parseXMLWith` below. +import Proven.SafeXML.Types +import Proven.SafeXML.Builder +import Proven.SafeXML.Proofs +import Proven.SafeXML.Parser +import Data.String +import System +import KnownDefects %default total -||| Property: Valid XML parses -prop_validXMLParses : isOk (parseXML "text") = True -prop_validXMLParses = Refl - -||| Property: Unclosed tag fails -prop_unclosedTagFails : isErr (parseXML "") = True -prop_unclosedTagFails = Refl - -||| Property: Mismatched tags fail -prop_mismatchedTagsFail : isErr (parseXML "") = True -prop_mismatchedTagsFail = Refl - -||| Property: XXE attack blocked (DOCTYPE) -prop_xxeBlocked : isErr (parseXML "]>&xxe;") = True -prop_xxeBlocked = Refl - -||| Property: Billion laughs attack blocked -prop_billionLaughsBlocked : isErr (parseXML "]>&lol2;") = True -prop_billionLaughsBlocked = Refl - -||| OWED: Entity expansion limited -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_entityExpansionLimited_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_entityExpansionLimited : (xml : String) -> - entityCount xml > maxEntityExpansions -> - isErr (parseXML xml) = True - -||| Property: Escape special characters -prop_escapeSpecialChars : escapeXML "<>&\"'" = "<>&"'" -prop_escapeSpecialChars = Refl - -||| OWED: Escape is safe for attribute values -||| Held back by Idris2 0.8.0 not reducing the underlying computation by -||| Refl at the type level (the original orphan-test author wrote this as -||| a `?prop_escapeAttrSafe_rhs` hole). May discharge as `= Refl` once the relevant -||| String/List/Char reduction blocker lifts, or by manual case analysis; -||| revisit during Phase 2 Day-3+ DISCHARGE pass. -0 prop_escapeAttrSafe : (s : String) -> isValidAttrValue (escapeXML s) = True - -||| Property: Valid element name passes -prop_validElementName : isOk (validateElementName "myElement") = True -prop_validElementName = Refl - -||| Property: Invalid element name fails (starts with number) -prop_invalidElementNameNum : isErr (validateElementName "123element") = True -prop_invalidElementNameNum = Refl - -||| Property: External DTD blocked -prop_externalDTDBlocked : isErr (parseXML "") = True -prop_externalDTDBlocked = Refl +-------------------------------------------------------------------------------- +-- Type-level proofs +-------------------------------------------------------------------------------- + +||| Secure defaults forbid DTDs (configuration half of prop_externalDTDBlocked). +prop_secureDefaultsBlockDTD : Types.secureDefaults.allowDTD = False +prop_secureDefaultsBlockDTD = Refl + +||| Secure defaults forbid external entities (configuration half of prop_xxeBlocked). +prop_secureDefaultsBlockXXE : Types.secureDefaults.allowExternalEntities = False +prop_secureDefaultsBlockXXE = Refl + +||| Even the permissive options never allow external entities. +prop_permissiveStillBlocksXXE : Types.permissiveOptions.allowExternalEntities = False +prop_permissiveStillBlocksXXE = Refl + +||| Secure defaults allow no entity definitions and zero expansions +||| (configuration half of prop_billionLaughsBlocked). +prop_secureDefaultsNoExpansion : + (Types.secureDefaults.allowInternalEntities, Types.secureDefaults.maxEntityExpansions) = (False, 0) +prop_secureDefaultsNoExpansion = Refl + +||| A document with no root element is rejected by the builder. +prop_documentNeedsRoot : isErr (buildDocument Builder.document) = True +prop_documentNeedsRoot = Refl + +-------------------------------------------------------------------------------- +-- Outstanding obligations (named propositions, not witnesses) +-------------------------------------------------------------------------------- + +||| Shape of the secure parser, `Proven.SafeXML.Parser.parseXMLWith`. +public export +XMLParser : Type +XMLParser = XMLSecurityOptions -> String -> XMLResult XMLDocument + +||| OWED: a well-formed document parses under secure defaults (was +||| prop_validXMLParses). Blocked by: no proof over the parser's String +||| loops; executed below on `parseXMLWith`. This names the proposition; it is not a witness. +public export +XmlWellFormedAcceptedObligation : XMLParser -> Type +XmlWellFormedAcceptedObligation p = + isOk (p secureDefaults "text") = True + +||| OWED: an unclosed element is rejected (was prop_unclosedTagFails). +||| Blocked by: no proof over the parser; executed below. This names the proposition; +||| it is not a witness. +public export +XmlUnclosedRejectedObligation : XMLParser -> Type +XmlUnclosedRejectedObligation p = isErr (p secureDefaults "") = True + +||| OWED: mismatched tags are rejected (was prop_mismatchedTagsFail). +||| Blocked by: no proof over the parser; executed below. This names the proposition; it is not a witness. +public export +XmlMismatchedRejectedObligation : XMLParser -> Type +XmlMismatchedRejectedObligation p = isErr (p secureDefaults "") = True + +||| OWED: an XXE payload is rejected (was prop_xxeBlocked). +||| Blocked by: no proof over the parser; executed below. This names the proposition; it is not a witness. +public export +XmlXxeRejectedObligation : XMLParser -> Type +XmlXxeRejectedObligation p = + isErr (p secureDefaults + "]>&xxe;") = True + +||| OWED: a billion-laughs payload is rejected (was prop_billionLaughsBlocked). +||| Blocked by: no proof over the parser; executed below. This names the proposition; it is not a witness. +public export +XmlBillionLaughsRejectedObligation : XMLParser -> Type +XmlBillionLaughsRejectedObligation p = + isErr (p secureDefaults + "]>&lol2;") = True + +||| OWED: an external DTD reference is rejected (was prop_externalDTDBlocked). +||| Blocked by: no proof over the parser; executed below. This names the proposition; it is not a witness. +public export +XmlExternalDtdRejectedObligation : XMLParser -> Type +XmlExternalDtdRejectedObligation p = + isErr (p secureDefaults "") = True + +||| OWED: with DTDs disabled, any input carrying a DOCTYPE is rejected; entity +||| definitions need a DTD, so this bounds entity expansion (was +||| prop_entityExpansionLimited; `entityCount` never existed). Blocked by: no +||| proof over the parser, and `isInfixOf` over an abstract String does not reduce. +||| This names the proposition; it is not a witness. +public export +XmlDtdRejectedObligation : XMLParser -> Type +XmlDtdRejectedObligation p = + (opts : XMLSecurityOptions) -> opts.allowDTD = False -> + (xml : String) -> isInfixOf " + isErr (p opts xml) = True + +||| OWED: an escaped attribute value contains no raw quote or angle bracket +||| (was prop_escapeAttrSafe; `isValidAttrValue` never existed). Blocked by: +||| `pack`/`unpack` over an abstract String do not reduce, and the per-character +||| escaper is private. This names the proposition; it is not a witness. +public export +XmlAttrEscapeSafeObligation : Type +XmlAttrEscapeSafeObligation = + (s : String) -> + all (\c => not (c `elem` ['"', '\'', '<', '>'])) (unpack (xmlAttrValue s).escaped) = True + +-------------------------------------------------------------------------------- +-- Execution checks +-------------------------------------------------------------------------------- + +private +check : String -> Bool -> IO () +check label True = putStrLn ("PASS (execution): " ++ label) +check label False = do + putStrLn ("FAIL: " ++ label) + exitFailure + +private +renders : XMLResult XMLNode -> String -> Bool +renders (Ok n) expected = renderNode n == expected +renders (Err _) _ = False + +private +hostile : List String +hostile = ["", "", "]]>", + "\"' onload=\"x", "&xxe;", "