Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8300585

4.10.1.9.putfield: verification rules allow early write to superclass field

XMLWordPrintable

    • vm

      Extracted from: https://github.com/eclipse-openj9/openj9/issues/16524

      public class AcceptInvalid01 {
          public AcceptInvalid01() {
              this.field = new InputMethodEvent((Component)null, -100, (TextHitInfo)null, (TextHitInfo)null);
              super();
          }

          public static void main(String[] var0) {
          }
      }

      The above decompiled class is an invalid class (per the JLS), which was generated by a jasm file. IBM J9 VM accepts this "invalid" classfile while hotspot rejects it with:

      Caused by: java.lang.VerifyError: Bad type on operand stack in putfield
      Exception Details:
        Location:
          AcceptInvalid01.<init>()V @22: putfield
        Reason:
          Type uninitializedThis (current frame, stack[0]) is not assignable to 'AcceptInvalid01' (constant pool 4)
        Current Frame:
          bci: @22
          flags: { flagThisUninit }
          locals: { uninitializedThis }
          stack: { uninitializedThis, 'java/awt/event/InputMethodEvent' }
        Bytecode:
          0000000: 2abb 0003 5901 c000 0110 9c01 c000 0601
          0000010: c000 06b7 0005 b500 042a b700 02b1

      However the JVMS states:

      4.9.2 Structural Constraints
      ...
      each instance initialization method, except for the instance initialization method
      derived from the constructor of class Object, must call either another instance
      initialization method of this or an instance initialization method of its direct
      superclass super before its instance members are accessed.

      However, instance fields of this that are declared in the current class may be
      assigned by putfield before calling any instance initialization method. <--------

      4.10 Verification of class Files
      ...
      putfield

      A putfield instruction with operand CP is type safe iff all of the following are true:
      Its first operand, CP, refers to a constant pool entry denoting a field
      whose declared type is FieldType, declared in a class FieldClassName.
      FieldClassName must not be an array type

      If the instruction occurs in an instance initialization method of the class
      FieldClassName, then one can validly pop types matching FieldType and
      uninitializedThis off the incoming operand stack yielding the outgoing
      type state. This allows instance fields of this that are declared in the current
      class to be assigned prior to complete initialization of this. <---------

      Based on the JVM Spec above, my understand is that this behavior shouldn't be captured as the assignment via putfield is totally valid & allowed before completing the initialization of this in the instance initialization method of the current class.

            dlsmith Dan Smith
            shadowbug Shadow Bug
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: