< prev index next >

src/hotspot/share/prims/jvm.cpp

Print this page

  41 #include "classfile/modules.hpp"
  42 #include "classfile/packageEntry.hpp"
  43 #include "classfile/stringTable.hpp"
  44 #include "classfile/symbolTable.hpp"
  45 #include "classfile/systemDictionary.hpp"
  46 #include "classfile/vmClasses.hpp"
  47 #include "classfile/vmSymbols.hpp"
  48 #include "gc/shared/collectedHeap.inline.hpp"
  49 #include "interpreter/bytecode.hpp"
  50 #include "interpreter/bytecodeUtils.hpp"
  51 #include "jfr/jfrEvents.hpp"
  52 #include "jvm.h"
  53 #include "logging/log.hpp"
  54 #include "memory/oopFactory.hpp"
  55 #include "memory/referenceType.hpp"
  56 #include "memory/resourceArea.hpp"
  57 #include "memory/universe.hpp"
  58 #include "oops/access.inline.hpp"
  59 #include "oops/constantPool.hpp"
  60 #include "oops/fieldStreams.inline.hpp"

  61 #include "oops/instanceKlass.hpp"
  62 #include "oops/klass.inline.hpp"
  63 #include "oops/method.hpp"
  64 #include "oops/recordComponent.hpp"
  65 #include "oops/objArrayKlass.hpp"
  66 #include "oops/objArrayOop.inline.hpp"
  67 #include "oops/oop.inline.hpp"

  68 #include "prims/foreignGlobals.hpp"
  69 #include "prims/jvm_misc.hpp"
  70 #include "prims/jvmtiExport.hpp"
  71 #include "prims/jvmtiThreadState.inline.hpp"
  72 #include "prims/stackwalk.hpp"
  73 #include "runtime/arguments.hpp"
  74 #include "runtime/atomic.hpp"
  75 #include "runtime/continuation.hpp"
  76 #include "runtime/globals_extension.hpp"
  77 #include "runtime/handles.inline.hpp"
  78 #include "runtime/init.hpp"
  79 #include "runtime/interfaceSupport.inline.hpp"
  80 #include "runtime/deoptimization.hpp"
  81 #include "runtime/handshake.hpp"
  82 #include "runtime/java.hpp"
  83 #include "runtime/javaCalls.hpp"
  84 #include "runtime/javaThread.hpp"
  85 #include "runtime/jfieldIDWorkaround.hpp"
  86 #include "runtime/jniHandles.inline.hpp"
  87 #include "runtime/os.inline.hpp"

 396 
 397   return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
 398 JVM_END
 399 
 400 
 401 /*
 402  * Return the temporary directory that the VM uses for the attach
 403  * and perf data files.
 404  *
 405  * It is important that this directory is well-known and the
 406  * same for all VM instances. It cannot be affected by configuration
 407  * variables such as java.io.tmpdir.
 408  */
 409 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
 410   HandleMark hm(THREAD);
 411   const char* temp_dir = os::get_temp_directory();
 412   Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
 413   return (jstring) JNIHandles::make_local(THREAD, h());
 414 JVM_END
 415 




















































































































































 416 
 417 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
 418 
 419 extern volatile jint vm_created;
 420 
 421 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
 422   EventShutdown event;
 423   if (event.should_commit()) {
 424     event.set_reason("Shutdown requested from Java");
 425     event.commit();
 426   }
 427 JVM_END
 428 
 429 
 430 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
 431   before_exit(thread, true);
 432   vm_exit(code);
 433 JVM_END
 434 
 435 

 604 
 605   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 606   return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
 607                                   start_index, frames_array_h, THREAD);
 608 JVM_END
 609 
 610 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
 611   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 612   objArrayHandle frames_array_h(THREAD, fa);
 613   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 614   Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
 615 
 616   StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
 617 JVM_END
 618 
 619 // java.lang.Object ///////////////////////////////////////////////
 620 
 621 
 622 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
 623   // as implemented in the classic virtual machine; return 0 if object is null
 624   return handle == nullptr ? 0 :
 625          checked_cast<jint>(ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)));




















 626 JVM_END
 627 
 628 
 629 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
 630   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 631   ObjectSynchronizer::wait(obj, ms, CHECK);
 632 JVM_END
 633 
 634 
 635 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
 636   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 637   ObjectSynchronizer::notify(obj, CHECK);
 638 JVM_END
 639 
 640 
 641 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
 642   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 643   ObjectSynchronizer::notifyall(obj, CHECK);
 644 JVM_END
 645 

 653   // Just checking that the cloneable flag is set correct
 654   if (obj->is_array()) {
 655     guarantee(klass->is_cloneable(), "all arrays are cloneable");
 656   } else {
 657     guarantee(obj->is_instance(), "should be instanceOop");
 658     bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
 659     guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
 660   }
 661 #endif
 662 
 663   // Check if class of obj supports the Cloneable interface.
 664   // All arrays are considered to be cloneable (See JLS 20.1.5).
 665   // All j.l.r.Reference classes are considered non-cloneable.
 666   if (!klass->is_cloneable() ||
 667       (klass->is_instance_klass() &&
 668        InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
 669     ResourceMark rm(THREAD);
 670     THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
 671   }
 672 






 673   // Make shallow object copy
 674   const size_t size = obj->size();
 675   oop new_obj_oop = nullptr;
 676   if (obj->is_array()) {
 677     const int length = ((arrayOop)obj())->length();
 678     new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
 679                                                    /* do_zero */ true, CHECK_NULL);
 680   } else {
 681     new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
 682   }
 683 
 684   HeapAccess<>::clone(obj(), new_obj_oop, size);
 685 
 686   Handle new_obj(THREAD, new_obj_oop);
 687   // Caution: this involves a java upcall, so the clone should be
 688   // "gc-robust" by this stage.
 689   if (klass->has_finalizer()) {
 690     assert(obj->is_instance(), "should be instanceOop");
 691     new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
 692     new_obj = Handle(THREAD, new_obj_oop);

1147   oop result = java_lang_Class::name(java_class, CHECK_NULL);
1148   return (jstring) JNIHandles::make_local(THREAD, result);
1149 JVM_END
1150 
1151 
1152 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1153   JvmtiVMObjectAllocEventCollector oam;
1154   oop mirror = JNIHandles::resolve_non_null(cls);
1155 
1156   // Special handling for primitive objects
1157   if (java_lang_Class::is_primitive(mirror)) {
1158     // Primitive objects does not have any interfaces
1159     objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1160     return (jobjectArray) JNIHandles::make_local(THREAD, r);
1161   }
1162 
1163   Klass* klass = java_lang_Class::as_Klass(mirror);
1164   // Figure size of result array
1165   int size;
1166   if (klass->is_instance_klass()) {
1167     size = InstanceKlass::cast(klass)->local_interfaces()->length();

1168   } else {
1169     assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1170     size = 2;
1171   }
1172 
1173   // Allocate result array
1174   objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1175   objArrayHandle result (THREAD, r);
1176   // Fill in result
1177   if (klass->is_instance_klass()) {
1178     // Regular instance klass, fill in all local interfaces
1179     for (int index = 0; index < size; index++) {
1180       Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);

1181       result->obj_at_put(index, k->java_mirror());
1182     }
1183   } else {
1184     // All arrays implement java.lang.Cloneable and java.io.Serializable
1185     result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1186     result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1187   }
1188   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1189 JVM_END
1190 
1191 
1192 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1193   oop mirror = JNIHandles::resolve_non_null(cls);
1194   if (java_lang_Class::is_primitive(mirror)) {
1195     return JNI_FALSE;
1196   }
1197   Klass* k = java_lang_Class::as_Klass(mirror);
1198   return k->is_hidden();
1199 JVM_END
1200 
1201 
1202 class ScopedValueBindingsResolver {
1203 public:
1204   InstanceKlass* Carrier_klass;
1205   ScopedValueBindingsResolver(JavaThread* THREAD) {
1206     Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1207     Carrier_klass = InstanceKlass::cast(k);
1208   }
1209 };
1210 
1211 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1212   ResourceMark rm(THREAD);
1213   GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1214   JvmtiVMObjectAllocEventCollector oam;
1215 
1216   static ScopedValueBindingsResolver resolver(THREAD);
1217 
1218   // Iterate through Java frames
1219   vframeStream vfst(thread);
1220   for(; !vfst.at_end(); vfst.next()) {
1221     int loc = -1;

1665   }
1666 
1667   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1668 
1669   // Ensure class is linked
1670   k->link_class(CHECK_NULL);
1671 
1672   Array<Method*>* methods = k->methods();
1673   int methods_length = methods->length();
1674 
1675   // Save original method_idnum in case of redefinition, which can change
1676   // the idnum of obsolete methods.  The new method will have the same idnum
1677   // but if we refresh the methods array, the counts will be wrong.
1678   ResourceMark rm(THREAD);
1679   GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1680   int num_methods = 0;
1681 
1682   // Select methods matching the criteria.
1683   for (int i = 0; i < methods_length; i++) {
1684     Method* method = methods->at(i);
1685     if (want_constructor && !method->is_object_initializer()) {
1686       continue;
1687     }
1688     if (!want_constructor &&
1689         (method->is_object_initializer() || method->is_static_initializer() ||
1690          method->is_overpass())) {
1691       continue;
1692     }
1693     if (publicOnly && !method->is_public()) {
1694       continue;
1695     }
1696     idnums->push(method->method_idnum());
1697     ++num_methods;
1698   }
1699 
1700   // Allocate result
1701   objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1702   objArrayHandle result (THREAD, r);
1703 
1704   // Now just put the methods that we selected above, but go by their idnum
1705   // in case of redefinition.  The methods can be redefined at any safepoint,
1706   // so above when allocating the oop array and below when creating reflect
1707   // objects.
1708   for (int i = 0; i < num_methods; i++) {
1709     methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1710     if (method.is_null()) {
1711       // Method may have been deleted and seems this API can handle null
1712       // Otherwise should probably put a method that throws NSME
1713       result->obj_at_put(i, nullptr);
1714     } else {
1715       oop m;
1716       if (want_constructor) {

1717         m = Reflection::new_constructor(method, CHECK_NULL);
1718       } else {
1719         m = Reflection::new_method(method, false, CHECK_NULL);
1720       }
1721       result->obj_at_put(i, m);
1722     }
1723   }
1724 
1725   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1726 }
1727 
1728 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1729 {
1730   return get_class_declared_methods_helper(env, ofClass, publicOnly,
1731                                            /*want_constructor*/ false,
1732                                            vmClasses::reflect_Method_klass(), THREAD);
1733 }
1734 JVM_END
1735 
1736 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))

1966   constantTag tag = cp->tag_at(index);
1967   if (!tag.is_method() && !tag.is_interface_method()) {
1968     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1969   }
1970   int klass_ref  = cp->uncached_klass_ref_index_at(index);
1971   Klass* k_o;
1972   if (force_resolution) {
1973     k_o = cp->klass_at(klass_ref, CHECK_NULL);
1974   } else {
1975     k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
1976     if (k_o == nullptr) return nullptr;
1977   }
1978   InstanceKlass* k = InstanceKlass::cast(k_o);
1979   Symbol* name = cp->uncached_name_ref_at(index);
1980   Symbol* sig  = cp->uncached_signature_ref_at(index);
1981   methodHandle m (THREAD, k->find_method(name, sig));
1982   if (m.is_null()) {
1983     THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1984   }
1985   oop method;
1986   if (m->is_object_initializer()) {
1987     method = Reflection::new_constructor(m, CHECK_NULL);
1988   } else {
1989     // new_method accepts <clinit> as Method here
1990     method = Reflection::new_method(m, true, CHECK_NULL);
1991   }
1992   return JNIHandles::make_local(THREAD, method);
1993 }
1994 
1995 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
1996 {
1997   JvmtiVMObjectAllocEventCollector oam;
1998   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
1999   bounds_check(cp, index, CHECK_NULL);
2000   jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2001   return res;
2002 }
2003 JVM_END
2004 
2005 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2006 {
2007   JvmtiVMObjectAllocEventCollector oam;
2008   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2009   bounds_check(cp, index, CHECK_NULL);

2419   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2420   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2421   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2422   return method->size_of_parameters();
2423 JVM_END
2424 
2425 
2426 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2427   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2428   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2429   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2430   return method->verifier_max_stack();
2431 JVM_END
2432 
2433 
2434 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2435   ResourceMark rm(THREAD);
2436   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2437   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2438   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2439   return method->name() == vmSymbols::object_initializer_name();
2440 JVM_END
2441 
2442 
2443 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2444   ResourceMark rm(THREAD);
2445   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2446   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2447   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2448   return method->is_overpass();
2449 JVM_END
2450 
2451 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2452   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2453   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2454   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2455   return method->name()->as_utf8();
2456 JVM_END
2457 
2458 
2459 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))

3228 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3229   void* find_result = os::dll_lookup(handle, name);
3230   log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3231                     find_result != nullptr ? "Found" : "Failed to find",
3232                     name, p2i(handle));
3233   return find_result;
3234 JVM_END
3235 
3236 
3237 // JNI version ///////////////////////////////////////////////////////////////////////////////
3238 
3239 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3240   return Threads::is_supported_jni_version_including_1_1(version);
3241 JVM_END
3242 
3243 
3244 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3245   return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3246 JVM_END
3247 




3248 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3249   return VMContinuations ? JNI_TRUE : JNI_FALSE;
3250 JVM_END
3251 
3252 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3253   return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3254 JVM_END
3255 
3256 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3257   return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3258 JVM_END
3259 
3260 // String support ///////////////////////////////////////////////////////////////////////////
3261 
3262 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3263   JvmtiVMObjectAllocEventCollector oam;
3264   if (str == nullptr) return nullptr;
3265   oop string = JNIHandles::resolve_non_null(str);
3266   oop result = StringTable::intern(string, CHECK_NULL);
3267   return (jstring) JNIHandles::make_local(THREAD, result);

3307 
3308 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3309                                     Handle loader, jboolean throwError, TRAPS) {
3310   Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3311 
3312   // Check if we should initialize the class
3313   if (init && klass->is_instance_klass()) {
3314     klass->initialize(CHECK_NULL);
3315   }
3316   return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3317 }
3318 
3319 
3320 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3321 
3322 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3323   Handle method_handle;
3324   if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3325     method_handle = Handle(THREAD, JNIHandles::resolve(method));
3326     Handle receiver(THREAD, JNIHandles::resolve(obj));
3327     objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));


3328     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3329     jobject res = JNIHandles::make_local(THREAD, result);
3330     if (JvmtiExport::should_post_vm_object_alloc()) {
3331       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3332       assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3333       if (java_lang_Class::is_primitive(ret_type)) {
3334         // Only for primitive type vm allocates memory for java object.
3335         // See box() method.
3336         JvmtiExport::post_vm_object_alloc(thread, result);
3337       }
3338     }
3339     return res;
3340   } else {
3341     THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3342   }
3343 JVM_END
3344 
3345 
3346 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))


3347   oop constructor_mirror = JNIHandles::resolve(c);
3348   objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3349   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3350   jobject res = JNIHandles::make_local(THREAD, result);
3351   if (JvmtiExport::should_post_vm_object_alloc()) {
3352     JvmtiExport::post_vm_object_alloc(thread, result);
3353   }
3354   return res;
3355 JVM_END
3356 
3357 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3358   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3359   HeapShared::initialize_from_archived_subgraph(THREAD, k);
3360 JVM_END
3361 
3362 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3363                                               jclass caller,
3364                                               jstring interfaceMethodName,
3365                                               jobject factoryType,
3366                                               jobject interfaceMethodType,
3367                                               jobject implementationMember,
3368                                               jobject dynamicMethodType,

3568   JvmtiVMObjectAllocEventCollector oam;
3569 
3570   // Check if threads is null
3571   if (threads == nullptr) {
3572     THROW_NULL(vmSymbols::java_lang_NullPointerException());
3573   }
3574 
3575   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3576   objArrayHandle ah(THREAD, a);
3577   int num_threads = ah->length();
3578   // check if threads is non-empty array
3579   if (num_threads == 0) {
3580     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3581   }
3582 
3583   // check if threads is not an array of objects of Thread class
3584   Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3585   if (k != vmClasses::Thread_klass()) {
3586     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3587   }

3588 
3589   ResourceMark rm(THREAD);
3590 
3591   GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3592   for (int i = 0; i < num_threads; i++) {
3593     oop thread_obj = ah->obj_at(i);
3594     instanceHandle h(THREAD, (instanceOop) thread_obj);
3595     thread_handle_array->append(h);
3596   }
3597 
3598   // The JavaThread references in thread_handle_array are validated
3599   // in VM_ThreadDump::doit().
3600   Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3601   return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3602 
3603 JVM_END
3604 
3605 // JVM monitoring and management support
3606 JVM_LEAF(void*, JVM_GetManagement(jint version))
3607   return Management::get_jmm_interface(version);
3608 JVM_END
3609 
3610 // com.sun.tools.attach.VirtualMachine agent properties support
3611 //
3612 // Initialize the agent properties with the properties maintained in the VM
3613 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))

  41 #include "classfile/modules.hpp"
  42 #include "classfile/packageEntry.hpp"
  43 #include "classfile/stringTable.hpp"
  44 #include "classfile/symbolTable.hpp"
  45 #include "classfile/systemDictionary.hpp"
  46 #include "classfile/vmClasses.hpp"
  47 #include "classfile/vmSymbols.hpp"
  48 #include "gc/shared/collectedHeap.inline.hpp"
  49 #include "interpreter/bytecode.hpp"
  50 #include "interpreter/bytecodeUtils.hpp"
  51 #include "jfr/jfrEvents.hpp"
  52 #include "jvm.h"
  53 #include "logging/log.hpp"
  54 #include "memory/oopFactory.hpp"
  55 #include "memory/referenceType.hpp"
  56 #include "memory/resourceArea.hpp"
  57 #include "memory/universe.hpp"
  58 #include "oops/access.inline.hpp"
  59 #include "oops/constantPool.hpp"
  60 #include "oops/fieldStreams.inline.hpp"
  61 #include "oops/flatArrayKlass.hpp"
  62 #include "oops/instanceKlass.hpp"
  63 #include "oops/klass.inline.hpp"
  64 #include "oops/method.hpp"
  65 #include "oops/recordComponent.hpp"
  66 #include "oops/objArrayKlass.hpp"
  67 #include "oops/objArrayOop.inline.hpp"
  68 #include "oops/oop.inline.hpp"
  69 #include "oops/refArrayOop.inline.hpp"
  70 #include "prims/foreignGlobals.hpp"
  71 #include "prims/jvm_misc.hpp"
  72 #include "prims/jvmtiExport.hpp"
  73 #include "prims/jvmtiThreadState.inline.hpp"
  74 #include "prims/stackwalk.hpp"
  75 #include "runtime/arguments.hpp"
  76 #include "runtime/atomic.hpp"
  77 #include "runtime/continuation.hpp"
  78 #include "runtime/globals_extension.hpp"
  79 #include "runtime/handles.inline.hpp"
  80 #include "runtime/init.hpp"
  81 #include "runtime/interfaceSupport.inline.hpp"
  82 #include "runtime/deoptimization.hpp"
  83 #include "runtime/handshake.hpp"
  84 #include "runtime/java.hpp"
  85 #include "runtime/javaCalls.hpp"
  86 #include "runtime/javaThread.hpp"
  87 #include "runtime/jfieldIDWorkaround.hpp"
  88 #include "runtime/jniHandles.inline.hpp"
  89 #include "runtime/os.inline.hpp"

 398 
 399   return (jobjectArray) JNIHandles::make_local(THREAD, result_h());
 400 JVM_END
 401 
 402 
 403 /*
 404  * Return the temporary directory that the VM uses for the attach
 405  * and perf data files.
 406  *
 407  * It is important that this directory is well-known and the
 408  * same for all VM instances. It cannot be affected by configuration
 409  * variables such as java.io.tmpdir.
 410  */
 411 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
 412   HandleMark hm(THREAD);
 413   const char* temp_dir = os::get_temp_directory();
 414   Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
 415   return (jstring) JNIHandles::make_local(THREAD, h());
 416 JVM_END
 417 
 418 static void validate_array_arguments(Klass* elmClass, jint len, TRAPS) {
 419   if (len < 0) {
 420     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Array length is negative");
 421   }
 422   elmClass->initialize(CHECK);
 423   if (elmClass->is_array_klass() || elmClass->is_identity_class()) {
 424     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is not a value class");
 425   }
 426   if (elmClass->is_abstract()) {
 427     THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Element class is abstract");
 428   }
 429 }
 430 
 431 JVM_ENTRY(jarray, JVM_CopyOfSpecialArray(JNIEnv *env, jarray orig, jint from, jint to))
 432   oop o = JNIHandles::resolve_non_null(orig);
 433   assert(o->is_array(), "Must be");
 434   oop array = nullptr;
 435   arrayOop org = (arrayOop)o;
 436   arrayHandle oh(THREAD, org);
 437   ObjArrayKlass* ak = ObjArrayKlass::cast(org->klass());
 438   InlineKlass* vk = InlineKlass::cast(ak->element_klass());
 439   int len = to - from;  // length of the new array
 440   if (ak->is_null_free_array_klass()) {
 441     if ((len != 0) && (from >= org->length() || to > org->length())) {
 442       THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Copying of null-free array with uninitialized elements");
 443     }
 444   }
 445   if (org->is_flatArray()) {
 446     FlatArrayKlass* fak = FlatArrayKlass::cast(org->klass());
 447     LayoutKind lk = fak->layout_kind();
 448     ArrayKlass::ArrayProperties props = ArrayKlass::ArrayProperties::DEFAULT;
 449     switch(lk) {
 450       case LayoutKind::ATOMIC_FLAT:
 451         props = ArrayKlass::ArrayProperties::NULL_RESTRICTED;
 452       break;
 453       case LayoutKind::NON_ATOMIC_FLAT:
 454         props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::NULL_RESTRICTED | ArrayKlass::ArrayProperties::NON_ATOMIC);
 455       break;
 456       case LayoutKind::NULLABLE_ATOMIC_FLAT:
 457       props = ArrayKlass::ArrayProperties::NON_ATOMIC;
 458       break;
 459       default:
 460         ShouldNotReachHere();
 461     }
 462     array = oopFactory::new_flatArray(vk, len, props, lk, CHECK_NULL);
 463     arrayHandle ah(THREAD, (arrayOop)array);
 464     int end = to < oh()->length() ? to : oh()->length();
 465     for (int i = from; i < end; i++) {
 466       void* src = ((flatArrayOop)oh())->value_at_addr(i, fak->layout_helper());
 467       void* dst = ((flatArrayOop)ah())->value_at_addr(i - from, fak->layout_helper());
 468       vk->copy_payload_to_addr(src, dst, lk, false);
 469     }
 470     array = ah();
 471   } else {
 472     ArrayKlass::ArrayProperties props = org->is_null_free_array() ? ArrayKlass::ArrayProperties::NULL_RESTRICTED : ArrayKlass::ArrayProperties::DEFAULT;
 473     array = oopFactory::new_objArray(vk, len, props,  CHECK_NULL);
 474     int end = to < oh()->length() ? to : oh()->length();
 475     for (int i = from; i < end; i++) {
 476       if (i < ((objArrayOop)oh())->length()) {
 477         ((objArrayOop)array)->obj_at_put(i - from, ((objArrayOop)oh())->obj_at(i));
 478       } else {
 479         assert(!ak->is_null_free_array_klass(), "Must be a nullable array");
 480         ((objArrayOop)array)->obj_at_put(i - from, nullptr);
 481       }
 482     }
 483   }
 484   return (jarray) JNIHandles::make_local(THREAD, array);
 485 JVM_END
 486 
 487 JVM_ENTRY(jarray, JVM_NewNullRestrictedNonAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
 488   oop mirror = JNIHandles::resolve_non_null(elmClass);
 489   oop init = JNIHandles::resolve(initVal);
 490   if (init == nullptr) {
 491     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Initial value cannot be null");
 492   }
 493   Handle init_h(THREAD, init);
 494   Klass* klass = java_lang_Class::as_Klass(mirror);
 495   if (klass != init_h()->klass()) {
 496     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Type mismatch between array and initial value");
 497   }
 498   validate_array_arguments(klass, len, CHECK_NULL);
 499   InlineKlass* vk = InlineKlass::cast(klass);
 500   ArrayKlass::ArrayProperties props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::NON_ATOMIC | ArrayKlass::ArrayProperties::NULL_RESTRICTED);
 501   objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
 502   for (int i = 0; i < len; i++) {
 503     array->obj_at_put(i, init_h() /*, CHECK_NULL*/ );
 504   }
 505   return (jarray) JNIHandles::make_local(THREAD, array);
 506 JVM_END
 507 
 508 JVM_ENTRY(jarray, JVM_NewNullRestrictedAtomicArray(JNIEnv *env, jclass elmClass, jint len, jobject initVal))
 509   oop mirror = JNIHandles::resolve_non_null(elmClass);
 510   oop init = JNIHandles::resolve(initVal);
 511   if (init == nullptr) {
 512     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Initial value cannot be null");
 513   }
 514   Handle init_h(THREAD, init);
 515   Klass* klass = java_lang_Class::as_Klass(mirror);
 516   if (klass != init_h()->klass()) {
 517     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Type mismatch between array and initial value");
 518   }
 519   validate_array_arguments(klass, len, CHECK_NULL);
 520   InlineKlass* vk = InlineKlass::cast(klass);
 521   ArrayKlass::ArrayProperties props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::NULL_RESTRICTED);
 522   objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
 523   for (int i = 0; i < len; i++) {
 524     array->obj_at_put(i, init_h() /*, CHECK_NULL*/ );
 525   }
 526   return (jarray) JNIHandles::make_local(THREAD, array);
 527 JVM_END
 528 
 529 JVM_ENTRY(jarray, JVM_NewNullableAtomicArray(JNIEnv *env, jclass elmClass, jint len))
 530   oop mirror = JNIHandles::resolve_non_null(elmClass);
 531   Klass* klass = java_lang_Class::as_Klass(mirror);
 532   klass->initialize(CHECK_NULL);
 533   validate_array_arguments(klass, len, CHECK_NULL);
 534   InlineKlass* vk = InlineKlass::cast(klass);
 535   ArrayKlass::ArrayProperties props = (ArrayKlass::ArrayProperties)(ArrayKlass::ArrayProperties::DEFAULT);
 536   objArrayOop array = oopFactory::new_objArray(klass, len, props, CHECK_NULL);
 537   return (jarray) JNIHandles::make_local(THREAD, array);
 538 JVM_END
 539 
 540 JVM_ENTRY(jboolean, JVM_IsFlatArray(JNIEnv *env, jobject obj))
 541   arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
 542   return oop->is_flatArray();
 543 JVM_END
 544 
 545 JVM_ENTRY(jboolean, JVM_IsNullRestrictedArray(JNIEnv *env, jobject obj))
 546   arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
 547   return oop->is_null_free_array();
 548 JVM_END
 549 
 550 JVM_ENTRY(jboolean, JVM_IsAtomicArray(JNIEnv *env, jobject obj))
 551   // There are multiple cases where an array can/must support atomic access:
 552   //   - the array is a reference array
 553   //   - the array uses an atomic flat layout: NULLABLE_ATOMIC_FLAT or ATOMIC_FLAT
 554   //   - the array is flat and its component type is naturally atomic
 555   arrayOop oop = arrayOop(JNIHandles::resolve_non_null(obj));
 556   if (oop->is_refArray()) return true;
 557   if (oop->is_flatArray()) {
 558     FlatArrayKlass* fak = FlatArrayKlass::cast(oop->klass());
 559     if (fak->layout_kind() == LayoutKind::ATOMIC_FLAT || fak->layout_kind() == LayoutKind::NULLABLE_ATOMIC_FLAT) {
 560       return true;
 561     }
 562     if (fak->element_klass()->is_naturally_atomic()) return true;
 563   }
 564   return false;
 565 JVM_END
 566 
 567 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
 568 
 569 extern volatile jint vm_created;
 570 
 571 JVM_ENTRY_NO_ENV(void, JVM_BeforeHalt())
 572   EventShutdown event;
 573   if (event.should_commit()) {
 574     event.set_reason("Shutdown requested from Java");
 575     event.commit();
 576   }
 577 JVM_END
 578 
 579 
 580 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
 581   before_exit(thread, true);
 582   vm_exit(code);
 583 JVM_END
 584 
 585 

 754 
 755   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 756   return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, last_batch_count, buffer_size,
 757                                   start_index, frames_array_h, THREAD);
 758 JVM_END
 759 
 760 JVM_ENTRY(void, JVM_SetStackWalkContinuation(JNIEnv *env, jobject stackStream, jlong anchor, jobjectArray frames, jobject cont))
 761   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 762   objArrayHandle frames_array_h(THREAD, fa);
 763   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 764   Handle cont_h(THREAD, JNIHandles::resolve_non_null(cont));
 765 
 766   StackWalk::setContinuation(stackStream_h, anchor, frames_array_h, cont_h, THREAD);
 767 JVM_END
 768 
 769 // java.lang.Object ///////////////////////////////////////////////
 770 
 771 
 772 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
 773   // as implemented in the classic virtual machine; return 0 if object is null
 774   if (handle == nullptr) {
 775     return 0;
 776   }
 777   oop obj = JNIHandles::resolve_non_null(handle);
 778   if (EnableValhalla && obj->klass()->is_inline_klass()) {
 779       JavaValue result(T_INT);
 780       JavaCallArguments args;
 781       Handle ho(THREAD, obj);
 782       args.push_oop(ho);
 783       methodHandle method(THREAD, Universe::value_object_hash_code_method());
 784       JavaCalls::call(&result, method, &args, THREAD);
 785       if (HAS_PENDING_EXCEPTION) {
 786         if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
 787           Handle e(THREAD, PENDING_EXCEPTION);
 788           CLEAR_PENDING_EXCEPTION;
 789           THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
 790         }
 791       }
 792       return result.get_jint();
 793   } else {
 794     return checked_cast<jint>(ObjectSynchronizer::FastHashCode(THREAD, obj));
 795   }
 796 JVM_END
 797 
 798 
 799 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
 800   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 801   ObjectSynchronizer::wait(obj, ms, CHECK);
 802 JVM_END
 803 
 804 
 805 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
 806   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 807   ObjectSynchronizer::notify(obj, CHECK);
 808 JVM_END
 809 
 810 
 811 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
 812   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 813   ObjectSynchronizer::notifyall(obj, CHECK);
 814 JVM_END
 815 

 823   // Just checking that the cloneable flag is set correct
 824   if (obj->is_array()) {
 825     guarantee(klass->is_cloneable(), "all arrays are cloneable");
 826   } else {
 827     guarantee(obj->is_instance(), "should be instanceOop");
 828     bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
 829     guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
 830   }
 831 #endif
 832 
 833   // Check if class of obj supports the Cloneable interface.
 834   // All arrays are considered to be cloneable (See JLS 20.1.5).
 835   // All j.l.r.Reference classes are considered non-cloneable.
 836   if (!klass->is_cloneable() ||
 837       (klass->is_instance_klass() &&
 838        InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
 839     ResourceMark rm(THREAD);
 840     THROW_MSG_NULL(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
 841   }
 842 
 843   if (klass->is_inline_klass()) {
 844     // Value instances have no identity, so return the current instance instead of allocating a new one
 845     // Value classes cannot have finalizers, so the method can return immediately
 846     return JNIHandles::make_local(THREAD, obj());
 847   }
 848 
 849   // Make shallow object copy
 850   const size_t size = obj->size();
 851   oop new_obj_oop = nullptr;
 852   if (obj->is_array()) {
 853     const int length = ((arrayOop)obj())->length();
 854     new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
 855                                                    /* do_zero */ true, CHECK_NULL);
 856   } else {
 857     new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
 858   }
 859 
 860   HeapAccess<>::clone(obj(), new_obj_oop, size);
 861 
 862   Handle new_obj(THREAD, new_obj_oop);
 863   // Caution: this involves a java upcall, so the clone should be
 864   // "gc-robust" by this stage.
 865   if (klass->has_finalizer()) {
 866     assert(obj->is_instance(), "should be instanceOop");
 867     new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
 868     new_obj = Handle(THREAD, new_obj_oop);

1323   oop result = java_lang_Class::name(java_class, CHECK_NULL);
1324   return (jstring) JNIHandles::make_local(THREAD, result);
1325 JVM_END
1326 
1327 
1328 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1329   JvmtiVMObjectAllocEventCollector oam;
1330   oop mirror = JNIHandles::resolve_non_null(cls);
1331 
1332   // Special handling for primitive objects
1333   if (java_lang_Class::is_primitive(mirror)) {
1334     // Primitive objects does not have any interfaces
1335     objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1336     return (jobjectArray) JNIHandles::make_local(THREAD, r);
1337   }
1338 
1339   Klass* klass = java_lang_Class::as_Klass(mirror);
1340   // Figure size of result array
1341   int size;
1342   if (klass->is_instance_klass()) {
1343     InstanceKlass* ik = InstanceKlass::cast(klass);
1344     size = ik->local_interfaces()->length();
1345   } else {
1346     assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1347     size = 2;
1348   }
1349 
1350   // Allocate result array
1351   objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1352   objArrayHandle result (THREAD, r);
1353   // Fill in result
1354   if (klass->is_instance_klass()) {
1355     // Regular instance klass, fill in all local interfaces
1356     for (int index = 0; index < size; index++) {
1357       InstanceKlass* ik = InstanceKlass::cast(klass);
1358       Klass* k = ik->local_interfaces()->at(index);
1359       result->obj_at_put(index, k->java_mirror());
1360     }
1361   } else {
1362     // All arrays implement java.lang.Cloneable and java.io.Serializable
1363     result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1364     result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1365   }
1366   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1367 JVM_END
1368 
1369 
1370 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1371   oop mirror = JNIHandles::resolve_non_null(cls);
1372   if (java_lang_Class::is_primitive(mirror)) {
1373     return JNI_FALSE;
1374   }
1375   Klass* k = java_lang_Class::as_Klass(mirror);
1376   return k->is_hidden();
1377 JVM_END
1378 

1379 class ScopedValueBindingsResolver {
1380 public:
1381   InstanceKlass* Carrier_klass;
1382   ScopedValueBindingsResolver(JavaThread* THREAD) {
1383     Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1384     Carrier_klass = InstanceKlass::cast(k);
1385   }
1386 };
1387 
1388 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1389   ResourceMark rm(THREAD);
1390   GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1391   JvmtiVMObjectAllocEventCollector oam;
1392 
1393   static ScopedValueBindingsResolver resolver(THREAD);
1394 
1395   // Iterate through Java frames
1396   vframeStream vfst(thread);
1397   for(; !vfst.at_end(); vfst.next()) {
1398     int loc = -1;

1842   }
1843 
1844   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(ofMirror));
1845 
1846   // Ensure class is linked
1847   k->link_class(CHECK_NULL);
1848 
1849   Array<Method*>* methods = k->methods();
1850   int methods_length = methods->length();
1851 
1852   // Save original method_idnum in case of redefinition, which can change
1853   // the idnum of obsolete methods.  The new method will have the same idnum
1854   // but if we refresh the methods array, the counts will be wrong.
1855   ResourceMark rm(THREAD);
1856   GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1857   int num_methods = 0;
1858 
1859   // Select methods matching the criteria.
1860   for (int i = 0; i < methods_length; i++) {
1861     Method* method = methods->at(i);
1862     if (want_constructor && !method->is_object_constructor()) {
1863       continue;
1864     }
1865     if (!want_constructor &&
1866         (method->is_object_constructor() || method->is_class_initializer() ||
1867          method->is_overpass())) {
1868       continue;
1869     }
1870     if (publicOnly && !method->is_public()) {
1871       continue;
1872     }
1873     idnums->push(method->method_idnum());
1874     ++num_methods;
1875   }
1876 
1877   // Allocate result
1878   objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1879   objArrayHandle result (THREAD, r);
1880 
1881   // Now just put the methods that we selected above, but go by their idnum
1882   // in case of redefinition.  The methods can be redefined at any safepoint,
1883   // so above when allocating the oop array and below when creating reflect
1884   // objects.
1885   for (int i = 0; i < num_methods; i++) {
1886     methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1887     if (method.is_null()) {
1888       // Method may have been deleted and seems this API can handle null
1889       // Otherwise should probably put a method that throws NSME
1890       result->obj_at_put(i, nullptr);
1891     } else {
1892       oop m;
1893       if (want_constructor) {
1894         assert(method->is_object_constructor(), "must be");
1895         m = Reflection::new_constructor(method, CHECK_NULL);
1896       } else {
1897         m = Reflection::new_method(method, false, CHECK_NULL);
1898       }
1899       result->obj_at_put(i, m);
1900     }
1901   }
1902 
1903   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1904 }
1905 
1906 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1907 {
1908   return get_class_declared_methods_helper(env, ofClass, publicOnly,
1909                                            /*want_constructor*/ false,
1910                                            vmClasses::reflect_Method_klass(), THREAD);
1911 }
1912 JVM_END
1913 
1914 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))

2144   constantTag tag = cp->tag_at(index);
2145   if (!tag.is_method() && !tag.is_interface_method()) {
2146     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2147   }
2148   int klass_ref  = cp->uncached_klass_ref_index_at(index);
2149   Klass* k_o;
2150   if (force_resolution) {
2151     k_o = cp->klass_at(klass_ref, CHECK_NULL);
2152   } else {
2153     k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2154     if (k_o == nullptr) return nullptr;
2155   }
2156   InstanceKlass* k = InstanceKlass::cast(k_o);
2157   Symbol* name = cp->uncached_name_ref_at(index);
2158   Symbol* sig  = cp->uncached_signature_ref_at(index);
2159   methodHandle m (THREAD, k->find_method(name, sig));
2160   if (m.is_null()) {
2161     THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2162   }
2163   oop method;
2164   if (m->is_object_constructor()) {
2165     method = Reflection::new_constructor(m, CHECK_NULL);
2166   } else {

2167     method = Reflection::new_method(m, true, CHECK_NULL);
2168   }
2169   return JNIHandles::make_local(THREAD, method);
2170 }
2171 
2172 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2173 {
2174   JvmtiVMObjectAllocEventCollector oam;
2175   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2176   bounds_check(cp, index, CHECK_NULL);
2177   jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2178   return res;
2179 }
2180 JVM_END
2181 
2182 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2183 {
2184   JvmtiVMObjectAllocEventCollector oam;
2185   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2186   bounds_check(cp, index, CHECK_NULL);

2596   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2597   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2598   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2599   return method->size_of_parameters();
2600 JVM_END
2601 
2602 
2603 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2604   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2605   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2606   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2607   return method->verifier_max_stack();
2608 JVM_END
2609 
2610 
2611 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2612   ResourceMark rm(THREAD);
2613   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2614   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2615   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2616   return method->is_object_constructor();
2617 JVM_END
2618 
2619 
2620 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2621   ResourceMark rm(THREAD);
2622   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2623   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2624   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2625   return method->is_overpass();
2626 JVM_END
2627 
2628 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2629   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2630   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2631   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2632   return method->name()->as_utf8();
2633 JVM_END
2634 
2635 
2636 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))

3405 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3406   void* find_result = os::dll_lookup(handle, name);
3407   log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3408                     find_result != nullptr ? "Found" : "Failed to find",
3409                     name, p2i(handle));
3410   return find_result;
3411 JVM_END
3412 
3413 
3414 // JNI version ///////////////////////////////////////////////////////////////////////////////
3415 
3416 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3417   return Threads::is_supported_jni_version_including_1_1(version);
3418 JVM_END
3419 
3420 
3421 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3422   return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3423 JVM_END
3424 
3425 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3426   return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3427 JVM_END
3428 
3429 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3430   return VMContinuations ? JNI_TRUE : JNI_FALSE;
3431 JVM_END
3432 
3433 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3434   return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3435 JVM_END
3436 
3437 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3438   return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3439 JVM_END
3440 
3441 // String support ///////////////////////////////////////////////////////////////////////////
3442 
3443 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3444   JvmtiVMObjectAllocEventCollector oam;
3445   if (str == nullptr) return nullptr;
3446   oop string = JNIHandles::resolve_non_null(str);
3447   oop result = StringTable::intern(string, CHECK_NULL);
3448   return (jstring) JNIHandles::make_local(THREAD, result);

3488 
3489 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3490                                     Handle loader, jboolean throwError, TRAPS) {
3491   Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3492 
3493   // Check if we should initialize the class
3494   if (init && klass->is_instance_klass()) {
3495     klass->initialize(CHECK_NULL);
3496   }
3497   return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3498 }
3499 
3500 
3501 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3502 
3503 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3504   Handle method_handle;
3505   if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3506     method_handle = Handle(THREAD, JNIHandles::resolve(method));
3507     Handle receiver(THREAD, JNIHandles::resolve(obj));
3508     objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3509     assert(args() == nullptr || !args->is_flatArray(), "args are never flat or are they???");
3510 
3511     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3512     jobject res = JNIHandles::make_local(THREAD, result);
3513     if (JvmtiExport::should_post_vm_object_alloc()) {
3514       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3515       assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3516       if (java_lang_Class::is_primitive(ret_type)) {
3517         // Only for primitive type vm allocates memory for java object.
3518         // See box() method.
3519         JvmtiExport::post_vm_object_alloc(thread, result);
3520       }
3521     }
3522     return res;
3523   } else {
3524     THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3525   }
3526 JVM_END
3527 
3528 
3529 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3530   objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3531   assert(args() == nullptr || !args->is_flatArray(), "args are never flat or are they???");
3532   oop constructor_mirror = JNIHandles::resolve(c);

3533   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3534   jobject res = JNIHandles::make_local(THREAD, result);
3535   if (JvmtiExport::should_post_vm_object_alloc()) {
3536     JvmtiExport::post_vm_object_alloc(thread, result);
3537   }
3538   return res;
3539 JVM_END
3540 
3541 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3542   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3543   HeapShared::initialize_from_archived_subgraph(THREAD, k);
3544 JVM_END
3545 
3546 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3547                                               jclass caller,
3548                                               jstring interfaceMethodName,
3549                                               jobject factoryType,
3550                                               jobject interfaceMethodType,
3551                                               jobject implementationMember,
3552                                               jobject dynamicMethodType,

3752   JvmtiVMObjectAllocEventCollector oam;
3753 
3754   // Check if threads is null
3755   if (threads == nullptr) {
3756     THROW_NULL(vmSymbols::java_lang_NullPointerException());
3757   }
3758 
3759   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3760   objArrayHandle ah(THREAD, a);
3761   int num_threads = ah->length();
3762   // check if threads is non-empty array
3763   if (num_threads == 0) {
3764     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3765   }
3766 
3767   // check if threads is not an array of objects of Thread class
3768   Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3769   if (k != vmClasses::Thread_klass()) {
3770     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3771   }
3772   refArrayHandle rah(THREAD, (refArrayOop)ah()); // j.l.Thread is an identity class, arrays are always reference arrays
3773 
3774   ResourceMark rm(THREAD);
3775 
3776   GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3777   for (int i = 0; i < num_threads; i++) {
3778     oop thread_obj = rah->obj_at(i);
3779     instanceHandle h(THREAD, (instanceOop) thread_obj);
3780     thread_handle_array->append(h);
3781   }
3782 
3783   // The JavaThread references in thread_handle_array are validated
3784   // in VM_ThreadDump::doit().
3785   Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3786   return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3787 
3788 JVM_END
3789 
3790 // JVM monitoring and management support
3791 JVM_LEAF(void*, JVM_GetManagement(jint version))
3792   return Management::get_jmm_interface(version);
3793 JVM_END
3794 
3795 // com.sun.tools.attach.VirtualMachine agent properties support
3796 //
3797 // Initialize the agent properties with the properties maintained in the VM
3798 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
< prev index next >