1
Separation algebras for C verification in Coq Robbert Krebbers - - PowerPoint PPT Presentation
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 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
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
3
Why compiler (in)dependence matters
int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); }
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
8
Organization of permissions
Separation logic: ∪ main connective (from separation algebra) Operational semantics: need to know what is allowed
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
12
Aliasing
Aliasing: multiple pointers referring to the same object
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
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
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
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
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
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
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
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
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
15
The C memory compositionally
Def: The C memory is defined as: mem := cmap (T?:bit(F(L(C(Q)))))
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
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
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
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
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
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
17