System.loadLibrary and CLinker.lookup exception

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Apr 27 08:40:44 UTC 2022


Hi Rado,
I've touched on this in my previous email.

To summarize - in the preview API we have:

SymbolLookup::loaderLookup
SymbolLookup::libraryLookup

and then we have CLinker implements SymbolLookup (to provide "system" 
lookup).

Given the recent API renaming (e.g. CLinker -> Linker, and 
CLinker::systemCLinker --> Linker::nativeLinker), I think a good move 
could be to consolidate the system lookup under SymbolLookup as:

SymbolLookup::nativeLookup

(since we're using the term "native" to denote all things that are 
"special" to the platform in which the Java runtime is running).

Maurizio

On 26/04/2022 16:02, Radosław Smogura wrote:
>
> 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://urldefense.com/v3/__https://go.microsoft.com/fwlink/?LinkId=550986__;!!ACWV5N9M2RV99hQ!OIm4FaFh0FmzWbjJF1kbrLkF7yoSJ2PLVQiBohLY5UrrgUUSrXK7nLh-USPXeXbgHC5fKFFzSXYzL8HVfMGBWQ$> 
> 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> 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
>
> hasCLinker.systemCLinker().lookup() been deprecated?
> if so,
> https://github.com/openjdk/panama-foreign/blob/foreign-jextract/doc/panama_ffi.md 
> <https://urldefense.com/v3/__https://github.com/openjdk/panama-foreign/blob/foreign-jextract/doc/panama_ffi.md__;!!ACWV5N9M2RV99hQ!OIm4FaFh0FmzWbjJF1kbrLkF7yoSJ2PLVQiBohLY5UrrgUUSrXK7nLh-USPXeXbgHC5fKFFzSXYzL8Fgli0nOg$>
> 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