separation logic for non local control flow and block
play

Separation Logic for Non-local Control Flow and Block Scope - PowerPoint PPT Presentation

Separation Logic for Non-local Control Flow and Block Scope Variables Robbert Krebbers Joint work with Freek Wiedijk Radboud University Nijmegen February 4, 2013 @ Gallium, INRIA Rocquencourt, France What is this program supposed to do? int


  1. Program contexts (1) Singular program contexts: e | params � E ::= E S | block b � | call f � b where: ◮ block b � associates a block scope variable with its corresponding memory index b ◮ call f � e contains the location of the caller so that it can be restored when f returns ◮ params � b contains the memory indexes of the function parameters

  2. Program contexts (1) Singular program contexts: e | params � E ::= E S | block b � | call f � b where: ◮ block b � associates a block scope variable with its corresponding memory index b ◮ call f � e contains the location of the caller so that it can be restored when f returns ◮ params � b contains the memory indexes of the function parameters Program contexts k are lists of singular program contexts

  3. Program contexts (2) ◮ Program contexts contain the stack: getstack ( E S :: k ) := getstack k getstack ( block b � :: k ) := b :: getstack k getstack (call f � e :: k ) := [ ] getstack (params � b :: k ) := � b + + getstack k

  4. Program contexts (2) ◮ Program contexts contain the stack: getstack ( E S :: k ) := getstack k getstack ( block b � :: k ) := b :: getstack k getstack (call f � e :: k ) := [ ] getstack (params � b :: k ) := � b + + getstack k ◮ Remark: not getstack (call f � e :: k ) := getstack k

  5. States A state S ( k , φ, m ) consists of a program context k , focus φ , and memory m

  6. States A state S ( k , φ, m ) consists of a program context k , focus φ , and memory m We consider the following focuses: ◮ ( d , s ) execution of a statement s in direction d

  7. States A state S ( k , φ, m ) consists of a program context k , focus φ , and memory m We consider the following focuses: ◮ ( d , s ) execution of a statement s in direction d ◮ call f � v calling a function f ( � v )

  8. States A state S ( k , φ, m ) consists of a program context k , focus φ , and memory m We consider the following focuses: ◮ ( d , s ) execution of a statement s in direction d ◮ call f � v calling a function f ( � v ) ◮ return returning from a function

  9. Example The corresponding state is S ( k , φ, m ), where: int *p = NULL ◮ k = [ � ; goto l , l: x 0 := int 10 ; � , if (p) block b j � , if ( load x 0 ) return � , return int j = 10 l : � , x 0 := NULL ; � , ; block b p � p = &j goto l ] ◮ φ = ( ր , x 1 := x 0 ) ◮ m = { b p �→ ptr b j , b j �→ 10 }

  10. The small step semantics Some rules: ◮ S ( k , ( ց , e 1 := e 2 ) , m ) � S ( k , ( ր , e 1 := e 2 ) , m [ a := v ]) for a and v s.t. [ [ e 1 ] ] k , m = ptr a , [ [ e 2 ] ] k , m = v and m a � = ⊥ .

  11. The small step semantics Some rules: ◮ S ( k , ( ց , e 1 := e 2 ) , m ) � S ( k , ( ր , e 1 := e 2 ) , m [ a := v ]) for a and v s.t. [ [ e 1 ] ] k , m = ptr a , [ [ e 2 ] ] k , m = v and m a � = ⊥ . ◮ S ( k , ( ց , s 1 ; s 2 ) , m ) � S (( � ; s 2 ) :: k , ( ց , s 1 ) , m )

  12. The small step semantics Some rules: ◮ S ( k , ( ց , e 1 := e 2 ) , m ) � S ( k , ( ր , e 1 := e 2 ) , m [ a := v ]) for a and v s.t. [ [ e 1 ] ] k , m = ptr a , [ [ e 2 ] ] k , m = v and m a � = ⊥ . ◮ S ( k , ( ց , s 1 ; s 2 ) , m ) � S (( � ; s 2 ) :: k , ( ց , s 1 ) , m ) ◮ S (( � ; s 2 ) :: k , ( ր , s 1 ) , m ) � S (( s 1 ; � ) :: k , ( ց , s 2 ) , m )

  13. The small step semantics Some rules: ◮ S ( k , ( ց , e 1 := e 2 ) , m ) � S ( k , ( ր , e 1 := e 2 ) , m [ a := v ]) for a and v s.t. [ [ e 1 ] ] k , m = ptr a , [ [ e 2 ] ] k , m = v and m a � = ⊥ . ◮ S ( k , ( ց , s 1 ; s 2 ) , m ) � S (( � ; s 2 ) :: k , ( ց , s 1 ) , m ) ◮ S (( � ; s 2 ) :: k , ( ր , s 1 ) , m ) � S (( s 1 ; � ) :: k , ( ց , s 2 ) , m ) ◮ S (( s 1 ; � ) :: k , ( ր , s 2 ) , m ) � S ( k , ( ր , s 1 ; s 2 ) , m )

  14. The small step semantics Some rules: ◮ S ( k , ( ց , e 1 := e 2 ) , m ) � S ( k , ( ր , e 1 := e 2 ) , m [ a := v ]) for a and v s.t. [ [ e 1 ] ] k , m = ptr a , [ [ e 2 ] ] k , m = v and m a � = ⊥ . ◮ S ( k , ( ց , s 1 ; s 2 ) , m ) � S (( � ; s 2 ) :: k , ( ց , s 1 ) , m ) ◮ S (( � ; s 2 ) :: k , ( ր , s 1 ) , m ) � S (( s 1 ; � ) :: k , ( ց , s 2 ) , m ) ◮ S (( s 1 ; � ) :: k , ( ր , s 2 ) , m ) � S ( k , ( ր , s 1 ; s 2 ) , m ) ◮ S ( k , ( ց , goto l ) , m ) � S ( k , ( � l , goto l ) , m )

  15. The small step semantics Some rules: ◮ S ( k , ( ց , e 1 := e 2 ) , m ) � S ( k , ( ր , e 1 := e 2 ) , m [ a := v ]) for a and v s.t. [ [ e 1 ] ] k , m = ptr a , [ [ e 2 ] ] k , m = v and m a � = ⊥ . ◮ S ( k , ( ց , s 1 ; s 2 ) , m ) � S (( � ; s 2 ) :: k , ( ց , s 1 ) , m ) ◮ S (( � ; s 2 ) :: k , ( ր , s 1 ) , m ) � S (( s 1 ; � ) :: k , ( ց , s 2 ) , m ) ◮ S (( s 1 ; � ) :: k , ( ր , s 2 ) , m ) � S ( k , ( ր , s 1 ; s 2 ) , m ) ◮ S ( k , ( ց , goto l ) , m ) � S ( k , ( � l , goto l ) , m ) ◮ S ( k , ( � l , l : s ) , m ) � S (( l : � ) :: k , ( ց , s ) , m )

  16. The small step semantics Some rules: ◮ S ( k , ( ց , e 1 := e 2 ) , m ) � S ( k , ( ր , e 1 := e 2 ) , m [ a := v ]) for a and v s.t. [ [ e 1 ] ] k , m = ptr a , [ [ e 2 ] ] k , m = v and m a � = ⊥ . ◮ S ( k , ( ց , s 1 ; s 2 ) , m ) � S (( � ; s 2 ) :: k , ( ց , s 1 ) , m ) ◮ S (( � ; s 2 ) :: k , ( ր , s 1 ) , m ) � S (( s 1 ; � ) :: k , ( ց , s 2 ) , m ) ◮ S (( s 1 ; � ) :: k , ( ր , s 2 ) , m ) � S ( k , ( ր , s 1 ; s 2 ) , m ) ◮ S ( k , ( ց , goto l ) , m ) � S ( k , ( � l , goto l ) , m ) ◮ S ( k , ( � l , l : s ) , m ) � S (( l : � ) :: k , ( ց , s ) , m ) ◮ S ( k , ( ց , f ( � e )) , m ) � S (call f � e :: k , call f � v , m ) provided that [ [ e i ] ] k , m = v i for each i

  17. The small step semantics Lemma The small step semantics behaves as traversing through a zipper. That is, if S ( k , ( d , s ) , m ) � ∗ k S ( k , ( d ′ , s ′ ) , m ′ ) then s = s ′ .

  18. Hoare triples Traditional Hoare triples are of the shape { P } s { Q } Intuitive meaning: ◮ If P holds for the state before execution of s , ◮ and execution of s terminates, ◮ then Q will hold afterwards

  19. Extended Hoare ‘triples’ (1) Our Hoare sextuples are of the shape ∆; J ; R ⊢ { P } s { Q }

  20. Extended Hoare ‘triples’ (1) Our Hoare sextuples are of the shape ∆; J ; R ⊢ { P } s { Q } where: ◮ ∆ maps function names to their pre- and postconditions

  21. Extended Hoare ‘triples’ (1) Our Hoare sextuples are of the shape ∆; J ; R ⊢ { P } s { Q } where: ◮ ∆ maps function names to their pre- and postconditions ◮ J maps labels to their jumping condition When executing a goto l , the assertion J l has to hold

  22. Extended Hoare ‘triples’ (1) Our Hoare sextuples are of the shape ∆; J ; R ⊢ { P } s { Q } where: ◮ ∆ maps function names to their pre- and postconditions ◮ J maps labels to their jumping condition When executing a goto l , the assertion J l has to hold ◮ R has to hold to execute a return

  23. Extended Hoare ‘triples’ (2) Our Hoare sextuples are of the shape ∆; J ; R ⊢ { P } s { Q } Observations: ◮ The assertions P , Q , J and R correspond to the four directions ց , ր , � and ↑ ↑

  24. Extended Hoare ‘triples’ (2) Our Hoare sextuples are of the shape ∆; J ; R ⊢ { P } s { Q } Observations: ◮ The assertions P , Q , J and R correspond to the four directions ց , ր , � and ↑ ↑ ◮ We thus treat the sextuple as ∆; ¯ P ⊢ s where ¯ P ց = P , ¯ P ր = Q , ¯ P ( � l ) = J l and ¯ ↑ = R P ↑

  25. Some Hoare rules Weakening: ( ∀ l ∈ labels s . J ′ l → Jl ) ∈ labels s . Jl → J ′ l ) R → R ′ ( ∀ l / P ′ → P Q → Q ′ ∆; J ; R ⊢ { P } s { Q } ∆; J ′ ; R ′ ⊢ { P ′ } s { Q ′ }

  26. Some Hoare rules Weakening: ( ∀ l ∈ labels s . J ′ l → Jl ) ∈ labels s . Jl → J ′ l ) R → R ′ ( ∀ l / P ′ → P Q → Q ′ ∆; J ; R ⊢ { P } s { Q } ∆; J ′ ; R ′ ⊢ { P ′ } s { Q ′ } Composition: ∆; J ; R ⊢ { P } s 1 { P ′ } ∆; J ; R ⊢ { P ′ } s 2 { Q } ∆; J ; R ⊢ { P } s 1 ; s 2 { Q }

  27. Some Hoare rules Weakening: ( ∀ l ∈ labels s . J ′ l → Jl ) ∈ labels s . Jl → J ′ l ) R → R ′ ( ∀ l / P ′ → P Q → Q ′ ∆; J ; R ⊢ { P } s { Q } ∆; J ′ ; R ′ ⊢ { P ′ } s { Q ′ } Composition: ∆; J ; R ⊢ { P } s 1 { P ′ } ∆; J ; R ⊢ { P ′ } s 2 { Q } ∆; J ; R ⊢ { P } s 1 ; s 2 { Q } Non-local control: ∆; J ; R ⊢ { R } return { Q }

  28. Some Hoare rules Weakening: ( ∀ l ∈ labels s . J ′ l → Jl ) ∈ labels s . Jl → J ′ l ) R → R ′ ( ∀ l / P ′ → P Q → Q ′ ∆; J ; R ⊢ { P } s { Q } ∆; J ′ ; R ′ ⊢ { P ′ } s { Q ′ } Composition: ∆; J ; R ⊢ { P } s 1 { P ′ } ∆; J ; R ⊢ { P ′ } s 2 { Q } ∆; J ; R ⊢ { P } s 1 ; s 2 { Q } Non-local control: ∆; J ; R ⊢ { R } return { Q } ∆; J ; R ⊢ { J l } s { Q } ∆; J ; R ⊢ { J l } goto l { Q } ∆; J ; R ⊢ { J l } l : s { Q }

  29. The frame rule Used for local reasoning ∆; J ; R ⊢ { P } s { Q } ∆; J ∗ A ; R ∗ A ⊢ { P ∗ A } s { Q ∗ A }

  30. The frame rule Used for local reasoning ∆; J ; R ⊢ { P } s { Q } ∆; J ∗ A ; R ∗ A ⊢ { P ∗ A } s { Q ∗ A } Using our alternative notation: ∆; ¯ P ⊢ s ∆; ¯ P ∗ A ⊢ s

  31. The block scope variable rule The assertion A ↑ lifts the DeBruijn indexes in A ∆; J ↑ ∗ x 0 �→ -; R ↑ ∗ x 0 �→ - ⊢ { P ↑ ∗ x 0 �→ - } s { Q ↑ ∗ x 0 �→ - } ∆; J ; R ⊢ { P } block s { Q }

  32. The block scope variable rule The assertion A ↑ lifts the DeBruijn indexes in A ∆; J ↑ ∗ x 0 �→ -; R ↑ ∗ x 0 �→ - ⊢ { P ↑ ∗ x 0 �→ - } s { Q ↑ ∗ x 0 �→ - } ∆; J ; R ⊢ { P } block s { Q } Using our alternative notation ∆; ¯ P ↑ ∗ x 0 �→ - ⊢ s ∆; ¯ P ⊢ block s

  33. Example void swap(int *p, int *q) { int z = *p; *p = *q; *q = z; }

  34. Example { x 0 �→ p ∗ x 1 �→ q ∗ p �→ y ∗ q �→ z } block ( x 0 := load ( load x 1 ) ; load x 1 := load ( load x 2 ) ; load x 2 := load x 0 ) { x 0 �→ p ∗ x 1 �→ q ∗ p �→ z ∗ q �→ y }

  35. Example { x 0 �→ p ∗ x 1 �→ q ∗ p �→ y ∗ q �→ z } block ( { x 0 �→ - ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ y ∗ q �→ z } x 0 := load ( load x 1 ) ; load x 1 := load ( load x 2 ) ; load x 2 := load x 0 ) { x 0 �→ p ∗ x 1 �→ q ∗ p �→ z ∗ q �→ y }

  36. Example { x 0 �→ p ∗ x 1 �→ q ∗ p �→ y ∗ q �→ z } block ( { x 0 �→ - ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ y ∗ q �→ z } x 0 := load ( load x 1 ) ; { x 0 �→ y ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ y ∗ q �→ z } load x 1 := load ( load x 2 ) ; load x 2 := load x 0 ) { x 0 �→ p ∗ x 1 �→ q ∗ p �→ z ∗ q �→ y }

  37. Example { x 0 �→ p ∗ x 1 �→ q ∗ p �→ y ∗ q �→ z } block ( { x 0 �→ - ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ y ∗ q �→ z } x 0 := load ( load x 1 ) ; { x 0 �→ y ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ y ∗ q �→ z } load x 1 := load ( load x 2 ) ; { x 0 �→ y ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ z ∗ q �→ z } load x 2 := load x 0 ) { x 0 �→ p ∗ x 1 �→ q ∗ p �→ z ∗ q �→ y }

  38. Example { x 0 �→ p ∗ x 1 �→ q ∗ p �→ y ∗ q �→ z } block ( { x 0 �→ - ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ y ∗ q �→ z } x 0 := load ( load x 1 ) ; { x 0 �→ y ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ y ∗ q �→ z } load x 1 := load ( load x 2 ) ; { x 0 �→ y ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ z ∗ q �→ z } load x 2 := load x 0 { x 0 �→ y ∗ x 1 �→ p ∗ x 2 �→ q ∗ p �→ z ∗ q �→ y } ) { x 0 �→ p ∗ x 1 �→ q ∗ p �→ z ∗ q �→ y }

  39. Soundness of the axiomatic semantics ◮ Define ∆; J ; R � { P } s { Q } in terms of operational semantics

  40. Soundness of the axiomatic semantics ◮ Define ∆; J ; R � { P } s { Q } in terms of operational semantics ◮ Prove ∆; J ; R ⊢ { P } s { Q } implies ∆; J ; R � { P } s { Q }

  41. Soundness of the axiomatic semantics ◮ Define ∆; J ; R � { P } s { Q } in terms of operational semantics ◮ Prove ∆; J ; R ⊢ { P } s { Q } implies ∆; J ; R � { P } s { Q } ◮ Tricky definition of ∆; J ; R � { P } s { Q } because ◮ The frame rule ◮ Undefined behavior ◮ Non-local control ◮ Mutual recursion

  42. Formalization in Coq ◮ Extremely useful for debugging

  43. Formalization in Coq ◮ Extremely useful for debugging ◮ Notations close to those on paper

  44. Formalization in Coq ◮ Extremely useful for debugging ◮ Notations close to those on paper ◮ Also supports while and functions with return values

  45. Formalization in Coq ◮ Extremely useful for debugging ◮ Notations close to those on paper ◮ Also supports while and functions with return values ◮ Axiomatic semantics as derived lemmas instead of inference system

  46. Formalization in Coq ◮ Extremely useful for debugging ◮ Notations close to those on paper ◮ Also supports while and functions with return values ◮ Axiomatic semantics as derived lemmas instead of inference system ◮ Uses lots of automation

Recommend


More recommend