Duplicate :
|
This is a bug in the C2 compiler related to overflowing an array and loop optimizations. This is seen in JDK 6uXX and the latest JDK 7 HS 20 nightly builds. Test machine(s): OS: Solaris CPU Sparc JDK: 6u19, and JDK7 b120 - Java HotSpot(TM) Server VM (build 20.0-b03, mixed mode) Os: SUSE Enterprise Server 11 CPU: AMD Opteron (tm) Processor 848 Kernel: Linux 2.6.27.19-5-default #1 SMP 2009-02-28 04:40:21 +0100 x86_64 x86_64 x86_64 GNU/Linux JDK: 1.6.0_06, and JDK 7 b99 Test case: (also attached) public class OverflowTest{ static int test(byte a[]){ int result=0; for( int i=0; i<a.length; i+=((0x7fffffff>>1)+1) ){ result += a[i]; } return result; } public static void main(String [] args){ byte a[]=new byte[(0x7fffffff>>1)+2]; System.out.println(""+test(a)); } } Correct Results: java -Xms2048m -Xint OverflowTest > Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147483648 > at OverflowTest.test(OverflowTest.java:5) > at OverflowTest.main(OverflowTest.java:12) Incorrect Results: java -Xms2048m -Xcomp -XX:CompileOnly=OverflowTest.test OverflowTest > 0 If you disable loop optimization, the result is correct. java -Xms2048m -Xcomp -XX:CompileOnly=OverflowTest.test -XX:LoopOptsCount=0 OverflowTest > Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2147483648 > at OverflowTest.test(OverflowTest.java:5) > at OverflowTest.main(OverflowTest.java:12)