...
Declaring a variable as volatile or correctly synchronizing the code guarantees that 64-bit primitive long
and double
variables are accessed atomically. (For more information on sharing long
and double
variables among multiple threads, see CON05-J. Ensure atomicity when reading and writing 64-bit values.)
Noncompliant Code Example (Non-volatile Flag)
This noncompliant code example uses a shutdown()
method to set a non-volatile done
flag that is checked in the run()
method.
...
If one thread invokes the shutdown()
method to set the flag, a second thread might not observe that change. Consequently, the second thread may observe that done
is still false
and incorrectly invoke the sleep()
method. A compiler is allowed to optimize the code if it determines that the value of done
is never modified by the same thread, resulting in an infinite loop.
Compliant Solution (volatile
)
In this compliant solution, the done
flag is declared as volatile to ensure that writes are visible to other threads.
Code Block | ||
---|---|---|
| ||
final class ControlledStop implements Runnable { private volatile boolean done = false; @Override public void run() { while (!done) { try { // ... Thread.currentThread().sleep(1000); // Do something } catch(InterruptedException ie) { // Handle exception Thread.currentThread().interrupt(); // Reset interrupted status } } } public void shutdown() { done = true; } } |
Compliant Solution (java.util.concurrent.atomic.AtomicBoolean
)
In this compliant solution, the done
flag is declared as AtomicBoolean
. Atomic types also guarantee that writes are visible to other threads.
Code Block | ||
---|---|---|
| ||
final class ControlledStop implements Runnable { private final AtomicBoolean done = new AtomicBoolean(false); @Override public void run() { while (!done.get()) { try { // ... Thread.currentThread().sleep(1000); // Do something } catch(InterruptedException ie) { // Handle exception Thread.currentThread().interrupt(); // Reset interrupted status } } } public void shutdown() { done.set(true); } } |
Compliant Solution (synchronized
)
This compliant solution uses the intrinsic lock of the Class
object to ensure that updates become visible to other threads.
...
Compliance with CON07-J. Use private final lock objects to synchronize classes that may interact with untrusted code can reduce the likelihood of misuse by ensuring that untrusted callers cannot access the lock object.
Exceptions
CON00-EX1: Class
objects need not be made visible because they are created by the virtual machine and their initialization always precedes any subsequent use.
Risk Assessment
Failing to ensure the visibility of a shared primitive variable may result in a thread observing a stale value of the variable.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
CON00- J | medium | probable | medium | P8 | L2 |
Automated Detection
The following table summarizes the examples flagged as violations by SureLogic Flashlight:
...
The unprotected field can be observed through its graphical user interface (GUI).
Related Vulnerabilities
Any vulnerabilities resulting from the violation of this rule are listed on the CERT website.
References
Wiki Markup |
---|
\[[JLS 05|AA. Java References#JLS 05]\] [Chapter 17, Threads and Locks|http://java.sun.com/docs/books/jls/third_edition/html/memory.html], Section 17.4.5 Happens-Before Order, Section 17.4.3 Programs and Program Order, Section 17.4.8 Executions and Causality Requirements \[[Bloch 08|AA. Java References#Bloch 08]\] Item 66: Synchronize access to shared mutable data \[[Goetz 06|AA. Java References#Goetz 06]\] 3.4.2. "Example: Using Volatile to Publish Immutable Objects" \[[JPL 06|AA. Java References#JPL 06]\] 14.10.3. "The Happens-Before Relationship" \[[MITRE 09|AA. Java References#MITRE 09]\] [CWE ID 667|http://cwe.mitre.org/data/definitions/667.html] "Insufficient Locking," [CWE ID 413|http://cwe.mitre.org/data/definitions/413.html] "Insufficient Resource Locking," [CWE ID 567|http://cwe.mitre.org/data/definitions/567.html] "Unsynchronized Access to Shared Data" |
...