Misuse of synchronization primitives is a common source of concurrency issues. Synchronizing on objects that may be reused can result in deadlock and nondeterministic behavior. Consequently, programs must never synchronize on objects that may be reused.
Noncompliant Code Example (Boolean
Lock Object)
...
The Boolean
type is unsuitable for locking purposes because it allows only two values: true and false. Boolean literals containing the same value share unique instances of the Boolean
class in the JVM. In this example, initialized
references refers to the instance corresponding to the value false. If any other code were to inadvertently synchronizes synchronize on a Boolean
literal with the value false, the lock instance is would be reused and the system can could become unresponsiveness unresponsive or deadlockedcould deadlock.
Noncompliant Code Example (Boxed Primitive)
...
Boxed types may use the same instance for a range of integer values and ; consequently, they suffer from the same reuse problem as Boolean
constants. If The wrapper object are reused when the value of the primitive can be represented as a byte, the wrapper object is reused; JVM implementations are also permitted to reuse wrapper objects for larger ranges of values. Note that the use of the intrinsic lock associated with the boxed Integer
wrapper object is insecure; instances of the Integer
object constructed using the new
operator (new Integer(value)
) are unique and not reused. In general, holding a lock locks on any data type that contains a boxed value is are insecure.
Compliant Solution (Integer)
This compliant solution recommends locking on a non-boxed Integer
, using a variant of the private lock object idiom. The doSomething()
method synchronizes using the intrinsic lock of the Integer
instance, Lock
.
...
When explicitly constructed, an Integer
object has a unique reference and its own intrinsic lock that is distinct not shared with only from other Integer
objects or , but also from boxed integers having that have the same value. While this is an acceptable solution, it can cause maintenance problems because developers can incorrectly assume that boxed integers are also appropriate lock objects. A more appropriate solution is to synchronize on a private final lock object as described in the following compliant solution below.
Noncompliant Code Example (Interned String
Object)
...
Consequently, an interned String
object behaves like a global variable in the JVM. As demonstrated in this noncompliant code example, even if when every instance of an object maintains its own lock
field, the field references fields all refer to a common String
constant. Locking on String
constants has the same reuse problem as locking on Boolean
constants.
...
Code Block | ||
---|---|---|
| ||
// This bug was found in jetty-6.1.3 BoundedThreadPool private final String lock = "LOCK"; // ... synchronized (lock) { // ... } // ... |
A String
literal is a literals are constant and are automatically interned. Consequently, it this example suffers from the same pitfalls as the preceding noncompliant code example.
...
This compliant solution locks on a non-interned String
instance that is not interned.
Code Block | ||
---|---|---|
| ||
private final String lock = new String("LOCK"); public void doSomething() { synchronized (lock) { // ... } } |
A String
instance differs from a String
literal. The instance has a unique reference and its own intrinsic lock that is not shared by distinct from other String
object instances or literals. A Nevertheless, a better approach is to synchronize on a private final lock object as shown in the following compliant solution.
...
A significant number of concurrency vulnerabilities arise from locking on the wrong kind of object. It is important to consider the properties of the lock object rather than indiscreetly scavenging for objects on which to synchronize on.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
LCK01-J | medium | probable | medium | P8 | L2 |
...