Runtime Atomicity Analysis of Multi-threaded Programs Focus is on the paper: “Atomizer: A Dynamic Atomicity Checker for Multithreaded Programs” by C. Flanagan.

Slides:



Advertisements
Similar presentations
Bounded Model Checking of Concurrent Data Types on Relaxed Memory Models: A Case Study Sebastian Burckhardt Rajeev Alur Milo M. K. Martin Department of.
Advertisements

Threads Cannot be Implemented As a Library Andrew Hobbs.
50.530: Software Engineering Sun Jun SUTD. Week 13: Rely-Guarantee Reasoning.
Goldilocks: Efficiently Computing the Happens-Before Relation Using Locksets Tayfun Elmas 1, Shaz Qadeer 2, Serdar Tasiran 1 1 Koç University, İstanbul,
D u k e S y s t e m s Time, clocks, and consistency and the JMM Jeff Chase Duke University.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Concurrency 101 Shared state. Part 1: General Concepts 2.
/ PSWLAB Concurrent Bug Patterns and How to Test Them by Eitan Farchi, Yarden Nir, Shmuel Ur published in the proceedings of IPDPS’03 (PADTAD2003)
Atomizer: A Dynamic Atomicity Checker For Multithreaded Programs Stephen Freund Williams College Cormac Flanagan University of California, Santa Cruz.
Atomicity in Multi-Threaded Programs Prachi Tiwari University of California, Santa Cruz CMPS 203 Programming Languages, Fall 2004.
/ PSWLAB Atomizer: A Dynamic Atomicity Checker For Multithreaded Programs By Cormac Flanagan, Stephen N. Freund 24 th April, 2008 Hong,Shin.
CS 263 Course Project1 Survey: Type Systems for Race Detection and Atomicity Feng Zhou, 12/3/2003.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
“THREADS CANNOT BE IMPLEMENTED AS A LIBRARY” HANS-J. BOEHM, HP LABS Presented by Seema Saijpaul CS-510.
CS533 Concepts of Operating Systems Class 3 Data Races and the Case Against Threads.
C. FlanaganSAS’04: Type Inference Against Races1 Type Inference Against Races Cormac Flanagan UC Santa Cruz Stephen N. Freund Williams College.
C. FlanaganDynamic Analysis for Atomicity1. C. Flanagan2Dynamic Analysis for Atomicity Atomicity The method inc() is atomic if concurrent threads do not.
C. Flanagan1Atomicity for Reliable Concurrent Software - PLDI'05 Tutorial Atomicity for Reliable Concurrent Software Joint work with Stephen Freund Shaz.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Verifying Commit-Atomicity Using Model Checking Cormac Flanagan University of California, Santa Cruz.
CS510 Concurrent Systems Class 5 Threads Cannot Be Implemented As a Library.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Cormac Flanagan UC Santa Cruz Velodrome: A Sound and Complete Dynamic Atomicity Checker for Multithreaded Programs Jaeheon Yi UC Santa Cruz Stephen Freund.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
/ PSWLAB Eraser: A Dynamic Data Race Detector for Multithreaded Programs By Stefan Savage et al 5 th Mar 2008 presented by Hong,Shin Eraser:
C. FlanaganType Systems for Multithreaded Software1 Cormac Flanagan UC Santa Cruz Stephen N. Freund Williams College Shaz Qadeer Microsoft Research.
Runtime Refinement Checking of Concurrent Data Structures (the VYRD project) Serdar Tasiran Koç University, Istanbul, Turkey Shaz Qadeer Microsoft Research,
Eraser: A Dynamic Data Race Detector for Multithreaded Programs STEFAN SAVAGE, MICHAEL BURROWS, GREG NELSON, PATRICK SOBALVARRO, and THOMAS ANDERSON Ethan.
/ PSWLAB Type-Based Race Detection for J AVA by Cormac Flanagan, Stephen N. Freund 22 nd Feb 2008 presented by Hong,Shin Type-Based.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Shared Memory Consistency Models. SMP systems support shared memory abstraction: all processors see the whole memory and can perform memory operations.
Memory Consistency Models. Outline Review of multi-threaded program execution on uniprocessor Need for memory consistency models Sequential consistency.
Data races, informally [More formal definition to follow] “race condition” means two different things Data race: Two threads read/write, write/read, or.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
HARD: Hardware-Assisted lockset- based Race Detection P.Zhou, R.Teodorescu, Y.Zhou. HPCA’07 Shimin Chen LBA Reading Group Presentation.
/ PSWLAB Thread Modular Model Checking by Cormac Flanagan and Shaz Qadeer (published in Spin’03) Hong,Shin Thread Modular Model.
Specifying Multithreaded Java semantics for Program Verification Abhik Roychoudhury National University of Singapore (Joint work with Tulika Mitra)
Eraser: A dynamic Data Race Detector for Multithreaded Programs Stefan Savage, Michael Burrows, Greg Nelson, Patrick Sobalvarro, Thomas Anderson Presenter:
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Using Escape Analysis in Dynamic Data Race Detection Emma Harrington `15 Williams College
Threads Cannot Be Implemented As a Library
CS533 Concepts of Operating Systems Class 3
Effective Data-Race Detection for the Kernel
Chapter 5: Process Synchronization
Specifying Multithreaded Java semantics for Program Verification
Threads and Memory Models Hal Perkins Autumn 2011
Atomicity in Multithreaded Software
Threads and Memory Models Hal Perkins Autumn 2009
CSCI1600: Embedded and Real Time Software
CS533 Concepts of Operating Systems Class 3
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
Relaxed Consistency Finale
CSCI1600: Embedded and Real Time Software
Eraser: A dynamic data race detector for multithreaded programs
Presentation transcript:

Runtime Atomicity Analysis of Multi-threaded Programs Focus is on the paper: “Atomizer: A Dynamic Atomicity Checker for Multithreaded Programs” by C. Flanagan and S. Freund presented by Sebastian Burckhardt University of Pennsylvania CIS 700 – Runtime Verification Seminar Wednesday, October 20, 2004

-2- Outline of talk Verification of multithreaded programs in general Atomizer: the core concepts –Dynamic analysis –Reduction –Lock set algorithm Atomizer: the improvements Atomizer: evaluation

-3- Correctness of Multithreaded Programs “Multithreaded” means: concurrent, communication by shared memory Reasoning quite challenging even for experts Typically, programmers use fairly low-level synchronization primitives –Mutex, Locks –Semaphores –Monitors (re-popularized by Java) To make it worse, performance matters (otherwise, why bother with multiple threads?)

-4- Non-dynamic verification We won’t talk about these today. –Restrict design space type systems special-purpose languages Design paradigms –Static analysis Lexical Control flow Data flow

-5- Checking concurrent executions Problem: number of possible concurrent executions very large Approach I: Check them all –means: model check the concurrent model –not practical without heavy abstraction Approach II: Check just one –this is the regular “testing” method Approach III: Check one, and extrapolate –look for bad things that “could” happen

-6- What are the bad things we can look for? Deadlock Races –Definition of “race”: Two threads are allowed to access same variable at the same time, and at least one access is a write View inconsistency –intuitive description: grouping of variables inconsistent among threads Lack of atomicity

-7- What are we looking for? Deadlock –look for inconsistent order of lock acquisition Races –look for variables that aren’t consistently protected by some lock, by tracking locks held during each access (e.g. “Eraser” Lockset alg) View inconsistency –track variable sets associated which each lock (e.g. in JPaX, JNuke) Atomicity –Reduction-based (e.g. Atomizer) –Block based (e.g. Wang/Stoller’s tool)

-8- Atomicity Checking: Advantages Can find bugs that are resistant to regular testing, and race detection Good correspondence with programming methodology –easy to understand the idea –can verify interfaces, encouraging code reuse –programmer can gain confidence in code by validating atomicity assumptions Scalable –has been applied to >100k lines of Java code

-9- Example: java.lang.StringBuffer public final class StringBuffer { public synchronized StringBuffer append(StringBuffer sb) { int len = sb.length();... sb.getChars(0, len, value, count);... } public synchronized int length() {... } public synchronized void getChars(...) {... } }

-10- Example: java.lang.StringBuffer public final class StringBuffer { public synchronized StringBuffer append(StringBuffer sb) { int len = sb.length();... // another thread can modify sb here... // => len is no longer the correct length of len... // but there is no race. sb.getChars(0, len, value, count);... } public synchronized int length() {... } public synchronized void getChars(...) {... } }

-11- Definition A block of code is ‘atomic’ if for every legal execution of the program, there is an equivalent legal execution within which the entire block executes without preemption. Executions are “equivalent” iff –the (dynamic) instruction stream per thread is identical –the same read reads the value of the same write

-12- How does it work? (1) Identify blocks that are supposed to be atomic –use heuristics exported methods synchronized methods synchronized blocks –allow user annotations can ‘turn off’ the checking if there are false bugs can do additional checks by declaring atomic /*# atomic */ void getChars() {... }

-13- How does it work? (2) Perform instrumentation on the source code level –could also be done at the bytecode level –Instrumented source code produces event stream during execution Analyze event stream on-line (Atomizer) or off- line. –For each block that is supposed to be atomic, check whether there is an equivalent execution in which it is scheduled contiguously.

-14- How does it work? (3) We can’t possibly check all possible executions to find an equivalent atomic one Idea: Find a large class of instruction sequences for which we can always guarantee that it can be shuffled into an uninterrupted sequence by local, pairwise swaps. Then, warn user if supposedly atomic block does not belong to this class -> Lipton’s theory of reduction (1975)

-15- Semantic model Dynamic instruction stream of each thread consists of 4 types of instructions: –rd(x,v)read value v from shared var x –wr(x,v)write value v to shared var x –acq(m)acquire lock m –rel(m)release lock m

-16- Left-movers Can always swap an rel(m) with an interleaved instruction j1 of another thread to its left. Call this a left-mover. Reason –can always release lock earlier –read/write matching not affected by move j0 j1 j2 i0 rel(m)i2 j0 j1 j2 i0 rel(m)i2

-17- Right-movers Can always swap an acq(m) with an interleaved instruction j1 of another thread to its right. Call this a right-mover. Reason –lock is still available (j1 can not be acq(m)) –read/write matching not affected by move j0 j1 i0acq(m)i2 j0 j1 i0 acq(m)i2

-18- Non-movers Neither rd(x,v) nor wr(x,v) can in general be swapped with an adjacent interleaved instruction of another thread. Call them non-movers. wr(x,v) rd(x,v) wr(x,v) rd(x,v) wr(x,v)

-19- Both-movers If an access rd(x,v),wr(x,v) goes to a variable protected by a lock which is held by this thread, it is a both-mover. Reason –j1 can not be an access to x Suppose for now we know which locks can protect a variable j1 acq(m) rd(x,v) rel(m)

-20- Lipton’s Reduction Let’s denote the instructions as follows: L for left-mover, R for right-mover, N for non-mover, B for both-mover Then any execution sequence matching the following regular expression is equivalent to an atomic one: (R + B)* (N +  ) (L + B)* Examples: RL RBL NLLLB RNL BBB But not: NN LR

-21- Say the method “copy()” produces the dynamic instruction stream is it atomic? –For now, assume all methods of class A are synchronized Example b1 acq(m) rd(x,4) wr(y,4) rel(m) b3 public class A { private int x, y; public synchronized void copy() { y = x; }... (more methods)... }

-22- Example acq(m) b1 rd(x,4) b2 wr(y,4) b3 rel(m) acq(m) b1 rd(x,0) b2 wr(x,1) b3 rel(m) b1 acq(m) rd(x,4) wr(y,4) b2 rel(m) b3 b1 acq(m) rd(x,0) wr(x,1) b2 rel(m) b3 b1 acq(m) rd(x,4) wr(y,4) rel(m) b3 b1 acq(m) rd(x,0) wr(x,1) rel(m) b2 b3

-23- Implementation Can efficiently check if blocks match (R + B)* (N +  ) (L + B)* by using an online automaton. Problem: to classify variable accesses correctly, we need to know which locks protect which

-24- Which locks with which field? fields may not be protected by this object’s lock public class A2 { private int x,y; public synchronized swap() { int z = y; y = x; x = z;} public int getX() { return x; } public int getY() { return y; }... } field may be protected by a different object’s lock public class A2 { private int x,y; Integer mylock = new Integer(0); public copy() { synchronized(mylock) { y = x; } } public int getX() { synchronized(mylock) { return x; } } public int getY() { synchronized(mylock) { return x; } }... }

-25- Basic “Eraser” lockset algorithm Argue: “If a variable is consistently protected by some lock, this lock must be held during all accesses to that variable” Dynamically, we can look at the set of locks helds during each access so far, and keep track of their intersection –If the intersection is empty, there seems to be no consistent locking discipline - classify access as a non-mover –Otherwise, there seems to be a consistent locking discipline - classify access as a both-mover What about re-classifying accesses if changes occur during runtime? –can’t be done on-line, but could be done off-line

-26- Improve Classification (1) Avoid flagging some classic, safe usages –thread-local variables: need no lock to protect them –initialization: one thread initializes data, then passes it to another thread, thread-local from there on –Write once, read many times Track state for each field –use lock set for classification only if in state Shared Modified

-27- Improve Classification (2) Re-entrant locks –re-entrant acquires and releases are both-movers Redundant locks –if a lock is only accessed by one thread, it is redundant (thread-local locks) –if lock B is always held while accessing lock A, lock A is redundant (protected locks) –redundant acquires and releases are both-movers –can classify locks using the same lockset and thread-access algorithms as for fields

-28- Improve Classification (3) “Benign” read/write races public class A2 { private int x; public int read() { return x; } synchronized void inc() { x = x + 1; } } read() and inc() are atomic... (more or less) –track separate lockset containing locks held during all writes (= superset of locks held during all accesses) –classify read as both-mover if current thread holds a write lock, even if access-protecting lockset is empty

-29- It’s not that easy Unsynchronized reads and writes –are not atomic if more than 32 bit quantity more rules exists (e.g. volatile vs. non-volatile) –are not guaranteed to proceed in order only synchronization events are sequentially consistent. memory model relative to hardware is specified (?) memory model of hardware is not specified. does anybody know? –does Atomizer need adjustments for non- sequentially consistent machines?

-30- Evaluation

-31- Effect of improvements

-32- Atomizer paper: contributions Concise review of concepts –Formal semantics for multithreaded programs –Reduction idea, Lockset algorithm Description of the algorithm and some improvements –Formal description of the algorithm, formulation of theorem describing its correctness, in provable detail –Mentions optimizations: handle re-entrant locks, thread-local locks, protected Locks, write-protected data Experimental evaluation of the tool –performance, scale, usability

-33- Bibliography