Architecture
This page follows a program through cerastes verify, one stage at a time, and names the source directory of each
stage. The specification of the encoding itself is internal/encoding.md, and the first
section of that file has the same pipeline drawn as a diagram.
C/C++ source, cerastes attributes, <cerastes.h>
EDG front end (cerastes configuration), registered attributes
back end: IL walk and encoder src/backend
VIR: check, lower src/vir
Viper text and position map src/viper
Silicon, Z3 src/driver runs them
errors mapped back to C/C++ lines src/driver
The EDG front end in the cerastes configuration
cerastes reads the program through EDG’s front end, after it has parsed and type-checked it. EDG is not built as a
library by its own CMake, so cerastes compiles EDG’s front-end sources itself, in
cmake/EdgFrontEnd.cmake, with the configuration macros the back end needs. The
decision is 0002.
-
The macros live in one CMake target,
edg_config. Every target that includes an EDG header links it, because the layout of the IL structures depends on those macros. Among them: the IL is not lowered, prototype instantiations of templates and all template information stay in the IL, unrecognized attributes are recorded and nothing unused is pruned. -
EDG is built twice. The stock build, in
build/edg, givescpfe(scripts/cpfe.sh) and the generated headers (error tables) that cerastes reuses. The cerastes configuration is compiled by cerastes’s own CMake, producingbuild/cerastes-feandbuild/cerastes. The stock build is how a front-end question is checked against unmodified EDG behaviour.cmake/EdgFrontEnd.cmakerefuses generated headers that come from a different EDG tree. -
cerastes-feruns the front end and the back end without verifying;cerastes verifyis the verifier. The driver refuses options it cannot support, such as C before C23 and GNU emulation, before EDG runs (the list is at the top ofsrc/driver/main.cpp).
EDG’s own source is not in this repository: it is the submodule third_party/edg, a fork, described in
The EDG fork.
Annotations: registration through the fork
The contracts are attributes in the closed namespace cerastes, which the back end registers with EDG before it
runs (src/backend/register_attributes.cpp). EDG parses and type-checks
their arguments as expressions, so a contract is typed C or C++ code. That needs a general registration
mechanism in EDG, which is why the cerastes series exists on the fork (decisions
0006 to
0009). The checks EDG does not make, such as where an attribute may
appear, are in src/backend/annotation_checks.cpp.
Programs write the annotations through the macros of include/cerastes.h, which expand to the
attributes under the cerastes front end and to nothing under any other compiler, so the same file builds
with ordinary compilers. The user-facing meaning is in
Annotations.
The back end: from the IL to VIR
EDG calls the global function back_end() after the front end finishes without errors
(src/backend/back_end.cpp). Nothing is written to a file: the back end reads the IL in
memory. It does one of two things:
-
With
--cerastes_walk, it prints a deterministic listing of the IL (src/backend/walker.cpp). These listings are the golden tests oftests/il/, andscripts/walker_oracle.pychecks each against an independent print of the same IL (--il_display). -
In verify mode it checks the annotations and calls
verify_encode()(src/backend/encode.cpp), which builds a VIR program.
encode.cpp is where the meaning of the C and C++ program is decided: which constructs are accepted, and the rejection
of every other with a diagnostic (the diagnostics are listed in src/backend/diagnostics.def). The other files of
src/backend are helpers around it: il_query and il_names read the IL, expr_tree reads contract expressions,
render prints entities, verify_config holds the state of a verify run and the classification of files into
verified and system files by path (decision 0011 item 4).
To look at the IL yourself, two commands show it, both after scripts/bootstrap.sh:
scripts/cerastes-fe.sh --c++20 --cerastes_walk path/to/file.cpp
scripts/cerastes-fe.sh --c++20 --il_display path/to/file.cpp
The first is the walker’s listing, the second EDG’s own dump. What each IL fact means to the back end is in
internal/il-inventory.md.
VIR
VIR is the small verification IR that the encoder builds (src/vir/vir.h). It is independent of
EDG: src/vir and src/viper include no EDG header and do not link edg_config. Its types are bool, the
integer types (a value is a mathematical integer in the range of its C type), pointers and structures, and every node
carries the source position it came from, so an obligation can be reported at the right line.
-
check.cpptype-checks a program and fails closed: an operand type that is not exactly what an operation requires is rejected rather than silently converted. -
lower.cppgives the program its integer semantics (decision 0010) and produces the lowered program (lowered.h): the overflow, division and conversion checks become explicit obligations here. -
A structure’s members are enumerated as leaves, and since an array can be a member (decision 0063) there are three views of the list, in
vir.h.struct_object_leavesis the view of an object in memory: a cell for each scalar member and one array leaf for each array member, which is a block of its own.struct_value_leavesis the view of a structure value (a by-value parameter or result, an aggregate): every element of an array member is a leaf of its own, and a value holds at mostkMaxByValueArrayElementsarray elements (a constant invir.h, measured against Silicon’s time).struct_leavesis the old view and returns an array member as one leaf of array type; it is the poison, so that a site written before arrays in structures stops with an internal error on a structure that has one instead of treating the array as a cell. When you add a walk over the members of a structure, choose the object view or the value view. -
An array whose element is a structure (decision 0064) has no cell for an element: a leaf of the object is a leaf path (
object_leaf_paths), the member and index steps from the object to one scalar, and the object view marks a member that is an array of aggregates (aggregate_element) so that no walk reads it as a cell. The encoder reads such an array only where each of its rules has a lowering and a test (an automatic local array, in code); every other array of structures is rejected at its declaration with a diagnostic that names the milestone that lifts it, andcheck_programrefuses it a second time as a backstop. Where you lift a rejection, remove the guard of the walks it protects and keep their self-test cases as positive ones.
encode.cpp and lower.cpp hold all the C and C++ meaning; the emitter decides nothing.
Viper text and the position map
src/viper prints the lowered program as Viper text (emit.cpp) with the small prelude of functions it needs
(prelude.cpp), and writes the position map (posmap.cpp). The map says, for each line of Viper that can fail, which
source position and which error category it stands for; its format is section 5 of the encoding specification. Each
check is on its own line so that it has an entry.
A check of the permission to a whole range of array elements is a marked range row (decision
0059): its entry names the one Silicon reason that row may fail with,
insufficient.permission, and report.cpp accepts exactly that reason on it (and the failed assertion on every other row), so a
missing permission is told apart from a failure Silicon should never report.
The permission atoms over a range of elements (owns_range and reads_range, decision
0060) are the one place a contract holds a quantifier: each atom is one
forall k per field over the slots of the pointer’s block (offset_of(p) <= k < offset_of(p) + n, guarded by the atom’s own
guard), and a call, a return and a framed element read have their own explicit rows, so Silicon’s check of the quantified
permission never fails first. The lint admits exactly these shapes and the quantifiers below, and no other forall.
A user quantifier (cerastes_forall(f, …), decision
0061) is the other: the encoder reads the body of the pure function f at the
site, and lower.cpp writes one forall over the absolute slots of the elements it reads. Each read of the form
p[i], p[i + 1], … is a read class with a binder of its own (q0, q1, …), and the trigger set holds one term per class,
loc(block_of(p), q0). The arguments are evaluated once, before the binder, so their own checks are rows of the site. A
quantified clause is split per conjunct in a contract, at a call, at a return and in a loop invariant, and every check the body
needs (the permission to the elements, the bound of a named array, the flag that an element has a value, an overflow in the
body) is a row over the binder, reported at the cerastes_forall with a note at the operation in the function. A function’s
well-formedness method (f__wf) stays free of heap permission: a read under a binder is a ghost function of the pointer
parameter and the state, hvq_<method>__<parameter>_pre or _post, declared in a domain of its own with no axiom and
related to the scalar ghosts and to the element type’s range by assumed facts whose exact shapes the lint pins. In a loop
invariant and in a check the quantifier’s pointer argument is a pointer variable or a named local array. What is not read is
rejected with a message naming the session that will read it (the encoding specification lists them).
A pure function that an annotation applies, or that carries a contract, is the third (decision
0062). The encoder reads it once (read_pure in encode.cpp): its signature, its
contract, and its one return E;, which it encodes from a copy of the expression whose parameters are contract references, so that E follows
exactly the rules of a requires clause. In VIR the function carries pure and pure_body, and a call of it in an annotation is an
Apply node, which counts as a read of memory wherever a rule asks for an expression that reads none. lower.cpp writes a Viper
function pf_<name>__<id> (printed before the methods, with its contract, its measure and a body whose recursive calls each begin a line)
and, for every read function, a definition-check method pf_<name>__<id>__wf that asserts every obligation of the body, the ensures and
the measure, reported in the function at the operation. Silicon’s own check of the function proves the ensures and the termination;
those two rows are the only ones a user sees from it, and the position map says so. An application is (guard ? pf_g(args) : default) in a
contract, so that Viper’s check of the contract can never fail on it, and a row at each application (the permission to the places the callee
reads, then the pure lines and value clauses of its requires) says what the guard assumed. In a well-formedness method an application is a ghost
function hva_<method>__<callee>_<pre|post> of the method’s domain, related to the callee’s ensures and to the value it had on entry by assumed facts
the lint pins. The prelude’s functions print an empty decreases line in a program that holds a pf_ function, and nowhere else.
In a check the rows come first: the permission to the places the callee reads, then the callee’s pure lines and value clauses as precondition
rows, then the bound of a named array and the flag that every element read has a value, and only then the fact itself, so that a missing permission
is told apart from a wrong value (a failed row is assumed afterwards, so Viper’s own check of the application is silent). In a loop invariant the
application also has a guard, as in a contract (the places the loop holds contain what the callee reads, and its value clauses hold), but the rows still come
first: the framing, that the loop’s places contain the interval the callee reads, is asserted at entry and at the back edge, then the callee’s pure lines, the
bound and the flags as invariant lines. The guard keeps Viper’s own check of the application silent, not the user’s: where it is false the clause is read with a
default value, and a clause that the default breaks is reported as the invariant failing to be preserved, beside the framing row.
The invariant of a do … while (0) loop is no loop: its application is checked like a check and the fact is invariant-entry.
To keep the Viper of a run:
build/cerastes verify path/to/file.c --keep-vpr
This writes <stem>.vpr and <stem>.vpr.map in the current directory, where <stem> is the source file’s name without
its directory and suffix (or <path> and <path>.map with --keep-vpr=<path>).
build/cerastes-vir-selftest <dir> builds VIR programs by hand and writes the Viper for them, which
scripts/check.sh verifies; it tests lowering and the emitter before any front end is involved.
The driver
src/driver/verify_main.cpp implements cerastes verify <file> [options] (cerastes
--help lists them: include and macro options, --std=c++20|c23, --keep-vpr, --timeout, --strict-init, --strict-owner). It runs
EDG in process, lets the back end encode, then runs Silicon as a child process (silicon.cpp) and reports.
-
The tools are fixed when the build is configured: the paths of the JDK 17,
silicon.jarand Z3 are compiled in from whatscripts/bootstrap.shfound (tool_paths.h.in), so a stray environment variable cannot change a result. -
silicon_output.cppreads Silicon’s output;report.cppmatches each Silicon error to its position-map entry and prints it as a line of the form<file>:<line>: error: <category>: <message>. Errors are always reported at source lines, never at lines of the Viper (invariant 7). -
Exit status: 0 verified, 1 verification errors, 2 the program is not accepted (a front-end error or a rejection), 3 a tool failure. Decision 0011 has the rest of the contract.
cerastes --selftest runs the driver’s own unit tests.
vlibc
A call is checked against the callee’s contract, and a system header’s functions have none. third_party/vlibc
(cerastes-verifier/vlibc, a submodule) supplies them: contract headers that cerastes puts first in the search path in
verify mode, so a plain <stdlib.h> reads vlibc’s declarations of abort, exit, malloc, free and the like. A
declaration there is trusted if it carries a cerastes:trusted-contract tag. Decisions
0030,
0046 and
0048 say how it is pinned and trusted, and
0050 how vlibc’s own allocator (vlibc_malloc and the rest, in
vlibc’s src/, built as libvlibc) is read: scripts/vlibc_contracts.py checks that each vlibc_ contract is its
standard name’s word for word and writes the pairing into the generated name list, from which the encoder takes a
heap block’s allocator (the token’s value in the Viper); and
Soundness summarises what that means for a verdict. The user-facing side is
vlibc in the verifying page.