86
Using Types For Software Verification Ranjit Jhala, UC San Diego (with Pat Rondon, Ming Kawaguchi)

Using Types For Software Verification

  • Upload
    triage

  • View
    47

  • Download
    0

Embed Size (px)

DESCRIPTION

Using Types For Software Verification. Ranjit Jhala , UC San Diego (with Pat Rondon , Ming Kawaguchi). Once Upon a time …. char* rev_copy ( char* a, int n){ i = 0; j = n – 1; b = malloc (n); while (0

Citation preview

Page 1: Using  Types For  Software Verification

Using Types For Software Verification

Ranjit Jhala, UC San Diego(with Pat Rondon, Ming Kawaguchi)

Page 2: Using  Types For  Software Verification

char* rev_copy(char* a, int n){

i = 0; j = n – 1; b = malloc(n); while(0<=j){ b[i] = a[j]; i++; j--; } return b;}

Once Upon a time …

Page 3: Using  Types For  Software Verification

char* rev_copy(char* a, int n){

i = 0; j = n – 1; b = malloc(n); while(0<=j){ b[i] = a[j]; i++; j--; } return b;}

Memory Safety Verification

Access Within Array Bounds

Page 4: Using  Types For  Software Verification

char* rev_copy(char* a, int n){

i = 0; j = n – 1; b = malloc(n); while(j>=0){ b[i] = a[j]; i++; j--; } return b;}

assert (0<=i && i<n);

0:

1: 2:

How to prove assert never fails ?

assert (i<n);

0: i = 0; j = n–1; 1: while (0<=j){ 2: assert(i<n); i = i+1; j = j–1; }Access Within Array Bounds

Page 5: Using  Types For  Software Verification

How to prove asserts?Invariants [Floyd-Hoare]

Page 6: Using  Types For  Software Verification

Invariants

Predicate that is always true@ Program Location

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

true

i+j=n-1

i+j=n-1 Æ 0·j

Invariant Proves Assert

Page 7: Using  Types For  Software Verification

How to Prove Asserts?How to Find Invariants?

Page 8: Using  Types For  Software Verification

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

?

What are Invariants ?

??

Page 9: Using  Types For  Software Verification

What are Invariants ?

Let Xi = Invariant @ location i

Page 10: Using  Types For  Software Verification

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

?

What are Invariants ?

??

X0

X1

X2Properties of X0,X1,X2?

Page 11: Using  Types For  Software Verification

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

What are Invariants ?

X0

Initial Values ArbitraryX0= true

Page 12: Using  Types For  Software Verification

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

What are Invariants ?

i=0 Æ j=n-1 )

X1

true

X1

Page 13: Using  Types For  Software Verification

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

What are Invariants ?

0·j Æ X1 ) X2

X1X2

Page 14: Using  Types For  Software Verification

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

What are Invariants ?

X2 ) i<n

X2

Page 15: Using  Types For  Software Verification

0: i = 0; j = n–1; 1: while (0<=j){

2: assert(i<n);

i = i+1; j = j–1; }

What are Invariants ?

i=io+1 Æ j=jo-1 Æ [io/i][jo/j]X2 )

X1

X1X2

Page 16: Using  Types For  Software Verification

What are Invariants ?

… Æ [io/i][jo/j]X2 ) X1

Predicates X1, X2 s.t.

i=0 Æ j=n-1 ) X1

0·j Æ X1 ) X2

X2 ) i<n

Page 17: Using  Types For  Software Verification

How to Find Invariants? Solve for X1, X2...

(or, #yourfavoriteabstractinterpretation)Via SMT + Pred. Abstraction

Page 18: Using  Types For  Software Verification

RecapSafety

Invariants

Implications

AI, PA, CEGAR,…

X0 , X1

X0 ) X1

Page 19: Using  Types For  Software Verification

Plan

Classic SW Verification is hard to automate. Types offer automationand expressiveness too!

Page 20: Using  Types For  Software Verification

So, what’s hard?

Page 21: Using  Types For  Software Verification

int kmp_search(char str[], char pat[]){ p = 0; s = 0; while (p<pat.length && s<str.length){ if (str[s] == pat[p]){s++; p++;} else if (p == 0){s++;} else{p = table[p-1] + 1;} } if (p >= plen) {return (s-plen)}; return (-1);}

Need Universally Quantified Invariants

8i: 0·i<table.length )-1·table[i] Every element of table exceeds -1Prove Access Within Array Bounds

Page 22: Using  Types For  Software Verification

Need Universally Quantified Invariants

More complex for lists, trees, etc.

8x: next*(root,x) ) -1 · x.data

Page 23: Using  Types For  Software Verification

Quantifiers Kill SMT Solvers

Page 24: Using  Types For  Software Verification

Quantifiers Kill SMT SolversHow to Generalize and Instantiate?

Page 25: Using  Types For  Software Verification

Key: Invariants Without Quantifiers

Page 26: Using  Types For  Software Verification

Plan

Classic SW Verification is hard to automate. Types offer automationand expressiveness too!

Page 27: Using  Types For  Software Verification

Idea: Logically Qualified TypesFactor Invariant to Logic x Type

Idea: Liquid Types

Page 28: Using  Types For  Software Verification

LogicDescribes Individual Data

TypeQuantifies over Structure

Page 29: Using  Types For  Software Verification

factored into

8i: 0 ·i<table.length )-1· table[i]

table :: {v:int|-1 · v} array

Type Logic

Page 30: Using  Types For  Software Verification

factored into

8x: next*(root,x) )-1 · x.data

root :: {v:int|-1 · v} list

Type Logic

Page 31: Using  Types For  Software Verification

LogicDescribes Individual Data

TypeQuantifies over Structure

Theorem ProverReasoning about Individual Data

Type SystemQuantified Reasoning about Structure

Page 32: Using  Types For  Software Verification

Demo“Map-Reduce”

Page 33: Using  Types For  Software Verification

“Map-Reduce”map :: (e -> (k, v) list) -> e list -> (k, v) list

group :: (k, v) list -> (k, v list)

tablereduce :: (v -> v -> v) -> (k, v list)

table -> (k, v) table

Page 34: Using  Types For  Software Verification

K-Means Clustering

Page 35: Using  Types For  Software Verification

0. Choose K Centers Arbitrarily

Page 36: Using  Types For  Software Verification

1. (Map) Points to Nearest Center

Page 37: Using  Types For  Software Verification

2. (Group) Points by Center

Page 38: Using  Types For  Software Verification

3. (Reduce) Centroids into New Centers

Page 39: Using  Types For  Software Verification

Repeat 1,2,3 Until Convergence

Page 40: Using  Types For  Software Verification

DemoK-Means via Map-Reduce

Page 41: Using  Types For  Software Verification

Base TypesCollections

ClosuresGenerics

Page 42: Using  Types For  Software Verification

let rec ffor l u f =

if l < u then ( f l; ffor (l+1) u f )

Type of f

int ! unitTemplate of f

{v:int|X1}!unit

Liquid Type of f

{v:int|l·v Æ v<u} ! unit

l Flows Into Input of f {v:int|v=l} <: {v:int|X1}

l<u |-

l<u Æ v=l ) X1

Solution X1 = l·v Æ v<u

Reduces to

Page 43: Using  Types For  Software Verification

Base TypesCollections

ClosuresGenerics

Collections(Structure)

Page 44: Using  Types For  Software Verification

let group kvs = let t = H.create 37 in List.iter (fun (k,v) -> let vs = H.mem t k ? H.find t k : [] in H.add t k (v::vs) ) kvs; t

Page 45: Using  Types For  Software Verification

let vs = H.mem t k ? H.find t k : [] in

H.add t k (v::vs)

Types

t: (’a,’b list) H.t vs: ’b list

Templates

t (’a,{v:’b list| X1}) H.t

vs {v:’b list| X2}

{v:’b list|len v=0} <: {v:’b list| X2}

{v:’b list| X1} <: {v:’b list| X2}X1 ) X2

len v=0 ) X2

vs:{X2}|-{len v=len vs + 1} <: {X1}

X2[vs/v] Æ len v=len vs + 1 ) X1

Solution X1 = 0 < len vX2 = 0 ·len v

Liquid Type of t

(’a,{v:’b list| 0 < len v}) H.t

Page 46: Using  Types For  Software Verification

Collections(Data)

Page 47: Using  Types For  Software Verification

let nearest dist ctra x = let da = Array.map (dist x) ctra in

[min_index da, (x, 1)]Type of Output

int * ’b * int listTemplate of Output

{v:int | X1} * ’b * {v:int | X2} list

(’a !’b)!x:’a array!{v:’b array|len x = len v}

Liquid Type of

x:’a array!{v:int| 0·v Æ v < len x}

min_index da {v:int| 0·v Æ v < len da}da {v:’b array| len v = len ctra}

len da = len ctra Æ 0·v<len da ) X1

len da = len ctra Æ v=1 ) X2

da:{len v = len ctra}|-{ 0·v<len da} * ’b * {v=1} list <: {X1} * ’b * {X2}

list

Reduces To

Solution X1 = 0·v < len ctra X2 = 0 < v

Liquid Type of Output{v:int|0·v<len ctra}*’b*{v:int|0<v}

list

Page 48: Using  Types For  Software Verification

Base TypesCollections

ClosuresGenerics

Page 49: Using  Types For  Software Verification

let min_index a = let min = ref 0 in ffor 0 (Array.length a) (fun i -> if a.(i) < a.(!min) then min :=

i ); !min

Liquid Type of ffor 0 (len a)

({v:int|0· v < len a} ! unit)! unit

Template of (fun i ->...)

{v:int|Xi} ! unit

{Xi}!unit <: {0·v<len a}!unit{0·v<len a} unit{Xi} unit

{0·v<len a} <: {Xi}

Reduces To

unit <: unit0· v < len a ) Xi

Solution Xi = 0·v< len a

Liquid Type of (fun i ->...) {v:int|0·v<len a} ! unit

Liquid Type of fforl:int!u:int!({v:int|l·v<u}!unit)!unit

Liquid Type of ffor 0u:int!({v:int|0·v< u} ! unit)! unit

Page 50: Using  Types For  Software Verification

Base TypesCollections

ClosuresGenerics

Page 51: Using  Types For  Software Verification

mapreduce (nearest dist ctra) (centroid plus) xs

|> List.iter (fun (i,(x,sz)) -> ctra.(i)<- div x

sz) Type of mapreduce(’a !’b * ’c list) !...! ’b * ’c list

Template of mapreduce(’a ! {X1} * ’a * {X2} list)!...! {X1} * ’a * {X2} list

Type Instantiation ’a with ’a ’b with int

’c with ’a * int

Template Instantiation ’a with ’a

’b with {v:int|X1}

’c with ’a * {v:int|X2}

Liquid Type of (nearest dist ya)’a ! {0 · v < len ctra} * ’a * {0<v} list’a ! {0 · v < len ctra} * ’a * {0<v} list

<:’a ! {X1} * ’a * {X2} list

Solution X1 = 0 · v < len ctra X2 = 0 < v

Reduces To0 · v < len ctra ) X1

0 < v ) X2

Liquid Type of mapreduce Output {0 · v < len ctra} * ’a * {0 < v} list

Page 52: Using  Types For  Software Verification

Generics = “Meta” Invariants

Page 53: Using  Types For  Software Verification

Generics = “Meta Invariants”

foldl :: (a->b-> a)-> a-> b list-> a

Page 54: Using  Types For  Software Verification

Generics = “Meta Invariants”

foldl :: (a->b-> a)-> a-> b list-> a

Initial Value Satisfies a

Page 55: Using  Types For  Software Verification

Generics = “Meta Invariants”

foldl :: (a->b-> a)-> a-> b list-> a

Each “Iteration” Preserves a

Page 56: Using  Types For  Software Verification

Generics = “Meta Invariants”

foldl :: (a->b-> a)-> a-> b list-> a

Hence, Output Satisfies a

Page 57: Using  Types For  Software Verification

Generics = “Meta Invariants”

foldl :: (a->b-> a)-> a-> b list-> a

At callsite instantiate a for invariant!Iterated structure hidden from SMT

Page 58: Using  Types For  Software Verification

Plan

Classic SW Verification is hard to automate. Types offer automationand expressiveness too!

Page 59: Using  Types For  Software Verification

Liquid TypesExpressive

Page 60: Using  Types For  Software Verification

Data Structure Invariants

Piggyback Predicates On Types

Page 61: Using  Types For  Software Verification

[] ::

{x:int|0<x} listint list0<x Describes all elementsx:int

Representation

Page 62: Using  Types For  Software Verification

[] ::

0<x

x:int

Type Unfolding

[] ::

0<h

h:int

[] ::

0<x

x:int

Head TailEmptyPositive Property holds recursivelyList of positive integers

Page 63: Using  Types For  Software Verification

[] ::

0<x Describes all elementsx:int

x<v v Describes tail elements

Representation

Page 64: Using  Types For  Software Verification

[] ::

x<v

x:int

Type Unfolding

[] ::

h:int

[] ::

x<v

x:int

Head TailEmptyElements larger than head Property holds recursively

List of sorted integers

h<v

Push Edge Predicate Into NodeRename Variable

h<x

Page 65: Using  Types For  Software Verification

1. Representation2. Instantiation 3. Generalization

Piggyback Predicates on Type

Page 66: Using  Types For  Software Verification

Leaf

l r

l = Left subtreer = Right subtree

treeHeight

H l = Left subtree’s heightH r = Right subtree’s height

measure H =

| Leaf = 0| Node(x,l,r) = 1 + max (H l) (H r)

Height Balanced Tree

|Hl–Hr|<2

Node

Height difference bounded at each node

Page 67: Using  Types For  Software Verification

Piggyback Predicates On Types

Data Structure Invariants

Page 68: Using  Types For  Software Verification

[] ::

x:intUnfold

::

h:int

[] ::

x:int

l:sorted list h:int t:sorted

list & {h<x}

list

Instantiate

tl

match l with

h::t

x<Vx<V

h<x

Quantifier Instantiation

Page 69: Using  Types For  Software Verification

Piggyback Predicates On Types

Data Structure Invariants

Page 70: Using  Types For  Software Verification

[] ::

x:intFold

h:int

[] ::

x:int

::

l:sorted list h:int t:sorted

list & {h<x}

list

Generalize

tl

let l = h::t in

x<Vx<V

h<x

Quantifier Generalization

Page 71: Using  Types For  Software Verification

Liquid TypesAutomaticExpressive

Automatic Liquid Type InferenceBy Predicate Abstraction

Page 72: Using  Types For  Software Verification

0<x

[] ::

x:int

x<v

Automatic Liquid Type Inference

Predicates Determine InvariantLet X1, X2, ... = Unknown Predicates

Complex Subtyping Between data types

X1

X2

Reduces To Simple Implications Between X1, X2, ...

Solved by Predicate AbstractionOver atoms 0<x, x<v, ...

Page 73: Using  Types For  Software Verification

Demo

Page 74: Using  Types For  Software Verification

C or ML + Asserts Safe+Types

Error+TypesDsolve

Results

Atoms

Page 75: Using  Types For  Software Verification

Program (ML) Verified InvariantsList-based Sorting Sorted, Outputs Permutation of Input

Finite Map Balance, BST, Implements a SetRed-Black Trees Balance, BST, Color

Stablesort SortedExtensible Vectors Balance, Bounds Checking, …

Binary Heaps Heap, Returns Min, Implements SetSplay Heaps BST, Returns Min, Implements Set

Malloc Used and Free Lists Are AccurateBDDs Variable Order

Union Find AcyclicityBitvector Unification Acyclicity

Page 76: Using  Types For  Software Verification

Memory Safety of C Programs

Verified PropertySpatial Memory SafetyNo Buffer OverflowsNo Null Dereferences

Program (C) Lines Data Structures Usedstringlists 72 Arrays, Linked Lists

strcpy 77 Arraysadpcm 198 Arrays

pagemap 250 Arrays, Linked Listsmst 309 Arrays, Linked Lists, Graphs

power 620 Arrays, Linked Lists, Graphsks 650 Arrays, Linked Listsft 742 Arrays, Graphs

Page 77: Using  Types For  Software Verification

Take Home LessonsWhy is checking SW hard?Quantified invariants

How to avoid quantifiers? Factor invariant into liquid type

How to compute liquid type?SMT + Predicate Abstraction

Page 78: Using  Types For  Software Verification

“Back-End” LogicConstraint Solving

Rich Decidable Logics Qualifier Discovery…

Much Work Remains…

Page 79: Using  Types For  Software Verification

“Front-End” TypesDestructive Update

ConcurrencyObjects & Classes

Dynamic Languages…

Much Work Remains…

Page 80: Using  Types For  Software Verification

User InterfaceThe smarter your analysis,

the harder to tell why it fails!

Much Work Remains…

Page 81: Using  Types For  Software Verification

http://goto.ucsd.edu/liquidsource, papers, demo, etc.

Page 82: Using  Types For  Software Verification

Finite Maps (ML)5: ‘cat’

3: ‘cow’ 8: ‘tic’

1: ‘doc’ 4: ‘hog’ 7: ‘ant’ 9: ‘emu’From Ocaml Standard Library

Implemented as AVL TreesRotate/Rebalance on Insert/Delete

Verified InvariantsBinary Search Ordered

Height BalancedKeys Implement Set

Page 83: Using  Types For  Software Verification

Binary Decision Diagrams (ML)X1

X2 X2

X3

X4 X4

1

Graph-Based Boolean Formulas [Bryant 86]

X1ÛX2 Ù X3ÛX4 Efficient Formula Manipulation

Memoizing Results on SubformulasVerified Invariant

Variables Ordered Along Each Path

Page 84: Using  Types For  Software Verification

Vec: Extensible Arrays (317 LOC)

“Python-style” extensible arrays for Ocaml

find, insert, delete, join etc.

Efficiency via balanced trees

Balanced

Height difference between siblings ≤ 2

Dsolve found balance violation

Page 85: Using  Types For  Software Verification

fatal off-by-one error

Recursive Rebalance

Page 86: Using  Types For  Software Verification

Debugging via Inference

Using Dsolve we found

Where imbalance occurred

(specific path conditions)

How imbalance occurred

(left tree off by up to 4)

Leading to test and fix