在Java中取消引用`null`时是否可能产生未定义的行为? [英] Is it possible to produce undefined behavior when dereferencing `null` in Java?

查看:196
本文介绍了在Java中取消引用`null`时是否可能产生未定义的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解到,在C和C ++中反引用null有时有时会产生

I just learned that dereferencing null in C and C++ can sometimes produce undefined results. This is very intriguing to me, like all bizarre programming behaviors (I once had someone tell me that they debugged "corrupt RAM - program does not run as written" in a legitimate production environment). Because I'm primarily a Java developer, I was wondering if it's possible for this to happen in that language as well?

JLS并不具体说明如何实现null引用( 4.1 15.8.1 ),因此我不太确定但是我在想,可以通过使用不安全的API .不幸的是,我对JVM的内部工作了解不足,无法知道这是否可能.

The JLS is not specific on how the null reference is implemented (3.10.7, 4.1, 15.8.1), so I'm not quite sure. But I was thinking that it might be possible by directly manipulating the memory address with the Unsafe API. Unfortunately I don't have enough knowledge of the inner workings of the JVM to know whether this would be possible or not.

如果 是可能的,那么恶意程序也有可能这样做,这将引发一个有趣的安全问题.

If it is possible, then it would be possible for a malicious program to do so as well, which would open up an interesting security concern.

所以:在引用null时,Java是否有可能具有未定义的行为,而不是简单地抛出NullPointerException?

So: is it possible for Java to have undefined behavior when dereferencing null, rather than simply throwing a NullPointerException?

推荐答案

在纯Java中,您无法从null中获得未定义的行为(除非JVM中存在严重的错误!). JLS指定任何显式或隐式取消引用null的尝试都将导致NullPointerException.没有多余的空间允许任何与null的处理有关的未定义行为.

You cannot get undefined behaviour from a nullin pure Java (unless there is a serious bug in the JVM!). The JLS specifies that any attempt to explicitly or implicitly dereference a null will result in a NullPointerException. There is no wriggle room that allows for any undefined behaviour that is related to the handling of null.

但是,如果您的应用程序包含...或使用... native方法,则其中一种方法可能会误处理null,从而导致未定义的行为.您还可以使用Unsafe类获得未定义的行为.但是这两种情况都意味着您没有使用 pure Java. (当您走出 pure Java时,JLS的保证不再适用!)

However, if your application includes ... or makes use of ... native methods, it is possible for one of those methods to mishandle a null in a way that results in undefined behaviour. You can also get undefined behaviour using the Unsafe class. But both of these scenarios mean you are not using pure Java. (When you step outside of pure Java, the guarantees of the JLS no longer necessarily apply!)

(可能发生不可预测的事情的地方是多线程.但是即使如此,也定义了可能行为的 set .例如,如果您未充分同步状态共享,可能会在字段中看到过时的值.但是您不会看到完全随机的值……或者错误的地址会导致细分冲突.)

(The one area where unpredictable things can happen is in multi-threading. But even then, the set of possible behaviours is defined. For instance, if you don't synchronize state sharing adequately you may see stale values in fields. But you won't see totally random values ... or bad addresses that result in segmentation violations.)

如果有可能,那么恶意程序也有可能这样做,这将引发一个有趣的安全问题.

If it is possible, then it would be possible for a malicious program to do so as well, which would open up an interesting security concern.

恶意程序几乎可以执行任何操作.但是解决此问题的正确方法是在沙箱中执行您不信任的代码(即可能是恶意代码).典型的沙箱会禁止调用Unsafe或加载本机库...以及恶意程序可能利用的许多其他东西.

A malicious program can do almost anything. But the correct way to deal with this is to execute code that you don't trust (i.e. possibly malicious code) in a sandbox. A typical sandbox would forbid calling Unsafe or loading a native library ... and lots of other things that a malicious program could exploit.

这篇关于在Java中取消引用`null`时是否可能产生未定义的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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