frama-c training session introduction to acsl and its guiframa-c.com/training/presentation.pdf ·...

40
Frama-C Training Session Introduction to ACSL and its GUI Virgile Prevosto CEA List October 21 st , 2010

Upload: others

Post on 04-Jun-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Frama-C Training SessionIntroduction to ACSL and its GUI

Virgile Prevosto

CEA List

October 21st, 2010

Page 2: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

outline

Presentation

ACSL SpecificationsFunction contractsFirst-order logicLoopsAssertions

Deductive VerificationHoare logicPointers and MemoryJessie Plugin

Page 3: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

ACSL SpecificationsFunction contractsFirst-order logicLoopsAssertions

Deductive VerificationHoare logicPointers and MemoryJessie Plugin

Page 4: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

Motivations

Main objectiveStatically determine some semantic properties of a program

I safety: pointer are all valid, no arithmetic overflow, ...I terminationI functional propertiesI dead codeI ...

Embedded codeI Much simpler than desktop applicationsI Some parts are critical, i.e. a bug have severe consequences

(financial loss, or even dead people)I Thus a good target for static analysis

Page 5: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

Some tools

Polyspace Verifier Checks for (absence of) run-time errorC/C++/Ada)http://www.mathworks.com/products/polyspace/

ASTRÉE Absence of error without false alarm inSCADE-generated codehttp://www.di.ens.fr/~cousot/projets/ASTREE/

Coverity Checks for various code defects (C/C++/Java)http://www.coverity.com

Page 6: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

Some tools (cont’d)

a3 Worst-case execution time and Stack depthhttp://www.absint.com/

FLUCTUAT Accuracy of floating-point computations and originof rounding errorshttp://www-list.cea.fr/labos/fr/LSL/fluctuat/

Frama-C A toolbox for analysis of C programshttp://frama-c.com/

Page 7: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

A brief history

I 90’s: CAVEAT, an Hoare logic-based tool for C programsI 2000’s: CAVEAT used by Airbus during certification process

of the A380I 2002: Why and its C front-end CaduceusI 2006: Joint project to write a successor to CAVEAT and

CaduceusI 2008: First public release of Frama-C (Hydrogen)I today:

I Frama-C BoronI Multiple projects around the platformI A growing community of users

Page 8: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

Architecture

I A modular architectureI Kernel:

I CIL (U. Berkeley) library for the C front-endI ACSL front-endI Global management of analyzer’s state

I Various plug-ins for the analysisI Value analysis (abstract interpretation)I Jessie (translation to Why)I SlicingI Impact analysisI ...

Page 9: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

ACSL: ANSI/ISO C Specification Language

PresentationI Based on the notion of contract, à la EiffelI Allow the users to specify functional properties of their

programsI Allow communication between the various pluginI Independent from a particular analysis

Basic ComponentsI First-order logicI Pure C expressionsI C types + Z (integer) and R (real)I Built-ins predicates and logic functions, particularly over

pointers: \valid(p), \valid(p+0..2),\separated(p+0..2,q+0..5), \block_length(p),...

Page 10: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

ACSL SpecificationsFunction contractsFirst-order logicLoopsAssertions

Deductive VerificationHoare logicPointers and MemoryJessie Plugin

Page 11: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Key Ingredients

Specification of a functionI Contract between caller and calleeI Callee requires some pre-conditions from the callerI Callee ensures some post-conditions hold when it returns

A first exampleunsigned int M;/*@

requires \valid (p) && \valid (q);ensures M == (*p + *q) / 2;

*/void mean( unsigned int* p, unsigned int* q) {

if (*p >= *q) { M = (*p - *q) / 2 + *q; }else { M = (*q - *p) / 2 + *p; }

}

Page 12: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Specification of Side Effects

The specification/*@

requires \valid (p) && \valid (q);ensures M == (*p + *q) / 2;

*/void mean( unsigned int* p, unsigned int* q);

A valid implementation

Page 13: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Specification of Side Effects

The specification/*@

requires \valid (p) && \valid (q);ensures M == (*p + *q) / 2;

*/void mean( unsigned int* p, unsigned int* q);

A valid implementation

void mean(int *p, int* q){

*p = *q = M = 0;}

Page 14: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Specification of Side Effects

The specification/*@

requires \valid (p) && \valid (q);ensures M == (*p + *q) / 2;ensures *p == \old (*p) && *q == \old (*q);

*/void mean( unsigned int* p, unsigned int* q);

A valid implementation

Page 15: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Specification of Side Effects

The specification/*@

requires \valid (p) && \valid (q);ensures M == (*p + *q) / 2;ensures *p == \old (*p) && *q == \old (*q);

*/void mean( unsigned int* p, unsigned int* q);

A valid implementation

int A = 42;void mean(int *p, int* q) {

if (*p >= *q) ... else ...A = 0; }

Page 16: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Specification of Side Effects

The specification/*@

requires \valid (p) && \valid (q);ensures M == (*p + *q) / 2;assigns M;

*/void mean( unsigned int* p, unsigned int* q);

A valid implementation

Page 17: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Specification of Side Effects

The specification/*@

requires \valid (p) && \valid (q);ensures M == (*p + *q) / 2;assigns M;

*/void mean( unsigned int* p, unsigned int* q);

A valid implementation

void mean(int *p, int* q) {if (*p >= *q) { M = (*p - *q) / 2 + *q; }else { M = (*q - *p) / 2 + *p; }

}

Page 18: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

A more advanced example

Informal specI Input: a sorted array and its length, an element to search.I Output: index of the element or -1 if not found

Towards a formal specificationint find_array (int* arr , int length , int query );

I How to specify the two distinct outcome?I What does that mean for arr to be sorted?I How to prove the implementation?Deductive Verification

Page 19: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Function contracts

Behaviors

/*@ behavior found:assumes \exists integer i;

0<=i< length && arr[i] == query;ensures 0<= \result < length &&

arr[ \result ] == query;behavior not_found :

assumes \forall integer i;0<=i< length ==> arr[i] != query;

ensures \result == -1;complete behaviors ; disjoint behaviors ;

*/int find_array (int* arr , int length , int query );

Page 20: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - First-order logic

Predicate definition

/*@predicate sorted {L}( int* arr , int length ) =

\forall integer i,j;0<=i<=j< length ==> arr[i] <= arr[j];

*/

/*@ requires sorted {Here }(arr , length );requires \valid (arr +(0.. length -1));requires length >= 0;

*/int find_array (int* arr , int length , int query );

Page 21: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - First-order logic

Axiomatic definition

/*@inductive sorted {L}( int* arr , int length ) {

case singleton {L}:\forall int* arr; sorted {L}(arr ,0);

case trans{L}:\forall int* arr , integer length ;sorted {L}(arr , length )&& arr[length -1] <= arr[ length ]==> sorted {L}(arr , length + 1);

} */

/*@ requires sorted {Here }(arr , length );requires \valid (arr +(0.. length -1));requires length >= 0;

*/int find_array (int* arr , int length , int query );

Page 22: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Loops

Implementation of find_array

int find_array (int* arr , int length , int query){

int min = 0;int max = length - 1;int mean;while (min <= max) {

mean = min + (max - min) / 2;if (arr[mean] == query) return mean;if (arr[mean] < query)

min = mean + 1;else

max = mean - 1;}return -1;

}

Page 23: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

ACSL Specifications - Loops

Loop annotations

/*@ loop invariant 0<= min < length ;loop invariant 0<= max < length ;loop invariant

\forall integer i;0<=i<min ==> arr[i] < query;

loop invariant\forall integer i;

max <i< length ==> arr[i] > query;loop assigns mean , min , max;loop variant max - min;

*/while (min <= max) { ... }

Page 24: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

while (min <= max) {mean = min + (max - min) / 2;/*@ assert min <= mean <= max; */if (arr[mean] == query) return mean;if (arr[mean] < query)

min = mean + 1;else

max = mean - 1;

Page 25: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Presentation

ACSL SpecificationsFunction contractsFirst-order logicLoopsAssertions

Deductive VerificationHoare logicPointers and MemoryJessie Plugin

Page 26: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Hoare logic

Hoare logic

I Introduced by Floyd and Hoare (70s)I Hoare triple: {P}s{Q}, meaning: If P holds, then Q will

hold after the execution of statement sI Deduction rules on Hoare triples: Axiomatic semantic

Page 27: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Hoare logic

Some rule examples

{P}{P}P ⇒ P ′ {P ′}s{Q′} Q′ ⇒ Q

{P}s{Q}

{P}s_1{R} {R}s_2{Q}{P}s_1;s_2{Q}

e evaluates without error{P[x ← e]}x=e;{P}

{P ∧ e}s_1{Q} {P ∧ !e}s_2{Q}{P} if (e) s_1 else s_2{Q}

{I ∧ e}s{I}{I}while (e) s{I ∧ !e}

Page 28: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Hoare logic

Weakest pre-condition

I Program seen as a predicate transformerI Given a function s, a pre-condition Pre and a post-condition

PostI We start from Post at the end of the function and go

backwardsI At each step, we have a property Q and a statement s, and

compute the weakest pre-condition P such that {P}s{Q} isa valid Hoare triple.

I When we reach the beginning of the function with propertyP, we must prove Pre ⇒ P.

Page 29: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Hoare logic

Some rules

I AssignmentWP(x=e, Q) = Q[x← e]

I Sequence

WP(s_1;s_2, Q) = WP(s_1, WP(s_2, Q))

I ConditionalWP( if (e) s_1 else s_2, Q) =

e⇒WP(s_1, Q) ∧ !e⇒WP(s_2, Q)

I WhileWP(while (e) s, Q) =

I ∧ ∀ω.I ⇒ (e⇒WP(s, I) ∧ !e⇒ Q)

Page 30: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Pointers and Memory

Memory Model

IssueHow can we represent memory operations (*x, a[i]=42,. . . ) inthe logic

I If too low-level (a big array of bytes), proof obligations areintractable.

I If too abstract, some C constructions can not be represented(arbitrary pointer casts, aliasing)

I Standard solution (Burstal-Bornat): replace struct’scomponents by a function

Page 31: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Pointers and Memory

Aliasing

IssueThe same memory location can be accessed through differentmeans:int y;int* yptr = &y;*yptr = 3;/*@ assert y == 3; */

I Again, supposing that any two pointers can be aliases wouldlead to intractable proof obligations.

I Memory is separated in disjoint regionsI Some hypotheses are done (as additional pre-conditions)

Page 32: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

What is Jessie?

I Hoare-logic based plugin, developed at INRIA Saclay.I Input: a program and a specificationI Jessie generates verification conditionsI Use of Automated Theorem Provers to discharge the VCsI If all VCs are proved, the program is correct with respect to

the specificationI Otherwise: need to investigate why the proof fails

I Fix bug in the codeI Adds additional annotations to help ATPI Interactive Proof (Coq/Isabelle)

Page 33: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

What is Jessie Useful for?

UsageI Proof of functional properties of the programI Modular verification (function per function)

LimitationsI Cast between pointers and integersI Limited support for union typeI Aliasing requires some care

Page 34: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

What is Jessie Useful for?

UsageI Proof of functional properties of the programI Modular verification (function per function)

LimitationsI Cast between pointers and integersI Limited support for union typeI Aliasing requires some care

Page 35: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

What is Jessie Useful for?

UsageI Proof of functional properties of the programI Modular verification (function per function)

LimitationsI Cast between pointers and integersI Limited support for union typeI Aliasing requires some care

Page 36: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

What is Jessie Useful for?

UsageI Proof of functional properties of the programI Modular verification (function per function)

LimitationsI Cast between pointers and integersI Limited support for union typeI Aliasing requires some care

Page 37: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

What is Jessie Useful for?

UsageI Proof of functional properties of the programI Modular verification (function per function)

LimitationsI Cast between pointers and integersI Limited support for union typeI Aliasing requires some care

Page 38: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

What is Jessie Useful for?

UsageI Proof of functional properties of the programI Modular verification (function per function)

LimitationsI Cast between pointers and integersI Limited support for union typeI Aliasing requires some care

Page 39: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

From Frama-C to Theorem Provers

C file

Frama-CJessieWhy fileWhy

Verification conditions

Automated provers:Alt-ergoSimplifyZ3...

Proof assistants:CoqIsabellePVS

Page 40: Frama-C Training Session Introduction to ACSL and its GUIframa-c.com/training/presentation.pdf · Presentation ACSLSpecifications Functioncontracts First-orderlogic Loops Assertions

Deductive Verification - Jessie Plugin

In practice

I Launch GUI:frama-c -jessie file.c

I Batch processing with alt-ergo:frama-c -jessie -jessie-atp alt-ergo file.c

I Generate Coq file (to be completed interactively):frama-c -jessie -jessie-atp coq file.c

I Concentrate on functional properties:frama-c -jessie -jessie-behavior default file.c

I Concentrate on safety properties:frama-c -jessie -jessie-behavior safety file.c