boogie 2011 hi-lite

39
Why Hi-Lite Ada? erˆ ome Guitton, Johannes Kanig, Yannick Moy (AdaCore) Boogie Workshop - August 1st, 2011

Upload: adacore

Post on 22-Nov-2014

943 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Boogie 2011 Hi-Lite

Why Hi-Lite Ada?

Jerome Guitton, Johannes Kanig,Yannick Moy (AdaCore)

Boogie Workshop - August 1st, 2011

Page 2: Boogie 2011 Hi-Lite

Overview

Introduction

Ada 2012 and Alfa

Ada and Why

The translation

Page 3: Boogie 2011 Hi-Lite

Outline

Introduction

Ada 2012 and Alfa

Ada and Why

The translation

Page 4: Boogie 2011 Hi-Lite

Unit Proof - I

Motivation

I Allow gradual replacement of tests by program verificationI Provide other means of verification when formal verification

fails:I VCs too complex for automated toolsI manual proofs too costlyI program constructs out of scopeI aspects to be proved out of scope (timing, memory,

termination)

Concept

I Apply formal verification and tests on a per-function basis

I Tests still available to complement program verification

I Has been applied at Airbus to avionics software Level A

Page 5: Boogie 2011 Hi-Lite

Unit Proof - II

Problems

I Expertise: required for writing contracts and carrying proof

I Duplication: contract not shared between testing and proof

I Isolation: unit test and unit proof cannot be combined

I Confusion: not the same semantics for testing and proof

I Debugging: contracts and proof cannot be executed

Page 6: Boogie 2011 Hi-Lite

Hi-Lite : Test and Proof

French research project started in May 2010, over 3 years

Motivation

I Source language: Ada

I Combine test and proof in a single contract based technology

I Same language and semantics for test and proof

I Application to existing projects should be possible

I Ease of use: Automation

The upcoming avionics standard DO-178C

Page 7: Boogie 2011 Hi-Lite

Outline

Introduction

Ada 2012 and Alfa

Ada and Why

The translation

Page 8: Boogie 2011 Hi-Lite

New Forms of Expressions in Ada 2012

I if-expressions:

(if X = 0 then 0 else 1 / X)

I case-expressions:

type Week_Day is

(Mon , Tue , Wed , Thu , Fri , Sat , Sun);

...

(case X is

when Mon .. Fri => True

when others => False)

I quantified expressions:

(for all I in X’Range => X (I) > 0)

(for some I in X’Range => X (I) > 0)

Page 9: Boogie 2011 Hi-Lite

Contracts

A function with pre- and postcondition

function Search (S : String; C : Character)

return Natural

with

Pre => (S /= ""),

Post =>

((if Search ’Result /= 0 then

S (Search ’Result) = C)

and

(for all X in

S’First .. Search ’Result - 1 =>

S (X) /= C));

Page 10: Boogie 2011 Hi-Lite

The Alfa Subset of Ada

Definition

I Includes all features suitable for program verification

I Excludes pointers, concurrency, exceptions

I No side effects in annotations

I No ambiguous expressions

Classification of each function

I Non-Alfa: only very light restrictions

I Partially in Alfa: signature and contract of the function are inAlfa, no restriction on the body

I (Entirely) in Alfa: signature, contract and body of thefunction are in Alfa, only functions at least partially in Alfaare called

Page 11: Boogie 2011 Hi-Lite

Alfa and Ada

Alfa and Non-Alfa code can be freely mixed

I Automatic detection of functions that are (partially or fully) inAlfa

I Only those functions are translated to Why

I Remaining code can be covered by testing

Rationale

I You don’t need to write all your code in Alfa

I Application to legacy code base

I Allow packages with complex code (pointers, concurrency)

Page 12: Boogie 2011 Hi-Lite

Outline

Introduction

Ada 2012 and Alfa

Ada and Why

The translation

Page 13: Boogie 2011 Hi-Lite

The GNATprove tool

Page 14: Boogie 2011 Hi-Lite

Automatic Effects Computation

Procedure

I First phase of GNATprove

I Compute localized effects of each function (do not take intoaccount effects of called functions)

A few restrictions to Ada to maintain correctness

I No function pointers

I No implicit aliasing

I These situations are recognized by the compiler (work inprogress)

Page 15: Boogie 2011 Hi-Lite

An Ada ProgramSpec for package A

typesglobal vars

function declscontracts

Body for package A

local typeslocal varsfunctions

Page 16: Boogie 2011 Hi-Lite

An Ada ProgramSpec for package A

typesglobal vars

function declscontracts

Body for package A

local typeslocal varsfunctions

Spec for package B

Body for package B

Page 17: Boogie 2011 Hi-Lite

An Ada ProgramSpec for package A

typesglobal vars

function declscontracts

Body for package A

local typeslocal varsfunctions

Spec for package B

Body for package B

Page 18: Boogie 2011 Hi-Lite

An Ada ProgramSpec for package A

typesglobal vars

function declscontracts

Body for package A

local typeslocal varsfunctions

Spec for package B

Body for package B

Page 19: Boogie 2011 Hi-Lite

A First Idea for Why Files

code forspec of A

code forbody of A

Page 20: Boogie 2011 Hi-Lite

A First Idea for Why Files

code forspec of A

code forbody of A

code forspec of B

code forbody of B

Page 21: Boogie 2011 Hi-Lite

A First Idea for Why Files

code forspec of A

code forbody of A

code forspec of B

code forbody of B

Page 22: Boogie 2011 Hi-Lite

A First Idea for Why Files

code forspec of A

code forbody of A

code forspec of B

code forbody of B

Page 23: Boogie 2011 Hi-Lite

A First Idea for Why Files

code forspec of A

code forbody of A

code forspec of B

code forbody of B

Achieved

I Ada visibility modeled

I circular dependenciesavoided

I (mutual) recursion evenacross packages

Page 24: Boogie 2011 Hi-Lite

A First Idea for Why Files

code forspec of A

code forbody of A

code forspec of B

code forbody of B

Achieved

I Ada visibility modeled

I circular dependenciesavoided

I (mutual) recursion evenacross packages

Problem

I Effects on local variables introduce new dependencies

I Example: function in B modifies indirectly local variable of A

Page 25: Boogie 2011 Hi-Lite

A First Idea for Why Files

code forspec of A

code forbody of A

code forspec of B

code forbody of B

Achieved

I Ada visibility modeled

I circular dependenciesavoided

I (mutual) recursion evenacross packages

Problem

I Effects on local variables introduce new dependencies

I Example: function in B modifies indirectly local variable of A

I circular dependency!

Page 26: Boogie 2011 Hi-Lite

Our Proposed Solution

code for types andvars in spec of A

code for types andvars in body of A

code for funcsin spec of A

code for funcsin body of A

Page 27: Boogie 2011 Hi-Lite

Our Proposed Solution

code for types andvars in spec of A

code for types andvars in body of A

code for funcsin spec of A

code for funcsin body of A

Page 28: Boogie 2011 Hi-Lite

Our Proposed Solution

code for types andvars in spec of A

code for types andvars in body of A

code for funcsin spec of A

code for funcsin body of A

code for types andvars in spec of B

code for types andvars in body of B

code for funcsin spec of B

code for funcsin body of B

Page 29: Boogie 2011 Hi-Lite

Our Proposed Solution

code for types andvars in spec of A

code for types andvars in body of A

code for funcsin spec of A

code for funcsin body of A

code for types andvars in spec of B

code for types andvars in body of B

code for funcsin spec of B

code for funcsin body of B

Page 30: Boogie 2011 Hi-Lite

Our Proposed Solution

code for types andvars in spec of A

code for types andvars in body of A

code for funcsin spec of A

code for funcsin body of A

code for types andvars in spec of B

code for types andvars in body of B

code for funcsin spec of B

code for funcsin body of B

Page 31: Boogie 2011 Hi-Lite

Outline

Introduction

Ada 2012 and Alfa

Ada and Why

The translation

Page 32: Boogie 2011 Hi-Lite

Ada Integer Types

New type definition:

type One_Ten is range 1 .. 10;

Subtype (inherits base range):

subtype One_Ten_Integer is

Integer range 1 .. 10;

Inserted checks

I Range check on assignment (or parameter passing)

I Overflow check on intermediate operations on the base type

Page 33: Boogie 2011 Hi-Lite

Ada Integer Types in Why

type t

predicate t_in_range (x : int) = -128 <= x <= 127

logic t_to_int : t -> int

logic t_of_int : int -> t

parameter t_of_int_ :

n : int ->

{ t_in_range (n) } t { t_to_int (result) = n }

parameter t_in_range_ :

n : int ->

{ t_in_range (n) } int { result = n }

axiom t_range :

forall x : t. t_in_range (t_to_int (x))

...

Page 34: Boogie 2011 Hi-Lite

Execution Semantics for Assertions - I

Objective

I Same semantics for test and proof

I Assertions should be runtime error free

Possibilities

I Consider assertions with runtime errors as falseI Implicit assumptions in the case of preconditions

I Consider assertions with runtime errors as incorrectI Additional VCs to prove absence of runtime errors in specsI No implicit assumptions

I Hi-Lite: second possibility has been chosen for clarity

Page 35: Boogie 2011 Hi-Lite

Execution Semantics for Assertions - II

Example in Ada

function Add (X, Y : One_Ten) return One_Ten

with Pre => (X + Y < 10);

Translation to Why

let add (x : one_ten) (y : one_ten) =

{ true }

ignore

(one_ten_range_

(one_ten_to_int (x) + one_ten_to_int (y)) < 10);

assume

{ one_ten_to_int (x) + one_ten_to_int (y) < 10 };

... (* translated body of function Add *)

Page 36: Boogie 2011 Hi-Lite

Loop Assertions - I

The trivial translation:

Ada loop

while C loop

pragma Assert (P);

...

end loop;

Why translation

while C do

{ P }

...

done

Does not reflect runtime behavior:

I Assertion not executed when C is always false

I Assertion not executed after end of loop

Page 37: Boogie 2011 Hi-Lite

Loop Assertions - II

Our translation

if c then

try

while true do (* infinite loop *)

{ invariant c and p}

...

if not c then raise Exit

done

with Exit -> ();

Page 38: Boogie 2011 Hi-Lite

Quantified Expressions

A pathological example

(for all J in 1 .. 10 =>

(if J = 5 then J /= 1 / 0 else False))

A simple approach

∀ 1 ≤ j ≤ 10. if j = 5 then 0 6= 0 else true

Proposed by an anonymous referee

∀ 1 ≤ j ≤ 10.(∀ 1 ≤ j ′ ≤ j − 1. if j ′ = 5 then 0 6= 0 else true) and(∀ 1 ≤ j ′ ≤ j − 1. if j ′ = 5 then j ′ 6= 1/0 else false)⇒ if j = 5 then 0 6= 0 else true

Page 39: Boogie 2011 Hi-Lite

Conclusion

Hi-Lite

I Allow combining test and proof

I Easy adoption of formal verification, applied to new andexisting projects

Future Work

I Migration to Why3

I Verification of consistency of contracts