United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-7110586 C2 generates incorrect results
JDK-7110586 : C2 generates incorrect results

Details
Type:
Bug
Submit Date:
2011-11-10
Status:
Closed
Updated Date:
2013-04-24
Project Name:
JDK
Resolved Date:
2012-01-20
Component:
hotspot
OS:
generic
Sub-Component:
compiler
CPU:
generic
Priority:
P2
Resolution:
Fixed
Affected Versions:
hs22
Fixed Versions:
hs23 (b08)

Related Reports
Backport:
Backport:
Backport:
Backport:
Relates:

Sub Tasks

Description
See attached example.

Bug is reproduced on JDK7 b147 and still exist in HS23b05

Here is results for interpreter and C2:
Expected results (int):
[
Test.var_18 = 65
Test.var_20 = 218763548
Test.var_17 = false
Test.var_19 = [
Test_Class_0.var_1 = 11
Test_Class_0.var_4 = 0
Test_Class_0.var_5 = 640
Test_Class_0.var_2 = 612000468
Test_Class_0.var_3 = 1.413301352444783E308
]
]
Actual results(C2):
[
Test.var_18 = 65
Test.var_20 = 218763548
Test.var_17 = false
Test.var_19 = [
Test_Class_0.var_1 = 9
Test_Class_0.var_4 = 0
Test_Class_0.var_5 = 640
Test_Class_0.var_2 = 612000468
Test_Class_0.var_3 = 1.413301352444783E308
]

                                    

Comments
WORK AROUND

-XX:-LoopLimitCheck
                                     
2011-11-11
EVALUATION

Regression after 5091921 fix. Exact limit of empty loop calculated incorrectly.
                                     
2011-11-11
EVALUATION

http://hg.openjdk.java.net/hsx/hotspot-comp/hotspot/rev/e8fdaf4a66cb
                                     
2011-11-11
EVALUATION

The method exact_limit() which has incorrect code is used in two places:

1. policy_do_remove_empty_loop() to calculate final value of iteration variable in empty loops. We found failing case for this:

  static int test2() {
    int i = 0;
    for ( ; i < 11; i+=2) {}
    return i;
  }

Empty loop could be crated due to dead code elimination and also during loop unswitching:

  static int test2(int x, int arr[]) {
    int i = 0;
    for ( ; i < 11; i+=2) {
      if (x != 0) arr[i] = i;
    }
    return i;
  }

which will be transformed into

    if (x != 0) {
      for ( ; i < 11; i+=2) {
        arr[i] = i;
      }
    } else {
      for ( ; i < 11; i+=2) {
        // empty loop
      }
    }

2. PhaseIdealLoop::rc_predicate() calculate limit for range check elimination with loop predicates. It would cause intermediate SEGV in compiled code when accessing array outside of it's range. Here is test which does not throw ArrayIndexOutOfBounds exception:

intelsdv39% cat Test.java
public class Test {
  static int test2(int x, int arr[]) {
    int res = 0;

    for (int i = 0 ; i < 11; i+=2) {
      if (x != 0) {
        res = arr[i];
      } else {
        res = i;
      }
    }
    return res;
  }

  public static void main(String args[]) {
    int x2  = 0;
    // Warm up
    int[] arr = new int[11];
    for (int i=0; i<10000; i++) {
      x2  = test2(i&1, arr);
    }
    for (int i=0; i<10000; i++) {
      x2  = test2(i&1, arr);
    }
    // Test range check in compiled code
    boolean failed = true;
    int[] arr_fail = new int[9];
    try {
      for (int i=0; i<10000; i++) {
        x2  = test2(1, arr_fail);
      }
    } catch (ArrayIndexOutOfBoundsException ex) {
      failed = false;
    }

    if (failed) {
      System.out.println("ERROR: ArrayIndexOutOfBoundsException was not thrown");
      System.exit(97);
    }
  }
}
intelsdv39% /java/re/jdk/7u2/latest/binaries/solaris-i586/fastdebug/bin/java -Xbatch -XX:+PrintCompilation Test
VM option '+PrintCompilation'
    349    1    b        Test::test2 (31 bytes)
ERROR: ArrayIndexOutOfBoundsException was not thrown
intelsdv39% gamma -Xbatch -XX:+PrintCompilation Test
Using java runtime at: /export/kvn/jdk7u2/jre
VM option '+PrintCompilation'
   2979    1    b        Test::test2 (31 bytes)
   3020    1             Test::test2 (31 bytes)   made not entrant
intelsdv39%
                                     
2011-11-14
EVALUATION

http://hg.openjdk.java.net/hsx/hsx22/hotspot/rev/742a2251c87b
                                     
2011-11-17
EVALUATION

http://hg.openjdk.java.net/hsx/hotspot-emb/hotspot/rev/e8fdaf4a66cb
                                     
2011-12-15
EVALUATION

http://hg.openjdk.java.net/lambda/lambda/hotspot/rev/e8fdaf4a66cb
                                     
2012-03-22



Hardware and Software, Engineered to Work Together