Skip to content

[anneal][mir] Add byte-level memory and indirect locals - #3977

Draft
joshlf wants to merge 1 commit into
joshlf-anneal-mir-completeness-02-exec-machinefrom
joshlf-anneal-mir-completeness-03-memory-model
Draft

joshlf wants to merge 1 commit into
joshlf-anneal-mir-completeness-02-exec-machinefrom
joshlf-anneal-mir-completeness-03-memory-model

Conversation

@joshlf

@joshlf joshlf commented Nov 20, 2026

Copy link
Copy Markdown
Member

Authored by an AI agent acting on joshlf's behalf.

Human review surface

  • Pass 1 Lean TCB (anneal/trusted/lean/):
    • anneal/trusted/lean/Trusted/MirModel.lean: Added MemPlace, Option 1A three-constructor LocalSlot (dead | immediate | indirect), and Query memory model alignment notes (verbatim pinned rustc quotes).
    • anneal/trusted/lean/Trusted/MirSemantics/Layout.lean (new, candidate:mir_semantics): Target endianness (TargetEndian), profile-v1 byte order (profileTargetEndian := .big), LayoutData, intLayout (with natural vs. lean-miri RuntimeTargetDataLayout.default alignment documentation), and layoutOfTy (gating admitted enums via enumAdmitted). Placed in Trusted.MirSemantics.* so verifyRefEvaluatorIndependence bars M_ref from importing or referencing M_anneal layout rules.
    • anneal/trusted/lean/Trusted/MirSemantics/Memory.lean (new, candidate:mir_semantics): Deterministic byte-level MemoryState with monotonic AllocId (id 1 reserved in entryMemory for the return place), allocateStack, deallocateStack, readBytes, writeBytes, and writeUninit with liveness, bounds, alignment, and mutability checks.
    • anneal/trusted/lean/Trusted/MirSemantics/Bytes.lean (new, candidate:mir_semantics): Endian-parameterized integer byte codec (encodeUInt/decodeUInt), type-checked encodeVal/decodeVal (gating encodeVal on checkVal and moving evalConstLit here so constant and memory reads share validity checks), and typed loadIndirect/storeIndirect/deinitIndirect.
    • anneal/trusted/lean/Trusted/MirSemantics/{Enum,State,Step,CanaryFixtures}.lean and Trusted/MirSemantics.lean: Threaded State.mem, indirect reads/writes/move deinitialization (tyOk type check before indirect memory access matching lean-miri's OperandMachine.evalOperand -> MemoryOperand.readImmediate / Place.copyRuntimePlaceNoValidate fault precedence), StorageLive/StorageDead stack deallocation, indirect Return, forceAllocation, and new locked CanaryMemoryLifecycleAnnealClaim (Pass 1 canaries 19 -> 20).
  • Pass 2 Correspondence TCB (anneal/trusted/correspondence/):
    • anneal/trusted/correspondence/Trusted/Correspondence/{State,Simulation,CanaryFixtures}.lean and Trusted/Correspondence.lean: Mapped .indirect in slotToMiriLocalValue, pinned s.mem = entryMemory in SimulationInv (excluding .indirect from SlotMatchesTy, as .indirect is unreachable in profile v1 on both machines), and added locked BytesCodecMatchesMiriClaim (Pass 2 claims 33 -> 34) with an explicit ∃ rawAlloc, Allocation.tryNew ... = .ok rawAlloc anti-vacuity witness cross-checking encodeVal/decodeVal against Miri.Memory.Allocation.{writeScalar,readScalar,writeUninit} for .int.
  • Auditor & negative tests (anneal/trusted/audit/{TrustedBoundary.lean,src/lib.rs,tests/boundary_audit.rs}):
    • Locked CanaryMemoryLifecycleAnnealClaim (reqConsts incl. forceAllocation, MemoryState, allocateStack, deallocateStack, loadIndirect, storeIndirect, deinitIndirect, readBytes, writeBytes, writeUninit) and BytesCodecMatchesMiriClaim (reqConsts incl. encodeVal, decodeVal, profileTargetEndian, Allocation.{writeScalar,readScalar,writeUninit}), with matching fixtures and 2 negative tests. No allowlists relaxed.
  • Governing & design docs (anneal/docs/{trust-model,mir-correspondence-design}.md):
    • Documented Option 1A memory model, evalOperandToSlot two-stage fault precedence, RuntimeTargetDataLayout.default / Align.ONE alignment notes, and updated locked Pass 1/2 counts (and fixed stale vendor decl count in mir-correspondence-design.md).
  • Gate 5 reviewer disclosures:
    • forceAllocation and allocateStack are reachable from the Pass 1 root set solely via the audit-locked CanaryMemoryLifecycleAnnealClaim root (evalStep never calls them in profile v1).
    • SimulationInv pins s.mem = entryMemory and excludes .indirect slots (SlotMatchesTy requires .immediate _, preserving soundness on all Day-1 reachable programs; indirect lockstep is deferred to PR 05 when projections/references make .indirect reachable).
  • Intentional policy relaxations: none (all TCB and auditor invariants are strictly preserved or strengthened).

Summary

Stacks on #3972 (joshlf-anneal-mir-completeness-02-exec-machine) to introduce Option 1A byte-level stack memory (MemoryState), target layout (Trusted.MirSemantics.Layout), endian-parameterized value byte serialization (Bytes.lean), three-constructor LocalSlot (dead | immediate | indirect), and forceAllocation (Step.lean).

All 21 prior Pass 1 MirWp claims, all 33 prior Pass 2 claims, the new Pass 1 CanaryMemoryLifecycleAnnealClaim, and the new Pass 2 BytesCodecMatchesMiriClaim compile and replay cleanly under anneal-audit --require-correspondence with zero sorry.

Previously, Trusted.MirSemantics kept every live local as an immediate
value (LocalSlot.live) and had no memory model, so it could not
represent a local whose bytes live in a stack allocation. rustc and
lean-miri move a local into memory (force_allocation) as soon as a
place expression needs its address, and the projections, references,
and calls that later PRs admit all depend on that representation.

Mirror lean-miri's LocalValue one constructor to one constructor
(LocalSlot.dead | immediate | indirect), carry a deterministic
byte-level MemoryState in State, and add forceAllocation (paths below
are relative to anneal/):

- trusted/lean/Trusted/MirSemantics/Layout.lean (new): TargetEndian,
  the profile-v1 byte order (big-endian, matching the data layout that
  M_ref passes to lean-miri), LayoutData (size and alignment), and
  layoutOfTy for the Day-1 local types (none for enums that
  enumAdmitted rejects). These are M_anneal's own choices, so they sit
  outside Trusted.MirAst*, which the reference evaluator may import.
- trusted/lean/Trusted/MirSemantics/Memory.lean (new): MemoryState with
  a monotonic allocation-id counter (id 1 reserved for the caller-owned
  return place, as in lean-miri's callMain) and per-allocation bytes,
  alignment, mutability, and liveness; allocateStack, deallocateStack,
  readBytes, writeBytes, and writeUninit with rustc's liveness, bounds,
  alignment, and mutability checks.
- trusted/lean/Trusted/MirSemantics/Bytes.lean (new): endian-
  parameterized integer byte codec (write_target_uint /
  read_target_uint), encodeVal / decodeVal for Day-1 values (decoding
  through evalConstLit, which moves here so constants and memory reads
  share validity checks; encodeVal rejects values that fail checkVal),
  and typed loadIndirect / storeIndirect.
- trusted/lean/Trusted/{MirModel,MirSemantics/{State,Step,Enum}}.lean:
  MemPlace, the three-constructor LocalSlot, State.mem, indirect reads,
  writes, move deinitialization, StorageLive/StorageDead deallocation
  (deallocate_local), indirect Return, and forceAllocation. As in
  lean-miri, an operand's static type check (primitive readability, or
  equality with the destination type) runs before an indirect local's
  bytes are read or deinitialized, so an ill-typed use is unsupported
  rather than UB; evalOperandToSlot takes the check as tyOk.

No profile-v1 statement forces an allocation (projections,
address-of, and calls are unsupported), so neither machine creates an
indirect local on any execution from entry. Two new locked claims keep
the new definitions exercised and cross-checked:

- CanaryMemoryLifecycleAnnealClaim (Pass 1, 19 -> 20 canaries) applies
  forceAllocation between steps of concrete programs and pins, by
  kernel evaluation, the allocation id, the stored big-endian bytes,
  indirect reads, writes, moves, discriminant reads, and returns,
  StorageLive and StorageDead deallocation (a later read through the
  stale place is UB), the UB of reading uninitialized indirect bytes,
  and the precedence of type checks over memory reads.
- BytesCodecMatchesMiriClaim (Pass 2, 33 -> 34 claims) proves that its
  fresh lean-miri allocation exists and, for every integer width and
  bit pattern, that encodeVal produces exactly the bytes lean-miri's
  Allocation.writeScalar stores, that readScalar and decodeVal read the
  same value back, and that uninitialized bytes are rejected by both.

SimulationInv now pins State.mem to entryMemory and slotToMiriLocalValue
lifts .indirect places to LocalValue.live (.indirect _), while
SlotMatchesTy excludes .indirect slots because they are unreachable in
both machines; the indirect lockstep proof belongs to the first PR that
makes them reachable. theory/Anneal/MirWp, theory/Anneal/PureLift/
MirBridge.lean, and correspondence/ are updated for the new State and
LocalSlot shape; all previously locked Pass 1 and Pass 2 claims keep
their statements. trusted/audit/{TrustedBoundary.lean,src/lib.rs,
tests/boundary_audit.rs} lock the two new claims with negative tests,
and docs/{trust-model,mir-correspondence-design}.md record Option 1A
and fix stale claim counts.

No trust-boundary checks or auditor allowlists are relaxed.

Agent-authored-by: AI agent acting on joshlf's behalf
@joshlf
joshlf added this pull request to stack #3973 November 20, 2026 18:05
@joshlf

joshlf commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Authored by an AI agent acting on joshlf's behalf.

Review Receipt (Gate 1)

  • Reviewed range: target_base = merge_base = 0a65bcc51d5a80d8973c2764597e62a9167c95e2 ([anneal][mir] Use ExecutionMachine.step and choice-coupled claims #3972), cumulative from origin/main (1962a9c0e3eda8a3a58387f61c70ddd2c396b118), head = c52834c383acff50182240b3423f7b636d33632a (joshlf-anneal-mir-completeness-03-memory-model), 31 paths under anneal/.
  • Passes & reviewers: Two rounds of independent read-only Pass 1, Pass 2 (trust-review, including verbatim rustc source verification across all 27 quote blocks and TCB reachability tracing), and general change-review across two distinct model families (inspecting read-only via git from clean base, never entering candidate worktrees).
  • Findings & resolution:
    • Round 1 identified 1 blocking fault-precedence defect (B1: evalOperandToSlot eagerly read/deinitialized .indirect memory before evalPrimitiveOperand / evalRvalue .use checked static type compatibility) plus non-blocking hardening items (Layout.lean sitting under Trusted.MirAst* instead of Trusted.MirSemantics*, defensive encodeVal/layoutOfTy checks, BytesCodecMatchesMiriClaim anti-vacuity witness, two non-verbatim rustc quotes in MirModel.lean, stale canary counts in MirWp.lean/design doc, alignment doc note, and broader indirect lifecycle canary cases).
    • All resolved in c52834c383 and re-verified. Round 2 verdict: Unanimous APPROVE (0 blocking findings, 0 open trust questions).
  • Deterministic checks executed on c52834c383 (clean tree):
    • cargo run --locked -p anneal-audit -- --require-correspondence --miri-cache-dir <cache> --json: exit 0 (replay: true, replayedDeclarations: 5164, kernelDeclarations: 1514, axioms: [propext], miriReplay: true, miriReplayedDeclarations: 38728, miriClosureModules: 160, correspondenceReplay: true, correspondenceReplayedDeclarations: 2202/2202).
    • Mechanical TCB reachability closure over audit inventory dependencies: 57 roots; 353/353 user-written trusted/lean/ declarations reached (100%).
    • cargo test --locked -p anneal-audit: 61/61 passed (19 unit, 38 boundary_audit including both new PR 3 negative tests, 4 cli_environment).
    • Guard-sensitivity probe: removing forceAllocation and encodeVal from TrustedBoundary.lean reqConsts caused enforces_mir_wp_canary_claim_proof_rules and enforces_correspondence_non_backsliding to fail (restored cleanly); wrong alloc ID / byte order / removed tyOk check fails kernel rfl on CanaryMemoryLifecycleAnnealClaim.
    • cargo clippy --locked -p anneal-audit --all-targets -- -D warnings (0 warnings), cargo fmt -- --config-path /dev/null --check (clean), ./ci/check_todo.sh (root and anneal/), sha256sum --check --strict trusted/root-manifest.sha256, ./ci/check_job_dependencies.sh, ./ci/check_actions.sh, ./ci/check_non_stable_version.sh, python3 -B -m unittest discover -s ci -p 'test_*.py' (67/67 passed).
  • Checks not executed locally (gated on GitHub CI): nix build .#omnibus-archive, cargo test --workspace, and installed_smoke.
  • Remaining gates: Gate 2 (Codex review), Gate 3 (GitHub CI), Gate 5 (human review & merge).

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-20T18:07:43.732681Z c52834c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: c52834c383

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.99%. Comparing base (0a65bcc) to head (c52834c).

Additional details and impacted files
@@                               Coverage Diff                               @@
##           joshlf-anneal-mir-completeness-02-exec-machine    #3977   +/-   ##
===============================================================================
  Coverage                                           92.99%   92.99%           
===============================================================================
  Files                                                  16       16           
  Lines                                                2469     2469           
===============================================================================
  Hits                                                 2296     2296           
  Misses                                                173      173           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

2 participants