Code Architecture
Advanced
This document describes the ALVIE/Sancus implementation.
End-to-end pipeline
Section titled “End-to-end pipeline”learn.exe performs one active-learning experiment:
Testdl.Parserparses an enclave specification and an attacker specification.EnclaveandAttackercompile the TestDL syntax into instruction/action templates. The secret placeholder is expanded before learning.Inputgentracks which specification actions are legal after an observed input/output history and proposes the next input symbols.Sancus.Verilogimplements the SUL (system under learning). It generates a program, runs the Sancus Verilog testbench, parses the VCD dump, and turns the trace intoOutput_internalobservations. Compiled simulators are cached across runs, and only the top level of the testbench is traced.Lsharp.LSharplearns a Mealy machine using output queries and an equivalence oracle.Interopserializes the learned machine as a Graphviz.dotfile.fa.execonverts learned machines to LTSs and uses mCRL2 to enumerate distinguishing traces.
The learner is parameterized by the SUL and oracle interfaces.
It keeps caches of apartness, frontier and frontier-to-basis candidates across the run, updated incrementally as the observation tree grows; they rely on the oracles numbering new tree nodes increasingly.
The production configuration in learn.exe uses Sancus.Verilog with one of the oracle implementations in lib/lsharp/oracles/.
Main modules
Section titled “Main modules”| Module | Responsibility |
|---|---|
lib/sancus/testdl.ml | TestDL parser and specification types |
lib/sancus/attacker.ml | Attacker actions, compilation, derivatives, and action sets |
lib/sancus/enclave.ml | Enclave actions, compilation, derivatives, and action sets |
lib/sancus/inputgen.ml | Specification-guided input generation and matchability |
lib/sancus/sul/verilog.ml | Sancus SUL and VCD-to-observation conversion |
lib/sancus/output_internal.ml | Observable output and timing payload types |
lib/lsharp/lsharp.ml | L# learning loop |
lib/lsharp/observationtree.ml | Observation tree and basis/frontier operations |
lib/lsharp/oracles/ | PAC, random-walk, exhaustive, and incremental oracles |
lib/vcd/vcd.ml | VCD parser |
lib/prof/prof.ml | Optional profiler (ALVIE_PROFILE=1) |
lib/ltscomparator/cexfinder.ml | Model conversion and witness extraction |
bin/learn.ml | Experiment orchestration and model writing |
bin/fa.ml | Four-model flow-analysis comparison |
The SUL contract
Section titled “The SUL contract”lib/lsharp/sul.mli defines the backend contract:
clonecreates an independent learning state;preinitializes a fresh experiment;stepapplies one input and returns one output;postreleases backend resources.
step also supports silent and dry_output arguments.
L# uses these to separate observable steps from internal bookkeeping and to avoid rerunning a known output when exploring the observation tree.
The simulator backend keeps a configuration record containing the generated program, the current specification state, labels, and the last instruction number. pre resets that configuration.
The pending labels and the last instruction number are part of the returned output so that dry steps can restore them, but they are bookkeeping: output equality ignores them, so that states are told apart only by what is observed.
When an enclave is resumed with reti, the backend also identifies a segment of execution between two re-entries with an identical segment immediately before it, so that an enclave that can be resumed repeatedly still has a finite model (see --keep-repeated-reentries).
A step updates the specification state, generates the corresponding MSP430 program, runs the simulator, parses the VCD, and analyzes the trace at the instruction boundaries relevant to the input.
Outputs and observability
Section titled “Outputs and observability”The backend returns a list of Output_internal.element_t values plus labels and an execution-time summary.
Outputs distinguish normal, interrupt, reset, illegal, unsupported, and divergent behavior.
Timed payloads contain the cycle count k, interrupt-enable state, the public r4 value, the observed unprotected memory value, the timer counter, and the processor mode.
The payload is deliberately smaller than the simulator state: it represents what the attacker can observe. See the Log and Output Reference for the rendered symbols and payload fields.
Learning and comparison boundaries
Section titled “Learning and comparison boundaries”The learner does not compare secrets directly.
Run it separately for secret 0 and secret 1, with and without interrupt actions, producing four models.
The comparison tool receives the two interrupt-enabled models and the two no-interrupt models.
It removes witnesses already present without interrupts, then reports the remaining flow-analysis violations in <basename>_int.dot.
Each witness pairs two runs, one for each secret, with the same attacker inputs.
The runs are taken from the models as complete paths: a run ends at a reset, a divergence, a state without successors, or a state the same run has already visited, never because another run reached that state first.
This is why a single successful learning run is not a complete attack result: the four-model matrix and the comparison phase are both required.
Extension points
Section titled “Extension points”Add a TestDL action
Section titled “Add a TestDL action”Update the syntax and parser, the attacker or enclave atom type, compilation, derivative/action-set logic, and tests. Follow the Extending TestDL Actions guide.
Add a learning backend
Section titled “Add a learning backend”Implement the SUL signature, keep input and output types comparable and serializable, and instantiate LSharp with the new backend in an executable.
The backend must preserve the observable-output contract; changing payload semantics changes learned models and their comparability.
Add an oracle
Section titled “Add an oracle”Implement the Oracle functor signature in lib/lsharp/oracles/oracle.mli.
The oracle supplies output queries and equivalence queries; it may use Inputgen to stay within the selected TestDL specification.
Build and test
Section titled “Build and test”From alvie/code:
dune buildbash test/cli_diagnostics.shThe Testing ALVIE guide explains each suite, what it needs, and when to run it.
test/cli_diagnostics.sh runs the ALVIE-only diagnostic suite, including TestDL parsing and command-line validation.
It does not require the Sancus simulator or mCRL2.
The isolated feature suite exercises TestDL parsing and derivatives, code generation, specification-guided input generation, observation-tree algorithms, and serialization without external target tooling:
dune exec tt_features -- test --color=nevertt_attack is the simulator-backed regression suite.
It replays the supplied attack traces against expected observations for the relevant Sancus commits, secrets, and interrupt modes.
It requires the external Sancus checkout and its simulator toolchain:
dune exec tt_attack -- test --color=neverThe shell wrappers at repository root build first and place models, logs, and temporary simulator files under results/, logs/, and tmp/.
The sancus-core-gap checkout must be available at the repository root for simulator experiments.
Generated files
Section titled “Generated files”Source and specifications live in alvie/, spec-lib/, and src/.
Learned models, comparison witnesses, logs, and temporary simulator programs are generated artifacts.
Do not use generated .dot, VCD, or temporary files as the source of truth for backend behavior; inspect the OCaml SUL and the simulator/testbench when changing semantics.