1 /*
2  * Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2012 Red Hat, Inc.
4  * Copyright (c) 2021, Azul Systems, Inc. All rights reserved.
5  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6  *
7  * This code is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License version 2 only, as
9  * published by the Free Software Foundation.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  *
25  */
26 
27 #include "precompiled.hpp"
28 #include "jni.h"
29 #include "jvm.h"
30 #include "ci/ciReplay.hpp"
31 #include "classfile/altHashing.hpp"
32 #include "classfile/classFileStream.hpp"
33 #include "classfile/classLoader.hpp"
34 #include "classfile/classLoadInfo.hpp"
35 #include "classfile/javaClasses.hpp"
36 #include "classfile/javaClasses.inline.hpp"
37 #include "classfile/javaThreadStatus.hpp"
38 #include "classfile/moduleEntry.hpp"
39 #include "classfile/modules.hpp"
40 #include "classfile/symbolTable.hpp"
41 #include "classfile/systemDictionary.hpp"
42 #include "classfile/vmClasses.hpp"
43 #include "classfile/vmSymbols.hpp"
44 #include "compiler/compiler_globals.hpp"
45 #include "gc/shared/collectedHeap.hpp"
46 #include "gc/shared/gcLocker.inline.hpp"
47 #include "gc/shared/stringdedup/stringDedup.hpp"
48 #include "interpreter/linkResolver.hpp"
49 #include "jfr/jfrEvents.hpp"
50 #include "jfr/support/jfrThreadId.hpp"
51 #include "logging/log.hpp"
52 #include "memory/allocation.hpp"
53 #include "memory/allocation.inline.hpp"
54 #include "memory/oopFactory.hpp"
55 #include "memory/resourceArea.hpp"
56 #include "memory/universe.hpp"
57 #include "oops/access.inline.hpp"
58 #include "oops/arrayOop.hpp"
59 #include "oops/instanceKlass.inline.hpp"
60 #include "oops/instanceOop.hpp"
61 #include "oops/klass.inline.hpp"
62 #include "oops/markWord.hpp"
63 #include "oops/method.hpp"
64 #include "oops/objArrayKlass.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/oop.inline.hpp"
67 #include "oops/symbol.hpp"
68 #include "oops/typeArrayKlass.hpp"
69 #include "oops/typeArrayOop.inline.hpp"
70 #include "prims/jniCheck.hpp"
71 #include "prims/jniExport.hpp"
72 #include "prims/jniFastGetField.hpp"
73 #include "prims/jvm_misc.hpp"
74 #include "prims/jvmtiExport.hpp"
75 #include "prims/jvmtiThreadState.hpp"
76 #include "runtime/atomic.hpp"
77 #include "runtime/fieldDescriptor.inline.hpp"
78 #include "runtime/handles.inline.hpp"
79 #include "runtime/interfaceSupport.inline.hpp"
80 #include "runtime/java.hpp"
81 #include "runtime/javaCalls.hpp"
82 #include "runtime/jfieldIDWorkaround.hpp"
83 #include "runtime/jniHandles.inline.hpp"
84 #include "runtime/reflection.hpp"
85 #include "runtime/safepointVerifiers.hpp"
86 #include "runtime/sharedRuntime.hpp"
87 #include "runtime/signature.hpp"
88 #include "runtime/thread.inline.hpp"
89 #include "runtime/vmOperations.hpp"
90 #include "services/memTracker.hpp"
91 #include "services/runtimeService.hpp"
92 #include "utilities/defaultStream.hpp"
93 #include "utilities/dtrace.hpp"
94 #include "utilities/events.hpp"
95 #include "utilities/macros.hpp"
96 #include "utilities/vmError.hpp"
97 #if INCLUDE_JVMCI
98 #include "jvmci/jvmciCompiler.hpp"
99 #endif
100 
101 static jint CurrentVersion = JNI_VERSION_10;
102 
103 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
104 extern LONG WINAPI topLevelExceptionFilter(_EXCEPTION_POINTERS* );
105 #endif
106 
107 // The DT_RETURN_MARK macros create a scoped object to fire the dtrace
108 // '-return' probe regardless of the return path is taken out of the function.
109 // Methods that have multiple return paths use this to avoid having to
110 // instrument each return path.  Methods that use CHECK or THROW must use this
111 // since those macros can cause an immedate uninstrumented return.
112 //
113 // In order to get the return value, a reference to the variable containing
114 // the return value must be passed to the contructor of the object, and
115 // the return value must be set before return (since the mark object has
116 // a reference to it).
117 //
118 // Example:
119 // DT_RETURN_MARK_DECL(SomeFunc, int);
120 // JNI_ENTRY(int, SomeFunc, ...)
121 //   int return_value = 0;
122 //   DT_RETURN_MARK(SomeFunc, int, (const int&)return_value);
123 //   foo(CHECK_0)
124 //   return_value = 5;
125 //   return return_value;
126 // JNI_END
127 #define DT_RETURN_MARK_DECL(name, type, probe)                             \
128   DTRACE_ONLY(                                                             \
129     class DTraceReturnProbeMark_##name {                                   \
130      public:                                                               \
131       const type& _ret_ref;                                                \
132       DTraceReturnProbeMark_##name(const type& v) : _ret_ref(v) {}         \
133       ~DTraceReturnProbeMark_##name() {                                    \
134         probe;                                                             \
135       }                                                                    \
136     }                                                                      \
137   )
138 // Void functions are simpler since there's no return value
139 #define DT_VOID_RETURN_MARK_DECL(name, probe)                              \
140   DTRACE_ONLY(                                                             \
141     class DTraceReturnProbeMark_##name {                                   \
142      public:                                                               \
143       ~DTraceReturnProbeMark_##name() {                                    \
144         probe;                                                             \
145       }                                                                    \
146     }                                                                      \
147   )
148 
149 // Place these macros in the function to mark the return.  Non-void
150 // functions need the type and address of the return value.
151 #define DT_RETURN_MARK(name, type, ref) \
152   DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark(ref) )
153 #define DT_VOID_RETURN_MARK(name) \
154   DTRACE_ONLY( DTraceReturnProbeMark_##name dtrace_return_mark )
155 
156 
157 // Use these to select distinct code for floating-point vs. non-floating point
158 // situations.  Used from within common macros where we need slightly
159 // different behavior for Float/Double
160 #define FP_SELECT_Boolean(intcode, fpcode) intcode
161 #define FP_SELECT_Byte(intcode, fpcode)    intcode
162 #define FP_SELECT_Char(intcode, fpcode)    intcode
163 #define FP_SELECT_Short(intcode, fpcode)   intcode
164 #define FP_SELECT_Object(intcode, fpcode)  intcode
165 #define FP_SELECT_Int(intcode, fpcode)     intcode
166 #define FP_SELECT_Long(intcode, fpcode)    intcode
167 #define FP_SELECT_Float(intcode, fpcode)   fpcode
168 #define FP_SELECT_Double(intcode, fpcode)  fpcode
169 #define FP_SELECT(TypeName, intcode, fpcode) \
170   FP_SELECT_##TypeName(intcode, fpcode)
171 
172 // Choose DT_RETURN_MARK macros  based on the type: float/double -> void
173 // (dtrace doesn't do FP yet)
174 #define DT_RETURN_MARK_DECL_FOR(TypeName, name, type, probe)    \
175   FP_SELECT(TypeName, \
176     DT_RETURN_MARK_DECL(name, type, probe), DT_VOID_RETURN_MARK_DECL(name, probe) )
177 #define DT_RETURN_MARK_FOR(TypeName, name, type, ref) \
178   FP_SELECT(TypeName, \
179     DT_RETURN_MARK(name, type, ref), DT_VOID_RETURN_MARK(name) )
180 
181 
182 // out-of-line helpers for class jfieldIDWorkaround:
183 
is_valid_jfieldID(Klass * k,jfieldID id)184 bool jfieldIDWorkaround::is_valid_jfieldID(Klass* k, jfieldID id) {
185   if (jfieldIDWorkaround::is_instance_jfieldID(k, id)) {
186     uintptr_t as_uint = (uintptr_t) id;
187     intptr_t offset = raw_instance_offset(id);
188     if (is_checked_jfieldID(id)) {
189       if (!klass_hash_ok(k, id)) {
190         return false;
191       }
192     }
193     return InstanceKlass::cast(k)->contains_field_offset(offset);
194   } else {
195     JNIid* result = (JNIid*) id;
196 #ifdef ASSERT
197     return result != NULL && result->is_static_field_id();
198 #else
199     return result != NULL;
200 #endif
201   }
202 }
203 
204 
encode_klass_hash(Klass * k,intptr_t offset)205 intptr_t jfieldIDWorkaround::encode_klass_hash(Klass* k, intptr_t offset) {
206   if (offset <= small_offset_mask) {
207     Klass* field_klass = k;
208     Klass* super_klass = field_klass->super();
209     // With compressed oops the most super class with nonstatic fields would
210     // be the owner of fields embedded in the header.
211     while (InstanceKlass::cast(super_klass)->has_nonstatic_fields() &&
212            InstanceKlass::cast(super_klass)->contains_field_offset(offset)) {
213       field_klass = super_klass;   // super contains the field also
214       super_klass = field_klass->super();
215     }
216     debug_only(NoSafepointVerifier nosafepoint;)
217     uintptr_t klass_hash = field_klass->identity_hash();
218     return ((klass_hash & klass_mask) << klass_shift) | checked_mask_in_place;
219   } else {
220 #if 0
221     #ifndef PRODUCT
222     {
223       ResourceMark rm;
224       warning("VerifyJNIFields: long offset %d in %s", offset, k->external_name());
225     }
226     #endif
227 #endif
228     return 0;
229   }
230 }
231 
klass_hash_ok(Klass * k,jfieldID id)232 bool jfieldIDWorkaround::klass_hash_ok(Klass* k, jfieldID id) {
233   uintptr_t as_uint = (uintptr_t) id;
234   intptr_t klass_hash = (as_uint >> klass_shift) & klass_mask;
235   do {
236     debug_only(NoSafepointVerifier nosafepoint;)
237     // Could use a non-blocking query for identity_hash here...
238     if ((k->identity_hash() & klass_mask) == klass_hash)
239       return true;
240     k = k->super();
241   } while (k != NULL);
242   return false;
243 }
244 
verify_instance_jfieldID(Klass * k,jfieldID id)245 void jfieldIDWorkaround::verify_instance_jfieldID(Klass* k, jfieldID id) {
246   guarantee(jfieldIDWorkaround::is_instance_jfieldID(k, id), "must be an instance field" );
247   uintptr_t as_uint = (uintptr_t) id;
248   intptr_t offset = raw_instance_offset(id);
249   if (VerifyJNIFields) {
250     if (is_checked_jfieldID(id)) {
251       guarantee(klass_hash_ok(k, id),
252     "Bug in native code: jfieldID class must match object");
253     } else {
254 #if 0
255       #ifndef PRODUCT
256       if (Verbose) {
257   ResourceMark rm;
258   warning("VerifyJNIFields: unverified offset %d for %s", offset, k->external_name());
259       }
260       #endif
261 #endif
262     }
263   }
264   guarantee(InstanceKlass::cast(k)->contains_field_offset(offset),
265       "Bug in native code: jfieldID offset must address interior of object");
266 }
267 
268 // Implementation of JNI entries
269 
270 DT_RETURN_MARK_DECL(DefineClass, jclass
271                     , HOTSPOT_JNI_DEFINECLASS_RETURN(_ret_ref));
272 
273 JNI_ENTRY(jclass, jni_DefineClass(JNIEnv *env, const char *name, jobject loaderRef,
274                                   const jbyte *buf, jsize bufLen))
275   HOTSPOT_JNI_DEFINECLASS_ENTRY(
276     env, (char*) name, loaderRef, (char*) buf, bufLen);
277 
278   jclass cls = NULL;
279   DT_RETURN_MARK(DefineClass, jclass, (const jclass&)cls);
280 
281   // Class resolution will get the class name from the .class stream if the name is null.
282   TempNewSymbol class_name = name == NULL ? NULL :
283     SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
284                                         CHECK_NULL);
285 
286   ResourceMark rm(THREAD);
287   ClassFileStream st((u1*)buf, bufLen, NULL, ClassFileStream::verify);
288   Handle class_loader (THREAD, JNIHandles::resolve(loaderRef));
289   Handle protection_domain;
290   ClassLoadInfo cl_info(protection_domain);
291   Klass* k = SystemDictionary::resolve_from_stream(&st, class_name,
292                                                    class_loader,
293                                                    cl_info,
294                                                    CHECK_NULL);
295 
296   if (log_is_enabled(Debug, class, resolve)) {
297     trace_class_resolution(k);
298   }
299 
300   cls = (jclass)JNIHandles::make_local(THREAD, k->java_mirror());
301   return cls;
302 JNI_END
303 
304 
305 
306 DT_RETURN_MARK_DECL(FindClass, jclass
307                     , HOTSPOT_JNI_FINDCLASS_RETURN(_ret_ref));
308 
309 JNI_ENTRY(jclass, jni_FindClass(JNIEnv *env, const char *name))
310   HOTSPOT_JNI_FINDCLASS_ENTRY(env, (char *)name);
311 
312   jclass result = NULL;
313   DT_RETURN_MARK(FindClass, jclass, (const jclass&)result);
314 
315   // This should be ClassNotFoundException imo.
316   TempNewSymbol class_name =
317     SystemDictionary::class_name_symbol(name, vmSymbols::java_lang_NoClassDefFoundError(),
318                                         CHECK_NULL);
319 
320   //%note jni_3
321   Handle protection_domain;
322   // Find calling class
323   Klass* k = thread->security_get_caller_class(0);
324   // default to the system loader when no context
325   Handle loader(THREAD, SystemDictionary::java_system_loader());
326   if (k != NULL) {
327     // Special handling to make sure JNI_OnLoad and JNI_OnUnload are executed
328     // in the correct class context.
329     if (k->class_loader() == NULL &&
330         k->name() == vmSymbols::jdk_internal_loader_NativeLibraries()) {
331       JavaValue result(T_OBJECT);
332       JavaCalls::call_static(&result, k,
333                              vmSymbols::getFromClass_name(),
334                              vmSymbols::void_class_signature(),
335                              CHECK_NULL);
336       // When invoked from JNI_OnLoad, NativeLibraries::getFromClass returns
337       // a non-NULL Class object.  When invoked from JNI_OnUnload,
338       // it will return NULL to indicate no context.
339       oop mirror = result.get_oop();
340       if (mirror != NULL) {
341         Klass* fromClass = java_lang_Class::as_Klass(mirror);
342         loader = Handle(THREAD, fromClass->class_loader());
343         protection_domain = Handle(THREAD, fromClass->protection_domain());
344       }
345     } else {
346       loader = Handle(THREAD, k->class_loader());
347     }
348   }
349 
350   result = find_class_from_class_loader(env, class_name, true, loader,
351                                         protection_domain, true, thread);
352 
353   if (log_is_enabled(Debug, class, resolve) && result != NULL) {
354     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
355   }
356 
357   return result;
358 JNI_END
359 
360 DT_RETURN_MARK_DECL(FromReflectedMethod, jmethodID
361                     , HOTSPOT_JNI_FROMREFLECTEDMETHOD_RETURN((uintptr_t)_ret_ref));
362 
363 JNI_ENTRY(jmethodID, jni_FromReflectedMethod(JNIEnv *env, jobject method))
364   HOTSPOT_JNI_FROMREFLECTEDMETHOD_ENTRY(env, method);
365 
366   jmethodID ret = NULL;
367   DT_RETURN_MARK(FromReflectedMethod, jmethodID, (const jmethodID&)ret);
368 
369   // method is a handle to a java.lang.reflect.Method object
370   oop reflected  = JNIHandles::resolve_non_null(method);
371   oop mirror     = NULL;
372   int slot       = 0;
373 
374   if (reflected->klass() == vmClasses::reflect_Constructor_klass()) {
375     mirror = java_lang_reflect_Constructor::clazz(reflected);
376     slot   = java_lang_reflect_Constructor::slot(reflected);
377   } else {
378     assert(reflected->klass() == vmClasses::reflect_Method_klass(), "wrong type");
379     mirror = java_lang_reflect_Method::clazz(reflected);
380     slot   = java_lang_reflect_Method::slot(reflected);
381   }
382   Klass* k1 = java_lang_Class::as_Klass(mirror);
383 
384   // Make sure class is initialized before handing id's out to methods
385   k1->initialize(CHECK_NULL);
386   Method* m = InstanceKlass::cast(k1)->method_with_idnum(slot);
387   ret = m==NULL? NULL : m->jmethod_id();  // return NULL if reflected method deleted
388   return ret;
389 JNI_END
390 
391 DT_RETURN_MARK_DECL(FromReflectedField, jfieldID
392                     , HOTSPOT_JNI_FROMREFLECTEDFIELD_RETURN((uintptr_t)_ret_ref));
393 
394 JNI_ENTRY(jfieldID, jni_FromReflectedField(JNIEnv *env, jobject field))
395   HOTSPOT_JNI_FROMREFLECTEDFIELD_ENTRY(env, field);
396 
397   jfieldID ret = NULL;
398   DT_RETURN_MARK(FromReflectedField, jfieldID, (const jfieldID&)ret);
399 
400   // field is a handle to a java.lang.reflect.Field object
401   oop reflected   = JNIHandles::resolve_non_null(field);
402   oop mirror      = java_lang_reflect_Field::clazz(reflected);
403   Klass* k1       = java_lang_Class::as_Klass(mirror);
404   int slot        = java_lang_reflect_Field::slot(reflected);
405   int modifiers   = java_lang_reflect_Field::modifiers(reflected);
406 
407   // Make sure class is initialized before handing id's out to fields
408   k1->initialize(CHECK_NULL);
409 
410   // First check if this is a static field
411   if (modifiers & JVM_ACC_STATIC) {
412     intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
413     JNIid* id = InstanceKlass::cast(k1)->jni_id_for(offset);
414     assert(id != NULL, "corrupt Field object");
415     debug_only(id->set_is_static_field_id();)
416     // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
417     ret = jfieldIDWorkaround::to_static_jfieldID(id);
418     return ret;
419   }
420 
421   // The slot is the index of the field description in the field-array
422   // The jfieldID is the offset of the field within the object
423   // It may also have hash bits for k, if VerifyJNIFields is turned on.
424   intptr_t offset = InstanceKlass::cast(k1)->field_offset( slot );
425   assert(InstanceKlass::cast(k1)->contains_field_offset(offset), "stay within object");
426   ret = jfieldIDWorkaround::to_instance_jfieldID(k1, offset);
427   return ret;
428 JNI_END
429 
430 
431 DT_RETURN_MARK_DECL(ToReflectedMethod, jobject
432                     , HOTSPOT_JNI_TOREFLECTEDMETHOD_RETURN(_ret_ref));
433 
434 JNI_ENTRY(jobject, jni_ToReflectedMethod(JNIEnv *env, jclass cls, jmethodID method_id, jboolean isStatic))
435   HOTSPOT_JNI_TOREFLECTEDMETHOD_ENTRY(env, cls, (uintptr_t) method_id, isStatic);
436 
437   jobject ret = NULL;
438   DT_RETURN_MARK(ToReflectedMethod, jobject, (const jobject&)ret);
439 
440   methodHandle m (THREAD, Method::resolve_jmethod_id(method_id));
441   assert(m->is_static() == (isStatic != 0), "jni_ToReflectedMethod access flags doesn't match");
442   oop reflection_method;
443   if (m->is_initializer()) {
444     reflection_method = Reflection::new_constructor(m, CHECK_NULL);
445   } else {
446     reflection_method = Reflection::new_method(m, false, CHECK_NULL);
447   }
448   ret = JNIHandles::make_local(THREAD, reflection_method);
449   return ret;
450 JNI_END
451 
452 DT_RETURN_MARK_DECL(GetSuperclass, jclass
453                     , HOTSPOT_JNI_GETSUPERCLASS_RETURN(_ret_ref));
454 
455 JNI_ENTRY(jclass, jni_GetSuperclass(JNIEnv *env, jclass sub))
456   HOTSPOT_JNI_GETSUPERCLASS_ENTRY(env, sub);
457 
458   jclass obj = NULL;
459   DT_RETURN_MARK(GetSuperclass, jclass, (const jclass&)obj);
460 
461   oop mirror = JNIHandles::resolve_non_null(sub);
462   // primitive classes return NULL
463   if (java_lang_Class::is_primitive(mirror)) return NULL;
464 
465   // Rules of Class.getSuperClass as implemented by KLass::java_super:
466   // arrays return Object
467   // interfaces return NULL
468   // proper classes return Klass::super()
469   Klass* k = java_lang_Class::as_Klass(mirror);
470   if (k->is_interface()) return NULL;
471 
472   // return mirror for superclass
473   Klass* super = k->java_super();
474   // super2 is the value computed by the compiler's getSuperClass intrinsic:
475   debug_only(Klass* super2 = ( k->is_array_klass()
476                                  ? vmClasses::Object_klass()
477                                  : k->super() ) );
478   assert(super == super2,
479          "java_super computation depends on interface, array, other super");
480   obj = (super == NULL) ? NULL : (jclass) JNIHandles::make_local(THREAD, super->java_mirror());
481   return obj;
482 JNI_END
483 
484 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsAssignableFrom(JNIEnv *env, jclass sub, jclass super))
485   HOTSPOT_JNI_ISASSIGNABLEFROM_ENTRY(env, sub, super);
486 
487   oop sub_mirror   = JNIHandles::resolve_non_null(sub);
488   oop super_mirror = JNIHandles::resolve_non_null(super);
489   if (java_lang_Class::is_primitive(sub_mirror) ||
490       java_lang_Class::is_primitive(super_mirror)) {
491     jboolean ret = (sub_mirror == super_mirror);
492 
493     HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
494     return ret;
495   }
496   Klass* sub_klass   = java_lang_Class::as_Klass(sub_mirror);
497   Klass* super_klass = java_lang_Class::as_Klass(super_mirror);
498   assert(sub_klass != NULL && super_klass != NULL, "invalid arguments to jni_IsAssignableFrom");
499   jboolean ret = sub_klass->is_subtype_of(super_klass) ?
500                    JNI_TRUE : JNI_FALSE;
501 
502   HOTSPOT_JNI_ISASSIGNABLEFROM_RETURN(ret);
503   return ret;
504 JNI_END
505 
506 
507 DT_RETURN_MARK_DECL(Throw, jint
508                     , HOTSPOT_JNI_THROW_RETURN(_ret_ref));
509 
510 JNI_ENTRY(jint, jni_Throw(JNIEnv *env, jthrowable obj))
511   HOTSPOT_JNI_THROW_ENTRY(env, obj);
512 
513   jint ret = JNI_OK;
514   DT_RETURN_MARK(Throw, jint, (const jint&)ret);
515 
516   THROW_OOP_(JNIHandles::resolve(obj), JNI_OK);
517   ShouldNotReachHere();
518   return 0;  // Mute compiler.
519 JNI_END
520 
521 
522 DT_RETURN_MARK_DECL(ThrowNew, jint
523                     , HOTSPOT_JNI_THROWNEW_RETURN(_ret_ref));
524 
525 JNI_ENTRY(jint, jni_ThrowNew(JNIEnv *env, jclass clazz, const char *message))
526   HOTSPOT_JNI_THROWNEW_ENTRY(env, clazz, (char *) message);
527 
528   jint ret = JNI_OK;
529   DT_RETURN_MARK(ThrowNew, jint, (const jint&)ret);
530 
531   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz)));
532   Symbol*  name = k->name();
533   Handle class_loader (THREAD,  k->class_loader());
534   Handle protection_domain (THREAD, k->protection_domain());
535   THROW_MSG_LOADER_(name, (char *)message, class_loader, protection_domain, JNI_OK);
536   ShouldNotReachHere();
537   return 0;  // Mute compiler.
538 JNI_END
539 
540 
541 // JNI functions only transform a pending async exception to a synchronous
542 // exception in ExceptionOccurred and ExceptionCheck calls, since
543 // delivering an async exception in other places won't change the native
544 // code's control flow and would be harmful when native code further calls
545 // JNI functions with a pending exception. Async exception is also checked
546 // during the call, so ExceptionOccurred/ExceptionCheck won't return
547 // false but deliver the async exception at the very end during
548 // state transition.
549 
jni_check_async_exceptions(JavaThread * thread)550 static void jni_check_async_exceptions(JavaThread *thread) {
551   assert(thread == Thread::current(), "must be itself");
552   thread->check_and_handle_async_exceptions();
553 }
554 
555 JNI_ENTRY_NO_PRESERVE(jthrowable, jni_ExceptionOccurred(JNIEnv *env))
556   HOTSPOT_JNI_EXCEPTIONOCCURRED_ENTRY(env);
557 
558   jni_check_async_exceptions(thread);
559   oop exception = thread->pending_exception();
560   jthrowable ret = (jthrowable) JNIHandles::make_local(THREAD, exception);
561 
562   HOTSPOT_JNI_EXCEPTIONOCCURRED_RETURN(ret);
563   return ret;
564 JNI_END
565 
566 
567 JNI_ENTRY_NO_PRESERVE(void, jni_ExceptionDescribe(JNIEnv *env))
568   HOTSPOT_JNI_EXCEPTIONDESCRIBE_ENTRY(env);
569 
570   if (thread->has_pending_exception()) {
571     Handle ex(thread, thread->pending_exception());
572     thread->clear_pending_exception();
573     if (ex->is_a(vmClasses::ThreadDeath_klass())) {
574       // Don't print anything if we are being killed.
575     } else {
576       jio_fprintf(defaultStream::error_stream(), "Exception ");
577       if (thread != NULL && thread->threadObj() != NULL) {
578         ResourceMark rm(THREAD);
579         jio_fprintf(defaultStream::error_stream(),
580         "in thread \"%s\" ", thread->get_thread_name());
581       }
582       if (ex->is_a(vmClasses::Throwable_klass())) {
583         JavaValue result(T_VOID);
584         JavaCalls::call_virtual(&result,
585                                 ex,
586                                 vmClasses::Throwable_klass(),
587                                 vmSymbols::printStackTrace_name(),
588                                 vmSymbols::void_method_signature(),
589                                 THREAD);
590         // If an exception is thrown in the call it gets thrown away. Not much
591         // we can do with it. The native code that calls this, does not check
592         // for the exception - hence, it might still be in the thread when DestroyVM gets
593         // called, potentially causing a few asserts to trigger - since no pending exception
594         // is expected.
595         CLEAR_PENDING_EXCEPTION;
596       } else {
597         ResourceMark rm(THREAD);
598         jio_fprintf(defaultStream::error_stream(),
599         ". Uncaught exception of type %s.",
600         ex->klass()->external_name());
601       }
602     }
603   }
604 
605   HOTSPOT_JNI_EXCEPTIONDESCRIBE_RETURN();
606 JNI_END
607 
608 
609 JNI_ENTRY_NO_PRESERVE(void, jni_ExceptionClear(JNIEnv *env))
610   HOTSPOT_JNI_EXCEPTIONCLEAR_ENTRY(env);
611 
612   // The jni code might be using this API to clear java thrown exception.
613   // So just mark jvmti thread exception state as exception caught.
614   JvmtiThreadState *state = JavaThread::current()->jvmti_thread_state();
615   if (state != NULL && state->is_exception_detected()) {
616     state->set_exception_caught();
617   }
618   thread->clear_pending_exception();
619 
620   HOTSPOT_JNI_EXCEPTIONCLEAR_RETURN();
621 JNI_END
622 
623 
624 JNI_ENTRY(void, jni_FatalError(JNIEnv *env, const char *msg))
625   HOTSPOT_JNI_FATALERROR_ENTRY(env, (char *) msg);
626 
627   tty->print_cr("FATAL ERROR in native method: %s", msg);
628   thread->print_stack();
629   os::abort(); // Dump core and abort
630 JNI_END
631 
632 
633 JNI_ENTRY(jint, jni_PushLocalFrame(JNIEnv *env, jint capacity))
634   HOTSPOT_JNI_PUSHLOCALFRAME_ENTRY(env, capacity);
635 
636   //%note jni_11
637   if (capacity < 0 ||
638       ((MaxJNILocalCapacity > 0) && (capacity > MaxJNILocalCapacity))) {
639     HOTSPOT_JNI_PUSHLOCALFRAME_RETURN((uint32_t)JNI_ERR);
640     return JNI_ERR;
641   }
642   JNIHandleBlock* old_handles = thread->active_handles();
643   JNIHandleBlock* new_handles = JNIHandleBlock::allocate_block(thread);
644   assert(new_handles != NULL, "should not be NULL");
645   new_handles->set_pop_frame_link(old_handles);
646   thread->set_active_handles(new_handles);
647   jint ret = JNI_OK;
648   HOTSPOT_JNI_PUSHLOCALFRAME_RETURN(ret);
649   return ret;
650 JNI_END
651 
652 
653 JNI_ENTRY(jobject, jni_PopLocalFrame(JNIEnv *env, jobject result))
654   HOTSPOT_JNI_POPLOCALFRAME_ENTRY(env, result);
655 
656   //%note jni_11
657   Handle result_handle(thread, JNIHandles::resolve(result));
658   JNIHandleBlock* old_handles = thread->active_handles();
659   JNIHandleBlock* new_handles = old_handles->pop_frame_link();
660   if (new_handles != NULL) {
661     // As a sanity check we only release the handle blocks if the pop_frame_link is not NULL.
662     // This way code will still work if PopLocalFrame is called without a corresponding
663     // PushLocalFrame call. Note that we set the pop_frame_link to NULL explicitly, otherwise
664     // the release_block call will release the blocks.
665     thread->set_active_handles(new_handles);
666     old_handles->set_pop_frame_link(NULL);              // clear link we won't release new_handles below
667     JNIHandleBlock::release_block(old_handles, thread); // may block
668     result = JNIHandles::make_local(thread, result_handle());
669   }
670   HOTSPOT_JNI_POPLOCALFRAME_RETURN(result);
671   return result;
672 JNI_END
673 
674 
675 JNI_ENTRY(jobject, jni_NewGlobalRef(JNIEnv *env, jobject ref))
676   HOTSPOT_JNI_NEWGLOBALREF_ENTRY(env, ref);
677 
678   Handle ref_handle(thread, JNIHandles::resolve(ref));
679   jobject ret = JNIHandles::make_global(ref_handle, AllocFailStrategy::RETURN_NULL);
680 
681   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   HOTSPOT_JNI_DELETEGLOBALREF_ENTRY(env, ref);
688 
689   JNIHandles::destroy_global(ref);
690 
691   HOTSPOT_JNI_DELETEGLOBALREF_RETURN();
692 JNI_END
693 
694 JNI_ENTRY_NO_PRESERVE(void, jni_DeleteLocalRef(JNIEnv *env, jobject obj))
695   HOTSPOT_JNI_DELETELOCALREF_ENTRY(env, obj);
696 
697   JNIHandles::destroy_local(obj);
698 
699   HOTSPOT_JNI_DELETELOCALREF_RETURN();
700 JNI_END
701 
702 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsSameObject(JNIEnv *env, jobject r1, jobject r2))
703   HOTSPOT_JNI_ISSAMEOBJECT_ENTRY(env, r1, r2);
704 
705   jboolean ret = JNIHandles::is_same_object(r1, r2) ? JNI_TRUE : JNI_FALSE;
706 
707   HOTSPOT_JNI_ISSAMEOBJECT_RETURN(ret);
708   return ret;
709 JNI_END
710 
711 
712 JNI_ENTRY(jobject, jni_NewLocalRef(JNIEnv *env, jobject ref))
713   HOTSPOT_JNI_NEWLOCALREF_ENTRY(env, ref);
714 
715   jobject ret = JNIHandles::make_local(THREAD, JNIHandles::resolve(ref),
716                                        AllocFailStrategy::RETURN_NULL);
717 
718   HOTSPOT_JNI_NEWLOCALREF_RETURN(ret);
719   return ret;
720 JNI_END
721 
722 JNI_LEAF(jint, jni_EnsureLocalCapacity(JNIEnv *env, jint capacity))
723   HOTSPOT_JNI_ENSURELOCALCAPACITY_ENTRY(env, capacity);
724 
725   jint ret;
726   if (capacity >= 0 &&
727       ((MaxJNILocalCapacity <= 0) || (capacity <= MaxJNILocalCapacity))) {
728     ret = JNI_OK;
729   } else {
730     ret = JNI_ERR;
731   }
732 
733   HOTSPOT_JNI_ENSURELOCALCAPACITY_RETURN(ret);
734   return ret;
735 JNI_END
736 
737 // Return the Handle Type
738 JNI_LEAF(jobjectRefType, jni_GetObjectRefType(JNIEnv *env, jobject obj))
739   HOTSPOT_JNI_GETOBJECTREFTYPE_ENTRY(env, obj);
740 
741   jobjectRefType ret = JNIInvalidRefType;
742   if (obj != NULL) {
743     ret = JNIHandles::handle_type(thread, obj);
744   }
745 
746   HOTSPOT_JNI_GETOBJECTREFTYPE_RETURN((void *) ret);
747   return ret;
748 JNI_END
749 
750 
751 class JNI_ArgumentPusher : public SignatureIterator {
752  protected:
753   JavaCallArguments*  _arguments;
754 
push_int(jint x)755   void push_int(jint x)         { _arguments->push_int(x); }
push_long(jlong x)756   void push_long(jlong x)       { _arguments->push_long(x); }
push_float(jfloat x)757   void push_float(jfloat x)     { _arguments->push_float(x); }
push_double(jdouble x)758   void push_double(jdouble x)   { _arguments->push_double(x); }
push_object(jobject x)759   void push_object(jobject x)   { _arguments->push_jobject(x); }
760 
push_boolean(jboolean b)761   void push_boolean(jboolean b) {
762     // Normalize boolean arguments from native code by converting 1-255 to JNI_TRUE and
763     // 0 to JNI_FALSE.  Boolean return values from native are normalized the same in
764     // TemplateInterpreterGenerator::generate_result_handler_for and
765     // SharedRuntime::generate_native_wrapper.
766     push_int(b == 0 ? JNI_FALSE : JNI_TRUE);
767   }
768 
JNI_ArgumentPusher(Method * method)769   JNI_ArgumentPusher(Method* method)
770     : SignatureIterator(method->signature(),
771                         Fingerprinter(methodHandle(Thread::current(), method)).fingerprint())
772   {
773     _arguments = NULL;
774   }
775 
776  public:
777   virtual void push_arguments_on(JavaCallArguments* arguments) = 0;
778 };
779 
780 
781 class JNI_ArgumentPusherVaArg : public JNI_ArgumentPusher {
782   va_list _ap;
783 
set_ap(va_list rap)784   void set_ap(va_list rap) {
785     va_copy(_ap, rap);
786   }
787 
788   friend class SignatureIterator;  // so do_parameters_on can call do_type
do_type(BasicType type)789   void do_type(BasicType type) {
790     switch (type) {
791     // these are coerced to int when using va_arg
792     case T_BYTE:
793     case T_CHAR:
794     case T_SHORT:
795     case T_INT:         push_int(va_arg(_ap, jint)); break;
796     case T_BOOLEAN:     push_boolean((jboolean) va_arg(_ap, jint)); break;
797 
798     // each of these paths is exercised by the various jck Call[Static,Nonvirtual,][Void,Int,..]Method[A,V,] tests
799 
800     case T_LONG:        push_long(va_arg(_ap, jlong)); break;
801     // float is coerced to double w/ va_arg
802     case T_FLOAT:       push_float((jfloat) va_arg(_ap, jdouble)); break;
803     case T_DOUBLE:      push_double(va_arg(_ap, jdouble)); break;
804 
805     case T_ARRAY:
806     case T_OBJECT:      push_object(va_arg(_ap, jobject)); break;
807     default:            ShouldNotReachHere();
808     }
809   }
810 
811  public:
JNI_ArgumentPusherVaArg(jmethodID method_id,va_list rap)812   JNI_ArgumentPusherVaArg(jmethodID method_id, va_list rap)
813       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
814     set_ap(rap);
815   }
816 
~JNI_ArgumentPusherVaArg()817   ~JNI_ArgumentPusherVaArg() {
818     va_end(_ap);
819   }
820 
push_arguments_on(JavaCallArguments * arguments)821   virtual void push_arguments_on(JavaCallArguments* arguments) {
822     _arguments = arguments;
823     do_parameters_on(this);
824   }
825 };
826 
827 
828 class JNI_ArgumentPusherArray : public JNI_ArgumentPusher {
829  protected:
830   const jvalue *_ap;
831 
set_ap(const jvalue * rap)832   inline void set_ap(const jvalue *rap) { _ap = rap; }
833 
834   friend class SignatureIterator;  // so do_parameters_on can call do_type
do_type(BasicType type)835   void do_type(BasicType type) {
836     switch (type) {
837     case T_CHAR:        push_int((_ap++)->c); break;
838     case T_SHORT:       push_int((_ap++)->s); break;
839     case T_BYTE:        push_int((_ap++)->b); break;
840     case T_INT:         push_int((_ap++)->i); break;
841     case T_BOOLEAN:     push_boolean((_ap++)->z); break;
842     case T_LONG:        push_long((_ap++)->j); break;
843     case T_FLOAT:       push_float((_ap++)->f); break;
844     case T_DOUBLE:      push_double((_ap++)->d); break;
845     case T_ARRAY:
846     case T_OBJECT:      push_object((_ap++)->l); break;
847     default:            ShouldNotReachHere();
848     }
849   }
850 
851  public:
JNI_ArgumentPusherArray(jmethodID method_id,const jvalue * rap)852   JNI_ArgumentPusherArray(jmethodID method_id, const jvalue *rap)
853       : JNI_ArgumentPusher(Method::resolve_jmethod_id(method_id)) {
854     set_ap(rap);
855   }
856 
push_arguments_on(JavaCallArguments * arguments)857   virtual void push_arguments_on(JavaCallArguments* arguments) {
858     _arguments = arguments;
859     do_parameters_on(this);
860   }
861 };
862 
863 
864 enum JNICallType {
865   JNI_STATIC,
866   JNI_VIRTUAL,
867   JNI_NONVIRTUAL
868 };
869 
870 
871 
jni_invoke_static(JNIEnv * env,JavaValue * result,jobject receiver,JNICallType call_type,jmethodID method_id,JNI_ArgumentPusher * args,TRAPS)872 static void jni_invoke_static(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
873   methodHandle method(THREAD, Method::resolve_jmethod_id(method_id));
874 
875   // Create object to hold arguments for the JavaCall, and associate it with
876   // the jni parser
877   ResourceMark rm(THREAD);
878   int number_of_parameters = method->size_of_parameters();
879   JavaCallArguments java_args(number_of_parameters);
880 
881   assert(method->is_static(), "method should be static");
882 
883   // Fill out JavaCallArguments object
884   args->push_arguments_on(&java_args);
885   // Initialize result type
886   result->set_type(args->return_type());
887 
888   // Invoke the method. Result is returned as oop.
889   JavaCalls::call(result, method, &java_args, CHECK);
890 
891   // Convert result
892   if (is_reference_type(result->get_type())) {
893     result->set_jobject(JNIHandles::make_local(THREAD, result->get_oop()));
894   }
895 }
896 
897 
jni_invoke_nonstatic(JNIEnv * env,JavaValue * result,jobject receiver,JNICallType call_type,jmethodID method_id,JNI_ArgumentPusher * args,TRAPS)898 static void jni_invoke_nonstatic(JNIEnv *env, JavaValue* result, jobject receiver, JNICallType call_type, jmethodID method_id, JNI_ArgumentPusher *args, TRAPS) {
899   oop recv = JNIHandles::resolve(receiver);
900   if (recv == NULL) {
901     THROW(vmSymbols::java_lang_NullPointerException());
902   }
903   Handle h_recv(THREAD, recv);
904 
905   int number_of_parameters;
906   Method* selected_method;
907   {
908     Method* m = Method::resolve_jmethod_id(method_id);
909     number_of_parameters = m->size_of_parameters();
910     InstanceKlass* holder = m->method_holder();
911     if (call_type != JNI_VIRTUAL) {
912         selected_method = m;
913     } else if (!m->has_itable_index()) {
914       // non-interface call -- for that little speed boost, don't handlize
915       debug_only(NoSafepointVerifier nosafepoint;)
916       // jni_GetMethodID makes sure class is linked and initialized
917       // so m should have a valid vtable index.
918       assert(m->valid_vtable_index(), "no valid vtable index");
919       int vtbl_index = m->vtable_index();
920       if (vtbl_index != Method::nonvirtual_vtable_index) {
921         selected_method = h_recv->klass()->method_at_vtable(vtbl_index);
922       } else {
923         // final method
924         selected_method = m;
925       }
926     } else {
927       // interface call
928       int itbl_index = m->itable_index();
929       Klass* k = h_recv->klass();
930       selected_method = InstanceKlass::cast(k)->method_at_itable(holder, itbl_index, CHECK);
931     }
932   }
933 
934   methodHandle method(THREAD, selected_method);
935 
936   // Create object to hold arguments for the JavaCall, and associate it with
937   // the jni parser
938   ResourceMark rm(THREAD);
939   JavaCallArguments java_args(number_of_parameters);
940 
941   // handle arguments
942   assert(!method->is_static(), "method %s should not be static", method->name_and_sig_as_C_string());
943   java_args.push_oop(h_recv); // Push jobject handle
944 
945   // Fill out JavaCallArguments object
946   args->push_arguments_on(&java_args);
947   // Initialize result type
948   result->set_type(args->return_type());
949 
950   // Invoke the method. Result is returned as oop.
951   JavaCalls::call(result, method, &java_args, CHECK);
952 
953   // Convert result
954   if (is_reference_type(result->get_type())) {
955     result->set_jobject(JNIHandles::make_local(THREAD, result->get_oop()));
956   }
957 }
958 
959 DT_RETURN_MARK_DECL(AllocObject, jobject
960                     , HOTSPOT_JNI_ALLOCOBJECT_RETURN(_ret_ref));
961 
962 JNI_ENTRY(jobject, jni_AllocObject(JNIEnv *env, jclass clazz))
963   HOTSPOT_JNI_ALLOCOBJECT_ENTRY(env, clazz);
964 
965   jobject ret = NULL;
966   DT_RETURN_MARK(AllocObject, jobject, (const jobject&)ret);
967 
968   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
969   ret = JNIHandles::make_local(THREAD, i);
970   return ret;
971 JNI_END
972 
973 DT_RETURN_MARK_DECL(NewObjectA, jobject
974                     , HOTSPOT_JNI_NEWOBJECTA_RETURN(_ret_ref));
975 
976 JNI_ENTRY(jobject, jni_NewObjectA(JNIEnv *env, jclass clazz, jmethodID methodID, const jvalue *args))
977   HOTSPOT_JNI_NEWOBJECTA_ENTRY(env, clazz, (uintptr_t) methodID);
978 
979   jobject obj = NULL;
980   DT_RETURN_MARK(NewObjectA, jobject, (const jobject&)obj);
981 
982   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
983   obj = JNIHandles::make_local(THREAD, i);
984   JavaValue jvalue(T_VOID);
985   JNI_ArgumentPusherArray ap(methodID, args);
986   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
987   return obj;
988 JNI_END
989 
990 
991 DT_RETURN_MARK_DECL(NewObjectV, jobject
992                     , HOTSPOT_JNI_NEWOBJECTV_RETURN(_ret_ref));
993 
994 JNI_ENTRY(jobject, jni_NewObjectV(JNIEnv *env, jclass clazz, jmethodID methodID, va_list args))
995   HOTSPOT_JNI_NEWOBJECTV_ENTRY(env, clazz, (uintptr_t) methodID);
996 
997   jobject obj = NULL;
998   DT_RETURN_MARK(NewObjectV, jobject, (const jobject&)obj);
999 
1000   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
1001   obj = JNIHandles::make_local(THREAD, i);
1002   JavaValue jvalue(T_VOID);
1003   JNI_ArgumentPusherVaArg ap(methodID, args);
1004   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1005   return obj;
1006 JNI_END
1007 
1008 
1009 DT_RETURN_MARK_DECL(NewObject, jobject
1010                     , HOTSPOT_JNI_NEWOBJECT_RETURN(_ret_ref));
1011 
1012 JNI_ENTRY(jobject, jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...))
1013   HOTSPOT_JNI_NEWOBJECT_ENTRY(env, clazz, (uintptr_t) methodID);
1014 
1015   jobject obj = NULL;
1016   DT_RETURN_MARK(NewObject, jobject, (const jobject&)obj);
1017 
1018   instanceOop i = InstanceKlass::allocate_instance(JNIHandles::resolve_non_null(clazz), CHECK_NULL);
1019   obj = JNIHandles::make_local(THREAD, i);
1020   va_list args;
1021   va_start(args, methodID);
1022   JavaValue jvalue(T_VOID);
1023   JNI_ArgumentPusherVaArg ap(methodID, args);
1024   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_NULL);
1025   va_end(args);
1026   return obj;
1027 JNI_END
1028 
1029 
1030 JNI_ENTRY(jclass, jni_GetObjectClass(JNIEnv *env, jobject obj))
1031   HOTSPOT_JNI_GETOBJECTCLASS_ENTRY(env, obj);
1032 
1033   Klass* k = JNIHandles::resolve_non_null(obj)->klass();
1034   jclass ret =
1035     (jclass) JNIHandles::make_local(THREAD, k->java_mirror());
1036 
1037   HOTSPOT_JNI_GETOBJECTCLASS_RETURN(ret);
1038   return ret;
1039 JNI_END
1040 
1041 JNI_ENTRY_NO_PRESERVE(jboolean, jni_IsInstanceOf(JNIEnv *env, jobject obj, jclass clazz))
1042   HOTSPOT_JNI_ISINSTANCEOF_ENTRY(env, obj, clazz);
1043 
1044   jboolean ret = JNI_TRUE;
1045   if (obj != NULL) {
1046     ret = JNI_FALSE;
1047     Klass* k = java_lang_Class::as_Klass(
1048       JNIHandles::resolve_non_null(clazz));
1049     if (k != NULL) {
1050       ret = JNIHandles::resolve_non_null(obj)->is_a(k) ? JNI_TRUE : JNI_FALSE;
1051     }
1052   }
1053 
1054   HOTSPOT_JNI_ISINSTANCEOF_RETURN(ret);
1055   return ret;
1056 JNI_END
1057 
1058 
get_method_id(JNIEnv * env,jclass clazz,const char * name_str,const char * sig,bool is_static,TRAPS)1059 static jmethodID get_method_id(JNIEnv *env, jclass clazz, const char *name_str,
1060                                const char *sig, bool is_static, TRAPS) {
1061   // %%%% This code should probably just call into a method in the LinkResolver
1062   //
1063   // The class should have been loaded (we have an instance of the class
1064   // passed in) so the method and signature should already be in the symbol
1065   // table.  If they're not there, the method doesn't exist.
1066   const char *name_to_probe = (name_str == NULL)
1067                         ? vmSymbols::object_initializer_name()->as_C_string()
1068                         : name_str;
1069   TempNewSymbol name = SymbolTable::probe(name_to_probe, (int)strlen(name_to_probe));
1070   TempNewSymbol signature = SymbolTable::probe(sig, (int)strlen(sig));
1071 
1072   if (name == NULL || signature == NULL) {
1073     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), name_str);
1074   }
1075 
1076   oop mirror = JNIHandles::resolve_non_null(clazz);
1077   Klass* klass = java_lang_Class::as_Klass(mirror);
1078 
1079   // Throw a NoSuchMethodError exception if we have an instance of a
1080   // primitive java.lang.Class
1081   if (java_lang_Class::is_primitive(mirror)) {
1082     ResourceMark rm(THREAD);
1083     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), err_msg("%s%s.%s%s", is_static ? "static " : "", klass->signature_name(), name_str, sig));
1084   }
1085 
1086   // Make sure class is linked and initialized before handing id's out to
1087   // Method*s.
1088   klass->initialize(CHECK_NULL);
1089 
1090   Method* m;
1091   if (name == vmSymbols::object_initializer_name() ||
1092       name == vmSymbols::class_initializer_name()) {
1093     // Never search superclasses for constructors
1094     if (klass->is_instance_klass()) {
1095       m = InstanceKlass::cast(klass)->find_method(name, signature);
1096     } else {
1097       m = NULL;
1098     }
1099   } else {
1100     m = klass->lookup_method(name, signature);
1101     if (m == NULL &&  klass->is_instance_klass()) {
1102       m = InstanceKlass::cast(klass)->lookup_method_in_ordered_interfaces(name, signature);
1103     }
1104   }
1105   if (m == NULL || (m->is_static() != is_static)) {
1106     ResourceMark rm(THREAD);
1107     THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), err_msg("%s%s.%s%s", is_static ? "static " : "", klass->signature_name(), name_str, sig));
1108   }
1109   return m->jmethod_id();
1110 }
1111 
1112 
1113 JNI_ENTRY(jmethodID, jni_GetMethodID(JNIEnv *env, jclass clazz,
1114           const char *name, const char *sig))
1115   HOTSPOT_JNI_GETMETHODID_ENTRY(env, clazz, (char *) name, (char *) sig);
1116   jmethodID ret = get_method_id(env, clazz, name, sig, false, thread);
1117   HOTSPOT_JNI_GETMETHODID_RETURN((uintptr_t) ret);
1118   return ret;
1119 JNI_END
1120 
1121 
1122 JNI_ENTRY(jmethodID, jni_GetStaticMethodID(JNIEnv *env, jclass clazz,
1123           const char *name, const char *sig))
1124   HOTSPOT_JNI_GETSTATICMETHODID_ENTRY(env, (char *) clazz, (char *) name, (char *)sig);
1125   jmethodID ret = get_method_id(env, clazz, name, sig, true, thread);
1126   HOTSPOT_JNI_GETSTATICMETHODID_RETURN((uintptr_t) ret);
1127   return ret;
1128 JNI_END
1129 
1130 
1131 
1132 //
1133 // Calling Methods
1134 //
1135 
1136 
1137 #define DEFINE_CALLMETHOD(ResultType, Result, Tag \
1138                           , EntryProbe, ReturnProbe)    \
1139 \
1140   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##Method, ResultType \
1141                           , ReturnProbe);                          \
1142 \
1143 JNI_ENTRY(ResultType, \
1144           jni_Call##Result##Method(JNIEnv *env, jobject obj, jmethodID methodID, ...)) \
1145 \
1146   EntryProbe; \
1147   ResultType ret = 0;\
1148   DT_RETURN_MARK_FOR(Result, Call##Result##Method, ResultType, \
1149                      (const ResultType&)ret);\
1150 \
1151   va_list args; \
1152   va_start(args, methodID); \
1153   JavaValue jvalue(Tag); \
1154   JNI_ArgumentPusherVaArg ap(methodID, args); \
1155   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1156   va_end(args); \
1157   ret = jvalue.get_##ResultType(); \
1158   return ret;\
1159 JNI_END
1160 
1161 // the runtime type of subword integral basic types is integer
1162 DEFINE_CALLMETHOD(jboolean, Boolean, T_BOOLEAN
1163                   , HOTSPOT_JNI_CALLBOOLEANMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1164                   HOTSPOT_JNI_CALLBOOLEANMETHOD_RETURN(_ret_ref))
1165 DEFINE_CALLMETHOD(jbyte,    Byte,    T_BYTE
1166                   , HOTSPOT_JNI_CALLBYTEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1167                   HOTSPOT_JNI_CALLBYTEMETHOD_RETURN(_ret_ref))
1168 DEFINE_CALLMETHOD(jchar,    Char,    T_CHAR
1169                   , HOTSPOT_JNI_CALLCHARMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1170                   HOTSPOT_JNI_CALLCHARMETHOD_RETURN(_ret_ref))
1171 DEFINE_CALLMETHOD(jshort,   Short,   T_SHORT
1172                   , HOTSPOT_JNI_CALLSHORTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1173                   HOTSPOT_JNI_CALLSHORTMETHOD_RETURN(_ret_ref))
1174 
1175 DEFINE_CALLMETHOD(jobject,  Object,  T_OBJECT
1176                   , HOTSPOT_JNI_CALLOBJECTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1177                   HOTSPOT_JNI_CALLOBJECTMETHOD_RETURN(_ret_ref))
1178 DEFINE_CALLMETHOD(jint,     Int,     T_INT,
1179                   HOTSPOT_JNI_CALLINTMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1180                   HOTSPOT_JNI_CALLINTMETHOD_RETURN(_ret_ref))
1181 DEFINE_CALLMETHOD(jlong,    Long,    T_LONG
1182                   , HOTSPOT_JNI_CALLLONGMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1183                   HOTSPOT_JNI_CALLLONGMETHOD_RETURN(_ret_ref))
1184 // Float and double probes don't return value because dtrace doesn't currently support it
1185 DEFINE_CALLMETHOD(jfloat,   Float,   T_FLOAT
1186                   , HOTSPOT_JNI_CALLFLOATMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1187                   HOTSPOT_JNI_CALLFLOATMETHOD_RETURN())
1188 DEFINE_CALLMETHOD(jdouble,  Double,  T_DOUBLE
1189                   , HOTSPOT_JNI_CALLDOUBLEMETHOD_ENTRY(env, obj, (uintptr_t)methodID),
1190                   HOTSPOT_JNI_CALLDOUBLEMETHOD_RETURN())
1191 
1192 #define DEFINE_CALLMETHODV(ResultType, Result, Tag \
1193                           , EntryProbe, ReturnProbe)    \
1194 \
1195   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodV, ResultType \
1196                           , ReturnProbe);                          \
1197 \
1198 JNI_ENTRY(ResultType, \
1199           jni_Call##Result##MethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args)) \
1200 \
1201   EntryProbe;\
1202   ResultType ret = 0;\
1203   DT_RETURN_MARK_FOR(Result, Call##Result##MethodV, ResultType, \
1204                      (const ResultType&)ret);\
1205 \
1206   JavaValue jvalue(Tag); \
1207   JNI_ArgumentPusherVaArg ap(methodID, args); \
1208   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1209   ret = jvalue.get_##ResultType(); \
1210   return ret;\
1211 JNI_END
1212 
1213 // the runtime type of subword integral basic types is integer
1214 DEFINE_CALLMETHODV(jboolean, Boolean, T_BOOLEAN
1215                   , HOTSPOT_JNI_CALLBOOLEANMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1216                   HOTSPOT_JNI_CALLBOOLEANMETHODV_RETURN(_ret_ref))
1217 DEFINE_CALLMETHODV(jbyte,    Byte,    T_BYTE
1218                   , HOTSPOT_JNI_CALLBYTEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1219                   HOTSPOT_JNI_CALLBYTEMETHODV_RETURN(_ret_ref))
1220 DEFINE_CALLMETHODV(jchar,    Char,    T_CHAR
1221                   , HOTSPOT_JNI_CALLCHARMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1222                   HOTSPOT_JNI_CALLCHARMETHODV_RETURN(_ret_ref))
1223 DEFINE_CALLMETHODV(jshort,   Short,   T_SHORT
1224                   , HOTSPOT_JNI_CALLSHORTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1225                   HOTSPOT_JNI_CALLSHORTMETHODV_RETURN(_ret_ref))
1226 
1227 DEFINE_CALLMETHODV(jobject,  Object,  T_OBJECT
1228                   , HOTSPOT_JNI_CALLOBJECTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1229                   HOTSPOT_JNI_CALLOBJECTMETHODV_RETURN(_ret_ref))
1230 DEFINE_CALLMETHODV(jint,     Int,     T_INT,
1231                   HOTSPOT_JNI_CALLINTMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1232                   HOTSPOT_JNI_CALLINTMETHODV_RETURN(_ret_ref))
1233 DEFINE_CALLMETHODV(jlong,    Long,    T_LONG
1234                   , HOTSPOT_JNI_CALLLONGMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1235                   HOTSPOT_JNI_CALLLONGMETHODV_RETURN(_ret_ref))
1236 // Float and double probes don't return value because dtrace doesn't currently support it
1237 DEFINE_CALLMETHODV(jfloat,   Float,   T_FLOAT
1238                   , HOTSPOT_JNI_CALLFLOATMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1239                   HOTSPOT_JNI_CALLFLOATMETHODV_RETURN())
1240 DEFINE_CALLMETHODV(jdouble,  Double,  T_DOUBLE
1241                   , HOTSPOT_JNI_CALLDOUBLEMETHODV_ENTRY(env, obj, (uintptr_t)methodID),
1242                   HOTSPOT_JNI_CALLDOUBLEMETHODV_RETURN())
1243 
1244 #define DEFINE_CALLMETHODA(ResultType, Result, Tag \
1245                           , EntryProbe, ReturnProbe)    \
1246 \
1247   DT_RETURN_MARK_DECL_FOR(Result, Call##Result##MethodA, ResultType \
1248                           , ReturnProbe);                          \
1249 \
1250 JNI_ENTRY(ResultType, \
1251           jni_Call##Result##MethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args)) \
1252   EntryProbe; \
1253   ResultType ret = 0;\
1254   DT_RETURN_MARK_FOR(Result, Call##Result##MethodA, ResultType, \
1255                      (const ResultType&)ret);\
1256 \
1257   JavaValue jvalue(Tag); \
1258   JNI_ArgumentPusherArray ap(methodID, args); \
1259   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK_0); \
1260   ret = jvalue.get_##ResultType(); \
1261   return ret;\
1262 JNI_END
1263 
1264 // the runtime type of subword integral basic types is integer
1265 DEFINE_CALLMETHODA(jboolean, Boolean, T_BOOLEAN
1266                   , HOTSPOT_JNI_CALLBOOLEANMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1267                   HOTSPOT_JNI_CALLBOOLEANMETHODA_RETURN(_ret_ref))
1268 DEFINE_CALLMETHODA(jbyte,    Byte,    T_BYTE
1269                   , HOTSPOT_JNI_CALLBYTEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1270                   HOTSPOT_JNI_CALLBYTEMETHODA_RETURN(_ret_ref))
1271 DEFINE_CALLMETHODA(jchar,    Char,    T_CHAR
1272                   , HOTSPOT_JNI_CALLCHARMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1273                   HOTSPOT_JNI_CALLCHARMETHODA_RETURN(_ret_ref))
1274 DEFINE_CALLMETHODA(jshort,   Short,   T_SHORT
1275                   , HOTSPOT_JNI_CALLSHORTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1276                   HOTSPOT_JNI_CALLSHORTMETHODA_RETURN(_ret_ref))
1277 
1278 DEFINE_CALLMETHODA(jobject,  Object,  T_OBJECT
1279                   , HOTSPOT_JNI_CALLOBJECTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1280                   HOTSPOT_JNI_CALLOBJECTMETHODA_RETURN(_ret_ref))
1281 DEFINE_CALLMETHODA(jint,     Int,     T_INT,
1282                   HOTSPOT_JNI_CALLINTMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1283                   HOTSPOT_JNI_CALLINTMETHODA_RETURN(_ret_ref))
1284 DEFINE_CALLMETHODA(jlong,    Long,    T_LONG
1285                   , HOTSPOT_JNI_CALLLONGMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1286                   HOTSPOT_JNI_CALLLONGMETHODA_RETURN(_ret_ref))
1287 // Float and double probes don't return value because dtrace doesn't currently support it
1288 DEFINE_CALLMETHODA(jfloat,   Float,   T_FLOAT
1289                   , HOTSPOT_JNI_CALLFLOATMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1290                   HOTSPOT_JNI_CALLFLOATMETHODA_RETURN())
1291 DEFINE_CALLMETHODA(jdouble,  Double,  T_DOUBLE
1292                   , HOTSPOT_JNI_CALLDOUBLEMETHODA_ENTRY(env, obj, (uintptr_t)methodID),
1293                   HOTSPOT_JNI_CALLDOUBLEMETHODA_RETURN())
1294 
1295 DT_VOID_RETURN_MARK_DECL(CallVoidMethod, HOTSPOT_JNI_CALLVOIDMETHOD_RETURN());
1296 DT_VOID_RETURN_MARK_DECL(CallVoidMethodV, HOTSPOT_JNI_CALLVOIDMETHODV_RETURN());
1297 DT_VOID_RETURN_MARK_DECL(CallVoidMethodA, HOTSPOT_JNI_CALLVOIDMETHODA_RETURN());
1298 
1299 
1300 JNI_ENTRY(void, jni_CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID, ...))
1301   HOTSPOT_JNI_CALLVOIDMETHOD_ENTRY(env, obj, (uintptr_t) methodID);
1302   DT_VOID_RETURN_MARK(CallVoidMethod);
1303 
1304   va_list args;
1305   va_start(args, methodID);
1306   JavaValue jvalue(T_VOID);
1307   JNI_ArgumentPusherVaArg ap(methodID, args);
1308   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1309   va_end(args);
1310 JNI_END
1311 
1312 
1313 JNI_ENTRY(void, jni_CallVoidMethodV(JNIEnv *env, jobject obj, jmethodID methodID, va_list args))
1314   HOTSPOT_JNI_CALLVOIDMETHODV_ENTRY(env, obj, (uintptr_t) methodID);
1315   DT_VOID_RETURN_MARK(CallVoidMethodV);
1316 
1317   JavaValue jvalue(T_VOID);
1318   JNI_ArgumentPusherVaArg ap(methodID, args);
1319   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1320 JNI_END
1321 
1322 
1323 JNI_ENTRY(void, jni_CallVoidMethodA(JNIEnv *env, jobject obj, jmethodID methodID, const jvalue *args))
1324   HOTSPOT_JNI_CALLVOIDMETHODA_ENTRY(env, obj, (uintptr_t) methodID);
1325   DT_VOID_RETURN_MARK(CallVoidMethodA);
1326 
1327   JavaValue jvalue(T_VOID);
1328   JNI_ArgumentPusherArray ap(methodID, args);
1329   jni_invoke_nonstatic(env, &jvalue, obj, JNI_VIRTUAL, methodID, &ap, CHECK);
1330 JNI_END
1331 
1332 
1333 
1334 #define DEFINE_CALLNONVIRTUALMETHOD(ResultType, Result, Tag \
1335                                     , EntryProbe, ReturnProbe)      \
1336 \
1337   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##Method, ResultType \
1338                           , ReturnProbe);\
1339 \
1340 JNI_ENTRY(ResultType, \
1341           jni_CallNonvirtual##Result##Method(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...)) \
1342 \
1343   EntryProbe;\
1344   ResultType ret;\
1345   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##Method, ResultType, \
1346                      (const ResultType&)ret);\
1347 \
1348   va_list args; \
1349   va_start(args, methodID); \
1350   JavaValue jvalue(Tag); \
1351   JNI_ArgumentPusherVaArg ap(methodID, args); \
1352   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1353   va_end(args); \
1354   ret = jvalue.get_##ResultType(); \
1355   return ret;\
1356 JNI_END
1357 
1358 // the runtime type of subword integral basic types is integer
1359 DEFINE_CALLNONVIRTUALMETHOD(jboolean, Boolean, T_BOOLEAN
1360                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1361                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHOD_RETURN(_ret_ref))
1362 DEFINE_CALLNONVIRTUALMETHOD(jbyte,    Byte,    T_BYTE
1363                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1364                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHOD_RETURN(_ret_ref))
1365 DEFINE_CALLNONVIRTUALMETHOD(jchar,    Char,    T_CHAR
1366                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1367                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHOD_RETURN(_ret_ref))
1368 DEFINE_CALLNONVIRTUALMETHOD(jshort,   Short,   T_SHORT
1369                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1370                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHOD_RETURN(_ret_ref))
1371 
1372 DEFINE_CALLNONVIRTUALMETHOD(jobject,  Object,  T_OBJECT
1373                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1374                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHOD_RETURN(_ret_ref))
1375 DEFINE_CALLNONVIRTUALMETHOD(jint,     Int,     T_INT
1376                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1377                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHOD_RETURN(_ret_ref))
1378 DEFINE_CALLNONVIRTUALMETHOD(jlong,    Long,    T_LONG
1379                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1380 // Float and double probes don't return value because dtrace doesn't currently support it
1381                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHOD_RETURN(_ret_ref))
1382 DEFINE_CALLNONVIRTUALMETHOD(jfloat,   Float,   T_FLOAT
1383                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1384                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHOD_RETURN())
1385 DEFINE_CALLNONVIRTUALMETHOD(jdouble,  Double,  T_DOUBLE
1386                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_ENTRY(env, obj, cls, (uintptr_t)methodID),
1387                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHOD_RETURN())
1388 
1389 #define DEFINE_CALLNONVIRTUALMETHODV(ResultType, Result, Tag \
1390                                     , EntryProbe, ReturnProbe)      \
1391 \
1392   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodV, ResultType \
1393                           , ReturnProbe);\
1394 \
1395 JNI_ENTRY(ResultType, \
1396           jni_CallNonvirtual##Result##MethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args)) \
1397 \
1398   EntryProbe;\
1399   ResultType ret;\
1400   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodV, ResultType, \
1401                      (const ResultType&)ret);\
1402 \
1403   JavaValue jvalue(Tag); \
1404   JNI_ArgumentPusherVaArg ap(methodID, args); \
1405   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1406   ret = jvalue.get_##ResultType(); \
1407   return ret;\
1408 JNI_END
1409 
1410 // the runtime type of subword integral basic types is integer
1411 DEFINE_CALLNONVIRTUALMETHODV(jboolean, Boolean, T_BOOLEAN
1412                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1413                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODV_RETURN(_ret_ref))
1414 DEFINE_CALLNONVIRTUALMETHODV(jbyte,    Byte,    T_BYTE
1415                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1416                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODV_RETURN(_ret_ref))
1417 DEFINE_CALLNONVIRTUALMETHODV(jchar,    Char,    T_CHAR
1418                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1419                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODV_RETURN(_ret_ref))
1420 DEFINE_CALLNONVIRTUALMETHODV(jshort,   Short,   T_SHORT
1421                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1422                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODV_RETURN(_ret_ref))
1423 
1424 DEFINE_CALLNONVIRTUALMETHODV(jobject,  Object,  T_OBJECT
1425                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1426                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODV_RETURN(_ret_ref))
1427 DEFINE_CALLNONVIRTUALMETHODV(jint,     Int,     T_INT
1428                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1429                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHODV_RETURN(_ret_ref))
1430 DEFINE_CALLNONVIRTUALMETHODV(jlong,    Long,    T_LONG
1431                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1432 // Float and double probes don't return value because dtrace doesn't currently support it
1433                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODV_RETURN(_ret_ref))
1434 DEFINE_CALLNONVIRTUALMETHODV(jfloat,   Float,   T_FLOAT
1435                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1436                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODV_RETURN())
1437 DEFINE_CALLNONVIRTUALMETHODV(jdouble,  Double,  T_DOUBLE
1438                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_ENTRY(env, obj, cls, (uintptr_t)methodID),
1439                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODV_RETURN())
1440 
1441 #define DEFINE_CALLNONVIRTUALMETHODA(ResultType, Result, Tag \
1442                                     , EntryProbe, ReturnProbe)      \
1443 \
1444   DT_RETURN_MARK_DECL_FOR(Result, CallNonvirtual##Result##MethodA, ResultType \
1445                           , ReturnProbe);\
1446 \
1447 JNI_ENTRY(ResultType, \
1448           jni_CallNonvirtual##Result##MethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args)) \
1449 \
1450   EntryProbe;\
1451   ResultType ret;\
1452   DT_RETURN_MARK_FOR(Result, CallNonvirtual##Result##MethodA, ResultType, \
1453                      (const ResultType&)ret);\
1454 \
1455   JavaValue jvalue(Tag); \
1456   JNI_ArgumentPusherArray ap(methodID, args); \
1457   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK_0); \
1458   ret = jvalue.get_##ResultType(); \
1459   return ret;\
1460 JNI_END
1461 
1462 // the runtime type of subword integral basic types is integer
1463 DEFINE_CALLNONVIRTUALMETHODA(jboolean, Boolean, T_BOOLEAN
1464                             , HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1465                             HOTSPOT_JNI_CALLNONVIRTUALBOOLEANMETHODA_RETURN(_ret_ref))
1466 DEFINE_CALLNONVIRTUALMETHODA(jbyte,    Byte,    T_BYTE
1467                             , HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1468                             HOTSPOT_JNI_CALLNONVIRTUALBYTEMETHODA_RETURN(_ret_ref))
1469 DEFINE_CALLNONVIRTUALMETHODA(jchar,    Char,    T_CHAR
1470                             , HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1471                             HOTSPOT_JNI_CALLNONVIRTUALCHARMETHODA_RETURN(_ret_ref))
1472 DEFINE_CALLNONVIRTUALMETHODA(jshort,   Short,   T_SHORT
1473                             , HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1474                             HOTSPOT_JNI_CALLNONVIRTUALSHORTMETHODA_RETURN(_ret_ref))
1475 
1476 DEFINE_CALLNONVIRTUALMETHODA(jobject,  Object,  T_OBJECT
1477                             , HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1478                             HOTSPOT_JNI_CALLNONVIRTUALOBJECTMETHODA_RETURN(_ret_ref))
1479 DEFINE_CALLNONVIRTUALMETHODA(jint,     Int,     T_INT
1480                             , HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1481                             HOTSPOT_JNI_CALLNONVIRTUALINTMETHODA_RETURN(_ret_ref))
1482 DEFINE_CALLNONVIRTUALMETHODA(jlong,    Long,    T_LONG
1483                             , HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1484 // Float and double probes don't return value because dtrace doesn't currently support it
1485                             HOTSPOT_JNI_CALLNONVIRTUALLONGMETHODA_RETURN(_ret_ref))
1486 DEFINE_CALLNONVIRTUALMETHODA(jfloat,   Float,   T_FLOAT
1487                             , HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1488                             HOTSPOT_JNI_CALLNONVIRTUALFLOATMETHODA_RETURN())
1489 DEFINE_CALLNONVIRTUALMETHODA(jdouble,  Double,  T_DOUBLE
1490                             , HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_ENTRY(env, obj, cls, (uintptr_t)methodID),
1491                             HOTSPOT_JNI_CALLNONVIRTUALDOUBLEMETHODA_RETURN())
1492 
1493 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethod
1494                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_RETURN());
1495 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodV
1496                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_RETURN());
1497 DT_VOID_RETURN_MARK_DECL(CallNonvirtualVoidMethodA
1498                          , HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_RETURN());
1499 
1500 JNI_ENTRY(void, jni_CallNonvirtualVoidMethod(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, ...))
1501   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHOD_ENTRY(env, obj, cls, (uintptr_t) methodID);
1502   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethod);
1503 
1504   va_list args;
1505   va_start(args, methodID);
1506   JavaValue jvalue(T_VOID);
1507   JNI_ArgumentPusherVaArg ap(methodID, args);
1508   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1509   va_end(args);
1510 JNI_END
1511 
1512 
1513 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodV(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, va_list args))
1514   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODV_ENTRY(
1515                env, obj, cls, (uintptr_t) methodID);
1516   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodV);
1517 
1518   JavaValue jvalue(T_VOID);
1519   JNI_ArgumentPusherVaArg ap(methodID, args);
1520   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1521 JNI_END
1522 
1523 
1524 JNI_ENTRY(void, jni_CallNonvirtualVoidMethodA(JNIEnv *env, jobject obj, jclass cls, jmethodID methodID, const jvalue *args))
1525   HOTSPOT_JNI_CALLNONVIRTUALVOIDMETHODA_ENTRY(
1526                 env, obj, cls, (uintptr_t) methodID);
1527   DT_VOID_RETURN_MARK(CallNonvirtualVoidMethodA);
1528   JavaValue jvalue(T_VOID);
1529   JNI_ArgumentPusherArray ap(methodID, args);
1530   jni_invoke_nonstatic(env, &jvalue, obj, JNI_NONVIRTUAL, methodID, &ap, CHECK);
1531 JNI_END
1532 
1533 
1534 
1535 #define DEFINE_CALLSTATICMETHOD(ResultType, Result, Tag \
1536                                 , EntryProbe, ResultProbe) \
1537 \
1538   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##Method, ResultType \
1539                           , ResultProbe);                               \
1540 \
1541 JNI_ENTRY(ResultType, \
1542           jni_CallStatic##Result##Method(JNIEnv *env, jclass cls, jmethodID methodID, ...)) \
1543 \
1544   EntryProbe; \
1545   ResultType ret = 0;\
1546   DT_RETURN_MARK_FOR(Result, CallStatic##Result##Method, ResultType, \
1547                      (const ResultType&)ret);\
1548 \
1549   va_list args; \
1550   va_start(args, methodID); \
1551   JavaValue jvalue(Tag); \
1552   JNI_ArgumentPusherVaArg ap(methodID, args); \
1553   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
1554   va_end(args); \
1555   ret = jvalue.get_##ResultType(); \
1556   return ret;\
1557 JNI_END
1558 
1559 // the runtime type of subword integral basic types is integer
1560 DEFINE_CALLSTATICMETHOD(jboolean, Boolean, T_BOOLEAN
1561                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1562                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHOD_RETURN(_ret_ref));
1563 DEFINE_CALLSTATICMETHOD(jbyte,    Byte,    T_BYTE
1564                         , HOTSPOT_JNI_CALLSTATICBYTEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1565                         HOTSPOT_JNI_CALLSTATICBYTEMETHOD_RETURN(_ret_ref));
1566 DEFINE_CALLSTATICMETHOD(jchar,    Char,    T_CHAR
1567                         , HOTSPOT_JNI_CALLSTATICCHARMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1568                         HOTSPOT_JNI_CALLSTATICCHARMETHOD_RETURN(_ret_ref));
1569 DEFINE_CALLSTATICMETHOD(jshort,   Short,   T_SHORT
1570                         , HOTSPOT_JNI_CALLSTATICSHORTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1571                         HOTSPOT_JNI_CALLSTATICSHORTMETHOD_RETURN(_ret_ref));
1572 
1573 DEFINE_CALLSTATICMETHOD(jobject,  Object,  T_OBJECT
1574                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1575                         HOTSPOT_JNI_CALLSTATICOBJECTMETHOD_RETURN(_ret_ref));
1576 DEFINE_CALLSTATICMETHOD(jint,     Int,     T_INT
1577                         , HOTSPOT_JNI_CALLSTATICINTMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1578                         HOTSPOT_JNI_CALLSTATICINTMETHOD_RETURN(_ret_ref));
1579 DEFINE_CALLSTATICMETHOD(jlong,    Long,    T_LONG
1580                         , HOTSPOT_JNI_CALLSTATICLONGMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1581                         HOTSPOT_JNI_CALLSTATICLONGMETHOD_RETURN(_ret_ref));
1582 // Float and double probes don't return value because dtrace doesn't currently support it
1583 DEFINE_CALLSTATICMETHOD(jfloat,   Float,   T_FLOAT
1584                         , HOTSPOT_JNI_CALLSTATICFLOATMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1585                         HOTSPOT_JNI_CALLSTATICFLOATMETHOD_RETURN());
1586 DEFINE_CALLSTATICMETHOD(jdouble,  Double,  T_DOUBLE
1587                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_ENTRY(env, cls, (uintptr_t)methodID),
1588                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHOD_RETURN());
1589 
1590 #define DEFINE_CALLSTATICMETHODV(ResultType, Result, Tag \
1591                                 , EntryProbe, ResultProbe) \
1592 \
1593   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodV, ResultType \
1594                           , ResultProbe);                               \
1595 \
1596 JNI_ENTRY(ResultType, \
1597           jni_CallStatic##Result##MethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args)) \
1598 \
1599   EntryProbe; \
1600   ResultType ret = 0;\
1601   DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodV, ResultType, \
1602                      (const ResultType&)ret);\
1603 \
1604   JavaValue jvalue(Tag); \
1605   JNI_ArgumentPusherVaArg ap(methodID, args); \
1606   /* Make sure class is initialized before trying to invoke its method */ \
1607   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); \
1608   k->initialize(CHECK_0); \
1609   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
1610   va_end(args); \
1611   ret = jvalue.get_##ResultType(); \
1612   return ret;\
1613 JNI_END
1614 
1615 // the runtime type of subword integral basic types is integer
1616 DEFINE_CALLSTATICMETHODV(jboolean, Boolean, T_BOOLEAN
1617                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1618                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHODV_RETURN(_ret_ref));
1619 DEFINE_CALLSTATICMETHODV(jbyte,    Byte,    T_BYTE
1620                         , HOTSPOT_JNI_CALLSTATICBYTEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1621                         HOTSPOT_JNI_CALLSTATICBYTEMETHODV_RETURN(_ret_ref));
1622 DEFINE_CALLSTATICMETHODV(jchar,    Char,    T_CHAR
1623                         , HOTSPOT_JNI_CALLSTATICCHARMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1624                         HOTSPOT_JNI_CALLSTATICCHARMETHODV_RETURN(_ret_ref));
1625 DEFINE_CALLSTATICMETHODV(jshort,   Short,   T_SHORT
1626                         , HOTSPOT_JNI_CALLSTATICSHORTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1627                         HOTSPOT_JNI_CALLSTATICSHORTMETHODV_RETURN(_ret_ref));
1628 
1629 DEFINE_CALLSTATICMETHODV(jobject,  Object,  T_OBJECT
1630                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1631                         HOTSPOT_JNI_CALLSTATICOBJECTMETHODV_RETURN(_ret_ref));
1632 DEFINE_CALLSTATICMETHODV(jint,     Int,     T_INT
1633                         , HOTSPOT_JNI_CALLSTATICINTMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1634                         HOTSPOT_JNI_CALLSTATICINTMETHODV_RETURN(_ret_ref));
1635 DEFINE_CALLSTATICMETHODV(jlong,    Long,    T_LONG
1636                         , HOTSPOT_JNI_CALLSTATICLONGMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1637                         HOTSPOT_JNI_CALLSTATICLONGMETHODV_RETURN(_ret_ref));
1638 // Float and double probes don't return value because dtrace doesn't currently support it
1639 DEFINE_CALLSTATICMETHODV(jfloat,   Float,   T_FLOAT
1640                         , HOTSPOT_JNI_CALLSTATICFLOATMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1641                         HOTSPOT_JNI_CALLSTATICFLOATMETHODV_RETURN());
1642 DEFINE_CALLSTATICMETHODV(jdouble,  Double,  T_DOUBLE
1643                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_ENTRY(env, cls, (uintptr_t)methodID),
1644                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHODV_RETURN());
1645 
1646 #define DEFINE_CALLSTATICMETHODA(ResultType, Result, Tag \
1647                                 , EntryProbe, ResultProbe) \
1648 \
1649   DT_RETURN_MARK_DECL_FOR(Result, CallStatic##Result##MethodA, ResultType \
1650                           , ResultProbe);                               \
1651 \
1652 JNI_ENTRY(ResultType, \
1653           jni_CallStatic##Result##MethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args)) \
1654 \
1655   EntryProbe; \
1656   ResultType ret = 0;\
1657   DT_RETURN_MARK_FOR(Result, CallStatic##Result##MethodA, ResultType, \
1658                      (const ResultType&)ret);\
1659 \
1660   JavaValue jvalue(Tag); \
1661   JNI_ArgumentPusherArray ap(methodID, args); \
1662   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK_0); \
1663   ret = jvalue.get_##ResultType(); \
1664   return ret;\
1665 JNI_END
1666 
1667 // the runtime type of subword integral basic types is integer
1668 DEFINE_CALLSTATICMETHODA(jboolean, Boolean, T_BOOLEAN
1669                         , HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1670                         HOTSPOT_JNI_CALLSTATICBOOLEANMETHODA_RETURN(_ret_ref));
1671 DEFINE_CALLSTATICMETHODA(jbyte,    Byte,    T_BYTE
1672                         , HOTSPOT_JNI_CALLSTATICBYTEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1673                         HOTSPOT_JNI_CALLSTATICBYTEMETHODA_RETURN(_ret_ref));
1674 DEFINE_CALLSTATICMETHODA(jchar,    Char,    T_CHAR
1675                         , HOTSPOT_JNI_CALLSTATICCHARMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1676                         HOTSPOT_JNI_CALLSTATICCHARMETHODA_RETURN(_ret_ref));
1677 DEFINE_CALLSTATICMETHODA(jshort,   Short,   T_SHORT
1678                         , HOTSPOT_JNI_CALLSTATICSHORTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1679                         HOTSPOT_JNI_CALLSTATICSHORTMETHODA_RETURN(_ret_ref));
1680 
1681 DEFINE_CALLSTATICMETHODA(jobject,  Object,  T_OBJECT
1682                         , HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1683                         HOTSPOT_JNI_CALLSTATICOBJECTMETHODA_RETURN(_ret_ref));
1684 DEFINE_CALLSTATICMETHODA(jint,     Int,     T_INT
1685                         , HOTSPOT_JNI_CALLSTATICINTMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1686                         HOTSPOT_JNI_CALLSTATICINTMETHODA_RETURN(_ret_ref));
1687 DEFINE_CALLSTATICMETHODA(jlong,    Long,    T_LONG
1688                         , HOTSPOT_JNI_CALLSTATICLONGMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1689                         HOTSPOT_JNI_CALLSTATICLONGMETHODA_RETURN(_ret_ref));
1690 // Float and double probes don't return value because dtrace doesn't currently support it
1691 DEFINE_CALLSTATICMETHODA(jfloat,   Float,   T_FLOAT
1692                         , HOTSPOT_JNI_CALLSTATICFLOATMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1693                         HOTSPOT_JNI_CALLSTATICFLOATMETHODA_RETURN());
1694 DEFINE_CALLSTATICMETHODA(jdouble,  Double,  T_DOUBLE
1695                         , HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_ENTRY(env, cls, (uintptr_t)methodID),
1696                         HOTSPOT_JNI_CALLSTATICDOUBLEMETHODA_RETURN());
1697 
1698 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethod
1699                          , HOTSPOT_JNI_CALLSTATICVOIDMETHOD_RETURN());
1700 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodV
1701                          , HOTSPOT_JNI_CALLSTATICVOIDMETHODV_RETURN());
1702 DT_VOID_RETURN_MARK_DECL(CallStaticVoidMethodA
1703                          , HOTSPOT_JNI_CALLSTATICVOIDMETHODA_RETURN());
1704 
1705 JNI_ENTRY(void, jni_CallStaticVoidMethod(JNIEnv *env, jclass cls, jmethodID methodID, ...))
1706   HOTSPOT_JNI_CALLSTATICVOIDMETHOD_ENTRY(env, cls, (uintptr_t) methodID);
1707   DT_VOID_RETURN_MARK(CallStaticVoidMethod);
1708 
1709   va_list args;
1710   va_start(args, methodID);
1711   JavaValue jvalue(T_VOID);
1712   JNI_ArgumentPusherVaArg ap(methodID, args);
1713   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
1714   va_end(args);
1715 JNI_END
1716 
1717 
1718 JNI_ENTRY(void, jni_CallStaticVoidMethodV(JNIEnv *env, jclass cls, jmethodID methodID, va_list args))
1719   HOTSPOT_JNI_CALLSTATICVOIDMETHODV_ENTRY(env, cls, (uintptr_t) methodID);
1720   DT_VOID_RETURN_MARK(CallStaticVoidMethodV);
1721 
1722   JavaValue jvalue(T_VOID);
1723   JNI_ArgumentPusherVaArg ap(methodID, args);
1724   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
1725 JNI_END
1726 
1727 
1728 JNI_ENTRY(void, jni_CallStaticVoidMethodA(JNIEnv *env, jclass cls, jmethodID methodID, const jvalue *args))
1729   HOTSPOT_JNI_CALLSTATICVOIDMETHODA_ENTRY(env, cls, (uintptr_t) methodID);
1730   DT_VOID_RETURN_MARK(CallStaticVoidMethodA);
1731 
1732   JavaValue jvalue(T_VOID);
1733   JNI_ArgumentPusherArray ap(methodID, args);
1734   jni_invoke_static(env, &jvalue, NULL, JNI_STATIC, methodID, &ap, CHECK);
1735 JNI_END
1736 
1737 
1738 //
1739 // Accessing Fields
1740 //
1741 
1742 
1743 DT_RETURN_MARK_DECL(GetFieldID, jfieldID
1744                     , HOTSPOT_JNI_GETFIELDID_RETURN((uintptr_t)_ret_ref));
1745 
1746 JNI_ENTRY(jfieldID, jni_GetFieldID(JNIEnv *env, jclass clazz,
1747           const char *name, const char *sig))
1748   HOTSPOT_JNI_GETFIELDID_ENTRY(env, clazz, (char *) name, (char *) sig);
1749   jfieldID ret = 0;
1750   DT_RETURN_MARK(GetFieldID, jfieldID, (const jfieldID&)ret);
1751 
1752   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
1753 
1754   // The class should have been loaded (we have an instance of the class
1755   // passed in) so the field and signature should already be in the symbol
1756   // table.  If they're not there, the field doesn't exist.
1757   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
1758   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
1759   if (fieldname == NULL || signame == NULL) {
1760     ResourceMark rm;
1761     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1762   }
1763 
1764   // Make sure class is initialized before handing id's out to fields
1765   k->initialize(CHECK_NULL);
1766 
1767   fieldDescriptor fd;
1768   if (!k->is_instance_klass() ||
1769       !InstanceKlass::cast(k)->find_field(fieldname, signame, false, &fd)) {
1770     ResourceMark rm;
1771     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), err_msg("%s.%s %s", k->external_name(), name, sig));
1772   }
1773 
1774   // A jfieldID for a non-static field is simply the offset of the field within the instanceOop
1775   // It may also have hash bits for k, if VerifyJNIFields is turned on.
1776   ret = jfieldIDWorkaround::to_instance_jfieldID(k, fd.offset());
1777   return ret;
1778 JNI_END
1779 
1780 
1781 JNI_ENTRY(jobject, jni_GetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID))
1782   HOTSPOT_JNI_GETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID);
1783   oop o = JNIHandles::resolve_non_null(obj);
1784   Klass* k = o->klass();
1785   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1786   // Keep JVMTI addition small and only check enabled flag here.
1787   // jni_GetField_probe() assumes that is okay to create handles.
1788   if (JvmtiExport::should_post_field_access()) {
1789     o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false);
1790   }
1791   oop loaded_obj = HeapAccess<ON_UNKNOWN_OOP_REF>::oop_load_at(o, offset);
1792   jobject ret = JNIHandles::make_local(THREAD, loaded_obj);
1793   HOTSPOT_JNI_GETOBJECTFIELD_RETURN(ret);
1794   return ret;
1795 JNI_END
1796 
1797 
1798 
1799 #define DEFINE_GETFIELD(Return,Fieldname,Result \
1800   , EntryProbe, ReturnProbe) \
1801 \
1802   DT_RETURN_MARK_DECL_FOR(Result, Get##Result##Field, Return \
1803   , ReturnProbe); \
1804 \
1805 JNI_ENTRY_NO_PRESERVE(Return, jni_Get##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID)) \
1806 \
1807   EntryProbe; \
1808   Return ret = 0;\
1809   DT_RETURN_MARK_FOR(Result, Get##Result##Field, Return, (const Return&)ret);\
1810 \
1811   oop o = JNIHandles::resolve_non_null(obj); \
1812   Klass* k = o->klass(); \
1813   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
1814   /* Keep JVMTI addition small and only check enabled flag here.       */ \
1815   if (JvmtiExport::should_post_field_access()) { \
1816     o = JvmtiExport::jni_GetField_probe(thread, obj, o, k, fieldID, false); \
1817   } \
1818   ret = o->Fieldname##_field(offset); \
1819   return ret; \
1820 JNI_END
1821 
1822 DEFINE_GETFIELD(jboolean, bool,   Boolean
1823                 , HOTSPOT_JNI_GETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1824                 HOTSPOT_JNI_GETBOOLEANFIELD_RETURN(_ret_ref))
1825 DEFINE_GETFIELD(jbyte,    byte,   Byte
1826                 , HOTSPOT_JNI_GETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1827                 HOTSPOT_JNI_GETBYTEFIELD_RETURN(_ret_ref))
1828 DEFINE_GETFIELD(jchar,    char,   Char
1829                 , HOTSPOT_JNI_GETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1830                 HOTSPOT_JNI_GETCHARFIELD_RETURN(_ret_ref))
1831 DEFINE_GETFIELD(jshort,   short,  Short
1832                 , HOTSPOT_JNI_GETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1833                 HOTSPOT_JNI_GETSHORTFIELD_RETURN(_ret_ref))
1834 DEFINE_GETFIELD(jint,     int,    Int
1835                 , HOTSPOT_JNI_GETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1836                 HOTSPOT_JNI_GETINTFIELD_RETURN(_ret_ref))
1837 DEFINE_GETFIELD(jlong,    long,   Long
1838                 , HOTSPOT_JNI_GETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1839                 HOTSPOT_JNI_GETLONGFIELD_RETURN(_ret_ref))
1840 // Float and double probes don't return value because dtrace doesn't currently support it
1841 DEFINE_GETFIELD(jfloat,   float,  Float
1842                 , HOTSPOT_JNI_GETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1843                 HOTSPOT_JNI_GETFLOATFIELD_RETURN())
1844 DEFINE_GETFIELD(jdouble,  double, Double
1845                 , HOTSPOT_JNI_GETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1846                 HOTSPOT_JNI_GETDOUBLEFIELD_RETURN())
1847 
jni_GetBooleanField_addr()1848 address jni_GetBooleanField_addr() {
1849   return (address)jni_GetBooleanField;
1850 }
jni_GetByteField_addr()1851 address jni_GetByteField_addr() {
1852   return (address)jni_GetByteField;
1853 }
jni_GetCharField_addr()1854 address jni_GetCharField_addr() {
1855   return (address)jni_GetCharField;
1856 }
jni_GetShortField_addr()1857 address jni_GetShortField_addr() {
1858   return (address)jni_GetShortField;
1859 }
jni_GetIntField_addr()1860 address jni_GetIntField_addr() {
1861   return (address)jni_GetIntField;
1862 }
jni_GetLongField_addr()1863 address jni_GetLongField_addr() {
1864   return (address)jni_GetLongField;
1865 }
jni_GetFloatField_addr()1866 address jni_GetFloatField_addr() {
1867   return (address)jni_GetFloatField;
1868 }
jni_GetDoubleField_addr()1869 address jni_GetDoubleField_addr() {
1870   return (address)jni_GetDoubleField;
1871 }
1872 
1873 JNI_ENTRY_NO_PRESERVE(void, jni_SetObjectField(JNIEnv *env, jobject obj, jfieldID fieldID, jobject value))
1874   HOTSPOT_JNI_SETOBJECTFIELD_ENTRY(env, obj, (uintptr_t) fieldID, value);
1875   oop o = JNIHandles::resolve_non_null(obj);
1876   Klass* k = o->klass();
1877   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1878   // Keep JVMTI addition small and only check enabled flag here.
1879   if (JvmtiExport::should_post_field_modification()) {
1880     jvalue field_value;
1881     field_value.l = value;
1882     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, JVM_SIGNATURE_CLASS, (jvalue *)&field_value);
1883   }
1884   HeapAccess<ON_UNKNOWN_OOP_REF>::oop_store_at(o, offset, JNIHandles::resolve(value));
1885   HOTSPOT_JNI_SETOBJECTFIELD_RETURN();
1886 JNI_END
1887 
1888 
1889 #define DEFINE_SETFIELD(Argument,Fieldname,Result,SigType,unionType \
1890                         , EntryProbe, ReturnProbe) \
1891 \
1892 JNI_ENTRY_NO_PRESERVE(void, jni_Set##Result##Field(JNIEnv *env, jobject obj, jfieldID fieldID, Argument value)) \
1893 \
1894   EntryProbe; \
1895 \
1896   oop o = JNIHandles::resolve_non_null(obj); \
1897   Klass* k = o->klass(); \
1898   int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);  \
1899   /* Keep JVMTI addition small and only check enabled flag here.       */ \
1900   if (JvmtiExport::should_post_field_modification()) { \
1901     jvalue field_value; \
1902     field_value.unionType = value; \
1903     o = JvmtiExport::jni_SetField_probe(thread, obj, o, k, fieldID, false, SigType, (jvalue *)&field_value); \
1904   } \
1905   if (SigType == JVM_SIGNATURE_BOOLEAN) { value = ((jboolean)value) & 1; } \
1906   o->Fieldname##_field_put(offset, value); \
1907   ReturnProbe; \
1908 JNI_END
1909 
1910 DEFINE_SETFIELD(jboolean, bool,   Boolean, JVM_SIGNATURE_BOOLEAN, z
1911                 , HOTSPOT_JNI_SETBOOLEANFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1912                 HOTSPOT_JNI_SETBOOLEANFIELD_RETURN())
1913 DEFINE_SETFIELD(jbyte,    byte,   Byte,    JVM_SIGNATURE_BYTE, b
1914                 , HOTSPOT_JNI_SETBYTEFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1915                 HOTSPOT_JNI_SETBYTEFIELD_RETURN())
1916 DEFINE_SETFIELD(jchar,    char,   Char,    JVM_SIGNATURE_CHAR, c
1917                 , HOTSPOT_JNI_SETCHARFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1918                 HOTSPOT_JNI_SETCHARFIELD_RETURN())
1919 DEFINE_SETFIELD(jshort,   short,  Short,   JVM_SIGNATURE_SHORT, s
1920                 , HOTSPOT_JNI_SETSHORTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1921                 HOTSPOT_JNI_SETSHORTFIELD_RETURN())
1922 DEFINE_SETFIELD(jint,     int,    Int,     JVM_SIGNATURE_INT, i
1923                 , HOTSPOT_JNI_SETINTFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1924                 HOTSPOT_JNI_SETINTFIELD_RETURN())
1925 DEFINE_SETFIELD(jlong,    long,   Long,    JVM_SIGNATURE_LONG, j
1926                 , HOTSPOT_JNI_SETLONGFIELD_ENTRY(env, obj, (uintptr_t)fieldID, value),
1927                 HOTSPOT_JNI_SETLONGFIELD_RETURN())
1928 // Float and double probes don't return value because dtrace doesn't currently support it
1929 DEFINE_SETFIELD(jfloat,   float,  Float,   JVM_SIGNATURE_FLOAT, f
1930                 , HOTSPOT_JNI_SETFLOATFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1931                 HOTSPOT_JNI_SETFLOATFIELD_RETURN())
1932 DEFINE_SETFIELD(jdouble,  double, Double,  JVM_SIGNATURE_DOUBLE, d
1933                 , HOTSPOT_JNI_SETDOUBLEFIELD_ENTRY(env, obj, (uintptr_t)fieldID),
1934                 HOTSPOT_JNI_SETDOUBLEFIELD_RETURN())
1935 
1936 DT_RETURN_MARK_DECL(ToReflectedField, jobject
1937                     , HOTSPOT_JNI_TOREFLECTEDFIELD_RETURN(_ret_ref));
1938 
1939 JNI_ENTRY(jobject, jni_ToReflectedField(JNIEnv *env, jclass cls, jfieldID fieldID, jboolean isStatic))
1940   HOTSPOT_JNI_TOREFLECTEDFIELD_ENTRY(env, cls, (uintptr_t) fieldID, isStatic);
1941   jobject ret = NULL;
1942   DT_RETURN_MARK(ToReflectedField, jobject, (const jobject&)ret);
1943 
1944   fieldDescriptor fd;
1945   bool found = false;
1946   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1947 
1948   assert(jfieldIDWorkaround::is_static_jfieldID(fieldID) == (isStatic != 0), "invalid fieldID");
1949 
1950   if (isStatic) {
1951     // Static field. The fieldID a JNIid specifying the field holder and the offset within the Klass*.
1952     JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
1953     assert(id->is_static_field_id(), "invalid static field id");
1954     found = id->find_local_field(&fd);
1955   } else {
1956     // Non-static field. The fieldID is really the offset of the field within the instanceOop.
1957     int offset = jfieldIDWorkaround::from_instance_jfieldID(k, fieldID);
1958     found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, &fd);
1959   }
1960   assert(found, "bad fieldID passed into jni_ToReflectedField");
1961   oop reflected = Reflection::new_field(&fd, CHECK_NULL);
1962   ret = JNIHandles::make_local(THREAD, reflected);
1963   return ret;
1964 JNI_END
1965 
1966 
1967 //
1968 // Accessing Static Fields
1969 //
1970 DT_RETURN_MARK_DECL(GetStaticFieldID, jfieldID
1971                     , HOTSPOT_JNI_GETSTATICFIELDID_RETURN((uintptr_t)_ret_ref));
1972 
1973 JNI_ENTRY(jfieldID, jni_GetStaticFieldID(JNIEnv *env, jclass clazz,
1974           const char *name, const char *sig))
1975   HOTSPOT_JNI_GETSTATICFIELDID_ENTRY(env, clazz, (char *) name, (char *) sig);
1976   jfieldID ret = NULL;
1977   DT_RETURN_MARK(GetStaticFieldID, jfieldID, (const jfieldID&)ret);
1978 
1979   // The class should have been loaded (we have an instance of the class
1980   // passed in) so the field and signature should already be in the symbol
1981   // table.  If they're not there, the field doesn't exist.
1982   TempNewSymbol fieldname = SymbolTable::probe(name, (int)strlen(name));
1983   TempNewSymbol signame = SymbolTable::probe(sig, (int)strlen(sig));
1984   if (fieldname == NULL || signame == NULL) {
1985     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
1986   }
1987   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
1988   // Make sure class is initialized before handing id's out to static fields
1989   k->initialize(CHECK_NULL);
1990 
1991   fieldDescriptor fd;
1992   if (!k->is_instance_klass() ||
1993       !InstanceKlass::cast(k)->find_field(fieldname, signame, true, &fd)) {
1994     THROW_MSG_0(vmSymbols::java_lang_NoSuchFieldError(), (char*) name);
1995   }
1996 
1997   // A jfieldID for a static field is a JNIid specifying the field holder and the offset within the Klass*
1998   JNIid* id = fd.field_holder()->jni_id_for(fd.offset());
1999   debug_only(id->set_is_static_field_id();)
2000 
2001   debug_only(id->verify(fd.field_holder()));
2002 
2003   ret = jfieldIDWorkaround::to_static_jfieldID(id);
2004   return ret;
2005 JNI_END
2006 
2007 
2008 JNI_ENTRY(jobject, jni_GetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID))
2009   HOTSPOT_JNI_GETSTATICOBJECTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID);
2010 #if INCLUDE_JNI_CHECK
2011   DEBUG_ONLY(Klass* param_k = jniCheck::validate_class(thread, clazz);)
2012 #endif // INCLUDE_JNI_CHECK
2013   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2014   assert(id->is_static_field_id(), "invalid static field id");
2015   // Keep JVMTI addition small and only check enabled flag here.
2016   // jni_GetField_probe() assumes that is okay to create handles.
2017   if (JvmtiExport::should_post_field_access()) {
2018     JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true);
2019   }
2020   jobject ret = JNIHandles::make_local(THREAD, id->holder()->java_mirror()->obj_field(id->offset()));
2021   HOTSPOT_JNI_GETSTATICOBJECTFIELD_RETURN(ret);
2022   return ret;
2023 JNI_END
2024 
2025 
2026 #define DEFINE_GETSTATICFIELD(Return,Fieldname,Result \
2027                               , EntryProbe, ReturnProbe) \
2028 \
2029   DT_RETURN_MARK_DECL_FOR(Result, GetStatic##Result##Field, Return \
2030                           , ReturnProbe);                                          \
2031 \
2032 JNI_ENTRY(Return, jni_GetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID)) \
2033   EntryProbe; \
2034   Return ret = 0;\
2035   DT_RETURN_MARK_FOR(Result, GetStatic##Result##Field, Return, \
2036                      (const Return&)ret);\
2037   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
2038   assert(id->is_static_field_id(), "invalid static field id"); \
2039   /* Keep JVMTI addition small and only check enabled flag here. */ \
2040   /* jni_GetField_probe() assumes that is okay to create handles. */ \
2041   if (JvmtiExport::should_post_field_access()) { \
2042     JvmtiExport::jni_GetField_probe(thread, NULL, NULL, id->holder(), fieldID, true); \
2043   } \
2044   ret = id->holder()->java_mirror()-> Fieldname##_field (id->offset()); \
2045   return ret;\
2046 JNI_END
2047 
2048 DEFINE_GETSTATICFIELD(jboolean, bool,   Boolean
2049                       , HOTSPOT_JNI_GETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t) fieldID), HOTSPOT_JNI_GETSTATICBOOLEANFIELD_RETURN(_ret_ref))
2050 DEFINE_GETSTATICFIELD(jbyte,    byte,   Byte
2051                       , HOTSPOT_JNI_GETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICBYTEFIELD_RETURN(_ret_ref)   )
2052 DEFINE_GETSTATICFIELD(jchar,    char,   Char
2053                       , HOTSPOT_JNI_GETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICCHARFIELD_RETURN(_ret_ref)   )
2054 DEFINE_GETSTATICFIELD(jshort,   short,  Short
2055                       , HOTSPOT_JNI_GETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICSHORTFIELD_RETURN(_ret_ref)  )
2056 DEFINE_GETSTATICFIELD(jint,     int,    Int
2057                       , HOTSPOT_JNI_GETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),     HOTSPOT_JNI_GETSTATICINTFIELD_RETURN(_ret_ref)    )
2058 DEFINE_GETSTATICFIELD(jlong,    long,   Long
2059                       , HOTSPOT_JNI_GETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),    HOTSPOT_JNI_GETSTATICLONGFIELD_RETURN(_ret_ref)   )
2060 // Float and double probes don't return value because dtrace doesn't currently support it
2061 DEFINE_GETSTATICFIELD(jfloat,   float,  Float
2062                       , HOTSPOT_JNI_GETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),   HOTSPOT_JNI_GETSTATICFLOATFIELD_RETURN()          )
2063 DEFINE_GETSTATICFIELD(jdouble,  double, Double
2064                       , HOTSPOT_JNI_GETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),  HOTSPOT_JNI_GETSTATICDOUBLEFIELD_RETURN()         )
2065 
2066 JNI_ENTRY(void, jni_SetStaticObjectField(JNIEnv *env, jclass clazz, jfieldID fieldID, jobject value))
2067  HOTSPOT_JNI_SETSTATICOBJECTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value);
2068   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID);
2069   assert(id->is_static_field_id(), "invalid static field id");
2070   // Keep JVMTI addition small and only check enabled flag here.
2071   // jni_SetField_probe() assumes that is okay to create handles.
2072   if (JvmtiExport::should_post_field_modification()) {
2073     jvalue field_value;
2074     field_value.l = value;
2075     JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, JVM_SIGNATURE_CLASS, (jvalue *)&field_value);
2076   }
2077   id->holder()->java_mirror()->obj_field_put(id->offset(), JNIHandles::resolve(value));
2078   HOTSPOT_JNI_SETSTATICOBJECTFIELD_RETURN();
2079 JNI_END
2080 
2081 
2082 
2083 #define DEFINE_SETSTATICFIELD(Argument,Fieldname,Result,SigType,unionType \
2084                               , EntryProbe, ReturnProbe) \
2085 \
2086 JNI_ENTRY(void, jni_SetStatic##Result##Field(JNIEnv *env, jclass clazz, jfieldID fieldID, Argument value)) \
2087   EntryProbe; \
2088 \
2089   JNIid* id = jfieldIDWorkaround::from_static_jfieldID(fieldID); \
2090   assert(id->is_static_field_id(), "invalid static field id"); \
2091   /* Keep JVMTI addition small and only check enabled flag here. */ \
2092   /* jni_SetField_probe() assumes that is okay to create handles. */ \
2093   if (JvmtiExport::should_post_field_modification()) { \
2094     jvalue field_value; \
2095     field_value.unionType = value; \
2096     JvmtiExport::jni_SetField_probe(thread, NULL, NULL, id->holder(), fieldID, true, SigType, (jvalue *)&field_value); \
2097   } \
2098   if (SigType == JVM_SIGNATURE_BOOLEAN) { value = ((jboolean)value) & 1; } \
2099   id->holder()->java_mirror()-> Fieldname##_field_put (id->offset(), value); \
2100   ReturnProbe;\
2101 JNI_END
2102 
2103 DEFINE_SETSTATICFIELD(jboolean, bool,   Boolean, JVM_SIGNATURE_BOOLEAN, z
2104                       , HOTSPOT_JNI_SETSTATICBOOLEANFIELD_ENTRY(env, clazz, (uintptr_t)fieldID, value),
2105                       HOTSPOT_JNI_SETSTATICBOOLEANFIELD_RETURN())
2106 DEFINE_SETSTATICFIELD(jbyte,    byte,   Byte,    JVM_SIGNATURE_BYTE, b
2107                       , HOTSPOT_JNI_SETSTATICBYTEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2108                       HOTSPOT_JNI_SETSTATICBYTEFIELD_RETURN())
2109 DEFINE_SETSTATICFIELD(jchar,    char,   Char,    JVM_SIGNATURE_CHAR, c
2110                       , HOTSPOT_JNI_SETSTATICCHARFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2111                       HOTSPOT_JNI_SETSTATICCHARFIELD_RETURN())
2112 DEFINE_SETSTATICFIELD(jshort,   short,  Short,   JVM_SIGNATURE_SHORT, s
2113                       , HOTSPOT_JNI_SETSTATICSHORTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2114                       HOTSPOT_JNI_SETSTATICSHORTFIELD_RETURN())
2115 DEFINE_SETSTATICFIELD(jint,     int,    Int,     JVM_SIGNATURE_INT, i
2116                       , HOTSPOT_JNI_SETSTATICINTFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2117                       HOTSPOT_JNI_SETSTATICINTFIELD_RETURN())
2118 DEFINE_SETSTATICFIELD(jlong,    long,   Long,    JVM_SIGNATURE_LONG, j
2119                       , HOTSPOT_JNI_SETSTATICLONGFIELD_ENTRY(env, clazz, (uintptr_t) fieldID, value),
2120                       HOTSPOT_JNI_SETSTATICLONGFIELD_RETURN())
2121 // Float and double probes don't return value because dtrace doesn't currently support it
2122 DEFINE_SETSTATICFIELD(jfloat,   float,  Float,   JVM_SIGNATURE_FLOAT, f
2123                       , HOTSPOT_JNI_SETSTATICFLOATFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2124                       HOTSPOT_JNI_SETSTATICFLOATFIELD_RETURN())
2125 DEFINE_SETSTATICFIELD(jdouble,  double, Double,  JVM_SIGNATURE_DOUBLE, d
2126                       , HOTSPOT_JNI_SETSTATICDOUBLEFIELD_ENTRY(env, clazz, (uintptr_t) fieldID),
2127                       HOTSPOT_JNI_SETSTATICDOUBLEFIELD_RETURN())
2128 
2129 //
2130 // String Operations
2131 //
2132 
2133 // Unicode Interface
2134 
2135 DT_RETURN_MARK_DECL(NewString, jstring
2136                     , HOTSPOT_JNI_NEWSTRING_RETURN(_ret_ref));
2137 
2138 JNI_ENTRY(jstring, jni_NewString(JNIEnv *env, const jchar *unicodeChars, jsize len))
2139  HOTSPOT_JNI_NEWSTRING_ENTRY(env, (uint16_t *) unicodeChars, len);
2140   jstring ret = NULL;
2141   DT_RETURN_MARK(NewString, jstring, (const jstring&)ret);
2142   oop string=java_lang_String::create_oop_from_unicode((jchar*) unicodeChars, len, CHECK_NULL);
2143   ret = (jstring) JNIHandles::make_local(THREAD, string);
2144   return ret;
2145 JNI_END
2146 
2147 
2148 JNI_ENTRY_NO_PRESERVE(jsize, jni_GetStringLength(JNIEnv *env, jstring string))
2149   HOTSPOT_JNI_GETSTRINGLENGTH_ENTRY(env, string);
2150   jsize ret = 0;
2151   oop s = JNIHandles::resolve_non_null(string);
2152   ret = java_lang_String::length(s);
2153  HOTSPOT_JNI_GETSTRINGLENGTH_RETURN(ret);
2154   return ret;
2155 JNI_END
2156 
2157 
2158 JNI_ENTRY_NO_PRESERVE(const jchar*, jni_GetStringChars(
2159   JNIEnv *env, jstring string, jboolean *isCopy))
2160  HOTSPOT_JNI_GETSTRINGCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2161   jchar* buf = NULL;
2162   oop s = JNIHandles::resolve_non_null(string);
2163   typeArrayOop s_value = java_lang_String::value(s);
2164   if (s_value != NULL) {
2165     int s_len = java_lang_String::length(s, s_value);
2166     bool is_latin1 = java_lang_String::is_latin1(s);
2167     buf = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
2168     /* JNI Specification states return NULL on OOM */
2169     if (buf != NULL) {
2170       if (s_len > 0) {
2171         if (!is_latin1) {
2172           ArrayAccess<>::arraycopy_to_native(s_value, (size_t) typeArrayOopDesc::element_offset<jchar>(0),
2173                                              buf, s_len);
2174         } else {
2175           for (int i = 0; i < s_len; i++) {
2176             buf[i] = ((jchar) s_value->byte_at(i)) & 0xff;
2177           }
2178         }
2179       }
2180       buf[s_len] = 0;
2181       //%note jni_5
2182       if (isCopy != NULL) {
2183         *isCopy = JNI_TRUE;
2184       }
2185     }
2186   }
2187   HOTSPOT_JNI_GETSTRINGCHARS_RETURN(buf);
2188   return buf;
2189 JNI_END
2190 
2191 
2192 JNI_ENTRY_NO_PRESERVE(void, jni_ReleaseStringChars(JNIEnv *env, jstring str, const jchar *chars))
2193   HOTSPOT_JNI_RELEASESTRINGCHARS_ENTRY(env, str, (uint16_t *) chars);
2194   //%note jni_6
2195   if (chars != NULL) {
2196     // Since String objects are supposed to be immutable, don't copy any
2197     // new data back.  A bad user will have to go after the char array.
2198     FreeHeap((void*) chars);
2199   }
2200   HOTSPOT_JNI_RELEASESTRINGCHARS_RETURN();
2201 JNI_END
2202 
2203 
2204 // UTF Interface
2205 
2206 DT_RETURN_MARK_DECL(NewStringUTF, jstring
2207                     , HOTSPOT_JNI_NEWSTRINGUTF_RETURN(_ret_ref));
2208 
2209 JNI_ENTRY(jstring, jni_NewStringUTF(JNIEnv *env, const char *bytes))
2210   HOTSPOT_JNI_NEWSTRINGUTF_ENTRY(env, (char *) bytes);
2211   jstring ret;
2212   DT_RETURN_MARK(NewStringUTF, jstring, (const jstring&)ret);
2213 
2214   oop result = java_lang_String::create_oop_from_str((char*) bytes, CHECK_NULL);
2215   ret = (jstring) JNIHandles::make_local(THREAD, result);
2216   return ret;
2217 JNI_END
2218 
2219 
2220 JNI_ENTRY(jsize, jni_GetStringUTFLength(JNIEnv *env, jstring string))
2221  HOTSPOT_JNI_GETSTRINGUTFLENGTH_ENTRY(env, string);
2222   oop java_string = JNIHandles::resolve_non_null(string);
2223   jsize ret = java_lang_String::utf8_length(java_string);
2224   HOTSPOT_JNI_GETSTRINGUTFLENGTH_RETURN(ret);
2225   return ret;
2226 JNI_END
2227 
2228 
2229 JNI_ENTRY(const char*, jni_GetStringUTFChars(JNIEnv *env, jstring string, jboolean *isCopy))
2230  HOTSPOT_JNI_GETSTRINGUTFCHARS_ENTRY(env, string, (uintptr_t *) isCopy);
2231   char* result = NULL;
2232   oop java_string = JNIHandles::resolve_non_null(string);
2233   typeArrayOop s_value = java_lang_String::value(java_string);
2234   if (s_value != NULL) {
2235     size_t length = java_lang_String::utf8_length(java_string, s_value);
2236     /* JNI Specification states return NULL on OOM */
2237     result = AllocateHeap(length + 1, mtInternal, 0, AllocFailStrategy::RETURN_NULL);
2238     if (result != NULL) {
2239       java_lang_String::as_utf8_string(java_string, s_value, result, (int) length + 1);
2240       if (isCopy != NULL) {
2241         *isCopy = JNI_TRUE;
2242       }
2243     }
2244   }
2245  HOTSPOT_JNI_GETSTRINGUTFCHARS_RETURN(result);
2246   return result;
2247 JNI_END
2248 
2249 
2250 JNI_LEAF(void, jni_ReleaseStringUTFChars(JNIEnv *env, jstring str, const char *chars))
2251  HOTSPOT_JNI_RELEASESTRINGUTFCHARS_ENTRY(env, str, (char *) chars);
2252   if (chars != NULL) {
2253     FreeHeap((char*) chars);
2254   }
2255 HOTSPOT_JNI_RELEASESTRINGUTFCHARS_RETURN();
2256 JNI_END
2257 
2258 
2259 JNI_ENTRY_NO_PRESERVE(jsize, jni_GetArrayLength(JNIEnv *env, jarray array))
2260  HOTSPOT_JNI_GETARRAYLENGTH_ENTRY(env, array);
2261   arrayOop a = arrayOop(JNIHandles::resolve_non_null(array));
2262   assert(a->is_array(), "must be array");
2263   jsize ret = a->length();
2264  HOTSPOT_JNI_GETARRAYLENGTH_RETURN(ret);
2265   return ret;
2266 JNI_END
2267 
2268 
2269 //
2270 // Object Array Operations
2271 //
2272 
2273 DT_RETURN_MARK_DECL(NewObjectArray, jobjectArray
2274                     , HOTSPOT_JNI_NEWOBJECTARRAY_RETURN(_ret_ref));
2275 
2276 JNI_ENTRY(jobjectArray, jni_NewObjectArray(JNIEnv *env, jsize length, jclass elementClass, jobject initialElement))
2277  HOTSPOT_JNI_NEWOBJECTARRAY_ENTRY(env, length, elementClass, initialElement);
2278   jobjectArray ret = NULL;
2279   DT_RETURN_MARK(NewObjectArray, jobjectArray, (const jobjectArray&)ret);
2280   Klass* ek = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(elementClass));
2281   Klass* ak = ek->array_klass(CHECK_NULL);
2282   ObjArrayKlass::cast(ak)->initialize(CHECK_NULL);
2283   objArrayOop result = ObjArrayKlass::cast(ak)->allocate(length, CHECK_NULL);
2284   oop initial_value = JNIHandles::resolve(initialElement);
2285   if (initial_value != NULL) {  // array already initialized with NULL
2286     for (int index = 0; index < length; index++) {
2287       result->obj_at_put(index, initial_value);
2288     }
2289   }
2290   ret = (jobjectArray) JNIHandles::make_local(THREAD, result);
2291   return ret;
2292 JNI_END
2293 
2294 DT_RETURN_MARK_DECL(GetObjectArrayElement, jobject
2295                     , HOTSPOT_JNI_GETOBJECTARRAYELEMENT_RETURN(_ret_ref));
2296 
2297 JNI_ENTRY(jobject, jni_GetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index))
2298  HOTSPOT_JNI_GETOBJECTARRAYELEMENT_ENTRY(env, array, index);
2299   jobject ret = NULL;
2300   DT_RETURN_MARK(GetObjectArrayElement, jobject, (const jobject&)ret);
2301   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2302   if (a->is_within_bounds(index)) {
2303     ret = JNIHandles::make_local(THREAD, a->obj_at(index));
2304     return ret;
2305   } else {
2306     ResourceMark rm(THREAD);
2307     stringStream ss;
2308     ss.print("Index %d out of bounds for length %d", index, a->length());
2309     THROW_MSG_0(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2310   }
2311 JNI_END
2312 
2313 DT_VOID_RETURN_MARK_DECL(SetObjectArrayElement
2314                          , HOTSPOT_JNI_SETOBJECTARRAYELEMENT_RETURN());
2315 
2316 JNI_ENTRY(void, jni_SetObjectArrayElement(JNIEnv *env, jobjectArray array, jsize index, jobject value))
2317  HOTSPOT_JNI_SETOBJECTARRAYELEMENT_ENTRY(env, array, index, value);
2318   DT_VOID_RETURN_MARK(SetObjectArrayElement);
2319 
2320   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(array));
2321   oop v = JNIHandles::resolve(value);
2322   if (a->is_within_bounds(index)) {
2323     if (v == NULL || v->is_a(ObjArrayKlass::cast(a->klass())->element_klass())) {
2324       a->obj_at_put(index, v);
2325     } else {
2326       ResourceMark rm(THREAD);
2327       stringStream ss;
2328       Klass *bottom_kl = ObjArrayKlass::cast(a->klass())->bottom_klass();
2329       ss.print("type mismatch: can not store %s to %s[%d]",
2330                v->klass()->external_name(),
2331                bottom_kl->is_typeArray_klass() ? type2name_tab[ArrayKlass::cast(bottom_kl)->element_type()] : bottom_kl->external_name(),
2332                index);
2333       for (int dims = ArrayKlass::cast(a->klass())->dimension(); dims > 1; --dims) {
2334         ss.print("[]");
2335       }
2336       THROW_MSG(vmSymbols::java_lang_ArrayStoreException(), ss.as_string());
2337     }
2338   } else {
2339     ResourceMark rm(THREAD);
2340     stringStream ss;
2341     ss.print("Index %d out of bounds for length %d", index, a->length());
2342     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2343   }
2344 JNI_END
2345 
2346 
2347 
2348 #define DEFINE_NEWSCALARARRAY(Return,Allocator,Result \
2349                               ,EntryProbe,ReturnProbe)  \
2350 \
2351   DT_RETURN_MARK_DECL(New##Result##Array, Return \
2352                       , ReturnProbe); \
2353 \
2354 JNI_ENTRY(Return, \
2355           jni_New##Result##Array(JNIEnv *env, jsize len)) \
2356   EntryProbe; \
2357   Return ret = NULL;\
2358   DT_RETURN_MARK(New##Result##Array, Return, (const Return&)ret);\
2359 \
2360   oop obj= oopFactory::Allocator(len, CHECK_NULL); \
2361   ret = (Return) JNIHandles::make_local(THREAD, obj); \
2362   return ret;\
2363 JNI_END
2364 
DEFINE_NEWSCALARARRAY(jbooleanArray,new_boolArray,Boolean,HOTSPOT_JNI_NEWBOOLEANARRAY_ENTRY (env,len),HOTSPOT_JNI_NEWBOOLEANARRAY_RETURN (_ret_ref))2365 DEFINE_NEWSCALARARRAY(jbooleanArray, new_boolArray,   Boolean,
2366                       HOTSPOT_JNI_NEWBOOLEANARRAY_ENTRY(env, len),
2367                       HOTSPOT_JNI_NEWBOOLEANARRAY_RETURN(_ret_ref))
2368 DEFINE_NEWSCALARARRAY(jbyteArray,    new_byteArray,   Byte,
2369                       HOTSPOT_JNI_NEWBYTEARRAY_ENTRY(env, len),
2370                       HOTSPOT_JNI_NEWBYTEARRAY_RETURN(_ret_ref))
2371 DEFINE_NEWSCALARARRAY(jshortArray,   new_shortArray,  Short,
2372                       HOTSPOT_JNI_NEWSHORTARRAY_ENTRY(env, len),
2373                       HOTSPOT_JNI_NEWSHORTARRAY_RETURN(_ret_ref))
2374 DEFINE_NEWSCALARARRAY(jcharArray,    new_charArray,   Char,
2375                       HOTSPOT_JNI_NEWCHARARRAY_ENTRY(env, len),
2376                       HOTSPOT_JNI_NEWCHARARRAY_RETURN(_ret_ref))
2377 DEFINE_NEWSCALARARRAY(jintArray,     new_intArray,    Int,
2378                       HOTSPOT_JNI_NEWINTARRAY_ENTRY(env, len),
2379                       HOTSPOT_JNI_NEWINTARRAY_RETURN(_ret_ref))
2380 DEFINE_NEWSCALARARRAY(jlongArray,    new_longArray,   Long,
2381                       HOTSPOT_JNI_NEWLONGARRAY_ENTRY(env, len),
2382                       HOTSPOT_JNI_NEWLONGARRAY_RETURN(_ret_ref))
2383 DEFINE_NEWSCALARARRAY(jfloatArray,   new_floatArray,  Float,
2384                       HOTSPOT_JNI_NEWFLOATARRAY_ENTRY(env, len),
2385                       HOTSPOT_JNI_NEWFLOATARRAY_RETURN(_ret_ref))
2386 DEFINE_NEWSCALARARRAY(jdoubleArray,  new_doubleArray, Double,
2387                       HOTSPOT_JNI_NEWDOUBLEARRAY_ENTRY(env, len),
2388                       HOTSPOT_JNI_NEWDOUBLEARRAY_RETURN(_ret_ref))
2389 
2390 // Return an address which will fault if the caller writes to it.
2391 
2392 static char* get_bad_address() {
2393   static char* bad_address = NULL;
2394   if (bad_address == NULL) {
2395     size_t size = os::vm_allocation_granularity();
2396     bad_address = os::reserve_memory(size);
2397     if (bad_address != NULL) {
2398       os::protect_memory(bad_address, size, os::MEM_PROT_READ,
2399                          /*is_committed*/false);
2400       MemTracker::record_virtual_memory_type((void*)bad_address, mtInternal);
2401     }
2402   }
2403   return bad_address;
2404 }
2405 
2406 
2407 
2408 #define DEFINE_GETSCALARARRAYELEMENTS(ElementTag,ElementType,Result, Tag \
2409                                       , EntryProbe, ReturnProbe) \
2410 \
2411 JNI_ENTRY_NO_PRESERVE(ElementType*, \
2412           jni_Get##Result##ArrayElements(JNIEnv *env, ElementType##Array array, jboolean *isCopy)) \
2413   EntryProbe; \
2414   /* allocate an chunk of memory in c land */ \
2415   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2416   ElementType* result; \
2417   int len = a->length(); \
2418   if (len == 0) { \
2419     if (isCopy != NULL) { \
2420       *isCopy = JNI_FALSE; \
2421     } \
2422     /* Empty array: legal but useless, can't return NULL. \
2423      * Return a pointer to something useless. \
2424      * Avoid asserts in typeArrayOop. */ \
2425     result = (ElementType*)get_bad_address(); \
2426   } else { \
2427     /* JNI Specification states return NULL on OOM */                    \
2428     result = NEW_C_HEAP_ARRAY_RETURN_NULL(ElementType, len, mtInternal); \
2429     if (result != NULL) {                                                \
2430       /* copy the array to the c chunk */                                \
2431       ArrayAccess<>::arraycopy_to_native(a, typeArrayOopDesc::element_offset<ElementType>(0), \
2432                                          result, len);                   \
2433       if (isCopy) {                                                      \
2434         *isCopy = JNI_TRUE;                                              \
2435       }                                                                  \
2436     }                                                                    \
2437   } \
2438   ReturnProbe; \
2439   return result; \
2440 JNI_END
2441 
2442 DEFINE_GETSCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2443                               , HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2444                               HOTSPOT_JNI_GETBOOLEANARRAYELEMENTS_RETURN((uintptr_t*)result))
2445 DEFINE_GETSCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2446                               , HOTSPOT_JNI_GETBYTEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2447                               HOTSPOT_JNI_GETBYTEARRAYELEMENTS_RETURN((char*)result))
2448 DEFINE_GETSCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2449                               , HOTSPOT_JNI_GETSHORTARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2450                               HOTSPOT_JNI_GETSHORTARRAYELEMENTS_RETURN((uint16_t*)result))
2451 DEFINE_GETSCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2452                               , HOTSPOT_JNI_GETCHARARRAYELEMENTS_ENTRY(env, (uint16_t*) array, (uintptr_t *) isCopy),
2453                               HOTSPOT_JNI_GETCHARARRAYELEMENTS_RETURN(result))
2454 DEFINE_GETSCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2455                               , HOTSPOT_JNI_GETINTARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2456                               HOTSPOT_JNI_GETINTARRAYELEMENTS_RETURN((uint32_t*)result))
2457 DEFINE_GETSCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2458                               , HOTSPOT_JNI_GETLONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2459                               HOTSPOT_JNI_GETLONGARRAYELEMENTS_RETURN(((uintptr_t*)result)))
2460 // Float and double probes don't return value because dtrace doesn't currently support it
2461 DEFINE_GETSCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2462                               , HOTSPOT_JNI_GETFLOATARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2463                               HOTSPOT_JNI_GETFLOATARRAYELEMENTS_RETURN(result))
2464 DEFINE_GETSCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2465                               , HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) isCopy),
2466                               HOTSPOT_JNI_GETDOUBLEARRAYELEMENTS_RETURN(result))
2467 
2468 
2469 #define DEFINE_RELEASESCALARARRAYELEMENTS(ElementTag,ElementType,Result,Tag \
2470                                           , EntryProbe, ReturnProbe);\
2471 \
2472 JNI_ENTRY_NO_PRESERVE(void, \
2473           jni_Release##Result##ArrayElements(JNIEnv *env, ElementType##Array array, \
2474                                              ElementType *buf, jint mode)) \
2475   EntryProbe; \
2476   typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2477   int len = a->length(); \
2478   if (len != 0) {   /* Empty array:  nothing to free or copy. */  \
2479     if ((mode == 0) || (mode == JNI_COMMIT)) { \
2480       ArrayAccess<>::arraycopy_from_native(buf, a, typeArrayOopDesc::element_offset<ElementType>(0), len); \
2481     } \
2482     if ((mode == 0) || (mode == JNI_ABORT)) { \
2483       FreeHeap(buf); \
2484     } \
2485   } \
2486   ReturnProbe; \
2487 JNI_END
2488 
2489 DEFINE_RELEASESCALARARRAYELEMENTS(T_BOOLEAN, jboolean, Boolean, bool
2490                                   , HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2491                                   HOTSPOT_JNI_RELEASEBOOLEANARRAYELEMENTS_RETURN())
2492 DEFINE_RELEASESCALARARRAYELEMENTS(T_BYTE,    jbyte,    Byte,    byte
2493                                   , HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_ENTRY(env, array, (char *) buf, mode),
2494                                   HOTSPOT_JNI_RELEASEBYTEARRAYELEMENTS_RETURN())
2495 DEFINE_RELEASESCALARARRAYELEMENTS(T_SHORT,   jshort,   Short,   short
2496                                   ,  HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2497                                   HOTSPOT_JNI_RELEASESHORTARRAYELEMENTS_RETURN())
2498 DEFINE_RELEASESCALARARRAYELEMENTS(T_CHAR,    jchar,    Char,    char
2499                                   ,  HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_ENTRY(env, array, (uint16_t *) buf, mode),
2500                                   HOTSPOT_JNI_RELEASECHARARRAYELEMENTS_RETURN())
2501 DEFINE_RELEASESCALARARRAYELEMENTS(T_INT,     jint,     Int,     int
2502                                   , HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_ENTRY(env, array, (uint32_t *) buf, mode),
2503                                   HOTSPOT_JNI_RELEASEINTARRAYELEMENTS_RETURN())
2504 DEFINE_RELEASESCALARARRAYELEMENTS(T_LONG,    jlong,    Long,    long
2505                                   , HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_ENTRY(env, array, (uintptr_t *) buf, mode),
2506                                   HOTSPOT_JNI_RELEASELONGARRAYELEMENTS_RETURN())
2507 DEFINE_RELEASESCALARARRAYELEMENTS(T_FLOAT,   jfloat,   Float,   float
2508                                   , HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_ENTRY(env, array, (float *) buf, mode),
2509                                   HOTSPOT_JNI_RELEASEFLOATARRAYELEMENTS_RETURN())
2510 DEFINE_RELEASESCALARARRAYELEMENTS(T_DOUBLE,  jdouble,  Double,  double
2511                                   , HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_ENTRY(env, array, (double *) buf, mode),
2512                                   HOTSPOT_JNI_RELEASEDOUBLEARRAYELEMENTS_RETURN())
2513 
check_bounds(jsize start,jsize copy_len,jsize array_len,TRAPS)2514 static void check_bounds(jsize start, jsize copy_len, jsize array_len, TRAPS) {
2515   ResourceMark rm(THREAD);
2516   if (copy_len < 0) {
2517     stringStream ss;
2518     ss.print("Length %d is negative", copy_len);
2519     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2520   } else if (start < 0 || (start > array_len - copy_len)) {
2521     stringStream ss;
2522     ss.print("Array region %d.." INT64_FORMAT " out of bounds for length %d",
2523              start, (int64_t)start+(int64_t)copy_len, array_len);
2524     THROW_MSG(vmSymbols::java_lang_ArrayIndexOutOfBoundsException(), ss.as_string());
2525   }
2526 }
2527 
2528 #define DEFINE_GETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2529                                     , EntryProbe, ReturnProbe); \
2530   DT_VOID_RETURN_MARK_DECL(Get##Result##ArrayRegion \
2531                            , ReturnProbe); \
2532 \
2533 JNI_ENTRY(void, \
2534 jni_Get##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2535              jsize len, ElementType *buf)) \
2536   EntryProbe; \
2537   DT_VOID_RETURN_MARK(Get##Result##ArrayRegion); \
2538   typeArrayOop src = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2539   check_bounds(start, len, src->length(), CHECK); \
2540   if (len > 0) {    \
2541     ArrayAccess<>::arraycopy_to_native(src, typeArrayOopDesc::element_offset<ElementType>(start), buf, len); \
2542   } \
2543 JNI_END
2544 
2545 DEFINE_GETSCALARARRAYREGION(T_BOOLEAN, jboolean,Boolean, bool
2546                             , HOTSPOT_JNI_GETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2547                             HOTSPOT_JNI_GETBOOLEANARRAYREGION_RETURN());
2548 DEFINE_GETSCALARARRAYREGION(T_BYTE,    jbyte,   Byte,    byte
2549                             ,  HOTSPOT_JNI_GETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2550                             HOTSPOT_JNI_GETBYTEARRAYREGION_RETURN());
2551 DEFINE_GETSCALARARRAYREGION(T_SHORT,   jshort,  Short,   short
2552                             , HOTSPOT_JNI_GETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2553                             HOTSPOT_JNI_GETSHORTARRAYREGION_RETURN());
2554 DEFINE_GETSCALARARRAYREGION(T_CHAR,    jchar,   Char,    char
2555                             ,  HOTSPOT_JNI_GETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t*) buf),
2556                             HOTSPOT_JNI_GETCHARARRAYREGION_RETURN());
2557 DEFINE_GETSCALARARRAYREGION(T_INT,     jint,    Int,     int
2558                             , HOTSPOT_JNI_GETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t*) buf),
2559                             HOTSPOT_JNI_GETINTARRAYREGION_RETURN());
2560 DEFINE_GETSCALARARRAYREGION(T_LONG,    jlong,   Long,    long
2561                             ,  HOTSPOT_JNI_GETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2562                             HOTSPOT_JNI_GETLONGARRAYREGION_RETURN());
2563 DEFINE_GETSCALARARRAYREGION(T_FLOAT,   jfloat,  Float,   float
2564                             , HOTSPOT_JNI_GETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2565                             HOTSPOT_JNI_GETFLOATARRAYREGION_RETURN());
2566 DEFINE_GETSCALARARRAYREGION(T_DOUBLE,  jdouble, Double,  double
2567                             , HOTSPOT_JNI_GETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2568                             HOTSPOT_JNI_GETDOUBLEARRAYREGION_RETURN());
2569 
2570 
2571 #define DEFINE_SETSCALARARRAYREGION(ElementTag,ElementType,Result, Tag \
2572                                     , EntryProbe, ReturnProbe); \
2573   DT_VOID_RETURN_MARK_DECL(Set##Result##ArrayRegion \
2574                            ,ReturnProbe);           \
2575 \
2576 JNI_ENTRY(void, \
2577 jni_Set##Result##ArrayRegion(JNIEnv *env, ElementType##Array array, jsize start, \
2578              jsize len, const ElementType *buf)) \
2579   EntryProbe; \
2580   DT_VOID_RETURN_MARK(Set##Result##ArrayRegion); \
2581   typeArrayOop dst = typeArrayOop(JNIHandles::resolve_non_null(array)); \
2582   check_bounds(start, len, dst->length(), CHECK); \
2583   if (len > 0) { \
2584     ArrayAccess<>::arraycopy_from_native(buf, dst, typeArrayOopDesc::element_offset<ElementType>(start), len); \
2585   } \
2586 JNI_END
2587 
2588 DEFINE_SETSCALARARRAYREGION(T_BOOLEAN, jboolean, Boolean, bool
2589                             , HOTSPOT_JNI_SETBOOLEANARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *)buf),
2590                             HOTSPOT_JNI_SETBOOLEANARRAYREGION_RETURN())
2591 DEFINE_SETSCALARARRAYREGION(T_BYTE,    jbyte,    Byte,    byte
2592                             , HOTSPOT_JNI_SETBYTEARRAYREGION_ENTRY(env, array, start, len, (char *) buf),
2593                             HOTSPOT_JNI_SETBYTEARRAYREGION_RETURN())
2594 DEFINE_SETSCALARARRAYREGION(T_SHORT,   jshort,   Short,   short
2595                             , HOTSPOT_JNI_SETSHORTARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2596                             HOTSPOT_JNI_SETSHORTARRAYREGION_RETURN())
2597 DEFINE_SETSCALARARRAYREGION(T_CHAR,    jchar,    Char,    char
2598                             , HOTSPOT_JNI_SETCHARARRAYREGION_ENTRY(env, array, start, len, (uint16_t *) buf),
2599                             HOTSPOT_JNI_SETCHARARRAYREGION_RETURN())
2600 DEFINE_SETSCALARARRAYREGION(T_INT,     jint,     Int,     int
2601                             , HOTSPOT_JNI_SETINTARRAYREGION_ENTRY(env, array, start, len, (uint32_t *) buf),
2602                             HOTSPOT_JNI_SETINTARRAYREGION_RETURN())
2603 DEFINE_SETSCALARARRAYREGION(T_LONG,    jlong,    Long,    long
2604                             , HOTSPOT_JNI_SETLONGARRAYREGION_ENTRY(env, array, start, len, (uintptr_t *) buf),
2605                             HOTSPOT_JNI_SETLONGARRAYREGION_RETURN())
2606 DEFINE_SETSCALARARRAYREGION(T_FLOAT,   jfloat,   Float,   float
2607                             , HOTSPOT_JNI_SETFLOATARRAYREGION_ENTRY(env, array, start, len, (float *) buf),
2608                             HOTSPOT_JNI_SETFLOATARRAYREGION_RETURN())
2609 DEFINE_SETSCALARARRAYREGION(T_DOUBLE,  jdouble,  Double,  double
2610                             , HOTSPOT_JNI_SETDOUBLEARRAYREGION_ENTRY(env, array, start, len, (double *) buf),
2611                             HOTSPOT_JNI_SETDOUBLEARRAYREGION_RETURN())
2612 
2613 
2614 DT_RETURN_MARK_DECL(RegisterNatives, jint
2615                     , HOTSPOT_JNI_REGISTERNATIVES_RETURN(_ret_ref));
2616 
2617 JNI_ENTRY(jint, jni_RegisterNatives(JNIEnv *env, jclass clazz,
2618                                     const JNINativeMethod *methods,
2619                                     jint nMethods))
2620   HOTSPOT_JNI_REGISTERNATIVES_ENTRY(env, clazz, (void *) methods, nMethods);
2621   jint ret = 0;
2622   DT_RETURN_MARK(RegisterNatives, jint, (const jint&)ret);
2623 
2624   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2625 
2626   // There are no restrictions on native code registering native methods,
2627   // which allows agents to redefine the bindings to native methods, however
2628   // we issue a warning if any code running outside of the boot/platform
2629   // loader is rebinding any native methods in classes loaded by the
2630   // boot/platform loader that are in named modules. That will catch changes
2631   // to platform classes while excluding classes added to the bootclasspath.
2632   bool do_warning = false;
2633 
2634   // Only instanceKlasses can have native methods
2635   if (k->is_instance_klass()) {
2636     oop cl = k->class_loader();
2637     InstanceKlass* ik = InstanceKlass::cast(k);
2638     // Check for a platform class
2639     if ((cl ==  NULL || SystemDictionary::is_platform_class_loader(cl)) &&
2640         ik->module()->is_named()) {
2641       Klass* caller = thread->security_get_caller_class(1);
2642       // If no caller class, or caller class has a different loader, then
2643       // issue a warning below.
2644       do_warning = (caller == NULL) || caller->class_loader() != cl;
2645     }
2646   }
2647 
2648 
2649   for (int index = 0; index < nMethods; index++) {
2650     const char* meth_name = methods[index].name;
2651     const char* meth_sig = methods[index].signature;
2652     int meth_name_len = (int)strlen(meth_name);
2653 
2654     // The class should have been loaded (we have an instance of the class
2655     // passed in) so the method and signature should already be in the symbol
2656     // table.  If they're not there, the method doesn't exist.
2657     TempNewSymbol  name = SymbolTable::probe(meth_name, meth_name_len);
2658     TempNewSymbol  signature = SymbolTable::probe(meth_sig, (int)strlen(meth_sig));
2659 
2660     if (name == NULL || signature == NULL) {
2661       ResourceMark rm(THREAD);
2662       stringStream st;
2663       st.print("Method %s.%s%s not found", k->external_name(), meth_name, meth_sig);
2664       // Must return negative value on failure
2665       THROW_MSG_(vmSymbols::java_lang_NoSuchMethodError(), st.as_string(), -1);
2666     }
2667 
2668     if (do_warning) {
2669       ResourceMark rm(THREAD);
2670       log_warning(jni, resolve)("Re-registering of platform native method: %s.%s%s "
2671               "from code in a different classloader", k->external_name(), meth_name, meth_sig);
2672     }
2673 
2674     bool res = Method::register_native(k, name, signature,
2675                                        (address) methods[index].fnPtr, THREAD);
2676     if (!res) {
2677       ret = -1;
2678       break;
2679     }
2680   }
2681   return ret;
2682 JNI_END
2683 
2684 
2685 JNI_ENTRY(jint, jni_UnregisterNatives(JNIEnv *env, jclass clazz))
2686  HOTSPOT_JNI_UNREGISTERNATIVES_ENTRY(env, clazz);
2687   Klass* k   = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz));
2688   //%note jni_2
2689   if (k->is_instance_klass()) {
2690     for (int index = 0; index < InstanceKlass::cast(k)->methods()->length(); index++) {
2691       Method* m = InstanceKlass::cast(k)->methods()->at(index);
2692       if (m->is_native()) {
2693         m->clear_native_function();
2694         m->set_signature_handler(NULL);
2695       }
2696     }
2697   }
2698  HOTSPOT_JNI_UNREGISTERNATIVES_RETURN(0);
2699   return 0;
2700 JNI_END
2701 
2702 //
2703 // Monitor functions
2704 //
2705 
2706 DT_RETURN_MARK_DECL(MonitorEnter, jint
2707                     , HOTSPOT_JNI_MONITORENTER_RETURN(_ret_ref));
2708 
2709 JNI_ENTRY(jint, jni_MonitorEnter(JNIEnv *env, jobject jobj))
2710  HOTSPOT_JNI_MONITORENTER_ENTRY(env, jobj);
2711   jint ret = JNI_ERR;
2712   DT_RETURN_MARK(MonitorEnter, jint, (const jint&)ret);
2713 
2714   // If the object is null, we can't do anything with it
2715   if (jobj == NULL) {
2716     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
2717   }
2718 
2719   Handle obj(thread, JNIHandles::resolve_non_null(jobj));
2720   ObjectSynchronizer::jni_enter(obj, thread);
2721   ret = JNI_OK;
2722   return ret;
2723 JNI_END
2724 
2725 DT_RETURN_MARK_DECL(MonitorExit, jint
2726                     , HOTSPOT_JNI_MONITOREXIT_RETURN(_ret_ref));
2727 
2728 JNI_ENTRY(jint, jni_MonitorExit(JNIEnv *env, jobject jobj))
2729  HOTSPOT_JNI_MONITOREXIT_ENTRY(env, jobj);
2730   jint ret = JNI_ERR;
2731   DT_RETURN_MARK(MonitorExit, jint, (const jint&)ret);
2732 
2733   // Don't do anything with a null object
2734   if (jobj == NULL) {
2735     THROW_(vmSymbols::java_lang_NullPointerException(), JNI_ERR);
2736   }
2737 
2738   Handle obj(THREAD, JNIHandles::resolve_non_null(jobj));
2739   ObjectSynchronizer::jni_exit(obj(), CHECK_(JNI_ERR));
2740 
2741   ret = JNI_OK;
2742   return ret;
2743 JNI_END
2744 
2745 //
2746 // Extensions
2747 //
2748 
2749 DT_VOID_RETURN_MARK_DECL(GetStringRegion
2750                          , HOTSPOT_JNI_GETSTRINGREGION_RETURN());
2751 
2752 JNI_ENTRY(void, jni_GetStringRegion(JNIEnv *env, jstring string, jsize start, jsize len, jchar *buf))
2753  HOTSPOT_JNI_GETSTRINGREGION_ENTRY(env, string, start, len, buf);
2754   DT_VOID_RETURN_MARK(GetStringRegion);
2755   oop s = JNIHandles::resolve_non_null(string);
2756   typeArrayOop s_value = java_lang_String::value(s);
2757   int s_len = java_lang_String::length(s, s_value);
2758   if (start < 0 || len < 0 || start > s_len - len) {
2759     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
2760   } else {
2761     if (len > 0) {
2762       bool is_latin1 = java_lang_String::is_latin1(s);
2763       if (!is_latin1) {
2764         ArrayAccess<>::arraycopy_to_native(s_value, typeArrayOopDesc::element_offset<jchar>(start),
2765                                            buf, len);
2766       } else {
2767         for (int i = 0; i < len; i++) {
2768           buf[i] = ((jchar) s_value->byte_at(i + start)) & 0xff;
2769         }
2770       }
2771     }
2772   }
2773 JNI_END
2774 
2775 DT_VOID_RETURN_MARK_DECL(GetStringUTFRegion
2776                          , HOTSPOT_JNI_GETSTRINGUTFREGION_RETURN());
2777 
2778 JNI_ENTRY(void, jni_GetStringUTFRegion(JNIEnv *env, jstring string, jsize start, jsize len, char *buf))
2779  HOTSPOT_JNI_GETSTRINGUTFREGION_ENTRY(env, string, start, len, buf);
2780   DT_VOID_RETURN_MARK(GetStringUTFRegion);
2781   oop s = JNIHandles::resolve_non_null(string);
2782   typeArrayOop s_value = java_lang_String::value(s);
2783   int s_len = java_lang_String::length(s, s_value);
2784   if (start < 0 || len < 0 || start > s_len - len) {
2785     THROW(vmSymbols::java_lang_StringIndexOutOfBoundsException());
2786   } else {
2787     //%note jni_7
2788     if (len > 0) {
2789       // Assume the buffer is large enough as the JNI spec. does not require user error checking
2790       java_lang_String::as_utf8_string(s, s_value, start, len, buf, INT_MAX);
2791       // as_utf8_string null-terminates the result string
2792     } else {
2793       // JDK null-terminates the buffer even in len is zero
2794       if (buf != NULL) {
2795         buf[0] = 0;
2796       }
2797     }
2798   }
2799 JNI_END
2800 
lock_gc_or_pin_object(JavaThread * thread,jobject obj)2801 static oop lock_gc_or_pin_object(JavaThread* thread, jobject obj) {
2802   if (Universe::heap()->supports_object_pinning()) {
2803     const oop o = JNIHandles::resolve_non_null(obj);
2804     return Universe::heap()->pin_object(thread, o);
2805   } else {
2806     GCLocker::lock_critical(thread);
2807     return JNIHandles::resolve_non_null(obj);
2808   }
2809 }
2810 
unlock_gc_or_unpin_object(JavaThread * thread,jobject obj)2811 static void unlock_gc_or_unpin_object(JavaThread* thread, jobject obj) {
2812   if (Universe::heap()->supports_object_pinning()) {
2813     const oop o = JNIHandles::resolve_non_null(obj);
2814     return Universe::heap()->unpin_object(thread, o);
2815   } else {
2816     GCLocker::unlock_critical(thread);
2817   }
2818 }
2819 
2820 JNI_ENTRY(void*, jni_GetPrimitiveArrayCritical(JNIEnv *env, jarray array, jboolean *isCopy))
2821  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_ENTRY(env, array, (uintptr_t *) isCopy);
2822   if (isCopy != NULL) {
2823     *isCopy = JNI_FALSE;
2824   }
2825   oop a = lock_gc_or_pin_object(thread, array);
2826   assert(a->is_array(), "just checking");
2827   BasicType type;
2828   if (a->is_objArray()) {
2829     type = T_OBJECT;
2830   } else {
2831     type = TypeArrayKlass::cast(a->klass())->element_type();
2832   }
2833   void* ret = arrayOop(a)->base(type);
2834  HOTSPOT_JNI_GETPRIMITIVEARRAYCRITICAL_RETURN(ret);
2835   return ret;
2836 JNI_END
2837 
2838 
2839 JNI_ENTRY(void, jni_ReleasePrimitiveArrayCritical(JNIEnv *env, jarray array, void *carray, jint mode))
2840   HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_ENTRY(env, array, carray, mode);
2841   unlock_gc_or_unpin_object(thread, array);
2842 HOTSPOT_JNI_RELEASEPRIMITIVEARRAYCRITICAL_RETURN();
2843 JNI_END
2844 
2845 
lock_gc_or_pin_string_value(JavaThread * thread,oop str)2846 static typeArrayOop lock_gc_or_pin_string_value(JavaThread* thread, oop str) {
2847   if (Universe::heap()->supports_object_pinning()) {
2848     // Forbid deduplication before obtaining the value array, to prevent
2849     // deduplication from replacing the value array while setting up or in
2850     // the critical section.  That would lead to the release operation
2851     // unpinning the wrong object.
2852     if (StringDedup::is_enabled()) {
2853       NoSafepointVerifier nsv;
2854       StringDedup::forbid_deduplication(str);
2855     }
2856     typeArrayOop s_value = java_lang_String::value(str);
2857     return (typeArrayOop) Universe::heap()->pin_object(thread, s_value);
2858   } else {
2859     Handle h(thread, str);      // Handlize across potential safepoint.
2860     GCLocker::lock_critical(thread);
2861     return java_lang_String::value(h());
2862   }
2863 }
2864 
unlock_gc_or_unpin_string_value(JavaThread * thread,oop str)2865 static void unlock_gc_or_unpin_string_value(JavaThread* thread, oop str) {
2866   if (Universe::heap()->supports_object_pinning()) {
2867     typeArrayOop s_value = java_lang_String::value(str);
2868     Universe::heap()->unpin_object(thread, s_value);
2869   } else {
2870     GCLocker::unlock_critical(thread);
2871   }
2872 }
2873 
2874 JNI_ENTRY(const jchar*, jni_GetStringCritical(JNIEnv *env, jstring string, jboolean *isCopy))
2875   HOTSPOT_JNI_GETSTRINGCRITICAL_ENTRY(env, string, (uintptr_t *) isCopy);
2876   oop s = JNIHandles::resolve_non_null(string);
2877   jchar* ret;
2878   if (!java_lang_String::is_latin1(s)) {
2879     typeArrayOop s_value = lock_gc_or_pin_string_value(thread, s);
2880     ret = (jchar*) s_value->base(T_CHAR);
2881     if (isCopy != NULL) *isCopy = JNI_FALSE;
2882   } else {
2883     // Inflate latin1 encoded string to UTF16
2884     typeArrayOop s_value = java_lang_String::value(s);
2885     int s_len = java_lang_String::length(s, s_value);
2886     ret = NEW_C_HEAP_ARRAY_RETURN_NULL(jchar, s_len + 1, mtInternal);  // add one for zero termination
2887     /* JNI Specification states return NULL on OOM */
2888     if (ret != NULL) {
2889       for (int i = 0; i < s_len; i++) {
2890         ret[i] = ((jchar) s_value->byte_at(i)) & 0xff;
2891       }
2892       ret[s_len] = 0;
2893     }
2894     if (isCopy != NULL) *isCopy = JNI_TRUE;
2895   }
2896  HOTSPOT_JNI_GETSTRINGCRITICAL_RETURN((uint16_t *) ret);
2897   return ret;
2898 JNI_END
2899 
2900 
2901 JNI_ENTRY(void, jni_ReleaseStringCritical(JNIEnv *env, jstring str, const jchar *chars))
2902   HOTSPOT_JNI_RELEASESTRINGCRITICAL_ENTRY(env, str, (uint16_t *) chars);
2903   oop s = JNIHandles::resolve_non_null(str);
2904   bool is_latin1 = java_lang_String::is_latin1(s);
2905   if (is_latin1) {
2906     // For latin1 string, free jchar array allocated by earlier call to GetStringCritical.
2907     // This assumes that ReleaseStringCritical bookends GetStringCritical.
2908     FREE_C_HEAP_ARRAY(jchar, chars);
2909   } else {
2910     // For non-latin1 string, drop the associated gc-locker/pin.
2911     unlock_gc_or_unpin_string_value(thread, s);
2912   }
2913 HOTSPOT_JNI_RELEASESTRINGCRITICAL_RETURN();
2914 JNI_END
2915 
2916 
2917 JNI_ENTRY(jweak, jni_NewWeakGlobalRef(JNIEnv *env, jobject ref))
2918   HOTSPOT_JNI_NEWWEAKGLOBALREF_ENTRY(env, ref);
2919   Handle ref_handle(thread, JNIHandles::resolve(ref));
2920   jweak ret = JNIHandles::make_weak_global(ref_handle, AllocFailStrategy::RETURN_NULL);
2921   if (ret == NULL) {
2922     THROW_OOP_(Universe::out_of_memory_error_c_heap(), NULL);
2923   }
2924   HOTSPOT_JNI_NEWWEAKGLOBALREF_RETURN(ret);
2925   return ret;
2926 JNI_END
2927 
2928 // Must be JNI_ENTRY (with HandleMark)
2929 JNI_ENTRY(void, jni_DeleteWeakGlobalRef(JNIEnv *env, jweak ref))
2930   HOTSPOT_JNI_DELETEWEAKGLOBALREF_ENTRY(env, ref);
2931   JNIHandles::destroy_weak_global(ref);
2932   HOTSPOT_JNI_DELETEWEAKGLOBALREF_RETURN();
2933 JNI_END
2934 
2935 
2936 JNI_ENTRY_NO_PRESERVE(jboolean, jni_ExceptionCheck(JNIEnv *env))
2937  HOTSPOT_JNI_EXCEPTIONCHECK_ENTRY(env);
2938   jni_check_async_exceptions(thread);
2939   jboolean ret = (thread->has_pending_exception()) ? JNI_TRUE : JNI_FALSE;
2940  HOTSPOT_JNI_EXCEPTIONCHECK_RETURN(ret);
2941   return ret;
2942 JNI_END
2943 
2944 
2945 // Initialization state for three routines below relating to
2946 // java.nio.DirectBuffers
2947 static          int directBufferSupportInitializeStarted = 0;
2948 static volatile int directBufferSupportInitializeEnded   = 0;
2949 static volatile int directBufferSupportInitializeFailed  = 0;
2950 static jclass    bufferClass                 = NULL;
2951 static jclass    directBufferClass           = NULL;
2952 static jclass    directByteBufferClass       = NULL;
2953 static jmethodID directByteBufferConstructor = NULL;
2954 static jfieldID  directBufferAddressField    = NULL;
2955 static jfieldID  bufferCapacityField         = NULL;
2956 
lookupOne(JNIEnv * env,const char * name,TRAPS)2957 static jclass lookupOne(JNIEnv* env, const char* name, TRAPS) {
2958   Handle loader;            // null (bootstrap) loader
2959   Handle protection_domain; // null protection domain
2960 
2961   TempNewSymbol sym = SymbolTable::new_symbol(name);
2962   jclass result =  find_class_from_class_loader(env, sym, true, loader, protection_domain, true, CHECK_NULL);
2963 
2964   if (log_is_enabled(Debug, class, resolve) && result != NULL) {
2965     trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
2966   }
2967   return result;
2968 }
2969 
2970 // These lookups are done with the NULL (bootstrap) ClassLoader to
2971 // circumvent any security checks that would be done by jni_FindClass.
JNI_ENTRY(bool,lookupDirectBufferClasses (JNIEnv * env))2972 JNI_ENTRY(bool, lookupDirectBufferClasses(JNIEnv* env))
2973 {
2974   if ((bufferClass           = lookupOne(env, "java/nio/Buffer", thread))           == NULL) { return false; }
2975   if ((directBufferClass     = lookupOne(env, "sun/nio/ch/DirectBuffer", thread))   == NULL) { return false; }
2976   if ((directByteBufferClass = lookupOne(env, "java/nio/DirectByteBuffer", thread)) == NULL) { return false; }
2977   return true;
2978 }
2979 JNI_END
2980 
2981 
initializeDirectBufferSupport(JNIEnv * env,JavaThread * thread)2982 static bool initializeDirectBufferSupport(JNIEnv* env, JavaThread* thread) {
2983   if (directBufferSupportInitializeFailed) {
2984     return false;
2985   }
2986 
2987   if (Atomic::cmpxchg(&directBufferSupportInitializeStarted, 0, 1) == 0) {
2988     if (!lookupDirectBufferClasses(env)) {
2989       directBufferSupportInitializeFailed = 1;
2990       return false;
2991     }
2992 
2993     // Make global references for these
2994     bufferClass           = (jclass) env->NewGlobalRef(bufferClass);
2995     directBufferClass     = (jclass) env->NewGlobalRef(directBufferClass);
2996     directByteBufferClass = (jclass) env->NewGlobalRef(directByteBufferClass);
2997 
2998     // Global refs will be NULL if out-of-memory (no exception is pending)
2999     if (bufferClass == NULL || directBufferClass == NULL || directByteBufferClass == NULL) {
3000       directBufferSupportInitializeFailed = 1;
3001       return false;
3002     }
3003 
3004     // Get needed field and method IDs
3005     directByteBufferConstructor = env->GetMethodID(directByteBufferClass, "<init>", "(JI)V");
3006     if (env->ExceptionCheck()) {
3007       env->ExceptionClear();
3008       directBufferSupportInitializeFailed = 1;
3009       return false;
3010     }
3011     directBufferAddressField    = env->GetFieldID(bufferClass, "address", "J");
3012     if (env->ExceptionCheck()) {
3013       env->ExceptionClear();
3014       directBufferSupportInitializeFailed = 1;
3015       return false;
3016     }
3017     bufferCapacityField         = env->GetFieldID(bufferClass, "capacity", "I");
3018     if (env->ExceptionCheck()) {
3019       env->ExceptionClear();
3020       directBufferSupportInitializeFailed = 1;
3021       return false;
3022     }
3023 
3024     if ((directByteBufferConstructor == NULL) ||
3025         (directBufferAddressField    == NULL) ||
3026         (bufferCapacityField         == NULL)) {
3027       directBufferSupportInitializeFailed = 1;
3028       return false;
3029     }
3030 
3031     directBufferSupportInitializeEnded = 1;
3032   } else {
3033     while (!directBufferSupportInitializeEnded && !directBufferSupportInitializeFailed) {
3034       os::naked_yield();
3035     }
3036   }
3037 
3038   return !directBufferSupportInitializeFailed;
3039 }
3040 
jni_NewDirectByteBuffer(JNIEnv * env,void * address,jlong capacity)3041 extern "C" jobject JNICALL jni_NewDirectByteBuffer(JNIEnv *env, void* address, jlong capacity)
3042 {
3043   // thread_from_jni_environment() will block if VM is gone.
3044   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3045 
3046  HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_ENTRY(env, address, capacity);
3047 
3048   if (!directBufferSupportInitializeEnded) {
3049     if (!initializeDirectBufferSupport(env, thread)) {
3050       HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(NULL);
3051       return NULL;
3052     }
3053   }
3054 
3055   // Being paranoid about accidental sign extension on address
3056   jlong addr = (jlong) ((uintptr_t) address);
3057   // NOTE that package-private DirectByteBuffer constructor currently
3058   // takes int capacity
3059   jint  cap  = (jint)  capacity;
3060   jobject ret = env->NewObject(directByteBufferClass, directByteBufferConstructor, addr, cap);
3061   HOTSPOT_JNI_NEWDIRECTBYTEBUFFER_RETURN(ret);
3062   return ret;
3063 }
3064 
3065 DT_RETURN_MARK_DECL(GetDirectBufferAddress, void*
3066                     , HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_RETURN((void*) _ret_ref));
3067 
jni_GetDirectBufferAddress(JNIEnv * env,jobject buf)3068 extern "C" void* JNICALL jni_GetDirectBufferAddress(JNIEnv *env, jobject buf)
3069 {
3070   // thread_from_jni_environment() will block if VM is gone.
3071   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3072 
3073   HOTSPOT_JNI_GETDIRECTBUFFERADDRESS_ENTRY(env, buf);
3074   void* ret = NULL;
3075   DT_RETURN_MARK(GetDirectBufferAddress, void*, (const void*&)ret);
3076 
3077   if (!directBufferSupportInitializeEnded) {
3078     if (!initializeDirectBufferSupport(env, thread)) {
3079       return 0;
3080     }
3081   }
3082 
3083   if ((buf != NULL) && (!env->IsInstanceOf(buf, directBufferClass))) {
3084     return 0;
3085   }
3086 
3087   ret = (void*)(intptr_t)env->GetLongField(buf, directBufferAddressField);
3088   return ret;
3089 }
3090 
3091 DT_RETURN_MARK_DECL(GetDirectBufferCapacity, jlong
3092                     , HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_RETURN(_ret_ref));
3093 
jni_GetDirectBufferCapacity(JNIEnv * env,jobject buf)3094 extern "C" jlong JNICALL jni_GetDirectBufferCapacity(JNIEnv *env, jobject buf)
3095 {
3096   // thread_from_jni_environment() will block if VM is gone.
3097   JavaThread* thread = JavaThread::thread_from_jni_environment(env);
3098 
3099   HOTSPOT_JNI_GETDIRECTBUFFERCAPACITY_ENTRY(env, buf);
3100   jlong ret = -1;
3101   DT_RETURN_MARK(GetDirectBufferCapacity, jlong, (const jlong&)ret);
3102 
3103   if (!directBufferSupportInitializeEnded) {
3104     if (!initializeDirectBufferSupport(env, thread)) {
3105       ret = 0;
3106       return ret;
3107     }
3108   }
3109 
3110   if (buf == NULL) {
3111     return -1;
3112   }
3113 
3114   if (!env->IsInstanceOf(buf, directBufferClass)) {
3115     return -1;
3116   }
3117 
3118   // NOTE that capacity is currently an int in the implementation
3119   ret = env->GetIntField(buf, bufferCapacityField);
3120   return ret;
3121 }
3122 
3123 
3124 JNI_LEAF(jint, jni_GetVersion(JNIEnv *env))
3125   HOTSPOT_JNI_GETVERSION_ENTRY(env);
3126   HOTSPOT_JNI_GETVERSION_RETURN(CurrentVersion);
3127   return CurrentVersion;
3128 JNI_END
3129 
3130 extern struct JavaVM_ main_vm;
3131 
3132 JNI_LEAF(jint, jni_GetJavaVM(JNIEnv *env, JavaVM **vm))
3133   HOTSPOT_JNI_GETJAVAVM_ENTRY(env, (void **) vm);
3134   *vm  = (JavaVM *)(&main_vm);
3135   HOTSPOT_JNI_GETJAVAVM_RETURN(JNI_OK);
3136   return JNI_OK;
3137 JNI_END
3138 
3139 
3140 JNI_ENTRY(jobject, jni_GetModule(JNIEnv* env, jclass clazz))
3141   return Modules::get_module(clazz, THREAD);
3142 JNI_END
3143 
3144 
3145 // Structure containing all jni functions
3146 struct JNINativeInterface_ jni_NativeInterface = {
3147     NULL,
3148     NULL,
3149     NULL,
3150 
3151     NULL,
3152 
3153     jni_GetVersion,
3154 
3155     jni_DefineClass,
3156     jni_FindClass,
3157 
3158     jni_FromReflectedMethod,
3159     jni_FromReflectedField,
3160 
3161     jni_ToReflectedMethod,
3162 
3163     jni_GetSuperclass,
3164     jni_IsAssignableFrom,
3165 
3166     jni_ToReflectedField,
3167 
3168     jni_Throw,
3169     jni_ThrowNew,
3170     jni_ExceptionOccurred,
3171     jni_ExceptionDescribe,
3172     jni_ExceptionClear,
3173     jni_FatalError,
3174 
3175     jni_PushLocalFrame,
3176     jni_PopLocalFrame,
3177 
3178     jni_NewGlobalRef,
3179     jni_DeleteGlobalRef,
3180     jni_DeleteLocalRef,
3181     jni_IsSameObject,
3182 
3183     jni_NewLocalRef,
3184     jni_EnsureLocalCapacity,
3185 
3186     jni_AllocObject,
3187     jni_NewObject,
3188     jni_NewObjectV,
3189     jni_NewObjectA,
3190 
3191     jni_GetObjectClass,
3192     jni_IsInstanceOf,
3193 
3194     jni_GetMethodID,
3195 
3196     jni_CallObjectMethod,
3197     jni_CallObjectMethodV,
3198     jni_CallObjectMethodA,
3199     jni_CallBooleanMethod,
3200     jni_CallBooleanMethodV,
3201     jni_CallBooleanMethodA,
3202     jni_CallByteMethod,
3203     jni_CallByteMethodV,
3204     jni_CallByteMethodA,
3205     jni_CallCharMethod,
3206     jni_CallCharMethodV,
3207     jni_CallCharMethodA,
3208     jni_CallShortMethod,
3209     jni_CallShortMethodV,
3210     jni_CallShortMethodA,
3211     jni_CallIntMethod,
3212     jni_CallIntMethodV,
3213     jni_CallIntMethodA,
3214     jni_CallLongMethod,
3215     jni_CallLongMethodV,
3216     jni_CallLongMethodA,
3217     jni_CallFloatMethod,
3218     jni_CallFloatMethodV,
3219     jni_CallFloatMethodA,
3220     jni_CallDoubleMethod,
3221     jni_CallDoubleMethodV,
3222     jni_CallDoubleMethodA,
3223     jni_CallVoidMethod,
3224     jni_CallVoidMethodV,
3225     jni_CallVoidMethodA,
3226 
3227     jni_CallNonvirtualObjectMethod,
3228     jni_CallNonvirtualObjectMethodV,
3229     jni_CallNonvirtualObjectMethodA,
3230     jni_CallNonvirtualBooleanMethod,
3231     jni_CallNonvirtualBooleanMethodV,
3232     jni_CallNonvirtualBooleanMethodA,
3233     jni_CallNonvirtualByteMethod,
3234     jni_CallNonvirtualByteMethodV,
3235     jni_CallNonvirtualByteMethodA,
3236     jni_CallNonvirtualCharMethod,
3237     jni_CallNonvirtualCharMethodV,
3238     jni_CallNonvirtualCharMethodA,
3239     jni_CallNonvirtualShortMethod,
3240     jni_CallNonvirtualShortMethodV,
3241     jni_CallNonvirtualShortMethodA,
3242     jni_CallNonvirtualIntMethod,
3243     jni_CallNonvirtualIntMethodV,
3244     jni_CallNonvirtualIntMethodA,
3245     jni_CallNonvirtualLongMethod,
3246     jni_CallNonvirtualLongMethodV,
3247     jni_CallNonvirtualLongMethodA,
3248     jni_CallNonvirtualFloatMethod,
3249     jni_CallNonvirtualFloatMethodV,
3250     jni_CallNonvirtualFloatMethodA,
3251     jni_CallNonvirtualDoubleMethod,
3252     jni_CallNonvirtualDoubleMethodV,
3253     jni_CallNonvirtualDoubleMethodA,
3254     jni_CallNonvirtualVoidMethod,
3255     jni_CallNonvirtualVoidMethodV,
3256     jni_CallNonvirtualVoidMethodA,
3257 
3258     jni_GetFieldID,
3259 
3260     jni_GetObjectField,
3261     jni_GetBooleanField,
3262     jni_GetByteField,
3263     jni_GetCharField,
3264     jni_GetShortField,
3265     jni_GetIntField,
3266     jni_GetLongField,
3267     jni_GetFloatField,
3268     jni_GetDoubleField,
3269 
3270     jni_SetObjectField,
3271     jni_SetBooleanField,
3272     jni_SetByteField,
3273     jni_SetCharField,
3274     jni_SetShortField,
3275     jni_SetIntField,
3276     jni_SetLongField,
3277     jni_SetFloatField,
3278     jni_SetDoubleField,
3279 
3280     jni_GetStaticMethodID,
3281 
3282     jni_CallStaticObjectMethod,
3283     jni_CallStaticObjectMethodV,
3284     jni_CallStaticObjectMethodA,
3285     jni_CallStaticBooleanMethod,
3286     jni_CallStaticBooleanMethodV,
3287     jni_CallStaticBooleanMethodA,
3288     jni_CallStaticByteMethod,
3289     jni_CallStaticByteMethodV,
3290     jni_CallStaticByteMethodA,
3291     jni_CallStaticCharMethod,
3292     jni_CallStaticCharMethodV,
3293     jni_CallStaticCharMethodA,
3294     jni_CallStaticShortMethod,
3295     jni_CallStaticShortMethodV,
3296     jni_CallStaticShortMethodA,
3297     jni_CallStaticIntMethod,
3298     jni_CallStaticIntMethodV,
3299     jni_CallStaticIntMethodA,
3300     jni_CallStaticLongMethod,
3301     jni_CallStaticLongMethodV,
3302     jni_CallStaticLongMethodA,
3303     jni_CallStaticFloatMethod,
3304     jni_CallStaticFloatMethodV,
3305     jni_CallStaticFloatMethodA,
3306     jni_CallStaticDoubleMethod,
3307     jni_CallStaticDoubleMethodV,
3308     jni_CallStaticDoubleMethodA,
3309     jni_CallStaticVoidMethod,
3310     jni_CallStaticVoidMethodV,
3311     jni_CallStaticVoidMethodA,
3312 
3313     jni_GetStaticFieldID,
3314 
3315     jni_GetStaticObjectField,
3316     jni_GetStaticBooleanField,
3317     jni_GetStaticByteField,
3318     jni_GetStaticCharField,
3319     jni_GetStaticShortField,
3320     jni_GetStaticIntField,
3321     jni_GetStaticLongField,
3322     jni_GetStaticFloatField,
3323     jni_GetStaticDoubleField,
3324 
3325     jni_SetStaticObjectField,
3326     jni_SetStaticBooleanField,
3327     jni_SetStaticByteField,
3328     jni_SetStaticCharField,
3329     jni_SetStaticShortField,
3330     jni_SetStaticIntField,
3331     jni_SetStaticLongField,
3332     jni_SetStaticFloatField,
3333     jni_SetStaticDoubleField,
3334 
3335     jni_NewString,
3336     jni_GetStringLength,
3337     jni_GetStringChars,
3338     jni_ReleaseStringChars,
3339 
3340     jni_NewStringUTF,
3341     jni_GetStringUTFLength,
3342     jni_GetStringUTFChars,
3343     jni_ReleaseStringUTFChars,
3344 
3345     jni_GetArrayLength,
3346 
3347     jni_NewObjectArray,
3348     jni_GetObjectArrayElement,
3349     jni_SetObjectArrayElement,
3350 
3351     jni_NewBooleanArray,
3352     jni_NewByteArray,
3353     jni_NewCharArray,
3354     jni_NewShortArray,
3355     jni_NewIntArray,
3356     jni_NewLongArray,
3357     jni_NewFloatArray,
3358     jni_NewDoubleArray,
3359 
3360     jni_GetBooleanArrayElements,
3361     jni_GetByteArrayElements,
3362     jni_GetCharArrayElements,
3363     jni_GetShortArrayElements,
3364     jni_GetIntArrayElements,
3365     jni_GetLongArrayElements,
3366     jni_GetFloatArrayElements,
3367     jni_GetDoubleArrayElements,
3368 
3369     jni_ReleaseBooleanArrayElements,
3370     jni_ReleaseByteArrayElements,
3371     jni_ReleaseCharArrayElements,
3372     jni_ReleaseShortArrayElements,
3373     jni_ReleaseIntArrayElements,
3374     jni_ReleaseLongArrayElements,
3375     jni_ReleaseFloatArrayElements,
3376     jni_ReleaseDoubleArrayElements,
3377 
3378     jni_GetBooleanArrayRegion,
3379     jni_GetByteArrayRegion,
3380     jni_GetCharArrayRegion,
3381     jni_GetShortArrayRegion,
3382     jni_GetIntArrayRegion,
3383     jni_GetLongArrayRegion,
3384     jni_GetFloatArrayRegion,
3385     jni_GetDoubleArrayRegion,
3386 
3387     jni_SetBooleanArrayRegion,
3388     jni_SetByteArrayRegion,
3389     jni_SetCharArrayRegion,
3390     jni_SetShortArrayRegion,
3391     jni_SetIntArrayRegion,
3392     jni_SetLongArrayRegion,
3393     jni_SetFloatArrayRegion,
3394     jni_SetDoubleArrayRegion,
3395 
3396     jni_RegisterNatives,
3397     jni_UnregisterNatives,
3398 
3399     jni_MonitorEnter,
3400     jni_MonitorExit,
3401 
3402     jni_GetJavaVM,
3403 
3404     jni_GetStringRegion,
3405     jni_GetStringUTFRegion,
3406 
3407     jni_GetPrimitiveArrayCritical,
3408     jni_ReleasePrimitiveArrayCritical,
3409 
3410     jni_GetStringCritical,
3411     jni_ReleaseStringCritical,
3412 
3413     jni_NewWeakGlobalRef,
3414     jni_DeleteWeakGlobalRef,
3415 
3416     jni_ExceptionCheck,
3417 
3418     jni_NewDirectByteBuffer,
3419     jni_GetDirectBufferAddress,
3420     jni_GetDirectBufferCapacity,
3421 
3422     // New 1_6 features
3423 
3424     jni_GetObjectRefType,
3425 
3426     // Module features
3427 
3428     jni_GetModule
3429 };
3430 
3431 
3432 // For jvmti use to modify jni function table.
3433 // Java threads in native contiues to run until it is transitioned
3434 // to VM at safepoint. Before the transition or before it is blocked
3435 // for safepoint it may access jni function table. VM could crash if
3436 // any java thread access the jni function table in the middle of memcpy.
3437 // To avoid this each function pointers are copied automically.
copy_jni_function_table(const struct JNINativeInterface_ * new_jni_NativeInterface)3438 void copy_jni_function_table(const struct JNINativeInterface_ *new_jni_NativeInterface) {
3439   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
3440   intptr_t *a = (intptr_t *) jni_functions();
3441   intptr_t *b = (intptr_t *) new_jni_NativeInterface;
3442   for (uint i=0; i <  sizeof(struct JNINativeInterface_)/sizeof(void *); i++) {
3443     Atomic::store(a++, *b++);
3444   }
3445 }
3446 
quicken_jni_functions()3447 void quicken_jni_functions() {
3448   // Replace Get<Primitive>Field with fast versions
3449   if (UseFastJNIAccessors && !VerifyJNIFields && !CheckJNICalls) {
3450     address func;
3451     func = JNI_FastGetField::generate_fast_get_boolean_field();
3452     if (func != (address)-1) {
3453       jni_NativeInterface.GetBooleanField = (GetBooleanField_t)func;
3454     }
3455     func = JNI_FastGetField::generate_fast_get_byte_field();
3456     if (func != (address)-1) {
3457       jni_NativeInterface.GetByteField = (GetByteField_t)func;
3458     }
3459     func = JNI_FastGetField::generate_fast_get_char_field();
3460     if (func != (address)-1) {
3461       jni_NativeInterface.GetCharField = (GetCharField_t)func;
3462     }
3463     func = JNI_FastGetField::generate_fast_get_short_field();
3464     if (func != (address)-1) {
3465       jni_NativeInterface.GetShortField = (GetShortField_t)func;
3466     }
3467     func = JNI_FastGetField::generate_fast_get_int_field();
3468     if (func != (address)-1) {
3469       jni_NativeInterface.GetIntField = (GetIntField_t)func;
3470     }
3471     func = JNI_FastGetField::generate_fast_get_long_field();
3472     if (func != (address)-1) {
3473       jni_NativeInterface.GetLongField = (GetLongField_t)func;
3474     }
3475     func = JNI_FastGetField::generate_fast_get_float_field();
3476     if (func != (address)-1) {
3477       jni_NativeInterface.GetFloatField = (GetFloatField_t)func;
3478     }
3479     func = JNI_FastGetField::generate_fast_get_double_field();
3480     if (func != (address)-1) {
3481       jni_NativeInterface.GetDoubleField = (GetDoubleField_t)func;
3482     }
3483   }
3484 }
3485 
3486 // Returns the function structure
jni_functions()3487 struct JNINativeInterface_* jni_functions() {
3488 #if INCLUDE_JNI_CHECK
3489   if (CheckJNICalls) return jni_functions_check();
3490 #endif // INCLUDE_JNI_CHECK
3491   return &jni_NativeInterface;
3492 }
3493 
3494 // Returns the function structure
jni_functions_nocheck()3495 struct JNINativeInterface_* jni_functions_nocheck() {
3496   return &jni_NativeInterface;
3497 }
3498 
post_thread_start_event(const JavaThread * jt)3499 static void post_thread_start_event(const JavaThread* jt) {
3500   assert(jt != NULL, "invariant");
3501   EventThreadStart event;
3502   if (event.should_commit()) {
3503     event.set_thread(JFR_THREAD_ID(jt));
3504     event.set_parentThread((traceid)0);
3505 #if INCLUDE_JFR
3506     if (EventThreadStart::is_stacktrace_enabled()) {
3507       jt->jfr_thread_local()->set_cached_stack_trace_id((traceid)0);
3508       event.commit();
3509       jt->jfr_thread_local()->clear_cached_stack_trace();
3510     } else
3511 #endif
3512     {
3513       event.commit();
3514     }
3515   }
3516 }
3517 
3518 // Invocation API
3519 
3520 
3521 // Forward declaration
3522 extern const struct JNIInvokeInterface_ jni_InvokeInterface;
3523 
3524 // Global invocation API vars
3525 volatile int vm_created = 0;
3526 // Indicate whether it is safe to recreate VM. Recreation is only
3527 // possible after a failed initial creation attempt in some cases.
3528 volatile int safe_to_recreate_vm = 1;
3529 struct JavaVM_ main_vm = {&jni_InvokeInterface};
3530 
3531 
3532 #define JAVASTACKSIZE (400 * 1024)    /* Default size of a thread java stack */
3533 enum { VERIFY_NONE, VERIFY_REMOTE, VERIFY_ALL };
3534 
3535 DT_RETURN_MARK_DECL(GetDefaultJavaVMInitArgs, jint
3536                     , HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_RETURN(_ret_ref));
3537 
JNI_GetDefaultJavaVMInitArgs(void * args_)3538 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetDefaultJavaVMInitArgs(void *args_) {
3539   HOTSPOT_JNI_GETDEFAULTJAVAVMINITARGS_ENTRY(args_);
3540   JDK1_1InitArgs *args = (JDK1_1InitArgs *)args_;
3541   jint ret = JNI_ERR;
3542   DT_RETURN_MARK(GetDefaultJavaVMInitArgs, jint, (const jint&)ret);
3543 
3544   if (Threads::is_supported_jni_version(args->version)) {
3545     ret = JNI_OK;
3546   }
3547   // 1.1 style no longer supported in hotspot.
3548   // According the JNI spec, we should update args->version on return.
3549   // We also use the structure to communicate with launcher about default
3550   // stack size.
3551   if (args->version == JNI_VERSION_1_1) {
3552     args->version = JNI_VERSION_1_2;
3553     // javaStackSize is int in arguments structure
3554     assert(jlong(ThreadStackSize) * K < INT_MAX, "integer overflow");
3555     args->javaStackSize = (jint)(ThreadStackSize * K);
3556   }
3557   return ret;
3558 }
3559 
3560 DT_RETURN_MARK_DECL(CreateJavaVM, jint
3561                     , HOTSPOT_JNI_CREATEJAVAVM_RETURN(_ret_ref));
3562 
JNI_CreateJavaVM_inner(JavaVM ** vm,void ** penv,void * args)3563 static jint JNI_CreateJavaVM_inner(JavaVM **vm, void **penv, void *args) {
3564   HOTSPOT_JNI_CREATEJAVAVM_ENTRY((void **) vm, penv, args);
3565 
3566   jint result = JNI_ERR;
3567   DT_RETURN_MARK(CreateJavaVM, jint, (const jint&)result);
3568 
3569   // We're about to use Atomic::xchg for synchronization.  Some Zero
3570   // platforms use the GCC builtin __sync_lock_test_and_set for this,
3571   // but __sync_lock_test_and_set is not guaranteed to do what we want
3572   // on all architectures.  So we check it works before relying on it.
3573 #if defined(ZERO) && defined(ASSERT)
3574   {
3575     jint a = 0xcafebabe;
3576     jint b = Atomic::xchg(&a, (jint) 0xdeadbeef);
3577     void *c = &a;
3578     void *d = Atomic::xchg(&c, &b);
3579     assert(a == (jint) 0xdeadbeef && b == (jint) 0xcafebabe, "Atomic::xchg() works");
3580     assert(c == &b && d == &a, "Atomic::xchg() works");
3581   }
3582 #endif // ZERO && ASSERT
3583 
3584   // At the moment it's only possible to have one Java VM,
3585   // since some of the runtime state is in global variables.
3586 
3587   // We cannot use our mutex locks here, since they only work on
3588   // Threads. We do an atomic compare and exchange to ensure only
3589   // one thread can call this method at a time
3590 
3591   // We use Atomic::xchg rather than Atomic::add/dec since on some platforms
3592   // the add/dec implementations are dependent on whether we are running
3593   // on a multiprocessor Atomic::xchg does not have this problem.
3594   if (Atomic::xchg(&vm_created, 1) == 1) {
3595     return JNI_EEXIST;   // already created, or create attempt in progress
3596   }
3597 
3598   // If a previous creation attempt failed but can be retried safely,
3599   // then safe_to_recreate_vm will have been reset to 1 after being
3600   // cleared here. If a previous creation attempt succeeded and we then
3601   // destroyed that VM, we will be prevented from trying to recreate
3602   // the VM in the same process, as the value will still be 0.
3603   if (Atomic::xchg(&safe_to_recreate_vm, 0) == 0) {
3604     return JNI_ERR;
3605   }
3606 
3607   assert(vm_created == 1, "vm_created is true during the creation");
3608 
3609   /**
3610    * Certain errors during initialization are recoverable and do not
3611    * prevent this method from being called again at a later time
3612    * (perhaps with different arguments).  However, at a certain
3613    * point during initialization if an error occurs we cannot allow
3614    * this function to be called again (or it will crash).  In those
3615    * situations, the 'canTryAgain' flag is set to false, which atomically
3616    * sets safe_to_recreate_vm to 1, such that any new call to
3617    * JNI_CreateJavaVM will immediately fail using the above logic.
3618    */
3619   bool can_try_again = true;
3620 
3621   result = Threads::create_vm((JavaVMInitArgs*) args, &can_try_again);
3622   if (result == JNI_OK) {
3623     JavaThread *thread = JavaThread::current();
3624     assert(!thread->has_pending_exception(), "should have returned not OK");
3625     /* thread is thread_in_vm here */
3626     *vm = (JavaVM *)(&main_vm);
3627     *(JNIEnv**)penv = thread->jni_environment();
3628 
3629 #if INCLUDE_JVMCI
3630     if (EnableJVMCI) {
3631       if (UseJVMCICompiler) {
3632         // JVMCI is initialized on a CompilerThread
3633         if (BootstrapJVMCI) {
3634           JavaThread* THREAD = thread; // For exception macros.
3635           JVMCICompiler* compiler = JVMCICompiler::instance(true, CATCH);
3636           compiler->bootstrap(THREAD);
3637           if (HAS_PENDING_EXCEPTION) {
3638             HandleMark hm(THREAD);
3639             vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3640           }
3641         }
3642       }
3643     }
3644 #endif
3645 
3646     // Notify JVMTI
3647     if (JvmtiExport::should_post_thread_life()) {
3648        JvmtiExport::post_thread_start(thread);
3649     }
3650 
3651     post_thread_start_event(thread);
3652 
3653 #ifndef PRODUCT
3654     if (ReplayCompiles) ciReplay::replay(thread);
3655 #endif
3656 
3657 #ifdef ASSERT
3658     // Some platforms (like Win*) need a wrapper around these test
3659     // functions in order to properly handle error conditions.
3660     if (ErrorHandlerTest != 0) {
3661       VMError::controlled_crash(ErrorHandlerTest);
3662     }
3663 #endif
3664 
3665     // Since this is not a JVM_ENTRY we have to set the thread state manually before leaving.
3666     ThreadStateTransition::transition(thread, _thread_in_vm, _thread_in_native);
3667     MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
3668   } else {
3669     // If create_vm exits because of a pending exception, exit with that
3670     // exception.  In the future when we figure out how to reclaim memory,
3671     // we may be able to exit with JNI_ERR and allow the calling application
3672     // to continue.
3673     if (Universe::is_fully_initialized()) {
3674       // otherwise no pending exception possible - VM will already have aborted
3675       JavaThread* THREAD = JavaThread::current(); // For exception macros.
3676       if (HAS_PENDING_EXCEPTION) {
3677         HandleMark hm(THREAD);
3678         vm_exit_during_initialization(Handle(THREAD, PENDING_EXCEPTION));
3679       }
3680     }
3681 
3682     if (can_try_again) {
3683       // reset safe_to_recreate_vm to 1 so that retrial would be possible
3684       safe_to_recreate_vm = 1;
3685     }
3686 
3687     // Creation failed. We must reset vm_created
3688     *vm = 0;
3689     *(JNIEnv**)penv = 0;
3690     // reset vm_created last to avoid race condition. Use OrderAccess to
3691     // control both compiler and architectural-based reordering.
3692     Atomic::release_store(&vm_created, 0);
3693   }
3694 
3695   // Flush stdout and stderr before exit.
3696   fflush(stdout);
3697   fflush(stderr);
3698 
3699   return result;
3700 
3701 }
3702 
JNI_CreateJavaVM(JavaVM ** vm,void ** penv,void * args)3703 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_CreateJavaVM(JavaVM **vm, void **penv, void *args) {
3704   jint result = JNI_ERR;
3705   // On Windows, let CreateJavaVM run with SEH protection
3706 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3707   __try {
3708 #endif
3709     result = JNI_CreateJavaVM_inner(vm, penv, args);
3710 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3711   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
3712     // Nothing to do.
3713   }
3714 #endif
3715   return result;
3716 }
3717 
JNI_GetCreatedJavaVMs(JavaVM ** vm_buf,jsize bufLen,jsize * numVMs)3718 _JNI_IMPORT_OR_EXPORT_ jint JNICALL JNI_GetCreatedJavaVMs(JavaVM **vm_buf, jsize bufLen, jsize *numVMs) {
3719   HOTSPOT_JNI_GETCREATEDJAVAVMS_ENTRY((void **) vm_buf, bufLen, (uintptr_t *) numVMs);
3720 
3721   if (vm_created == 1) {
3722     if (numVMs != NULL) *numVMs = 1;
3723     if (bufLen > 0)     *vm_buf = (JavaVM *)(&main_vm);
3724   } else {
3725     if (numVMs != NULL) *numVMs = 0;
3726   }
3727   HOTSPOT_JNI_GETCREATEDJAVAVMS_RETURN(JNI_OK);
3728   return JNI_OK;
3729 }
3730 
3731 extern "C" {
3732 
3733 DT_RETURN_MARK_DECL(DestroyJavaVM, jint
3734                     , HOTSPOT_JNI_DESTROYJAVAVM_RETURN(_ret_ref));
3735 
jni_DestroyJavaVM_inner(JavaVM * vm)3736 static jint JNICALL jni_DestroyJavaVM_inner(JavaVM *vm) {
3737   HOTSPOT_JNI_DESTROYJAVAVM_ENTRY(vm);
3738   jint res = JNI_ERR;
3739   DT_RETURN_MARK(DestroyJavaVM, jint, (const jint&)res);
3740 
3741   if (vm_created == 0) {
3742     res = JNI_ERR;
3743     return res;
3744   }
3745 
3746   JNIEnv *env;
3747   JavaVMAttachArgs destroyargs;
3748   destroyargs.version = CurrentVersion;
3749   destroyargs.name = (char *)"DestroyJavaVM";
3750   destroyargs.group = NULL;
3751   res = vm->AttachCurrentThread((void **)&env, (void *)&destroyargs);
3752   if (res != JNI_OK) {
3753     return res;
3754   }
3755 
3756   // Since this is not a JVM_ENTRY we have to set the thread state manually before entering.
3757   JavaThread* thread = JavaThread::current();
3758 
3759   // We are going to VM, change W^X state to the expected one.
3760   MACOS_AARCH64_ONLY(WXMode oldmode = thread->enable_wx(WXWrite));
3761 
3762   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
3763   Threads::destroy_vm();
3764   // Don't bother restoring thread state, VM is gone.
3765   vm_created = 0;
3766   return JNI_OK;
3767 }
3768 
jni_DestroyJavaVM(JavaVM * vm)3769 jint JNICALL jni_DestroyJavaVM(JavaVM *vm) {
3770   jint result = JNI_ERR;
3771   // On Windows, we need SEH protection
3772 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3773   __try {
3774 #endif
3775     result = jni_DestroyJavaVM_inner(vm);
3776 #if defined(_WIN32) && !defined(USE_VECTORED_EXCEPTION_HANDLING)
3777   } __except(topLevelExceptionFilter((_EXCEPTION_POINTERS*)_exception_info())) {
3778     // Nothing to do.
3779   }
3780 #endif
3781   return result;
3782 }
3783 
attach_current_thread(JavaVM * vm,void ** penv,void * _args,bool daemon)3784 static jint attach_current_thread(JavaVM *vm, void **penv, void *_args, bool daemon) {
3785   JavaVMAttachArgs *args = (JavaVMAttachArgs *) _args;
3786 
3787   // Check below commented out from JDK1.2fcs as well
3788   /*
3789   if (args && (args->version != JNI_VERSION_1_1 || args->version != JNI_VERSION_1_2)) {
3790     return JNI_EVERSION;
3791   }
3792   */
3793 
3794   Thread* t = Thread::current_or_null();
3795   if (t != NULL) {
3796     // If executing from an atexit hook we may be in the VMThread.
3797     if (t->is_Java_thread()) {
3798       // If the thread has been attached this operation is a no-op
3799       *(JNIEnv**)penv = t->as_Java_thread()->jni_environment();
3800       return JNI_OK;
3801     } else {
3802       return JNI_ERR;
3803     }
3804   }
3805 
3806   // Create a thread and mark it as attaching so it will be skipped by the
3807   // ThreadsListEnumerator - see CR 6404306
3808   JavaThread* thread = new JavaThread(true);
3809 
3810   // Set correct safepoint info. The thread is going to call into Java when
3811   // initializing the Java level thread object. Hence, the correct state must
3812   // be set in order for the Safepoint code to deal with it correctly.
3813   thread->set_thread_state(_thread_in_vm);
3814   thread->record_stack_base_and_size();
3815   thread->register_thread_stack_with_NMT();
3816   thread->initialize_thread_current();
3817   MACOS_AARCH64_ONLY(thread->init_wx());
3818 
3819   if (!os::create_attached_thread(thread)) {
3820     thread->smr_delete();
3821     return JNI_ERR;
3822   }
3823   // Enable stack overflow checks
3824   thread->stack_overflow_state()->create_stack_guard_pages();
3825 
3826   thread->initialize_tlab();
3827 
3828   thread->cache_global_variables();
3829 
3830   // This thread will not do a safepoint check, since it has
3831   // not been added to the Thread list yet.
3832   { MutexLocker ml(Threads_lock);
3833     // This must be inside this lock in order to get FullGCALot to work properly, i.e., to
3834     // avoid this thread trying to do a GC before it is added to the thread-list
3835     thread->set_active_handles(JNIHandleBlock::allocate_block());
3836     Threads::add(thread, daemon);
3837   }
3838   // Create thread group and name info from attach arguments
3839   oop group = NULL;
3840   char* thread_name = NULL;
3841   if (args != NULL && Threads::is_supported_jni_version(args->version)) {
3842     group = JNIHandles::resolve(args->group);
3843     thread_name = args->name; // may be NULL
3844   }
3845   if (group == NULL) group = Universe::main_thread_group();
3846 
3847   // Create Java level thread object and attach it to this thread
3848   bool attach_failed = false;
3849   {
3850     EXCEPTION_MARK;
3851     HandleMark hm(THREAD);
3852     Handle thread_group(THREAD, group);
3853     thread->allocate_threadObj(thread_group, thread_name, daemon, THREAD);
3854     if (HAS_PENDING_EXCEPTION) {
3855       CLEAR_PENDING_EXCEPTION;
3856       // cleanup outside the handle mark.
3857       attach_failed = true;
3858     }
3859   }
3860 
3861   if (attach_failed) {
3862     // Added missing cleanup
3863     thread->cleanup_failed_attach_current_thread(daemon);
3864     return JNI_ERR;
3865   }
3866 
3867   // mark the thread as no longer attaching
3868   // this uses a fence to push the change through so we don't have
3869   // to regrab the threads_lock
3870   thread->set_done_attaching_via_jni();
3871 
3872   // Set java thread status.
3873   java_lang_Thread::set_thread_status(thread->threadObj(),
3874               JavaThreadStatus::RUNNABLE);
3875 
3876   // Notify the debugger
3877   if (JvmtiExport::should_post_thread_life()) {
3878     JvmtiExport::post_thread_start(thread);
3879   }
3880 
3881   post_thread_start_event(thread);
3882 
3883   *(JNIEnv**)penv = thread->jni_environment();
3884 
3885   // Now leaving the VM, so change thread_state. This is normally automatically taken care
3886   // of in the JVM_ENTRY. But in this situation we have to do it manually. Notice, that by
3887   // using ThreadStateTransition::transition, we do a callback to the safepoint code if
3888   // needed.
3889 
3890   ThreadStateTransition::transition(thread, _thread_in_vm, _thread_in_native);
3891   MACOS_AARCH64_ONLY(thread->enable_wx(WXExec));
3892 
3893   // Perform any platform dependent FPU setup
3894   os::setup_fpu();
3895 
3896   return JNI_OK;
3897 }
3898 
3899 
jni_AttachCurrentThread(JavaVM * vm,void ** penv,void * _args)3900 jint JNICALL jni_AttachCurrentThread(JavaVM *vm, void **penv, void *_args) {
3901   HOTSPOT_JNI_ATTACHCURRENTTHREAD_ENTRY(vm, penv, _args);
3902   if (vm_created == 0) {
3903     HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
3904     return JNI_ERR;
3905   }
3906 
3907   jint ret = attach_current_thread(vm, penv, _args, false);
3908   HOTSPOT_JNI_ATTACHCURRENTTHREAD_RETURN(ret);
3909   return ret;
3910 }
3911 
3912 
jni_DetachCurrentThread(JavaVM * vm)3913 jint JNICALL jni_DetachCurrentThread(JavaVM *vm)  {
3914   HOTSPOT_JNI_DETACHCURRENTTHREAD_ENTRY(vm);
3915   if (vm_created == 0) {
3916     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_ERR);
3917     return JNI_ERR;
3918   }
3919 
3920   Thread* current = Thread::current_or_null();
3921 
3922   // If the thread has already been detached the operation is a no-op
3923   if (current == NULL) {
3924     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
3925     return JNI_OK;
3926   }
3927 
3928   // If executing from an atexit hook we may be in the VMThread.
3929   if (!current->is_Java_thread()) {
3930     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
3931     return JNI_ERR;
3932   }
3933 
3934   VM_Exit::block_if_vm_exited();
3935 
3936   JavaThread* thread = current->as_Java_thread();
3937   if (thread->has_last_Java_frame()) {
3938     HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN((uint32_t) JNI_ERR);
3939     // Can't detach a thread that's running java, that can't work.
3940     return JNI_ERR;
3941   }
3942 
3943   // We are going to VM, change W^X state to the expected one.
3944   MACOS_AARCH64_ONLY(thread->enable_wx(WXWrite));
3945 
3946   // Safepoint support. Have to do call-back to safepoint code, if in the
3947   // middle of a safepoint operation
3948   ThreadStateTransition::transition_from_native(thread, _thread_in_vm);
3949 
3950   // XXX: Note that JavaThread::exit() call below removes the guards on the
3951   // stack pages set up via enable_stack_{red,yellow}_zone() calls
3952   // above in jni_AttachCurrentThread. Unfortunately, while the setting
3953   // of the guards is visible in jni_AttachCurrentThread above,
3954   // the removal of the guards is buried below in JavaThread::exit()
3955   // here. The abstraction should be more symmetrically either exposed
3956   // or hidden (e.g. it could probably be hidden in the same
3957   // (platform-dependent) methods where we do alternate stack
3958   // maintenance work?)
3959   thread->exit(false, JavaThread::jni_detach);
3960   thread->smr_delete();
3961 
3962   // Go to the execute mode, the initial state of the thread on creation.
3963   // Use os interface as the thread is not a JavaThread anymore.
3964   MACOS_AARCH64_ONLY(os::current_thread_enable_wx(WXExec));
3965 
3966   HOTSPOT_JNI_DETACHCURRENTTHREAD_RETURN(JNI_OK);
3967   return JNI_OK;
3968 }
3969 
3970 DT_RETURN_MARK_DECL(GetEnv, jint
3971                     , HOTSPOT_JNI_GETENV_RETURN(_ret_ref));
3972 
jni_GetEnv(JavaVM * vm,void ** penv,jint version)3973 jint JNICALL jni_GetEnv(JavaVM *vm, void **penv, jint version) {
3974   HOTSPOT_JNI_GETENV_ENTRY(vm, penv, version);
3975   jint ret = JNI_ERR;
3976   DT_RETURN_MARK(GetEnv, jint, (const jint&)ret);
3977 
3978   if (vm_created == 0) {
3979     *penv = NULL;
3980     ret = JNI_EDETACHED;
3981     return ret;
3982   }
3983 
3984   if (JniExportedInterface::GetExportedInterface(vm, penv, version, &ret)) {
3985     return ret;
3986   }
3987 
3988 #ifndef JVMPI_VERSION_1
3989 // need these in order to be polite about older agents
3990 #define JVMPI_VERSION_1   ((jint)0x10000001)
3991 #define JVMPI_VERSION_1_1 ((jint)0x10000002)
3992 #define JVMPI_VERSION_1_2 ((jint)0x10000003)
3993 #endif // !JVMPI_VERSION_1
3994 
3995   Thread* thread = Thread::current_or_null();
3996   if (thread != NULL && thread->is_Java_thread()) {
3997     if (Threads::is_supported_jni_version_including_1_1(version)) {
3998       *(JNIEnv**)penv = thread->as_Java_thread()->jni_environment();
3999       ret = JNI_OK;
4000       return ret;
4001 
4002     } else if (version == JVMPI_VERSION_1 ||
4003                version == JVMPI_VERSION_1_1 ||
4004                version == JVMPI_VERSION_1_2) {
4005       tty->print_cr("ERROR: JVMPI, an experimental interface, is no longer supported.");
4006       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4007       ret = JNI_EVERSION;
4008       return ret;
4009     } else if (JvmtiExport::is_jvmdi_version(version)) {
4010       tty->print_cr("FATAL ERROR: JVMDI is no longer supported.");
4011       tty->print_cr("Please use the supported interface: the JVM Tool Interface (JVM TI).");
4012       ret = JNI_EVERSION;
4013       return ret;
4014     } else {
4015       *penv = NULL;
4016       ret = JNI_EVERSION;
4017       return ret;
4018     }
4019   } else {
4020     *penv = NULL;
4021     ret = JNI_EDETACHED;
4022     return ret;
4023   }
4024 }
4025 
4026 
jni_AttachCurrentThreadAsDaemon(JavaVM * vm,void ** penv,void * _args)4027 jint JNICALL jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *_args) {
4028   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_ENTRY(vm, penv, _args);
4029   if (vm_created == 0) {
4030   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN((uint32_t) JNI_ERR);
4031     return JNI_ERR;
4032   }
4033 
4034   jint ret = attach_current_thread(vm, penv, _args, true);
4035   HOTSPOT_JNI_ATTACHCURRENTTHREADASDAEMON_RETURN(ret);
4036   return ret;
4037 }
4038 
4039 
4040 } // End extern "C"
4041 
4042 const struct JNIInvokeInterface_ jni_InvokeInterface = {
4043     NULL,
4044     NULL,
4045     NULL,
4046 
4047     jni_DestroyJavaVM,
4048     jni_AttachCurrentThread,
4049     jni_DetachCurrentThread,
4050     jni_GetEnv,
4051     jni_AttachCurrentThreadAsDaemon
4052 };
4053