<div dir="ltr">Thanks for the clarification.<div><br></div><div>It would certainly make sense to mention it in the documentation as well.</div><div><br></div><div>I did an experiment with CAS similar to the one in your answer as I had seen the restriction mentioned in the MemorySegment documentation. Strangely enough, I couldn't find any mention in the VarHandle documentation. Possibly, it's documented somewhere else. It seems additional restrictions apply to CAS, e.g. it's not supported for "short" (2 byte) access and likely several other value types. But that's not really relevant for my work...</div><div><br></div><div>Regards</div><div>Manuel</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am Di., 2. Jan. 2024 um 10:35 Uhr schrieb Per-Ake Minborg <<a href="mailto:per-ake.minborg@oracle.com">per-ake.minborg@oracle.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div class="msg-7045143613683573561">
<div 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><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><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><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><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><span style="font-size:12pt;font-family:Aptos,Aptos_EmbeddedFont,Aptos_MSFontService,Calibri,Helvetica,sans-serif;margin:0px;color:rgb(0,0,0)">jshell --enable-preview
<div>| Welcome to JShell -- Version 21</div>
<div>| For an introduction type: /help intro</div>
<div><br>
</div>
<div><br>
</div>
<div>jshell> var segment = java.lang.foreign.Arena.ofAuto().allocate(16)</div>
<div>segment ==> MemorySegment{ heapBase: Optional.empty address:105553116302944 limit: 16 }</div>
<div><br>
</div>
<div><span>jshell> var h = java.lang.foreign.ValueLayout.JAVA_INT_UNALIGNED.varHandle();</span><br>
</div>
<div>h ==> VarHandle[varType=int, coord=[interface java.lang.foreign.MemorySegment]]</div>
<div><br>
</div>
<div><span>jshell> h.compareAndSet(segment, 0, 1);</span><br>
</div>
<div>$4 ==> true</div>
<div><br>
</div>
<div><span>jshell> var slice = segment.asSlice(1)</span><br>
</div>
<div>slice ==> MemorySegment{ heapBase: Optional.empty address:105553116302945 limit: 15 }</div>
<div><br>
</div>
<div>jshell> h.compareAndSet(slice, 0, 1);</div>
<div>| Exception java.lang.IllegalArgumentException: Misaligned access at address: 105553116302945</div>
<div>| at VarHandleSegmentViewBase.newIllegalArgumentExceptionForMisalignedAccess (VarHandleSegmentViewBase.java:57)</div>
<div>| at VarHandleSegmentAsInts.offset (VarHandleSegmentAsInts.java:89)</div>
<div>| at VarHandleSegmentAsInts.compareAndSet (VarHandleSegmentAsInts.java:192)</div>
<div>| at (#6:1)</div>
<div><br>
</div>
<div>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="m_-7045143613683573561appendonsend"></div>
<hr style="display:inline-block;width:98%">
<div id="m_-7045143613683573561divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> panama-dev <<a href="mailto:panama-dev-retn@openjdk.org" target="_blank">panama-dev-retn@openjdk.org</a>> on behalf of Manuel Bleichenbacher <<a href="mailto:manuel.bleichenbacher@gmail.com" target="_blank">manuel.bleichenbacher@gmail.com</a>><br>
<b>Sent:</b> Thursday, December 28, 2023 4:52 PM<br>
<b>To:</b> <a href="mailto:panama-dev@openjdk.org" target="_blank">panama-dev@openjdk.org</a> <<a href="mailto:panama-dev@openjdk.org" target="_blank">panama-dev@openjdk.org</a>><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>
</div>
</div></blockquote></div>