Encoding Hoare Logic in Typed Certified Code Nikolaos S. Papaspyrou - - PowerPoint PPT Presentation

encoding hoare logic in typed certified code
SMART_READER_LITE
LIVE PREVIEW

Encoding Hoare Logic in Typed Certified Code Nikolaos S. Papaspyrou - - PowerPoint PPT Presentation

Encoding Hoare Logic in Typed Certified Code Nikolaos S. Papaspyrou Michalis A. Papakyriakou Angelos Manousaridis National Technical University of Athens School of Electrical and Computer Engineering Software Engineering Laboratory {nickie,


slide-1
SLIDE 1

Encoding Hoare Logic in Typed Certified Code

Nikolaos S. Papaspyrou Michalis A. Papakyriakou Angelos Manousaridis

National Technical University of Athens School of Electrical and Computer Engineering Software Engineering Laboratory {nickie, mpapakyr, amanous}@softlab.ntua.gr

5th Panhellenic Logic Symposium Athens, July 25-28, 2005

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-2
SLIDE 2

Outline

Motivation Hoare logic Typed certified code Can we combine the two? Our approach The type language The computation language Encoding Hoare logic Problems with Hoare Logic And their Solution Example Conclusions

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-3
SLIDE 3

Hoare Logic (i)

◮ Introduced the strength of formal logic in

computer programming

◮ A tool to:

◮ reason about program properties and

prove correctness

◮ derive programs from their specifications

  • C. A. R. Hoare, “An axiomatic basis for computer

programming”, Communications of the ACM,

  • vol. 12, no. 10, pp. 576–585, 1969.
  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-4
SLIDE 4

Hoare Logic (ii)

◮ Hoare triples represent program specifications

{P} program {Q}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-5
SLIDE 5

Hoare Logic (ii)

◮ Hoare triples represent program specifications

{P} program {Q}

◮ Example: greatest common divisor

{n+m > 0} a := n; b := m; while a > 0 and b > 0 do if a > b then a := a mod b else b := b mod a; r := a+b {r > 0 ∧ r\n ∧ r\m ∧ (∀r′ ∈ N. r′\n ∧ r′\m ⇒ r′ ≤ r)}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-6
SLIDE 6

Typed Certified Code (i)

Methodology:

◮ a sound formal logic is used ◮ combined with the programming language ◮ program specifications are expressed as

propositions in this logic

◮ proofs of these propositions are embedded in

programs

◮ either explicitly given by the programmer ◮ or automatically constructed by the compiler

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-7
SLIDE 7

Typed Certified Code (ii)

Proposed solutions:

◮ Typed Intermediate Language (TIL); Harper and

Morrisett, 1995

◮ Typed Assembly Language (TAL); Morrisett,

Walker, Crary and Glew, 1998

◮ Proof-Carrying Code (PCC); Necula, 1998 ◮ Foundational Proof-Carrying Code, Appel, 2001 ◮ Shao, Saha, Trifonov and Papaspyrou, 2002, 2005 ◮ Crary and Vanderwaart, 2002

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-8
SLIDE 8

Typed Certified Code (iii)

◮ Example: greatest common divisor

◮ gcd : nat ։ nat ։ nat

“A function taking two naturals and returning some natural.”

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-9
SLIDE 9

Typed Certified Code (iii)

◮ Example: greatest common divisor

◮ gcd : nat ։ nat ։ nat

“A function taking two naturals and returning some natural.”

◮ gcd : ∀n:Nat.∀m:Nat.∀p∗:(n+m > 0).

snatn ։ snatm ։ nat “One of the arguments shall not be zero.”

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-10
SLIDE 10

Typed Certified Code (iii)

◮ Example: greatest common divisor

◮ gcd : nat ։ nat ։ nat

“A function taking two naturals and returning some natural.”

◮ gcd : ∀n:Nat.∀m:Nat.∀p∗:(n+m > 0).

snatn ։ snatm ։ nat “One of the arguments shall not be zero.”

◮ Singleton type

snatn A data type whose elements are representations

  • f the single integer value n : Nat
  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-11
SLIDE 11

Typed Certified Code (iii)

◮ Example: greatest common divisor

◮ gcd : nat ։ nat ։ nat

“A function taking two naturals and returning some natural.”

◮ gcd : ∀n:Nat.∀m:Nat.∀p∗:(n+m > 0).

snatn ։ snatm ։ nat “One of the arguments shall not be zero.”

◮ Singleton type

snatn A data type whose elements are representations

  • f the single integer value n : Nat

◮ (Syntactic sugar)

nat ≡ ∃r:Nat.snatr

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-12
SLIDE 12

Typed Certified Code (iv)

◮ Example (continued)

◮ gcd : ∀n:Nat.∀m:Nat.∀p∗:(n+m > 0).

snatn ։ snatm ։ ∃r:Nat. ∃q∗:(r > 0). snatr “The result shall not be zero.”

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-13
SLIDE 13

Typed Certified Code (iv)

◮ Example (continued)

◮ gcd : ∀n:Nat.∀m:Nat.∀p∗:(n+m > 0).

snatn ։ snatm ։ ∃r:Nat. ∃q∗:(r > 0). snatr “The result shall not be zero.”

◮ gcd : ∀n:Nat.∀m:Nat.∀p∗:(n+m > 0).

snatn ։ snatm ։ ∃r:Nat. ∃q∗:(r > 0∧r\n∧r\m). snatr “The result shall not be zero and shall divide both arguments.”

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-14
SLIDE 14

Typed Certified Code (v)

◮ Example (continued)

◮ gcd : ∀n:Nat.∀m:Nat.∀p∗:(n+m > 0).

snatn ։ snatm ։ ∃r:Nat. ∃q∗

1:(r > 0∧r\n∧r\m).

∃q∗

2:(Πr′:Nat.r′\n∧r′\m → r′ ≤ r).

snatr “The result shall be the greatest common divisor

  • f the two arguments.”
  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-15
SLIDE 15

Can we combine the two?

◮ Hoare Logic

+ long studied, large body of scientific knowledge + simple axioms and rules + works with variables and destructive update − does not work well with (higher-order) functions − proofs of specifications cannot be automatically verified

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-16
SLIDE 16

Can we combine the two?

◮ Hoare Logic

+ long studied, large body of scientific knowledge + simple axioms and rules + works with variables and destructive update − does not work well with (higher-order) functions − proofs of specifications cannot be automatically verified

◮ Typed Certified Code

− relatively new approach − highly complex type system − does not work well with variables and destructive update + works well with (higher-order) functions + proofs of specifications can be automatically verified

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-17
SLIDE 17

Overview of the Type Language

◮ A variation of the Calculus of Inductive

Constructions

◮ Incorporates higher-order predicate logic ◮ Complete grammar:

A,B ::= Set | Type | Ext | X | ΠX:A.B | λ X:A.B | AB | Ind(X:A){ A} | Constr(n,A) | Elim[A′](A:B B){ A}

A → B ≡ ΠXnew:A.B Papaspyrou, Vytiniotis and Koutavas, “Logic-Enhanced Type Systems”, PLS 4, 2003. Shao, Trifonov, Saha and Papaspyrou, “A Type System for Certified Binaries”, ACM TOPLAS,

  • vol. 27, no. 1, pp. 1-45, 2005.
  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-18
SLIDE 18

The Computation Language (i)

◮ The simple imperative language WHILE

x : Var variables e : Expr ::= n | b | x | ⋄e | e⋆e c : Comm ::= skip | x := e | c;c | if e then c else c | while e do c ⋄ : UnOp ::= − | ¬ ⋆ : BinOp ::= + | − | ∗ | div | mod | = | = | < | > | ≤ | ≥ | and | or

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-19
SLIDE 19

The Computation Language (ii)

Typing

◮ Types

τ : Ω ::= int | bool

◮ Type environments

Γ : Env = Var → Ω

◮ Typing of expressions

Γ ⊢ e : τ

◮ Typing of commands

Γ ⊢ c

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-20
SLIDE 20

The Computation Language (ii)

Typing

◮ Types

τ : Ω ::= int | bool

◮ Type environments

Γ : Env = Var → Ω

◮ Typing of expressions

Γ ⊢ e : τ

◮ Typing of commands

Γ ⊢ c Semantics

◮ Meaning of types

[ [int] ] = Int, [ [bool] ] = Bool

◮ Stores

s : StoreΓ = Πx:Var.[ [Γx] ]

◮ Meaning of expressions

[ [e] ]s ⇓ v

◮ Meaning of commands

[ [c] ]s ⇓ s′

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-21
SLIDE 21

Encoding Hoare Logic (i)

◮ Predicates

P,Q,R : PredΓ = StoreΓ → Set

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-22
SLIDE 22

Encoding Hoare Logic (i)

◮ Predicates

P,Q,R : PredΓ = StoreΓ → Set

◮ Specification of commands

{P} c {Q} Γ ⊢ c P,Q : PredΓ

◮ {P} c {Q} is valid if for all s : StoreΓ,

if Ps and [ [c] ]s ⇓ s′, for some s′ : StoreΓ, then Qs′

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-23
SLIDE 23

Encoding Hoare Logic (i)

◮ Predicates

P,Q,R : PredΓ = StoreΓ → Set

◮ Specification of commands

{P} c {Q} Γ ⊢ c P,Q : PredΓ

◮ {P} c {Q} is valid if for all s : StoreΓ,

if Ps and [ [c] ]s ⇓ s′, for some s′ : StoreΓ, then Qs′

◮ Specification of expressions

{P} e {F} Γ ⊢ e : τ P : PredΓ F : [ [τ ] ] → PredΓ

◮ {P} e {F} is valid if for all s : StoreΓ,

if Ps and [ [e] ]s ⇓ v for some v : [ [τ ] ], then F vs

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-24
SLIDE 24

Encoding Hoare Logic (ii)

◮ Hoare logic axioms and inference rules

{P} skip {P} {P} e {λ v.λ s.Qs{x → v}} {P} x := e {Q} {P} c1 {R} {R} c2 {Q} {P} c1;c2 {Q} {P} e {F} {F true} c1 {Q} {F false} c2 {Q} {P} if e then c1 else c2 {Q} {P} e {F} {F true} c {P} {P} while e do c {F false}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-25
SLIDE 25

Encoding Hoare Logic (ii)

◮ Hoare logic axioms and inference rules

{P} skip {P} {P} e {λ v.λ s.Qs{x → v}} {P} x := e {Q} {P} c1 {R} {R} c2 {Q} {P} c1;c2 {Q} {P} e {F} {F true} c1 {Q} {F false} c2 {Q} {P} if e then c1 else c2 {Q} {P} e {F} {F true} c {P} {P} while e do c {F false}

◮ Consequence rules

P ⇒ P′ {P′} c {Q} {P} c {Q} {P} c {Q′} Q′ ⇒ Q {P} c {Q}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-26
SLIDE 26

Problems with Hoare Logic

Proof of specifications is undecidable!

◮ e.g. can we find the unknown R?

{P} c1 {R} {R} c2 {Q} {P} c1;c2 {Q}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-27
SLIDE 27

Problems with Hoare Logic

Proof of specifications is undecidable!

◮ e.g. can we find the unknown R?

{P} c1 {R} {R} c2 {Q} {P} c1;c2 {Q}

◮ Weakest preconditions

(Dijkstra 1976) R = wp[c2](Q)

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-28
SLIDE 28

Problems with Hoare Logic

Proof of specifications is undecidable!

◮ e.g. can we find the unknown R?

{P} c1 {R} {R} c2 {Q} {P} c1;c2 {Q}

◮ Weakest preconditions

(Dijkstra 1976) R = wp[c2](Q)

◮ but what about F and P′?

{P} e {F} {F true} c {P} {P} while e do c {F false} P ⇒ P′ {P′} c {Q} {P} c {Q}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-29
SLIDE 29

Problems with Hoare Logic

Proof of specifications is undecidable!

◮ e.g. can we find the unknown R?

{P} c1 {R} {R} c2 {Q} {P} c1;c2 {Q}

◮ Weakest preconditions

(Dijkstra 1976) R = wp[c2](Q)

◮ but what about F and P′?

{P} e {F} {F true} c {P} {P} while e do c {F false} P ⇒ P′ {P′} c {Q} {P} c {Q}

◮ Solution: annotate programs with proof hints!

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-30
SLIDE 30

And their Solution (i)

Annotated computation language WHILE

e : AExprΓ ::= ... | assert[p : P ⇒ Q],e c : ACommΓ ::= ... | inv[P] while e do c | assert[p : P ⇒ Q]

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-31
SLIDE 31

And their Solution (i)

Annotated computation language WHILE

e : AExprΓ ::= ... | assert[p : P ⇒ Q],e c : ACommΓ ::= ... | inv[P] while e do c | assert[p : P ⇒ Q]

Proof hints:

◮ All loops have explicit invariants

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-32
SLIDE 32

And their Solution (i)

Annotated computation language WHILE

e : AExprΓ ::= ... | assert[p : P ⇒ Q],e c : ACommΓ ::= ... | inv[P] while e do c | assert[p : P ⇒ Q]

Proof hints:

◮ All loops have explicit invariants ◮ All uses of the consequence rules are replaced

by assert constructs, which

◮ provide the implication involved

P ⇒ Q

◮ provide a proof of this implication

p p : Πs:StoreΓ.Ps → Qs

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-33
SLIDE 33

And their Solution (ii)

◮ Revised axioms and inference rules

{P} assert[p : P ⇒ Q] {Q} {P} e {F} {F true} c {P} {P} inv[P] while e do c {F false}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-34
SLIDE 34

And their Solution (ii)

◮ Revised axioms and inference rules

{P} assert[p : P ⇒ Q] {Q} {P} e {F} {F true} c {P} {P} inv[P] while e do c {F false}

◮ Well definedness of weakest preconditions

if c = assert[p : P ⇒ Q] then wp[c](Q′) = P provided Q ⇔ Q′

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-35
SLIDE 35

And their Solution (ii)

◮ Revised axioms and inference rules

{P} assert[p : P ⇒ Q] {Q} {P} e {F} {F true} c {P} {P} inv[P] while e do c {F false}

◮ Well definedness of weakest preconditions

if c = assert[p : P ⇒ Q] then wp[c](Q′) = P provided Q ⇔ Q′

◮ The relation P ⇔ Q defines a decidable notion of

predicate equivalence

◮ We use αβηι-equality in CIC ◮ A weaker notion of equivalence would result in

fewer explicit assertions

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-36
SLIDE 36

And their Solution (iii)

Theorem

Annotations preserve typing, semantics and logic

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-37
SLIDE 37

And their Solution (iii)

Theorem

Annotations preserve typing, semantics and logic

Theorem

Weakest preconditions are correct and exact

  • 1. If wp[c](Q) is defined

then {wp[c](Q)} c {Q} is derivable.

  • 2. If {P} c {Q} is derivable

then wp[c](Q) is defined and P ⇔ wp[c](Q)

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-38
SLIDE 38

And their Solution (iii)

Theorem

Annotations preserve typing, semantics and logic

Theorem

Weakest preconditions are correct and exact

  • 1. If wp[c](Q) is defined

then {wp[c](Q)} c {Q} is derivable.

  • 2. If {P} c {Q} is derivable

then wp[c](Q) is defined and P ⇔ wp[c](Q)

Theorem

Proof of specifications is decidable

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-39
SLIDE 39

Example

◮ Find the integer part of log2(n)

{λ s.sn ≥ 1∧sn = X} m := 0; while n > 1 do n := n div 2; m := m+1 {λ s.2sm ≤ X < 2sm+1}

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-40
SLIDE 40

Example

◮ Find the integer part of log2(n)

{λ s.sn ≥ 1∧sn = X} m := 0; while n > 1 do n := n div 2; m := m+1 {λ s.2sm ≤ X < 2sm+1}

◮ Loop invariant

λ s.X/2sm = sn ∧ sn ≥ 1 ∧ sm ≥ 0

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-41
SLIDE 41

Example

◮ Find the integer part of log2(n)

{λ s.sn ≥ 1∧sn = X} m := 0; while n > 1 do n := n div 2; m := m+1 {λ s.2sm ≤ X < 2sm+1}

◮ Loop invariant

λ s.X/2sm = sn ∧ sn ≥ 1 ∧ sm ≥ 0

◮ 4 assertions, of which the hardest to prove is

(λ s.X/2sm = sn ∧ sn ≥ 1 ∧ sm ≥ 0 ∧ sn ≤ 1) ⇒ (λ s.2sm ≤ X < 2sm+1)

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-42
SLIDE 42

Conclusions (i)

Our contribution

◮ Hoare logic and type certified code combined ◮ Certified programs can be represented in a

high-level imperative language with proof hints as annotations

◮ Specifications are expressed as Hoare triples ◮ Proof checking is decidable and efficient ◮ The annotated language is consistent to the

  • riginal in terms of typing, operational semantics

and validity of specifications

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-43
SLIDE 43

Conclusions (ii)

Related work

◮ Hamid and Shao (2004): low-level typed

assembly programs, predefined safety policy

◮ Franssen and de Swart (2004): similar,

many-sorted first-order logic, differs in expressiveness and non-foundational character

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-44
SLIDE 44

Conclusions (ii)

Related work

◮ Hamid and Shao (2004): low-level typed

assembly programs, predefined safety policy

◮ Franssen and de Swart (2004): similar,

many-sorted first-order logic, differs in expressiveness and non-foundational character Future work

◮ Fewer explicit assertions: weaken the P ⇔ Q

equivalence relation

◮ amounts to theorem proving

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code

slide-45
SLIDE 45

Conclusions (ii)

Related work

◮ Hamid and Shao (2004): low-level typed

assembly programs, predefined safety policy

◮ Franssen and de Swart (2004): similar,

many-sorted first-order logic, differs in expressiveness and non-foundational character Future work

◮ Fewer explicit assertions: weaken the P ⇔ Q

equivalence relation

◮ amounts to theorem proving

◮ Exploit more of Hoare logic’s benefits in typed

certified code

◮ variables and destructive update ◮ pointers and dynamic variables

  • N. S. Papaspyrou, M. A. Papakyriakou, A. Manousaridis

Encoding Hoare Logic in Typed Certified Code