Java:int数组使用非零元素初始化 [英] Java: int array initializes with nonzero elements

查看:172
本文介绍了Java:int数组使用非零元素初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据JLS,初始化后应该用零填充 int 数组。但是,我面临的情况并非如此。这种行为首先发生在JDK 7u4中,也发生在所有后续更新中(我使用64位实现)。以下代码抛出异常:

According to the JLS, an int array should be filled by zeros just after initialization. However, I am faced with a situation where it is not. Such a behavior occurs first in JDK 7u4 and also occurs in all later updates (I use 64-bit implementation). The following code throws exception:

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);
    }

在JVM执行代码块编译后不会发生异常使用 -Xint 标记。此外, Arrays.fill(...)语句(与此代码中的所有其他语句一样)是必需的,如果不存在则不会发生异常。很明显,这个可能的错误与一些JVM优化有关。出于这种行为的原因有什么想法?

The exception occurs after the JVM performs compilation of the code block and does not arise with -Xint flag. Additionally, the Arrays.fill(...) statement (as all other statements in this code) is necessary, and the exception does not occurs if it is absent. It is clear that this possible bug is bounded with some JVM optimization. Any ideas for the reason of such a behavior?

更新:

我在HotSpot 64位上看到这种行为服务器VM,Java版本从1.7.0_04到1.7.0_10在Gentoo Linux,Debian Linux(内核3.0版本)和MacOS Lion上。使用上面的代码可以始终重现此错误。我没有使用32位JDK或Windows测试此问题。我已经向Oracle发送了一个错误报告(错误ID 7196857),它将在几天内出现在公共Oracle错误数据库中。

Update:
I see this behavior on HotSpot 64-bit server VM, Java version from 1.7.0_04 to 1.7.0_10 on Gentoo Linux, Debian Linux (both kernel 3.0 version) and MacOS Lion. This error can always be reproduced with the code above. I did not test this problem with a 32-bit JDK or on Windows. I already sent a bug report to the Oracle (bug id 7196857) and it will appear in public Oracle bug database in few days.

更新:

Oracle在他们的公共错误数据库中发布了这个错误: http ://bugs.sun.com/bugdatabase/view_bug.do?bug_id = 7196857

推荐答案

我们在这里面对JIT编译器中的错误。编译器在 Arrays.fill(...)中分配后确定已分配的数组已填充,但检查分配和填充之间的使用是否有问题。因此,编译器执行非法优化 - 它会跳过已分配数组的归零。

Here we are faced with a bug in the JIT-compiler. Compiler determines that the allocated array is filled after allocation in Arrays.fill(...), but the check for uses between the allocation and the fill is faulty. So, compiler performs an illegal optimization - it skips zeroing of allocated array.

此错误放在Oracle错误跟踪器中(bug id 7196857 )。不幸的是,我没有等待甲骨文有关以下几点的任何澄清。正如我所看到的,这个bug是特定于操作系统的:它在64位Linux和Mac上完全可以重现,但是,正如我在评论中看到的那样,它在Windows上不经常再现(对于类似版本的JDK)。另外,知道什么时候修复这个bug会很好。

This bug is placed in Oracle bug tracker (bug id 7196857). Unfortunately, I did not wait for any clarifications from Oracle about the following points. As I see, this bug is OS-specific: it absolutely reproducible on 64-bit Linux and Mac, but, as I see from comments, it reproduces not regularly on Windows (for similar versions of JDK). Additionally it would be nice to know when this bug will be fixed.

目前只有建议:如果依赖,请不要使用JDK1.7.0_04或更高版本新声明的数组的JLS。

There is only advice at the moment: do not use JDK1.7.0_04 or later if you depend on JLS for newly declared arrays.

10月5日更新:

在2012年10月4日发布的JDK 7u10(早期访问)的新 Build 10 ,至少在Linux上修复了这个错误操作系统(我没有测试其他)。感谢@Makoto,他发现Oracle错误数据库中的公共访问不再可用。不幸的是,我不知道甲骨文删除它从公共访问的原因,但它在谷歌的cache 。此外,此错误引起了Redhat的注意:CVE标识符 CVE-2012-4420 bugzilla )和 CVE-2012-4416 bugzilla )被分配到这个缺陷。

In the new Build 10 of the JDK 7u10 (early access) released at October 04, 2012, this bug was fixed at least for Linux OS (I did not test for other). Thanks to @Makoto, who found that this bug is no longer available for public access in Oracle bug database. Unfortunately, I do not know for the reasons Oracle removed it from public access, but it is available in Google cache. Also, this bug has caught the attention of Redhat: the CVE identifiers CVE-2012-4420 (bugzilla) and CVE-2012-4416 (bugzilla) were assigned to this flaw.

这篇关于Java:int数组使用非零元素初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆