Separation algebras for C verification in Coq Robbert Krebbers - - PowerPoint PPT Presentation

separation algebras for c verification in coq
SMART_READER_LITE
LIVE PREVIEW

Separation algebras for C verification in Coq Robbert Krebbers - - PowerPoint PPT Presentation

Separation algebras for C verification in Coq Robbert Krebbers ICIS, Radboud University Nijmegen, The Netherlands July 18, 2014 @ VSTTE, Vienna, Austria 1 Context of this talk Formalin (Krebbers & Wiedijk) Compiler independent C


slide-1
SLIDE 1

1

Separation algebras for C verification in Coq

Robbert Krebbers

ICIS, Radboud University Nijmegen, The Netherlands

July 18, 2014 @ VSTTE, Vienna, Austria

slide-2
SLIDE 2

2

Context of this talk

Formalin (Krebbers & Wiedijk)

◮ Compiler independent C semantics in Coq ◮ Take underspecification by C11 seriously ◮ Operational semantics ◮ Executable semantics ◮ Typing and type checker ◮ Separation logic

H H

C11

O

slide-3
SLIDE 3

2

Context of this talk

Formalin (Krebbers & Wiedijk)

◮ Compiler independent C semantics in Coq ◮ Take underspecification by C11 seriously ◮ Operational semantics ◮ Executable semantics ◮ Typing and type checker ◮ Separation logic ⇒ topic of this talk

H H

C11

O

slide-4
SLIDE 4

3

Why compiler (in)dependence matters

int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); }

slide-5
SLIDE 5

3

Why compiler (in)dependence matters

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

slide-6
SLIDE 6

3

Why compiler (in)dependence matters

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

slide-7
SLIDE 7

3

Why compiler (in)dependence matters

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 ◮ undefined behavior: garbage in, garbage out ⇒ all bets are off ◮ thus both compilers are right

slide-8
SLIDE 8

3

Why compiler (in)dependence matters

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 ◮ undefined behavior: garbage in, garbage out ⇒ all bets are off ◮ thus both compilers are right

Formalin should account for all undefined behavior

slide-9
SLIDE 9

4

Separation logic for C [Krebbers, POPL’14]

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}

slide-10
SLIDE 10

4

Separation logic for C [Krebbers, POPL’14]

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

slide-11
SLIDE 11

4

Separation logic for C [Krebbers, POPL’14]

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

slide-12
SLIDE 12

5

Connectives of separation logic

The connectives of separation logic are defined as: emp := λm . m = ∅ P ∗ Q := λm . ∃m1 m2 . m = m1 ∪ m2 ∧ P m1 ∧ Q m2

slide-13
SLIDE 13

5

Connectives of separation logic

The connectives of separation logic are defined as: emp := λm . m = ∅ P ∗ Q := λm . ∃m1 m2 . m = m1 ∪ m2 ∧ P m1 ∧ Q m2 Definition of is non-trivial:

◮ Complex memory based on structured trees ◮ Fractional permissions for share-accounting

For example needed in x + x

◮ Existence permissions for pointer arithmetic

For example needed in *(p + 1) = (*p = 1)

◮ Locked permissions for sequence point restriction

slide-14
SLIDE 14

5

Connectives of separation logic

The connectives of separation logic are defined as: emp := λm . m = ∅ P ∗ Q := λm . ∃m1 m2 . m = m1 ∪ m2 ∧ P m1 ∧ Q m2 Definition of is non-trivial:

◮ Complex memory based on structured trees ◮ Fractional permissions for share-accounting

For example needed in x + x

◮ Existence permissions for pointer arithmetic

For example needed in *(p + 1) = (*p = 1)

◮ Locked permissions for sequence point restriction

Use separation algebras [Calcagno et al., LICS’07] to abstractly describe the permissions and memory

slide-15
SLIDE 15

6

Tweaked version of separation algebras in Coq

Def: A simple separation algebra consists of a set A, with:

◮ An element ∅ : A ◮ A predicate valid : A → Prop ◮ Binary relations ⊥, ⊆ : A → A → Prop ◮ Binary operations ∪, \ : A → A → A

slide-16
SLIDE 16

6

Tweaked version of separation algebras in Coq

Def: A simple separation algebra consists of a set A, with:

◮ An element ∅ : A ◮ A predicate valid : A → Prop ◮ Binary relations ⊥, ⊆ : A → A → Prop ◮ Binary operations ∪, \ : A → A → A

Total instead of partial

slide-17
SLIDE 17

6

Tweaked version of separation algebras in Coq

Def: A simple separation algebra consists of a set A, with:

◮ An element ∅ : A ◮ A predicate valid : A → Prop ◮ Binary relations ⊥, ⊆ : A → A → Prop ◮ Binary operations ∪, \ : A → A → A

Total instead of partial Disjointness

slide-18
SLIDE 18

6

Tweaked version of separation algebras in Coq

Def: A simple separation algebra consists of a set A, with:

◮ An element ∅ : A ◮ A predicate valid : A → Prop ◮ Binary relations ⊥, ⊆ : A → A → Prop ◮ Binary operations ∪, \ : A → A → A

To avoid subset types Total instead of partial Disjointness

slide-19
SLIDE 19

6

Tweaked version of separation algebras in Coq

Def: A simple separation algebra consists of a set A, with:

◮ An element ∅ : A ◮ A predicate valid : A → Prop ◮ Binary relations ⊥, ⊆ : A → A → Prop ◮ Binary operations ∪, \ : A → A → A

To avoid subset types Total instead of partial Disjointness Satisfying the following laws:

  • 1. If x ⊥ y, then y ⊥ x and x ∪ y = y ∪ x
  • 2. If valid x, then ∅ ⊥ x and ∅ ∪ x = x
  • 3. Associative, non-empty, cancellative, positive, . . .
slide-20
SLIDE 20

7

Example: fractional separation algebra

Fractional permissions [0, 1]Q [Boyland, SAS’09] No access Read-only Exclusive access 1 Rational numbers make it possible to split Read-only permissions

slide-21
SLIDE 21

7

Example: fractional separation algebra

Fractional permissions [0, 1]Q [Boyland, SAS’09] No access Read-only Exclusive access 1 Rational numbers make it possible to split Read-only permissions Def: The simple fractional separation algebra Q is defined as: valid x := 0 ≤ x ≤ 1 x ⊥ y := 0 ≤ x, y ∧ x + y ≤ 1 x ⊆ y := 0 ≤ x ≤ y ≤ 1 ∅ := 0 x ∪ y := x + y x \ y := x − y

slide-22
SLIDE 22

8

Organization of permissions

Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed

slide-23
SLIDE 23

8

Organization of permissions

Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed Def: Lattice of permission kinds (pkind, ⊆k) Freeable Writable Readable Locked Existing ⊥

◮ Freeable: reading, writing, deallocation

slide-24
SLIDE 24

8

Organization of permissions

Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed Def: Lattice of permission kinds (pkind, ⊆k) Freeable Writable Readable Locked Existing ⊥

◮ Freeable: reading, writing, deallocation ◮ Writable: reading, writing

slide-25
SLIDE 25

8

Organization of permissions

Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed Def: Lattice of permission kinds (pkind, ⊆k) Freeable Writable Readable Locked Existing ⊥

◮ Freeable: reading, writing, deallocation ◮ Writable: reading, writing ◮ Readable: reading

slide-26
SLIDE 26

8

Organization of permissions

Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed Def: Lattice of permission kinds (pkind, ⊆k) Freeable Writable Readable Locked Existing ⊥

◮ Freeable: reading, writing, deallocation ◮ Writable: reading, writing ◮ Readable: reading ◮ Existing: existence permissions, only

pointer arithmetic

slide-27
SLIDE 27

8

Organization of permissions

Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed Def: Lattice of permission kinds (pkind, ⊆k) Freeable Writable Readable Locked Existing ⊥

◮ Freeable: reading, writing, deallocation ◮ Writable: reading, writing ◮ Readable: reading ◮ Existing: existence permissions, only

pointer arithmetic

◮ Locked: temporarily locked until next

sequence point Example: (x = 3) + (*p = 4); Undefined behavior if &x == p

slide-28
SLIDE 28

8

Organization of permissions

Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed Def: Lattice of permission kinds (pkind, ⊆k) Freeable Writable Readable Locked Existing ⊥

◮ Freeable: reading, writing, deallocation ◮ Writable: reading, writing ◮ Readable: reading ◮ Existing: existence permissions, only

pointer arithmetic

◮ Locked: temporarily locked until next

sequence point Example: (x = 3) + (*p = 4); Undefined behavior if &x == p

◮ ⊥: no operations allowed

Example: free(p); return (p-p);

slide-29
SLIDE 29

9

Interaction with permission kinds

Def: A C permissions system is a separation algebra A with functions kind : A → pkind, lock, unlock : A → A satisfying: unlock (lock x) = x provided that Writable ⊆k kind x kind (lock x) = Locked provided that Writable ⊆k kind x

slide-30
SLIDE 30

9

Interaction with permission kinds

Def: A C permissions system is a separation algebra A with functions kind : A → pkind, lock, unlock, 1

2 : A → A

satisfying: unlock (lock x) = x provided that Writable ⊆k kind x kind (lock x) = Locked provided that Writable ⊆k kind x kind 1

2x

  • =
  • Readable

if Writable ⊆k kind x kind x

  • therwise

Example: use 1

2 in x + x

slide-31
SLIDE 31

9

Interaction with permission kinds

Def: A C permissions system is a separation algebra A with functions kind : A → pkind, lock, unlock, 1

2 : A → A and token : A

satisfying: unlock (lock x) = x provided that Writable ⊆k kind x kind (lock x) = Locked provided that Writable ⊆k kind x kind 1

2x

  • =
  • Readable

if Writable ⊆k kind x kind x

  • therwise

kind token = Existing kind (x \ token) =

  • Writable

if kind x = Freeable kind x if Existing ⊆k kind x Example: use 1

2 in x + x

Example: use \ token in *(p + 1) = (*p = 1)

slide-32
SLIDE 32

10

Implementation of permissions

Def: C permissions are defined as perm := F(L(C(Q))) = {Freed} + {◦, •} × Q × Q with: Readable Freeable Writable Existing Locked

  • (0, 1)
  • (0, 0)
  • (0, 1)

Freed ⊥

slide-33
SLIDE 33

10

Implementation of permissions

Def: C permissions are defined as perm := F(L(C(Q))) = {Freed} + {◦, •} × Q × Q Fractional SA with: Readable Freeable Writable Existing Locked

  • (0, 1)
  • (0, 0)
  • (0, 1)

Freed ⊥

slide-34
SLIDE 34

10

Implementation of permissions

Def: C permissions are defined as perm := F(L(C(Q))) = {Freed} + {◦, •} × Q × Q Countable SA Fractional SA with: Readable Freeable Writable Existing Locked

  • (0, 1)
  • (0, 0)
  • (0, 1)

Freed ⊥

slide-35
SLIDE 35

10

Implementation of permissions

Def: C permissions are defined as perm := F(L(C(Q))) = {Freed} + {◦, •} × Q × Q Lockable SA Countable SA Fractional SA with: Readable Freeable Writable Existing Locked

  • (0, 1)
  • (0, 0)
  • (0, 1)

Freed ⊥

slide-36
SLIDE 36

10

Implementation of permissions

Def: C permissions are defined as perm := F(L(C(Q))) = {Freed} + {◦, •} × Q × Q Freeable SA Lockable SA Countable SA Fractional SA with: Readable Freeable Writable Existing Locked

  • (0, 1)
  • (0, 0)
  • (0, 1)

Freed ⊥

slide-37
SLIDE 37

11

The C memory

Extremely complex:

◮ Pointer arithmetic ◮ Difficult interaction between low and high level

◮ Types ◮ Object representations

◮ Byte-wise operations on all objects ◮ Non-aliasing restrictions ◮ Permissions

slide-38
SLIDE 38

12

Aliasing

Aliasing: multiple pointers referring to the same object

slide-39
SLIDE 39

12

Aliasing

Aliasing: multiple pointers referring to the same object int f(int *p, int *q) { int x = *p; *q = 314; return x; } If p and q alias, the original value n of *p is returned n p q

slide-40
SLIDE 40

12

Aliasing

Aliasing: multiple pointers referring to the same object int f(int *p, int *q) { int x = *p; *q = 314; return x *p; } If p and q alias, the original value n of *p is returned n p q Optimizing x away is unsound: 314 would be returned

slide-41
SLIDE 41

12

Aliasing

Aliasing: multiple pointers referring to the same object int f(int *p, int *q) { int x = *p; *q = 314; return x *p; } If p and q alias, the original value n of *p is returned n p q Optimizing x away is unsound: 314 would be returned Alias analysis: to determine whether pointers can alias

slide-42
SLIDE 42

13

Aliasing with different types

Consider a similar function: int h(int *p, float *q) { int x = *p; *q = 3.14; return x; }

slide-43
SLIDE 43

13

Aliasing with different types

Consider a similar function: int h(int *p, float *q) { int x = *p; *q = 3.14; return x; } It can still be called with aliased pointers: union { int x; float y; } u; u.x = 271; return h(&u.x, &u.y); x y &u.x &u.y C89 allows p and q to be aliased, and thus requires it to return 271

slide-44
SLIDE 44

13

Aliasing with different types

Consider a similar function: int h(int *p, float *q) { int x = *p; *q = 3.14; return x; } It can still be called with aliased pointers: union { int x; float y; } u; u.x = 271; return h(&u.x, &u.y); x y &u.x &u.y C89 allows p and q to be aliased, and thus requires it to return 271 C99/C11 allows type-based alias analysis:

◮ A compiler can assume that p and q do not alias ◮ Reads/writes with “the wrong type” yield undefined behavior

slide-45
SLIDE 45

14

The C memory as structured forest [Krebbers, CPP’13]

Consider: struct T { union U { signed char x[2]; int y; } u; void *p; } s = { { .x = {33,34} }, s.u.x + 2 } As a picture: ws =

.0 signed char: 10000100 01000100 ???????? ???????? void∗: (ptr p)0 (ptr p)1 . . . (ptr p)31

p

slide-46
SLIDE 46

14

The C memory as structured forest [Krebbers, CPP’13]

Consider: struct T { union U { signed char x[2]; int y; } u; void *p; } s = { { .x = {33,34} }, s.u.x + 2 } As a picture: ws =

.0 signed char: 10000100 01000100 ???????? ???????? void∗: (ptr p)0 (ptr p)1 . . . (ptr p)31

p

Captures aliasing restrictions of C11

slide-47
SLIDE 47

14

The C memory as structured forest [Krebbers, CPP’13]

Consider: struct T { union U { signed char x[2]; int y; } u; void *p; } s = { { .x = {33,34} }, s.u.x + 2 } As a picture: ws =

.0 signed char: 10000100 01000100 ???????? ???????? void∗: (ptr p)0 (ptr p)1 . . . (ptr p)31

p

Captures aliasing restrictions of C11 Generalization of [Krebbers, CPP’13] is a separation algebra

slide-48
SLIDE 48

15

The C memory compositionally

Def: The C memory is defined as: mem := cmap (T?:bit(F(L(C(Q)))))

slide-49
SLIDE 49

15

The C memory compositionally

Def: The C memory is defined as: mem := cmap (T?:bit(F(L(C(Q))))) Permissions

slide-50
SLIDE 50

15

The C memory compositionally

Def: The C memory is defined as: mem := cmap (T?:bit(F(L(C(Q))))) (Bit) tagged SA Permissions

slide-51
SLIDE 51

15

The C memory compositionally

Def: The C memory is defined as: mem := cmap (T?:bit(F(L(C(Q))))) Structured memory SA Generalization of [Krebbers, CPP’13] (Bit) tagged SA Permissions

slide-52
SLIDE 52

16

The bigger picture / Future work

Non-local control & block scope variables

(FoSSaCS, 2013)

C-types & strict aliasing restrictions

(CPP, 2013)

Sequence points & non-determinism

(POPL, 2014)

Separation algebras for the C memory

(VSTTE, 2014)

slide-53
SLIDE 53

16

The bigger picture / Future work

Non-local control & block scope variables

(FoSSaCS, 2013)

C-types & strict aliasing restrictions

(CPP, 2013)

Sequence points & non-determinism

(POPL, 2014)

Separation algebras for the C memory

(VSTTE, 2014)

Separation logic for C11’

slide-54
SLIDE 54

16

The bigger picture / Future work

Non-local control & block scope variables

(FoSSaCS, 2013)

C-types & strict aliasing restrictions

(CPP, 2013)

Sequence points & non-determinism

(POPL, 2014)

Separation algebras for the C memory

(VSTTE, 2014)

Separation logic for C11’ Interpreter for C11’

slide-55
SLIDE 55

17

Questions

Sources: http://robbertkrebbers.nl/research/ch2o/ (http://xkcd.com/371/)