8282608: RawNativeLibraryImpl can't be passed to NativeLibraries::findEntry0

Reviewed-by: mcimadamore, dholmes
This commit is contained in:
Mandy Chung 2022-03-04 03:41:41 +00:00
parent 8478173d83
commit 7e1c67d4af
4 changed files with 18 additions and 27 deletions

View file

@ -281,7 +281,7 @@ public final class NativeLibraries {
* the VM when it loads the library, and used by the VM to pass the correct * the VM when it loads the library, and used by the VM to pass the correct
* version of JNI to the native methods. * version of JNI to the native methods.
*/ */
static class NativeLibraryImpl implements NativeLibrary { static class NativeLibraryImpl extends NativeLibrary {
// the class from which the library is loaded, also indicates // the class from which the library is loaded, also indicates
// the loader this native library belongs. // the loader this native library belongs.
final Class<?> fromClass; final Class<?> fromClass;
@ -309,7 +309,7 @@ public final class NativeLibraries {
@Override @Override
public long find(String name) { public long find(String name) {
return findEntry0(this, name); return findEntry0(handle, name);
} }
/* /*
@ -534,10 +534,4 @@ public final class NativeLibraries {
*/ */
private static native void unload(String name, boolean isBuiltin, long handle); private static native void unload(String name, boolean isBuiltin, long handle);
private static native String findBuiltinLib(String name); private static native String findBuiltinLib(String name);
/*
* Returns the address of the named symbol defined in the given library.
* Returns 0 if not found.
*/
static native long findEntry0(NativeLibrary lib, String name);
} }

View file

@ -28,8 +28,8 @@ package jdk.internal.loader;
/** /**
* NativeLibrary represents a loaded native library instance. * NativeLibrary represents a loaded native library instance.
*/ */
public interface NativeLibrary { public abstract class NativeLibrary {
String name(); public abstract String name();
/** /**
* Finds the address of the entry of the given name. Returns 0 * Finds the address of the entry of the given name. Returns 0
@ -37,7 +37,7 @@ public interface NativeLibrary {
* *
* @param name the name of the symbol to be found * @param name the name of the symbol to be found
*/ */
long find(String name); public abstract long find(String name);
/** /**
* Finds the address of the entry of the given name. * Finds the address of the entry of the given name.
@ -45,11 +45,17 @@ public interface NativeLibrary {
* @param name the name of the symbol to be found * @param name the name of the symbol to be found
* @throws NoSuchMethodException if the named entry is not found. * @throws NoSuchMethodException if the named entry is not found.
*/ */
default long lookup(String name) throws NoSuchMethodException { public final long lookup(String name) throws NoSuchMethodException {
long addr = find(name); long addr = find(name);
if (0 == addr) { if (0 == addr) {
throw new NoSuchMethodException("Cannot find symbol " + name + " in library " + name()); throw new NoSuchMethodException("Cannot find symbol " + name + " in library " + name());
} }
return addr; return addr;
} }
/*
* Returns the address of the named symbol defined in the library of
* the given handle. Returns 0 if not found.
*/
static native long findEntry0(long handle, String name);
} }

View file

@ -35,9 +35,6 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import static jdk.internal.loader.NativeLibraries.*;
/** /**
* RawNativeLibraries has the following properties: * RawNativeLibraries has the following properties:
* 1. Native libraries loaded in this RawNativeLibraries instance are * 1. Native libraries loaded in this RawNativeLibraries instance are
@ -136,7 +133,7 @@ public final class RawNativeLibraries {
nl.close(); nl.close();
} }
static class RawNativeLibraryImpl implements NativeLibrary { static class RawNativeLibraryImpl extends NativeLibrary {
// the name of the raw native library. // the name of the raw native library.
final String name; final String name;
// opaque handle to raw native library, used in native code. // opaque handle to raw native library, used in native code.
@ -153,7 +150,7 @@ public final class RawNativeLibraries {
@Override @Override
public long find(String name) { public long find(String name) {
return findEntry0(this, name); return findEntry0(handle, name);
} }
/* /*

View file

@ -218,24 +218,18 @@ Java_jdk_internal_loader_NativeLibraries_unload
JNU_ReleaseStringPlatformChars(env, name, cname); JNU_ReleaseStringPlatformChars(env, name, cname);
} }
/* /*
* Class: jdk_internal_loader_NativeLibraries * Class: jdk_internal_loader_NativeLibrary
* Method: findEntry0 * Method: findEntry0
* Signature: (Ljdk/internal/loader/NativeLibrary;Ljava/lang/String;)J * Signature: (JLjava/lang/String;)J
*/ */
JNIEXPORT jlong JNICALL JNIEXPORT jlong JNICALL
Java_jdk_internal_loader_NativeLibraries_findEntry0 Java_jdk_internal_loader_NativeLibrary_findEntry0
(JNIEnv *env, jclass cls, jobject lib, jstring name) (JNIEnv *env, jclass cls, jlong handle, jstring name)
{ {
jlong handle;
const char *cname; const char *cname;
jlong res; jlong res;
if (!initIDs(env))
return jlong_zero;
handle = (*env)->GetLongField(env, lib, handleID);
cname = (*env)->GetStringUTFChars(env, name, 0); cname = (*env)->GetStringUTFChars(env, name, 0);
if (cname == 0) if (cname == 0)
return jlong_zero; return jlong_zero;