Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

22
Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs Mike Barnett and Rustan Leino Microsoft Research Redmond, WA, USA

description

Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs. Mike Barnett and Rustan Leino Microsoft Research Redmond, WA, USA. Spec# Programming System. Spec#. runtime monitoring. Spec# compiler. MSIL (“bytecode”). Spec# program verifier. translator. inference engine. - PowerPoint PPT Presentation

Transcript of Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

Page 1: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

Well-cooked Spaghetti:

Weakest-Precondition of Unstructured Programs

Mike Barnett and Rustan LeinoMicrosoft ResearchRedmond, WA, USA

Page 2: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

2

Spec# Programming System

V.C. generator

automatictheorem prover

verification condition

Spec#

“correct” or list of errors

Spec# compilerMSIL (“bytecode”)translator

Boogie PLinference engine

Spec# program verifier

runtime monitoring

Page 3: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

3

Spec# demo

Page 4: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

4

Spec# Demo: What It Showed

• Non-null type system• Pre- and postconditions• Object invariants

• Dynamic monitoring• Static verification

Page 5: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

5

Verification Condition

• First-order logical formula• Valid if and only if program meets

its specification

• This leaves a lot of room for engineering…

Page 6: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

6

General Problem

• Duplication of postconditions for if-statements

wp( if (b) {x++;} else {x--;} , p ≠ null )= (b ⇒ wp( x++ , p ≠ null )) ∧ (¬b ⇒wp( x-- , p ≠ null ))

Page 7: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

7

Definition of Language

Program ::= Block+

Block ::= BlockId : Stmt; goto BlockId*

Stmt ::= VarId := Expr | havoc VarId| assert Expr | assume Expr| Stmt ; Stmt | skip

Page 8: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

8

Example

y := … x …

z := x

x := … x := …

z1 := x0

x1 := … x2 := …x1 := …x3 := x1

x2 := …x3 := x2

y1 := … x3 …

goto S,T;

assume E;S;

assume ¬E;T;

if (E){ S;}else{ T;}

Page 9: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

9

The recipe

0. Cut LoopsConsider arbitrary loop iteration

1. Passify the programCombine control flow and data flow

2. Construct weakest precondition formulaConnect the program to its semantics

Page 10: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

10

Cutting Loops: the general idea

x := …

Without a loop invariant, this loses all information about x.

havoc x;

Page 11: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

11

Cut Loops: Sub-steps

0.0. assert P assert P; assume P

0.1. Push asserts up-stream.0.2. Delete back edges after “havoc”-ing

loop targets.

Page 12: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

12

Cutting the loops… Putting the steps together

assert J;

x := …

assert J; assume J;assume J;

x := …assert J;

assert J; Note how this corresponds to the classical notion of a loop invariant: checking it in two places allows you to assume it at the top of the loop.

havoc x; assume J;

Page 13: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

13

Passification

y := … x …

z := x

x := … x := …

z1 := x0

x1 := … x2 := …x1 := …x3 := x1

x2 := …x3 := x2

y1 := … x3 …

assume z1 = x0

assume x1 = …assume x3 = x1

assume x2 = …assume x3 = x2

assume y1 = … x3 …

Page 14: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

14

Weakest Precondition

• For each block A, Aok holds when all executions starting at A are okay.

• Block Equation for each block A (BEA):

• VC (semantics of entire program):

• VC is obviously linear (in size of the passive program)

Aok ≡ wp(S, (∧ B ∈ Succ(A) : Bok))

(∧ A : BEA) ⇒ Startok

Page 15: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

15

Example

int M(int x) requires 100 <= x; ensures result == 0;{ while (0 < x) invariant 0 <= x; { x = x – 1; } return x;}

Page 16: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

16

CFG

assert 0 ≤ x;

assume 100 ≤ x;

assume 0 < x;x := x – 1;

assume ¬(0 < x);r := x;assert r = 0;

postcondition

precondition

loop invariant

loop guard

negation of guard

Body

Start

LoopHead

After

Page 17: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

17

Loop-Free CFG

havoc x;assume 0 ≤ x;

assume 100 ≤ x;assert 0 ≤ x;

assume 0 < x;x := x – 1;assert 0 ≤ x;assume ¬(0 <

x);r := x;assert r = 0;

Body

Start

LoopHead

After

Page 18: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

18

Passive Program

skip;assume 0 ≤ x1;

assume 100 ≤ x0;assert 0 ≤ x0;

assume 0 < x1;assume x2 = x1 – 1;assert 0 ≤ x2;assume ¬(0 <

x1);assume r1 = x1;assert r1 = 0;

Body

Start

LoopHead

After

Page 19: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

19

Block Equations

LoopHeadok ≡ 0 ≤ x1 ⇒ Bodyok ∧ Afterok

Startok ≡ 100 ≤ x0 ⇒ 0 ≤ x0∧ LoopHeadok

Bodyok ≡ 0 < x1 ⇒ x2 = x1 – 1 ⇒ 0 ≤ x2 ∧ true

Afterok ≡ ¬(0 < x1) ⇒ r1 = x1⇒r1 = 0 ∧ true

Page 20: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

20

Verification Condition

LoopHeadok ≡ 0 ≤ x1 ⇒ Bodyok ∧ Afterok

Bodyok ≡ 0 < x1 ⇒ x2 = x1 – 1 ⇒ 0 ≤ x2 ∧ true

Afterok ≡ ¬(0 < x1) ⇒ r1 = x1⇒r1 = 0 ∧ true

Startok

Startok ≡ 100 ≤ x0 ⇒ 0 ≤ x0∧ LoopHeadok

Page 21: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

21

Related Work

• Distribution via wlp: We could have done this too (it is ESC/Java’s technique), but converting to a structured program leads to a blow up (and requires heuristics).

• Possibly could have used a target language with “S!T” (and “raise”) for exceptional composition.

Page 22: Well-cooked Spaghetti: Weakest-Precondition of Unstructured Programs

22

Conclusion

• Room for more improvement– Recent experiments on directing

theorem prover along certain paths.• Useful even for systems that use

structured programs.