[lworld] JVM_CopyOfSpecialArray does not handle all inputs correctly

XMLWordPrintable

      `JVM_CopyOfSpecialArray` is used to implement `Arrays.copyOf` and `Arrays.copyOfRange`.

      Most of the parameter exception checking is not checked and thrown in the Java code.

      `JVM_CopyOfSpecialArray` does not check the inputs correctly. May throw the wrong exceptions etc.

      `Arrays.copyOf` and `Arrays.copyOfRange` have different requirements for what values for the from index. For larger than the original length - 1 the former creates a null padded larger array, the latter throws an exception.

      With the following program we see differences between enable-preview and not:

      ```
      import java.util.Arrays;

      class ArraysTest {
        public static void main(String args[]) throws Exception {
          var array = new Integer[20];
          System.out.println(Arrays.copyOfRange(array, -10, -5));
          System.out.println(Arrays.copyOfRange(array, 20, 30));
        }
      }
      ```

      With the output:
      ```
      % images/jdk/bin/javac --source 27 --enable-preview ArraysTest.java
      % images/jdk/bin/java --enable-preview ArraysTest
      [Ljava.lang.Integer;@2b2fa4f7
      [Ljava.lang.Integer;@1dbd16a6
      % images/jdk/bin/javac --source 27 ArraysTest.java
      % images/jdk/bin/java ArraysTest
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: arraycopy: source index -10 out of bounds for object array[20]
              at java.base/java.lang.System.arraycopy(Native Method)
              at java.base/java.util.Arrays.copyOfRange(Arrays.java:3822)
              at java.base/java.util.Arrays.copyOfRange(Arrays.java:3772)
              at ArraysTest.main(ArraysTest.java:6)
      ```

      Side note: We currently also crash on uninitialised classes because we do not have the null reset value setup. However this issue is resolved with JDK-8377451.
      ```
      import java.util.Arrays;

      value record Values(int i) {}

      class ArraysTest {
        public static void main(String args[]) throws Exception {
          var array = new Values[20];
          System.out.println(Arrays.copyOfRange(array, 0, 10));
        }
      }
      ```

            Assignee:
            Paul Hübner
            Reporter:
            Axel Boldt-Christmas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: