Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/native/libjava/NativeLibraries.c
67743 views
1
/*
2
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#include <stdlib.h>
27
#include <assert.h>
28
29
#include "jni.h"
30
#include "jni_util.h"
31
#include "jlong.h"
32
#include "jvm.h"
33
#include "jdk_internal_loader_NativeLibraries.h"
34
#include <string.h>
35
36
typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
37
typedef void (JNICALL *JNI_OnUnload_t)(JavaVM *, void *);
38
39
static jfieldID handleID;
40
static jfieldID jniVersionID;
41
static void *procHandle;
42
43
44
static jboolean initIDs(JNIEnv *env)
45
{
46
if (handleID == 0) {
47
jclass this =
48
(*env)->FindClass(env, "jdk/internal/loader/NativeLibraries$NativeLibraryImpl");
49
if (this == 0)
50
return JNI_FALSE;
51
handleID = (*env)->GetFieldID(env, this, "handle", "J");
52
if (handleID == 0)
53
return JNI_FALSE;
54
jniVersionID = (*env)->GetFieldID(env, this, "jniVersion", "I");
55
if (jniVersionID == 0)
56
return JNI_FALSE;
57
procHandle = getProcessHandle();
58
}
59
return JNI_TRUE;
60
}
61
62
63
/*
64
* Support for finding JNI_On(Un)Load_<lib_name> if it exists.
65
* If cname == NULL then just find normal JNI_On(Un)Load entry point
66
*/
67
static void *findJniFunction(JNIEnv *env, void *handle,
68
const char *cname, jboolean isLoad) {
69
const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
70
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
71
const char **syms;
72
int symsLen;
73
void *entryName = NULL;
74
char *jniFunctionName;
75
int i;
76
size_t len;
77
78
// Check for JNI_On(Un)Load<_libname> function
79
if (isLoad) {
80
syms = onLoadSymbols;
81
symsLen = sizeof(onLoadSymbols) / sizeof(char *);
82
} else {
83
syms = onUnloadSymbols;
84
symsLen = sizeof(onUnloadSymbols) / sizeof(char *);
85
}
86
for (i = 0; i < symsLen; i++) {
87
// cname + sym + '_' + '\0'
88
if ((len = (cname != NULL ? strlen(cname) : 0) + strlen(syms[i]) + 2) >
89
FILENAME_MAX) {
90
goto done;
91
}
92
jniFunctionName = malloc(len);
93
if (jniFunctionName == NULL) {
94
JNU_ThrowOutOfMemoryError(env, NULL);
95
goto done;
96
}
97
buildJniFunctionName(syms[i], cname, jniFunctionName);
98
entryName = JVM_FindLibraryEntry(handle, jniFunctionName);
99
free(jniFunctionName);
100
if(entryName) {
101
break;
102
}
103
}
104
105
done:
106
return entryName;
107
}
108
109
/*
110
* Class: jdk_internal_loader_NativeLibraries
111
* Method: load
112
* Signature: (Ljava/lang/String;ZZ)Z
113
*/
114
JNIEXPORT jboolean JNICALL
115
Java_jdk_internal_loader_NativeLibraries_load
116
(JNIEnv *env, jobject this, jobject lib, jstring name,
117
jboolean isBuiltin, jboolean isJNI, jboolean throwExceptionIfFail)
118
{
119
const char *cname;
120
jint jniVersion;
121
jthrowable cause;
122
void * handle;
123
jboolean loaded = JNI_FALSE;
124
125
if (!initIDs(env))
126
return JNI_FALSE;
127
128
cname = JNU_GetStringPlatformChars(env, name, 0);
129
if (cname == 0)
130
return JNI_FALSE;
131
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname, throwExceptionIfFail);
132
if (isJNI) {
133
if (handle) {
134
JNI_OnLoad_t JNI_OnLoad;
135
JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
136
isBuiltin ? cname : NULL,
137
JNI_TRUE);
138
if (JNI_OnLoad) {
139
JavaVM *jvm;
140
(*env)->GetJavaVM(env, &jvm);
141
jniVersion = (*JNI_OnLoad)(jvm, NULL);
142
} else {
143
jniVersion = 0x00010001;
144
}
145
146
cause = (*env)->ExceptionOccurred(env);
147
if (cause) {
148
(*env)->ExceptionClear(env);
149
(*env)->Throw(env, cause);
150
if (!isBuiltin) {
151
JVM_UnloadLibrary(handle);
152
}
153
goto done;
154
}
155
156
if (!JVM_IsSupportedJNIVersion(jniVersion) ||
157
(isBuiltin && jniVersion < JNI_VERSION_1_8)) {
158
char msg[256];
159
jio_snprintf(msg, sizeof(msg),
160
"unsupported JNI version 0x%08X required by %s",
161
jniVersion, cname);
162
JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
163
if (!isBuiltin) {
164
JVM_UnloadLibrary(handle);
165
}
166
goto done;
167
}
168
(*env)->SetIntField(env, lib, jniVersionID, jniVersion);
169
} else {
170
cause = (*env)->ExceptionOccurred(env);
171
if (cause) {
172
(*env)->ExceptionClear(env);
173
(*env)->SetLongField(env, lib, handleID, (jlong)0);
174
(*env)->Throw(env, cause);
175
}
176
goto done;
177
}
178
}
179
(*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
180
loaded = JNI_TRUE;
181
182
done:
183
JNU_ReleaseStringPlatformChars(env, name, cname);
184
return loaded;
185
}
186
187
/*
188
* Class: jdk_internal_loader_NativeLibraries
189
* Method: unload
190
* Signature: (Ljava/lang/String;ZZJ)V
191
*/
192
JNIEXPORT void JNICALL
193
Java_jdk_internal_loader_NativeLibraries_unload
194
(JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jboolean isJNI, jlong address)
195
{
196
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
197
void *handle;
198
JNI_OnUnload_t JNI_OnUnload;
199
const char *cname;
200
201
if (!initIDs(env))
202
return;
203
cname = JNU_GetStringPlatformChars(env, name, 0);
204
if (cname == NULL) {
205
return;
206
}
207
handle = jlong_to_ptr(address);
208
if (isJNI) {
209
JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
210
isBuiltin ? cname : NULL,
211
JNI_FALSE);
212
if (JNI_OnUnload) {
213
JavaVM *jvm;
214
(*env)->GetJavaVM(env, &jvm);
215
(*JNI_OnUnload)(jvm, NULL);
216
}
217
}
218
if (!isBuiltin) {
219
JVM_UnloadLibrary(handle);
220
}
221
JNU_ReleaseStringPlatformChars(env, name, cname);
222
}
223
224
225
/*
226
* Class: jdk_internal_loader_NativeLibraries
227
* Method: findEntry0
228
* Signature: (Ljava/lang/String;)J
229
*/
230
JNIEXPORT jlong JNICALL
231
Java_jdk_internal_loader_NativeLibraries_findEntry0
232
(JNIEnv *env, jobject this, jobject lib, jstring name)
233
{
234
jlong handle;
235
const char *cname;
236
jlong res;
237
238
if (!initIDs(env))
239
return jlong_zero;
240
241
handle = (*env)->GetLongField(env, lib, handleID);
242
cname = (*env)->GetStringUTFChars(env, name, 0);
243
if (cname == 0)
244
return jlong_zero;
245
res = ptr_to_jlong(JVM_FindLibraryEntry(jlong_to_ptr(handle), cname));
246
(*env)->ReleaseStringUTFChars(env, name, cname);
247
return res;
248
}
249
250
/*
251
* Class: jdk_internal_loader_NativeLibraries
252
* Method: findBuiltinLib
253
* Signature: (Ljava/lang/String;)Ljava/lang/String;
254
*/
255
JNIEXPORT jstring JNICALL
256
Java_jdk_internal_loader_NativeLibraries_findBuiltinLib
257
(JNIEnv *env, jclass cls, jstring name)
258
{
259
const char *cname;
260
char *libName;
261
size_t prefixLen = strlen(JNI_LIB_PREFIX);
262
size_t suffixLen = strlen(JNI_LIB_SUFFIX);
263
size_t len;
264
jstring lib;
265
void *ret;
266
const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
267
268
if (name == NULL) {
269
JNU_ThrowInternalError(env, "NULL filename for native library");
270
return NULL;
271
}
272
procHandle = getProcessHandle();
273
cname = JNU_GetStringPlatformChars(env, name, 0);
274
if (cname == NULL) {
275
return NULL;
276
}
277
// Copy name Skipping PREFIX
278
len = strlen(cname);
279
if (len <= (prefixLen+suffixLen)) {
280
JNU_ReleaseStringPlatformChars(env, name, cname);
281
return NULL;
282
}
283
libName = malloc(len + 1); //+1 for null if prefix+suffix == 0
284
if (libName == NULL) {
285
JNU_ReleaseStringPlatformChars(env, name, cname);
286
JNU_ThrowOutOfMemoryError(env, NULL);
287
return NULL;
288
}
289
if (len > prefixLen) {
290
strcpy(libName, cname+prefixLen);
291
}
292
JNU_ReleaseStringPlatformChars(env, name, cname);
293
294
// Strip SUFFIX
295
libName[strlen(libName)-suffixLen] = '\0';
296
297
// Check for JNI_OnLoad_libname function
298
ret = findJniFunction(env, procHandle, libName, JNI_TRUE);
299
if (ret != NULL) {
300
lib = JNU_NewStringPlatform(env, libName);
301
free(libName);
302
return lib;
303
}
304
free(libName);
305
return NULL;
306
}
307
308