Axiomatic Semantics Semantics and Application to Program Verification
Total Page:16
File Type:pdf, Size:1020Kb
Axiomatic semantics Semantics and Application to Program Verification Antoine Min´e Ecole´ normale sup´erieure, Paris year 2015{2016 Course 6 18 March 2016 Course 6 Axiomatic semantics Antoine Min´e p. 1 / 60 Introduction Operational semantics Models precisely program execution as low-level transitions between internal states (transition systems, execution traces, big-step semantics) Denotational semantics Maps programs into objects in a mathematical domain (higher level, compositional, domain oriented) Aximoatic semantics (today) Prove properties about programs programs are annotated with logical assertions a rule-system defines the validity of assertions (logical proofs) clearly separates programs from specifications (specification ' user-provided abstraction of the behavior, it is not unique) enables the use of logic tools (partial automation, increased confidence) Course 6 Axiomatic semantics Antoine Min´e p. 2 / 60 Overview Specifications (informal examples) Floyd{Hoare logic Dijkstra's predicate calculus (weakest precondition, strongest postcondition) Verification conditions (partially automated program verification) Total correctness (termination) Non-determinism Arrays Concurrency Course 6 Axiomatic semantics Antoine Min´e p. 3 / 60 Specifications Specifications Course 6 Axiomatic semantics Antoine Min´e p. 4 / 60 express the intended behavior of the function (returned value) add requirements for the function to actually behave as intended (a requires/ensures pair is a function contract) strengthen the requirements to ensure termination Specifications Example: function specification example in C + ACSL int mod(intA, intB) { int Q = 0; int R = A; while (R >= B) { R = R - B; Q = Q + 1; } returnR; } Course 6 Axiomatic semantics Antoine Min´e p. 5 / 60 add requirements for the function to actually behave as intended (a requires/ensures pair is a function contract) strengthen the requirements to ensure termination Specifications Example: function specification example in C + ACSL //@ ensures nresult == A mod B; int mod(intA, intB) { int Q = 0; int R = A; while (R >= B) { R = R - B; Q = Q + 1; } returnR; } express the intended behavior of the function (returned value) Course 6 Axiomatic semantics Antoine Min´e p. 5 / 60 strengthen the requirements to ensure termination Specifications Example: function specification example in C + ACSL //@ requires A>=0 && B>=0; //@ ensures nresult == A mod B; int mod(intA, intB) { int Q = 0; int R = A; while (R >= B) { R = R - B; Q = Q + 1; } returnR; } express the intended behavior of the function (returned value) add requirements for the function to actually behave as intended (a requires/ensures pair is a function contract) Course 6 Axiomatic semantics Antoine Min´e p. 5 / 60 Specifications Example: function specification example in C + ACSL //@ requires A>=0 && B>0; //@ ensures nresult == A mod B; int mod(intA, intB) { int Q = 0; int R = A; while (R >= B) { R = R - B; Q = Q + 1; } returnR; } express the intended behavior of the function (returned value) add requirements for the function to actually behave as intended (a requires/ensures pair is a function contract) strengthen the requirements to ensure termination Course 6 Axiomatic semantics Antoine Min´e p. 5 / 60 Specifications Example: program annotations example with full assertions //@ requires A>=0 && B>0; //@ ensures nresult == A mod B; int mod(int A, int B) { int Q = 0; int R = A; //@ assert A>=0 && B>0 && Q=0 && R==A; while (R >= B) { //@ assert A>=0 && B>0 && R>=B && A==Q*B+R; R = R - B; Q = Q + 1; } //@ assert A>=0 && B>0 && R>=0 && R<B && A==Q*B+R; return R; } Assertions give detail about the internal computations why and how contracts are fulfilled (Note: r = a mod b means 9q: a = qb + r ^ 0 ≤ r < b) Course 6 Axiomatic semantics Antoine Min´e p. 6 / 60 Specifications Example: ghost variables example with ghost variables //@ requires A>=0 && B>0; //@ ensures nresult == A mod B; int mod(int A, int B) { int R = A; while (R >= B) { R = R - B; } // 9Q: A = QB + R and 0 ≤ R < B return R; } The annotations can be more complex than the program itself Course 6 Axiomatic semantics Antoine Min´e p. 7 / 60 Specifications Example: ghost variables example with ghost variables //@ requires A>=0 && B>0; //@ ensures nresult == A mod B; int mod(int A, int B) { //@ ghost int q = 0; int R = A; //@ assert A>=0 && B>0 &&q=0 && R==A; while (R >= B) { //@ assert A>=0 && B>0 && R>=B && A==q*B+R; R = R - B; //@ ghost q = q + 1; } //@ assert A>=0 && B>0 && R>=0 && R<B && A==q*B+R; return R; } The annotations can be more complex than the program itself and require reasoning on enriched states (ghost variables) Course 6 Axiomatic semantics Antoine Min´e p. 7 / 60 Specifications Example: class invariants example in ESC/Java public class OrderedArray { int a[]; int nb; //@invariant nb >= 0 && nb <= 20 //@invariant (nforall int i; (i >= 0 && i < nb-1) ==> a[i] <= a[i+1]) public OrderedArray() { a = new int[20]; nb = 0; } public void add(int v) { if (nb >= 20) return; int i; for (i=nb; i > 0 && a[i-1] > v; i--) a[i] = a[i-1]; a[i] = v; nb++; } } class invariant: property of the fields true outside all methods it can be temporarily broken within a method but it must be restored before exiting the method Course 6 Axiomatic semantics Antoine Min´e p. 8 / 60 Specifications Language support Contracts (and class invariants): built in few languages (Eiffel) available as a library / external tool (C, Java, C#, etc.) Contracts can be: checked dynamically checked statically (Frama-C, Why, ESC/Java) inferred statically (CodeContracts) In this course: deductive methods (logic) to check (prove) statically (at compile-time) partially automatically (with user help) that contracts hold Course 6 Axiomatic semantics Antoine Min´e p. 9 / 60 Floyd{Hoare logic Floyd{Hoare logic Course 6 Axiomatic semantics Antoine Min´e p. 10 / 60 Floyd{Hoare logic Hoare triples Hoare triple: fPg prog fQg prog is a program fragment P and Q are logical assertions over program variables def (e.g. P =( X ≥ 0 ^ Y ≥ 0) _ (X < 0 ^ Y < 0)) A triple means: if P holds before prog is executed then Q holds after the execution of prog unless prog does not terminate or encounters an error P is the precondition, Q is the postcondition fPg prog fQg expresses partial correctness (does not rule out errors and non-termination) Hoare triples serve as judgements in a proof system (introduced in [Hoare69]) Course 6 Axiomatic semantics Antoine Min´e p. 11 / 60 Floyd{Hoare logic Language stat ::= X expr (assignment) j skip (do nothing) j fail (error) j stat; stat (sequence) j if expr then stat else stat (conditional) j while expr do stat (loop) X 2 V: integer-valued variables expr: integer arithmetic expressions we assume that: expressions are deterministic (for now) expression evaluation does not cause error (only fail does) for instance, to avoid divisions by zero, we assume that all divisions are explicitly guarded as in : if X = 0 then fail else ··· =X ··· Course 6 Axiomatic semantics Antoine Min´e p. 12 / 60 Floyd{Hoare logic Hoare rules: axioms Axioms: fPg skip fPg fPg fail fQg any property true before skip is true afterwards any property is true after fail Course 6 Axiomatic semantics Antoine Min´e p. 13 / 60 Floyd{Hoare logic Hoare rules: axioms Assignment axiom: fP[e=X ]g X e fPg for P over X to be true after X e P must be true over e before the assignment P[e=X ] is P where all free occurrences of X are replaced with e e must be deterministic the rule is \backwards": P appears as a postcondition examples: ftrueg X 5 fX = 5g fY = 5g X Y fX = 5g fX + 1 ≥ 0g X X + 1 fX ≥ 0g ffalseg X Y + 3 fY = 0 ^ X = 12g fY 2 [0; 10]g X Y + 3 fX = Y + 3 ^ Y 2 [0; 10]g Course 6 Axiomatic semantics Antoine Min´e p. 14 / 60 Floyd{Hoare logic Hoare rules: consequence Rule of consequence: P ) P0 Q0 ) Q fP0g c fQ0g fPg c fQg we can weaken a Hoare triple by: weakening its postcondition Q ( Q0 strengthening its precondition P ) P0 we assume a logic system to be available to prove formulas on assertions, such as P ) P0 (e.g., arithmetic, set theory, etc.) examples: the axiom for fail can be replaced with ftrueg fail ffalseg (as P ) true and false ) Q always hold) fX = 99 ^ Y 2 [1; 10]g X Y + 10 fX = Y + 10 ^ Y 2 [1; 10]g (as fY 2 [1; 10]g X Y + 10 fX = Y + 10 ^ Y 2 [1; 10]g and X = 99 ^ Y 2 [1; 10] ) Y 2 [1; 10]) Course 6 Axiomatic semantics Antoine Min´e p. 15 / 60 Floyd{Hoare logic Hoare rules: tests fP ^ eg s fQg fP ^ :eg t fQg Tests: fPg if e then s else t fQg to prove that Q holds after the test we prove that it holds after each branch (s, t) under the assumption that the branch is executed (e, :e) example: fX < 0g X −X fX > 0g fX > 0g skip fX > 0g f(X 6= 0) ^ (X < 0)g X −X fX > 0g f(X 6= 0) ^ (X ≥ 0)g skip fX > 0g fX 6= 0g if X < 0 then X −X else skip fX > 0g Course 6 Axiomatic semantics Antoine Min´e p. 16 / 60 Floyd{Hoare logic Hoare rules: sequences fPg s fRg fRg t fQg Sequences: fPg s; t fQg to prove a sequence s; t we must invent an intermediate assertion R implied by P after s, and implying Q after t (often denoted fPg s fRg t fQg) example: fX = 1 ^ Y = 1g X X + 1 fX = 2 ^ Y = 1g Y Y − 1 fX = 2 ^ Y = 0g Course 6 Axiomatic semantics Antoine Min´e p.