15 213 The course that gives CMU its

  • Slides: 24
Download presentation
15 -213 “The course that gives CMU its Zip!” Verifying Programs with BDDs Topics

15 -213 “The course that gives CMU its Zip!” Verifying Programs with BDDs Topics n n class-bdd. ppt Representing Boolean functions with Binary Decision Diagrams Application to program verification 15 -213, F’ 08

Verification Example int abs(int x) { int mask = x>>31; return (x ^ mask)

Verification Example int abs(int x) { int mask = x>>31; return (x ^ mask) + ~mask + 1; } int test_abs(int x) { return (x < 0) ? -x : x; } Do these functions produce identical results? How could you find out? How about exhaustive testing? – 2–

More Examples int add. XY(int x, int y) { return x+y; } int mul.

More Examples int add. XY(int x, int y) { return x+y; } int mul. XY(int x, int y) { return x*y; } – 3– ? int add. YX(int x, int y) { return y+x; } ? int mul. YX(int x, int y) { return y*x; } = =

How Can We Verify Programs? Testing n Exhaustive testing not generally feasible n Currently,

How Can We Verify Programs? Testing n Exhaustive testing not generally feasible n Currently, programs only tested over small fraction of possible cases Formal Verification n Mathematical “proof” that code is correct c a b n – 4– Did Pythagoras show that a 2 + b 2 = c 2 by testing?

Bit-Level Program Verification int abs(int x) { int mask = x>>31; return (x ^

Bit-Level Program Verification int abs(int x) { int mask = x>>31; return (x ^ mask) + ~mask + 1; } x 0 y 0 x 1 x 2 y 1 y 2 x 1 x 2 • • • abs • • • – 5– • • absi • • • x 31 y 31 n View computer word as 32 separate bit values Each output becomes Boolean function of inputs n x 31 yi

Extracting Boolean Representation Straight-Line Evaluation int bit. Or(int x, int y) { return ~(~x

Extracting Boolean Representation Straight-Line Evaluation int bit. Or(int x, int y) { return ~(~x & ~y); } int test_bit. Or(int x, int y) { return x | y; } Do these functions produce identical results? – 6– x y v 1 = ~x v 2 = ~y v 3 = v 1 & v 2 v 4 = ~v 3 v 5 = x | y t v 4 == v 5 =

Tabular Function Representation n List every possible function value Complexity n – 7– Function

Tabular Function Representation n List every possible function value Complexity n – 7– Function with n variables

Algebraic Function Representation x 2 · x 3 x 1 · x 3 n

Algebraic Function Representation x 2 · x 3 x 1 · x 3 n n f(x 1, x 2, x 3) = (x 1 + x 2) · x 3 Boolean Algebra Complexity n n Representation Determining properties of function l E. g. , deciding whether two expressions are equivalent – 8–

Tree Representation Truth Table n n – 9– Decision Tree Vertex represents decision Follow

Tree Representation Truth Table n n – 9– Decision Tree Vertex represents decision Follow green (dashed) line for value 0 Follow red (solid) line for value 1 Function value determined by leaf value Complexity

Ordered Binary Decision Diagrams Initial Tree Reduced Graph (x 1 + x 2) ·

Ordered Binary Decision Diagrams Initial Tree Reduced Graph (x 1 + x 2) · x 3 Canonical representation of Boolean function n Two functions equivalent if and only if graphs isomorphic l Can be tested in linear time n – 10 – Desirable property: simplest form is canonical.

Example Functions Constants Variable Unique unsatisfiable function Unique tautology Typical Function n (x 1

Example Functions Constants Variable Unique unsatisfiable function Unique tautology Typical Function n (x 1 + x 2 ) · x 4 n No vertex labeled x 3 u n – 11 – independent of x 3 Many subgraphs shared Treat variable as function Odd Parity Linear representation

More Complex Functions n n n Add 4 -bit words a and b Get

More Complex Functions n n n Add 4 -bit words a and b Get 4 -bit sum S Carry output bit Cout Shared Representation n n – 12 – Graph with multiple roots 31 nodes for 4 -bit adder 571 nodes for 64 -bit adder Linear growth!

Symbolic Execution x 2 x 0 y v 1 = ~x ~y x 0

Symbolic Execution x 2 x 0 y v 1 = ~x ~y x 0 1 0 y 1 1 0 x 2 0 1 1 x 0 0 1 y 1 0 1 y 0 x 1 y 2 1 – 13 – 0 y 2 1 v 2 = x 1 1 0 (3 -bit word size) 0 y 0 0 1 0

Symbolic Execution (cont. ) v 3 = v 1 & v 2 x 1

Symbolic Execution (cont. ) v 3 = v 1 & v 2 x 1 y 2 1 v 4 = y 1 0 ~v 3 1 0 – 14 – = v 4 == v 5 0 x 0 y 1 1 1 x 1 y 2 t y 0 1 x 2 0 0 x 0 y 1 1 x | y 1 x 1 y 2 v 5 = y 0 0 x 2 0 x 0 0 y 0 1 1 0 1

Counterexample Generation Straight-Line Evaluation int bit. Or(int x, int y) { return ~(~x &

Counterexample Generation Straight-Line Evaluation int bit. Or(int x, int y) { return ~(~x & ~y); } int bit. Xor(int x, int y) { return x ^ y; } Find values of x & y for which these programs produce different results – 15 – x y v 1 = ~x v 2 = ~y v 3 = v 1 & v 2 v 4 = ~v 3 v 5 = x ^ y t v 4 == v 5 =

Symbolic Execution v 4 = ~v 3 x 2 x 1 y 2 0

Symbolic Execution v 4 = ~v 3 x 2 x 1 y 2 0 x 0 y 1 1 0 y 0 1 0 t = v 4 == v 5 1 x 2 v 5 = x ^ y x 2 x 1 x 0 y 2 y 2 y 1 y 0 0 1 0 1 x = 111 y = 001 x 1 y 1 x 0 y 0 1 – 16 – 0

Performance: Good int add. XY(int x, int y) { return x+y; } – 17

Performance: Good int add. XY(int x, int y) { return x+y; } – 17 – int add. YX(int x, int y) { return y+x; }

Performance: Bad int mul. XY(int x, int y) { return x*y; } – 18

Performance: Bad int mul. XY(int x, int y) { return x*y; } – 18 – int mul. YX(int x, int y) { return y*x; }

Why Is Multiplication Slow? Multiplication function intractable for BDDs l Exponential growth, regardless of

Why Is Multiplication Slow? Multiplication function intractable for BDDs l Exponential growth, regardless of variable ordering Node Counts Bits Add-4 – 19 – Multiplication-4 Add Mult 4 21 155 8 41 14560

What if Multiplication were Easy? int factor. K(int x, int y) { int K

What if Multiplication were Easy? int factor. K(int x, int y) { int K = XXXX. . . X; int range. OK = 1 < x && x <= y; int factor. OK = x*y == K; return !(range. OK && factor. OK); } – 20 – int one(int x, int y) { return 1; }

Dealing with Conditionals int abs(int x) { int r; if (x < 0) r

Dealing with Conditionals int abs(int x) { int r; if (x < 0) r = -x; else r = x; return r; } r r Context defined value x 1 0 0 t 1 = x<0 1 0 0 v 1 = -x t 1 0 0 r = v 1 t 1 t 1? v 1: 0 r = x !t 1 1 t 1? v 1: x v 2 = r 1 1 t 1? v 1: x During Evaluation, Keep Track of: n Current Context: Under what condition would code be evaluated n Definedness (for each variable) l Has it been assigned a value – 21 –

Dealing with Loops int ilog 2(unsigned x) { int r = -1; while (x)

Dealing with Loops int ilog 2(unsigned x) { int r = -1; while (x) { r++; x >>= 1; } return r; } Unroll n Turn into bounded sequence of conditionals l Default limit = 33 n – 22 – Signal runtime error if don’t complete within limit Unrolled int ilog 2(unsigned x) { int r = -1; if (x) { r++; x >>= 1; } else return r; . . . if (x) { r++; x >>= 1; } else return r; error(); }

Evaluation Strengths n Provides 100% guarantee of correctness n Performance very good for simple

Evaluation Strengths n Provides 100% guarantee of correctness n Performance very good for simple arithmetic functions Weaknesses n n – 23 – Important integer functions have exponential blowup Not practical for programs that build and operate on large data structures

Some History Origins n Lee 1959, Akers 1976 l Idea of representing Boolean function

Some History Origins n Lee 1959, Akers 1976 l Idea of representing Boolean function as BDD n Hopcroft, Fortune, Schmidt 1978 l Recognized that ordered BDDs were like finite state machines l Polynomial algorithm for equivalence n Bryant 1986 l Proposed as useful data structure + efficient algorithms n Mc. Millan 1987 l Developed symbolic model checking l Method for verifying complex sequential systems n Bryant 1991 l Proved that multiplication has exponential BDD l No matter how variables are ordered – 24 –