380 likes | 405 Views
Learn how to perform program analysis by solving constraints to find invariants, compute weakest precondition/strongest postcondition, and detect general counter-examples. Dive deeper into constraint-based solutions for program verification.
E N D
Constraint Solving • Goal: Perform program analysis by solving constraints • Can be used to • Find invariants for program verification • Compute weakest precondition/strongest postcondition • Computing most general counter-examples
Why constraint-based? • Alternatives are fixed-point techniques • data-flow analysis • model checking • Constraint-based are goal-directed • Don't require widening heuristics • This paper's primary contribution is to focus only on constraint-based solutions
The Problems • Program verification • We have an assertion A that we want to prove • Program verification analysis should discover some invariant I such that I => A • Previous constraint-based techniques are only intra-procedural
The Problems • Weakest precondition/strongest postcondition • WP is the weakest precondition required (before executing the program) such that if it is satisfied the program is guaranteed not to fail • Will not an assertion • May not terminate • B is weaker than A if it implies “less” • A => B but not B => A
Program Model • x denotes integer variable • e is linear arithmetic expression • Linear assignments • x := e • x := ? (non-deterministic assignment) • assume(p): Assume p is true from earlier action • assert(p): Halt program if p is not true • p is boolean predicate of linear inequalities • Form: e >= 0
Program Model • Example: • x + y >= 30 • x + y – 30 >= 0 • x <= 30 • -x >= -30 • -x + 30 >= 0
Program Verification • Idea: Convert program verification problem to second-order constraint satisfaction problem • The unknowns in the constraint correspond to the program invariants • Goal is to find the solution for unknowns that satisfy constraint • Then we also find program invariants
Approach (Overview) • Convert program to second-order logical constraints • Assume that second-order unknowns in the constraint are actually first-order unknowns (using templates) • Convert first-order universal quantification to first-order existential quantification (using integer-specific lemma) • Convert formula to SAT and solve
Convert program to constraints • Choose a cut set • Set of program points such that each cycle in the CFG passes through at least one of the points • At each cut point • Define “relation” I over all live program variables • The relation is the invariant at each point in the program • True at entry and exit • Unknown everywhere else • These are what we want to solve
Constraint • In words: At all adjacent cut points, the invariant at each cut point should be “compatible”
Constraint • The overall constraint is the conjunction over each pair of adjacent cut points
Convert to first-order unknown • The unknowns in our constraint are the unknown second-order relations • We restrict them to templates, i.e., • Problem: How do we pick the template? • What if the template is not expressive enough?
Simplify unknownsFarkas’ Lemma Of course, this will not discover things that are satisfiable in rationals but not integers
Simplify unknowns Bottom constraint comes from polynomial comparison in y to negative lambda
Convert to SAT • Using bitvector theory • When satisfied, plug solutions back into template to get invariant
Choice of cut-set • Contribution: Some cut sets lead to better invariants • Choosing disjoint paths in loops will result in better invariants
Procedure summaries • Can use this technique to generate pre/post pairs (not well described)
Weakest precondition • Limit weakest precondition up to some constant c and fitting a template structure • First, treat the entry relation as unknown • Then, iteratively search for weaker preconditions • This doesn’t scale!
Weakest precondition • Instead, they add a new constraint to the VC
Weakest precondition • But, this structure does not fit Farkas’ lemma • They present two strategies • One does binary search over each possible constant assignment • The other sets the relation at each cut point to be the weakest consistent with its neighbors
Strongest postcondition • This is very similar to weakest precondition
Most General Counter-Example If each assert statement fails, set an err variable to true and go to the end of the program. Then, compute weakest precondition. This gives the “opposite” of the regular weakest precondition.
Most General Terminating Counter-Example Assert(false)
Program Verification using Predicate Abstraction • Problem: Program Verification as before • They want to analyze linked list and sorting algorithms • This requires finding more expressive invariants • Before: Only linear inequalities connected by conjunctions and disjunctions • Now: Arbitrary first order logical formulas
Predicate Abstraction • Predicate Abstraction • The programmer knows the invariant has some form (forall x, there exists y, …..) • Also knows the basic predicates • Relations • Variables • Constants • Marks unknowns • The algorithms in this paper find the unknowns
Templates Unknown
Program Model • Similar to other paper, but not limited to linear expressions • Not using Farkas’ lemma anymore
Three Algorithms • Two iterative data-flow like propagation of facts • Constraint-based (like the previous paper)