---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 10 Edition"
java_version: 10
chapter: "Chapter 4. The class File Format"
section: "4.10.1"
heading: "4.10.1. Verification by Type Checking"
parent: "4.10. Verification of class Files"
source: https://docs.oracle.com/javase/specs/jvms/se10/html/jvms-4.html#jvms-4.10.1
id: "4.10.1"
url: https://jvm.obfuscated.software/jvms/se10/4.10.1.html
raw: https://jvm.obfuscated.software/jvms/se10/4.10.1.md
---

# 4.10.1. Verification by Type Checking

A `class` file whose version number is 50.0 or above ([§4.1](4.1.md)) 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](4.10.2.md)).

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 once 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](4.7.3.md)). A list of stack map frames is given by the `StackMapTable` attribute ([§4.7.4](4.7.4.md)) 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.

A class is type safe if all its methods are type safe, and it does not subclass a `final` class.

```
classIsTypeSafe(Class) :-
    classClassName(Class, Name),
    classDefiningLoader(Class, L),
    superclassChain(Name, L, Chain),
    Chain \= [],
    classSuperClassName(Class, SuperclassName),
    loadedClass(SuperclassName, L, Superclass),
    classIsNotFinal(Superclass),
    classMethods(Class, Methods),
    checklist(methodIsTypeSafe(Class), Methods).
```

```
classIsTypeSafe(Class) :-
    classClassName(Class, 'java/lang/Object'),
    classDefiningLoader(Class, L),
    isBootstrapLoader(L),
    classMethods(Class, Methods),
    checklist(methodIsTypeSafe(Class), Methods).
```

The Prolog predicate `classIsTypeSafe` assumes that `Class` is a Prolog term representing a binary class that has been successfully parsed and loaded. This specification does not mandate the precise structure of this term, but does require that certain predicates be defined upon it.

For example, we assume a predicate `classMethods(Class, Methods)` that, given a term representing a class as described above as its first argument, binds its second argument to a list comprising all the methods of the class, represented in a convenient form described later.

Iff the predicate `classIsTypeSafe` is not true, the type checker must throw the exception `VerifyError` to indicate that the `class` file is malformed. Otherwise, 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](4.10.1.1.md)).
- Second, we specify the type system known to the type checker ([§4.10.1.2](4.10.1.2.md)).
- Third, we specify the Prolog representation of instructions and stack map frames ([§4.10.1.3](4.10.1.3.md), [§4.10.1.4](4.10.1.4.md)).
- Fourth, we specify how a method is type checked, for methods without code ([§4.10.1.5](4.10.1.5.md)) and methods with code ([§4.10.1.6](4.10.1.6.md)).
- Fifth, we discuss type checking issues common to all load and store instructions ([§4.10.1.7](4.10.1.7.md)), and also issues of access to `protected` members ([§4.10.1.8](4.10.1.8.md)).
- Finally, we specify the rules to type check each instruction ([§4.10.1.9](4.10.1.9.md)).

**Sections:**

- [4.10.1.1. Accessors for Java Virtual Machine Artifacts](4.10.1.1.md)
- [4.10.1.2. Verification Type System](4.10.1.2.md)
- [4.10.1.3. Instruction Representation](4.10.1.3.md)
- [4.10.1.4. Stack Map Frames and Type Transitions](4.10.1.4.md)
- [4.10.1.5. Type Checking Abstract and Native Methods](4.10.1.5.md)
- [4.10.1.6. Type Checking Methods with Code](4.10.1.6.md)
- [4.10.1.7. Type Checking Load and Store Instructions](4.10.1.7.md)
- [4.10.1.8. Type Checking for protected Members](4.10.1.8.md)
- [4.10.1.9. Type Checking Instructions](4.10.1.9.md)
