Verifying Concurrent Programs using Contracts Ricardo J. Dias, Carla - - PowerPoint PPT Presentation

verifying concurrent programs using contracts
SMART_READER_LITE
LIVE PREVIEW

Verifying Concurrent Programs using Contracts Ricardo J. Dias, Carla - - PowerPoint PPT Presentation

Verifying Concurrent Programs using Contracts Ricardo J. Dias, Carla Ferreira, Jan Fiedor, Jo ao M. Lourenc o, Ale s Smr cka, Diogo G. Sousa, Tom a s Vojnar Brno University of Technology (BUT) Universidade Nova de Lisboa (UNL)


slide-1
SLIDE 1

Verifying Concurrent Programs using Contracts

Ricardo J. Dias, Carla Ferreira, Jan Fiedor, Jo˜ ao M. Lourenc ¸o, Aleˇ s Smrˇ cka, Diogo G. Sousa, Tom´ aˇ s Vojnar

Brno University of Technology (BUT) Universidade Nova de Lisboa (UNL)

ICST, March 15, 2017

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 1 / 17

slide-2
SLIDE 2

Outline

1

Contracts for Concurrency

2

Static Validation

3

Dynamic Validation

4

Conclusion

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 2 / 17

slide-3
SLIDE 3

Contracts

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method.

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-4
SLIDE 4

Contracts

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-5
SLIDE 5

Contracts

Execution remove(idx)

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-6
SLIDE 6

Contracts

Execution indexOf(obj) remove(idx)

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-7
SLIDE 7

Contracts

Execution indexOf(obj) remove(idx)

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-8
SLIDE 8

Contracts

Thread 1 indexOf(obj) remove(idx) Thread 2 indexOf(obj) remove(idx)

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-9
SLIDE 9

Contracts

Thread 1 indexOf(obj) remove(idx) Thread 2 indexOf(obj) remove(idx)

1 2 3 4 a b

  • bj

c d

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-10
SLIDE 10

Contracts

Thread 1 indexOf(obj) remove(idx) Thread 2 indexOf(obj) remove(idx)

1 2 3 4 a b

  • bj

c d

= 2 = 2

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-11
SLIDE 11

Contracts

Thread 1 indexOf(obj) remove(idx) Thread 2 indexOf(obj) remove(idx)

1 2 3 4 a b

  • bj

c d

= 2 = 2

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-12
SLIDE 12

Contracts

Thread 1 indexOf(obj) remove(idx) Thread 2 indexOf(obj) remove(idx) = 2 = 2

1 2 3 a b c d

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-13
SLIDE 13

Contracts

Thread 1 indexOf(obj) remove(idx) Thread 2 indexOf(obj) remove(idx) = 2 = 2

1 2 3 a b c d

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-14
SLIDE 14

Contracts

Thread 1 indexOf(obj) remove(idx) Thread 2 indexOf(obj) remove(idx) = 2 = 2

1 2 a b d

Contract

Consists of a pre- and post-condition of a method. When a call of the method satisfies its pre-condition, the post-condition is guaranteed to be satisfied upon return from the method. Example: java.util.ArrayList class

Method: remove(idx)

Pre-condition: indexOf(obj) called Post-condition: obj removed

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 3 / 17

slide-15
SLIDE 15

Contracts for Concurrency

Contract (in concurrent setting)

A sequence of method calls which must be executed atomically.

Definition

Let ΣM be a set of all public method names (the API) of a software module (or library). A contract is a set R of clauses where each clause ̺ ∈ R is a regular expression over ΣM. A contract violation occurs if any of the sequences represented by the contract clauses is interleaved with an execution of methods from ΣM.

Contract for the java.util.ArrayList class (̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 4 / 17

slide-16
SLIDE 16

Contracts for Concurrency

Contract (in concurrent setting)

A sequence of method calls which must be executed atomically.

Definition

Let ΣM be a set of all public method names (the API) of a software module (or library). A contract is a set R of clauses where each clause ̺ ∈ R is a regular expression over ΣM. A contract violation occurs if any of the sequences represented by the contract clauses is interleaved with an execution of methods from ΣM.

Contract for the java.util.ArrayList class (̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 4 / 17

slide-17
SLIDE 17

Contracts for Concurrency

Contract (in concurrent setting)

A sequence of method calls which must be executed atomically.

Definition

Let ΣM be a set of all public method names (the API) of a software module (or library). A contract is a set R of clauses where each clause ̺ ∈ R is a regular expression over ΣM. A contract violation occurs if any of the sequences represented by the contract clauses is interleaved with an execution of methods from ΣM.

Contract for the java.util.ArrayList class (̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 4 / 17

slide-18
SLIDE 18

Extending Contracts with Parameters

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-19
SLIDE 19

Extending Contracts with Parameters

Motivation

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-20
SLIDE 20

Extending Contracts with Parameters

Motivation

void replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-21
SLIDE 21

Extending Contracts with Parameters

Motivation

void replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-22
SLIDE 22

Extending Contracts with Parameters

Motivation

void replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } a a

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-23
SLIDE 23

Extending Contracts with Parameters

Motivation

void replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-24
SLIDE 24

Extending Contracts with Parameters

Motivation

void replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } idx idx

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-25
SLIDE 25

Extending Contracts with Parameters

Motivation

void replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } void erase(Object c) { int idx; while ((idx=array.indexOf(c)) > -1) { array.remove(idx); } }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-26
SLIDE 26

Extending Contracts with Parameters

Motivation

void atomic replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } void atomic erase(Object c) { int idx; while ((idx=array.indexOf(c)) > -1) { array.remove(idx); } }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-27
SLIDE 27

Extending Contracts with Parameters

Motivation

void atomic replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } void atomic erase(Object c) { int idx; while ((idx=array.indexOf(c)) > -1) { array.remove(idx); } } void f() { Object x,y,z; ... replace(x,y); ... erase(z); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-28
SLIDE 28

Extending Contracts with Parameters

Motivation

void atomic replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } void atomic erase(Object c) { int idx; while ((idx=array.indexOf(c)) > -1) { array.remove(idx); } } void f() { Object x,y,z; ... replace(x,y); ... erase(z); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-29
SLIDE 29

Extending Contracts with Parameters

Motivation

void atomic replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } void atomic erase(Object c) { int idx; while ((idx=array.indexOf(c)) > -1) { array.remove(idx); } } void f() { Object x,y,z; ... replace(x,y); ... erase(z); } x array

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-30
SLIDE 30

Extending Contracts with Parameters

Motivation

void atomic replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } void atomic erase(Object c) { int idx; while ((idx=array.indexOf(c)) > -1) { array.remove(idx); } } void f() { Object x,y,z; ... replace(x,y); ... erase(z); } x array

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-31
SLIDE 31

Extending Contracts with Parameters

Motivation

void atomic replace(Object a, Object b) { if (array.contains(a)) { int idx=array.indexOf(a); array.set(idx,b); } } void atomic erase(Object c) { int idx; while ((idx=array.indexOf(c)) > -1) { array.remove(idx); } } void f() { Object x,y,z; ... replace(x,y); ... erase(z); } x array

Contract contains indexOf violated!

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-32
SLIDE 32

Extending Contracts with Parameters

Allows one to express how the flow of data influences the dependencies between methods Contract specification extended by considering

Method call parameters Return values

Expressed as meta-variables

Contract for the java.util.ArrayList class (̺′

1) contains(X) indexOf(X)

(̺′

2) X = indexOf( ) ( remove(X) | set(X, ) | get(X) )

(̺′

3) X = size() ( remove(X) | set(X, ) | get(X) )

(̺′

4) add(X) ( get(X) | indexOf(X) )

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-33
SLIDE 33

Extending Contracts with Parameters

Allows one to express how the flow of data influences the dependencies between methods Contract specification extended by considering

Method call parameters Return values

Expressed as meta-variables

Contract for the java.util.ArrayList class (̺′

1) contains(X) indexOf(X)

(̺′

2) X = indexOf( ) ( remove(X) | set(X, ) | get(X) )

(̺′

3) X = size() ( remove(X) | set(X, ) | get(X) )

(̺′

4) add(X) ( get(X) | indexOf(X) )

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 5 / 17

slide-34
SLIDE 34

Extending Contracts with Spoilers

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-35
SLIDE 35

Extending Contracts with Spoilers

Motivation

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-36
SLIDE 36

Extending Contracts with Spoilers

Motivation Contract for the java.util.ArrayList class

(̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-37
SLIDE 37

Extending Contracts with Spoilers

Motivation Contract for the java.util.ArrayList class

(̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-38
SLIDE 38

Extending Contracts with Spoilers

Motivation Contract for the java.util.ArrayList class

(̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Thread 1 contains indexOf Thread 2 contains Thread 3 indexOf Thread 4 set Thread 5 remove Thread 6 get Thread 7 size Thread 8 add Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-39
SLIDE 39

Extending Contracts with Spoilers

Motivation Contract for the java.util.ArrayList class

(̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Thread 1 contains indexOf Thread 2 contains Thread 3 indexOf Thread 4 set Thread 5 remove Thread 6 get Thread 7 size Thread 8 add

7 contract violations

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-40
SLIDE 40

Extending Contracts with Spoilers

Motivation Contract for the java.util.ArrayList class

(̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Thread 1 contains indexOf Thread 2 contains Thread 3 indexOf Thread 4 set Thread 5 remove Thread 6 get Thread 7 size Thread 8 add

7 contract violations

ΣM (methods) contains indexOf set remove get size add

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-41
SLIDE 41

Extending Contracts with Spoilers

Motivation Contract for the java.util.ArrayList class

(̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf )

Thread 1 contains indexOf Thread 2 contains Thread 3 indexOf Thread 4 set Thread 5 remove Thread 6 get Thread 7 size Thread 8 add

7 contract violations

ΣM (methods) contains indexOf set remove get size add

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-42
SLIDE 42

Extending Contracts with Spoilers

Motivation Contract for the java.util.ArrayList class

(̺1) contains indexOf (̺2) indexOf ( set | remove | get ) (̺3) size ( remove | set | get ) (̺4) add ( get | indexOf ) ΣM (methods) contains indexOf set remove get size add

Thread 1 contains indexOf Thread 2 contains Thread 3 indexOf Thread 4 set Thread 5 remove Thread 6 get Thread 7 size Thread 8 add

1 contract violation and 6 false alarms!

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-43
SLIDE 43

Extending Contracts with Spoilers

Allows one to express in which context the contract clauses shall be enforced

Definition

Let R be the set of target clauses where each target ̺ ∈ R is a regular expression over ΣM. Let S be the set of spoilers where each spoiler σ ∈ S is a regular expression over ΣM. A contract is then a relation C ⊆ R × S which defines for each target the spoilers that may cause atomicity violations.

Contract for the java.util.ArrayList class (̺′′

1 ) contains indexOf remove

(̺′′

2 ) indexOf (remove | set | get) remove | add | set

(̺′′

3 ) size (remove | set | get) remove

(̺′′

4 ) add indexOf remove | set

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-44
SLIDE 44

Extending Contracts with Spoilers

Allows one to express in which context the contract clauses shall be enforced

Definition

Let R be the set of target clauses where each target ̺ ∈ R is a regular expression over ΣM. Let S be the set of spoilers where each spoiler σ ∈ S is a regular expression over ΣM. A contract is then a relation C ⊆ R × S which defines for each target the spoilers that may cause atomicity violations.

Contract for the java.util.ArrayList class (̺′′

1 ) contains indexOf remove

(̺′′

2 ) indexOf (remove | set | get) remove | add | set

(̺′′

3 ) size (remove | set | get) remove

(̺′′

4 ) add indexOf remove | set

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-45
SLIDE 45

Extending Contracts with Spoilers

Allows one to express in which context the contract clauses shall be enforced

Definition

Let R be the set of target clauses where each target ̺ ∈ R is a regular expression over ΣM. Let S be the set of spoilers where each spoiler σ ∈ S is a regular expression over ΣM. A contract is then a relation C ⊆ R × S which defines for each target the spoilers that may cause atomicity violations.

Contract for the java.util.ArrayList class (̺′′

1 ) contains indexOf remove

(̺′′

2 ) indexOf (remove | set | get) remove | add | set

(̺′′

3 ) size (remove | set | get) remove

(̺′′

4 ) add indexOf remove | set

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 6 / 17

slide-46
SLIDE 46

Static Validation

Based on grammars and parsing trees Supports contracts with parameters only Analyses all executions of a program

May report false positives

Uses points-to information to handle multiple instances of a module Class Scope Mode

Allows the analysis to handle large programs Checks each class individually Calls to other classes are ignored

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 7 / 17

slide-47
SLIDE 47

Static Validation Algorithm

1 Require:

P: client’s program, R: module contract;

2 for t ∈ threads(P) do 3

Gt ← build grammar(t);

4

G′

t ← subword grammar(Gt); 5

for ̺ ∈ R do

6

T ← parse(G′

t , ̺); 7

for τ ∈ T do

8

N ← lowest common ancestor(τ, ̺);

9

if ¬run atomically(N) then return ERROR;

10 return OK;

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 8 / 17

slide-48
SLIDE 48

Illustration of the Static Validation Approach

void run() { if (cond) f(); else { m.indexOf(); g(); } } void atomic f() { m.indexOf(); g(); } void atomic g() { m.remove(); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 9 / 17

slide-49
SLIDE 49

Illustration of the Static Validation Approach

entry cond f() m.indexOf() g() return

R

entry m.indexOf() g() return entry m.remove() return

F G void run() { if (cond) f(); else { m.indexOf(); g(); } } void atomic f() { m.indexOf(); g(); } void atomic g() { m.remove(); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 9 / 17

slide-50
SLIDE 50

Illustration of the Static Validation Approach

entry cond f() m.indexOf() g() return

R

entry m.indexOf() g() return entry m.remove() return

F G R → a G R → F F → a G G → b void run() { if (cond) f(); else { m.indexOf(); g(); } } void atomic f() { m.indexOf(); g(); } void atomic g() { m.remove(); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 9 / 17

slide-51
SLIDE 51

Illustration of the Static Validation Approach

entry cond f() m.indexOf() g() return

R

entry m.indexOf() g() return entry m.remove() return

F G R → a G R → F F → a G G → b R → a G R → F

a → a a → ǫ

F → a G G → b

b → b b → ǫ

void run() { if (cond) f(); else { m.indexOf(); g(); } } void atomic f() { m.indexOf(); g(); } void atomic g() { m.remove(); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 9 / 17

slide-52
SLIDE 52

Illustration of the Static Validation Approach

entry cond f() m.indexOf() g() return

R

entry m.indexOf() g() return entry m.remove() return

F G R → a G R → F F → a G G → b R → a G R → F

a → a a → ǫ

F → a G G → b

b → b b → ǫ a b

G R F

a b

G R void run() { if (cond) f(); else { m.indexOf(); g(); } } void atomic f() { m.indexOf(); g(); } void atomic g() { m.remove(); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 9 / 17

slide-53
SLIDE 53

Experimental Results

Benchmark Clauses Contract False Potential Real SLOC Time (s) Violations Positives AV AV Allocate Vector 1 1 1 183 0.120 Coord03 4 1 1 151 0.093 Coord04 2 1 1 35 0.039 Jigsaw 1 1 1 100 0.044 Local 2 1 1 24 0.033 Knight 1 1 1 135 0.219 NASA 1 1 1 89 0.035 Store 1 1 1 621 0.090 StringBuffer 1 1 1 27 0.032 UnderReporting 1 1 1 20 0.029 VectorFail 2 1 1 70 0.048 Account 4 2 2 42 0.041 Arithmetic DB 2 2 2 243 0.272 Connection 2 2 2 74 0.058 Elevator 2 2 2 268 0.333 OpenJMS 0.7 6 54 10 28 4 163K 148 Tomcat 6.0 9 157 16 47 3 239K 3070 Cassandra 2.0 1 60 24 15 2 192K 246 Derby 10.10 1 19 5 7 1 793K 522 Lucene 4.6 3 136 21 76 478K 151

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 10 / 17

slide-54
SLIDE 54

Dynamic Validation

Based on happens-before relation and vector clocks Supports both contracts with spoilers and parameters Analyses a concrete execution of a program

If a contract is violated in the execution, it will be detected Extrapolation based on the happens-before relation Noise injection to force rare interleavings (executions)

On-the-fly validation

Uses a partial trace (trace window) Does not require a trace to be available Each thread needs to remember

1

Last instance of each spoiler

2

Last instance of each target

3

Up to |T| additional instances of each target

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 11 / 17

slide-55
SLIDE 55

Dynamic Validation Algorithm

Data: trace window υ, event e ∈ E generated by thread t ∈ T

1 if ∃̺ ∈ R, r ∈ [̺]υ t : e = end(r) then 2

for σ ∈ C(̺), u ∈ T : u t do

3

if ∃s ∈ [σ]υ

u : start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s) then r is violated by s ; 4

if ∃s ∈ [σ]τ

u : start(s) ∈ υ ∧ end(s) υ then 5

if start(s) ≺hb start(r) then

6

if ∃r′ ∈ [̺]υ

t : r′ r ∧ start(s) ⊀hb start(r′) then PV̺,σ t

(u) = VCend(r′)(t) ;

7

if ∃r′ ∈ [̺]υ

t : r′ r then υ → r′ ; 8 if σ ∈ S, s ∈ [σ]υ t : end(s) = e then 9

if ∃s′ ∈ [σ]υ

t : s′ s then υ → s′ ; 10

for ̺ ∈ C(σ), u ∈ T : u t do

11

if ∃r ∈ [̺]υ

u : start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s) then r is violated by s ; 12

if PV̺,σ

u

(t) 0 ∧ PV̺,σ

u

(t) ≤ VCend(s)(u) then

13

an instance of ̺ is violated by s;

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 12 / 17

slide-56
SLIDE 56

Illustration of the Dynamic Validation Approach

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-57
SLIDE 57

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-58
SLIDE 58

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Atomicity Violation?

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-59
SLIDE 59

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Atomicity Violation? No

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-60
SLIDE 60

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Atomicity Violation? No Data Race?

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-61
SLIDE 61

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Atomicity Violation? No Data Race?

lock(q) queue init unlock(q) lock(q) queue send unlock(q)

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-62
SLIDE 62

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Atomicity Violation? No Data Race?

lock(q) queue init unlock(q) lock(q) queue send unlock(q)

No

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-63
SLIDE 63

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Atomicity Violation? No Data Race?

lock(q) queue init unlock(q) lock(q) queue send unlock(q)

No Order Violation!

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-64
SLIDE 64

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Atomicity Violation? No Data Race?

lock(q) queue init unlock(q) lock(q) queue send unlock(q)

No Order Violation! Can we detect it using contracts?

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-65
SLIDE 65

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-66
SLIDE 66

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive} void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-67
SLIDE 67

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-68
SLIDE 68

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-69
SLIDE 69

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-70
SLIDE 70

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-71
SLIDE 71

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [2,0]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-72
SLIDE 72

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-73
SLIDE 73

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0]

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-74
SLIDE 74

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0]

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s) et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-75
SLIDE 75

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0]

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s) et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

[1,0] [1,1]

¬(1 ≤ 0) void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-76
SLIDE 76

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0]

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s) et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

[1,0] [1,1]

¬(1 ≤ 0)

[1,1] [2,0]

∧ ¬(2 ≤ 1) void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-77
SLIDE 77

Illustration of the Dynamic Validation Approach

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

Contract: thread start queue init

← {queue send, queue receive}

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0]

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s) et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

[1,0] [1,1]

¬(1 ≤ 0)

[1,1] [2,0]

∧ ¬(2 ≤ 1)

Contract violated!

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-78
SLIDE 78

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-79
SLIDE 79

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0] [1,0] [1,1]

¬(1 ≤ 0)

[1,1] [2,0]

∧ ¬(2 ≤ 1)

Contract violated!

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-80
SLIDE 80

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [1,1] [1,1] [2,0] [1,0] [1,1]

¬(1 ≤ 0)

[1,1] [2,0]

∧ ¬(2 ≤ 1)

Contract violated!

r

Extrapolation!

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-81
SLIDE 81

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-82
SLIDE 82

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [2,0] [2,0] [3,0] [2,1] [2,1] [2,2]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-83
SLIDE 83

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [2,0] [2,0] [3,0] [2,1] [2,1][2,1] [2,2]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-84
SLIDE 84

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] [2,0] [2,0] [3,0] [2,1] [2,1][2,1] [2,2] [1,0] [2,1]

¬(1 ≤ 0)

[2,1] [2,0]

∧ ¬(2 ≤ 2)

No violation

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-85
SLIDE 85

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] Noise

Noise injection!

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-86
SLIDE 86

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] Noise

Noise injection!

[2,1] [2,1] [3,1] [1,1] [1,1] [1,2]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-87
SLIDE 87

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] Noise

Noise injection!

[2,1] [2,1] [3,1] [1,1] [1,1] [1,2] [2,1] [2,1] [3,1] [1,1] [1,1][1,1] [1,2]

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-88
SLIDE 88

Illustration of the Dynamic Validation Approach

Contract: thread start queue init

← {queue send, queue receive}

et ≺hb eu ∼ VCet(t) ≤ VCeu(t)

Thread 1 (tmain) thread start thread create queue init queue receive Thread 2 (worker) thread start do work queue send lock(q) queue init unlock(q) lock(q) queue send unlock(q)

r r s

start(s) ⊀hb start(r) ∧ end(r) ⊀hb end(s)

[1,0] [0,1] [1,0] [2,0] [1,1] [1,1] Noise

Noise injection!

[2,1] [2,1] [3,1] [1,1] [1,1] [1,2] [2,1] [2,1] [3,1] [1,1] [1,1][1,1] [1,2] [1,0] [1,1]

¬(1 ≤ 0)

[1,1] [2,1]

∧ ¬(2 ≤ 1)

Contract violated!

void tmain() { thread_create(worker,input); queue_init(); result = queue_receive(); } void worker(void* data) { do_work(); queue_send(result); }

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 13 / 17

slide-89
SLIDE 89

Discarding Spoilers

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-90
SLIDE 90

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-91
SLIDE 91

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-92
SLIDE 92

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-93
SLIDE 93

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-94
SLIDE 94

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-95
SLIDE 95

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-96
SLIDE 96

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 a3 b3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-97
SLIDE 97

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 a3 b3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-98
SLIDE 98

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 a3 b3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-99
SLIDE 99

Discarding Spoilers

Thread 1 a1 b1 a2 Thread 2 x1 y1 x2 y2 Thread 3 x3 y3 Thread 4 x4 y4 a3 b3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 14 / 17

slide-100
SLIDE 100

Discarding Targets

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-101
SLIDE 101

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-102
SLIDE 102

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-103
SLIDE 103

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-104
SLIDE 104

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-105
SLIDE 105

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-106
SLIDE 106

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-107
SLIDE 107

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-108
SLIDE 108

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-109
SLIDE 109

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-110
SLIDE 110

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-111
SLIDE 111

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-112
SLIDE 112

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-113
SLIDE 113

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-114
SLIDE 114

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-115
SLIDE 115

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-116
SLIDE 116

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0] PV = [1,0,3,0]

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-117
SLIDE 117

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

PV = [1,0,3,0] x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-118
SLIDE 118

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

PV = [1,0,3,0] x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

y1 y2 Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-119
SLIDE 119

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

PV = [1,0,3,0] x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

y1 y2

[3,4,2,0] [0,1,3,0]

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-120
SLIDE 120

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

PV = [1,0,3,0] x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

y1 y2

[3,4,2,0] [0,1,3,0] b1 ⊀hb y1 b3 ⊀hb y2

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-121
SLIDE 121

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

PV = [1,0,3,0] x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

y1 y2

[3,4,2,0] [0,1,3,0] b1 ⊀hb y1 b3 ⊀hb y2 [3,4,2,0] [0,1,3,0] [0,1,0,0] [1,3,0,0] ¬(1 ≤ 4) ¬(3 ≤ 1)

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-122
SLIDE 122

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

PV = [1,0,3,0] x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

y1 y2

[3,4,2,0] [0,1,3,0] b1 ⊀hb y1 b3 ⊀hb y2 [3,4,2,0] [0,1,3,0] [0,1,0,0] [1,3,0,0] ¬(1 ≤ 4) ¬(3 ≤ 1)

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-123
SLIDE 123

Discarding Targets

Thread 1 x1 Thread 2 a1 b1 a2 b2 a3 b3 a4 b4 Thread 3 x2 Thread 4 x3

PV = [1,0,3,0] x1 ⊀hb a1 ∧ b1 ⊀hb y1 x2 ⊀hb a3 ∧ b3 ⊀hb y2 [0,1,0,0] [1,3,0,0]

y1 y2

[3,4,2,0] [0,1,3,0] b1 ⊀hb y1 b3 ⊀hb y2 [3,4,2,0] [0,1,3,0] [0,1,0,0] [1,3,0,0] ¬(1 ≤ 4) ¬(3 ≤ 1) x1 y1 cannot violate a1 b1 x2 y2 violated a3 b3

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 15 / 17

slide-124
SLIDE 124

Experimental Results

Benchmark T/S Contract False Potential Real SLOC Time (s) pairs Violations Positives AV AV Coord03 8 380 380 116 1.01 Coord04 4 24 24 53 0.52 Local 4 2 2 27 0.52 NASA 1 100 100 96 0.60 Account 1 176 176 54 0.53 Link Manager 2 1 1 1.5K 1.14 Chromium-1 2 2 2 7.5M 49.12

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 16 / 17

slide-125
SLIDE 125

Conclusion and Future Work

We have extended contracts for concurrency with

Parameters (flow of data) Spoilers (contextual information)

We have proposed two methods to validate such contracts

Static method based on grammars and parsing trees On-the-fly dynamic method based on happens-before relation and vector clocks

We have evaluated both of these methods on both simple as well as real-world programs Future work

Support for more parameters in the dynamic approach Support for spoilers in the static approach Combine the static and dynamic approaches Automatically derive contracts

Jan Fiedor (BUT) Verifying Programs using Contracts ICST, March 15, 2017 17 / 17