Skip to content

feat(codegen): Kani harnesses for generated-code AADL contract preservation#224

Open
avrabe wants to merge 2 commits into
mainfrom
feat/v0.10.x-kani-codegen-contracts
Open

feat(codegen): Kani harnesses for generated-code AADL contract preservation#224
avrabe wants to merge 2 commits into
mainfrom
feat/v0.10.x-kani-codegen-contracts

Conversation

@avrabe
Copy link
Copy Markdown
Contributor

@avrabe avrabe commented May 14, 2026

Summary

This PR adds spar's Logika-equivalent strategy for generated-code correctness: instead of importing a new prover language, three Kani bounded model-checking harnesses give machine-checked proofs that each codegen pass preserves the source AADL contract.

  • prove_thread_period_preserved — for any Period p ∈ (0, 1_000_000_000] ns, the emitted dispatch-metadata string round-trips back to exactly p (no truncation, no off-by-one). Uses kani::any() over the full nanosecond range; kani::assume(period_ns > 0 && period_ns <= MAX_PERIOD_NS).
  • prove_port_direction_preservedOut source maps exclusively to a WIT setter; In sink to a getter; a well-formed Out→In connection always produces a complementary setter+getter pair; same-direction features never produce a complementary pair. Proves the AADL §9 directionality contract over all direction combinations.
  • prove_access_right_preservedAccess_Rights = Read_Only never produces &mut in the generated access shim; Read_Write always does. Proves read-only enforcement at the Rust type level.

Wire-up

  • crates/spar-codegen/tests/kani_contracts.rs — three #[cfg(kani)] #[kani::proof] harnesses
  • crates/spar-codegen/Cargo.tomlkani-harnesses feature flag (no-op at runtime; enables cargo build -p spar-codegen --features kani-harnesses for CI compilation gating)
  • .github/workflows/ci.yml — CI Kani job extended to run each harness by name
  • artifacts/requirements.yamlREQ-KANI-CODEGEN-001 (implemented, tags: codegen/kani/verification/v0100/safety)
  • artifacts/verification.yamlTEST-KANI-CODEGEN (passing, satisfies REQ-KANI-CODEGEN-001)

Design note

Kani cannot symbolically construct SystemInstance values (they embed la_arena::Idx handles requiring a live arena). Following the pattern established in kani_codegen.rs and kani_solver.rs, each harness models the pure functions the codegen calls and asserts the invariant that pass must satisfy. Any divergence between the Kani model and production would be caught by the existing unit + golden tests.

Test plan

  • cargo build -p spar-codegen --features kani-harnesses — compiles clean
  • cargo test -p spar-codegen — 19 unit tests pass, harness files load without error
  • cargo kani --tests -p spar-codegen --harness prove_thread_period_preserved (CI)
  • cargo kani --tests -p spar-codegen --harness prove_port_direction_preserved (CI)
  • cargo kani --tests -p spar-codegen --harness prove_access_right_preserved (CI)
  • rivet validate — no new errors (pre-existing YAML parse issue at verification.yaml:1648 is unrelated)

🤖 Generated with Claude Code

…vation

Add three #[kani::proof] harnesses in crates/spar-codegen/tests/kani_contracts.rs
that prove each codegen pass preserves the AADL source contract (spar's
Logika-equivalent strategy: machine-checked proofs on the generated-code path).

- prove_thread_period_preserved: for any Period p in (0, 1_000_000_000] ns,
  the emitted dispatch-metadata string round-trips back to exactly p (no
  truncation, no off-by-one)
- prove_port_direction_preserved: Out→In connections produce complementary
  WIT setter+getter pairs; same-direction connections never produce a
  complementary pair (AADL §9 directionality contract)
- prove_access_right_preserved: Access_Rights = Read_Only never produces
  &mut in the generated access shim; Read_Write always does (type-level
  read-only enforcement)

Wire-up: add kani-harnesses feature flag to spar-codegen/Cargo.toml; extend
CI Kani job to run each harness explicitly; add REQ-KANI-CODEGEN-001 +
TEST-KANI-CODEGEN to artifacts YAML.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@avrabe avrabe enabled auto-merge (squash) May 14, 2026 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant