automating relatively complete verification of higher-order functional programs

26
Automating Relatively Complete Verification of Higher-Order Functional Programs Hiroshi Unno (University of Tsukuba) Tachio Terauchi (Nagoya University) Naoki Kobayashi (University of Tokyo) 2013/1/23 POPL 2013 1

Upload: lacy-faulkner

Post on 30-Dec-2015

37 views

Category:

Documents


0 download

DESCRIPTION

Automating Relatively Complete Verification of Higher-Order Functional Programs. Hiroshi Unno (University of Tsukuba) Tachio Terauchi (Nagoya University) Naoki Kobayashi (University of Tokyo). Path-Sensitive Verifier for Functional Programs (cf. SLAM, BLAST, โ€ฆ for Imperative Programs). - PowerPoint PPT Presentation

TRANSCRIPT

Automating Relatively Complete Verification of Higher-Order Functional Programs

Hiroshi Unno (University of Tsukuba)Tachio Terauchi (Nagoya University)

Naoki Kobayashi (University of Tokyo)

2013/1/23 POPL 2013 1

Path-Sensitive Verifier for Functional Programs(cf. SLAM, BLAST, โ€ฆ for Imperative Programs)

2013/1/23 POPL 2013 2

let rec mc x = if x > 100 then x โ€“ 10 else mc (mc (x + 11))inlet n = randi() inif n ยท 101 then assert (mc n = 91) Verifier

Program & Spec.Result

Certificateor

Counterexample

All these verifiers are based on refinement type system

(cf. Hoare logic for first-orderimperative programs)

Demo

โ€ข Refinement type inference by Horn clause solving [Unno and Kobayashi 2008, 2009]

โ€ข Liquid Types [Rondon, Kawaguchi and Jhala 2008, โ€ฆ]โ€ข Depcegar [Terauchi 2010]โ€ข MoCHi [Sato, Unno and Kobayashi 2011, 2013]โ€ข HMC [Jhala, Majumdar and Rybalchenko 2011]

Refinement Types

Non-negative integers

Functions that take an integer andreturn an integer not less than

2013/1/23 POPL 2013 3

FOL formulas for refinement

Soundness of refinement type system : is safe (i.e., )if is well-typed (i.e., )

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ ๐’™ ๐’‡= ๐’‡ ๐’™

Example: Typing Safe Program under

2013/1/23 POPL 2013 4

๐’™ : ๐ข๐ง๐ญโ†’ {๐‚โˆจ๐‚=๐’™ }โ†’๐ฎ๐ง๐ข๐ญ

{๐‚|๐‚=๐’Š }โ†’๐ฎ๐ง๐ข๐ญ

๐’™ : ๐ข๐ง๐ญโ†’ ( {๐‚|๐‚=๐’™ }โ†’๐ฎ๐ง๐ข๐ญ )โ†’๐ฎ๐ง๐ข๐ญ

( {๐‚|๐‚=๐’Š }โ†’๐ฎ๐ง๐ข๐ญ )โ†’๐ฎ๐ง๐ข๐ญ( {๐‚|๐‚=๐’Š }โ†’๐ฎ๐ง๐ข๐ญ )โ†’๐ฎ๐ง๐ข๐ญWell-typed!

Automated Verification viaRefinement Type Inference

โ€ข Input a program โ€ข Infer a type environment such that

(cf. invariant inference for Hoare logic)

2013/1/23 POPL 2013 5

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ ๐’™ ๐’‡= ๐’‡ ๐’™๐šช={๐š๐ฉ๐ฉ๐ฅ๐ฒโ†ฆ๐’™ : ๐ข๐ง๐ญโ†’ ( {๐‚|๐‚=๐’™ }โ†’๐ฎ๐ง๐ข๐ญ )โ†’๐ฎ๐ง๐ข๐ญ

๐œ๐ก๐ž๐œ๐คโ†ฆ๐’™ :๐ข๐ง๐ญโ†’ {๐‚|๐‚=๐’™ }โ†’๐ฎ๐ง๐ข๐ญ ,๐ฆ๐š๐ข๐งโ†ฆ๐ข๐ง๐ญโ†’๐ฎ๐ง๐ข๐ญ }

Limitation of Refinement Type System

Incompleteness: There is a safe but untypable program

2013/1/23 POPL 2013 6

whereas Hoare logic is relatively complete

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ๐ฌ๐ฐ ๐’‡ ๐’™= ๐’‡ ๐’™

Example: Safe but Untypable Program

2013/1/23 POPL 2013 7

{๐‚|๐‚=๐’Š }โ†’๐ฎ๐ง๐ข๐ญ

( {๐‚|๐‘ท (๐‚ )}โ†’๐ฎ๐ง๐ข๐ญ )โ†’ ๐’™ : {๐‚|๐‘ธ(๐‚)}โ†’๐ฎ๐ง๐ข๐ญ( {๐‚|๐‘ท (๐‚ )}โ†’๐ฎ๐ง๐ข๐ญ )โ†’ ๐’™ : {๐‚|๐‘ธ(๐‚)}โ†’๐ฎ๐ง๐ข๐ญCannot

depend on

Untypable because:

Refinement predicate for

Refinement predicate for the 1st arg. of

Our Contributions

โ€ข Relatively complete extension ofordinary refinement type system

โ€ข Type inference method for

2013/1/23 POPL 2013 8

Our Contributions

โ€ข Relatively complete extension ofordinary refinement type system

โ€ข Type inference method for

2013/1/23 POPL 2013 9

for any safe programgiven an oracle to decide the validity of

formulas of Peano arithmetic

Our Design Goal of

โ€ข Easy to automate type checking & inferenceโ€“ By exploiting techniques from first-order

automated theorem proving (e.g., interpolation, SMT)โ€ข Rejected alternative designs:โ€“ Refinement predicates on functions (cf. Coq)

โ€“ Unrestricted use of quantification (cf. Dependent ML)

2013/1/23 POPL 2013 10

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ๐ฌ๐ฐ ๐’‡ ๐’™= ๐’‡ ๐’™

Our Approach: RestrictedUse of Quantification

โ€ข Add one universal quantifier over integerjust before each function parameter[Goerdt 1985, German, Clarke, and Halpern 1983, 1989]

2013/1/23 POPL 2013 11

โˆ€๐’‚ . ( {๐‚|๐‘ท (๐‚ ,๐’‚) }โ†’๐ฎ๐ง๐ข๐ญ )โ†’ {๐‚|๐‘ธ (๐‚ ,๐’‚)}โ†’๐ฎ๐ง๐ข๐ญ

A quantifier instantiation for

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ๐ฌ๐ฐ ๐’‡ ๐’™= ๐’‡ ๐’™

Example: Typing under

2013/1/23 POPL 2013 12

{๐‚|๐‚=๐’Š }โ†’๐ฎ๐ง๐ข๐ญ {๐‚|๐‚=๐’Š }

โˆ€๐’‚ . ( {๐‚|๐‘ท (๐‚ ,๐’‚) }โ†’๐ฎ๐ง๐ข๐ญ )โ†’ {๐‚|๐‘ธ (๐‚ ,๐’‚)}โ†’๐ฎ๐ง๐ข๐ญ{๐‚โˆจ๐‚=๐’‚} {๐‚|๐‚=๐’‚ }

[๐’Š]Well-typed!

Theorem: Relative Completeness of

2013/1/23 POPL 2013 13

A program is safe

There exists a substitution for โ€œ?โ€s such that

Our Contributions

โ€ข Relatively complete extension ofordinary refinement type system

โ€ข Type inference method for

2013/1/23 POPL 2013 14

Type Inference for

โ€ข Find a substitution as well asa type environment such that

2013/1/23 POPL 2013 15

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ๐ฌ๐ฐ ๐’‡ ๐’™= ๐’‡ ๐’™

Our Approach

โ€ข Counterexample guided inference of and (cf. CEGAR in software model checking for imperative programs)โ€“ For inference of a type environment :โ€ข Use existing refinement type inference methods for

[Terauchi 2010, Kobayashi, Sato, Unno 2011]

โ€“ For inference of a substitution for โ€œ?โ€s: โ€ข Use a new method based on non-linear constraint solving

2013/1/23 POPL 2013 16

Our Approach

โ€ข Counterexample guided inference of and (cf. CEGAR in software model checking for imperative programs)โ€“ For inference of a type environment :โ€ข Use existing refinement type inference methods for

[Terauchi 2010, Kobayashi, Sato, Unno 2011]

โ€“ For inference of a substitution for โ€œ?โ€s: โ€ข Use a new method based on non-linear constraint solving

2013/1/23 POPL 2013 17

Counterexample GuidedRefinement Type Inference

2013/1/23 POPL 2013 18

Input Program

unsafe

Step 1: Fixed-PointType Inference [1,2]

Step 3: Refinement [1,2]

safe

Counter-example s.t.

CandidateType Envs.

yes

โˆƒ๐šช .๐šชโŠข๐…

โˆƒ๐šชโˆˆ๐šซ .๐šชโŠข๐‘ท

Step 2: SafetyCheck of [2]

no

unknown

ยฌโˆƒ๐šชโˆˆ๐šซ .๐šชโŠข๐‘ท

ยฌโˆƒ๐šช .๐šชโŠข๐…

New CandidateType Envs.

s.t.

[1] Terauchi POPL 2010 [2] Kobayashi, Sato, Unno PLDI 2011

Our Approach

โ€ข Counterexample guided inference of and (cf. CEGAR in software model checking for imperative programs)โ€“ For inference of a type environment :โ€ข Use existing refinement type inference methods for

[Terauchi 2010, Kobayashi, Sato, Unno 2011]

โ€“ For inference of a substitution for โ€œ?โ€s: โ€ข Use a new method based on non-linear constraint solving

2013/1/23 POPL 2013 19

Candidate Substitution

Counterexample GuidedSubstitution Inference

2013/1/23 POPL 2013 20

Input Program

unsafe

Step 1: Fixed-PointType Inference [1,2]

safe

Counter-example s.t.

CandidateType Envs.

yes

โˆƒ๐šช .๐šชโŠข๐ˆ ๐…

โˆƒ๐šชโˆˆ๐šซ .๐šชโŠข๐ˆ๐‘ท

Step 2: SafetyCheck of [2]

no

ยฌโˆƒ๐šชโˆˆ๐šซ .๐šชโŠข๐ˆ ๐‘ท

ยฌโˆƒ๐šช .๐šชโŠข๐ˆ๐…

New CandidateType Envs.

s.t.

[1] Terauchi POPL 2010 [2] Kobayashi, Sato, Unno PLDI 2011

Instantiated Program

New Candidate Substitution

s.t.

Step 3: Refinement [1,2]

Finding New Candidate Substitution

โ€ข Input:a safe non-recursive fragment such that

โ€ข Output: such that

2013/1/23 POPL 2013 21

By reduction to non-linear constraint solving using linear expression templates for

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ๐ฌ๐ฐ ๐’‡ ๐’™= ๐’‡ ๐’™๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ๐ฌ๐ฐ ๐’‡ ๐’™= ๐’‡ ๐’™

Example: Reduction to Non-Linear Constraint Solving

2013/1/23 POPL 2013 22

โˆƒ๐’„๐ŸŽ ,๐’„๐Ÿ ,๐‘ท ,๐‘ธ

. (โˆ€๐’‚ ,๐’™ , ๐’Š .๐’‚=๐’„๐ŸŽ+๐’„๐Ÿ โ‹…๐’Šโˆง๐’™=๐’Šโ‡’๐‘ธ (๐’‚ , ๐’™ )โˆ€๐’‚ ,๐’™ ,๐‚ .๐‘ธ (๐’‚ ,๐’™ )โˆง๐‚=๐’™โ‡’ ๐‘ท (๐’‚ ,๐‚)โˆ€๐’‚ ,๐‚ , ๐’Š .๐‘ท (๐’‚ ,๐‚ )โˆง๐’‚=๐’„๐ŸŽ+๐’„๐Ÿโ‹…๐’Šโ‡’๐‚=๐’Š)

โˆ€๐’‚ .( {๐‚|๐‘ท (๐’‚ ,๐‚ ) }โ†’๐ฎ๐ง๐ข๐ญ )โ†’ {๐’™โˆจ {๐‘ธ (๐’‚ , ๐’™ ) }โ†’๐ฎ๐ง๐ข๐ญ

Example: Non-linearConstraint Solving (1/2)

2013/1/23 POPL 2013 23

โˆƒ๐’„๐ŸŽ ,๐’„๐Ÿ .โˆ€ ๐’‚ , ๐’Š , ๐’‹ .๐’‚=๐’„๐ŸŽ+๐’„๐Ÿโ‹…๐’Šโˆง

๐’‚=๐’„๐ŸŽ+๐’„๐Ÿโ‹… ๐’‹โ‡’ ๐’Š= ๐’‹

โˆƒ๐’„๐ŸŽ ,๐’„๐Ÿ ,๐‘ท ,๐‘ธ

. (โˆ€๐’‚ ,๐’™ , ๐’Š .๐’‚=๐’„๐ŸŽ+๐’„๐Ÿ โ‹…๐’Šโˆง๐’™=๐’Šโ‡’๐‘ธ (๐’‚ , ๐’™ )โˆ€๐’‚ ,๐’™ ,๐‚ .๐‘ธ (๐’‚ ,๐’™ )โˆง๐‚=๐’™โ‡’ ๐‘ท (๐’‚ ,๐‚)โˆ€๐’‚ ,๐‚ , ๐’Š .๐‘ท (๐’‚ ,๐‚ )โˆง๐’‚=๐’„๐ŸŽ+๐’„๐Ÿโ‹…๐’Šโ‡’๐‚=๐’Š)

Elim.

Elim.

โˆƒ๐’„๐ŸŽ ,๐’„๐Ÿ ,๐‘ธ

. (โˆ€๐’‚ ,๐’™ , ๐’Š .๐’‚=๐’„๐ŸŽ+๐’„๐Ÿ โ‹…๐’Šโˆง๐’™=๐’Šโ‡’๐‘ธ (๐’‚ , ๐’™ )

โˆ€๐’‚ ,๐’™ ,๐‚ , ๐’Š . ๐‘ธ (๐’‚ , ๐’™ )โˆง๐‚=๐’™โˆง๐’‚=๐’„๐ŸŽ+๐’„๐Ÿโ‹…๐’Šโ‡’๐‚=๐’Š )

iff

Example: Non-linearConstraint Solving (2/2)

2013/1/23 POPL 2013 24

โˆƒ๐’„๐ŸŽ ,๐’„๐Ÿ .โˆ€ ๐’‚ , ๐’Š , ๐’‹ .๐’‚=๐’„๐ŸŽ+๐’„๐Ÿโ‹… ๐’Šโˆง๐’‚=๐’„๐ŸŽ+๐’„๐Ÿโ‹… ๐’‹โ‡’ ๐’Š= ๐’‹

โˆƒ๐’„๐ŸŽ ,๐’„๐Ÿ .โˆƒ๐€๐Ÿ ,๐€๐Ÿ .๐€๐Ÿ+๐€๐Ÿ=๐ŸŽโˆง

๐’„๐Ÿโ‹…๐€๐Ÿ=๐Ÿโˆง๐’„๐Ÿโ‹…๐€๐Ÿ=โˆ’๐Ÿ

๐’„๐ŸŽ=๐ŸŽ ,๐’„๐Ÿ=๐Ÿ

๐ฅ๐ž๐ญ ๐š๐ฉ๐ฉ๐ฅ๐ฒ๐ฌ๐ฐ ๐’‡ ๐’™= ๐’‡ ๐’™๐’Š

to

Bit-vector modeling & SMT [Gulwani, Srivastava, Venkatesan 2008]

Farkasโ€™ lemma: iff

Implementation

โ€ข Extended MoCHi [Sato, Unno and Kobayashi 2011, 2013]

with the type inference method for

2013/1/23 POPL 2013 25

let rec mc x = if x > 100 then x โ€“ 10 else mc (mc (x + 11))inlet n = randi() inif n ยท 101 then assert (mc n = 91) MoCHi

Program & Spec.Result

Certificateor

Counterexample

Conclusion

โ€ข Relatively complete refinement type system โ€“ Restricted use of quantificationโ€ข Add one universal quantifier over integer

just before each function parameter

โ€ข Type inference method for โ€“ Counterexample guided inference of and โ€ข inference by application of existing refinement type

inference methods [Terauchi 2010, Kobayashi, Sato, Unno 2011]

โ€ข Inference by reduction to non-linear constraint solving

2013/1/23 POPL 2013 26