Specification and Verification of Multithreaded Object-Oriented - - PowerPoint PPT Presentation

specification and verification of multithreaded object
SMART_READER_LITE
LIVE PREVIEW

Specification and Verification of Multithreaded Object-Oriented - - PowerPoint PPT Presentation

Specification and Verification of Multithreaded Object-Oriented Programs with Separation Logic Cl ement Hurlin INRIA Sophia Antipolis M editerran ee, France Universiteit Twente, The Netherlands Soutenance de th` ese Th` ese


slide-1
SLIDE 1

Specification and Verification of Multithreaded Object-Oriented Programs with Separation Logic

Cl´ ement Hurlin

INRIA Sophia Antipolis – M´ editerran´ ee, France Universiteit Twente, The Netherlands

Soutenance de th` ese

Th` ese dirig´ ee par Marieke Huisman Th` ese effectu´ ee au sein des ´ equipes Everest et FMT 14 septembre 2009

slide-2
SLIDE 2

Un programme, qu’est ce que c’est ?

Un programme est une suite d’instructions. Exemple de programme calculant la valeur d’une fraction: lire n; lire d; affiche n / d; ë ë ë

2

slide-3
SLIDE 3

Un programme, qu’est ce que c’est ?

Un programme est une suite d’instructions. Exemple de programme calculant la valeur d’une fraction: lire n; lire d; affiche n / d; Le but de cette th` ese: S’assurer que les programmes fonctionnent correctement. ë ë ë

2

slide-4
SLIDE 4

Un programme, qu’est ce que c’est ?

Un programme est une suite d’instructions. Exemple de programme calculant la valeur d’une fraction: lire n; lire d; affiche n / d; Le but de cette th` ese: S’assurer que les programmes fonctionnent correctement. ë Par exemple, le programme ci-dessus est-il correct ? ë Hum, pas vraiment, on peut effectuer une division par z´

  • ero. . .

ë C.` a.d. que ce programme peut planter.

2

slide-5
SLIDE 5

Un programme, qu’est ce que c’est ?

Un programme est une suite d’instructions. Exemple de programme calculant la valeur d’une fraction: lire n; lire d; si d ✘ 0 alors affiche (n / d); sinon affiche ‘‘Erreur : d doit etre different de zero.’’; Le but de cette th` ese: S’assurer que les programmes fonctionnent correctement. ë Par exemple, le programme ci-dessus est-il correct ? ë Oui!

3

slide-6
SLIDE 6

V´ erifier des programmes

Pour s’assurer du bon fonctionnement des programmes, on les v´ erifie. V´ erifier un programme P, c ¸a consiste ` a: Sp´ ecifier formellement P, c.` a.d. exprimer ce que P est cens´ e faire. V´ erifier que P satisfait sa sp´ ecification. programme P programme P sp´ ecification formelle S + + P respecte S ? sp´ ecification informelle

4

slide-7
SLIDE 7

Sujets d’´ etude de cette th` ese

programme P programme P sp´ ecification formelle S + + P respecte S ? sp´ ecification informelle comment sp´ ecifier ?

5

slide-8
SLIDE 8

Sujets d’´ etude de cette th` ese

programme P programme P sp´ ecification formelle S + + P respecte S ? sp´ ecification informelle comment sp´ ecifier ?

5

slide-9
SLIDE 9

Sujets d’´ etude de cette th` ese

programme P programme P sp´ ecification formelle S + + P respecte S ? sp´ ecification informelle comment sp´ ecifier ? comment v´ erifier ?

5

slide-10
SLIDE 10

Sujets d’´ etude de cette th` ese

programme P programme P sp´ ecification formelle S + + P respecte S ? sp´ ecification informelle comment sp´ ecifier ? comment v´ erifier ?

5

slide-11
SLIDE 11

Sujets d’´ etude de cette th` ese

programme P programme P sp´ ecification formelle S + + P respecte S ? sp´ ecification informelle comment sp´ ecifier ? comment v´ erifier ?

Pour des programmes objets et multi-processeurs.

6

slide-12
SLIDE 12

Objectives of the thesis

To adapt separation logic to multithreaded Java

ë I.e. to support Java’s primitives for multithreading: ♣1q fork/join ♣2q Reentrant locks ⑥

7

slide-13
SLIDE 13

Objectives of the thesis

To adapt separation logic to multithreaded Java

ë I.e. to support Java’s primitives for multithreading: ♣1q fork/join ♣2q Reentrant locks By using variants of separation logic [Reynolds’02]:

Separation logic for while programs with a parallel operator ⑥ [O’Hearn’07] Separation logic for sequential Java programs [Parkinson’05]

7

slide-14
SLIDE 14

Objectives of the thesis

To adapt separation logic to multithreaded Java

ë I.e. to support Java’s primitives for multithreading: ♣1q fork/join ♣2q Reentrant locks By using variants of separation logic [Reynolds’02]:

Separation logic for while programs with a parallel operator ⑥ [O’Hearn’07] Separation logic for sequential Java programs [Parkinson’05]

Side effects of the thesis: Three analyses based on separation logic

7

slide-15
SLIDE 15

Our tool for reasoning: Separation Logic

Our assertion language is permission accounting separation logic [Reynolds’02,Bornat et al.’05]. Formulas represent permissions to access the heap. Formula x.f

π

ÞÝ Ñ v has a dual meaning:

x.f contains value v. Permission π to access field x.f.

Permissions π are fractions in ♣0,1s [Boyland’03].

Permission 1 grants write and read access. Any permission ➔ 1 grants readonly access.

8

slide-16
SLIDE 16

Our tool for reasoning: Separation Logic

Our assertion language is permission accounting separation logic [Reynolds’02,Bornat et al.’05]. Formulas represent permissions to access the heap. Formula x.f

π

ÞÝ Ñ v has a dual meaning:

x.f contains value v. Permission π to access field x.f.

Permissions π are fractions in ♣0,1s [Boyland’03].

Permission 1 grants write and read access. Any permission ➔ 1 grants readonly access.

Abstract predicates represent complex formulas [Parkinson’05]: They are defined in classes. They have at least one parameter (the receiver)

8

slide-17
SLIDE 17

Our tool for reasoning: Separation Logic

Our assertion language is permission accounting separation logic [Reynolds’02,Bornat et al.’05]. Formulas represent permissions to access the heap. Formula x.f

π

ÞÝ Ñ v has a dual meaning:

x.f contains value v. Permission π to access field x.f.

Permissions π are fractions in ♣0,1s [Boyland’03].

Permission 1 grants write and read access. Any permission ➔ 1 grants readonly access.

Abstract predicates represent complex formulas [Parkinson’05]: They are defined in classes. They have at least one parameter (the receiver) Compared to the literature: We mix object-orientation and permissions. Classes can be parameterized by specification values.

8

slide-18
SLIDE 18

Objective 1: fork/join [AMAST’08]

fork and join are the two primitives used to create and wait threads (in Java, C++, C, python, etc.): t.fork() starts a new thread t. t.join() waits until thread t terminates. ë fork and join are more general than ⑥.

9

slide-19
SLIDE 19

Objective 1: fork/join [AMAST’08]

fork and join are the two primitives used to create and wait threads (in Java, C++, C, python, etc.): t.fork() starts a new thread t. t.join() waits until thread t terminates. ë fork and join are more general than ⑥. In terms of resources (i.e. the heap), fork and join behave as follows: t.fork() consumes the resource needed by t to execute.

9

slide-20
SLIDE 20

Objective 1: fork/join [AMAST’08]

fork and join are the two primitives used to create and wait threads (in Java, C++, C, python, etc.): t.fork() starts a new thread t. t.join() waits until thread t terminates. ë fork and join are more general than ⑥. In terms of resources (i.e. the heap), fork and join behave as follows: t.fork() consumes the resource needed by t to execute. t.join() gets back [a part of] t’s resource when t terminates.

9

slide-21
SLIDE 21

fork and join in terms of resources (1)

thread t0

10

slide-22
SLIDE 22

fork and join in terms of resources (1)

thread t0 t1.fork() thread t1

10

slide-23
SLIDE 23

fork and join in terms of resources (1)

thread t0 t1.fork() thread t1

10

slide-24
SLIDE 24

fork and join in terms of resources (1)

thread t0 t1.fork() thread t1 t1.join()

10

slide-25
SLIDE 25

Fork and join in terms of resources (2)

thread t0 thread t2

11

slide-26
SLIDE 26

Fork and join in terms of resources (2)

thread t0 thread t2 t1.fork() thread t1

11

slide-27
SLIDE 27

Fork and join in terms of resources (2)

thread t0 thread t2 t1.fork() thread t1

11

slide-28
SLIDE 28

Fork and join in terms of resources (2)

thread t0 thread t2 t1.fork() thread t1 t1.join() t1.join()

11

slide-29
SLIDE 29

Hoare rules for fork and join

class Thread extends Object{ void fork(); void join(); void run() { null } }

12

slide-30
SLIDE 30

Hoare rules for fork and join

class Thread extends Object{ void fork(); void join(); void run() { null } } When t.fork() is called, t.run() is executed in parallel.

12

slide-31
SLIDE 31

Hoare rules for fork and join

class Thread extends Object{ pred preFork = true; // to be extended in subclasses requires preFork; ensures true; void fork(); void join(); requires preFork; ensures true; void run() { null } } When t.fork() is called, t.run() is executed in parallel.

13

slide-32
SLIDE 32

Hoare rules for fork and join

class Thread extends Object{ pred preFork = true; pred postJoin = true; // to be extended in subclasses requires preFork; ensures true; void fork(); requires Join(this); ensures postJoin; void join(); requires preFork; ensures postJoin; void run() { null } } t.join() resumes when t terminates. Join(t): the thread in which this formula appears can get back t’s postcondition when t terminates. ë

14

slide-33
SLIDE 33

Hoare rules for fork and join

class Thread extends Object{ pred preFork = true; pred postJoin = true; // to be extended in subclasses requires preFork; ensures true; void fork(); requires Join(this); ensures postJoin; void join(); requires preFork; ensures postJoin; void run() { null } } t.join() resumes when t terminates. Join(t): the thread in which this formula appears can get back t’s postcondition when t terminates. ë But this does not allow concurrent joiners.

14

slide-34
SLIDE 34

Fork and join in terms of resources (2)

thread t0 thread t2 t1.fork() thread t1

15

slide-35
SLIDE 35

Fork and join in terms of resources (2)

thread t0 thread t2 t1.fork() thread t1 t1.join() t1.join()

15

slide-36
SLIDE 36

Hoare rules for fork and join

class Thread extends Object{ pred preFork = true; pred postJoin<perm p> = true; requires preFork; ensures true; void fork(); requires Join(this,p); ensures postJoin<p>; void join(); requires preFork; ensures postJoin<1>; void run() { null } } ë We parameterize Join and postJoin by a permission. Join(t,p) give access to fraction p of thread t’s postcondition.

16

slide-37
SLIDE 37

Hoare rules for fork and join

class Thread extends Object{ pred preFork = true; group postJoin = true; requires preFork; ensures true; void fork(); requires Join(this,p); ensures postJoin<p>; void join(); requires preFork; ensures postJoin<1>; void run() { null } } For soundness: postJoin is a special predicate: a group. ë It satisfies ❅perm p.postJoin<p> *-* (postJoin<p④2>*postJoin<p④2>).

17

slide-38
SLIDE 38

Objective 2: reentrant locks [APLAS’08]

Reentrant locks are the main primitive to acquire/release locks in Java. They can be acquired more than once (and released accordingly) ë Convenient for programmers (no need to acquire conditionally)

18

slide-39
SLIDE 39

Objective 2: reentrant locks [APLAS’08]

Reentrant locks are the main primitive to acquire/release locks in Java. They can be acquired more than once (and released accordingly) ë Convenient for programmers (no need to acquire conditionally) class Set{ int size(){ // client and helper method } bool has(Element e){ // client method } }

18

slide-40
SLIDE 40

Objective 2: reentrant locks [APLAS’08]

Reentrant locks are the main primitive to acquire/release locks in Java. They can be acquired more than once (and released accordingly) ë Convenient for programmers (no need to acquire conditionally) class Set{ int size(){ lock(this); ... unlock(this); return ...; } bool has(Element e){ lock(this); bool result; if(size()==0) unlock(this); return false; else ...; unlock(this); return ...; } }

19

slide-41
SLIDE 41

Objective 2: reentrant locks [APLAS’08]

Reentrant locks are the main primitive to acquire/release locks in Java. They can be acquired more than once (and released accordingly) ë Convenient for programmers (no need to acquire conditionally) class Set{ int size(){ lock(this); ... unlock(this); return ...; } bool has(Element e){ lock(this); bool result; if(size()==0) unlock(this); return false; else ...; unlock(this); return ...; } }

20

slide-42
SLIDE 42

Objective 2: reentrant locks [APLAS’08]

In separation logic [O’Hearn’07]: Each lock guards a part of the heap called the lock’s resource invariant. Resource invariants are exchanged between locks and threads:

When a lock is acquired, it lends its resource invariant to the acquiring thread. When a lock is released, it claims back its resource invariant from the releasing

thread.

21

slide-43
SLIDE 43

Objective 2: reentrant locks [APLAS’08]

In separation logic [O’Hearn’07]: Each lock guards a part of the heap called the lock’s resource invariant. Resource invariants are exchanged between locks and threads:

When a lock is acquired, it lends its resource invariant to the acquiring thread. When a lock is released, it claims back its resource invariant from the releasing

thread.

Resource invariants are represented by the distinguished abstract predicate inv: class Object{ pred inv = true; }

21

slide-44
SLIDE 44

Objective 2: reentrant locks [APLAS’08]

In separation logic [O’Hearn’07]: Each lock guards a part of the heap called the lock’s resource invariant. Resource invariants are exchanged between locks and threads:

When a lock is acquired, it lends its resource invariant to the acquiring thread. When a lock is released, it claims back its resource invariant from the releasing

thread.

ë

22

slide-45
SLIDE 45

Objective 2: reentrant locks [APLAS’08]

In separation logic [O’Hearn’07]: Each lock guards a part of the heap called the lock’s resource invariant. Resource invariants are exchanged between locks and threads:

When a lock is acquired, it lends its resource invariant to the acquiring thread. When a lock is released, it claims back its resource invariant from the releasing

thread.

But this is unsound for reentrant locks! ë We need to distinguish between initial acquirements and reentrant acquirements.

22

slide-46
SLIDE 46

Separation Logic for Reentrant Locks

4 formulas to speak about locks (where S is a multiset): Lockset(S) ⑤ S contains x ⑤ x.fresh ⑤ x.initialized For each thread, we track the set of currently held locks: Lockset(S): S is the multiset of currently held locks. S contains x: lockset S contains lock x.

23

slide-47
SLIDE 47

Separation Logic for Reentrant Locks

4 formulas to speak about locks (where S is a multiset): Lockset(S) ⑤ S contains x ⑤ x.fresh ⑤ x.initialized For each thread, we track the set of currently held locks: Lockset(S): S is the multiset of currently held locks. S contains x: lockset S contains lock x. For each lock, we track its abstract lock state: x.fresh: x’s resource invariant is not initialized x.initialized: x’s resource invariant is initialized.

23

slide-48
SLIDE 48

Initializing Locks

C< ¯ π> ➔: Γ♣xq (New) Γ ✩ ttrue✉ x ✏ new C< ¯ π> tx.init * C classof x * Γ♣uq➔:Object x!=u * x.fresh✉

ë After creation a lock cannot be acquired: x.initialized fails to match (Lock)’s precondition.

✩ t ✉ t ✥ ✉

ë ë

24

slide-49
SLIDE 49

Initializing Locks

C< ¯ π> ➔: Γ♣xq (New) Γ ✩ ttrue✉ x ✏ new C< ¯ π> tx.init * C classof x * Γ♣uq➔:Object x!=u * x.fresh✉

ë After creation a lock cannot be acquired: x.initialized fails to match (Lock)’s precondition.

(Commit) Γ ✩ tLockset(S)*x.inv*x.fresh✉ x.commit tLockset(S)*✥(S contains x)*x.initialized✉

ë x.commit is a no-op. ë After being committed a lock can be acquired: (Commit)’s postcondition entails x.initialized.

24

slide-50
SLIDE 50

Acquiring Locks

(Lock) Γ ✩ tLockset(S)*(✥S contains x)*x.initialized✉ lock♣xq tLockset(x☎S)*x.inv✉

ë First acquirement: resource invariants obtained. ë Nothing special to handle subclassing.

(Re-Lock) Γ ✩ tLockset(x☎S)✉lock♣xqtLockset(x☎x☎S)✉

ë Reentrant acquirement: x’s resource invariant not obtained.

25

slide-51
SLIDE 51

Releasing Locks

The 2 rules for releasing locks are dual to the rules for acquirement. ë Hence, we do not discuss them.

26

slide-52
SLIDE 52

Objectives 1 and 2: Achievements

A sound verification system for realistic multithreaded Java programs. Usability tested against challenging case studies:

Concurrent iterator Lock coupling algorithm (still some limitations)

Algorithmic verification still to be developed ë

27

slide-53
SLIDE 53

Objectives 1 and 2: Achievements

A sound verification system for realistic multithreaded Java programs. Usability tested against challenging case studies:

Concurrent iterator Lock coupling algorithm (still some limitations)

Algorithmic verification still to be developed After that: 3 new analyses based on separation logic ë 2 of these analyses are sketched in the next slides

27

slide-54
SLIDE 54

1st Analysis: Fast Disproving of Entailment [IWACO’09]

Goal: Disprove entailment between separation logic formulas ë I.e. to prove A ✫ B ë ë ✩ ✍ ✩ ✫ ë

28

slide-55
SLIDE 55

1st Analysis: Fast Disproving of Entailment [IWACO’09]

Goal: Disprove entailment between separation logic formulas ë I.e. to prove A ✫ B Usefulness: Program verifiers spend their time checking entailment. ë I.e. given the program’s state A, and the next command’s precondition B, ë program verifiers have to find a F such that A ✩ B✍F. ✩ ✫ ë

28

slide-56
SLIDE 56

1st Analysis: Fast Disproving of Entailment [IWACO’09]

Goal: Disprove entailment between separation logic formulas ë I.e. to prove A ✫ B Usefulness: Program verifiers spend their time checking entailment. ë I.e. given the program’s state A, and the next command’s precondition B, ë program verifiers have to find a F such that A ✩ B✍F. In full separation logic, ✩ is undecidable. If we can prove that A ✫ B, then we know that F cannot be found. ë This avoids trying to prove unprovable programs.

28

slide-57
SLIDE 57

Disproving Technique

Soundness of the proof system: A ✩ B implies ♣❅h, h ⑤ ù A Ñ h ⑤ ù Bq ♣❉ ⑤ ù ❫✥ ⑤ ù q ✫ ✫

29

slide-58
SLIDE 58

Disproving Technique

Soundness of the proof system: A ✩ B implies ♣❅h, h ⑤ ù A Ñ h ⑤ ù Bq Contraposition: ♣❉h,h ⑤ ù A❫✥h ⑤ ù Bq implies A ✫ B Goal of this work: Take A and B and prove that A ✫ B By discriminating models of A and B

29

slide-59
SLIDE 59

Disproving Technique (classical semantics)

Objective: Find h such that h ⑤ ù A and ✥h ⑤ ù B We compute bounds on the size of models. max : Formula Ñ Size min : Formula Ñ Size size : Model Ñ Size

max(A) size(h) min(A)

❅ ⑤ ù ♣ q ↕ ♣ q ↕ ♣ q

30

slide-60
SLIDE 60

Disproving Technique (classical semantics)

Objective: Find h such that h ⑤ ù A and ✥h ⑤ ù B We compute bounds on the size of models. max : Formula Ñ Size min : Formula Ñ Size size : Model Ñ Size

max(A) size(h) min(A)

Properties of max and min (classical semantics): ❅h,h ⑤ ù A implies min♣Aq ↕ size♣hq ↕ max♣Aq

30

slide-61
SLIDE 61

Disproving Technique (classical semantics)

♣❉h,h ⑤ ù A❫✥h ⑤ ù Bq implies A ✫ B ❅h,h ⑤ ù A implies min♣Aq ↕ size♣hq ↕ max♣Aq Ó max♣Aq ➔ min♣Bq implies A ✫ B

31

slide-62
SLIDE 62

1st Analysis: Achievements

A fast technique to disprove entailment. Two different trade offs between speed and precision

32

slide-63
SLIDE 63

1st Analysis: Achievements

A fast technique to disprove entailment. Two different trade offs between speed and precision (two ways to define Size)

32

slide-64
SLIDE 64

1st Analysis: Achievements

A fast technique to disprove entailment. Two different trade offs between speed and precision (two ways to define Size) Proven correct in Coq License-left proof scripts

32

slide-65
SLIDE 65

2nd Analysis: Optimizations by Proof Rewriting [SAS’09]

We parallelize and optimize proven programs. To parallelize programs, you need to know: What data is accessed by programs. What data is not accessed by programs.

33

slide-66
SLIDE 66

2nd Analysis: Optimizations by Proof Rewriting [SAS’09]

We parallelize and optimize proven programs. To parallelize programs, you need to know: What data is accessed by programs. What data is not accessed by programs. The good thing is: Separation logic proofs exhibit how data is accessed (or not):

Antiframes exhibit data that is accessed.

(explained next)

The (Frame) rule exhibits data that is not accessed.

(explained next)

33

slide-67
SLIDE 67

2nd Analysis: Optimizations by Proof Rewriting [SAS’09]

We parallelize and optimize proven programs. To parallelize programs, you need to know: What data is accessed by programs. What data is not accessed by programs. The good thing is: Separation logic proofs exhibit how data is accessed (or not):

Antiframes exhibit data that is accessed.

(explained next)

The (Frame) rule exhibits data that is not accessed.

(explained next)

Optimizations are expressed with a rewrite system between proof trees. Proof trees are derivations of Hoare triplets.

33

slide-68
SLIDE 68

High-Level Procedure

program verifier

program C

proof tree generator

C correct C wrong

proof tree rewriter

C, P (P is C’s proof) Copt, Popt (Copt is C parallelized and optimized) contributions

34

slide-69
SLIDE 69

Separation Logic: (Frame) rule tΞa✉CtΞa✶✉ (Frame Ξf) tΞa ✍Ξf✉CtΞa✶ ✍Ξf✉

Ξf Ξa Ξf Ξa′ C

Ξa is the antiframe Ð accessed data Ξf is the frame Ð not-accessed data Later, (Fr) sometimes abbreviates (Frame).

35

slide-70
SLIDE 70

With Frames: Parallelization Is Easy tΞ✉CtΘ✉

(Fr Ξ✶)

tΞ✍Ξ✶✉CtΘ✍Ξ✶✉ tΞ✶✉C✶tΘ✶✉

(Fr Θ)

tΘ✍Ξ✶✉C✶tΘ✍Θ✶✉ (Seq) tΞ✍Ξ✶✉C; C✶tΘ✍Θ✶✉ Ó Parallelize tΞ✉CtΘ✉ tΞ✶✉C✶tΘ✶✉ (Parallel) tΞ✍Ξ✶✉C ⑥ C✶tΘ✍Θ✶✉

36

slide-71
SLIDE 71

Parallelize’s left hand side tΞ✉CtΘ✉

(Fr Ξ✶)

tΞ✍Ξ✶✉CtΘ✍Ξ✶✉ tΞ✶✉C✶tΘ✶✉

(Fr Θ)

tΘ✍Ξ✶✉C✶tΘ✍Θ✶✉ (Seq) tΞ✍Ξ✶✉C; C✶tΘ✍Θ✶✉

Ξ′ Ξ Ξ′ Θ C

37

slide-72
SLIDE 72

Parallelize’s left hand side tΞ✉CtΘ✉

(Fr Ξ✶)

tΞ✍Ξ✶✉CtΘ✍Ξ✶✉ tΞ✶✉C✶tΘ✶✉

(Fr Θ)

tΘ✍Ξ✶✉C✶tΘ✍Θ✶✉ (Seq) tΞ✍Ξ✶✉C; C✶tΘ✍Θ✶✉

Ξ′ Ξ Ξ′ Θ C Θ′ C′ Θ

37

slide-73
SLIDE 73

Parallelize’s right hand side tΞ✉CtΘ✉ tΞ✶✉C✶tΘ✶✉ (Parallel) tΞ✍Ξ✶✉C ⑥ C✶tΘ✍Θ✶✉

Ξ′ Ξ Θ′ C′ Θ C

38

slide-74
SLIDE 74

Parallelize

tΞ✉CtΘ✉ (Fr Ξ✶) tΞ✍Ξ✶✉CtΘ✍Ξ✶✉ tΞ✶✉C✶tΘ✶✉ (Fr Θ) tΘ✍Ξ✶✉C✶tΘ✍Θ✶✉ (Seq) tΞ✍Ξ✶✉C; C✶tΘ✍Θ✶✉ Ó Parallelize tΞ✉CtΘ✉ tΞ✶✉C✶tΘ✶✉ (Parallel) tΞ✍Ξ✶✉C ⑥ C✶tΘ✍Θ✶✉

Ξ′ Ξ Ξ′ Θ C Θ′ C′ Θ

Ó

Ξ′ Ξ Θ′ C′ Θ C

39

slide-75
SLIDE 75

2nd Analysis: Achievements

An entirely new technique to parallelize and optimize programs.

ë Other optimizations than parallelization have been studied.

No ad-hoc analyses: separation logic proofs are taken as analyses. Can parallelize any code (i.e. not focused on loops). Soundness is easier to prove than for classical approaches. License-left prototype implementation.

40

slide-76
SLIDE 76

Related Work

Program verification: Separation logic for sequential Java [Parkinson’05,Distefano et al.’08,Chin et al.’08] Separation logic for multithreaded C [Gotsman et al.’07,Appel et al.’07] Boogie for multithreaded C# [Barnett et al.’04,Jacobs et al.’06] ESC/Java2 for Java [Leino et al.’02,Kiniry et al.’04]

41

slide-77
SLIDE 77

Related Work

Program verification: Separation logic for sequential Java [Parkinson’05,Distefano et al.’08,Chin et al.’08] Separation logic for multithreaded C [Gotsman et al.’07,Appel et al.’07] Boogie for multithreaded C# [Barnett et al.’04,Jacobs et al.’06] ESC/Java2 for Java [Leino et al.’02,Kiniry et al.’04] Algorithms for entailment/disproving: Sound and complete entailment in Smallfoot [Berdine et al.’04] Sound entailment in JStar [Parkinson et al.’08] Sound and complete entailment and refutation [Galmiche et al.’08]

41

slide-78
SLIDE 78

Related Work

Program verification: Separation logic for sequential Java [Parkinson’05,Distefano et al.’08,Chin et al.’08] Separation logic for multithreaded C [Gotsman et al.’07,Appel et al.’07] Boogie for multithreaded C# [Barnett et al.’04,Jacobs et al.’06] ESC/Java2 for Java [Leino et al.’02,Kiniry et al.’04] Algorithms for entailment/disproving: Sound and complete entailment in Smallfoot [Berdine et al.’04] Sound entailment in JStar [Parkinson et al.’08] Sound and complete entailment and refutation [Galmiche et al.’08] Automatic parallelization: Many “classical” approaches By using separation logic [Raza et al.’09]

41

slide-79
SLIDE 79

Main Publications

Separation Logic Contracts for a Java-like Language with Fork/Join; Haack and Hurlin; AMAST’08

Reasoning about Java’s Reentrant Locks; Haack, Huisman, and Hurlin;

APLAS’08

Specifying and Checking Protocols of Multithreaded Classes; Hurlin; SAC’09

Resource Usage Protocols for Iterators; Haack and Hurlin; Journal of Object Technology’09

Size Does Matter: Two Certified Abstractions to Disprove Entailment in

Intuitionistic and Classical Separation Logic; Hurlin, Bobot, and Summers; IWACO’09

Automatic Parallelization and Optimization of Programs by Proof Rewriting;

Hurlin; SAS’09

42

slide-80
SLIDE 80

Main Publications

Separation Logic Contracts for a Java-like Language with Fork/Join; Haack and Hurlin; AMAST’08

Reasoning about Java’s Reentrant Locks; Haack, Huisman, and Hurlin;

APLAS’08

Specifying and Checking Protocols of Multithreaded Classes; Hurlin; SAC’09

Resource Usage Protocols for Iterators; Haack and Hurlin; Journal of Object Technology’09

Size Does Matter: Two Certified Abstractions to Disprove Entailment in

Intuitionistic and Classical Separation Logic; Hurlin, Bobot, and Summers; IWACO’09

Automatic Parallelization and Optimization of Programs by Proof Rewriting;

Hurlin; SAS’09 Developments:

1 Some Coq proofs for the AMAST and APLAS papers. 2 ocaml implementation of some of the techniques described in the SAC paper. 3 Full Coq proofs for the IWACO paper. 4 ocaml and Java+tom prototype implementation of the SAS paper.

42

slide-81
SLIDE 81

Conclusion

First, we developed: A sound verification system for multithreaded Java programs in separation logic, ë ë

43

slide-82
SLIDE 82

Conclusion

First, we developed: A sound verification system for multithreaded Java programs in separation logic, ë that uses realistic primitives, ë

43

slide-83
SLIDE 83

Conclusion

First, we developed: A sound verification system for multithreaded Java programs in separation logic, ë that uses realistic primitives, ë and that handles challenging examples (iterator, lock-coupling).

43

slide-84
SLIDE 84

Conclusion

First, we developed: A sound verification system for multithreaded Java programs in separation logic, ë that uses realistic primitives, ë and that handles challenging examples (iterator, lock-coupling). Second: We extended previous work on protocols. (omitted in this talk) We discovered a fast algorithm to disprove entailment. We showed how to parallelize and optimize programs by rewriting their proofs.

43

slide-85
SLIDE 85

Future Work

For the verification system: Implementing it! Doing a large case study For the disproving algorithm: Extension to object-orientation ë By keeping its simplicity and its usefulness (not straightforward) For the parallelizing analysis: Extension to object-oriented programs (easy) Extension to loops Ñ to battle it out with classical parallelizers!

44

slide-86
SLIDE 86

Thank you

slide-87
SLIDE 87

Fork and join in terms of resources (2)

thread t0

46

slide-88
SLIDE 88

Fork and join in terms of resources (2)

thread t0 t1.fork() thread t1

46

slide-89
SLIDE 89

Fork and join in terms of resources (2)

thread t0 t1.fork() thread t1

46

slide-90
SLIDE 90

Fork and join in terms of resources (2)

thread t0 t1.fork() thread t1 t2.start() thread t2

46

slide-91
SLIDE 91

Releasing Locks

(Re-Unlock) Γ ✩ tLockset(x☎x☎S)✉unlock♣xqtLockset(x☎S)✉

ë Releasing x but x’s reentrancy level → 1: invariant not abandoned.

(Unlock) Γ ✩ tLockset(x☎S)*x.inv✉unlock♣xqtLockset(S)✉

ë x’s reentrancy level not known to be → 1, x’s resource invariant abandoned.

47

slide-92
SLIDE 92

Disproving Technique (classical semantics)

♣❉h,h ⑤ ù A❫✥h ⑤ ù Bq implies A ✫ B ❅h,h ⑤ ù A implies min♣Aq ↕ size♣hq ↕ max♣Aq Ó max♣Aq ➔ min♣Bq implies A ✫ B

48

slide-93
SLIDE 93

Defining size

size♣hq

✏ sum of h’s permissions size: Model Ñ Perm ë ♣ q ♣

✶ ✁ q

q rs ♣♣ q ♣

✶ ✁ q

q rsq ✏

49

slide-94
SLIDE 94

Defining size

size♣hq

✏ sum of h’s permissions size: Model Ñ Perm Models h are lists of triples of an address, a permission, and a value. ë An example model is ♣42,π,3q :: ♣47,π✶,✁5q :: ♣42,π✷,0q :: rs. size♣♣42,π,3q :: ♣47,π✶,✁5q :: ♣42,π✷,0q :: rsq ✏ π π✶ π✷

49

slide-95
SLIDE 95

Defining max/min (classical semantics)(excerpt)

max♣

π

ÞÑ q✏π max♣A✍Bq ✏max♣Aqπ max♣Bq min♣

π

ÞÑ q✏π min♣A✍Bq ✏min♣Aqπ min♣Bq h ⑤ ù a π ÞÑ v iff h ✏ ♣a,π,vq h ⑤ ù A✍B iff ❉hA,hB,h ✏ hA❩hB,hA ⑤ ù A and hB ⑤ ù B

50

slide-96
SLIDE 96

Defining max/min (classical semantics)(excerpt)

max♣A❫Bq✏minπ ♣max♣Aq,max♣Bqq max♣A❴Bq✏maxπ♣max♣Aq,max♣Bqq min♣A❫Bq✏maxπ♣min♣Aq,min♣Bqq min♣A❴Bq✏minπ ♣min♣Aq,min♣Bqq

h ⑤ ù A❫B iff h ⑤ ù A and h ⑤ ù B h ⑤ ù A❴B iff h ⑤ ù A or h ⑤ ù B

51

slide-97
SLIDE 97

Defining max/min (classical semantics)(excerpt)

max♣

π

ÞÑ q✏π max♣A✍Bq ✏max♣Aqπ max♣Bq min♣

π

ÞÑ q✏π min♣A✍Bq ✏min♣Aqπ min♣Bq h ⑤ ù a π ÞÑ v iff h ✏ ♣a,π,vq h ⑤ ù A✍B iff ❉hA,hB,h ✏ hA❩hB,hA ⑤ ù A and hB ⑤ ù B

max(A) size(A) max(B) size(B)

52

slide-98
SLIDE 98

Defining max/min (classical semantics)(excerpt)

max♣

π

ÞÑ q✏π max♣A✍Bq ✏max♣Aqπ max♣Bq ♣ q min♣

π

ÞÑ q✏π min♣A✍Bq ✏min♣Aqπ min♣Bq h ⑤ ù a π ÞÑ v iff h ✏ ♣a,π,vq h ⑤ ù A✍B iff ❉hA,hB,h ✏ hA ❩hB,hA ⑤ ù A and hB ⑤ ù B

max(A) size(A) max(B) size(B)

53

slide-99
SLIDE 99

Defining max/min (classical semantics)(excerpt)

max♣

π

ÞÑ q✏π max♣A✍Bq ✏max♣Aqπ max♣Bq min♣

π

ÞÑ q✏π min♣A✍Bq ✏min♣Aqπ min♣Bq h ⑤ ù a π ÞÑ v iff h ✏ ♣a,π,vq h ⑤ ù A✍B iff ❉hA,hB,h ✏ hA ❩hB,hA ⑤ ù A and hB ⑤ ù B

max(A) size(A) max(B) size(B)

53

slide-100
SLIDE 100

Defining max/min (classical semantics)(excerpt)

max♣

π

ÞÑ q✏π max♣A✍Bq ✏max♣Aqπ max♣Bq min♣

π

ÞÑ q✏π min♣A✍Bq ✏min♣Aqπ min♣Bq h ⑤ ù a π ÞÑ v iff h ✏ ♣a,π,vq h ⑤ ù A✍B iff ❉hA,hB,h ✏ hA ❩hB,hA ⑤ ù A and hB ⑤ ù B

max(A ⋆ B)

54

slide-101
SLIDE 101

Defining max/min (classical semantics)

max♣

π

ÞÑ q ✏π max♣A✍Bq ✏max♣Aqπ max♣Bq max♣A -* Bq✏max♣Bq✁π min♣Aq min♣

π

ÞÑ q ✏π min♣A✍Bq ✏min♣Aqπ min♣Bq min♣A -* Bq✏min♣Bq✁π max♣Aq h ⑤ ù a π ÞÑ v iff h ✏ ♣a,π,vq h ⑤ ù A✍B iff ❉hA,hB,h ✏ hA ❩hB,hA ⑤ ù A and hB ⑤ ù B h ⑤ ù A -* B iff ❅hA,hA ⑤ ù A and hA and h are compatible implies hA ❩h ⑤ ù A✍B

55

slide-102
SLIDE 102

Defining max/min (classical semantics)

max♣❅π.Aq✏max♣Arπw④πsq max♣❉π.Aq✏max♣Arπw④πsq max♣❅v.Aq ✏ max♣❉v.Aq ✏ max♣Aq min♣❅π.Aq✏min♣Arπw④πsq min♣❉π.Aq✏min♣Arε④πsq min♣❅v.Aq ✏ min♣❉v.Aq ✏ min♣Aq

ë Standard semantics of quantifiers (omitted) ε is an infinitely small permission

56

slide-103
SLIDE 103

Defining max/min (classical semantics)

max♣bq✏✽ min♣bq✏π0

h ⑤ ù b iff

  • racle♣bq

b is a pure formula, i.e., it does not depend on the heap h. π0 is the minimal permission.

57

slide-104
SLIDE 104

Toy Example

Λn,m,k

x,y,z

✏ xÞÑrf : ns✍yÞÑrf : ms✍zÞÑrf : ks

(Mutate) tΛx✉xÑf ✏ ntΛn

x✉

(Fr Λ ,

y,z)

tΛ , ,

x,y,z✉xÑf ✏ ntΛn, , x,y,z✉

(Mutate) tΛy✉yÑf ✏ mtΛm

y ✉

(Fr Λn,

x,z)

tΛn, ,

x,y,z✉yÑf ✏ mtΛn,m, x,y,z ✉

(Mutate) tΛz✉zÑf ✏ ktΛk

z✉

(Fr Λn,m

x,y )

tΛn,m,

x,y,z ✉zÑf ✏ ktΛn,m,k x,y,z ✉

(Seq) tΛn, ,

x,y,z✉yÑf ✏ m; zÑf ✏ ktΛn,m,k x,y,z ✉

(Seq) tΛ , ,

x,y,z✉xÑf ✏ n; yÑf ✏ m; zÑf ✏ ktΛn,m,k x,y,z ✉

Ó (Mutate) tΛx✉xÑf ✏ ntΛn

x✉

(Mutate) tΛy✉yÑf ✏ mtΛm

y ✉

(Mutate) tΛz✉zÑf ✏ ktΛk

z✉

(Parallel) tΛ ,

y,z✉yÑf ✏ m ⑥ zÑf ✏ ktΛm,k y,z ✉

(Parallel) tΛ , ,

x,y,z✉xÑf ✏ n ⑥ ♣yÑf ✏ m ⑥ zÑf ✏ kqtΛn,m,k x,y,z ✉

58

slide-105
SLIDE 105

Toy Example

Λn,m,k

x,y,z

✏ xÞÑrf : ns✍yÞÑrf : ms✍zÞÑrf : ks

(Mutate) tΛx✉xÑf ✏ ntΛn

x✉

(Fr Λ ,

y,z)

tΛ , ,

x,y,z✉xÑf ✏ ntΛn, , x,y,z✉

(Mutate) tΛy✉yÑf ✏ mtΛm

y ✉

(Fr Λn,

x,z)

tΛn, ,

x,y,z✉yÑf ✏ mtΛn,m, x,y,z ✉

(Mutate) tΛz✉zÑf ✏ ktΛk

z✉

(Fr Λn,m

x,y )

tΛn,m,

x,y,z ✉zÑf ✏ ktΛn,m,k x,y,z ✉

(Seq) tΛn, ,

x,y,z✉yÑf ✏ m; zÑf ✏ ktΛn,m,k x,y,z ✉

(Seq) tΛ , ,

x,y,z✉xÑf ✏ n; yÑf ✏ m; zÑf ✏ ktΛn,m,k x,y,z ✉

Ó (Mutate) tΛx✉xÑf ✏ ntΛn

x✉

(Mutate) tΛy✉yÑf ✏ mtΛm

y ✉

(Mutate) tΛz✉zÑf ✏ ktΛk

z✉

(Parallel) tΛ ,

y,z✉yÑf ✏ m ⑥ zÑf ✏ ktΛm,k y,z ✉

(Parallel) tΛ , ,

x,y,z✉xÑf ✏ n ⑥ ♣yÑf ✏ m ⑥ zÑf ✏ kqtΛn,m,k x,y,z ✉

Hypothesis: the left hand side is a valid proof tree. Soundness follows from the inclusion of the rhs’s leaves in the lhs’s leaves.

58

slide-106
SLIDE 106

Guarantees of the Rewrite System

The rewrite system modifies programs but preserves specifications:

P tΞ✉CtΘ✉ Ó Q tΞ✉C✶tΘ✉

The program and the proof are modified: P,C Ñ Q,C✶, ë but specifications are preserved: Ξ,Θ Ñ Ξ,Θ. Conjecture: Programs related with Ñ are equivalent from a big step p.o.v.

59

slide-107
SLIDE 107

We Want the Frames

The (Frame) rule is the central ingredient of our procedure. Problem: Existing program verifiers (e.g. smallfoot) do not make frames explicit. Π ✩ F ✏ E ... (Mutate) tΠ ¦ FÞÑrρs✉EÑf ✏ GtΠ ¦ FÞÑrρ✶s✉ ë Π is “too big”: there exists a “smaller” antiframe Πa such that Πa ✩ F ✏ E.

60

slide-108
SLIDE 108

Recall the big picture ?

program verifier

program C

proof tree generator

C correct C wrong

proof tree rewriter

C, P (P is C’s proof) Copt, Popt (Copt is C parallelized and optimized)

In the proof tree generator: Proof rules with explicit frames. But still usage of the program’s verifier normal rules for verification. Next slides: Proof rules with explicit {anti-,} frames

61

slide-109
SLIDE 109

We Want the Frames

Berdine, Calcagno, and O’Hearn “Symbolic Execution with Separation Logic”

Π ✩ F ✏ E ... (Mutate) tΠ ¦ FÞÑrρs✉EÑf ✏ GtΠ ¦ FÞÑrρ✶s✉

With explicit frames and antiframes

Πa ✩ F ✏ E ... (Mutate) tΠa ¦ FÞÑrρs✉EÑf ✏ GtΠa ¦ FÞÑrρ✶s✉ (Frame Ξf ) t♣Πa ¦ FÞÑrρsq✍Ξf ✉EÑf ✏ Gt♣Πa ¦ FÞÑrρ✶sq✍Ξf ✉

62

slide-110
SLIDE 110

We Want the Frames

Berdine, Calcagno, and O’Hearn “Symbolic Execution with Separation Logic”

... x✶ fresh Π ✩ F ✏ E (Lookup) tΠ ¦ Σ✍FÞÑrρs✉x :✏ EÑft...❫Πrx✶④xs ¦ ♣Σ✍FÞÑrρsqrx✶④xs✉

With explicit frames and antiframes

x✶ fresh ... Πa ✩ F ✏ E Ξ ✏ Πarx✶④xs❫... ¦ ♣Σa ✍FÞÑrρsqrx✶④xs (Lookup) tΠa ¦ Σa ✍FÞÑrρs✉x :✏ EÑftΞ✉ x ❘ Ξf (Frame Ξf ) t ♣Πa ¦ Σa ✍FÞÑrρsq

❧♦♦♦♦♦♦♦♦♦♠♦♦♦♦♦♦♦♦♦♥

antiframe needed to prove EÞÑrρs and affected by [x’/x] ✍ Ξf

❧♦ ♦♠♦ ♦♥

frame unaffected by [x’/x] ✉x :✏ EÑftΞ✍Ξf ✉

63

slide-111
SLIDE 111

We Want the Frames

Berdine, Calcagno, and O’Hearn “Symbolic Execution with Separation Logic”

Π ✩ ❑ (Inconsistent) tΠ ¦ Σ✉CtΘ✉

With explicit frames and antiframes

Πa ✩ ❑ (Inconsistent) tΠa ¦ emp✉CtΘ✉ (Frame Πf ¦ Σf ) t Πa ¦ emp

❧♦♦♠♦♦♥

sufficient antiframe to prove ❑

✍ Πf ¦ Σf

❧♦ ♦♠♦ ♦♥

frame

✉CtΘ✉

emp

✏ the heap is empty.

64

slide-112
SLIDE 112

Shape of Generated Trees

Because of the algorithm used in program verifiers, ë proof trees have a special shape. For successive commands C0,C1,C2,..., proof trees have this shape:

..

(Fr Ξ)

t..✉C0t..✉ ..

(Fr Θ)

t..✉C1t..✉ ..

(Fr Ξ✶)

t..✉C2t..✉ .. t..✉C2; ..t..✉

(Seq)

t..✉C1; C2; ..t..✉

(Seq)

t..✉C0; C1; C2; ..t..✉

A (Frame) at each command. ë Problem: Frames are redundant (i.e., Ξ❨Θ❨Ξ✶ ✘ ❍). ë In practice, this must be avoided for optimizations to fire.

65

slide-113
SLIDE 113

We Want the Frames

We rewrite proof trees to frame multiple commands, ë i.e., we factorize frames. Below, Ξc is the factorized frame. Guard: Ξf ô Ξf0 ✍Ξc and Θf ô Θf0 ✍Ξc tΞa✉CtΞp✉ (Frame Ξf ) tΞa ✍Ξf ✉CtΞp ✍Ξf ✉ tΘa✉C✶tΘp✉ (Frame Θf ) tΘa ✍Θf ✉C✶tΘp ✍Θf ✉ (Seq) tΞa ✍Ξf ✉C; C✶tΘp ✍Θf ✉ Ó FactorizeFrames tΞa✉CtΞp✉ (Fr Ξf0) tΞa ✍Ξf0✉CtΞp ✍Ξf0✉ tΘa✉C✶tΘp✉ (Fr Θf0) tΘa ✍Θf0✉C✶tΘp ✍Θf0✉ (Seq) tΞa ✍Ξf0✉C; C✶tΘp ✍Θf0✉ (Frame Ξc) tΞa ✍Ξf ✉C; C✶tΘp ✍Θf ✉

66

slide-114
SLIDE 114

FactorizeFrames’s left hand side

tΞa✉CtΞp✉ (Fr Ξf) tΞa ✍Ξf✉CtΞp ✍Ξf✉ tΘa✉C✶tΘp✉ (Fr Θf) tΘa ✍Θf✉C✶tΘp ✍Θf✉ (Seq) tΞa ✍Ξf✉C; C✶tΘp ✍Θf✉

Ξf Ξa Ξf Ξp C

ô ✍ ô ✍

67

slide-115
SLIDE 115

FactorizeFrames’s left hand side

tΞa✉CtΞp✉ (Fr Ξf) tΞa ✍Ξf✉CtΞp ✍Ξf✉ tΘa✉C✶tΘp✉ (Fr Θf) tΘa ✍Θf✉C✶tΘp ✍Θf✉ (Seq) tΞa ✍Ξf✉C; C✶tΘp ✍Θf✉

Ξf Ξa Ξf Ξp C Θf Θa Θf Θp C′

ô ✍ ô ✍

67

slide-116
SLIDE 116

FactorizeFrames’s left hand side

tΞa✉CtΞp✉ (Fr Ξf) tΞa ✍Ξf✉CtΞp ✍Ξf✉ tΘa✉C✶tΘp✉ (Fr Θf) tΘa ✍Θf✉C✶tΘp ✍Θf✉ (Seq) tΞa ✍Ξf✉C; C✶tΘp ✍Θf✉

Ξf Ξa Ξf Ξp C Θf Θa Θf Θp C′

Guard: Ξf ô Ξf0 ✍Ξc and Θf ô Θf0 ✍Ξc The part of the heap framed twice (in red) is the common frame Ξc.

67

slide-117
SLIDE 117

We Want the Frames

Guard: Ξf ô Ξf0 ✍Ξc and Θf ô Θf0 ✍Ξc tΞa✉CtΞp✉ (Frame Ξf ) tΞa ✍Ξf ✉CtΞp ✍Ξf ✉ tΘa✉C✶tΘp✉ (Frame Θf ) tΘa ✍Θf ✉C✶tΘp ✍Θf ✉ (Seq) tΞa ✍Ξf ✉C; C✶tΘp ✍Θf ✉ Ó FactorizeFrames tΞa✉CtΞp✉ (Fr Ξf0) tΞa ✍Ξf0✉CtΞp ✍Ξf0✉ tΘa✉C✶tΘp✉ (Fr Θf0) tΘa ✍Θf0✉C✶tΘp ✍Θf0✉ (Seq) tΞa ✍Ξf0✉C; C✶tΘp ✍Θf0✉ (Frame Ξc) tΞa ✍Ξf ✉C; C✶tΘp ✍Θf ✉

ë The common part of Ξf and Θf (i.e., Ξc) is framed separately. ë The new application of (Frame) is on a longer command (C; C✶) than before.

68

slide-118
SLIDE 118

Example

requires tree♣xq; ensures tree♣xq; rotate tree♣x;q{ local x1,x2; if♣x ✏ nilq{} else{ x1 :✏ xÑl; x2 :✏ xÑr; xÑl ✏ x2; xÑr ✏ x1; rotate tree♣x1;q; rotate tree♣x2;q; }} Ñ requires tree♣xq; ensures tree♣xq; rotate tree♣x;q{ local x1,x2; if♣x ✏ nilq{} else{ x1 :✏ xÑl; x2 :✏ xÑr; ♣xÑl ✏ x2; xÑr ✏ x1q ⑤⑤ rotate tree♣x1;q ⑤⑤ rotate tree♣x2;q; }}

69

slide-119
SLIDE 119

Example

requires tree♣xq; ensures tree♣xq; rotate tree♣x;q{ local x1,x2; if♣x ✏ nilq{} else{ x1 :✏ xÑl; x2 :✏ xÑr; xÑl ✏ x2; xÑr ✏ x1; rotate tree♣x1;q; rotate tree♣x2;q; }} Ñ requires tree♣xq; ensures tree♣xq; rotate tree♣x;q{ local x1,x2; if♣x ✏ nilq{} else{ x1 :✏ xÑl; x2 :✏ xÑr; ♣xÑl ✏ x2; xÑr ✏ x1q ⑤⑤ rotate tree♣x1;q ⑤⑤ rotate tree♣x2;q; }} Implementation: The rewrite rules have been implemented in Java+tom. tom extends Java to pattern match against tom/user-defined Java objects. Each rewrite rule is less than 75 lines of code (i.e. manageable). Use of tom’s strategies to fine tune optimizations.

69

slide-120
SLIDE 120

List of Optimizations

Optimizations include: parallelization (previous slides) Improvement of temporal locality (omitted in this talk) A generic optimization with 4 concrete applications: (omitted in this talk)

Early lock releasing Late lock acquirement Early disposal Late allocation

70

slide-121
SLIDE 121

GenericOptimization

tΞa✉CtΞp✉ (Frame Ξf ) tΞa ✍Ξf ✉CtΞp ✍Ξf ✉ tΘa✉C✶tΘp✉ (Frame Θf ) tΘa ✍Θf ✉C✶tΘp ✍Θf ✉ (Seq) tΞa ✍Ξf ✉C; C✶tΞ✶✉ Ó GenericOptimization tΘa✉C✶tΘp✉ (Fr ...) tΞa ✍Θa ✍Ξr✉C✶tΞa ✍Θp ✍Ξr✉ tΞa✉CtΞp✉ (Fr ...) t...✉CtΞp ✍Θp ✍Ξr✉ (Seq) tΞa ✍Ξf ✉C✶; CtΞ✶✉ Guard: Θf ô Ξp ✍Ξr This optimization changes the program order. ë The guard requires that C✶ frames the postcondition of C (Ξp).

71

slide-122
SLIDE 122

Locks in Separation Logic

Each lock guards a part of the heap called the lock’s resource invariant. Resource invariants are exchanged between locks and threads:

1 When a lock is acquired, it lends its resource invariant to the acquiring thread. 2 When a lock is released, it claims back its resource invariant from the releasing

thread.

t ✉ ♣ qt ✉ t ✉ ♣ qt ✉

ë

72

slide-123
SLIDE 123

Locks in Separation Logic

Each lock guards a part of the heap called the lock’s resource invariant. Resource invariants are exchanged between locks and threads:

1 When a lock is acquired, it lends its resource invariant to the acquiring thread. 2 When a lock is released, it claims back its resource invariant from the releasing

thread.

Formally:

Ξ is x’s resource invariant (Lock) temp✉lock♣xqtΞ✉ Ξ is x’s resource invariant (Unlock) tΞ✉unlock♣xqtemp✉ (emp represents the empty heap)

ë

72

slide-124
SLIDE 124

Locks in Separation Logic

Each lock guards a part of the heap called the lock’s resource invariant. Resource invariants are exchanged between locks and threads:

1 When a lock is acquired, it lends its resource invariant to the acquiring thread. 2 When a lock is released, it claims back its resource invariant from the releasing

thread.

Formally:

Ξ is x’s resource invariant (Lock) temp✉lock♣xqtΞ✉ Ξ is x’s resource invariant (Unlock) tΞ✉unlock♣xqtemp✉ (emp represents the empty heap)

Next slide: ë Instantiation of the generic optimization to optimize usage of locks

72

slide-125
SLIDE 125

Instantiating GenericOpt.: EarlyUnlocking

tΞa✉CtΞp✉

(Fr Ξf )

tΞa ✍Ξf ✉CtΞp ✍Ξf ✉ Θa is x’s resource invariant (Unlock) tΘa✉unlock♣xqtemp✉

(Fr Θf )

tΘa ✍Θf ✉unlock♣xqtΘf ✉

(Seq)

tΞa ✍Ξf ✉C; unlock♣xqtΘf ✉ Ó EarlyUnlocking Θa is x’s resource invariant (Unlock) tΘa✉unlock♣xqtemp✉

(Fr ...)

tΞa ✍Θa ✍Ξr✉unlock♣xqtΞa ✍Ξr✉ tΞa✉CtΞp✉

(Fr Ξr)

tΞa ✍Ξr✉CtΞp ✍Ξr✉

(Seq)

tΞa ✍Ξf ✉unlock♣xq; CtΞp ✍Ξr✉ Guard: Θf ô Ξp ✍Ξr

(Ξp ✍Ξf ô Θa ✍Θf + guard) implies Ξf ô Θa ✍Ξr. ë Command C does not access x’s resource invariant: Better unlock x before executing C!

73

slide-126
SLIDE 126

Example (2)

requires x ÞÑrval : s; ensures emp; copy and dispose♣x;q{ local v; lock♣rcÞÑrval: sq; v :✏ xÑval; cÑval ✏ v; dispose♣xq; unlock♣rcÞÑrval: sq; } Ñ requires x ÞÑrval : s; ensures emp; copy and dispose♣x;q{ local v; v :✏ xÑval; dispose♣xq; lock♣rcÞÑrval: sq; cÑval ✏ v; unlock♣rcÞÑrval: sq; } r is a lock with resource invariant c ÞÑrval : s, i.e., one cell c with field val. Optimizations: The critical region is shortened. Memory is disposed as soon as possible.

74

slide-127
SLIDE 127

TemporalLocality

temporal locality

✏ time between two accesses to the same heap cell ë the smaller the better (no need to free/load processors’s caches)

75

slide-128
SLIDE 128

TemporalLocality

Intuition below: C and C✷ access the same part of the heap ë Execute them successively

tΞ✉CtΞ✶✉ (Fr Θ) tΞ✍Θ✉CtΞ✶ ✍Θ✉ tΘ✉C✶tΘ✶✉ (Fr Ξ✶) tΞ✶ ✍Θ✉C✶tΞ✶ ✍Θ✶✉ (Seq) tΞ✍Θ✉C; C✶tΞ✶ ✍Θ✶✉ tΞ✶✉C✷tΞ✷✉ (Fr Θ✶) tΞ✶ ✍Θ✶✉C✷tΞ✷ ✍Θ✶✉ (Seq) tΞ✍Θ✉C; C✶; C✷tΞ✶✶ ✍Θ✶✉ Ó TemporalLocality tΞ✉CtΞ✶✉ tΞ✶✉C✷tΞ✷✉ (Seq) tΞ✉C; C✷tΞ✷✉ (Fr Θ) tΞ✍Θ✉C; C✷tΞ✶✶ ✍Θ✉ tΘ✉C✶tΘ✶✉ (Fr Ξ✷) tΞ✷ ✍Θ✉C✶tΞ✷ ✍Θ✶✉ (Seq) tΞ✍Θ✉C; C✷; C✶tΞ✷ ✍Θ✶✉

76

slide-129
SLIDE 129

Instantiating GenericOpt.: LateLocking

Ξp is x’s resource invariant (Lock) temp✉lock♣xqtΞp✉ (Frame Ξf ) tΞf ✉lock♣xqtΞp ✍Ξf ✉ tΘa✉C✶tΘp✉ (Frame Θf ) tΘa ✍Θf ✉C✶tΘp ✍Θf ✉ (Seq) tΞf ✉lock♣xq; C✶tΘp ✍Θf ✉ Ó LateLocking tΘa✉C✶tΘp✉ (Frame Ξr) tΘa ✍Ξr✉C✶tΘp ✍Ξr✉ Ξp is x’s resource invariant (Lock) temp✉lock♣xqtΞp✉ (Frame Θp ✍Ξr) tΘp ✍Ξr✉lock♣xqtΞp ✍Θp ✍Ξr✉ (Seq) tΞf ✉C✶; lock♣xqtΞp ✍Θp ✍Ξr✉ Guard: Θf ô Ξp ✍Ξr

ë The guard means that command C✶ does not access x’s resource invariant: Better lock x after executing C✶!

77

slide-130
SLIDE 130

Example (3)

requires tree(t); ensures emp; disp tree(t) { local i,j; if(t ✏ nil)t✉ else t i :✏ tÑl; j :✏ tÑr; disp tree(i); disp tree(j); dispose♣tq; } }

Ñ

requires tree(t); ensures emp; disp tree(t) { local i,j; if(t ✏ nil)t✉ else t i :✏ tÑl; j :✏ tÑr; dispose♣tq ⑥ (disp tree(i) ⑥ disp tree(j)); } }

78