Iron: Managing Obligations in Higher-Order Concurrent Separation - - PowerPoint PPT Presentation

iron managing obligations in higher order concurrent
SMART_READER_LITE
LIVE PREVIEW

Iron: Managing Obligations in Higher-Order Concurrent Separation - - PowerPoint PPT Presentation

Iron: Managing Obligations in Higher-Order Concurrent Separation Logic s Bizjak 1 Daniel Gratzer 1 Ale Robbert Krebbers 2 Lars Birkedal 1 1 Aarhus University 2 Delft University of Technology January 17, 2019 POPL 2019


slide-1
SLIDE 1

Iron: Managing Obligations in Higher-Order Concurrent Separation Logic

Aleˇ s Bizjak1 Daniel Gratzer1 Robbert Krebbers2 Lars Birkedal1

1Aarhus University 2Delft University of Technology

January 17, 2019 POPL 2019 https://iris-project.org/iron/

slide-2
SLIDE 2

The Problem

Resources we use in programs impose obligations:

  • Memory must be properly freed.
  • File handles must be closed after use.
  • Locks must be acquired and released properly.

1

slide-3
SLIDE 3

Example

let ℓ = ref(None) in fork { (rec go () = match !ℓ with | None ⇒ go () | Some(x) ⇒ free(x); free(ℓ) end end) () }; ℓ ← Some(ref(1))

2

slide-4
SLIDE 4

Example

let ℓ = ref(None) in fork { (rec go () = match !ℓ with | None ⇒ go () | Some(x) ⇒ free(x); free(ℓ) end end) () }; ℓ ← Some(ref(1)) Channel

2

slide-5
SLIDE 5

Example

let ℓ = ref(None) in fork { (rec go () = match !ℓ with | None ⇒ go () | Some(x) ⇒ free(x); free(ℓ) end end) () }; ℓ ← Some(ref(1)) Channel Wait...

2

slide-6
SLIDE 6

Example

let ℓ = ref(None) in fork { (rec go () = match !ℓ with | None ⇒ go () | Some(x) ⇒ free(x); free(ℓ) end end) () }; ℓ ← Some(ref(1)) Channel Wait... Signal the thread

2

slide-7
SLIDE 7

Example

let ℓ = ref(None) in fork { (rec go () = match !ℓ with | None ⇒ go () | Some(x) ⇒ free(x); free(ℓ) end end) () }; ℓ ← Some(ref(1)) Channel Wait... Signal the thread ...then clean up

2

slide-8
SLIDE 8

Wish list

We want a concurrent separation logic to prove these properties:

  • Has thread-local reasoning
  • Can express complex and modular specifications
  • Handles complicated language features (especially fork)
  • Is amenable to mechanization
  • Can prove leak-freedom

3

slide-9
SLIDE 9

Wish list

We want a concurrent separation logic to prove these properties:

  • Has thread-local reasoning
  • Can express complex and modular specifications
  • Handles complicated language features (especially fork)
  • Is amenable to mechanization
  • Can prove leak-freedom

Iris (a state of the art concurrent separation logic) gives us the first 4.

3

slide-10
SLIDE 10

Our Contribution

Trackable resources: a general mechanism for managing obligations and Iron: a separation logic implementing it:

  • Includes all proof techniques of Iris

(ghost state, impredicative invariants, updates, etc...)

  • Supports all the language features of Iris
  • Fully mechanized in Coq

4

slide-11
SLIDE 11

Other Approaches: Iris

Iris and other affine logics gives us safety (and correctness):

Theorem

If {True} e {True} holds then e does not get stuck. We wish to strengthen this to ensure leak-freedom.

5

slide-12
SLIDE 12

Other Approaches: CSL

O’Hearn [2007] and Brookes [2007] ensured leak-freedom through linearity for statically scoped concurrency: Γ ⊢ {Emp} ref(v) {ℓ. ℓ → v} Γ ⊢ {ℓ → w} free(ℓ) {Emp} Γ ⊢ {P1} e1 {Q1} Γ ⊢ {P2} e2 {Q2} Γ ⊢ {P1 ∗ P2} e1 || e2 {Q1 ∗ Q2}

6

slide-13
SLIDE 13

Other Approaches: CSL

O’Hearn [2007] and Brookes [2007] ensured leak-freedom through linearity for statically scoped concurrency: Γ ⊢ {Emp} ref(v) {ℓ. ℓ → v} Γ ⊢ {ℓ → w} free(ℓ) {Emp} Γ ⊢ {P1} e1 {Q1} Γ ⊢ {P2} e2 {Q2} Γ ⊢ {P1 ∗ P2} e1 || e2 {Q1 ∗ Q2} Our heap view is empty

6

slide-14
SLIDE 14

Other Approaches: CSL

O’Hearn [2007] and Brookes [2007] ensured leak-freedom through linearity for statically scoped concurrency: Γ ⊢ {Emp} ref(v) {ℓ. ℓ → v} Γ ⊢ {ℓ → w} free(ℓ) {Emp} Γ ⊢ {P1} e1 {Q1} Γ ⊢ {P2} e2 {Q2} Γ ⊢ {P1 ∗ P2} e1 || e2 {Q1 ∗ Q2} Does not share memory Our heap view is empty

6

slide-15
SLIDE 15

Scoped Invariants

With only parallel composition scoped invariants are sufficient: Γ, r : R ⊢ {P} e {Q} Γ ⊢ {P ∗ R} resource r in e {Q ∗ R} Scoped invariants are insufficient for the “unscoped concurrency”: let ℓ = ref(1) in resource r in fork {with r do !ℓ} ; free(ℓ)

7

slide-16
SLIDE 16

Unscoped Invariants

With fork we need unscoped invariants:

{P ∗ R

N} e {v. Q}

{P ∗ R} e {v. Q}

  • Invariants persist forever and can be duplicated freely.
  • There is no deallocation rule; it must be encoded.

8

slide-17
SLIDE 17

Unscoped Invariants

With fork we need unscoped invariants:

{P ∗ R

N} e {v. Q}

{P ∗ R} e {v. Q}

  • Invariants persist forever and can be duplicated freely.
  • There is no deallocation rule; it must be encoded.

We can always put resources in an invariant and forget them – no linearity!

8

slide-18
SLIDE 18

Resolving This Tension

  • Scoped tracks obligations but does not handle fork.
  • Unscoped handles fork but does not track obligations.
  • Invariants are complex to prove sound; we prefer not to modify them.

We will modify → instead so that unscoped invariants are suitable.

9

slide-19
SLIDE 19

Crucial Idea: Trackable Resources

We keep the affine logic so Iris’s implementation of invariants can be reused.

  • Index ℓ →π v with π ∈ (0, 1]
  • Add a new proposition eπ with π ∈ (0, 1]

π indicates how much of the heap we know about through the proposition.

10

slide-20
SLIDE 20

Intuition for Fractions

If we own...

  • ℓ →1 v then the only thing the heap contains is ℓ → v.
  • e1 the heap contains nothing at all.
  • ℓ →π v and π < 1 the heap may contain other locations.
  • ℓ1 →1/

2 v and ℓ2 →1/ 2 w the heap contains just ℓ1 and ℓ2.

We can prove leak-freedom by owning e1.

11

slide-21
SLIDE 21

Intuition for Fractions

If we own...

  • ℓ →1 v then the only thing the heap contains is ℓ → v.
  • e1 the heap contains nothing at all.
  • ℓ →π v and π < 1 the heap may contain other locations.
  • ℓ1 →1/

2 v and ℓ2 →1/ 2 w the heap contains just ℓ1 and ℓ2.

We can prove leak-freedom by owning e1.

11

slide-22
SLIDE 22

Intuition for Fractions

If we own...

  • ℓ →1 v then the only thing the heap contains is ℓ → v.
  • e1 the heap contains nothing at all.
  • ℓ →π v and π < 1 the heap may contain other locations.
  • ℓ1 →1/

2 v and ℓ2 →1/ 2 w the heap contains just ℓ1 and ℓ2.

We can prove leak-freedom by owning e1.

11

slide-23
SLIDE 23

Intuition for Fractions

If we own...

  • ℓ →1 v then the only thing the heap contains is ℓ → v.
  • e1 the heap contains nothing at all.
  • ℓ →π v and π < 1 the heap may contain other locations.
  • ℓ1 →1/

2 v and ℓ2 →1/ 2 w the heap contains just ℓ1 and ℓ2.

We can prove leak-freedom by owning e1.

11

slide-24
SLIDE 24

Fractional Permissions?

What does ℓ →π v mean?

  • With fractional permissions we own π of the location.
  • With Iron we own π of the entire heap.

Crucial difference: we can write to ℓ →1/

2 v in Iron but not in Boyland [2003].

12

slide-25
SLIDE 25

Working with Fractions in Programs: The Heap

The program logic adapts to handle these fractions as follows: {eπ} ref(v) {ℓ. ℓ →π v} {ℓ →π w} free(ℓ) {eπ} {ℓ →π v} !ℓ {w. w = v ∧ ℓ →π v} {ℓ →π w} ℓ ← v {ℓ →π v} eπ1 ∗ eπ2 ⊣⊢ eπ1+π2 (ℓ →π1 v) ∗ eπ2 ⊣⊢ ℓ →π1+π2 v

13

slide-26
SLIDE 26

Working with Fractions in Programs: Concurrency

The standard rule for fork holds: {P} e {True} {P} fork {e} {v. v = ()} This rule is insufficient if the forked-off thread outlives its parent: fork { let ℓ = ref(1) in free(ℓ) }; 1 + 1

14

slide-27
SLIDE 27

Working with Fractions in Programs: Concurrency

We must also allow the forked-off thread to terminate with eπ: {P} e {True} {P} fork {e} {v. v = ()} {P} e {eπ} {P} fork {e} {v. v = () ∗ eπ}

15

slide-28
SLIDE 28

Adequacy

Iron provides us with strong guarantees about programs:

Theorem

If {eπ} e {eπ}:

  • 1. e does not get stuck
  • 2. If (e, h) →∗ ([v, v0, ..., vn

thread results

], h′) then h = h′.

16

slide-29
SLIDE 29

Adequacy

Iron provides us with strong guarantees about programs:

Theorem

If {eπ} e {eπ}:

  • 1. e does not get stuck
  • 2. If (e, h) →∗ ([v, v0, ..., vn

thread results

], h′) then h = h′. If we forget part of eπ then we cannot apply our adequacy theorem; the triple won’t hold!

16

slide-30
SLIDE 30

Taking Stock

At this point, Iron is already useful!

17

slide-31
SLIDE 31

Taking Stock

At this point, Iron is already useful! But it isn’t easy; there’s boilerplate with fractions everywhere:

{(π1 + π2 = 1) ∗ (ℓ1 →π1 v1) ∗ (ℓ2 →π2/

2 v2) ∗ (ℓ3 →π2/ 2 v3)}

free(ℓ1); free(ℓ2); free(ℓ3) {e1}

17

slide-32
SLIDE 32

The Lifted Logic

We can lift the operators of BI to functions, [0, 1] → iProp. (P ∗ Q)(π) ∃π1, π2. π1 + π2 = π ∧ P(π1) ∗ Q(π2) (ℓ → v)(π) π > 0 ∧ ℓ →π v Emp(π) π = 0 Other operations are lifted point-wise.

18

slide-33
SLIDE 33

The Lifted Logic

We can lift the operators of BI to functions, [0, 1] → iProp. (P ∗ Q)(π) ∃π1, π2. π1 + π2 = π ∧ P(π1) ∗ Q(π2) (ℓ → v)(π) π > 0 ∧ ℓ →π v Emp(π) π = 0 Other operations are lifted point-wise. The lifted logic is really linear! ℓ1 → v1 ∗ ℓ2 → v2 ⊢ ℓ1 → v1

18

slide-34
SLIDE 34

The Lifted Logic: New Rules

The lifted program logic mirrors standard linear separation logic: {Emp} ref(v) {ℓ. ℓ → v} {ℓ → −} free(ℓ) {Emp} {ℓ → v} !ℓ {w. w = v ∧ ℓ → v} {ℓ → −} ℓ ← v {ℓ → v} {P} e {Emp} {P} fork {e} {v. v = () ∧ Emp}

19

slide-35
SLIDE 35

The Lifted Logic: Invariants

  • We developed a specialized form of invariants for lifted propositions.
  • They require permission to open in order to support deallocation.
  • They integrate well with the lifted logic but are limited.

20

slide-36
SLIDE 36

The Lifted Logic: Invariants

  • We developed a specialized form of invariants for lifted propositions.
  • They require permission to open in order to support deallocation.
  • They integrate well with the lifted logic but are limited.

Sometimes we still need the unlifted logic for the more general invariants. e1 || e2 let h = spawn(λ . e1) in let v2 = e2 in let v1 = join(h) in (v1, v2)

20

slide-37
SLIDE 37

Using Iron

We’ve used Iron to formalize a number of examples:

  • 1. An implementation of e1 || e2
  • 2. Various examples of resource transfer
  • 3. A lock-free queue
  • 4. An asynchronous message system with cleanup

Aside from the first, all of these are proven in the lifted logic.

21

slide-38
SLIDE 38

Conclusions

Trackable resources: a general mechanism for managing obligations and Iron: a separation logic implementing it:

  • Includes all proof techniques of Iris

(ghost state, impredicative invariants, updates, etc...)

  • Supports all the language features of Iris
  • Fully mechanized in Coq

https://iris-project.org/iron/

22