Top Banner
Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani
48

Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Jan 02, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Parameterized Verification of Thread-safe Libraries

Thomas BallSagar Chaki

Sriram K. Rajamani

Page 2: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Outline

Motivation Context – SLAM Models: LGFSMs and PLS Reachability for PLS Beacon and Rockall

Page 3: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Motivation Object-oriented (O-O) design very common

Libraries export a set of classes Internal state maintained via member variables

Multithreading becoming more prevalent O-O libraries designed to be thread-safe

Usually via some locking mechanism How can you check if your library is thread

safe?

Page 4: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Stack of integersclass IntStack {

private:public:

};

int *Data; int Size;

void Push(int newTop) {Data[Size++] = newTop;

}

int Pop(void) {if(Size > 0) return Data[--Size];else return –1;

}

Page 5: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Making it thread-safeclass IntStack {

private: int *Data; int Size;

public:void Push(int newTop) {

Data[Size++] = newTop;

}};

mutex Lock;

Lock.lock();

Lock.unlock();

Page 6: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

The eternal question

So you have made it thread-safe .. eh !

How do you know ?

Page 7: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

More precisely … How do you know the value of Size is

always non-negative ? Ok … let us make it simpler … Assuming at most two threads can

access the stack simultaneously, how do you answer the above question ?

Page 8: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

That’s easy …

main()Push()Pop()

Size

main()Push()Pop()

• main calls Push and Pop non-deterministically

• Push and Pop manipulate Size

• Allow lock and unlock operations

• Verify AG(Size >= 0)

Thread 1 Thread 2Shared

Page 9: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Can we model check this? Will not work in general

Specifically if you allow recursion Not even with just two threads Not even with just boolean variables The problem is undecidable

Page 10: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Outline

Motivation Context – SLAM Models: LGFSMs and PLS Reachability for PLS Beacon and Rockall

Page 11: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

The SLAM Toolkit

Program

SLIC

specification

Page 12: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

P’

B

Is ERROR reachable?

Yes, path p No

Return “No”Is p feasible in P?

Is ERROR reachable?

don’t know

No, explanation Yes

Return “Yes”, pReturn “don’t know”

Bebop

Newton

C2bp

Predicates from instrumentation

Page 13: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Model checking multi-threaded boolean programs

B1 | B2

Given two CFGs G1 and G2, whether they have a non-empty intersection is undecidable

You can reduce this question to a safety (reachability) question for two boolean programs which communicate via shared variables and interleave arbitrarily

Page 14: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

One solution: FSM abstactions

B1 F1

B2 F2

F1 | F2

B1 | B2

Page 15: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

FSM Abstractions Number of threads not known in

advance Possible solutions

Model check a finite number of threads

Use parameterized model checking and handle an arbitrary number of threads

Page 16: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

So now what ? Number of threads not known in

advance Possible solutions

Model check a finite number of threads Blows up pretty quickly

Use parameterized model checking and handle an arbitrary number of threads

Can handle some examples even when instantiation to finite number of threads blows up!!

Page 17: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Model checking multi-threaded boolean programs

B F

F*

B*

Rest of the talk is on this step

Page 18: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Outline

Motivation Context – SLAM Models: LGFSMs and PLS Reachability for PLS Beacon and Rockall

Page 19: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Local-Global FSM

L1

G1

L2

G2

L1

G3

Page 20: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

L1

G1

L1

L1

G1

L2

G2

L1

G3

Page 21: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

L1

G1

L1

L2

G2

L1

L1

G1

L2

G2

L1

G3

Page 22: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

L1

G1

L1

L2

G2

L1

L1

G2

L2

L1

G1

L2

G2

L1

G3

Page 23: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

L1

G1

L1

L2

G2

L1

L1

G2

L2

L1

G3

L1

L1

G1

L2

G2

L1

G3

Page 24: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

L1

G1

L1

L2

G2

L1

L1

G2

L2

L1

G3

L1

L1

G1

L2

G2

L1

G3

Page 25: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Parameterized Library System Given a LGFSM F and a positive integer

n, F(n) denotes a library system composed of n copies of F n copies of L initialized to IL 1 copy of G initialized to IG Global configuration : [g,l1, … , ln] Each copy of F executes asynchronously but

only one copy executes at a time

Page 26: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Outline

Motivation Context – SLAM Models: LGFSMs and PLS Reachability for PLS Beacon and Rockall

Page 27: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

PLS Reachability Global state g is reachable in F(n) if

and only if some configuration [g,l1, … ln] is reachable

Given a LGFSM F and a global state g, is there a positive integer n such that g is reachable in F(n) ? Parameterized reachability problem

This problem is decidable

Page 28: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

L1G1

L1

L2G2

L1

L1G2

L2

L1G3

L1

Page 29: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

L1G1

L1

L2G2

L1

L1G2

L2

L1G3

L1

(Global state, #of processes in L1, #of processes in L2)

(G1,2,0)

(G2,1,1)

(G2,1,1)

(G3,2,0)

Configurations: an alternate representation

Page 30: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Relations on configurations (g,l1, … , ln) = (g’,l1’, … , ln’) iff

g = g’ and li = li’ for each I

(g,l1, … , ln) (g’,l1’, … , ln’) iff g = g’ and li li’ for each i

C < C’ iff C C’ and C != C’

Page 31: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Reachability on configurations Start with initial configuration

Keep exploring new configurations from current configurations.

Follow two rules!

Page 32: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Rule 1

If C1 is a new configuration and there exists a reachable configuration C2 such that C1 C2

drop C1 !

Page 33: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Example

(g,1,2)

(g,0,2)

Page 34: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Example

(g,1,2)

Page 35: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Example

(g,1,2)

(…)

Page 36: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Rule 2

If C1 is a new configuration and there exists an ancestor C2 such that C2 C1

Replace ancestor with “*”s appropriately !

Page 37: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Example

(g,1,2)

(g,1,3)

Page 38: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Example

(g,1,*)

Page 39: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Example

(g,1,*)

(…)

Page 40: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Claim The procedure is sound and complete

Every reachable state is discovered Every discovered state is reachable

The procedure always terminates Dickson’s lemma

Page 41: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Complexity The reachability tree traversal can take non-

primitive recursive space in # of local states Space lower bound O(2^sqrt(n))

Lipton 1976 Algorithm with space O(2^n.log(n)) exists

Rackoff 1978 Impractical – most people use some form of

reachability tree traversal Closely related with Petri nets and VAS

Coverability problem for Petri nets

Page 42: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Outline

Motivation Context – SLAM Models: LGFSMs and PLS Reachability for PLS Beacon and Rockall

Page 43: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Beacon An implementation of the above

algorithm Inputs : a LGFSM and a global state

transitions expressed by guarded commands

Output : answer to the parameterized reachability problem

Page 44: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Implementation tricks Construct reachability tree (instead of

graph) Need to check subsumption only on parents

along one path Represent * as largest unsigned integer

Check for overflows Data structure tricks to check

subsumption quickly Represent state sets as trees as well

Page 45: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Rockall Thread-safe O-O memory manager

developed at MSR by Michael Parkes

Memory organized in buckets Each bucket manages memory blocks of

a given size Buckets arranged in a tree like hierarchy.

Each bucket gets memory from a bigger bucket

Topmost bucket gets memory from the OS Several locks are used

Page 46: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Rockall LGFSM was constructed manually

Worst case # of steps O(10 ^ 10 ^ 600)

Checked that no memory location was allocated or freed more than once in a row 4 hours on a 800 MHz Pentium III with 512 MB Explored 2 million configurations Finite version with 4 threads did not terminate

with SMV !!

Page 47: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

What about Java? Parameterized verification of Java

libraries Can model:

Synchronized Wait Notify

Cannot model Notify-all Can be handled using backward reachability

starting with updward closed sets

Page 48: Parameterized Verification of Thread-safe Libraries Thomas Ball Sagar Chaki Sriram K. Rajamani.

Conclusions Applying parameterized verification to a

new domain Major challenge is getting an LGFSM from

source code Manual construction mostly infeasible

Efficient implementation - BEACON Lots of other optimizations could be tried

Work in relation to coverability graphs for PNs