An Overview of the Aeneas Rust Verification Toolchain The Aeneas verification toolchain translates safe Rust programs into pure functional programs so proof engineers can verify functional properties in theorem provers such as Lean without reasoning about memory or aliasing, according to a new blog post by the tool's author. Aeneas performs its translation through a symbolic execution that implements a borrow-checker, which the author says has been formalized and can validate some programs rejected by Rust's current borrow-checker or Polonius. Microsoft is using Aeneas to verify implementations of its SymCrypt cryptographic library. An Overview of the Aeneas Rust Verification Toolchain I finally took some time to write a blog-post that presents Aeneas. This post is essentially the original ICFP 22 paper but with more examples and up-to-date explanations the formalism has evolved quite a bit . The explanations I present here do not cover the latest features supported by the implementation such as nested mutable borrows, iterators, or dyn traits, but is nonetheless a quite comprehensive overview. Enjoy Introduction Aeneas https://github.com/AeneasVerif/aeneas is a verification toolchain for Rust programs. It leverages Rust’s ownership discipline to translate Rust code into pure functional programs, eliminating memory reasoning entirely. The proof engineer can then verify the resulting code in a theorem prover such as Lean, focusing on functional properties rather than on memory or aliasing. The key observation is that a large class of safe Rust programs are functional in essence. References and borrows are an implementation device for performance and memory layout; coupled with Rust’s linear ownership discipline, these programs admit a pure functional equivalent. Aeneas makes this equivalence concrete. The key technical device that allows supporting the most subtle patterns, functions returning mutable borrows, is something I called backward functions . Aeneas is also more than a compiler from Rust to whatever theorem prover you want: making the translation work required introducing novel semantics for safe Rust, while the translation itself is performed by means of a symbolic execution or abstract interpretation, depending on your way of seeing things for this semantics. Importantly, this symbolic execution implements a borrow-checker, a fact that we have formalized. This means that whenever you run Aeneas to translate a program you actually also borrow-check it. Interestingly, this borrow-checker can validate some programs that are not supported by the current borrow-checker or Polonius see the examples in the blog post . Aeneas is being used in practice Microsoft is using it to verify implementations of the SymCrypt https://github.com/microsoft/SymCrypt cryptographic library blog post https://www.microsoft.com/en-us/research/blog/rewriting-symcrypt-in-rust-to-modernize-microsofts-cryptographic-library/ . Aeneas and its Functional Translation, by Example Before jumping into the various facets of our formalism, we keep an eye on the prize, and immediately showcase how Aeneas translates Rust programs to pure equivalents. In this section, and for the remainder of this post, we use Lean syntax for our functional translation; it greatly resembles OCaml and other ML languages, and as such should be familiar to the reader. A brief note about terminology: we refer to regions , emphasizing that a region encompasses a set of borrows and loans at a given program point. The Rust compiler and documentation, however, refer to lifetimes , which conveys the idea of a syntactic bracket, and a specific implementation technique to enforce soundness. In this post, whenever we talk about Rust specifically, we use “lifetime”; whenever we emphasize our semantic view of ownership, we use “region”. Mutable Borrows, Functionally To warm up, we consider an example that, albeit small, showcases many of Rust’s features, including its ownership mechanism. In the Rust program below, incr increments a reference, and test incr acts as a representative caller of the function. js fn incr x: &mut i32 { x = x + 1; } fn test incr { let mut y = 0i32; incr &mut y ; assert y == 1 ; } The incr function operates by reference; that is, it receives the address of a 32-bit signed integer x , as indicated by the & reference type. In addition, incr is allowed to modify the contents at address x , because the reference is of the mut mutable kind, which permits memory modification. Finally, the Rust type system enforces that mutable references have a unique owner: the definition of incr type-checks, meaning that the function not only guarantees it does not duplicate ownership of x , but also can rely on the fact that no one else owns x . In test incr , we allocate a mutable value let mut on the stack; upon calling incr , we take a mutable reference &mut to y . Statically, y becomes unavailable as long as &mut y is active. In Rust parlance, y is mutably borrowed and its ownership has been transferred to the mutable reference. To type-check the call, the type-checker performs a lifetime analysis: the incr function has type &'a mut i32 - , and the &mut y borrow has type &'b mut i32 ; both 'a and 'b are lifetime variables. For now, suffices to say that the type-checker ascertains that the lifetime 'b of the mutable borrow satisfies the lifetime annotation 'a in the type of the callee, and deems the call valid. Immediately after the call, Rust terminates the region 'b , in effect relinquishing ownership of the mutable reference &'b mut y so as to make y usable again inside test incr . This in turn allows the assert to type-check, and thus the whole program. Undoubtedly, this is a very minimalistic program; yet, there are two properties of interest that we may want to establish already. The obvious one: the assertion always succeeds. More subtly, doing so requires us to prove an additional property, namely that the addition at line 2 does not overflow. The key insight of Aeneas is that even though the program manipulates references and borrows, none of this is informative when it comes to reasoning about the program. More precisely: x and y are uniquely owned, meaning that there are no stray aliases through which x or y may be modified; in other words, to understand what happens to y , it suffices to track what happens to &mut y , and therefore to x . Feeding this program to Aeneas generates the following translation, where + is syntactic sugar for an Aeneas primitive that captures the semantics of error-on-overflow in Rust. def incr x : I32 : Result I32 := x + 1 i32 -- evaluates to fail in case of overflow def test incr : Result Unit := do let y <- incr 0 i32 -- monadic bind massert y = 1 i32 -- monadic assert assert test incr = ok This program is semantically equivalent to the original Rust code, but does not rely on the memory: we have leveraged the precise ownership discipline of Rust to generate a functional, pure version of the program. In hindsight, the usage of references in Rust was merely an implementation detail, which is why incr becomes a simple possibly-overflowing addition. Should the call to incr succeed, its result is bound to y ; the assert simply becomes a boolean test that may generate a failure in the error monad. For the purposes of unit-testing, we can use Lean’s normalize to evaluate that test incr always succeeds, leveraging the fact that Aeneas produces an executable translation, not a logical encoding. In the remainder of this section, we use <- , Lean’s bind operator in the error monad. Returning a Mutable Borrow, and Backward Functions Rust programs, however, rarely admit such immediate translations. To see why, consider the following example, where the choose function returns a borrow, as indicated by its return type &'a mut . php fn choose<'a, T b: bool, x: &'a mut T, y: &'a mut T - &'a mut T { if b { return x; } else { return y; } } fn test choose { let mut x = 0i32; let mut y = 0i32; let z = choose true, &mut x, &mut y ; z = z + 1; assert z == 1 ; assert x == 1 ; assert y == 0 ; } The choose function is polymorphic over type T and lifetime 'a ; the lifetime annotation captures the expectation that both x and y be in the same region. At call site, x and y are borrowed line 7 : they become unusable, and give birth to two intermediary values &mut x and &mut y of type &'a mut i32 . The value returned by choose also lives in region 'a , i.e., z also has type &'a mut i32 . The usage of z lines 8-9 is valid because the region 'a still exists; the Rust type-checker infers that region 'a ought to be terminated after line 9, which ends the borrows and therefore allows the caller to regain full ownership of x and y , so that the assert s at lines 10-11 are well-formed. At first glance, it appears we can translate choose to an obvious conditional. But if we reason about the semantics of choose from the caller’s perspective, it turns out that the intuitive translation is not sufficient to capture what happens, e.g., the fact that x is updated while y is left unchanged, as we observe at lines 10 and 11. To solve this issue we observe that at call site, choose is an opaque, separate function, meaning the caller cannot reason about its precise definition — all that is available is the function type. This type, however, contains precise region information, which one can use to summarize its behavior. When performing the function call, the ownership of x and y is transferred to region 'a in exchange for z ; symmetrically, when the lifetime 'a terminates, z is relinquished to region 'a in exchange for regaining ownership of x and y . The former operation flows forward ; the latter flows backward . Using a separation-logic oriented analogy: borrows and regions encode a magic wand that is introduced in a function call and eliminated when the corresponding region terminates. Our point is: both function call and region termination are semantically meaningful. With the choose example, Aeneas emits a forward function which itself returns a backward continuation. The forward function is used to model the function call at line 7, while the backward continuation is used to propagate changes back into x and y when lifetime 'a terminates. def choose {T : Type} b : Bool x : T y : T : Result T × T → T × T := if b then ok x, fun z = z, y else ok y, fun z = x, z def test choose : Result Unit := do let z, choose back <- choose true 0 i32 0 i32 let z1 <- z + 1 i32 massert z1 = 1 i32 -- monadic assert let x, y := choose back z1 massert x = 1 i32 massert y = 0 i32 ok assert test choose = ok The call to choose returns a pair: the selected value, and a backward continuation choose back . We bind the result of the addition provided no overflow occurs to z1 ; then, per the rules of Rust’s type-checker, region 'a terminates which compels us to call the backward continuation choose back . The intuitive effect of calling choose back is as follows: we relinquish z1 , which was in region 'a ; doing so, we propagate any updates that may have been performed through z onto the variables whose ownership was transferred to 'a in the first place, namely x and y . This bidirectional approach is akin to lenses, except we propagate the output back to possibly-many inputs; in this case, z is a view over either x or y , and the backward function reflects the update to z onto the original variables. Thus, both variables are re-bound, before chaining the two asserts. The key advantage of returning the backward function as a continuation is that it captures the choice made by choose i.e., whether b was true or false in a closure, so the caller does not need to pass back the original arguments. From the caller’s perspective, the computational content of choose is unknown; but the signature of choose reveals the effect it may have onto its inputs x and y , which in turns allows us to derive the type of the forward and backward functions from the signature of choose itself. The result is a modular, functional translation that does not rely on any sort of cross-function inlining or whole-program analysis. To synthesize the backward continuation, it suffices to invert the direction of assignments; in one case, z flows to x and y remains unchanged; the other case is symmetrical. Recursion and Data Structures It might not be immediately obvious that this translation technique scales up beyond toy examples; we now crank up the complexity and show how Aeneas can handle a wide variety of idioms while still delivering on the original promise of a lightweight functional translation. Our next example is list nth mut , which allows taking a mutable reference to the n -th element of a list, mutating it, and regaining ownership of the list. enum List