JVM Knowledge

The type checker enforces a type system based upon a hierarchy of verification types, illustrated below.

Verification type hierarchy:

                             top
                 ____________/\____________
                /                          \
               /                            \
            oneWord                       twoWord
           /   |   \                     /       \
          /    |    \                   /         \
        int  float  reference        long        double
                     /     \
                    /       \_____________
                   /                      \
                  /                        \
           uninitialized                    +------------------+
            /         \                     |  reference type  |
           /           \                    |  hierarchy       |
uninitializedThis  uninitialized(Offset)    +------------------+
                                                     |
                                                     |
                                                    null

Most verification types have a direct correspondence with the primitive and reference types described in §2.3 and §2.4, and represented by field descriptors in Table 4.3-A:

  • The primitive types double, float, int, and long (field descriptors D, F, I, J) each correspond to the verification type of the same name.

  • The primitive types byte, char, short, and boolean (field descriptors B, C, S, Z) all correspond to the verification type int.

  • Class and interface types (field descriptors beginning L) correspond to verification types that use the functor class. The verification type class(N, L) represents the type of the class or interface whose binary name is N as loaded by the loader L. Note that L is an initiating loader (§5.3) of the class represented by class(N, L) and may, or may not, be the class's defining loader.

    For example, the class type Object would be represented as class('java/lang/Object', L), where the defining loader of class java/lang/Object, as loaded by L, is the bootstrap loader.

  • Array types (field descriptors beginning [) correspond to verification types that use the functor arrayOf. Note that the primitive types byte, char, short, and boolean do not correspond to verification types, but an array type whose element type is byte, char, short, or boolean does correspond to a verification type; such verification types support the baload, bastore, caload, castore, saload, sastore, and newarray instructions.

    • The verification type arrayOf(T) represents the array type whose component type is the verification type T.
    • The verification type arrayOf(byte) represents the array type whose component type is byte.
    • The verification type arrayOf(char) represents the array type whose component type is char.
    • The verification type arrayOf(short) represents the array type whose component type is short.
    • The verification type arrayOf(boolean) represents the array type whose component type is boolean.

    For example, the array types int[] and Object[] would be represented by the verification types arrayOf(int) and arrayOf(class('java/lang/Object', L)) respectively. The array types byte[] and boolean[][] would be represented by the verification types arrayOf(byte) and arrayOf(arrayOf(boolean)) respectively.

The remaining verification types are described as follows:

  • The verification types top, oneWord, twoWord, and reference denote abstract unions of other types, as illustrated above, and are represented in Prolog as atoms.
  • The verification types uninitialized, uninitializedThis, and uninitialized(Offset) describe references to objects created with new that have not yet been initialized (§2.9.1). The types uninitialized and uninitializedThis are represented as atoms. The type uninitialized(Offset) is represented by applying the functor uninitialized to an argument representing the numerical value of the Offset.
  • The verification type null describes the result of the aconst_null instruction, and is represented in Prolog as an atom.

The subtyping rules for verification types are as follows.

Subtyping is reflexive.

isAssignable(X, X).

The type top is a supertype of all other types.

isAssignable(oneWord, top).
isAssignable(twoWord, top).

A type is a subtype of some other type, X, if it has a supertype that is a subtype of X.

isAssignable(int, X)    :- isAssignable(oneWord, X).
isAssignable(float, X)  :- isAssignable(oneWord, X).
isAssignable(long, X)   :- isAssignable(twoWord, X).
isAssignable(double, X) :- isAssignable(twoWord, X).

isAssignable(reference, X)   :- isAssignable(oneWord, X).
isAssignable(class(_, _), X) :- isAssignable(reference, X).
isAssignable(arrayOf(_), X)  :- isAssignable(reference, X).

isAssignable(null, X) :- isAssignable(reference, X).

isAssignable(uninitialized, X)     :- isAssignable(reference, X).
isAssignable(uninitializedThis, X) :- isAssignable(uninitialized, X).
isAssignable(uninitialized(_), X)  :- isAssignable(uninitialized, X).

The type null is a subtype of all reference types.

isAssignable(null, class(_, _)).
isAssignable(null, arrayOf(_)).

These subtype rules are not necessarily the most obvious formulation of subtyping. There is a clear split between subtyping rules among reference types and rules for the remaining verification types. The split allows us to state general subtyping relations between reference types and other verification types. These relations hold independently of a reference type's position in the type hierarchy, and help to prevent excessive class loading by a Java Virtual Machine implementation. For example, we do not want to start climbing the superclass hierarchy in response to a query of the form class(foo, L) <: twoWord.

We also have a rule that says subtyping is reflexive, so it is always trivially possible to assign between two identical types—both in the case of simple types, like from int to int, and more complex types, like from arrayOf(int) to arrayOf(int) or from class('C', Loader) to class('C', Loader). Together, the above rules cover all subtyping relationships except for between two distinct reference types.

The isWideningReference predicate is used to determine if an assignment from one reference type to another, different, reference type is allowed.

isAssignable(From, To) :- isWideningReference(From, To).

The isWideningReference predicate is not reflexive: widening only occurs when converting between distinct reference types.

The verifier allows any class or interface type to be widened to any interface type.

isWideningReference(class(_, _), class(To, L)) :-
    loadedClass(To, L, ToClass),
    classIsInterface(ToClass).

This approach is less strict than the Java programming language, which will not allow an assignment to an interface unless the referenced object is statically known to implement the interface. The Java Virtual Machine instead uses a run-time check to ensure that invocations of interface methods actually operate on objects that implement the interface (§invokeinterface). There is no requirement that a reference stored by a variable of an interface type refers to an object that actually implements that interface.

A class type can be widened to another class type if the second type refers to the loaded class of one of the first type's superclasses, or if they both refer to the same class with different initiating loaders.

isWideningReference(class(From, L1), class(To, L2)) :-
    From \= To,
    loadedClass(From, L1, FromClass),
    loadedClass(To, L2, ToClass),
    loadedSuperclasses(FromClass, Supers),
    member(ToClass, Supers).

isWideningReference(class(ClassName, L1), class(ClassName, L2)) :-
    L1 \= L2,
    loadedClass(ClassName, L1, Class),
    loadedClass(ClassName, L2, Class).

If two class types S and T have the same name and the same loader, there is no need to "widen" from one to the other, as isAssignable is already true, according to the earlier rule.

Array types are subtypes of Object, Cloneable, and java.io.Serializable. Subtyping between arrays of reference type is covariant.

isWideningReference(arrayOf(_), class(ClassName, L)) :-
    (ClassName = 'java/lang/Object' ;
     ClassName = 'java/lang/Cloneable' ;
     ClassName = 'java/io/Serializable')
    loadedClass(ClassName, L, LoadedClass),
    classDefiningLoader(LoadedClass, BL),
    isBootstrapLoader(BL).

isWideningReference(arrayOf(X), arrayOf(Y)) :-
    isWideningReference(X, Y).