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

Compiler produces incorrect code for code with exception handling

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "26-internal" 2026-03-17
      OpenJDK Runtime Environment (fastdebug build 26-internal-adhoc.User.jdk)
      OpenJDK 64-Bit Server VM (fastdebug build 26-internal-adhoc.User.jdk, mixed mode, sharing)

      OpenJDK 64-Bit Server VM (fastdebug 26-internal-adhoc.User.jdk) for windows-amd64 JRE (26-internal-adhoc.User.jdk), built on 2025-09-03T14:32:18Z with MS VC++ 16.10 / 16.11 (VS2019

      The error is reproducible with JDK 25 as well.

      A DESCRIPTION OF THE PROBLEM :
      In https://github.com/openjdk/jdk/blob/444a8fa14e8ab016b8aae018054c5dc1eb843fee/src/hotspot/share/oops/method.cpp#L241:

      If during class resolution of an exception type an error is thrown, the exception search is attempted again but at handler_bci:
      https://github.com/openjdk/jdk/blob/444a8fa14e8ab016b8aae018054c5dc1eb843fee/src/hotspot/share/oops/method.cpp#L299

      An exception table entry could be placed around handler_bci that catches that exception, and control, at least during use of the interpreter, goes to said handler.
      JIT seem to remove that guard, and original exception is thrown instead.

      The program executes correctly with -Xint.
      The program does not execute correctly with -XX:TieredStopAtLevel=3, 2 or 1.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the program, ASM dependency is needed.

      The catch type in the test program could be replaced with a class that is in the same module, but has it's access set to PRIVATE.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Program completes, final call prints `1`.
      ACTUAL -
      Exception in thread "main" java.lang.IllegalAccessError: class Test/0x0000000013138c00 (in unnamed module @0x3cd3e762) cannot access class jdk.internal.agent.AgentConfigurationError (in module jdk.management.agent) because module jdk.management.agent does not export jdk.internal.agent to unnamed module @0x3cd3e762
              at Test12.invoke(Test12.java:66)
              at Test12.main(Test12.java:60)

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

      import java.lang.invoke.MethodHandle;
      import java.lang.invoke.MethodHandles;
      import java.lang.invoke.MethodType;

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

      public class Test12 {

      public static void main(String[] args) throws Throwable {
      var cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES) {
      @Override
      protected String getCommonSuperClass(String type1, String type2) {
      return "java/lang/Throwable";
      }
      };
      cw.visit(V1_8, ACC_PUBLIC, "Test", null, "java/lang/Object", null);
      var mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
      mv.visitCode();
      mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
      mv.visitMethodInsn(INVOKESTATIC, "Test", "run", "()I", false);
      mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(I)V", false);
      mv.visitInsn(RETURN);
      mv.visitMaxs(-1, -1);
      mv.visitEnd();
      mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "run", "()I", null, null);

      var div_start = new Label();
      var div_end = new Label();
      var div_handler = new Label();
      var guard_start = div_handler;
      var guard_end = new Label();
      var guard_handler = new Label();

      mv.visitInsn(ICONST_0);
      mv.visitInsn(ICONST_0);
      mv.visitLabel(div_start);
      mv.visitInsn(IDIV);
      mv.visitLabel(div_end);
      mv.visitInsn(IRETURN);
      mv.visitLabel(div_handler);
      mv.visitInsn(NOP); // Non-empty exception range.
      mv.visitLabel(guard_end);
      mv.visitInsn(ICONST_0);
      mv.visitInsn(IRETURN);
      mv.visitLabel(guard_handler);
      mv.visitInsn(ICONST_1);
      mv.visitInsn(IRETURN);
      mv.visitTryCatchBlock(div_start, div_end, div_handler, "jdk/internal/agent/AgentConfigurationError");
      mv.visitTryCatchBlock(guard_start, guard_end, guard_handler, "java/lang/IllegalAccessError");
      mv.visitMaxs(-1, -1);
      mv.visitEnd();
      cw.visitEnd();
      byte[] bytes = cw.toByteArray();
      var l = MethodHandles.lookup().defineHiddenClass(bytes, true);
      var mh = l.findStatic(l.lookupClass(), "run", MethodType.methodType(int.class));
      for (int i = 0; i < 16_000; i++) {
      invoke(mh);
      }
      System.out.println(invoke(mh));
      }

      private static int invoke(MethodHandle mh) throws Throwable {
      return (int) mh.invokeExact();
      }
      }

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

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: