310 likes | 402 Views
Visions of Verification. Bruce W. Weide, et al. Computer Science and Engineering The Ohio State University http://www.cse.ohio-state.edu/rsrg. Outline. Design-by-Contract Testing vs. Verification Steps Toward Verification Conclusions. Modular reasoning boundary. uses. implements.
E N D
Visions of Verification Bruce W. Weide, et al. Computer Science and Engineering The Ohio State University http://www.cse.ohio-state.edu/rsrg
Outline • Design-by-Contract • Testing vs. Verification • Steps Toward Verification • Conclusions
Modular reasoning boundary uses implements uses implements implements uses 1. Design-by-Contract
A Contract • Mathematical models for values of all types of variables ("objects") • Preconditions and postconditions for all exported operations ("methods")
Example Contract contract List_Template (type Item) typeList is modeled by (left: string of Item, right: string of Item) exemplar p initialization ensures p = (< >, < >) // operations end List_Template
Example Operation operation Remove (updates p: List, replaces x: Item) requires |p.right| > 0 ensures #p = (p.left, <x> * p.right)
List_Template Operations Insert (updates p: List, alters x: Item) Remove (updates p: List, replaces x: Item) Advance (updates p: List) Move_To_Start (updates p: List) Move_To_Finish (updates p: List) Left_Length (restores p: List): Integer Right_Length (restores p: List): Integer
A Contract Realization • Representation data structures for all types it implements • Code for algorithms for all operations it implements • Loop invariants, representation invariants, abstraction relations as needed
A Contract Enhancement enhancement Reversal_Capability for List_Template operation Reverse (updates p: List) requires |p.left| = 0 ensures p = (reverse (#p.right), < >) end Reversal_Capability
An Enhancement Realization realization Dubious for Reversal_Capability procedure Reverse (updates p: List) decreasing |p.right| if Right_Length (p) > 0 then variable x: Item Remove (p, x) Reverse (p) Insert (p, x) end if end Reverse end Dubious
Modular reasoning boundary uses Reversal_Capability implements Dubious uses List_Template implements uses 2. Testing vs. Verification Is this claim valid?
Testing • Objective: to show code is incorrect • Approach: • Select a small finite number of test cases • Run code on test cases • Compare behavior to that specified by contract • Possible outcomes: • "Code is incorrect" • "Code may be correct for test cases" "Darn!"
Verification Automatically? Hoare's "verifying compiler" • Objective: to show code is correct • Approach: • Generate verification conditions • Mathematically prove verification conditions • Possible outcomes: • "Code is correct in all cases" • "Code is incorrect (and here is the bug!)" • "Sorry, can't tell because of incompleteness" "Darn!"
is correct Verification Condition Generator Automated Theorem Prover is incorrect (here!) Verifying Compiler
Testing Verification 3. Steps Toward Verification DEET Can show code correct Can show code incorrect Can find substitution bugs Can pinpoint bugs Modular Automated Practical
What is DEET? • N,N-diethyl-m-toluamide • NEJM 2002: "DEET is best bug repellant" • Detecting Errors Efficiently without Testing • Approaching all advantages of verification— except showing that code is correct—by… • Trying to show that code is incorrect
Step 1: Generate VCs • Between each pair of statements in code: • What path condition can get us here? • What can we assume here? • What do we need to confirm here?
0 P: true 1 P: |p0.right| > 0 2 P: |p0.right| > 0 3 P: |p0.right| > 0 4 P: |p0.right| > 0 5 P: true if Right_Length (p) > 0 then variable x: Item Remove (p, x) Reverse (p) Insert (p, x) end if
0 A: |p0.left| = 0 1 A: INIT(x1) and p1 = p0 2 A: p2.left = p1.left and p1.right = <x2> * p2.right 3 A: x3 = x2 and p3 = (reverse(p2), < >) 4 A: p4 = (p3.left, <x3> * p3.right) 5 A: if |p0.right| > 0 then p5 = p4 else p5 = p0 if Right_Length (p) > 0 then variable x: Item Remove (p, x) Reverse (p) Insert (p, x) end if
0 C: true 1 C: |p1.right| > 0 2 C: |p2.left| = 0 and |p2.right| < |p0.right| 3 C: true 4 C: true 5 C: p5 = (reverse(p0.right), < >) if Right_Length (p) > 0 then variable x: Item Remove (p, x) Reverse (p) Insert (p, x) end if
Constructing the VCs All of mathematics… • Sound and relatively complete proof system [Krone 88, Heym 95] combines P, A, and C assertions into verification conditions, each of which must be proved • For each i, VCi must be true for all values of the variables: ( (0≤j≤i(Pj Aj))) (Pi Ci) plus all assumptions through state i… may be used to confirm what is needed in state i
Step 2: Generate Error Hypotheses • Negation of VCi is error hypothesis EHi: Pi (0≤j≤i(Pj Aj)) Ci • If there exists a satisfying assignment of values to the variables of EHi, then VCi cannot be true for all values of the variables • Such an assignment is a witness to the incorrectness of the code
Finding a Witness • Problem: Proving an EH with an automated theorem prover is really no easier than proving a VC…
Step 3: Restrict Scope of Variables • "Small scope hypothesis" [Jackson 00] says that many program errors are revealed by "small" witnesses Original (infinite) state space Restricted (small, finite) state space
A New Version of the Problem • Restrict Item variables to a single value • Restrict string of Item variables to two values: empty string, or length one • Result of finiteness: EH can be rewritten as a propositional formula for which a satisfying assignment is a witness to incorrect code
Sample Clauses • |p2.left| = 0 becomes: len_p2_left_equals_zero • Also needed (among many other facts from mathematics): len_p2_left_equals_zero p2_left_equals_empty
Another Sample • p1 = p0becomes: ((p1_left_equals_empty p0_left_equals_empty) (p1_right_equals_empty p0_right_equals_empty)) ((p1_left_equals_s1 p0_left_equals_s1) (p1_right_equals_s1 p0_right_equals_s1)) ...
Step 4: Search for Witnesses • SAT solver used [Küchlin, et al. 01] directly handles non-CNF formulas • Solver shows EH for example given here to be (uniquely) satisfiable • Computation time: less than a second • Witness explicitly describes value of each variable at each state in the program, making it easy to debug incorrect code
4. Conclusions • Many variations should be explored: • How should quantifiers be handled? • Should simplification be done with original EHs, or by the SAT solver? • Can restricted scopes of variable values be incrementally enlarged without starting over? • Can a more general computer algebra system be used in place of a SAT solver?
Conclusions • Verifying compiler is not beyond the realm of possibility • But theorem provers must be improved significantly to achieve it! • Combining current verification technology with a "testing psychology" may be a viable intermediate step
Visions of Verification Questions? http://www.cse.ohio-state.edu/rsrg