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

an operational and axiomatic semantics for non
SMART_READER_LITE
LIVE PREVIEW

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 =


slide-1
SLIDE 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

slide-2
SLIDE 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

slide-3
SLIDE 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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 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

slide-7
SLIDE 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

slide-8
SLIDE 8

Examples

◮ Side-effects are useful in a while, for, return, . . .

while ((x = getchar()) != EOF) /* do something */

4 / 16

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 15

Key idea

Observation: non-determinism corresponds to concurrency Idea: use the separation logic rule for parallel composition {P1} e1 {Q1} {P2} e2 {Q2} {P1 ∗ P2} e1 ⊚ e2 {Q1 ∗ Q2}

6 / 16

slide-16
SLIDE 16

Key idea

Observation: non-determinism corresponds to concurrency Idea: use the separation logic rule for parallel composition {P1} e1 {Q1} {P2} e2 {Q2} {P1 ∗ P2} e1 ⊚ e2 {Q1 ∗ Q2} What does this mean:

◮ Split the memory into two disjoint parts ◮ Prove that e1 and e2 can be executed safely in their part ◮ Now e1 ⊚ e2 can be executed safely in the whole memory

6 / 16

slide-17
SLIDE 17

Key idea

Observation: non-determinism corresponds to concurrency Idea: use the separation logic rule for parallel composition {P1} e1 {Q1} {P2} e2 {Q2} {P1 ∗ P2} e1 ⊚ e2 {Q1 ∗ Q2} What does this mean:

◮ Split the memory into two disjoint parts ◮ Prove that e1 and e2 can be executed safely in their part ◮ Now e1 ⊚ e2 can be executed safely in the whole memory

Disjointness ⇒ no sequence point violation

6 / 16

slide-18
SLIDE 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

slide-19
SLIDE 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

slide-20
SLIDE 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

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 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

slide-24
SLIDE 24

The language

Our language ⊚ ∈ binop ::= == | <= | + | - | * | / | % | . . . e ∈ expr ::= xi | [v]Ω | e1 := e2 | f ( e) | load e | alloc | free e | e1 ⊚ e2 | e1 ? e2 : e3 | (τ) e s ∈ stmt ::= e | skip | goto l | return e | block c s | s1 ; s2 | l : s | while(e) s | if (e) s1 else s2

9 / 16

slide-25
SLIDE 25

The language

Our language ⊚ ∈ binop ::= == | <= | + | - | * | / | % | . . . e ∈ expr ::= xi | [v]Ω | e1 := e2 | f ( e) | load e | alloc | free e | e1 ⊚ e2 | e1 ? e2 : e3 | (τ) e s ∈ stmt ::= e | skip | goto l | return e | block c s | s1 ; s2 | l : s | while(e) s | if (e) s1 else s2 Values [v]Ω carry a set Ω of indexes (memory locations) to be unlocked at the next sequence point

9 / 16

slide-26
SLIDE 26

Operational semantics

Head reduction for expressions (e, m)h (e′, m′) (9 rules)

10 / 16

slide-27
SLIDE 27

Operational semantics

Head reduction for expressions (e, m)h (e′, m′) (9 rules)

◮ On assignments: locked index b added to Ω1 ∪ Ω2

([ptr b]Ω1 :=[v]Ω2, m)h ([v]{b}∪Ω1∪Ω2, lock b (m[b:=v]))

◮ On sequence points: Ω unlocked in memory

([v]Ω ? e2 : e3, m)h (e2, unlock Ω m) provided . . .

10 / 16

slide-28
SLIDE 28

Operational semantics

Head reduction for expressions (e, m)h (e′, m′) (9 rules)

◮ On assignments: locked index b added to Ω1 ∪ Ω2

([ptr b]Ω1 :=[v]Ω2, m)h ([v]{b}∪Ω1∪Ω2, lock b (m[b:=v]))

◮ On sequence points: Ω unlocked in memory

([v]Ω ? e2 : e3, m)h (e2, unlock Ω m) provided . . . Gives a local treatment of sequence points

10 / 16

slide-29
SLIDE 29

Operational semantics

Head reduction for expressions (e, m)h (e′, m′) (9 rules)

◮ On assignments: locked index b added to Ω1 ∪ Ω2

([ptr b]Ω1 :=[v]Ω2, m)h ([v]{b}∪Ω1∪Ω2, lock b (m[b:=v]))

◮ On sequence points: Ω unlocked in memory

([v]Ω ? e2 : e3, m)h (e2, unlock Ω m) provided . . . Gives a local treatment of sequence points Small step reduction S(k, φ, m) S(k′, φ′, m′) (33 rules)

◮ (k, φ) gives the position in the whole program ◮ Uses evaluation contexts to lift head reduction ◮ Different φs for expressions, statements, function calls

10 / 16

slide-30
SLIDE 30

Assertions of separation logic

Defined using a shallow embedding: P, Q ∈ assert := stack → mem → Prop Maps variables to indexes in memory

11 / 16

slide-31
SLIDE 31

Assertions of separation logic

Defined using a shallow embedding: P, Q ∈ assert := stack → mem → Prop Maps variables to indexes in memory Some assertions: (P ∗ Q) ρ m := ∃m1 m2 . m = m1 ∪ m2 ∧ m1 ⊥ m2 ∧ P ρ m1 ∧ Q ρ m2

11 / 16

slide-32
SLIDE 32

Assertions of separation logic

Defined using a shallow embedding: P, Q ∈ assert := stack → mem → Prop Maps variables to indexes in memory Some assertions: (P ∗ Q) ρ m := ∃m1 m2 . m = m1 ∪ m2 ∧ m1 ⊥ m2 ∧ P ρ m1 ∧ Q ρ m2 (e1

γ

→ e2) ρ m := ∃b v . [ [ e1 ] ]ρ,m = ptr b ∧ [ [ e2 ] ]ρ,m = v ∧ m = {(b, (v, γ))} (with e1 and e2 side-effect free)

11 / 16

slide-33
SLIDE 33

Assertions of separation logic

Defined using a shallow embedding: P, Q ∈ assert := stack → mem → Prop Maps variables to indexes in memory Some assertions: (P ∗ Q) ρ m := ∃m1 m2 . m = m1 ∪ m2 ∧ m1 ⊥ m2 ∧ P ρ m1 ∧ Q ρ m2 (e1

γ

→ e2) ρ m := ∃b v . [ [ e1 ] ]ρ,m = ptr b ∧ [ [ e2 ] ]ρ,m = v ∧ m = {(b, (v, γ))} (P ⊲) ρ m := P ρ (unlock (locks m)m) (with e1 and e2 side-effect free)

11 / 16

slide-34
SLIDE 34

The Hoare “triples”

Statement judgment ∆; J; R ⊢ {P} s {Q} Function conditions Goto/return conditions

12 / 16

slide-35
SLIDE 35

The Hoare “triples”

Statement judgment ∆; J; R ⊢ {P} s {Q} Function conditions Goto/return conditions Q : assert Expression judgment ∆ ⊢ {P} e {Q} Q : val → assert In the semantics: if P holds beforehand, then

◮ e does not crash ◮ Q v holds afterwards when terminating with v ◮ with framing memories that can change at each step

12 / 16

slide-36
SLIDE 36

The axiomatic semantics

24 separation logic proof rules, e.g.: For assignments:

Write ⊆ kind γ {P1} e1 {Q1} {P2} e2 {Q2} ∀av . ((Q1 a ∗ Q2 v) → ((a

γ

→ –) ∗ R a v)) {P1 ∗ P2} e1 := e2 {λv . ∃a . (a

lock γ

→ v) ∗ R a v}

13 / 16

slide-37
SLIDE 37

The axiomatic semantics

24 separation logic proof rules, e.g.: For assignments:

Write ⊆ kind γ {P1} e1 {Q1} {P2} e2 {Q2} ∀av . ((Q1 a ∗ Q2 v) → ((a

γ

→ –) ∗ R a v)) {P1 ∗ P2} e1 := e2 {λv . ∃a . (a

lock γ

→ v) ∗ R a v}

For dereferencing:

kind γ = Locked {P} e {λa . ∃v . Q a v ∗ (a

γ

→ v)} {P} load e {λv . ∃a . Q a v ∗ (a

γ

→ v)}

13 / 16

slide-38
SLIDE 38

The axiomatic semantics

24 separation logic proof rules, e.g.: For assignments:

Write ⊆ kind γ {P1} e1 {Q1} {P2} e2 {Q2} ∀av . ((Q1 a ∗ Q2 v) → ((a

γ

→ –) ∗ R a v)) {P1 ∗ P2} e1 := e2 {λv . ∃a . (a

lock γ

→ v) ∗ R a v}

For dereferencing:

kind γ = Locked {P} e {λa . ∃v . Q a v ∗ (a

γ

→ v)} {P} load e {λv . ∃a . Q a v ∗ (a

γ

→ v)}

For the conditional:

∆ ⊢ {P} e1 {λv . v = indet ∧ P′v ⊲} ∆ ⊢ {∃v . istrue v ∧ P′ v} e2 {Q} . . . ∆ ⊢ {P} e1 ? e2 : e3 {Q}

13 / 16

slide-39
SLIDE 39

The axiomatic semantics

24 separation logic proof rules, e.g.: For assignments:

Write ⊆ kind γ {P1} e1 {Q1} {P2} e2 {Q2} ∀av . ((Q1 a ∗ Q2 v) → ((a

γ

→ –) ∗ R a v)) {P1 ∗ P2} e1 := e2 {λv . ∃a . (a

lock γ

→ v) ∗ R a v}

For dereferencing:

kind γ = Locked {P} e {λa . ∃v . Q a v ∗ (a

γ

→ v)} {P} load e {λv . ∃a . Q a v ∗ (a

γ

→ v)}

For the conditional:

∆ ⊢ {P} e1 {λv . v = indet ∧ P′v ⊲} ∆ ⊢ {∃v . istrue v ∧ P′ v} e2 {Q} . . . ∆ ⊢ {P} e1 ? e2 : e3 {Q}

Common separation logic and more complex rules can be derived

13 / 16

slide-40
SLIDE 40

Formalization in Coq

◮ Based on [Krebbers/Wiedijk, FoSSaCS’13] ◮ Extremely useful for debugging ◮ Notations close to those on paper ◮ Various extensions of the separation logic ◮ Uses lots of automation ◮ 10 000 lines of code Lemma ax_load ∆ A γ e P Q : perm_kind γ = Locked → ∆ \ A e {{ P }} e {{ λ a, ∃ v, Q a v ∗ valc a → {γ} valc v }} → ∆ \ A e {{ P }} load e {{ λ v, ∃ a, Q a v ∗ valc a → {γ} valc v }}.

14 / 16

slide-41
SLIDE 41

Future research

◮ Integration with the C type system [Krebbers, CPP’13] ◮ Integration with non-aliasing restrictions [Krebbers, CPP’13] ◮ Interpreter in Coq ◮ Verification condition generator in Coq ◮ Automation of separation logic

15 / 16

slide-42
SLIDE 42

Questions

Sources: http://robbertkrebbers.nl/research/ch2o/

16 / 16