Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8352619

Inconsistent Behavior of "intern" Across HotSpot Versions and JVM Implementation

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      macOS version:

      ```java
      Software:

          System Software Overview:

            System Version: macOS 13.2 (22D49)
            Kernel Version: Darwin 22.3.0
            Boot Volume: Macintosh HD
            Boot Mode: Normal
            Computer Name: MacBook Pro
            User Name: MacBook
            Secure Virtual Memory: Enabled
            System Integrity Protection: Enabled
            Time since boot: 1 day, 2 hours, 19 minutes

      Hardware:

          Hardware Overview:

            Model Name: MacBook Pro
            Model Identifier: MacBookPro18,1
            Model Number: MK193CH/A
            Chip: Apple M1 Pro
            Total Number of Cores: 10 (8 performance and 2 efficiency)
            Memory: 16 GB
            System Firmware Version: 8419.80.7
            OS Loader Version: 8419.80.7
            Activation Lock Status: Disabled
      ```

      OpenJDK version:

      ```jsx
      HotSpot:
      JDK 8:
      openjdk version "1.8.0_442"
      OpenJDK Runtime Environment (Temurin)(build 1.8.0_442-b06)
      OpenJDK 64-Bit Server VM (Temurin)(build 25.442-b06, mixed mode)

      JDK 11:
      openjdk version "11.0.26" 2025-01-21
      OpenJDK Runtime Environment Temurin-11.0.26+4 (build 11.0.26+4)
      OpenJDK 64-Bit Server VM Temurin-11.0.26+4 (build 11.0.26+4, mixed mode)

      JDK 17:
      openjdk version "17.0.14" 2025-01-21
      OpenJDK Runtime Environment Temurin-17.0.14+7 (build 17.0.14+7)
      OpenJDK 64-Bit Server VM Temurin-17.0.14+7 (build 17.0.14+7, mixed mode)

      JDK 21:
      openjdk version "21.0.6" 2025-01-21 LTS
      OpenJDK Runtime Environment Temurin-21.0.6+7 (build 21.0.6+7-LTS)
      OpenJDK 64-Bit Server VM Temurin-21.0.6+7 (build 21.0.6+7-LTS, mixed mode)

      OpenJ9:
      JDK 8:
      openjdk version "1.8.0_442"
      IBM Semeru Runtime Open Edition (build 1.8.0_442-b06)
      Eclipse OpenJ9 VM (build openj9-0.49.0, JRE 1.8.0 Mac OS X amd64-64-Bit Compressed References 20250205_1183 (JIT enabled, AOT enabled)
      OpenJ9 - 3c3d179854
      OMR - e49875871
      JCL - 61f83383b8 based on jdk8u442-b06)

      JDK 11:
      openjdk version "11.0.26" 2025-01-21
      IBM Semeru Runtime Open Edition 11.0.26.0 (build 11.0.26+4)
      Eclipse OpenJ9 VM 11.0.26.0 (build openj9-0.49.0, JRE 11 Mac OS X aarch64-64-Bit 20250205_839 (JIT enabled, AOT enabled)
      OpenJ9 - 3c3d179854
      OMR - e49875871
      JCL - 674ad23a80 based on jdk-11.0.26+4)

      JDK 17:
      openjdk version "17.0.14" 2025-01-21
      IBM Semeru Runtime Open Edition 17.0.14.0 (build 17.0.14+7)
      Eclipse OpenJ9 VM 17.0.14.0 (build openj9-0.49.0, JRE 17 Mac OS X aarch64-64-Bit 20250121_796 (JIT enabled, AOT enabled)
      OpenJ9 - 3c3d179854
      OMR - e49875871
      JCL - cbbc8b94a62 based on jdk-17.0.14+7)

      JDK 21:
      openjdk version "21.0.6" 2025-01-21 LTS
      IBM Semeru Runtime Open Edition 21.0.6.0 (build 21.0.6+7-LTS)
      Eclipse OpenJ9 VM 21.0.6.0 (build openj9-0.49.0, JRE 21 Mac OS X aarch64-64-Bit 20250121_371 (JIT enabled, AOT enabled)
      OpenJ9 - 3c3d179854
      OMR - e49875871
      JCL - e01368f00df based on jdk-21.0.6+7)

      GraalVM:
      JDK 17:
      java version "17.0.14" 2025-01-21 LTS
      Java(TM) SE Runtime Environment Oracle GraalVM 17.0.14+8.1 (build 17.0.14+8-LTS-jvmci-23.0-b54)
      Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 17.0.14+8.1 (build 17.0.14+8-LTS-jvmci-23.0-b54, mixed mode, sharing)

      JDK 21:
      java version "21.0.6" 2025-01-21 LTS
      Java(TM) SE Runtime Environment Oracle GraalVM 21.0.6+8.1 (build 21.0.6+8-LTS-jvmci-23.1-b55)
      Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 21.0.6+8.1 (build 21.0.6+8-LTS-jvmci-23.1-b55, mixed mode, sharing)

      JDK 24:
      java version "24" 2025-03-18
      Java(TM) SE Runtime Environment Oracle GraalVM 24+36.1 (build 24+36-jvmci-b01)
      Java HotSpot(TM) 64-Bit Server VM Oracle GraalVM 24+36.1 (build 24+36-jvmci-b01, mixed mode, sharing)
      ```

      A DESCRIPTION OF THE PROBLEM :
      Hi, we have encountered a very strange behavior in a simple test program, where the execution results vary across different versions of HotSpot, and between HotSpot and other JVM implementations (OpenJ9 and GraalVM).

      ### **1、Inconsistency Across HotSpot Versions**

      Consider the following test program:

      ```
      public class Test {
      public static void main(String[] args) {
      String s1 = String.valueOf(4);
      String s2 = s1.intern();
      String s3 = "4";

      System.out.println(s1 == s2);
      System.out.println(s2 == s3);
      System.out.println(s1 == s3);
      }
      }
      ```

      The results vary across different JDK versions:

      ```
      JDK 8: true true true
      JDK 11: false true false
      JDK 17: true true true
      JDK 21: true true true
      ```

      Clearly, **JDK 11 exhibits behavior inconsistent with other HotSpot versions**, suggesting a potential issue in HotSpot of JDK 11.

      Interestingly, if we change the value `"4"` to **any other number** (e.g., `"1"`, `"2"`, `"3"`, `"5"`, `"6"`, `"7"`, etc.), all JDK versions produce the same expected output:

      ```
      public class Test {
      public static void main(String[] args) {
      String s1 = String.valueOf(5);
      String s2 = s1.intern();
      String s3 = "5";

      System.out.println(s1 == s2);
      System.out.println(s2 == s3);
      System.out.println(s1 == s3);
      }
      }
      //Output: true true true
      ```

      Observed execution results:

      ```
      JDK 8: true true true
      JDK 11: true true true
      JDK 17: true true true
      JDK 21: true true true
      ```

      This strongly suggests that **JDK 11's inconsistent behavior only occurs when the value is `"4"`, making it a very peculiar anomaly**.

      ### **2、Inconsistency Across Different JVM Implementations**

      Additionally, the behavior of HotSpot differs from other JVM implementations such as **OpenJ9** and **GraalVM**. For these alternative JVMs, regardless of which number is used in the test program, the output is **always**:

      ```
      OpenJ9:
      JDK 8: false true false
      JDK 11: false true false
      JDK 17: false true false
      JDK 21: false true false

      GraalVM:
      JDK 17: false true false
      JDK 21: false true false
      JDK 24: false true false
      ```

      This highlights a clear discrepancy **between HotSpot and other JVM implementations**. Given how simple this test program is, the observed inconsistencies are highly unusual.

      However, we are unable to determine the root cause of this behavior and would appreciate any insights you can provide.

      Thank you!

      ### Observed Behavior:

      1. **Inconsistency across HotSpot versions:**
      - When the value is `"4"`, HotSpot exhibits inconsistent behavior across JDK versions:

      ```
      JDK 8: true true true
      JDK 11: false true false
      JDK 17: true true true
      JDK 21: true true true
      ```

      1. **Inconsistency across JVM implementations:**
      - When using values **other than `"4"`**, different JVM implementations still produce inconsistent results:

      ```
      HotSpot:
      JDK 8: true true true
      JDK 11: false true false
      JDK 17: true true true
      JDK 21: true true true

      OpenJ9:
      JDK 8: false true false
      JDK 11: false true false
      JDK 17: false true false
      JDK 21: false true false

      GraalVM:
      JDK 17: false true false
      JDK 21: false true false
      JDK 24: false true false
      ```

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - Compile the `Test.java` file using `javac`.
      - Run the program on different JDK versions and JVM implementations.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      All HotSpot versions and all JVM implementations should produce consistent results.
      ACTUAL -
      - **HotSpot exhibits inconsistent behavior across different JDK versions**, with JDK 11 producing an unexpected result when the value is `"4"`.

      - **Different JVM implementations (HotSpot, OpenJ9, and GraalVM) produce different results**, with OpenJ9 and GraalVM consistently returning `false true false`, whereas HotSpot exhibits variability.

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: