[foreign-memaccess+abi] RFR: 8317514: Ensure MemorySegment is initialized before touching NativeMemorySegmentImpl

Jorn Vernee jvernee at openjdk.org
Mon Oct 9 18:27:04 UTC 2023


Please review this fix for a class initializer deadlock.

The issue is that `MemorySegment` references `NativeMemorySegmentImpl` in its initializer, through the `NULL` constant. `NativeMemorySegmentImpl` also depends on `MemorySegment` to be initialized, since it's a super interface with default methods. The VM does not drop the initialization monitor of `NativeMemorySegmentImpl` while trying to initialize super classes/interfaces (as mandated by the JVM spec).

When 2 threads try to intialize `MemorySegment` (e.g. through `ofAddress`) and `NativeMemorySegmentImpl` (e.g. through Arena::allocateFrom), at the same time, a deadlock can occur.

This PR fixes this issue by ensuring `MemorySegment` is initialized in all cases where we directly access `NativeMemorySegmentImpl` or `MappedMemorySegmentImpl`.

We might consider a fix that removes the cycle in the future as well.

-------------

Commit messages:
 - add test
 - Polish
 - ensure MS init
 - Ensure init in ArenaImpl

Changes: https://git.openjdk.org/panama-foreign/pull/902/files
 Webrev: https://webrevs.openjdk.org/?repo=panama-foreign&pr=902&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8317514
  Stats: 91 lines in 4 files changed: 91 ins; 0 del; 0 mod
  Patch: https://git.openjdk.org/panama-foreign/pull/902.diff
  Fetch: git fetch https://git.openjdk.org/panama-foreign.git pull/902/head:pull/902

PR: https://git.openjdk.org/panama-foreign/pull/902


More information about the panama-dev mailing list