Program Verification / Automated Theorem Proving

20
Program Verificat ion Using Spec#

description

Prove correctness of the program Cost effective way to develop and maintain high-quality software

Transcript of Program Verification / Automated Theorem Proving

Page 1: Program Verification / Automated Theorem Proving

Program Verification Using Spec#

Page 2: Program Verification / Automated Theorem Proving

Motivation

• Prove correctness of the program

• Cost effective way to develop and maintain high-quality software.

Page 3: Program Verification / Automated Theorem Proving

Road Map• Design by Contract• Spec# Architecture• Demo

Page 4: Program Verification / Automated Theorem Proving

Design by Contract• First appeared in Eiffel• formal, precise and

verifiable interface

Page 5: Program Verification / Automated Theorem Proving

Pre Conditions

class ArrayList {public virtual void Insert( int index , object value)

requires 0 <= index && index <= Count; //Pre condition{ }

Page 6: Program Verification / Automated Theorem Proving

Post Conditions

class ArrayList {public virtual void Insert( int index , object value)requires 0 <= index && index <= Count; ensures Count == old(Count) + 1; //Post conditionsensures value == this[index];{ }

Page 7: Program Verification / Automated Theorem Proving

Not Enough

• Method Constructs not enough

• Enforce constraints on private members?

• Abstraction Violation?• How to ensure object’s

state?

Page 8: Program Verification / Automated Theorem Proving

Object Invariants

class SortOrder {ItemsList[ ]! randomList;ItemsList[ ]! sortedList;invariant randomList.Length == sortedList .Length;

Page 9: Program Verification / Automated Theorem Proving

Blame Game• Require failure =>

Blame the method caller (Client)

Ensure failure => Blame the method implementor (Provider)

Page 10: Program Verification / Automated Theorem Proving

Spec# Architecture

Spec# Compiler

Verification Code Generator (Boogie)

Automatic Theorem Prover (Boogie)

Page 11: Program Verification / Automated Theorem Proving

Why extend C#???

• Non Null Types• Method Contracts• Checked / Unchecked

Exceptions

Page 12: Program Verification / Automated Theorem Proving

Non Nullable Typespublic class Program{

public static void Main(string![]! args) {

for (int i=0; i< args.Length; i++){ Console.WriteLine(arg[i]);}Console.ReadLine();

}}

Page 13: Program Verification / Automated Theorem Proving

Exceptions

Failures

Provider

AdmissibleObserved Program

Errors

Client

Page 14: Program Verification / Automated Theorem Proving

Assertions???

• Why just simple assertions can’t help?• Callbacks, Multi Threads, Inheritance

Page 15: Program Verification / Automated Theorem Proving

Code Comparison

C#public class SomeClass { public SomeClass() { } public int SomeMethod(int i) { return 50/i; } }

Spec#public class SomeClass { public SomeClass() { } public int SomeMethod(int i) requires i != 0; { return 50/i; } }

Page 16: Program Verification / Automated Theorem Proving

IL (C#).method public hidebysig instance int32 SomeMethod(int32 i) cil managed{ // Code size 5 (0x5) .maxstack 8 IL_0000: ldc.i4.s 50 IL_0002: ldarg.1 IL_0003: div IL_0004: ret} // end of method SomeClass::SomeMethod

Page 17: Program Verification / Automated Theorem Proving

IL (Spec#).method public hidebysig instance int32 SomeMethod(int32 i) cil managed{ .custom instance void

[System.Compiler.Runtime]Microsoft.Contracts.EnsuresAttribute::.ctor(string) = smthng .locals init (int32 V_0, class

[System.Compiler.Runtime]Microsoft.Contracts.ContractMarkerException V_1, int32 V_2)// Some Usual Operations .try { …. IL_0016: ldstr "Postcondition 'i != 0' violated from method

classLibrary1.SomeClass.SomeMethod(System.Int32)'" IL_001b: newobj instance void

[System.Compiler.Runtime]Microsoft.Contracts.EnsuresException::.ctor(string) IL_0020: throw ….} // end .try….IL_002e: ret} // end of method SomeClass::SomeMethod

Page 18: Program Verification / Automated Theorem Proving

Runtime Checks

• Preconditions and postconditions are turned into inlined code

• Performance • Extra methods and

fields in the compiled code

Page 19: Program Verification / Automated Theorem Proving

Automated Theorem Prover

• BoogiePL• Simplify Theorem Prover • Propositional Calculus

Page 20: Program Verification / Automated Theorem Proving

Demo