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

Compare with Current View Page History

« Previous Version 474 Next »

Guidelines

Introduction

Memory that can be shared between threads is called shared memory or heap memory. The term variable 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, and exception handler parameters are never shared between threads and are unaffected by the [memory model].

In modern shared-memory multiprocessor architectures, each processor has one or more levels of cache that are periodically reconciled with main memory as shown in the following figure:

The visibility of writes to shared variables can be problematic because the value of a shared variable may be cached; writing its value to main memory may be delayed. Consequently, another thread may read a stale value of the variable.

A further concern is that concurrent executions of code are typically interleaved and statements may be reordered by the compiler or runtime system to optimize performance. This results in execution orders that are difficult to discern by examination of the source code. Failure to account for possible reorderings is a common source of data races.

Consider the following example in which a and b are (shared) global variables or instance fields, but r1 and r2 are local variables that are inaccessible to other threads.

Initially, let a = 0 and b = 0.

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

LCK00-J

low

probable

medium

P4

L3

LCK01-J

medium

probable

medium

P8

L2

LCK02-J

medium

probable

medium

P8

L2

LCK03-J

medium

probable

medium

P8

L2

LCK04-J

low

probable

medium

P4

L3

LCK05-J

low

probable

medium

P4

L3

LCK06-J

medium

probable

medium

P8

L2

LCK07-J

low

likely

high

P3

L3

LCK08-J

low

likely

low

P9

L2

LCK09-J

low

probable

high

P2

L3

LCK10-J

low

probable

medium

P4

L3

LCK11-J

low

probable

medium

P4

L3


VNA06-J. Do not assume that declaring a reference volatile guarantees visibility of the members of the referenced object      The CERT Oracle Secure Coding Standard for Java      

  • No labels