< prev index next >

src/hotspot/share/prims/jvm.cpp

Print this page

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

  59 #include "oops/instanceKlass.hpp"
  60 #include "oops/klass.inline.hpp"
  61 #include "oops/method.hpp"
  62 #include "oops/recordComponent.hpp"
  63 #include "oops/objArrayKlass.hpp"
  64 #include "oops/objArrayOop.inline.hpp"
  65 #include "oops/oop.inline.hpp"
  66 #include "prims/jvm_misc.hpp"
  67 #include "prims/jvmtiExport.hpp"
  68 #include "prims/jvmtiThreadState.hpp"
  69 #include "prims/stackwalk.hpp"
  70 #include "runtime/arguments.hpp"
  71 #include "runtime/atomic.hpp"
  72 #include "runtime/globals_extension.hpp"
  73 #include "runtime/handles.inline.hpp"
  74 #include "runtime/init.hpp"
  75 #include "runtime/interfaceSupport.inline.hpp"
  76 #include "runtime/deoptimization.hpp"
  77 #include "runtime/handshake.hpp"
  78 #include "runtime/java.hpp"

 581   // and a StackFrameInfo[] array (or derivative) otherwise. It should never
 582   // be null.
 583   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 584   objArrayHandle frames_array_h(THREAD, fa);
 585 
 586   int limit = start_index+frame_count;
 587   if (frames_array_h->length() < limit) {
 588     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
 589   }
 590 
 591   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 592   return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, frame_count,
 593                                    start_index, frames_array_h, THREAD);
 594 JVM_END
 595 
 596 // java.lang.Object ///////////////////////////////////////////////
 597 
 598 
 599 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
 600   // as implemented in the classic virtual machine; return 0 if object is NULL
 601   return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;





















 602 JVM_END
 603 
 604 
 605 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
 606   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 607   JavaThreadInObjectWaitState jtiows(thread, ms != 0);
 608   if (JvmtiExport::should_post_monitor_wait()) {
 609     JvmtiExport::post_monitor_wait(thread, obj(), ms);
 610 
 611     // The current thread already owns the monitor and it has not yet
 612     // been added to the wait queue so the current thread cannot be
 613     // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
 614     // event handler cannot accidentally consume an unpark() meant for
 615     // the ParkEvent associated with this ObjectMonitor.
 616   }
 617   ObjectSynchronizer::wait(obj, ms, CHECK);
 618 JVM_END
 619 
 620 
 621 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))

 633 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
 634   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 635   Klass* klass = obj->klass();
 636   JvmtiVMObjectAllocEventCollector oam;
 637 
 638 #ifdef ASSERT
 639   // Just checking that the cloneable flag is set correct
 640   if (obj->is_array()) {
 641     guarantee(klass->is_cloneable(), "all arrays are cloneable");
 642   } else {
 643     guarantee(obj->is_instance(), "should be instanceOop");
 644     bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
 645     guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
 646   }
 647 #endif
 648 
 649   // Check if class of obj supports the Cloneable interface.
 650   // All arrays are considered to be cloneable (See JLS 20.1.5).
 651   // All j.l.r.Reference classes are considered non-cloneable.
 652   if (!klass->is_cloneable() ||

 653       (klass->is_instance_klass() &&
 654        InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
 655     ResourceMark rm(THREAD);
 656     THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
 657   }
 658 
 659   // Make shallow object copy
 660   const int size = obj->size();
 661   oop new_obj_oop = NULL;
 662   if (obj->is_array()) {
 663     const int length = ((arrayOop)obj())->length();
 664     new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
 665                                                    /* do_zero */ true, CHECK_NULL);
 666   } else {
 667     new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
 668   }
 669 
 670   HeapAccess<>::clone(obj(), new_obj_oop, size);
 671 
 672   Handle new_obj(THREAD, new_obj_oop);

1130   oop result = java_lang_Class::name(java_class, CHECK_NULL);
1131   return (jstring) JNIHandles::make_local(THREAD, result);
1132 JVM_END
1133 
1134 
1135 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1136   JvmtiVMObjectAllocEventCollector oam;
1137   oop mirror = JNIHandles::resolve_non_null(cls);
1138 
1139   // Special handling for primitive objects
1140   if (java_lang_Class::is_primitive(mirror)) {
1141     // Primitive objects does not have any interfaces
1142     objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1143     return (jobjectArray) JNIHandles::make_local(THREAD, r);
1144   }
1145 
1146   Klass* klass = java_lang_Class::as_Klass(mirror);
1147   // Figure size of result array
1148   int size;
1149   if (klass->is_instance_klass()) {
1150     size = InstanceKlass::cast(klass)->local_interfaces()->length();

1151   } else {
1152     assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1153     size = 2;
1154   }
1155 
1156   // Allocate result array
1157   objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1158   objArrayHandle result (THREAD, r);
1159   // Fill in result
1160   if (klass->is_instance_klass()) {
1161     // Regular instance klass, fill in all local interfaces
1162     for (int index = 0; index < size; index++) {
1163       Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);

1164       result->obj_at_put(index, k->java_mirror());
1165     }
1166   } else {
1167     // All arrays implement java.lang.Cloneable and java.io.Serializable
1168     result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1169     result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());

1170   }
1171   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1172 JVM_END
1173 
1174 
1175 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1176   oop mirror = JNIHandles::resolve_non_null(cls);
1177   if (java_lang_Class::is_primitive(mirror)) {
1178     return JNI_FALSE;
1179   }
1180   Klass* k = java_lang_Class::as_Klass(mirror);
1181   jboolean result = k->is_interface();
1182   assert(!result || k->is_instance_klass(),
1183          "all interfaces are instance types");
1184   // The compiler intrinsic for isInterface tests the
1185   // Klass::_access_flags bits in the same way.
1186   return result;
1187 JVM_END
1188 
1189 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))

1745     int length = components->length();
1746     assert(length >= 0, "unexpected record_components length");
1747     objArrayOop record_components =
1748       oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1749     objArrayHandle components_h (THREAD, record_components);
1750 
1751     for (int x = 0; x < length; x++) {
1752       RecordComponent* component = components->at(x);
1753       assert(component != NULL, "unexpected NULL record component");
1754       oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1755       components_h->obj_at_put(x, component_oop);
1756     }
1757     return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
1758   }
1759 
1760   return NULL;
1761 }
1762 JVM_END
1763 
1764 static bool select_method(const methodHandle& method, bool want_constructor) {


1765   if (want_constructor) {
1766     return (method->is_initializer() && !method->is_static());
1767   } else {
1768     return  (!method->is_initializer() && !method->is_overpass());


1769   }
1770 }
1771 
1772 static jobjectArray get_class_declared_methods_helper(
1773                                   JNIEnv *env,
1774                                   jclass ofClass, jboolean publicOnly,
1775                                   bool want_constructor,
1776                                   Klass* klass, TRAPS) {
1777 
1778   JvmtiVMObjectAllocEventCollector oam;
1779 
1780   oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1781   // Exclude primitive types and array types
1782   if (java_lang_Class::is_primitive(ofMirror)
1783       || java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1784     // Return empty array
1785     oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1786     return (jobjectArray) JNIHandles::make_local(THREAD, res);
1787   }
1788 

1811     }
1812   }
1813 
1814   // Allocate result
1815   objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1816   objArrayHandle result (THREAD, r);
1817 
1818   // Now just put the methods that we selected above, but go by their idnum
1819   // in case of redefinition.  The methods can be redefined at any safepoint,
1820   // so above when allocating the oop array and below when creating reflect
1821   // objects.
1822   for (int i = 0; i < num_methods; i++) {
1823     methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1824     if (method.is_null()) {
1825       // Method may have been deleted and seems this API can handle null
1826       // Otherwise should probably put a method that throws NSME
1827       result->obj_at_put(i, NULL);
1828     } else {
1829       oop m;
1830       if (want_constructor) {


1831         m = Reflection::new_constructor(method, CHECK_NULL);
1832       } else {
1833         m = Reflection::new_method(method, false, CHECK_NULL);
1834       }
1835       result->obj_at_put(i, m);
1836     }
1837   }
1838 
1839   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1840 }
1841 
1842 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1843 {
1844   return get_class_declared_methods_helper(env, ofClass, publicOnly,
1845                                            /*want_constructor*/ false,
1846                                            vmClasses::reflect_Method_klass(), THREAD);
1847 }
1848 JVM_END
1849 
1850 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))

2093   constantTag tag = cp->tag_at(index);
2094   if (!tag.is_method() && !tag.is_interface_method()) {
2095     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2096   }
2097   int klass_ref  = cp->uncached_klass_ref_index_at(index);
2098   Klass* k_o;
2099   if (force_resolution) {
2100     k_o = cp->klass_at(klass_ref, CHECK_NULL);
2101   } else {
2102     k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2103     if (k_o == NULL) return NULL;
2104   }
2105   InstanceKlass* k = InstanceKlass::cast(k_o);
2106   Symbol* name = cp->uncached_name_ref_at(index);
2107   Symbol* sig  = cp->uncached_signature_ref_at(index);
2108   methodHandle m (THREAD, k->find_method(name, sig));
2109   if (m.is_null()) {
2110     THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
2111   }
2112   oop method;
2113   if (!m->is_initializer() || m->is_static()) {
2114     method = Reflection::new_method(m, true, CHECK_NULL);
2115   } else {
2116     method = Reflection::new_constructor(m, CHECK_NULL);


2117   }
2118   return JNIHandles::make_local(THREAD, method);
2119 }
2120 
2121 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
2122 {
2123   JvmtiVMObjectAllocEventCollector oam;
2124   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2125   bounds_check(cp, index, CHECK_NULL);
2126   jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
2127   return res;
2128 }
2129 JVM_END
2130 
2131 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
2132 {
2133   JvmtiVMObjectAllocEventCollector oam;
2134   constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2135   bounds_check(cp, index, CHECK_NULL);
2136   jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);

2367   Klass* k = java_lang_Class::as_Klass(r);
2368   assert(k->is_instance_klass(), "must be an instance klass");
2369   if (!k->is_instance_klass()) return false;
2370 
2371   ResourceMark rm(THREAD);
2372   const char* name = k->name()->as_C_string();
2373   bool system_class = k->class_loader() == NULL;
2374   return JavaAssertions::enabled(name, system_class);
2375 
2376 JVM_END
2377 
2378 
2379 // Return a new AssertionStatusDirectives object with the fields filled in with
2380 // command-line assertion arguments (i.e., -ea, -da).
2381 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2382   JvmtiVMObjectAllocEventCollector oam;
2383   oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2384   return JNIHandles::make_local(THREAD, asd);
2385 JVM_END
2386 





































2387 // Verification ////////////////////////////////////////////////////////////////////////////////
2388 
2389 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2390 
2391 // RedefineClasses support: bug 6214132 caused verification to fail.
2392 // All functions from this section should call the jvmtiThreadSate function:
2393 //   Klass* class_to_verify_considering_redefinition(Klass* klass).
2394 // The function returns a Klass* of the _scratch_class if the verifier
2395 // was invoked in the middle of the class redefinition.
2396 // Otherwise it returns its argument value which is the _the_class Klass*.
2397 // Please, refer to the description in the jvmtiThreadSate.hpp.
2398 
2399 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2400   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2401   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2402   return k->name()->as_utf8();
2403 JVM_END
2404 
2405 
2406 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))

2529   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2530   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2531   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2532   return method->size_of_parameters();
2533 JVM_END
2534 
2535 
2536 JVM_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2537   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2538   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2539   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2540   return method->verifier_max_stack();
2541 JVM_END
2542 
2543 
2544 JVM_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2545   ResourceMark rm(THREAD);
2546   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2547   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2548   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2549   return method->name() == vmSymbols::object_initializer_name();
2550 JVM_END
2551 
2552 
2553 JVM_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
2554   ResourceMark rm(THREAD);
2555   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2556   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2557   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2558   return method->is_overpass();
2559 JVM_END
2560 
2561 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2562   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2563   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2564   Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
2565   return method->name()->as_utf8();
2566 JVM_END
2567 
2568 
2569 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))

3465   //   the checkPackageAccess relative to the initiating class loader via the
3466   //   protection_domain. The protection_domain is passed as NULL by the java code
3467   //   if there is no security manager in 3-arg Class.forName().
3468   Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3469 
3470   // Check if we should initialize the class
3471   if (init && klass->is_instance_klass()) {
3472     klass->initialize(CHECK_NULL);
3473   }
3474   return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3475 }
3476 
3477 
3478 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3479 
3480 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3481   Handle method_handle;
3482   if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3483     method_handle = Handle(THREAD, JNIHandles::resolve(method));
3484     Handle receiver(THREAD, JNIHandles::resolve(obj));
3485     objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3486     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3487     jobject res = JNIHandles::make_local(THREAD, result);
3488     if (JvmtiExport::should_post_vm_object_alloc()) {
3489       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3490       assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
3491       if (java_lang_Class::is_primitive(ret_type)) {
3492         // Only for primitive type vm allocates memory for java object.
3493         // See box() method.
3494         JvmtiExport::post_vm_object_alloc(thread, result);
3495       }
3496     }
3497     return res;
3498   } else {
3499     THROW_0(vmSymbols::java_lang_StackOverflowError());
3500   }
3501 JVM_END
3502 
3503 
3504 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))

3505   oop constructor_mirror = JNIHandles::resolve(c);
3506   objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3507   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3508   jobject res = JNIHandles::make_local(THREAD, result);
3509   if (JvmtiExport::should_post_vm_object_alloc()) {
3510     JvmtiExport::post_vm_object_alloc(thread, result);
3511   }
3512   return res;
3513 JVM_END
3514 
3515 // Atomic ///////////////////////////////////////////////////////////////////////////////////////////
3516 
3517 JVM_LEAF(jboolean, JVM_SupportsCX8())
3518   return VM_Version::supports_cx8();
3519 JVM_END
3520 
3521 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3522   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3523   assert(k->is_klass(), "just checking");
3524   HeapShared::initialize_from_archived_subgraph(k, THREAD);
3525 JVM_END
3526 

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

 582   // and a StackFrameInfo[] array (or derivative) otherwise. It should never
 583   // be null.
 584   objArrayOop fa = objArrayOop(JNIHandles::resolve_non_null(frames));
 585   objArrayHandle frames_array_h(THREAD, fa);
 586 
 587   int limit = start_index+frame_count;
 588   if (frames_array_h->length() < limit) {
 589     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "not enough space in buffers");
 590   }
 591 
 592   Handle stackStream_h(THREAD, JNIHandles::resolve_non_null(stackStream));
 593   return StackWalk::fetchNextBatch(stackStream_h, mode, anchor, frame_count,
 594                                    start_index, frames_array_h, THREAD);
 595 JVM_END
 596 
 597 // java.lang.Object ///////////////////////////////////////////////
 598 
 599 
 600 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
 601   // as implemented in the classic virtual machine; return 0 if object is NULL
 602   if (handle == NULL) {
 603     return 0;
 604   }
 605   oop obj = JNIHandles::resolve_non_null(handle);
 606   if (EnableValhalla && obj->klass()->is_inline_klass()) {
 607       JavaValue result(T_INT);
 608       JavaCallArguments args;
 609       Handle ho(THREAD, obj);
 610       args.push_oop(ho);
 611       methodHandle method(THREAD, Universe::primitive_type_hash_code_method());
 612       JavaCalls::call(&result, method, &args, THREAD);
 613       if (HAS_PENDING_EXCEPTION) {
 614         if (!PENDING_EXCEPTION->is_a(vmClasses::Error_klass())) {
 615           Handle e(THREAD, PENDING_EXCEPTION);
 616           CLEAR_PENDING_EXCEPTION;
 617           THROW_MSG_CAUSE_(vmSymbols::java_lang_InternalError(), "Internal error in hashCode", e, false);
 618         }
 619       }
 620       return result.get_jint();
 621   } else {
 622     return ObjectSynchronizer::FastHashCode(THREAD, obj);
 623   }
 624 JVM_END
 625 
 626 
 627 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
 628   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 629   JavaThreadInObjectWaitState jtiows(thread, ms != 0);
 630   if (JvmtiExport::should_post_monitor_wait()) {
 631     JvmtiExport::post_monitor_wait(thread, obj(), ms);
 632 
 633     // The current thread already owns the monitor and it has not yet
 634     // been added to the wait queue so the current thread cannot be
 635     // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
 636     // event handler cannot accidentally consume an unpark() meant for
 637     // the ParkEvent associated with this ObjectMonitor.
 638   }
 639   ObjectSynchronizer::wait(obj, ms, CHECK);
 640 JVM_END
 641 
 642 
 643 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))

 655 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
 656   Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
 657   Klass* klass = obj->klass();
 658   JvmtiVMObjectAllocEventCollector oam;
 659 
 660 #ifdef ASSERT
 661   // Just checking that the cloneable flag is set correct
 662   if (obj->is_array()) {
 663     guarantee(klass->is_cloneable(), "all arrays are cloneable");
 664   } else {
 665     guarantee(obj->is_instance(), "should be instanceOop");
 666     bool cloneable = klass->is_subtype_of(vmClasses::Cloneable_klass());
 667     guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
 668   }
 669 #endif
 670 
 671   // Check if class of obj supports the Cloneable interface.
 672   // All arrays are considered to be cloneable (See JLS 20.1.5).
 673   // All j.l.r.Reference classes are considered non-cloneable.
 674   if (!klass->is_cloneable() ||
 675        klass->is_inline_klass() ||
 676       (klass->is_instance_klass() &&
 677        InstanceKlass::cast(klass)->reference_type() != REF_NONE)) {
 678     ResourceMark rm(THREAD);
 679     THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
 680   }
 681 
 682   // Make shallow object copy
 683   const int size = obj->size();
 684   oop new_obj_oop = NULL;
 685   if (obj->is_array()) {
 686     const int length = ((arrayOop)obj())->length();
 687     new_obj_oop = Universe::heap()->array_allocate(klass, size, length,
 688                                                    /* do_zero */ true, CHECK_NULL);
 689   } else {
 690     new_obj_oop = Universe::heap()->obj_allocate(klass, size, CHECK_NULL);
 691   }
 692 
 693   HeapAccess<>::clone(obj(), new_obj_oop, size);
 694 
 695   Handle new_obj(THREAD, new_obj_oop);

1153   oop result = java_lang_Class::name(java_class, CHECK_NULL);
1154   return (jstring) JNIHandles::make_local(THREAD, result);
1155 JVM_END
1156 
1157 
1158 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1159   JvmtiVMObjectAllocEventCollector oam;
1160   oop mirror = JNIHandles::resolve_non_null(cls);
1161 
1162   // Special handling for primitive objects
1163   if (java_lang_Class::is_primitive(mirror)) {
1164     // Primitive objects does not have any interfaces
1165     objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), 0, CHECK_NULL);
1166     return (jobjectArray) JNIHandles::make_local(THREAD, r);
1167   }
1168 
1169   Klass* klass = java_lang_Class::as_Klass(mirror);
1170   // Figure size of result array
1171   int size;
1172   if (klass->is_instance_klass()) {
1173     InstanceKlass* ik = InstanceKlass::cast(klass);
1174     size = ik->local_interfaces()->length();
1175   } else {
1176     assert(klass->is_objArray_klass() || klass->is_typeArray_klass() || klass->is_flatArray_klass(), "Illegal mirror klass");
1177     size = 3;
1178   }
1179 
1180   // Allocate result array
1181   objArrayOop r = oopFactory::new_objArray(vmClasses::Class_klass(), size, CHECK_NULL);
1182   objArrayHandle result (THREAD, r);
1183   // Fill in result
1184   if (klass->is_instance_klass()) {
1185     // Regular instance klass, fill in all local interfaces
1186     for (int index = 0; index < size; index++) {
1187       InstanceKlass* ik = InstanceKlass::cast(klass);
1188       Klass* k = ik->local_interfaces()->at(index);
1189       result->obj_at_put(index, k->java_mirror());
1190     }
1191   } else {
1192     // All arrays implement java.lang.Cloneable, java.io.Serializable and java.lang.IdentityObject
1193     result->obj_at_put(0, vmClasses::Cloneable_klass()->java_mirror());
1194     result->obj_at_put(1, vmClasses::Serializable_klass()->java_mirror());
1195     result->obj_at_put(2, vmClasses::IdentityObject_klass()->java_mirror());
1196   }
1197   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1198 JVM_END
1199 
1200 
1201 JVM_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1202   oop mirror = JNIHandles::resolve_non_null(cls);
1203   if (java_lang_Class::is_primitive(mirror)) {
1204     return JNI_FALSE;
1205   }
1206   Klass* k = java_lang_Class::as_Klass(mirror);
1207   jboolean result = k->is_interface();
1208   assert(!result || k->is_instance_klass(),
1209          "all interfaces are instance types");
1210   // The compiler intrinsic for isInterface tests the
1211   // Klass::_access_flags bits in the same way.
1212   return result;
1213 JVM_END
1214 
1215 JVM_ENTRY(jboolean, JVM_IsHiddenClass(JNIEnv *env, jclass cls))

1771     int length = components->length();
1772     assert(length >= 0, "unexpected record_components length");
1773     objArrayOop record_components =
1774       oopFactory::new_objArray(vmClasses::RecordComponent_klass(), length, CHECK_NULL);
1775     objArrayHandle components_h (THREAD, record_components);
1776 
1777     for (int x = 0; x < length; x++) {
1778       RecordComponent* component = components->at(x);
1779       assert(component != NULL, "unexpected NULL record component");
1780       oop component_oop = java_lang_reflect_RecordComponent::create(ik, component, CHECK_NULL);
1781       components_h->obj_at_put(x, component_oop);
1782     }
1783     return (jobjectArray)JNIHandles::make_local(THREAD, components_h());
1784   }
1785 
1786   return NULL;
1787 }
1788 JVM_END
1789 
1790 static bool select_method(const methodHandle& method, bool want_constructor) {
1791   bool is_ctor = (method->is_object_constructor() ||
1792                   method->is_static_init_factory());
1793   if (want_constructor) {
1794     return is_ctor;
1795   } else {
1796     return (!is_ctor &&
1797             !method->is_class_initializer() &&
1798             !method->is_overpass());
1799   }
1800 }
1801 
1802 static jobjectArray get_class_declared_methods_helper(
1803                                   JNIEnv *env,
1804                                   jclass ofClass, jboolean publicOnly,
1805                                   bool want_constructor,
1806                                   Klass* klass, TRAPS) {
1807 
1808   JvmtiVMObjectAllocEventCollector oam;
1809 
1810   oop ofMirror = JNIHandles::resolve_non_null(ofClass);
1811   // Exclude primitive types and array types
1812   if (java_lang_Class::is_primitive(ofMirror)
1813       || java_lang_Class::as_Klass(ofMirror)->is_array_klass()) {
1814     // Return empty array
1815     oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1816     return (jobjectArray) JNIHandles::make_local(THREAD, res);
1817   }
1818 

1841     }
1842   }
1843 
1844   // Allocate result
1845   objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
1846   objArrayHandle result (THREAD, r);
1847 
1848   // Now just put the methods that we selected above, but go by their idnum
1849   // in case of redefinition.  The methods can be redefined at any safepoint,
1850   // so above when allocating the oop array and below when creating reflect
1851   // objects.
1852   for (int i = 0; i < num_methods; i++) {
1853     methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
1854     if (method.is_null()) {
1855       // Method may have been deleted and seems this API can handle null
1856       // Otherwise should probably put a method that throws NSME
1857       result->obj_at_put(i, NULL);
1858     } else {
1859       oop m;
1860       if (want_constructor) {
1861         assert(method->is_object_constructor() ||
1862                method->is_static_init_factory(), "must be");
1863         m = Reflection::new_constructor(method, CHECK_NULL);
1864       } else {
1865         m = Reflection::new_method(method, false, CHECK_NULL);
1866       }
1867       result->obj_at_put(i, m);
1868     }
1869   }
1870 
1871   return (jobjectArray) JNIHandles::make_local(THREAD, result());
1872 }
1873 
1874 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1875 {
1876   return get_class_declared_methods_helper(env, ofClass, publicOnly,
1877                                            /*want_constructor*/ false,
1878                                            vmClasses::reflect_Method_klass(), THREAD);
1879 }
1880 JVM_END
1881 
1882 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))

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


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

2399   Klass* k = java_lang_Class::as_Klass(r);
2400   assert(k->is_instance_klass(), "must be an instance klass");
2401   if (!k->is_instance_klass()) return false;
2402 
2403   ResourceMark rm(THREAD);
2404   const char* name = k->name()->as_C_string();
2405   bool system_class = k->class_loader() == NULL;
2406   return JavaAssertions::enabled(name, system_class);
2407 
2408 JVM_END
2409 
2410 
2411 // Return a new AssertionStatusDirectives object with the fields filled in with
2412 // command-line assertion arguments (i.e., -ea, -da).
2413 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
2414   JvmtiVMObjectAllocEventCollector oam;
2415   oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
2416   return JNIHandles::make_local(THREAD, asd);
2417 JVM_END
2418 
2419 // Arrays support /////////////////////////////////////////////////////////////
2420 
2421 JVM_ENTRY(jboolean, JVM_ArrayIsAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2422   oop o = JNIHandles::resolve(array);
2423   Klass* k = o->klass();
2424   if ((o == NULL) || (!k->is_array_klass())) {
2425     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2426   }
2427   return ArrayKlass::cast(k)->element_access_is_atomic();
2428 JVM_END
2429 
2430 JVM_ENTRY(jobject, JVM_ArrayEnsureAccessAtomic(JNIEnv *env, jclass unused, jobject array))
2431   oop o = JNIHandles::resolve(array);
2432   Klass* k = o->klass();
2433   if ((o == NULL) || (!k->is_array_klass())) {
2434     THROW_0(vmSymbols::java_lang_IllegalArgumentException());
2435   }
2436   if (k->is_flatArray_klass()) {
2437     FlatArrayKlass* vk = FlatArrayKlass::cast(k);
2438     if (!vk->element_access_is_atomic()) {
2439       /**
2440        * Need to decide how to implement:
2441        *
2442        * 1) Change to objArrayOop layout, therefore oop->klass() differs so
2443        * then "<atomic>[Qfoo;" klass needs to subclass "[Qfoo;" to pass through
2444        * "checkcast" & "instanceof"
2445        *
2446        * 2) Use extra header in the flatArrayOop to flag atomicity required and
2447        * possibly per instance lock structure. Said info, could be placed in
2448        * "trailer" rather than disturb the current arrayOop
2449        */
2450       Unimplemented();
2451     }
2452   }
2453   return array;
2454 JVM_END
2455 
2456 // Verification ////////////////////////////////////////////////////////////////////////////////
2457 
2458 // Reflection for the verifier /////////////////////////////////////////////////////////////////
2459 
2460 // RedefineClasses support: bug 6214132 caused verification to fail.
2461 // All functions from this section should call the jvmtiThreadSate function:
2462 //   Klass* class_to_verify_considering_redefinition(Klass* klass).
2463 // The function returns a Klass* of the _scratch_class if the verifier
2464 // was invoked in the middle of the class redefinition.
2465 // Otherwise it returns its argument value which is the _the_class Klass*.
2466 // Please, refer to the description in the jvmtiThreadSate.hpp.
2467 
2468 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
2469   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2470   k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2471   return k->name()->as_utf8();
2472 JVM_END
2473 
2474 
2475 JVM_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))

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

3534   //   the checkPackageAccess relative to the initiating class loader via the
3535   //   protection_domain. The protection_domain is passed as NULL by the java code
3536   //   if there is no security manager in 3-arg Class.forName().
3537   Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3538 
3539   // Check if we should initialize the class
3540   if (init && klass->is_instance_klass()) {
3541     klass->initialize(CHECK_NULL);
3542   }
3543   return (jclass) JNIHandles::make_local(THREAD, klass->java_mirror());
3544 }
3545 
3546 
3547 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3548 
3549 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3550   Handle method_handle;
3551   if (thread->stack_overflow_state()->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3552     method_handle = Handle(THREAD, JNIHandles::resolve(method));
3553     Handle receiver(THREAD, JNIHandles::resolve(obj));
3554     objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3555     oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3556     jobject res = JNIHandles::make_local(THREAD, result);
3557     if (JvmtiExport::should_post_vm_object_alloc()) {
3558       oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3559       assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
3560       if (java_lang_Class::is_primitive(ret_type)) {
3561         // Only for primitive type vm allocates memory for java object.
3562         // See box() method.
3563         JvmtiExport::post_vm_object_alloc(thread, result);
3564       }
3565     }
3566     return res;
3567   } else {
3568     THROW_0(vmSymbols::java_lang_StackOverflowError());
3569   }
3570 JVM_END
3571 
3572 
3573 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
3574   objArrayHandle args = oopFactory::ensure_objArray(JNIHandles::resolve(args0), CHECK_NULL);
3575   oop constructor_mirror = JNIHandles::resolve(c);

3576   oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
3577   jobject res = JNIHandles::make_local(THREAD, result);
3578   if (JvmtiExport::should_post_vm_object_alloc()) {
3579     JvmtiExport::post_vm_object_alloc(thread, result);
3580   }
3581   return res;
3582 JVM_END
3583 
3584 // Atomic ///////////////////////////////////////////////////////////////////////////////////////////
3585 
3586 JVM_LEAF(jboolean, JVM_SupportsCX8())
3587   return VM_Version::supports_cx8();
3588 JVM_END
3589 
3590 JVM_ENTRY(void, JVM_InitializeFromArchive(JNIEnv* env, jclass cls))
3591   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
3592   assert(k->is_klass(), "just checking");
3593   HeapShared::initialize_from_archived_subgraph(k, THREAD);
3594 JVM_END
3595 
< prev index next >