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