program verification with flow-effect types · down cycle up cycle …will tweak this cfg to bound...

23
Program Verification with Flow-Effect Types Paritosh Shroff Johns Hopkins University (joint work with Chris Skalka and Scott Smith)

Upload: others

Post on 14-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Program Verification with Flow-Effect Types

Paritosh ShroffJohns Hopkins University

(joint work with Chris Skalka and Scott Smith)

Page 2: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Refinements in Type Theory

Operational AwarenessSimple types (int, bool, τ → τ)Polymorphic types (′a, ′b, ′a → ′b)Subtypes (τ <: τ)Subtyping constraint types (t \ {τ1 <: τ2, τ3 <: τ4, …})Effect types (τ τ)Singleton types ({0}, {true})Flow-effect types

incorporate flow-sensitivity

⎯→⎯σ

unordered data-flowi.e. flow-insensitive

Page 3: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Temporally Ordered Data-Flow

let x = ref 5 inx := true;

!x ∧ false

Flow-sensitivity

x 5

No ML-style value restriction

Page 4: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Temporally Ordered Data-Flow

let x = ref 5 inx := true;

!x ∧ false

Flow-sensitivity

x true

No ML-style value restriction

Page 5: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Flow-Effect Types

Embody all of the computational structure of programsakin to A-normalized expressions

explicitly order atomic computation steps (δ)

(1 + 2) + 3 ½let x1 C {1} + {2} inlet x2 C x1 + {3} inx2

let x1 C δ1 inlet x2 C δ2 in…let xn C δ

nin

xn

Page 6: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Flow-Effect “Types”?Origins in type theory and shared methodologysubtyping constraints (data-flow) + trace-based effectsSequence of data-flows

[δ1 <: x1 ; δ2 <: x2 ; … ; δn <: xn]

let x1 C δ1 inlet x2 C δ2 in…let xn C δ

nin

xn

…as opposed to unordered set{δ1 <: x1 , δ2 <: x2 , … , δn <: xn}

λx.e ½ λx.let x1 C δ1 inlet x2 C δ2 in…let xn C δ

nin

xn

[δ1 <: x1 ; δ2 <: x2 ; … ; δn

<: xn]x xn≅

function body is the effect

Page 7: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Flow-Effect Type ClosureIdealized expression computation

abstract interpretation [Cousot and Cousot]higher-ordertrace-based [Colby and Lee, POPL’96]

Naϊve Closure: mimics expression computation i.e. runs themnon-diverging computations ⇒ no problem(λid.id(5) + 1; id(true)) (λx.x)

diverging computations ⇒ diverging closures(λx.x x) (λx.x x)

Goal: Find a sound approximation for divergingnaïve closures

Page 8: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Diverging Computations

Execute some piece of code infinitely oftenunbounded recursion ⇒ unbounded stack size(λx.x x) (λx.x x)

Crux of a Sound Decidable Closure (Ω-Closure)

bounded stack sizefix-point for recursive computations

prune-rerun technique

Page 9: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Structure of Recursionλsumn. if n = 0 then

0else

sum (n – 1) + n

sum (n)n = 0

true

false

0

sum (n – 1)rn – 1 + n

rn – 1

Control-flow graph (CFG) for operational behavior of ‘sum’

down cycle

up cycle

…will tweak this CFG to bound the stack

Page 10: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Abstract Int/Bool (…for now)

sum (n)n = {0}

{true}

{false}

{0}

sum (n – {1})rn – 1 + n

rn – 1

sum (n) assert(n = int)

int

sum (int)

rn – 1

assert(rn – 1 = int)

int

CFG for operational behavior of abstracted ‘sum’

only data is abstractednot the control-flow

Page 11: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

prune-rerun Technique

sum (n) assert(n = int)

int

sum (int)

rn – 1

assert(rn – 1 = int)

int

CFG for type closure of ‘sum’ via prune-rerun technique

sum (n) assert(n = int)

sum (int) B r′

int B r

int B r

r

assert(r′ = int)prune

rerun

prune ≡ “short-circuit” function call

Page 12: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Ω−Closure: sum (int)

n a int

n a intr′a r

n a intr′a rr a int

sum (int) assert(n = int)

sum (int) B r′

prune

int B r

int B r

r

assert(r′ = int)

inchoate

not fix-point(rerun)

sum (int)

n a intr′a rr a int

assert(n = int)

sum (int) B r′

prune

int B r

int B r

r

assert(r′ = int)

rerun

n a intr′a rr a int

n a intr′a rr a int

choate now ☺

fix-point(done ☺)

inchoate ∼ futures

Page 13: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Summary of Ω-Closure

Environment-basedmonotonic

Non Recursive Computationssimply run

Recursive Computationsprune recursive calls

rerun until fix-point (or an error) is found

Page 14: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Extensions to Ω-Closure

A. Singleton Types via Type Operators{true}, {false}, {1}, {2}, …∧, ∨, =, +, –, *, /, …

{1} + {2}, {true} ∧ {false}

B. Higher-Order Parametric Polymorphism via Argument Tagging

(λid.id (5) + 1; id (true) ∧ false) (λx.x)CPA-style [Ole Agesen, ECOOP’95]

C. Mutable State via Abstract Heap

Page 15: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Singleton Types via Type Operators

n a {5}n a {5}n a n – {1}r′a r

n a {5}n a n – {1}r′a rr a r′ + n

sum {5} n = {0}

sum (n – {1}) B r′

prune

r′ + n B r

r

not fix-point(rerun)

{false}

sum {5}

prune

rerun

{0} B rr

fix-point(after one more rerun)

n = {0}

n a {5}n a n – 1r′a rr a r′ + n

{false}

{true}

r′ + n B rsum (n – {1}) B r′

n a {5}n a n – {1}r′a rr a r′ + n

n a {5}n a n – {1}r′a rr a r′ + nr a {0}

Page 16: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Higher-Order Parametric Polymorphism via Argument Tagging

(λid.id {5} + {1}; id {true} ∧ {false}) (λx.x)

id {5} + {1}; id {true} ∧ {false} id a λx.x

x + {1}; id {true} ∧ {false} id a λx.xx a {5}

id {true} ∧ {false} id a λx.xx a {5}

x ∧ {false}id a λx.xx a {5}x a {true}

Type Error

Page 17: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Higher-Order Parametric Polymorphism via Argument Tagging

id a λx.x

id a λx.xx{5} a {5}

id a λx.xx{5} a {5}

id a λx.xx{5} a {5}x{true} a {true}

{false}

(λid.id {5} + {1}; id {true} ∧ {false}) (λx.x)

id {5} + {1}; id {true} ∧ {false}

x{5} + {1}; id {true} ∧ {false}

id {true} ∧ {false}

x{true} ∧ {false}

Page 18: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Mutable State via Abstract Heap

Mutable abstract heapref, get, set mimic run-time heap operationsRecursive data structures

collapsed to single abstract heap locationsE.g. linked-list

No need for ML-style value restrictionheap operations are flow-sensitivememory-based fixed points

Verifying Temporal Heap Properties [Yahav et al,ESOP’03]

Page 19: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Properties of Ω-Closure

Soundness If e has a type closure then it either diverges or computes to a value.

finite automaton simulates the execution of eComputability Type closure is computable for any e.

bounded stack depth, to number of functions in emonotonic environmentno new type creation ⇒ bounded environment

Page 20: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Conjectures about Ω-Closure

Completeness Ω-closure based flow-effect type system is HM-complete.

ordered subtyping constraint closureComplexity Ω-closure is computable in exponential time.

Page 21: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Applications

Model Checkingfinite automaton of program executioncontrol-flow + data-flow

Automated Verification of Programshigher-order

akin to ESP, ARCHER, SLAM for first-orderassert (x = y): verify program equivalences

Program Analysis in Compilers

Page 22: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Future Work

Apply to Java and ML-like languagesobject-oriented ⇒ higher-order features

Path-sensitivitytag branches before mergingsplit branches based on tag when needed

Inductive Assertionsassert (n ≥ 0)

Static array bounds check

Page 23: Program Verification with Flow-Effect Types · down cycle up cycle …will tweak this CFG to bound the stack. Abstract Int/Bool (…for now) sum (n) n = {0} {true} {false} {0} sum

Download

Ω-Closure based type system has been implemented

www.cs.jhu.edu/~pari/floweffecttypes