Thread Safety Violations Fully Automatic and Precise Detection...

90
1 Fully Automatic and Precise Detection of Thread Safety Violations Michael Pradel and Thomas R. Gross Department of Computer Science ETH Zurich

Transcript of Thread Safety Violations Fully Automatic and Precise Detection...

Page 1: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

1

Fully Automatic and Precise Detection ofThread Safety Violations

Michael Pradel and Thomas R. Gross

Department of Computer ScienceETH Zurich

Page 2: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

2

Motivation

Thread-safe classes:Building blocks for concurrent programs

Page 3: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

2

Motivation

Thread-safe classes:Building blocks for concurrent programs

Page 4: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

2

Motivation

Thread-safe classes:Building blocks for concurrent programs

Page 5: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

3

Example from JDK

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Page 6: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

3

Example from JDK

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

IndexOutOfBoundsException

Confirmed as bug: Issue #7100996

!

Page 7: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

3

Example from JDK

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

IndexOutOfBoundsException

Confirmed as bug: Issue #7100996

!

How to testthread safety?

Page 8: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

4

Goal

Automatic and precise bug detection

ClassThreadsafetyviolations

Tool

Page 9: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

4

Goal

Automatic and precise bug detection

ClassThreadsafetyviolations

Tests

Falsepositives

Formalspecs

Tool

Page 10: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

4

Goal

Automatic and precise bug detection

ClassThreadsafetyviolations

Tool

Page 11: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

5

Thread-Safe Classes

“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code” page 18

Page 12: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

5

Thread-Safe Classes

“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code” page 18

Page 13: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

5

Thread-Safe Classes

“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code” page 18

Page 14: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

5

Thread-Safe Classes

“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code”

“operations ... behave as if they occurin some serial order that is consistentwith the order of the method callsmade by each of the individualthreads”

page 18

StringBuffer API documentation, JDK 6

Page 15: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

5

Thread-Safe Classes

“behaves correctly when accessedfrom multiple threads ... with noadditional synchronization ... (inthe) calling code”

“operations ... behave as if they occurin some serial order that is consistentwith the order of the method callsmade by each of the individualthreads”

page 18

StringBuffer API documentation, JDK 6

Page 16: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

6

Example

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

Page 17: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

6

Example

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

"abc" 3 "cab" 3 "acb" 3 "ac" 7

Page 18: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

6

Example

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

"abc" 3 "cab" 3 "acb" 3 "ac" 7

Page 19: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

6

Example

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

"abc" 3 "cab" 3 "acb" 3 "ac" 7

Page 20: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

6

Example

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

"abc" 3 "cab" 3 "acb" 3 "ac" 7

Page 21: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

6

Example

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

"abc" 3 "cab" 3 "acb" 3 "ac" 7

Page 22: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

7

Approach

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 23: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

7

Approach

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 24: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

7

Approach

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 25: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

8

Generating Concurrent Tests

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Example:

Page 26: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

8

Generating Concurrent Tests

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Sequential prefix:

Create and set upCUT instance

Example:

Page 27: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

8

Generating Concurrent Tests

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Concurrent suffixes:

Use shared CUTinstance

Example:

Page 28: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

9

Test Generation Algorithm

1. Create prefix� Instantiate CUT

� Call methods

2. Create suffixes for prefix� Call methods on shared CUT instance

3. Prefix + two suffixes = test

Page 29: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Page 30: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Randomlyselect aconstructor

Page 31: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Randomlyselect aconstructor

Page 32: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

After adding a call:Execute

Page 33: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

After adding a call:Execute

3

Page 34: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Randomlyselect amethod

Page 35: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Randomlyselect amethod

b.append(/* String */)

Page 36: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

b.append(/* String */)

Arguments:a) Take available objectb) Call method returning

required typec) Random value

Page 37: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Arguments:a) Take available objectb) Call method returning

required typec) Random value

Page 38: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

After adding a call:Execute

Page 39: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

After adding a call:Execute

3

Page 40: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

10

Creating a Prefix

1. Create prefix� Instantiate CUT

� Call methods

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Page 41: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

Page 42: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

Page 43: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

Randomlyselect amethod

Page 44: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

Randomlyselect amethod

b.insert(/* int */, /* CharSequence */)

Page 45: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)b.insert(/* int */, /* CharSequence */)

Arguments:a) Take available objectb) Call method returning

required typec) Random value

Page 46: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)b.insert(-5, b)

Arguments:a) Take available objectb) Call method returning

required typec) Random value

Page 47: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)b.insert(-5, b)

After adding a call:Execute

Page 48: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)b.insert(-5, b)

After adding a call:Execute

!

Page 49: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)b.insert(/* int */, /* CharSequence */)

Arguments:a) Take available objectb) Call method returning

required typec) Random value

Page 50: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

Arguments:a) Take available objectb) Call method returning

required typec) Random value

Page 51: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

After adding a call:Execute

Page 52: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

After adding a call:Execute

3

Page 53: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

Page 54: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Page 55: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

After adding a call:Execute

Page 56: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

After adding a call:Execute

3

Page 57: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

11

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Page 58: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

12

Creating a Test

3. Prefix + two suffixes = test

Page 59: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

12

Creating a Test

3. Prefix + two suffixes = test

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Page 60: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

12

Creating a Test

3. Prefix + two suffixes = test

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Spawn new threadfor each suffix

Page 61: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

13

Approach

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 62: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

13

Approach

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 63: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

14

Thread Safety Oracle

Does the test executionexpose a thread safetyviolation?

� Focus on exceptionsand deadlocks

� Compare concurrentexecution tolinearizations

Page 64: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

15

Assumptions

Concurrency-only crashes are undesired� Matches definition of thread safety

Control over all input to tests� Sequential execution: Deterministic

Page 65: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

16

Linearizations

� Put all calls into one thread� Preserve order of calls within a thread

Page 66: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

16

Linearizations

213

2

13

21

3

� Put all calls into one thread� Preserve order of calls within a thread

21 3

Page 67: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Page 68: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Execute concurrently

Page 69: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Exception ordeadlock?

Page 70: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Execute linearization

Page 71: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Samefailure?

Page 72: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Execute linearization

Page 73: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Samefailure?

Page 74: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

17

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Thread safetyviolation

Page 75: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

18

Example

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

Page 76: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

18

Example

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

Thread 1 Thread 2

!

Page 77: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

18

Example

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

StringBuffer b = ..

b.append("abc")

b.insert(1, b)

b.deleteCharAt(1) 3

Thread 1 Thread 2

!

Page 78: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

18

Example

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

StringBuffer b = ..

b.append("abc")

b.insert(1, b)

b.deleteCharAt(1) 3

Thread 1 Thread 2

StringBuffer b = ..

b.append("abc")

b.deleteCharAt(1)

b.insert(1, b) 3

!

Page 79: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

18

Example

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b) b.deleteCharAt(1)

StringBuffer b = ..

b.append("abc")

b.insert(1, b)

b.deleteCharAt(1) 3

Thread 1 Thread 2

StringBuffer b = ..

b.append("abc")

b.deleteCharAt(1)

b.insert(1, b) 3

!Thread safety violation

Page 80: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

19

Properties of the Oracle

Sound but incomplete *

� All reported violations are real� Cannot guarantee thread safety

Independent of bug type� Data races� Atomicity violations� Deadlocks

* with respect to incorrectness

Page 81: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

20

Implementation

Page 82: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

21

Evaluation

1. Effectiveness in finding bugs

2. Performance

Setup:

� Thread-safe classes from six Javalibraries (e.g., JDK, Apache DBCP)

� Intel Xeon (8x3GHz)

Page 83: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

22

Bugs

Found 15 bugs and 0 false positives

� 9 known bugs

� 6 previously unknown bugs� E.g., in JDK and Apache DBCP

Page 84: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

23

Example: Apache DBCP

DataSource ds = new DataSource()

ds.setDataSourceName("a") ds.close()

Thread 1 Thread 2

Page 85: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

23

Example: Apache DBCP

DataSource ds = new DataSource()

ds.setDataSourceName("a") ds.close()

Thread 1 Thread 2

ConcurrentModificationException

Reason: Unsynchronized use ofthread-unsafe collection

Confirmed as bug: Issue #369

!

Page 86: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

24

Kinds of Failures

12 of 15 failures are implicit (VM or JDK)

Most common:

� NullPointerException� ConcurrentModificationException

Page 87: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

25

Performance

Page 88: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

25

Performance

8 hours

Page 89: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

26

Conclusion

Concurrency: More and more important

Need tools to test thread-safe classes

This work:� Fully automatic testing� Only real bugs reported

Page 90: Thread Safety Violations Fully Automatic and Precise Detection ofmp.binaervarianz.de/pldi2012_slides.pdf · 2013. 10. 4. · Thread Safety Violations Michael Pradel and Thomas R.

27

Thank you!

Try it:

Fully Automatic and Precise Detection of Thread Safety ViolationsMichael Pradel and Thomas R. Gross, ETH Zurich