Top Banner
Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567
33

Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Mar 26, 2015

Download

Documents

Riley Bruce
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Modular Verification of Higher-Order Methods in JML

Gary T. Leavens,Steve Shaner, and David A.

NaumannSupport from US NSF grant

CCF-0429567

Page 2: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Summary

Problem How to reason about

higher-order methods?Approach Greybox [Büchi-Weck97,99]

specifications, Copy rule / substitution [Morgan88], Structurally-restricted refinement

Contribution Structural matching for refinement Integration with JML

Page 3: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Background:Higher-Order Methods A higher-order method (HOM)

makes mandatory callsto weakly-specified methods

Page 4: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Example (part 1)Class with a `method parameter’

public class Counter { protected /*@ spec_public @*/ int count

= 0; protected /*@ spec_public @*/ Listener

lnr;

//@ assignable this.lnr; //@ ensures this.lnr == lnr; public Counter(Listener lnr) { // parameter this.lnr = lnr; }

Page 5: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Example (part 2): HOM

public void bump() { this.count = this.count + 1; this.lnr.actionPerformed(this.count);

}}

Page 6: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Mandatory Calls areWeakly Specifiedpublic interface Listener {

//@ assignable this.objectState; void actionPerformed(int x);}

Page 7: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Subtype of Listener

public class LastVal implements Listener { private /*@ spec_public @*/ int val = 0;

//@ in objectState;

//@ ensures \result == this.val; public /*@ pure @*/ int getVal() { return

this.val; }

//@ also //@ assignable objectState; //@ ensures this.val == x; public void actionPerformed(int x) { this.val = x; }}

Page 8: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Subtype of Listener

public class LastVal implements Listener { private /*@ spec_public @*/ int val = 0;

//@ in objectState;

//@ ensures \result == this.val; public /*@ pure @*/ int getVal() { return

this.val; }

//@ also //@ assignable objectState; //@ ensures this.val == x; public void actionPerformed(int x) { this.val = x; }}

Page 9: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Reasoning Problem:Want strong conclusionsLastVal lv = new LastVal();//@ assert lv.val == 0;Counter c = new Counter(lv);//@ assert c.lnr == lv && c.count == 0; c.bump();//@ assert lv.val == 1;

Page 10: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Why is strong conclusion valid?Copy rule and substitutionsLastVal lv = new LastVal();//@ assert lv.val == 0;Counter c = new Counter(lv);//@ assert c.lnr == lv && c.count == 0; c.count = c.count+1;c.lnr.actionPerformed(c.count);//@ assert lv.val == 1;

Page 11: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Problem Summary

Specification of higher-order methods(challenge 8 in [Leavens-Leino-Müller06]) Abstract

Specifies mandatory calls Suppress other details

Allows strong conclusions (challenge 9) Copy rule and substitution

Soundness Must make mandatory calls

Page 12: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Use of Higher-Order Methods

Key in design patterns [Gamma-etal95]: Observer Template Method Chain of Responsibility

Clients of design patterns: Interpreter Command State Strategy Visitor

Page 13: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

How to Specify HOMs?Standard Pre/Post …/*@ assignable this.count, lnr.objectState; @ ensures this.count ==\old(this.count+1); @*/public void bump() {

this.count = this.count + 1;this.lnr.actionPerformed(this.count);

}

Page 14: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

… Is Not Enough

LastVal lv = new LastVal();//@ assert lv.val == 0;Counter c = new Counter(lv);//@ assert c.lnr == lv && c.count == 0; c.bump();//@ assume c.count == 1;//@ hence_by (* ??? *);//@ assert lv.val == 1;

Page 15: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Higher-Order Specifications?E.g., [Ernst-Navlakhla-Ogden82]:

/*@ forall int x; @ requires \req(this.lnr.actionPerformed)(x); @ assignable this.count, @ \asgn(this.lnr.actionPerformed); @ ensures this.count ==\old(this.count+1) @ &&\ens(this.lnr.actionPerformed)

(this.count); @*/public void bump() { /* … */ }

Page 16: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Problems with Higher-Order Specifications Often longer and more complex than

code Harder to learn and teach Harder for tools to work with? Calls are not mandatory

Page 17: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Greybox (Ref. Calc.) Approach [Büchi–Weck97, 99]

vs.

Better:lnr.actionPerformed()

lnr.actionPerformed()

c.count++

Page 18: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

/*@ public model_program { @ normal_behavior @ assignable this.count; @ ensures this.count ==\

old(this.count+1); @ @ this.lnr.actionPerformed(this.count); @ } @*/public void bump() { /* … */ }

Greybox Approach in JML:Model Program Specification

Page 19: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Reasoning About HOM Calls

LastVal lv = new LastVal();//@ assert lv.val == 0;Counter c = new Counter(lv);//@ assert c.lnr == lv && c.count == 0; c.bump();//@ assert lv.val == 1;

Page 20: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Approach: Model Program Copy Rule and SubstitutionLastVal lv = new LastVal();//@ assert lv.val == 0;Counter c = new Counter(lv);//@ assert c.lnr == lv && c.count == 0; /*@ normal_behavior @ assignable c.count; @ ensures c.count == \

old(c.count+1); @*/c.lnr.actionPerformed(c.count);//@ assert lv.val == 1;

Page 21: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Rule for HOM Calls(Copy Rule + Substitution)

P { y.m(z); } Q

y: T, methType(T,m) = x:S -> void,

specFor(T,m) = C,

C’ = C [y,z/this,x], P { C’ } Q

Page 22: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Strong Conclusions fromCopy + Contextual Knowledge

//@ assert c.lnr == lv;c.bump();

Copy/Substitute model program

/*@ normal_behavior @ assignable c.count; @ ensures c.count == \old(c.count+1); @*/c.lnr.actionPerformed(c.count);

Context

lv.actionPerformed(c.count);

+

Page 23: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Soundness fromRestricting Implementations For soundness of HOM call rule:

(copy rule) body refines model program

(use of context) restrict refinement somandatory calls must happen in specified states

Page 24: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Notion of Refinement with Mandatory Calls in Given States Need to define

“structure-preserving refinement” Approach:

Restrict implementations Pattern matching

Model program vs. Implementation

Page 25: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Kinds of Patterns

Refinable (holes, wildcards) Specification statements

(normal_behavior) Mandatory

Calls Everything else

Page 26: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Pattern Matching

normal_behavior … following normal_behavior …{ C }

Model program

Method implementati

on

m();

m();

Page 27: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Implementing Higher-Order Methods/*@ public model_program { @ normal_behavior @ assignable this.count; @ ensures this.count == \

old(this.count+1); @ this.lnr.actionPerformed(this.count); @ } @*/public void bump() { /*@ following normal_behavior @ assignable this.count; @ ensures this.count == \

old(this.count+1);@*/ { this.count = this.count+1; } this.lnr.actionPerformed(this.count);}

Page 28: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Refinement

P ⊑ C iff C pattern matches against P The resulting following statements

are provable

Page 29: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Rule for following

P { following normal_behavior requires P’; ensures Q’;

C

} Q

P ==> P’, Q’ ==> Q,

P’ { C } Q’

Page 30: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Future Work

Soundness argument Connection to Büchi and Weck’s work

(refinement of traces) Case studies Implementation in JML tools Exceptions Concurrency

Page 31: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Related Work

Büchi and Weck (1997, 99): Idea of greybox approach Trace semantics Doesn’t focus on reasoning about calls Needs definition of structure

preservationMorgan (1988): Uses adaptation and substitution for

procedures

Page 32: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Related Work

Ernst, Navlakhla, and Ogden (1982): Higher-order logic specifications Harder to write and use Mandatory calls might not be madeSoundarajan and Fridella (2004): Higher-order trace-based specifications Can ensure mandatory calls are made Harder to write and use

Page 33: Modular Verification of Higher-Order Methods in JML Gary T. Leavens, Steve Shaner, and David A. Naumann Support from US NSF grant CCF-0429567.

Conclusions

Greybox (refinement-style) model programs Clear specification of HOMs Allow strong conclusions

Soundness: Restrictions on refinement Use of pattern matching