C2 miscompilation: Arrays.copyOfRange fails to throw ArrayIndexOutOfBoundsException after JIT optimization

XMLWordPrintable

    • Type: Bug
    • Resolution: Duplicate
    • Priority: P3
    • None
    • Affects Version/s: 17.0.18
    • Component/s: hotspot

      ADDITIONAL SYSTEM INFORMATION :
      Software:
          System Software Overview:
            System Version: Ubuntu 22.04.4 LTS
            Kernel Version: 4.15.0-45-generic
            Boot Volume: overlay
            Boot Mode: Legacy BIOS
            Computer Name: 899309ef3feb
            User Name: root
            Time since boot: 21 weeks, 2 days, 6 hours, 47 minutes

      Hardware:
          Hardware Overview:
            Model: Unknown
            Model Identifier: Unknown
            Chip: Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz
            Total Number of Cores: 40 (10 physical x 2 logical)
            Memory: 125Gi
            System Firmware Version: Unknown

      A DESCRIPTION OF THE PROBLEM :
      There is a compliance issue in HotSpot's C2 compiler regarding java.util.Arrays.copyOfRange. According to the Java API specification, an ArrayIndexOutOfBoundsException must be thrown if the from index is greater than the original array's length. However, after the method is warmed up and compiled by C2, the exception is no longer thrown, leading to incorrect program behavior.

      REGRESSION : Java version that customer using for 11.0.30, 17.0.18



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. javac CopyOfRangeBug.java
      2. java -Xcomp CopyOfRangeBug

      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;
      public class FailedToCatch {
          static int counter = 0;

          public static void main(String[] args) {
              Object[] array = new Object[((byte) (10))];
              for (int i = 0; i < 50000; i++) {
                  test(array);
              }
              if (counter != 50000) {
                  throw new RuntimeException("Test failed");
              }
          }

          static void test(Object[] array) {
              try {
                  Arrays.copyOfRange(array, 15, 20, Object[].class);
              } catch (ArrayIndexOutOfBoundsException e) {
                  counter++;
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY :
      ALWAYS

            Assignee:
            Unassigned
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: