45
Program verification Generalities about software Verification – Model Checking Laure Gonnord David Monniaux September 20, 2016 1 / 43

Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Program verificationGeneralities about software Verification – Model Checking

Laure Gonnord David Monniaux

September 20, 2016

1 / 43

Page 2: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

The teaching staff

I Laure Gonnord, associate professor, LIP laboratory,University of [email protected]

I David Monniaux, CNRS senior researcher, VERIMAGlaboratory, [email protected]

2 / 43

Page 3: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Schedule

Software, bugs, and proofs

States, reachability, and all that

Explicit (finite state) Forward Model Checking

3 / 43

Page 4: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Safe software ?

I safety-critical software (control of vehicles e.g. airplanesand cars, surgical robots, radiation therapy. . . )

I less critical software (flight management systems,financial transactions, unmanned spacecraft. . . )

I general-purpose software?

I (hot topic) software facing the Internet, newer securityand privacy issues (e.g. 0-day vulnerabilities sold tointelligence services; are lives at stake ?)

4 / 43

Page 5: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Software engineering

Considers the means of production of software:

I documentation imperatives

I organization of software development teams

I good programming practices

I use of appropriate programming languages

I software development environments

Try to reduce the number of errors at the source.

“Software metrics”Use of lint-like tools or more advanced

Not covered in this course

5 / 43

Page 6: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Proving properties of software

I Basic idea: software has mathematically definedbehaviour

I Possible to do mathematical proofs on software

I Possible to automate these proofs

6 / 43

Page 7: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Program proofs

Proving that software truly does what it is meant to do.

behaviours ⊆ acceptable behaviours

I What does software do?

I What is it meant to do?

I What is a proof?

7 / 43

Page 8: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Semantics

A precise definition of what a program does — given for allprograms within a programming language.

Very difficult for a full industrial language e.g. C++

vs

A definition in ± vague natural language (e.g. ISO C andC++ standards, programming language manuals. . . )

Imprecise, fuzzy, sometimes contradictory definitions.Language lawyers. Endless discussions on what a programshould or should not be doing, on what a compiler has theright to do or not.

8 / 43

Page 9: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Specification

What software should do

I informal definition in natural language

I formal mathematical definition

Is the specification consistent?

Difficulties in writing specifications:

I Are all requirements taken into account?

I Redundancy with implementation

9 / 43

Page 10: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Specification example: sort

Unix command sort

(Without the options) Simple informal specification: “sort thelines in a file”

In more detail: complicated — e.g

I what is the sorting order wrt non-ASCII characters?

I how are equivalent lines sorted (e.g. numeric ordering)

Mathematical definition possible, but long.

10 / 43

Page 11: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Difficulty

behaviours ⊆ acceptable behaviours

Both sets are not well defined in general.

May need to fix

I language definition

I target compilation environment (evaluation order, size ofbasic types, alignment. . . )

I precise specification

How about proofs?

11 / 43

Page 12: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

The Halting Problem

Simple language: integers (Z), tests, loops

There is no algorithm that says, given a program,whether this program halts. (Turing)

12 / 43

Page 13: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

The Halting Problem, proof - Program Version.

Suppose we have a “magical analyzer” A: answer A(P ,X ) = 1if “program P terminates eventually on input X” A(P ,X ) = 0otherwise.

int B(Program x) {

if (A(x,x)==0) {

return 1;

} else {

while(true) {}

}

}

What is B(B)? (B applied to its own source code)

13 / 43

Page 14: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

The Halting Problem, contradiction

int B(Program x) {

if (A(x,x)==0) {

return 1;

} else {

while(true) {}

}

}

If B(B) = 1 then A(B ,B) = 0 “program B does notterminate on input B”. Absurd!

If B(B) loops then A(B ,B) = 1 “program B terminates oninput B”. Absurd!

There is no magical static analyser.

14 / 43

Page 15: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Workarounds

What is impossible is to check reachability

1. automatically

2. without false positives

3. without false negatives

4. on systems of unbounded state

5. with unbounded execution time

Lifting restrictions opens possibilities!

15 / 43

Page 16: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Course plan

Fully automatic decision procedures.

1. Course 1 : Finite state or infinite state model checking

2. Course 2 & 3 : Abstract interpretation (no false negative)

3. 2 Labs → one project.

4. Course 4 & 5 : Applications to safety-critical softwareverification, termination, worst-case execution time. . .

5. Paper reading → talks.

16 / 43

Page 17: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Schedule

Software, bugs, and proofs

States, reachability, and all that

Explicit (finite state) Forward Model Checking

17 / 43

Page 18: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Starting states + transitions

State of the program / of the machine = values of variables,registers, memories. . . within Σ.

Par exemple :

I if system state = 17 Booleans, then Σ = {0, 1}17;

I if system state = 3 unbounded integers, then Σ = Z3;

I if finite automaton, Σ is the set of states;

I if stack automaton, state = pair (automaton state, stackcontents), so Σ = ΣS × Σ∗P .

Transition relation → : x → y = “if I’m at x I can go to yat the next step”

18 / 43

Page 19: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Safety properties - Simple case

Show that a program cannot reach a “bad state” (crash,out-of-specification).Set W of bad states.

Show that there is no n ≥ 0 and σ0 → σ1 → . . . σn, σ0 initialstate (= reset), σn ∈ W (trace of n steps leading to a badstate).

Otherwise said: σ0 →∗ σn ∈ W . →∗ reflexive transitiveclosure of →.

19 / 43

Page 20: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Reachable states

Let Σ0 ⊆ Σ be the initial states.The set A of reachable states is the set of states σ such that

∃σ0 ∈ Σ0 σ0 →∗ σ (1)

We want to show that A ∩W = ∅.

20 / 43

Page 21: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Bounding the state space

Restrict to a finite number of variables of a finite type.

Finite state space =⇒ “it’s just a big finite automaton!”

Everything is decidable!

21 / 43

Page 22: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Schedule

Software, bugs, and proofs

States, reachability, and all that

Explicit (finite state) Forward Model Checking

22 / 43

Page 23: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Explicit-state model checking

Given a transition relation τ

I Set R := {initialstate}I For each state x in R , add all x ′ such that (x , x ′) |= τ

I Do it until R is saturated (no new states are added)

I Then R is the set of reachable states.

Then test whether R contains undesirable states.

23 / 43

Page 24: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Implementation issues

If state = n Boolean variables, 2n possible states.

Memory usage linear in number of reachable states.

Store states in hash table.Store states in distributed hash table.

Tool example: CADP (INRIA Grenoble)

24 / 43

Page 25: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Explicit state model checking, a weakness

Representation expensive even if the set of reachable states is“simple”.e.g. {0, 1}n “everything reachable” needs Θ(2n) memory

Try to compress sets of states by symbolic representation.

25 / 43

Page 26: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Reachable states as a limit

Xn is the set of states reachable within n steps of→: X0 = Σ0,X1 = Σ0 ∪ R(Σ0), X2 = Σ0 ∪ R(Σ0) ∪ R(R(Σ0)), etc.

with R(X ) = {y ∈ Σ | ∃x ∈ X x → y}.

Xk grows wrt ⊆.Its limit (= union of all terms) is the set of reachable states.

26 / 43

Page 27: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Iterative computation

Remark Xn+1 = Σ0 ∪ R(Xn).

Intuition: to reach in at most n + 1 steps

I either in 0 steps = initial states Σ0

I either in 0 < k ≤ n + 1 steps, thus in at most n steps(Xn) followed by another step

But how to efficiently represent the Xn and compute overthem?

27 / 43

Page 28: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

The problem

Representing compactly sets of Boolean states

A set of vector n Booleans = a function from {0, 1}n into{0, 1}.

Example: {(0, 0, 0), (1, 1, 0)} represented by (0, 0, 0) 7→ 1,(1, 1, 0) 7→ 1 and 0 elsewhere.

28 / 43

Page 29: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Expanded BDD

Binary decision diagrams

Given ordered Boolean variables (a, b, c), represent(a ∧ c) ∨ (b ∧ c) :

a

bb

c c c c

0 1 100 0 10

0 1

29 / 43

Page 30: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Removing useless nodes

Silly to keep two identical subtrees:

a

bb

c c c c

0 1 100 0 10

0 1

identiquesidentiques

30 / 43

Page 31: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Compression

c

10

a

b

c

0 1

0 1

0

identiques

31 / 43

Page 32: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Reduced BDD

a

b

c

0 1

0

0

1

Idea: turn the original tree into a DAG with maximalsharing.

Two different but isomorphic subtrees are never created.Canonicity: a given example is always encoded by the sameDAG.

32 / 43

Page 33: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Implementation: hash-consing

Important: implementation technique that you may use inother contexts

“Consing” from “constructor” (cf Lisp : cons).

In computer science, particularly in functionalprogramming, hash consing is a technique used toshare values that are structurally equal. [...] Aninteresting property of hash consing is that twostructures can be tested for equality in constanttime, which in turn can improve efficiency of divideand conquer algorithms when data sets containoverlapping blocks.

https://en.wikipedia.org/wiki/Hash_consing

33 / 43

Page 34: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Implementation: hash-consing 2

Keep a hash table of all nodes created, with hashcode H(x)computed quickly.If node = (v , b0, b1) compute H from v and unique identifiersof b0 and b1

Unique identifier = address (if unmovable) or serial number

If an object matching (v , b0, b1) already exists in the table,return it

How to collect garbage nodes? (unreachable)

34 / 43

Page 35: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Garbage collection in hash consing

Needs weak pointers: the pointer from the hash table shouldbe ignored by the GC when it computes reachable objects

I Java WeakHashMap

I OCaml Weak

(Other use of weak pointers: caching recent computations.)

35 / 43

Page 36: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Garbage collection in hash consing

Needs weak pointers: the pointer from the hash table shouldbe ignored by the GC when it computes reachable objects

I Java WeakHashMap

I OCaml Weak

(Other use of weak pointers: caching recent computations.)

35 / 43

Page 37: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Hash-consing is magical

Ensures:

I maximal sharing: never two identical objects in two 6=locations in memory

I ultra-fast equality test: sufficient to compare pointers(or unique identifiers)

And once we have it, BDDs are easy.

36 / 43

Page 38: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

BDD operations

Once a variable ordering is chosen:

I Create BDD false, true(1-node constants).

I Create BDD for v , for v any variable.

I Operations ∧, ∨, etc.

37 / 43

Page 39: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Binary BDD operations

Operations ∧, ∨: recursive descent on both subtrees, withmemoizing:

I store values of f (a, b) already computed in a hash table

I index the table by the unique identifiers of a and b

Complexity with and without dynamic programming?

I without dynamic programming: unfolds DAG into tree ⇒exponential

I with dynamic programming O(|a|.|b|) where |x | the sizeof DAG x

38 / 43

Page 40: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Binary BDD operations

Operations ∧, ∨: recursive descent on both subtrees, withmemoizing:

I store values of f (a, b) already computed in a hash table

I index the table by the unique identifiers of a and b

Complexity with and without dynamic programming?

I without dynamic programming: unfolds DAG into tree ⇒exponential

I with dynamic programming O(|a|.|b|) where |x | the sizeof DAG x

38 / 43

Page 41: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Quantifiers

BDD for formula F over variables x , y , z .Want a BDD for formula ∃x F over variables y et z .[∃x F ](y , z) ≡ F (0, y , z) ∨ F (1, y , z): computeF [0/x ] ∨ F [1/x ] (F [b/x ] is F where x has been replaced byb).

Same for ∀ but with ∧.

Otherwise said quantifier elimination.

39 / 43

Page 42: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Back to transition systems

I The set Σ0 of initial states is defined by a formula overx1, . . . , xn ⇒ a BDD over n variables.

I The transition relation T over Boolean variablesx1, . . . , xn, x

′1, . . . , x

′n (x ′ = updated x) ⇒ a BDD over 2n

variables.

Recall φ(X ) = Σ0 ∪ R(X ), in formulas:

φ(X ) = Σ0 ∨ (∃x1, . . . , xn(X ∧ T ))[x ′1/x1, . . . , x′n/xn] (2)

All operations doable on BDDs!

40 / 43

Page 43: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Iterative computations over BDDs

Compute sequence X0, . . . with X0 = Σ0 and Xn+1 = φ(Xn),stop when Xn = Xn+1 (recall: ultra-fast equality test!)

(Or stop when Xi intersects bad states.)

Sounds very simplebut many possible optimizations and variants (e.g. signedBDDs), much work needed

In practice, need other operators (e.g. “constrain”,“restrict”. . . )

41 / 43

Page 44: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Industrial use: hardware

Clocked hardware ' reset state + transition relation

Checking properties of circuits during conception (buildingprototypes is very expensive)

Tools such as Cadence-SMV

42 / 43

Page 45: Program veri cationlaure.gonnord.org/pro/teaching/CR10_1617/cours1_intro_BDD_SAT_B… · Program veri cation Generalities about software Veri cation { Model Checking Laure Gonnord

Bounded model checking

BDDs are too costly (worst-case exponential time and space)

Unbounded reachability in Boolean circuits isPSPACE-complete

Idea: limit search to n steps, “only” NP-completeComing next : Bounded MC

43 / 43