1 total correctness of recursive functions using jml4 fspv george karabotsos, patrice chalin, perry...

42
1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research Group Dept. of Computer Science and Software Engineering Concordia University, Montréal, Canada {g_karab, chalin, perry, leveda}@dsrg.org

Upload: lee-woods

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

1

Total Correctness of Recursive Functions Using JML4 FSPV

George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas

Dependable Software Research Group

Dept. of Computer Science and Software EngineeringConcordia University, Montréal, Canada

{g_karab, chalin, perry, leveda}@dsrg.org

Page 2: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

2

Research Context

• Java Modeling Language (JML)

• Program Verification– Aging 1st generation tools

• Integrated Verification Environment– JML4 …

Page 3: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

3

• Put screen shot here

Page 4: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

4

Eclipse-based IVE

• Next-GenerationResearch Platform

• Integrates existing tools– RAC (jml & jmlc)– ESC (ESC/Java2)

• ESC4 & FSPV-TG

Platform Runtime

Eclipse Platform · Workbench· Workspace· Team· Help

Eclipse 3.4

Simplify

JML4

JML JDT

JML 1st Generation

Tools

Proof General

CVC3

Isabelle

ESC4

FSPV TG

JML SV

...

Page 5: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

5

JML4 Extends Eclipse JDT

(Full) Parsing

Type Checking

diet AST

ASTReferenced

types (*.java)Diet

Parsing

Diet Parsing

External Spec Merge

Spec files (*.jml, *.java)

Code Generation (incl. RAC code)

AST (+ proof status)

Diet Parsing

Flow Analysis

JML+Java (*.java)

*.class

Static Verification (incl. FSPV-TG)

Java Development Tooling (JDT)

• JDT/JML4 Core Phases

Page 6: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

6

Isabelle

• Theorem prover framework– Isabelle/Pure (meta-logic)– classical reasoner– simplifier

• Provides machinery for defining new logics– existing logics can be extended– syntax can be added as sugar

Page 7: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

7

Isabelle/Simpl

• Extends Isabelle/HOL• Hoare Rules for

– Partial & Total Correctness

• VCs generated within Simpl– through vcg and vcg_step tactics

• Proven Sound and Complete• Expressive

– Global and local variables, exceptions, abrupt termination, procedures, breaks out of loops, procedures, references and heap

Page 8: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

8

JML4 Static-Verification DataflowDataflow in FSPV-TG

Proof General

from front-end, after Flow Analysis phase

ATPs

decorated with proof

status

AST

AST

FSPV-TG

PrestateDecoration

TheoryTranslation

SideEffectHandling

SimplTranslationTheory

files

ESC4

External to Eclipse• Translate JML

to Hoare triples• Store info for \

old• Remove side

effects from expressions

• Generate Simpl

Page 9: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

9

McCarthy’s 91 Function

public class McCarthy {//@ requires n >= 0;//@ ensures \result == (100 < n ? n-10 : 91);//@ measured_by 101 - n;public static int f91(int n) {if(100 < n)return n - 10;elsereturn f91(f91(n + 11));

}}

Page 10: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

10

McCarthy’s 91 Simpl Theory

Page 11: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

11

McCarthy’s 91 Simpl Theory

MethodTranslation

TotalCorrectnessProof

Page 12: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

12

Components of an Isabelle/Simpl Theory

• Stateshoarestate vars =

x::τ1…

• Proceduresprocedures N (x::τ1, y::τ2, …| z::τ3) where v::τ4 … in B

• Hoare TuplesΓ, Θ ├ {|P|} B {|Q|}, {|R|}

Γ, Θ ├t {|P|} B {|Q|}, {|R|}

Page 13: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

13

Hoarestate

• Define global and local variables• Statespaces

– Modeled as a function from abstract names to abstract values

– Organizes • distinctness of names and • projection/injection of concrete values into the abstract one.

• Locales– Support modular reasoning– Allows multiple inheritance of other locales– Allows for renaming components

Page 14: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

14

McCarthy 91 Functionpublic static int f91(int n) {

if(100 < n)return n - 10;

elsereturn f91(f91(n + 11));

}

Page 15: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

15

McCarthy 91 Functionpublic static int f91(int n) {

if(100 < n)return n - 10;

elsereturn f91(f91(n + 11));

}

Name

Input

Output

Page 16: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

16

McCarthy 91 Functionpublic static int f91(int n) {

if(100 < n)return n - 10;

elsereturn f91(f91(n + 11));

}

Name

Input

Output

SimplVariable

InnerRec.

BinderVariable

Outer Rec.

Page 17: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

17

Generate for Simpl• a hoarestate(McCarthy_f91_int_impl)

– statespace, locale

• Functions– Copying the actual to formal parameters– Updating global variables– Copying the formal result parameter

Page 18: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

18

McCarthy 91- Proving Correctness//@ requires n >= 0;//@ ensures \result == (100<n ? n-10 : 91);//@ measured_by 101 - n;public static int f91(int n)

Page 19: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

19

McCarthy 91- Proving Correctness//@ requires n >= 0;//@ ensures \result == (100<n ? n-10 : 91);//@ measured_by 101 - n;public static int f91(int n)

locale Name

Page 20: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

20

McCarthy 91- Proving Correctness//@ requires n >= 0;//@ ensures \result == (100<n ? n-10 : 91);//@ measured_by 101 - n;public static int f91(int n)

locale Name

PreStatement

Post

Page 21: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

21

McCarthy 91- Proving Correctness//@ requires n >= 0;//@ ensures \result == (100<n ? n-10 : 91);//@ measured_by 101 - n;public static int f91(int n)

locale Nameprestate

PreStatement

Post

Page 22: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

22

McCarthy 91- Proving Correctness//@ requires n >= 0;//@ ensures \result == (100<n ? n-10 : 91);//@ measured_by 101 - n;public static int f91(int n)

locale Nameprestate

PreStatement

Post

measureVC generator

auto

Page 23: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

23

Fibonacci

//@ public static native int fib_spec(int n);

//@ requires n>=0;//@ ensures \result == fib_spec(n);//@ measured_by n;public static /*@ pure */ int fib(int n) { if(n == 0) return 0; else if (n == 1) return 1; else return fib(n-1) + fib(n-2);}

Page 24: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

24

Fibonacci

Page 25: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

25

Fibonacciclass Fibonacci { //@ requires n>=0; //@ ensures \result == (n==0)? 0 : (n==1) ? 1 //@ : fib_spec(n-1)+fib_spec(n-2); //@ measured_by n; //@ public static pure model //@ int fib_spec(int n);

//@ requires n>=0; //@ ensures \result == fib_spec(n); //@ measured_by n; public static /*@ pure */ int fib(int n) { ...

Page 26: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

26

Ackermann

//@ public static native int ack_spec(int n);//@ requires n >= 0 && m >= 0 ;//@ ensures \result == ack_spec(n,m);public static int ack(int n, int m) { if(n == 0)

return m + 1;else if(m == 0) return ack(n-1, m);else return ack(n-1, ack(n, m-1));

}

Page 27: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

27

Ackermann

Page 28: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

28

Milestones

• FSPV TG– Supporting functional subset of JML+Java

• Study the adequacy of Isabelle/Simpl– Non-recursive Programs

• Cube, Factorial, Summation

– Total Correctness of Recursive Programs• Factorial, Fibonacci, McCarthy’s 91, Ackermann

– Classes• Point

Page 29: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

29

Related Work

• LOOP– VerifiCard project (Industry)

• JACK– Banking Application (Academia)

• Krakatoa/Why

Page 30: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

30

Comparison TableLOOP JACK Krakatoa/Why

FSPV-TGSimpl

Maintained Open Source Proven Sound

Proven Complete Above two proofs done in PVS N/A by hand in Isabelle

VC generation donein prover

Termination ofRecursive Functions

Page 31: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

31

Future Work

• Update the translator to reflect current state.• Case Study• Object Oriented Support

– Inheritance– Behavioral Subtyping

• Additional Language Elements– Exceptions– Loops with break and continue statements

• JML– Revise measured_by clause (see Ackermann)

Page 32: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

32

Thank you!

George Karabotsos, Patrice Chalin, Perry R. James, Leveda GiannasDependable Software Research Group

Dept. of Computer Science and Software EngineeringConcordia University, Montréal, Canada

{g_karab, chalin, perry, leveda}@dsrg.org

Page 33: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

33

Fields + Memory

Page 34: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

34

Total Correctness Proof for the f91 Function

//@ requires n >= 0;//@ ensures \result == (100<n ? n-10 : 91);//@ measured_by 101 - n;public static int f91(int n)

Page 35: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

35

Constructor

• Constructors defined as a regular procedure

• Allocate memory using the NEW operator

•Providing a size (sz) and

•A list of with the fields and their initialization

• Assignments of input values to fields

Page 36: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

36

Method Calls

Page 37: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

37

Point Class

Public class Point public int XCoord;public int YCoord;

public Point(int x, int y) {XCoord = x;YCoord = y;}//@ensures XCoord==\old(XCoord)+dx; //@ensures YCoord==\old(YCoord)+dy;public void move(int dx, int dy) {XCoord += dx;YCoord += dy;}//@ requires P == null;//@ ensures \result != null;//@ ensures \result.XCoord == 11;//@ ensures \result.YCoord == 11;public static Point tester(Point P){P = new Point(10,11);P.move(1,0);return P;}

}

Page 38: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

38

Point ClassSimpl Theory

Page 39: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

39

Point ClassSimpl Theory

fields

Constructor

move

tester

Page 40: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

40

Memory in Simpl

• References and Heap

• Two components:– A list of allocated references– A natural number indicating the amount of

available memory

• Expressed as a hoarestatehoarestate globals_memory =

alloc = “ref list”

free = nat

Page 41: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

41

Fields

• Defined as maps from ref => τhoarestate globals_Point=globals_memory +

XCoord :: “ref => int”

YCoord :: “ref => int”

• Accessing a field:`P->`XCoord

Page 42: 1 Total Correctness of Recursive Functions Using JML4 FSPV George Karabotsos, Patrice Chalin, Perry R. James, Leveda Giannas Dependable Software Research

42

Case Study—Benchmarks1

• Adding and Multiplying numbers• Binary Search in an Array• Sorting a Queue• Layered Implementation of a Map ADT• Linked-List Implementation of a Queue ADT• Iterators• Input/Output Streams• An Integrated Application[1] B. Weide et al., “Incremental Benchmarks for Software Verification Tools and Techniques,”

Verified Software: Theories, Tools, Experiments, 2008, pp. 84-98