Problemadvanced
Shape analysis
Also called heap shape analysis.
Infer the shape of heap data structures a program builds, such as acyclic lists or trees, to prove memory safety and data invariants.
Shape analysis is the problem of finding, at each point of a program, a description of the heap that is good enough to prove facts about the form of its linked structures: this variable points to an acyclic list, these two lists share no cell, this structure is a tree, no dereference meets null, and no cell becomes unreachable without being freed. It differs from points-to analysis, which names each cell by the line that allocated it. That cannot tell one cell of a list from the next, so it cannot see that a list stays acyclic. Shape analysis keeps enough about reachability and sharing to see it. The exact problem is undecidable, so every method computes a sound over-approximation, in the sense of abstract interpretation. Three-valued logic shape analysis and bi-abduction are two methods for it.
In plain words
A program that builds lists and trees in memory makes a picture of boxes and arrows. The picture changes as the program runs, and it can grow without limit. Shape analysis wants to say what every picture at a given line looks like, for every input, without running the program. For example: the arrows from this variable lead through some boxes and end at nothing, and never loop back. To do this, it draws a small picture that stands for all the big ones. Boxes that look alike are folded into one box that means "one or more boxes like this". The hard part is to fold enough that the picture stays small, but not so much that the analysis forgets that the list has no loop.
Definition
Programs and states. A program has pointer variables and cells with a field n (more fields for trees). The statements are x = null, x = new, x = y, x = y.n, x.n = y, x.n = null and branches on x == null or x == y, with control flow as in a control flow graph. A state is a pair : maps each variable to a cell or null, and maps each allocated cell to the value of its field. Reading y.n when is null is an error.
Collecting semantics. Let be a set of start states at the entry, for example all states where points to an acyclic list. Let map a family of sets of states, one per program point, to at the entry joined with the successors of the states in under each statement. The reachable states are , the least fixed point in the order of inclusion, where (the empty set) means that a point is unreachable.
Shape properties. A shape property is a set of states that is closed under renaming cells: it cares how the cells are linked, not which addresses they have. Examples:
- acyclic list from x: following
nfrom reachesnull; - disjoint lists: no cell is reachable both from and from ;
- tree: no cell has two incoming fields, and no cycle;
- no leak: every allocated cell is reachable from some variable;
- safe dereference at p:
nullwhenever the statement at readsy.n.
The problem. Given a program, , and a shape property for some points , decide whether for each of them.
The analysis version. Choose an abstract domain with a concretization from abstract values to sets of states. A shape analysis computes an abstract value for each point with
which holds when , or with a Galois connection when . The property is proved when . A failure to prove it is not a proof of the opposite: the analysis is sound but not complete. In the convention used here, lower abstract values mean fewer states, means no state and means any state.
Variants
- Which properties. Acyclicity and sharing; reachability between variables; trees and DAGs; doubly linked lists, where
prevmust be the inverse ofnext; memory safety (no null dereference, no use after free, no leak); data in the cells, such as sortedness. - Intraprocedural or compositional. A whole-program analysis follows each call into the callee. A compositional one infers a summary for each procedure once, which needs a way to guess the part of the heap a call does not touch; bi-abduction is that guess in separation logic.
- Verification or inference. With loop invariants and specifications written by the user, checking can be done in a decidable logic, as in PALE. Without them, the analysis must infer invariants, which needs abstraction and, for loops, joins or widening.
- May and must. "x may point to a cyclic structure" and "x must point to an acyclic list" are the two sides of one sound answer. Proofs of safety use the must side.
- Concurrency. Threads that share a heap need the shapes of both the heap and the ownership of its parts.
Examples
List reversal. The loop reverses a list in place:
// Init: x points to an acyclic list, y and t are null
y = null
while x != null:
t = x.n
x.n = y
y = x
x = t
// Goal: y points to an acyclic list, and x is null
On a list of three cells null, the states at the loop head are:
| trip | list from x | list from y |
|---|---|---|
| 0 | null |
null |
| 1 | null |
null |
| 2 | null |
null |
| 3 | null |
null |
The shape property that holds at the loop head in every trip, for every list length, is: x and y point to two acyclic lists with no common cell. At exit is null, so points to an acyclic list. The dereference x.n is safe because the loop test says null.
Why points-to analysis is not enough. If all list cells come from one allocation site , a points-to analysis has one abstract cell with and . This is the same answer for an acyclic list, for a cyclic list, and for two lists that share their tail. So it cannot prove the goal.
What a shape domain keeps. In separation logic, the loop invariant is : two list segments ending in null, in disjoint parts of the heap. In three-valued logic, the cells reachable from fold into one summary node and those from into another, and a predicate records that no cell is reachable from both. Either form is a finite description of infinitely many concrete heaps, and either keeps exactly the facts that the reversal needs.
Known algorithms
| method | describes the heap by | notes |
|---|---|---|
| Jones and Muchnick (1979) | bounded graphs, cut off at depth | the first shape analysis; -limiting loses all shape beyond depth |
| storage shape graphs (Chase, Wegman and Zadeck 1990) | one node per allocation site, with counts of heap references | keeps simple lists and trees, loses them under destructive update |
| Ghiya and Hendren (1996) | path facts between pointers | classifies each structure as a tree, a DAG or a cyclic graph, for parallelizing C |
| shape graphs (Sagiv, Reps and Wilhelm 1998) | nodes named by the variables that point to them, with sharing bits | proves in-place list reversal and insertion keep a list acyclic |
| three-valued logic shape analysis (1999, 2002) | logical structures with a value for "unknown", canonical abstraction | parametric in the predicates; TVLA is the tool |
| separation logic with list segments (Distefano, O'Hearn and Yang 2006) | points-to facts and list segments, joined by | abstraction by rewriting long segments into one |
| bi-abduction (Calcagno, Distefano, O'Hearn and Yang 2009) | the same formulas, per procedure | compositional; the basis of Infer |
| PALE (Møller and Schwartzbach 2001) | monadic second-order logic over graph types | checks given invariants; decidable but costly |
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Three-valued logic shape analysis | O(E M v^2 N^5) worst | O(L M v N^2) extra | deterministiciterativesoundterminates | 1 language |
Bounds
- Undecidable in general. Landi (1992) showed that may alias is undecidable for languages with
if, loops, dynamic storage and recursive data structures, and must alias is not even recursively enumerable. Shape properties are at least as hard: "x and y never point to the same cell at " is an alias question. The reduction under Reductions gives the same result for pointer programs without integers. See also the undecidability of may alias and Rice's theorem. - Finite abstractions terminate. With canonical abstraction over unary predicates, each abstract heap has at most nodes, one per vector of values , or of the predicates ( when every predicate is definite), so there are finitely many abstract heaps, and the fixed point is reached. The bound on the number of abstract heaps is doubly exponential in ; in practice the predicates are chosen so that it is much smaller.
- Decidable entailment for lists. Berdine, Calcagno and O'Hearn (2004) showed that entailment between separation logic formulas built from points-to facts and list segments is decidable, which lets an analysis check that one abstract heap is covered by another.
- Bounded heaps. If the number of cells is bounded, the state space is finite and every property is decidable by exploring it, but the bound is a restriction on the program, not an analysis.
Reductions
- From halting. A two-counter machine runs as a pointer program: counter is the length of a list ; increment is
t = new; t.n = li; li = t; decrement isli = li.n; the zero test isli == null. The machine halts if and only if the program reaches its halt point. Putz = nulland thenw = z.nat the halt point. The program has no null dereference if and only if the machine does not halt. So the problem is undecidable even without integers. - To alias analysis. A shape answer gives alias answers: two variables that point into disjoint structures do not alias. So shape analysis is at least as precise as the alias analysis it implies, and costs more.
- To logic. Checking that an abstract heap covers another is an entailment in the logic of the domain. In separation logic it is the entailment problem; in three-valued logic it is the embedding order between structures.
Applications
- Memory safety. Proving the absence of null dereferences, leaks, use after free and double free. Infer, built on bi-abduction, runs on large code bases, and SLAyer and SpaceInvader checked device drivers.
- Parallelization and dependence testing. If two pointers reach disjoint trees, loops over them can run in parallel; this was the aim of Ghiya and Hendren.
- Memory management. Knowing that a cell is unreachable after some point lets a compiler free it at compile time, or allocate it on the stack, as in escape analysis. This was the aim of Jones and Muchnick.
- Verification of data structure code. TVLA proved properties of sorting routines on lists, and that list operations keep their structure.
References
- Neil D. Jones and Steven S. Muchnick, Flow analysis and optimization of LISP-like structures, Proceedings of the 6th ACM Symposium on Principles of Programming Languages (POPL '79), 1979, pages 244-256, the first analysis of the shapes that unbounded structures take, with bounded descriptions of the heap
- David R. Chase, Mark N. Wegman and F. Kenneth Zadeck, Analysis of pointers and structures, Proceedings of the ACM SIGPLAN 1990 Conference on Programming Language Design and Implementation, ACM SIGPLAN Notices 25(6), 1990, pages 296-310, storage shape graphs, one node for many cells, and counts of heap references
- Rakesh Ghiya and Laurie J. Hendren, Is it a tree, a DAG, or a cyclic graph? A shape analysis for heap-directed pointers in C, Proceedings of the 23rd ACM Symposium on Principles of Programming Languages (POPL '96), 1996, pages 1-15, the shape (tree, DAG or cycle) of the structure reached from each pointer, for dependence testing and parallelization
- Mooly Sagiv, Thomas Reps and Reinhard Wilhelm, Solving shape-analysis problems in languages with destructive updating, ACM Transactions on Programming Languages and Systems 20(1), 1998, pages 1-50, shape graphs with sharing information that keep lists acyclic through destructive updates such as in-place reversal
- Mooly Sagiv, Thomas Reps and Reinhard Wilhelm, Parametric shape analysis via 3-valued logic, ACM Transactions on Programming Languages and Systems 24(3), 2002, pages 217-298, heaps as logical structures, canonical abstraction and the embedding theorem
- Dino Distefano, Peter W. O'Hearn and Hongseok Yang, A local shape analysis based on separation logic, Tools and Algorithms for the Construction and Analysis of Systems (TACAS 2006), LNCS 3920, pages 287-302, an abstract domain of separation logic formulas with list segments, and abstraction by rewriting
- William Landi, Undecidability of static analysis, ACM Letters on Programming Languages and Systems 1(4), 1992, pages 323-337, may alias is undecidable and must alias is not recursively enumerable, for languages with loops, dynamic storage and recursive data structures
- Josh Berdine, Cristiano Calcagno and Peter W. O'Hearn, A decidable fragment of separation logic, FSTTCS 2004, LNCS 3328, pages 97-109, entailment for the list fragment of separation logic is decidable
- Anders Møller and Michael I. Schwartzbach, The pointer assertion logic engine, Proceedings of the ACM SIGPLAN 2001 Conference on Programming Language Design and Implementation, pages 221-231, shape properties with user loop invariants, checked in monadic second-order logic with MONA
Where this leads
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 2N7G4M Edit this page History Report an error Markdown