SlideShare a Scribd company logo
8th Central and Eastern European
Software Engineering Conference
in Russia - CEE-SECR 2012
November 1 - 2, Moscow



     Dynamic data race detection in
       concurrent Java programs

                  Vitaly Trifanov, Dmitry Tsitelov
                           Devexperts LLC
Agenda
What are data races and why they are dangerous


Automatic races detection
  approaches, pros & cons


Happens-before race detection algorithm
  Vector clocks


Our dynamic race detector
  implementation
  solved problems
Data Race Example
public class Account {
    private int amount = 0;
    public void deposit(int x) {amount += x;}
    public int getAmount() {return amount;}
}


public class TestRace {
        public static void main (String[] args) {
         final Account a = new Account();
         Thread t1 = depositAccountInNewThread(a, 5);
         Thread t2 = depositAccountInNewThread(a, 6);
         t1.join();
         t2.join();
         System.out.println(account.getAmount()); //may print 5, 6, 11.
    }
}
Expected Execution
Racy Execution
Data Races

Data race occurs when many threads access the same
  shared data concurrently; at least one writes




Usually it’s a bug
Data Races Are Dangerous

Hard to detect if occurred
  no immediate effects
  program continues to work
  damage global data structures


Hard to find manually
  Not reproducible - depends on threads timing
  Dev & QA platforms are not so multicore
Automatic Race Detection
20+ years of research


Static
  analyze program code offline
  data races prevention (extend type system, annotations)


Dynamic: analyze real program executions
  On-the-fly
  Post-mortem
Dynamic Detectors vs Static
Static Approach
Pros
  Doesn’t require program execution
  Analyzes all code
  Doesn’t depend on program input, environment, etc.


Cons
  Unsolvable in common case
  Has to reduce depth of analysis


A lot of existing tools for Java
  FindBugs, jChord, etc
Dynamic Approach
Pros
  Complete information about program flow
  Lower level of false alarms


Cons
  Very large overhead


No existing stable dynamic detectors for Java
Static vs Dynamic: What To Do?
Use both approaches 


Static (FindBugs/Sonar, jChord, …)
  Eliminate provable synchronization inconsistencies
   on the early stage

Dynamic
  Try existing tools, but they are unstable
     IBM MSDK, Thread Sanitizer for Java
  That’s why we’ve developed our own!
Data Race Detector Concept
Application uses libraries and frameworks via API
  At least JRE


API is well documented
  “Class XXX is thread-safe”
  “Class YYY is not thread-safe”
  “XXX.get() is synchronized with preceding call of XXX.set()”


Describe behavior of API and exclude library from
 analysis
DRD: How It’s Organized
What Operations to Intercept?

Synchronization operations
  thread start/join/interrupt
  synchronized
  volatile read/write
  java.util.concurrent


Accesses to shared data
  fields
  objects
How It Works


                             Instrumented
Application
                 DRD agent    app classes
 classes
                              interceptor




                                Race
                  Config      detection
                               module
JLS: Publishing Data



  Publish changes




                    Receive changes
JLS: Synchronized-With Relation

“Synchronized-with” relation
  unlock monitor M ↦ all subsequent locks on M
  volatile write ↦ all subsequent volatile reads
  …




Notation: send ↦ receive
JLS: Happens-Before & Data Races

X happens-before Y, when
  X, Y - in same thread, X before Y in program order
  X is synchronized-with Y
  Transitivity: exists Z: hb(X, Z) && hb(Z, Y)


Data race: 2 conflicting accesses, not ordered by
  happens-before relation
Happens-Before Example




No data race
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock
Vector Clock


        Not ordered!
          A: 3 > 2
          B: 3 < 4
How It Works. No Data Race Example
How It Works. Data Race Example
Code Instrumentation
Check everything => huge overhead


Race detection scope
  Accesses to our fields
  Foreign calls (treat them as read or write)


Sync scope
  Detect sync events in our code
  Describe contracts of excluded classes
  Treat these contracts as synchronization events
Race Detection
private class Storage {
   private Map<Integer, Item> items = new HashMap<Integer, Item> ();

    public void store(Item item) {
        items.put(item.getId(), item);
    }

    public void saveToDisk() {                 On each access of “items” field we check
                                               On each access of “items” field we check
        for (Item item : items.values()) {
               //serialize and save                      race on this field
                                                          race on this field
               saveItem(item);
               //...
        }
    }
                                               On each call of “items” method we check
                                               On each call of “items” method we check
    public Item getItem(int id) {                       race on this object
                                                         race on this object
        return items.get(id);
    }

    public void reload() {
       items = deserealizeFromFile();            Each field of class Item is protected the
                                                 Each field of class Item is protected the
    }                                          same way as field “items” of class Storage
                                                same way as field “items” of class Storage
}
Synchronization Contract Example
Clocks Storing
Thread clock
  ThreadLocal<VectorClock >


Field XXX
  volatile transient VectorClock XXX_vc;


Foreign objects, monitors
   WeakIdentityConcurrentHashMap<Object,VectorClock>


 Volatiles, synchronization contracts
   ConcurrentHashMap <???, VectorClock>
Composite Keys
 AtomicLongFieldUpdater.CAS(Object o, long offset, long v)
   param 0 + param 1


 Volatile field “abc” of object o
   object + field name


 AtomicInteger.set() & AtomicInteger.get()
   object


 ConcurrentMap.put(key, value) & ConcurrentMap.get(key)
   object + param 0
Solved Problems
Composite keys for contracts and volatiles
  Generate them on-the-fly


Avoid unnecessary keys creation
  ThreadLocal<MutableKeyXXX> for each CompositeKeyXXX


Loading of classes, generated on-the-fly
  Instrument ClassLoader.loadClass()
Solved Problems
Don’t break serialization
  compute serialVersiodUid before instrumentation


Caching components of dead clocks
  when thread dies, its time frames doesn’t grow anymore
  cache frames of dead threads to avoid memory leaks
  local last-known generation & global generation
DRD in Real Life: QD


                ✔ 6 races found


  QD




QD + DRD
DRD in Real Life: MARS UI


  MARS

                 ✔ 5 races found




MARS + DRD
DRD Race Report Example
WRITE_READ data race between current thread Thread-12(id = 33) and thread
  Thread-11(id = 32)


Race target : field my/app/DataServiceImpl.stopped


Thread 32 accessed it in my/app/DataServiceImpl.access$400(line : 29)


--------------------------------Stack trace for racing thread (id = 32) is not available.------------


-------------------------------------Current thread's stack trace (id = 33) : ---------------------------
   at my.app.DataServiceImpl.stop(DataServiceImpl.java:155)
   at my.app.DataManager.close(DataManager.java:201)
   ...
DRD Advantages
Doesn’t break serialization


No memory leaks


Few garbage


No JVM modification


Synchronization contracts
  very important: Unsafe, AbstractQueuedSynchronizer
Links
http://code.devexperts.com/display/DRD/:
 documentation, links, etc
Contact us: drd-support@devexperts.com


Useful links: see also on product page
  IBM MSDK
  ThreadSanitizer for Java
  jChord
  FindBugs


  JLS «Threads and locks» chapter
Q&A

More Related Content

PPTX
Core java concepts
PDF
Java Day-6
PPTX
Joshua bloch effect java chapter 3
PDF
OOPs & Inheritance Notes
PDF
Automated Discovery of Deserialization Gadget Chains
PPT
Serialization/deserialization
PDF
Java Serialization Deep Dive
PPT
Objective c
Core java concepts
Java Day-6
Joshua bloch effect java chapter 3
OOPs & Inheritance Notes
Automated Discovery of Deserialization Gadget Chains
Serialization/deserialization
Java Serialization Deep Dive
Objective c

What's hot (20)

PPTX
Java I/O and Object Serialization
PDF
Csharp_Chap03
PPT
Core java concepts
PPTX
PPT
Core Java Concepts
DOCX
Jist of Java
DOCX
JAVA CONCEPTS AND PRACTICES
DOC
Serialization in .NET
PPTX
Static analysis: Around Java in 60 minutes
PPT
Core java concepts
PPTX
Let's talk about jni
PDF
04. constructor & destructor
PPT
Chapter 4 - Defining Your Own Classes - Part I
PPT
Chapter 7 - Defining Your Own Classes - Part II
PDF
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
ODP
Quick introduction to Java Garbage Collector (JVM GC)
PDF
A Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
PPTX
Collections
PDF
PDF
Procedure Typing for Scala
Java I/O and Object Serialization
Csharp_Chap03
Core java concepts
Core Java Concepts
Jist of Java
JAVA CONCEPTS AND PRACTICES
Serialization in .NET
Static analysis: Around Java in 60 minutes
Core java concepts
Let's talk about jni
04. constructor & destructor
Chapter 4 - Defining Your Own Classes - Part I
Chapter 7 - Defining Your Own Classes - Part II
Java/Scala Lab 2016. Григорий Кравцов: Реализация и тестирование DAO слоя с н...
Quick introduction to Java Garbage Collector (JVM GC)
A Runtime Monitoring Framework for Event Streams with Non-Primitive Arguments
Collections
Procedure Typing for Scala
Ad

Viewers also liked (9)

PPTX
Browsers. Magic is inside.
PDF
Windows, doors and secret passages: approaches to the space organization in t...
PDF
20130420 bitbyte
PPTX
Effective websites development
PPT
How to improve java performance
PDF
Codefreeze rus
PDF
Codefreeze eng
PPT
Dynamic data race detection in concurrent Java programs
PDF
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
Browsers. Magic is inside.
Windows, doors and secret passages: approaches to the space organization in t...
20130420 bitbyte
Effective websites development
How to improve java performance
Codefreeze rus
Codefreeze eng
Dynamic data race detection in concurrent Java programs
ACM ICPC 2014 NEERC (Northeastern European Regional Contest) Problems Review
Ad

Similar to Drd secr final1_3 (20)

PPT
Java Performance Tuning
PPT
.NET Reflection
PPTX
Java Unit Test and Coverage Introduction
PPTX
Object Oriented Code RE with HexraysCodeXplorer
PDF
How to write clean & testable code without losing your mind
PPTX
Java Language fundamental
PPTX
Lambdas puzzler - Peter Lawrey
PPT
JavaSecure
PPT
04 Data Access
PPT
Java14
PDF
Jvm internals
PDF
Tools and Techniques for Understanding Threading Behavior in Android
PPTX
NET Systems Programming Learned the Hard Way.pptx
PDF
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
PDF
Sperasoft‬ talks j point 2015
PDF
Automated Discovery of Deserialization Gadget Chains
PDF
SNMP Project: SNMP-based Network Anomaly Detection Using Clustering
PPT
Reversing JavaScript
PDF
findbugs Bernhard Merkle
Java Performance Tuning
.NET Reflection
Java Unit Test and Coverage Introduction
Object Oriented Code RE with HexraysCodeXplorer
How to write clean & testable code without losing your mind
Java Language fundamental
Lambdas puzzler - Peter Lawrey
JavaSecure
04 Data Access
Java14
Jvm internals
Tools and Techniques for Understanding Threading Behavior in Android
NET Systems Programming Learned the Hard Way.pptx
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Sperasoft‬ talks j point 2015
Automated Discovery of Deserialization Gadget Chains
SNMP Project: SNMP-based Network Anomaly Detection Using Clustering
Reversing JavaScript
findbugs Bernhard Merkle

Drd secr final1_3

  • 1. 8th Central and Eastern European Software Engineering Conference in Russia - CEE-SECR 2012 November 1 - 2, Moscow Dynamic data race detection in concurrent Java programs Vitaly Trifanov, Dmitry Tsitelov Devexperts LLC
  • 2. Agenda What are data races and why they are dangerous Automatic races detection approaches, pros & cons Happens-before race detection algorithm Vector clocks Our dynamic race detector implementation solved problems
  • 3. Data Race Example public class Account { private int amount = 0; public void deposit(int x) {amount += x;} public int getAmount() {return amount;} } public class TestRace { public static void main (String[] args) { final Account a = new Account(); Thread t1 = depositAccountInNewThread(a, 5); Thread t2 = depositAccountInNewThread(a, 6); t1.join(); t2.join(); System.out.println(account.getAmount()); //may print 5, 6, 11. } }
  • 6. Data Races Data race occurs when many threads access the same shared data concurrently; at least one writes Usually it’s a bug
  • 7. Data Races Are Dangerous Hard to detect if occurred no immediate effects program continues to work damage global data structures Hard to find manually Not reproducible - depends on threads timing Dev & QA platforms are not so multicore
  • 8. Automatic Race Detection 20+ years of research Static analyze program code offline data races prevention (extend type system, annotations) Dynamic: analyze real program executions On-the-fly Post-mortem
  • 10. Static Approach Pros Doesn’t require program execution Analyzes all code Doesn’t depend on program input, environment, etc. Cons Unsolvable in common case Has to reduce depth of analysis A lot of existing tools for Java FindBugs, jChord, etc
  • 11. Dynamic Approach Pros Complete information about program flow Lower level of false alarms Cons Very large overhead No existing stable dynamic detectors for Java
  • 12. Static vs Dynamic: What To Do? Use both approaches  Static (FindBugs/Sonar, jChord, …) Eliminate provable synchronization inconsistencies on the early stage Dynamic Try existing tools, but they are unstable  IBM MSDK, Thread Sanitizer for Java That’s why we’ve developed our own!
  • 13. Data Race Detector Concept Application uses libraries and frameworks via API At least JRE API is well documented “Class XXX is thread-safe” “Class YYY is not thread-safe” “XXX.get() is synchronized with preceding call of XXX.set()” Describe behavior of API and exclude library from analysis
  • 14. DRD: How It’s Organized
  • 15. What Operations to Intercept? Synchronization operations thread start/join/interrupt synchronized volatile read/write java.util.concurrent Accesses to shared data fields objects
  • 16. How It Works Instrumented Application DRD agent app classes classes interceptor Race Config detection module
  • 17. JLS: Publishing Data Publish changes Receive changes
  • 18. JLS: Synchronized-With Relation “Synchronized-with” relation unlock monitor M ↦ all subsequent locks on M volatile write ↦ all subsequent volatile reads … Notation: send ↦ receive
  • 19. JLS: Happens-Before & Data Races X happens-before Y, when X, Y - in same thread, X before Y in program order X is synchronized-with Y Transitivity: exists Z: hb(X, Z) && hb(Z, Y) Data race: 2 conflicting accesses, not ordered by happens-before relation
  • 29. Vector Clock Not ordered! A: 3 > 2 B: 3 < 4
  • 30. How It Works. No Data Race Example
  • 31. How It Works. Data Race Example
  • 32. Code Instrumentation Check everything => huge overhead Race detection scope Accesses to our fields Foreign calls (treat them as read or write) Sync scope Detect sync events in our code Describe contracts of excluded classes Treat these contracts as synchronization events
  • 33. Race Detection private class Storage { private Map<Integer, Item> items = new HashMap<Integer, Item> (); public void store(Item item) { items.put(item.getId(), item); } public void saveToDisk() { On each access of “items” field we check On each access of “items” field we check for (Item item : items.values()) { //serialize and save race on this field race on this field saveItem(item); //... } } On each call of “items” method we check On each call of “items” method we check public Item getItem(int id) { race on this object race on this object return items.get(id); } public void reload() { items = deserealizeFromFile(); Each field of class Item is protected the Each field of class Item is protected the } same way as field “items” of class Storage same way as field “items” of class Storage }
  • 35. Clocks Storing Thread clock ThreadLocal<VectorClock > Field XXX volatile transient VectorClock XXX_vc; Foreign objects, monitors  WeakIdentityConcurrentHashMap<Object,VectorClock>  Volatiles, synchronization contracts  ConcurrentHashMap <???, VectorClock>
  • 36. Composite Keys  AtomicLongFieldUpdater.CAS(Object o, long offset, long v)  param 0 + param 1  Volatile field “abc” of object o  object + field name  AtomicInteger.set() & AtomicInteger.get()  object  ConcurrentMap.put(key, value) & ConcurrentMap.get(key)  object + param 0
  • 37. Solved Problems Composite keys for contracts and volatiles Generate them on-the-fly Avoid unnecessary keys creation ThreadLocal<MutableKeyXXX> for each CompositeKeyXXX Loading of classes, generated on-the-fly Instrument ClassLoader.loadClass()
  • 38. Solved Problems Don’t break serialization compute serialVersiodUid before instrumentation Caching components of dead clocks when thread dies, its time frames doesn’t grow anymore cache frames of dead threads to avoid memory leaks local last-known generation & global generation
  • 39. DRD in Real Life: QD ✔ 6 races found QD QD + DRD
  • 40. DRD in Real Life: MARS UI MARS ✔ 5 races found MARS + DRD
  • 41. DRD Race Report Example WRITE_READ data race between current thread Thread-12(id = 33) and thread Thread-11(id = 32) Race target : field my/app/DataServiceImpl.stopped Thread 32 accessed it in my/app/DataServiceImpl.access$400(line : 29) --------------------------------Stack trace for racing thread (id = 32) is not available.------------ -------------------------------------Current thread's stack trace (id = 33) : --------------------------- at my.app.DataServiceImpl.stop(DataServiceImpl.java:155) at my.app.DataManager.close(DataManager.java:201) ...
  • 42. DRD Advantages Doesn’t break serialization No memory leaks Few garbage No JVM modification Synchronization contracts very important: Unsafe, AbstractQueuedSynchronizer
  • 43. Links http://code.devexperts.com/display/DRD/: documentation, links, etc Contact us: drd-support@devexperts.com Useful links: see also on product page IBM MSDK ThreadSanitizer for Java jChord FindBugs JLS «Threads and locks» chapter
  • 44. Q&A

Editor's Notes

  • #7: Hard to find at development/review stage Control of program execution flow is frequently delegated to frameworks Hard to detect manually during testing
  • #14: safe/read/write contracts synchronization contracts
  • #53: ThreadLocal&lt;MutableInteger&gt; : increment my counter, push it ’ s value to global AtiomicInteger each N-th hit. When thread dies, increment global generation. Each clock stores last known generation. When it becomes less than global, check who have died and cache corresponding frames.
  • #61: Module testing – check module having contracts of others Lightweight mode : instrument little, most important parts of code Support for synchronization on array cells