JDK-8297730 : C2: Arraycopy intrinsic throws incorrect exception
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 9,11,17,18,19,20,21
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2022-10-24
  • Updated: 2023-01-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 21
21Unresolved
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
# Java version
java 19.0.1 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)

# Operating system details
$ cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.5 LTS"
NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

$ uname -a
Linux seoul 5.4.0-99-generic #112~18.04.1-Ubuntu SMP Thu Feb 3 14:09:57 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
Different outputs of the attached program are observed between different compilation tiers. This bug affects java 11.0.17 2022-10-18, java 17 2021-09-14 LTS, Oracle JDK 18.0.2.1, Oracle JDK 19.0.1, and openjdk 20-ea 2023-03-21. It was not reproduced in java version "1.8.0_351".

REGRESSION : Last worked in version 8u351

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
The following steps shows how to reproduce the bug on Java 19.0.1 in a
Ubuntu Linux environment.

# Compile
$ javac C.java

# Default or compilation up to level 4
$ java C
# Output (incorrect)
99997

# Compilation up to level 1
$ java -XX:TieredStopAtLevel=1 C
# Output (correct)
100000

# Interpreter or compilation up to level 0
$ java -Xint C
# Output (correct)
100000

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
100000
ACTUAL -
99997

---------- BEGIN SOURCE ----------
public class C {
    static byte[] m(byte[] arg1) {
        byte[] b = new byte[-1];
        System.arraycopy(arg1, 0, b, 0, arg1.length);
        return b;
    }

    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i < 100_000; ++i) {
            try {
                System.out.println(m(null));
            } catch (Throwable e) {
                if (e instanceof java.lang.NegativeArraySizeException) {
                    count++;
                }
            }
        }
        System.out.println(count); // Should be 100_000
    }
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Interpreter or compilation for Level 0, 1, 2, 3 are fine.
$ java -XX:TieredStopAtLevel=3 C

FREQUENCY : always



Comments
Thanks Christian for narrowing it down. Seems to have been introduced between b50 and b65 in 9 (unfortunately, don't have much to go by in terms of intermediate builds). There are a few _arraycopy related changes committed in this range.
19-12-2022

This already reproduces with JDK 9 (tested with JDK 9+181). Since this is an old regression and the fork is coming up next Thursday, I'm moving this to JDK 21. If a fix is still found in time, this can be re-targeted to JDK 20.
29-11-2022

ILW = Throwing NullPointerException instead of NegativeArraySizeException with _arraycopy intrinsic, single testcase, disable intrinsic with -XX:DisableIntrinsic=_arraycopy = HLM = P3
29-11-2022

This is reproducible with latest JDK 20 and should be moved from JI to JDK (hotspot/compiler).
28-11-2022

Hi Daniel, can you look into this issue
26-10-2022