324 if (src == NULL || dst == NULL) {
325 THROW(vmSymbols::java_lang_NullPointerException());
326 }
327 arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
328 arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
329 assert(s->is_oop(), "JVM_ArrayCopy: src not an oop");
330 assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop");
331 // Do copy
332 s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread);
333 JVM_END
334
335
336 static void set_property(Handle props, const char* key, const char* value, TRAPS) {
337 JavaValue r(T_OBJECT);
338 // public synchronized Object put(Object key, Object value);
339 HandleMark hm(THREAD);
340 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
341 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK);
342 JavaCalls::call_virtual(&r,
343 props,
344 KlassHandle(THREAD, SystemDictionary::Properties_klass()),
345 vmSymbols::put_name(),
346 vmSymbols::object_object_object_signature(),
347 key_str,
348 value_str,
349 THREAD);
350 }
351
352
353 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
354
355
356 JVM_ENTRY(jobject, JVM_InitProperties(JNIEnv *env, jobject properties))
357 JVMWrapper("JVM_InitProperties");
358 ResourceMark rm;
359
360 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
361
362 // System property list includes both user set via -D option and
363 // jvm system specific properties.
364 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
604 JVM_END
605
606
607 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
608 JVMWrapper("JVM_MonitorNotify");
609 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
610 ObjectSynchronizer::notify(obj, CHECK);
611 JVM_END
612
613
614 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
615 JVMWrapper("JVM_MonitorNotifyAll");
616 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
617 ObjectSynchronizer::notifyall(obj, CHECK);
618 JVM_END
619
620
621 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
622 JVMWrapper("JVM_Clone");
623 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
624 const KlassHandle klass (THREAD, obj->klass());
625 JvmtiVMObjectAllocEventCollector oam;
626
627 #ifdef ASSERT
628 // Just checking that the cloneable flag is set correct
629 if (obj->is_array()) {
630 guarantee(klass->is_cloneable(), "all arrays are cloneable");
631 } else {
632 guarantee(obj->is_instance(), "should be instanceOop");
633 bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass());
634 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
635 }
636 #endif
637
638 // Check if class of obj supports the Cloneable interface.
639 // All arrays are considered to be cloneable (See JLS 20.1.5)
640 if (!klass->is_cloneable()) {
641 ResourceMark rm(THREAD);
642 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
643 }
644
980 TempNewSymbol klass_name = SymbolTable::new_symbol(str, str_len, CHECK_NULL);
981
982 // Security Note:
983 // The Java level wrapper will perform the necessary security check allowing
984 // us to pass the NULL as the initiating class loader.
985 Handle h_loader(THREAD, JNIHandles::resolve(loader));
986 if (UsePerfData) {
987 is_lock_held_by_thread(h_loader,
988 ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(),
989 THREAD);
990 }
991
992 Klass* k = SystemDictionary::find_instance_or_array_klass(klass_name,
993 h_loader,
994 Handle(),
995 CHECK_NULL);
996 #if INCLUDE_CDS
997 if (k == NULL) {
998 // If the class is not already loaded, try to see if it's in the shared
999 // archive for the current classloader (h_loader).
1000 instanceKlassHandle ik = SystemDictionaryShared::find_or_load_shared_class(
1001 klass_name, h_loader, CHECK_NULL);
1002 k = ik();
1003 }
1004 #endif
1005 return (k == NULL) ? NULL :
1006 (jclass) JNIHandles::make_local(env, k->java_mirror());
1007 JVM_END
1008
1009 // Module support //////////////////////////////////////////////////////////////////////////////
1010
1011 JVM_ENTRY(void, JVM_DefineModule(JNIEnv *env, jobject module, jboolean is_open, jstring version,
1012 jstring location, const char* const* packages, jsize num_packages))
1013 JVMWrapper("JVM_DefineModule");
1014 Modules::define_module(module, version, location, packages, num_packages, CHECK);
1015 JVM_END
1016
1017 JVM_ENTRY(void, JVM_SetBootLoaderUnnamedModule(JNIEnv *env, jobject module))
1018 JVMWrapper("JVM_SetBootLoaderUnnamedModule");
1019 Modules::set_bootloader_unnamed_module(module, CHECK);
1020 JVM_END
1021
1022 JVM_ENTRY(void, JVM_AddModuleExports(JNIEnv *env, jobject from_module, const char* package, jobject to_module))
1060 assert(k->is_klass(), "just checking");
1061 name = k->external_name();
1062 }
1063 oop result = StringTable::intern((char*) name, CHECK_NULL);
1064 return (jstring) JNIHandles::make_local(env, result);
1065 JVM_END
1066
1067
1068 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1069 JVMWrapper("JVM_GetClassInterfaces");
1070 JvmtiVMObjectAllocEventCollector oam;
1071 oop mirror = JNIHandles::resolve_non_null(cls);
1072
1073 // Special handling for primitive objects
1074 if (java_lang_Class::is_primitive(mirror)) {
1075 // Primitive objects does not have any interfaces
1076 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
1077 return (jobjectArray) JNIHandles::make_local(env, r);
1078 }
1079
1080 KlassHandle klass(thread, java_lang_Class::as_Klass(mirror));
1081 // Figure size of result array
1082 int size;
1083 if (klass->is_instance_klass()) {
1084 size = InstanceKlass::cast(klass())->local_interfaces()->length();
1085 } else {
1086 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1087 size = 2;
1088 }
1089
1090 // Allocate result array
1091 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), size, CHECK_NULL);
1092 objArrayHandle result (THREAD, r);
1093 // Fill in result
1094 if (klass->is_instance_klass()) {
1095 // Regular instance klass, fill in all local interfaces
1096 for (int index = 0; index < size; index++) {
1097 Klass* k = InstanceKlass::cast(klass())->local_interfaces()->at(index);
1098 result->obj_at_put(index, k->java_mirror());
1099 }
1100 } else {
1101 // All arrays implement java.lang.Cloneable and java.io.Serializable
1102 result->obj_at_put(0, SystemDictionary::Cloneable_klass()->java_mirror());
1103 result->obj_at_put(1, SystemDictionary::Serializable_klass()->java_mirror());
1104 }
1105 return (jobjectArray) JNIHandles::make_local(env, result());
1106 JVM_END
1107
1108
1109 JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1110 JVMWrapper("JVM_IsInterface");
1111 oop mirror = JNIHandles::resolve_non_null(cls);
1112 if (java_lang_Class::is_primitive(mirror)) {
1113 return JNI_FALSE;
1114 }
1115 Klass* k = java_lang_Class::as_Klass(mirror);
1116 jboolean result = k->is_interface();
1117 assert(!result || k->is_instance_klass(),
1161 }
1162 JVM_END
1163
1164
1165 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1166 JVMWrapper("JVM_GetProtectionDomain");
1167 if (JNIHandles::resolve(cls) == NULL) {
1168 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
1169 }
1170
1171 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1172 // Primitive types does not have a protection domain.
1173 return NULL;
1174 }
1175
1176 oop pd = java_lang_Class::protection_domain(JNIHandles::resolve(cls));
1177 return (jobject) JNIHandles::make_local(env, pd);
1178 JVM_END
1179
1180
1181 static bool is_authorized(Handle context, instanceKlassHandle klass, TRAPS) {
1182 // If there is a security manager and protection domain, check the access
1183 // in the protection domain, otherwise it is authorized.
1184 if (java_lang_System::has_security_manager()) {
1185
1186 // For bootstrapping, if pd implies method isn't in the JDK, allow
1187 // this context to revert to older behavior.
1188 // In this case the isAuthorized field in AccessControlContext is also not
1189 // present.
1190 if (Universe::protection_domain_implies_method() == NULL) {
1191 return true;
1192 }
1193
1194 // Whitelist certain access control contexts
1195 if (java_security_AccessControlContext::is_authorized(context)) {
1196 return true;
1197 }
1198
1199 oop prot = klass->protection_domain();
1200 if (prot != NULL) {
1201 // Call pd.implies(new SecurityPermission("createAccessControlContext"))
1202 // in the new wrapper.
1203 methodHandle m(THREAD, Universe::protection_domain_implies_method());
1204 Handle h_prot(THREAD, prot);
1205 JavaValue result(T_BOOLEAN);
1206 JavaCallArguments args(h_prot);
1207 JavaCalls::call(&result, m, &args, CHECK_false);
1208 return (result.get_jboolean() != 0);
1209 }
1210 }
1211 return true;
1212 }
1213
1214 // Create an AccessControlContext with a protection domain with null codesource
1215 // and null permissions - which gives no permissions.
1216 oop create_dummy_access_control_context(TRAPS) {
1217 InstanceKlass* pd_klass = SystemDictionary::ProtectionDomain_klass();
1218 Handle obj = pd_klass->allocate_instance_handle(CHECK_NULL);
1219 // Call constructor ProtectionDomain(null, null);
1220 JavaValue result(T_VOID);
1221 JavaCalls::call_special(&result, obj, KlassHandle(THREAD, pd_klass),
1222 vmSymbols::object_initializer_name(),
1223 vmSymbols::codesource_permissioncollection_signature(),
1224 Handle(), Handle(), CHECK_NULL);
1225
1226 // new ProtectionDomain[] {pd};
1227 objArrayOop context = oopFactory::new_objArray(pd_klass, 1, CHECK_NULL);
1228 context->obj_at_put(0, obj());
1229
1230 // new AccessControlContext(new ProtectionDomain[] {pd})
1231 objArrayHandle h_context(THREAD, context);
1232 oop acc = java_security_AccessControlContext::create(h_context, false, Handle(), CHECK_NULL);
1233 return acc;
1234 }
1235
1236 JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException))
1237 JVMWrapper("JVM_DoPrivileged");
1238
1239 if (action == NULL) {
1240 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action");
1241 }
1242
1243 // Compute the frame initiating the do privileged operation and setup the privileged stack
1244 vframeStream vfst(thread);
1245 vfst.security_get_caller_frame(1);
1246
1247 if (vfst.at_end()) {
1248 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "no caller?");
1249 }
1250
1251 Method* method = vfst.method();
1252 instanceKlassHandle klass (THREAD, method->method_holder());
1253
1254 // Check that action object understands "Object run()"
1255 Handle h_context;
1256 if (context != NULL) {
1257 h_context = Handle(THREAD, JNIHandles::resolve(context));
1258 bool authorized = is_authorized(h_context, klass, CHECK_NULL);
1259 if (!authorized) {
1260 // Create an unprivileged access control object and call it's run function
1261 // instead.
1262 oop noprivs = create_dummy_access_control_context(CHECK_NULL);
1263 h_context = Handle(THREAD, noprivs);
1264 }
1265 }
1266
1267 // Check that action object understands "Object run()"
1268 Handle object (THREAD, JNIHandles::resolve(action));
1269
1270 // get run() method
1271 Method* m_oop = object->klass()->uncached_lookup_method(
1272 vmSymbols::run_method_name(),
1435 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1436 debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0));
1437 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
1438 return k->modifier_flags();
1439 JVM_END
1440
1441
1442 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
1443
1444 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1445 JvmtiVMObjectAllocEventCollector oam;
1446 // ofClass is a reference to a java_lang_Class object. The mirror object
1447 // of an InstanceKlass
1448
1449 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1450 ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_instance_klass()) {
1451 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
1452 return (jobjectArray)JNIHandles::make_local(env, result);
1453 }
1454
1455 instanceKlassHandle k(thread, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
1456 InnerClassesIterator iter(k);
1457
1458 if (iter.length() == 0) {
1459 // Neither an inner nor outer class
1460 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
1461 return (jobjectArray)JNIHandles::make_local(env, result);
1462 }
1463
1464 // find inner class info
1465 constantPoolHandle cp(thread, k->constants());
1466 int length = iter.length();
1467
1468 // Allocate temp. result array
1469 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), length/4, CHECK_NULL);
1470 objArrayHandle result (THREAD, r);
1471 int members = 0;
1472
1473 for (; !iter.done(); iter.next()) {
1474 int ioff = iter.inner_class_info_index();
1475 int ooff = iter.outer_class_info_index();
1476
1477 if (ioff != 0 && ooff != 0) {
1478 // Check to see if the name matches the class we're looking for
1479 // before attempting to find the class.
1480 if (cp->klass_name_at_matches(k, ooff)) {
1481 Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL);
1482 if (outer_klass == k()) {
1483 Klass* ik = cp->klass_at(ioff, CHECK_NULL);
1484 instanceKlassHandle inner_klass (THREAD, ik);
1485
1486 // Throws an exception if outer klass has not declared k as
1487 // an inner klass
1488 Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
1489
1490 result->obj_at_put(members, inner_klass->java_mirror());
1491 members++;
1492 }
1493 }
1494 }
1495 }
1496
1497 if (members != length) {
1498 // Return array of right length
1499 objArrayOop res = oopFactory::new_objArray(SystemDictionary::Class_klass(), members, CHECK_NULL);
1500 for(int i = 0; i < members; i++) {
1501 res->obj_at_put(i, result->obj_at(i));
1502 }
1503 return (jobjectArray)JNIHandles::make_local(env, res);
1504 }
1515 return NULL;
1516 }
1517
1518 bool inner_is_member = false;
1519 Klass* outer_klass
1520 = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))
1521 )->compute_enclosing_class(&inner_is_member, CHECK_NULL);
1522 if (outer_klass == NULL) return NULL; // already a top-level class
1523 if (!inner_is_member) return NULL; // an anonymous class (inside a method)
1524 return (jclass) JNIHandles::make_local(env, outer_klass->java_mirror());
1525 }
1526 JVM_END
1527
1528 JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
1529 {
1530 oop mirror = JNIHandles::resolve_non_null(cls);
1531 if (java_lang_Class::is_primitive(mirror) ||
1532 !java_lang_Class::as_Klass(mirror)->is_instance_klass()) {
1533 return NULL;
1534 }
1535 instanceKlassHandle k(THREAD, InstanceKlass::cast(java_lang_Class::as_Klass(mirror)));
1536 int ooff = 0, noff = 0;
1537 if (InstanceKlass::find_inner_classes_attr(k, &ooff, &noff, THREAD)) {
1538 if (noff != 0) {
1539 constantPoolHandle i_cp(thread, k->constants());
1540 Symbol* name = i_cp->symbol_at(noff);
1541 Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
1542 return (jstring) JNIHandles::make_local(env, str());
1543 }
1544 }
1545 return NULL;
1546 }
1547 JVM_END
1548
1549 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
1550 assert (cls != NULL, "illegal class");
1551 JVMWrapper("JVM_GetClassSignature");
1552 JvmtiVMObjectAllocEventCollector oam;
1553 ResourceMark rm(THREAD);
1554 // Return null for arrays and primatives
1555 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1573 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1574 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1575 if (k->is_instance_klass()) {
1576 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1577 return (jbyteArray) JNIHandles::make_local(env, a);
1578 }
1579 }
1580 return NULL;
1581 JVM_END
1582
1583
1584 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) {
1585 // some of this code was adapted from from jni_FromReflectedField
1586
1587 oop reflected = JNIHandles::resolve_non_null(field);
1588 oop mirror = java_lang_reflect_Field::clazz(reflected);
1589 Klass* k = java_lang_Class::as_Klass(mirror);
1590 int slot = java_lang_reflect_Field::slot(reflected);
1591 int modifiers = java_lang_reflect_Field::modifiers(reflected);
1592
1593 KlassHandle kh(THREAD, k);
1594 intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot);
1595
1596 if (modifiers & JVM_ACC_STATIC) {
1597 // for static fields we only look in the current class
1598 if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) {
1599 assert(false, "cannot find static field");
1600 return false;
1601 }
1602 } else {
1603 // for instance fields we start with the current class and work
1604 // our way up through the superclass chain
1605 if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) {
1606 assert(false, "cannot find instance field");
1607 return false;
1608 }
1609 }
1610 return true;
1611 }
1612
1613 static Method* jvm_get_method_common(jobject method) {
1614 // some of this code was adapted from from jni_FromReflectedMethod
1615
1616 oop reflected = JNIHandles::resolve_non_null(method);
1617 oop mirror = NULL;
1618 int slot = 0;
1619
1620 if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) {
1621 mirror = java_lang_reflect_Constructor::clazz(reflected);
1622 slot = java_lang_reflect_Constructor::slot(reflected);
1623 } else {
1624 assert(reflected->klass() == SystemDictionary::reflect_Method_klass(),
1625 "wrong type");
1740 return (jobjectArray)JNIHandles::make_local(env, result());
1741 }
1742 }
1743 JVM_END
1744
1745 // New (JDK 1.4) reflection implementation /////////////////////////////////////
1746
1747 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1748 {
1749 JVMWrapper("JVM_GetClassDeclaredFields");
1750 JvmtiVMObjectAllocEventCollector oam;
1751
1752 // Exclude primitive types and array types
1753 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1754 java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_array_klass()) {
1755 // Return empty array
1756 oop res = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), 0, CHECK_NULL);
1757 return (jobjectArray) JNIHandles::make_local(env, res);
1758 }
1759
1760 instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
1761 constantPoolHandle cp(THREAD, k->constants());
1762
1763 // Ensure class is linked
1764 k->link_class(CHECK_NULL);
1765
1766 // Allocate result
1767 int num_fields;
1768
1769 if (publicOnly) {
1770 num_fields = 0;
1771 for (JavaFieldStream fs(k()); !fs.done(); fs.next()) {
1772 if (fs.access_flags().is_public()) ++num_fields;
1773 }
1774 } else {
1775 num_fields = k->java_fields_count();
1776 }
1777
1778 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), num_fields, CHECK_NULL);
1779 objArrayHandle result (THREAD, r);
1780
1781 int out_idx = 0;
1782 fieldDescriptor fd;
1783 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
1784 if (!publicOnly || fs.access_flags().is_public()) {
1785 fd.reinitialize(k(), fs.index());
1786 oop field = Reflection::new_field(&fd, CHECK_NULL);
1787 result->obj_at_put(out_idx, field);
1788 ++out_idx;
1789 }
1790 }
1791 assert(out_idx == num_fields, "just checking");
1792 return (jobjectArray) JNIHandles::make_local(env, result());
1793 }
1794 JVM_END
1795
1796 static bool select_method(methodHandle method, bool want_constructor) {
1797 if (want_constructor) {
1798 return (method->is_initializer() && !method->is_static());
1799 } else {
1800 return (!method->is_initializer() && !method->is_overpass());
1801 }
1802 }
1803
1804 static jobjectArray get_class_declared_methods_helper(
1805 JNIEnv *env,
1806 jclass ofClass, jboolean publicOnly,
1807 bool want_constructor,
1808 Klass* klass, TRAPS) {
1809
1810 JvmtiVMObjectAllocEventCollector oam;
1811
1812 // Exclude primitive types and array types
1813 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
1814 || java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_array_klass()) {
1815 // Return empty array
1816 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1817 return (jobjectArray) JNIHandles::make_local(env, res);
1818 }
1819
1820 instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
1821
1822 // Ensure class is linked
1823 k->link_class(CHECK_NULL);
1824
1825 Array<Method*>* methods = k->methods();
1826 int methods_length = methods->length();
1827
1828 // Save original method_idnum in case of redefinition, which can change
1829 // the idnum of obsolete methods. The new method will have the same idnum
1830 // but if we refresh the methods array, the counts will be wrong.
1831 ResourceMark rm(THREAD);
1832 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1833 int num_methods = 0;
1834
1835 for (int i = 0; i < methods_length; i++) {
1836 methodHandle method(THREAD, methods->at(i));
1837 if (select_method(method, want_constructor)) {
1838 if (!publicOnly || method->is_public()) {
1839 idnums->push(method->method_idnum());
1840 ++num_methods;
1896 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1897 }
1898
1899 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1900 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
1901 }
1902 JVM_END
1903
1904
1905 // Constant pool access //////////////////////////////////////////////////////////
1906
1907 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
1908 {
1909 JVMWrapper("JVM_GetClassConstantPool");
1910 JvmtiVMObjectAllocEventCollector oam;
1911
1912 // Return null for primitives and arrays
1913 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1914 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1915 if (k->is_instance_klass()) {
1916 instanceKlassHandle k_h(THREAD, k);
1917 Handle jcp = reflect_ConstantPool::create(CHECK_NULL);
1918 reflect_ConstantPool::set_cp(jcp(), k_h->constants());
1919 return JNIHandles::make_local(jcp());
1920 }
1921 }
1922 return NULL;
1923 }
1924 JVM_END
1925
1926
1927 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused))
1928 {
1929 JVMWrapper("JVM_ConstantPoolGetSize");
1930 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
1931 return cp->length();
1932 }
1933 JVM_END
1934
1935
1936 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index))
1958 }
1959 Klass* k = ConstantPool::klass_at_if_loaded(cp, index);
1960 if (k == NULL) return NULL;
1961 return (jclass) JNIHandles::make_local(k->java_mirror());
1962 }
1963 JVM_END
1964
1965 static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
1966 constantTag tag = cp->tag_at(index);
1967 if (!tag.is_method() && !tag.is_interface_method()) {
1968 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1969 }
1970 int klass_ref = cp->uncached_klass_ref_index_at(index);
1971 Klass* k_o;
1972 if (force_resolution) {
1973 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1974 } else {
1975 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
1976 if (k_o == NULL) return NULL;
1977 }
1978 instanceKlassHandle k(THREAD, k_o);
1979 Symbol* name = cp->uncached_name_ref_at(index);
1980 Symbol* sig = cp->uncached_signature_ref_at(index);
1981 methodHandle m (THREAD, k->find_method(name, sig));
1982 if (m.is_null()) {
1983 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1984 }
1985 oop method;
1986 if (!m->is_initializer() || m->is_static()) {
1987 method = Reflection::new_method(m, true, CHECK_NULL);
1988 } else {
1989 method = Reflection::new_constructor(m, CHECK_NULL);
1990 }
1991 return JNIHandles::make_local(method);
1992 }
1993
1994 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
1995 {
1996 JVMWrapper("JVM_ConstantPoolGetMethodAt");
1997 JvmtiVMObjectAllocEventCollector oam;
1998 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2009 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2010 bounds_check(cp, index, CHECK_NULL);
2011 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2012 return res;
2013 }
2014 JVM_END
2015
2016 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
2017 constantTag tag = cp->tag_at(index);
2018 if (!tag.is_field()) {
2019 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2020 }
2021 int klass_ref = cp->uncached_klass_ref_index_at(index);
2022 Klass* k_o;
2023 if (force_resolution) {
2024 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2025 } else {
2026 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2027 if (k_o == NULL) return NULL;
2028 }
2029 instanceKlassHandle k(THREAD, k_o);
2030 Symbol* name = cp->uncached_name_ref_at(index);
2031 Symbol* sig = cp->uncached_signature_ref_at(index);
2032 fieldDescriptor fd;
2033 Klass* target_klass = k->find_field(name, sig, &fd);
2034 if (target_klass == NULL) {
2035 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
2036 }
2037 oop field = Reflection::new_field(&fd, CHECK_NULL);
2038 return JNIHandles::make_local(field);
2039 }
2040
2041 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index))
2042 {
2043 JVMWrapper("JVM_ConstantPoolGetFieldAt");
2044 JvmtiVMObjectAllocEventCollector oam;
2045 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2046 bounds_check(cp, index, CHECK_NULL);
2047 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
2048 return res;
2049 }
2599 default:
2600 fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
2601 }
2602 ShouldNotReachHere();
2603 return NULL;
2604 JVM_END
2605
2606
2607 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2608 JVMWrapper("JVM_GetCPFieldModifiers");
2609 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2610 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2611 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2612 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2613 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2614 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2615 switch (cp->tag_at(cp_index).value()) {
2616 case JVM_CONSTANT_Fieldref: {
2617 Symbol* name = cp->uncached_name_ref_at(cp_index);
2618 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2619 for (JavaFieldStream fs(k_called); !fs.done(); fs.next()) {
2620 if (fs.name() == name && fs.signature() == signature) {
2621 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
2622 }
2623 }
2624 return -1;
2625 }
2626 default:
2627 fatal("JVM_GetCPFieldModifiers: illegal constant");
2628 }
2629 ShouldNotReachHere();
2630 return 0;
2631 JVM_END
2632
2633
2634 JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2635 JVMWrapper("JVM_GetCPMethodModifiers");
2636 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2637 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2638 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2639 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2753
2754 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
2755
2756 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock
2757 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or
2758 // OSThread objects. The exception to this rule is when the target object is the thread
2759 // doing the operation, in which case we know that the thread won't exit until the
2760 // operation is done (all exits being voluntary). There are a few cases where it is
2761 // rather silly to do operations on yourself, like resuming yourself or asking whether
2762 // you are alive. While these can still happen, they are not subject to deadlocks if
2763 // the lock is held while the operation occurs (this is not the case for suspend, for
2764 // instance), and are very unlikely. Because IsAlive needs to be fast and its
2765 // implementation is local to this file, we always lock Threads_lock for that one.
2766
2767 static void thread_entry(JavaThread* thread, TRAPS) {
2768 HandleMark hm(THREAD);
2769 Handle obj(THREAD, thread->threadObj());
2770 JavaValue result(T_VOID);
2771 JavaCalls::call_virtual(&result,
2772 obj,
2773 KlassHandle(THREAD, SystemDictionary::Thread_klass()),
2774 vmSymbols::run_method_name(),
2775 vmSymbols::void_method_signature(),
2776 THREAD);
2777 }
2778
2779
2780 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2781 JVMWrapper("JVM_StartThread");
2782 JavaThread *native_thread = NULL;
2783
2784 // We cannot hold the Threads_lock when we throw an exception,
2785 // due to rank ordering issues. Example: we might need to grab the
2786 // Heap_lock while we construct the exception.
2787 bool throw_illegal_thread_state = false;
2788
2789 // We must release the Threads_lock before we can post a jvmti event
2790 // in Thread::start.
2791 {
2792 // Ensure that the C++ Thread and OSThread structures aren't freed before
2793 // we operate.
3209 JVM_END
3210
3211
3212 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
3213 JVMWrapper("JVM_GetClassContext");
3214 ResourceMark rm(THREAD);
3215 JvmtiVMObjectAllocEventCollector oam;
3216 vframeStream vfst(thread);
3217
3218 if (SystemDictionary::reflect_CallerSensitive_klass() != NULL) {
3219 // This must only be called from SecurityManager.getClassContext
3220 Method* m = vfst.method();
3221 if (!(m->method_holder() == SystemDictionary::SecurityManager_klass() &&
3222 m->name() == vmSymbols::getClassContext_name() &&
3223 m->signature() == vmSymbols::void_class_array_signature())) {
3224 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext");
3225 }
3226 }
3227
3228 // Collect method holders
3229 GrowableArray<KlassHandle>* klass_array = new GrowableArray<KlassHandle>();
3230 for (; !vfst.at_end(); vfst.security_next()) {
3231 Method* m = vfst.method();
3232 // Native frames are not returned
3233 if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) {
3234 Klass* holder = m->method_holder();
3235 assert(holder->is_klass(), "just checking");
3236 klass_array->append(holder);
3237 }
3238 }
3239
3240 // Create result array of type [Ljava/lang/Class;
3241 objArrayOop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), klass_array->length(), CHECK_NULL);
3242 // Fill in mirrors corresponding to method holders
3243 for (int i = 0; i < klass_array->length(); i++) {
3244 result->obj_at_put(i, klass_array->at(i)->java_mirror());
3245 }
3246
3247 return (jobjectArray) JNIHandles::make_local(env, result);
3248 JVM_END
3249
3576 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
3577 VM_Exit::block_if_vm_exited();
3578 JVMWrapper("JVM_RawMonitorExit");
3579 ((Mutex*) mon)->jvm_raw_unlock();
3580 }
3581
3582
3583 // Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
3584
3585 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3586 Handle loader, Handle protection_domain,
3587 jboolean throwError, TRAPS) {
3588 // Security Note:
3589 // The Java level wrapper will perform the necessary security check allowing
3590 // us to pass the NULL as the initiating class loader. The VM is responsible for
3591 // the checkPackageAccess relative to the initiating class loader via the
3592 // protection_domain. The protection_domain is passed as NULL by the java code
3593 // if there is no security manager in 3-arg Class.forName().
3594 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3595
3596 KlassHandle klass_handle(THREAD, klass);
3597 // Check if we should initialize the class
3598 if (init && klass_handle->is_instance_klass()) {
3599 klass_handle->initialize(CHECK_NULL);
3600 }
3601 return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror());
3602 }
3603
3604
3605 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3606
3607 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3608 JVMWrapper("JVM_InvokeMethod");
3609 Handle method_handle;
3610 if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3611 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3612 Handle receiver(THREAD, JNIHandles::resolve(obj));
3613 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3614 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3615 jobject res = JNIHandles::make_local(env, result);
3616 if (JvmtiExport::should_post_vm_object_alloc()) {
3617 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3618 assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
3619 if (java_lang_Class::is_primitive(ret_type)) {
3620 // Only for primitive type vm allocates memory for java object.
3621 // See box() method.
3729 return properties;
3730 JVM_END
3731
3732 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
3733 {
3734 JVMWrapper("JVM_GetEnclosingMethodInfo");
3735 JvmtiVMObjectAllocEventCollector oam;
3736
3737 if (ofClass == NULL) {
3738 return NULL;
3739 }
3740 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
3741 // Special handling for primitive objects
3742 if (java_lang_Class::is_primitive(mirror())) {
3743 return NULL;
3744 }
3745 Klass* k = java_lang_Class::as_Klass(mirror());
3746 if (!k->is_instance_klass()) {
3747 return NULL;
3748 }
3749 instanceKlassHandle ik_h(THREAD, k);
3750 int encl_method_class_idx = ik_h->enclosing_method_class_index();
3751 if (encl_method_class_idx == 0) {
3752 return NULL;
3753 }
3754 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::Object_klass(), 3, CHECK_NULL);
3755 objArrayHandle dest(THREAD, dest_o);
3756 Klass* enc_k = ik_h->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
3757 dest->obj_at_put(0, enc_k->java_mirror());
3758 int encl_method_method_idx = ik_h->enclosing_method_method_index();
3759 if (encl_method_method_idx != 0) {
3760 Symbol* sym = ik_h->constants()->symbol_at(
3761 extract_low_short_from_int(
3762 ik_h->constants()->name_and_type_at(encl_method_method_idx)));
3763 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3764 dest->obj_at_put(1, str());
3765 sym = ik_h->constants()->symbol_at(
3766 extract_high_short_from_int(
3767 ik_h->constants()->name_and_type_at(encl_method_method_idx)));
3768 str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3769 dest->obj_at_put(2, str());
3770 }
3771 return (jobjectArray) JNIHandles::make_local(dest());
3772 }
3773 JVM_END
3774
3775 JVM_ENTRY(void, JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size))
3776 {
3777 memset(info, 0, info_size);
3778
3779 info->jvm_version = Abstract_VM_Version::jvm_version();
3780 info->patch_version = Abstract_VM_Version::vm_patch_version();
3781
3782 // when we add a new capability in the jvm_version_info struct, we should also
3783 // consider to expose this new capability in the sun.rt.jvmCapabilities jvmstat
3784 // counter defined in runtimeService.cpp.
3785 info->is_attachable = AttachListener::is_attach_supported();
3786 }
3787 JVM_END
3788
3789 // Returns an array of java.lang.String objects containing the input arguments to the VM.
3790 JVM_ENTRY(jobjectArray, JVM_GetVmArguments(JNIEnv *env))
3791 ResourceMark rm(THREAD);
3792
3793 if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) {
3794 return NULL;
3795 }
3796
3797 char** vm_flags = Arguments::jvm_flags_array();
3798 char** vm_args = Arguments::jvm_args_array();
3799 int num_flags = Arguments::num_jvm_flags();
3800 int num_args = Arguments::num_jvm_args();
3801
3802 instanceKlassHandle ik (THREAD, SystemDictionary::String_klass());
3803 objArrayOop r = oopFactory::new_objArray(ik(), num_args + num_flags, CHECK_NULL);
3804 objArrayHandle result_h(THREAD, r);
3805
3806 int index = 0;
3807 for (int j = 0; j < num_flags; j++, index++) {
3808 Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
3809 result_h->obj_at_put(index, h());
3810 }
3811 for (int i = 0; i < num_args; i++, index++) {
3812 Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
3813 result_h->obj_at_put(index, h());
3814 }
3815 return (jobjectArray) JNIHandles::make_local(env, result_h());
3816 JVM_END
3817
3818 JVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name))
3819 return os::get_signal_number(name);
3820 JVM_END
|
324 if (src == NULL || dst == NULL) {
325 THROW(vmSymbols::java_lang_NullPointerException());
326 }
327 arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
328 arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
329 assert(s->is_oop(), "JVM_ArrayCopy: src not an oop");
330 assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop");
331 // Do copy
332 s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread);
333 JVM_END
334
335
336 static void set_property(Handle props, const char* key, const char* value, TRAPS) {
337 JavaValue r(T_OBJECT);
338 // public synchronized Object put(Object key, Object value);
339 HandleMark hm(THREAD);
340 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
341 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK);
342 JavaCalls::call_virtual(&r,
343 props,
344 SystemDictionary::Properties_klass(),
345 vmSymbols::put_name(),
346 vmSymbols::object_object_object_signature(),
347 key_str,
348 value_str,
349 THREAD);
350 }
351
352
353 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
354
355
356 JVM_ENTRY(jobject, JVM_InitProperties(JNIEnv *env, jobject properties))
357 JVMWrapper("JVM_InitProperties");
358 ResourceMark rm;
359
360 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
361
362 // System property list includes both user set via -D option and
363 // jvm system specific properties.
364 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
604 JVM_END
605
606
607 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
608 JVMWrapper("JVM_MonitorNotify");
609 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
610 ObjectSynchronizer::notify(obj, CHECK);
611 JVM_END
612
613
614 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
615 JVMWrapper("JVM_MonitorNotifyAll");
616 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
617 ObjectSynchronizer::notifyall(obj, CHECK);
618 JVM_END
619
620
621 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
622 JVMWrapper("JVM_Clone");
623 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
624 Klass* klass = obj->klass();
625 JvmtiVMObjectAllocEventCollector oam;
626
627 #ifdef ASSERT
628 // Just checking that the cloneable flag is set correct
629 if (obj->is_array()) {
630 guarantee(klass->is_cloneable(), "all arrays are cloneable");
631 } else {
632 guarantee(obj->is_instance(), "should be instanceOop");
633 bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass());
634 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
635 }
636 #endif
637
638 // Check if class of obj supports the Cloneable interface.
639 // All arrays are considered to be cloneable (See JLS 20.1.5)
640 if (!klass->is_cloneable()) {
641 ResourceMark rm(THREAD);
642 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
643 }
644
980 TempNewSymbol klass_name = SymbolTable::new_symbol(str, str_len, CHECK_NULL);
981
982 // Security Note:
983 // The Java level wrapper will perform the necessary security check allowing
984 // us to pass the NULL as the initiating class loader.
985 Handle h_loader(THREAD, JNIHandles::resolve(loader));
986 if (UsePerfData) {
987 is_lock_held_by_thread(h_loader,
988 ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(),
989 THREAD);
990 }
991
992 Klass* k = SystemDictionary::find_instance_or_array_klass(klass_name,
993 h_loader,
994 Handle(),
995 CHECK_NULL);
996 #if INCLUDE_CDS
997 if (k == NULL) {
998 // If the class is not already loaded, try to see if it's in the shared
999 // archive for the current classloader (h_loader).
1000 k = SystemDictionaryShared::find_or_load_shared_class(klass_name, h_loader, CHECK_NULL);
1001 }
1002 #endif
1003 return (k == NULL) ? NULL :
1004 (jclass) JNIHandles::make_local(env, k->java_mirror());
1005 JVM_END
1006
1007 // Module support //////////////////////////////////////////////////////////////////////////////
1008
1009 JVM_ENTRY(void, JVM_DefineModule(JNIEnv *env, jobject module, jboolean is_open, jstring version,
1010 jstring location, const char* const* packages, jsize num_packages))
1011 JVMWrapper("JVM_DefineModule");
1012 Modules::define_module(module, version, location, packages, num_packages, CHECK);
1013 JVM_END
1014
1015 JVM_ENTRY(void, JVM_SetBootLoaderUnnamedModule(JNIEnv *env, jobject module))
1016 JVMWrapper("JVM_SetBootLoaderUnnamedModule");
1017 Modules::set_bootloader_unnamed_module(module, CHECK);
1018 JVM_END
1019
1020 JVM_ENTRY(void, JVM_AddModuleExports(JNIEnv *env, jobject from_module, const char* package, jobject to_module))
1058 assert(k->is_klass(), "just checking");
1059 name = k->external_name();
1060 }
1061 oop result = StringTable::intern((char*) name, CHECK_NULL);
1062 return (jstring) JNIHandles::make_local(env, result);
1063 JVM_END
1064
1065
1066 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
1067 JVMWrapper("JVM_GetClassInterfaces");
1068 JvmtiVMObjectAllocEventCollector oam;
1069 oop mirror = JNIHandles::resolve_non_null(cls);
1070
1071 // Special handling for primitive objects
1072 if (java_lang_Class::is_primitive(mirror)) {
1073 // Primitive objects does not have any interfaces
1074 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
1075 return (jobjectArray) JNIHandles::make_local(env, r);
1076 }
1077
1078 Klass* klass = java_lang_Class::as_Klass(mirror);
1079 // Figure size of result array
1080 int size;
1081 if (klass->is_instance_klass()) {
1082 size = InstanceKlass::cast(klass)->local_interfaces()->length();
1083 } else {
1084 assert(klass->is_objArray_klass() || klass->is_typeArray_klass(), "Illegal mirror klass");
1085 size = 2;
1086 }
1087
1088 // Allocate result array
1089 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), size, CHECK_NULL);
1090 objArrayHandle result (THREAD, r);
1091 // Fill in result
1092 if (klass->is_instance_klass()) {
1093 // Regular instance klass, fill in all local interfaces
1094 for (int index = 0; index < size; index++) {
1095 Klass* k = InstanceKlass::cast(klass)->local_interfaces()->at(index);
1096 result->obj_at_put(index, k->java_mirror());
1097 }
1098 } else {
1099 // All arrays implement java.lang.Cloneable and java.io.Serializable
1100 result->obj_at_put(0, SystemDictionary::Cloneable_klass()->java_mirror());
1101 result->obj_at_put(1, SystemDictionary::Serializable_klass()->java_mirror());
1102 }
1103 return (jobjectArray) JNIHandles::make_local(env, result());
1104 JVM_END
1105
1106
1107 JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
1108 JVMWrapper("JVM_IsInterface");
1109 oop mirror = JNIHandles::resolve_non_null(cls);
1110 if (java_lang_Class::is_primitive(mirror)) {
1111 return JNI_FALSE;
1112 }
1113 Klass* k = java_lang_Class::as_Klass(mirror);
1114 jboolean result = k->is_interface();
1115 assert(!result || k->is_instance_klass(),
1159 }
1160 JVM_END
1161
1162
1163 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
1164 JVMWrapper("JVM_GetProtectionDomain");
1165 if (JNIHandles::resolve(cls) == NULL) {
1166 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
1167 }
1168
1169 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1170 // Primitive types does not have a protection domain.
1171 return NULL;
1172 }
1173
1174 oop pd = java_lang_Class::protection_domain(JNIHandles::resolve(cls));
1175 return (jobject) JNIHandles::make_local(env, pd);
1176 JVM_END
1177
1178
1179 static bool is_authorized(Handle context, InstanceKlass* klass, TRAPS) {
1180 // If there is a security manager and protection domain, check the access
1181 // in the protection domain, otherwise it is authorized.
1182 if (java_lang_System::has_security_manager()) {
1183
1184 // For bootstrapping, if pd implies method isn't in the JDK, allow
1185 // this context to revert to older behavior.
1186 // In this case the isAuthorized field in AccessControlContext is also not
1187 // present.
1188 if (Universe::protection_domain_implies_method() == NULL) {
1189 return true;
1190 }
1191
1192 // Whitelist certain access control contexts
1193 if (java_security_AccessControlContext::is_authorized(context)) {
1194 return true;
1195 }
1196
1197 oop prot = klass->protection_domain();
1198 if (prot != NULL) {
1199 // Call pd.implies(new SecurityPermission("createAccessControlContext"))
1200 // in the new wrapper.
1201 methodHandle m(THREAD, Universe::protection_domain_implies_method());
1202 Handle h_prot(THREAD, prot);
1203 JavaValue result(T_BOOLEAN);
1204 JavaCallArguments args(h_prot);
1205 JavaCalls::call(&result, m, &args, CHECK_false);
1206 return (result.get_jboolean() != 0);
1207 }
1208 }
1209 return true;
1210 }
1211
1212 // Create an AccessControlContext with a protection domain with null codesource
1213 // and null permissions - which gives no permissions.
1214 oop create_dummy_access_control_context(TRAPS) {
1215 InstanceKlass* pd_klass = SystemDictionary::ProtectionDomain_klass();
1216 Handle obj = pd_klass->allocate_instance_handle(CHECK_NULL);
1217 // Call constructor ProtectionDomain(null, null);
1218 JavaValue result(T_VOID);
1219 JavaCalls::call_special(&result, obj, pd_klass,
1220 vmSymbols::object_initializer_name(),
1221 vmSymbols::codesource_permissioncollection_signature(),
1222 Handle(), Handle(), CHECK_NULL);
1223
1224 // new ProtectionDomain[] {pd};
1225 objArrayOop context = oopFactory::new_objArray(pd_klass, 1, CHECK_NULL);
1226 context->obj_at_put(0, obj());
1227
1228 // new AccessControlContext(new ProtectionDomain[] {pd})
1229 objArrayHandle h_context(THREAD, context);
1230 oop acc = java_security_AccessControlContext::create(h_context, false, Handle(), CHECK_NULL);
1231 return acc;
1232 }
1233
1234 JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException))
1235 JVMWrapper("JVM_DoPrivileged");
1236
1237 if (action == NULL) {
1238 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action");
1239 }
1240
1241 // Compute the frame initiating the do privileged operation and setup the privileged stack
1242 vframeStream vfst(thread);
1243 vfst.security_get_caller_frame(1);
1244
1245 if (vfst.at_end()) {
1246 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "no caller?");
1247 }
1248
1249 Method* method = vfst.method();
1250 InstanceKlass* klass = method->method_holder();
1251
1252 // Check that action object understands "Object run()"
1253 Handle h_context;
1254 if (context != NULL) {
1255 h_context = Handle(THREAD, JNIHandles::resolve(context));
1256 bool authorized = is_authorized(h_context, klass, CHECK_NULL);
1257 if (!authorized) {
1258 // Create an unprivileged access control object and call it's run function
1259 // instead.
1260 oop noprivs = create_dummy_access_control_context(CHECK_NULL);
1261 h_context = Handle(THREAD, noprivs);
1262 }
1263 }
1264
1265 // Check that action object understands "Object run()"
1266 Handle object (THREAD, JNIHandles::resolve(action));
1267
1268 // get run() method
1269 Method* m_oop = object->klass()->uncached_lookup_method(
1270 vmSymbols::run_method_name(),
1433 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1434 debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0));
1435 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
1436 return k->modifier_flags();
1437 JVM_END
1438
1439
1440 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
1441
1442 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1443 JvmtiVMObjectAllocEventCollector oam;
1444 // ofClass is a reference to a java_lang_Class object. The mirror object
1445 // of an InstanceKlass
1446
1447 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1448 ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_instance_klass()) {
1449 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
1450 return (jobjectArray)JNIHandles::make_local(env, result);
1451 }
1452
1453 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
1454 InnerClassesIterator iter(k);
1455
1456 if (iter.length() == 0) {
1457 // Neither an inner nor outer class
1458 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
1459 return (jobjectArray)JNIHandles::make_local(env, result);
1460 }
1461
1462 // find inner class info
1463 constantPoolHandle cp(thread, k->constants());
1464 int length = iter.length();
1465
1466 // Allocate temp. result array
1467 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), length/4, CHECK_NULL);
1468 objArrayHandle result (THREAD, r);
1469 int members = 0;
1470
1471 for (; !iter.done(); iter.next()) {
1472 int ioff = iter.inner_class_info_index();
1473 int ooff = iter.outer_class_info_index();
1474
1475 if (ioff != 0 && ooff != 0) {
1476 // Check to see if the name matches the class we're looking for
1477 // before attempting to find the class.
1478 if (cp->klass_name_at_matches(k, ooff)) {
1479 Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL);
1480 if (outer_klass == k) {
1481 Klass* ik = cp->klass_at(ioff, CHECK_NULL);
1482 InstanceKlass* inner_klass = InstanceKlass::cast(ik);
1483
1484 // Throws an exception if outer klass has not declared k as
1485 // an inner klass
1486 Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
1487
1488 result->obj_at_put(members, inner_klass->java_mirror());
1489 members++;
1490 }
1491 }
1492 }
1493 }
1494
1495 if (members != length) {
1496 // Return array of right length
1497 objArrayOop res = oopFactory::new_objArray(SystemDictionary::Class_klass(), members, CHECK_NULL);
1498 for(int i = 0; i < members; i++) {
1499 res->obj_at_put(i, result->obj_at(i));
1500 }
1501 return (jobjectArray)JNIHandles::make_local(env, res);
1502 }
1513 return NULL;
1514 }
1515
1516 bool inner_is_member = false;
1517 Klass* outer_klass
1518 = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))
1519 )->compute_enclosing_class(&inner_is_member, CHECK_NULL);
1520 if (outer_klass == NULL) return NULL; // already a top-level class
1521 if (!inner_is_member) return NULL; // an anonymous class (inside a method)
1522 return (jclass) JNIHandles::make_local(env, outer_klass->java_mirror());
1523 }
1524 JVM_END
1525
1526 JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
1527 {
1528 oop mirror = JNIHandles::resolve_non_null(cls);
1529 if (java_lang_Class::is_primitive(mirror) ||
1530 !java_lang_Class::as_Klass(mirror)->is_instance_klass()) {
1531 return NULL;
1532 }
1533 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1534 int ooff = 0, noff = 0;
1535 if (InstanceKlass::find_inner_classes_attr(k, &ooff, &noff, THREAD)) {
1536 if (noff != 0) {
1537 constantPoolHandle i_cp(thread, k->constants());
1538 Symbol* name = i_cp->symbol_at(noff);
1539 Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
1540 return (jstring) JNIHandles::make_local(env, str());
1541 }
1542 }
1543 return NULL;
1544 }
1545 JVM_END
1546
1547 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
1548 assert (cls != NULL, "illegal class");
1549 JVMWrapper("JVM_GetClassSignature");
1550 JvmtiVMObjectAllocEventCollector oam;
1551 ResourceMark rm(THREAD);
1552 // Return null for arrays and primatives
1553 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1571 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1572 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
1573 if (k->is_instance_klass()) {
1574 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
1575 return (jbyteArray) JNIHandles::make_local(env, a);
1576 }
1577 }
1578 return NULL;
1579 JVM_END
1580
1581
1582 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) {
1583 // some of this code was adapted from from jni_FromReflectedField
1584
1585 oop reflected = JNIHandles::resolve_non_null(field);
1586 oop mirror = java_lang_reflect_Field::clazz(reflected);
1587 Klass* k = java_lang_Class::as_Klass(mirror);
1588 int slot = java_lang_reflect_Field::slot(reflected);
1589 int modifiers = java_lang_reflect_Field::modifiers(reflected);
1590
1591 InstanceKlass* ik = InstanceKlass::cast(k);
1592 intptr_t offset = ik->field_offset(slot);
1593
1594 if (modifiers & JVM_ACC_STATIC) {
1595 // for static fields we only look in the current class
1596 if (!ik->find_local_field_from_offset(offset, true, &fd)) {
1597 assert(false, "cannot find static field");
1598 return false;
1599 }
1600 } else {
1601 // for instance fields we start with the current class and work
1602 // our way up through the superclass chain
1603 if (!ik->find_field_from_offset(offset, false, &fd)) {
1604 assert(false, "cannot find instance field");
1605 return false;
1606 }
1607 }
1608 return true;
1609 }
1610
1611 static Method* jvm_get_method_common(jobject method) {
1612 // some of this code was adapted from from jni_FromReflectedMethod
1613
1614 oop reflected = JNIHandles::resolve_non_null(method);
1615 oop mirror = NULL;
1616 int slot = 0;
1617
1618 if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) {
1619 mirror = java_lang_reflect_Constructor::clazz(reflected);
1620 slot = java_lang_reflect_Constructor::slot(reflected);
1621 } else {
1622 assert(reflected->klass() == SystemDictionary::reflect_Method_klass(),
1623 "wrong type");
1738 return (jobjectArray)JNIHandles::make_local(env, result());
1739 }
1740 }
1741 JVM_END
1742
1743 // New (JDK 1.4) reflection implementation /////////////////////////////////////
1744
1745 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1746 {
1747 JVMWrapper("JVM_GetClassDeclaredFields");
1748 JvmtiVMObjectAllocEventCollector oam;
1749
1750 // Exclude primitive types and array types
1751 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1752 java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_array_klass()) {
1753 // Return empty array
1754 oop res = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), 0, CHECK_NULL);
1755 return (jobjectArray) JNIHandles::make_local(env, res);
1756 }
1757
1758 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
1759 constantPoolHandle cp(THREAD, k->constants());
1760
1761 // Ensure class is linked
1762 k->link_class(CHECK_NULL);
1763
1764 // Allocate result
1765 int num_fields;
1766
1767 if (publicOnly) {
1768 num_fields = 0;
1769 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
1770 if (fs.access_flags().is_public()) ++num_fields;
1771 }
1772 } else {
1773 num_fields = k->java_fields_count();
1774 }
1775
1776 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), num_fields, CHECK_NULL);
1777 objArrayHandle result (THREAD, r);
1778
1779 int out_idx = 0;
1780 fieldDescriptor fd;
1781 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
1782 if (!publicOnly || fs.access_flags().is_public()) {
1783 fd.reinitialize(k, fs.index());
1784 oop field = Reflection::new_field(&fd, CHECK_NULL);
1785 result->obj_at_put(out_idx, field);
1786 ++out_idx;
1787 }
1788 }
1789 assert(out_idx == num_fields, "just checking");
1790 return (jobjectArray) JNIHandles::make_local(env, result());
1791 }
1792 JVM_END
1793
1794 static bool select_method(methodHandle method, bool want_constructor) {
1795 if (want_constructor) {
1796 return (method->is_initializer() && !method->is_static());
1797 } else {
1798 return (!method->is_initializer() && !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 // Exclude primitive types and array types
1811 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
1812 || java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_array_klass()) {
1813 // Return empty array
1814 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
1815 return (jobjectArray) JNIHandles::make_local(env, res);
1816 }
1817
1818 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
1819
1820 // Ensure class is linked
1821 k->link_class(CHECK_NULL);
1822
1823 Array<Method*>* methods = k->methods();
1824 int methods_length = methods->length();
1825
1826 // Save original method_idnum in case of redefinition, which can change
1827 // the idnum of obsolete methods. The new method will have the same idnum
1828 // but if we refresh the methods array, the counts will be wrong.
1829 ResourceMark rm(THREAD);
1830 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
1831 int num_methods = 0;
1832
1833 for (int i = 0; i < methods_length; i++) {
1834 methodHandle method(THREAD, methods->at(i));
1835 if (select_method(method, want_constructor)) {
1836 if (!publicOnly || method->is_public()) {
1837 idnums->push(method->method_idnum());
1838 ++num_methods;
1894 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1895 }
1896
1897 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1898 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
1899 }
1900 JVM_END
1901
1902
1903 // Constant pool access //////////////////////////////////////////////////////////
1904
1905 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
1906 {
1907 JVMWrapper("JVM_GetClassConstantPool");
1908 JvmtiVMObjectAllocEventCollector oam;
1909
1910 // Return null for primitives and arrays
1911 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1912 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
1913 if (k->is_instance_klass()) {
1914 InstanceKlass* k_h = InstanceKlass::cast(k);
1915 Handle jcp = reflect_ConstantPool::create(CHECK_NULL);
1916 reflect_ConstantPool::set_cp(jcp(), k_h->constants());
1917 return JNIHandles::make_local(jcp());
1918 }
1919 }
1920 return NULL;
1921 }
1922 JVM_END
1923
1924
1925 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused))
1926 {
1927 JVMWrapper("JVM_ConstantPoolGetSize");
1928 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
1929 return cp->length();
1930 }
1931 JVM_END
1932
1933
1934 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index))
1956 }
1957 Klass* k = ConstantPool::klass_at_if_loaded(cp, index);
1958 if (k == NULL) return NULL;
1959 return (jclass) JNIHandles::make_local(k->java_mirror());
1960 }
1961 JVM_END
1962
1963 static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
1964 constantTag tag = cp->tag_at(index);
1965 if (!tag.is_method() && !tag.is_interface_method()) {
1966 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1967 }
1968 int klass_ref = cp->uncached_klass_ref_index_at(index);
1969 Klass* k_o;
1970 if (force_resolution) {
1971 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1972 } else {
1973 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
1974 if (k_o == NULL) return NULL;
1975 }
1976 InstanceKlass* k = InstanceKlass::cast(k_o);
1977 Symbol* name = cp->uncached_name_ref_at(index);
1978 Symbol* sig = cp->uncached_signature_ref_at(index);
1979 methodHandle m (THREAD, k->find_method(name, sig));
1980 if (m.is_null()) {
1981 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1982 }
1983 oop method;
1984 if (!m->is_initializer() || m->is_static()) {
1985 method = Reflection::new_method(m, true, CHECK_NULL);
1986 } else {
1987 method = Reflection::new_constructor(m, CHECK_NULL);
1988 }
1989 return JNIHandles::make_local(method);
1990 }
1991
1992 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
1993 {
1994 JVMWrapper("JVM_ConstantPoolGetMethodAt");
1995 JvmtiVMObjectAllocEventCollector oam;
1996 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2007 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2008 bounds_check(cp, index, CHECK_NULL);
2009 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
2010 return res;
2011 }
2012 JVM_END
2013
2014 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
2015 constantTag tag = cp->tag_at(index);
2016 if (!tag.is_field()) {
2017 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
2018 }
2019 int klass_ref = cp->uncached_klass_ref_index_at(index);
2020 Klass* k_o;
2021 if (force_resolution) {
2022 k_o = cp->klass_at(klass_ref, CHECK_NULL);
2023 } else {
2024 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
2025 if (k_o == NULL) return NULL;
2026 }
2027 InstanceKlass* k = InstanceKlass::cast(k_o);
2028 Symbol* name = cp->uncached_name_ref_at(index);
2029 Symbol* sig = cp->uncached_signature_ref_at(index);
2030 fieldDescriptor fd;
2031 Klass* target_klass = k->find_field(name, sig, &fd);
2032 if (target_klass == NULL) {
2033 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
2034 }
2035 oop field = Reflection::new_field(&fd, CHECK_NULL);
2036 return JNIHandles::make_local(field);
2037 }
2038
2039 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index))
2040 {
2041 JVMWrapper("JVM_ConstantPoolGetFieldAt");
2042 JvmtiVMObjectAllocEventCollector oam;
2043 constantPoolHandle cp = constantPoolHandle(THREAD, reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
2044 bounds_check(cp, index, CHECK_NULL);
2045 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
2046 return res;
2047 }
2597 default:
2598 fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
2599 }
2600 ShouldNotReachHere();
2601 return NULL;
2602 JVM_END
2603
2604
2605 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2606 JVMWrapper("JVM_GetCPFieldModifiers");
2607 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2608 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2609 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2610 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2611 ConstantPool* cp = InstanceKlass::cast(k)->constants();
2612 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
2613 switch (cp->tag_at(cp_index).value()) {
2614 case JVM_CONSTANT_Fieldref: {
2615 Symbol* name = cp->uncached_name_ref_at(cp_index);
2616 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
2617 InstanceKlass* ik = InstanceKlass::cast(k_called);
2618 for (JavaFieldStream fs(ik); !fs.done(); fs.next()) {
2619 if (fs.name() == name && fs.signature() == signature) {
2620 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
2621 }
2622 }
2623 return -1;
2624 }
2625 default:
2626 fatal("JVM_GetCPFieldModifiers: illegal constant");
2627 }
2628 ShouldNotReachHere();
2629 return 0;
2630 JVM_END
2631
2632
2633 JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2634 JVMWrapper("JVM_GetCPMethodModifiers");
2635 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
2636 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
2637 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2638 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2752
2753 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
2754
2755 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock
2756 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or
2757 // OSThread objects. The exception to this rule is when the target object is the thread
2758 // doing the operation, in which case we know that the thread won't exit until the
2759 // operation is done (all exits being voluntary). There are a few cases where it is
2760 // rather silly to do operations on yourself, like resuming yourself or asking whether
2761 // you are alive. While these can still happen, they are not subject to deadlocks if
2762 // the lock is held while the operation occurs (this is not the case for suspend, for
2763 // instance), and are very unlikely. Because IsAlive needs to be fast and its
2764 // implementation is local to this file, we always lock Threads_lock for that one.
2765
2766 static void thread_entry(JavaThread* thread, TRAPS) {
2767 HandleMark hm(THREAD);
2768 Handle obj(THREAD, thread->threadObj());
2769 JavaValue result(T_VOID);
2770 JavaCalls::call_virtual(&result,
2771 obj,
2772 SystemDictionary::Thread_klass(),
2773 vmSymbols::run_method_name(),
2774 vmSymbols::void_method_signature(),
2775 THREAD);
2776 }
2777
2778
2779 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2780 JVMWrapper("JVM_StartThread");
2781 JavaThread *native_thread = NULL;
2782
2783 // We cannot hold the Threads_lock when we throw an exception,
2784 // due to rank ordering issues. Example: we might need to grab the
2785 // Heap_lock while we construct the exception.
2786 bool throw_illegal_thread_state = false;
2787
2788 // We must release the Threads_lock before we can post a jvmti event
2789 // in Thread::start.
2790 {
2791 // Ensure that the C++ Thread and OSThread structures aren't freed before
2792 // we operate.
3208 JVM_END
3209
3210
3211 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
3212 JVMWrapper("JVM_GetClassContext");
3213 ResourceMark rm(THREAD);
3214 JvmtiVMObjectAllocEventCollector oam;
3215 vframeStream vfst(thread);
3216
3217 if (SystemDictionary::reflect_CallerSensitive_klass() != NULL) {
3218 // This must only be called from SecurityManager.getClassContext
3219 Method* m = vfst.method();
3220 if (!(m->method_holder() == SystemDictionary::SecurityManager_klass() &&
3221 m->name() == vmSymbols::getClassContext_name() &&
3222 m->signature() == vmSymbols::void_class_array_signature())) {
3223 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext");
3224 }
3225 }
3226
3227 // Collect method holders
3228 GrowableArray<Klass*>* klass_array = new GrowableArray<Klass*>();
3229 for (; !vfst.at_end(); vfst.security_next()) {
3230 Method* m = vfst.method();
3231 // Native frames are not returned
3232 if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) {
3233 Klass* holder = m->method_holder();
3234 assert(holder->is_klass(), "just checking");
3235 klass_array->append(holder);
3236 }
3237 }
3238
3239 // Create result array of type [Ljava/lang/Class;
3240 objArrayOop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), klass_array->length(), CHECK_NULL);
3241 // Fill in mirrors corresponding to method holders
3242 for (int i = 0; i < klass_array->length(); i++) {
3243 result->obj_at_put(i, klass_array->at(i)->java_mirror());
3244 }
3245
3246 return (jobjectArray) JNIHandles::make_local(env, result);
3247 JVM_END
3248
3575 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
3576 VM_Exit::block_if_vm_exited();
3577 JVMWrapper("JVM_RawMonitorExit");
3578 ((Mutex*) mon)->jvm_raw_unlock();
3579 }
3580
3581
3582 // Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
3583
3584 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
3585 Handle loader, Handle protection_domain,
3586 jboolean throwError, TRAPS) {
3587 // Security Note:
3588 // The Java level wrapper will perform the necessary security check allowing
3589 // us to pass the NULL as the initiating class loader. The VM is responsible for
3590 // the checkPackageAccess relative to the initiating class loader via the
3591 // protection_domain. The protection_domain is passed as NULL by the java code
3592 // if there is no security manager in 3-arg Class.forName().
3593 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3594
3595 // Check if we should initialize the class
3596 if (init && klass->is_instance_klass()) {
3597 klass->initialize(CHECK_NULL);
3598 }
3599 return (jclass) JNIHandles::make_local(env, klass->java_mirror());
3600 }
3601
3602
3603 // Method ///////////////////////////////////////////////////////////////////////////////////////////
3604
3605 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
3606 JVMWrapper("JVM_InvokeMethod");
3607 Handle method_handle;
3608 if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
3609 method_handle = Handle(THREAD, JNIHandles::resolve(method));
3610 Handle receiver(THREAD, JNIHandles::resolve(obj));
3611 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
3612 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
3613 jobject res = JNIHandles::make_local(env, result);
3614 if (JvmtiExport::should_post_vm_object_alloc()) {
3615 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
3616 assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
3617 if (java_lang_Class::is_primitive(ret_type)) {
3618 // Only for primitive type vm allocates memory for java object.
3619 // See box() method.
3727 return properties;
3728 JVM_END
3729
3730 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
3731 {
3732 JVMWrapper("JVM_GetEnclosingMethodInfo");
3733 JvmtiVMObjectAllocEventCollector oam;
3734
3735 if (ofClass == NULL) {
3736 return NULL;
3737 }
3738 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
3739 // Special handling for primitive objects
3740 if (java_lang_Class::is_primitive(mirror())) {
3741 return NULL;
3742 }
3743 Klass* k = java_lang_Class::as_Klass(mirror());
3744 if (!k->is_instance_klass()) {
3745 return NULL;
3746 }
3747 InstanceKlass* ik = InstanceKlass::cast(k);
3748 int encl_method_class_idx = ik->enclosing_method_class_index();
3749 if (encl_method_class_idx == 0) {
3750 return NULL;
3751 }
3752 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::Object_klass(), 3, CHECK_NULL);
3753 objArrayHandle dest(THREAD, dest_o);
3754 Klass* enc_k = ik->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
3755 dest->obj_at_put(0, enc_k->java_mirror());
3756 int encl_method_method_idx = ik->enclosing_method_method_index();
3757 if (encl_method_method_idx != 0) {
3758 Symbol* sym = ik->constants()->symbol_at(
3759 extract_low_short_from_int(
3760 ik->constants()->name_and_type_at(encl_method_method_idx)));
3761 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3762 dest->obj_at_put(1, str());
3763 sym = ik->constants()->symbol_at(
3764 extract_high_short_from_int(
3765 ik->constants()->name_and_type_at(encl_method_method_idx)));
3766 str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
3767 dest->obj_at_put(2, str());
3768 }
3769 return (jobjectArray) JNIHandles::make_local(dest());
3770 }
3771 JVM_END
3772
3773 JVM_ENTRY(void, JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size))
3774 {
3775 memset(info, 0, info_size);
3776
3777 info->jvm_version = Abstract_VM_Version::jvm_version();
3778 info->patch_version = Abstract_VM_Version::vm_patch_version();
3779
3780 // when we add a new capability in the jvm_version_info struct, we should also
3781 // consider to expose this new capability in the sun.rt.jvmCapabilities jvmstat
3782 // counter defined in runtimeService.cpp.
3783 info->is_attachable = AttachListener::is_attach_supported();
3784 }
3785 JVM_END
3786
3787 // Returns an array of java.lang.String objects containing the input arguments to the VM.
3788 JVM_ENTRY(jobjectArray, JVM_GetVmArguments(JNIEnv *env))
3789 ResourceMark rm(THREAD);
3790
3791 if (Arguments::num_jvm_args() == 0 && Arguments::num_jvm_flags() == 0) {
3792 return NULL;
3793 }
3794
3795 char** vm_flags = Arguments::jvm_flags_array();
3796 char** vm_args = Arguments::jvm_args_array();
3797 int num_flags = Arguments::num_jvm_flags();
3798 int num_args = Arguments::num_jvm_args();
3799
3800 InstanceKlass* ik = SystemDictionary::String_klass();
3801 objArrayOop r = oopFactory::new_objArray(ik, num_args + num_flags, CHECK_NULL);
3802 objArrayHandle result_h(THREAD, r);
3803
3804 int index = 0;
3805 for (int j = 0; j < num_flags; j++, index++) {
3806 Handle h = java_lang_String::create_from_platform_dependent_str(vm_flags[j], CHECK_NULL);
3807 result_h->obj_at_put(index, h());
3808 }
3809 for (int i = 0; i < num_args; i++, index++) {
3810 Handle h = java_lang_String::create_from_platform_dependent_str(vm_args[i], CHECK_NULL);
3811 result_h->obj_at_put(index, h());
3812 }
3813 return (jobjectArray) JNIHandles::make_local(env, result_h());
3814 JVM_END
3815
3816 JVM_ENTRY_NO_ENV(jint, JVM_FindSignal(const char *name))
3817 return os::get_signal_number(name);
3818 JVM_END
|