From de5ee810405562bd193cb3864a99606a316b5ef1 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:18:42 +0000 Subject: [PATCH 01/11] test(ffi): add fail-closed native Rustler symbol contract and planted controls --- scripts/check-native-nif-exports.sh | 89 +++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 scripts/check-native-nif-exports.sh diff --git a/scripts/check-native-nif-exports.sh b/scripts/check-native-nif-exports.sh new file mode 100644 index 00000000..31951062 --- /dev/null +++ b/scripts/check-native-nif-exports.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +# +# Verify the *defined* exported symbols in a compiled libproven artifact +# cover the Rustler consumer's declared C ABI. This is a necessary, not +# sufficient, ABI condition: it does not establish compatible signatures, +# ownership, verified Idris provenance, or correct behaviour. +# +# Usage: bash scripts/check-native-nif-exports.sh LIBPROVEN [NIF_LIB_RS] +# Exit 0: all required names are defined; 1: missing symbols; 2: NOT RUN. +set -euo pipefail + +if [[ $# -lt 1 || $# -gt 2 ]]; then + echo 'PROVEN_NIF_ABI_CLASS=not_run:usage' >&2 + echo 'usage: check-native-nif-exports.sh LIBPROVEN [NIF_LIB_RS]' >&2 + exit 2 +fi + +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +lib=$1 +nif=${2:-"$root/bindings/elixir/native/proven_nif/src/lib.rs"} + +if [[ ! -f $lib || ! -r $lib || ! -f $nif || ! -r $nif ]]; then + echo 'PROVEN_NIF_ABI_CLASS=not_run:missing_input' >&2 + exit 2 +fi +for cmd in awk comm nm sort wc; do + command -v "$cmd" >/dev/null 2>&1 || { + echo "PROVEN_NIF_ABI_CLASS=not_run:missing_tool:$cmd" >&2 + exit 2 + } +done + +tmp=$(mktemp -d) +trap 'rm -rf -- "$tmp"' EXIT + +# Scan only the Rust extern-C block. Do not count Rust NIF wrappers or +# incidental function names elsewhere in the file. +awk ' + /^[[:space:]]*extern "C"[[:space:]]*\{/ { in_extern=1; next } + in_extern && /^[[:space:]]*\}/ { in_extern=0 } + in_extern && /^[[:space:]]*fn[[:space:]]+proven_[A-Za-z0-9_]+[[:space:]]*\(/ { + line=$0 + sub(/^[[:space:]]*fn[[:space:]]+/, "", line) + sub(/\(.*/, "", line) + print line + } +' "$nif" | LC_ALL=C sort -u > "$tmp/required" + +if [[ ! -s $tmp/required ]]; then + echo 'PROVEN_NIF_ABI_CLASS=not_run:empty_contract' >&2 + exit 2 +fi + +case $lib in + *.a) nm -g --defined-only "$lib" > "$tmp/nm" 2>"$tmp/nm-error" || { + echo 'PROVEN_NIF_ABI_CLASS=not_run:unreadable_archive' >&2 + cat "$tmp/nm-error" >&2 + exit 2 + } ;; + *.so|*.so.*) nm -D --defined-only "$lib" > "$tmp/nm" 2>"$tmp/nm-error" || { + echo 'PROVEN_NIF_ABI_CLASS=not_run:unreadable_shared_lib' >&2 + cat "$tmp/nm-error" >&2 + exit 2 + } ;; + *) + echo 'PROVEN_NIF_ABI_CLASS=not_run:unsupported_artifact' >&2 + exit 2 ;; +esac + +# Use only defined symbols; never mistake an undefined (U) reference for +# evidence that a function exists. nm --defined-only excludes those. +awk '{ if ($NF ~ /^proven_[A-Za-z0-9_]+$/) print $NF }' "$tmp/nm" | + LC_ALL=C sort -u > "$tmp/defined" + +LC_ALL=C comm -23 "$tmp/required" "$tmp/defined" > "$tmp/missing" + +required_count=$(wc -l < "$tmp/required") +defined_count=$(wc -l < "$tmp/defined") +missing_count=$(wc -l < "$tmp/missing") +echo "NIF ABI symbols: required=$required_count artifact_defined=$defined_count missing=$missing_count" +if [[ -s $tmp/missing ]]; then + echo 'PROVEN_NIF_ABI_CLASS=missing_symbols' >&2 + sed 's/^/ missing C ABI symbol: /' "$tmp/missing" >&2 + exit 1 +fi +echo 'PROVEN_NIF_ABI_CLASS=symbols_present_only' +echo 'NOTE: symbol coverage is NOT ABI type conformance or formal proof.' From ead5185991006be95ed68b78becf4f006d05ca95 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:18:45 +0000 Subject: [PATCH 02/11] test(ffi): add fail-closed native Rustler symbol contract and planted controls --- tests/check-native-nif-exports.sh | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/check-native-nif-exports.sh diff --git a/tests/check-native-nif-exports.sh b/tests/check-native-nif-exports.sh new file mode 100644 index 00000000..97f8bb6f --- /dev/null +++ b/tests/check-native-nif-exports.sh @@ -0,0 +1,60 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +# Self-test the libproven/Rustler symbol-coverage gate with planted failures. +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +driver="$root/scripts/check-native-nif-exports.sh" +for cmd in cc ar; do + command -v "$cmd" >/dev/null 2>&1 || { + echo "native-nif-symbol-controls: NOT RUN (missing $cmd)" >&2 + exit 2 + } +done + +tmp=$(mktemp -d) +trap 'rm -rf -- "$tmp"' EXIT +cat > "$tmp/lib.rs" <<'EOF' +#[link(name = "proven")] +extern "C" { + fn proven_init() -> i32; + fn proven_math_div(a: i64, b: i64) -> i64; +} +fn wrapper_not_a_required_export() {} +EOF +cat > "$tmp/good.c" <<'EOF' +int proven_init(void) { return 0; } +long proven_math_div(long a, long b) { return b ? a / b : 0; } +EOF +cat > "$tmp/bad.c" <<'EOF' +int proven_init(void) { return 0; } +EOF +cc -shared -fPIC -o "$tmp/good.so" "$tmp/good.c" +cc -shared -fPIC -o "$tmp/bad.so" "$tmp/bad.c" +cc -c -o "$tmp/good.o" "$tmp/good.c" +ar rcs "$tmp/good.a" "$tmp/good.o" + +expect() { + local label=$1 want=$2 pattern=$3 + shift 3 + local got=0 + "$@" > "$tmp/log" 2>&1 || got=$? + if [[ $got != "$want" ]] || ! grep -Fq "$pattern" "$tmp/log"; then + echo "FAILED: $label (exit $got, expected $want, pattern $pattern)" >&2 + cat "$tmp/log" >&2 + exit 1 + fi + echo "ok: $label" +} + +expect 'all exported symbols (shared)' 0 'symbols_present_only' bash "$driver" "$tmp/good.so" "$tmp/lib.rs" +expect 'all exported symbols (static)' 0 'symbols_present_only' bash "$driver" "$tmp/good.a" "$tmp/lib.rs" +expect 'planted missing symbol is refused' 1 'missing C ABI symbol: proven_math_div' bash "$driver" "$tmp/bad.so" "$tmp/lib.rs" +expect 'missing library is NOT RUN' 2 'not_run:missing_input' bash "$driver" "$tmp/nonexistent.so" "$tmp/lib.rs" +: > "$tmp/empty.rs" +expect 'empty consumer contract is NOT RUN' 2 'not_run:empty_contract' bash "$driver" "$tmp/good.so" "$tmp/empty.rs" + +# An invalid artifact must not be accepted merely because it has a .so suffix. +printf 'not an ELF binary\n' > "$tmp/invalid.so" +expect 'unreadable artifact is NOT RUN' 2 'not_run:unreadable_shared_lib' bash "$driver" "$tmp/invalid.so" "$tmp/lib.rs" +echo 'native-nif-symbol-controls: 6 cases passed (actual ABI not yet verified)' From fe46abaa85023d8d88c44b347596c299733c704e Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:18:48 +0000 Subject: [PATCH 03/11] test(ffi): add fail-closed native Rustler symbol contract and planted controls --- docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc | 57 +++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc diff --git a/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc new file mode 100644 index 00000000..d63ea8c6 --- /dev/null +++ b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc @@ -0,0 +1,57 @@ +// SPDX-License-Identifier: CC-BY-SA-4.0 +// SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell += Native libproven / Rustler ABI boundary + +This repository currently has a *broken consumer boundary*, not a missing +search directory alone. `bindings/elixir/native/proven_nif/src/lib.rs` +declares 66 `proven_*` C ABI functions. The current +`ffi/zig/src/main.zig` exports 17 `proven_*` functions and does not +define most of the required functions, such as `proven_math_div`. +The shared name `proven_init` also has incompatible return types: +Rust declares `i32` while Zig returns an optional `*Handle`. +These are source observations, not a verified inventory of any +generated Idris2 RefC C output. + +== A mandatory precondition, not a substitute for proof + +Run `bash scripts/check-native-nif-exports.sh /path/to/libproven.so` +*after* the authentic Idris2 -> RefC -> Zig library build. +For a static library, pass `libproven.a` instead. + +The checker extracts required C symbols from the Rust `extern "C"` block +and confirms that the compiled library *defines* all of them. Missing +names cause exit 1; missing tools/inputs or unreadable artifacts cause +exit 2 (NOT RUN); only a complete symbol list gives exit 0. + +The script expressly **does not verify ABI types, memory ownership, +verified Idris2 provenance, dynamic dependencies, or behavioural +semantics**. Its positive controls are fixture libraries, not +`libproven` evidence. Type/layout compatibility must additionally be +checked against an authoritative C header, with compiled calls and +negative fixtures, before declaring the full interface safe. +Actual runtime behaviour must be exercised from Elixir and Rust. + +== Required upstream repairs + +. Resolve the currently private `nextgen-languages/language-bridges` + dependency with correctly scoped credentials or a reviewed vendor pin. +. Produce genuine Idris2 RefC code, with pinned compiler/runtime inputs, + and include the required ABI implementation in the Zig linker. +. Agree whether the consumer links a static or shared `libproven`. + The Zig build currently emits a static target while Rust explicitly + requests `dylib=proven`. +. Repair all 66 function contracts, including `proven_init` return + type and correct ownership of returned buffers. +. Check exported symbols on *the exact artifact to be consumed*; run + typed calls against it and the real Elixir/Rustler integration. +. Publish immutable, attestable artifact provenance and a pinned, + reproducible consumer configuration. Never silently substitute + `build_standalone.zig`, which builds a narrower pure Zig ABI. + +== Positive and negative gate controls + +`bash tests/check-native-nif-exports.sh` runs six isolated cases: +a defined shared and static fixture, deliberately missing symbols, +missing input, an empty contract, and a corrupt binary. These tests +establish only that the name-presence gate fails closed; they do +not certify this repository's actual `libproven` ABI. From 254bd071441e06d721d14cf492775c6126c033cb Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:19:55 +0000 Subject: [PATCH 04/11] ci(ffi): exercise planted controls and gate real Idris-Zig artifact symbol coverage --- .github/workflows/ffi-full-integration.yml | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/.github/workflows/ffi-full-integration.yml b/.github/workflows/ffi-full-integration.yml index e8487f65..0db4e0f1 100644 --- a/.github/workflows/ffi-full-integration.yml +++ b/.github/workflows/ffi-full-integration.yml @@ -16,6 +16,9 @@ on: - 'proven-ffi.ipkg' - 'scripts/build-refc.sh' - '.github/workflows/ffi-full-integration.yml' + - 'bindings/elixir/native/proven_nif/src/lib.rs' + - 'scripts/check-native-nif-exports.sh' + - 'tests/check-native-nif-exports.sh' pull_request: branches: [main] paths: @@ -24,11 +27,28 @@ on: - 'proven-ffi.ipkg' - 'scripts/build-refc.sh' - '.github/workflows/ffi-full-integration.yml' + - 'bindings/elixir/native/proven_nif/src/lib.rs' + - 'scripts/check-native-nif-exports.sh' + - 'tests/check-native-nif-exports.sh' workflow_dispatch: concurrency: group: ffi-full-integration-${{ github.ref }} cancel-in-progress: true jobs: + nif-abi-symbol-controls: + name: Native ABI symbol validator (planted controls only) + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - name: Checkout proven + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 + with: + persist-credentials: false + - name: Execute real positive and negative controls + # This proves the validator detects missing exports. It does NOT + # imply any real libproven artifact was built or validated. + run: bash tests/check-native-nif-exports.sh + idris-refc-zig: name: Idris -> RefC -> Zig runs-on: ubuntu-latest @@ -126,3 +146,13 @@ jobs: -Didris-c-support="$C_SUPPORT" \ -Didris-support-lib="$SUPPORT_LIB" ls -la zig-out/lib/libproven.* + - name: Verify the genuine built artifact exports every Rustler NIF symbol + if: steps.bridge-token.outputs.available == 'true' + run: | + set -euo pipefail + artifact="$GITHUB_WORKSPACE/ffi/zig/zig-out/lib/libproven.a" + if [ ! -f "$artifact" ]; then + echo "::error::Missing actual libproven.a, not a successful integration." + exit 1 + fi + bash scripts/check-native-nif-exports.sh "$artifact" From e1a877851c8026048e3bbceb1ad7f261a3afd987 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:21:52 +0000 Subject: [PATCH 05/11] fix(ffi): fail closed when private Idris2 bridge dependency is unavailable --- .github/workflows/ffi-full-integration.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ffi-full-integration.yml b/.github/workflows/ffi-full-integration.yml index 0db4e0f1..6f86b5e5 100644 --- a/.github/workflows/ffi-full-integration.yml +++ b/.github/workflows/ffi-full-integration.yml @@ -70,10 +70,11 @@ jobs: 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" + echo "::error title=Full FFI not run::LANGUAGE_BRIDGES_TOKEN is absent; genuine Idris2/RefC/Zig integration cannot be verified." + echo "PROVEN_FFI_CLASS=not_run:missing_private_dependency" + exit 2 fi + echo "available=true" >> "$GITHUB_OUTPUT" - name: Checkout language-bridges if: steps.bridge-token.outputs.available == 'true' uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4 From 597e440ed58d0c3845c93aa3cad50b8a33dbb3f7 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:53:30 +0000 Subject: [PATCH 06/11] fix(refc): reject missing or stale Idris2 C output and eliminate duplicate staging --- scripts/build-refc.sh | 63 +++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/scripts/build-refc.sh b/scripts/build-refc.sh index a16fe8a4..579a00a7 100755 --- a/scripts/build-refc.sh +++ b/scripts/build-refc.sh @@ -121,6 +121,11 @@ fi mkdir -p "$REFC_OUTPUT_DIR" +# A successful compiler exit is not enough: the C must be produced by THIS +# invocation. A prior run's generated file cannot serve as fresh evidence. +REFC_BUILD_STAMP=$(mktemp "$PROVEN_ROOT/build/.refc-start.XXXXXXXX") +trap 'rm -f "$REFC_BUILD_STAMP"' EXIT + # --------------------------------------------------------------------------- # Step 1: Compile Idris2 to C via RefC backend # --------------------------------------------------------------------------- @@ -150,51 +155,43 @@ fi EXEC_DIR="$PROVEN_ROOT/build/exec" BUILD_DIR="$PROVEN_ROOT/build" -# Find all generated .c files from the RefC compilation +# Only include RefC output created by the current compiler invocation. +# Do not also scan BUILD_DIR: it contains EXEC_DIR AND the previously staged +# build/refc directory, which made the old implementation double-count outputs +# and accept stale C as though Idris2 had generated it again. 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..." +if [ -d "$EXEC_DIR" ]; then + while IFS= read -r -d '' f; do + GENERATED_C_FILES+=("$f") + done < <(find "$EXEC_DIR" -type f -name '*.c' -newer "$REFC_BUILD_STAMP" -print0 2>/dev/null) + while IFS= read -r -d '' f; do + GENERATED_H_FILES+=("$f") + done < <(find "$EXEC_DIR" -type f -name '*.h' -newer "$REFC_BUILD_STAMP" -print0 2>/dev/null) +fi - # 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 +if [ "${#GENERATED_C_FILES[@]}" -eq 0 ]; then + die "RefC generated no fresh C source; cannot claim a build artifact. No prior staged output may be reused." 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 +log "Found ${#GENERATED_C_FILES[@]} fresh generated C file(s):" +for f in "${GENERATED_C_FILES[@]}"; do + verbose " $(realpath --relative-to="$PROVEN_ROOT" "$f")" +done + +if [ "${#GENERATED_H_FILES[@]}" -gt 0 ]; then + log "Found ${#GENERATED_H_FILES[@]} fresh generated header file(s):" fi # --------------------------------------------------------------------------- # Step 3: Copy/symlink generated files to build/refc/ for Zig consumption # --------------------------------------------------------------------------- +# Remove stale staged C/H only after verifying fresh RefC output exists. +# This directory is owned by this build script and must represent one run. +find "$REFC_OUTPUT_DIR" -maxdepth 1 -type f \( -name '*.c' -o -name '*.h' \) -delete + # 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/" From 9c95a139e2e47efe7d1901b91fb3af8dd051378c Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:54:43 +0000 Subject: [PATCH 07/11] test(refc): plant empty, stale, fresh and compiler-failure staging controls --- tests/check-refc-staging.sh | 67 +++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/check-refc-staging.sh diff --git a/tests/check-refc-staging.sh b/tests/check-refc-staging.sh new file mode 100644 index 00000000..2ff52142 --- /dev/null +++ b/tests/check-refc-staging.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: MPL-2.0 +# SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell +# Test the actual RefC staging script without requiring a compiler or private +# bridge. These fixtures do NOT establish that generated C is correct Idris. +set -euo pipefail +root=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +script="$root/scripts/build-refc.sh" +tmp=$(mktemp -d) +trap 'rm -rf -- "$tmp"' EXIT +project="$tmp/project" +prefix="$tmp/idris-install" +mkdir -p "$project/build/exec" "$project/build/refc" "$prefix/idris2-0.8.0/support/refc" +touch "$prefix/idris2-0.8.0/support/refc/libidris2_refc.a" +printf 'package proven-ffi\n' > "$project/proven-ffi.ipkg" +cat > "$tmp/fake-idris2" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +case "${1:-}" in + --version) echo 'Idris 2, version 0.8.0' ;; + --prefix) printf '%s\n' "$FAKE_IDRIS_PREFIX" ;; + --codegen) + [[ "${2:-}" == refc && "${3:-}" == --build ]] || exit 9 + case "${FAKE_ACTION:-}" in + write) + mkdir -p "$PROVEN_ROOT/build/exec" + printf '/* generated this run */\n' > "$PROVEN_ROOT/build/exec/proven-ffi.c" + ;; + none) : ;; + fail) echo 'planted RefC compiler failure' >&2; exit 13 ;; + *) exit 10 ;; + esac + ;; + *) exit 11 ;; +esac +EOF +chmod +x "$tmp/fake-idris2" + +run_case() { + local action=$1 want=$2 expected=$3 rc=0 + env IDRIS2="$tmp/fake-idris2" PROVEN_ROOT="$project" \ + FAKE_IDRIS_PREFIX="$prefix" FAKE_ACTION="$action" \ + bash "$script" --clean > "$tmp/output.log" 2>&1 || rc=$? + if [[ "$rc" != "$want" ]] || ! grep -Fq "$expected" "$tmp/output.log"; then + printf 'FAILED: action %s expected exit %s and text %q, got %s\n' "$action" "$want" "$expected" "$rc" >&2 + cat "$tmp/output.log" >&2 + exit 1 + fi + echo "ok: $action -> $want ($expected)" +} + +run_case none 1 'RefC generated no fresh C source' +printf '/* stale earlier build */\n' > "$project/build/exec/proven-ffi.c" +touch -t 202001010000 "$project/build/exec/proven-ffi.c" +run_case none 1 'RefC generated no fresh C source' +printf '/* unrelated stale stage */\n' > "$project/build/refc/old.c" +run_case write 0 'Found 1 fresh generated C file(s)' +[[ $(find "$project/build/refc" -maxdepth 1 -name '*.c' -type f | wc -l) == 1 ]] || { + echo 'FAILED: stale or duplicate C persisted in output stage' >&2 + exit 1 +} +grep -Fq 'generated this run' "$project/build/refc/proven-ffi.c" || { + echo 'FAILED: expected fresh content not staged' >&2 + exit 1 +} +run_case fail 13 'planted RefC compiler failure' +echo 'RefC staging: 4 negative/positive cases passed; no real codegen proof claimed.' From 788daff05899388f98cb6e5592631917dcca5f80 Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:54:57 +0000 Subject: [PATCH 08/11] ci(refc): run adversarial fresh-output staging controls --- .github/workflows/ffi-full-integration.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ffi-full-integration.yml b/.github/workflows/ffi-full-integration.yml index 6f86b5e5..e27fc024 100644 --- a/.github/workflows/ffi-full-integration.yml +++ b/.github/workflows/ffi-full-integration.yml @@ -19,6 +19,7 @@ on: - 'bindings/elixir/native/proven_nif/src/lib.rs' - 'scripts/check-native-nif-exports.sh' - 'tests/check-native-nif-exports.sh' + - 'tests/check-refc-staging.sh' pull_request: branches: [main] paths: @@ -30,6 +31,7 @@ on: - 'bindings/elixir/native/proven_nif/src/lib.rs' - 'scripts/check-native-nif-exports.sh' - 'tests/check-native-nif-exports.sh' + - 'tests/check-refc-staging.sh' workflow_dispatch: concurrency: group: ffi-full-integration-${{ github.ref }} @@ -48,6 +50,9 @@ jobs: # This proves the validator detects missing exports. It does NOT # imply any real libproven artifact was built or validated. run: bash tests/check-native-nif-exports.sh + - name: RefC staging regression controls + # Planted empty/stale compiler outputs must not become fake evidence. + run: bash tests/check-refc-staging.sh idris-refc-zig: name: Idris -> RefC -> Zig From 60b636f1b6b99996c8f0e26fc565e8558267dd1e Mon Sep 17 00:00:00 2001 From: "Jonathan D.A. Jewell" <6759885+hyperpolymath@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:55:36 +0000 Subject: [PATCH 09/11] docs(ffi): record measured Idris2 RefC package scope and fresh-stage controls --- docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc index d63ea8c6..06a187cd 100644 --- a/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc +++ b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc @@ -12,6 +12,29 @@ Rust declares `i32` while Zig returns an optional `*Handle`. These are source observations, not a verified inventory of any generated Idris2 RefC C output. +== Actual Idris2 RefC package scope + +On 2026-11-15 an isolated checkout ran `idris2 --build proven-ffi.ipkg` +and `idris2 --codegen refc --build proven-ffi.ipkg` using Idris2 +0.8.0. Both compiled successfully. The package currently lists +`Proven.Core`, `Proven.SafeUrl.Parser`, +`Proven.FFI.SafeUrl` and `Proven.FFI.Main`; compilation also loads +the imported SafeUrl/Query modules. `Proven.FFI.Main` references +*SafeUrl only*. This package does not presently include an Idris2 FFI +implementation for the Rustler NIF's math, crypto, string and other +API families. A successfully generated `proven-ffi.c` is **not** a +complete 66-function Idris-backed ABI. + +The existing `scripts/build-refc.sh` also previously scanned +`build/exec` and its parent `build` (double-counting the same +source), and could report success even when the compiler produced no +C. It now requires at least one *fresh* generated C source, deduplicates +the scan, and replaces stale staged sources. The four fake-compiler +controls in `tests/check-refc-staging.sh` exercise empty, stale, +fresh and failed generation, while an independent live Idris2/RefC +build has confirmed one generated source file. Neither fixture +testing nor one generated C source proves complete ABI parity. + == A mandatory precondition, not a substitute for proof Run `bash scripts/check-native-nif-exports.sh /path/to/libproven.so` From 8f75c7ffd03d57813646e755b6ab83a9bec25ec8 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:35:50 +0000 Subject: [PATCH 10/11] fix(ffi): handle qualified Rust declarations and default-version symbols in NIF export checks --- scripts/check-native-nif-exports.sh | 13 +++++++++---- tests/check-native-nif-exports.sh | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/scripts/check-native-nif-exports.sh b/scripts/check-native-nif-exports.sh index 31951062..ee0f604c 100644 --- a/scripts/check-native-nif-exports.sh +++ b/scripts/check-native-nif-exports.sh @@ -40,10 +40,10 @@ trap 'rm -rf -- "$tmp"' EXIT awk ' /^[[:space:]]*extern "C"[[:space:]]*\{/ { in_extern=1; next } in_extern && /^[[:space:]]*\}/ { in_extern=0 } - in_extern && /^[[:space:]]*fn[[:space:]]+proven_[A-Za-z0-9_]+[[:space:]]*\(/ { + in_extern && /^[[:space:]]*((pub(\([^)]*\))?|safe|unsafe)[[:space:]]+)*fn[[:space:]]+proven_[A-Za-z0-9_]+[[:space:]]*\(/ { line=$0 - sub(/^[[:space:]]*fn[[:space:]]+/, "", line) - sub(/\(.*/, "", line) + sub(/^[[:space:]]*((pub(\([^)]*\))?|safe|unsafe)[[:space:]]+)*fn[[:space:]]+/, "", line) + sub(/[[:space:]]*\(.*/, "", line) print line } ' "$nif" | LC_ALL=C sort -u > "$tmp/required" @@ -71,7 +71,12 @@ esac # Use only defined symbols; never mistake an undefined (U) reference for # evidence that a function exists. nm --defined-only excludes those. -awk '{ if ($NF ~ /^proven_[A-Za-z0-9_]+$/) print $NF }' "$tmp/nm" | +# Default-version definitions satisfy unversioned consumers; single-@ aliases do not. +awk '{ + symbol=$NF + sub(/@@[^@[:space:]]+$/, "", symbol) + if (symbol ~ /^proven_[A-Za-z0-9_]+$/) print symbol +}' "$tmp/nm" | LC_ALL=C sort -u > "$tmp/defined" LC_ALL=C comm -23 "$tmp/required" "$tmp/defined" > "$tmp/missing" diff --git a/tests/check-native-nif-exports.sh b/tests/check-native-nif-exports.sh index 97f8bb6f..2440b460 100644 --- a/tests/check-native-nif-exports.sh +++ b/tests/check-native-nif-exports.sh @@ -18,7 +18,7 @@ cat > "$tmp/lib.rs" <<'EOF' #[link(name = "proven")] extern "C" { fn proven_init() -> i32; - fn proven_math_div(a: i64, b: i64) -> i64; + pub fn proven_math_div(a: i64, b: i64) -> i64; } fn wrapper_not_a_required_export() {} EOF @@ -50,6 +50,25 @@ expect() { expect 'all exported symbols (shared)' 0 'symbols_present_only' bash "$driver" "$tmp/good.so" "$tmp/lib.rs" expect 'all exported symbols (static)' 0 'symbols_present_only' bash "$driver" "$tmp/good.a" "$tmp/lib.rs" expect 'planted missing symbol is refused' 1 'missing C ABI symbol: proven_math_div' bash "$driver" "$tmp/bad.so" "$tmp/lib.rs" +# Each qualified declaration must still require the missing division export. +for qualifier in 'pub(crate)' 'pub(super)' 'pub(in crate::ffi)' safe unsafe 'pub safe' 'pub(crate) unsafe'; do + printf 'extern "C" {\n %s fn proven_math_div(a: i64, b: i64) -> i64;\n}\n' "$qualifier" > "$tmp/qualified.rs" + expect "qualified declaration ($qualifier)" 1 'missing C ABI symbol: proven_math_div' bash "$driver" "$tmp/bad.so" "$tmp/qualified.rs" +done + +cat > "$tmp/versions.map" <<'EOF' +PROVEN_1 { global: proven_*; local: *; }; +EOF +cc -shared -fPIC -Wl,--version-script="$tmp/versions.map" -o "$tmp/default-version.so" "$tmp/good.c" +expect 'default-version exports satisfy contract' 0 'symbols_present_only' bash "$driver" "$tmp/default-version.so" "$tmp/lib.rs" + +cat > "$tmp/nondefault.c" <<'EOF' +int proven_init(void) { return 0; } +long div_impl(long a, long b) { return b ? a / b : 0; } +__asm__(".symver div_impl,proven_math_div@PROVEN_1"); +EOF +cc -shared -fPIC -Wl,--version-script="$tmp/versions.map" -o "$tmp/nondefault-version.so" "$tmp/nondefault.c" +expect 'single-@ export does not satisfy contract' 1 'missing C ABI symbol: proven_math_div' bash "$driver" "$tmp/nondefault-version.so" "$tmp/lib.rs" expect 'missing library is NOT RUN' 2 'not_run:missing_input' bash "$driver" "$tmp/nonexistent.so" "$tmp/lib.rs" : > "$tmp/empty.rs" expect 'empty consumer contract is NOT RUN' 2 'not_run:empty_contract' bash "$driver" "$tmp/good.so" "$tmp/empty.rs" @@ -57,4 +76,4 @@ expect 'empty consumer contract is NOT RUN' 2 'not_run:empty_contract' bash "$dr # An invalid artifact must not be accepted merely because it has a .so suffix. printf 'not an ELF binary\n' > "$tmp/invalid.so" expect 'unreadable artifact is NOT RUN' 2 'not_run:unreadable_shared_lib' bash "$driver" "$tmp/invalid.so" "$tmp/lib.rs" -echo 'native-nif-symbol-controls: 6 cases passed (actual ABI not yet verified)' +echo 'native-nif-symbol-controls: 15 cases passed (actual ABI not yet verified)' From e757802b8ddf344e69485fd30b7d89e2a3a2ea11 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:44:00 +0000 Subject: [PATCH 11/11] test(refc): verify stale staged sources are removed without --clean --- docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc | 2 +- tests/check-refc-staging.sh | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc index 06a187cd..85976029 100644 --- a/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc +++ b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc @@ -29,7 +29,7 @@ The existing `scripts/build-refc.sh` also previously scanned `build/exec` and its parent `build` (double-counting the same source), and could report success even when the compiler produced no C. It now requires at least one *fresh* generated C source, deduplicates -the scan, and replaces stale staged sources. The four fake-compiler +the scan, and replaces stale staged sources. The five fake-compiler controls in `tests/check-refc-staging.sh` exercise empty, stale, fresh and failed generation, while an independent live Idris2/RefC build has confirmed one generated source file. Neither fixture diff --git a/tests/check-refc-staging.sh b/tests/check-refc-staging.sh index 2ff52142..8a5e429e 100644 --- a/tests/check-refc-staging.sh +++ b/tests/check-refc-staging.sh @@ -38,9 +38,11 @@ chmod +x "$tmp/fake-idris2" run_case() { local action=$1 want=$2 expected=$3 rc=0 + local -a args=(--clean) + if [[ ${4:-} == no-clean ]]; then args=(); fi env IDRIS2="$tmp/fake-idris2" PROVEN_ROOT="$project" \ FAKE_IDRIS_PREFIX="$prefix" FAKE_ACTION="$action" \ - bash "$script" --clean > "$tmp/output.log" 2>&1 || rc=$? + bash "$script" "${args[@]}" > "$tmp/output.log" 2>&1 || rc=$? if [[ "$rc" != "$want" ]] || ! grep -Fq "$expected" "$tmp/output.log"; then printf 'FAILED: action %s expected exit %s and text %q, got %s\n' "$action" "$want" "$expected" "$rc" >&2 cat "$tmp/output.log" >&2 @@ -63,5 +65,11 @@ grep -Fq 'generated this run' "$project/build/refc/proven-ffi.c" || { echo 'FAILED: expected fresh content not staged' >&2 exit 1 } +printf '/* unrelated stale stage */\n' > "$project/build/refc/old.c" +run_case write 0 'Found 1 fresh generated C file(s)' no-clean +[[ ! -e "$project/build/refc/old.c" ]] || { + echo 'FAILED: stale C persisted in output stage without --clean' >&2 + exit 1 +} run_case fail 13 'planted RefC compiler failure' -echo 'RefC staging: 4 negative/positive cases passed; no real codegen proof claimed.' +echo 'RefC staging: 5 negative/positive cases passed; no real codegen proof claimed.'