Bound Analysis of Imperative Programs with the Size change - - PowerPoint PPT Presentation

bound analysis of imperative programs with the size
SMART_READER_LITE
LIVE PREVIEW

Bound Analysis of Imperative Programs with the Size change - - PowerPoint PPT Presentation

Bound Analysis of Imperative Programs with the Size change Abstraction Florian Zuleger, TU Vienna SAS, Venice, 16.09.2011 Joint work with Sumit Gulwani, Microsoft Research Moritz Sinn, Helmut Veith, TU Vienna Resource Bounds Programs consume a


slide-1
SLIDE 1

Bound Analysis of Imperative Programs with the Size‐change Abstraction

Florian Zuleger, TU Vienna SAS, Venice, 16.09.2011 Joint work with Sumit Gulwani, Microsoft Research Moritz Sinn, Helmut Veith, TU Vienna

slide-2
SLIDE 2

Resource Bounds

Programs consume a variety of resources:

– CPU time, memory, network bandwidth, power

Bounding the use of such resources is important:

– Economic incentives – Better user experience – Hard constraints on availability of resources

Program correctness depends on bounding quantitative properties of data:

– Information leakage, Propagation of numerical errors

slide-3
SLIDE 3

The Reachability‐bound Problem

(Gulwani, Zuleger, PLDI 2010) Given a control location l inside a program P. How often can l be visited inside P?

void main (int n, C[] temp) { int i := 0; while (i < n) { int j := i+1; while (j < n) { if (nondet()) {

l: temp[n] := new C();

j--; n--; } j++; } i++; } }

Goal: A symbolic bound Bound(l) in terms of the inputs of P.

slide-4
SLIDE 4

Bound Computation and Termination

A bound for a loop implies the termination of the loop. ⇒ Computing bounds is more difficult than proving termination! Can successfull techniques for termination analysis be extended to bound analysis? What about

  • Size‐change Abstraction (Ben‐Amram, Lee, Jones, 2001)
  • Transition Invariants (Podelski, Rybalchenko, 2004)?

Not so easy...

Yes!

slide-5
SLIDE 5

Outline

  • 1. Introduction
  • 2. Comparing SCA with Transition Invariants
  • 3. SCA solves Technical Challanges
  • 4. How to apply SCA on Imperative Programs
slide-6
SLIDE 6

Size‐change Abstraction (SCA)

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y‐1 = y‘

slide-7
SLIDE 7

Size‐change Abstraction (SCA)

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l α(ρ1) α(ρ2)

Predicate abstract domain consisting of inequalities between integer variables (primed and unprimed)

α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘

slide-8
SLIDE 8

Size‐change Abstraction (SCA)

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l α(ρ1) α(ρ2) α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘

Finite powerset abstract domain whose base elements are conjuncts of inequalities between integer variables (primed and unprimed)

slide-9
SLIDE 9

Size‐change Abstraction (SCA)

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l α(ρ1) α(ρ2)

Control flow graph whose edges are labeled by size‐change graphs

x y 0’ x’ y’

= < = > <

x y 0’ x’ y’

= < > <

α(ρ1) ≡ α(ρ2) ≡

=

slide-10
SLIDE 10

SCA is a Success Story

  • Termination is decidable in PSPACE

(Ben‐Amram, Lee, Jones, 2001; Ben‐Amram,2011)

  • Complete method for extracting ranking functions
  • n terminating instances (possibly exponentially

large)

  • SCA based termination analysis is implemented in

widely‐used systems such as ACL2, Isabelle, AProVE

  • The industrial‐strength tool ACL2 can automatically

prove the termination of 98% of the functions in its database

slide-11
SLIDE 11

Good Computational Properties

  • Enjoys built‐in disjunction.
  • Transitive hulls can be computed without
  • verapproximation techniques such as

widening.

  • Transitive hulls preserve termination.
  • Abstraction can be done by SMT solver calls.

⇒ Potential for automation

slide-12
SLIDE 12

Transition Invariants

  • Have been developed as adaption of SCA to

imperative programs

  • Experimentally proven useful on device drivers

in the Terminator tool; see Cook et al. 2006

  • More general than SCA; for formal comparison

see Heizmann et al. 2011

slide-13
SLIDE 13

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

slide-14
SLIDE 14

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

Well‐founded relations

slide-15
SLIDE 15

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

Well‐founded relations x and y are local ranking functions

slide-16
SLIDE 16

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

Transitive hull in the concrete Well‐founded relations x and y are local ranking functions

slide-17
SLIDE 17

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else { x := n; y--; } }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

slide-18
SLIDE 18

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else { x := n; y--; } }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

We reset x to n!

slide-19
SLIDE 19

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else { x := n; y--; } }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

We reset x to n! Same termination proof!

slide-20
SLIDE 20

Transition Invariants

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else { x := n; y--; } }

l ρ1 ρ2 ρ1 ≡ x > 0 Æ y > 0 Æ x‐1 = x‘ Æ y = y‘ ρ2 ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y‐1 = y‘

Termination proof: (ρ1 ∪ ρ2)+ ⊆ T1 ∪ T2,

where T1 ≡ x > 0 Æ x‘ = x‐1 and T2 ≡ y > 0 Æ y‘ = y‐1

We reset x to n! Same termination proof!

slide-21
SLIDE 21

Size‐change Abstraction (SCA)

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; } void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else { x := n; y--; } }

α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘ α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y > y‘

slide-22
SLIDE 22

Size‐change Abstraction (SCA)

void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else y--; } void main (int n) { int x=n; int y=n; l: while (x>0 Æ y>0) { if (nondet()) x--; else { x := n; y--; } }

α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘ α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y > y‘

slide-23
SLIDE 23

Bounds by SCA

Our bound algorithm for SCA:

  • uses only the abstracted transitions
  • discovers x and y as norms by heuristics

x and y stay constant on the respective other transition Our tool computes the ranking function x+y, which results in Bound(l) = 2n

  • nly x is increased on

the other transition Our tool computes the ranking function (x,y), which results in Bound(l) = n2 α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘ α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y > y‘

slide-24
SLIDE 24

Outline

  • 1. Introduction
  • 2. Comparing SCA with Transition Invariants
  • 3. SCA solves Technical Challanges
  • 4. How to apply SCA on Imperative Programs
slide-25
SLIDE 25

SCA solves Technical Challanges

We do not use SCA because we like the formalism, but because we believe that SCA is the right abstraction for the bound analysis of imperative programs.

slide-26
SLIDE 26

Technical Challenges

I. Bounds are often non‐linear expressions

  • II. Proving a bound often requires disjunctive

invariants

  • III. Bounds cannot be predicted by templates
  • IV. How to exploit program structure for bound

computation is unclear

slide-27
SLIDE 27

Bounds by SCA

Our bound algorithm for SCA:

  • uses only the abstracted transitions
  • discovers x and y as norms by heuristics

x and y stay constant on the respective other transition Our tool computes the ranking function x+y, which results in Bound(l) = 2n

  • nly x is increased on

the other transition Our tool computes the ranking function (x,y), which results in Bound(l) = n2 α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘ α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y > y‘

slide-28
SLIDE 28

Bounds by SCA

Our bound algorithm for SCA:

  • uses only the abstracted transitions
  • discovers x and y as norms by heuristics

x and y stay constant on the respective other transition Our tool computes the ranking function x+y, which results in Bound(l) = 2n

  • nly x is increased on

the other transition Our tool computes the ranking function (x,y), which results in Bound(l) = n2 α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘ α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y > y‘

slide-29
SLIDE 29

Bounds by SCA

Our bound algorithm for SCA:

  • uses only the abstracted transitions
  • discovers x and y as norms by heuristics

x and y stay constant on the respective other transition Our tool computes the ranking function x+y, which results in Bound(l) = 2n

  • nly x is increased on

the other transition Our tool computes the ranking function (x,y), which results in Bound(l) = n2 α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ x = x‘ Æ y > y‘ α(ρ1) ≡ x > 0 Æ y > 0 Æ x > x‘ Æ y = y‘ α(ρ2) ≡ x > 0 Æ y > 0 Æ n = x‘ Æ y > y‘

slide-30
SLIDE 30

Outline

  • 1. Introduction
  • 2. Comparing SCA with Transition Invariants
  • 3. SCA solves Technical Challanges
  • 4. How to apply SCA on Imperative Programs
slide-31
SLIDE 31

How to apply SCA on Imperative Programs

I. Transition System Generation by Pathwise Analysis

  • II. Heuristics for Extracting Norms
  • III. Dealing with Control Structure of Loops by

Contextualization

slide-32
SLIDE 32

Transition System Generation by Pathwise Analysis

Goal: Transition System for l1 Idea: Enumerating all paths from l1 to l1

ρ1 ≡ i < n Æ i‘ = i + 1 Æ j‘ = 0 ρ2 ≡ j > 0 Æ i‘ = i ‐ 1 ρ3 ≡ j ≤ 0 ρ4 ≡ i < n Æ i‘ = i + 1 Æ j‘ = j + 1 l1 l2 ρ1 ρ2 ρ3 ρ4

slide-33
SLIDE 33

Transition System Generation by Pathwise Analysis

We first summarize the inner loop.

ρ1 ≡ i < n Æ i‘ = i + 1 Æ j‘ = 0 ρ2 ≡ j > 0 Æ i‘ = i ‐ 1 ρ3 ≡ j ≤ 0 ρ4 ≡ i < n Æ i‘ = i + 1 Æ j‘ = j + 1 l1 l2 ρ1 ρ2 ρ3 ρ4

slide-34
SLIDE 34

Transition System Generation by Pathwise Analysis

We first summarize the inner loop. Using this summary we compute a transitition system for the outer loop.

ρ1 ≡ i < n Æ i‘ = i + 1 Æ j‘ = 0 ρ2 ≡ j > 0 Æ i‘ = i ‐ 1 ρ3 ≡ j ≤ 0 ρ4 ≡ i < n Æ i‘ = i + 1 Æ j‘ = j + 1 l1 l2 ρ1 ρ2 ρ3 ρ4

slide-35
SLIDE 35

Transition System of the Outer Loop

ρ1 ≡ i < n Æ i‘ = i + 1 Æ j‘ = 0 ρ2 ≡ j > 0 Æ i‘ = i ‐ 1 ρ3 ≡ j ≤ 0 ρ4a ≡ i‘ = i Æ j‘ = j ρ4b ≡ i < n Æ i‘ > i Æ j‘ > j Summary[l2] = {ρ4a, ρ4a} l1 l2 ρ1 ρ2 ρ3

We obtain Summary[l2] by:

  • 1. Recursively computing a transition system for the

inner loop and size‐change abstracting it.

  • 2. Computing the transitive hull using SCA.
slide-36
SLIDE 36

Transition System of the Outer Loop

ρ1 ≡ i < n Æ i‘ = i + 1 Æ j‘ = 0 ρ2 ≡ j > 0 Æ i‘ = i ‐ 1 ρ3 ≡ j ≤ 0 ρ4a ≡ i‘ = i Æ j‘ = j ρ4b ≡ i < n Æ i‘ > i Æ j‘ > j Summary[l2] = {ρ4a, ρ4a}

We obtain a transition system for l1 by enumerating all paths using the different disjuncts of the summary:

{ρ1 ◦ ρ4a ◦ ρ2, ρ1 ◦ ρ4a ◦ ρ3 , ρ1 ◦ ρ4b ◦ ρ2, ρ1 ◦ ρ4b ◦ ρ3} l1 l2 ρ1 ρ2 ρ3

slide-37
SLIDE 37

Transition System Generation by Pathwise Analysis

ρ1 ≡ i < n Æ i‘ = i + 1 Æ j‘ = 0 ρ2 ≡ j > 0 Æ i‘ = i ‐ 1 ρ3 ≡ j ≤ 0 ρ4a ≡ i‘ = i Æ j‘ = j ρ4b ≡ i < n Æ i‘ > i Æ j‘ > j Summary[l2] = {ρ4a, ρ4a}

We obtain a transition system for l1 by enumerating all paths using the different disjuncts of the summary:

{false, i < n Æ i‘ = i + 1 Æ j‘ = 0, i < n Æ i‘ > i Æ j‘ > 0, false} = {i < n Æ i‘ = i + 1 Æ j‘ = 0, i < n Æ i‘ > i Æ j‘ > 0} l1 l2 ρ1 ρ2 ρ3

slide-38
SLIDE 38

Discussion of Pathwise Analysis

  • Pathprecise reasoning: abstraction or

infeasibility analysis of complete paths

  • Leverages the progress in SMT solver

technology to static analysis

  • Generalization of classical SCA
  • More precise than blockwise analysis
slide-39
SLIDE 39

Discussion of Pathwise Analysis

  • Pathprecise reasoning: abstraction or

infeasibility analysis of complete paths

  • Leverages the progress in SMT solver

technology to static analysis

  • Generalization of classical SCA
  • More precise than blockwise analysis
slide-40
SLIDE 40

How to apply SCA on Imperative Programs

I. Transition System Generation by Pathwise Analysis

  • II. Heuristics for Extracting Norms
  • III. Dealing with Control Structure of Loops by

Contextualization

slide-41
SLIDE 41

Given some transition system, its set of norms is the union of the norms of all its transitions.

Let ρ be the formula of some transition.

If the inequality e1 ≥ e2 syntactically appears in ρ, then e1‐e2 is a candidate for an arithmetic norm. We check with an SMT solver for each candidate e: If ρ ⇒ e[X’/X] ≤ e‐1, then e is a norm.

i’≥i+1 Æ i<n n‐i Example

Norms

This pattern‐based technique readily extends to non‐arithmetic norms: Proof rules for bitvectors and data‐structures can be found in Gulwani, Zuleger, 2010.

slide-42
SLIDE 42

How to apply SCA on Imperative Programs

I. Transition System Generation by Pathwise Analysis

  • II. Heuristics for Extracting Norms
  • III. Dealing with Control Structure of Loops by

Contextualization

slide-43
SLIDE 43

Contextualization

The first step in bound analysis is the construction of a program such that ‐ every location stores the information what transition is executed next, and ‐ only feasible transitions are added. Construction is done by SMT solver queries.

l ρ1ρ2 ρ3 ρ4 ρ5 ρ6

Computed transition system:

slide-44
SLIDE 44

Contextualization

l1 l2 l3 l4 l6 l5 ρ2 ρ1 ρ2 ρ1 ρ3 ρ4 ρ3 ρ4 ρ5 ρ6 ρ5 ρ6

The first step in bound analysis is the construction of a program such that ‐ every location stores the information what transition is executed next, and ‐ only feasible transitions are added. Construction is done by SMT solver queries. Contextualized transition system:

slide-45
SLIDE 45

DAG of SCCs

The CFG can be decomposed into its DAG of SCCs. ⇒ Uncovers the control structure

  • f the loop.

l1 l2 l3 l4 l6 l5 ρ2 ρ1 ρ2 ρ1 ρ3 ρ4 ρ3 ρ4 ρ5 ρ6 ρ5 ρ6 Bounds are computed in two steps:

  • 1. Bounds are computed for every

SCC in isolation

  • 2. These bounds are composed to

an overall bound using the DAG structure.

slide-46
SLIDE 46

Loopus

  • Built over LLVM Compiler Framework, inputs C source code
  • Uses Yices solver as the logical reasoning engine.
  • Aliasing was handled using optimistic assumptions.
  • 4090 of 4302 loops of the cBench benchmark handled in less than

1000 seconds (3923 loops in less than 4 seconds)

  • Success ratio of 75% for computing loop bounds.
  • Representative failure cases:

– Insufficient invariant analysis – Memory updates and pointer arithmetic – Irreducible CFGs not implemented – Loops that are not meant to terminate – Complex invariants would be needed

slide-47
SLIDE 47

Conclusion

Size‐change Abstraction is the right abstraction for bound analyis of imperative programs:

  • We have given the first algorithm for

computing bounds with SCA

  • We have shown how to apply SCA to

imperative programs

slide-48
SLIDE 48

Questions?