JDK-8336293 : C2 produces inconsistent results with loops
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 17,21,23
  • Priority: P2
  • Status: Open
  • Resolution: Unresolved
  • OS: linux_ubuntu
  • CPU: x86_64
  • Submitted: 2024-07-01
  • Updated: 2024-07-12
Related Reports
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
```bash
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
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"
UBUNTU_CODENAME=jammy

jdk version: jdk-21.0.3

A DESCRIPTION OF THE PROBLEM :
When running the test case using the release version of OpenJDK21, it can be observed that the results are inconsistent before and after applying the -Xint parameter. We believe this could be a rather serious issue, as testing has shown that this problem occurs with OpenJDK17 release version, OpenJDK17 mainline, and OpenJDK23 mainline. However, it does not occur with OpenJDK11 and OpenJDK8.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
javac Test.java && java Test

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
4000000
ACTUAL -
3991842

---------- BEGIN SOURCE ----------
class Test {
  int a = 400;
  double[] b = new double[a];
  long c;

  void bMeth() {
    long d;
    double[] e = new double[a];
    for (int f = 0; f < e.length; f++)
      e[f] = 1.116242;
    d = 1;
    while (++d < 7)
      synchronized (new Test()) { }
    long g = 0;
    for (int f = 0; f < e.length; f++)
      g += e[f];
    c += g;
  }

  void h() {
    for (int f = 0; f < 10000; ++f)
      bMeth();
    System.out.println(c);
  }

  public static void main(String[] i) {
    Test j = new Test();
    j.h();
  }
}
---------- END SOURCE ----------