






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Ensuring the correctness of multithreaded programs is difficult, due to the potential for unexpected interactions between concurrent threads. Much previous work has focused on detecting race condi- tions, but the absence of race conditions does not by itself prevent undesired thread interactions. We focus on the more fundamental non-interference property of atomicity ; a method is atomic if its ex- ecution is not affected by and does not interfere with concurrently- executing threads. Atomic methods can be understood according to their sequential semantics, which significantly simplifies (formal and informal) correctness arguments.
This paper presents a dynamic analysis for detecting atomicity vi- olations. This analysis combines ideas from both Lipton’s theory of reduction and earlier dynamic race detectors. Experience with a prototype checker for multithreaded Java code demonstrates that this approach is effective for detecting errors due to unintended in- teractions between threads. In particular, our atomicity checker de- tects errors that would be missed by standard race detectors, and it produces fewer false alarms on benign races that do not cause atomicity violations. Our experimental results also indicate that the majority of methods in our benchmarks are atomic, supporting our hypothesis that atomicity is a standard methodology in multi- threaded programming.
Categories and Subject Descriptors: D.2.4 [Software Engineer- ing]: Software/Program Verification– reliability ; D.2.5 [Software Engineering]: Testing and Debugging– monitors, testing tools ; F.3. [Logics and Meanings of Programs]: Specifying and Verifying and Reasoning about Programs.
General Terms: Languages, Algorithms, Verification.
Keywords: Atomicity, dynamic analysis, reduction.
Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. POPL’04, January 14–16, 2004, Venice, Italy. Copyright 2004 ACM 1-58113-729-X/04/0001 ...$5.
Multiple threads of control are widely used in software develop- ment because they help reduce latency, increase throughput, and provide better utilization of multiprocessor machines. However, reasoning about the behavior and correctness of multithreaded code is difficult, due to the need to consider all possible interleavings of the executions of the various threads. Thus, methods for specifying and controlling the interference between threads are crucial to the cost-effective development of reliable multithreaded software.
Much previous work on controlling thread interference has focused on race conditions. A race condition occurs when two threads si- multaneously access the same data variable, and at least one of the accesses is a write [47]. In practice, race conditions are commonly avoided by protecting each data structure with a lock [6]. This lock- based synchronization discipline is supported by a variety of type systems [21, 20, 19, 22, 8, 7, 28] and other static [49, 23, 10, 13] and dynamic [47, 11, 51, 42, 45] analyses.
Unfortunately, the absence of race conditions is not sufficient to ensure the absence of errors due to unexpected interference between threads. As a concrete illustration of this limitation, consider the following excerpt from the class java.lang.StringBuffer. All fields of a StringBuffer object are protected by the implicit lock associated with the object, and all StringBuffer methods should be safe for concurrent use by multiple threads.
Excerpt from java.lang.StringBuffer public final class StringBuffer {
public synchronized StringBuffer append(StringBuffer sb) { int len = sb.length(); ... // other threads may change sb.length(), ... // so len does not reflect the length of sb sb.getChars(0, len, value, count); ... }
public synchronized int length() { ... } public synchronized void getChars(...) { ... } ... }
The append method shown above first calls sb.length(), which acquires the lock sb, retrieves the length of sb, and releases the lock. The length of sb is stored in the variable len. At this point, a second thread could remove characters from sb. In this situation,
len is now stale [9] and no longer reflects the current length of sb, and so the getChars method is called with an invalid len argu- ment, and may throw an exception. Thus, StringBuffer objects cannot be safely used by multiple threads, even though the imple- mentation is free of race conditions.
Recent results have shown that subtle defects of a similar nature are common, even in well-tested libraries [24]. Havelund reports finding similar errors in NASA’s Remote Agent spacecraft con- troller [1], and Burrows and Leino [9] and von Praun and Gross [51] have detected comparable defects in Java applications. Clearly, the construction of reliable multithreaded software requires the devel- opment and application of more systematic methods for controlling the interference between concurrent threads.
This paper focuses on a strong yet widely-applicable non- interference property called atomicity. A method (or in general a code block) is atomic if for every (arbitrarily interleaved) program execution, there is an equivalent execution with the same overall behavior where the atomic method is executed serially, that is, the method’s execution is not interleaved with actions of other threads.
Atomicity corresponds to a natural programming methodology, es-
sentially dating back to Hoare’s monitors^1 [32]. Many existing classes and library interfaces already follow this methodology, our experimental results indicate that the vast majority of methods in our benchmarks are atomic.
In addition, atomicity provides a strong, indeed maximal, guaran- tee of non-interference between threads. This guarantee reduces the challenging problem of reasoning about an atomic method’s behav- ior in a multithreaded context to the simpler problem of reasoning about the method’s sequential behavior. The latter problem is sig- nificantly more amenable to standard techniques such as manual code inspection, dynamic testing, and static analysis.
In summary, atomicity is a widely-applicable and fundamental cor- rectness property of multithreaded code. However, traditional test- ing techniques are inadequate to verify atomicity. While testing may discover a particular interleaving on which an atomicity viola- tion results in erroneous behavior, the exponentially-large number of possible interleavings makes obtaining adequate test coverage essentially impossible.
This paper presents a dynamic analysis for detecting atomicity vio- lations. For each code block annotated as being atomic, our analy- sis verifies that every execution of that code block is not affected by and does not interfere with other threads. Intuitively, this approach increases the coverage of traditional dynamic testing. Instead of waiting for a particular interleaving on which an atomicity violation causes erroneous behavior, such as a program crash, the checker actively looks for evidence of atomicity violations that may cause errors under other interleavings. Our approach synthesizes ideas from dynamic race detectors (such as Eraser’s Lockset algorithm) and Lipton’s theory of reduction (described in Section 3.1). For the StringBuffer class described above, our technique detects that append contains a window of vulnerability between where the lock sb is released inside length and then re-acquired inside getChars, and produces the following warning, even on executions where this window of vulnerability is not exploited by concurrent threads.
(^1) Monitors are less general in that they rely on syntactic scope
restrictions and do not support dynamically-allocated shared data.
Atomizer error report StringBuffer.append is not atomic: Atomic block entered at StringBuffer.append(StringBuffer.java:445) at BreakStringBuffer.main(BreakStringBuffer.java:21)
Atomic block commits at lock release: at StringBuffer.length(StringBuffer.java:144) at StringBuffer.append(StringBuffer.java:451) at BreakStringBuffer.main(BreakStringBuffer.java:21)
Atomicity violation at lock acquire: at StringBuffer.getChars(StringBuffer.java:326) at StringBuffer.append(StringBuffer.java:455) at BreakStringBuffer.main(BreakStringBuffer.java:21)
We have implemented this dynamic analysis in an automatic check- ing tool called the Atomizer. The application of this tool to over 100,000 lines of Java code demonstrates that it provides an effective approach for detecting defects in multithreaded programs, includ- ing some defects that would be missed by existing race-detection tools. In addition, the Atomizer produces fewer false alarms on be- nign races that do not cause atomicity violations. Finally, our results suggest that a large majority of the exported methods in our bench- marks are atomic, which validates our hypothesis that atomicity is a widely-used programming methodology.
We propose that the application of this technique during the devel- opment, validation, and evolution of multithreaded programs will provide multiple benefits, including:
Dynamic atomicity checking complements existing static tech- niques, such as the type system for atomicity developed by Flana- gan and Qadeer [24], since most software is currently validated us- ing a combination of static type checking and dynamic testing. For large programs, a benefit of the dynamic approach is that it avoids the overhead of type annotations or type inference, particularly for legacy code. Combining dynamic atomicity checking with other static checkers for multithreaded code, such as the Calvin-R tool developed by Freund and Qadeer [26], would yield similar benefits.
The presentation of our results proceeds as follows. Section 2 in- troduces a model of concurrent programs that we use as the basis for our development. Section 3 describes our dynamic analysis for atomicity. Section 4 describes how the Atomizer implements this analysis, and Section 5 presents our experimental results. Section 6 discusses related work, and we conclude with Section 7.
ment. (The restriction ¬A(Π) avoids consideration of partially- executed atomic blocks.)
In this section, we present an instrumented semantics that dynam- ically detects violations of the above atomicity requirement. We start by reviewing Lipton’s theory of reduction [38], which forms the basis of our approach.
The theory of reduction is based on the notion of right-mover and left-mover actions. An action b is a right-mover if, for any execution where the action b performed by one thread is immediately followed by an action c of a concurrent thread, the actions b and c can be swapped without changing the resulting state, as shown below:
Commuting actions
c b Σ′ 1
b c
For example, if the operation b is a lock acquire, then the action c of the second thread neither acquires nor releases the lock, and so cannot affect the state of that lock. Hence the acquire operation can be moved to the right of c without changing the resulting state, and we classify each lock acquire operation as a right-mover.
Conversely, an action c is a left-mover if whenever c immediately follows an action b of a different thread, the actions b and c can be swapped, again without changing the resulting state. Suppose the operation c by the second thread is a lock release. During b, the second thread holds the lock, and b can neither acquire nor release the lock. Hence the lock release operation can be moved to the left of b without changing the resulting state, and we classify each lock release operation as a left-mover.
Next, consider an access (read or write) to a variable that is shared by multiple threads. If the variable is protected by some lock that is held whenever the variable is accessed, then two threads can never access the variable at the same time, and we classify each access to that variable as a both-mover , which means that it is both a right- mover and a left-mover. If the variable is not consistently protected by some lock, we classify the variable access as a non-mover.
To illustrate how the classification of actions as various kinds of movers enables us to verify atomicity, consider the first execution trace in the diagram below. In this trace, a thread (1) acquires a lock m, (2) reads a variable x protected by that lock, (3) updates x, and then (4) releases m. The execution path of this thread is inter- leaved with arbitrary actions b 1 , b 2 , b 3 of other threads. Because the acquire operation is a right-mover and the write and release op- erations are left-movers, there exists an equivalent serial execution in which the operations of this path are not interleaved with opera- tions of other threads, as illustrated by the following diagram. Thus the execution path is atomic.
Reduced execution sequence
Σ′ 1 Σ′ 2 Σ′ 3 Σ′ 4 Σ′ 5 Σ′ 6
acq(m) rd(x,0) wr(x,1) rel(m) Σ 7
Σ 0 Σ 1 Σ 2 Σ 3 Σ 4 Σ 5 Σ 6 Σ 7
Σ 0
b 3
b 2 b 3
b 1
b 1
acq(m) rd(x,0) b 2 wr(x,1) rel(m)
More generally, suppose a path through a code block contains a sequence of right-movers, followed by at most one non-mover ac- tion and then a sequence of left-movers. Then this path can be reduced to an equivalent serial execution, with the same resulting state, where the path is executed without any interleaved actions by other threads.
We next leverage the theory of reduction to verify atomicity dy- namically. In an initial presentation of our approach, we assume the programmer provides a partial function
P : Var −→◦ Lock
that maps protected shared variables to associated locks; if P (x) is undefined, then x is not protected by any lock.
We develop an instrumented semantics that only admits code paths that are reducible, and which goes wrong on irreducible paths. To record whether each thread is in the right-mover or left-mover part of an atomic block, we extend the state space with an instrumenta- tion store :
φ : Tid → {InRight, InLeft }
Each state is now a triple (σ, φ, Π). If A(Π(t)) 6 = 0, then thread t is inside an atomic block, and φ(t) indicates whether the thread is in the right-mover or left-mover part of that atomic block. The initial instrumentation store φ 0 is given by φ 0 (t) = InRight for all t ∈ Tid.
The following relation Σ ⇒at φ′^ updates the instrumentation store whenever thread t performs operation a. The rule [INS ACCESS PROT] deals with an access to a protected variable while holding the appropriate lock. This action is a both-mover and so the instrumen- tation store φ does not change. Accesses to unprotected variables are non-movers, and they can occur outside atomic blocks: see [INS ACCESS OUTSIDE]. Unprotected accesses are also allowed inside an atomic block, and they cause a transition from the right-mover to the left-mover part of the atomic block: see [INS ACCESS COM- MIT]. Acquire operations are right-movers, and they can occur out- side or in the right-mover part of an atomic block, and conversely for release operations.
The relation Σ ⇒at wrong holds if the operation a by thread t would go wrong by accessing a protected variable without hold- ing the correct lock [WRONG RACE], or by performing a non-left- mover action in the left-mover part of an atomic block. Non-left- mover actions include accessing an unprotected variable [WRONG UNPROTECT] or acquiring a lock [WRONG ACQUIRE].
Instrumented operations: Σ ⇒at φ′^ and Σ ⇒at wrong
[INS ACCESS PROT] a ∈ {rd(x, v), wr(x, v)} P (x) defined σ(P (x)) = t (σ, φ, Π) ⇒at φ
[INS ACCESS COMMIT] a ∈ {rd(x, v), wr(x, v)} P (x) undefined A(Π(t)) 6 = 0 φ(t) = InRight (σ, φ, Π) ⇒at φ[t := InLeft]
[INS ACCESS OUTSIDE] a ∈ {rd(x, v), wr(x, v)} P (x) undefined A(Π(t)) = 0 (σ, φ, Π) ⇒at φ
[INS ACQUIRE]
or
φ(t) = InRight A(Π(t)) = 0 (σ, φ, Π) ⇒acq t (m)φ
[INS RELEASE]
(σ, φ, Π) ⇒rel t (m)φ[t := InLeft]
[INS ENTER]
(σ, φ, Π) ⇒begint φ[t := InRight ]
[INS OTHER] a ∈ {end, } (σ, φ, Π) ⇒at φ
[WRONG RACE] a ∈ {rd(x, v), wr(x, v)} P (x) defined σ(P (x)) 6 = t (σ, φ, Π) ⇒at wrong
[WRONG UNPROTECT] a ∈ {rd(x, v), wr(x, v)} P (x) undefined A(Π(t)) 6 = 0 φ(t) = InLeft (σ, φ, Π) ⇒at wrong
[WRONG ACQUIRE] A(Π(t)) 6 = 0 φ(t) = InLeft (σ, φ, Π) ⇒acq t (m)wrong
The instrumented transition relation Σ ⇒ Σ′^ performs an instru- mented step of an arbitrary thread; and Σ ⇒ wrong holds if a step from Σ could violate the synchronization discipline or the atomicity requirement.
Instrumented semantics: Σ ⇒ Σ′^ and Σ ⇒ wrong
[INS STEP] T (t, Π(t), a, π′) σ →at σ′ (σ, φ, Π) ⇒at φ′ (σ, φ, Π) ⇒ (σ′^ , φ′, Π[t := π′])
[INS WRONG] T (t, Π(t), a, π′) σ →at σ′ (σ, φ, Π) ⇒at wrong (σ, φ, Π) ⇒ wrong
The following theorems state that the instrumented semantics is identical to the standard semantics, except that the instrumented semantics records additional information and may go wrong. In ad- dition, any instrumented execution that does not go wrong satisfies the atomicity requirement.
THEOREM 1 (EQUIVALENCE OF SEMANTICS).
1. If (σ, φ, Π) ⇒∗^ (σ′, φ′, Π′) , then (σ, Π) →∗^ (σ′, Π′)_.
(a) (σ, φ, Π) ⇒∗^ wrong , or
(b) ∃φ′^ such that (σ, φ, Π) ⇒∗^ (σ′, φ′, Π′).
PROOF: By induction over derivations.
If (σ 0 , φ 0 , Π 0 ) ⇒∗^ (σ, φ, Π) and ¬A(Π) then (σ 0 , Π 0 ) 7 →∗ (σ, Π).
PROOF: See Appendix.
If the instrumented semantics admits a particular execution, then not only is that execution reducible, but many similar executions are also reducible. In particular, when an atomic block is being executed, the only scheduling decision that affects program behav- ior is when the commit operation (the transition from InRight to InLeft) is scheduled. Scheduling decisions regarding when other operations in the atomic block are scheduled are irrelevant, in that they do not affect program behavior or reducibility. Hence, one test run under our instrumented semantics can simultaneously verify the reducibility of many executions of the standard semantics.
The instrumented semantics of the previous section relies on the programmer to specify protecting locks for shared variables. To avoid burdening the programmer, we next extend the instrumented semantics to infer protecting locks, using a variant of Eraser’s Lock- set algorithm [47]. We extend the instrumentation store φ to map each variable x to a set of candidate locks for x, such that these candidate locks have all been held on every access to x seen so far:
φ : (Tid → {InRight , InLeft}) ∪ (Var → 2 Lock )
The initial candidate lock set for each variable is the set of all locks, that is, φ 0 (x) = Lock for all x ∈ Var.
The relation Σ Vat φ′^ updates the extended instrumentation store whenever thread t performs operation a on the global store. The rule [INS 2 ACCESS] for a variable access removes from the vari- able’s candidate lock set all locks not held by the current thread. We use H(t, σ) to denote the set of locks held by thread t in state σ:
H(t, σ) = {m ∈ Lock | σ(m) = t}
If the candidate lock set for a variable becomes empty, then all ac- cesses to that variable should be treated as non-movers, but previ- ous accesses may already have been incorrectly classified as both- movers. For example, if φ(x) = {m} when thread t enters the following function double, then the first access to x by thread t will be classified as a both-mover. If, at that point, an action of a concurrent thread causes φ(x) to become empty, the analysis will classify the second access to x by t as a non-mover, but will not re- classify the first access, and thus the analysis will fail to recognize that double may not be reducible.
/*# atomic */ void double() { synchronized (m) { int t = x; x = 2 * t; } }
Thus, to ensure soundness, the lock inference semantics does not support unprotected variables, and instead requires every variable to have a protecting lock. If the candidate lock set becomes empty, then that state goes wrong, via [WRONG 2 RACE].
Read-Shared: The field has been read, but not written, by multi- ple threads. Shared-Modified: The field has been read and written by multiple threads, and a candidate lock set records which locks have been consistently held when accessing this field. When entering this state, the candidate set is initialized with all locks held by the current thread.
When a thread accesses a field, the Atomizer run-time updates its state according to the following transition diagram. (The Atomizer does not instrument array accesses.)
The instrumented semantics for lock inference in Section 3.3 goes wrong on any race condition. Since programs frequently have be- nign races, the Atomizer implements a relaxed version of this se- mantics that accommodates such benign race conditions. If the candidate lock set for a variable becomes empty, then subsequent accesses to that variable are considered non-movers. Note that previous accesses to that variable, which were earlier classified as movers, will not be re-classified as non-movers, since storing a his- tory of all variable accesses would be expensive. Thus, as men- tioned in Section 3.3, these relaxed rules introduce a degree of un- soundness. We believe this unsoundness rarely causes the Atom- izer to miss atomicity violations in practice because it requires an unlucky scheduling of operations and because the Atomizer will report the problem on the next execution of the non-atomic code fragment. The following rules adapt the relations Σ Vat φ′^ and Σ Vat wrong to express this relaxed semantics.
Relaxed instrumentation: Σ Vat φ′^ and Σ Vat wrong
[INS 2 RACE] a ∈ {rd(x, v), wr(x, v)} φ(x) ∩ H(t, σ) = ∅ φ(t) = InRight or ¬A(Π(t)) (σ, φ, Π) Vat φ[t := InLeft, x := ∅]
[WRONG 2 RACE] a ∈ {rd(x, v), wr(x, v)} φ(x) ∩ H(t, σ) = ∅ A(Π(t)) φ(t) = InLeft (σ, φ, Π) Vat wrong
To produce clear error messages like that in Section 1, the Atom- izer can optionally capture stack traces (in the form of Exception objects) at the entry and commit points of each atomic block, and include these stack traces in error messages. Since the Atomizer supports nested atomic blocks, a single operation could result in multiple atomicity violations.
The Atomizer may produce false alarms due to imprecisions in the Lockset and reduction algorithms. We next present several im- provements that eliminate many of these false alarms. We start by revisiting the treatment of synchronization operations during reduction. The classification of lock acquires and releases as right-movers and left-movers, respectively, is correct but overly- conservative in some cases. In particular, modular programs typ- ically include redundant synchronization operations that we can more precisely characterize as both-movers.
Re-entrant locks. Lock acquires are in general only right-movers and not left-movers. However, Java provides re-entrant locks, and a re-entrant lock acquire is a both-mover, because this operation cannot interact with other threads. Similarly, a re-entrant release is also a both-mover.
Thread-local locks. If a lock is used by only a single thread, ac- quires and releases of that lock are both-movers.
Thread-local (2) locks. Adding another Thread-local state, as in our Lockset algorithm, eliminates false alarms caused by initializa- tion patterns in which one thread creates and initializes a protected object, and then transfers ownership of both the object and its pro- tecting lock to another thread.
Protected locks. Suppose each thread always holds some lock m 1 before acquiring lock m 2. In this case, two threads cannot attempt to acquire m 2 simultaneously, and so operations on the lock m 2 are also both-movers.
Write-protected data. Consider the following two methods, in which the variable x is protected by a lock for all writes, but not protected for reads.
/*# atomic / int read() { return x; } /# atomic */ void inc() { synchronized (lock) { x = x+1; } }
If x is a 32-bit variable, then the read() method is atomic on a sequentially-consistent machine, even though no protecting lock is held. Despite the presence of such unprotected reads, the inc() method is also atomic. In particular, when the lock is held, a read of x is a both-mover, since no other thread can write to x without holding the lock.
To handle examples like this one, we use a variant of the Lock- set algorithm. For each field, this algorithm infers a lock set pair , consisting of:
The access-protecting lock set is always a subset of the write- protecting lock set. A field read is a both-mover if the current thread holds at least one of the write-protecting locks; otherwise the read is a non-mover. In contrast, a field write is a both-mover only if the access-protecting lock set is non-empty; otherwise the write is a non-mover.
Using these lock set pairs, the Atomizer can infer that inc() is atomic, since it consists of a right-mover (the lock acquire); a both- mover (the read of x); an atomic action (since the write of x does not commute with concurrent reads of other threads); and a left- mover (the lock release). In comparison, existing race-detection tools would produce a false warning about the race condition in read(), even though this race condition is benign and does not affect the atomicity of either method.
Num. Num. Max. Num. Base Atomizer Atomicity Benchmark Lines Threads Locks Locks Held Lock Set Pairs Time (s) Slowdown Warnings Errors elevator 529 5 8 1 17 11.14 — 2 0 hedc 29,948 26 385 3 728 8.36 — 4 1 tsp 706 10 2 1 5 0.94 48.2 7 0 sor 17,690 4 1 1 2 0.70 7.3 0 0 moldyn 1,291 5 1 1 2 3.62 11.8 0 0 montecarlo 3,557 5 1 1 2 7.94 2.2 1 0 raytracer 1,859 5 5 1 7 5.96 36.6 1 1 mtrt 11,315 6 7 2 7 2.33 46.4 6 0 jigsaw 90,100 53 706 31 4,531 13.49 4.7 34 1 specJBB 30,490 10 262,000 6 340,088 18.01 11.2 4 0 webl 22,284 5 402,445 3 452,685 60.35 — 19 0 lib-java 75,305 39 816,617 6 986,855 96.5 — 19 4 Figure 1. Summary of test programs and performance.
This section summarizes our experience applying the Atomizer to twelve benchmark programs. These programs include elevator, a discrete event simulator for elevators [51]; hedc, a tool to access astrophysics data from Web sources [51]; tsp, a Traveling Sales- man Problem solver [51]; sor, a scientific computing program [51]; mtrt, a multithreaded ray-tracing program from the SPEC JVM benchmark suite [48]; jigsaw, an open source web server [54] con- figured to serve a fixed number of pages to a crawler; specJBB, the SPEC JBB2000 business object simulator [48] configured to pro- cess a fixed number of transactions; moldyn, montecarlo, and raytracer from the Java Grande benchmark suite [33]; webl, a scripting language interpreter for processing web pages, configured to execute a simple web crawler [34]; and lib-java. This last pro- gram is an uninstrumented test harness (comprised of webl, jbb, and hedc) that tests an instrumented version of the standard Java libraries java.lang, java.io, java.net, and java.util. All programs other than lib-java use uninstrumented libraries.
The Atomizer instrumented these programs using the heuristics de- scribed in Section 4 (exported methods and synchronized blocks are annotated as atomic). To ensure that our measurements would ac- curately reflect the cost of the underlying analysis, the Atomizer did not record stack histories for atomic block entry and commit points for these tests. We performed the experiments on a Red Hat Linux 8.0 computer with dual 3.06GHz Pentium 4 Xeon processors and 2GB of memory. We used the Sun JDK 1.4.2 compiler and virtual machine for all benchmarks except lib-java, for which we used the Sun JDK 1.3.1 virtual machine due to compatibility problems.
Figure 1 presents statistics for the test programs using all the exten- sions from Section 4.3. The number of locks and distinct lock set pairs were relatively small for most programs, although the larger programs used many objects as locks, in some cases several orders of magnitude more than in comparably-sized C programs [47].
The slowdown incurred by the instrumentation varied from 2.2x to roughly 50x. We only report slowdowns for compute-bound pro- grams. Those programs with very little slowdown, such as sor and montecarlo, spent most of the time in uninstrumented library code. We believe that slowdowns of 20x–40x are representative for most programs. However, we did not focus on efficiency in this prototype, and there is much room for improvement. In particular, static analyses have reduced the overhead of dynamic race detection to under 50% [51], which suggests that similar performance could be achieved when checking atomicity.
The “Atomicity Warnings” column in Figure 1 reports the number of atomic blocks and methods that failed the Atomizer’s atomic- ity requirements during test runs. Figure 2 shows the cumulative benefit of the extensions from Section 4.3. The “Basic” column in- dicates the number of warnings reported for each program using the basic Lockset and reduction algorithms. The succeeding columns show the number of warnings as each refinement from Section 4. is added. Cumulatively, these five refinements are quite effective: they reduce the number of warnings by roughly 70% (from 341 to 97).
The last column in Figure 1 reports the number of atomicity vio- lations that we consider errors, either because they could lead to undesirable program behavior or because they violate documented atomicity properties. Despite checking only mature software, the Atomizer identified a number of potentially damaging errors. Half of the errors were reported for atomic blocks with multiple data races or a single data race followed by a lock acquire. The remain- der contained a lock acquire operation after a lock release.
When testing the instrumented libraries, the Atomizer warned of an atomicity violation in the synchronized method PrintStream.println(String s), which uses two calls to write the string s and the following new-line character to a stream stored in the instance variable out. However, a different thread in the system also wrote to out, potentially at the same time, which could cause the output stream to be corrupted. A comparable error in PrintWriter had been previously identified by a static type system [24], but the Atomizer caught this defect with no programmer intervention and pinpointed an exact location in the program where the bug could manifest itself.
The Atomizer reported an error in jigsaw that was also found stat- ically using a view consistency analysis [51]. In this case, a specific interleaving could allow an entry to be added to a resource store af- ter the store had been closed as part of the shutdown process. Other errors included a known problem with Hashtable iterators in the presence of concurrent modifications, and a case where multiple threads updated a Calendar object through non-atomic methods.
In most programs, the warnings that did not indicate defects could be suppressed by inserting a handful of annotations. A significant number of false alarms were due to the overly-optimistic heuristics employed by the Atomizer to identify atomic blocks. Fewer false alarms would be produced when checking code with programmer- inserted /*# atomic */ declarations. For example, atomicity vi- olations were often reported on methods called near the top-level
This type system for atomicity was inspired by the Calvin-R [26] static checking tool for multithreaded programs. Calvin-R supports modular verification of multithreaded programs by annotating each procedure with a specification; this specification is related to the procedure implementation via abstraction relation that combines the notions of simulation and reduction. In ongoing work, the no- tions of reduction and atomicity are used by Qadeer et al [46] to infer concise procedure summaries in an analysis for multithreaded programs.
An alternative approach for verifying atomicity using model- checking is being explored by Hatcliff et al [30]. In addition to us- ing Lipton’s theory of reduction, they also investigate an approach based on partial order reductions. Their experimental results sug- gest that the model-checking approach for verifying atomicity is feasible for unit-testing, where the reachable state space is smaller than in integration-testing.
Atomicity is a semantic correctness condition for multithreaded software. In this respect, it is similar to strict serializability [43], a correctness condition for database transactions, and linearizabil- ity [31], a correctness condition for concurrent objects. Verifying that an object is linearizable requires full program verification. We hope that our analysis for atomicity can be leveraged to develop lightweight checking tools for related correctness conditions.
Artho et al [1] have developed a dynamic analysis tool to identify one class of “higher-level races”. The analysis is based on the no- tion of view consistency. Intuitively, a view is the set of variables accessed within a synchronized block. Thread A is view consis- tent with B if all views from the execution of A, intersected with the maximal view of B, are ordered by subset inclusion. Viola- tions of view consistency can indicate that a program may be using shared variables in a problematic way. View consistency violations can also be detected statically [52]. ESC/Java has been extended to catch a different notion of higher-level races, where a stale value from one synchronized block is used in a subsequent synchronized block [9].
In recent work, Wang and Stoller [53] developed several algorithms for checking atomicity dynamically, including the basic algorithm described in Section 3.3. Their work focuses primarily on more ex- pressive algorithms that can verify additional execution sequences as serializable. They have not yet applied their algorithms to large programs. Our experiments on large programs motivated the de- velopment of several crucial improvements to the basic algorithm, such as our support for redundant synchronization operations and write-protected data, and has allowed us to validate the efficiency and effectiveness of our approach.
While our tool checks atomicity, other researchers have proposed using atomicity as a language primitive, essentially implementing the serialized semantics 7 →. Lomet [40] first proposed the use of atomic blocks for synchronization. The Argus [39] and Avalon [17] projects developed language support for implementing atomic ob- jects. Persistent languages [2, 3] are attempting to augment atomic- ity with data persistence in order to introduce transactions into pro- gramming languages. A more recent approach to supporting atom- icity uses lightweight transactions implemented in the run-time sys- tem [29]. An alternative is to generate synchronization code auto- matically from high-level specifications [14].
Developing reliable multithreaded software is notoriously difficult, because concurrent threads often interact in unexpected and erro- neous ways. Programmers try to avoid unintended interactions by designing methods and interfaces that are atomic, but traditional testing techniques are inadequate for verifying atomicity.
This paper presents a dynamic analysis designed to catch atomic- ity violations would be missed by traditional testing or (static or dynamic) race-detection techniques. This analysis has been imple- mented and applied to a range of benchmark programs, and has successfully detected atomicity violations in these programs. In ad- dition, our experimental results suggest that over 90% of the meth- ods in our benchmarks are atomic, which validates our hypothesis that atomicity is a fundamental design principle in multithreaded programs.
For future work, we hope to study hybrid atomicity checkers based on a synthesis of the dynamic and static approaches. In one com- bination, a static type-based analysis may verify many expected race-freedom and atomicity properties, and the dynamic atomicity checker could then focus on the unverified residue. For race detec- tion, this hybrid approach has reduced the instrumentation overhead by an order of magnitude [51, 42]; we expect comparable improve- ments when checking atomicity.
Acknowledgments. We thank Mart´ın Abadi, Shaz Qadeer, Rob O’Callahan, and Scott Stoller for valuable comments on this work. We also thank Christof von Praun for his assistance in collecting test programs. This work was partly supported by the National Sci- ence Foundation under Grants CCR-0341179 and CCR-0341387, and by faculty research funds granted by the University of Califor- nia at Santa Cruz and by Williams College.
[1] C. Artho, K. Havelund, and A. Biere. High-level data races. In The First International Workshop on Verification and Validation of Enter- prise Information Systems , 2003. [2] M. P. Atkinson, K. J. Chisholm, and W. P. Cockshott. PS-Algol: an Algol with a persistent heap. ACM SIGPLAN Notices , 17(7):24–31,
[3] M. P. Atkinson and D. Morrison. Procedures as persistent data ob- jects. ACM Transactions on Programming Languages and Systems , 7(4):539–559, 1985. [4] R.-J. Back. A method for refining atomicity in parallel algorithms. In PARLE 89: Parallel Architectures and Languages Europe , volume 366 of Lecture Notes in Computer Science , pages 199–216. Springer- Verlag, 1989. [5] D. F. Bacon, R. E. Strom, and A. Tarafdar. Guava: A dialect of Java without data races. In Proceedings of the ACM Conference on Object- Oriented Programming, Systems, Languages and Applications , pages 382–400, 2001. [6] A. D. Birrell. An introduction to programming with threads. Research Report 35, Digital Equipment Corporation Systems Research Center,
[7] C. Boyapati, R. Lee, and M. Rinard. A type system for preventing data races and deadlocks in Java programs. In Proceedings of the ACM Conference on Object-Oriented Programming, Systems, Languages and Applications , pages 211–230, 2002. [8] C. Boyapati and M. Rinard. A parameterized type system for race-free Java programs. In Proceedings of the ACM Conference on Object-
Oriented Programming, Systems, Languages and Applications , pages 56–69, 2001. [9] M. Burrows and K. R. M. Leino. Finding stale-value errors in concur- rent programs. Technical Note 2002-004, Compaq Systems Research Center, 2002.
[10] A. T. Chamillard, L. A. Clarke, and G. S. Avrunin. An empirical comparison of static concurrency analysis techniques. Technical Re- port 96-084, Department of Computer Science, University of Mas- sachusetts at Amherst, 1996.
[11] J.-D. Choi, K. Lee, A. Loginov, R. O’Callahan, V. Sarkar, and M. Srid- hara. Efficient and precise datarace detection for multithreaded object- oriented programs. In Proceedings of the ACM Conference on Pro- gramming Language Design and Implementation , pages 258–269,
[12] E. Cohen and L. Lamport. Reduction in TLA. In Proceedings of the International Conference on Concurrency Theory , volume 1466 of Lecture Notes in Computer Science , pages 317–331. Springer-Verlag,
[13] J. C. Corbett. Evaluating deadlock detection methods for concurrent software. IEEE Transactions on Software Engineering , 22(3):161– 180, 1996.
[14] X. Deng, M. Dwyer, J. Hatcliff, and M. Mizuno. Invariant-based specification, synthesis, and verification of synchronization in concur- rent programs. In International Conference on Software Engineering , pages 442–452, 2002.
[15] T. W. Doeppner, Jr. Parallel program correctness through refinement. In Proceedings of the ACM Symposium on the Principles of Program- ming Languages , pages 155–169, 1977.
[16] M. B. Dwyer and L. A. Clarke. Data flow analysis for verifying prop- erties of concurrent programs. Technical Report 94-045, Department of Computer Science, University of Massachusetts at Amherst, 1994.
[17] J. L. Eppinger, L. B. Mummert, and A. Z. Spector. Camelot and Avalon: A Distributed Transaction Facility. Morgan Kaufmann, 1991.
[18] L. Fajstrup, E. Goubault, and M. Raussen. Detecting deadlocks in con- current systems. In D. Sangiorgi and R. de Simone, editors, Proceed- ings of the International Conference on Concurrency Theory , volume 1466 of Lecture Notes in Computer Science , pages 332–347. Springer- Verlag, 1998.
[19] C. Flanagan and M. Abadi. Object types against races. In J. C. M. Baeten and S. Mauw, editors, Proceedings of the International Confer- ence on Concurrency Theory , volume 1664 of Lecture Notes in Com- puter Science , pages 288–303. Springer-Verlag, 1999.
[20] C. Flanagan and M. Abadi. Types for safe locking. In S. D. Swier- stra, editor, Proceedings of European Symposium on Programming , volume 1576 of Lecture Notes in Computer Science , pages 91–108. Springer-Verlag, 1999.
[21] C. Flanagan and S. N. Freund. Type-based race detection for Java. In Proceedings of the ACM Conference on Programming Language Design and Implementation , pages 219–232, 2000.
[22] C. Flanagan and S. N. Freund. Detecting Race Conditions in Large Programs. In Workshop on Program Analysis for Software Tools and Engineering , pages 90–96, 2001.
[23] C. Flanagan, K. R. M. Leino, M. Lillibridge, G. Nelson, J. B. Saxe, and R. Stata. Extended static checking for Java. In Proceedings of the ACM Conference on Programming Language Design and Implemen- tation , pages 234–245, 2002.
[24] C. Flanagan and S. Qadeer. A type and effect system for atomicity. In Proceedings of the ACM Conference on Programming Language Design and Implementation , pages 338–349, 2003.
[25] C. Flanagan and S. Qadeer. Types for atomicity. In Proceedings of the ACM Workshop on Types in Language Design and Implementation , pages 1–12, 2003.
[26] S. N. Freund and S. Qadeer. Checking concise specifications for mul-
tithreaded software. In Workshop on Formal Techniques for Java-like Programs , 2003. [27] J. Gosling, B. Joy, and G. Steele. The Java Language Specification. Addison-Wesley, 1996. [28] D. Grossman. Type-safe multithreading in Cyclone. In Proceedings of the ACM Workshop on Types in Language Design and Implementa- tion , pages 13–25, 2003. [29] T. L. Harris and K. Fraser. Language support for lightweight trans- actions. In Proceedings of the ACM Conference on Object-Oriented Programming, Systems, Languages and Applications , pages 388–402,
[30] J. Hatcliff, Robby, and M. B. Dwyer. Verifying atomicity specifica- tions for concurrent object-oriented software using model-checking. In Proceedings of the International Conference on Verification, Model Checking and Abstract Interpretation , 2004. [31] M. P. Herlihy and J. M. Wing. Linearizability: A correctness con- dition for concurrent objects. ACM Transactions on Programming Languages and Systems , 12(3):463–492, 1990. [32] C. Hoare. Monitors: an operating systems structuring concept. Com- munications of the ACM , 17(10):549–557, 1974. [33] Java Grande Forum. Java Grande benchmark suite. Available from http://www.javagrande.org/, 2003. [34] T. Kistler and J. Marais. WebL – a programming language for the web. In Proceedings of the International World Wide Web Conference , volume 30 of Computer Networks and ISDN Systems , pages 259–270. Elsevier, 1998. [35] N. Kobayashi. A partially deadlock-free typed process calculus. ACM Transactions on Programming Languages and Systems , 20(2):436– 482, 1998. [36] N. Kobayashi, S. Saito, and E. Sumii. An implicitly-typed deadlock- free process calculus. In C. Palamidessi, editor, Proceedings of the International Conference on Concurrency Theory , volume 1877 of Lecture Notes in Computer Science , pages 489–503. Springer-Verlag,
[37] L. Lamport and F. B. Schneider. Pretending atomicity. Research Re- port 44, DEC Systems Research Center, 1989. [38] R. J. Lipton. Reduction: A method of proving properties of parallel programs. Communications of the ACM , 18(12):717–721, 1975. [39] B. Liskov, D. Curtis, P. Johnson, and R. Scheifler. Implementation of Argus. In Proceedings of the Symposium on Operating Systems Principles , pages 111–122, 1987. [40] D. B. Lomet. Process structuring, synchronization, and recovery using atomic actions. Language Design for Reliable Software , pages 128– 137, 1977. [41] J. Misra. A Discipline of Multiprogramming: Programming Theory for Distributed Applications. Springer-Verlag, 2001. [42] R. O’Callahan and J.-D. Choi. Hybrid dynamic data race detection. In ACM SIGPLAN Symposium on Principles and Practice of Parallel Programming , pages 167–178, 2003. [43] C. Papadimitriou. The theory of database concurrency control. Com- puter Science Press, 1986. [44] Polyspace technologies, 2003. Available at http://www.poly- space.com. [45] E. Pozniansky and A. Schuster. Efficient on-the-fly data race detection in multithreaded C++ programs. In Proceedings of the ACM Sym- posium on Principles and Practice of Parallel Programming , pages 179–190, 2003. [46] S. Qadeer, S. K. Rajamani, and J. Rehof. Summarizing procedures in concurrent programs. In Proceedings of the ACM Symposium on the Principles of Programming Languages , 2004. [47] S. Savage, M. Burrows, G. Nelson, P. Sobalvarro, and T. E. Anderson.