Separation Logic
Extension of Hoare logic to reason about shared mutable data structures, where pointers can alias.
Reynolds and O'Hearn built separation logic around 2001 to verify C-like pointer code. Since then, it has been applied to languages such as Java and Python.
Heap Aliasing
When you put pointers and pointer aliasing together, Hoare Logic cannot express this.
int a = 1;
int *x = &a, *y = &a; // x == y
*x = 2; // *y is now 2
How do you write this as a Math equation?
Math has no *x, so the best I can think of is \(x = y\).
That only says two values are equal, not that writing through \(x\) changes what \(y\) points to.
Hoare Logic reasons about variables like in Math, so it can't express aliasing either.
Points-to analysis
Points-to analysis is the static analysis way to handle aliasing.
It computes, for each pointer, the set of locations it may point to,
so in the example above both x and y point to \(\{a\}\), and they may alias.
Andersen's analysis is precise but cubic, and Steensgaard's is almost linear but coarser.
It over-approximates, so it can say "may alias" but rarely "must alias",
while separation logic proves exactly which cells are disjoint.
Heap & Store
In separation logic, the store maps variable names to values (which can be addresses), and the heap maps addresses to values. Hoare logic only has the store, and separation logic adds the heap.
int n = 3;
int *x = malloc(sizeof(int)); // say malloc returns 100
*x = 2;
- Store: \(n = 3\), \(x = 100\)
- Heap: \(100 \mapsto 2\)
Note
Separation logic follows the OS view closely.
The store is the stack and the heap is the heap segment that malloc and free manage.
The only difference is that the stack has no addresses,
so the store holds variables' values directly, like registers.
Assertions about the heap
Separation logic makes assertions describe a piece of the heap, instead of the whole state.
- \(\text{emp}\) holds for the empty heap.
- \(e \mapsto e'\) holds for a heap with exactly one cell, at address \(e\), holding \(e'\).
- \(P * Q\) holds if the heap splits into two disjoint parts, one satisfying \(P\) and the other satisfying \(Q\).
\(*\) is the separating conjunction, and the disjointness is the whole point. \(x \mapsto 1 * y \mapsto 1\) already implies \(x \neq y\), since the two cells have to sit in different parts of the heap. Compare with \(\land\), where both sides describe the same heap.
\(e \mapsto -\) is short for \(\exists v.\ e \mapsto v\), a cell whose contents you don't care about.
Magic wand
\(P \mathrel{-\!\!*} Q\) is the separating implication, or magic wand. It holds for a heap if adding any disjoint heap satisfying \(P\) gives a heap satisfying \(Q\). So it's a heap missing \(P\), and adding \(P\) back gets you \(Q\).
Rules for heap commands
Each heap command gets a small axiom that mentions only the cell it touches.
(Lookup) and (Alloc) need \(x\) not to appear in \(e\) or \(v\).
The preconditions are also safety requirements. A triple now means \(S\) doesn't crash, as well as the usual partial correctness. So (Mutate) can only run if you own the cell at \(e\), which rules out writing through dangling or null pointers.
The frame rule
The small axioms are useless on their own, since a real heap has more than one cell. The frame rule adds the rest of the heap back.
\(S\) must not assign to any variable free in \(R\).
\(R\) is the frame, the part of the heap \(S\) doesn't touch. The rule is sound because of \(*\). Since \(R\) describes cells disjoint from the ones in \(P\), and \(S\) only touches cells in \(P\) (or it would crash), \(R\) still holds afterwards.
This is the same composition story as Hoare logic, but for the heap. You prove a function against only the cells it uses, and (Frame) lets you call it in any bigger heap without proving anything new. It's called local reasoning.
Example: swap
t := [x]; s := [y]; [x] := s; [y] := t
Each step is one small axiom plus (Frame) for the other cell. For example, \([x] := s\) uses (Mutate) on \(x \mapsto a\), with \(y \mapsto b\) as the frame. The precondition never says \(x \neq y\), but the proof is only valid when it holds, because \(*\) demands it.
Inductive predicates
Data structures are described with recursive predicates. A linked list from \(x\) holding the sequence \(\alpha\) is
The \(*\) makes every node a different cell, so the list can't have cycles.
Tools
Facebook's Infer runs a separation logic analysis on large codebases to find null dereferences and leaks. VeriFast and Viper verify annotated programs, and Iris is a framework in Coq for concurrent separation logic.
Dynamic frames
Dynamic frames (Kassios, 2006) is the other common way to reason about the heap, and it's what Dafny uses.
Each spec names the set of heap locations it may read or modify,
often as a ghost field like Repr, and the set can change as the program runs.
Where separation logic gets disjointness from \(*\),
dynamic frames states it with set operations like Repr !! other.Repr.