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

StackMapTable is invalid if frames appear in dead code

    XMLWordPrintable

Details

    Description

      If a class contains dead code, and if the dead code contains stack map frames, the written attribute ends up invalid, causing a class format error.

      A reproducer:

       ClassFile classFile = ClassFile.of(ClassFile.StackMapsOption.DROP_STACK_MAPS);
          byte[] bytes = classFile.build(ClassDesc.of("foo.Bar"), classBuilder -> classBuilder.withMethod(
                  "foo",
                  MethodTypeDesc.ofDescriptor("()J"),
                  0,
                  methodBuilder -> {
                      methodBuilder.withCode(codeBuilder -> {
                          codeBuilder.new_(ClassDesc.of(RuntimeException.class.getName()));
                          codeBuilder.dup();
                          codeBuilder.invokespecial(ClassDesc.of(RuntimeException.class.getName()),
                                  "<init>",
                                  MethodTypeDesc.ofDescriptor("()V"));
                          codeBuilder.athrow();
                          Label f2 = codeBuilder.newBoundLabel();
                          codeBuilder.lstore(1);
                          Label f3 = codeBuilder.newBoundLabel();
                          codeBuilder.lload(1);
                          codeBuilder.lreturn();
                          codeBuilder.with(StackMapTableAttribute.of(List.of(
                                  StackMapFrameInfo.of(f2,
                                          List.of(StackMapFrameInfo.ObjectVerificationTypeInfo.of(ClassDesc.of("foo.Bar"))),
                                          List.of(StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_LONG)),
                                  StackMapFrameInfo.of(f3,
                                          List.of(StackMapFrameInfo.ObjectVerificationTypeInfo.of(ClassDesc.of("foo.Bar")),
                                                  StackMapFrameInfo.SimpleVerificationTypeInfo.ITEM_LONG),
                                          List.of())
                          )));
                      });
                  }));
          new ClassLoader() {
              @Override
              protected Class<?> findClass(String name) throws ClassNotFoundException {
                  if (name.equals("foo.Bar")) {
                      return defineClass(name, bytes, 0, bytes.length);
                  } else {
                      return super.findClass(name);
                  }
              }
          }.findClass("foo.Bar").getMethods();

      Attachments

        Issue Links

          Activity

            People

              asotona Adam Sotona
              winterhalter Rafael Winterhalter
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: