Intro to SMT Solvers for Program Analysis and Verification

smt solvers in n.w
1 / 75
Embed
Share

Discover the world of SMT solvers in program analysis and verification with lectures covering topics such as SAT solving, array decision procedures, and more. Build your own solver and delve into theory decision procedures on top of Z3. Explore the theory of arrays, functions, and predicates while gaining hands-on experience through lab sessions.

  • SMT Solvers
  • Program Analysis
  • Verification
  • Array Theory
  • SAT Solving

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. SMT solvers in Program Analysis and Verification Nikolaj Bj rner Microsoft Research Lecture 5

  2. Overview of the lectures Day Topics Lab 1 Overview of SMT and applications. SAT solving part I. SAT solving part II. Congruence closure Program exploration with Pex 2 Encoding combinatorial problems 3 Combining solvers. A solver for arithmetic. Solvers for Bit-vectors, arrays, data-types, and other theories Solvers part II. Extended topics: Pattern matching Encoding arithmetic problems 4 Build your own solver 5 Program verification with Spec#/Boogie

  3. Summary of Day 5 Array decision procedures (part 2) Quantifiers and SMT solvers Lab: Build your own theory decision procedure on top of Z3

  4. Theory of arrays Functions: F= { read, write } Predicates: P= { = } Convention a[i] means: read(a,i) Non-extensional arrays TA: a, i, v . write(a,i,v)[i] = v a, i, j, v . i j write(a,i,v)[j] = a[j] Extensional arrays: TEA = TA + a, b. (( i. a[i] = b[i]) a = b)

  5. Array examples = = = ( , , ) [ ] b j [ ] a j b write a i v i j = = = ( , , ) ( , , ) v = b c w write a i v write a j w = [ ] b j [ ] c i b c

  6. Array examples = = = ( , , ) [ ] b j [ ] a j b write a i v i j Is valid = ( , , ) [ ] b j [ ] a j b write a i v i j Is unsat = = = ( , , ) write a i v [ ] [ ]) a j [ ] a j b write a i v j i j b j Is unsat (array axiom) ( ( , , )[ ] i j Is unsat (congruence) = ( , , ) [ ] b j b write a i v i j = = [ ] a j ( , , )[ ] [ ] a j write a i v j

  7. Array examples = = = ( , , ) ( , , ) v = b c w write a i v write a j w Is valid = [ ] b j [ ] c i b c = = = ( , , ) ( , , ) v = b c w write a i v write a j w Is unsat [ ] b j [ ] c i b c

  8. Array examples = = = = = ( , , ) ( , , ) write a j w i = b c w v b i write a i v write a j w b j [ ] [ ] c i Is unsat = = = [ ] [ ] c i c j ( , , )[ ] ( , , )[ ] w v b j write a i v j = ( , , )[ ] [ ] a j write a i v j Array axiom

  9. Array examples = = = = = = ( , , ) ( , , ) write a i v write a j w i = b c w v b i i write a i v write a j w b j = Is unsat [ ] [ ] c i c j j ( , , )[ ] ( , , )[ ] j = = ( , , )[ ] ( , , )[ ] [ ] [ ] a i write a i v write a j w i j a j

  10. Array examples = i j Case: = = = = = ( , , ) ( , , ) write a i v write a j w i = b c w v b i write a i v write a j w b j = [ ] [ ] c i c j ( , , )[ ] ( , , )[ ] j

  11. Array examples = = = = = = i j ( , , ) ( , , ) write a i v write a j w i = b c w v b i write a i v i write a i v write a j w b j = Case: [ ] [ ] c i c j ( , , )[ ] ( , , )[ ] j = ( , , )[ ] v Array axiom

  12. Array examples = = = = = ( , , ) ( , , ) write a i v write a j w i = b c w v b i write a i v i w = write a i v write a j w b j = = i j Case: [ ] [ ] c i c j ( , , )[ ] ( , , )[ ] j = ( , , )[ ] v v

  13. Array examples b write a i v c write a j w w b j v c i b c i j write a i v i w v b c = = = = = = ( , , ) ( , , ) write a i v write a j w i = = i j Case: = [ ] [ ] ( , , )[ ] ( , , )[ ] j = ( , , )[ ] v = Congruence

  14. Array examples a few steps = = = = i j ( , , ) ( , , ) write a i v write a j w i = b c w v b write a i v write a j w i write a i v write a j w b j = Case: [ ] [ ] c i c ( , , )[ ] ( , , )[ ] j = = ( , , )[ ] ( , , )[ ] [ ] [ ] a i j a j

  15. Array examples i j Case: = = = = ( , , ) ( , , ) write a i v write a j w i = b c w v b write a i v write a j w b j = = = [ ] [ ] c i c ( , , )[ ] ( , , )[ ] [ ] [ ] a i j a j

  16. Array examples i j Case: = = = = ( , , ) ( , , ) write a i v write a j w i = b c w v b write a i v write a j w b j = = = [ ] [ ] c i c k b k ( , , )[ ] ( , , )[ ] [ ] [ ] a i j a j = = ( . [ ] [ ]) c k b c Extensionality

  17. Array examples i j Case: = = = = ( , , ) ( , , ) write a i v write a j w i = b c w v b write a i v write a j w b j = = = [ ] [ ] c i c k b k ( , , )[ ] ( , , )[ ] [ ] [ ] a i j a j = ( . [ ] [ ]) c k Extensionality

  18. Array examples i j Case: = = = = ( , , ) ( , , ) write a i v write a j w i = b c w v b write a i v write a j w b j = = = [ ] [ ] c i c ( , , )[ ] ( , , )[ ] [ ] [ ] a i j a j [ ] b k [ ] c k Skolemize

  19. Array examples ( , , ) ( , , ) [ ] [ ] v c i write a j w i b c b k c k i k write a i v k j k write a j w k = = = = = b c w write a i v write a j w b j = = i j Case: = = ( , , )[ ] ( , , )[ ] [ ] [ ] a i write a i v j a j [ ] = [ ] Array axiom = ( ( ( , , )[ ] ( , , )[ ] [ ]) [ ]) c k b k =

  20. Array examples ( , , ) ( , , ) [ ] [ ] v c i write a j w i b c b k c k i k write a i v k j k write a j w k yadayadayada = = = = b c w write a i v write a j w b j = = = i j Case: = = ( , , )[ ] ( , , )[ ] [ ] [ ] a i write a i v j a j [ ] = [ ] = ( ( ( , , )[ ] ( , , )[ ] [ ]) [ ]) c k b k =

  21. Decision procedures for arrays Let L be literals over F= { read, write } Find M such that: M TA L Basic algorithm, reduce to E: for every sub-term read(a,i), write(b,j,v) in L i j a = b read(write(b,j,v),i) = read(a,i) read(write(b,j,v),j) = v Find ME, such that ME EL AssertedAxioms

  22. Decision procedures for arrays Correctness of basic algorithm: ME satisfies array axioms on terms in L. To show that ME can be extended to model for arrays: From Congurence Closure C* aM = [| *d1 *r1 , *d2 *r2 , *d3 .., else vroot(a) |] Where readM(aM, *di ) = *r1 e.g., *r1 = root(read(root(a),root(i)) under C* Model satisfies array axioms. For every write(a,i,v) the model satisfies write(a,i,v)[j] = a[j] whenever iM jM (first axiom) and also write(a,i,v)[i] = v (second axiom). vroot(a) was added to make arrays different unless they were forced to be (no extensionality) build model:

  23. Non-extensionality A non-theorem = = = ( , , ) [ ] a i b write a i v v a b a and b need not be equal even if the array axioms hold.

  24. Extensional arrays To enforce: a, b. (( i. a[i]= b[i]) a = b) For every pair a, b in L, Add fresh constant iab Add axiom a b a[iab] b[iab]

  25. Arrays galore Arrays may be more than just read/write. The constant array: v, i . const(v)[i] = v Generalized write: a,b,c, i . a[i] = b[i] write (a,b,c)[i] = c[i] a,b,c, i . a[i] b[i] write (a,b,c)[i] = b[i] We now have sets: = const(false), T = const(true), A B = write ( ,A,B)[i] A B = write (T,A,B)[i] Ranges: l,u, x . range(l,u)[x] l x u

  26. Arrays galore Claim: Same kind of reduction to E (and arithmetic) works Integer ranges, require slightly more range(l,u)[l-1], range(l,u)[u+1] range(l,u)[l], range(l,u)[u] Is there a general principle underpinning such extensions?

  27. Arrays galore Consider a more general formulation. is a conjunction of: Equalities, disequalities i, j, k . G(i,j,k) F(i,j,k) Where G is a guard formula comparing indices: And-or formula of i j, i c Claim: We can always eliminate i =j from the guard. Where F is a general formula with arrays, Restriction: no nested array formulas. Example: j . if i = j then b[i] = v else b[i] = a[i] Encodes that b = write(a,i,v)

  28. Arrays galore i, j, k . G(i,j,k) F(i,j,k) Where G is a guard formula comparing indices: And-or formula of i j, i c Claim: We can always eliminate i =j or i = c from the guard. i, k . k c i c F(i,i,k) i, j, k . i = j k c j c F(i,j,k)

  29. Arrays galore i, j, k . G(i,j,k) F(i,j,k) Where G is a guard formula comparing indices: And-or formula of i j, i c Claim: We can always or from the guard i, j, k . G(i,j,k) G (i,j,k) F(i,j,k) i, j, k . G(i,j,k) F(i,j,k) i, j, k . G (i,j,k) F(i,j,k)

  30. Arrays galore i, j, k . G(i,j,k) F(i,j,k) Where G is conjunction of i j, i c Decision procedure: Collect all c, where a[c] or c = i Instantiate quantifiers by all combinations of such indices. Check for E satisfiability of ground formula. Correctness: All quantified formulas are satisfied by C*.

  31. Arrays galore -Arithmetic i, j, k . G(i,j,k) F(i,j,k) Where G is conjunction of i < j, i < c, i > c Decision procedure: Collect all c, where a[c], c < i , c > i occurs in formula. For each c, also add c-1, c+1 to collection. Instantiate quantifiers by all combinations of collected indices. Check for ILA + E satisfiability of ground formula.

  32. Other theories common in program analysis Bit-vectors Algebraic data-types Queues Partial orders Binary relations Heaps (reachability)

  33. Checking validity Checking the validity of in a theory T: is T-valid T-unsat: T-unsat: T-unsat: T-unsat: T-unsat: x y z u . x z . [f(x),g(x,z)] [f(a1),g(a1,b1)] [f(an),g(an,bn)] 1 m where each i is a conjunction. (prenex of ) (skolemize) (instantiate) ( if compactness) (DNF)

  34. DPLL(QT) cute quantifiers We can use DPLL(T) for with quantifiers. Treat quantified sub-formulas as atomic predicates. In other words, if x. (x) is a sub-formula if , then introduce fresh p. Solve instead [ x. (x) p]

  35. DPLL(QT) Suppose DPLL(T) sets pto false any model M for must satisfy: M x. (x) for some skx: M (skx) In general: p (skx)

  36. DPLL(QT) Suppose DPLL(T) sets pto true any model M for must satisfy: M x. (x) for every term t: M (t) In general: For every term t. p (t)

  37. DPLL(QT) Summary of auxiliary axioms: p (skx) p (t) For fixed, fresh skx For every term t. Which terms t to use for auxiliary axioms of the second kind?

  38. DPLL(QT) with E-matching p (t) For every term t. Approach: Add patterns to quantifiers Search for instantiations in E-graph. a,i,v { write(a,i,v) } . read(write(a,i,v),i) = v

  39. DPLL(QT) with E-matching p (t) For every term t. Approach: Add patterns to quantifiers Search for pattern matches in E-graph. a,i,v { write(a,i,v) } . read(write(a,i,v),i) = v Add equality every time there is a write(b,j,w) term in E.

  40. DPLL(QT) with E-matching Array example a,i,v { write(a,i,v) } . write(a,i,v)[i] = v Add equality every time there is a write(b,j,w) term in E. a,i,j,v { write(a,i,v)[j] } . i j write(a,i,v)[j]=a[j] Add implication every time there is a read of a write. a,i,j,v { write(a,i,v), a[j] } . i j write(a,i,v)[j]=a[j] Add implication every time there is both a write and a read of a.

  41. The E-matching problem Input A set of ground equations E a ground term t and a pattern pat, with variables. Output The set of substitutions modulo E over the variables in pat, such that E t = (pat)

  42. An E-matching Example Given: A,I,J,V { write(A,I,V), A[J] } . I J write(A,I,V)[J]=A[J] E = { g(a) = f(b, c), b = d, a = c } Match: E write(g(c),2,1) = (write(A,I,V)), f(d,a)[4] = (A[J]) For = [ A g(c), I 2, V 1, J 4 ]

  43. An E-matching algorithm Review: Standard matching match(t, X, ) = [ X t] if X dom( ) match(t, X, ) = match(t, X, ) = fail match(t, t , ) = match( f(..), g(..), ) = fail match(f(t1,..,tn), f(pat1,..,patn), )= match(t1,pat1, match(tn, patn, )) if (X) = t if (X) t

  44. An E-matching algorithm E-matching generalizes standard matching: Every term t can be congruent to a set of other terms class(t) = {t1,..,tn} in the E-graph. Each congruent term is tried. Terms are equal if they are in the same class. find(t) is the equivalence class root. t and t are equal if: find(t) = find(t )

  45. E-matching example = = ( , , ) ( , , ) b store c i v store d j w E-graph: [ ] b k Term: ( , , )[ ] store A I V ( , , )[ ], [ ], ) ( , , ), ,{[ ( , , ), ( , , ),{[ ( , , ), ( , , ),{[ , , i V J Pattern: match store A I V J b k match store A I V b match store A I V store c i v match store A I V store d j w J k A c I = ( ( ( ( = ]}) J k ]}) k J k = ]}) d I J k A {[ , ],[ , , , ]} v J j V w

  46. Abstract E-matching

  47. The E-matching challenge E-matching is in theory NP-hard The real challenge is finding new matches Incrementally during a backtracking search In a large database of patterns, many sharing substantial structure [de Moura & Bj rner CADE 2007]

Related


More Related Content