Issue #1593: Write down the semantics of the reduced Nikaia subset that copy-and-patch stencils are proved against
Open and answer this issue on GitHub.
emitter, blocked, future · opened 2026-11-14 · updated 2026-11-21 · 1 comment
The goal
#1235 may build a copy-and-patch backend: machine code made by copying precompiled pieces (“stencils”, one per construct and operand shape, with holes for registers, constants, addresses and jump targets) and patching the holes. This issue records how that backend is verified, so the plan is not lost. It is filed for the future (owner, 2026-11-14): it is not an open decision, it waits for the backend, and it changes no part of the specification now.
The idea: a copy-and-patch backend is built from a fixed, finite set of pieces. So it can be verified once, piece by piece, instead of checking every program it compiles. Together with what Nikaia already proves about a program, the result is that the machine code behaves as the checked program says.
Not decided here, on purpose: whether the Rust backend ever gets an outside verifier (Kani, Creusot, Verus, Aeneas/Lean or another method). That stays open; this issue is about the copy-and-patch backend only.
Where it belongs
- Waits on #1235 (the backend itself, at least its step 7 prototype, and the reduction of the checked tree that both emitters read; see the section on it there).
- Related: #796 (decision basis for a formally verified compiler; this is a route to its item 14 for one backend), #1104 (the certificate checker free of run-time errors, which this plan trusts),
docs/formal-verification.md items 10 and 12.
Nikaia has no IR on purpose. The stencils are chosen from the checked tree, after it has been reduced and optimised as #1235 describes, onto a subset of Nikaia itself. So the prover and the emitter work on the same tree, and there is no gap between where a program is proved and what the backend reads. The semantics this plan needs is the semantics of that reduced subset, not of a second language.
What is to be proved: five obligations
- A formal semantics of the reduced subset of Nikaia that the stencils are chosen from: what each construct means, step by step. This is the large intellectual piece; it is kept small by keeping the subset small (#1235).
- Each stencil does what its construct means, for every value in its holes. The stencil’s finished machine code is run symbolically against a formal model of the processor (the official Sail models for Arm and RISC-V, symbolic evaluation with Isla), with the holes kept symbolic; a solver shows over bitvectors that the result matches the semantics of 1. Stencils are short and nearly branch-free, so each goal is small. Because the finished code is checked, the C compiler that produced the stencils (clang) is not trusted.
- The stencils fit together. A relation is fixed between the program’s values and the machine state (“value v is in register r or stack slot s”); every stencil must keep it from its entry to its exit. Correctness of any sequence of stencils then follows by induction: one small general proof (the simulation argument known from CompCert); the work per construct is all in 2.
- The patcher is right. It copies bytes and writes values into holes of each relocation kind (absolute 64-bit, relative 32-bit, …): every value fits its field (a
rel32 jump reaches only so far), every offset stays inside the buffer. A few hundred lines, written in Nikaia and proved by Nikaia’s own prover.
- The choice of stencil is right. The table “construct and operand shape → stencil variant” chooses a variant only where it applies; small, checked in full.
Proofs checked by our own checker
The goals of 2 can be bit-blasted to SAT, and the solver asked for an LRAT proof. The certificate checker already replays LRAT-like clause steps (ADR-270 D5). So the stencil proofs can be checked by our own small checker, without trusting the solver that found them.
What the result says, and what it does not
- Says: the machine code the copy-and-patch backend writes for a program means what the checked, reduced program means; with Nikaia’s own proofs about that program (indexes, arithmetic,
asserts, preconditions) these hold of the machine code too.
- Trusted: the processor model (Sail), our certificate checker, the semantics of 1 as written, the OS and loader.
- Open, to be stated with the claim:
- the step from the full checked tree to the reduced subset: each reduction and optimisation rule needs its own proof, rule by rule, or is re-checked by the checker and the prover on its output (#1235);
- every processor architecture separately: Arm and RISC-V have official Sail models; for x86 only partial models exist (from memory, to be checked);
- concurrency (threads, atomics, the memory model): research, not covered by 3 as stated;
- calls from stencils into the run-time part of
std and into foreign code: hold only under those functions’ contracts, which the claim names;
- for use as a JIT: write-xor-execute memory and, on Arm, flushing the instruction cache belong to the patcher’s obligations.
Done when
The semantics of the reduced subset of Nikaia (obligation 1) is written down construct by construct, covering at least the constructs the step 7 prototype of #1235 covers, as the reference the stencil proofs of #2549 state their goals against; the report gives a Found: line per finding.
To build (once #1235 has the backend)
- Write down the semantics of the reduced subset (obligation 1), construct by construct, as the reference the stencil proofs are stated against; first the constructs the step 7 prototype of #1235 covers.
The other steps moved to their own issues (see Split below): the stencil prover and its replay (old 2, 3), the relation (old 4), the patcher and the table (old 5, 6), the build refusing an unproved stencil (old 7).
Files and limits
- New: the semantics text, the
tools/*.nika of 2, 4 to 6, their tests. No new Rust (CLAUDE.md, Written in Nikaia only).
- Must not change: the Rust backend and its output; this plan does not touch it.
- Limits: the processor models are outside code (Sail, Isla); how they are brought into the build is decided at step 2.
Ruled out
- Verifying each program’s machine code instead of the backend once: the work would grow with every program; the finite stencil set is what makes this backend verifiable.
- Trusting clang’s output for the stencils: the finished machine code is checked instead (obligation 2).
- An intermediate representation of our own as the source of the stencils: Nikaia has none on purpose; the reduced subset of Nikaia takes its place.
References
- Sail (ISA specification language and the Arm and RISC-V models): https://github.com/rems-project/sail
- Isla (symbolic evaluation of Sail models): https://github.com/rems-project/isla
- CompCert’s simulation argument: https://arxiv.org/pdf/0902.2137
Split (2026-11-21)
- #2549 Prove each copy-and-patch stencil against the processor’s Sail model, the proof replayed by our certificate checker
- #2551 Prove that copy-and-patch stencils fit together: every stencil keeps the value-to-register relation
- #2550 Prove the copy-and-patch patcher and the stencil table (relocations fit, the right variant is chosen)
- #2553 The copy-and-patch stencil build refuses a stencil without a checked proof
Split into run-sized issues (2026-11-21)
Found built: nothing. The copy-and-patch backend does not exist yet (#1235 is a note, no stencil in the tree, git log --grep stencil shows only a supervisor note), so every step still waits on it.
Where the steps went:
- Step 1, the semantics of the reduced subset: stays here.
- Steps 2 and 3, the stencil prover against the Sail model and the replay by the certificate checker: #2549.
- Step 4, the relation every stencil keeps: #2551 (after #2549).
- Steps 5 and 6, the patcher and the stencil table: #2550.
- Step 7, the stencil build refuses an unproved stencil: #2553 (after the three above).
All parts keep blocked and Needs #1235.
Generated by Claude Code
Back to Open Issues