VarHandle with PathElement
Manuel Bleichenbacher
manuel.bleichenbacher at gmail.com
Sun Feb 18 08:24:33 UTC 2024
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/966a279a/attachment.htm>
More information about the panama-dev
mailing list