DEVELOPMENT EDITION · 769442725406
Verification Manual
On this page

Proof enclaves

Ownership keeps every structure a tree: one owner, borrows and observers that end. A doubly linked list, a graph with shared vertices or a table probed in place are not trees. A proof enclave lets a package keep such a representation behind a fence, prove its own bodies against a logical model with a heap logic, and give clients an ordinary value-like API that they reason about through the model alone.

This chapter covers how to write an enclave, what the fence checks, what the heap engine proves, and what stays trusted.

A package that is an enclave

A package becomes an enclave through Verada's tool annotation. Its pointers live in its private part, its public types are limited private, and each has a ghost model:

package Stacks
  with Annotate => (Verada, Enclave)
is
   type Stack is limited private
     with Default_Initial_Condition => Size (Stack) = 0;
   function Model (S : Stack) return Natural with Ghost;
   function Size (S : Stack) return Natural
     with Post => Size'Result = Model (S);
   procedure Push (S : in out Stack; X : Integer)
     with Pre => Size (S) < 1000, Post => Size (S) = Size (S)'Old + 1;
   procedure Pop (S : in out Stack; X : out Integer)
     with Pre => Size (S) > 0, Post => Size (S) = Size (S)'Old - 1;
private
   type Node;
   type Node_Ptr is access Node;
   ...

The full example, with its type invariant over a ghost sequence of nodes and a footprint, is test/programs/run/enclave-stack. Varc writes @Annotate(Verada, Enclave). Vibe writes enclave() first in a module, with Ref[T], Seq[T], Cells[...] and __invariant__ (see the Vibe enclaves chapter). Each translates to the others with veradic translate.

The fence

The fence is a set of legality rules, each with a code, that keep the enclave's pointers from escaping and its invariant from being observed broken:

Code What it rejects
B2017 a type holding the enclave's pointers that is not limited private
B2018 an enclave type without a ghost Model or a Type_Invariant
B2019 an access type or access result in the visible part
B2020 a cell that holds what a cell may not (a limited, owning or enclave object)
B2021 an enclave variable outside an Abstract_State
B2022 an enclave type crossing to foreign code, or volatile, or at an address
B2023 a call out of the enclave to a subprogram that reaches its state or objects
B2024 a call out given an enclave object other than as in
B2025 Exceptional_Cases on a public operation
B2026 X'Old as the argument of a function over the enclave's cells

Within the fence, ownership checking is off for the enclave's own access types only. Every other type in the body is checked as usual.

What the heap engine proves

Each operation is proved against its contract on the model, with the type invariant assumed on entry and proved on exit. Each object of an enclave type carries a ghost footprint, a Verada.Footprints set of the cells it owns, and the engine proves the frame obligations:

Code Obligation
B5116 a cell read is in the footprints of the objects the operation was given
B5117 a cell written is in a writable footprint
B5118 a footprint grows only by fresh cells
B5119 two objects' footprints share no cell

Clients therefore see an operation change only the objects it was given. A function of an enclave object is opaque to clients: they reason through the model's function, never through the cells.

A generic enclave whose formals are private types is proved once, with its formal types abstract. Its instances are not proved again.

Changing an element in place

A client never holds a pointer into a cell. To change an element without copying it out and back, an enclave declares a generic procedure that the client instantiates with its own procedure and a ghost function describing it. Verada.Containers.Linked_Lists.Update_Element is one (test). The client's procedure must not reach the enclave (Global => null): B2023 rejects one that does, and B3010 rejects passing the list while the procedure also reaches it.

The library's enclaves

Unit What it is
Verada.Containers.Linked_Lists a doubly linked list
Verada.Containers.Graphs a directed graph with shared vertices
Verada.Containers.Hash_Maps a hash map probed in place

Each is proved whole, frames included, at the default budget.

Development runs

A run of a program with enclaves uses a checking allocator for their cells. Using a freed cell is B6011 and freeing one twice is B6012, both run-time checks. A function over an enclave's cells that recurses deeper than there are cells is B6013. In the REPL, a failure inside an enclave poisons its objects (B7010) until :reset.

Trust

The heap engine proves an enclave's frames when each of its types keeps a footprint. An enclave with a type that keeps none has its frames trusted: that its operations change only their own objects' cells is assumed, and every proof run says so (B8002). --no-assume rejects trusted enclaves with the other assumptions, and veradic trust lists them, so a release build accepts only enclaves the heap engine proved.