93
1 Program Testing and Analysis: Testing Concurrent Programs (Part 2) Dr. Michael Pradel Software Lab, TU Darmstadt

(Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

1

Program Testing and Analysis:

Testing Concurrent Programs(Part 2)

Dr. Michael Pradel

Software Lab, TU Darmstadt

Page 2: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

2

Warm-up Quiz

var x = 5;var y = Number(5);var z = new Number(5);x.foo = "bar"; y.foo = "bar"; z.foo = "bar";console.log(x.foo);console.log(y.foo);console.log(z.foo);

What does the following code print?

barundefinedbar

undefinedundefinedundefined

barbarbar

Some-thingelse

Page 3: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

2

Warm-up Quiz

var x = 5;var y = Number(5);var z = new Number(5);x.foo = "bar"; y.foo = "bar"; z.foo = "bar";console.log(x.foo);console.log(y.foo);console.log(z.foo);

What does the following code print?

barundefinedbar

undefinedundefinedundefined

barbarbar

Some-thingelse

”undefined” (x and y areprimitive values, whichcannot have properties)

”bar” (z is an object)

Page 4: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

3

Outline

1. Introduction

2. Dynamic Data Race Detection

3. Testing Thread-Safe Classes

4. Exploring Interleavings

Mostly based on these papers:

� Eraser: A Dynamic Data Race Detector for MultithreadedPrograms, Savage et al., ACM TOCS, 1997

� Fully Automatic and Precise Detection of Thread SafetyViolations, Pradel and Gross, PLDI 2012

� Finding and Reproducing Heisenbugs in ConcurrentPrograms, Musuvathi et al., USENIX 2008

Page 5: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

4

Thread Safety

� Popular way to encapsulate the challenges ofconcurrent programming: Thread-safe classes

� Class ensures correct synchronization

� Clients can use instances as if they were alone

� Rest of program can treat implementation ofthread-safe class as a blackbox

Page 6: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

5

Thread Safety (2)

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

Page 7: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

5

Thread Safety (2)

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

Page 8: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

5

Thread Safety (2)

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

Page 9: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

5

Thread Safety (2)

“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 10: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

5

Thread Safety (2)

“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 11: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

6

Example from JDK

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

Page 12: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

6

Example from JDK

StringBuffer b = new StringBuffer()

b.append("a")

b.append("b")

b.append("c")

Thread 1 Thread 2

Quiz: What can be the content of b ifStringBuffer is thread-safe?

Page 13: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

6

Example from JDK

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 "bac" 7

Page 14: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

6

Example from JDK

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 "bac" 7

Page 15: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

6

Example from JDK

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 "bac" 7

Page 16: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

6

Example from JDK

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 "bac" 7

Page 17: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

6

Example from JDK

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 "bac" 7

Page 18: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

7

Testing Thread-Safe Classes

� Correctness of program relies on thread safety ofspecific classes

� But: What if the class is actually not thread-safe?

� ConTeGe = Concurrent Test Generator

� Creates multi-threaded unit tests

� Detects thread safety violations by comparingconcurrent behavior against linearizations

Page 19: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

8

Example Bug from JDK

StringBuffer b = new StringBuffer()

b.append("abc")

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

Thread 1 Thread 2

Page 20: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

8

Example Bug from JDK

StringBuffer b = new StringBuffer()

b.append("abc")

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

Thread 1 Thread 2

IndexOutOfBoundsException

Issue #7100996 in Oracle’s bug tracker

!

Page 21: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

9

ConTeGe

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 22: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

9

ConTeGe

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 23: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

9

ConTeGe

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 24: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

10

Generating Concurrent Tests

StringBuffer b = new StringBuffer()

b.append("abc")

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

Thread 1 Thread 2

Example:

Page 25: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

10

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 26: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

10

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 27: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

11

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

Selection of methods similar tofeedback-directed test generation

Page 28: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 29: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 30: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 32: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 33: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 34: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 35: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 36: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 37: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 38: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 39: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

12

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 40: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

Page 41: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

Page 42: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 43: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 44: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 45: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 46: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 47: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 49: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 50: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 51: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 52: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

Creating Suffixes

2. Create suffixesfor prefix

� Call methods onshared CUT instance

StringBuffer b = new StringBuffer()

b.append("abc")

b.insert(1, b)

Page 53: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 54: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 55: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 56: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

13

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 57: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

14

Creating a Test

3. Prefix + two suffixes = test

Page 58: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

14

Creating a Test

3. Prefix + two suffixes = test

StringBuffer b = new StringBuffer()

b.append("abc")

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

Page 59: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

14

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 60: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

15

Approach

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 61: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

15

Approach

Bug

Classundertest(CUT)

Execute

Thread safetyoracle

Generate aconcurrent test

Page 62: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

16

Thread Safety Oracle

Does the test executionexpose a thread safetyviolation?

� Focus on exceptionsand deadlocks

� Compare concurrentexecution tolinearizations

Page 63: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

17

Linearizations

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

Page 64: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

17

Linearizations

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

21 3

Page 65: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

17

Linearizations

213

2

13

21

3

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

21 3

Page 66: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

3

3

Page 67: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Execute concurrently

3

3

Page 68: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Exception ordeadlock? 3

3

Page 69: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Execute linearization

3

3

Page 70: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Samefailure?

3

3

Page 71: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Execute linearization

3

3

Page 72: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Samefailure?

3

3

Page 73: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

3

3

All linearizationschecked

Page 74: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

18

The Oracle

Exception ordeadlock?

Execute concurrently

No

Yes

Yes

No Thread safetyviolation

Samefailure?

Execute linearization All linearizationschecked

Thread safetyviolation

3

3

Page 75: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

19

Example

StringBuffer b = new StringBuffer()

b.append("abc")

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

Thread 1 Thread 2

Page 76: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

19

Example

StringBuffer b = new StringBuffer()

b.append("abc")

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

Thread 1 Thread 2

!

Page 77: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

19

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: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

19

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: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

19

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: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

20

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: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

21

Implementation & Results

� Implemented for Java classes

� Applied to popular thread-safe classesfrom JDK, Apache libraries, etc.

� Found 15 concurrency bugs, includingpreviously unknown problems in JDK

� Takes between several seconds andseveral hours (worst case: 19 hours)

Page 82: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

22

Open Challenges

� How to generate tests that are likely to triggerbugs? (Currently: random decisions)

� Static analysis to find potential bugs; focus on thoseduring test generation

� Use feedback from test execution to steer testgeneration towards not yet explored behavior

� How to generate tests for larger pieces ofconcurrent software, e.g., entire libraries orprograms?

Page 83: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

22

Open Challenges

� How to generate tests that are likely to triggerbugs? (Currently: random decisions)

� Static analysis to find potential bugs; focus on thoseduring test generation

� Use feedback from test execution to steer testgeneration towards not yet explored behavior

� How to generate tests for larger pieces ofconcurrent software, e.g., entire libraries orprograms?

Hint: Opportunities for master theses

Page 84: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

23

Outline

1. Introduction

2. Dynamic Data Race Detection

3. Testing Thread-Safe Classes

4. Exploring Interleavings

Mostly based on these papers:

� Eraser: A Dynamic Data Race Detector for MultithreadedPrograms, Savage et al., ACM TOCS, 1997

� Fully Automatic and Precise Detection of Thread SafetyViolations, Pradel and Gross, PLDI 2012

� Finding and Reproducing Heisenbugs in ConcurrentPrograms, Musuvathi et al., USENIX 2008

Page 85: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

24

Scheduling Non-Determinism

� A single program executed with a single inputmay have many different interleavings

� Scheduler decides interleavingsnon-deterministically

� Some interleavings may expose bugs, othersexecute correctly (”Heisenbugs”)

� Challenge: How to explore different interleavings?How to detect buggy interleavings?

Page 86: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

25

CHESS in a Nutshell

� A user mode scheduler that controlsall scheduling non-determinism

� Guarantees:� Every program run takes a new thread

interleaving� Can reproduce the interleaving for every run

� Systematic but non-exhaustiveexploration of the set of possibleinterleavings

Page 87: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

26

Tree of Interleavings

� Search space of possibleinterleavings: Represent as a tree

� Nodes = points of scheduling decision

� Edges = decisions taken

� Each path = one possible schedule

Page 88: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

27

Example

// bank account

int balance = 10;

// deposit money

int tmp1 = balance;

balance = tmp1 + 5;

// withdraw money

int tmp2 = balance;

balance = tmp2 - 7;

Thread 1 Thread 2

Page 89: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

1

Page 90: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

29

State Space Explosion

Thread 1:

instr. 1instr. 2...instr. k

n threads

k instructions

� Number ofinterleavings: O(nn·k)

� Exponential in both n

and k

� Typically:n < 10, k > 100

� Exploring allinterleavings does notscale to largeprograms (i.e., large k)

Thread 2:

instr. 1instr. 2...instr. k

Page 91: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

30

Preemption Bounding

� Limit exploration to schedules with a smallnumber c of preemptions� Preemption = Context switches forced by the

scheduler

� Number of schedules: O((n2 · k)c · n!)� Exponential in c and n, but not in k

� Based on empirical observation: Mostconcurrency bugs can be triggered with few (< 2)preemptions

Page 92: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

31

Implementation and Results

� Implemented via binary instrumentation

� Applied to eight mid-size and large systems (upto 175K lines of code),

� Found a total of 27 bugs

� Major benefit over stress testing: Once a failure isdetected, can easily reproduce and debug it

Page 93: (Part 2) Testing Concurrent Programs Program Testing and ... · Thread Safety (2) “behaves correctly when accessed from multiple threads ... with no additional synchronization

32

Summary

� Concurrent programming is inevitable

� Writing correct concurrent programsis hard

� Techniques to detect concurrencybugs� Dynamic data race detection� Test generation and thread safety checking� Systematic exploration of interleavings