[英]How can the JVM verify there's no potential operand stack overflow when loading a class?
Going over some presentation, I've come across the following claim: When the JVM loads a class, it can analyze its content and make sure there's no overflow or underflow of the operand stack . 经过一些演示,我遇到了以下声明:当JVM加载一个类时,它可以分析其内容并确保操作数堆栈没有溢出或下溢 。 I've found a lot of sources that make the same claim, but without specifying how it's done.
我发现很多来源都提出了相同的主张,但没有说明它是如何完成的。
It is unclear to me how such verification can be made using static analysis. 我不清楚如何使用静态分析进行此类验证。 Say I have a (malicious) method that gets some value as an argument, and uses it to perform a series of pops.
假设我有一个(恶意)方法,它可以获得一些值作为参数,并使用它来执行一系列弹出。 At load time, the number of iterations is not known, as it depends on the argument given by the method's caller.
在加载时,迭代次数是未知的,因为它取决于方法调用者给出的参数。 Therefore, it seems to me that only at runtime should it be possible to determined whether there will be an underflow or not.
因此,在我看来,只有在运行时才能确定是否会出现下溢。 What am I missing here?
我在这里错过了什么?
You can find basic description of the Bytecode Verifier in Java Virtual Machine specification . 您可以在Java虚拟机规范中找到Bytecode Verifier的基本描述。
To put it simple, stack depth is known at every branching point, and two execution paths merging at the same merge point must also have the same stack depth. 简单来说,堆栈深度在每个分支点都是已知的,并且在同一个合并点处合并的两个执行路径也必须具有相同的堆栈深度。 So, the verifier won't allow you to perform series of pops without corresponding puts.
因此,验证者不允许您在没有相应的放置的情况下执行一系列弹出。
The code of the method is split into execution blocks. 该方法的代码被分成执行块。 A "block" is a sequence of instructions that can be executed without jumping out or into.
“块”是一系列指令,可以在不跳出或跳入的情况下执行。 The blocks build a directed graph of all possible execution paths.
这些块构建了所有可能执行路径的有向图。
A block always expects a certain stack size at its beginning and has a fixed stack size at its end (beginning + all the pushes - all the pops). 块总是期望在其开始时具有特定的堆栈大小并且在其末尾具有固定的堆栈大小(开始+所有推送 - 所有弹出)。 The verifier checks that for all blocks 'a' that can be reached from a given block 'b', the end stack-size of b matches the beginning stack-size of a.
验证器检查对于从给定块'b'可以到达的所有块'a',b的结束堆栈大小匹配a的开始堆栈大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.