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

Segmentation fault in JDK 8/11 when using Unsafe.allocateInstance() in loops

XMLWordPrintable

    • x86_64
    • linux_ubuntu

      ADDITIONAL SYSTEM INFORMATION :
      Additional information:

      wsl2-ubuntu 22.04
      Linux 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

      java version "1.8.0_451"
      Java(TM) SE Runtime Environment (build 1.8.0_451-b10)
      Java HotSpot(TM) 64-Bit Server VM (build 25.451-b10, mixed mode)

      java version "11.0.27" 2025-04-15 LTS
      Java(TM) SE Runtime Environment 18.9 (build 11.0.27+8-LTS-232)
      Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.27+8-LTS-232, mixed mode)


      A DESCRIPTION OF THE PROBLEM :
      The following test program will cause Segmentation fault in JDK 8 and 11. However, it executes successfully in JDK 17 and 21.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Using JDK 8 or 11, run the following command:
      ```
      javac Test.java
      java Test
      ```

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program should execute successfully.
      ACTUAL -
      The program will cause Segmentation fault.
      ```
      Segmentation fault (core dumped)
      ```

      ---------- BEGIN SOURCE ----------
      class Test {
          void test() {
              java.lang.reflect.Field field = null;
              try {
                  java.lang.Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
                  java.lang.reflect.Field[] fields = sun.misc.Unsafe.class.getDeclaredFields();
                  for (java.lang.reflect.Field f : fields) {
                      if (f.getType() == unsafeClass) {
                          f.setAccessible(true);
                          field = f;
                          break;
                      }
                  }
                  if (field != null) {
                      sun.misc.Unsafe unsafe = (sun.misc.Unsafe) field.get(null);
                      for (int j = 0; j < 100; j++) {
                          unsafe.allocateInstance(int[].class);
                      }
                  }
              } catch (Exception e) {
              }
          }

          public static void main(String[] args) {
              Test t = new Test();
              for (int i = 0; i < 100_000; ++i) {
                  t.test();
              }
          }
      }

      ---------- END SOURCE ----------

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

              Created:
              Updated: