Download Hybrid Dynamic Data Race Detection
Transcript
/*BEFORE*/ class Example {
Object f;
synchronized void setF(Object p) {
this.f = p;
} }
/*AFTER*/ class Example {
Object f, _detector_state;
final static int FIELD_ID_F = 1;
synchronized void setF(Object p) {
ThreadInfo current =
ThreadInfo.getCurrentThreadInfo();
Detector.acquiredLock(this, current);
try {
this.f = p;
// thread locality check
if (this._detector_state != current) {
this._detector_state =
Detector.performedWrite(this,
FIELD_ID_F, this._detector_state,
current);
}
Detector.releasedLock(this, current);
} catch (Throwable t) {
Detector.releasedLock(this, current);
throw t;
} } }
of the race detection state. Our instrumentation is carefully engineered so that races in updates to race detection state can only lead
to missed races (false negatives). We did not observe this happening over multiple runs of our experiments.
6.5 Handling Class Initializers
Java class initialization performs rather complex synchronization
[14]. In particular, while a thread is initializing a class, any other
thread attempting to access the class will block until initialization
is complete. Thus, while a thread is initializing a class C , accesses
it performs to static fields of C cannot race with any other accesses
to those fields. Many programs rely on this behavior. We simply
do not instrument accesses to static fields of C by C ’s initializer;
this eliminates most false race reports involving class initializers.
6.6 Debugging Support
In large programs it can be difficult to understand the behavior
that leads to a potential data race. Our detailed mode assists by
reporting, for each of the two accesses deemed to be a potential
race, the name of the thread performing the access, the type of the
access, the set of locks held, and a full stack trace for the thread.
Collecting and storing a full stack trace is very expensive, especially for events that might not even contribute to races in the future, and therefore previous race detection tools have not reported
stack traces for both events in a race. However the information is
invaluable, particularly because in Java lock acquisition is lexically
scoped, and therefore walking the stack shows where every held
lock was acquired. The redundancy optimizations described above
are critical to making stack trace collection practical, because we
do not need to collect stack traces (or any other information) for
redundant events.
Our race detector reports a potential race as soon as the second
racing event occurs. Therefore it is easy to interrupt the program
and activate a debugger to inspect the full program state at the second event.
Figure 6: Instrumented Code Example
virtual machine.
Figure 6 shows Java code equivalent to the code produced by our
instrumentation for simple mode.
Instrumentation of memory accesses and lock acquisitions or releases is inserted after the corresponding bytecode instructions, to
ensure that if the instruction throws an exception, then we do not
record an event that did not happen. To record the locking behavior
of synchronized methods, we insert code at the start of the method
to record lock acquisition, and we insert code before every return
instruction to record lock release. We also add a “catch-all” exception handler over the whole method to record lock release in that
case.
We instrument calls to Thread.start, join, notify and
wait at each call site, because these are native methods whose
code we cannot modify.
We also attempt to automatically identify some “shared channel”
data structures. Most such structures sometimes block, and in Java,
this is usually implemented by using notify and wait. When
we see a class with methods calling notify or wait, then we
instrument all lock acquisitions and releases in that class to send
a thread message when a lock is released, and to receive thread
messages when a lock is acquired (if a message is pending for that
lock).
In simple mode, our instrumentation engine inlines each thread
locality check into the user code performing the access. Other than
that, each inserted code snippet simply calls a method in our race
detector, passing in all relevant parameters.
The race detection state passed in is an object containing all the
per-object state maintained by the race detector. This is normally
an array indexed by field identifier since the race detector actually
keeps state for each memory location. The instrumentation stores a
reference to this state in an added field in the accessed object itself.
No synchronization is performed around the loading and storing
6.7 Limitations
Our tool misses some events. It does not capture field accesses
performed by native code or by reflection, nor does it capture lock
acquisition or release by native code. Furthermore, we do not currently instrument Java library code.
Lost lock acquisitions can lead to false positives. Lost field accesses can lead to false negatives. In practice we have not noticed
any problems.
7. EXPERIMENTAL RESULTS
Here we present evidence showing that our tool achieves very
precise results with reasonable slowdown. We also show the effects
of our different optimizations.
7.1 Benchmarks
We applied the tool to the programs shown in table 1, drawn from
a variety of sources. We do not know the exact number of source
lines for tomcat and resin, because they include components
for which source is not available. The “Threads” column shows the
number of application threads created. The “MaxLocks” column
shows the maximum number of locks held at once by an application
thread.
We modified elevator slightly to force it to terminate when
the simulation finishes (normally it just hangs).
The original specjbb runs for a fixed length of time and reports the number of transactions processed per second. We modified specjbb to process a fixed number of transactions (100,000)
174