author | rriggs |
Fri, 09 Nov 2018 13:28:16 -0500 | |
changeset 52478 | b915bd68d907 |
parent 47707 | 67aa34b019e1 |
child 58481 | 48a73ec3a817 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
31084
0acb07ceb0bc
8081674: EmptyStackException at startup if running with extended or unsupported charset
simonis
parents:
27184
diff
changeset
|
2 |
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 "java_lang_ClassLoader.h" |
|
34 |
#include "java_lang_ClassLoader_NativeLibrary.h" |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
35 |
#include <string.h> |
2 | 36 |
|
37 |
/* defined in libverify.so/verify.dll (src file common/check_format.c) */ |
|
38 |
extern jboolean VerifyClassname(char *utf_name, jboolean arrayAllowed); |
|
39 |
extern jboolean VerifyFixClassname(char *utf_name); |
|
40 |
||
41 |
static JNINativeMethod methods[] = { |
|
42 |
{"retrieveDirectives", "()Ljava/lang/AssertionStatusDirectives;", (void *)&JVM_AssertionStatusDirectives} |
|
43 |
}; |
|
44 |
||
45 |
JNIEXPORT void JNICALL |
|
46 |
Java_java_lang_ClassLoader_registerNatives(JNIEnv *env, jclass cls) |
|
47 |
{ |
|
48 |
(*env)->RegisterNatives(env, cls, methods, |
|
49 |
sizeof(methods)/sizeof(JNINativeMethod)); |
|
50 |
} |
|
51 |
||
52 |
/* Convert java string to UTF char*. Use local buffer if possible, |
|
53 |
otherwise malloc new memory. Returns null IFF malloc failed. */ |
|
54 |
static char* |
|
55 |
getUTF(JNIEnv *env, jstring str, char* localBuf, int bufSize) |
|
56 |
{ |
|
57 |
char* utfStr = NULL; |
|
58 |
||
59 |
int len = (*env)->GetStringUTFLength(env, str); |
|
60 |
int unicode_len = (*env)->GetStringLength(env, str); |
|
61 |
if (len >= bufSize) { |
|
62 |
utfStr = malloc(len + 1); |
|
63 |
if (utfStr == NULL) { |
|
64 |
JNU_ThrowOutOfMemoryError(env, NULL); |
|
65 |
return NULL; |
|
66 |
} |
|
67 |
} else { |
|
68 |
utfStr = localBuf; |
|
69 |
} |
|
70 |
(*env)->GetStringUTFRegion(env, str, 0, unicode_len, utfStr); |
|
71 |
||
72 |
return utfStr; |
|
73 |
} |
|
74 |
||
75 |
JNIEXPORT jclass JNICALL |
|
76 |
Java_java_lang_ClassLoader_defineClass1(JNIEnv *env, |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
31084
diff
changeset
|
77 |
jclass cls, |
2 | 78 |
jobject loader, |
79 |
jstring name, |
|
80 |
jbyteArray data, |
|
81 |
jint offset, |
|
82 |
jint length, |
|
83 |
jobject pd, |
|
84 |
jstring source) |
|
85 |
{ |
|
86 |
jbyte *body; |
|
87 |
char *utfName; |
|
88 |
jclass result = 0; |
|
89 |
char buf[128]; |
|
90 |
char* utfSource; |
|
91 |
char sourceBuf[1024]; |
|
92 |
||
93 |
if (data == NULL) { |
|
94 |
JNU_ThrowNullPointerException(env, 0); |
|
95 |
return 0; |
|
96 |
} |
|
97 |
||
98 |
/* Work around 4153825. malloc crashes on Solaris when passed a |
|
99 |
* negative size. |
|
100 |
*/ |
|
101 |
if (length < 0) { |
|
102 |
JNU_ThrowArrayIndexOutOfBoundsException(env, 0); |
|
103 |
return 0; |
|
104 |
} |
|
105 |
||
106 |
body = (jbyte *)malloc(length); |
|
107 |
||
108 |
if (body == 0) { |
|
109 |
JNU_ThrowOutOfMemoryError(env, 0); |
|
110 |
return 0; |
|
111 |
} |
|
112 |
||
113 |
(*env)->GetByteArrayRegion(env, data, offset, length, body); |
|
114 |
||
115 |
if ((*env)->ExceptionOccurred(env)) |
|
116 |
goto free_body; |
|
117 |
||
118 |
if (name != NULL) { |
|
119 |
utfName = getUTF(env, name, buf, sizeof(buf)); |
|
120 |
if (utfName == NULL) { |
|
121 |
goto free_body; |
|
122 |
} |
|
123 |
VerifyFixClassname(utfName); |
|
124 |
} else { |
|
125 |
utfName = NULL; |
|
126 |
} |
|
127 |
||
128 |
if (source != NULL) { |
|
129 |
utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf)); |
|
130 |
if (utfSource == NULL) { |
|
131 |
goto free_utfName; |
|
132 |
} |
|
133 |
} else { |
|
134 |
utfSource = NULL; |
|
135 |
} |
|
136 |
result = JVM_DefineClassWithSource(env, utfName, loader, body, length, pd, utfSource); |
|
137 |
||
138 |
if (utfSource && utfSource != sourceBuf) |
|
139 |
free(utfSource); |
|
140 |
||
141 |
free_utfName: |
|
142 |
if (utfName && utfName != buf) |
|
143 |
free(utfName); |
|
144 |
||
145 |
free_body: |
|
146 |
free(body); |
|
147 |
return result; |
|
148 |
} |
|
149 |
||
150 |
JNIEXPORT jclass JNICALL |
|
151 |
Java_java_lang_ClassLoader_defineClass2(JNIEnv *env, |
|
44359
c6761862ca0b
8174823: Module system implementation refresh (3/2017)
alanb
parents:
31084
diff
changeset
|
152 |
jclass cls, |
2 | 153 |
jobject loader, |
154 |
jstring name, |
|
155 |
jobject data, |
|
156 |
jint offset, |
|
157 |
jint length, |
|
158 |
jobject pd, |
|
159 |
jstring source) |
|
160 |
{ |
|
161 |
jbyte *body; |
|
162 |
char *utfName; |
|
163 |
jclass result = 0; |
|
164 |
char buf[128]; |
|
165 |
char* utfSource; |
|
166 |
char sourceBuf[1024]; |
|
167 |
||
168 |
assert(data != NULL); // caller fails if data is null. |
|
169 |
assert(length >= 0); // caller passes ByteBuffer.remaining() for length, so never neg. |
|
170 |
// caller passes ByteBuffer.position() for offset, and capacity() >= position() + remaining() |
|
171 |
assert((*env)->GetDirectBufferCapacity(env, data) >= (offset + length)); |
|
172 |
||
173 |
body = (*env)->GetDirectBufferAddress(env, data); |
|
174 |
||
175 |
if (body == 0) { |
|
176 |
JNU_ThrowNullPointerException(env, 0); |
|
177 |
return 0; |
|
178 |
} |
|
179 |
||
180 |
body += offset; |
|
181 |
||
182 |
if (name != NULL) { |
|
183 |
utfName = getUTF(env, name, buf, sizeof(buf)); |
|
184 |
if (utfName == NULL) { |
|
185 |
JNU_ThrowOutOfMemoryError(env, NULL); |
|
186 |
return result; |
|
187 |
} |
|
188 |
VerifyFixClassname(utfName); |
|
189 |
} else { |
|
190 |
utfName = NULL; |
|
191 |
} |
|
192 |
||
193 |
if (source != NULL) { |
|
194 |
utfSource = getUTF(env, source, sourceBuf, sizeof(sourceBuf)); |
|
195 |
if (utfSource == NULL) { |
|
196 |
JNU_ThrowOutOfMemoryError(env, NULL); |
|
197 |
goto free_utfName; |
|
198 |
} |
|
199 |
} else { |
|
200 |
utfSource = NULL; |
|
201 |
} |
|
202 |
result = JVM_DefineClassWithSource(env, utfName, loader, body, length, pd, utfSource); |
|
203 |
||
204 |
if (utfSource && utfSource != sourceBuf) |
|
205 |
free(utfSource); |
|
206 |
||
207 |
free_utfName: |
|
208 |
if (utfName && utfName != buf) |
|
209 |
free(utfName); |
|
210 |
||
211 |
return result; |
|
212 |
} |
|
213 |
||
3833
b666ed188811
4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents:
2448
diff
changeset
|
214 |
/* |
b666ed188811
4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents:
2448
diff
changeset
|
215 |
* Returns NULL if class not found. |
b666ed188811
4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents:
2448
diff
changeset
|
216 |
*/ |
2 | 217 |
JNIEXPORT jclass JNICALL |
218 |
Java_java_lang_ClassLoader_findBootstrapClass(JNIEnv *env, jobject loader, |
|
219 |
jstring classname) |
|
220 |
{ |
|
221 |
char *clname; |
|
222 |
jclass cls = 0; |
|
223 |
char buf[128]; |
|
224 |
||
225 |
if (classname == NULL) { |
|
226 |
return 0; |
|
227 |
} |
|
228 |
||
229 |
clname = getUTF(env, classname, buf, sizeof(buf)); |
|
230 |
if (clname == NULL) { |
|
231 |
JNU_ThrowOutOfMemoryError(env, NULL); |
|
232 |
return NULL; |
|
233 |
} |
|
234 |
VerifyFixClassname(clname); |
|
235 |
||
236 |
if (!VerifyClassname(clname, JNI_TRUE)) { /* expects slashed name */ |
|
237 |
goto done; |
|
238 |
} |
|
239 |
||
3833
b666ed188811
4917309: (cl) Reduce internal usage of ClassNotFoundExceptions during class-loading
mchung
parents:
2448
diff
changeset
|
240 |
cls = JVM_FindClassFromBootLoader(env, clname); |
2 | 241 |
|
242 |
done: |
|
243 |
if (clname != buf) { |
|
244 |
free(clname); |
|
245 |
} |
|
246 |
||
247 |
return cls; |
|
248 |
} |
|
249 |
||
250 |
JNIEXPORT jclass JNICALL |
|
251 |
Java_java_lang_ClassLoader_findLoadedClass0(JNIEnv *env, jobject loader, |
|
252 |
jstring name) |
|
253 |
{ |
|
254 |
if (name == NULL) { |
|
255 |
return 0; |
|
256 |
} else { |
|
257 |
return JVM_FindLoadedClass(env, loader, name); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
static jfieldID handleID; |
|
262 |
static jfieldID jniVersionID; |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
263 |
static void *procHandle; |
2 | 264 |
|
265 |
static jboolean initIDs(JNIEnv *env) |
|
266 |
{ |
|
267 |
if (handleID == 0) { |
|
268 |
jclass this = |
|
269 |
(*env)->FindClass(env, "java/lang/ClassLoader$NativeLibrary"); |
|
270 |
if (this == 0) |
|
271 |
return JNI_FALSE; |
|
272 |
handleID = (*env)->GetFieldID(env, this, "handle", "J"); |
|
273 |
if (handleID == 0) |
|
274 |
return JNI_FALSE; |
|
275 |
jniVersionID = (*env)->GetFieldID(env, this, "jniVersion", "I"); |
|
276 |
if (jniVersionID == 0) |
|
277 |
return JNI_FALSE; |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
278 |
procHandle = getProcessHandle(); |
2 | 279 |
} |
280 |
return JNI_TRUE; |
|
281 |
} |
|
282 |
||
283 |
typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *); |
|
284 |
typedef void (JNICALL *JNI_OnUnload_t)(JavaVM *, void *); |
|
285 |
||
286 |
/* |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
287 |
* Support for finding JNI_On(Un)Load_<lib_name> if it exists. |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
288 |
* If cname == NULL then just find normal JNI_On(Un)Load entry point |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
289 |
*/ |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
290 |
static void *findJniFunction(JNIEnv *env, void *handle, |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
291 |
const char *cname, jboolean isLoad) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
292 |
const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
293 |
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
294 |
const char **syms; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
295 |
int symsLen; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
296 |
void *entryName = NULL; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
297 |
char *jniFunctionName; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
298 |
int i; |
22962 | 299 |
size_t len; |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
300 |
|
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
301 |
// Check for JNI_On(Un)Load<_libname> function |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
302 |
if (isLoad) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
303 |
syms = onLoadSymbols; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
304 |
symsLen = sizeof(onLoadSymbols) / sizeof(char *); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
305 |
} else { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
306 |
syms = onUnloadSymbols; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
307 |
symsLen = sizeof(onUnloadSymbols) / sizeof(char *); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
308 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
309 |
for (i = 0; i < symsLen; i++) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
310 |
// cname + sym + '_' + '\0' |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
311 |
if ((len = (cname != NULL ? strlen(cname) : 0) + strlen(syms[i]) + 2) > |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
312 |
FILENAME_MAX) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
313 |
goto done; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
314 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
315 |
jniFunctionName = malloc(len); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
316 |
if (jniFunctionName == NULL) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
317 |
JNU_ThrowOutOfMemoryError(env, NULL); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
318 |
goto done; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
319 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
320 |
buildJniFunctionName(syms[i], cname, jniFunctionName); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
321 |
entryName = JVM_FindLibraryEntry(handle, jniFunctionName); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
322 |
free(jniFunctionName); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
323 |
if(entryName) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
324 |
break; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
325 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
326 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
327 |
|
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
328 |
done: |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
329 |
return entryName; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
330 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
331 |
|
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
332 |
/* |
2 | 333 |
* Class: java_lang_ClassLoader_NativeLibrary |
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
334 |
* Method: load0 |
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
335 |
* Signature: (Ljava/lang/String;Z)Z |
2 | 336 |
*/ |
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
337 |
JNIEXPORT jboolean JNICALL |
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
338 |
Java_java_lang_ClassLoader_00024NativeLibrary_load0 |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
339 |
(JNIEnv *env, jobject this, jstring name, jboolean isBuiltin) |
2 | 340 |
{ |
341 |
const char *cname; |
|
342 |
jint jniVersion; |
|
343 |
jthrowable cause; |
|
344 |
void * handle; |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
345 |
jboolean loaded = JNI_FALSE; |
2 | 346 |
|
347 |
if (!initIDs(env)) |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
348 |
return JNI_FALSE; |
2 | 349 |
|
350 |
cname = JNU_GetStringPlatformChars(env, name, 0); |
|
351 |
if (cname == 0) |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
352 |
return JNI_FALSE; |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
353 |
handle = isBuiltin ? procHandle : JVM_LoadLibrary(cname); |
2 | 354 |
if (handle) { |
355 |
JNI_OnLoad_t JNI_OnLoad; |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
356 |
JNI_OnLoad = (JNI_OnLoad_t)findJniFunction(env, handle, |
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
357 |
isBuiltin ? cname : NULL, |
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
358 |
JNI_TRUE); |
2 | 359 |
if (JNI_OnLoad) { |
360 |
JavaVM *jvm; |
|
361 |
(*env)->GetJavaVM(env, &jvm); |
|
362 |
jniVersion = (*JNI_OnLoad)(jvm, NULL); |
|
363 |
} else { |
|
364 |
jniVersion = 0x00010001; |
|
365 |
} |
|
366 |
||
367 |
cause = (*env)->ExceptionOccurred(env); |
|
368 |
if (cause) { |
|
369 |
(*env)->ExceptionClear(env); |
|
370 |
(*env)->Throw(env, cause); |
|
16506
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
371 |
if (!isBuiltin) { |
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
372 |
JVM_UnloadLibrary(handle); |
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
373 |
} |
2 | 374 |
goto done; |
375 |
} |
|
376 |
||
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
377 |
if (!JVM_IsSupportedJNIVersion(jniVersion) || |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
378 |
(isBuiltin && jniVersion < JNI_VERSION_1_8)) { |
2 | 379 |
char msg[256]; |
380 |
jio_snprintf(msg, sizeof(msg), |
|
381 |
"unsupported JNI version 0x%08X required by %s", |
|
382 |
jniVersion, cname); |
|
383 |
JNU_ThrowByName(env, "java/lang/UnsatisfiedLinkError", msg); |
|
16506
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
384 |
if (!isBuiltin) { |
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
385 |
JVM_UnloadLibrary(handle); |
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
386 |
} |
2 | 387 |
goto done; |
388 |
} |
|
389 |
(*env)->SetIntField(env, this, jniVersionID, jniVersion); |
|
390 |
} else { |
|
391 |
cause = (*env)->ExceptionOccurred(env); |
|
392 |
if (cause) { |
|
393 |
(*env)->ExceptionClear(env); |
|
6850
56966b0a6a0d
6989466: Miscellaneous compiler warnings in java/lang, java/util, java/io, sun/misc native code
alanb
parents:
5506
diff
changeset
|
394 |
(*env)->SetLongField(env, this, handleID, (jlong)0); |
2 | 395 |
(*env)->Throw(env, cause); |
396 |
} |
|
397 |
goto done; |
|
398 |
} |
|
399 |
(*env)->SetLongField(env, this, handleID, ptr_to_jlong(handle)); |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
400 |
loaded = JNI_TRUE; |
2 | 401 |
|
402 |
done: |
|
403 |
JNU_ReleaseStringPlatformChars(env, name, cname); |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
404 |
return loaded; |
2 | 405 |
} |
406 |
||
407 |
/* |
|
408 |
* Class: java_lang_ClassLoader_NativeLibrary |
|
409 |
* Method: unload |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
410 |
* Signature: (Ljava/lang/String;ZJ)V |
2 | 411 |
*/ |
412 |
JNIEXPORT void JNICALL |
|
413 |
Java_java_lang_ClassLoader_00024NativeLibrary_unload |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
414 |
(JNIEnv *env, jclass cls, jstring name, jboolean isBuiltin, jlong address) |
2 | 415 |
{ |
416 |
const char *onUnloadSymbols[] = JNI_ONUNLOAD_SYMBOLS; |
|
417 |
void *handle; |
|
418 |
JNI_OnUnload_t JNI_OnUnload; |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
419 |
const char *cname; |
2 | 420 |
|
421 |
if (!initIDs(env)) |
|
422 |
return; |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
423 |
cname = JNU_GetStringPlatformChars(env, name, 0); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
424 |
if (cname == NULL) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
425 |
return; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
426 |
} |
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
427 |
handle = jlong_to_ptr(address); |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
428 |
JNI_OnUnload = (JNI_OnUnload_t )findJniFunction(env, handle, |
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
429 |
isBuiltin ? cname : NULL, |
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
430 |
JNI_FALSE); |
2 | 431 |
if (JNI_OnUnload) { |
432 |
JavaVM *jvm; |
|
433 |
(*env)->GetJavaVM(env, &jvm); |
|
434 |
(*JNI_OnUnload)(jvm, NULL); |
|
435 |
} |
|
16506
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
436 |
if (!isBuiltin) { |
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
437 |
JVM_UnloadLibrary(handle); |
2a43ffb8a318
8010668: builtin JNI libraries should not be unloaded
chegar
parents:
16479
diff
changeset
|
438 |
} |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
439 |
JNU_ReleaseStringPlatformChars(env, name, cname); |
2 | 440 |
} |
441 |
||
442 |
/* |
|
443 |
* Class: java_lang_ClassLoader_NativeLibrary |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
444 |
* Method: findEntry |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
445 |
* Signature: (Ljava/lang/String;)J |
2 | 446 |
*/ |
447 |
JNIEXPORT jlong JNICALL |
|
47707
67aa34b019e1
8164512: Replace ClassLoader use of finalizer with phantom reference to unload native library
mchung
parents:
47216
diff
changeset
|
448 |
Java_java_lang_ClassLoader_00024NativeLibrary_findEntry |
2 | 449 |
(JNIEnv *env, jobject this, jstring name) |
450 |
{ |
|
451 |
jlong handle; |
|
452 |
const char *cname; |
|
453 |
jlong res; |
|
454 |
||
455 |
if (!initIDs(env)) |
|
456 |
return jlong_zero; |
|
457 |
||
458 |
handle = (*env)->GetLongField(env, this, handleID); |
|
459 |
cname = (*env)->GetStringUTFChars(env, name, 0); |
|
460 |
if (cname == 0) |
|
461 |
return jlong_zero; |
|
462 |
res = ptr_to_jlong(JVM_FindLibraryEntry(jlong_to_ptr(handle), cname)); |
|
463 |
(*env)->ReleaseStringUTFChars(env, name, cname); |
|
464 |
return res; |
|
465 |
} |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
466 |
/* |
31084
0acb07ceb0bc
8081674: EmptyStackException at startup if running with extended or unsupported charset
simonis
parents:
27184
diff
changeset
|
467 |
* Class: java_lang_ClassLoader |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
468 |
* Method: findBuiltinLib |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
469 |
* Signature: (Ljava/lang/String;)Ljava/lang/String; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
470 |
*/ |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
471 |
JNIEXPORT jstring JNICALL |
31084
0acb07ceb0bc
8081674: EmptyStackException at startup if running with extended or unsupported charset
simonis
parents:
27184
diff
changeset
|
472 |
Java_java_lang_ClassLoader_findBuiltinLib |
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
473 |
(JNIEnv *env, jclass cls, jstring name) |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
474 |
{ |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
475 |
const char *cname; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
476 |
char *libName; |
22962 | 477 |
size_t prefixLen = strlen(JNI_LIB_PREFIX); |
478 |
size_t suffixLen = strlen(JNI_LIB_SUFFIX); |
|
479 |
size_t len; |
|
16479
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
480 |
jstring lib; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
481 |
void *ret; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
482 |
const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
483 |
|
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
484 |
if (name == NULL) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
485 |
JNU_ThrowInternalError(env, "NULL filename for native library"); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
486 |
return NULL; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
487 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
488 |
procHandle = getProcessHandle(); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
489 |
cname = JNU_GetStringPlatformChars(env, name, 0); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
490 |
if (cname == NULL) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
491 |
return NULL; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
492 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
493 |
// Copy name Skipping PREFIX |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
494 |
len = strlen(cname); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
495 |
if (len <= (prefixLen+suffixLen)) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
496 |
JNU_ReleaseStringPlatformChars(env, name, cname); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
497 |
return NULL; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
498 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
499 |
libName = malloc(len + 1); //+1 for null if prefix+suffix == 0 |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
500 |
if (libName == NULL) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
501 |
JNU_ReleaseStringPlatformChars(env, name, cname); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
502 |
JNU_ThrowOutOfMemoryError(env, NULL); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
503 |
return NULL; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
504 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
505 |
if (len > prefixLen) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
506 |
strcpy(libName, cname+prefixLen); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
507 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
508 |
JNU_ReleaseStringPlatformChars(env, name, cname); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
509 |
|
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
510 |
// Strip SUFFIX |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
511 |
libName[strlen(libName)-suffixLen] = '\0'; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
512 |
|
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
513 |
// Check for JNI_OnLoad_libname function |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
514 |
ret = findJniFunction(env, procHandle, libName, JNI_TRUE); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
515 |
if (ret != NULL) { |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
516 |
lib = JNU_NewStringPlatform(env, libName); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
517 |
free(libName); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
518 |
return lib; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
519 |
} |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
520 |
free(libName); |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
521 |
return NULL; |
d845c18d13f2
8005716: Enhance JNI specification to allow support of static JNI libraries in Embedded JREs
alanb
parents:
7668
diff
changeset
|
522 |
} |