SlideShare ist ein Scribd-Unternehmen logo
1 von 45
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
Devexperts DRD site: documentation, links, etc
Contact us: drd-support@devexperts.com


IBM MSDK
ThreadSanitizer for Java
jChord
FindBugs


JLS «Threads and locks» chapter
Q&A
Synchronization contracts: interfaces
Usually interface contract is described (ConcurrentMap)


Instrumentation stage:
  specified interface might even not been loaded yet
  get “potential contracts” from method name & signature
  code path for each potential contract


Runtime:
  is potential contract real?
  caching: {class, contract_id} → boolean

Weitere ähnliche Inhalte

Was ist angesagt?

서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015NAVER / MusicPlatform
 
2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - englishJen Yee Hong
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)HamletDRC
 
Generics past, present and future
Generics  past, present and futureGenerics  past, present and future
Generics past, present and futureRichardWarburton
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortStefan Marr
 
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI векеДмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI векеSergey Platonov
 
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Stefan Marr
 
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
Gor Nishanov,  C++ Coroutines – a negative overhead abstractionGor Nishanov,  C++ Coroutines – a negative overhead abstraction
Gor Nishanov, C++ Coroutines – a negative overhead abstractionSergey Platonov
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and deletePlatonov Sergey
 
Dynamic Binary Analysis and Obfuscated Codes
Dynamic Binary Analysis and Obfuscated Codes Dynamic Binary Analysis and Obfuscated Codes
Dynamic Binary Analysis and Obfuscated Codes Jonathan Salwan
 
Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performanceDuoyi Wu
 
Verilog Lecture4 2014
Verilog Lecture4 2014Verilog Lecture4 2014
Verilog Lecture4 2014Béo Tú
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataDaniel Bristot de Oliveira
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaWei-Bo Chen
 
Clone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsClone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsNikolaos Tsantalis
 
Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines Parashuram N
 

Was ist angesagt? (20)

서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
서버 개발자가 바라 본 Functional Reactive Programming with RxJava - SpringCamp2015
 
2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english2018 cosup-delete unused python code safely - english
2018 cosup-delete unused python code safely - english
 
Java 7
Java 7Java 7
Java 7
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 
Generics past, present and future
Generics  past, present and futureGenerics  past, present and future
Generics past, present and future
 
Collections forceawakens
Collections forceawakensCollections forceawakens
Collections forceawakens
 
Building High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low EffortBuilding High-Performance Language Implementations With Low Effort
Building High-Performance Language Implementations With Low Effort
 
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI векеДмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI веке
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
Zero-Overhead Metaprogramming: Reflection and Metaobject Protocols Fast and w...
 
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
Gor Nishanov,  C++ Coroutines – a negative overhead abstractionGor Nishanov,  C++ Coroutines – a negative overhead abstraction
Gor Nishanov, C++ Coroutines – a negative overhead abstraction
 
С++ without new and delete
С++ without new and deleteС++ without new and delete
С++ without new and delete
 
Dynamic Binary Analysis and Obfuscated Codes
Dynamic Binary Analysis and Obfuscated Codes Dynamic Binary Analysis and Obfuscated Codes
Dynamic Binary Analysis and Obfuscated Codes
 
Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performance
 
Verilog Lecture4 2014
Verilog Lecture4 2014Verilog Lecture4 2014
Verilog Lecture4 2014
 
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using AutomataModeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
Modeling the Behavior of Threads in the PREEMPT_RT Linux Kernel Using Automata
 
AST Transformations at JFokus
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
 
Triton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON ChinaTriton and Symbolic execution on GDB@DEF CON China
Triton and Symbolic execution on GDB@DEF CON China
 
Clone Refactoring with Lambda Expressions
Clone Refactoring with Lambda ExpressionsClone Refactoring with Lambda Expressions
Clone Refactoring with Lambda Expressions
 
Understanding Javascript Engines
Understanding Javascript Engines Understanding Javascript Engines
Understanding Javascript Engines
 

Andere mochten auch

TMPA-2013 Tsytelov Trifanov Devexperts
TMPA-2013 Tsytelov Trifanov DevexpertsTMPA-2013 Tsytelov Trifanov Devexperts
TMPA-2013 Tsytelov Trifanov DevexpertsIosif Itkin
 
Effective websites development
Effective websites developmentEffective websites development
Effective websites developmentDevexperts
 
Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...Devexperts
 
How to improve java performance
How to improve java performanceHow to improve java performance
How to improve java performanceDevexperts
 
Drd secr final1_3
Drd secr final1_3Drd secr final1_3
Drd secr final1_3Devexperts
 
Browsers. Magic is inside.
Browsers. Magic is inside.Browsers. Magic is inside.
Browsers. Magic is inside.Devexperts
 
20130420 bitbyte
20130420 bitbyte20130420 bitbyte
20130420 bitbyteDevexperts
 
Codefreeze rus
Codefreeze rusCodefreeze rus
Codefreeze rusDevexperts
 
Codefreeze eng
Codefreeze engCodefreeze eng
Codefreeze engDevexperts
 
HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013HSA Foundation
 
Folha Dominical - 06.02.11 Nº359
Folha Dominical - 06.02.11 Nº359Folha Dominical - 06.02.11 Nº359
Folha Dominical - 06.02.11 Nº359Comunidades Vivas
 
Ismael mijan golfiños na ensenada diario de ferrol
Ismael mijan   golfiños na ensenada diario de ferrolIsmael mijan   golfiños na ensenada diario de ferrol
Ismael mijan golfiños na ensenada diario de ferrolIsmael Mijan
 
Aviso convocatoria suministro vias serranía
Aviso convocatoria suministro  vias serraníaAviso convocatoria suministro  vias serranía
Aviso convocatoria suministro vias serraníavideodigital
 
Brainsharing: Durch kontinuierlichen Wissenstransfer zum Projekterfolg
Brainsharing: Durch kontinuierlichen Wissenstransfer zum ProjekterfolgBrainsharing: Durch kontinuierlichen Wissenstransfer zum Projekterfolg
Brainsharing: Durch kontinuierlichen Wissenstransfer zum Projekterfolgwisdomclouds
 
Tiempo Interior 0912a(Diciembre)
Tiempo Interior 0912a(Diciembre)Tiempo Interior 0912a(Diciembre)
Tiempo Interior 0912a(Diciembre)guested08fae80
 
E-waste Africa Project - From Europe to West-Africa
E-waste Africa Project - From Europe to West-AfricaE-waste Africa Project - From Europe to West-Africa
E-waste Africa Project - From Europe to West-AfricaOeko-Institut
 
El Doctor Juan Coullaut y el tema 5 de la visión
El Doctor Juan Coullaut y el tema 5 de la visiónEl Doctor Juan Coullaut y el tema 5 de la visión
El Doctor Juan Coullaut y el tema 5 de la visiónDoctor Juan Coullaut
 

Andere mochten auch (20)

TMPA-2013 Tsytelov Trifanov Devexperts
TMPA-2013 Tsytelov Trifanov DevexpertsTMPA-2013 Tsytelov Trifanov Devexperts
TMPA-2013 Tsytelov Trifanov Devexperts
 
Effective websites development
Effective websites developmentEffective websites development
Effective websites development
 
Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...Windows, doors and secret passages: approaches to the space organization in t...
Windows, doors and secret passages: approaches to the space organization in t...
 
How to improve java performance
How to improve java performanceHow to improve java performance
How to improve java performance
 
Drd secr final1_3
Drd secr final1_3Drd secr final1_3
Drd secr final1_3
 
Browsers. Magic is inside.
Browsers. Magic is inside.Browsers. Magic is inside.
Browsers. Magic is inside.
 
20130420 bitbyte
20130420 bitbyte20130420 bitbyte
20130420 bitbyte
 
Codefreeze rus
Codefreeze rusCodefreeze rus
Codefreeze rus
 
Codefreeze eng
Codefreeze engCodefreeze eng
Codefreeze eng
 
HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013HSA Memory Model Hot Chips 2013
HSA Memory Model Hot Chips 2013
 
Memory models
Memory modelsMemory models
Memory models
 
Folha Dominical - 06.02.11 Nº359
Folha Dominical - 06.02.11 Nº359Folha Dominical - 06.02.11 Nº359
Folha Dominical - 06.02.11 Nº359
 
Ismael mijan golfiños na ensenada diario de ferrol
Ismael mijan   golfiños na ensenada diario de ferrolIsmael mijan   golfiños na ensenada diario de ferrol
Ismael mijan golfiños na ensenada diario de ferrol
 
Aviso convocatoria suministro vias serranía
Aviso convocatoria suministro  vias serraníaAviso convocatoria suministro  vias serranía
Aviso convocatoria suministro vias serranía
 
Diabetes mellitus
Diabetes mellitusDiabetes mellitus
Diabetes mellitus
 
Brainsharing: Durch kontinuierlichen Wissenstransfer zum Projekterfolg
Brainsharing: Durch kontinuierlichen Wissenstransfer zum ProjekterfolgBrainsharing: Durch kontinuierlichen Wissenstransfer zum Projekterfolg
Brainsharing: Durch kontinuierlichen Wissenstransfer zum Projekterfolg
 
Saxion ondernemers top 50 en tip 10 2011
Saxion ondernemers top 50 en tip 10 2011Saxion ondernemers top 50 en tip 10 2011
Saxion ondernemers top 50 en tip 10 2011
 
Tiempo Interior 0912a(Diciembre)
Tiempo Interior 0912a(Diciembre)Tiempo Interior 0912a(Diciembre)
Tiempo Interior 0912a(Diciembre)
 
E-waste Africa Project - From Europe to West-Africa
E-waste Africa Project - From Europe to West-AfricaE-waste Africa Project - From Europe to West-Africa
E-waste Africa Project - From Europe to West-Africa
 
El Doctor Juan Coullaut y el tema 5 de la visión
El Doctor Juan Coullaut y el tema 5 de la visiónEl Doctor Juan Coullaut y el tema 5 de la visión
El Doctor Juan Coullaut y el tema 5 de la visión
 

Ähnlich wie Dynamic data race detection in concurrent Java programs

Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance TuningMinh Hoang
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionAlex Su
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerAlex Matrosov
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindAndreas Czakaj
 
Automated Discovery of Deserialization Gadget Chains
 Automated Discovery of Deserialization Gadget Chains Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsPriyanka Aash
 
Tools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidTools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidIntel® Software
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugketan_patel25
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxpetabridge
 
findbugs Bernhard Merkle
findbugs Bernhard Merklefindbugs Bernhard Merkle
findbugs Bernhard Merklebmerkle
 
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016Christian Schneider
 
Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsAutomated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsPriyanka Aash
 
Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsAutomated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsPriyanka Aash
 
Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015Sperasoft
 

Ähnlich wie Dynamic data race detection in concurrent Java programs (20)

Java Performance Tuning
Java Performance TuningJava Performance Tuning
Java Performance Tuning
 
.NET Reflection
.NET Reflection.NET Reflection
.NET Reflection
 
Java Unit Test and Coverage Introduction
Java Unit Test and Coverage IntroductionJava Unit Test and Coverage Introduction
Java Unit Test and Coverage Introduction
 
Object Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorerObject Oriented Code RE with HexraysCodeXplorer
Object Oriented Code RE with HexraysCodeXplorer
 
04 Data Access
04 Data Access04 Data Access
04 Data Access
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
How to write clean & testable code without losing your mind
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mind
 
Automated Discovery of Deserialization Gadget Chains
 Automated Discovery of Deserialization Gadget Chains Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget Chains
 
Tools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidTools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in Android
 
JavaSecure
JavaSecureJavaSecure
JavaSecure
 
Lambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter LawreyLambdas puzzler - Peter Lawrey
Lambdas puzzler - Peter Lawrey
 
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtugVk.amberfog.com gtug part1_introduction2_javaandroid_gtug
Vk.amberfog.com gtug part1_introduction2_javaandroid_gtug
 
Jvm internals
Jvm internalsJvm internals
Jvm internals
 
Java
JavaJava
Java
 
NET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptxNET Systems Programming Learned the Hard Way.pptx
NET Systems Programming Learned the Hard Way.pptx
 
findbugs Bernhard Merkle
findbugs Bernhard Merklefindbugs Bernhard Merkle
findbugs Bernhard Merkle
 
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016
Surviving the Java Deserialization Apocalypse // OWASP AppSecEU 2016
 
Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsAutomated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget Chains
 
Automated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget ChainsAutomated Discovery of Deserialization Gadget Chains
Automated Discovery of Deserialization Gadget Chains
 
Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015Sperasoft‬ talks j point 2015
Sperasoft‬ talks j point 2015
 

Dynamic data race detection in concurrent Java programs

  • 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 Devexperts DRD site: documentation, links, etc Contact us: drd-support@devexperts.com IBM MSDK ThreadSanitizer for Java jChord FindBugs JLS «Threads and locks» chapter
  • 44. Q&A
  • 45. Synchronization contracts: interfaces Usually interface contract is described (ConcurrentMap) Instrumentation stage: specified interface might even not been loaded yet get “potential contracts” from method name & signature code path for each potential contract Runtime: is potential contract real? caching: {class, contract_id} → boolean

Hinweis der Redaktion

  1. Hard to find at development/review stage Control of program execution flow is frequently delegated to frameworks Hard to detect manually during testing
  2. safe/read/write contracts synchronization contracts
  3. 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.
  4. Module testing – check module having contracts of others Lightweight mode : instrument little, most important parts of code Support for synchronization on array cells