diff --git a/.github/workflows/ffi-full-integration.yml b/.github/workflows/ffi-full-integration.yml index 66a4cf4f..65eb1adc 100644 --- a/.github/workflows/ffi-full-integration.yml +++ b/.github/workflows/ffi-full-integration.yml @@ -16,6 +16,10 @@ on: - 'scripts/build-header-refc.sh' - 'scripts/test-refc-staging.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' + - 'tests/check-refc-staging.sh' pull_request: paths: - 'src/**' @@ -25,11 +29,32 @@ on: - 'scripts/build-header-refc.sh' - 'scripts/test-refc-staging.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' + - 'tests/check-refc-staging.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 + - 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 runs-on: ubuntu-22.04 @@ -39,6 +64,53 @@ jobs: 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 "::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 + 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 uses: mlugg/setup-zig@e7d1537c378b83b8049f65dda471d87a2f7b2df2 # v1 with: @@ -54,22 +126,24 @@ jobs: set -euo pipefail source build/refc/sdk.env cd ffi/zig - 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 - - name: Reject stale output and misleading compiler success - run: bash scripts/test-refc-staging.sh - - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4 - if: always() - with: - name: full-refc-build-evidence - path: build/refc/ - if-no-files-found: error + 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.* + - 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" diff --git a/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc new file mode 100644 index 00000000..85976029 --- /dev/null +++ b/docs/ffi/NATIVE-NIF-ABI-CONTRACT.adoc @@ -0,0 +1,80 @@ +// 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. + +== 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 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 +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` +*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. diff --git a/scripts/build-refc.sh b/scripts/build-refc.sh index 9d7036cc..5eaa8a8c 100755 --- a/scripts/build-refc.sh +++ b/scripts/build-refc.sh @@ -31,42 +31,159 @@ 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 - cat "$out/build.log" >&2; exit 1 -fi -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; + +die() { + echo "[build-refc] ERROR: $*" >&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" + +# --------------------------------------------------------------------------- +# 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" + +# 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 +# --------------------------------------------------------------------------- + +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" + +# 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=() + +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 + +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 + +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/" +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" diff --git a/scripts/check-native-nif-exports.sh b/scripts/check-native-nif-exports.sh new file mode 100644 index 00000000..ee0f604c --- /dev/null +++ b/scripts/check-native-nif-exports.sh @@ -0,0 +1,94 @@ +#!/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:]]*((pub(\([^)]*\))?|safe|unsafe)[[:space:]]+)*fn[[:space:]]+proven_[A-Za-z0-9_]+[[:space:]]*\(/ { + line=$0 + sub(/^[[:space:]]*((pub(\([^)]*\))?|safe|unsafe)[[:space:]]+)*fn[[:space:]]+/, "", line) + sub(/[[:space:]]*\(.*/, "", 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. +# 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" + +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.' diff --git a/tests/check-native-nif-exports.sh b/tests/check-native-nif-exports.sh new file mode 100644 index 00000000..2440b460 --- /dev/null +++ b/tests/check-native-nif-exports.sh @@ -0,0 +1,79 @@ +#!/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; + pub 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" +# 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" + +# 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: 15 cases passed (actual ABI not yet verified)' diff --git a/tests/check-refc-staging.sh b/tests/check-refc-staging.sh new file mode 100644 index 00000000..8a5e429e --- /dev/null +++ b/tests/check-refc-staging.sh @@ -0,0 +1,75 @@ +#!/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 + 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" "${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 + 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 +} +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: 5 negative/positive cases passed; no real codegen proof claimed.'