outline static analysis symbolic execution and inductive
play

Outline Static Analysis: Symbolic Execution and Inductive - PowerPoint PPT Presentation

Outline Static Analysis: Symbolic Execution and Inductive Verification Methods Overview TDDC90: Software Security Symbolic Execution Ahmed Rezine Hoare Triples and Deductive Reasoning IDA, Linkpings Universitet Hsttermin 2014 Static


  1. Outline Static Analysis: Symbolic Execution and Inductive Verification Methods Overview TDDC90: Software Security Symbolic Execution Ahmed Rezine Hoare Triples and Deductive Reasoning IDA, Linköpings Universitet Hösttermin 2014 Static Program Analysis and Approximations Static Program Analysis and Approximations ■ Finding all configurations or behaviours (and hence errors) of arbitrary computer programs can be easily reduced to the halting problem of a Turing machine. We want to answer whether the program is safe or not (i.e., has some erroneous reachable configurations or not): ■ This problem is proven to be undecidable, i.e., there is no algorithm that is guaranteed to terminate and to give an exact answer to the problem. ■ An algorithm is sound in the case where each time it reports the program is safe wrt. some errors, then the original Safe Program Unsafe Program program is indeed safe wrt. those errors ■ An algorithm is complete in the case where each time it is given a program that is safe wrt. some errors, then it does report it to be safe wrt. those errors

  2. Static Program Analysis and Approximations Static Program Analysis and Approximations ■ A sound analysis cannot give false negatives ■ The idea is then to come up with efficient approximations and algorithms to give correct answers in as many cases as possible. ■ A complete analysis cannot give false positives Over-approximation Under-approximation False Positive False Negative Two Lectures on Static Analysis First, What Are SMT Solvers? ■ Stands for Satisfiability Modulo Theory ■ Intuitively, these are constraint solvers that extend SAT solvers to richer theories These two lectures on static program analysis briefly introduce ■ Many solvers exist (Face’s, CVC, STP, OpenSMT), you will different types of analysis: use Z3 http://z3.codeplex.com in the lab. ■ Previous lecture: ■ SAT solvers find a satisfying assignment to a formula where all ■ syntactic analysis: scalable but neither sound nor complete variables are booleans or establishes its unsatisfiability ■ abstract interpretation sound but not complete ■ SMT solvers find satisfying assignments to first order formulas ■ This lecture: where some variables may range over other values than just ■ symbolic executions: complete but not sound booleans ■ inductive methods: may require heavy human interaction in ■ For instance, formulas can involve Linear real arithmetic, Linear proving the program correct integer arithmetic, uninterpreted functions, bit-vectors, etc. ■ E.g., f ( x )! = z ❫ f ( 2 y ) = z ❫ x � y = y is unsat while f ( x )! = z ❫ f ( 2 y ) = z ❫ x + y = y is sat. ■ Many applications in verification, testing, planning, theorem proving, etc.

  3. Outline Testing ■ Most common form of software validation Overview ■ Explores only one possible execution at a time Symbolic Execution ■ For each new value, run a new test. ■ On a 32 bit machine, if(i==2014) bug() would require 2 32 Hoare Triples and Deductive Reasoning different values to make sure there is no bug. ■ The idea in symbolic testing is to associate symbolic values to the variables Symbolic Testing Symbolic Execution: a simple example ■ Can we get to the ERROR? explore using SSA forms. ■ Useful to check array out of bounds, assertion violations, etc. ■ Main idea by JC. King in “Symbolic Execution and Program Testing” in the 70s foo(int x,y,z){ PC 1 = true 1 x = y - z; PC 2 = PC 1 x ✼✦ x 0 ❀ y ✼✦ y 0 ❀ z ✼✦ z 0 2 ■ Use symbolic values instead of concrete ones if(x==z){ PC 3 = PC 2 ❫ x 1 = y 0 � z 0 x ✼✦ x 1 ❀ y ✼✦ y 0 ❀ z ✼✦ z 0 3 z = z - 3; PC 4 = PC 3 ❫ x 1 = z 0 x ✼✦ x 1 ❀ y ✼✦ y 0 ❀ z ✼✦ z 0 ■ Along the path, maintain a Patch Constraint ( PC ) and a 4 if (4*z < x + y){ PC 5 = PC 4 ❫ z 1 = z 0 � 3 x ✼✦ x 1 ❀ y ✼✦ y 0 ❀ z ✼✦ z 1 5 symbolic state ( ✛ ) if (25 > x + y) { 6 PC 6 = PC 5 ❫ 4 ✄ z 1 ❁ x 1 + y 0 x ✼✦ x 1 ❀ y ✼✦ y 0 ❀ z ✼✦ z 1 ... 7 ■ PC collects constraints on variables’ values along a path, } 8 else{ ■ ✛ associates variables to symbolic expressions, 9 ERROR; PC 10 = PC 6 ❫ 25 ✔ x 1 + y 0 x ✼✦ x 1 ❀ y ✼✦ y 0 ❀ z ✼✦ z 1 10 ■ We get concrete values if PC is satisfiable } 11 } 12 ■ The program can be run on these values } 13 ... 14 ■ Negate a condition in the path constraint to get another path PC = ( x 1 = y 0 � z 0 ❫ x 1 = z 0 ❫ z 1 = z 0 � 3 ❫ 4 ✄ z 1 ❁ x 1 + y 0 ❫ 25 ✔ x 1 + y 0 ) Check satisfiability with an SMT solver (e.g., http://rise4fun.com/Z3 )

  4. Symbolic execution today Outline ■ Leverages on the impressive advancements for SMT solvers Overview ■ Modern symbolic execution frameworks are not purely symbolic, and not necessarily static: Symbolic Execution ■ They can follow a concrete execution while collecting constraints along the way, or ■ They can treat some of the variables concretely, and some Hoare Triples and Deductive Reasoning other symbolically ■ This allows them to scale, to handle closed code or complex queries Function Specifications and Correctness Hoare Triples and Partial Correctness ■ Contract between the caller and the implementation. Total ■ a Hoare triple ❢ P ❣ stmt ❢ R ❣ consists in: Correctness requires that: ■ a predicate pre-condition P ■ if the pre-condition ( -100 <= x && x <= 100 ) holds ■ an instruction stmt , ■ then the implementation terminates, ■ a predicate post-condition R ■ after termination, the following post-condition holds ■ intuitively, ❢ P ❣ stmt ❢ R ❣ holds if whenever P holds and stmt ( x>=0 && \result == x || x<0 && \result == -x ) is executed and terminates ( partial correctness ), then R ■ Partial Correctness does not require termination holds after stmt terminates. ■ For example: /*@ requires -100 <= x && x <= 100; 1 @ ensures x>=0 && \result == x || x<0 && \result == -x; 2 ■ ❢ true ❣ x = y ❢ ( x == y ) ❣ */ 3 ■ ❢ ( x == 1 )&&( y == 2 ) ❣ x = y ❢ ( x == 2 ) ❣ int abs(int x){ 4 ■ ❢ ( x ❃ = 1 ) ❣ y = 2 ❢ ( x == 0 ) ❥❥ ( y ❁ = 10 ) ❣ if(x < 0) 5 return -x; 6 ■ ❢ ( x ❃ = 1 ) ❣ ( if ( y == 2 ) then x = 0 ) ❢ ( x ❃ = 0 ) ❣ return x; 7 ■ ❢ false ❣ x = 1 ❢ ( x == 2 ) ❣ } 8

  5. Weakest Precondition Weakest Precondition of assignments ■ if ❢ P ❣ stmt ❢ R ❣ and P ✵ ✮ P for any P ✵ s.t. ❢ P ✵ ❣ stmt ❢ R ❣ , ■ wp ( x = E ❀ R ) = R [ x ❂ E ] , i.e., replace each occurrence of x in then P is the weakest precondition of R wrt. stmt , written R by E . wp ( stmt ❀ R ) ■ For instance: ■ wp ( x = x + 1 ❀ x ❃ = 1 ) = ( x ❃ = 0 ) . ■ wp ( x = 3 ❀ x == 5 ) = ( x == 5 )[ x ❂ 3 ] = ( 3 == 5 ) = false ( x ❃ = 5 ) ❀ ( x = 6 ) ❀ ( x ❃ = 0 && y = 8 ) are all valid ■ wp ( x = 3 ❀ x ❃ = 0 ) = ( x ❃ = 0 )[ x ❂ 3 ] = ( 3 ❃ = 0 ) = true ■ wp ( x = y + 5 ❀ x ❃ = 0 ) = ( x ❃ = 0 )[ x ❂ y + 5 ] = ( y + 5 ❃ = 0 ) preconditions, but they are not weaker than x ❃ = 0. ■ wp ( x = 5 ✄ y + 2 ✄ z ❀ x + y ❃ = 0 ) = ( x + y ❃ = ■ Intuitively wp ( stmt ❀ R ) is the weakest predicate P for which 0 )[ x ❂ 5 ✄ y + 2 ✄ z ] = ( 6 ✄ y + 2 ✄ z ❃ = 0 ) ❢ P ❣ stmt ❢ R ❣ holds Weakest Precondition of sequences Weakest Precondition of conditionals ■ Assume a sequence of two instructions stmt ; stmt ✵ ; , for ■ Assume a conditional ( if ( B ) then stmt else stmt ✵ ) , for example x = 2 ✄ y ; y = x + 3 ✄ y ; example ( if ( x ❃ y ) then z = x else z = y ) ■ the the weakest precondition is given by: ■ The weakest precondition is given by: wp ( stmt ; stmt ✵ ❀ R ) = wp ( stmt ❀ wp ( stmt ✵ ❀ R )) , ✥ ✦ wp (( if ( B ) then stmt else stmt ✵ ) ❀ R ) wp ( x = 2 ✄ y ; y = x + 3 ✄ y ❀ y ❃ 10 ) ( B ✮ wp ( stmt ❀ R ))&&(! B ✮ wp ( stmt ✵ ❀ R )) = = wp ( x = 2 ✄ y ❀ wp ( y = x + 3 ✄ y ❀ y ❃ 10 )) ■ For example, = wp ( x = 2 ✄ y ❀ ( y ❃ 10 )[ y ❂ x + 3 ✄ y ]) wp (( if ( x ❃ y ) then z = x else z = y ) ❀ z ❁ = 10 ) = wp ( x = 2 ✄ y ❀ x + 3 ✄ y ❃ 10 ) ■ = ( x ❃ y ✮ wp ( z = x ❀ z ❁ = 10 )) , = ( x + 3 ✄ y ❃ 10 )[ x ❂ 2 ✄ y ] &&( x ❁ = y ✮ wp ( z = y ❀ z ❁ = 10 )) = ( 2 ✄ y + 3 ✄ y ❃ 10 ) = ( x ❃ y ✮ x ❁ = 10 )&&( x ❁ = y ✮ y ❁ = 10 ) = y ❃ 2

  6. Hoare Triples for Loops, Partial Correctness Hoare Triples for Loops, Total Correctness ■ ❢ P ❣ ( while ( B ) do ❢ stmt ❣ ) ❢ R ❣ ■ In order to establish ❢ P ❣ ( while ( B ) do ❢ stmt ❣ ) ❢ R ❣ , you will ■ Partial correctness: if we start from P and need to find an invariant Inv such that: ( while ( B ) do ❢ stmt ❣ ) terminates, then R terminates. ■ P ✮ Inv ■ P ✮ Inv ■ ❢ Inv && B ❣ stmt ❢ Inv ❣ ■ ❢ Inv && B ❣ stmt ❢ Inv ❣ ■ ( Inv &&! B ) ✮ R ■ ( Inv &&! B ) ✮ R ■ For example ❢ i == j == 0 ❣ ( while ( i ❁ 10 ) do ❢ i = i + 1 ; j = ■ Total correctness: the loop does terminate: find a variant j + 1 ❣ ) ❢ j == 10 ❣ , we need to find Inv such that: function v such that: ■ ( i == j == 0 ) ✮ Inv ■ ( Inv && B ) ✮ ( v ❃ 0 ) ■ ❢ Inv &&( i ❁ 10 ) ❣ i = i + 1 ; j = j + 1 ❢ Inv ❣ ■ ❢ Inv && B && v = v 0 ❣ stmt ❢ v ❁ v 0 ❣ ■ ( Inv && i ❃ = 10 ) ✮ j == 10 ■ For example ( while ( i ❁ 10 ) do ❢ i = i + 1 ; j = j + 1 ❣ ) can be shown to terminate with v = ( 10 � i ) and Inv = ( i ❁ = 10 )

Recommend


More recommend