United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: 7196857 Int array initializes with nonzero elements.
7196857 : Int array initializes with nonzero elements.

Details
Type:
Bug
Submit Date:
2012-09-07
Status:
In Progress
Updated Date:
2013-06-11
Project Name:
JDK
Resolved Date:
Component:
hotspot
OS:
generic
Sub-Component:
compiler
CPU:
generic
Priority:
P2
Resolution:
Unresolved
Affected Versions:
7
Targeted Versions:
hs25

Related Reports

Sub Tasks

Description
FULL PRODUCT VERSION :
java version: all versions from 1.7.0_04 up to 1.7.0_10
Java HotSpot(TM) 64-Bit Server VM


FULL OS VERSION :
All amd64  Linux, Windows, MacOS distributions.  Not tested on x86 OSes.

A DESCRIPTION OF THE PROBLEM :
Primitive integer array has nonzero elements just after allocation. This situation is in the contradiction with JLS.  Such a behavior occurs after jvm performs the compilation of the corresponding place of the code and only if corresponding code contains Arrays.fill(..) statement somewhere after array initialization (but not just after initialization). For more details see simple code example below.

THE PROBLEM WAS REPRODUCIBLE WITH -Xint FLAG: No

THE PROBLEM WAS REPRODUCIBLE WITH -server FLAG: Yes

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just run the code listed below.

EXPECTED VERSUS ACTUAL BEHAVIOR :
Expected: Build success
Actual: RuntimeException
REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class JvmBug {
public static void main(String[] args) {
        int[] a;
        int n = 0;
        for (int i = 0; i < 100000000; ++i) {
            a = new int[10];
            for (int f : a)
                if (f != 0)
                    throw new RuntimeException("Array just after allocation: "+  Arrays.toString(a));
            Arrays.fill(a, 0);
            for (int j = 0; j < a.length; ++j)
                a[j] = (n - j)*i;
            for (int f : a)
                n += f;
        }
        System.out.println(n);
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Add Arrays.fill(a,0) just after array initialization.

                                    

Comments
Oddly enough, did not reproduce for me.  x86_64, Mac,

Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

or with

java version "1.8.0-internal-fastdebug"
Java(TM) SE Runtime Environment (build 1.8.0-internal-fastdebug-dr2chase_2013_06_10_15_00-b00)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b37-fastdebug, mixed mode)

was this fixed accidentally?
I will continue to investigate.

                                     
2013-06-11
The problem is caused by: when optimizing Arrays.fill() calls, C2 checks if that call is tightly coupled with an array allocation and omits the implicit zeroing if it is; but it's missing necessary checks of the use of the implicit zero'd values, so it may mistakenly omit the zeroing even when there are uses of the array's contents in between the allocation site and the Arrays.fill() call.

The fix would have to enforce the check so that it respects the uses of implicit zero'd values.
                                     
2012-10-18
EVALUATION

The bug reproduces with the fill or with a zeroing for loop, but not without.

So compiler correctly matches a complete array fill, but the check for uses between the allocation and the fill is faulty.
                                     
2012-09-07



Hardware and Software, Engineered to Work Together