< 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))

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

2432   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2433   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2434   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2435   return method->size_of_parameters();
2436 JVM_END
2437 
2438 
2439 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2440   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2441   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2442   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2443   return method->verifier_max_stack();
2444 JVM_END
2445 
2446 
2447 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2448   ResourceMark rm(THREAD);
2449   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2450   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2451   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2452   return method->name() == vmSymbols::object_initializer_name();
2453 JVM_END
2454 
2455 
2456 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2457   ResourceMark rm(THREAD);
2458   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2459   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2460   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2461   return method->is_overpass();
2462 JVM_END
2463 
2464 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2465   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2466   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2467   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2468   return method->name()->as_utf8();
2469 JVM_END
2470 
2471 
2472 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))

3240 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3241   void* find_result = os::dll_lookup(handle, name);
3242   log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3243                     find_result != nullptr ? "Found" : "Failed to find",
3244                     name, p2i(handle));
3245   return find_result;
3246 JVM_END
3247 
3248 
3249 // JNI version ///////////////////////////////////////////////////////////////////////////////
3250 
3251 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3252   return Threads::is_supported_jni_version_including_1_1(version);
3253 JVM_END
3254 
3255 
3256 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3257   return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3258 JVM_END
3259 




3260 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3261   return VMContinuations ? JNI_TRUE : JNI_FALSE;
3262 JVM_END
3263 
3264 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3265   return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3266 JVM_END
3267 
3268 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3269   return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3270 JVM_END
3271 
3272 // String support ///////////////////////////////////////////////////////////////////////////
3273 
3274 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3275   JvmtiVMObjectAllocEventCollector oam;
3276   if (str == nullptr) return nullptr;
3277   oop string = JNIHandles::resolve_non_null(str);
3278   oop result = StringTable::intern(string, CHECK_NULL);
3279   return (jstring) JNIHandles::make_local(THREAD, result);

3319 
3320 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3321                                     Handle loader, jboolean throwError, TRAPS) {
3322   Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3323 
3324   // Check if we should initialize the class
3325   if (init && klass->is_instance_klass()) {
3326     klass->initialize(CHECK_NULL);
3327   }
3328   return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3329 }
3330 
3331 
3332 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3333 
3334 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3335   Handle method_handle;
3336   if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3337     method_handle = Handle(THREAD, JNIHandles::resolve(method));
3338     Handle receiver(THREAD, JNIHandles::resolve(obj));
3339     objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));


3340     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3341     jobject res = JNIHandles::make_local(THREAD, result);
3342     if (JvmtiExport::should_post_vm_object_alloc()) {
3343       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3344       assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3345       if (java_lang_Class::is_primitive(ret_type)) {
3346         // Only for primitive type vm allocates memory for java object.
3347         // See box() method.
3348         JvmtiExport::post_vm_object_alloc(thread, result);
3349       }
3350     }
3351     return res;
3352   } else {
3353     THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3354   }
3355 JVM_END
3356 
3357 
3358 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))


3359   oop constructor_mirror = JNIHandles::resolve(c);
3360   objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3361   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3362   jobject res = JNIHandles::make_local(THREAD, result);
3363   if (JvmtiExport::should_post_vm_object_alloc()) {
3364     JvmtiExport::post_vm_object_alloc(thread, result);
3365   }
3366   return res;
3367 JVM_END
3368 
3369 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3370   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3371   HeapShared::initialize_from_archived_subgraph(THREAD, k);
3372 JVM_END
3373 
3374 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3375                                               jclass caller,
3376                                               jstring interfaceMethodName,
3377                                               jobject factoryType,
3378                                               jobject interfaceMethodType,
3379                                               jobject implementationMember,
3380                                               jobject dynamicMethodType,

3580   JvmtiVMObjectAllocEventCollector oam;
3581 
3582   // Check if threads is null
3583   if (threads == nullptr) {
3584     THROW_NULL(vmSymbols::java_lang_NullPointerException());
3585   }
3586 
3587   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3588   objArrayHandle ah(THREAD, a);
3589   int num_threads = ah->length();
3590   // check if threads is non-empty array
3591   if (num_threads == 0) {
3592     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3593   }
3594 
3595   // check if threads is not an array of objects of Thread class
3596   Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3597   if (k != vmClasses::Thread_klass()) {
3598     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3599   }

3600 
3601   ResourceMark rm(THREAD);
3602 
3603   GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3604   for (int i = 0; i < num_threads; i++) {
3605     oop thread_obj = ah->obj_at(i);
3606     instanceHandle h(THREAD, (instanceOop) thread_obj);
3607     thread_handle_array->append(h);
3608   }
3609 
3610   // The JavaThread references in thread_handle_array are validated
3611   // in VM_ThreadDump::doit().
3612   Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3613   return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3614 
3615 JVM_END
3616 
3617 // JVM monitoring and management support
3618 JVM_LEAF(void*, JVM_GetManagement(jint version))
3619   return Management::get_jmm_interface(version);
3620 JVM_END
3621 
3622 // com.sun.tools.attach.VirtualMachine agent properties support
3623 //
3624 // Initialize the agent properties with the properties maintained in the VM
3625 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))

2157   constantTag tag = cp->tag_at(index);
2158   if (!tag.is_method() && !tag.is_interface_method()) {
2159     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2160   }
2161   int klass_ref  = cp->uncached_klass_ref_index_at(index);
2162   Klass* k_o;
2163   if (force_resolution) {
2164     k_o = cp->klass_at(klass_ref, CHECK_NULL);
2165   } else {
2166     k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2167     if (k_o == nullptr) return nullptr;
2168   }
2169   InstanceKlass* k = InstanceKlass::cast(k_o);
2170   Symbol* name = cp->uncached_name_ref_at(index);
2171   Symbol* sig  = cp->uncached_signature_ref_at(index);
2172   methodHandle m (THREAD, k->find_method(name, sig));
2173   if (m.is_null()) {
2174     THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2175   }
2176   oop method;
2177   if (m->is_object_constructor()) {
2178     method = Reflection::new_constructor(m, CHECK_NULL);
2179   } else {

2180     method = Reflection::new_method(m, true, CHECK_NULL);
2181   }
2182   return JNIHandles::make_local(THREAD, method);
2183 }
2184 
2185 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2186 {
2187   JvmtiVMObjectAllocEventCollector oam;
2188   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2189   bounds_check(cp, index, CHECK_NULL);
2190   jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2191   return res;
2192 }
2193 JVM_END
2194 
2195 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2196 {
2197   JvmtiVMObjectAllocEventCollector oam;
2198   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2199   bounds_check(cp, index, CHECK_NULL);

2609   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2610   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2611   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2612   return method->size_of_parameters();
2613 JVM_END
2614 
2615 
2616 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2617   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2618   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2619   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2620   return method->verifier_max_stack();
2621 JVM_END
2622 
2623 
2624 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2625   ResourceMark rm(THREAD);
2626   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2627   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2628   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2629   return method->is_object_constructor();
2630 JVM_END
2631 
2632 
2633 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2634   ResourceMark rm(THREAD);
2635   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2636   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2637   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2638   return method->is_overpass();
2639 JVM_END
2640 
2641 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2642   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2643   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2644   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2645   return method->name()->as_utf8();
2646 JVM_END
2647 
2648 
2649 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))

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

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

3545   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3546   jobject res = JNIHandles::make_local(THREAD, result);
3547   if (JvmtiExport::should_post_vm_object_alloc()) {
3548     JvmtiExport::post_vm_object_alloc(thread, result);
3549   }
3550   return res;
3551 JVM_END
3552 
3553 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3554   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3555   HeapShared::initialize_from_archived_subgraph(THREAD, k);
3556 JVM_END
3557 
3558 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3559                                               jclass caller,
3560                                               jstring interfaceMethodName,
3561                                               jobject factoryType,
3562                                               jobject interfaceMethodType,
3563                                               jobject implementationMember,
3564                                               jobject dynamicMethodType,

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