Algorithm Search⌘K

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 (E,H)(E, H): EE maps each variable to a cell or null, and HH maps each allocated cell to the value of its field. Reading y.n when yy is null is an error.

Collecting semantics. Let Init\mathrm{Init} be a set of start states at the entry, for example all states where xx points to an acyclic list. Let FF map a family X=(Xp)X = (X_p) of sets of states, one per program point, to Init\mathrm{Init} at the entry joined with the successors of the states in XX under each statement. The reachable states are R=lfp FR = \mathrm{lfp}\, F, the least fixed point in the order of inclusion, where ⊥\bot (the empty set) means that a point is unreachable.

Shape properties. A shape property is a set Φ\Phi 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 n from E(x)E(x) reaches null;
  • disjoint lists: no cell is reachable both from xx and from yy;
  • tree: no cell has two incoming fields, and no cycle;
  • no leak: every allocated cell is reachable from some variable;
  • safe dereference at p: E(y)≠E(y) \ne null whenever the statement at pp reads y.n.

The problem. Given a program, Init\mathrm{Init}, and a shape property Φp\Phi_p for some points pp, decide whether Rp⊆ΦpR_p \subseteq \Phi_p for each of them.

The analysis version. Choose an abstract domain D♯D^\sharp with a concretization γ\gamma from abstract values to sets of states. A shape analysis computes an abstract value apa_p for each point with

lfp F⊆γ(lfp F♯),\mathrm{lfp}\, F \subseteq \gamma(\mathrm{lfp}\, F^\sharp),

which holds when F∘γ⊆γ∘F♯F \circ \gamma \subseteq \gamma \circ F^\sharp, or with a Galois connection when α∘F⊑F♯∘α\alpha \circ F \sqsubseteq F^\sharp \circ \alpha. The property is proved when γ(ap)⊆Φp\gamma(a_p) \subseteq \Phi_p. 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, ⊥\bot means no state and ⊤\top means any state.

Variants

  • Which properties. Acyclicity and sharing; reachability between variables; trees and DAGs; doubly linked lists, where prev must be the inverse of next; 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 a→b→c→a \to b \to c \to null, the states at the loop head are:

trip list from x list from y
0 a→b→c→a \to b \to c \to null null
1 b→c→b \to c \to null a→a \to null
2 c→c \to null b→a→b \to a \to null
3 null c→b→a→c \to b \to a \to 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 xx is null, so yy points to an acyclic list. The dereference x.n is safe because the loop test says x≠x \ne null.

Why points-to analysis is not enough. If all list cells come from one allocation site LL, a points-to analysis has one abstract cell LL with L.n→{L,null}L.n \to \{L, \mathrm{null}\} and x,y→{L,null}x, y \to \{L, \mathrm{null}\}. 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 ls(x,null)∗ls(y,null)\mathrm{ls}(x, \mathrm{null}) * \mathrm{ls}(y, \mathrm{null}): two list segments ending in null, in disjoint parts of the heap. In three-valued logic, the cells reachable from xx fold into one summary node and those from yy 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 kk the first shape analysis; kk-limiting loses all shape beyond depth kk
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
AlgorithmTimeSpacePropertiesCode
Three-valued logic shape analysisO(E M v^2 N^5) worstO(L M v N^2) extradeterministiciterativesoundterminates1 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 pp" 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 kk unary predicates, each abstract heap has at most 3k3^k nodes, one per vector of values 00, 11 or 12\tfrac12 of the predicates (2k2^k 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 kk; 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 cic_i is the length of a list lil_i; increment is t = new; t.n = li; li = t; decrement is li = li.n; the zero test is li == null. The machine halts if and only if the program reaches its halt point. Put z = null and then w = z.n at 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml