Question

How does JVM ensure that jump instructions (jsr and goto) jump to the instruction and not data or instruction arguments?

Was it helpful?

Solution

This is part of the bytecode verifier.

In Java bytecode, instructions all have a known length and must appear one after another starting at offset 0, so you can always tell statically where the beginning of each instruction is. Therefore, it's just a simple matter of making sure the jump target is the beginning of a valid instruction. Unlike with Dalvik or x86, you aren't allowed to jump into the middle of an instruction.

You can find the source code for the verifier here. Actually there are two verifiers. This is the classic verfier, used for code which doesn't have a StackMapTable (this includes all code containing jsr).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top