Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision...

55
Decision Procedures An Algorithmic Point of View D.Kroening O.Strichman Bit-Vectors Chapter 6 Revision 1.2

Transcript of Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision...

Page 1: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Decision ProceduresAn Algorithmic Point of View

D.Kroening O.Strichman

Bit-Vectors

Chapter 6

Revision 1.2

Page 2: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Outline

1 Introduction to Bit-Vector Logic

2 Syntax

3 Semantics

4 Decision procedures for Bit-Vector LogicFlattening Bit-Vector LogicIncremental Flattening

Decision Procedures – Bit-Vectors 2

Page 3: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Decision Procedures for System-Level Software

What kind of logic do we need for system-level software?

State { int created = 0; }

IoCreateDevice.exit {if ($return==STATUS SUCCESS)

created = 1;

}

IoDeleteDevice.exit { created = 0; }

fun AddDevice.exit {if (created && (pdevobj->Flags & DO DEVICE INITIALIZING) != 0) {abort "AddDevice routine failed to set "

"~DO DEVICE INITIALIZING flag";

}}

��

Bit-wise AND

An Invariant of Microsoft Windows Device Drivers

Decision Procedures – Bit-Vectors 3

Page 4: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Decision Procedures for System-Level Software

What kind of logic do we need for system-level software?

State { int created = 0; }

IoCreateDevice.exit {if ($return==STATUS SUCCESS)

created = 1;

}

IoDeleteDevice.exit { created = 0; }

fun AddDevice.exit {if (created && (pdevobj->Flags & DO DEVICE INITIALIZING) != 0) {abort "AddDevice routine failed to set "

"~DO DEVICE INITIALIZING flag";

}}

��

Bit-wise AND

An Invariant of Microsoft Windows Device Drivers

Decision Procedures – Bit-Vectors 3

Page 5: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Decision Procedures for System-Level Software

What kind of logic do we need for system-level software?

State { int created = 0; }

IoCreateDevice.exit {if ($return==STATUS SUCCESS)

created = 1;

}

IoDeleteDevice.exit { created = 0; }

fun AddDevice.exit {if (created && (pdevobj->Flags & DO DEVICE INITIALIZING) != 0) {abort "AddDevice routine failed to set "

"~DO DEVICE INITIALIZING flag";

}}

��

Bit-wise AND

An Invariant of Microsoft Windows Device Drivers

Decision Procedures – Bit-Vectors 3

Page 6: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Decision Procedures for System-Level Software

What kind of logic do we need for system-level software?

We need bit-vector logic – with bit-wise operators, arithmeticoverflow

We want to scale to large programs – must verify largeformulas

Examples of program analysis tools that generate bit-vectorformulas:

CBMCSATABSF-Soft (NEC)SATURN (Stanford, Alex Aiken)EXE (Stanford, Dawson Engler, David Dill)Variants of those developed at IBM, Microsoft

Decision Procedures – Bit-Vectors 4

Page 7: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Decision Procedures for System-Level Software

What kind of logic do we need for system-level software?

We need bit-vector logic – with bit-wise operators, arithmeticoverflow

We want to scale to large programs – must verify largeformulas

Examples of program analysis tools that generate bit-vectorformulas:

CBMCSATABSF-Soft (NEC)SATURN (Stanford, Alex Aiken)EXE (Stanford, Dawson Engler, David Dill)Variants of those developed at IBM, Microsoft

Decision Procedures – Bit-Vectors 4

Page 8: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Bit-Vector Logic: Syntax

formula : formula ∨ formula | ¬formula | atomatom : term rel term | Boolean-Identifier | term[ constant ]

rel : = | <

term : term op term | identifier | ∼ term | constant |atom?term:term |term[ constant : constant ] | ext( term )

op : + | − | · | / | << | >> | & | | | ⊕ | ◦

∼ x: bit-wise negation of x

ext(x): sign- or zero-extension of x

x << d: left shift with distance d

x ◦ y: concatenation of x and y

Decision Procedures – Bit-Vectors 5

Page 9: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Bit-Vector Logic: Syntax

formula : formula ∨ formula | ¬formula | atomatom : term rel term | Boolean-Identifier | term[ constant ]

rel : = | <

term : term op term | identifier | ∼ term | constant |atom?term:term |term[ constant : constant ] | ext( term )

op : + | − | · | / | << | >> | & | | | ⊕ | ◦

∼ x: bit-wise negation of x

ext(x): sign- or zero-extension of x

x << d: left shift with distance d

x ◦ y: concatenation of x and y

Decision Procedures – Bit-Vectors 5

Page 10: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics

Danger!

(x− y > 0) ⇐⇒ (x > y)

Valid over R/N, but not over the bit-vectors.(Many compilers have this sort of bug)

Decision Procedures – Bit-Vectors 6

Page 11: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Width and Encoding

The meaning depends on the width and encoding of thevariables.

Typical encodings:

Binary encoding

〈x〉U :=l−1∑i=0

ai · 2i

Two’s complement

〈x〉S := −2n−1 · an−1 +l−2∑i=0

ai · 2i

But maybe also fixed-point, floating-point, . . .

Decision Procedures – Bit-Vectors 7

Page 12: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Width and Encoding

The meaning depends on the width and encoding of thevariables.

Typical encodings:

Binary encoding

〈x〉U :=l−1∑i=0

ai · 2i

Two’s complement

〈x〉S := −2n−1 · an−1 +l−2∑i=0

ai · 2i

But maybe also fixed-point, floating-point, . . .

Decision Procedures – Bit-Vectors 7

Page 13: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Examples

〈11001000〉U = 200

〈11001000〉S = −128 + 64 + 8 = −56

〈01100100〉S = 100

Decision Procedures – Bit-Vectors 8

Page 14: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Width and Encoding

Notation to clarify width and encoding:

x[32]S

���

Width in bits@

@I

U: unsigned binaryS: signed two’s complement

Decision Procedures – Bit-Vectors 9

Page 15: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Width and Encoding

Notation to clarify width and encoding:

x[32]S

���

Width in bits@

@I

U: unsigned binaryS: signed two’s complement

Decision Procedures – Bit-Vectors 9

Page 16: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Bit-vectors Made Formal

Definition (Bit-Vector)

A bit-vector is a vector of Boolean values with a given length l:

b : {0, . . . , l − 1} −→ {0, 1}

The value of bit number i of x is x(i).

︸ ︷︷ ︸l bits

b0b1b2bl−1 bl−2

We also write xi for x(i).

Decision Procedures – Bit-Vectors 10

Page 17: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Bit-vectors Made Formal

Definition (Bit-Vector)

A bit-vector is a vector of Boolean values with a given length l:

b : {0, . . . , l − 1} −→ {0, 1}

The value of bit number i of x is x(i).

︸ ︷︷ ︸l bits

b0b1b2bl−1 bl−2

We also write xi for x(i).

Decision Procedures – Bit-Vectors 10

Page 18: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Lambda-Notation for Bit-Vectors

λ expressions are functions without a name

Examples:

The vector of length l that consists of zeros:

λi ∈ {0, . . . , l − 1}.0

A function that inverts (flips all bits in) a bit-vector:

bv -invert(x) := λi ∈ {0, . . . , l − 1}.¬xi

A bit-wise OR:

bv -or(x, y) := λi ∈ {0, . . . , l − 1}.(xi ∨ yi)

=⇒ we now have semantics for the bit-wise operators.

Decision Procedures – Bit-Vectors 11

Page 19: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Lambda-Notation for Bit-Vectors

λ expressions are functions without a name

Examples:

The vector of length l that consists of zeros:

λi ∈ {0, . . . , l − 1}.0

A function that inverts (flips all bits in) a bit-vector:

bv -invert(x) := λi ∈ {0, . . . , l − 1}.¬xi

A bit-wise OR:

bv -or(x, y) := λi ∈ {0, . . . , l − 1}.(xi ∨ yi)

=⇒ we now have semantics for the bit-wise operators.Decision Procedures – Bit-Vectors 11

Page 20: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Example

(x[10] ◦ y[5])[14] ⇐⇒ x[9]

This is translated as follows:

x[9] = x9

(x ◦ y) = λi.(i < 5)?yi : xi−5

(x ◦ y)[14] = (λi.(i < 5)?yi : xi−5)(14)

Final result:

(λi.(i < 5)?yi : xi−5)(14) ⇐⇒ x9

Decision Procedures – Bit-Vectors 12

Page 21: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Example

(x[10] ◦ y[5])[14] ⇐⇒ x[9]

This is translated as follows:

x[9] = x9

(x ◦ y) = λi.(i < 5)?yi : xi−5

(x ◦ y)[14] = (λi.(i < 5)?yi : xi−5)(14)

Final result:

(λi.(i < 5)?yi : xi−5)(14) ⇐⇒ x9

Decision Procedures – Bit-Vectors 12

Page 22: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Example

(x[10] ◦ y[5])[14] ⇐⇒ x[9]

This is translated as follows:

x[9] = x9

(x ◦ y) = λi.(i < 5)?yi : xi−5

(x ◦ y)[14] = (λi.(i < 5)?yi : xi−5)(14)

Final result:

(λi.(i < 5)?yi : xi−5)(14) ⇐⇒ x9

Decision Procedures – Bit-Vectors 12

Page 23: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Example

(x[10] ◦ y[5])[14] ⇐⇒ x[9]

This is translated as follows:

x[9] = x9

(x ◦ y) = λi.(i < 5)?yi : xi−5

(x ◦ y)[14] = (λi.(i < 5)?yi : xi−5)(14)

Final result:

(λi.(i < 5)?yi : xi−5)(14) ⇐⇒ x9

Decision Procedures – Bit-Vectors 12

Page 24: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Example

(x[10] ◦ y[5])[14] ⇐⇒ x[9]

This is translated as follows:

x[9] = x9

(x ◦ y) = λi.(i < 5)?yi : xi−5

(x ◦ y)[14] = (λi.(i < 5)?yi : xi−5)(14)

Final result:

(λi.(i < 5)?yi : xi−5)(14) ⇐⇒ x9

Decision Procedures – Bit-Vectors 12

Page 25: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Arithmetic Expressions

What is the output of the following program?

unsigned char number = 200;number = number + 100;printf("Sum: %d\n", number);

On most architectures, this is 44!

11001000 = 200+ 01100100 = 100

= 00101100 = 44

=⇒ Bit-vector arithmetic uses modular arithmetic!

Decision Procedures – Bit-Vectors 13

Page 26: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Arithmetic Expressions

What is the output of the following program?

unsigned char number = 200;number = number + 100;printf("Sum: %d\n", number);

On most architectures, this is 44!

11001000 = 200+ 01100100 = 100

= 00101100 = 44

=⇒ Bit-vector arithmetic uses modular arithmetic!

Decision Procedures – Bit-Vectors 13

Page 27: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Arithmetic Expressions

What is the output of the following program?

unsigned char number = 200;number = number + 100;printf("Sum: %d\n", number);

On most architectures, this is 44!

11001000 = 200+ 01100100 = 100

= 00101100 = 44

=⇒ Bit-vector arithmetic uses modular arithmetic!

Decision Procedures – Bit-Vectors 13

Page 28: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Arithmetic Expressions

Semantics for addition, subtraction:

a[l] +U b[l] = c[l] ⇐⇒ 〈a〉U + 〈b〉U = 〈c〉U mod 2l

a[l] −U b[l] = c[l] ⇐⇒ 〈a〉U − 〈b〉U = 〈c〉U mod 2l

a[l] +S b[l] = c[l] ⇐⇒ 〈a〉S + 〈b〉S = 〈c〉S mod 2l

a[l] −S b[l] = c[l] ⇐⇒ 〈a〉S − 〈b〉S = 〈c〉S mod 2l

We can even mix the encodings:

a[l]U +U b[l]S = c[l]U ⇐⇒ 〈a〉U + 〈b〉S = 〈c〉U mod 2l

Decision Procedures – Bit-Vectors 14

Page 29: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Arithmetic Expressions

Semantics for addition, subtraction:

a[l] +U b[l] = c[l] ⇐⇒ 〈a〉U + 〈b〉U = 〈c〉U mod 2l

a[l] −U b[l] = c[l] ⇐⇒ 〈a〉U − 〈b〉U = 〈c〉U mod 2l

a[l] +S b[l] = c[l] ⇐⇒ 〈a〉S + 〈b〉S = 〈c〉S mod 2l

a[l] −S b[l] = c[l] ⇐⇒ 〈a〉S − 〈b〉S = 〈c〉S mod 2l

We can even mix the encodings:

a[l]U +U b[l]S = c[l]U ⇐⇒ 〈a〉U + 〈b〉S = 〈c〉U mod 2l

Decision Procedures – Bit-Vectors 14

Page 30: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Arithmetic Expressions

Semantics for addition, subtraction:

a[l] +U b[l] = c[l] ⇐⇒ 〈a〉U + 〈b〉U = 〈c〉U mod 2l

a[l] −U b[l] = c[l] ⇐⇒ 〈a〉U − 〈b〉U = 〈c〉U mod 2l

a[l] +S b[l] = c[l] ⇐⇒ 〈a〉S + 〈b〉S = 〈c〉S mod 2l

a[l] −S b[l] = c[l] ⇐⇒ 〈a〉S − 〈b〉S = 〈c〉S mod 2l

We can even mix the encodings:

a[l]U +U b[l]S = c[l]U ⇐⇒ 〈a〉U + 〈b〉S = 〈c〉U mod 2l

Decision Procedures – Bit-Vectors 14

Page 31: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Relational Operators

Semantics for <, ≤, ≥, and so on:

a[l]U < b[l]U ⇐⇒ 〈a〉U < 〈b〉Ua[l]S < b[l]S ⇐⇒ 〈a〉S < 〈b〉S

Mixed encodings:

a[l]U < b[l]S ⇐⇒ 〈a〉U < 〈b〉Sa[l]S < b[l]U ⇐⇒ 〈a〉S < 〈b〉U

Note that most compilers don’t support comparisons with mixedencodings.

Decision Procedures – Bit-Vectors 15

Page 32: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Semantics for Relational Operators

Semantics for <, ≤, ≥, and so on:

a[l]U < b[l]U ⇐⇒ 〈a〉U < 〈b〉Ua[l]S < b[l]S ⇐⇒ 〈a〉S < 〈b〉S

Mixed encodings:

a[l]U < b[l]S ⇐⇒ 〈a〉U < 〈b〉Sa[l]S < b[l]U ⇐⇒ 〈a〉S < 〈b〉U

Note that most compilers don’t support comparisons with mixedencodings.

Decision Procedures – Bit-Vectors 15

Page 33: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Complexity

Satisfiability is undecidable for an unbounded width, evenwithout arithmetic.

It is NP-complete otherwise.

Decision Procedures – Bit-Vectors 16

Page 34: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Complexity

Satisfiability is undecidable for an unbounded width, evenwithout arithmetic.

It is NP-complete otherwise.

Decision Procedures – Bit-Vectors 16

Page 35: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

A Simple Decision Procedure

Transform Bit-Vector Logic to Propositional Logic

Most commonly used decision procedure

Also called ’bit-blasting’

Bit-Vector Flattening

1 Convert propositional part as before

2 Add a Boolean variable for each bit of each sub-expression(term)

3 Add constraint for each sub-expression

We denote the new Boolean variable for bit i of term t by µ(t)i.

Decision Procedures – Bit-Vectors 17

Page 36: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

A Simple Decision Procedure

Transform Bit-Vector Logic to Propositional Logic

Most commonly used decision procedure

Also called ’bit-blasting’

Bit-Vector Flattening

1 Convert propositional part as before

2 Add a Boolean variable for each bit of each sub-expression(term)

3 Add constraint for each sub-expression

We denote the new Boolean variable for bit i of term t by µ(t)i.

Decision Procedures – Bit-Vectors 17

Page 37: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Bit-vector Flattening

What constraints do we generate for a given term?

This is easy for the bit-wise operators.

Example for a|[l]b:

l−1∧i=0

(µ(t)i = (ai ∨ bi))

(read x = y over bits as x ⇐⇒ y)

We can transform this into CNF using Tseitin’s method.

Decision Procedures – Bit-Vectors 18

Page 38: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Bit-vector Flattening

What constraints do we generate for a given term?

This is easy for the bit-wise operators.

Example for a|[l]b:

l−1∧i=0

(µ(t)i = (ai ∨ bi))

(read x = y over bits as x ⇐⇒ y)

We can transform this into CNF using Tseitin’s method.

Decision Procedures – Bit-Vectors 18

Page 39: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Bit-vector Flattening

What constraints do we generate for a given term?

This is easy for the bit-wise operators.

Example for a|[l]b:

l−1∧i=0

(µ(t)i = (ai ∨ bi))

(read x = y over bits as x ⇐⇒ y)

We can transform this into CNF using Tseitin’s method.

Decision Procedures – Bit-Vectors 18

Page 40: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Flattening Bit-Vector Arithmetic

How to flatten a + b?

−→ we can build a circuit that adds them!

FA

iba

so

Full Adder

s ≡ (a + b + i ) mod 2 ≡ a⊕ b⊕ i

o ≡ (a + b + i ) div 2 ≡ a · b + a · i + b · i

The full adder in CNF:

(a ∨ b ∨ ¬o) ∧ (a ∨ ¬b ∨ i ∨ ¬o) ∧ (a ∨ ¬b ∨ ¬i ∨ o)∧(¬a ∨ b ∨ i ∨ ¬o) ∧ (¬a ∨ b ∨ ¬i ∨ o) ∧ (¬a ∨ ¬b ∨ o)

Decision Procedures – Bit-Vectors 19

Page 41: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Flattening Bit-Vector Arithmetic

How to flatten a + b?

−→ we can build a circuit that adds them!

FA

iba

so

Full Adder

s ≡ (a + b + i ) mod 2 ≡ a⊕ b⊕ i

o ≡ (a + b + i ) div 2 ≡ a · b + a · i + b · i

The full adder in CNF:

(a ∨ b ∨ ¬o) ∧ (a ∨ ¬b ∨ i ∨ ¬o) ∧ (a ∨ ¬b ∨ ¬i ∨ o)∧(¬a ∨ b ∨ i ∨ ¬o) ∧ (¬a ∨ b ∨ ¬i ∨ o) ∧ (¬a ∨ ¬b ∨ o)

Decision Procedures – Bit-Vectors 19

Page 42: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Flattening Bit-Vector Arithmetic

Ok, this is good for one bit! How about more?

8-Bit ripple carry adder (RCA)

i

FA FA FA FA FA FA FA FA

a7b7 a6b6 a5b5 a5b4 a4b3 a3b2 a2b1 a0b0

os7 s6 s5 s4 s3 s2 s1 s0

Also called carry chain adder

Adds l variables

Adds 6 · l clauses

Decision Procedures – Bit-Vectors 20

Page 43: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Flattening Bit-Vector Arithmetic

Ok, this is good for one bit! How about more?

8-Bit ripple carry adder (RCA)

i

FA FA FA FA FA FA FA FA

a7b7 a6b6 a5b5 a5b4 a4b3 a3b2 a2b1 a0b0

os7 s6 s5 s4 s3 s2 s1 s0

Also called carry chain adder

Adds l variables

Adds 6 · l clauses

Decision Procedures – Bit-Vectors 20

Page 44: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Multipliers

Multipliers result in very hard formulas

Example:

a · b = c ∧ b · a 6= c ∧ x < y ∧ x > y

CNF: About 11000 variables, unsolvable for current SATsolvers

Similar problems with division, modulo

Q: Why is this hard?

Q: How do we fix this?

Decision Procedures – Bit-Vectors 21

Page 45: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Multipliers

Multipliers result in very hard formulas

Example:

a · b = c ∧ b · a 6= c ∧ x < y ∧ x > y

CNF: About 11000 variables, unsolvable for current SATsolvers

Similar problems with division, modulo

Q: Why is this hard?

Q: How do we fix this?

Decision Procedures – Bit-Vectors 21

Page 46: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incremental Flattening

?

ϕf := ϕsk , F := ∅

?

Is ϕf SAT?

?No!

UNSAT

-Yes! compute I

?I = ∅

SAT

6I 6= ∅

Pick F ′ ⊆ (I \ F )F := F ∪ F ′

ϕf := ϕf ∧Constraint(F )�

ϕsk : Boolean part of ϕF : set of terms that are in the encoding

I: set of terms that are inconsistent with the current assignment

Decision Procedures – Bit-Vectors 22

Page 47: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incremental Flattening

?

ϕf := ϕsk , F := ∅

?

Is ϕf SAT?

?No!

UNSAT

-Yes! compute I

?I = ∅

SAT

6I 6= ∅

Pick F ′ ⊆ (I \ F )F := F ∪ F ′

ϕf := ϕf ∧Constraint(F )�

ϕsk : Boolean part of ϕF : set of terms that are in the encoding

I: set of terms that are inconsistent with the current assignment

Decision Procedures – Bit-Vectors 22

Page 48: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incremental Flattening

?

ϕf := ϕsk , F := ∅

?

Is ϕf SAT?

?No!

UNSAT

-Yes! compute I

?I = ∅

SAT

6I 6= ∅

Pick F ′ ⊆ (I \ F )F := F ∪ F ′

ϕf := ϕf ∧Constraint(F )�

ϕsk : Boolean part of ϕF : set of terms that are in the encoding

I: set of terms that are inconsistent with the current assignment

Decision Procedures – Bit-Vectors 22

Page 49: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incremental Flattening

?

ϕf := ϕsk , F := ∅

?

Is ϕf SAT?

?No!

UNSAT

-Yes! compute I

?I = ∅

SAT

6I 6= ∅

Pick F ′ ⊆ (I \ F )F := F ∪ F ′

ϕf := ϕf ∧Constraint(F )�

ϕsk : Boolean part of ϕF : set of terms that are in the encodingI: set of terms that are inconsistent with the current assignment

Decision Procedures – Bit-Vectors 22

Page 50: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incremental Flattening

?

ϕf := ϕsk , F := ∅

?

Is ϕf SAT?

?No!

UNSAT

-Yes! compute I

?I = ∅

SAT

6I 6= ∅

Pick F ′ ⊆ (I \ F )F := F ∪ F ′

ϕf := ϕf ∧Constraint(F )�

ϕsk : Boolean part of ϕF : set of terms that are in the encodingI: set of terms that are inconsistent with the current assignment

Decision Procedures – Bit-Vectors 22

Page 51: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incremental Flattening

?

ϕf := ϕsk , F := ∅

?

Is ϕf SAT?

?No!

UNSAT

-Yes! compute I

?I = ∅

SAT

6I 6= ∅

Pick F ′ ⊆ (I \ F )F := F ∪ F ′

ϕf := ϕf ∧Constraint(F )�

ϕsk : Boolean part of ϕF : set of terms that are in the encodingI: set of terms that are inconsistent with the current assignment

Decision Procedures – Bit-Vectors 22

Page 52: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incremental Flattening

Idea: add ’easy’ parts of the formula first

Only add hard parts when needed

ϕf only gets stronger – use an incremental SAT solver

Decision Procedures – Bit-Vectors 23

Page 53: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incomplete Assignments

Hey: initially, we only have the skeleton!How do we know what terms are inconsistent with the currentassignment if the variables aren’t even in ϕf?

Solution: guess some values for the missing variables.If you guess right, it’s good.

Ideas:

All zerosSign extension for signed bit-vectorsTry to propagate constants (a = b + 1)

Decision Procedures – Bit-Vectors 24

Page 54: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incomplete Assignments

Hey: initially, we only have the skeleton!How do we know what terms are inconsistent with the currentassignment if the variables aren’t even in ϕf?

Solution: guess some values for the missing variables.If you guess right, it’s good.

Ideas:

All zerosSign extension for signed bit-vectorsTry to propagate constants (a = b + 1)

Decision Procedures – Bit-Vectors 24

Page 55: Decision Procedures - Bit-Vectors · 2019-10-07 · Decision Procedures – Bit-Vectors 4. Decision Procedures for System-Level Software What kind of logic do we need for system-level

Incomplete Assignments

Hey: initially, we only have the skeleton!How do we know what terms are inconsistent with the currentassignment if the variables aren’t even in ϕf?

Solution: guess some values for the missing variables.If you guess right, it’s good.

Ideas:

All zerosSign extension for signed bit-vectorsTry to propagate constants (a = b + 1)

Decision Procedures – Bit-Vectors 24