hoare logic and model checking
play

Hoare Logic and Model Checking In the previous lecture we saw the - PowerPoint PPT Presentation

Introduction Hoare Logic and Model Checking In the previous lecture we saw the informal concepts that Separation Logic is based on. This lecture will Kasper Svendsen University of Cambridge introduce a formal proof system for Separation


  1. Introduction Hoare Logic and Model Checking In the previous lecture we saw the informal concepts that Separation Logic is based on. This lecture will Kasper Svendsen University of Cambridge • introduce a formal proof system for Separation logic CST Part II – 2016/17 • present examples to illustrate the power of Separation logic Acknowledgement: slides heavily based on previous versions by Mike Gordon and Alan Mycroft The lecture will be focused on partial correctness. 1 Separation Logic Separation logic inherits all the partial correctness rules from Hoare logic that you have already seen and extends them with A proof system for Separation logic • the frame rule • rules for each new heap-primitive Some of the derived rules for plain Hoare logic no longer hold for separation logic (e.g., the rule of constancy). 2

  2. The frame rule The heap assignment rule The frame rule expresses that Separation logic triples always Separation logic triples must assert ownership of any heap-cells preserve any resources disjoint from the precondition. modified by the command. The heap assignment axiom thus asserts ownership of the heap location being assigned. ` { P } C { Q } mod ( C ) \ FV ( R ) = ; ` { P ⇤ R } C { Q ⇤ R } ` { E 1 7! } [ E 1 ] := E 2 { E 1 7! E 2 } The second hypothesis ensures that the frame R does not refer to Here we use E 1 7! as shorthand for 9 v . E 1 7! v . any program variables modified by the command C . 3 4 The heap dereference rule Separation logic The assignment rule introduces a new points-to assertion for each newly allocated location: Separation logic triples must ensure the command does not fault. The heap dereference rule thus asserts ownership of the given heap location to ensure the location is allocated in the heap. ` { X = x } X := cons ( E 1 , ..., E n ) { X 7! E 1 [ x / X ] , ..., E n [ x / X ] } ` { E 7! v ^ X = x } X := [ E ] { E [ x / X ] 7! v ^ X = v } The deallocation rule destroys the points-to assertion for the location to be deallocated: Here the auxiliary variable x is used to refer to the initial value of X in the postcondition. ` { E 7! } dispose ( E ) { emp } 5 6

  3. Swap example Swap example Below is a proof-outline of the main steps: { X 7! v 1 ⇤ Y 7! v 2 } A := [ X ]; To illustrate these rules, consider the following code-snippet: { X 7! v 1 ⇤ Y 7! v 2 ^ A = v 1 } C swap ⌘ A := [ X ]; B := [ Y ]; [ X ] := B ; [ Y ] := A ; B := [ Y ]; { X 7! v 1 ⇤ Y 7! v 2 ^ A = v 1 ^ B = v 2 } We want to show that it swaps the values in the locations referenced by X and Y , when X and Y do not alias: [ X ] := B ; { X 7! B ⇤ Y 7! v 2 ^ A = v 1 ^ B = v 2 } { X 7! v 1 ⇤ Y 7! v 2 } C swap { X 7! v 2 ⇤ Y 7! v 1 } [ Y ] := A ; { X 7! B ⇤ Y 7! A ^ A = v 1 ^ B = v 2 } { X 7! v 2 ⇤ Y 7! v 1 } 7 8 Swap example Swap example To prove this first triple, we use the heap-dereference rule to derive: { X 7! v 1 ^ A = a } A := [ X ] { X [ a / A ] 7! v 1 ^ A = v 1 } Then we existentially quantify the auxiliary variable a : { 9 a . X 7! v 1 ^ A = a } A := [ X ] { 9 a . X [ a / A ] 7! v 1 ^ A = v 1 } For the last application of consequence, we need to show that: Applying the rule-of-consequence we obtain: ` ( X 7! v 1 ^ A = v 1 ) ⇤ Y 7! v 2 ) X 7! v 1 ⇤ Y 7! v 2 ^ A = v 1 { X 7! v 1 } A := [ X ] { X 7! v 1 ^ A = v 1 } To prove this we need proof rules for the new separation logic Since A := [ X ] does not modify Y we can frame on Y 7! v 2 : primitives. { X 7! v 1 ⇤ Y 7! v 2 } A := [ X ] { ( X 7! v 1 ^ A = v 1 ) ⇤ Y 7! v 2 } Lastly, by the rule-of-consequence we obtain: { X 7! v 1 ⇤ Y 7! v 2 } A := [ X ] { X 7! v 1 ⇤ Y 7! v 2 ^ A = v 1 } 9 10

  4. Separation logic assertions Separation logic assertions Separation conjunction is commutative and associative operator Separating conjunction distributes over disjunction and with emp as a neutral element: semi-distributes over conjunction: ` P ⇤ Q , Q ⇤ P ` ( P _ Q ) ⇤ R , ( P ⇤ R ) _ ( Q ⇤ R ) ` ( P ⇤ Q ) ⇤ R , P ⇤ ( Q ⇤ R ) ` ( P ^ Q ) ⇤ R ) ( P ⇤ R ) ^ ( Q ⇤ R ) ` P ⇤ emp , P Taking R ⌘ X 7! 1 _ Y 7! 1, P ⌘ X 7! 1 and Q ⌘ X 7! 1 yields a Separation conjunction is monotone with respect to implication: counterexample to distributivity over conjunction in the other direction: ` P 1 ) Q 1 ` P 2 ) Q 2 6 | = ( P ⇤ R ) ^ ( Q ⇤ R ) ) ( P ^ Q ) ⇤ R ` P 1 ⇤ P 2 ) Q 1 ⇤ Q 2 11 12 Separation logic assertions An assertion is pure if it does not contain emp , 7! or , ! . Separation conjunction and conjunction collapses for pure Verifying abstract data types assertions: ` P ^ Q ) P ⇤ Q when P or Q is pure ` P ⇤ Q ) P ^ Q when P and Q are pure ` ( P ^ Q ) ⇤ R , P ^ ( Q ⇤ R ) when P is pure 13

  5. Verifying ADTs A linked list library First we need to define a memory representation for our linked lists. Separation Logic is very well-suited for specifying and reasoning We will use a singly-linked list, starting from some designated head about data structures typically found in standard libraries such as variable that refers to the first element of the list and terminating lists, queues, stacks, etc. with a null -pointer. To illustrate we will specify and verify a library for working with For instance, we will represent a list containing the values 12 , 99 linked lists in Separation Logic. and 37 as follows head 12 99 37 14 15 Representation predicates Representation predicates We are going to define the list ( head , ↵ ) predicate by induction on the list ↵ . We need to consider two cases: the empty list and an To formalise the memory representation, Separation Logic uses element x appended to a list � . representation predicates that relate an abstract description of the state of the data structure with its memory representations. An empty list is represented as a null -pointer def For our example, we want a predicate list ( head , ↵ ) that relates a list ( head , []) = head = null mathematical list, ↵ , with its memory representation. The list x :: � is represented by a reference to two consecutive To define such a predicate formally, we need to extend the heap-cells that contain the value x and a representation of the rest assertion logic to reason about mathematical lists, support for of the list, respectively predicates and inductive definitions. We will elide these details. def = 9 y . head 7! x ⇤ ( head + 1) 7! y ⇤ list ( y , � ) list ( head , x :: � ) 16 17

  6. Representation predicates Representation predicates The representation predicate allows us to specify the behaviour of the list operations by their e ff ect on the abstract state of the list We can specify all the operations of the library in a similar manner Imagine C push is an implementation of an push operation that { emp } C new { list ( HEAD , []) } pushes the value stored in variable X to the front of the list { list ( HEAD , ↵ ) ^ X = x } C push { list ( HEAD , x :: ↵ ) } referenced by variable HEAD and stores a reference to the new list in HEAD { list ( HEAD , x :: ↵ ) } { list ( HEAD , ↵ ) ^ RET = x } C pop { list ( HEAD , []) } { list ( HEAD , []) ^ RET = null } C pop We can specify this operation in terms of its behaviour on the { list ( HEAD , ↵ ) } { emp } abstract state of the list as follows C delete { list ( HEAD , ↵ ) ^ X = x } C add { list ( HEAD , x :: ↵ ) } 18 19 Implementation of push Proof outline for push Here is a proof outline for the push operation. The push operation stores the HEAD pointer pointer into a { list ( HEAD , ↵ ) ^ X = x } temporary variable Y before allocating two consecutive heap-cells Y := HEAD for the new list element and updating HEAP : { list ( Y , ↵ ) ^ X = x } C push ⌘ Y := HEAD ; HEAD := cons ( X , Y ) HEAD := cons ( X , Y ) { list ( Y , ↵ ) ⇤ HEAD 7! X , Y ^ X = x } We wish to prove it satisfies the following specification: { list ( HEAD , X :: ↵ ) ^ X = x } { list ( HEAD , ↵ ) ^ X = x } C push { list ( HEAD , x :: ↵ ) } { list ( HEAD , x :: ↵ ) } For the cons step we frame o ff list ( Y , ↵ ) ^ X = x . 20 21

  7. Implementation of delete Proof outline for delete The delete operation iterates down over the list, deallocating { list ( HEAD , ↵ ) } nodes until it reaches the end of the list. X := HEAD ; { list ( X , ↵ ) } C delete ⌘ X := HEAD ; { 9 ↵ . list ( X , ↵ ) } while X 6 = NULL do while X 6 = NULL do Y := [ X + 1]; dispose ( X ); dispose ( X + 1); X := Y { 9 ↵ . list ( X , ↵ ) ^ X 6 = NULL } To prove that delete satisfies its intended specification, ( Y := [ X + 1]; dispose ( X ); dispose ( X + 1); X := Y ) { list ( HEAD , ↵ ) } C delete { emp } { 9 ↵ . list ( X , ↵ ) } { list ( X , ↵ ) ^ ¬ ( X 6 = NULL ) } we need a suitable invariant: that we own the rest of the list. { emp } 22 23 Proof outline for loop-body of delete To verify the loop-body we need a lemma to unfold the list representation predicate in the non-null case: { 9 ↵ . list ( X , ↵ ) ^ X 6 = NULL } { 9 v , t , ↵ . X 7! v , t ⇤ list ( t , ↵ ) } Concurrency (not examinable) Y := [ X + 1]; { 9 v , ↵ . X 7! v , Y ⇤ list ( Y , ↵ ) } dispose ( X ); dispose ( X + 1); { 9 ↵ . list ( Y , ↵ ) } X := Y { 9 ↵ . list ( X , ↵ ) } 24

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