Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong...

84
Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology Axis: Automatically Fixing Atomicity Violations rough Solving Control Constrain 1

Transcript of Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong...

Page 1: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

1

Peng Liu and Charles Zhang

Prism Research Group

Department of Computer Science and Engineering

Hong Kong University of Science and Technology

Axis: Automatically Fixing Atomicity Violations

through Solving Control Constraints

Page 2: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

2

Motivation

An Atomicity Violation (AV) in StringBuffer.The accesses, assumed to be atomic, are interleaved non-serializably by a remote access.

run(){ // s1.append(s2): synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 1

Page 3: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

3

Motivation

An Atomicity Violation (AV) in StringBuffer.The accesses, assumed to be atomic, are interleaved non-serializably by a remote access.

run(){ // s1.append(s2): synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 1

Page 4: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

4

Motivation

An Atomicity Violation (AV) in StringBuffer.The accesses, assumed to be atomic, are interleaved non-serializably by a remote access.

run(){ // s1.append(s2): synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 1

Page 5: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

5

Motivation

An Atomicity Violation (AV) in StringBuffer.The accesses, assumed to be atomic, are interleaved non-serializably by a remote access.

run(){ // s1.append(s2): synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 1

Page 6: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

6

Motivation

An Atomicity Violation (AV) in StringBuffer.The accesses, assumed to be atomic, are interleaved non-serializably by a remote access.

run(){ // s1.append(s2): synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

run(){

s2.delete(0, s2.length()); }

Thread 1 Thread 2

Page 7: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

7

Motivation

An Atomicity Violation (AV) in JDK StringBuffer.The accesses, assumed to be atomic, are interleaved non-serializably by a remote access.

run(){ // s1.append(s2): synchronized(s1) { a int len = s2.length();

b s2.getChars(0, len, s1…); }}

run(){

r s2.delete(0, s2.length()); }

Thread 1 Thread 2

Page 8: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

8

Motivation

Common approach of Fixing the Atomicity ViolationSynchronize the atomicity sequence (from a to b) and the remote access (r) with locks.

run(){ // s1.append(s2): synchronized(s1) { + lockM.lock(); a int len = s2.length();

b s2.getChars(0, len, s1…); + lockM.unlock(); }}

run(){

+ lockM.lock(); r s2.delete(0, s2.length()); + lockM.unlock();

}

Thread 1 Thread 2

Page 9: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

9

Motivation

Problems with Fixing the Violations

Sacrifice the concurrency!

IntroduceNew deadlocks!

Page 10: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

10

Motivation

Problems with Fixing the Violations

Sacrifice the concurrency!

IntroduceNew deadlocks!

Page 11: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

11

Motivation

Introduce New DeadlocksAV (a,b,r) and the original lock oL protecting some irrelevant vars.

Thread 1 Thread 2

a

oL.lock();…oL.unlock();

b

oL.lock();

...

r

...

oL.unlock();

Page 12: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

12

Motivation

Introduce New DeadlocksAV (a,b,r) and the original lock oL protecting some irrelevant vars.

Thread 1 Thread 2

a

oL.lock();…oL.unlock();

b

oL.lock();

...

r

...

oL.unlock();

Page 13: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

13

Motivation

Introduce New DeadlocksAV (a,b,r) and the original lock oL protecting some irrelevant vars.

Thread 1 Thread 2

a

oL.lock();…oL.unlock();

b

oL.lock();

...

r

...

oL.unlock();

Page 14: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

14

Motivation

Introduce New DeadlocksAV (a,b,r) and the original lock oL protecting some irrelevant vars.

Thread 1 Thread 2

a

oL.lock();…oL.unlock();

b

oL.lock();

...

r

...

oL.unlock();

+ L.lock();

+ L.unlock();

+ L.lock();

+ L.unlock();

Page 15: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

15

Motivation

Introduce New DeadlocksAV (a,b,r) and the original lock oL protecting some irrelevant vars.

Thread 1 Thread 2

a

oL.lock();…oL.unlock();

b

oL.lock();

...

r

...

oL.unlock();

+ L.lock();

+ L.unlock();

+ L.lock();

+ L.unlock();

Page 16: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

16

Motivation

Problems with Fixing the Violations

Sacrifice the concurrency!

IntroduceNew deadlocks!

Page 17: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

17

Motivation

Sacrifice the Concurrency GreatlyTwo overlapping Avs: (a,b,r) and (a’b,r’).

Thread 1 Thread 3

a

b

Thread 2

r’

a’

…r…

b’

Page 18: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

18

Motivation

Sacrifice the Concurrency GreatlyTwo overlapping Avs: (a,b,r) and (a’b,r’).

Thread 1 Thread 3

a

b

Thread 2

r’

a’

…r…

b’

Page 19: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

19

Motivation

Sacrifice the Concurrency GreatlyTwo overlapping Avs: (a,b,r) and (a’b,r’).

Thread 1 Thread 3

a

b

Thread 2

r’

a’

…r…

b’

Page 20: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

20

Motivation

Sacrifice the Concurrency GreatlyTwo overlapping Avs: (a,b,r) and (a’b,r’).

Thread 1 Thread 3

a

b

Thread 2

r’

a’

…r…

b’

Page 21: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

21

Motivation

Sacrifice the Concurrency GreatlyTwo overlapping Avs: (a,b,r) and (a’b,r’).

Thread 1 Thread 3

a

b

Thread 2

r’

a’

…r…

b’

Page 22: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

22

Motivation

Sacrifice the Concurrency GreatlyTwo overlapping Avs: (a,b,r) and (a’b,r’).

Thread 1 Thread 3

a

b

Thread 2

r’

a’

…r…

b’

Page 23: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

23

Motivation

Sacrifice the Concurrency GreatlyTwo overlapping Avs: (a,b,r) and (a’b,r’).

Thread 1 Thread 3

+L.lock();a

b+L.unlock();

Thread 2

+L.lock();

r’

+L.unlock();

+L.lock();a’

…r…

b’ +L.unlock();

Page 24: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

24

Motivation

Our Guarantee

Sacrifice the concurrency

minimally!

IntroduceNew deadlocks?

No!

Page 25: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

25

Motivation

Our Guarantee

Sacrifice the concurrency

minimally!

IntroduceNew deadlocks?

No!

Page 26: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

26

Motivation

Our ApproachCode ToPetri Net

Buggy Code

ConstraintConstructor

ConstraintSolver

Petri NetTo Code

RepairedCode

Page 27: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

27

Motivation

Our ApproachCode ToPetri Net

Buggy Code

ConstraintConstructor

ConstraintSolver

Petri NetTo Code

RepairedCode

Bug report:<6@func1, 8@func1, 14@func2><2@func3, 14@func3, 20@func4>

……

Page 28: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

28

Motivation

Our ApproachCode ToPetri Net

Buggy Code

ConstraintConstructor

ConstraintSolver

Petri NetTo Code

RepairedCode

Page 29: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

29

Motivation

Our Approach

• Constraints: no two pandas on the single-plank bridge simultaneously.

• Solver: control theory.

ConstraintConstructor

ConstraintSolver

Page 30: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

30

Motivation

Rationale

• Performance– Loose constraints–Concurrency-preserving solver.

• Safety–Handle deadlocks with solver

ConstraintConstructor

ConstraintSolver

Page 31: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

31

Code ToPetri Net

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

Page 32: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

32

Code ToPetri Net

Abstract graphical and mathematical model.

• Places (circles)• Transitions (horizontal bars)• Arcs between them

Page 33: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

33

Code ToPetri Net

Abstract graphical and mathematical model.

• Places (circles)• Transitions (horizontal bars)• Arcs between them

Page 34: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

34

Code ToPetri Net

Abstract graphical and mathematical model.

• Places (circles)• Transitions (horizontal bars)• Arcs between them

Page 35: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

35

Code ToPetri Net

Abstract graphical and mathematical model.

• Places (circles)• Transitions (horizontal bars)• Arcs between them

Page 36: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

36

Code ToPetri Net

Abstract graphical and mathematical model.

• Places contain tokens• Transitions, when

triggered, move tokens• Arcs (the weights)

determine how many to move. 1 by default.

Page 37: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

37

Code ToPetri Net

Abstract graphical and mathematical model.

• Places contain tokens• Transitions, when

triggered, move tokens• Arcs (the weights)

determine how many to move. 1 by default.

Page 38: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

38

Code ToPetri Net

Abstract graphical and mathematical model.

• Places contain tokens• Transitions, when

triggered, move tokens• Arcs (the weights) instruct

how many to remove or give. 1 by default.

Page 39: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

39

Code ToPetri Net

Abstract graphical and mathematical model.

• A transition can be triggered only if the input place contains enough tokens.

Page 40: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

40

Code ToPetri Net

Statements -> places. Control flows-> transitions

I f (…)

Branch 1 Branch 2

Branch

Page 41: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

41

Code ToPetri Net

Statements -> places. Control flows-> transitions

I f (…)

Branch 1 Branch 2

Branch

Page 42: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

42

Code ToPetri Net

Statements -> places. Control flows-> transitions

I f (…)

Branch 1 Branch 2

Branch

Page 43: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

43

Code ToPetri Net

Statements -> places. Control flows-> transitions

I f (…)

Branch 1 Branch 2

Branch

Page 44: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

44

Code ToPetri Net

Statements -> places. Control flows-> transitions

I f (…)

Branch 1 Branch 2

Branch

Page 45: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

45

Code ToPetri Net

Statements -> places. Control flows-> transitions

while(…)

S1

S2

Loop

Page 46: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

46

Code ToPetri Net

Statements -> places. Control flows-> transitions

while(…)

S1

S2

Loop

Page 47: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

47

Code ToPetri Net

Statements -> places. Control flows-> transitions

while(…)

S1

S2

Loop

Page 48: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

48

Code ToPetri Net

Statements -> places. Start/Join/Control flows-> transitions

thread1.start( ) ;thread2.start( ) ;

Threading

S1;S2;

S3;S4;

Thread 1: Thread 2:

thread1. join() ;thread2. join() ;

Page 49: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

49

Code ToPetri Net

Statements -> places. Start/Join/Control flows-> transitions

thread1.start( ) ;thread2.start( ) ;

Threading

S1;S2;

S3;S4;

Thread 1: Thread 2:

thread1. join() ;thread2. join() ;

Page 50: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

50

Code ToPetri Net

Statements -> places. Start/Join/Control flows-> transitions

thread1.start( ) ;thread2.start( ) ;

Threading

S1;S2;

S3;S4;

Thread 1: Thread 2:

thread1. join() ;thread2. join() ;

Page 51: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

51

Code ToPetri Net

Statements -> places. Lock/Unlock/Control flows-> transitions

Locking

Thread 1:

L . lock() ;S1;L .unlock() ;

Thread 2:

L . lock() ;S2;L .unlock() ;

Page 52: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

52

Code ToPetri Net

Statements -> places. Lock/Unlock/Control flows-> transitions

Locking

Thread 1:

L . lock() ;S1;L .unlock() ;

Thread 2:

L . lock() ;S2;L .unlock() ;

Page 53: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

53

Code ToPetri Net

Statements -> places. Lock/Unlock/Control flows-> transitions

Locking

Thread 1:

L . lock() ;S1;L .unlock() ;

Thread 2:

L . lock() ;S2;L .unlock() ;

Page 54: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

54

Code ToPetri Net

Mathematical form.

T1 T2

P1 -1

P2 0

P3 0 1

D =

Structure Matrix

(initial) Token Distribution Vector

U0 =

P1 1

P2 0

P3 0

P1

T1 T2

P2 P3-1

1

Page 55: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Code ToPetri Net

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

55

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 56: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Code ToPetri Net

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

56

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P7

P8

P9

T6

T7

P6

Page 57: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Code ToPetri Net

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

57

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P7

P8

P9

T6

T7

P6

Page 58: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Code ToPetri Net

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

58

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 59: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Code ToPetri Net

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

59

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 60: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Code ToPetri Net

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

60

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 61: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

ConstraintConstructor

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 62: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

ConstraintConstructor

62

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 63: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

run(){

s2.delete(0, s2.length()); }

thread1.join(); thread2.join();

ConstraintConstructor

63

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 64: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

64

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

Inter-thread constraint:U(P3) + U(P7) <=1

run(){

s2.delete(0, s2.length()); }

Page 65: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

65

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

thread1.join(); thread2.join();

run(){

s2.delete(0, s2.length()); }

Page 66: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

66

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

Inter-thread constraint:U(P4) + U(P7) <=1

run(){

s2.delete(0, s2.length()); }

Page 67: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

67

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

thread1.join(); thread2.join();

run(){

s2.delete(0, s2.length()); }

Page 68: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

68

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

thread1.join(); thread2.join();

run(){

s2.delete(0, s2.length()); }

Page 69: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

69

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

thread1.start(); thread2.start();

Thread 1

run(){ synchronized(s1) { int len = s2.length();

s2.getChars(0, len, s1…); }}

Thread 2

thread1.join(); thread2.join();

run(){

s2.delete(0, s2.length()); }

Page 70: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

70

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Intra-thread constraint:thread-representing token stays in P3, or P4, but not both at any time.U(P3) + U(P4) <=1

Page 71: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintConstructor

71

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Inter-thread:U(P3) + U(P7) <=1U(P4) + U(P7) <=1Intra-thread:U(P3) + U(P4) <=1

Equivalent form:U(P3) + U(P4) + U(P7) <=1

Page 72: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintSolver

Supervision Based on Place Invariants (SBPI)Input: constraints, e.g., U(P3) + U(P4) + U(P7) <=1Output: augmentation to PN to satisfy the constraints.

Output augmentation:• New control places with tokens• Arcs connecting them to the original PN.• Mathematic form.

Page 73: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintSolver

Output augmentation in its math form:• New control places with tokens• Arcs connecting them to the original PN.

Output of our example:• New place M. M has one token (U0

M = 1 ).• Connecting to T2, T6, from T4, T7. DM =

T1 T2 T3 T4 T5 T6 T7

M 0 -1 0 1 0 -1 1

Page 74: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintSolver

Output augmentation in its math form:• New control places with tokens• Arcs connecting them to the original PN.

Output of our example:• New place M. M has one token (U0

M = 1 ).• Connecting to T2, T6, from T4, T7. DM =

T1 T2 T3 T4 T5 T6 T7

M 0 -1 0 1 0 -1 1

Page 75: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintSolver

Output:• New place M, with one token.• Connecting to T2, T6, from T4, T7.

M

75

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Page 76: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

ConstraintSolver

Wang’s deadlock avoidance (SBPI) [POPL’09]• Reuse.• Part program. Deadlocks introduced by our fix.

Page 77: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Petri NetTo Code

• Control place -> lock (static field).• Arc to (from) a transition -> locking operation at

the control flow (between a pair of statements).

Injected locking operations should not affect other control flows (sharing common statements).

Page 78: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

M

78

P1T1

P2T2

P3

T3P4T4

P5

L

T5

P6

P7

P8

P9

T6

T7

Petri NetTo Code

Thread 1synchronized(s1) { +lockM.lock(); int len = s2.length(); s2.getChars(0, len, s1…); +lockM.unlock(); }

Thread 2

+lockM.lock();s2.delete(0, s2.length());+lockM.unlock();

Page 79: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Evaluation

Benchmarks:• OpenJMS, messaging service implementation.• Derby, Apache’s database system.• Jigsaw, W3C’s webserver platform. Violation detection: Pecan [ISSTA ‘11]Evaluated properties:• Performance of patched code• Safety

Page 80: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Evaluation

Benchmarks:• OpenJMS, messaging service implementation.• Derby, Apache’s database system.• Jigsaw, W3C’s webserver platform. Violation detection: Pecan [ISSTA ‘11]Evaluated properties:• Performance of patched code• Safety

Page 81: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Evaluation (performance)

Compared to the state of the art fixes,1. Axis-noDA is 7% faster2. Axis-DA is only 3% slower. (It has strong safety guarantee.)

OpenJMS Jigsaw Derby

Page 82: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Evaluation (safety)

1. No deadlocks are identified for Axis-DA.2. Frequent deadlocks for other fixes, including Axis-noDA and

AFix.

Patched program T=2 T=4 T=8 T=12

OpenJMS Axis-noDA 0 0 2 7

AFix 0 0 2 5

Jigsaw Axis-noDA 20 20 20 20

AFix 20 20 20 20

Derby Axis-noDA 0 0 0 11

AFix 0 0 0 7

Page 83: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Conclusion

• A formal violation-fixing approach with strong guarantees on • Performance. Sacrifice the concurrency minimally.• Safety. No deadlocks are introduced.

• Implementation & Evaluation• Compared to the state of the art,• Axis-noDA is 7% faster.• Axis-DA (with strong safety guarantee) is merely

3% slower.

Page 84: Peng Liu and Charles Zhang Prism Research Group Department of Computer Science and Engineering Hong Kong University of Science and Technology 1.

Thank you!Q&A