< 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/objArrayKlass.hpp"
  65 #include "oops/objArrayOop.inline.hpp"
  66 #include "oops/oop.inline.hpp"
  67 #include "oops/recordComponent.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/atomicAccess.hpp"
  75 #include "runtime/continuation.hpp"
  76 #include "runtime/deoptimization.hpp"
  77 #include "runtime/globals_extension.hpp"
  78 #include "runtime/handles.inline.hpp"
  79 #include "runtime/handshake.hpp"
  80 #include "runtime/init.hpp"
  81 #include "runtime/interfaceSupport.inline.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);

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

1166   } else {
1167     assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1168     size = 2;
1169   }
1170 
1171   // Allocate result array
1172   objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1173   objArrayHandle result (THREAD, r);
1174   // Fill in result
1175   if (klass->is_instance_klass()) {
1176     // Regular instance klass, fill in all local interfaces
1177     for (int index = 0; index < size; index++) {
1178       InstanceKlass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1179       result->obj_at_put(index, k->java_mirror());
1180     }
1181   } else {
1182     // All arrays implement java.lang.Cloneable and java.io.Serializable
1183     result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1184     result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1185   }
1186   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1187 JVM_END
1188 
1189 
1190 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))
1191   oop mirror = JNIHandles::resolve_non_null(cls);
1192   if (java_lang_Class::is_primitive(mirror)) {
1193     return JNI_FALSE;
1194   }
1195   Klass* k = java_lang_Class::as_Klass(mirror);
1196   return k->is_hidden();
1197 JVM_END
1198 
1199 
1200 class ScopedValueBindingsResolver {
1201 public:
1202   InstanceKlass* Carrier_klass;
1203   ScopedValueBindingsResolver(JavaThread* THREAD) {
1204     Klass *k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_ScopedValue_Carrier(), true, THREAD);
1205     Carrier_klass = InstanceKlass::cast(k);
1206   }
1207 };
1208 
1209 JVM_ENTRY(jobject, JVM_FindScopedValueBindings(JNIEnv *env, jclass cls))
1210   ResourceMark rm(THREAD);
1211   GrowableArray<Handle>* local_array = new GrowableArray<Handle>(12);
1212   JvmtiVMObjectAllocEventCollector oam;
1213 
1214   static ScopedValueBindingsResolver resolver(THREAD);
1215 
1216   // Iterate through Java frames
1217   vframeStream vfst(thread);
1218   for(; !vfst.at_end(); vfst.next()) {
1219     int loc = -1;

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

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

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

2402 
2403 
2404 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2405   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2406   Method* method = ik->methods()->at(method_index);
2407   return method->size_of_parameters();
2408 JVM_END
2409 
2410 
2411 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2412   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2413   Method* method = ik->methods()->at(method_index);
2414   return method->verifier_max_stack();
2415 JVM_END
2416 
2417 
2418 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2419   ResourceMark rm(THREAD);
2420   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2421   Method* method = ik->methods()->at(method_index);
2422   return method->name() == vmSymbols::object_initializer_name();
2423 JVM_END
2424 
2425 
2426 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2427   ResourceMark rm(THREAD);
2428   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2429   Method* method = ik->methods()->at(method_index);
2430   return method->is_overpass();
2431 JVM_END
2432 
2433 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2434   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2435   Method* method = ik->methods()->at(method_index);
2436   return method->name()->as_utf8();
2437 JVM_END
2438 
2439 
2440 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2441   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2442   Method* method = ik->methods()->at(method_index);

3196 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3197   void* find_result = os::dll_lookup(handle, name);
3198   log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3199                     find_result != nullptr ? "Found" : "Failed to find",
3200                     name, p2i(handle));
3201   return find_result;
3202 JVM_END
3203 
3204 
3205 // JNI version ///////////////////////////////////////////////////////////////////////////////
3206 
3207 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3208   return Threads::is_supported_jni_version_including_1_1(version);
3209 JVM_END
3210 
3211 
3212 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3213   return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3214 JVM_END
3215 




3216 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3217   return VMContinuations ? JNI_TRUE : JNI_FALSE;
3218 JVM_END
3219 
3220 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3221   return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3222 JVM_END
3223 
3224 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3225   return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3226 JVM_END
3227 
3228 // String support ///////////////////////////////////////////////////////////////////////////
3229 
3230 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3231   JvmtiVMObjectAllocEventCollector oam;
3232   if (str == nullptr) return nullptr;
3233   oop string = JNIHandles::resolve_non_null(str);
3234   oop result = StringTable::intern(string, CHECK_NULL);
3235   return (jstring) JNIHandles::make_local(THREAD, result);

3275 
3276 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3277                                     Handle loader, jboolean throwError, TRAPS) {
3278   Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3279 
3280   // Check if we should initialize the class
3281   if (init && klass->is_instance_klass()) {
3282     klass->initialize(CHECK_NULL);
3283   }
3284   return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3285 }
3286 
3287 
3288 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3289 
3290 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3291   Handle method_handle;
3292   if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3293     method_handle = Handle(THREAD, JNIHandles::resolve(method));
3294     Handle receiver(THREAD, JNIHandles::resolve(obj));
3295     objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));


3296     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3297     jobject res = JNIHandles::make_local(THREAD, result);
3298     if (JvmtiExport::should_post_vm_object_alloc()) {
3299       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3300       assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3301       if (java_lang_Class::is_primitive(ret_type)) {
3302         // Only for primitive type vm allocates memory for java object.
3303         // See box() method.
3304         JvmtiExport::post_vm_object_alloc(thread, result);
3305       }
3306     }
3307     return res;
3308   } else {
3309     THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3310   }
3311 JVM_END
3312 
3313 
3314 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))


3315   oop constructor_mirror = JNIHandles::resolve(c);
3316   objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3317   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3318   jobject res = JNIHandles::make_local(THREAD, result);
3319   if (JvmtiExport::should_post_vm_object_alloc()) {
3320     JvmtiExport::post_vm_object_alloc(thread, result);
3321   }
3322   return res;
3323 JVM_END
3324 
3325 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3326   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3327   HeapShared::initialize_from_archived_subgraph(THREAD, k);
3328 JVM_END
3329 
3330 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3331                                               jclass caller,
3332                                               jstring interfaceMethodName,
3333                                               jobject factoryType,
3334                                               jobject interfaceMethodType,
3335                                               jobject implementationMember,
3336                                               jobject dynamicMethodType,

3533   JvmtiVMObjectAllocEventCollector oam;
3534 
3535   // Check if threads is null
3536   if (threads == nullptr) {
3537     THROW_NULL(vmSymbols::java_lang_NullPointerException());
3538   }
3539 
3540   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3541   objArrayHandle ah(THREAD, a);
3542   int num_threads = ah->length();
3543   // check if threads is non-empty array
3544   if (num_threads == 0) {
3545     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3546   }
3547 
3548   // check if threads is not an array of objects of Thread class
3549   Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3550   if (k != vmClasses::Thread_klass()) {
3551     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3552   }

3553 
3554   ResourceMark rm(THREAD);
3555 
3556   GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3557   for (int i = 0; i < num_threads; i++) {
3558     oop thread_obj = ah->obj_at(i);
3559     instanceHandle h(THREAD, (instanceOop) thread_obj);
3560     thread_handle_array->append(h);
3561   }
3562 
3563   // The JavaThread references in thread_handle_array are validated
3564   // in VM_ThreadDump::doit().
3565   Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3566   return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3567 
3568 JVM_END
3569 
3570 // JVM monitoring and management support
3571 JVM_LEAF(void*, JVM_GetManagement(jint version))
3572   return Management::get_jmm_interface(version);
3573 JVM_END
3574 
3575 // com.sun.tools.attach.VirtualMachine agent properties support
3576 //
3577 // Initialize the agent properties with the properties maintained in the VM
3578 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/objArrayKlass.hpp"
  66 #include "oops/objArrayOop.inline.hpp"
  67 #include "oops/oop.inline.hpp"
  68 #include "oops/recordComponent.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/atomicAccess.hpp"
  77 #include "runtime/continuation.hpp"
  78 #include "runtime/deoptimization.hpp"
  79 #include "runtime/globals_extension.hpp"
  80 #include "runtime/handles.inline.hpp"
  81 #include "runtime/handshake.hpp"
  82 #include "runtime/init.hpp"
  83 #include "runtime/interfaceSupport.inline.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);

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

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

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

2129   constantTag tag = cp->tag_at(index);
2130   if (!tag.is_method() && !tag.is_interface_method()) {
2131     THROW_MSG_NULL(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2132   }
2133   int klass_ref  = cp->uncached_klass_ref_index_at(index);
2134   Klass* k_o;
2135   if (force_resolution) {
2136     k_o = cp->klass_at(klass_ref, CHECK_NULL);
2137   } else {
2138     k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2139     if (k_o == nullptr) return nullptr;
2140   }
2141   InstanceKlass* k = InstanceKlass::cast(k_o);
2142   Symbol* name = cp->uncached_name_ref_at(index);
2143   Symbol* sig  = cp->uncached_signature_ref_at(index);
2144   methodHandle m (THREAD, k->find_method(name, sig));
2145   if (m.is_null()) {
2146     THROW_MSG_NULL(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2147   }
2148   oop method;
2149   if (m->is_object_constructor()) {
2150     method = Reflection::new_constructor(m, CHECK_NULL);
2151   } else {

2152     method = Reflection::new_method(m, true, CHECK_NULL);
2153   }
2154   return JNIHandles::make_local(THREAD, method);
2155 }
2156 
2157 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jint index))
2158 {
2159   JvmtiVMObjectAllocEventCollector oam;
2160   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2161   bounds_check(cp, index, CHECK_NULL);
2162   jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2163   return res;
2164 }
2165 JVM_END
2166 
2167 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jint index))
2168 {
2169   JvmtiVMObjectAllocEventCollector oam;
2170   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2171   bounds_check(cp, index, CHECK_NULL);

2578 
2579 
2580 JVM_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2581   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2582   Method* method = ik->methods()->at(method_index);
2583   return method->size_of_parameters();
2584 JVM_END
2585 
2586 
2587 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2588   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2589   Method* method = ik->methods()->at(method_index);
2590   return method->verifier_max_stack();
2591 JVM_END
2592 
2593 
2594 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2595   ResourceMark rm(THREAD);
2596   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2597   Method* method = ik->methods()->at(method_index);
2598   return method->is_object_constructor();
2599 JVM_END
2600 
2601 
2602 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2603   ResourceMark rm(THREAD);
2604   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2605   Method* method = ik->methods()->at(method_index);
2606   return method->is_overpass();
2607 JVM_END
2608 
2609 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2610   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2611   Method* method = ik->methods()->at(method_index);
2612   return method->name()->as_utf8();
2613 JVM_END
2614 
2615 
2616 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2617   InstanceKlass* ik = get_instance_klass_considering_redefinition(cls, thread);
2618   Method* method = ik->methods()->at(method_index);

3372 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3373   void* find_result = os::dll_lookup(handle, name);
3374   log_info(library)("%s %s in library with handle " INTPTR_FORMAT,
3375                     find_result != nullptr ? "Found" : "Failed to find",
3376                     name, p2i(handle));
3377   return find_result;
3378 JVM_END
3379 
3380 
3381 // JNI version ///////////////////////////////////////////////////////////////////////////////
3382 
3383 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3384   return Threads::is_supported_jni_version_including_1_1(version);
3385 JVM_END
3386 
3387 
3388 JVM_LEAF(jboolean, JVM_IsPreviewEnabled(void))
3389   return Arguments::enable_preview() ? JNI_TRUE : JNI_FALSE;
3390 JVM_END
3391 
3392 JVM_LEAF(jboolean, JVM_IsValhallaEnabled(void))
3393   return EnableValhalla ? JNI_TRUE : JNI_FALSE;
3394 JVM_END
3395 
3396 JVM_LEAF(jboolean, JVM_IsContinuationsSupported(void))
3397   return VMContinuations ? JNI_TRUE : JNI_FALSE;
3398 JVM_END
3399 
3400 JVM_LEAF(jboolean, JVM_IsForeignLinkerSupported(void))
3401   return ForeignGlobals::is_foreign_linker_supported() ? JNI_TRUE : JNI_FALSE;
3402 JVM_END
3403 
3404 JVM_LEAF(jboolean, JVM_IsStaticallyLinked(void))
3405   return is_vm_statically_linked() ? JNI_TRUE : JNI_FALSE;
3406 JVM_END
3407 
3408 // String support ///////////////////////////////////////////////////////////////////////////
3409 
3410 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3411   JvmtiVMObjectAllocEventCollector oam;
3412   if (str == nullptr) return nullptr;
3413   oop string = JNIHandles::resolve_non_null(str);
3414   oop result = StringTable::intern(string, CHECK_NULL);
3415   return (jstring) JNIHandles::make_local(THREAD, result);

3455 
3456 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3457                                     Handle loader, jboolean throwError, TRAPS) {
3458   Klass* klass = SystemDictionary::resolve_or_fail(name, loader, throwError != 0, CHECK_NULL);
3459 
3460   // Check if we should initialize the class
3461   if (init && klass->is_instance_klass()) {
3462     klass->initialize(CHECK_NULL);
3463   }
3464   return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3465 }
3466 
3467 
3468 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3469 
3470 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3471   Handle method_handle;
3472   if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3473     method_handle = Handle(THREAD, JNIHandles::resolve(method));
3474     Handle receiver(THREAD, JNIHandles::resolve(obj));
3475     objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3476     assert(args() == nullptr || !args->is_flatArray(), "args are never flat or are they???");
3477 
3478     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3479     jobject res = JNIHandles::make_local(THREAD, result);
3480     if (JvmtiExport::should_post_vm_object_alloc()) {
3481       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3482       assert(ret_type != nullptr, "sanity check: ret_type oop must not be null!");
3483       if (java_lang_Class::is_primitive(ret_type)) {
3484         // Only for primitive type vm allocates memory for java object.
3485         // See box() method.
3486         JvmtiExport::post_vm_object_alloc(thread, result);
3487       }
3488     }
3489     return res;
3490   } else {
3491     THROW_NULL(vmSymbols::java_lang_StackOverflowError());
3492   }
3493 JVM_END
3494 
3495 
3496 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3497   objArrayHandle args(THREAD, (objArrayOop)JNIHandles::resolve(args0));
3498   assert(args() == nullptr || !args->is_flatArray(), "args are never flat or are they???");
3499   oop constructor_mirror = JNIHandles::resolve(c);

3500   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3501   jobject res = JNIHandles::make_local(THREAD, result);
3502   if (JvmtiExport::should_post_vm_object_alloc()) {
3503     JvmtiExport::post_vm_object_alloc(thread, result);
3504   }
3505   return res;
3506 JVM_END
3507 
3508 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3509   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3510   HeapShared::initialize_from_archived_subgraph(THREAD, k);
3511 JVM_END
3512 
3513 JVM_ENTRY(void, JVM_RegisterLambdaProxyClassForArchiving(JNIEnv* env,
3514                                               jclass caller,
3515                                               jstring interfaceMethodName,
3516                                               jobject factoryType,
3517                                               jobject interfaceMethodType,
3518                                               jobject implementationMember,
3519                                               jobject dynamicMethodType,

3716   JvmtiVMObjectAllocEventCollector oam;
3717 
3718   // Check if threads is null
3719   if (threads == nullptr) {
3720     THROW_NULL(vmSymbols::java_lang_NullPointerException());
3721   }
3722 
3723   objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
3724   objArrayHandle ah(THREAD, a);
3725   int num_threads = ah->length();
3726   // check if threads is non-empty array
3727   if (num_threads == 0) {
3728     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3729   }
3730 
3731   // check if threads is not an array of objects of Thread class
3732   Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass();
3733   if (k != vmClasses::Thread_klass()) {
3734     THROW_NULL(vmSymbols::java_lang_IllegalArgumentException());
3735   }
3736   refArrayHandle rah(THREAD, (refArrayOop)ah()); // j.l.Thread is an identity class, arrays are always reference arrays
3737 
3738   ResourceMark rm(THREAD);
3739 
3740   GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
3741   for (int i = 0; i < num_threads; i++) {
3742     oop thread_obj = rah->obj_at(i);
3743     instanceHandle h(THREAD, (instanceOop) thread_obj);
3744     thread_handle_array->append(h);
3745   }
3746 
3747   // The JavaThread references in thread_handle_array are validated
3748   // in VM_ThreadDump::doit().
3749   Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
3750   return (jobjectArray)JNIHandles::make_local(THREAD, stacktraces());
3751 
3752 JVM_END
3753 
3754 // JVM monitoring and management support
3755 JVM_LEAF(void*, JVM_GetManagement(jint version))
3756   return Management::get_jmm_interface(version);
3757 JVM_END
3758 
3759 // com.sun.tools.attach.VirtualMachine agent properties support
3760 //
3761 // Initialize the agent properties with the properties maintained in the VM
3762 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
< prev index next >