<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-size: 12pt; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; margin: 0px; color: rgb(0, 0, 0);">
Hi Manuel!<br>
<br>
Thanks for your feedback!</div>
<div style="font-size: 12pt; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; margin: 0px; color: rgb(0, 0, 0);">
<br>
</div>
<div class="elementToProof"><span style="font-size: 12pt; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; margin: 0px; color: rgb(0, 0, 0);">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).<br>
<br>
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.</span></div>
<div class="elementToProof"><span style="font-size: 12pt; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; margin: 0px; color: rgb(0, 0, 0);"><br>
</span></div>
<div class="elementToProof"><span style="font-size: 12pt; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; margin: 0px; color: rgb(0, 0, 0);">I think the documentation can be improved in this area. I will bring it
 up with the team.<br>
<br>
Here is an example of how to reproduce unaligned CAS-operation alignment behavior:</span></div>
<div class="elementToProof"><span style="font-size: 12pt; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; margin: 0px; color: rgb(0, 0, 0);"><br>
</span></div>
<div class="elementToProof"><span style="font-size: 12pt; font-family: Aptos, Aptos_EmbeddedFont, Aptos_MSFontService, Calibri, Helvetica, sans-serif; margin: 0px; color: rgb(0, 0, 0);" class="ContentPasted0">jshell --enable-preview
<div class="ContentPasted0">|  Welcome to JShell -- Version 21</div>
<div class="ContentPasted0">|  For an introduction type: /help intro</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0"><br>
</div>
<div class="ContentPasted0">jshell> var segment = java.lang.foreign.Arena.ofAuto().allocate(16)</div>
<div class="ContentPasted0">segment ==> MemorySegment{ heapBase: Optional.empty address:105553116302944 limit: 16 }</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0"><span style="">jshell> var h = java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED.varHandle();</span><br>
</div>
<div class="ContentPasted0">h ==> VarHandle[varType=int, coord=[interface java.lang.foreign.MemorySegment]]</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0"><span style="">jshell> h.compareAndSet(segment, 0, 1);</span><br>
</div>
<div class="ContentPasted0">$4 ==> true</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0"><span style="">jshell> var slice = segment.asSlice(1)</span><br>
</div>
<div class="ContentPasted0">slice ==> MemorySegment{ heapBase: Optional.empty address:105553116302945 limit: 15 }</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0">jshell> h.compareAndSet(slice, 0, 1);</div>
<div class="ContentPasted0">|  Exception java.lang.IllegalArgumentException: Misaligned access at address: 105553116302945</div>
<div class="ContentPasted0">|        at VarHandleSegmentViewBase.newIllegalArgumentExceptionForMisalignedAccess (VarHandleSegmentViewBase.java:57)</div>
<div class="ContentPasted0">|        at VarHandleSegmentAsInts.offset (VarHandleSegmentAsInts.java:89)</div>
<div class="ContentPasted0">|        at VarHandleSegmentAsInts.compareAndSet (VarHandleSegmentAsInts.java:192)</div>
<div class="ContentPasted0">|        at (#6:1)</div>
<div><br class="ContentPasted0">
</div>
<div class="ContentPasted0">Note that in the final API (in Java 22), there is an extra coordinate for an offset value for the VarHandle above.<br>
<br>
Best, Per Minborg</div>
</span></div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> panama-dev <panama-dev-retn@openjdk.org> on behalf of Manuel Bleichenbacher <manuel.bleichenbacher@gmail.com><br>
<b>Sent:</b> Thursday, December 28, 2023 4:52 PM<br>
<b>To:</b> panama-dev@openjdk.org <panama-dev@openjdk.org><br>
<b>Subject:</b> Will unaligned memory access work on all platforms?</font>
<div> </div>
</div>
<div>
<div dir="ltr">Hi,
<div><br>
</div>
<div>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?</div>
<div><br>
</div>
<div>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?</div>
<div><br>
</div>
<div>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?</div>
<div><br>
</div>
<div>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.</div>
<div><br>
</div>
<div>Regards</div>
<div>Manuel Bleichenbacher</div>
<div><br>
</div>
</div>
</div>
</body>
</html>