System.loadLibrary and CLinker.lookup exception
Radosław Smogura
mail at smogura.eu
Tue Apr 26 15:02:04 UTC 2022
Hi all,
I wanted to touch this topic before, because I got catch in this trap, too.
I wonder if there’s a way to slightly refactor API to make it more “browsable” – I mean that potential user of API would see that there are two different lookups – system one, and loader one.
One thing which came from top of my head is to separate SymbolLookup from Clinker, in particularly:
Make SymbolLookup having following static methods: loaderLookup and systemLookup and instance - lookup.
Narrow Clinker responsibility only to create up & down call stubs based on lookups from particular “version” of SymbolLookup.
Kind regards,
Radoslaw Smogura
Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows
________________________________
From: Jack Andrews <effbiae at gmail.com>
Sent: Tuesday, April 26, 2022 1:23:25 PM
To: Radosław Smogura <mail at smogura.eu>
Cc: panama-dev at openjdk.java.net <panama-dev at openjdk.java.net>
Subject: Re: System.loadLibrary and CLinker.lookup exception
On Tue, 26 Apr 2022 at 20:51, Radosław Smogura <mail at smogura.eu<mailto:mail at smogura.eu>> wrote:
Hi Jack,
Can you try SymbolLookup (Java SE 18 & JDK 18) (oracle.com)<https://docs.oracle.com/en/java/javase/18/docs/api/jdk.incubator.foreign/jdk/incubator/foreign/SymbolLookup.html#loaderLookup()>
that works: SymbolLookup.loaderLookup() lets me find kinit
has CLinker.systemCLinker().lookup() been deprecated?
if so,
https://github.com/openjdk/panama-foreign/blob/foreign-jextract/doc/panama_ffi.md
needs updating
$ cat Example.java
import jdk.incubator.foreign.*;
import java.lang.invoke.*;
import static jdk.incubator.foreign.ValueLayout.*;
public class Example {
static CLinker LINKER = CLinker.systemCLinker();
public static void main(String[] args) throws Throwable {
System.loadLibrary("k");
SymbolLookup L = SymbolLookup.loaderLookup();
NativeSymbol S = L.lookup("kinit").get();
System.out.println(S);
MethodHandle kinit = LINKER.downcallHandle(
L.lookup("kinit").get(),
FunctionDescriptor.of(JAVA_INT)
);
int r = (int) kinit.invoke();
System.out.println(r);
/*20*/ NativeSymbol T = LINKER.lookup("kinit").get();
System.out.println(T);
}
}
$ LD_LIBRARY_PATH=. java --add-modules jdk.incubator.foreign --enable-native-access=ALL-UNNAMED Example.java
WARNING: Using incubator modules: jdk.incubator.foreign
warning: using incubating module(s): jdk.incubator.foreign
1 warning
NativeSymbolImpl[name=kinit, address=MemoryAddress{ offset=0x7fad2ac4a6f0 }, scope=jdk.internal.foreign.ResourceScopeImpl$GlobalScopeImpl at 3b2c72c2]
717736192
Exception in thread "main" java.util.NoSuchElementException: No value present
at java.base/java.util.Optional.get(Optional.java:143)
at Example.main(Example.java:20)
More information about the panama-dev
mailing list