Contracts
A foreign function may carry a contract: requires and ensures clauses
written in the function’s own language. A contract does two jobs, and
only one of them is fic’s.
- The model assumes it.
--emit=xtaturns a contracted function that a protection domain calls into a UPPAAL functionffi_<name>: eachrequiresis anassertat every call (never a guard), and the deterministicensuresis the value the call returns. Anensuresthat only bounds the return value to an interval, comparing it with constants under&&and||(\result == 0 || \result == 1,0 <= \result <= 3), makes the value a choice: in an event, the edge that fires it selects any value the clause allows. That is how a driver’s input enters the model. A call returning a struct takes the fields itsensuresfix (\result.origin == origin), and keeps the rest. - A verifier proves it against the code: Frama-C with
-wpfor C,pancake2viperfor Pancake.fire verifyruns them; see thefire verifychapter of the manual for the tools and their setup.
So the model is only as sound as the contracts, and fic never derives a clause itself: it translates what the annotation states.
The subset fic reads
Both notations are lifted to one neutral form, so one subset applies:
integer and boolean literals, parameters, .fis constants, the return
value and a field of it (\result.f), the parameter’s value on entry,
+ - * / %, the comparisons,
== and !=, ! && ||, and c ? a : b.
| C (ACSL) | Pancake | |
|---|---|---|
| Notation | /*@ ... */ before the function | annotation lines inside the function body |
| Preconditions | requires | requires |
| Postconditions | ensures | ensures |
| Return value | \result | retval |
| Value on entry | \old(x) | old(x) |
| Purity | assigns \nothing; | derived from the body |
/*@ requires 0 <= r && r < ROUNDS;
@ ensures \result == r + 1;
@ assigns \nothing;
@*/
int64_t tally(int64_t r) { return r + 1; }
A constant declared in the .fis may be named in a clause (ROUNDS); the
generated header gives each language its value.
Anything outside the subset is rejected by its name, so a contract never seems read when it was not:
error: cq/ping.fi: ACSL contract error in "pingpong.c": function `tally`:
`ensures \forall integer k; ...` uses the quantifier `\forall`, which is
outside fic's contract subset (arithmetic, comparisons, `! && ||`, `? :`,
`\old`, `\result`, parameters)
In ACSL, quantifiers, memory predicates (\valid, *p), implication,
bit operators and C casts are outside it; in Pancake, so are permissions,
quantifiers, the heap, implication, calls, bit operators and shifts.
Clauses the subset does not cover at all are the verifier’s and are not an
error: in ACSL named behaviours, assumes, terminates and exits, and
in Pancake any annotation other than requires and ensures (a
trusted, an invariant).
What the model needs of a call
For a Pancake function the model checks each call site, because it must produce a value the automaton can use:
- A call whose value is used, rather than made as a bare statement,
needs a deterministic
ensures retval == E. Without one,--checkreports that the function has “no deterministicensures retval == …clause”. - A call in a guard must also be pure, because the generated C may
evaluate a guard more than once. A Pancake function is impure if it
calls a foreign function, stores to memory, reads shared memory, raises,
or writes a
global, directly or through a callee.
An ACSL contract is lifted and rendered the same way. assigns \nothing;
marks a C function pure, and a missing assigns clause counts as
impure, since ACSL’s default permits every effect; the guard check
currently runs for Pancake contracts only.
What fire verify proves
fire verify runs the contract prover first (Frama-C WP for a C import,
pancake2viper for Pancake), then verifyta on the model that assumes
the contracts. Both must pass: a model check over an unproved contract
proves nothing about the code behind it.