Will unaligned memory access work on all platforms?
Per-Ake Minborg
per-ake.minborg at oracle.com
Tue Jan 2 09:35:47 UTC 2024
Hi Manuel!
Thanks for your feedback!
In short, var handle operations with normal memory semantics (such as get() and set()) will always work with unaligned access whereas atomic operations, such as compare-and-set, will always fail with an IllegalArgumentException if attempted on a physical memory address that is not properly aligned. MemorySegment get and set operations will just delegate to the var handle operations and consequently, they will always work (as they always use normal memory semantics).
The behavior is the same regardless of whether it is a heap or a native segment and it is not possible to crash the JVM using unaligned access.
I think the documentation can be improved in this area. I will bring it up with the team.
Here is an example of how to reproduce unaligned CAS-operation alignment behavior:
jshell --enable-preview
| Welcome to JShell -- Version 21
| For an introduction type: /help intro
jshell> var segment = java.lang.foreign.Arena.ofAuto().allocate(16)
segment ==> MemorySegment{ heapBase: Optional.empty address:105553116302944 limit: 16 }
jshell> var h = java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED.varHandle();
h ==> VarHandle[varType=int, coord=[interface java.lang.foreign.MemorySegment]]
jshell> h.compareAndSet(segment, 0, 1);
$4 ==> true
jshell> var slice = segment.asSlice(1)
slice ==> MemorySegment{ heapBase: Optional.empty address:105553116302945 limit: 15 }
jshell> h.compareAndSet(slice, 0, 1);
| Exception java.lang.IllegalArgumentException: Misaligned access at address: 105553116302945
| at VarHandleSegmentViewBase.newIllegalArgumentExceptionForMisalignedAccess (VarHandleSegmentViewBase.java:57)
| at VarHandleSegmentAsInts.offset (VarHandleSegmentAsInts.java:89)
| at VarHandleSegmentAsInts.compareAndSet (VarHandleSegmentAsInts.java:192)
| at (#6:1)
Note that in the final API (in Java 22), there is an extra coordinate for an offset value for the VarHandle above.
Best, Per Minborg
________________________________
From: panama-dev <panama-dev-retn at openjdk.org> on behalf of Manuel Bleichenbacher <manuel.bleichenbacher at gmail.com>
Sent: Thursday, December 28, 2023 4:52 PM
To: panama-dev at openjdk.org <panama-dev at openjdk.org>
Subject: Will unaligned memory access work on all platforms?
Hi,
Instances of MemoryLayout support specifying alignment constraints. However, if the alignment is set to 1 byte for all layouts, will unaligned access work on all platforms?
In a short test on a Mac with Apple Silicon, unaligned access works with both native and heap segments, across 8-byte boundaries using MemorySegment.get(), MemorySegment.set(), VarHandle.get() and VarHandle.set(). And the result will likely be the same on x86 machine, independent of the operating system. But is it guaranteed to work on any operating system and any CPU architecture, even on those ones where machine instructions may not read from or write to unaligned addresses?
If it does not work on all platform, what would happen instead? Will it throw an exception? Could it crash the JVM? Is the behavior the same for native and heap segments?
While researching this topic, I kept wondering where to look for documentation about it. The only description I've found is in documentation of the MemorySegment class. It has quite a long chapter about alignment, yet it doesn't really tell why you would want to use alignment constraints in the first place and what happens if an unaligned access is made anyway, with or without alignment constraint on the layout.
Regards
Manuel Bleichenbacher
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240102/249c1fef/attachment.htm>
More information about the panama-dev
mailing list