Creusot 0.15.0
We are pleased to announce the release of version 0.15.0 of Creusot, where formal contracts for Rust are forged.
In this post, we highlight a small selection of new features. See the full changelog for the rest.
What's new in Creusot?
During this month, Creusot has received some amazing external contributions, amounting to a third of the merged PRs!
Logic aliases
There are two kinds of functions in Creusot: program functions and logic functions. Morally, they belong to entirely different worlds (Rust vs. Pearlite), but because Creusot piggybacks on rustc's parsing and typechecking infrastructure, these functions appear in the same namespace. Sometimes, there are program and logic functions that represent a common concept, and we must add suffixes to distinguish between them. For example:
lenvs.len_logiccontainsvs.contains_logicderefvs.deref_logicnewvs.new_logicleading_zerosvs.leading_zeros_logic
You get the idea. We are forced to use more verbose names either in programs or in specifications.
Logic aliases enable program functions to be used in specifications
as alternative names of corresponding logic functions.
For instance, the following declares a function len as a logic alias
for len_logic:
impl<T> Seq<T> {
#[logic_alias(self.len_logic())]
fn len(&self) -> Int { ... }
}
That allows using len in specifications.
impl<T> Seq<T> {
#[ensures(result == (self.len() == 0))] // `.len()` will be expanded to `.len_logic()`
fn is_empty(&self) -> bool { ... }
}
To prevent meaningless and confusing aliases, the result of the program
function must equal its logic counterpart. This extra post-condition is automatically
inserted by #[logic_alias]:
#[logic_alias(self.len_logic())]
// implies
#[ensures(result == self.len_logic())]
The attribute #[logic_alias] can contain an arbitrary expression,
as long as it has the same type and the constraint above is satisfied.
This allows patching some minor differences in argument types.
For example, for Creusot's type FSet, the program function contains expects
a borrow &T (similarly to other collection types in Rust),
but in logic, there is no ownership and &T has the exact same interpretation as T,
hence the logic function contains_logic has a parameter of type T.
The logic alias below makes the expression self.contains(t) expand to
self.contains_logic(*t) when it appears in a specification,
where the dereference *t is necessary for typing but really denotes the same
value as t.
impl FSet<T> {
#[logic_alias(self.contains_logic(*t))]
pub fn contains(&self, t: &T) -> bool { ... }
#[logic]
pub fn contains_logic(self, t: T) -> bool { ... }
}
In practice, this means that we must usually add an ampersand to the argument
when using contains in specifications instead of contains_logic,
which is not ideal, but it is what it is. Example:
#[ensures(result == !(*self).contains(&value))] // shorter than `.contains_logic(value)`
pub fn insert(&mut self, value: T) -> bool {
Credits to Maël Coulmance, research engineer in the Creusot team.
Derive DeepModel
The trait DeepModel maps a Rust type to a mathematical model of it.
trait DeepModel {
type DeepModelTy;
#[logic]
fn deep_model(self) -> Self::DeepModelTy;
}
A DeepModel is notably used by the specification of eq: the comparison
x == y in Rust actually tells us whether x.deep_model() and y.deep_model()
are equal, using a primitive notion of equality in logic.
For example, a Vec is modeled by a Seq, the type of mathematical sequences.
Thus, comparing vectors with == only compares the sequence of elements they represent,
hiding low-level details like the address of the elements are stored and the capacity
of the vector.
Users can implement DeepModel for their own types. In common situations, it
can be derived, similarly to PartialEq and Eq. One subtlety however is that
the trait DeepModel contains an associated type DeepModelTy. When deriving
DeepModel, we also generate a type definition which is a copy of the original
type where all of the fields are replaced with their deep model.
#[derive(DeepModel)]
pub struct S(A, B);
// expands to
pub struct SDeepModel(A::DeepModelTy, B::DeepModelTy);
impl DeepModel for S {
type DeepModel = SDeepModel;
#[logic]
fn deep_model(self) -> Self::DeepModelTy {
SDeepModel(self.0.deep_model(), self.1.deep_model())
}
}
Another problem with our previous implementation was the visibility of the
generated deep_model function.
As a logic function, there are two kinds of visibility that apply to it:
-
the visibility of the function name, which is the one you're already familiar with in Rust (using
pubor not), -
and the visibility of the function body. In proofs, it can be useful to unfold the definitions of functions you use, which requires the function body to be visible at that point in the proof. Other definitions are tied to implementation details that should remain hidden from users. To control who can see the function body, the
#[logic]attribute supports a sub-attributeopenwhich works similarly topub:
#[logic(open)] // Expose the definition to clients
#[logic(open(self))] // Expose the definition to the current module (default)
Omitting open defaults to #[logic(open(self))], which makes the definition
visible to the current module only.
What visibility should we use for deep_model? For a struct with private fields,
we are constrained by the fact that the fields are private:
its accessors appear in the definition of deep_model, so the body must also remain private
to keep the fields hidden.
But for a type with public fields, it is reasonable to make the body of deep_model public as well.
// Example 1: private field
pub struct P(usize);
impl DeepModel for P {
type DeepModelTy = Int;
// By default, body is private, i.e., visible only in the current module
// (where the field of `P` is also visible).
#[logic]
fn deep_model(self) -> Self::DeepModelTy {
self.0.deep_model()
}
}
// Example 2: public field
pub struct Q(pub usize);
impl DeepModel for Q {
type DeepModelTy = Int;
// `open` = body is public
#[logic(open)]
fn deep_model(self) -> Self::DeepModelTy {
self.0.deep_model()
}
}
More generally, Creusot now takes into account fields with mixed visibilities
(as well as #[non_exhaustive]), assigning to the body of deep_model the
most restrictive visibility of its fields.[1]
We thank Martin Habovštiak for contributing these improvements.
Specs for discriminants
The discriminant is an integer that identifies one of the variants of an
enum. For example, the following enum has constructors A, B, C, D,
with discriminants 0, 3, 4, 10.
enum Foo {
A,
B = 3,
C,
D = 10,
}
Since this release, Creusot defines a logic function to denote the
discriminant in specifications: discriminant_value_logic (which is aliased by
core::intrinsics::discriminant_value using the feature discussed above).
The discriminant can be accessed in different ways in Rust.
-
by casting a field-less enum to an integer;
Foo::C as u8 // Creusot can prove that this equals 4 -
using the intrinsic
core::intrinsics::discriminant_value, which is notably used by the derive macros forHash,PartialEq, andPartialOrd[2] (in nightly toolchains, which Creusot relies on); -
using
std::mem::discriminant. This is the main use case that is not yet supported by Creusot.The challenge is a small technicality: the documentation
std::mem::discriminantexplicitly leaves its value for non-enums unspecified (unlike the underlyingcore::intrinsics::discriminant_value, which is unstable but documented to return 0 in that case).
We thank Sarah Quiñones for implementing this feature.
-
A further improvement that could be done in future work is to add an
#[ensures]clause to expose the value of public fields. ↩ -
Creusot currently redefines the derive macros for
PartialEqandPartialOrd. The lack of support for discriminant was one reason, which is why we generated a more naive definition instead (and still do). The other reason is that the methods must carry an explicit specification. We plan to solve this in the next release, so that we no longer need to redefine these derive macros. ↩