JDK-4329831 : HotSpot violates Memory Model in the Java Language Specification
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 2.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2000-04-12
  • Updated: 2006-07-19
  • Resolved: 2006-07-19
Related Reports
Duplicate :  
Relates :  
Description
Name: tb29552			Date: 04/12/2000


/*
java version "1.3.0rc1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0rc1-T)
Java HotSpot(TM) Client VM (build 1.3.0rc1-S, mixed mode)


All known JVM's perform optimizations that violate
chapter 17 of the Java Language Specification, which describes.
the model for how threads can interact through shared memory. This
includes all versions of Sun HotSpot JVM.

This is a duplicate of bug 4242244, which was incorrectly closed by
someone who thought that it only applied to pre-HotSpot JVM's, even though
bug 4242244 clearly mentions that the bug occurs in HotSpot.

This can be considered either a bug in the compiler or a problem
with chapter 17 of the specification (I would argue for the later).
I have gone over this with Guy Steele, and he agrees with me
that the optimization is not allowed by the current specification.

I have written a paper on the Java memory model which describes
these issues at length:
  "Fixing the Java Memory Model", William Pugh, ACM SIGPLAN Java Grande
  Conference, 1999, available from
        http://www.cs.umd.edu/~pugh/java.

Below I include a sample program that tests for violation of the
Java memory model.

Running this program produces messages such as:
C:pugh # java Coherence
i = 12497, j = 433852, k = 12497, j > k -- in violation of JMM

showing that the JVM is in violation of the specification.
*/

/**
 * This class checks to see if a Java Virtual Machine
 * respects the Java Memory Model, as described in
 * Chapter 17 of the Java Language Specification.
 *
 * The Java Memory Model prohibits certain compiler optimizations.
 * In particular, it requires that for each memory location in isolation,
 * the reads and writes to that memory location have sequentially
 * consistent semantics. One effect of this is that once a thread
 * sees a write to a variable by another thread, it cannot forget that
 * it has seen the write.
 *
 * In particular, for the code sequence:
 *
 *    i = p.x;
 *    j = q.x;
 *    k = p.x;
 *
 * the compiler may not eliminate the second load of p.x unless
 * it can prove that q and p do not point to the same object,
 * or that no other thread can update p.x.
 *
 * This is spelled out in much more detail in
 *
 * "Fixing the Java Memory Model", William Pugh, ACM SIGPLAN Java Grande
 * Conference, 1999, available from http://www.cs.umd.edu/~pugh/java.
 *
 */
import java.awt.Point;

public class Coherence {
    static Point p = new Point();
    static Point q = p;

    public static void check() {
        boolean optimizationDone = true;
        boolean interleavingSeen = false;
        boolean loopHoistingDone = true;
        Point pp = p;
        Point qq = q;
        int i, i0, j, k, m;
        i = 0;
        for (int l = 0; l < 10000000; l++) {
            i0 = i;
            i = pp.x;
            j = qq.x;
            k = pp.x;
            m = pp.x;

            if (l > 0 && i0 != i)
                loopHoistingDone = false;
            if (k != m)
                optimizationDone = false;
            if (i != j)
                interleavingSeen = true;
            if (j > k) {
                System.out.println(
                                   "i = " + i
                                   + ", j = " + j
                                   + ", k = " + k
                                   + ", j > k -- in violation of JMM");
                System.exit(0);
            }
        }
        if (!optimizationDone) {
            System.out.println("optimization not done (yet)");
            interleavingSeen = false;
        } else if (loopHoistingDone)
            System.out.println(
                       "Extremely poor interleaving or Loop hoisting done");
        else if (!interleavingSeen)
            System.out.println("no intra-loop interleaving seen");
        else
            System.out.println(
                "Saw intra-loop interleaving and only legal optimizations");
        Thread.yield();

    }


    public static void main(String args[]) {

        Thread t1 = new Thread() {
            public void run() {
                while (true) {
                    for (int l = 0; l < 10000000; l++) {
                        p.x++;
                    }
                    Thread.yield();
                }
            }
        };
        Thread t2 = new Thread() {
            public void run() {
                for (int l = 0; l < 10; l++)
                    check();
                System.out.println("No violation of the JMM detected");
                System.exit(0);
            }
        };
        t1.start();
        t2.start();
    }
}

(Review ID: 102088) 
======================================================================

Comments
EVALUATION The Java Memory Model was revised for tiger. I am closing this as a dup of the JSR-133 RFE 4639373: Revise Java Memory Model (JSR-133) The submitter was a member of the EG, so is unlikely to have objections.
19-07-2006

WORK AROUND Name: tb29552 Date: 04/12/2000 Workaround As a Java programmer, you have to assume that the specification is meaningless. In a compiler or JIT, you either have to ignore the specification, or disable optimizations (done by other compilers), degrading performance. ======================================================================
11-06-2004

EVALUATION Yes, the problems of the memory model are well known. Until a revised model is devised and accepted thru the JCP, I would not expect either the current spec or implementations to change. Of course, we could just declare that we want sequential consistency, which is the only thing users will ever understand. However, I'm just a reactionary; I don't really expect that to happen. gilad.bracha@eng 2001-01-04
04-01-2001