hoare logic and model checking
play

Hoare Logic and Model Checking Kasper Svendsen University of - PowerPoint PPT Presentation

Hoare Logic and Model Checking Kasper Svendsen University of Cambridge CST Part II 2016/17 Acknowledgement: slides heavily based on previous versions by Mike Gordon and Alan Mycroft Pointers Pointers and state So far, we have been


  1. Hoare Logic and Model Checking Kasper Svendsen University of Cambridge CST Part II – 2016/17 Acknowledgement: slides heavily based on previous versions by Mike Gordon and Alan Mycroft

  2. Pointers

  3. Pointers and state So far, we have been reasoning about a language without pointers, where all values were numbers. In this lecture we will extend the WHILE language with pointers and introduce an extension of Hoare logic, called Separation Logic, to simplify reasoning about pointers. 1

  4. Pointers and state E ::= N | null | V | E 1 + E 2 | expressions | E 1 − E 2 | E 1 × E 2 | · · · B ::= T | F | E 1 = E 2 boolean expressions | E 1 ≤ E 2 | E 1 ≥ E 2 | · · · skip | C 1 ; C 2 | V := E C ::= commands | if B then C 1 else C 2 | while B do C | V := [ E ] | [ E 1 ] := E 2 | V := cons ( E 1 , ..., E n ) | dispose ( E ) 2

  5. Pointers and state Commands are now evaluated with respect to a heap h that stores the current value of allocated locations. Reading, writing and disposing of pointers fails if the given location is not currently allocated. Fetch assignment command: V := [ E ] • evaluates E to a location l and assigns the current value of l to V ; faults if l is not currently allocated 3

  6. Pointers and state Heap assignment command: [ E 1 ] := E 2 • evaluates E 1 to a location l and E 2 to a value v and updates the heap to map l to v ; faults if l is not currently allocated Pointer disposal command, dispose ( E ) • evaluates E to a location l and deallocates location l from the heap; faults if l is not currently allocated 4

  7. Pointers and state Allocation assignment command: V := cons ( E 1 , ..., E n ) • chooses n consecutive unallocated locations, say l 1 , ..., l n , evaluates E 1 , ..., E n to values v 1 , ..., v n , updates the heap to map l i to v i for each i and assigns l 1 to V Allocation never fails. The language supports pointer arithmetic: e.g., X := cons (0 , 1); Y := [ X + 1] 5

  8. Pointers and state In this extended language we can work with proper data structures, like the following singly-linked list. head 12 99 37 For instance, this operation deletes the first element of the list: x := [ head + 1]; // lookup address of second element dispose ( head ); // deallocate first element dispose ( head + 1); head := x // swing head to point to second element 6

  9. Operational semantics

  10. Pointers and state For the WHILE language we modelled the state as a function assigning values (numbers) to all variables: def s ∈ State = Var → Val To model pointers we will split the state into a stack and a heap • a stack assigns values to program variables, and • a heap maps locations to values def State = Store × Heap 7

  11. Pointers and state Values now includes both numbers and locations def Val = Z + Loc Locations are modelled as natural numbers def Loc = N To model allocation, we model the heap as a finite function = Loc fin def def = Var → Val → Val Store Heap 8

  12. Pointers and state WHILE p programs can fail in several ways • dereferencing an invalid pointer • invalid pointer arithmetic To model failure we introduce a distinguished failure value � E [ [ − ] ] : Exp × Store → { � } + Val B [ [ − ] ] : BExp × Store → { � } + B ⇓ : P ( Cmd × State × ( { � } ∪ State )) 9

  13. Pointer dereference E [ [ E ] ]( s ) = l l ∈ dom ( h ) � V := [ E ] , ( s , h ) � ⇓ ( s [ V �→ h ( l )] , h ) E [ [ E ] ]( s ) = l l �∈ dom ( h ) � V := [ E ] , ( s , h ) � ⇓ � 10

  14. Pointer assignment E [ [ E 1 ] ]( s ) = l E [ [ E 2 ] ]( s ) = v l ∈ dom ( h ) v � = � � [ E 1 ] := E 2 , ( s , h ) � ⇓ ( s , h [ l �→ v ]) E [ [ E 1 ] ]( s ) = l l �∈ dom ( h ) E [ [ E 2 ] ]( s ) = � � [ E 1 ] := E 2 , ( s , h ) � ⇓ � � [ E 1 ] := E 2 , ( s , h ) � ⇓ � 11

  15. Reasoning about pointers

  16. Reasoning about pointers In standard Hoare logic we can syntactically approximate the set of program variables that might be affected by a command C . mod ( skip ) = ∅ mod ( X := E ) = { X } mod ( C 1 ; C 2 ) = mod ( C 1 ) ∪ mod ( C 2 ) mod ( if B then C 1 else C 2 ) = mod ( C 1 ) ∪ mod ( C 2 ) mod ( while B do C ) = mod ( C ) 12

  17. The rule of constancy The rule of constancy expresses that assertions that do not refer to variables modified by a command are automatically preserved during its execution. ⊢ { P } C { Q } mod ( C ) ∩ FV ( R ) = ∅ ⊢ { P ∧ R } C { Q ∧ R } This rule derivable in standard Hoare logic. This rule is important for modularity as it allows us to only mention the part of the state that we access. 13

  18. Reasoning about pointers Imagine we extended Hoare logic with a new assertion, E 1 ֒ → E 2 , for asserting that location E 1 currently contains the value E 2 and extend the proof system with the following axiom: ⊢ {⊤} [ E 1 ] := E 2 { E 1 ֒ → E 2 } Then we loose the rule of constancy: ⊢ {⊤} [ X ] := 1 { X ֒ → 1 } ⊢ {⊤ ∧ Y ֒ → 0 } [ X ] := 1 { X ֒ → 1 ∧ Y ֒ → 0 } (the post-condition is false if X and Y refer to the same location.) 14

  19. Reasoning about pointers In the presence of pointers, syntactically distinct variables can refer to the same location. Updates made through one variable can thus influence the state referenced by other variables. This complicates reasoning as we explicitly have to track inequality of pointers to reason about updates: ⊢ { E 1 � = E 3 ∧ E 3 ֒ → E 4 } [ E 1 ] := E 2 { E 1 ֒ → E 2 ∧ E 3 ֒ → E 4 } 15

  20. Separation logic

  21. Separation logic Separation logic is an extension of Hoare logic that simplifies reasoning about mutable state using new connectives to control aliasing. Separation logic was proposed by John Reynolds in 2000 and developed further by Peter O’Hearn and Hongsek Yang around 2001. It is still a very active area of research. 16

  22. Separation logic Separation logic introduces two new concepts for reasoning about mutable state:: • ownership : Separation logic assertions do not just describe properties of the current state, they also assert ownership of part of the heap. • separation : Separation logic introduces a new connective, written P ∗ Q , for asserting that the part of the heap owned by P and Q are disjoint . This makes it easy to describe data structures without sharing. 17

  23. Separation logic Separation logic introduces a new assertion, written E 1 �→ E 2 , for reasoning about individual heap cells. The points-to assertion, E 1 �→ E 2 , asserts • that the current value of heap location E 1 is E 2 , and • asserts ownership of heap location E 1 . 18

  24. Meaning of separation logic assertions The semantics of a separation logic assertion, written [ [ P ] ]( s ), is a set of heaps that satisfy the assertion P . The intended meaning is that if h ∈ [ [ P ] ]( s ) then P asserts ownership of any locations in dom ( h ). The heaps h ∈ [ [ P ] ]( s ) are thus referred to as partial heaps , since they only contain the locations owned by P . The empty heap assertion, only holds for the empty heap: def [ [ emp ] ]( s ) = { [] } 19

  25. Meaning of separation logic assertions The points-to assertion, E 1 �→ E 2 , asserts ownership of the location referenced by E 1 and that this location currently contains E 2 : def [ E 1 �→ E 2 ] = { h | dom ( h ) = {E [ ]( s ) } [ ]( s ) [ E 1 ] ∧ h ( E [ [ E 1 ] ]( s )) = E [ [ E 2 ] ]( s ) } Separating conjunction, P ∗ Q , asserts that the heap can be split into two distjoint parts such that one satisfies P and the other Q : def [ [ P ∗ Q ] ]( s ) = { h | ∃ h 1 , h 2 . h = h 1 ⊎ h 2 ∧ h 1 ∈ [ [ P ] ]( s ) ∧ h 2 ∈ [ [ Q ] ]( s ) } Here we use h 1 ⊎ h 2 as shorthand for h 1 ∪ h 2 where h 1 ⊎ h 2 is only defined when dom ( h 1 ) ∩ dom ( h 2 ) = ∅ . 20

  26. Examples of separation logic assertions 1. X �→ E 1 ∗ Y �→ E 2 This assertion is unsatisfiable in a state where X and Y refer to the same location, since X �→ E 1 and Y �→ E 2 would both assert ownership of the same location. The following heap satisfies the assertion: E 1 E 2 X Y 2. X �→ E ∗ X �→ E This assertion is not satisfiable. 21

  27. Meaning of separation logic assertions The first-order primitives are interpreted much like for Hoare logic: def [ [ ⊥ ] ]( s ) = ∅ def [ [ ⊤ ] ]( s ) = Heap def [ [ P ∧ Q ] ]( s ) = [ [ P ] ]( s ) ∩ [ [ Q ] ]( s ) def [ [ P ∨ Q ] ]( s ) = [ [ P ] ]( s ) ∪ [ [ Q ] ]( s ) def [ [ P ⇒ Q ] ]( s ) = { h | h ∈ [ [ P ] ]( s ) ⇒ h ∈ [ [ Q ] ]( s ) } . . . 22

  28. Examples of separation logic assertions 3. X �→ E 1 ∧ Y �→ E 2 This asserts that X and Y alias each other and E 1 = E 2 : E 1 X Y 23

  29. Examples of separation logic assertions 4. X �→ Y ∗ Y �→ X X Y 5. X �→ E 1 , Y ∗ Y �→ E 2 , null E 1 E 2 X Here X �→ E 1 , ..., E n is shorthand for X �→ E 1 ∗ ( X + 1) �→ E 2 ∗ · · · ∗ ( X + n − 1) �→ E n 24

  30. Summary: Separation logic assertions Separation logic assertions describe properties of the current state and assert ownership of parts of the current heap. Separation logic controls aliasing of pointers by asserting that assertions own disjoint heap parts. 25

  31. Separation logic triples

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend