34
#include "jdk_internal_loader_NativeLibraries.h"
36
typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
37
typedef void (JNICALL *JNI_OnUnload_t)(JavaVM *, void *);
39
static jfieldID handleID;
40
static jfieldID jniVersionID;
41
static void *procHandle;
43
static jboolean initIDs(JNIEnv *env)
47
(*env)->FindClass(env, "jdk/internal/loader/NativeLibraries$NativeLibraryImpl");
50
handleID = (*env)->GetFieldID(env, nlClz, "handle", "J");
53
jniVersionID = (*env)->GetFieldID(env, nlClz, "jniVersion", "I");
54
if (jniVersionID == 0)
56
procHandle = getProcessHandle();
66
static void *findJniFunction(JNIEnv *env, void *handle,
67
const char *cname, jboolean isLoad) {
68
const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
69
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
72
void *entryName = NULL;
73
char *jniFunctionName;
80
symsLen = sizeof(onLoadSymbols) / sizeof(char *);
82
syms = onUnloadSymbols;
83
symsLen = sizeof(onUnloadSymbols) / sizeof(char *);
85
for (i = 0; i < symsLen; i++) {
87
if ((len = (cname != NULL ? strlen(cname) : 0) + strlen(syms[i]) + 2) >
91
jniFunctionName = malloc(len);
92
if (jniFunctionName == NULL) {
93
JNU_ThrowOutOfMemoryError(env, NULL);
96
buildJniFunctionName(syms[i], cname, jniFunctionName);
97
entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
98
free(jniFunctionName);
113
JNIEXPORT jboolean JNICALL
114
Java_jdk_internal_loader_NativeLibraries_load
115
(JNIEnv *env, jclass cls, jobject lib, jstring name,
116
jboolean isBuiltin, jboolean throwExceptionIfFail)
122
jboolean loaded = JNI_FALSE;
127
cname = JNU_GetStringPlatformChars(env, name, 0);
130
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname, throwExceptionIfFail);
132
JNI_OnLoad_t JNI_OnLoad;
133
JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
134
isBuiltin ? cname : NULL,
138
(*env)->GetJavaVM(env, &jvm);
139
jniVersion = (*JNI_OnLoad)(jvm, NULL);
141
jniVersion = 0x00010001;
144
cause = (*env)->ExceptionOccurred(env);
146
(*env)->ExceptionClear(env);
147
(*env)->Throw(env, cause);
149
JVM_UnloadLibrary(handle);
154
if (!JVM_IsSupportedJNIVersion(jniVersion) ||
155
(isBuiltin && jniVersion < JNI_VERSION_1_8)) {
157
jio_snprintf(msg, sizeof(msg),
158
"unsupported JNI version 0x%08X required by %s",
160
JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
162
JVM_UnloadLibrary(handle);
166
(*env)->SetIntField(env, lib, jniVersionID, jniVersion);
168
cause = (*env)->ExceptionOccurred(env);
170
(*env)->ExceptionClear(env);
171
(*env)->SetLongField(env, lib, handleID, (jlong)0);
172
(*env)->Throw(env, cause);
177
(*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
181
JNU_ReleaseStringPlatformChars(env, name, cname);
190
JNIEXPORT void JNICALL
191
Java_jdk_internal_loader_NativeLibraries_unload
192
(JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jlong address)
195
JNI_OnUnload_t JNI_OnUnload;
200
cname = JNU_GetStringPlatformChars(env, name, 0);
204
handle = jlong_to_ptr(address);
206
JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
207
isBuiltin ? cname : NULL,
211
(*env)->GetJavaVM(env, &jvm);
212
(*JNI_OnUnload)(jvm, NULL);
215
JVM_UnloadLibrary(handle);
217
JNU_ReleaseStringPlatformChars(env, name, cname);
225
JNIEXPORT jlong JNICALL
226
Java_jdk_internal_loader_NativeLibrary_findEntry0
227
(JNIEnv *env, jclass cls, jlong handle, jstring name)
232
cname = (*env)->GetStringUTFChars(env, name, 0);
235
res = ptr_to_jlong(JVM_FindLibraryEntry(jlong_to_ptr(handle), cname));
236
(*env)->ReleaseStringUTFChars(env, name, cname);
245
JNIEXPORT jstring JNICALL
246
Java_jdk_internal_loader_NativeLibraries_findBuiltinLib
247
(JNIEnv *env, jclass cls, jstring name)
251
size_t prefixLen = strlen(JNI_LIB_PREFIX);
252
size_t suffixLen = strlen(JNI_LIB_SUFFIX);
258
JNU_ThrowInternalError(env, "NULL filename for native library");
261
procHandle = getProcessHandle();
262
cname = JNU_GetStringPlatformChars(env, name, 0);
268
if (len <= (prefixLen+suffixLen)) {
269
JNU_ReleaseStringPlatformChars(env, name, cname);
272
libName = malloc(len + 1);
273
if (libName == NULL) {
274
JNU_ReleaseStringPlatformChars(env, name, cname);
275
JNU_ThrowOutOfMemoryError(env, NULL);
278
if (len > prefixLen) {
279
strcpy(libName, cname+prefixLen);
281
JNU_ReleaseStringPlatformChars(env, name, cname);
284
libName[strlen(libName)-suffixLen] = '\0';
287
ret = findJniFunction(env, procHandle, libName, JNI_TRUE);
289
lib = JNU_NewStringPlatform(env, libName);