Andy Malakov software blog

Thursday, July 31, 2008

A bug in Sun's 64-bit hotspot compiler

A colleague of mine found a scary bug in Sun's hotspot compiler (64 bit version). It is reproducible on several computers in our office (CPUs: 2x AMD Opteron, Intel E8400; OS: Windows XP Professional 64-bit). Here is the code:

public class FailingTest {

private final java.io.RandomAccessFile raf;
private long sequence;

public FailingTest() throws Exception {
java.io.File f = new java.io.File("test.dat");
f.delete();
raf = new java.io.RandomAccessFile(f, "rw");
f.deleteOnExit();
}

public void test(long timestamp) throws Exception {
// This condition never happens but is required to
// reproduce this bug, so is unused timestamp argument
if (timestamp < 0)
System.exit(0);

final long valueToStore = sequence++;
raf.seek(0);
raf.writeLong(valueToStore);
raf.seek(0);
final long valueRead = raf.readLong();
if (valueRead != valueToStore)
System.err.println("Error: Read value " +
valueRead + " != " + valueToStore);
}

public static void main (String [] args) throws Exception {
FailingTest t = new FailingTest();
for (int ii = 0; ii < 1000000; ii++)
t.test(ii);
}
}

On my machine this test fails with the following output (I run it with -server -XX:CompileThreshold=100):

Error: Read value 4826 != 4825

The test fails every time with different numbers, with any version of 64-bit Sun JDK 1.6 we tried. This problem is NOT reproducible under 32-bit version and NOT reproducible on BEA's JRockit 1.6 (64 bit). Just verified that problem remains on the latest JVM available: build 1.6.0_10-beta-b25, Java HotSpot(TM) 64-Bit Server VM (build 11.0-b12, mixed mode).