A class file whose version number is 50.0 or above (§4.1) must be verified using the type checking rules given in this section.
If, and only if, a class file's version number equals 50.0, then if the type checking fails, a Java Virtual Machine implementation may choose to attempt to perform verification by type inference (§4.10.2).
This is a pragmatic adjustment, designed to ease the transition to the new verification discipline. Many tools that manipulate class files may alter the bytecodes of a method in a manner that requires adjustment of the method's stack map frames. If a tool does not make the necessary adjustments to the stack map frames, type checking may fail even though the bytecode is in principle valid (and would consequently verify under the old type inference scheme). To allow implementors time to adapt their tools, Java Virtual Machine implementations may fall back to the older verification discipline, but only for a limited time.
In cases where type checking fails but type inference is invoked and succeeds, a certain performance penalty is expected. Such a penalty is unavoidable. It also should serve as a signal to tool vendors that their output needs to be adjusted, and provides vendors with additional incentive to make these adjustments.
In summary, failover to verification by type inference supports both the gradual addition of stack map frames to the Java SE Platform (if they are not present in a version 50.0 class file, failover is allowed) and the gradual removal of the jsr and jsr_w instructions from the Java SE Platform (if they are present in a version 50.0 class file, failover is allowed).
If a Java Virtual Machine implementation ever attempts to perform verification by type inference on version 50.0 class files, it must do so in all cases where verification by type checking fails.
This means that a Java Virtual Machine implementation cannot choose to resort to type inference in one case and not in another. It must either reject class files that do not verify via type checking, or else consistently failover to the type inferencing verifier whenever type checking fails.
The type checker enforces type rules that are specified by means of Prolog clauses. English language text is used to describe the type rules in an informal way, while the Prolog clauses provide a formal specification.
The type checker requires a list of stack map frames for each method with a Code attribute (§4.7.3). A list of stack map frames is given by the StackMapTable attribute (§4.7.4) of a Code attribute. The intent is that a stack map frame must appear at the beginning of each basic block in a method. The stack map frame specifies the verification type of each operand stack entry and of each local variable at the start of each basic block. The type checker reads the stack map frames for each method with a Code attribute and uses these maps to generate a proof of the type safety of the instructions in the Code attribute.
The Prolog predicate methodIsTypeSafe (§4.10.1.5) determines whether a method with a Code attribute is type safe. If a method with a Code attribute is not type safe, verification throws a VerifyError to indicate that the class file is malformed.
If, for a given class, every method with a Code attribute passes the methodIsTypeSafe check, the class file has type checked successfully and bytecode verification has completed successfully.
The rest of this section explains the process of type checking in detail:
- First, we give Prolog predicates for core Java Virtual Machine artifacts like classes and methods (§4.10.1.1).
- Second, we specify the type system known to the type checker (§4.10.1.2).
- Third, we specify the Prolog representation of instructions and stack map frames (§4.10.1.3, §4.10.1.4).
- Fourth, we specify how a method is type checked (§4.10.1.5, §4.10.1.6).
- Fifth, we discuss type checking issues common to all load and store instructions (§4.10.1.7), and also issues of access to
protectedmembers (§4.10.1.8). - Finally, we specify the rules to type check each instruction (§4.10.1.9).
Sections:
- 4.10.1.1. Accessors for Java Virtual Machine Artifacts
- 4.10.1.2. Verification Type System
- 4.10.1.3. Instruction Representation
- 4.10.1.4. Stack Map Frames and Type Transitions
- 4.10.1.5. Type Checking Methods
- 4.10.1.6. Type Checking Code Streams
- 4.10.1.7. Type Checking Load and Store Instructions
- 4.10.1.8. Type Checking for protected Members
- 4.10.1.9. Type Checking Instructions