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

Closing the original Arena clears reinterpreted MemorySegments

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Linux <computer-name> 6.6.87.2-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Thu Jun 5 18:30:46 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

      openjdk version "17.0.15" 2025-04-15
      OpenJDK Runtime Environment (build 17.0.15+6-Ubuntu-0ubuntu124.04)
      OpenJDK 64-Bit Server VM (build 17.0.15+6-Ubuntu-0ubuntu124.04, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      If I create a MemorySegment using one Arena, then use MemorySegment.reinterpret to attach it to another Arena, the contents are valid only until the first Arena is closed. Afterwards, the contents of the reinterpreted MemorySegment are empty (all zeroes).

      I have tested this with both Java 24 and Java 25, the issue exists on both. I have tried the same on Windows and there everything works as expected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the given test case code on both Linux and Windows.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Original: foo
      Reinterpreted before close: foo
      Reinterpreted after close: foo
      0: 102
      1: 111
      2: 111
      3: 0
      MemorySegment{ address: 0x<something>, byteSize: 4 }

      This is the output produced on Windows.
      ACTUAL -
      Original: foo
      Reinterpreted before close: foo
      Reinterpreted after close:
      0: 0
      1: 0
      2: 0
      3: 0
      MemorySegment{ kind: native, address: 0x<something>, byteSize: 4 }

      ---------- BEGIN SOURCE ----------
      try (Arena reinterpretTarget = Arena.ofConfined()) {
          MemorySegment reinterpreted;

          try (Arena arena = Arena.ofConfined()) {
              MemorySegment segment = arena.allocateFrom("foo");

              reinterpreted = segment.reinterpret(reinterpretTarget, System.out::println);

              System.out.printf("Original: %s%n", reinterpreted.getString(0));
              System.out.printf("Reinterpreted before close: %s%n", reinterpreted.getString(0));
          }

          System.out.printf("Reinterpreted after close: %s%n", reinterpreted.getString(0));
          for (int i = 0; i < reinterpreted.byteSize(); i++) {
              System.out.printf("%d: %d%n", i, reinterpreted.get(ValueLayout.JAVA_BYTE, i));
          }
      }

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

        1. Test.java
          1.0 kB
          Anjana Sajeev

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

              Created:
              Updated:
              Resolved: