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

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

Embed Size (px)

Citation preview

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