24
Motivation Our Work Summary Java+ITP: A Verification Tool Based on Hoare Logic and Algebraic Semantics Ralf Sasse, José Meseguer Department of Computer Science University of Illinois at Urbana-Champaign 6th International Workshop on Rewriting Logic and its Applications, 2006 Ralf Sasse, José Meseguer Java+ITP

Java+ITP: A Verification Tool Based on Hoare Logic and

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

Java+ITP: A Verification Tool Based on HoareLogic and Algebraic Semantics

Ralf Sasse, José Meseguer

Department of Computer ScienceUniversity of Illinois at Urbana-Champaign

6th International Workshop on Rewriting Logic and itsApplications, 2006

Ralf Sasse, José Meseguer Java+ITP

Page 2: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

Outline

1 MotivationLong-Term GoalsGeneral IdeaPrevious Work

2 Our WorkPreliminariesMain ResultsAn Example

Ralf Sasse, José Meseguer Java+ITP

Page 3: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

Long-Term GoalsGeneral IdeaPrevious Work

Long-Term Goals

Investigate modularity and extensibility ofprogramming languagesHoare logics

Source-code level reasoning.Generic and modular program logics wanted.Develop theorem proving technology on top of the logics.This case study is a first step in this direction.

Ralf Sasse, José Meseguer Java+ITP

Page 4: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

Long-Term GoalsGeneral IdeaPrevious Work

Problem Considered

Sequential Java subset:arithmetic expressions, assignments, sequentialcomposition, loops

Hoare logic for this programming language withside-effects, mathematically justified.Standard Hoare rules break down!Mechanization available, supporting:

Compositional reasoning to decompose Hoare triplesGeneration of first-order verification conditionsDischarging verification conditions with Maude’s inductivetheorem prover (ITP)

Ralf Sasse, José Meseguer Java+ITP

Page 5: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

Long-Term GoalsGeneral IdeaPrevious Work

ASIP+ITP

We have benefitted from previous experience at UCM onASIP+ITP, a tool that works on Hoare logics for a simpletoy language.However, in ASIP+ITP:

no side-effects in conditionsno variable declarations and shadowingsimple state infrastructure: direct mapping of variables tovaluesno compositional decomposition of Hoare triples supported

Ralf Sasse, José Meseguer Java+ITP

Page 6: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Java Semantics

Algebraic semantics (only equations used)Continuation passing style approachExtracted from a larger rewriting logic semantics for Java(JavaFAN)

Ralf Sasse, José Meseguer Java+ITP

Page 7: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Hoare Triples

Consist of:pre condition Apost condition Bprogram p

Written: {A} p {B}

Implicit state S used, made explicit when necessary.Meaning: A(S)⇒ B(S/(S|p))

Ralf Sasse, José Meseguer Java+ITP

Page 8: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Our Hoare Rules

Standard sequential composition rule:

{A} p {B} {B} q {C}{A} p q {C}

Standard conditional rule, breaks down here:

{A ∧ t} p {B} {A ∧ ¬t} q {B}{A} if t p else q {B}

Correct conditional rule, for side-effects in condition:

{A ∧ t} t | p {B} {A ∧ ¬t} t | q {B}{A} if t p else q {B}

Ralf Sasse, José Meseguer Java+ITP

Page 9: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Our Hoare Rules

Standard sequential composition rule:

{A} p {B} {B} q {C}{A} p q {C}

Standard conditional rule, breaks down here:

{A ∧ t} p {B} {A ∧ ¬t} q {B}{A} if t p else q {B}

Correct conditional rule, for side-effects in condition:

{A ∧ t} t | p {B} {A ∧ ¬t} t | q {B}{A} if t p else q {B}

Ralf Sasse, José Meseguer Java+ITP

Page 10: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Our Hoare Rules

Standard sequential composition rule:

{A} p {B} {B} q {C}{A} p q {C}

Standard conditional rule, breaks down here:

{A ∧ t} p {B} {A ∧ ¬t} q {B}{A} if t p else q {B}

Correct conditional rule, for side-effects in condition:

{A ∧ t} t | p {B} {A ∧ ¬t} t | q {B}{A} if t p else q {B}

Ralf Sasse, José Meseguer Java+ITP

Page 11: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Our Hoare Rules

Standard while loop rule, breaks down here:

{A ∧ t} p {A}{A} while t p {A ∧ ¬t}

Correct while loop rule, for side-effects in condition:

{A ∧ t} t | p {A} {A ∧ ¬t} t {A ∧ ¬t}{A} while t p {A ∧ ¬t}

Ralf Sasse, José Meseguer Java+ITP

Page 12: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Our Hoare Rules

Standard while loop rule, breaks down here:

{A ∧ t} p {A}{A} while t p {A ∧ ¬t}

Correct while loop rule, for side-effects in condition:

{A ∧ t} t | p {A} {A ∧ ¬t} t {A ∧ ¬t}{A} while t p {A ∧ ¬t}

Ralf Sasse, José Meseguer Java+ITP

Page 13: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Java+ITP tool: javax-inv

The javax-inv command applies multiple rules andcreates first-order proof obligations.Target Hoare triple is {P} init loop {Q}.

{P} init {I} I ⇒ I {I} loop {I ∧ ¬t} (I ∧ ¬t) ⇒ Q{I} loop {Q}

{P} init loop {Q}

loop = while t p:

{I ∧ t} t | p {I} {I ∧ ¬t} t {I ∧ ¬t}{I} while t p {I ∧ ¬t}

Ralf Sasse, José Meseguer Java+ITP

Page 14: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Java+ITP tool: javax-inv

The javax-inv command applies multiple rules andcreates first-order proof obligations.Target Hoare triple is {P} init loop {Q}.

{P} init {I} I ⇒ I {I} loop {I ∧ ¬t} (I ∧ ¬t) ⇒ Q{I} loop {Q}

{P} init loop {Q}

loop = while t p:

{I ∧ t} t | p {I} {I ∧ ¬t} t {I ∧ ¬t}{I} while t p {I ∧ ¬t}

Ralf Sasse, José Meseguer Java+ITP

Page 15: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Java+ITP tool: Compositionality

Hoare triples can be added as proof goals withouttranslating them to first-order goals right away.Java+ITP supports decomposition of such Hoare triples.Simpler Hoare triples can be translated to first-order goalson user command.

Ralf Sasse, José Meseguer Java+ITP

Page 16: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Example Program

Binomial coefficient program for(n

k

), with inputs ’N and ’K.

int ’N ; int ’Nfac ; int ’K ; int ’Kfac ;int ’N-Kfac ; int ’BC ; int ’I ;

’I = #i(0) ; ’Nfac = #i(1) ;while (’I < ’N){ ’I = ’I + #i(1) ; ’Nfac = ’Nfac * ’I ; }

’I = #i(0) ; ’Kfac = #i(1) ;while (’I < ’K){ ’I = ’I + #i(1) ; ’Kfac = ’Kfac * ’I ; }

’I = #i(0) ; ’N-Kfac = #i(1) ;while (’I < (’N - ’K)){ ’I = ’I + #i(1) ; ’N-Kfac = ’N-Kfac * ’I ; }

’BC = ’Nfac / (’Kfac * ’N-Kfac) ;

Ralf Sasse, José Meseguer Java+ITP

Page 17: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Verification Property

Property to be verified, with S the state and N and K the inputs:

{S[’N] = N ∧ S[’K] = K

∧ 0 <= N ∧ 0 <= K ∧ 0 <= N - K}

binomial-coefficient-program

{S[’Nfac] = N!

∧S[’Kfac] = K!

∧S[’N-Kfac] = (N - K)!

∧S[’BC] = bc(N, K)}

Ralf Sasse, José Meseguer Java+ITP

Page 18: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Example Roadmap

Load all necessary parts into Maude:our modified ITPour Java semanticsthe module defining the binomial coefficient code

Enter the property we want to show, as seen above.Decompose the proof obligation.Transform proof obligations to first-order proof obligations,done for one proof obligation here.Use ITP to discharge the first-order proof obligations.

Ralf Sasse, José Meseguer Java+ITP

Page 19: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Enter Proof Obligation

select ITP-TOOL .loop init-itp .

(add-hoare-triple CHOOSE-JAVAX :--- specification variables(N:Int ; K:Int)--- precondition((int-val(S:WrappedState[’N])) = (N:Int)& (int-val(S:WrappedState[’K])) = (K:Int)& (0 <= N:Int) = (true)& (0 <= K:Int) = (true)& (0 <= N:Int - K:Int) = (true))--- programchoose

Ralf Sasse, José Meseguer Java+ITP

Page 20: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Enter Proof Obligation (II)

--- postcondition((int-val(S:WrappedState[’Nfac]))= ((N:Int)!)

& (int-val(S:WrappedState[’Kfac]))= ((K:Int)!)

& (int-val(S:WrappedState[’N-Kfac]))= ((N:Int - K:Int)!)

& (int-val(S:WrappedState[’BC]))= (choose(N:Int, K:Int)))

--- initialization codechoose-init.)

Ralf Sasse, José Meseguer Java+ITP

Page 21: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Decompose Proof Obligation

(decomp:---- name of the HTchoose@0---- prefix codefacN---- midcondition((int-val(S:WrappedState[’N])) = (N:Int)& (int-val(S:WrappedState[’K])) = (K:Int)& (0 <= N:Int) = (true)& (0 <= K:Int) = (true)& (0 <= N:Int - K:Int) = (true)& (int-val(S:WrappedState[’Nfac]))

= ((N:Int)!))---- rest of codechoose/facN .)

Ralf Sasse, José Meseguer Java+ITP

Page 22: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

First Decomposed Proof Obligation

=================================hoare-label: [email protected]================================={ (int-val(S:WrappedState[’N])= N:Int)&(int-val(S:WrappedState[’K])= K:Int))&(0 <= N:Int - K:Int = true))&(0 <= N:Int = true)) &(0 <= K:Int = true)}facN{ (int-val(S:WrappedState[’Nfac])= N:Int !)&(int-val(S:WrappedState[’N])= N:Int)&(int-val(S:WrappedState[’K])= K:Int)&(0 <= N:Int - K:Int = true)&(0 <= N:Int = true) &(0 <= K:Int = true)}

Ralf Sasse, José Meseguer Java+ITP

Page 23: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

PreliminariesMain ResultsAn Example

Create First-Order Proof Obligation for [email protected]

(create-FO-goal-hoare-inv:---- name of the [email protected] invariant((int-val(S:WrappedState[’Nfac]))= ((int-val(S:WrappedState[’I]))!)& (0 <= int-val(S:WrappedState[’I]))= (true)& (int-val(S:WrappedState[’I])

<= int-val(S:WrappedState[’N]))= (true)& (int-val(S:WrappedState[’N]))= (N:Int)& (int-val(S:WrappedState[’K]))= (K:Int)& (0 <= N:Int) = (true)& (0 <= K:Int) = (true)& (0 <= N:Int - K:Int) = (true) ) .)

Ralf Sasse, José Meseguer Java+ITP

Page 24: Java+ITP: A Verification Tool Based on Hoare Logic and

MotivationOur WorkSummary

Summary

Proof of concept for Hoare logics for a non-trivial language.Compositionality on the source-code level.Useful in teaching about algebraic semantics and Hoarelogics.

OutlookExtend this to a larger Java fragment.Generalize it, independent of the actual language, each ruleonly dependent on a single language feature.

Ralf Sasse, José Meseguer Java+ITP