Loading class with nested annotations causes stack overflow in VM

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      HotSpot skips parsing of annotations and defers that task to Java side. Algorithm to skip the parsing is recursive. With enough levels of nesting, thread will run out of stack space in native code.
      See skip_annotation_value function and it's friends in classFileParser.cpp

      No crash report is attached, as the error reporting is not even reached on Windows (Program terminates with STATUS_STACK_OVERFLOW).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the program.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Class is loaded successfully.
      ACTUAL -
      VM crashes

      ---------- BEGIN SOURCE ----------
      import org.objectweb.asm.AnnotationVisitor;
      import org.objectweb.asm.ClassWriter;

      import java.lang.invoke.MethodHandles;
      import java.util.ArrayList;

      import static org.objectweb.asm.Opcodes.*;

      public class NestedAnnotationsTest {

      public static void main(String[] args) throws Exception {
      var cw = new ClassWriter(0);
      cw.visit(V17, 0, "Annotations", null, "java/lang/Object", null);
      final int number_of_annotations = 65535;
      var av = cw.visitAnnotation("LTest;", true);
      var stack = new ArrayList<AnnotationVisitor>(number_of_annotations + 1);
      stack.add(av);
      for (int i = 0; i < number_of_annotations; i++) {
      stack.add(av = av.visitAnnotation("value", "LTest;"));
      }
      for (int i = number_of_annotations; i != 0;) {
      stack.get(--i).visitEnd();
      }

      cw.visitEnd();
      // Does not matter whether the class is hidden, used for simplicity’ sake.
      MethodHandles.lookup().defineHiddenClass(cw.toByteArray(), true);
      }
      }

      ---------- END SOURCE ----------

            Assignee:
            Johan Sjölen
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated: