Algorithm Search⌘K

Definitioncore

Collecting semantics

Also called static semantics of reachable states.

The semantics that maps each program point to the set of all states that reach it, the most precise property an analysis can compute.

The collecting semantics of a program maps each program point to the set of all states that some run of the program has at that point. It is the least fixed point of a set of equations over sets of states, so it is the strongest property of each point that holds in every run. Every analysis in abstract interpretation is proved sound against it, and no algorithm computes it in general.

Definition

The programs are in the toy IR: three-address code in basic blocks, with unbounded integers. This page uses the Cousot convention for the order: sets of states are ordered by inclusion ⊆\subseteq, the join is union, and the bottom element ∅\emptyset means that no state reaches the point. Lower is more precise. The dataflow pages, such as maximal fixed-point solution, use the dual meet order of Kildall; turning the order upside down maps one convention onto the other.

States. Let VV be the set of names of the program. An environment is a partial map ρ:V⇀Z\rho : V \rightharpoonup \mathbb{Z}. A state at the start of block ℓ\ell is the pair (ℓ,ρ)(\ell, \rho). Let Env\mathit{Env} be the set of all environments.

Edge transformers. For each CFG edge ℓ→ℓ′\ell \to \ell', let postℓ→ℓ′(X)\mathit{post}_{\ell \to \ell'}(X) be the set of environments ρ′\rho' such that some ρ∈X\rho \in X, run through the instructions of ℓ\ell, gives ρ′\rho', and the terminator of ℓ\ell then goes to ℓ′\ell'. A run that traps or returns in ℓ\ell adds nothing. Since postℓ→ℓ′\mathit{post}_{\ell \to \ell'} is defined one element at a time, it preserves all unions:

postℓ→ℓ′(⋃iXi)=⋃ipostℓ→ℓ′(Xi).\mathit{post}_{\ell \to \ell'}\Bigl(\bigcup_{i} X_i\Bigr) = \bigcup_{i} \mathit{post}_{\ell \to \ell'}(X_i).

The equations. Let Init⊆Env\mathit{Init} \subseteq \mathit{Env} be the set of initial environments: each one binds the parameters to integers, or to the integers that a precondition allows. The lattice is L=P(Env)LabelsL = \mathcal{P}(\mathit{Env})^{\mathit{Labels}} with the pointwise order. It is a complete lattice. The function F:L→LF : L \to L is

F(S)(ℓ)=[ℓ=entry] Init ∪⋃ℓ′→ℓpostℓ′→ℓ(S(ℓ′)),F(S)(\ell) = [\ell = \mathit{entry}]\,\mathit{Init} \ \cup \bigcup_{\ell' \to \ell} \mathit{post}_{\ell' \to \ell}\bigl(S(\ell')\bigr),

where [ℓ=entry] Init[\ell = \mathit{entry}]\,\mathit{Init} is Init\mathit{Init} at the entry block and ∅\emptyset elsewhere. FF is monotone. The collecting semantics is

Coll=lfp F.\mathit{Coll} = \mathrm{lfp}\, F .

It exists by the Knaster-Tarski theorem, and it is the intersection of all SS with F(S)⊆SF(S) \subseteq S.

The reachable states. Let R(ℓ)R(\ell) be the set of environments ρ\rho such that some run from an initial environment arrives at the start of block ℓ\ell with environment ρ\rho. Then Coll=R\mathit{Coll} = R.

Proof. RR is a fixed point of FF: a state at the start of ℓ\ell is reached either at the start of the run (ℓ=entry\ell = \mathit{entry}, ρ∈Init\rho \in \mathit{Init}) or by one block step from a state that is reached at a predecessor, and each such step gives a state in R(ℓ)R(\ell). So Coll⊆R\mathit{Coll} \subseteq R. For the other inclusion, take any SS with F(S)⊆SF(S) \subseteq S. By induction on the number kk of block steps of a run, the state after kk steps is in SS: for k=0k = 0 it is in Init⊆F(S)(entry)⊆S(entry)\mathit{Init} \subseteq F(S)(\mathit{entry}) \subseteq S(\mathit{entry}), and one more step applies a post\mathit{post} of an edge, which stays inside F(S)⊆SF(S) \subseteq S. So R⊆SR \subseteq S for every such SS, and R⊆CollR \subseteq \mathit{Coll}. □\square

Iteration. Since each post\mathit{post} preserves unions, FF preserves the unions of increasing chains, so the Kleene fixed-point theorem gives Coll=⋃k≥0Fk(⊥)\mathit{Coll} = \bigcup_{k \ge 0} F^k(\bot). By induction on kk, Fk(⊥)(ℓ)F^k(\bot)(\ell) is the set of states that runs of fewer than kk block steps reach at ℓ\ell. The union may need infinitely many steps.

Paths. Unfold the union over the steps of a run. For a path p=ℓ0→ℓ1→⋯→ℓkp = \ell_0 \to \ell_1 \to \cdots \to \ell_k with ℓ0=entry\ell_0 = \mathit{entry} and ℓk=ℓ\ell_k = \ell, let postp\mathit{post}_p be the composition of the edge transformers along pp. Then

Coll(ℓ)=⋃p : entry⇝ℓpostp(Init).\mathit{Coll}(\ell) = \bigcup_{p \,:\, \mathit{entry} \leadsto \ell} \mathit{post}_p(\mathit{Init}).

This is the join over all paths, in the Cousot order, of the concrete transfer functions. Every path counts, also a path that no run follows, because postp\mathit{post}_p is empty for such a path. The equation solution and the path solution agree because each post\mathit{post} distributes over union; this is the case of equality in the MOP and MFP coincidence theorem, read in the dual order.

Soundness of an analysis. An analysis gives, for each block ℓ\ell, an abstract value A(ℓ)A(\ell) with a meaning γ(A(ℓ))⊆Env\gamma(A(\ell)) \subseteq \mathit{Env} (see abstract domain). It is sound when

∀ℓ:Coll(ℓ)⊆γ(A(ℓ)).\forall \ell : \quad \mathit{Coll}(\ell) \subseteq \gamma(A(\ell)).

Not computable. A toy IR program with unbounded integers can simulate a register machine. So the question "is Coll(ℓ)=∅\mathit{Coll}(\ell) = \emptyset?" for the block that holds ret is the halting problem, and it is undecidable. By Rice's theorem, every nontrivial question about Coll\mathit{Coll} is undecidable. An analysis therefore computes an over-approximation in an abstract domain.

Examples and non-examples

A loop with a parameter. The parameter is n.

entry:
  i = 0
  jump head
head:
  t = i < n
  br t body exit
body:
  i = i + 1
  jump head
exit:
  ret i

The edges are entry →\to head, head →\to body, head →\to exit and body →\to head. With Init={{n↦3}}\mathit{Init} = \{ \{n \mapsto 3\} \}, round-robin iteration in the order entry, head, body, exit gives these values of i at the start of each block. Each value computed this way stays inside Coll\mathit{Coll}, since FF is monotone and each step applies one component of FF to values inside Coll\mathit{Coll}, and each round is at least the Kleene iterate of the same number, so it ends at Coll\mathit{Coll}.

round head body exit
1 {0}\{0\} {0}\{0\} ∅\emptyset
2 {0,1}\{0, 1\} {0,1}\{0, 1\} ∅\emptyset
3 {0,1,2}\{0, 1, 2\} {0,1,2}\{0, 1, 2\} ∅\emptyset
4 {0,1,2,3}\{0, 1, 2, 3\} {0,1,2}\{0, 1, 2\} {3}\{3\}
5 no change

The plain Kleene iterates Fk(⊥)F^k(\bot) reach the same result and are stable from k=9k = 9, because they move one block per step.

With Init\mathit{Init} equal to all values of n, the result is an infinite set at each block. Write the states as pairs (n,i)(n, i):

Coll(head)={(n,i):0≤i≤max⁡(n,0)},Coll(exit)={(n,i):i=max⁡(n,0)}.\mathit{Coll}(\mathit{head}) = \{ (n, i) : 0 \le i \le \max(n, 0) \}, \qquad \mathit{Coll}(\mathit{exit}) = \{ (n, i) : i = \max(n, 0) \}.

Here Fk(⊥)F^k(\bot) grows at each step, and only the infinite union reaches the fixed point. An analysis in the interval domain finds i≥0i \ge 0 at head; one in the polyhedra domain also finds i≤ni \le n when n≥0n \ge 0 holds on entry.

An unreachable block. Change the terminator of head to jump head. Then no edge leads to exit, and Coll(exit)=∅\mathit{Coll}(\mathit{exit}) = \emptyset, the bottom element. In the Cousot order ∅\emptyset means "no state", not "no information".

Non-example: one value set per name. Two runs reach a point with (x,y)=(1,2)(x, y) = (1, 2) and (2,1)(2, 1). The collecting semantics is the set {(1,2),(2,1)}\{(1, 2), (2, 1)\}, so it proves x+y=3x + y = 3. The map that keeps one set of values per name, x∈{1,2}x \in \{1, 2\} and y∈{1,2}y \in \{1, 2\}, also allows (1,1)(1, 1). That map is an abstraction of the collecting semantics (the cartesian abstraction), not the collecting semantics itself.

Non-example: one run. The outcome of one run, run(prog, args), is a concrete execution. The collecting semantics collects the states of all runs from all initial states in Init\mathit{Init}.

Non-example: the trace semantics. The set of all finite runs, each one a sequence of states, is more precise than the collecting semantics. It can say "at head, i grows by one between two visits", and the set of reachable states cannot. The collecting semantics is the abstraction of the trace semantics that keeps, for each point, the set of last states of the traces that end there.

  • Abstract domain: a set of descriptions of sets of states, with a concretization γ\gamma, used to approximate Coll\mathit{Coll}.
  • Galois connection: the pair (α,γ)(\alpha, \gamma) that gives the best description of a set of states.
  • Abstract interpretation: the general technique that computes a sound approximation of lfp F\mathrm{lfp}\, F; this page gives its concrete side.
  • Inductive invariant: a family II with F(I)⊆IF(I) \subseteq I. Each one contains Coll\mathit{Coll}, and Coll\mathit{Coll} is itself inductive, so it is the strongest inductive invariant.
  • Meet-over-all-paths solution: the path formula above, in the dataflow convention and over an abstract lattice.
  • Fixed point, Knaster-Tarski theorem and Kleene fixed-point theorem: the existence of lfp F\mathrm{lfp}\, F and its computation as a limit.
  • Variants. The relational collecting semantics collects pairs (initial environment, current environment); procedure summaries use it. The backward collecting semantics collects the states from which an error state can be reached, with pre\mathit{pre} in place of post\mathit{post}.

References

  1. Patrick Cousot and Radhia Cousot, Abstract interpretation: a unified lattice model for static analysis of programs by construction or approximation of fixpoints, POPL 1977, pages 238 to 252, the static semantics of a program as the least fixed point of a system of equations over sets of environments, one per program point, and the correctness of abstract interpretations with respect to it
  2. Patrick Cousot and Radhia Cousot, Static determination of dynamic properties of programs, Proceedings of the Second International Symposium on Programming, B. Robinet (ed.), Dunod, Paris, 1976, pages 106 to 130, the semantic equations of a flowchart program and the correctness of the interval analysis with respect to them
  3. Flemming Nielson, Hanne Riis Nielson and Chris Hankin, Principles of Program Analysis, Springer, 1999, Chapter 4 (Abstract Interpretation), the correctness relations and the collecting semantics against which the analyses are proved correct
  4. Xavier Rival and Kwangkeun Yi, Introduction to Static Analysis, An Abstract Interpretation Perspective, MIT Press, 2020, Chapter 3 and Chapter 4 (the concrete semantics as a least fixed point over sets of states, the reachable states of a transition system)
  5. Patrick Cousot, Principles of Abstract Interpretation, MIT Press, 2021, the chapters on the structural fixpoint semantics and on the reachability semantics

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)
WritingW4 complete
  • a review of the text: no review record reviews/AR4EWF/text.yaml