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 March 19, 2013 @ FoSSaCS, Rome, Italy What is this program supposed to do? int *p = NULL; l: if


  1. Separation Logic for Non-local Control Flow and Block Scope Variables Robbert Krebbers Joint work with Freek Wiedijk Radboud University Nijmegen March 19, 2013 @ FoSSaCS, Rome, Italy

  2. What is this program supposed to do? int *p = NULL; l: if (p) { return (*p); } else { int j = 10; p = &j; goto l; }

  3. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p return (*p); } else { NULL int j = 10; p = &j; goto l; }

  4. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p return (*p); } else { NULL int j = 10; p = &j; goto l; }

  5. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p j return (*p); } else { NULL 10 int j = 10; p = &j; goto l; }

  6. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p j return (*p); } else { • 10 int j = 10; p = &j; goto l; }

  7. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p j return (*p); } else { • 10 int j = 10; p = &j; goto l; }

  8. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p return (*p); } else { • int j = 10; p = &j; goto l; }

  9. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p return (*p); } else { • int j = 10; p = &j; goto l; }

  10. What is this program supposed to do? int *p = NULL; memory: l: if (p) { p return (*p); } else { • int j = 10; p = &j; goto l; } It exhibits undefined behavior, thus it may do anything

  11. Undefined behavior in C ◮ Undefined behavior is shown by “wrong” C programs ◮ Programs may do anything on undefined behavior ◮ It allows compilers to omit (expensive) dynamic checks

  12. Undefined behavior in C ◮ Undefined behavior is shown by “wrong” C programs ◮ Programs may do anything on undefined behavior ◮ It allows compilers to omit (expensive) dynamic checks ◮ It cannot be checked for statically ◮ Not accounting for it means that ◮ programs can be proven to be correct with respect to the formal semantics . . . ◮ whereas they may crash when compiled with an actual compiler

  13. Undefined behavior in C ◮ Undefined behavior is shown by “wrong” C programs ◮ Programs may do anything on undefined behavior ◮ It allows compilers to omit (expensive) dynamic checks ◮ It cannot be checked for statically ◮ Not accounting for it means that ◮ programs can be proven to be correct with respect to the formal semantics . . . ◮ whereas they may crash when compiled with an actual compiler This talk: undefined behavior due to dangling pointers by non-local control flow and block scopes

  14. Goto considered harmful? http://xkcd.com/292/

  15. Goto considered harmful? http://xkcd.com/292/ Not necessarily: ⊢ { P } . . . goto main_sub3; . . . { Q }

  16. Contribution A concise small step operational, and axiomatic, semantics for goto, supporting: ◮ local variables (and pointers to those), ◮ mutual recursion, ◮ separation logic, ◮ soundness proof fully checked by Coq

  17. Approach ◮ Execute gotos and returns in small steps ◮ Not so much to search for labels, . . . ◮ but to naturally perform required allocations and deallocations

  18. Approach ◮ Execute gotos and returns in small steps ◮ Not so much to search for labels, . . . ◮ but to naturally perform required allocations and deallocations ◮ Traversal through the AST in the following directions: ◮ ց downwards to the next statement ◮ ր upwards to the next statement

  19. Approach ◮ Execute gotos and returns in small steps ◮ Not so much to search for labels, . . . ◮ but to naturally perform required allocations and deallocations ◮ Traversal through the AST in the following directions: ◮ ց downwards to the next statement ◮ ր upwards to the next statement ◮ � l to a label l: after a goto l ↑ to the top of the statement after a return ◮ ↑

  20. Example int *p = NULL l: if (p) return (*p) int j = 10 ; p = &j goto l

  21. Example int *p = NULL direction: ց l: memory: if (p) p return (*p) int j = 10 NULL ; p = &j goto l

  22. Example int *p = NULL direction: ց l: memory: if (p) p return (*p) int j = 10 NULL ; p = &j goto l

  23. Example int *p = NULL direction: ց l: memory: if (p) p return (*p) int j = 10 NULL ; p = &j goto l

  24. Example int *p = NULL direction: ց l: memory: if (p) p j return (*p) int j = 10 NULL 10 ; p = &j goto l

  25. Example int *p = NULL direction: ց l: memory: if (p) p j return (*p) int j = 10 NULL 10 ; p = &j goto l

  26. Example int *p = NULL direction: ց l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  27. Example int *p = NULL direction: ր l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  28. Example int *p = NULL direction: ր l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  29. Example int *p = NULL direction: ց l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  30. Example int *p = NULL direction: ց l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  31. Example int *p = NULL direction: � l l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  32. Example int *p = NULL direction: � l l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  33. Example int *p = NULL direction: � l l: memory: if (p) p j return (*p) int j = 10 • 10 ; p = &j goto l

  34. Example int *p = NULL direction: � l l: memory: if (p) p return (*p) int j = 10 • ; p = &j goto l

  35. Example int *p = NULL direction: � l l: memory: if (p) p return (*p) int j = 10 • ; p = &j goto l

  36. Example int *p = NULL direction: ց l: memory: if (p) p return (*p) int j = 10 • ; p = &j goto l

  37. Example int *p = NULL direction: ց l: memory: if (p) p return (*p) int j = 10 • ; p = &j goto l

  38. Example int *p = NULL direction: ց l: memory: if (p) p return (*p) int j = 10 • ; p = &j goto l

  39. How to model the current location in the program Huet’s zipper Purely functional way to store a pointer into a data structure

  40. Statement contexts ◮ Statements: s ::= block s | e l := e r | f ( � e ) | skip | goto l | l : s | s 1 ; s 2 | if ( e ) s 1 s 2 | return

  41. Statement contexts ◮ Statements: s ::= block s | e l := e r | f ( � e ) | skip | goto l | l : s | s 1 ; s 2 | if ( e ) s 1 s 2 | return ◮ The block construct is unnamed as we use De Bruijn indexes

  42. Statement contexts ◮ Statements: s ::= block s | e l := e r | f ( � e ) | skip | goto l | l : s | s 1 ; s 2 | if ( e ) s 1 s 2 | return ◮ The block construct is unnamed as we use De Bruijn indexes ◮ Singular statement contexts: E S ::= � ; s 2 | s 1 ; � | if ( e ) � s 2 | if ( e ) s 1 � | l : �

  43. Statement contexts ◮ Statements: s ::= block s | e l := e r | f ( � e ) | skip | goto l | l : s | s 1 ; s 2 | if ( e ) s 1 s 2 | return ◮ The block construct is unnamed as we use De Bruijn indexes ◮ Singular statement contexts: E S ::= � ; s 2 | s 1 ; � | if ( e ) � s 2 | if ( e ) s 1 � | l : � ◮ A pair ( � E S , s ) forms a zipper for statements, where � � E S is a statement turned ◮ E S inside-out s ◮ s is the focused substatement

  44. Program contexts ◮ Make the zipper stateful to also contain the stack (to assign memory indexes to local variables) ◮ Extend the zipper dynamically on function calls

  45. Program contexts ◮ Make the zipper stateful to also contain the stack (to assign memory indexes to local variables) ◮ Extend the zipper dynamically on function calls ◮ Program contexts k are lists of singular program contexts: E ::= E S | block b � | . . . where block b � associates a block scope variable with its corresponding memory index b

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

  47. 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

  48. 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 )

  49. 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

  50. 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 }

Recommend


More recommend