Techniqueresearch
Bi-abduction
Also called biabductive inference.
Infers the missing precondition and the leftover frame of a call in separation logic, which makes heap analysis compositional.
Bi-abduction answers one question about a procedure call in separation logic. The caller is in a symbolic state and the callee needs the precondition . Bi-abduction finds two formulas: the anti-frame , the part of that does not have, and the frame , the part of that does not use, with . The analysis adds to the precondition of the caller and carries across the call by the frame rule. This gives a shape analysis that infers a specification for each procedure from its body and the specifications of its callees, without the callers, so it can run on one procedure at a time, bottom-up over the call graph. Calcagno, Distefano, O'Hearn and Yang introduced it at POPL 2009, and it is the core of the Facebook Infer analyzer.
In plain words
A procedure needs some memory to exist before it runs, and it changes some of that memory. When one procedure calls another, the analysis knows what memory the caller has at that point and what the callee needs. Two parts are missing. The first is memory that the callee needs and the caller does not have yet. The analysis guesses it and adds it to what the caller itself must get from its own caller. The second is memory that the caller has and the callee never touches. That part passes over the call unchanged. Bi-abduction finds both parts in one step. Each procedure then gets a contract from its own code and the contracts of the procedures it calls. So the analysis can treat one procedure at a time, from the leaves of the call graph up.
When it applies
- The program changes the heap through pointers, as C, C++, Objective-C and Java programs do, and the question is about memory safety: null dereferences, accesses to freed or unallocated cells, and memory leaks.
- No specifications are given. Bi-abduction infers the preconditions, so the user does not write them.
- The program is large or changes often. Each procedure is analyzed once from the specifications of its callees, so a change needs new analysis only of the changed procedures and of the callers whose callee specifications change.
- The heap shapes fit a fixed set of inductive predicates, such as list segments. The proof search and the abstraction are only as good as these predicates.
The template
Symbolic heaps. A state is a stack , from program variables and logical variables to values, and a heap , a finite partial map from addresses to values. The constant is not an address. A symbolic heap is , where is a conjunction of equalities and disequalities between variables and , and is a separating conjunction of spatial atoms:
- when is empty;
- when , a heap of exactly one cell (one field per cell keeps the notation short);
- when is the union of two disjoint heaps and with and ;
- is the least predicate with , a list segment from to .
Logical variables that occur only in a precondition stand for unknown values and are read as existentially bound. The entailment means that every state that satisfies satisfies .
Triples and the frame rule. A triple is read in the fault-avoiding way: from every state that satisfies , the command does not access a cell outside the heap, and every final state satisfies . The frame rule is
It is sound because a command that runs safely from the heap of touches only cells of that heap, so a disjoint heap that satisfies is still there at the end. The rule fails for a reading of triples that allows faults: from , a write to could then change a cell of .
The bi-abduction question. Given and , find and with
With fixed to this is abduction. With fixed to it is frame inference. The answer is not unique: and is an answer when is satisfiable, and it is useless. A good answer has a small , and Calcagno and coauthors search for one with a set of proof rules. The procedure below covers points-to atoms and list segments. It matches the atoms of one by one against , moves the atoms that it cannot match into , and returns what is left of as . Variables of that do not occur in are the logical variables of the callee, and a match instantiates them.
biabduce(A, B): A = PiA & SigA, B = PiB & SigB
S := atoms of SigA ; G := atoms of SigB ; M := emp ; Q := conjuncts of PiB
while G is not empty:
take g from G
if g = (x |-> e) and S has (x' |-> e') with PiA |- x = x':
remove (x' |-> e') from S ; add e = e' to Q
else if g = ls(x, y) and PiA |- x = y:
skip
else if g = ls(x, y) and S has (x' |-> z) with PiA |- x = x':
remove (x' |-> z) from S ; add ls(z, y) to G ; add x != y to Q
else:
M := M * g
instantiate the callee variables from the equalities in Q
M := M & (the conjuncts of Q that PiA does not imply)
F := PiA & S
if PiA & M is unsatisfiable: report an error
return (M, F)
The unsatisfiable case is how the analysis finds bugs. If and the callee needs , then contains , which cannot hold with , and the call is a null dereference.
The call rule and its soundness. Let the analysis of the caller have reached the call with a precondition and a current state , so that holds for the code before the call. Let be a specification of , with its parameters replaced by the arguments. Bi-abduction gives , and the analysis continues with the precondition and the state .
Claim. If and hold, , assigns no variable free in , and assigns no variable free in , then holds.
Proof sketch. The frame rule on with the frame gives . The rule of consequence and give . The frame rule on with the frame gives . The rule of sequential composition joins the two.
The side condition on is why the analysis writes over logical variables that keep the values of the parameters at entry: can assign the program variables, but no command assigns a logical variable. The satisfiability check matters too. If were unsatisfiable, the triple would hold for the trivial reason that no state meets its precondition. Calcagno and coauthors state a stronger soundness theorem for the whole analysis; this sketch covers the step at one call.
The compositional analysis. Bi-abduction is one step of an analysis that infers a set of pairs (precondition, postcondition) for each procedure.
- Order the procedures bottom-up by the call graph. A set of mutually recursive procedures, a strongly connected component, is iterated to a fixed point.
- Footprint phase. Run a symbolic execution of the body from the precondition . A load or store of a cell that is not in the state is a call with a small specification, such as , and bi-abduction adds the cell to the precondition. At a call, bi-abduction runs against each specification of the callee. At loop heads, an abstraction folds chains of cells into list segments, so that the footprint stops growing and the iteration terminates.
- Re-execution phase. The preconditions from step 2 are guesses: a precondition found on one path may not be enough for another path. Run each candidate again without abduction. Keep it only if the run needs nothing that is missing, and take the final states of that run as its postconditions.
- Store the pairs of each procedure as its specifications for the callers.
Classic examples
Frame inference at a call. The caller is in the state and calls free(x), with the specification . The procedure matches against , so and . After the call the state is : the cell of passed over the call unchanged.
Frame and anti-frame together. Take a callee and a caller.
void set_next(node *p, node *q) { p->next = q; } // {p |-> b} set_next(p, q) {p |-> q}
void g(node *x, node *y) {
x->next = NULL; // line 1
set_next(y, x); // line 2
}
The analysis of g starts with the precondition . Line 1 writes the cell of x, which is missing, so bi-abduction gives . The precondition becomes and the state . At line 2 the callee needs . The state does not imply , so the atom does not match and goes to the anti-frame: and . By the call rule, g has the specification
The separating conjunction in the precondition says that x and y are different cells. An analysis that also splits on finds the second specification : the match of with sets , and the frame is empty. Callers of g use whichever specification their state matches.
A null dereference. If a caller of g is in the state , bi-abduction against puts into together with . That is unsatisfiable, and the analysis reports a null dereference at the call, in the caller, without looking into g again.
A list traversal. For while (x != NULL) x = x->next; the footprint phase adds one cell per iteration to the precondition: , then , and so on, where is the entry value of x. The abstraction folds such a chain into , the footprint stops growing, and the exit condition gives the precondition : the loop needs a list from x that ends in null.
Variations
- Frame inference alone. The Smallfoot tool of Berdine, Calcagno and O'Hearn used frame inference to check given specifications. Bi-abduction adds the anti-frame, which removes the need to write the preconditions.
- Richer predicates. Doubly linked lists, trees and nested lists need more inductive predicates and more rules in the proof search and the abstraction. The method stays the same.
- Bugs instead of proofs. Infer reports the errors that bi-abduction finds, such as null dereferences and leaks, and runs on each code change at review time. It uses heuristics that drop some specifications and paths, so in practice it finds bugs and does not prove their absence.
- Under-approximate analysis. Later analyses in Infer use bi-abduction with an under-approximate reading, in the style of incorrectness logic, where every reported bug is reachable.
- Abduction in other domains. The same question, find the missing hypothesis, is asked in arithmetic and first-order theories to infer preconditions and to explain false alarms.
- Whole-program shape analysis. Three-valued logic shape analysis analyzes the program from its entry and can track more general shapes, but it does not give procedure specifications that hold in every caller.
Pitfalls
- Taking any answer. and is always an answer when it is satisfiable, and it hides every match. The quality of the analysis rests on the search for a small , and a weakest need not exist.
- Skipping the satisfiability check. An unsatisfiable makes every triple valid. It is the signal of a bug, not a specification.
- Skipping re-execution. A precondition from the footprint phase can be too weak for some paths. Without the second phase the specifications can be unsound.
- Writing over program variables. The code before the call can assign them, and then the frame rule does not apply to . Use logical variables for the values at entry.
- Expecting a proof from Infer. The production tool trades soundness for speed and few false alarms. A clean run is not a proof of memory safety.
- Shapes outside the predicates. A data structure that the predicates cannot describe makes the abstraction lose it, and the analysis then finds no precondition or a precondition that callers never meet.
Exercises
- Run
biabduceon and , and on and . Give and in both cases. - Show that the frame rule fails if triples allow faults. Give , , and for which holds in that reading and does not.
- In the call rule, find and where assigns a variable of , and show that the conclusion fails.
- Give and for which two answers and exist and neither nor holds.
- Write a recursive procedure that frees a list, and infer its specification by hand with the footprint and re-execution phases.
References
- Cristiano Calcagno, Dino Distefano, Peter O'Hearn and Hongseok Yang, Compositional shape analysis by means of bi-abduction, Proceedings of the 36th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL '09), 2009, pages 289-300, the bi-abduction question, the proof rules for abduction and frame inference over symbolic heaps, and the bottom-up inference of procedure specifications
- Cristiano Calcagno, Dino Distefano, Peter W. O'Hearn and Hongseok Yang, Compositional shape analysis by means of bi-abduction, Journal of the ACM 58(6), article 26, 2011, the full version, with the footprint phase, the re-execution phase that checks candidate preconditions, and the experiments on large C code
- Cristiano Calcagno, Dino Distefano, Jeremy Dubreil, Dominik Gabi, Pieter Hooimeijer, Martino Luca, Peter O'Hearn, Irene Papakonstantinou, Jim Purbrick and Dulma Rodriguez, Moving fast with software verification, NASA Formal Methods (NFM 2015), LNCS 9058, 2015, pages 3-11, the use of bi-abduction in Facebook Infer and the analysis of each code change at review time
- John C. Reynolds, Separation logic, a logic for shared mutable data structures, Proceedings of the 17th Annual IEEE Symposium on Logic in Computer Science (LICS 2002), 2002, pages 55-74, the separating conjunction, the points-to assertion and the frame rule
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 3ADHQE Edit this page History Report an error Markdown