System.loadLibrary and CLinker.lookup exception

Jack Andrews effbiae at gmail.com
Wed Apr 27 04:26:38 UTC 2022


On Wed, 27 Apr 2022 at 03:00, Ty Young <youngty1997 at gmail.com> wrote:

> Getting a symbol from a specific library has been possible for over a
> month in the preview branch:
>
>
> https://github.com/openjdk/panama-foreign/blob/foreign-preview/src/java.base/share/classes/java/lang/foreign/SymbolLookup.java#L126


guys, someone has just done a copy+paste for these related (practically
identical)  functions.

@CallerSensitive
static SymbolLookup libraryLookup(String name, MemorySession session) {
Reflection.ensureNativeAccess(Reflection.getCallerClass(),
SymbolLookup.class, "libraryLookup");
Objects.requireNonNull(name);
Objects.requireNonNull(session);
RawNativeLibraries nativeLibraries =
RawNativeLibraries.newInstance(MethodHandles.lookup());
NativeLibrary library = nativeLibraries.load(name);
if (library == null) {
throw new IllegalArgumentException("Cannot open library: " + name);
}
return libraryLookup(nativeLibraries, library, session);
}
..
@CallerSensitive
static SymbolLookup libraryLookup(Path path, MemorySession session) {
Reflection.ensureNativeAccess(Reflection.getCallerClass(),
SymbolLookup.class, "libraryLookup");
Objects.requireNonNull(path);
Objects.requireNonNull(session);
RawNativeLibraries nativeLibraries =
RawNativeLibraries.newInstance(MethodHandles.lookup());
NativeLibrary library = nativeLibraries.load(path);
if (library == null) {
throw new IllegalArgumentException("Cannot open library: " + path);
}
return libraryLookup(nativeLibraries, library, session);
}

this really needs to be replaced with a private method:

static private SymbolLookup libraryLookupPathOrString(Object pathOrString,
MemorySession session) {
  Reflection.ensureNativeAccess(Reflection.getCallerClass(),
SymbolLookup.class, "libraryLookup");
  Objects.requireNonNull(pathOrString);
  Objects.requireNonNull(session);
  RawNativeLibraries nativeLibraries =
RawNativeLibraries.newInstance(MethodHandles.lookup());
  NativeLibrary library = nativeLibraries.load(pathOrString);
  if (library == null) {
    throw new IllegalArgumentException("Cannot open library: " + path);
  }
  return libraryLookup(nativeLibraries, library, session);
}

and shrink the two methods to:

static SymbolLookup libraryLookupPathOrString(String name, MemorySession
session) {
  return libraryLookupPathOrString(name,session)
}
static SymbolLookup libraryLookupPathOrString(Path path, MemorySession
session) {
  return libraryLookupPathOrString(path,session)
}

my java is rusty, but you get the idea.
best,
jack


More information about the panama-dev mailing list