You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 162 Next »

Guidelines

CON00-J. Declare shared variables as volatile to ensure visibility and limit reordering of statements

CON01-J. Design APIs that ensure atomicity of composite operations and visibility of results

CON02-J. Always synchronize on the appropriate object

CON03-J. Do not use background threads during class initialization

CON04-J. Use the private lock object idiom instead of intrinsic synchronization

CON05-J. Ensure that threads do not fail during activation

CON06-J. Do not defer a thread that is holding a lock

CON07-J. Ensure atomicity of calls to thread-safe APIs

CON08-J. Do not call alien methods or constructors that synchronize on the same object as the calling class

CON09-J. Do not invoke alien methods that rely on invariants protected by the same lock object

CON10-J. Methods that override synchronized methods must also possess synchronization capabilities

CON11-J. Do not assume that declaring an object volatile guarantees visibility of its members

CON12-J. Avoid deadlock by requesting and releasing locks in the same order

CON13-J. Do not try to force thread shutdown

CON14-J. Do not let the "this" reference escape during object construction

CON15-J. Ensure actively held locks are released on exceptional conditions

CON16-J. Do not expect sleep() and yield() methods to have any synchronization semantics

CON17-J. Avoid using ThreadGroup APIs

CON18-J. Always invoke wait() and await() methods inside a loop

CON19-J. Use notifyAll() instead of notify() to resume waiting threads

CON20-J. Never apply a lock to methods making network calls

CON21-J. Facilitate thread reuse by using Thread Pools

CON22-J. Do not use incorrect forms of the double-checked locking idiom

CON23-J. Address the shortcomings of the Singleton design pattern

CON24-J. Use a unique channel to acquire locks on any file

CON25-J. Ensure atomicity when reading and writing 64-bit values

CON26-J. Do not publish partially-constructed objects

CON27-J. Do not execute classes that use ThreadLocal objects in a thread pool

Introduction

Memory that can be shared between threads is called shared memory or heap memory. The term variable is as used in this section refers to both fields and array elements [[JLS 05]]. Variables that are shared between threads are referred to as shared variables. All instance fields, static fields, and array elements are shared variables and are stored in heap memory. Local variables, formal method parameters, or exception handler parameters are never shared between threads and are not affected by the [memory model].

Two major concerns in concurrent Java programming are visibility and reordering.

Visibility refers to when a write to a variable becomes
Visibility refers to the requirement that every thread sees the value of the most recent update to a shared variable. This is essential in multi-threaded programs because the value of a shared variable may be cached and not written to main memory immediately. Consequently, another thread may retrieve a stale value of the variable when attempting to read it from the main memory. Using the volatile keyword mitigates this risk and so does correct synchronization.

The restricted or no reordering requirement refers to ensuring that the execution sequence of a set of statements does not vary when observed by multiple threads. Concurrent executions of code are typically interleaved and statements may be reordered by the compiler or runtime system to facilitate various optimizations. This results in execution orders that are not immediately obvious from an examination of the source-code. Failure to account for this hazard is a common source of data races. Restricting the set of possible reorderings makes it easier to reason about the safety of the code. This risk is mitigated by correctly synchronizing the code and in some cases, by using the volatile keyword.

Even if statements execute in the expected order (program order), caching can prevent the latest values from being reflected in the main memory (visibility hazard). Program order is the execution order that is expected when a single thread is running the statements sequentially, as written in a method.

The Java Language Specification defines the Java Memory Model (JMM) which provides certain guarantees to the Java programmer.

There are two requirements for writing provably correct multi-threaded code:

1. Happens-before consistency: If two accesses of a shared variable follow the happens-before relationship, data races arising from statement reorderings cannot occur. However, this is necessary but not sufficient for acceptable program behavior.

Consider the following example in which a and b are (shared) global variables or instance fields but r1 and r2 are local variables not accessible by other threads.

Initially, let a = 0 and b = 0.

Thread 1

Thread 2

a = 10;

b = 20;

r1 = b;

r2 = a;

Because, in Thread 1, the two assignments a = 10; and r1 = b; are not related, the compiler or runtime system is free to reorder them. Similarly in Thread 2, the statements may be freely reordered. Although it may seem counter-intuitive, the Java memory model allows a read to see a write that occurs later in the execution order.

A possible execution order showing actual assignments is:

Execution Order

Assignment

Assigned Value

Notes

1.

a = 10;

10

 

2.

b = 20;

20

 

3.

r1 = b;

0

Reads initial value of b, that is 0

4.

r2 = a;

0

Reads initial value of a, that is 0

In this ordering, r1 and r2 read the original values of the variables a and b even though they are expected to see the updated values, 10 and 20. Another possible execution order showing actual assignments is:

Execution Order

Statement

Assigned Value

Notes

1.

r1 = b;

20

Reads later value (in step 4.) of write, that is 20

2.

r2 = a;

10

Reads later value (in step 3.) of write, that is 10

3.

a = 10;

10

 

4.

b = 20;

20

 

In this ordering, r1 and r2 read the values of a and b written from step 3 and 4, even before the statements corresponding to these steps have executed.

"The fact that we allow a read to see a write that comes later in the execution order can sometimes thus result in unacceptable behaviors." [[JLS 05]]. Both, the use of synchronization and the volatile keyword prevent a thread from observing inconsistent values of shared variables.

2. Sequential consistency: This property provides a very strong guarantee that the compiler will not optimize away or reorder any statements. It guarantees that the program is free from data races. It also ensures that each access of a variable is atomic and immediately visible to other threads.

For example, consider a set of statements that are being executed by multiple threads:

Thread 1,2,3...

Statement 1

Statement 2

Statement 3

If statements 1, 2 and 3 are always executed sequentially by all threads as given in this program order, they are sequentially consistent with respect to each other. The sequential consistency property also requires that a read operation in some thread does not see the value of a future write operation taking place in the same or another thread. Similarly, a read operation is guaranteed to see the value of the last write to the variable from any thread.

The use of sequential consistency as the sole memory model mechanism makes it easy for a programmer to reason with the program's logic in a multithreading scenario, however, introduces a performance penalty because the compiler is prohibited from reordering code for performing complex optimizations. Using volatile variables reduces this performance penalty at the cost of strong sequential consistency guarantees.

Risk Assessment Summary

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

CON00-J

medium

probable

medium

P8

L2

CON01-J

medium

probable

medium

P8

L2

CON02-J

low

likely

high

P3

L3

CON03-J

low

probable

medium

P4

L3

CON04-J

low

probable

medium

P4

L3

CON05-J

low

probable

medium

P4

L3

CON06-J

low

probable

medium

P4

L3

CON07-J

low

likely

high

P3

L3

CON08-J

low

likely

high

P3

L3

CON09-J

low

probable

medium

P4

L3

CON10-J

low

probable

medium

P4

L3

CON11-J

low

likely

high

P3

L3

CON12-J

low

probable

medium

P4

L3

CON14-J

low

probable

medium

P4

L3

CON15-J

low

likely

low

P9

L2

CON16-J

low

probable

medium

P4

L3

CON17-J

low

probable

low

P6

L2

CON18-J

low

unlikely

medium

P2

L3

CON19-J

low

unlikely

medium

P2

L3

CON20-J

low

probable

high

P2

L3

CON21-J

low

probable

high

P2

L3

CON22-J

low

probable

medium

P4

L3

CON23-J

low

unlikely

medium

P2

L3

CON24-J

low

unlikely

medium

P2

L3

CON25-J

low

unlikely

medium

P2

L3


IDS17-J. Understand how escape characters are interpreted when String literals are compiled      The CERT Sun Microsystems Secure Coding Standard for Java      VOID CON00-J. Synchronize access to shared mutable variables

  • No labels