<div dir="ltr">Thanks for the response. I've missed that. <div><br></div><div>It works now.</div><font color="#888888"><div><br></div><div>-- Manuel</div></font></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Am So., 18. Feb. 2024 um 12:20 Uhr schrieb Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com">maurizio.cimadamore@oracle.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>
<div>
<p>Hi Manuel,<br>
all var handles created from path elements in Java 22 accept an
additional "long" offset parameter, which can be used in case
where the var handle access must be composed with some other var
handle/method handle. See:<br>
<br>
<a href="https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/foreign/MemoryLayout.html#variable-length" target="_blank">https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/foreign/MemoryLayout.html#variable-length</a><br>
</p>
<p>It seem like we have missed the update on the javadoc for
Linker.Option.captureCallState unfortunately.</p>
<p>Maurizio<br>
</p>
<div>On 18/02/2024 08:24, Manuel
Bleichenbacher wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">In the upgrade of code from JDK 21 to JDK 22, I've
run into a problem with VarHandle instances built using
PathElement. Something must have changed, which I'm not aware
of, or I must have been using them incorrectly all along, or
there is a bug.
<div><br>
</div>
<div>The VarHandle instances can be created but when they are
used to access data an exception like this one is thrown:</div>
<div><br>
</div>
<div>Exception in thread "main"
java.lang.invoke.WrongMethodTypeException: cannot convert
MethodHandle(VarHandle,MemorySegment,long)int to
(VarHandle,MemorySegment)int<br>
at
java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:903)<br>
at
java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:870)<br>
at Main.main(Main.java:16)<br>
<br>
The code to reproduce the error can be found below. It crashes
on "errnoHandle.get(...)".</div>
<div><br>
</div>
<div>The code is mostly form the documentation of
Linker.Option.captureCallState() (see <a href="https://cr.openjdk.org/~mcimadamore/jdk/FFM_22_PR/javadoc/java.base/java/lang/foreign/Linker.Option.html#captureCallState(java.lang.String...)" target="_blank">https://cr.openjdk.org/~mcimadamore/jdk/FFM_22_PR/javadoc/java.base/java/lang/foreign/Linker.Option.html#captureCallState(java.lang.String...)</a>)
But it doesn't just affect call state. It affects all kinds of
VarHandle instances built with PathElement.</div>
<div><br>
</div>
<div>The bug occurred with the OpenJDK build 29, both on macOS
and Linux. </div>
<div><br>
</div>
<div>Any help is appreciated.</div>
<div><br>
</div>
<div>Regards</div>
<div>Manuel</div>
<div><br>
</div>
<div><br>
</div>
<div>import java.lang.foreign.*;<br>
import static java.lang.foreign.ValueLayout.JAVA_INT;<br>
<br>
public class Main {<br>
public static void main(String[] args) throws Throwable {<br>
var linker = Linker.nativeLinker();<br>
var descriptor = FunctionDescriptor.of(JAVA_INT,
JAVA_INT);<br>
var ccs = Linker.Option.captureCallState("errno");<br>
var handle =
linker.downcallHandle(linker.defaultLookup().find("close").get(),
descriptor, ccs);<br>
var capturedStateLayout =
Linker.Option.captureStateLayout();<br>
var errnoHandle =
capturedStateLayout.varHandle(MemoryLayout.PathElement.groupElement("errno"));<br>
<br>
try (Arena arena = Arena.ofConfined()) {<br>
var capturedState =
arena.allocate(capturedStateLayout);<br>
var res = (int) handle.invokeExact(capturedState,
100);<br>
int errno = (int) errnoHandle.get(capturedState);<br>
System.out.println("errno = " + errno);<br>
}<br>
}<br>
}<br>
</div>
<div><br>
</div>
<div><br>
</div>
</div>
</blockquote>
</div>
</blockquote></div>