Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/native/libjava/NativeLibraries.c
41149 views
1
/*
2
* Copyright (c) 2020, 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, jboolean isBuiltin, jboolean isJNI)
117
{
118
const char *cname;
119
jint jniVersion;
120
jthrowable cause;
121
void * handle;
122
jboolean loaded = JNI_FALSE;
123
124
if (!initIDs(env))
125
return JNI_FALSE;
126
127
cname = JNU_GetStringPlatformChars(env, name, 0);
128
if (cname == 0)
129
return JNI_FALSE;
130
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname);
131
if (isJNI) {
132
if (handle) {
133
JNI_OnLoad_t JNI_OnLoad;
134
JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle,
135
isBuiltin ? cname : NULL,
136
JNI_TRUE);
137
if (JNI_OnLoad) {
138
JavaVM *jvm;
139
(*env)->GetJavaVM(env, &jvm);
140
jniVersion = (*JNI_OnLoad)(jvm, NULL);
141
} else {
142
jniVersion = 0x00010001;
143
}
144
145
cause = (*env)->ExceptionOccurred(env);
146
if (cause) {
147
(*env)->ExceptionClear(env);
148
(*env)->Throw(env, cause);
149
if (!isBuiltin) {
150
JVM_UnloadLibrary(handle);
151
}
152
goto done;
153
}
154
155
if (!JVM_IsSupportedJNIVersion(jniVersion) ||
156
(isBuiltin && jniVersion < JNI_VERSION_1_8)) {
157
char msg[256];
158
jio_snprintf(msg, sizeof(msg),
159
"unsupported JNI version 0x%08X required by %s",
160
jniVersion, cname);
161
JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg);
162
if (!isBuiltin) {
163
JVM_UnloadLibrary(handle);
164
}
165
goto done;
166
}
167
(*env)->SetIntField(env, lib, jniVersionID, jniVersion);
168
} else {
169
cause = (*env)->ExceptionOccurred(env);
170
if (cause) {
171
(*env)->ExceptionClear(env);
172
(*env)->SetLongField(env, lib, handleID, (jlong)0);
173
(*env)->Throw(env, cause);
174
}
175
goto done;
176
}
177
}
178
(*env)->SetLongField(env, lib, handleID, ptr_to_jlong(handle));
179
loaded = JNI_TRUE;
180
181
done:
182
JNU_ReleaseStringPlatformChars(env, name, cname);
183
return loaded;
184
}
185
186
/*
187
* Class: jdk_internal_loader_NativeLibraries
188
* Method: unload
189
* Signature: (Ljava/lang/String;ZZJ)V
190
*/
191
JNIEXPORT void JNICALL
192
Java_jdk_internal_loader_NativeLibraries_unload
193
(JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jboolean isJNI, jlong address)
194
{
195
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS;
196
void *handle;
197
JNI_OnUnload_t JNI_OnUnload;
198
const char *cname;
199
200
if (!initIDs(env))
201
return;
202
cname = JNU_GetStringPlatformChars(env, name, 0);
203
if (cname == NULL) {
204
return;
205
}
206
handle = jlong_to_ptr(address);
207
if (isJNI) {
208
JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle,
209
isBuiltin ? cname : NULL,
210
JNI_FALSE);
211
if (JNI_OnUnload) {
212
JavaVM *jvm;
213
(*env)->GetJavaVM(env, &jvm);
214
(*JNI_OnUnload)(jvm, NULL);
215
}
216
}
217
if (!isBuiltin) {
218
JVM_UnloadLibrary(handle);
219
}
220
JNU_ReleaseStringPlatformChars(env, name, cname);
221
}
222
223
224
/*
225
* Class: jdk_internal_loader_NativeLibraries
226
* Method: findEntry0
227
* Signature: (Ljava/lang/String;)J
228
*/
229
JNIEXPORT jlong JNICALL
230
Java_jdk_internal_loader_NativeLibraries_findEntry0
231
(JNIEnv *env, jobject this, jobject lib, jstring name)
232
{
233
jlong handle;
234
const char *cname;
235
jlong res;
236
237
if (!initIDs(env))
238
return jlong_zero;
239
240
handle = (*env)->GetLongField(env, lib, handleID);
241
cname = (*env)->GetStringUTFChars(env, name, 0);
242
if (cname == 0)
243
return jlong_zero;
244
res = ptr_to_jlong(JVM_FindLibraryEntry(jlong_to_ptr(handle), cname));
245
(*env)->ReleaseStringUTFChars(env, name, cname);
246
return res;
247
}
248
249
/*
250
* Class: jdk_internal_loader_NativeLibraries
251
* Method: findBuiltinLib
252
* Signature: (Ljava/lang/String;)Ljava/lang/String;
253
*/
254
JNIEXPORT jstring JNICALL
255
Java_jdk_internal_loader_NativeLibraries_findBuiltinLib
256
(JNIEnv *env, jclass cls, jstring name)
257
{
258
const char *cname;
259
char *libName;
260
size_t prefixLen = strlen(JNI_LIB_PREFIX);
261
size_t suffixLen = strlen(JNI_LIB_SUFFIX);
262
size_t len;
263
jstring lib;
264
void *ret;
265
const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
266
267
if (name == NULL) {
268
JNU_ThrowInternalError(env, "NULL filename for native library");
269
return NULL;
270
}
271
procHandle = getProcessHandle();
272
cname = JNU_GetStringPlatformChars(env, name, 0);
273
if (cname == NULL) {
274
return NULL;
275
}
276
// Copy name Skipping PREFIX
277
len = strlen(cname);
278
if (len <= (prefixLen+suffixLen)) {
279
JNU_ReleaseStringPlatformChars(env, name, cname);
280
return NULL;
281
}
282
libName = malloc(len + 1); //+1 for null if prefix+suffix == 0
283
if (libName == NULL) {
284
JNU_ReleaseStringPlatformChars(env, name, cname);
285
JNU_ThrowOutOfMemoryError(env, NULL);
286
return NULL;
287
}
288
if (len > prefixLen) {
289
strcpy(libName, cname+prefixLen);
290
}
291
JNU_ReleaseStringPlatformChars(env, name, cname);
292
293
// Strip SUFFIX
294
libName[strlen(libName)-suffixLen] = '\0';
295
296
// Check for JNI_OnLoad_libname function
297
ret = findJniFunction(env, procHandle, libName, JNI_TRUE);
298
if (ret != NULL) {
299
lib = JNU_NewStringPlatform(env, libName);
300
free(libName);
301
return lib;
302
}
303
free(libName);
304
return NULL;
305
}
306
307