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