Mercurial > hg > truffle
annotate src/share/vm/prims/jni.cpp @ 235:9c2ecc2ffb12 jdk7-b31
Merge
author | trims |
---|---|
date | Fri, 11 Jul 2008 01:14:44 -0700 |
parents | d1605aabd0a1 8d852b81e775 |
children | 1fdb98a17101 |
rev | line source |
---|---|
0 | 1 /* |
196 | 2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved. |
0 | 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. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, | |
20 * CA 95054 USA or visit www.sun.com if you need additional information or | |
21 * have any questions. | |
22 * | |
23 */ | |
24 | |
25 # include "incls/_precompiled.incl" | |
26 # include "incls/_jni.cpp.incl" | |
27 | |
28 static jint CurrentVersion = JNI_VERSION_1_6; | |
29 | |
30 | |
31 // The DT_RETURN_MARK macros create a scoped object to fire the dtrace | |
32 // '-return' probe regardless of the return path is taken out of the function. | |
33 // Methods that have multiple return paths use this to avoid having to | |
34 // instrument each return path. Methods that use CHECK or THROW must use this | |
35 // since those macros can cause an immedate uninstrumented return. | |
36 // | |
37 // In order to get the return value, a reference to the variable containing | |
38 // the return value must be passed to the contructor of the object, and | |
39 // the return value must be set before return (since the mark object has | |
40 // a reference to it). | |
41 // | |
42 // Example: | |
43 // DT_RETURN_MARK_DECL(SomeFunc, int); | |
44 // JNI_ENTRY(int, SomeFunc, ...) | |
45 // int return_value = 0; | |
46 // DT_RETURN_MARK(SomeFunc, int, (const int&)return_value); | |
47 // foo(CHECK_0) | |
48 // return_value = 5; | |
49 // return return_value; | |
50 // JNI_END | |
51 #define DT_RETURN_MARK_DECL(name, type) \ | |
52 HS_DTRACE_PROBE_DECL1(hotspot_jni, name##__return, type); \ | |
53 DTRACE_ONLY( \ | |
54 class DTraceReturnProbeMark_##name { \ | |
55 public: \ | |
56 const type& _ret_ref; \ | |
57 DTraceReturnProbeMark_##name(const type& v) : _ret_ref(v) {} \ | |
58 ~DTraceReturnProbeMark_##name() { \ | |
59 HS_DTRACE_PROBE1(hotspot_jni, name##__return, _ret_ref); \ | |
60 } \ | |
61 } \ | |
62 ) | |
63 // Void functions are simpler since there's no return value | |
64 #define DT_VOID_RETURN_MARK_DECL(name) \ | |
65 HS_DTRACE_PROBE_DECL0(hotspot_jni, name##__return); \ | |
66 DTRACE_ONLY( \ | |
67 class DTraceReturnProbeMark_##name { \ | |
68 public: \ | |
69 ~DTraceReturnProbeMark_##name() { \ | |
70 HS_DTRACE_PROBE0(hotspot_jni, name##__return); \ | |
71 } \ | |
72 } \ | |
73 ) | |
74 | |
75 | |
76 // Place these macros in the function to mark the return. Non-void | |
77 // functions need the type and address of the return value. | |
78 #define DT_RETURN_MARK(name, type, ref) \ | |
79 DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark(ref) ) | |
80 #define DT_VOID_RETURN_MARK(name) \ | |
81 DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark ) | |
82 | |
83 | |
84 // Use these to select distinct code for floating-point vs. non-floating point | |
85 // situations. Used from within common macros where we need slightly | |
86 // different behavior for Float/Double | |
87 #define FP_SELECT_Boolean(intcode, fpcode) intcode | |
88 #define FP_SELECT_Byte(intcode, fpcode) intcode | |
89 #define FP_SELECT_Char(intcode, fpcode) intcode | |
90 #define FP_SELECT_Short(intcode, fpcode) intcode | |
91 #define FP_SELECT_Object(intcode, fpcode) intcode | |
92 #define FP_SELECT_Int(intcode, fpcode) intcode | |
93 #define FP_SELECT_Long(intcode, fpcode) intcode | |
94 #define FP_SELECT_Float(intcode, fpcode) fpcode | |
95 #define FP_SELECT_Double(intcode, fpcode) fpcode | |
96 #define FP_SELECT(TypeName, intcode, fpcode) \ | |
97 FP_SELECT_##TypeName(intcode, fpcode) | |
98 | |
99 #define COMMA , | |
100 | |
101 // Choose DT_RETURN_MARK macros based on the type: float/double -> void | |
102 // (dtrace doesn't do FP yet) | |
103 #define DT_RETURN_MARK_DECL_FOR(TypeName, name, type) \ | |
104 FP_SELECT(TypeName, \ | |
105 DT_RETURN_MARK_DECL(name, type), DT_VOID_RETURN_MARK_DECL(name) ) | |
106 #define DT_RETURN_MARK_FOR(TypeName, name, type, ref) \ | |
107 FP_SELECT(TypeName, \ | |
108 DT_RETURN_MARK(name, type, ref), DT_VOID_RETURN_MARK(name) ) | |
109 | |
110 | |
111 // out-of-line helpers for class jfieldIDWorkaround: | |
112 | |
113 bool jfieldIDWorkaround::is_valid_jfieldID(klassOop k, jfieldID id) { | |
114 if (jfieldIDWorkaround::is_instance_jfieldID(k, id)) { | |
115 uintptr_t as_uint = (uintptr_t) id; | |
116 intptr_t offset = raw_instance_offset(id); | |
117 if (is_checked_jfieldID(id)) { | |
118 if (!klass_hash_ok(k, id)) { | |
119 return false; | |
120 } | |
121 } | |
122 return instanceKlass::cast(k)->contains_field_offset(offset); | |
123 } else { | |
124 JNIid* result = (JNIid*) id; | |
125 #ifdef ASSERT | |
126 return result != NULL && result->is_static_field_id(); | |
127 #else | |
128 return result != NULL; | |
129 #endif | |
130 } | |
131 } | |
132 | |
133 | |
134 intptr_t jfieldIDWorkaround::encode_klass_hash(klassOop k, intptr_t offset) { | |
135 if (offset <= small_offset_mask) { | |
136 klassOop field_klass = k; | |
137 klassOop super_klass = Klass::cast(field_klass)->super(); | |
113
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
138 // With compressed oops the most super class with nonstatic fields would |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
139 // be the owner of fields embedded in the header. |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
140 while (instanceKlass::cast(super_klass)->has_nonstatic_fields() && |
ba764ed4b6f2
6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents:
0
diff
changeset
|
141 instanceKlass::cast(super_klass)->contains_field_offset(offset)) { |
0 | 142 field_klass = super_klass; // super contains the field also |
143 super_klass = Klass::cast(field_klass)->super(); | |
144 } | |
145 debug_only(No_Safepoint_Verifier nosafepoint;) | |
146 uintptr_t klass_hash = field_klass->identity_hash(); | |
147 return ((klass_hash & klass_mask) << klass_shift) | checked_mask_in_place; | |
148 } else { | |
149 #if 0 | |
150 #ifndef PRODUCT | |
151 { | |
152 ResourceMark rm; | |
153 warning("VerifyJNIFields: long offset %d in %s", offset, Klass::cast(k)->external_name()); | |
154 } | |
155 #endif | |
156 #endif | |
157 return 0; | |
158 } | |
159 } | |
160 | |
161 bool jfieldIDWorkaround::klass_hash_ok(klassOop k, jfieldID id) { | |
162 uintptr_t as_uint = (uintptr_t) id; | |
163 intptr_t klass_hash = (as_uint >> klass_shift) & klass_mask; | |
164 do { | |
165 debug_only(No_Safepoint_Verifier nosafepoint;) | |
166 // Could use a non-blocking query for identity_hash here... | |
167 if ((k->identity_hash() & klass_mask) == klass_hash) | |
168 return true; | |
169 k = Klass::cast(k)->super(); | |
170 } while (k != NULL); | |
171 return false; | |
172 } | |
173 | |
174 void jfieldIDWorkaround::verify_instance_jfieldID(klassOop k, jfieldID id) { | |
175 guarantee(jfieldIDWorkaround::is_instance_jfieldID(k, id), "must be an instance field" ); | |
176 uintptr_t as_uint = (uintptr_t) id; | |
177 intptr_t offset = raw_instance_offset(id); | |
178 if (VerifyJNIFields) { | |
179 if (is_checked_jfieldID(id)) { | |
180 guarantee(klass_hash_ok(k, id), | |
181 "Bug in native code: jfieldID class must match object"); | |
182 } else { | |
183 #if 0 | |
184 #ifndef PRODUCT | |
185 if (Verbose) { | |
186 ResourceMark rm; | |
187 warning("VerifyJNIFields: unverified offset %d for %s", offset, Klass::cast(k)->external_name()); | |
188 } | |
189 #endif | |
190 #endif | |
191 } | |
192 } | |
193 guarantee(instanceKlass::cast(k)->contains_field_offset(offset), | |
194 "Bug in native code: jfieldID offset must address interior of object"); | |
195 } | |
196 | |
197 // Pick a reasonable higher bound for local capacity requested | |
198 // for EnsureLocalCapacity and PushLocalFrame. We don't want it too | |
199 // high because a test (or very unusual application) may try to allocate | |
200 // that many handles and run out of swap space. An implementation is | |
201 // permitted to allocate more handles than the ensured capacity, so this | |
202 // value is set high enough to prevent compatibility problems. | |
203 const int MAX_REASONABLE_LOCAL_CAPACITY = 4*K; | |
204 | |
205 | |
206 // Wrapper to trace JNI functions | |
207 | |
208 #ifdef ASSERT | |
209 Histogram* JNIHistogram; | |
210 static volatile jint JNIHistogram_lock = 0; | |
211 | |
212 class JNITraceWrapper : public StackObj { | |
213 public: | |
214 JNITraceWrapper(const char* format, ...) { | |
215 if (TraceJNICalls) { | |
216 va_list ap; | |
217 va_start(ap, format); | |
218 tty->print("JNI "); | |
219 tty->vprint_cr(format, ap); | |
220 va_end(ap); | |
221 } | |
222 } | |
223 }; | |
224 | |
225 class JNIHistogramElement : public HistogramElement { | |
226 public: | |
227 JNIHistogramElement(const char* name); | |
228 }; | |
229 | |
230 JNIHistogramElement::JNIHistogramElement(const char* elementName) { | |
231 _name = elementName; | |
232 uintx count = 0; | |
233 | |
234 while (Atomic::cmpxchg(1, &JNIHistogram_lock, 0) != 0) { | |
235 while (OrderAccess::load_acquire(&JNIHistogram_lock) != 0) { | |
236 count +=1; | |
237 if ( (WarnOnStalledSpinLock > 0) | |
238 && (count % WarnOnStalledSpinLock == 0)) { | |
239 warning("JNIHistogram_lock seems to be stalled"); | |
240 } | |
241 } | |
242 } | |
243 | |
244 | |
245 if(JNIHistogram == NULL) | |
246 JNIHistogram = new Histogram("JNI Call Counts",100); | |
247 | |
248 JNIHistogram->add_element(this); | |
249 Atomic::dec(&JNIHistogram_lock); | |
250 } | |
251 | |
252 #define JNICountWrapper(arg) \ | |
253 static JNIHistogramElement* e = new JNIHistogramElement(arg); \ | |
254 /* There is a MT-race condition in VC++. So we need to make sure that that e has been initialized */ \ | |
255 if (e != NULL) e->increment_count() | |
256 #define JNIWrapper(arg) JNICountWrapper(arg); JNITraceWrapper(arg) | |
257 #else | |
258 #define JNIWrapper(arg) | |
259 #endif | |
260 | |
261 | |
262 // Implementation of JNI entries | |
263 | |
264 DT_RETURN_MARK_DECL(DefineClass, jclass); | |
265 | |
266 JNI_ENTRY(jclass, jni_DefineClass(JNIEnv *env, const char *name, jobject loaderRef, | |
267 const jbyte *buf, jsize bufLen)) | |
268 JNIWrapper("DefineClass"); | |
269 | |
270 DTRACE_PROBE5(hotspot_jni, DefineClass__entry, | |
271 env, name, loaderRef, buf, bufLen); | |
272 jclass cls = NULL; | |
273 DT_RETURN_MARK(DefineClass, jclass, (const jclass&)cls); | |
274 | |
275 // Since exceptions can be thrown, class initialization can take place | |
276 // if name is NULL no check for class name in .class stream has to be made. | |
277 symbolHandle class_name; | |
278 if (name != NULL) { | |
279 const int str_len = (int)strlen(name); | |
280 if (str_len > symbolOopDesc::max_length()) { | |
281 // It's impossible to create this class; the name cannot fit | |
282 // into the constant pool. | |
283 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); | |
284 } | |
285 class_name = oopFactory::new_symbol_handle(name, str_len, CHECK_NULL); | |
286 } | |
287 | |
288 ResourceMark rm(THREAD); | |
289 ClassFileStream st((u1*) buf, bufLen, NULL); | |
290 Handle class_loader (THREAD, JNIHandles::resolve(loaderRef)); | |
291 | |
292 if (UsePerfData && !class_loader.is_null()) { | |
293 // check whether the current caller thread holds the lock or not. | |
294 // If not, increment the corresponding counter | |
295 if (ObjectSynchronizer:: | |
296 query_lock_ownership((JavaThread*)THREAD, class_loader) != | |
297 ObjectSynchronizer::owner_self) { | |
298 ClassLoader::sync_JNIDefineClassLockFreeCounter()->inc(); | |
299 } | |
300 } | |
301 klassOop k = SystemDictionary::resolve_from_stream(class_name, class_loader, | |
302 Handle(), &st, CHECK_NULL); | |
303 | |
304 cls = (jclass)JNIHandles::make_local( | |
305 env, Klass::cast(k)->java_mirror()); | |
306 return cls; | |
307 JNI_END | |
308 | |
309 | |
310 | |
311 static bool first_time_FindClass = true; | |
312 | |
313 DT_RETURN_MARK_DECL(FindClass, jclass); | |
314 | |
315 JNI_ENTRY(jclass, jni_FindClass(JNIEnv *env, const char *name)) | |
316 JNIWrapper("FindClass"); | |
317 DTRACE_PROBE2(hotspot_jni, FindClass__entry, env, name); | |
318 | |
319 jclass result = NULL; | |
320 DT_RETURN_MARK(FindClass, jclass, (const jclass&)result); | |
321 | |
322 // Remember if we are the first invocation of jni_FindClass | |
323 bool first_time = first_time_FindClass; | |
324 first_time_FindClass = false; | |
325 | |
326 // Sanity check the name: it cannot be null or larger than the maximum size | |
327 // name we can fit in the constant pool. | |
328 if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) { | |
329 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); | |
330 } | |
331 | |
332 //%note jni_3 | |
333 Handle loader; | |
334 Handle protection_domain; | |
335 // Find calling class | |
336 instanceKlassHandle k (THREAD, thread->security_get_caller_class(0)); | |
337 if (k.not_null()) { | |
338 loader = Handle(THREAD, k->class_loader()); | |
339 // Special handling to make sure JNI_OnLoad and JNI_OnUnload are executed | |
340 // in the correct class context. | |
341 if (loader.is_null() && | |
342 k->name() == vmSymbols::java_lang_ClassLoader_NativeLibrary()) { | |
343 JavaValue result(T_OBJECT); | |
344 JavaCalls::call_static(&result, k, | |
345 vmSymbolHandles::getFromClass_name(), | |
346 vmSymbolHandles::void_class_signature(), | |
347 thread); | |
348 if (HAS_PENDING_EXCEPTION) { | |
349 Handle ex(thread, thread->pending_exception()); | |
350 CLEAR_PENDING_EXCEPTION; | |
351 THROW_HANDLE_0(ex); | |
352 } | |
353 oop mirror = (oop) result.get_jobject(); | |
354 loader = Handle(THREAD, | |
355 instanceKlass::cast(java_lang_Class::as_klassOop(mirror))->class_loader()); | |
356 protection_domain = Handle(THREAD, | |
357 instanceKlass::cast(java_lang_Class::as_klassOop(mirror))->protection_domain()); | |
358 } | |
359 } else { | |
360 // We call ClassLoader.getSystemClassLoader to obtain the system class loader. | |
361 loader = Handle(THREAD, SystemDictionary::java_system_loader()); | |
362 } | |
363 | |
364 symbolHandle sym = oopFactory::new_symbol_handle(name, CHECK_NULL); | |
365 result = find_class_from_class_loader(env, sym, true, loader, | |
366 protection_domain, true, thread); | |
367 | |
368 // If we were the first invocation of jni_FindClass, we enable compilation again | |
369 // rather than just allowing invocation counter to overflow and decay. | |
370 // Controlled by flag DelayCompilationDuringStartup. | |
371 if (first_time && !CompileTheWorld) | |
372 CompilationPolicy::completed_vm_startup(); | |
373 | |
374 return result; | |
375 JNI_END | |
376 | |
377 DT_RETURN_MARK_DECL(FromReflectedMethod, jmethodID); | |
378 | |
379 JNI_ENTRY(jmethodID, jni_FromReflectedMethod(JNIEnv *env, jobject method)) | |
380 JNIWrapper("FromReflectedMethod"); | |
381 DTRACE_PROBE2(hotspot_jni, FromReflectedMethod__entry, env, method); | |
382 jmethodID ret = NULL; | |
383 DT_RETURN_MARK(FromReflectedMethod, jmethodID, (const jmethodID&)ret); | |
384 | |
385 // method is a handle to a java.lang.reflect.Method object | |
386 oop reflected = JNIHandles::resolve_non_null(method); | |
387 oop mirror = NULL; | |
388 int slot = 0; | |
389 | |
390 if (reflected->klass() == SystemDictionary::reflect_constructor_klass()) { | |
391 mirror = java_lang_reflect_Constructor::clazz(reflected); | |
392 slot = java_lang_reflect_Constructor::slot(reflected); | |
393 } else { | |
394 assert(reflected->klass() == SystemDictionary::reflect_method_klass(), "wrong type"); | |
395 mirror = java_lang_reflect_Method::clazz(reflected); | |
396 slot = java_lang_reflect_Method::slot(reflected); | |
397 } | |
398 klassOop k = java_lang_Class::as_klassOop(mirror); | |
399 | |
400 KlassHandle k1(THREAD, k); | |
401 // Make sure class is initialized before handing id's out to methods | |
402 Klass::cast(k1())->initialize(CHECK_NULL); | |
403 methodOop m = instanceKlass::cast(k1())->method_with_idnum(slot); | |
404 ret = m==NULL? NULL : m->jmethod_id(); // return NULL if reflected method deleted | |
405 return ret; | |
406 JNI_END | |
407 | |
408 DT_RETURN_MARK_DECL(FromReflectedField, jfieldID); | |
409 | |
410 JNI_ENTRY(jfieldID, jni_FromReflectedField(JNIEnv *env, jobject field)) | |
411 JNIWrapper("FromReflectedField"); | |
412 DTRACE_PROBE2(hotspot_jni, FromReflectedField__entry, env, field); | |
413 jfieldID ret = NULL; | |
414 DT_RETURN_MARK(FromReflectedField, jfieldID, (const jfieldID&)ret); | |
415 | |
416 // field is a handle to a java.lang.reflect.Field object | |
417 oop reflected = JNIHandles::resolve_non_null(field); | |
418 oop mirror = java_lang_reflect_Field::clazz(reflected); | |
419 klassOop k = java_lang_Class::as_klassOop(mirror); | |
420 int slot = java_lang_reflect_Field::slot(reflected); | |
421 int modifiers = java_lang_reflect_Field::modifiers(reflected); | |
422 | |
423 KlassHandle k1(THREAD, k); | |
424 // Make sure class is initialized before handing id's out to fields | |
425 Klass::cast(k1())->initialize(CHECK_NULL); | |
426 | |
427 // First check if this is a static field | |
428 if (modifiers & JVM_ACC_STATIC) { | |
429 intptr_t offset = instanceKlass::cast(k1())->offset_from_fields( slot ); | |
430 JNIid* id = instanceKlass::cast(k1())->jni_id_for(offset); | |
431 assert(id != NULL, "corrupt Field object"); | |
432 debug_only(id->set_is_static_field_id();) | |
433 // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the klassOop | |
434 ret = jfieldIDWorkaround::to_static_jfieldID(id); | |
435 return ret; | |
436 } | |
437 | |
438 // The slot is the index of the field description in the field-array | |
439 // The jfieldID is the offset of the field within the object | |
440 // It may also have hash bits for k, if VerifyJNIFields is turned on. | |
441 intptr_t offset = instanceKlass::cast(k1())->offset_from_fields( slot ); | |
442 assert(instanceKlass::cast(k1())->contains_field_offset(offset), "stay within object"); | |
443 ret = jfieldIDWorkaround::to_instance_jfieldID(k1(), offset); | |
444 return ret; | |
445 JNI_END | |
446 | |
447 DT_RETURN_MARK_DECL(ToReflectedMethod, jobject); | |
448 | |
449 JNI_ENTRY(jobject, jni_ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID method_id, jboolean isStatic)) | |
450 JNIWrapper("ToReflectedMethod"); | |
451 DTRACE_PROBE4(hotspot_jni, ToReflectedMethod__entry, env, cls, method_id, isStatic); | |
452 jobject ret = NULL; | |
453 DT_RETURN_MARK(ToReflectedMethod, jobject, (const jobject&)ret); | |
454 | |
455 methodHandle m (THREAD, JNIHandles::resolve_jmethod_id(method_id)); | |
456 assert(m->is_static() == (isStatic != 0), "jni_ToReflectedMethod access flags doesn't match"); | |
457 oop reflection_method; | |
458 if (m->is_initializer()) { | |
459 reflection_method = Reflection::new_constructor(m, CHECK_NULL); | |
460 } else { | |
461 reflection_method = Reflection::new_method(m, UseNewReflection, false, CHECK_NULL); | |
462 } | |
463 ret = JNIHandles::make_local(env, reflection_method); | |
464 return ret; | |
465 JNI_END | |
466 | |
467 DT_RETURN_MARK_DECL(GetSuperclass, jclass); | |
468 | |
469 JNI_ENTRY(jclass, jni_GetSuperclass(JNIEnv *env, jclass sub)) | |
470 JNIWrapper("GetSuperclass"); | |
471 DTRACE_PROBE2(hotspot_jni, GetSuperclass__entry, env, sub); | |
472 jclass obj = NULL; | |
473 DT_RETURN_MARK(GetSuperclass, jclass, (const jclass&)obj); | |
474 | |
475 oop mirror = JNIHandles::resolve_non_null(sub); | |
476 // primitive classes return NULL | |
477 if (java_lang_Class::is_primitive(mirror)) return NULL; | |
478 | |
479 // Rules of Class.getSuperClass as implemented by KLass::java_super: | |
480 // arrays return Object | |
481 // interfaces return NULL | |
482 // proper classes return Klass::super() | |
483 klassOop k = java_lang_Class::as_klassOop(mirror); | |
484 if (Klass::cast(k)->is_interface()) return NULL; | |
485 | |
486 // return mirror for superclass | |
487 klassOop super = Klass::cast(k)->java_super(); | |
488 // super2 is the value computed by the compiler's getSuperClass intrinsic: | |
489 debug_only(klassOop super2 = ( Klass::cast(k)->oop_is_javaArray() | |
490 ? SystemDictionary::object_klass() | |
491 : Klass::cast(k)->super() ) ); | |
492 assert(super == super2, | |
493 "java_super computation depends on interface, array, other super"); | |
494 obj = (super == NULL) ? NULL : (jclass) JNIHandles::make_local(Klass::cast(super)->java_mirror()); | |
495 return obj; | |
496 JNI_END | |
497 | |
498 JNI_QUICK_ENTRY(jboolean, jni_IsAssignableFrom(JNIEnv *env, jclass sub, jclass super)) | |
499 JNIWrapper("IsSubclassOf"); | |
500 DTRACE_PROBE3(hotspot_jni, IsAssignableFrom__entry, env, sub, super); | |
501 oop sub_mirror = JNIHandles::resolve_non_null(sub); | |
502 oop super_mirror = JNIHandles::resolve_non_null(super); | |
503 if (java_lang_Class::is_primitive(sub_mirror) || | |
504 java_lang_Class::is_primitive(super_mirror)) { | |
505 jboolean ret = (sub_mirror == super_mirror); | |
506 DTRACE_PROBE1(hotspot_jni, IsAssignableFrom__return, ret); | |
507 return ret; | |
508 } | |
509 klassOop sub_klass = java_lang_Class::as_klassOop(sub_mirror); | |
510 klassOop super_klass = java_lang_Class::as_klassOop(super_mirror); | |
511 assert(sub_klass != NULL && super_klass != NULL, "invalid arguments to jni_IsAssignableFrom"); | |
512 jboolean ret = Klass::cast(sub_klass)->is_subtype_of(super_klass) ? | |
513 JNI_TRUE : JNI_FALSE; | |
514 DTRACE_PROBE1(hotspot_jni, IsAssignableFrom__return, ret); | |
515 return ret; | |
516 JNI_END | |
517 | |
518 DT_RETURN_MARK_DECL(Throw, jint); | |
519 | |
520 JNI_ENTRY(jint, jni_Throw(JNIEnv *env, jthrowable obj)) | |
521 JNIWrapper("Throw"); | |
522 DTRACE_PROBE2(hotspot_jni, Throw__entry, env, obj); | |
523 jint ret = JNI_OK; | |
524 DT_RETURN_MARK(Throw, jint, (const jint&)ret); | |
525 | |
526 THROW_OOP_(JNIHandles::resolve(obj), JNI_OK); | |
527 ShouldNotReachHere(); | |
528 JNI_END | |
529 | |
530 DT_RETURN_MARK_DECL(ThrowNew, jint); | |
531 | |
532 JNI_ENTRY(jint, jni_ThrowNew(JNIEnv *env, jclass clazz, const char *message)) | |
533 JNIWrapper("ThrowNew"); | |
534 DTRACE_PROBE3(hotspot_jni, ThrowNew__entry, env, clazz, message); | |
535 jint ret = JNI_OK; | |
536 DT_RETURN_MARK(ThrowNew, jint, (const jint&)ret); | |
537 | |
538 instanceKlass* k = instanceKlass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz))); | |
539 symbolHandle name = symbolHandle(THREAD, k->name()); | |
540 Handle class_loader (THREAD, k->class_loader()); | |
541 Handle protection_domain (THREAD, k->protection_domain()); | |
542 THROW_MSG_LOADER_(name, (char *)message, class_loader, protection_domain, JNI_OK); | |
543 ShouldNotReachHere(); | |
544 JNI_END | |
545 | |
546 | |
547 // JNI functions only transform a pending async exception to a synchronous | |
548 // exception in ExceptionOccurred and ExceptionCheck calls, since | |
549 // delivering an async exception in other places won't change the native | |
550 // code's control flow and would be harmful when native code further calls | |
551 // JNI functions with a pending exception. Async exception is also checked | |
552 // during the call, so ExceptionOccurred/ExceptionCheck won't return | |
553 // false but deliver the async exception at the very end during | |
554 // state transition. | |
555 | |
556 static void jni_check_async_exceptions(JavaThread *thread) { | |
557 assert(thread == Thread::current(), "must be itself"); | |
558 thread->check_and_handle_async_exceptions(); | |
559 } | |
560 | |
561 JNI_ENTRY_NO_PRESERVE(jthrowable, jni_ExceptionOccurred(JNIEnv *env)) | |
562 JNIWrapper("ExceptionOccurred"); | |
563 DTRACE_PROBE1(hotspot_jni, ExceptionOccurred__entry, env); | |
564 jni_check_async_exceptions(thread); | |
565 oop exception = thread->pending_exception(); | |
566 jthrowable ret = (jthrowable) JNIHandles::make_local(env, exception); | |
567 DTRACE_PROBE1(hotspot_jni, ExceptionOccurred__return, ret); | |
568 return ret; | |
569 JNI_END | |
570 | |
571 | |
572 JNI_ENTRY_NO_PRESERVE(void, jni_ExceptionDescribe(JNIEnv *env)) | |
573 JNIWrapper("ExceptionDescribe"); | |
574 DTRACE_PROBE1(hotspot_jni, ExceptionDescribe__entry, env); | |
575 if (thread->has_pending_exception()) { | |
576 Handle ex(thread, thread->pending_exception()); | |
577 thread->clear_pending_exception(); | |
578 if (ex->is_a(SystemDictionary::threaddeath_klass())) { | |
579 // Don't print anything if we are being killed. | |
580 } else { | |
581 jio_fprintf(defaultStream::error_stream(), "Exception "); | |
582 if (thread != NULL && thread->threadObj() != NULL) { | |
583 ResourceMark rm(THREAD); | |
584 jio_fprintf(defaultStream::error_stream(), | |
585 "in thread \"%s\" ", thread->get_thread_name()); | |
586 } | |
587 if (ex->is_a(SystemDictionary::throwable_klass())) { | |
588 JavaValue result(T_VOID); | |
589 JavaCalls::call_virtual(&result, | |
590 ex, | |
591 KlassHandle(THREAD, | |
592 SystemDictionary::throwable_klass()), | |
593 vmSymbolHandles::printStackTrace_name(), | |
594 vmSymbolHandles::void_method_signature(), | |
595 THREAD); | |
596 // If an exception is thrown in the call it gets thrown away. Not much | |
597 // we can do with it. The native code that calls this, does not check | |
598 // for the exception - hence, it might still be in the thread when DestroyVM gets | |
599 // called, potentially causing a few asserts to trigger - since no pending exception | |
600 // is expected. | |
601 CLEAR_PENDING_EXCEPTION; | |
602 } else { | |
603 ResourceMark rm(THREAD); | |
604 jio_fprintf(defaultStream::error_stream(), | |
605 ". Uncaught exception of type %s.", | |
606 Klass::cast(ex->klass())->external_name()); | |
607 } | |
608 } | |
609 } | |
610 DTRACE_PROBE(hotspot_jni, ExceptionDescribe__return); | |
611 JNI_END | |
612 | |
613 | |
614 JNI_QUICK_ENTRY(void, jni_ExceptionClear(JNIEnv *env)) | |
615 JNIWrapper("ExceptionClear"); | |
616 DTRACE_PROBE1(hotspot_jni, ExceptionClear__entry, env); | |
617 | |
618 // The jni code might be using this API to clear java thrown exception. | |
619 // So just mark jvmti thread exception state as exception caught. | |
620 JvmtiThreadState *state = JavaThread::current()->jvmti_thread_state(); | |
621 if (state != NULL && state->is_exception_detected()) { | |
622 state->set_exception_caught(); | |
623 } | |
624 thread->clear_pending_exception(); | |
625 DTRACE_PROBE(hotspot_jni, ExceptionClear__return); | |
626 JNI_END | |
627 | |
628 | |
629 JNI_ENTRY(void, jni_FatalError(JNIEnv *env, const char *msg)) | |
630 JNIWrapper("FatalError"); | |
631 DTRACE_PROBE2(hotspot_jni, FatalError__entry, env, msg); | |
632 tty->print_cr("FATAL ERROR in native method: %s", msg); | |
633 thread->print_stack(); | |
227
8d852b81e775
6694099: Hotspot vm_exit_out_of_memory should dump core
poonam
parents:
113
diff
changeset
|
634 os::abort(); // Dump core and abort |
0 | 635 JNI_END |
636 | |
637 | |
638 JNI_ENTRY(jint, jni_PushLocalFrame(JNIEnv *env, jint capacity)) | |
639 JNIWrapper("PushLocalFrame"); | |
640 DTRACE_PROBE2(hotspot_jni, PushLocalFrame__entry, env, capacity); | |
641 //%note jni_11 | |
642 if (capacity < 0 && capacity > MAX_REASONABLE_LOCAL_CAPACITY) { | |
643 DTRACE_PROBE1(hotspot_jni, PushLocalFrame__return, JNI_ERR); | |
644 return JNI_ERR; | |
645 } | |
646 JNIHandleBlock* old_handles = thread->active_handles(); | |
647 JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread); | |
648 assert(new_handles != NULL, "should not be NULL"); | |
649 new_handles->set_pop_frame_link(old_handles); | |
650 thread->set_active_handles(new_handles); | |
651 jint ret = JNI_OK; | |
652 DTRACE_PROBE1(hotspot_jni, PushLocalFrame__return, ret); | |
653 return ret; | |
654 JNI_END | |
655 | |
656 | |
657 JNI_ENTRY(jobject, jni_PopLocalFrame(JNIEnv *env, jobject result)) | |
658 JNIWrapper("PopLocalFrame"); | |
659 DTRACE_PROBE2(hotspot_jni, PopLocalFrame__entry, env, result); | |
660 //%note jni_11 | |
661 Handle result_handle(thread, JNIHandles::resolve(result)); | |
662 JNIHandleBlock* old_handles = thread->active_handles(); | |
663 JNIHandleBlock* new_handles = old_handles->pop_frame_link(); | |
664 if (new_handles != NULL) { | |
665 // As a sanity check we only release the handle blocks if the pop_frame_link is not NULL. | |
666 // This way code will still work if PopLocalFrame is called without a corresponding | |
667 // PushLocalFrame call. Note that we set the pop_frame_link to NULL explicitly, otherwise | |
668 // the release_block call will release the blocks. | |
669 thread->set_active_handles(new_handles); | |
670 old_handles->set_pop_frame_link(NULL); // clear link we won't release new_handles below | |
671 JNIHandleBlock::release_block(old_handles, thread); // may block | |
672 result = JNIHandles::make_local(thread, result_handle()); | |
673 } | |
674 DTRACE_PROBE1(hotspot_jni, PopLocalFrame__return, result); | |
675 return result; | |
676 JNI_END | |
677 | |
678 | |
679 JNI_ENTRY(jobject, jni_NewGlobalRef(JNIEnv *env, jobject ref)) | |
680 JNIWrapper("NewGlobalRef"); | |
681 DTRACE_PROBE2(hotspot_jni, NewGlobalRef__entry, env, ref); | |
682 Handle ref_handle(thread, JNIHandles::resolve(ref)); | |
683 jobject ret = JNIHandles::make_global(ref_handle); | |
684 DTRACE_PROBE1(hotspot_jni, NewGlobalRef__return, ret); | |
685 return ret; | |
686 JNI_END | |
687 | |
688 // Must be JNI_ENTRY (with HandleMark) | |
689 JNI_ENTRY_NO_PRESERVE(void, jni_DeleteGlobalRef(JNIEnv *env, jobject ref)) | |
690 JNIWrapper("DeleteGlobalRef"); | |
691 DTRACE_PROBE2(hotspot_jni, DeleteGlobalRef__entry, env, ref); | |
692 JNIHandles::destroy_global(ref); | |
693 DTRACE_PROBE(hotspot_jni, DeleteGlobalRef__return); | |
694 JNI_END | |
695 | |
696 JNI_QUICK_ENTRY(void, jni_DeleteLocalRef(JNIEnv *env, jobject obj)) | |
697 JNIWrapper("DeleteLocalRef"); | |
698 DTRACE_PROBE2(hotspot_jni, DeleteLocalRef__entry, env, obj); | |
699 JNIHandles::destroy_local(obj); | |
700 DTRACE_PROBE(hotspot_jni, DeleteLocalRef__return); | |
701 JNI_END | |
702 | |
703 JNI_QUICK_ENTRY(jboolean, jni_IsSameObject(JNIEnv *env, jobject r1, jobject r2)) | |
704 JNIWrapper("IsSameObject"); | |
705 DTRACE_PROBE3(hotspot_jni, IsSameObject__entry, env, r1, r2); | |
706 oop a = JNIHandles::resolve(r1); | |
707 oop b = JNIHandles::resolve(r2); | |
708 jboolean ret = (a == b) ? JNI_TRUE : JNI_FALSE; | |
709 DTRACE_PROBE1(hotspot_jni, IsSameObject__return, ret); | |
710 return ret; | |
711 JNI_END | |
712 | |
713 | |
714 JNI_ENTRY(jobject, jni_NewLocalRef(JNIEnv *env, jobject ref)) | |
715 JNIWrapper("NewLocalRef"); | |
716 DTRACE_PROBE2(hotspot_jni, NewLocalRef__entry, env, ref); | |
717 jobject ret = JNIHandles::make_local(env, JNIHandles::resolve(ref)); | |
718 DTRACE_PROBE1(hotspot_jni, NewLocalRef__return, ret); | |
719 return ret; | |
720 JNI_END | |
721 | |
722 JNI_LEAF(jint, jni_EnsureLocalCapacity(JNIEnv *env, jint capacity)) | |
723 JNIWrapper("EnsureLocalCapacity"); | |
724 DTRACE_PROBE2(hotspot_jni, EnsureLocalCapacity__entry, env, capacity); | |
725 jint ret; | |
726 if (capacity >= 0 && capacity <= MAX_REASONABLE_LOCAL_CAPACITY) { | |
727 ret = JNI_OK; | |
728 } else { | |
729 ret = JNI_ERR; | |
730 } | |
731 DTRACE_PROBE1(hotspot_jni, EnsureLocalCapacity__return, ret); | |
732 return ret; | |
733 JNI_END | |
734 | |
735 // Return the Handle Type | |
736 JNI_LEAF(jobjectRefType, jni_GetObjectRefType(JNIEnv *env, jobject obj)) | |
737 JNIWrapper("GetObjectRefType"); | |
738 DTRACE_PROBE2(hotspot_jni, GetObjectRefType__entry, env, obj); | |
739 jobjectRefType ret; | |
740 if (JNIHandles::is_local_handle(thread, obj) || | |
741 JNIHandles::is_frame_handle(thread, obj)) | |
742 ret = JNILocalRefType; | |
743 else if (JNIHandles::is_global_handle(obj)) | |
744 ret = JNIGlobalRefType; | |
745 else if (JNIHandles::is_weak_global_handle(obj)) | |
746 ret = JNIWeakGlobalRefType; | |
747 else | |
748 ret = JNIInvalidRefType; | |
749 DTRACE_PROBE1(hotspot_jni, GetObjectRefType__return, ret); | |
750 return ret; | |
751 JNI_END | |
752 | |
753 | |
754 class JNI_ArgumentPusher : public SignatureIterator { | |
755 protected: | |
756 JavaCallArguments* _arguments; | |
757 | |
758 virtual void get_bool () = 0; | |
759 virtual void get_char () = 0; | |
760 virtual void get_short () = 0; | |
761 virtual void get_byte () = 0; | |
762 virtual void get_int () = 0; | |
763 virtual void get_long () = 0; | |
764 virtual void get_float () = 0; | |
765 virtual void get_double () = 0; | |
766 virtual void get_object () = 0; | |
767 | |
768 JNI_ArgumentPusher(Thread *thread, symbolOop signature) | |
769 : SignatureIterator(thread, signature) { | |
770 this->_return_type = T_ILLEGAL; | |
771 _arguments = NULL; | |
772 } | |
773 | |
774 public: | |
775 virtual void iterate( uint64_t fingerprint ) = 0; | |
776 | |
777 void set_java_argument_object(JavaCallArguments *arguments) { _arguments = arguments; } | |
778 | |
779 inline void do_bool() { if (!is_return_type()) get_bool(); } | |
780 inline void do_char() { if (!is_return_type()) get_char(); } | |
781 inline void do_short() { if (!is_return_type()) get_short(); } | |
782 inline void do_byte() { if (!is_return_type()) get_byte(); } | |
783 inline void do_int() { if (!is_return_type()) get_int(); } | |
784 inline void do_long() { if (!is_return_type()) get_long(); } | |
785 inline void do_float() { if (!is_return_type()) get_float(); } | |
786 inline void do_double() { if (!is_return_type()) get_double(); } | |
787 inline void do_object(int begin, int end) { if (!is_return_type()) get_object(); } | |
788 inline void do_array(int begin, int end) { if (!is_return_type()) get_object(); } // do_array uses get_object -- there is no get_array | |
789 inline void do_void() { } | |
790 | |
791 JavaCallArguments* arguments() { return _arguments; } | |
792 void push_receiver(Handle h) { _arguments->push_oop(h); } | |
793 }; | |
794 | |
795 | |
796 class JNI_ArgumentPusherVaArg : public JNI_ArgumentPusher { | |
797 protected: | |
798 va_list _ap; | |
799 | |
800 inline void get_bool() { _arguments->push_int(va_arg(_ap, jint)); } // bool is coerced to int when using va_arg | |
801 inline void get_char() { _arguments->push_int(va_arg(_ap, jint)); } // char is coerced to int when using va_arg | |
802 inline void get_short() { _arguments->push_int(va_arg(_ap, jint)); } // short is coerced to int when using va_arg | |
803 inline void get_byte() { _arguments->push_int(va_arg(_ap, jint)); } // byte is coerced to int when using va_arg | |
804 inline void get_int() { _arguments->push_int(va_arg(_ap, jint)); } | |
805 | |
806 // each of these paths is exercized by the various jck Call[Static,Nonvirtual,][Void,Int,..]Method[A,V,] tests | |
807 | |
808 inline void get_long() { _arguments->push_long(va_arg(_ap, jlong)); } | |
809 inline void get_float() { _arguments->push_float((jfloat)va_arg(_ap, jdouble)); } // float is coerced to double w/ va_arg | |
810 inline void get_double() { _arguments->push_double(va_arg(_ap, jdouble)); } | |
811 inline void get_object() { jobject l = va_arg(_ap, jobject); | |
812 _arguments->push_oop(Handle((oop *)l, false)); } | |
813 | |
814 inline void set_ap(va_list rap) { | |
815 #ifdef va_copy | |
816 va_copy(_ap, rap); | |
817 #elif defined (__va_copy) | |
818 __va_copy(_ap, rap); | |
819 #else | |
820 _ap = rap; | |
821 #endif | |
822 } | |
823 | |
824 public: | |
825 JNI_ArgumentPusherVaArg(Thread *thread, symbolOop signature, va_list rap) | |
826 : JNI_ArgumentPusher(thread, signature) { | |
827 set_ap(rap); | |
828 } | |
829 JNI_ArgumentPusherVaArg(Thread *thread, jmethodID method_id, va_list rap) | |
830 : JNI_ArgumentPusher(thread, JNIHandles::resolve_jmethod_id(method_id)->signature()) { | |
831 set_ap(rap); | |
832 } | |
833 | |
834 // Optimized path if we have the bitvector form of signature | |
835 void iterate( uint64_t fingerprint ) { | |
836 if ( fingerprint == UCONST64(-1) ) SignatureIterator::iterate();// Must be too many arguments | |
837 else { | |
838 _return_type = (BasicType)((fingerprint >> static_feature_size) & | |
839 result_feature_mask); | |
840 | |
841 assert(fingerprint, "Fingerprint should not be 0"); | |
842 fingerprint = fingerprint >> (static_feature_size + result_feature_size); | |
843 while ( 1 ) { | |
844 switch ( fingerprint & parameter_feature_mask ) { | |
845 case bool_parm: | |
846 case char_parm: | |
847 case short_parm: | |
848 case byte_parm: | |
849 case int_parm: | |
850 get_int(); | |
851 break; | |
852 case obj_parm: | |
853 get_object(); | |
854 break; | |
855 case long_parm: | |
856 get_long(); | |
857 break; | |
858 case float_parm: | |
859 get_float(); | |
860 break; | |
861 case double_parm: | |
862 get_double(); | |
863 break; | |
864 case done_parm: | |
865 return; | |
866 break; | |
867 default: | |
868 ShouldNotReachHere(); | |
869 break; | |
870 } | |
871 fingerprint >>= parameter_feature_size; | |
872 } | |
873 } | |
874 } | |
875 }; | |
876 | |
877 | |
878 class JNI_ArgumentPusherArray : public JNI_ArgumentPusher { | |
879 protected: | |
880 const jvalue *_ap; | |
881 | |
882 inline void get_bool() { _arguments->push_int((jint)(_ap++)->z); } | |
883 inline void get_char() { _arguments->push_int((jint)(_ap++)->c); } | |
884 inline void get_short() { _arguments->push_int((jint)(_ap++)->s); } | |
885 inline void get_byte() { _arguments->push_int((jint)(_ap++)->b); } | |
886 inline void get_int() { _arguments->push_int((jint)(_ap++)->i); } | |
887 | |
888 inline void get_long() { _arguments->push_long((_ap++)->j); } | |
889 inline void get_float() { _arguments->push_float((_ap++)->f); } | |
890 inline void get_double() { _arguments->push_double((_ap++)->d);} | |
891 inline void get_object() { _arguments->push_oop(Handle((oop *)(_ap++)->l, false)); } | |
892 | |
893 inline void set_ap(const jvalue *rap) { _ap = rap; } | |
894 | |
895 public: | |
896 JNI_ArgumentPusherArray(Thread *thread, symbolOop signature, const jvalue *rap) | |
897 : JNI_ArgumentPusher(thread, signature) { | |
898 set_ap(rap); | |
899 } | |
900 JNI_ArgumentPusherArray(Thread *thread, jmethodID method_id, const jvalue *rap) | |
901 : JNI_ArgumentPusher(thread, JNIHandles::resolve_jmethod_id(method_id)->signature()) { | |
902 set_ap(rap); | |
903 } | |
904 | |
905 // Optimized path if we have the bitvector form of signature | |
906 void iterate( uint64_t fingerprint ) { | |
907 if ( fingerprint == UCONST64(-1) ) SignatureIterator::iterate(); // Must be too many arguments | |
908 else { | |
909 _return_type = (BasicType)((fingerprint >> static_feature_size) & | |
910 result_feature_mask); | |
911 assert(fingerprint, "Fingerprint should not be 0"); | |
912 fingerprint = fingerprint >> (static_feature_size + result_feature_size); | |
913 while ( 1 ) { | |
914 switch ( fingerprint & parameter_feature_mask ) { | |
915 case bool_parm: | |
916 get_bool(); | |
917 break; | |
918 case char_parm: | |
919 get_char(); | |
920 break; | |
921 case short_parm: | |
922 get_short(); | |
923 break; | |
924 case byte_parm: | |
925 get_byte(); | |
926 break; | |
927 case int_parm: | |
928 get_int(); | |
929 break; | |
930 case obj_parm: | |
931 get_object(); | |
932 break; | |
933 case long_parm: | |
934 get_long(); | |
935 break; | |
936 case float_parm: | |
937 get_float(); | |
938 break; | |
939 case double_parm: | |
940 get_double(); | |
941 break; | |
942 case done_parm: | |
943 return; | |
944 break; | |
945 default: | |
946 ShouldNotReachHere(); | |
947 break; | |
948 } | |
949 fingerprint >>= parameter_feature_size; | |
950 } | |
951 } | |
952 } | |
953 }; | |
954 | |
955 | |
956 enum JNICallType { | |
957 JNI_STATIC, | |
958 JNI_VIRTUAL, | |
959 JNI_NONVIRTUAL | |
960 }; | |
961 | |
962 static methodHandle jni_resolve_interface_call(Handle recv, methodHandle method, TRAPS) { | |
963 assert(!method.is_null() , "method should not be null"); | |
964 | |
965 KlassHandle recv_klass; // Default to NULL (use of ?: can confuse gcc) | |
966 if (recv.not_null()) recv_klass = KlassHandle(THREAD, recv->klass()); | |
967 KlassHandle spec_klass (THREAD, method->method_holder()); | |
968 symbolHandle name (THREAD, method->name()); | |
969 symbolHandle signature (THREAD, method->signature()); | |
970 CallInfo info; | |
971 LinkResolver::resolve_interface_call(info, recv, recv_klass, spec_klass, name, signature, KlassHandle(), false, true, CHECK_(methodHandle())); | |
972 return info.selected_method(); | |
973 } | |
974 | |
975 static methodHandle jni_resolve_virtual_call(Handle recv, methodHandle method, TRAPS) { | |
976 assert(!method.is_null() , "method should not be null"); | |
977 | |
978 KlassHandle recv_klass; // Default to NULL (use of ?: can confuse gcc) | |
979 if (recv.not_null()) recv_klass = KlassHandle(THREAD, recv->klass()); | |
980 KlassHandle spec_klass (THREAD, method->method_holder()); | |
981 symbolHandle name (THREAD, method->name()); | |
982 symbolHandle signature (THREAD, method->signature()); | |
983 CallInfo info; | |
984 LinkResolver::resolve_virtual_call(info, recv, recv_klass, spec_klass, name, signature, KlassHandle(), false, true, CHECK_(methodHandle())); | |
985 return info.selected_method(); | |
986 } | |
987 | |
988 | |
989 | |
990 static void jni_invoke_static(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) { | |
991 methodHandle method(THREAD, JNIHandles::resolve_jmethod_id(method_id)); | |
992 | |
993 // Create object to hold arguments for the JavaCall, and associate it with | |
994 // the jni parser | |
995 ResourceMark rm(THREAD); | |
996 int number_of_parameters = method->size_of_parameters(); | |
997 JavaCallArguments java_args(number_of_parameters); | |
998 args->set_java_argument_object(&java_args); | |
999 | |
1000 assert(method->is_static(), "method should be static"); | |
1001 | |
1002 // Fill out JavaCallArguments object | |
1003 args->iterate( Fingerprinter(THREAD, method).fingerprint() ); | |
1004 // Initialize result type | |
1005 result->set_type(args->get_ret_type()); | |
1006 | |
1007 // Invoke the method. Result is returned as oop. | |
1008 JavaCalls::call(result, method, &java_args, CHECK); | |
1009 | |
1010 // Convert result | |
1011 if (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY) { | |
1012 result->set_jobject(JNIHandles::make_local(env, (oop) result->get_jobject())); | |
1013 } | |
1014 } | |
1015 | |
1016 | |
1017 static void jni_invoke_nonstatic(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) { | |
1018 oop recv = JNIHandles::resolve(receiver); | |
1019 if (recv == NULL) { | |
1020 THROW(vmSymbols::java_lang_NullPointerException()); | |
1021 } | |
1022 Handle h_recv(THREAD, recv); | |
1023 | |
1024 int number_of_parameters; | |
1025 methodOop selected_method; | |
1026 { | |
1027 methodOop m = JNIHandles::resolve_jmethod_id(method_id); | |
1028 number_of_parameters = m->size_of_parameters(); | |
1029 klassOop holder = m->method_holder(); | |
1030 if (!(Klass::cast(holder))->is_interface()) { | |
1031 // non-interface call -- for that little speed boost, don't handlize | |
1032 debug_only(No_Safepoint_Verifier nosafepoint;) | |
1033 if (call_type == JNI_VIRTUAL) { | |
1034 // jni_GetMethodID makes sure class is linked and initialized | |
1035 // so m should have a valid vtable index. | |
1036 int vtbl_index = m->vtable_index(); | |
1037 if (vtbl_index != methodOopDesc::nonvirtual_vtable_index) { | |
1038 klassOop k = h_recv->klass(); | |
1039 // k might be an arrayKlassOop but all vtables start at | |
1040 // the same place. The cast is to avoid virtual call and assertion. | |
1041 instanceKlass *ik = (instanceKlass*)k->klass_part(); | |
1042 selected_method = ik->method_at_vtable(vtbl_index); | |
1043 } else { | |
1044 // final method | |
1045 selected_method = m; | |
1046 } | |
1047 } else { | |
1048 // JNI_NONVIRTUAL call | |
1049 selected_method = m; | |
1050 } | |
1051 } else { | |
1052 // interface call | |
1053 KlassHandle h_holder(THREAD, holder); | |
1054 | |
1055 int itbl_index = m->cached_itable_index(); | |
1056 if (itbl_index == -1) { | |
1057 itbl_index = klassItable::compute_itable_index(m); | |
1058 m->set_cached_itable_index(itbl_index); | |
1059 // the above may have grabbed a lock, 'm' and anything non-handlized can't be used again | |
1060 } | |
1061 klassOop k = h_recv->klass(); | |
1062 selected_method = instanceKlass::cast(k)->method_at_itable(h_holder(), itbl_index, CHECK); | |
1063 } | |
1064 } | |
1065 | |
1066 methodHandle method(THREAD, selected_method); | |
1067 | |
1068 // Create object to hold arguments for the JavaCall, and associate it with | |
1069 // the jni parser | |
1070 ResourceMark rm(THREAD); | |
1071 JavaCallArguments java_args(number_of_parameters); | |
1072 args->set_java_argument_object(&java_args); | |
1073 | |
1074 // handle arguments | |
1075 assert(!method->is_static(), "method should not be static"); | |
1076 args->push_receiver(h_recv); // Push jobject handle | |
1077 | |
1078 // Fill out JavaCallArguments object | |
1079 args->iterate( Fingerprinter(THREAD, method).fingerprint() ); | |
1080 // Initialize result type | |
1081 result->set_type(args->get_ret_type()); | |
1082 | |
1083 // Invoke the method. Result is returned as oop. | |
1084 JavaCalls::call(result, method, &java_args, CHECK); | |
1085 | |
1086 // Convert result | |
1087 if (result->get_type() == T_OBJECT || result->get_type() == T_ARRAY) { | |
1088 result->set_jobject(JNIHandles::make_local(env, (oop) result->get_jobject())); | |
1089 } | |
1090 } | |
1091 | |
1092 | |
1093 static instanceOop alloc_object(jclass clazz, TRAPS) { | |
1094 KlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz))); | |
1095 Klass::cast(k())->check_valid_for_instantiation(false, CHECK_NULL); | |
1096 instanceKlass::cast(k())->initialize(CHECK_NULL); | |
1097 instanceOop ih = instanceKlass::cast(k())->allocate_instance(THREAD); | |
1098 return ih; | |
1099 } | |
1100 | |
1101 DT_RETURN_MARK_DECL(AllocObject, jobject); | |
1102 | |
1103 JNI_ENTRY(jobject, jni_AllocObject(JNIEnv *env, jclass clazz)) | |
1104 JNIWrapper("AllocObject"); | |
1105 | |
1106 DTRACE_PROBE2(hotspot_jni, AllocObject__entry, env, clazz); | |
1107 jobject ret = NULL; | |
1108 DT_RETURN_MARK(AllocObject, jobject, (const jobject&)ret); | |
1109 | |
1110 instanceOop i = alloc_object(clazz, CHECK_NULL); | |
1111 ret = JNIHandles::make_local(env, i); | |
1112 return ret; | |
1113 JNI_END | |
1114 | |
1115 DT_RETURN_MARK_DECL(NewObjectA, jobject); | |
1116 | |
1117 JNI_ENTRY(jobject, jni_NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args)) | |
1118 JNIWrapper("NewObjectA"); | |
1119 DTRACE_PROBE3(hotspot_jni, NewObjectA__entry, env, clazz, methodID); | |
1120 jobject obj = NULL; | |
1121 DT_RETURN_MARK(NewObjectA, jobject, (const jobject)obj); | |
1122 | |
1123 instanceOop i = alloc_object(clazz, CHECK_NULL); | |
1124 obj = JNIHandles::make_local(env, i); | |
1125 JavaValue jvalue(T_VOID); | |
1126 JNI_ArgumentPusherArray ap(THREAD, methodID, args); | |
1127 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL); | |
1128 return obj; | |
1129 JNI_END | |
1130 | |
1131 DT_RETURN_MARK_DECL(NewObjectV, jobject); | |
1132 | |
1133 JNI_ENTRY(jobject, jni_NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args)) | |
1134 JNIWrapper("NewObjectV"); | |
1135 DTRACE_PROBE3(hotspot_jni, NewObjectV__entry, env, clazz, methodID); | |
1136 jobject obj = NULL; | |
1137 DT_RETURN_MARK(NewObjectV, jobject, (const jobject&)obj); | |
1138 | |
1139 instanceOop i = alloc_object(clazz, CHECK_NULL); | |
1140 obj = JNIHandles::make_local(env, i); | |
1141 JavaValue jvalue(T_VOID); | |
1142 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1143 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL); | |
1144 return obj; | |
1145 JNI_END | |
1146 | |
1147 DT_RETURN_MARK_DECL(NewObject, jobject); | |
1148 | |
1149 JNI_ENTRY(jobject, jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)) | |
1150 JNIWrapper("NewObject"); | |
1151 DTRACE_PROBE3(hotspot_jni, NewObject__entry, env, clazz, methodID); | |
1152 jobject obj = NULL; | |
1153 DT_RETURN_MARK(NewObject, jobject, (const jobject&)obj); | |
1154 | |
1155 instanceOop i = alloc_object(clazz, CHECK_NULL); | |
1156 obj = JNIHandles::make_local(env, i); | |
1157 va_list args; | |
1158 va_start(args, methodID); | |
1159 JavaValue jvalue(T_VOID); | |
1160 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1161 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL); | |
1162 va_end(args); | |
1163 return obj; | |
1164 JNI_END | |
1165 | |
1166 | |
1167 JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj)) | |
1168 JNIWrapper("GetObjectClass"); | |
1169 DTRACE_PROBE2(hotspot_jni, GetObjectClass__entry, env, obj); | |
1170 klassOop k = JNIHandles::resolve_non_null(obj)->klass(); | |
1171 jclass ret = | |
1172 (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror()); | |
1173 DTRACE_PROBE1(hotspot_jni, GetObjectClass__return, ret); | |
1174 return ret; | |
1175 JNI_END | |
1176 | |
1177 JNI_QUICK_ENTRY(jboolean, jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz)) | |
1178 JNIWrapper("IsInstanceOf"); | |
1179 DTRACE_PROBE3(hotspot_jni, IsInstanceOf__entry, env, obj, clazz); | |
1180 jboolean ret = JNI_TRUE; | |
1181 if (obj != NULL) { | |
1182 ret = JNI_FALSE; | |
1183 klassOop k = java_lang_Class::as_klassOop( | |
1184 JNIHandles::resolve_non_null(clazz)); | |
1185 if (k != NULL) { | |
1186 ret = JNIHandles::resolve_non_null(obj)->is_a(k) ? JNI_TRUE : JNI_FALSE; | |
1187 } | |
1188 } | |
1189 DTRACE_PROBE1(hotspot_jni, IsInstanceOf__return, ret); | |
1190 return ret; | |
1191 JNI_END | |
1192 | |
1193 | |
1194 static jmethodID get_method_id(JNIEnv *env, jclass clazz, const char *name_str, | |
1195 const char *sig, bool is_static, TRAPS) { | |
1196 // %%%% This code should probably just call into a method in the LinkResolver | |
1197 // | |
1198 // The class should have been loaded (we have an instance of the class | |
1199 // passed in) so the method and signature should already be in the symbol | |
1200 // table. If they're not there, the method doesn't exist. | |
1201 symbolHandle signature = | |
1202 symbolHandle(THREAD, SymbolTable::probe(sig, (int)strlen(sig))); | |
1203 symbolHandle name; | |
1204 if (name_str == NULL) { | |
1205 name = vmSymbolHandles::object_initializer_name(); | |
1206 } else { | |
1207 name = symbolHandle(THREAD, | |
1208 SymbolTable::probe(name_str, (int)strlen(name_str))); | |
1209 } | |
1210 if (name.is_null() || signature.is_null()) { | |
1211 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str); | |
1212 } | |
1213 | |
1214 // Throw a NoSuchMethodError exception if we have an instance of a | |
1215 // primitive java.lang.Class | |
1216 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(clazz))) { | |
1217 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str); | |
1218 } | |
1219 | |
1220 KlassHandle klass(THREAD, | |
1221 java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz))); | |
1222 | |
1223 // Make sure class is linked and initialized before handing id's out to | |
1224 // methodOops. | |
1225 Klass::cast(klass())->initialize(CHECK_NULL); | |
1226 | |
1227 methodOop m; | |
1228 if (name() == vmSymbols::object_initializer_name() || | |
1229 name() == vmSymbols::class_initializer_name()) { | |
1230 // Never search superclasses for constructors | |
1231 if (klass->oop_is_instance()) { | |
1232 m = instanceKlass::cast(klass())->find_method(name(), signature()); | |
1233 } else { | |
1234 m = NULL; | |
1235 } | |
1236 } else { | |
1237 m = klass->lookup_method(name(), signature()); | |
1238 // Look up interfaces | |
1239 if (m == NULL && klass->oop_is_instance()) { | |
1240 m = instanceKlass::cast(klass())->lookup_method_in_all_interfaces(name(), | |
1241 signature()); | |
1242 } | |
1243 } | |
1244 if (m == NULL || (m->is_static() != is_static)) { | |
1245 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str); | |
1246 } | |
1247 return m->jmethod_id(); | |
1248 } | |
1249 | |
1250 | |
1251 JNI_ENTRY(jmethodID, jni_GetMethodID(JNIEnv *env, jclass clazz, | |
1252 const char *name, const char *sig)) | |
1253 JNIWrapper("GetMethodID"); | |
1254 DTRACE_PROBE4(hotspot_jni, GetMethodID__entry, env, clazz, name, sig); | |
1255 jmethodID ret = get_method_id(env, clazz, name, sig, false, thread); | |
1256 DTRACE_PROBE1(hotspot_jni, GetMethodID__return, ret); | |
1257 return ret; | |
1258 JNI_END | |
1259 | |
1260 | |
1261 JNI_ENTRY(jmethodID, jni_GetStaticMethodID(JNIEnv *env, jclass clazz, | |
1262 const char *name, const char *sig)) | |
1263 JNIWrapper("GetStaticMethodID"); | |
1264 DTRACE_PROBE4(hotspot_jni, GetStaticMethodID__entry, env, clazz, name, sig); | |
1265 jmethodID ret = get_method_id(env, clazz, name, sig, true, thread); | |
1266 DTRACE_PROBE1(hotspot_jni, GetStaticMethodID__return, ret); | |
1267 return ret; | |
1268 JNI_END | |
1269 | |
1270 | |
1271 | |
1272 // | |
1273 // Calling Methods | |
1274 // | |
1275 | |
1276 | |
1277 #define DEFINE_CALLMETHOD(ResultType, Result, Tag) \ | |
1278 \ | |
1279 DT_RETURN_MARK_DECL_FOR(Result, Call##Result##Method, ResultType);\ | |
1280 DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodV, ResultType);\ | |
1281 DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodA, ResultType);\ | |
1282 \ | |
1283 JNI_ENTRY(ResultType, \ | |
1284 jni_Call##Result##Method(JNIEnv *env, jobject obj, jmethodID methodID, ...)) \ | |
1285 JNIWrapper("Call" XSTR(Result) "Method"); \ | |
1286 \ | |
1287 DTRACE_PROBE3(hotspot_jni, Call##Result##Method__entry, env, obj, methodID);\ | |
1288 ResultType ret = 0;\ | |
1289 DT_RETURN_MARK_FOR(Result, Call##Result##Method, ResultType, \ | |
1290 (const ResultType&)ret);\ | |
1291 \ | |
1292 va_list args; \ | |
1293 va_start(args, methodID); \ | |
1294 JavaValue jvalue(Tag); \ | |
1295 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); \ | |
1296 jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \ | |
1297 va_end(args); \ | |
1298 ret = jvalue.get_##ResultType(); \ | |
1299 return ret;\ | |
1300 JNI_END \ | |
1301 \ | |
1302 \ | |
1303 JNI_ENTRY(ResultType, \ | |
1304 jni_Call##Result##MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)) \ | |
1305 JNIWrapper("Call" XSTR(Result) "MethodV"); \ | |
1306 \ | |
1307 DTRACE_PROBE3(hotspot_jni, Call##Result##MethodV__entry, env, obj, methodID);\ | |
1308 ResultType ret = 0;\ | |
1309 DT_RETURN_MARK_FOR(Result, Call##Result##MethodV, ResultType, \ | |
1310 (const ResultType&)ret);\ | |
1311 \ | |
1312 JavaValue jvalue(Tag); \ | |
1313 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); \ | |
1314 jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \ | |
1315 ret = jvalue.get_##ResultType(); \ | |
1316 return ret;\ | |
1317 JNI_END \ | |
1318 \ | |
1319 \ | |
1320 JNI_ENTRY(ResultType, \ | |
1321 jni_Call##Result##MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args)) \ | |
1322 JNIWrapper("Call" XSTR(Result) "MethodA"); \ | |
1323 DTRACE_PROBE3(hotspot_jni, Call##Result##MethodA__entry, env, obj, methodID);\ | |
1324 ResultType ret = 0;\ | |
1325 DT_RETURN_MARK_FOR(Result, Call##Result##MethodA, ResultType, \ | |
1326 (const ResultType&)ret);\ | |
1327 \ | |
1328 JavaValue jvalue(Tag); \ | |
1329 JNI_ArgumentPusherArray ap(THREAD, methodID, args); \ | |
1330 jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \ | |
1331 ret = jvalue.get_##ResultType(); \ | |
1332 return ret;\ | |
1333 JNI_END | |
1334 | |
1335 // the runtime type of subword integral basic types is integer | |
1336 DEFINE_CALLMETHOD(jboolean, Boolean, T_BOOLEAN) | |
1337 DEFINE_CALLMETHOD(jbyte, Byte, T_BYTE) | |
1338 DEFINE_CALLMETHOD(jchar, Char, T_CHAR) | |
1339 DEFINE_CALLMETHOD(jshort, Short, T_SHORT) | |
1340 | |
1341 DEFINE_CALLMETHOD(jobject, Object, T_OBJECT) | |
1342 DEFINE_CALLMETHOD(jint, Int, T_INT) | |
1343 DEFINE_CALLMETHOD(jlong, Long, T_LONG) | |
1344 DEFINE_CALLMETHOD(jfloat, Float, T_FLOAT) | |
1345 DEFINE_CALLMETHOD(jdouble, Double, T_DOUBLE) | |
1346 | |
1347 DT_VOID_RETURN_MARK_DECL(CallVoidMethod); | |
1348 DT_VOID_RETURN_MARK_DECL(CallVoidMethodV); | |
1349 DT_VOID_RETURN_MARK_DECL(CallVoidMethodA); | |
1350 | |
1351 JNI_ENTRY(void, jni_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...)) | |
1352 JNIWrapper("CallVoidMethod"); | |
1353 DTRACE_PROBE3(hotspot_jni, CallVoidMethod__entry, env, obj, methodID); | |
1354 DT_VOID_RETURN_MARK(CallVoidMethod); | |
1355 | |
1356 va_list args; | |
1357 va_start(args, methodID); | |
1358 JavaValue jvalue(T_VOID); | |
1359 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1360 jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK); | |
1361 va_end(args); | |
1362 JNI_END | |
1363 | |
1364 | |
1365 JNI_ENTRY(void, jni_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)) | |
1366 JNIWrapper("CallVoidMethodV"); | |
1367 DTRACE_PROBE3(hotspot_jni, CallVoidMethodV__entry, env, obj, methodID); | |
1368 DT_VOID_RETURN_MARK(CallVoidMethodV); | |
1369 | |
1370 JavaValue jvalue(T_VOID); | |
1371 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1372 jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK); | |
1373 JNI_END | |
1374 | |
1375 | |
1376 JNI_ENTRY(void, jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args)) | |
1377 JNIWrapper("CallVoidMethodA"); | |
1378 DTRACE_PROBE3(hotspot_jni, CallVoidMethodA__entry, env, obj, methodID); | |
1379 DT_VOID_RETURN_MARK(CallVoidMethodA); | |
1380 | |
1381 JavaValue jvalue(T_VOID); | |
1382 JNI_ArgumentPusherArray ap(THREAD, methodID, args); | |
1383 jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK); | |
1384 JNI_END | |
1385 | |
1386 | |
1387 #define DEFINE_CALLNONVIRTUALMETHOD(ResultType, Result, Tag) \ | |
1388 \ | |
1389 DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##Method, ResultType);\ | |
1390 DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodV, ResultType);\ | |
1391 DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodA, ResultType);\ | |
1392 \ | |
1393 JNI_ENTRY(ResultType, \ | |
1394 jni_CallNonvirtual##Result##Method(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...)) \ | |
1395 JNIWrapper("CallNonvitual" XSTR(Result) "Method"); \ | |
1396 \ | |
1397 DTRACE_PROBE4(hotspot_jni, CallNonvirtual##Result##Method__entry, env, obj, cls, methodID);\ | |
1398 ResultType ret;\ | |
1399 DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##Method, ResultType, \ | |
1400 (const ResultType&)ret);\ | |
1401 \ | |
1402 va_list args; \ | |
1403 va_start(args, methodID); \ | |
1404 JavaValue jvalue(Tag); \ | |
1405 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); \ | |
1406 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \ | |
1407 va_end(args); \ | |
1408 ret = jvalue.get_##ResultType(); \ | |
1409 return ret;\ | |
1410 JNI_END \ | |
1411 \ | |
1412 JNI_ENTRY(ResultType, \ | |
1413 jni_CallNonvirtual##Result##MethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args)) \ | |
1414 JNIWrapper("CallNonvitual" XSTR(Result) "#MethodV"); \ | |
1415 DTRACE_PROBE4(hotspot_jni, CallNonvirtual##Result##MethodV__entry, env, obj, cls, methodID);\ | |
1416 ResultType ret;\ | |
1417 DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodV, ResultType, \ | |
1418 (const ResultType&)ret);\ | |
1419 \ | |
1420 JavaValue jvalue(Tag); \ | |
1421 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); \ | |
1422 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \ | |
1423 ret = jvalue.get_##ResultType(); \ | |
1424 return ret;\ | |
1425 JNI_END \ | |
1426 \ | |
1427 JNI_ENTRY(ResultType, \ | |
1428 jni_CallNonvirtual##Result##MethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args)) \ | |
1429 JNIWrapper("CallNonvitual" XSTR(Result) "MethodA"); \ | |
1430 DTRACE_PROBE4(hotspot_jni, CallNonvirtual##Result##MethodA__entry, env, obj, cls, methodID);\ | |
1431 ResultType ret;\ | |
1432 DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodA, ResultType, \ | |
1433 (const ResultType&)ret);\ | |
1434 \ | |
1435 JavaValue jvalue(Tag); \ | |
1436 JNI_ArgumentPusherArray ap(THREAD, methodID, args); \ | |
1437 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \ | |
1438 ret = jvalue.get_##ResultType(); \ | |
1439 return ret;\ | |
1440 JNI_END | |
1441 | |
1442 // the runtime type of subword integral basic types is integer | |
1443 DEFINE_CALLNONVIRTUALMETHOD(jboolean, Boolean, T_BOOLEAN) | |
1444 DEFINE_CALLNONVIRTUALMETHOD(jbyte, Byte, T_BYTE) | |
1445 DEFINE_CALLNONVIRTUALMETHOD(jchar, Char, T_CHAR) | |
1446 DEFINE_CALLNONVIRTUALMETHOD(jshort, Short, T_SHORT) | |
1447 | |
1448 DEFINE_CALLNONVIRTUALMETHOD(jobject, Object, T_OBJECT) | |
1449 DEFINE_CALLNONVIRTUALMETHOD(jint, Int, T_INT) | |
1450 DEFINE_CALLNONVIRTUALMETHOD(jlong, Long, T_LONG) | |
1451 DEFINE_CALLNONVIRTUALMETHOD(jfloat, Float, T_FLOAT) | |
1452 DEFINE_CALLNONVIRTUALMETHOD(jdouble, Double, T_DOUBLE) | |
1453 | |
1454 | |
1455 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethod); | |
1456 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodV); | |
1457 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodA); | |
1458 | |
1459 JNI_ENTRY(void, jni_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...)) | |
1460 JNIWrapper("CallNonvirtualVoidMethod"); | |
1461 | |
1462 DTRACE_PROBE4(hotspot_jni, CallNonvirtualVoidMethod__entry, | |
1463 env, obj, cls, methodID); | |
1464 DT_VOID_RETURN_MARK(CallNonvirtualVoidMethod); | |
1465 | |
1466 va_list args; | |
1467 va_start(args, methodID); | |
1468 JavaValue jvalue(T_VOID); | |
1469 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1470 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK); | |
1471 va_end(args); | |
1472 JNI_END | |
1473 | |
1474 | |
1475 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args)) | |
1476 JNIWrapper("CallNonvirtualVoidMethodV"); | |
1477 | |
1478 DTRACE_PROBE4(hotspot_jni, CallNonvirtualVoidMethodV__entry, | |
1479 env, obj, cls, methodID); | |
1480 DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodV); | |
1481 | |
1482 JavaValue jvalue(T_VOID); | |
1483 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1484 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK); | |
1485 JNI_END | |
1486 | |
1487 | |
1488 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args)) | |
1489 JNIWrapper("CallNonvirtualVoidMethodA"); | |
1490 DTRACE_PROBE4(hotspot_jni, CallNonvirtualVoidMethodA__entry, | |
1491 env, obj, cls, methodID); | |
1492 DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodA); | |
1493 JavaValue jvalue(T_VOID); | |
1494 JNI_ArgumentPusherArray ap(THREAD, methodID, args); | |
1495 jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK); | |
1496 JNI_END | |
1497 | |
1498 | |
1499 #define DEFINE_CALLSTATICMETHOD(ResultType, Result, Tag) \ | |
1500 \ | |
1501 DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##Method, ResultType);\ | |
1502 DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodV, ResultType);\ | |
1503 DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodA, ResultType);\ | |
1504 \ | |
1505 JNI_ENTRY(ResultType, \ | |
1506 jni_CallStatic##Result##Method(JNIEnv *env, jclass cls, jmethodID methodID, ...)) \ | |
1507 JNIWrapper("CallStatic" XSTR(Result) "Method"); \ | |
1508 \ | |
1509 DTRACE_PROBE3(hotspot_jni, CallStatic##Result##Method__entry, env, cls, methodID);\ | |
1510 ResultType ret = 0;\ | |
1511 DT_RETURN_MARK_FOR(Result, CallStatic##Result##Method, ResultType, \ | |
1512 (const ResultType&)ret);\ | |
1513 \ | |
1514 va_list args; \ | |
1515 va_start(args, methodID); \ | |
1516 JavaValue jvalue(Tag); \ | |
1517 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); \ | |
1518 jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \ | |
1519 va_end(args); \ | |
1520 ret = jvalue.get_##ResultType(); \ | |
1521 return ret;\ | |
1522 JNI_END \ | |
1523 \ | |
1524 JNI_ENTRY(ResultType, \ | |
1525 jni_CallStatic##Result##MethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args)) \ | |
1526 JNIWrapper("CallStatic" XSTR(Result) "MethodV"); \ | |
1527 DTRACE_PROBE3(hotspot_jni, CallStatic##Result##MethodV__entry, env, cls, methodID);\ | |
1528 ResultType ret = 0;\ | |
1529 DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodV, ResultType, \ | |
1530 (const ResultType&)ret);\ | |
1531 \ | |
1532 JavaValue jvalue(Tag); \ | |
1533 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); \ | |
1534 jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \ | |
1535 ret = jvalue.get_##ResultType(); \ | |
1536 return ret;\ | |
1537 JNI_END \ | |
1538 \ | |
1539 JNI_ENTRY(ResultType, \ | |
1540 jni_CallStatic##Result##MethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args)) \ | |
1541 JNIWrapper("CallStatic" XSTR(Result) "MethodA"); \ | |
1542 DTRACE_PROBE3(hotspot_jni, CallStatic##Result##MethodA__entry, env, cls, methodID);\ | |
1543 ResultType ret = 0;\ | |
1544 DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodA, ResultType, \ | |
1545 (const ResultType&)ret);\ | |
1546 \ | |
1547 JavaValue jvalue(Tag); \ | |
1548 JNI_ArgumentPusherArray ap(THREAD, methodID, args); \ | |
1549 jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \ | |
1550 ret = jvalue.get_##ResultType(); \ | |
1551 return ret;\ | |
1552 JNI_END | |
1553 | |
1554 // the runtime type of subword integral basic types is integer | |
1555 DEFINE_CALLSTATICMETHOD(jboolean, Boolean, T_BOOLEAN) | |
1556 DEFINE_CALLSTATICMETHOD(jbyte, Byte, T_BYTE) | |
1557 DEFINE_CALLSTATICMETHOD(jchar, Char, T_CHAR) | |
1558 DEFINE_CALLSTATICMETHOD(jshort, Short, T_SHORT) | |
1559 | |
1560 DEFINE_CALLSTATICMETHOD(jobject, Object, T_OBJECT) | |
1561 DEFINE_CALLSTATICMETHOD(jint, Int, T_INT) | |
1562 DEFINE_CALLSTATICMETHOD(jlong, Long, T_LONG) | |
1563 DEFINE_CALLSTATICMETHOD(jfloat, Float, T_FLOAT) | |
1564 DEFINE_CALLSTATICMETHOD(jdouble, Double, T_DOUBLE) | |
1565 | |
1566 | |
1567 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethod); | |
1568 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodV); | |
1569 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodA); | |
1570 | |
1571 JNI_ENTRY(void, jni_CallStaticVoidMethod(JNIEnv *env, jclass cls, jmethodID methodID, ...)) | |
1572 JNIWrapper("CallStaticVoidMethod"); | |
1573 DTRACE_PROBE3(hotspot_jni, CallStaticVoidMethod__entry, env, cls, methodID); | |
1574 DT_VOID_RETURN_MARK(CallStaticVoidMethod); | |
1575 | |
1576 va_list args; | |
1577 va_start(args, methodID); | |
1578 JavaValue jvalue(T_VOID); | |
1579 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1580 jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK); | |
1581 va_end(args); | |
1582 JNI_END | |
1583 | |
1584 | |
1585 JNI_ENTRY(void, jni_CallStaticVoidMethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args)) | |
1586 JNIWrapper("CallStaticVoidMethodV"); | |
1587 DTRACE_PROBE3(hotspot_jni, CallStaticVoidMethodV__entry, env, cls, methodID); | |
1588 DT_VOID_RETURN_MARK(CallStaticVoidMethodV); | |
1589 | |
1590 JavaValue jvalue(T_VOID); | |
1591 JNI_ArgumentPusherVaArg ap(THREAD, methodID, args); | |
1592 jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK); | |
1593 JNI_END | |
1594 | |
1595 | |
1596 JNI_ENTRY(void, jni_CallStaticVoidMethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args)) | |
1597 JNIWrapper("CallStaticVoidMethodA"); | |
1598 DTRACE_PROBE3(hotspot_jni, CallStaticVoidMethodA__entry, env, cls, methodID); | |
1599 DT_VOID_RETURN_MARK(CallStaticVoidMethodA); | |
1600 | |
1601 JavaValue jvalue(T_VOID); | |
1602 JNI_ArgumentPusherArray ap(THREAD, methodID, args); | |
1603 jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK); | |
1604 JNI_END | |
1605 | |
1606 | |
1607 // | |
1608 // Accessing Fields | |
1609 // | |
1610 | |
1611 | |
1612 DT_RETURN_MARK_DECL(GetFieldID, jfieldID); | |
1613 | |
1614 JNI_ENTRY(jfieldID, jni_GetFieldID(JNIEnv *env, jclass clazz, | |
1615 const char *name, const char *sig)) | |
1616 JNIWrapper("GetFieldID"); | |
1617 DTRACE_PROBE4(hotspot_jni, GetFieldID__entry, env, clazz, name, sig); | |
1618 jfieldID ret = 0; | |
1619 DT_RETURN_MARK(GetFieldID, jfieldID, (const jfieldID&)ret); | |
1620 | |
1621 // The class should have been loaded (we have an instance of the class | |
1622 // passed in) so the field and signature should already be in the symbol | |
1623 // table. If they're not there, the field doesn't exist. | |
1624 symbolHandle fieldname = | |
1625 symbolHandle(THREAD, SymbolTable::probe(name, (int)strlen(name))); | |
1626 symbolHandle signame = | |
1627 symbolHandle(THREAD, SymbolTable::probe(sig, (int)strlen(sig))); | |
1628 if (fieldname.is_null() || signame.is_null()) { | |
1629 THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name); | |
1630 } | |
1631 KlassHandle k(THREAD, | |
1632 java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz))); | |
1633 // Make sure class is initialized before handing id's out to fields | |
1634 Klass::cast(k())->initialize(CHECK_NULL); | |
1635 | |
1636 fieldDescriptor fd; | |
1637 if (!Klass::cast(k())->oop_is_instance() || | |
1638 !instanceKlass::cast(k())->find_field(fieldname(), signame(), false, &fd)) { | |
1639 THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name); | |
1640 } | |
1641 | |
1642 // A jfieldID for a non-static field is simply the offset of the field within the instanceOop | |
1643 // It may also have hash bits for k, if VerifyJNIFields is turned on. | |
1644 ret = jfieldIDWorkaround::to_instance_jfieldID(k(), fd.offset()); | |
1645 return ret; | |
1646 JNI_END | |
1647 | |
1648 | |
1649 JNI_ENTRY(jobject, jni_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID)) | |
1650 JNIWrapper("GetObjectField"); | |
1651 DTRACE_PROBE3(hotspot_jni, GetObjectField__entry, env, obj, fieldID); | |
1652 oop o = JNIHandles::resolve_non_null(obj); | |
1653 klassOop k = o->klass(); | |
1654 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID); | |
1655 // Keep JVMTI addition small and only check enabled flag here. | |
1656 // jni_GetField_probe() assumes that is okay to create handles. | |
1657 if (JvmtiExport::should_post_field_access()) { | |
1658 o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false); | |
1659 } | |
1660 jobject ret = JNIHandles::make_local(env, o->obj_field(offset)); | |
1661 DTRACE_PROBE1(hotspot_jni, GetObjectField__return, ret); | |
1662 return ret; | |
1663 JNI_END | |
1664 | |
1665 | |
1666 #define DEFINE_GETFIELD(Return,Fieldname,Result) \ | |
1667 \ | |
1668 DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return);\ | |
1669 \ | |
1670 JNI_QUICK_ENTRY(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \ | |
1671 JNIWrapper("Get" XSTR(Result) "Field"); \ | |
1672 \ | |
1673 DTRACE_PROBE3(hotspot_jni, Get##Result##Field__entry, env, obj, fieldID);\ | |
1674 Return ret = 0;\ | |
1675 DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\ | |
1676 \ | |
1677 oop o = JNIHandles::resolve_non_null(obj); \ | |
1678 klassOop k = o->klass(); \ | |
1679 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID); \ | |
1680 /* Keep JVMTI addition small and only check enabled flag here. */ \ | |
1681 /* jni_GetField_probe_nh() assumes that is not okay to create handles */ \ | |
1682 /* and creates a ResetNoHandleMark. */ \ | |
1683 if (JvmtiExport::should_post_field_access()) { \ | |
1684 o = JvmtiExport::jni_GetField_probe_nh(thread, obj, o, k, fieldID, false); \ | |
1685 } \ | |
1686 ret = o->Fieldname##_field(offset); \ | |
1687 return ret; \ | |
1688 JNI_END | |
1689 | |
1690 DEFINE_GETFIELD(jboolean, bool, Boolean) | |
1691 DEFINE_GETFIELD(jbyte, byte, Byte) | |
1692 DEFINE_GETFIELD(jchar, char, Char) | |
1693 DEFINE_GETFIELD(jshort, short, Short) | |
1694 DEFINE_GETFIELD(jint, int, Int) | |
1695 DEFINE_GETFIELD(jlong, long, Long) | |
1696 DEFINE_GETFIELD(jfloat, float, Float) | |
1697 DEFINE_GETFIELD(jdouble, double, Double) | |
1698 | |
1699 address jni_GetBooleanField_addr() { | |
1700 return (address)jni_GetBooleanField; | |
1701 } | |
1702 address jni_GetByteField_addr() { | |
1703 return (address)jni_GetByteField; | |
1704 } | |
1705 address jni_GetCharField_addr() { | |
1706 return (address)jni_GetCharField; | |
1707 } | |
1708 address jni_GetShortField_addr() { | |
1709 return (address)jni_GetShortField; | |
1710 } | |
1711 address jni_GetIntField_addr() { | |
1712 return (address)jni_GetIntField; | |
1713 } | |
1714 address jni_GetLongField_addr() { | |
1715 return (address)jni_GetLongField; | |
1716 } | |
1717 address jni_GetFloatField_addr() { | |
1718 return (address)jni_GetFloatField; | |
1719 } | |
1720 address jni_GetDoubleField_addr() { | |
1721 return (address)jni_GetDoubleField; | |
1722 } | |
1723 | |
1724 JNI_QUICK_ENTRY(void, jni_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID, jobject value)) | |
1725 JNIWrapper("SetObjectField"); | |
1726 DTRACE_PROBE4(hotspot_jni, SetObjectField__entry, env, obj, fieldID, value); | |
1727 oop o = JNIHandles::resolve_non_null(obj); | |
1728 klassOop k = o->klass(); | |
1729 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID); | |
1730 // Keep JVMTI addition small and only check enabled flag here. | |
1731 // jni_SetField_probe_nh() assumes that is not okay to create handles | |
1732 // and creates a ResetNoHandleMark. | |
1733 if (JvmtiExport::should_post_field_modification()) { | |
1734 jvalue field_value; | |
1735 field_value.l = value; | |
1736 o = JvmtiExport::jni_SetField_probe_nh(thread, obj, o, k, fieldID, false, 'L', (jvalue *)&field_value); | |
1737 } | |
1738 o->obj_field_put(offset, JNIHandles::resolve(value)); | |
1739 DTRACE_PROBE(hotspot_jni, SetObjectField__return); | |
1740 JNI_END | |
1741 | |
1742 #define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType) \ | |
1743 \ | |
1744 JNI_QUICK_ENTRY(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \ | |
1745 JNIWrapper("Set" XSTR(Result) "Field"); \ | |
1746 \ | |
1747 HS_DTRACE_PROBE_CDECL_N(hotspot_jni, Set##Result##Field__entry, \ | |
1748 ( JNIEnv*, jobject, jfieldID FP_SELECT_##Result(COMMA Argument,/*empty*/) ) ); \ | |
1749 HS_DTRACE_PROBE_N(hotspot_jni, Set##Result##Field__entry, \ | |
1750 ( env, obj, fieldID FP_SELECT_##Result(COMMA value,/*empty*/) ) ); \ | |
1751 \ | |
1752 oop o = JNIHandles::resolve_non_null(obj); \ | |
1753 klassOop k = o->klass(); \ | |
1754 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID); \ | |
1755 /* Keep JVMTI addition small and only check enabled flag here. */ \ | |
1756 /* jni_SetField_probe_nh() assumes that is not okay to create handles */ \ | |
1757 /* and creates a ResetNoHandleMark. */ \ | |
1758 if (JvmtiExport::should_post_field_modification()) { \ | |
1759 jvalue field_value; \ | |
1760 field_value.unionType = value; \ | |
1761 o = JvmtiExport::jni_SetField_probe_nh(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \ | |
1762 } \ | |
1763 o->Fieldname##_field_put(offset, value); \ | |
1764 DTRACE_PROBE(hotspot_jni, Set##Result##Field__return);\ | |
1765 JNI_END | |
1766 | |
1767 DEFINE_SETFIELD(jboolean, bool, Boolean, 'Z', z) | |
1768 DEFINE_SETFIELD(jbyte, byte, Byte, 'B', b) | |
1769 DEFINE_SETFIELD(jchar, char, Char, 'C', c) | |
1770 DEFINE_SETFIELD(jshort, short, Short, 'S', s) | |
1771 DEFINE_SETFIELD(jint, int, Int, 'I', i) | |
1772 DEFINE_SETFIELD(jlong, long, Long, 'J', j) | |
1773 DEFINE_SETFIELD(jfloat, float, Float, 'F', f) | |
1774 DEFINE_SETFIELD(jdouble, double, Double, 'D', d) | |
1775 | |
1776 DT_RETURN_MARK_DECL(ToReflectedField, jobject); | |
1777 | |
1778 JNI_ENTRY(jobject, jni_ToReflectedField(JNIEnv *env, jclass cls, jfieldID fieldID, jboolean isStatic)) | |
1779 JNIWrapper("ToReflectedField"); | |
1780 DTRACE_PROBE4(hotspot_jni, ToReflectedField__entry, | |
1781 env, cls, fieldID, isStatic); | |
1782 jobject ret = NULL; | |
1783 DT_RETURN_MARK(ToReflectedField, jobject, (const jobject&)ret); | |
1784 | |
1785 fieldDescriptor fd; | |
1786 bool found = false; | |
1787 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)); | |
1788 | |
1789 assert(jfieldIDWorkaround::is_static_jfieldID(fieldID) == (isStatic != 0), "invalid fieldID"); | |
1790 | |
1791 if (isStatic) { | |
1792 // Static field. The fieldID a JNIid specifying the field holder and the offset within the klassOop. | |
1793 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); | |
1794 assert(id->is_static_field_id(), "invalid static field id"); | |
1795 found = instanceKlass::cast(id->holder())->find_local_field_from_offset(id->offset(), true, &fd); | |
1796 } else { | |
1797 // Non-static field. The fieldID is really the offset of the field within the instanceOop. | |
1798 int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID); | |
1799 found = instanceKlass::cast(k)->find_field_from_offset(offset, false, &fd); | |
1800 } | |
1801 assert(found, "bad fieldID passed into jni_ToReflectedField"); | |
1802 oop reflected = Reflection::new_field(&fd, UseNewReflection, CHECK_NULL); | |
1803 ret = JNIHandles::make_local(env, reflected); | |
1804 return ret; | |
1805 JNI_END | |
1806 | |
1807 | |
1808 // | |
1809 // Accessing Static Fields | |
1810 // | |
1811 DT_RETURN_MARK_DECL(GetStaticFieldID, jfieldID); | |
1812 | |
1813 JNI_ENTRY(jfieldID, jni_GetStaticFieldID(JNIEnv *env, jclass clazz, | |
1814 const char *name, const char *sig)) | |
1815 JNIWrapper("GetStaticFieldID"); | |
1816 DTRACE_PROBE4(hotspot_jni, GetStaticFieldID__entry, env, clazz, name, sig); | |
1817 jfieldID ret = NULL; | |
1818 DT_RETURN_MARK(GetStaticFieldID, jfieldID, (const jfieldID&)ret); | |
1819 | |
1820 // The class should have been loaded (we have an instance of the class | |
1821 // passed in) so the field and signature should already be in the symbol | |
1822 // table. If they're not there, the field doesn't exist. | |
1823 symbolHandle fieldname = | |
1824 symbolHandle(THREAD, SymbolTable::probe(name, (int)strlen(name))); | |
1825 symbolHandle signame = | |
1826 symbolHandle(THREAD, SymbolTable::probe(sig, (int)strlen(sig))); | |
1827 if (fieldname.is_null() || signame.is_null()) { | |
1828 THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name); | |
1829 } | |
1830 KlassHandle k(THREAD, | |
1831 java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz))); | |
1832 // Make sure class is initialized before handing id's out to static fields | |
1833 Klass::cast(k())->initialize(CHECK_NULL); | |
1834 | |
1835 fieldDescriptor fd; | |
1836 if (!Klass::cast(k())->oop_is_instance() || | |
1837 !instanceKlass::cast(k())->find_field(fieldname(), signame(), true, &fd)) { | |
1838 THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name); | |
1839 } | |
1840 | |
1841 // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the klassOop | |
1842 JNIid* id = instanceKlass::cast(fd.field_holder())->jni_id_for(fd.offset()); | |
1843 debug_only(id->set_is_static_field_id();) | |
1844 | |
1845 debug_only(int first_offset = instanceKlass::cast(fd.field_holder())->offset_of_static_fields();) | |
1846 debug_only(int end_offset = first_offset + (instanceKlass::cast(fd.field_holder())->static_field_size() * wordSize);) | |
1847 assert(id->offset() >= first_offset && id->offset() < end_offset, "invalid static field offset"); | |
1848 | |
1849 ret = jfieldIDWorkaround::to_static_jfieldID(id); | |
1850 return ret; | |
1851 JNI_END | |
1852 | |
1853 | |
1854 JNI_ENTRY(jobject, jni_GetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID)) | |
1855 JNIWrapper("GetStaticObjectField"); | |
1856 DTRACE_PROBE3(hotspot_jni, GetStaticObjectField__entry, env, clazz, fieldID); | |
1857 #ifndef JNICHECK_KERNEL | |
1858 DEBUG_ONLY(klassOop param_k = jniCheck::validate_class(thread, clazz);) | |
1859 #endif // JNICHECK_KERNEL | |
1860 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); | |
1861 assert(id->is_static_field_id(), "invalid static field id"); | |
1862 // Keep JVMTI addition small and only check enabled flag here. | |
1863 // jni_GetField_probe() assumes that is okay to create handles. | |
1864 if (JvmtiExport::should_post_field_access()) { | |
1865 JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true); | |
1866 } | |
1867 jobject ret = JNIHandles::make_local(id->holder()->obj_field(id->offset())); | |
1868 DTRACE_PROBE1(hotspot_jni, GetStaticObjectField__return, ret); | |
1869 return ret; | |
1870 JNI_END | |
1871 | |
1872 #define DEFINE_GETSTATICFIELD(Return,Fieldname,Result) \ | |
1873 \ | |
1874 DT_RETURN_MARK_DECL_FOR(Result, GetStatic##Result##Field, Return);\ | |
1875 \ | |
1876 JNI_ENTRY(Return, jni_GetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID)) \ | |
1877 JNIWrapper("GetStatic" XSTR(Result) "Field"); \ | |
1878 DTRACE_PROBE3(hotspot_jni, GetStatic##Result##Field__entry, env, clazz, fieldID);\ | |
1879 Return ret = 0;\ | |
1880 DT_RETURN_MARK_FOR(Result, GetStatic##Result##Field, Return, \ | |
1881 (const Return&)ret);\ | |
1882 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \ | |
1883 assert(id->is_static_field_id(), "invalid static field id"); \ | |
1884 /* Keep JVMTI addition small and only check enabled flag here. */ \ | |
1885 /* jni_GetField_probe() assumes that is okay to create handles. */ \ | |
1886 if (JvmtiExport::should_post_field_access()) { \ | |
1887 JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true); \ | |
1888 } \ | |
1889 ret = id->holder()-> Fieldname##_field (id->offset()); \ | |
1890 return ret;\ | |
1891 JNI_END | |
1892 | |
1893 DEFINE_GETSTATICFIELD(jboolean, bool, Boolean) | |
1894 DEFINE_GETSTATICFIELD(jbyte, byte, Byte) | |
1895 DEFINE_GETSTATICFIELD(jchar, char, Char) | |
1896 DEFINE_GETSTATICFIELD(jshort, short, Short) | |
1897 DEFINE_GETSTATICFIELD(jint, int, Int) | |
1898 DEFINE_GETSTATICFIELD(jlong, long, Long) | |
1899 DEFINE_GETSTATICFIELD(jfloat, float, Float) | |
1900 DEFINE_GETSTATICFIELD(jdouble, double, Double) | |
1901 | |
1902 | |
1903 JNI_ENTRY(void, jni_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID, jobject value)) | |
1904 JNIWrapper("SetStaticObjectField"); | |
1905 DTRACE_PROBE4(hotspot_jni, SetStaticObjectField__entry, env, clazz, fieldID, value); | |
1906 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); | |
1907 assert(id->is_static_field_id(), "invalid static field id"); | |
1908 // Keep JVMTI addition small and only check enabled flag here. | |
1909 // jni_SetField_probe() assumes that is okay to create handles. | |
1910 if (JvmtiExport::should_post_field_modification()) { | |
1911 jvalue field_value; | |
1912 field_value.l = value; | |
1913 JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, 'L', (jvalue *)&field_value); | |
1914 } | |
1915 id->holder()->obj_field_put(id->offset(), JNIHandles::resolve(value)); | |
1916 DTRACE_PROBE(hotspot_jni, SetStaticObjectField__return); | |
1917 JNI_END | |
1918 | |
1919 | |
1920 #define DEFINE_SETSTATICFIELD(Argument,Fieldname,Result,SigType,unionType) \ | |
1921 \ | |
1922 JNI_ENTRY(void, jni_SetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID, Argument value)) \ | |
1923 JNIWrapper("SetStatic" XSTR(Result) "Field"); \ | |
1924 HS_DTRACE_PROBE_CDECL_N(hotspot_jni, SetStatic##Result##Field__entry,\ | |
1925 ( JNIEnv*, jclass, jfieldID FP_SELECT_##Result(COMMA Argument,/*empty*/) ) ); \ | |
1926 HS_DTRACE_PROBE_N(hotspot_jni, SetStatic##Result##Field__entry, \ | |
1927 ( env, clazz, fieldID FP_SELECT_##Result(COMMA value,/*empty*/) ) ); \ | |
1928 \ | |
1929 JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \ | |
1930 assert(id->is_static_field_id(), "invalid static field id"); \ | |
1931 /* Keep JVMTI addition small and only check enabled flag here. */ \ | |
1932 /* jni_SetField_probe() assumes that is okay to create handles. */ \ | |
1933 if (JvmtiExport::should_post_field_modification()) { \ | |
1934 jvalue field_value; \ | |
1935 field_value.unionType = value; \ | |
1936 JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, SigType, (jvalue *)&field_value); \ | |
1937 } \ | |
1938 id->holder()-> Fieldname##_field_put (id->offset(), value); \ | |
1939 DTRACE_PROBE(hotspot_jni, SetStatic##Result##Field__return);\ | |
1940 JNI_END | |
1941 | |
1942 DEFINE_SETSTATICFIELD(jboolean, bool, Boolean, 'Z', z) | |
1943 DEFINE_SETSTATICFIELD(jbyte, byte, Byte, 'B', b) | |
1944 DEFINE_SETSTATICFIELD(jchar, char, Char, 'C', c) | |
1945 DEFINE_SETSTATICFIELD(jshort, short, Short, 'S', s) | |
1946 DEFINE_SETSTATICFIELD(jint, int, Int, 'I', i) | |
1947 DEFINE_SETSTATICFIELD(jlong, long, Long, 'J', j) | |
1948 DEFINE_SETSTATICFIELD(jfloat, float, Float, 'F', f) | |
1949 DEFINE_SETSTATICFIELD(jdouble, double, Double, 'D', d) | |
1950 | |
1951 | |
1952 // | |
1953 // String Operations | |
1954 // | |
1955 | |
1956 // Unicode Interface | |
1957 | |
1958 DT_RETURN_MARK_DECL(NewString, jstring); | |
1959 | |
1960 JNI_ENTRY(jstring, jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len)) | |
1961 JNIWrapper("NewString"); | |
1962 DTRACE_PROBE3(hotspot_jni, NewString__entry, env, unicodeChars, len); | |
1963 jstring ret = NULL; | |
1964 DT_RETURN_MARK(NewString, jstring, (const jstring&)ret); | |
1965 oop string=java_lang_String::create_oop_from_unicode((jchar*) unicodeChars, len, CHECK_NULL); | |
1966 ret = (jstring) JNIHandles::make_local(env, string); | |
1967 return ret; | |
1968 JNI_END | |
1969 | |
1970 | |
1971 JNI_QUICK_ENTRY(jsize, jni_GetStringLength(JNIEnv *env, jstring string)) | |
1972 JNIWrapper("GetStringLength"); | |
1973 DTRACE_PROBE2(hotspot_jni, GetStringLength__entry, env, string); | |
1974 jsize ret = java_lang_String::length(JNIHandles::resolve_non_null(string)); | |
1975 DTRACE_PROBE1(hotspot_jni, GetStringLength__return, ret); | |
1976 return ret; | |
1977 JNI_END | |
1978 | |
1979 | |
1980 JNI_QUICK_ENTRY(const jchar*, jni_GetStringChars( | |
1981 JNIEnv *env, jstring string, jboolean *isCopy)) | |
1982 JNIWrapper("GetStringChars"); | |
1983 DTRACE_PROBE3(hotspot_jni, GetStringChars__entry, env, string, isCopy); | |
1984 //%note jni_5 | |
1985 if (isCopy != NULL) { | |
1986 *isCopy = JNI_TRUE; | |
1987 } | |
1988 oop s = JNIHandles::resolve_non_null(string); | |
1989 int s_len = java_lang_String::length(s); | |
1990 typeArrayOop s_value = java_lang_String::value(s); | |
1991 int s_offset = java_lang_String::offset(s); | |
1992 jchar* buf = NEW_C_HEAP_ARRAY(jchar, s_len + 1); // add one for zero termination | |
1993 if (s_len > 0) { | |
1994 memcpy(buf, s_value->char_at_addr(s_offset), sizeof(jchar)*s_len); | |
1995 } | |
1996 buf[s_len] = 0; | |
1997 DTRACE_PROBE1(hotspot_jni, GetStringChars__return, buf); | |
1998 return buf; | |
1999 JNI_END | |
2000 | |
2001 | |
2002 JNI_QUICK_ENTRY(void, jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars)) | |
2003 JNIWrapper("ReleaseStringChars"); | |
2004 DTRACE_PROBE3(hotspot_jni, ReleaseStringChars__entry, env, str, chars); | |
2005 //%note jni_6 | |
2006 if (chars != NULL) { | |
2007 // Since String objects are supposed to be immutable, don't copy any | |
2008 // new data back. A bad user will have to go after the char array. | |
2009 FreeHeap((void*) chars); | |
2010 } | |
2011 DTRACE_PROBE(hotspot_jni, ReleaseStringChars__return); | |
2012 JNI_END | |
2013 | |
2014 | |
2015 // UTF Interface | |
2016 | |
2017 DT_RETURN_MARK_DECL(NewStringUTF, jstring); | |
2018 | |
2019 JNI_ENTRY(jstring, jni_NewStringUTF(JNIEnv *env, const char *bytes)) | |
2020 JNIWrapper("NewStringUTF"); | |
2021 DTRACE_PROBE2(hotspot_jni, NewStringUTF__entry, env, bytes); | |
2022 jstring ret; | |
2023 DT_RETURN_MARK(NewStringUTF, jstring, (const jstring&)ret); | |
2024 | |
2025 oop result = java_lang_String::create_oop_from_str((char*) bytes, CHECK_NULL); | |
2026 ret = (jstring) JNIHandles::make_local(env, result); | |
2027 return ret; | |
2028 JNI_END | |
2029 | |
2030 | |
2031 JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string)) | |
2032 JNIWrapper("GetStringUTFLength"); | |
2033 DTRACE_PROBE2(hotspot_jni, GetStringUTFLength__entry, env, string); | |
2034 jsize ret = java_lang_String::utf8_length(JNIHandles::resolve_non_null(string)); | |
2035 DTRACE_PROBE1(hotspot_jni, GetStringUTFLength__return, ret); | |
2036 return ret; | |
2037 JNI_END | |
2038 | |
2039 | |
2040 JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy)) | |
2041 JNIWrapper("GetStringUTFChars"); | |
2042 DTRACE_PROBE3(hotspot_jni, GetStringUTFChars__entry, env, string, isCopy); | |
2043 ResourceMark rm; | |
2044 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(string)); | |
2045 int length = (int)strlen(str); | |
2046 char* result = AllocateHeap(length+1, "GetStringUTFChars"); | |
2047 strcpy(result, str); | |
2048 if (isCopy != NULL) *isCopy = JNI_TRUE; | |
2049 DTRACE_PROBE1(hotspot_jni, GetStringUTFChars__return, result); | |
2050 return result; | |
2051 JNI_END | |
2052 | |
2053 | |
2054 JNI_LEAF(void, jni_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *chars)) | |
2055 JNIWrapper("ReleaseStringUTFChars"); | |
2056 DTRACE_PROBE3(hotspot_jni, ReleaseStringUTFChars__entry, env, str, chars); | |
2057 if (chars != NULL) { | |
2058 FreeHeap((char*) chars); | |
2059 } | |
2060 DTRACE_PROBE(hotspot_jni, ReleaseStringUTFChars__return); | |
2061 JNI_END | |
2062 | |
2063 | |
2064 JNI_QUICK_ENTRY(jsize, jni_GetArrayLength(JNIEnv *env, jarray array)) | |
2065 JNIWrapper("GetArrayLength"); | |
2066 DTRACE_PROBE2(hotspot_jni, GetArrayLength__entry, env, array); | |
2067 arrayOop a = arrayOop(JNIHandles::resolve_non_null(array)); | |
2068 assert(a->is_array(), "must be array"); | |
2069 jsize ret = a->length(); | |
2070 DTRACE_PROBE1(hotspot_jni, GetArrayLength__return, ret); | |
2071 return ret; | |
2072 JNI_END | |
2073 | |
2074 | |
2075 // | |
2076 // Object Array Operations | |
2077 // | |
2078 | |
2079 DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray); | |
2080 | |
2081 JNI_ENTRY(jobjectArray, jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement)) | |
2082 JNIWrapper("NewObjectArray"); | |
2083 DTRACE_PROBE4(hotspot_jni, NewObjectArray__entry, env, length, elementClass, initialElement); | |
2084 jobjectArray ret = NULL; | |
2085 DT_RETURN_MARK(NewObjectArray, jobjectArray, (const jobjectArray&)ret); | |
2086 KlassHandle ek(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(elementClass))); | |
2087 klassOop ako = Klass::cast(ek())->array_klass(CHECK_NULL); | |
2088 KlassHandle ak = KlassHandle(THREAD, ako); | |
2089 objArrayKlass::cast(ak())->initialize(CHECK_NULL); | |
2090 objArrayOop result = objArrayKlass::cast(ak())->allocate(length, CHECK_NULL); | |
2091 oop initial_value = JNIHandles::resolve(initialElement); | |
2092 if (initial_value != NULL) { // array already initialized with NULL | |
2093 for (int index = 0; index < length; index++) { | |
2094 result->obj_at_put(index, initial_value); | |
2095 } | |
2096 } | |
2097 ret = (jobjectArray) JNIHandles::make_local(env, result); | |
2098 return ret; | |
2099 JNI_END | |
2100 | |
2101 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject); | |
2102 | |
2103 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index)) | |
2104 JNIWrapper("GetObjectArrayElement"); | |
2105 DTRACE_PROBE3(hotspot_jni, GetObjectArrayElement__entry, env, array, index); | |
2106 jobject ret = NULL; | |
2107 DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret); | |
2108 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array)); | |
2109 if (a->is_within_bounds(index)) { | |
2110 jobject ret = JNIHandles::make_local(env, a->obj_at(index)); | |
2111 return ret; | |
2112 } else { | |
2113 char buf[jintAsStringSize]; | |
2114 sprintf(buf, "%d", index); | |
2115 THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf); | |
2116 } | |
2117 JNI_END | |
2118 | |
2119 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement); | |
2120 | |
2121 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value)) | |
2122 JNIWrapper("SetObjectArrayElement"); | |
2123 DTRACE_PROBE4(hotspot_jni, SetObjectArrayElement__entry, env, array, index, value); | |
2124 DT_VOID_RETURN_MARK(SetObjectArrayElement); | |
2125 | |
2126 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array)); | |
2127 oop v = JNIHandles::resolve(value); | |
2128 if (a->is_within_bounds(index)) { | |
2129 if (v == NULL || v->is_a(objArrayKlass::cast(a->klass())->element_klass())) { | |
2130 a->obj_at_put(index, v); | |
2131 } else { | |
2132 THROW(vmSymbols::java_lang_ArrayStoreException()); | |
2133 } | |
2134 } else { | |
2135 char buf[jintAsStringSize]; | |
2136 sprintf(buf, "%d", index); | |
2137 THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), buf); | |
2138 } | |
2139 JNI_END | |
2140 | |
2141 | |
2142 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result) \ | |
2143 \ | |
2144 DT_RETURN_MARK_DECL_FOR(Result, New##Result##Array, Return);\ | |
2145 \ | |
2146 JNI_ENTRY(Return, \ | |
2147 jni_New##Result##Array(JNIEnv *env, jsize len)) \ | |
2148 JNIWrapper("New" XSTR(Result) "Array"); \ | |
2149 DTRACE_PROBE2(hotspot_jni, New##Result##Array__entry, env, len);\ | |
2150 Return ret = NULL;\ | |
2151 DT_RETURN_MARK_FOR(Result, New##Result##Array, Return, (const Return&)ret);\ | |
2152 \ | |
2153 oop obj= oopFactory::Allocator(len, CHECK_0); \ | |
2154 ret = (Return) JNIHandles::make_local(env, obj); \ | |
2155 return ret;\ | |
2156 JNI_END | |
2157 | |
2158 DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray, Boolean) | |
2159 DEFINE_NEWSCALARARRAY(jbyteArray, new_byteArray, Byte) | |
2160 DEFINE_NEWSCALARARRAY(jshortArray, new_shortArray, Short) | |
2161 DEFINE_NEWSCALARARRAY(jcharArray, new_charArray, Char) | |
2162 DEFINE_NEWSCALARARRAY(jintArray, new_intArray, Int) | |
2163 DEFINE_NEWSCALARARRAY(jlongArray, new_longArray, Long) | |
2164 DEFINE_NEWSCALARARRAY(jfloatArray, new_singleArray, Float) | |
2165 DEFINE_NEWSCALARARRAY(jdoubleArray, new_doubleArray, Double) | |
2166 | |
2167 | |
2168 // Return an address which will fault if the caller writes to it. | |
2169 | |
2170 static char* get_bad_address() { | |
2171 static char* bad_address = NULL; | |
2172 if (bad_address == NULL) { | |
2173 size_t size = os::vm_allocation_granularity(); | |
2174 bad_address = os::reserve_memory(size); | |
2175 if (bad_address != NULL) { | |
2176 os::commit_memory(bad_address, size); | |
2177 os::protect_memory(bad_address, size); | |
2178 } | |
2179 } | |
2180 return bad_address; | |
2181 } | |
2182 | |
2183 | |
2184 #define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag) \ | |
2185 \ | |
2186 JNI_QUICK_ENTRY(ElementType*, \ | |
2187 jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \ | |
2188 JNIWrapper("Get" XSTR(Result) "ArrayElements"); \ | |
2189 DTRACE_PROBE3(hotspot_jni, Get##Result##ArrayElements__entry, env, array, isCopy);\ | |
2190 /* allocate an chunk of memory in c land */ \ | |
2191 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \ | |
2192 ElementType* result; \ | |
2193 int len = a->length(); \ | |
2194 if (len == 0) { \ | |
2195 /* Empty array: legal but useless, can't return NULL. \ | |
2196 * Return a pointer to something useless. \ | |
2197 * Avoid asserts in typeArrayOop. */ \ | |
2198 result = (ElementType*)get_bad_address(); \ | |
2199 } else { \ | |
2200 result = NEW_C_HEAP_ARRAY(ElementType, len); \ | |
2201 /* copy the array to the c chunk */ \ | |
2202 memcpy(result, a->Tag##_at_addr(0), sizeof(ElementType)*len); \ | |
2203 } \ | |
2204 if (isCopy) *isCopy = JNI_TRUE; \ | |
2205 DTRACE_PROBE1(hotspot_jni, Get##Result##ArrayElements__return, result);\ | |
2206 return result; \ | |
2207 JNI_END | |
2208 | |
2209 DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool) | |
2210 DEFINE_GETSCALARARRAYELEMENTS(T_BYTE, jbyte, Byte, byte) | |
2211 DEFINE_GETSCALARARRAYELEMENTS(T_SHORT, jshort, Short, short) | |
2212 DEFINE_GETSCALARARRAYELEMENTS(T_CHAR, jchar, Char, char) | |
2213 DEFINE_GETSCALARARRAYELEMENTS(T_INT, jint, Int, int) | |
2214 DEFINE_GETSCALARARRAYELEMENTS(T_LONG, jlong, Long, long) | |
2215 DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT, jfloat, Float, float) | |
2216 DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE, jdouble, Double, double) | |
2217 | |
2218 | |
2219 #define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag) \ | |
2220 \ | |
2221 JNI_QUICK_ENTRY(void, \ | |
2222 jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \ | |
2223 ElementType *buf, jint mode)) \ | |
2224 JNIWrapper("Release" XSTR(Result) "ArrayElements"); \ | |
2225 DTRACE_PROBE4(hotspot_jni, Release##Result##ArrayElements__entry, env, array, buf, mode);\ | |
2226 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \ | |
2227 int len = a->length(); \ | |
2228 if (len != 0) { /* Empty array: nothing to free or copy. */ \ | |
2229 if ((mode == 0) || (mode == JNI_COMMIT)) { \ | |
2230 memcpy(a->Tag##_at_addr(0), buf, sizeof(ElementType)*len); \ | |
2231 } \ | |
2232 if ((mode == 0) || (mode == JNI_ABORT)) { \ | |
2233 FreeHeap(buf); \ | |
2234 } \ | |
2235 } \ | |
2236 DTRACE_PROBE(hotspot_jni, Release##Result##ArrayElements__return);\ | |
2237 JNI_END | |
2238 | |
2239 DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool) | |
2240 DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE, jbyte, Byte, byte) | |
2241 DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT, jshort, Short, short) | |
2242 DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR, jchar, Char, char) | |
2243 DEFINE_RELEASESCALARARRAYELEMENTS(T_INT, jint, Int, int) | |
2244 DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG, jlong, Long, long) | |
2245 DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT, jfloat, Float, float) | |
2246 DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE, jdouble, Double, double) | |
2247 | |
2248 #define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag) \ | |
2249 DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion);\ | |
2250 \ | |
2251 JNI_ENTRY(void, \ | |
2252 jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \ | |
2253 jsize len, ElementType *buf)) \ | |
2254 JNIWrapper("Get" XSTR(Result) "ArrayRegion"); \ | |
2255 DTRACE_PROBE5(hotspot_jni, Get##Result##ArrayRegion__entry, env, array, start, len, buf);\ | |
2256 DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \ | |
2257 typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \ | |
2258 if (start < 0 || len < 0 || ((unsigned int)start + (unsigned int)len > (unsigned int)src->length())) { \ | |
2259 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \ | |
2260 } else { \ | |
2261 if (len > 0) { \ | |
2262 int sc = typeArrayKlass::cast(src->klass())->log2_element_size(); \ | |
2263 memcpy((u_char*) buf, \ | |
2264 (u_char*) src->Tag##_at_addr(start), \ | |
2265 len << sc); \ | |
2266 } \ | |
2267 } \ | |
2268 JNI_END | |
2269 | |
2270 DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool) | |
2271 DEFINE_GETSCALARARRAYREGION(T_BYTE, jbyte, Byte, byte) | |
2272 DEFINE_GETSCALARARRAYREGION(T_SHORT, jshort, Short, short) | |
2273 DEFINE_GETSCALARARRAYREGION(T_CHAR, jchar, Char, char) | |
2274 DEFINE_GETSCALARARRAYREGION(T_INT, jint, Int, int) | |
2275 DEFINE_GETSCALARARRAYREGION(T_LONG, jlong, Long, long) | |
2276 DEFINE_GETSCALARARRAYREGION(T_FLOAT, jfloat, Float, float) | |
2277 DEFINE_GETSCALARARRAYREGION(T_DOUBLE, jdouble, Double, double) | |
2278 | |
2279 #define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag) \ | |
2280 DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion);\ | |
2281 \ | |
2282 JNI_ENTRY(void, \ | |
2283 jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \ | |
2284 jsize len, const ElementType *buf)) \ | |
2285 JNIWrapper("Set" XSTR(Result) "ArrayRegion"); \ | |
2286 DTRACE_PROBE5(hotspot_jni, Set##Result##ArrayRegion__entry, env, array, start, len, buf);\ | |
2287 DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \ | |
2288 typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \ | |
2289 if (start < 0 || len < 0 || ((unsigned int)start + (unsigned int)len > (unsigned int)dst->length())) { \ | |
2290 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); \ | |
2291 } else { \ | |
2292 if (len > 0) { \ | |
2293 int sc = typeArrayKlass::cast(dst->klass())->log2_element_size(); \ | |
2294 memcpy((u_char*) dst->Tag##_at_addr(start), \ | |
2295 (u_char*) buf, \ | |
2296 len << sc); \ | |
2297 } \ | |
2298 } \ | |
2299 JNI_END | |
2300 | |
2301 DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool) | |
2302 DEFINE_SETSCALARARRAYREGION(T_BYTE, jbyte, Byte, byte) | |
2303 DEFINE_SETSCALARARRAYREGION(T_SHORT, jshort, Short, short) | |
2304 DEFINE_SETSCALARARRAYREGION(T_CHAR, jchar, Char, char) | |
2305 DEFINE_SETSCALARARRAYREGION(T_INT, jint, Int, int) | |
2306 DEFINE_SETSCALARARRAYREGION(T_LONG, jlong, Long, long) | |
2307 DEFINE_SETSCALARARRAYREGION(T_FLOAT, jfloat, Float, float) | |
2308 DEFINE_SETSCALARARRAYREGION(T_DOUBLE, jdouble, Double, double) | |
2309 | |
2310 | |
2311 // | |
2312 // Interception of natives | |
2313 // | |
2314 | |
2315 // The RegisterNatives call being attempted tried to register with a method that | |
2316 // is not native. Ask JVM TI what prefixes have been specified. Then check | |
2317 // to see if the native method is now wrapped with the prefixes. See the | |
2318 // SetNativeMethodPrefix(es) functions in the JVM TI Spec for details. | |
2319 static methodOop find_prefixed_native(KlassHandle k, | |
2320 symbolHandle name, symbolHandle signature, TRAPS) { | |
2321 ResourceMark rm(THREAD); | |
2322 methodOop method; | |
2323 int name_len = name->utf8_length(); | |
2324 char* name_str = name->as_utf8(); | |
2325 int prefix_count; | |
2326 char** prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count); | |
2327 for (int i = 0; i < prefix_count; i++) { | |
2328 char* prefix = prefixes[i]; | |
2329 int prefix_len = (int)strlen(prefix); | |
2330 | |
2331 // try adding this prefix to the method name and see if it matches another method name | |
2332 int trial_len = name_len + prefix_len; | |
2333 char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1); | |
2334 strcpy(trial_name_str, prefix); | |
2335 strcat(trial_name_str, name_str); | |
2336 symbolHandle trial_name(THREAD, SymbolTable::probe(trial_name_str, trial_len)); | |
2337 if (trial_name.is_null()) { | |
2338 continue; // no such symbol, so this prefix wasn't used, try the next prefix | |
2339 } | |
2340 method = Klass::cast(k())->lookup_method(trial_name(), signature()); | |
2341 if (method == NULL) { | |
2342 continue; // signature doesn't match, try the next prefix | |
2343 } | |
2344 if (method->is_native()) { | |
2345 method->set_is_prefixed_native(); | |
2346 return method; // wahoo, we found a prefixed version of the method, return it | |
2347 } | |
2348 // found as non-native, so prefix is good, add it, probably just need more prefixes | |
2349 name_len = trial_len; | |
2350 name_str = trial_name_str; | |
2351 } | |
2352 return NULL; // not found | |
2353 } | |
2354 | |
2355 static bool register_native(KlassHandle k, symbolHandle name, symbolHandle signature, address entry, TRAPS) { | |
2356 methodOop method = Klass::cast(k())->lookup_method(name(), signature()); | |
2357 if (method == NULL) { | |
2358 ResourceMark rm; | |
2359 stringStream st; | |
2360 st.print("Method %s name or signature does not match", | |
2361 methodOopDesc::name_and_sig_as_C_string(Klass::cast(k()), name(), signature())); | |
2362 THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false); | |
2363 } | |
2364 if (!method->is_native()) { | |
2365 // trying to register to a non-native method, see if a JVM TI agent has added prefix(es) | |
2366 method = find_prefixed_native(k, name, signature, THREAD); | |
2367 if (method == NULL) { | |
2368 ResourceMark rm; | |
2369 stringStream st; | |
2370 st.print("Method %s is not declared as native", | |
2371 methodOopDesc::name_and_sig_as_C_string(Klass::cast(k()), name(), signature())); | |
2372 THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), false); | |
2373 } | |
2374 } | |
2375 | |
2376 if (entry != NULL) { | |
2377 method->set_native_function(entry, | |
2378 methodOopDesc::native_bind_event_is_interesting); | |
2379 } else { | |
2380 method->clear_native_function(); | |
2381 } | |
2382 if (PrintJNIResolving) { | |
2383 ResourceMark rm(THREAD); | |
2384 tty->print_cr("[Registering JNI native method %s.%s]", | |
2385 Klass::cast(method->method_holder())->external_name(), | |
2386 method->name()->as_C_string()); | |
2387 } | |
2388 return true; | |
2389 } | |
2390 | |
2391 DT_RETURN_MARK_DECL(RegisterNatives, jint); | |
2392 | |
2393 JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz, | |
2394 const JNINativeMethod *methods, | |
2395 jint nMethods)) | |
2396 JNIWrapper("RegisterNatives"); | |
2397 DTRACE_PROBE4(hotspot_jni, RegisterNatives__entry, env, clazz, methods, nMethods); | |
2398 jint ret = 0; | |
2399 DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret); | |
2400 | |
2401 KlassHandle h_k(thread, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz))); | |
2402 | |
2403 for (int index = 0; index < nMethods; index++) { | |
2404 const char* meth_name = methods[index].name; | |
2405 const char* meth_sig = methods[index].signature; | |
2406 int meth_name_len = (int)strlen(meth_name); | |
2407 | |
2408 // The class should have been loaded (we have an instance of the class | |
2409 // passed in) so the method and signature should already be in the symbol | |
2410 // table. If they're not there, the method doesn't exist. | |
2411 symbolHandle name(THREAD, SymbolTable::probe(meth_name, meth_name_len)); | |
2412 symbolHandle signature(THREAD, SymbolTable::probe(meth_sig, (int)strlen(meth_sig))); | |
2413 | |
2414 if (name.is_null() || signature.is_null()) { | |
2415 ResourceMark rm; | |
2416 stringStream st; | |
2417 st.print("Method %s.%s%s not found", Klass::cast(h_k())->external_name(), meth_name, meth_sig); | |
2418 // Must return negative value on failure | |
2419 THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1); | |
2420 } | |
2421 | |
2422 bool res = register_native(h_k, name, signature, | |
2423 (address) methods[index].fnPtr, THREAD); | |
2424 if (!res) { | |
2425 ret = -1; | |
2426 break; | |
2427 } | |
2428 } | |
2429 return ret; | |
2430 JNI_END | |
2431 | |
2432 | |
2433 JNI_ENTRY(jint, jni_UnregisterNatives(JNIEnv *env, jclass clazz)) | |
2434 JNIWrapper("UnregisterNatives"); | |
2435 DTRACE_PROBE2(hotspot_jni, UnregisterNatives__entry, env, clazz); | |
2436 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(clazz)); | |
2437 //%note jni_2 | |
2438 if (Klass::cast(k)->oop_is_instance()) { | |
2439 for (int index = 0; index < instanceKlass::cast(k)->methods()->length(); index++) { | |
2440 methodOop m = methodOop(instanceKlass::cast(k)->methods()->obj_at(index)); | |
2441 if (m->is_native()) { | |
2442 m->clear_native_function(); | |
2443 m->set_signature_handler(NULL); | |
2444 } | |
2445 } | |
2446 } | |
2447 DTRACE_PROBE1(hotspot_jni, UnregisterNatives__return, 0); | |
2448 return 0; | |
2449 JNI_END | |
2450 | |
2451 // | |
2452 // Monitor functions | |
2453 // | |
2454 | |
2455 DT_RETURN_MARK_DECL(MonitorEnter, jint); | |
2456 | |
2457 JNI_ENTRY(jint, jni_MonitorEnter(JNIEnv *env, jobject jobj)) | |
2458 DTRACE_PROBE2(hotspot_jni, MonitorEnter__entry, env, jobj); | |
2459 jint ret = JNI_ERR; | |
2460 DT_RETURN_MARK(MonitorEnter, jint, (const jint&)ret); | |
2461 | |
2462 // If the object is null, we can't do anything with it | |
2463 if (jobj == NULL) { | |
2464 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR); | |
2465 } | |
2466 | |
2467 Handle obj(thread, JNIHandles::resolve_non_null(jobj)); | |
2468 ObjectSynchronizer::jni_enter(obj, CHECK_(JNI_ERR)); | |
2469 ret = JNI_OK; | |
2470 return ret; | |
2471 JNI_END | |
2472 | |
2473 DT_RETURN_MARK_DECL(MonitorExit, jint); | |
2474 | |
2475 JNI_ENTRY(jint, jni_MonitorExit(JNIEnv *env, jobject jobj)) | |
2476 DTRACE_PROBE2(hotspot_jni, MonitorExit__entry, env, jobj); | |
2477 jint ret = JNI_ERR; | |
2478 DT_RETURN_MARK(MonitorExit, jint, (const jint&)ret); | |
2479 | |
2480 // Don't do anything with a null object | |
2481 if (jobj == NULL) { | |
2482 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR); | |
2483 } | |
2484 | |
2485 Handle obj(THREAD, JNIHandles::resolve_non_null(jobj)); | |
2486 ObjectSynchronizer::jni_exit(obj(), CHECK_(JNI_ERR)); | |
2487 | |
2488 ret = JNI_OK; | |
2489 return ret; | |
2490 JNI_END | |
2491 | |
2492 // | |
2493 // Extensions | |
2494 // | |
2495 | |
2496 DT_VOID_RETURN_MARK_DECL(GetStringRegion); | |
2497 | |
2498 JNI_ENTRY(void, jni_GetStringRegion(JNIEnv *env, jstring string, jsize start, jsize len, jchar *buf)) | |
2499 JNIWrapper("GetStringRegion"); | |
2500 DTRACE_PROBE5(hotspot_jni, GetStringRegion__entry, env, string, start, len, buf); | |
2501 DT_VOID_RETURN_MARK(GetStringRegion); | |
2502 oop s = JNIHandles::resolve_non_null(string); | |
2503 int s_len = java_lang_String::length(s); | |
2504 if (start < 0 || len < 0 || start + len > s_len) { | |
2505 THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException()); | |
2506 } else { | |
2507 if (len > 0) { | |
2508 int s_offset = java_lang_String::offset(s); | |
2509 typeArrayOop s_value = java_lang_String::value(s); | |
2510 memcpy(buf, s_value->char_at_addr(s_offset+start), sizeof(jchar)*len); | |
2511 } | |
2512 } | |
2513 JNI_END | |
2514 | |
2515 DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion); | |
2516 | |
2517 JNI_ENTRY(void, jni_GetStringUTFRegion(JNIEnv *env, jstring string, jsize start, jsize len, char *buf)) | |
2518 JNIWrapper("GetStringUTFRegion"); | |
2519 DTRACE_PROBE5(hotspot_jni, GetStringUTFRegion__entry, env, string, start, len, buf); | |
2520 DT_VOID_RETURN_MARK(GetStringUTFRegion); | |
2521 oop s = JNIHandles::resolve_non_null(string); | |
2522 int s_len = java_lang_String::length(s); | |
2523 if (start < 0 || len < 0 || start + len > s_len) { | |
2524 THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException()); | |
2525 } else { | |
2526 //%note jni_7 | |
2527 if (len > 0) { | |
2528 ResourceMark rm(THREAD); | |
2529 char *utf_region = java_lang_String::as_utf8_string(s, start, len); | |
2530 int utf_len = (int)strlen(utf_region); | |
2531 memcpy(buf, utf_region, utf_len); | |
2532 buf[utf_len] = 0; | |
2533 } else { | |
2534 // JDK null-terminates the buffer even in len is zero | |
2535 if (buf != NULL) { | |
2536 buf[0] = 0; | |
2537 } | |
2538 } | |
2539 } | |
2540 JNI_END | |
2541 | |
2542 | |
2543 JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy)) | |
2544 JNIWrapper("GetPrimitiveArrayCritical"); | |
2545 DTRACE_PROBE3(hotspot_jni, GetPrimitiveArrayCritical__entry, env, array, isCopy); | |
2546 GC_locker::lock_critical(thread); | |
2547 if (isCopy != NULL) { | |
2548 *isCopy = JNI_FALSE; | |
2549 } | |
2550 oop a = JNIHandles::resolve_non_null(array); | |
2551 assert(a->is_array(), "just checking"); | |
2552 BasicType type; | |
2553 if (a->is_objArray()) { | |
2554 type = T_OBJECT; | |
2555 } else { | |
2556 type = typeArrayKlass::cast(a->klass())->element_type(); | |
2557 } | |
2558 void* ret = arrayOop(a)->base(type); | |
2559 DTRACE_PROBE1(hotspot_jni, GetPrimitiveArrayCritical__return, ret); | |
2560 return ret; | |
2561 JNI_END | |
2562 | |
2563 | |
2564 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode)) | |
2565 JNIWrapper("ReleasePrimitiveArrayCritical"); | |
2566 DTRACE_PROBE4(hotspot_jni, ReleasePrimitiveArrayCritical__entry, env, array, carray, mode); | |
2567 // The array, carray and mode arguments are ignored | |
2568 GC_locker::unlock_critical(thread); | |
2569 DTRACE_PROBE(hotspot_jni, ReleasePrimitiveArrayCritical__return); | |
2570 JNI_END | |
2571 | |
2572 | |
2573 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy)) | |
2574 JNIWrapper("GetStringCritical"); | |
2575 DTRACE_PROBE3(hotspot_jni, GetStringCritical__entry, env, string, isCopy); | |
2576 GC_locker::lock_critical(thread); | |
2577 if (isCopy != NULL) { | |
2578 *isCopy = JNI_FALSE; | |
2579 } | |
2580 oop s = JNIHandles::resolve_non_null(string); | |
2581 int s_len = java_lang_String::length(s); | |
2582 typeArrayOop s_value = java_lang_String::value(s); | |
2583 int s_offset = java_lang_String::offset(s); | |
2584 const jchar* ret; | |
2585 if (s_len > 0) { | |
2586 ret = s_value->char_at_addr(s_offset); | |
2587 } else { | |
2588 ret = (jchar*) s_value->base(T_CHAR); | |
2589 } | |
2590 DTRACE_PROBE1(hotspot_jni, GetStringCritical__return, ret); | |
2591 return ret; | |
2592 JNI_END | |
2593 | |
2594 | |
2595 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars)) | |
2596 JNIWrapper("ReleaseStringCritical"); | |
2597 DTRACE_PROBE3(hotspot_jni, ReleaseStringCritical__entry, env, str, chars); | |
2598 // The str and chars arguments are ignored | |
2599 GC_locker::unlock_critical(thread); | |
2600 DTRACE_PROBE(hotspot_jni, ReleaseStringCritical__return); | |
2601 JNI_END | |
2602 | |
2603 | |
2604 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref)) | |
2605 JNIWrapper("jni_NewWeakGlobalRef"); | |
2606 DTRACE_PROBE2(hotspot_jni, NewWeakGlobalRef__entry, env, ref); | |
2607 Handle ref_handle(thread, JNIHandles::resolve(ref)); | |
2608 jweak ret = JNIHandles::make_weak_global(ref_handle); | |
2609 DTRACE_PROBE1(hotspot_jni, NewWeakGlobalRef__return, ret); | |
2610 return ret; | |
2611 JNI_END | |
2612 | |
2613 // Must be JNI_ENTRY (with HandleMark) | |
2614 JNI_ENTRY(void, jni_DeleteWeakGlobalRef(JNIEnv *env, jweak ref)) | |
2615 JNIWrapper("jni_DeleteWeakGlobalRef"); | |
2616 DTRACE_PROBE2(hotspot_jni, DeleteWeakGlobalRef__entry, env, ref); | |
2617 JNIHandles::destroy_weak_global(ref); | |
2618 DTRACE_PROBE(hotspot_jni, DeleteWeakGlobalRef__return); | |
2619 JNI_END | |
2620 | |
2621 | |
2622 JNI_QUICK_ENTRY(jboolean, jni_ExceptionCheck(JNIEnv *env)) | |
2623 JNIWrapper("jni_ExceptionCheck"); | |
2624 DTRACE_PROBE1(hotspot_jni, ExceptionCheck__entry, env); | |
2625 jni_check_async_exceptions(thread); | |
2626 jboolean ret = (thread->has_pending_exception()) ? JNI_TRUE : JNI_FALSE; | |
2627 DTRACE_PROBE1(hotspot_jni, ExceptionCheck__return, ret); | |
2628 return ret; | |
2629 JNI_END | |
2630 | |
2631 | |
2632 // Initialization state for three routines below relating to | |
2633 // java.nio.DirectBuffers | |
2634 static jint directBufferSupportInitializeStarted = 0; | |
2635 static volatile jint directBufferSupportInitializeEnded = 0; | |
2636 static volatile jint directBufferSupportInitializeFailed = 0; | |
2637 static jclass bufferClass = NULL; | |
2638 static jclass directBufferClass = NULL; | |
2639 static jclass directByteBufferClass = NULL; | |
2640 static jmethodID directByteBufferConstructor = NULL; | |
2641 static jfieldID directBufferAddressField = NULL; | |
2642 static jfieldID bufferCapacityField = NULL; | |
2643 | |
2644 static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) { | |
2645 Handle loader; // null (bootstrap) loader | |
2646 Handle protection_domain; // null protection domain | |
2647 | |
2648 symbolHandle sym = oopFactory::new_symbol_handle(name, CHECK_NULL); | |
2649 return find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL); | |
2650 } | |
2651 | |
2652 // These lookups are done with the NULL (bootstrap) ClassLoader to | |
2653 // circumvent any security checks that would be done by jni_FindClass. | |
2654 JNI_ENTRY(bool, lookupDirectBufferClasses(JNIEnv* env)) | |
2655 { | |
2656 if ((bufferClass = lookupOne(env, "java/nio/Buffer", thread)) == NULL) { return false; } | |
2657 if ((directBufferClass = lookupOne(env, "sun/nio/ch/DirectBuffer", thread)) == NULL) { return false; } | |
2658 if ((directByteBufferClass = lookupOne(env, "java/nio/DirectByteBuffer", thread)) == NULL) { return false; } | |
2659 return true; | |
2660 } | |
2661 JNI_END | |
2662 | |
2663 | |
2664 static bool initializeDirectBufferSupport(JNIEnv* env, JavaThread* thread) { | |
2665 if (directBufferSupportInitializeFailed) { | |
2666 return false; | |
2667 } | |
2668 | |
2669 if (Atomic::cmpxchg(1, &directBufferSupportInitializeStarted, 0) == 0) { | |
2670 if (!lookupDirectBufferClasses(env)) { | |
2671 directBufferSupportInitializeFailed = 1; | |
2672 return false; | |
2673 } | |
2674 | |
2675 // Make global references for these | |
2676 bufferClass = (jclass) env->NewGlobalRef(bufferClass); | |
2677 directBufferClass = (jclass) env->NewGlobalRef(directBufferClass); | |
2678 directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass); | |
2679 | |
2680 // Get needed field and method IDs | |
2681 directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "<init>", "(JI)V"); | |
2682 directBufferAddressField = env->GetFieldID(bufferClass, "address", "J"); | |
2683 bufferCapacityField = env->GetFieldID(bufferClass, "capacity", "I"); | |
2684 | |
2685 if ((directByteBufferConstructor == NULL) || | |
2686 (directBufferAddressField == NULL) || | |
2687 (bufferCapacityField == NULL)) { | |
2688 directBufferSupportInitializeFailed = 1; | |
2689 return false; | |
2690 } | |
2691 | |
2692 directBufferSupportInitializeEnded = 1; | |
2693 } else { | |
2694 ThreadInVMfromNative tivn(thread); // set state as yield_all can call os:sleep | |
2695 while (!directBufferSupportInitializeEnded && !directBufferSupportInitializeFailed) { | |
2696 os::yield_all(); | |
2697 } | |
2698 } | |
2699 | |
2700 return !directBufferSupportInitializeFailed; | |
2701 } | |
2702 | |
2703 extern "C" jobject JNICALL jni_NewDirectByteBuffer(JNIEnv *env, void* address, jlong capacity) | |
2704 { | |
2705 // thread_from_jni_environment() will block if VM is gone. | |
2706 JavaThread* thread = JavaThread::thread_from_jni_environment(env); | |
2707 | |
2708 JNIWrapper("jni_NewDirectByteBuffer"); | |
2709 DTRACE_PROBE3(hotspot_jni, NewDirectByteBuffer__entry, env, address, capacity); | |
2710 | |
2711 if (!directBufferSupportInitializeEnded) { | |
2712 if (!initializeDirectBufferSupport(env, thread)) { | |
2713 DTRACE_PROBE1(hotspot_jni, NewDirectByteBuffer__return, NULL); | |
2714 return NULL; | |
2715 } | |
2716 } | |
2717 | |
2718 // Being paranoid about accidental sign extension on address | |
2719 jlong addr = (jlong) ((uintptr_t) address); | |
2720 // NOTE that package-private DirectByteBuffer constructor currently | |
2721 // takes int capacity | |
2722 jint cap = (jint) capacity; | |
2723 jobject ret = env->NewObject(directByteBufferClass, directByteBufferConstructor, addr, cap); | |
2724 DTRACE_PROBE1(hotspot_jni, NewDirectByteBuffer__return, ret); | |
2725 return ret; | |
2726 } | |
2727 | |
2728 DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*); | |
2729 | |
2730 extern "C" void* JNICALL jni_GetDirectBufferAddress(JNIEnv *env, jobject buf) | |
2731 { | |
2732 // thread_from_jni_environment() will block if VM is gone. | |
2733 JavaThread* thread = JavaThread::thread_from_jni_environment(env); | |
2734 | |
2735 JNIWrapper("jni_GetDirectBufferAddress"); | |
2736 DTRACE_PROBE2(hotspot_jni, GetDirectBufferAddress__entry, env, buf); | |
2737 void* ret = NULL; | |
2738 DT_RETURN_MARK(GetDirectBufferAddress, void*, (const void*&)ret); | |
2739 | |
2740 if (!directBufferSupportInitializeEnded) { | |
2741 if (!initializeDirectBufferSupport(env, thread)) { | |
2742 return 0; | |
2743 } | |
2744 } | |
2745 | |
2746 if ((buf != NULL) && (!env->IsInstanceOf(buf, directBufferClass))) { | |
2747 return 0; | |
2748 } | |
2749 | |
2750 ret = (void*)(intptr_t)env->GetLongField(buf, directBufferAddressField); | |
2751 return ret; | |
2752 } | |
2753 | |
2754 DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong); | |
2755 | |
2756 extern "C" jlong JNICALL jni_GetDirectBufferCapacity(JNIEnv *env, jobject buf) | |
2757 { | |
2758 // thread_from_jni_environment() will block if VM is gone. | |
2759 JavaThread* thread = JavaThread::thread_from_jni_environment(env); | |
2760 | |
2761 JNIWrapper("jni_GetDirectBufferCapacity"); | |
2762 DTRACE_PROBE2(hotspot_jni, GetDirectBufferCapacity__entry, env, buf); | |
2763 jlong ret = -1; | |
2764 DT_RETURN_MARK(GetDirectBufferCapacity, jlong, (const jlong&)ret); | |
2765 | |
2766 if (!directBufferSupportInitializeEnded) { | |
2767 if (!initializeDirectBufferSupport(env, thread)) { | |
2768 ret = 0; | |
2769 return ret; | |
2770 } | |
2771 } | |
2772 | |
2773 if (buf == NULL) { | |
2774 return -1; | |
2775 } | |
2776 | |
2777 if (!env->IsInstanceOf(buf, directBufferClass)) { | |
2778 return -1; | |
2779 } | |
2780 | |
2781 // NOTE that capacity is currently an int in the implementation | |
2782 ret = env->GetIntField(buf, bufferCapacityField); | |
2783 return ret; | |
2784 } | |
2785 | |
2786 | |
2787 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env)) | |
2788 JNIWrapper("GetVersion"); | |
2789 DTRACE_PROBE1(hotspot_jni, GetVersion__entry, env); | |
2790 DTRACE_PROBE1(hotspot_jni, GetVersion__return, CurrentVersion); | |
2791 return CurrentVersion; | |
2792 JNI_END | |
2793 | |
2794 extern struct JavaVM_ main_vm; | |
2795 | |
2796 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm)) | |
2797 JNIWrapper("jni_GetJavaVM"); | |
2798 DTRACE_PROBE2(hotspot_jni, GetJavaVM__entry, env, vm); | |
2799 *vm = (JavaVM *)(&main_vm); | |
2800 DTRACE_PROBE1(hotspot_jni, GetJavaVM__return, JNI_OK); | |
2801 return JNI_OK; | |
2802 JNI_END | |
2803 | |
2804 // Structure containing all jni functions | |
2805 struct JNINativeInterface_ jni_NativeInterface = { | |
2806 NULL, | |
2807 NULL, | |
2808 NULL, | |
2809 | |
2810 NULL, | |
2811 | |
2812 jni_GetVersion, | |
2813 | |
2814 jni_DefineClass, | |
2815 jni_FindClass, | |
2816 | |
2817 jni_FromReflectedMethod, | |
2818 jni_FromReflectedField, | |
2819 | |
2820 jni_ToReflectedMethod, | |
2821 | |
2822 jni_GetSuperclass, | |
2823 jni_IsAssignableFrom, | |
2824 | |
2825 jni_ToReflectedField, | |
2826 | |
2827 jni_Throw, | |
2828 jni_ThrowNew, | |
2829 jni_ExceptionOccurred, | |
2830 jni_ExceptionDescribe, | |
2831 jni_ExceptionClear, | |
2832 jni_FatalError, | |
2833 | |
2834 jni_PushLocalFrame, | |
2835 jni_PopLocalFrame, | |
2836 | |
2837 jni_NewGlobalRef, | |
2838 jni_DeleteGlobalRef, | |
2839 jni_DeleteLocalRef, | |
2840 jni_IsSameObject, | |
2841 | |
2842 jni_NewLocalRef, | |
2843 jni_EnsureLocalCapacity, | |
2844 | |
2845 jni_AllocObject, | |
2846 jni_NewObject, | |
2847 jni_NewObjectV, | |
2848 jni_NewObjectA, | |
2849 | |
2850 jni_GetObjectClass, | |
2851 jni_IsInstanceOf, | |
2852 | |
2853 jni_GetMethodID, | |
2854 | |
2855 jni_CallObjectMethod, | |
2856 jni_CallObjectMethodV, | |
2857 jni_CallObjectMethodA, | |
2858 jni_CallBooleanMethod, | |
2859 jni_CallBooleanMethodV, | |
2860 jni_CallBooleanMethodA, | |
2861 jni_CallByteMethod, | |
2862 jni_CallByteMethodV, | |
2863 jni_CallByteMethodA, | |
2864 jni_CallCharMethod, | |
2865 jni_CallCharMethodV, | |
2866 jni_CallCharMethodA, | |
2867 jni_CallShortMethod, | |
2868 jni_CallShortMethodV, | |
2869 jni_CallShortMethodA, | |
2870 jni_CallIntMethod, | |
2871 jni_CallIntMethodV, | |
2872 jni_CallIntMethodA, | |
2873 jni_CallLongMethod, | |
2874 jni_CallLongMethodV, | |
2875 jni_CallLongMethodA, | |
2876 jni_CallFloatMethod, | |
2877 jni_CallFloatMethodV, | |
2878 jni_CallFloatMethodA, | |
2879 jni_CallDoubleMethod, | |
2880 jni_CallDoubleMethodV, | |
2881 jni_CallDoubleMethodA, | |
2882 jni_CallVoidMethod, | |
2883 jni_CallVoidMethodV, | |
2884 jni_CallVoidMethodA, | |
2885 | |
2886 jni_CallNonvirtualObjectMethod, | |
2887 jni_CallNonvirtualObjectMethodV, | |
2888 jni_CallNonvirtualObjectMethodA, | |
2889 jni_CallNonvirtualBooleanMethod, | |
2890 jni_CallNonvirtualBooleanMethodV, | |
2891 jni_CallNonvirtualBooleanMethodA, | |
2892 jni_CallNonvirtualByteMethod, | |
2893 jni_CallNonvirtualByteMethodV, | |
2894 jni_CallNonvirtualByteMethodA, | |
2895 jni_CallNonvirtualCharMethod, | |
2896 jni_CallNonvirtualCharMethodV, | |
2897 jni_CallNonvirtualCharMethodA, | |
2898 jni_CallNonvirtualShortMethod, | |
2899 jni_CallNonvirtualShortMethodV, | |
2900 jni_CallNonvirtualShortMethodA, | |
2901 jni_CallNonvirtualIntMethod, | |
2902 jni_CallNonvirtualIntMethodV, | |
2903 jni_CallNonvirtualIntMethodA, | |
2904 jni_CallNonvirtualLongMethod, | |
2905 jni_CallNonvirtualLongMethodV, | |
2906 jni_CallNonvirtualLongMethodA, | |
2907 jni_CallNonvirtualFloatMethod, | |
2908 jni_CallNonvirtualFloatMethodV, | |
2909 jni_CallNonvirtualFloatMethodA, | |
2910 jni_CallNonvirtualDoubleMethod, | |
2911 jni_CallNonvirtualDoubleMethodV, | |
2912 jni_CallNonvirtualDoubleMethodA, | |
2913 jni_CallNonvirtualVoidMethod, | |
2914 jni_CallNonvirtualVoidMethodV, | |
2915 jni_CallNonvirtualVoidMethodA, | |
2916 | |
2917 jni_GetFieldID, | |
2918 | |
2919 jni_GetObjectField, | |
2920 jni_GetBooleanField, | |
2921 jni_GetByteField, | |
2922 jni_GetCharField, | |
2923 jni_GetShortField, | |
2924 jni_GetIntField, | |
2925 jni_GetLongField, | |
2926 jni_GetFloatField, | |
2927 jni_GetDoubleField, | |
2928 | |
2929 jni_SetObjectField, | |
2930 jni_SetBooleanField, | |
2931 jni_SetByteField, | |
2932 jni_SetCharField, | |
2933 jni_SetShortField, | |
2934 jni_SetIntField, | |
2935 jni_SetLongField, | |
2936 jni_SetFloatField, | |
2937 jni_SetDoubleField, | |
2938 | |
2939 jni_GetStaticMethodID, | |
2940 | |
2941 jni_CallStaticObjectMethod, | |
2942 jni_CallStaticObjectMethodV, | |
2943 jni_CallStaticObjectMethodA, | |
2944 jni_CallStaticBooleanMethod, | |
2945 jni_CallStaticBooleanMethodV, | |
2946 jni_CallStaticBooleanMethodA, | |
2947 jni_CallStaticByteMethod, | |
2948 jni_CallStaticByteMethodV, | |
2949 jni_CallStaticByteMethodA, | |
2950 jni_CallStaticCharMethod, | |
2951 jni_CallStaticCharMethodV, | |
2952 jni_CallStaticCharMethodA, | |
2953 jni_CallStaticShortMethod, | |
2954 jni_CallStaticShortMethodV, | |
2955 jni_CallStaticShortMethodA, | |
2956 jni_CallStaticIntMethod, | |
2957 jni_CallStaticIntMethodV, | |
2958 jni_CallStaticIntMethodA, | |
2959 jni_CallStaticLongMethod, | |
2960 jni_CallStaticLongMethodV, | |
2961 jni_CallStaticLongMethodA, | |
2962 jni_CallStaticFloatMethod, | |
2963 jni_CallStaticFloatMethodV, | |
2964 jni_CallStaticFloatMethodA, | |
2965 jni_CallStaticDoubleMethod, | |
2966 jni_CallStaticDoubleMethodV, | |
2967 jni_CallStaticDoubleMethodA, | |
2968 jni_CallStaticVoidMethod, | |
2969 jni_CallStaticVoidMethodV, | |
2970 jni_CallStaticVoidMethodA, | |
2971 | |
2972 jni_GetStaticFieldID, | |
2973 | |
2974 jni_GetStaticObjectField, | |
2975 jni_GetStaticBooleanField, | |
2976 jni_GetStaticByteField, | |
2977 jni_GetStaticCharField, | |
2978 jni_GetStaticShortField, | |
2979 jni_GetStaticIntField, | |
2980 jni_GetStaticLongField, | |
2981 jni_GetStaticFloatField, | |
2982 jni_GetStaticDoubleField, | |
2983 | |
2984 jni_SetStaticObjectField, | |
2985 jni_SetStaticBooleanField, | |
2986 jni_SetStaticByteField, | |
2987 jni_SetStaticCharField, | |
2988 jni_SetStaticShortField, | |
2989 jni_SetStaticIntField, | |
2990 jni_SetStaticLongField, | |
2991 jni_SetStaticFloatField, | |
2992 jni_SetStaticDoubleField, | |
2993 | |
2994 jni_NewString, | |
2995 jni_GetStringLength, | |
2996 jni_GetStringChars, | |
2997 jni_ReleaseStringChars, | |
2998 | |
2999 jni_NewStringUTF, | |
3000 jni_GetStringUTFLength, | |
3001 jni_GetStringUTFChars, | |
3002 jni_ReleaseStringUTFChars, | |
3003 | |
3004 jni_GetArrayLength, | |
3005 | |
3006 jni_NewObjectArray, | |
3007 jni_GetObjectArrayElement, | |
3008 jni_SetObjectArrayElement, | |
3009 | |
3010 jni_NewBooleanArray, | |
3011 jni_NewByteArray, | |
3012 jni_NewCharArray, | |
3013 jni_NewShortArray, | |
3014 jni_NewIntArray, | |
3015 jni_NewLongArray, | |
3016 jni_NewFloatArray, | |
3017 jni_NewDoubleArray, | |
3018 | |
3019 jni_GetBooleanArrayElements, | |
3020 jni_GetByteArrayElements, | |
3021 jni_GetCharArrayElements, | |
3022 jni_GetShortArrayElements, | |
3023 jni_GetIntArrayElements, | |
3024 jni_GetLongArrayElements, | |
3025 jni_GetFloatArrayElements, | |
3026 jni_GetDoubleArrayElements, | |
3027 | |
3028 jni_ReleaseBooleanArrayElements, | |
3029 jni_ReleaseByteArrayElements, | |
3030 jni_ReleaseCharArrayElements, | |
3031 jni_ReleaseShortArrayElements, | |
3032 jni_ReleaseIntArrayElements, | |
3033 jni_ReleaseLongArrayElements, | |
3034 jni_ReleaseFloatArrayElements, | |
3035 jni_ReleaseDoubleArrayElements, | |
3036 | |
3037 jni_GetBooleanArrayRegion, | |
3038 jni_GetByteArrayRegion, | |
3039 jni_GetCharArrayRegion, | |
3040 jni_GetShortArrayRegion, | |
3041 jni_GetIntArrayRegion, | |
3042 jni_GetLongArrayRegion, | |
3043 jni_GetFloatArrayRegion, | |
3044 jni_GetDoubleArrayRegion, | |
3045 | |
3046 jni_SetBooleanArrayRegion, | |
3047 jni_SetByteArrayRegion, | |
3048 jni_SetCharArrayRegion, | |
3049 jni_SetShortArrayRegion, | |
3050 jni_SetIntArrayRegion, | |
3051 jni_SetLongArrayRegion, | |
3052 jni_SetFloatArrayRegion, | |
3053 jni_SetDoubleArrayRegion, | |
3054 | |
3055 jni_RegisterNatives, | |
3056 jni_UnregisterNatives, | |
3057 | |
3058 jni_MonitorEnter, | |
3059 jni_MonitorExit, | |
3060 | |
3061 jni_GetJavaVM, | |
3062 | |
3063 jni_GetStringRegion, | |
3064 jni_GetStringUTFRegion, | |
3065 | |
3066 jni_GetPrimitiveArrayCritical, | |
3067 jni_ReleasePrimitiveArrayCritical, | |
3068 | |
3069 jni_GetStringCritical, | |
3070 jni_ReleaseStringCritical, | |
3071 | |
3072 jni_NewWeakGlobalRef, | |
3073 jni_DeleteWeakGlobalRef, | |
3074 | |
3075 jni_ExceptionCheck, | |
3076 | |
3077 jni_NewDirectByteBuffer, | |
3078 jni_GetDirectBufferAddress, | |
3079 jni_GetDirectBufferCapacity, | |
3080 | |
3081 // New 1_6 features | |
3082 | |
3083 jni_GetObjectRefType | |
3084 }; | |
3085 | |
3086 | |
3087 // For jvmti use to modify jni function table. | |
3088 // Java threads in native contiues to run until it is transitioned | |
3089 // to VM at safepoint. Before the transition or before it is blocked | |
3090 // for safepoint it may access jni function table. VM could crash if | |
3091 // any java thread access the jni function table in the middle of memcpy. | |
3092 // To avoid this each function pointers are copied automically. | |
3093 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) { | |
3094 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint"); | |
3095 intptr_t *a = (intptr_t *) jni_functions(); | |
3096 intptr_t *b = (intptr_t *) new_jni_NativeInterface; | |
3097 for (uint i=0; i < sizeof(struct JNINativeInterface_)/sizeof(void *); i++) { | |
3098 Atomic::store_ptr(*b++, a++); | |
3099 } | |
3100 } | |
3101 | |
3102 void quicken_jni_functions() { | |
3103 // Replace Get<Primitive>Field with fast versions | |
3104 if (UseFastJNIAccessors && !JvmtiExport::can_post_field_access() | |
3105 && !VerifyJNIFields && !TraceJNICalls && !CountJNICalls && !CheckJNICalls | |
3106 #if defined(_WINDOWS) && defined(IA32) && defined(COMPILER2) | |
3107 // windows x86 currently needs SEH wrapper and the gain of the fast | |
3108 // versions currently isn't certain for server vm on uniprocessor. | |
3109 && os::is_MP() | |
3110 #endif | |
3111 ) { | |
3112 address func; | |
3113 func = JNI_FastGetField::generate_fast_get_boolean_field(); | |
3114 if (func != (address)-1) { | |
3115 jni_NativeInterface.GetBooleanField = (GetBooleanField_t)func; | |
3116 } | |
3117 func = JNI_FastGetField::generate_fast_get_byte_field(); | |
3118 if (func != (address)-1) { | |
3119 jni_NativeInterface.GetByteField = (GetByteField_t)func; | |
3120 } | |
3121 func = JNI_FastGetField::generate_fast_get_char_field(); | |
3122 if (func != (address)-1) { | |
3123 jni_NativeInterface.GetCharField = (GetCharField_t)func; | |
3124 } | |
3125 func = JNI_FastGetField::generate_fast_get_short_field(); | |
3126 if (func != (address)-1) { | |
3127 jni_NativeInterface.GetShortField = (GetShortField_t)func; | |
3128 } | |
3129 func = JNI_FastGetField::generate_fast_get_int_field(); | |
3130 if (func != (address)-1) { | |
3131 jni_NativeInterface.GetIntField = (GetIntField_t)func; | |
3132 } | |
3133 func = JNI_FastGetField::generate_fast_get_long_field(); | |
3134 if (func != (address)-1) { | |
3135 jni_NativeInterface.GetLongField = (GetLongField_t)func; | |
3136 } | |
3137 func = JNI_FastGetField::generate_fast_get_float_field(); | |
3138 if (func != (address)-1) { | |
3139 jni_NativeInterface.GetFloatField = (GetFloatField_t)func; | |
3140 } | |
3141 func = JNI_FastGetField::generate_fast_get_double_field(); | |
3142 if (func != (address)-1) { | |
3143 jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func; | |
3144 } | |
3145 } | |
3146 } | |
3147 | |
3148 // Returns the function structure | |
3149 struct JNINativeInterface_* jni_functions() { | |
3150 #ifndef JNICHECK_KERNEL | |
3151 if (CheckJNICalls) return jni_functions_check(); | |
3152 #else // JNICHECK_KERNEL | |
3153 if (CheckJNICalls) warning("-Xcheck:jni is not supported in kernel vm."); | |
3154 #endif // JNICHECK_KERNEL | |
3155 return &jni_NativeInterface; | |
3156 } | |
3157 | |
3158 // Returns the function structure | |
3159 struct JNINativeInterface_* jni_functions_nocheck() { | |
3160 return &jni_NativeInterface; | |
3161 } | |
3162 | |
3163 | |
3164 // Invocation API | |
3165 | |
3166 | |
3167 // Forward declaration | |
3168 extern const struct JNIInvokeInterface_ jni_InvokeInterface; | |
3169 | |
3170 // Global invocation API vars | |
3171 volatile jint vm_created = 0; | |
3172 // Indicate whether it is safe to recreate VM | |
3173 volatile jint safe_to_recreate_vm = 1; | |
3174 struct JavaVM_ main_vm = {&jni_InvokeInterface}; | |
3175 | |
3176 | |
3177 #define JAVASTACKSIZE (400 * 1024) /* Default size of a thread java stack */ | |
3178 #define PROCSTACKSIZE 0 /* 0 means default size in HPI */ | |
3179 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL }; | |
3180 | |
3181 HS_DTRACE_PROBE_DECL1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, void*); | |
3182 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint); | |
3183 | |
3184 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *args_) { | |
3185 HS_DTRACE_PROBE1(hotspot_jni, GetDefaultJavaVMInitArgs__entry, args_); | |
3186 JDK1_1InitArgs *args = (JDK1_1InitArgs *)args_; | |
3187 jint ret = JNI_ERR; | |
3188 DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret); | |
3189 | |
3190 if (Threads::is_supported_jni_version(args->version)) { | |
3191 ret = JNI_OK; | |
3192 } | |
3193 // 1.1 style no longer supported in hotspot. | |
3194 // According the JNI spec, we should update args->version on return. | |
3195 // We also use the structure to communicate with launcher about default | |
3196 // stack size. | |
3197 if (args->version == JNI_VERSION_1_1) { | |
3198 args->version = JNI_VERSION_1_2; | |
3199 // javaStackSize is int in arguments structure | |
3200 assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow"); | |
3201 args->javaStackSize = (jint)(ThreadStackSize * K); | |
3202 } | |
3203 return ret; | |
3204 } | |
3205 | |
3206 HS_DTRACE_PROBE_DECL3(hotspot_jni, CreateJavaVM__entry, vm, penv, args); | |
3207 DT_RETURN_MARK_DECL(CreateJavaVM, jint); | |
3208 | |
3209 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) { | |
3210 HS_DTRACE_PROBE3(hotspot_jni, CreateJavaVM__entry, vm, penv, args); | |
3211 | |
3212 jint result = JNI_ERR; | |
3213 DT_RETURN_MARK(CreateJavaVM, jint, (const jint&)result); | |
3214 | |
3215 // At the moment it's only possible to have one Java VM, | |
3216 // since some of the runtime state is in global variables. | |
3217 | |
3218 // We cannot use our mutex locks here, since they only work on | |
3219 // Threads. We do an atomic compare and exchange to ensure only | |
3220 // one thread can call this method at a time | |
3221 | |
3222 // We use Atomic::xchg rather than Atomic::add/dec since on some platforms | |
3223 // the add/dec implementations are dependent on whether we are running | |
3224 // on a multiprocessor, and at this stage of initialization the os::is_MP | |
3225 // function used to determine this will always return false. Atomic::xchg | |
3226 // does not have this problem. | |
3227 if (Atomic::xchg(1, &vm_created) == 1) { | |
3228 return JNI_ERR; // already created, or create attempt in progress | |
3229 } | |
3230 if (Atomic::xchg(0, &safe_to_recreate_vm) == 0) { | |
3231 return JNI_ERR; // someone tried and failed and retry not allowed. | |
3232 } | |
3233 | |
3234 assert(vm_created == 1, "vm_created is true during the creation"); | |
3235 | |
3236 /** | |
3237 * Certain errors during initialization are recoverable and do not | |
3238 * prevent this method from being called again at a later time | |
3239 * (perhaps with different arguments). However, at a certain | |
3240 * point during initialization if an error occurs we cannot allow | |
3241 * this function to be called again (or it will crash). In those | |
3242 * situations, the 'canTryAgain' flag is set to false, which atomically | |
3243 * sets safe_to_recreate_vm to 1, such that any new call to | |
3244 * JNI_CreateJavaVM will immediately fail using the above logic. | |
3245 */ | |
3246 bool can_try_again = true; | |
3247 | |
3248 result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again); | |
3249 if (result == JNI_OK) { | |
3250 JavaThread *thread = JavaThread::current(); | |
3251 /* thread is thread_in_vm here */ | |
3252 *vm = (JavaVM *)(&main_vm); | |
3253 *(JNIEnv**)penv = thread->jni_environment(); | |
3254 | |
3255 // Tracks the time application was running before GC | |
3256 RuntimeService::record_application_start(); | |
3257 | |
3258 // Notify JVMTI | |
3259 if (JvmtiExport::should_post_thread_life()) { | |
3260 JvmtiExport::post_thread_start(thread); | |
3261 } | |
3262 // Check if we should compile all classes on bootclasspath | |
3263 NOT_PRODUCT(if (CompileTheWorld) ClassLoader::compile_the_world();) | |
3264 // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving. | |
3265 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native); | |
3266 } else { | |
3267 if (can_try_again) { | |
3268 // reset safe_to_recreate_vm to 1 so that retrial would be possible | |
3269 safe_to_recreate_vm = 1; | |
3270 } | |
3271 | |
3272 // Creation failed. We must reset vm_created | |
3273 *vm = 0; | |
3274 *(JNIEnv**)penv = 0; | |
3275 // reset vm_created last to avoid race condition. Use OrderAccess to | |
3276 // control both compiler and architectural-based reordering. | |
3277 OrderAccess::release_store(&vm_created, 0); | |
3278 } | |
3279 | |
3280 return result; | |
3281 } | |
3282 | |
3283 HS_DTRACE_PROBE_DECL3(hotspot_jni, GetCreatedJavaVMs__entry, \ | |
3284 JavaVM**, jsize, jsize*); | |
3285 HS_DTRACE_PROBE_DECL1(hotspot_jni, GetCreatedJavaVMs__return, jint); | |
3286 | |
3287 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vm_buf, jsize bufLen, jsize *numVMs) { | |
3288 // See bug 4367188, the wrapper can sometimes cause VM crashes | |
3289 // JNIWrapper("GetCreatedJavaVMs"); | |
3290 HS_DTRACE_PROBE3(hotspot_jni, GetCreatedJavaVMs__entry, \ | |
3291 vm_buf, bufLen, numVMs); | |
3292 if (vm_created) { | |
3293 if (numVMs != NULL) *numVMs = 1; | |
3294 if (bufLen > 0) *vm_buf = (JavaVM *)(&main_vm); | |
3295 } else { | |
3296 if (numVMs != NULL) *numVMs = 0; | |
3297 } | |
3298 HS_DTRACE_PROBE1(hotspot_jni, GetCreatedJavaVMs__return, JNI_OK); | |
3299 return JNI_OK; | |
3300 } | |
3301 | |
3302 extern "C" { | |
3303 | |
3304 DT_RETURN_MARK_DECL(DestroyJavaVM, jint); | |
3305 | |
3306 jint JNICALL jni_DestroyJavaVM(JavaVM *vm) { | |
3307 DTRACE_PROBE1(hotspot_jni, DestroyJavaVM__entry, vm); | |
3308 jint res = JNI_ERR; | |
3309 DT_RETURN_MARK(DestroyJavaVM, jint, (const jint&)res); | |
3310 | |
3311 if (!vm_created) { | |
3312 res = JNI_ERR; | |
3313 return res; | |
3314 } | |
3315 | |
3316 JNIWrapper("DestroyJavaVM"); | |
3317 JNIEnv *env; | |
3318 JavaVMAttachArgs destroyargs; | |
3319 destroyargs.version = CurrentVersion; | |
3320 destroyargs.name = (char *)"DestroyJavaVM"; | |
3321 destroyargs.group = NULL; | |
3322 res = vm->AttachCurrentThread((void **)&env, (void *)&destroyargs); | |
3323 if (res != JNI_OK) { | |
3324 return res; | |
3325 } | |
3326 | |
3327 // Since this is not a JVM_ENTRY we have to set the thread state manually before entering. | |
3328 JavaThread* thread = JavaThread::current(); | |
3329 ThreadStateTransition::transition_from_native(thread, _thread_in_vm); | |
3330 if (Threads::destroy_vm()) { | |
3331 // Should not change thread state, VM is gone | |
3332 vm_created = false; | |
3333 res = JNI_OK; | |
3334 return res; | |
3335 } else { | |
3336 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native); | |
3337 res = JNI_ERR; | |
3338 return res; | |
3339 } | |
3340 } | |
3341 | |
3342 | |
3343 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) { | |
3344 JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args; | |
3345 | |
3346 // Check below commented out from JDK1.2fcs as well | |
3347 /* | |
3348 if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) { | |
3349 return JNI_EVERSION; | |
3350 } | |
3351 */ | |
3352 | |
3353 Thread* t = ThreadLocalStorage::get_thread_slow(); | |
3354 if (t != NULL) { | |
3355 // If the thread has been attached this operation is a no-op | |
3356 *(JNIEnv**)penv = ((JavaThread*) t)->jni_environment(); | |
3357 return JNI_OK; | |
3358 } | |
3359 | |
3360 // Create a thread and mark it as attaching so it will be skipped by the | |
3361 // ThreadsListEnumerator - see CR 6404306 | |
3362 JavaThread* thread = new JavaThread(true); | |
3363 | |
3364 // Set correct safepoint info. The thread is going to call into Java when | |
3365 // initializing the Java level thread object. Hence, the correct state must | |
3366 // be set in order for the Safepoint code to deal with it correctly. | |
3367 thread->set_thread_state(_thread_in_vm); | |
3368 // Must do this before initialize_thread_local_storage | |
3369 thread->record_stack_base_and_size(); | |
3370 thread->initialize_thread_local_storage(); | |
3371 | |
3372 if (!os::create_attached_thread(thread)) { | |
3373 delete thread; | |
3374 return JNI_ERR; | |
3375 } | |
3376 thread->initialize_tlab(); | |
3377 | |
3378 // Crucial that we do not have a safepoint check for this thread, since it has | |
3379 // not been added to the Thread list yet. | |
3380 { Threads_lock->lock_without_safepoint_check(); | |
3381 // This must be inside this lock in order to get FullGCALot to work properly, i.e., to | |
3382 // avoid this thread trying to do a GC before it is added to the thread-list | |
3383 thread->set_active_handles(JNIHandleBlock::allocate_block()); | |
3384 Threads::add(thread, daemon); | |
3385 Threads_lock->unlock(); | |
3386 } | |
3387 // Create thread group and name info from attach arguments | |
3388 oop group = NULL; | |
3389 char* thread_name = NULL; | |
3390 if (args != NULL && Threads::is_supported_jni_version(args->version)) { | |
3391 group = JNIHandles::resolve(args->group); | |
3392 thread_name = args->name; // may be NULL | |
3393 } | |
3394 if (group == NULL) group = Universe::main_thread_group(); | |
3395 | |
3396 // Create Java level thread object and attach it to this thread | |
3397 bool attach_failed = false; | |
3398 { | |
3399 EXCEPTION_MARK; | |
3400 HandleMark hm(THREAD); | |
3401 Handle thread_group(THREAD, group); | |
3402 thread->allocate_threadObj(thread_group, thread_name, daemon, THREAD); | |
3403 if (HAS_PENDING_EXCEPTION) { | |
3404 CLEAR_PENDING_EXCEPTION; | |
3405 // cleanup outside the handle mark. | |
3406 attach_failed = true; | |
3407 } | |
3408 } | |
3409 | |
3410 if (attach_failed) { | |
3411 // Added missing cleanup | |
3412 thread->cleanup_failed_attach_current_thread(); | |
3413 return JNI_ERR; | |
3414 } | |
3415 | |
3416 // mark the thread as no longer attaching | |
3417 // this uses a fence to push the change through so we don't have | |
3418 // to regrab the threads_lock | |
3419 thread->set_attached(); | |
3420 | |
3421 // Enable stack overflow checks | |
3422 thread->create_stack_guard_pages(); | |
3423 | |
3424 // Set java thread status. | |
3425 java_lang_Thread::set_thread_status(thread->threadObj(), | |
3426 java_lang_Thread::RUNNABLE); | |
3427 | |
3428 // Notify the debugger | |
3429 if (JvmtiExport::should_post_thread_life()) { | |
3430 JvmtiExport::post_thread_start(thread); | |
3431 } | |
3432 | |
3433 *(JNIEnv**)penv = thread->jni_environment(); | |
3434 | |
3435 // Now leaving the VM, so change thread_state. This is normally automatically taken care | |
3436 // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by | |
3437 // using ThreadStateTransition::transition, we do a callback to the safepoint code if | |
3438 // needed. | |
3439 | |
3440 ThreadStateTransition::transition_and_fence(thread, _thread_in_vm, _thread_in_native); | |
3441 | |
3442 // Perform any platform dependent FPU setup | |
3443 os::setup_fpu(); | |
3444 | |
3445 return JNI_OK; | |
3446 } | |
3447 | |
3448 | |
3449 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) { | |
3450 DTRACE_PROBE3(hotspot_jni, AttachCurrentThread__entry, vm, penv, _args); | |
3451 if (!vm_created) { | |
3452 DTRACE_PROBE1(hotspot_jni, AttachCurrentThread__return, JNI_ERR); | |
3453 return JNI_ERR; | |
3454 } | |
3455 | |
3456 JNIWrapper("AttachCurrentThread"); | |
3457 jint ret = attach_current_thread(vm, penv, _args, false); | |
3458 DTRACE_PROBE1(hotspot_jni, AttachCurrentThread__return, ret); | |
3459 return ret; | |
3460 } | |
3461 | |
3462 | |
3463 jint JNICALL jni_DetachCurrentThread(JavaVM *vm) { | |
3464 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__entry, vm); | |
3465 VM_Exit::block_if_vm_exited(); | |
3466 | |
3467 JNIWrapper("DetachCurrentThread"); | |
3468 | |
3469 // If the thread has been deattacted the operations is a no-op | |
3470 if (ThreadLocalStorage::thread() == NULL) { | |
3471 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_OK); | |
3472 return JNI_OK; | |
3473 } | |
3474 | |
3475 JavaThread* thread = JavaThread::current(); | |
3476 if (thread->has_last_Java_frame()) { | |
3477 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_ERR); | |
3478 // Can't detach a thread that's running java, that can't work. | |
3479 return JNI_ERR; | |
3480 } | |
3481 | |
3482 // Safepoint support. Have to do call-back to safepoint code, if in the | |
3483 // middel of a safepoint operation | |
3484 ThreadStateTransition::transition_from_native(thread, _thread_in_vm); | |
3485 | |
3486 // XXX: Note that JavaThread::exit() call below removes the guards on the | |
3487 // stack pages set up via enable_stack_{red,yellow}_zone() calls | |
3488 // above in jni_AttachCurrentThread. Unfortunately, while the setting | |
3489 // of the guards is visible in jni_AttachCurrentThread above, | |
3490 // the removal of the guards is buried below in JavaThread::exit() | |
3491 // here. The abstraction should be more symmetrically either exposed | |
3492 // or hidden (e.g. it could probably be hidden in the same | |
3493 // (platform-dependent) methods where we do alternate stack | |
3494 // maintenance work?) | |
3495 thread->exit(false, JavaThread::jni_detach); | |
3496 delete thread; | |
3497 | |
3498 DTRACE_PROBE1(hotspot_jni, DetachCurrentThread__return, JNI_OK); | |
3499 return JNI_OK; | |
3500 } | |
3501 | |
3502 DT_RETURN_MARK_DECL(GetEnv, jint); | |
3503 | |
3504 jint JNICALL jni_GetEnv(JavaVM *vm, void **penv, jint version) { | |
3505 DTRACE_PROBE3(hotspot_jni, GetEnv__entry, vm, penv, version); | |
3506 jint ret = JNI_ERR; | |
3507 DT_RETURN_MARK(GetEnv, jint, (const jint&)ret); | |
3508 | |
3509 if (!vm_created) { | |
3510 *penv = NULL; | |
3511 ret = JNI_EDETACHED; | |
3512 return ret; | |
3513 } | |
3514 | |
3515 if (JvmtiExport::is_jvmti_version(version)) { | |
3516 ret = JvmtiExport::get_jvmti_interface(vm, penv, version); | |
3517 return ret; | |
3518 } | |
3519 | |
3520 #ifndef JVMPI_VERSION_1 | |
3521 // need these in order to be polite about older agents | |
3522 #define JVMPI_VERSION_1 ((jint)0x10000001) | |
3523 #define JVMPI_VERSION_1_1 ((jint)0x10000002) | |
3524 #define JVMPI_VERSION_1_2 ((jint)0x10000003) | |
3525 #endif // !JVMPI_VERSION_1 | |
3526 | |
3527 Thread* thread = ThreadLocalStorage::thread(); | |
3528 if (thread != NULL && thread->is_Java_thread()) { | |
3529 if (Threads::is_supported_jni_version_including_1_1(version)) { | |
3530 *(JNIEnv**)penv = ((JavaThread*) thread)->jni_environment(); | |
3531 ret = JNI_OK; | |
3532 return ret; | |
3533 | |
3534 } else if (version == JVMPI_VERSION_1 || | |
3535 version == JVMPI_VERSION_1_1 || | |
3536 version == JVMPI_VERSION_1_2) { | |
3537 tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported."); | |
3538 tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI)."); | |
3539 ret = JNI_EVERSION; | |
3540 return ret; | |
3541 } else if (JvmtiExport::is_jvmdi_version(version)) { | |
3542 tty->print_cr("FATAL ERROR: JVMDI is no longer supported."); | |
3543 tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI)."); | |
3544 ret = JNI_EVERSION; | |
3545 return ret; | |
3546 } else { | |
3547 *penv = NULL; | |
3548 ret = JNI_EVERSION; | |
3549 return ret; | |
3550 } | |
3551 } else { | |
3552 *penv = NULL; | |
3553 ret = JNI_EDETACHED; | |
3554 return ret; | |
3555 } | |
3556 } | |
3557 | |
3558 | |
3559 jint JNICALL jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *_args) { | |
3560 DTRACE_PROBE3(hotspot_jni, AttachCurrentThreadAsDaemon__entry, vm, penv, _args); | |
3561 if (!vm_created) { | |
3562 DTRACE_PROBE1(hotspot_jni, AttachCurrentThreadAsDaemon__return, JNI_ERR); | |
3563 return JNI_ERR; | |
3564 } | |
3565 | |
3566 JNIWrapper("AttachCurrentThreadAsDaemon"); | |
3567 jint ret = attach_current_thread(vm, penv, _args, true); | |
3568 DTRACE_PROBE1(hotspot_jni, AttachCurrentThreadAsDaemon__return, ret); | |
3569 return ret; | |
3570 } | |
3571 | |
3572 | |
3573 } // End extern "C" | |
3574 | |
3575 const struct JNIInvokeInterface_ jni_InvokeInterface = { | |
3576 NULL, | |
3577 NULL, | |
3578 NULL, | |
3579 | |
3580 jni_DestroyJavaVM, | |
3581 jni_AttachCurrentThread, | |
3582 jni_DetachCurrentThread, | |
3583 jni_GetEnv, | |
3584 jni_AttachCurrentThreadAsDaemon | |
3585 }; |