2.36k likes | 2.52k Views
Program Analysis and Verification 0368- 4479 http://www.cs.tau.ac.il/~maon/teaching/2013-2014/paav/paav1314b.html. Noam Rinetzky Lecture 9: Abstract Interpretation II. Slides credit: Roman Manevich , Mooly Sagiv , Eran Yahav. From verification to analysis. Manual program verification
E N D
Program Analysis and Verification 0368-4479http://www.cs.tau.ac.il/~maon/teaching/2013-2014/paav/paav1314b.html Noam Rinetzky Lecture 9: Abstract Interpretation II Slides credit: Roman Manevich, MoolySagiv, EranYahav
From verification to analysis • Manual program verification • Verifier provides assertions • Loop invariants • Program analysis • Automatic program verification • Tool automatically synthesize assertions • Finds loop invariants
Abstract Interpretation [Cousot’77] • Mathematical foundation of static analysis
Abstract Interpretation [Cousot’77] • Mathematical framework for approximating semantics (aka abstraction) • Allows designing sound static analysis algorithms • Usually compute by iterating to a fixed-point • Computes (loop) invariants • Can be interpreted as axiomatic verification assertions • Generalizes Hoare Logic & WP / SP calculus
Abstract Interpretation [Cousot’77] • Mathematical foundation of static analysis • Abstract domains • Abstract states ~ Assertions • Join () ~ Weakening • Transformer functions • Abstract steps ~ Axioms • Chaotic iteration • Structured Programs ~ Control-flow graphs • Abstract computation ~ Loop invariants
Concrete Semantics set of states set of states statement S operational semantics(concrete semantics)
Conservative Semantics set of states set of states set of states statement S operational semantics(concrete semantics)
Abstract (conservative) interpretation generalizes axiomatic verification statement S abstract representation abstract representation abstract representation abstract semantics abstraction abstraction statement S set of states set of states operational semantics(concrete semantics) {P} S {Q} sp(S, P)
Abstract (conservative) interpretation abstract representation abstract representation statement S abstract semantics concretization concretization set of states set of states set of states statement S operational semantics(concrete semantics)
Abstract (conservative) interpretation abstract state abstract state statement S abstract semantics (transfer function) concretization concretization set of states set of states set of states statement S operational semantics(concrete semantics)
Abstract Interpretation [Cousot’77] • Mathematical foundation of static analysis • Abstract domains • Abstract states ~ Assertions • Join () ~ Weakening • Transformer functions • Abstract steps ~ Axioms • Chaotic iteration • Abstract computation ~ Loop invariants • Structured Programs ~ Control-flow graphs Lattices (D, , , , , ) Monotonic functions Fixpoints
A taxonomy of semantic domain types Complete Lattice(D, , , , , ) Lattice(D, , , , , ) Join semilattice(D, , , ) Meet semilattice(D, , , ) Complete partial order (CPO)(D, , ) Partial order (poset)(D, ) Preorder(D, )
Preorder • We say that a binary order relation over a set D is a preorder if the following conditions hold for every d, d’, d’’ D • Reflexive: d d • Transitive: d d’ and d’ d’’ implies d d’’
Preorder • We say that a binary order relation over a set D is a preorder if the following conditions hold for every d, d’, d’’ D • Reflexive: d d • Transitive: d d’ and d’ d’’ implies d d’’ d’’ Hasse Diagram d d’
Preorder • We say that a binary order relation over a set D is a preorder if the following conditions hold for every d, d’, d’’ D • Reflexive: d d • Transitive: d d’ and d’ d’’ implies d d’’ d’’ Hasse Diagram d d’
Partial order • We say that a binary order relation over a set D is a preorder if the following conditions hold for every d, d’, d’’ D • Reflexive: d d • Transitive: d d’ and d’ d’’ implies d d’’ • Anti-symmetric: d d’ and d’ d implies d = d’ d’’ Hasse Diagram d d’
Chains • d d’ means d d’ and d d’ • An ascending chain is a sequencex1 x2 … xk… • A descending chain is a sequencex1 x2 … xk… • The height of a poset(D, ) is the length of the maximal ascending chain in D
poset Hasse diagram (for CP) … … {x=-2} {x=-1} {x=0} {x=1} {x=2}
Some posets-related terminology • If x y(alt y ⊒x) we can say • x is lowerthan y • x is more precise than y • x is more concrete than y • x under-approximates y • y isgreaterthan x • y is less precise than x • y is more abstractthan x • y over-approximates x
Least upper bound (LUB) • (D, ) is a poset • b ∊ D is an upper bound of A⊆D if ∀a A:a b • b ∊ D is theleastupper bound of A⊆D if • b is an upper bound of A • If b’ is an upper bound of A then b b’ • Join: X = LUB of X • x y = {x,y} May not exist May not exist
Join operator • Properties of a join operator • Commutative: x y = y x • Associative: (x y) z = x (y z) • Idempotent: x x = x • A kind of abstract union (disjunction) operator • Top element of (D, ) is = D
Join Example … … {x=-2} {x=-1} {x=0} {x=1} {x=2}
Join Example … … {x=-2} {x=-1} {x=0} {x=1} {x=2}
Join Example … … {x=-2} {x=-1} {x=0} {x=1} {x=2}
Greatest lower bound (GLB) • (D, ) is a poset • b ∊ D is an lower bound of A⊆D if ∀a A:b a • b ∊ D is thegreatest lower bound of A⊆D if • b is an lower bound of A • If b’ is an lower bound of A then b’ b • Meet: X= GLB of X • x y = {x, y} May not exist May not exist
Meet operator • Properties of a meet operator • Commutative: x y = y x • Associative: (x y) z = x (y z) • Idempotent: x x = x • A kind of abstract intersection (conjunction) operator • Bottom element of (D, ) is = D
Complete partial order (CPO) • A poset (D , ) is a complete partial if every ascending chain x1 x2 … xk… has a LUB
Meet Example x0 x0 x<0 x=0 x>0
Meet Example x0 x0 x<0 x=0 x>0
Meet Example x0 x0 x<0 x=0 x>0
Complete partial order (CPO) • A poset (D , ) is a complete partial if every ascending chain x1 x2 … xk… has a LUB
Join semilattices • (D, , , ) is a join semilattice • (D, ) is a partial order • ∀X FIN D . X is defined • A top element
Meet semilattices • (D, , , ) is a meet semilattice • (D, ) is a partial order • ∀X FIN D . X is defined • A bottom element
Lattices • (D, , , , , ) is a lattice if • (D, , ,) is a join semilattice • (D, , , ) is a meet semilattice • A lattice (D, , , , , ) is a complete lattice if • X and Y are defined for arbitrary sets
Example: Powerset lattices • (2X, , , , , X) is the powersetlatticeof X • A complete lattice
Example: Sign lattice x0 x0 x<0 x=0 x>0
A taxonomy of semantic domain types Join/Meet exist for every subset of D Join/Meet exist for every finite subset of D (alternatively, binary join/meet) Complete Lattice(D, , , , , ) Lattice(D, , , , , ) Meet of the empty set Join of the empty set Join semilattice(D, , , ) Meet semilattice(D, , , ) poset with LUB for all ascending chains Complete partial order (CPO)(D, , ) reflexivetransitiveanti-symmetric: d d’ and d’ d implies d = d’ Partial order (poset)(D, ) • reflexive: d dtransitive: d d’, d’ d’’ implies d d’’ Preorder(D, )
Collecting semantics • For a set of program states State, we define the collecting lattice (2State, , , , , State) • The collecting semantics accumulates the (possibly infinite) sets of states generated during the execution • Not computable in general
Abstract (conservative) interpretation abstract representation abstract representation statement S abstract semantics concretization concretization set of states set of states set of states statement S operational semantics(concrete semantics)
Abstract (conservative) interpretation 0 < x 0 ≤ x x = x -1 abstract semantics concretization concretization {x↦1, x↦2, …} {x↦0, x↦1, …} {x↦0, x↦1, …} x=x-1 operational semantics(concrete semantics)
Abstract (conservative) interpretation 0 < x x = x -1 abstract semantics concretization concretization {x↦1, x↦2, …} {x↦0, x↦1, …} {…, x↦0, …} x=x-1 operational semantics(concrete semantics)
Abstract (non-conservative) interpretation 0 < x 0 < x x = x -1 abstract semantics concretization concretization {x↦1, x↦2, …} {x↦0, x↦1, …} { x↦1, …} x=x-1 ⊈ operational semantics(concrete semantics)
But … • what if we have x & y? • Define lattice (semantics) for each variable • Compose lattices • Goal: compositional definition • What if we have more than 1 statement? • Define semantics for entire program via CFG • Different “abstract states” at every CFG node
One lattice per variable true true x0 x0 y0 y0 x<0 x=0 x>0 y<0 y=0 y>0 false false How can we compose them?
Cartesian product of complete lattices • For two complete lattices L1 = (D1, 1, 1, 1, 1, 1) L2 = (D2, 2, 2, 2, 2, 2) • Define the posetLcart = (D1D2, cart, cart, cart, cart, cart)as follows: • (x1, x2) cart (y1, y2) iffx1 1 y1x2 2 y2 • cart = ? cart = ? cart = ? cart = ? • Lemma: L is a complete lattice • Define the Cartesian constructor Lcart = Cart(L1, L2)
Cartesian product example =(,) x0 x0 y0 y0 x0,y0 x0,y0 x0,y0 x0,y0 … … x0,y<0 x0,y<0 x0,y=0 x0,y=0 x0,y>0 x0,y>0 x>0,y0 x>0,y0 … x<0,y<0 x<0,y=0 x<0,y>0 x=0,y<0 x=0,y=0 x=0,y>0 x>0,y<0 x>0,y=0 x>0,y>0 =(, ) How does it represent(x<0y<0) (x>0y>0)? (false, false)
Disjunctive completion • For a complete lattice L = (D, , , , , ) • Define the powerset latticeL = (2D, , , , , ) = ? = ? = ? = ? = ? • Lemma: L is a complete lattice • L contains all subsets of D, which can be thought of as disjunctions of the corresponding predicates • Define the disjunctive completion constructorL = Disj(L)
The base lattice CP … … {x=-2} {x=-1} {x=0} {x=1} {x=2}
The disjunctive completion of CP What is the height of this lattice? true … … {x=-2} {x=-1} {x=0} {x=1} {x=2} … … … {x=-2x=-1} {x=-2x=0} {x=-2x=1} {x=1x=2} … … … {x=-1 x=1x=-2} {x=0 x=1x=2} … false