pattern-based synthesis of synchronization for the c++ memory model yuri meshman, noam rinetzky,...

27
Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

Upload: leon-paul

Post on 14-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

1

Pattern-based Synthesis of Synchronization for the C++ Memory Model

Yuri Meshman, Noam Rinetzky, Eran Yahav

Page 2: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

2

Goal

SynSynCpp

P

S

Verified

Note:Assuming: Asking:

Page 3: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

3

Thread 1:

store (flag1, 1);while(load (flag0) = 1 ){ if(load (turn) = 0 ){

store (flag1, 0);while(load (turn) = 0 )yield();store (flag1, 1);

} }... // critical sectionstore (turn, 0);store(flag1, 0);

store (flag0, 0);store (flag1, 0);store (turn, 0);Thread 0:

store(flag0, 1);while(load (flag1) = 1 ){ if(load (turn) =1 ){

store (flag0, 0); while(load (turn) = 1 ) yield();

store (flag0, 1); } }... // critical sectionstore (turn, 1);store (flag0, 0);

Dekker’s Algorithm

sequential consistency Yesspec: mutual exclusion over critical section

Page 4: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

4

Thread 1:

store (flag1, 1);while(load (flag0) = 1 ){ if(load (turn) = 0 ){

store (flag1, 0);while(load (turn) = 0 )yield();store (flag1, 1);

} }... // critical sectionstore (turn, 0);store(flag1, 0);

store (flag0, 0);store (flag1, 0);store (turn, 0);Thread 0:

store(flag0, 1);while(load (flag1) = 1 ){ if(load (turn) =1 ){

store (flag0, 0); while(load (turn) = 1 ) yield();

store (flag0, 1); } }... // critical sectionstore (turn, 1);store (flag0, 0);

Dekker’s Algorithm

sequential consistency

C++ relaxed model

Yes

No

rf rf

spec: mutual exclusion over critical section

Page 5: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

5

Thread 1:

storeSC (flag1, 1);while(loadSC (flag0) = 1 ){ if(loadSC (turn) = 0 ){

storeRLX (flag1, 0);while(loadRLX (turn) = 0 )yield();storeRLX (flag1, 1);

} }... // critical sectionstoreSC (turn, 0);storeREL(flag1, 0);

storeSC (flag0, 0);storeSC (flag1, 0);storeSC (turn, 0);Thread 0:

storeSC(flag0, 1);while(loadSC (flag1) = 1 ){ if(loadSC (turn) =1 ){

storeRLX (flag0, 0); while(loadRLX (turn) = 1 ) yield();

storeRLX (flag0, 1); } }... // critical sectionstoreSC (turn, 1);storeREL (flag0, 0);

Dekker’s Algorithm

sequential consistency

C++ relaxed model

Yes

No

spec: mutual exclusion over critical section

Yes

Page 6: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

6

In a nutshell

β€’ Goal Automatic inference of efficient and correct synchronization under C++ memory model.β€’ finite-state programs with bounded executions.

β€’ Challenge Adding minimal and correct synchronization.

β€’ Solution 1. Assume maximally relaxed parameters setting and 2. Iteratively remove behaviors until specification satisfied.

Page 7: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

7

Dekker’s Algorithm

Thread 1:

storeRLX (flag1, 1);while(loadRLX (flag0) = 1 ){ if(loadRLX (turn) = 0 ){

storeRLX (flag1, 0);while(loadRLX (turn) = 0 )yield();storeRLX (flag1, 1);

} }... // critical sectionstoreRLX (turn, 0);storeREL(flag1, 0);

storeRLX (flag0, 0);storeRLX(flag1, 0);storeRLX(turn, 0);Thread 0:

storeRLX (flag0, 1);while(loadRLX (flag1) = 1 ){ if(loadRLX (turn) =1 ){

storeRLX (flag0, 0); while(loadRLX (turn) = 1 ) yield();

storeRLX (flag0, 1); } }... // critical sectionstoreRLX (turn, 1);storeRLX (flag0, 0);

store flag0, 0

store flag1, 0

store turn, 0

store flag0, 1

load flag1

store flag1, 1

store turn, 1

load flag0

sb

sb

sb

sb

sb

asw asw

store flag1, 0store flag0, 0

sb

sb

rf

rf store turn, 0sb

critical section critical section

Page 8: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

What is an execution

β€’ Execution trace is:– Instructions

– relations:total/partial orders

– Axioms on those relations

8

store flag0, 0

store flag1, 0

store turn, 0

store flag0, 1

load flag1

store flag1, 1

store turn, 1

load flag0

sb

sb

sb

sb

sb

asw asw

store flag1, 0store flag0, 0

sb

sb

rf

rf store turn, 0sb

critical section critical section

*rf – read from sb – sequence before asw – additionally synchronize with sw – synchronize with

Page 9: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

Error trace 1 find pattern LB

*rf – read from sb – sequence before asw – additionally synchronize with sw – synchronize with

9

store flag0, 0

store flag1, 0

store turn, 0

store flag0, 1

load flag1

store flag1, 1

store turn, 1

load flag0

sb

sb

sb

sb

sb

asw asw

store flag1, 0store flag0, 0

sb

sb

rf

rf store turn, 0sb

critical section critical section

load buffering

Page 10: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

10

Error trace 1 preventedstore flag0, 0

store flag1, 0

store turn, 0

store flag0, 1

loadACQ flag1

store flag1, 1

store turn, 1

load flag0

sb

sb

sb

sb

sb

asw asw

storeREL flag1, 0store flag0, 0

sb

sb

rf

rf store turn, 0sb

critical section critical section

sw

*rf – read from sb – sequence before asw – additionally synchronize with sw – synchronize with

Page 11: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

11

Dekker’s Algorithm

sequential consistency

C++ relaxed model

Yes

No

spec: mutual exclusion over critical section

Thread 1:

storeRLX (flag1, 1);while(loadRLX (flag0) = 1 ){ if(loadRLX (turn) = 0 ){

storeRLX (flag1, 0);while(loadRLX (turn) = 0 )yield();storeRLX (flag1, 1);

} }... // critical sectionstoreRLX (turn, 0);storeREL(flag1, 0);

storeRLX (flag0, 0);storeRLX(flag1, 0);storeRLX(turn, 0);Thread 0:

storeRLX (flag0, 1);while(loadACQ (flag1) = 1 ){ if(loadRLX (turn) =1 ){

storeRLX (flag0, 0); while(loadRLX (turn) = 1 ) yield();

storeRLX (flag0, 1); } }... // critical sectionstoreRLX (turn, 1);storeRLX (flag0, 0);

Page 12: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

12

Thread 1:

storeSC (flag1, 1);while(loadSC (flag0) = 1 ){ if(loadSC (turn) = 0 ){

storeRLX (flag1, 0);while(loadRLX (turn) = 0 )yield();storeRLX (flag1, 1);

} }... // critical sectionstoreSC (turn, 0);storeREL(flag1, 0);

storeSC (flag0, 0);storeSC (flag1, 0);storeSC (turn, 0);Thread 0:

storeSC(flag0, 1);while(loadSC (flag1) = 1 ){ if(loadSC (turn) =1 ){

storeRLX (flag0, 0); while(loadRLX (turn) = 1 ) yield();

storeRLX (flag0, 1); } }... // critical sectionstoreSC (turn, 1);storeREL (flag0, 0);

Dekker’s Algorithm

sequential consistency

C++ relaxed model

Yes

No

spec: mutual exclusion over critical section

Yes

Page 13: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

13

Goal

SynSynCpp

P

S

Verified

Note:Assuming: pecAsking:

Page 14: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

14

Synthesis of Synchronization for the C++ Memory Model

SynSynCpp

P

SVerified

Note:Assuming: Asking:

Model Checker

}

Detect patterns and avoid traces

π‘†π΄π‘‡πœ‘

implement

All solutions to

Page 15: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

15

Challenges

1. ModelChecker: Enumerate error traces? 2. How to detect patterns and avoid traces?3. How to collect all blocked bad traces and produce a

solution?

CDSchecker

Page 16: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

Step 1: Get an error trace

16

store flag0, 0

store flag1, 0

store turn, 0

store flag0, 1

load flag1

store flag1, 1

store turn, 1

load flag0

sb

sb

sb

sb

sb

asw asw

store flag1, 0store flag0, 0

sb

sb

rf

rf store turn, 0sb

critical section critical section

Page 17: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

Step 2: Find a pattern

17

load flag1 load flag0

store flag1, 0store flag0, 0

sb

rfsb

Load Buffering pattern

Page 18: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

18

Step 3: block the pattern

repeat 1-3 for all error traces

Load Buffering pattern

REL -- store flag0, 1

ACQ -- load flag1

REL -- store flag1, 1

ACQ -- load flag0

Page 19: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

19

𝑏1

𝑏2

𝑏3

𝑏4

𝑓 𝑂𝑝1βˆ’π‘‡π‘œβˆ’π‘ƒπ‘Ÿπ‘’π‘£π‘’π‘›π‘‘ βˆ’πΈπ‘Ÿπ‘Ÿπ‘‡π‘Ÿπ‘Žπ‘π‘’ 1=𝑏1βˆ§π‘4

𝑓 π‘ƒπ‘Ÿπ‘’π‘£π‘’π‘›π‘‘βˆ’πΈπ‘Ÿπ‘Ÿπ‘‡π‘Ÿπ‘Žπ‘π‘’ 1= 𝑓 𝑂𝑝1βˆ’π‘‡π‘œβˆ’π‘ƒπ‘Ÿπ‘’π‘£π‘’π‘›π‘‘βˆ’πΈπ‘Ÿπ‘Ÿπ‘‡π‘Ÿπ‘Žπ‘π‘’ 1∨ 𝑓 𝑂𝑝2βˆ’π‘‡π‘œβˆ’π‘ƒπ‘Ÿπ‘’π‘£π‘’π‘›π‘‘ βˆ’πΈπ‘Ÿπ‘Ÿπ‘‡π‘Ÿπ‘Žπ‘π‘’1βˆ¨β€¦

πœ‘= 𝑓 π‘ƒπ‘Ÿπ‘’π‘£π‘’π‘›π‘‘ βˆ’πΈπ‘Ÿπ‘Ÿπ‘‡π‘Ÿπ‘Žπ‘π‘’1∧ 𝑓 π‘ƒπ‘Ÿπ‘’π‘£π‘’π‘›π‘‘βˆ’πΈπ‘Ÿπ‘Ÿπ‘‡π‘Ÿπ‘Žπ‘π‘’ 2βˆ§β€¦

𝑓 𝑂𝑝 2βˆ’π‘‡π‘œβˆ’π‘ƒπ‘Ÿπ‘’π‘£π‘’π‘›π‘‘ βˆ’πΈπ‘Ÿπ‘Ÿπ‘‡π‘Ÿπ‘Žπ‘π‘’1=𝑏3βˆ§π‘2

Step 4: construct

REL -- store flag0, 1

ACQ -- load flag1

REL -- store flag1, 1

ACQ -- load flag0

Page 20: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

20

Step 5: Constructing Program Solution

π‘†π΄π‘‡πœ‘

π‘–π‘šπ‘π‘™π‘’π‘šπ‘’π‘›π‘‘

avoidance =

sol1 sol2

𝑏1

𝑏2

𝑏3

𝑏4REL -- store flag0, 1

ACQ -- load flag1

REL -- store flag1, 1

ACQ -- load flag0

All solutions to

Page 21: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

21

Litmus test patterns

First approach:From litmus tests to patterns

(Store Buffering) (Message Passing) (Write Read Causality)

(Independent Reads Independent Writes) (Load Buffering)

Page 22: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

22

Patterns relaxation

(Load Buffering)

Unwindingthe cycle

Abstraction

Abstraction

Generalization

Page 23: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

23

Abstract patterns

RD_CL

RD_MSMissed Store

based on C++RMM RD property

Cycle

R one of two:1. R = hb U rf --- preventable with Rel-Acq synchronization2. R = possible order of instruction – preventable with SC

Page 24: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

24

Benchmarks

β€’ 9 concurrent algorithms– Mutual exclusion algorithms– RCU where an update waits only for the reads

whose consistency it affectsβ€’ Safety specifications

– mutual exclusion and reachability invariants– stack/RCU coherence invariants

Page 25: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

25

Results

AlgorithmTime(s)

# minimal solutions

Inferred synchronization (SC, REL, ACQ, RLX)

abp 20s.89 5 (5, 0, 0, 1), (4, 0, 0, 2), …

dekker 3m:22 13 (10, 1, 0, 8), (13, 0, 1, 5), …

d-prcu-v1 3m:14 7 (7, 2, 1, 0), (7, 1, 0, 2), …

d-prcu-v2 3h:53m 17 (9, 2, 1, 4), (12, 1, 1, 2), ...

Kessel 57m:16 2 (13, 1, 0, 0), (14, 0, 0, 0)

peterson 26m:41 2 (11, 1, 0, 1), *(12, 1, 0, 0), (13, 0, 0, 0)

bakery 10m:21 6 (16, 1, 1, 0), (17, 0, 1, 0), …

ticket 1m:08 4 (9, 0, 0, 1), (8, 0, 0, 2), …

treiber stack 1h:05 1 (0, 5, 3, 4) SC

RLX

ACQREL

Memory access patternshierarchy

Page 26: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

26

Synthesis of Synchronization for the C++ Memory Model

SynSynCpp

P

SVerified

Note:Assuming: Asking:

Model Checker

}

Detect patterns and avoid traces

π‘†π΄π‘‡πœ‘

implement

avoidance

Page 27: Pattern-based Synthesis of Synchronization for the C++ Memory Model Yuri Meshman, Noam Rinetzky, Eran Yahav 1

27

Summary

β€’ Synthesis procedure for inferring memory order synchronizations for C++ RMM

β€’ Searching for violation patternsβ€’ Generalized concrete patterns to abstract onesβ€’ Successfully synthesized nontrivial memory order

synchronization for challenging algorithms

Future Work: β€’ Violation patterns and avoidance templates are not

completeβ€’ Finite-state programs with bounded executions.

Tool: http://www.cs.technion.ac.il/~yurime/SynSynCpp/SynSynCppTool_v1stat.htm