mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 15:24:43 +02:00
8282515: More clean up on NativeLibraries just for JNI library use
Reviewed-by: mcimadamore
This commit is contained in:
parent
1485883c9e
commit
02aa7cef0a
6 changed files with 250 additions and 102 deletions
|
@ -25,13 +25,13 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jni.h"
|
||||
#include "jni_util.h"
|
||||
#include "jlong.h"
|
||||
#include "jvm.h"
|
||||
#include "jdk_internal_loader_NativeLibraries.h"
|
||||
#include <string.h>
|
||||
|
||||
typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
|
||||
typedef void (JNICALL *JNI_OnUnload_t)(JavaVM *, void *);
|
||||
|
@ -40,18 +40,17 @@ static jfieldID handleID;
|
|||
static jfieldID jniVersionID;
|
||||
static void *procHandle;
|
||||
|
||||
|
||||
static jboolean initIDs(JNIEnv *env)
|
||||
{
|
||||
if (handleID == 0) {
|
||||
jclass this =
|
||||
jclass nlClz =
|
||||
(*env)->FindClass(env, "jdk/internal/loader/NativeLibraries$NativeLibraryImpl");
|
||||
if (this == 0)
|
||||
if (nlClz == 0)
|
||||
return JNI_FALSE;
|
||||
handleID = (*env)->GetFieldID(env, this, "handle", "J");
|
||||
handleID = (*env)->GetFieldID(env, nlClz, "handle", "J");
|
||||
if (handleID == 0)
|
||||
return JNI_FALSE;
|
||||
jniVersionID = (*env)->GetFieldID(env, this, "jniVersion", "I");
|
||||
jniVersionID = (*env)->GetFieldID(env, nlClz, "jniVersion", "I");
|
||||
if (jniVersionID == 0)
|
||||
return JNI_FALSE;
|
||||
procHandle = getProcessHandle();
|
||||
|
@ -109,12 +108,12 @@ static void *findJniFunction(JNIEnv *env, void *handle,
|
|||
/*
|
||||
* Class: jdk_internal_loader_NativeLibraries
|
||||
* Method: load
|
||||
* Signature: (Ljava/lang/String;ZZ)Z
|
||||
* Signature: (Ljdk/internal/loader/NativeLibraries/NativeLibraryImpl;Ljava/lang/String;ZZ)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_jdk_internal_loader_NativeLibraries_load
|
||||
(JNIEnv *env, jobject this, jobject lib, jstring name,
|
||||
jboolean isBuiltin, jboolean isJNI, jboolean throwExceptionIfFail)
|
||||
(JNIEnv *env, jclass cls, jobject lib, jstring name,
|
||||
jboolean isBuiltin, jboolean throwExceptionIfFail)
|
||||
{
|
||||
const char *cname;
|
||||
jint jniVersion;
|
||||
|
@ -129,53 +128,52 @@ Java_jdk_internal_loader_NativeLibraries_load
|
|||
if (cname == 0)
|
||||
return JNI_FALSE;
|
||||
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname, throwExceptionIfFail);
|
||||
if (isJNI) {
|
||||
if (handle) {
|
||||
JNI_OnLoad_t JNI_OnLoad;
|
||||
JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
|
||||
isBuiltin ? cname : NULL,
|
||||
JNI_TRUE);
|
||||
if (JNI_OnLoad) {
|
||||
JavaVM *jvm;
|
||||
(*env)->GetJavaVM(env, &jvm);
|
||||
jniVersion = (*JNI_OnLoad)(jvm, NULL);
|
||||
} else {
|
||||
jniVersion = 0x00010001;
|
||||
}
|
||||
|
||||
cause = (*env)->ExceptionOccurred(env);
|
||||
if (cause) {
|
||||
(*env)->ExceptionClear(env);
|
||||
(*env)->Throw(env, cause);
|
||||
if (!isBuiltin) {
|
||||
JVM_UnloadLibrary(handle);
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!JVM_IsSupportedJNIVersion(jniVersion) ||
|
||||
(isBuiltin && jniVersion < JNI_VERSION_1_8)) {
|
||||
char msg[256];
|
||||
jio_snprintf(msg, sizeof(msg),
|
||||
"unsupported JNI version 0x%08X required by %s",
|
||||
jniVersion, cname);
|
||||
JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
|
||||
if (!isBuiltin) {
|
||||
JVM_UnloadLibrary(handle);
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
(*env)->SetIntField(env, lib, jniVersionID, jniVersion);
|
||||
if (handle) {
|
||||
JNI_OnLoad_t JNI_OnLoad;
|
||||
JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
|
||||
isBuiltin ? cname : NULL,
|
||||
JNI_TRUE);
|
||||
if (JNI_OnLoad) {
|
||||
JavaVM *jvm;
|
||||
(*env)->GetJavaVM(env, &jvm);
|
||||
jniVersion = (*JNI_OnLoad)(jvm, NULL);
|
||||
} else {
|
||||
cause = (*env)->ExceptionOccurred(env);
|
||||
if (cause) {
|
||||
(*env)->ExceptionClear(env);
|
||||
(*env)->SetLongField(env, lib, handleID, (jlong)0);
|
||||
(*env)->Throw(env, cause);
|
||||
jniVersion = 0x00010001;
|
||||
}
|
||||
|
||||
cause = (*env)->ExceptionOccurred(env);
|
||||
if (cause) {
|
||||
(*env)->ExceptionClear(env);
|
||||
(*env)->Throw(env, cause);
|
||||
if (!isBuiltin) {
|
||||
JVM_UnloadLibrary(handle);
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
|
||||
if (!JVM_IsSupportedJNIVersion(jniVersion) ||
|
||||
(isBuiltin && jniVersion < JNI_VERSION_1_8)) {
|
||||
char msg[256];
|
||||
jio_snprintf(msg, sizeof(msg),
|
||||
"unsupported JNI version 0x%08X required by %s",
|
||||
jniVersion, cname);
|
||||
JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
|
||||
if (!isBuiltin) {
|
||||
JVM_UnloadLibrary(handle);
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
(*env)->SetIntField(env, lib, jniVersionID, jniVersion);
|
||||
} else {
|
||||
cause = (*env)->ExceptionOccurred(env);
|
||||
if (cause) {
|
||||
(*env)->ExceptionClear(env);
|
||||
(*env)->SetLongField(env, lib, handleID, (jlong)0);
|
||||
(*env)->Throw(env, cause);
|
||||
}
|
||||
goto done;
|
||||
}
|
||||
|
||||
(*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
|
||||
loaded = JNI_TRUE;
|
||||
|
||||
|
@ -187,11 +185,11 @@ Java_jdk_internal_loader_NativeLibraries_load
|
|||
/*
|
||||
* Class: jdk_internal_loader_NativeLibraries
|
||||
* Method: unload
|
||||
* Signature: (Ljava/lang/String;ZZJ)V
|
||||
* Signature: (Ljava/lang/String;ZJ)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL
|
||||
Java_jdk_internal_loader_NativeLibraries_unload
|
||||
(JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jboolean isJNI, jlong address)
|
||||
(JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jlong address)
|
||||
{
|
||||
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
|
||||
void *handle;
|
||||
|
@ -205,15 +203,14 @@ Java_jdk_internal_loader_NativeLibraries_unload
|
|||
return;
|
||||
}
|
||||
handle = jlong_to_ptr(address);
|
||||
if (isJNI) {
|
||||
JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
|
||||
isBuiltin ? cname : NULL,
|
||||
JNI_FALSE);
|
||||
if (JNI_OnUnload) {
|
||||
JavaVM *jvm;
|
||||
(*env)->GetJavaVM(env, &jvm);
|
||||
(*JNI_OnUnload)(jvm, NULL);
|
||||
}
|
||||
|
||||
JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
|
||||
isBuiltin ? cname : NULL,
|
||||
JNI_FALSE);
|
||||
if (JNI_OnUnload) {
|
||||
JavaVM *jvm;
|
||||
(*env)->GetJavaVM(env, &jvm);
|
||||
(*JNI_OnUnload)(jvm, NULL);
|
||||
}
|
||||
if (!isBuiltin) {
|
||||
JVM_UnloadLibrary(handle);
|
||||
|
@ -225,11 +222,11 @@ Java_jdk_internal_loader_NativeLibraries_unload
|
|||
/*
|
||||
* Class: jdk_internal_loader_NativeLibraries
|
||||
* Method: findEntry0
|
||||
* Signature: (Ljava/lang/String;)J
|
||||
* Signature: (Ljdk/internal/loader/NativeLibrary;Ljava/lang/String;)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL
|
||||
Java_jdk_internal_loader_NativeLibraries_findEntry0
|
||||
(JNIEnv *env, jobject this, jobject lib, jstring name)
|
||||
(JNIEnv *env, jclass cls, jobject lib, jstring name)
|
||||
{
|
||||
jlong handle;
|
||||
const char *cname;
|
||||
|
|
99
src/java.base/share/native/libjava/RawNativeLibraries.c
Normal file
99
src/java.base/share/native/libjava/RawNativeLibraries.c
Normal file
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "jni.h"
|
||||
#include "jni_util.h"
|
||||
#include "jlong.h"
|
||||
#include "jvm.h"
|
||||
#include "jdk_internal_loader_RawNativeLibraries.h"
|
||||
|
||||
static jfieldID handleID;
|
||||
|
||||
static jboolean initIDs(JNIEnv *env)
|
||||
{
|
||||
if (handleID == 0) {
|
||||
jclass rnlClz =
|
||||
(*env)->FindClass(env, "jdk/internal/loader/RawNativeLibraries$RawNativeLibraryImpl");
|
||||
if (rnlClz == 0)
|
||||
return JNI_FALSE;
|
||||
handleID = (*env)->GetFieldID(env, rnlClz, "handle", "J");
|
||||
if (handleID == 0)
|
||||
return JNI_FALSE;
|
||||
}
|
||||
return JNI_TRUE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: jdk_internal_loader_RawNativeLibraries
|
||||
* Method: Java_jdk_internal_loader_RawNativeLibraries_load0
|
||||
* Signature: (Ljdk/internal/loader/RawNativeLibraries/RawNativeLibraryImpl;Ljava/lang/String;)Z
|
||||
*/
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_jdk_internal_loader_RawNativeLibraries_load0
|
||||
(JNIEnv *env, jclass cls, jobject lib, jstring name)
|
||||
{
|
||||
const char *cname;
|
||||
void * handle;
|
||||
|
||||
if (!initIDs(env))
|
||||
return JNI_FALSE;
|
||||
|
||||
cname = JNU_GetStringPlatformChars(env, name, 0);
|
||||
if (cname == 0)
|
||||
return JNI_FALSE;
|
||||
handle = JVM_LoadLibrary(cname, JNI_FALSE);
|
||||
(*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
|
||||
|
||||
JNU_ReleaseStringPlatformChars(env, name, cname);
|
||||
return handle != 0L;
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: jdk_internal_loader_RawNativeLibraries
|
||||
* Method: unload0
|
||||
* Signature: (Ljava/lang/String;J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_jdk_internal_loader_RawNativeLibraries_unload0
|
||||
(JNIEnv *env, jclass cls, jstring name, jlong address)
|
||||
{
|
||||
void *handle;
|
||||
const char *cname;
|
||||
|
||||
if (!initIDs(env))
|
||||
return;
|
||||
cname = JNU_GetStringPlatformChars(env, name, 0);
|
||||
if (cname == NULL) {
|
||||
return;
|
||||
}
|
||||
handle = jlong_to_ptr(address);
|
||||
|
||||
JVM_UnloadLibrary(handle);
|
||||
JNU_ReleaseStringPlatformChars(env, name, cname);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue