Skip to content
You are reading the current documentation of ALVIE: 2026.10. Other versions: development version, 2026.09 (extended documentation), 2026.09 (as released)

Code Architecture

Advanced

This document describes the ALVIE/Sancus implementation.

learn.exe performs one active-learning experiment:

  1. Testdl.Parser parses an enclave specification and an attacker specification.
  2. Enclave and Attacker compile the TestDL syntax into instruction/action templates. The secret placeholder is expanded before learning.
  3. Inputgen tracks which specification actions are legal after an observed input/output history and proposes the next input symbols.
  4. Sancus.Verilog implements the SUL (system under learning). It generates a program, runs the Sancus Verilog testbench, parses the VCD dump, and turns the trace into Output_internal observations. Compiled simulators are cached across runs, and only the top level of the testbench is traced.
  5. Lsharp.LSharp learns a Mealy machine using output queries and an equivalence oracle.
  6. Interop serializes the learned machine as a Graphviz .dot file.
  7. fa.exe converts 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/.

ModuleResponsibility
lib/sancus/testdl.mlTestDL parser and specification types
lib/sancus/attacker.mlAttacker actions, compilation, derivatives, and action sets
lib/sancus/enclave.mlEnclave actions, compilation, derivatives, and action sets
lib/sancus/inputgen.mlSpecification-guided input generation and matchability
lib/sancus/sul/verilog.mlSancus SUL and VCD-to-observation conversion
lib/sancus/output_internal.mlObservable output and timing payload types
lib/lsharp/lsharp.mlL# learning loop
lib/lsharp/observationtree.mlObservation tree and basis/frontier operations
lib/lsharp/oracles/PAC, random-walk, exhaustive, and incremental oracles
lib/vcd/vcd.mlVCD parser
lib/prof/prof.mlOptional profiler (ALVIE_PROFILE=1)
lib/ltscomparator/cexfinder.mlModel conversion and witness extraction
bin/learn.mlExperiment orchestration and model writing
bin/fa.mlFour-model flow-analysis comparison

lib/lsharp/sul.mli defines the backend contract:

  • clone creates an independent learning state;
  • pre initializes a fresh experiment;
  • step applies one input and returns one output;
  • post releases 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.

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.

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.

Update the syntax and parser, the attacker or enclave atom type, compilation, derivative/action-set logic, and tests. Follow the Extending TestDL Actions guide.

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.

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.

From alvie/code:

Terminal window
dune build
bash test/cli_diagnostics.sh

The 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:

Terminal window
dune exec tt_features -- test --color=never

tt_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:

Terminal window
dune exec tt_attack -- test --color=never

The 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.

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.