JDK-8020215 : Different execution plan when using JIT vs interpreter
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: hs24,hs25
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • Submitted: 2013-06-28
  • Updated: 2013-08-13
  • Resolved: 2013-07-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7 JDK 8 Other
7u40Fixed 8Fixed hs24Fixed
Description
FULL PRODUCT VERSION :
java version  " 1.7.0_25 " 
Java(TM) SE Runtime Environment (build 1.7.0_25_b16)
Java Hotspot(TM) 64-bit Server VM (build 23.25-b01, mixed mode)

FULL OS VERSION :
Windows 7 64-bit

EXTRA RELEVANT SYSTEM CONFIGURATION :
No additional configuration needed

A DESCRIPTION OF THE PROBLEM :
The attached sample program calls a method 10'000 times without changing any border conditions. As soon as the method gets compiled, the result of that method returns null instead of a instance.

Please note that the attached sample was stripped down a lot and multiple coding standards were violated to make the sample code as small as possible.

THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Did not try

REGRESSION.  Last worked in version 6u45

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) compile the sample java program
2) run it with the java interpreter -> run through
3) run it with the jit enabled -> returned method value is null and it throws an exception

EXPECTED VERSUS ACTUAL BEHAVIOR :
I would expect that the program never throws an excpetion
ERROR MESSAGES/STACK TRACES THAT OCCUR :
no error messages

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.ArrayList;
import java.util.List;

public class TestJITError {
public static class NamedObject {
    public int id;
    public String name;
public NamedObject(int id, String name) {
this.id = id;
this.name = name;
}
}

public static class NamedObjectList {
public List<NamedObject> namedObjectList = new ArrayList<NamedObject>();

    public NamedObject getBest(int id) {
        NamedObject bestObject = null;
        for (NamedObject o : namedObjectList) {
            bestObject = id==o.id ? getBetter(bestObject, o) : bestObject;
        }
        return bestObject != null ? bestObject : null;
    }

    private static NamedObject getBetter(NamedObject p1, NamedObject p2) {
    return p1 == null ? p2 : (p2 == null) ? p1 : p2.name.compareTo(p1.name)>=0 ? p2 : p1;
    }
}

public static void main(String[] args) {
// setup
NamedObjectList b = new NamedObjectList();
for (int i = 0; i < 10000; i++) {
b.namedObjectList.add(new NamedObject(1,  " 2012-12-31 " ));
}
b.namedObjectList.add(new NamedObject(2,  " 2013-12-31 " ));

// execution
for (int i = 0; i < 100000; i++) {
NamedObject x = b.getBest(2);
// test
if (x == null) {
throw new RuntimeException( " x should never be null here!! (i= "  + i +  " ) " );
}
}
System.out.println( " finished " );
}
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Refactoring of the java code will make it behave correctly.
Comments
Verified with JDK/JRE: 7u40b35. Reproduced with JDK/JRE: 7u40b31. Test: java -showversion TestJITError Host: localhost, Windows 7, amd64.
31-07-2013

7-critical-request justification: Was found by external customer with jdk7. Small safe changes. The fix is pushed into hs25/jdk8 and will be tested (see https://jbs.oracle.com/bugs/browse/JDK-8020482).
13-07-2013

workaround: -XX:-OptimizePtrCompare
10-07-2013

Yes, it is EA. It failedwith old (hs23) and new code (hs25).
09-07-2013

When I disable Escape Analysis the test passes: dhcp-santaclara22-3fl-west-10-132-189-104:~ ajiva$ jdk8/bin/java -showversion -XX:-DoEscapeAnalysis TestJITError java version "1.8.0-ea" Java(TM) SE Runtime Environment (build 1.8.0-ea-b94) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b36, mixed mode) finished dhcp-santaclara22-3fl-west-10-132-189-104:~ ajiva$ jdk8/bin/java -showversion TestJITError java version "1.8.0-ea" Java(TM) SE Runtime Environment (build 1.8.0-ea-b94) Java HotSpot(TM) 64-Bit Server VM (build 25.0-b36, mixed mode) Exception in thread "main" java.lang.RuntimeException: x should never be null here!! (i= 12288 ) at TestJITError.main(TestJITError.java:43) dhcp-santaclara22-3fl-west-10-132-189-104:~ ajiva$
09-07-2013