HotSpot JIT fails to observe side effects of class initialization triggered by 'new' instruction

XMLWordPrintable

      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 functional discrepancy between HotSpot and the JVM Specification regarding class initialization. In the provided reproducer, class 'B' instantiates class 'C' using a static field as a constructor argument.
      According to JVMS, the 'new' instruction for class 'C' must trigger its initialization before the subsequent 'getstatic' instruction loads the argument for the constructor. Class 'C' updates the static field during its initialization. However, HotSpot JIT appears to cache the field's value or reorder the instructions, passing 'null' to the constructor instead of the updated value.
      And you can run javap to check thr order of instructions.


      REGRESSION : Java version that customer using for 8u481, 11.0.30



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



      ---------- BEGIN SOURCE ----------
      class Test {
          public static void main(String[] args) {
              A.B.main();
          }
      }
      class A {
          static class B {
              static String field;

              static void main() {
                  String tmp = field;
                  new C(field);
              }
          }

          static class C {
              static {
                  B.field = "Hello";
              }

              C(String val) {
                  if (val == null) {
                      throw new RuntimeException();
                  }
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY :
      ALWAYS

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

              Created:
              Updated:
              Resolved: