如何在JVM中验证字节码? [英] How does bytecode get verified in the JVM?

查看:220
本文介绍了如何在JVM中验证字节码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在JVM中验证字节码?

How does bytecode get verified in the JVM?

推荐答案

Oracle本身有一个关于它如何工作的小片页这里

Oracle themselves have a little snippet page on how it works here.

基本上,JRE不信任JDK。那是因为它不知道哪个JDK编译器创建了类文件。它会将类文件视为敌对,直到验证为止。

Basically, the JRE doesn't trust the JDK. That's because it has no knowledge of which JDK compiler created the class file. It treats the class file as hostile until verified.

扩展,字节码验证是防止Sun称之为恶意编译器的必要步骤。 Sun自己的Java编译器确保Java源代码不违反安全规则,但是当应用程序导入代码片段时,如果代码片段遵循Java语言规则以确保安全,则它实际上知道。换句话说,代码可能不是由值得信赖的Java编译器生成的。

Expanding on that, the bytecode verification is a necessary step to protect from what Sun call a "hostile compiler". Sun's own Java compiler ensures that Java source code doesn't violate the safety rules but, when an application imports a code fragment, it doesn't actually know if the code fragment follows Java language rules for safety. In other words, the code may not have been produced by a trustworthy Java compiler.

在这种情况下,机器上的Java运行时系统必须假定片段是糟糕,并对其进行字节码验证。

In that case, the Java run time system on your machine has to assume the fragment is bad and subjects it to bytecode verification.

在完成此验证过程之前,Java虚拟机甚至不会查看字节码。在加载字节码时执行此操作还具有以下优点:每次执行代码时都不需要执行大量的运行时检查。因为它已被验证为正确,所以它一旦开始运行就可以比其他方式运行得更快。

The Java virtual machine does not even see the bytecode until it's been through this verification process. Doing this as the bytecode is loaded also has the advantage that a whole lot of run time checks don't need to be performed every time the code is executed. Because it's been verified as correct, it can, once it starts running, run faster than would otherwise be possible.

链接图的再现如下:

                    <<<=== Unsafe / Safe ===>>>
                                  \
+---------------+        +-------------------+
|  Java source  |   +--> |   Class loader    | --+
+---------------+   |    | Bytecode verifier |   |
        |           |    +-------------------+   |
        V           |             /              |
+---------------+   |             \              V
| Java compiler |  Network        /    +-------------------+
+---------------+   |             \    |      JVM/JIT      |
        |           |             /    +-------------------+
        V           |             \              |
+---------------+   |             /              V
| Java bytecode | --+             \    +-------------------+
+---------------+                 /    | Operating system  |
                                  \    +-------------------+
                                  /              |
                                  \              V
                                  /    +-------------------+
                                  \    |     Hardware      |
                                  /    +-------------------+
                                  \
                    <<<=== Unsafe / Safe ===>>>

这篇关于如何在JVM中验证字节码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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