an operational and axiomatic semantics for non
play

An Operational and Axiomatic Semantics for Non-determinism and - PowerPoint PPT Presentation

An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C Robbert Krebbers Radboud University Nijmegen January 22, 2014 @ POPL, San Diego, USA 1 / 16 What is this program supposed to do? int main() { int x; int y =


  1. An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C Robbert Krebbers Radboud University Nijmegen January 22, 2014 @ POPL, San Diego, USA 1 / 16

  2. What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } 2 / 16

  3. What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } Let us try some compilers ◮ Clang prints 4 7 , seems just left-right 2 / 16

  4. What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } Let us try some compilers ◮ Clang prints 4 7 , seems just left-right ◮ GCC prints 4 8 , does not correspond to any evaluation order 2 / 16

  5. What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } Let us try some compilers ◮ Clang prints 4 7 , seems just left-right ◮ GCC prints 4 8 , does not correspond to any evaluation order This program violates the sequence point restriction ◮ due to two unsequenced writes to x ◮ resulting in undefined behavior ◮ thus both compilers are right 2 / 16

  6. Undefined behavior in C “Garbage in, garbage out” principle ◮ Programs with undefined behavior are not statically excluded ◮ Undefined behavior ⇒ all bets are off ◮ Allows compilers to omit (expensive) dynamic checks 3 / 16

  7. Undefined behavior in C “Garbage in, garbage out” principle ◮ Programs with undefined behavior are not statically excluded ◮ Undefined behavior ⇒ all bets are off ◮ Allows compilers to omit (expensive) dynamic checks A compiler independent C semantics should account for undefined behavior 3 / 16

  8. Examples ◮ Side-effects are useful in a while , for , return , . . . while ((x = getchar()) != EOF) /* do something */ 4 / 16

  9. Examples ◮ Side-effects are useful in a while , for , return , . . . while ((x = getchar()) != EOF) /* do something */ ◮ Non-determinism is subtle in innocent looking examples: *p = g (x, y, z); Here, g may change p , so the evaluation order matters 4 / 16

  10. Examples ◮ Side-effects are useful in a while , for , return , . . . while ((x = getchar()) != EOF) /* do something */ ◮ Non-determinism is subtle in innocent looking examples: *p = g (x, y, z); Here, g may change p , so the evaluation order matters ◮ Interleaving of subexpressions is possible, for example printf("a") + (printf("b") + printf("c")); may print “ bac ” 4 / 16

  11. Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: ◮ expressions with function calls, assignments, conditionals ◮ undefined behavior due to integer overflow ◮ parametrized by integer types ◮ dynamically allocated memory ( malloc and free ) ◮ non-local control ( return and goto ) ◮ local variables (and pointers to those) ◮ mutual recursion ◮ separation logic ◮ soundness proof fully checked by Coq 5 / 16

  12. Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: ◮ expressions with function calls, assignments, conditionals ◮ undefined behavior due to integer overflow ◮ parametrized by integer types ◮ dynamically allocated memory ( malloc and free ) ◮ non-local control ( return and goto ) ◮ local variables (and pointers to those) ◮ mutual recursion ◮ separation logic ◮ soundness proof fully checked by Coq 5 / 16

  13. Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: ◮ expressions with function calls, assignments, conditionals ◮ undefined behavior due to integer overflow ◮ parametrized by integer types ◮ dynamically allocated memory ( malloc and free ) ◮ non-local control ( return and goto ) ◮ local variables (and pointers to those) ◮ mutual recursion ◮ separation logic ◮ soundness proof fully checked by Coq 5 / 16

  14. Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: ◮ expressions with function calls, assignments, conditionals ◮ undefined behavior due to integer overflow ◮ parametrized by integer types ◮ dynamically allocated memory ( malloc and free ) ◮ non-local control ( return and goto ) ◮ local variables (and pointers to those) ◮ mutual recursion ◮ separation logic ◮ soundness proof fully checked by Coq 5 / 16

  15. Key idea Observation : non-determinism corresponds to concurrency Idea : use the separation logic rule for parallel composition { P 1 } e 1 { Q 1 } { P 2 } e 2 { Q 2 } { P 1 ∗ P 2 } e 1 ⊚ e 2 { Q 1 ∗ Q 2 } 6 / 16

  16. Key idea Observation : non-determinism corresponds to concurrency Idea : use the separation logic rule for parallel composition { P 1 } e 1 { Q 1 } { P 2 } e 2 { Q 2 } { P 1 ∗ P 2 } e 1 ⊚ e 2 { Q 1 ∗ Q 2 } What does this mean: ◮ Split the memory into two disjoint parts ◮ Prove that e 1 and e 2 can be executed safely in their part ◮ Now e 1 ⊚ e 2 can be executed safely in the whole memory 6 / 16

  17. Key idea Observation : non-determinism corresponds to concurrency Idea : use the separation logic rule for parallel composition { P 1 } e 1 { Q 1 } { P 2 } e 2 { Q 2 } { P 1 ∗ P 2 } e 1 ⊚ e 2 { Q 1 ∗ Q 2 } What does this mean: ◮ Split the memory into two disjoint parts ◮ Prove that e 1 and e 2 can be executed safely in their part ◮ Now e 1 ⊚ e 2 can be executed safely in the whole memory Disjointness ⇒ no sequence point violation 6 / 16

  18. Definition of the memory Given a set of permissions P , we define: m ∈ mem := index → fin (val × P ) b ∈ index := N v ∈ val ::= indet | int τ n | ptr b | NULL Integer types: unsigned char, signed int, . . . 7 / 16

  19. Permission systems Actual permissions contains: ◮ Locked flag to catch sequence point violations ◮ Assignment: lock memory location ◮ Sequence point: unlock memory locations ◮ A fraction (0 , 1] Q to allow sharing ◮ Block scope variable or allocated with malloc flag 8 / 16

  20. Permission systems Actual permissions contains: ◮ Locked flag to catch sequence point violations ◮ Assignment: lock memory location ◮ Sequence point: unlock memory locations ◮ A fraction (0 , 1] Q to allow sharing ◮ Block scope variable or allocated with malloc flag A permission system P abstracts from these details. ◮ ∪ , \ : P → P → P ◮ ⊥ , ⊆ : P → P → Prop ◮ kind : P → { Free , Write , Read , Locked } ◮ lock , unlock : P → P ◮ satisfying certain axioms 8 / 16

  21. Permission systems Actual permissions contains: ◮ Locked flag to catch sequence point violations ◮ Assignment: lock memory location ◮ Sequence point: unlock memory locations ◮ A fraction (0 , 1] Q to allow sharing ◮ Block scope variable or allocated with malloc flag A permission system P abstracts from these details. ◮ ∪ , \ : P → P → P ◮ ⊥ , ⊆ : P → P → Prop ◮ kind : P → { Free , Write , Read , Locked } ◮ lock , unlock : P → P ◮ satisfying certain axioms 8 / 16

  22. Permission systems Actual permissions contains: ◮ Locked flag to catch sequence point violations ◮ Assignment: lock memory location ◮ Sequence point: unlock memory locations ◮ A fraction (0 , 1] Q to allow sharing ◮ Block scope variable or allocated with malloc flag A permission system P abstracts from these details. ◮ ∪ , \ : P → P → P ◮ ⊥ , ⊆ : P → P → Prop ◮ kind : P → { Free , Write , Read , Locked } ◮ lock , unlock : P → P ◮ satisfying certain axioms 8 / 16

  23. Permission systems Actual permissions contains: ◮ Locked flag to catch sequence point violations ◮ Assignment: lock memory location ◮ Sequence point: unlock memory locations ◮ A fraction (0 , 1] Q to allow sharing ◮ Block scope variable or allocated with malloc flag A permission system P abstracts from these details. ◮ ∪ , \ : P → P → P ◮ ⊥ , ⊆ : P → P → Prop ◮ kind : P → { Free , Write , Read , Locked } ◮ lock , unlock : P → P ◮ satisfying certain axioms We lift these operations to memories index → fin (val × P ) 8 / 16

  24. The language Our language ⊚ ∈ binop ::= == | <= | + | - | * | / | % | . . . e ∈ expr ::= x i | [ v ] Ω | e 1 := e 2 | f ( � e ) | load e | alloc | free e | e 1 ⊚ e 2 | e 1 ? e 2 : e 3 | ( τ ) e s ∈ stmt ::= e | skip | goto l | return e | block c s | s 1 ; s 2 | l : s | while ( e ) s | if ( e ) s 1 else s 2 9 / 16

  25. The language Our language ⊚ ∈ binop ::= == | <= | + | - | * | / | % | . . . e ∈ expr ::= x i | [ v ] Ω | e 1 := e 2 | f ( � e ) | load e | alloc | free e | e 1 ⊚ e 2 | e 1 ? e 2 : e 3 | ( τ ) e s ∈ stmt ::= e | skip | goto l | return e | block c s | s 1 ; s 2 | l : s | while ( e ) s | if ( e ) s 1 else s 2 Values [ v ] Ω carry a set Ω of indexes (memory locations) to be unlocked at the next sequence point 9 / 16

  26. Operational semantics Head reduction for expressions ( e , m ) � h ( e ′ , m ′ ) (9 rules) 10 / 16

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