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