VarHandle with PathElement

Manuel Bleichenbacher manuel.bleichenbacher at gmail.com
Sun Feb 18 13:02:25 UTC 2024


Thanks for the response. I've missed that.

It works now.

-- Manuel

Am So., 18. Feb. 2024 um 12:20 Uhr schrieb Maurizio Cimadamore <
maurizio.cimadamore at oracle.com>:

> Hi Manuel,
> 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:
>
>
> https://download.java.net/java/early_access/jdk22/docs/api/java.base/java/lang/foreign/MemoryLayout.html#variable-length
>
> It seem like we have missed the update on the javadoc for
> Linker.Option.captureCallState unfortunately.
>
> Maurizio
> On 18/02/2024 08:24, Manuel Bleichenbacher wrote:
>
> 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.
>
> The VarHandle instances can be created but when they are used to access
> data an exception like this one is thrown:
>
> Exception in thread "main" java.lang.invoke.WrongMethodTypeException:
> cannot convert MethodHandle(VarHandle,MemorySegment,long)int to
> (VarHandle,MemorySegment)int
> at
> java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:903)
> at java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:870)
> at Main.main(Main.java:16)
>
> The code to reproduce the error can be found below. It crashes on
> "errnoHandle.get(...)".
>
> The code is mostly form the documentation of
> Linker.Option.captureCallState() (see
> https://cr.openjdk.org/~mcimadamore/jdk/FFM_22_PR/javadoc/java.base/java/lang/foreign/Linker.Option.html#captureCallState(java.lang.String...))
> But it doesn't just affect call state. It affects all kinds of VarHandle
> instances built with PathElement.
>
> The bug occurred with the OpenJDK build 29, both on macOS and Linux.
>
> Any help is appreciated.
>
> Regards
> Manuel
>
>
> import java.lang.foreign.*;
> import static java.lang.foreign.ValueLayout.JAVA_INT;
>
> public class Main {
>     public static void main(String[] args) throws Throwable {
>         var linker = Linker.nativeLinker();
>         var descriptor = FunctionDescriptor.of(JAVA_INT, JAVA_INT);
>         var ccs = Linker.Option.captureCallState("errno");
>         var handle =
> linker.downcallHandle(linker.defaultLookup().find("close").get(),
> descriptor, ccs);
>         var capturedStateLayout = Linker.Option.captureStateLayout();
>         var errnoHandle =
> capturedStateLayout.varHandle(MemoryLayout.PathElement.groupElement("errno"));
>
>         try (Arena arena = Arena.ofConfined()) {
>             var capturedState = arena.allocate(capturedStateLayout);
>             var res = (int) handle.invokeExact(capturedState, 100);
>             int errno = (int) errnoHandle.get(capturedState);
>             System.out.println("errno = " + errno);
>         }
>     }
> }
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/panama-dev/attachments/20240218/d2e08236/attachment.htm>


More information about the panama-dev mailing list