< prev index next >

src/hotspot/share/prims/jvm.cpp

Print this page




 936                          Symbol::max_length(),
 937                          name);
 938       return 0;
 939     }
 940     class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL);
 941   }
 942 
 943   ResourceMark rm(THREAD);
 944   ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
 945   Handle class_loader (THREAD, JNIHandles::resolve(loader));
 946   if (UsePerfData) {
 947     is_lock_held_by_thread(class_loader,
 948                            ClassLoader::sync_JVMDefineClassLockFreeCounter(),
 949                            THREAD);
 950   }
 951   Handle protection_domain (THREAD, JNIHandles::resolve(pd));
 952   Klass* k = SystemDictionary::resolve_from_stream(class_name,
 953                                                    class_loader,
 954                                                    protection_domain,
 955                                                    &st,

 956                                                    CHECK_NULL);
 957 
 958   if (log_is_enabled(Debug, class, resolve) && k != NULL) {
 959     trace_class_resolution(k);
 960   }
 961 
 962   return (jclass) JNIHandles::make_local(env, k->java_mirror());
 963 }
 964 





































































































































 965 
 966 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
 967   JVMWrapper("JVM_DefineClass");
 968 
 969   return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, THREAD);
 970 JVM_END
 971 























 972 
 973 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
 974   JVMWrapper("JVM_DefineClassWithSource");
 975 
 976   return jvm_define_class_common(env, name, loader, buf, len, pd, source, THREAD);
 977 JVM_END
 978 
 979 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
 980   JVMWrapper("JVM_FindLoadedClass");
 981   ResourceMark rm(THREAD);
 982 
 983   Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
 984   Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL);
 985 
 986   const char* str   = java_lang_String::as_utf8_string(string());
 987   // Sanity check, don't expect null
 988   if (str == NULL) return NULL;
 989 
 990   const int str_len = (int)strlen(str);
 991   if (str_len > Symbol::max_length()) {


1373     return (jobjectArray)JNIHandles::make_local(env, res);
1374   }
1375 
1376   return (jobjectArray)JNIHandles::make_local(env, result());
1377 JVM_END
1378 
1379 
1380 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1381 {
1382   // ofClass is a reference to a java_lang_Class object.
1383   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1384       ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_instance_klass()) {
1385     return NULL;
1386   }
1387 
1388   bool inner_is_member = false;
1389   Klass* outer_klass
1390     = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))
1391                           )->compute_enclosing_class(&inner_is_member, CHECK_NULL);
1392   if (outer_klass == NULL)  return NULL;  // already a top-level class
1393   if (!inner_is_member)  return NULL;     // an anonymous class (inside a method)
1394   return (jclass) JNIHandles::make_local(env, outer_klass->java_mirror());
1395 }
1396 JVM_END
1397 
1398 JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
1399 {
1400   oop mirror = JNIHandles::resolve_non_null(cls);
1401   if (java_lang_Class::is_primitive(mirror) ||
1402       !java_lang_Class::as_Klass(mirror)->is_instance_klass()) {
1403     return NULL;
1404   }
1405   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1406   int ooff = 0, noff = 0;
1407   if (k->find_inner_classes_attr(&ooff, &noff, THREAD)) {
1408     if (noff != 0) {
1409       constantPoolHandle i_cp(thread, k->constants());
1410       Symbol* name = i_cp->symbol_at(noff);
1411       Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
1412       return (jstring) JNIHandles::make_local(env, str());
1413     }


1818                                              length + 1, CHECK_NULL);
1819     objArrayHandle result (THREAD, r);
1820     result->obj_at_put(0, host->java_mirror());
1821     if (length != 0) {
1822       int i;
1823       for (i = 0; i < length; i++) {
1824          int cp_index = members->at(i);
1825          Klass* k = host->constants()->klass_at(cp_index, CHECK_NULL);
1826          if (k->is_instance_klass()) {
1827            InstanceKlass* nest_host_k =
1828              InstanceKlass::cast(k)->nest_host(icce, CHECK_NULL);
1829            if (nest_host_k == host) {
1830              result->obj_at_put(i+1, k->java_mirror());
1831            }
1832            else {
1833              // k's nest host is legal but it isn't our host so
1834              // throw ICCE
1835              ResourceMark rm(THREAD);
1836              Exceptions::fthrow(THREAD_AND_LOCATION,
1837                                 icce,
1838                                 "Nest member %s in %s declares a different nest host of %s",

1839                                 k->external_name(),
1840                                 host->external_name(),
1841                                 nest_host_k->external_name()
1842                            );
1843              return NULL;
1844            }
1845          }
1846          else {
1847            // we have a bad nest member entry - throw ICCE
1848            ResourceMark rm(THREAD);
1849            Exceptions::fthrow(THREAD_AND_LOCATION,
1850                               icce,
1851                               "Class %s can not be a nest member of %s",
1852                               k->external_name(),
1853                               host->external_name()
1854                               );
1855            return NULL;
1856          }
1857       }
1858     }




 936                          Symbol::max_length(),
 937                          name);
 938       return 0;
 939     }
 940     class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL);
 941   }
 942 
 943   ResourceMark rm(THREAD);
 944   ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
 945   Handle class_loader (THREAD, JNIHandles::resolve(loader));
 946   if (UsePerfData) {
 947     is_lock_held_by_thread(class_loader,
 948                            ClassLoader::sync_JVMDefineClassLockFreeCounter(),
 949                            THREAD);
 950   }
 951   Handle protection_domain (THREAD, JNIHandles::resolve(pd));
 952   Klass* k = SystemDictionary::resolve_from_stream(class_name,
 953                                                    class_loader,
 954                                                    protection_domain,
 955                                                    &st,
 956                                                    NULL,  // dynamic_nest_host
 957                                                    CHECK_NULL);
 958 
 959   if (log_is_enabled(Debug, class, resolve) && k != NULL) {
 960     trace_class_resolution(k);
 961   }
 962 
 963   return (jclass) JNIHandles::make_local(env, k->java_mirror());
 964 }
 965 
 966 enum {
 967   NESTMATE              = java_lang_invoke_MemberName::MN_NESTMATE_CLASS,
 968   NONFINDABLE_CLASS     = java_lang_invoke_MemberName::MN_NONFINDABLE_CLASS,
 969   WEAK_CLASS            = java_lang_invoke_MemberName::MN_WEAK_CLASS,
 970   ACCESS_VM_ANNOTATIONS = java_lang_invoke_MemberName::MN_ACCESS_VM_ANNOTATIONS
 971 };
 972 
 973 /*
 974  * Define a class with the specified flags that indicates if it's a nestmate,
 975  * not findable, or weakly reachable from class loader.
 976  *
 977  * Same class may be defined by multiple threads at the same time.
 978  * Should the VM keep the classData (the one successfully defined the class)
 979  * as if a private static field is declared in the class?
 980  */
 981 static jclass jvm_lookup_define_class(JNIEnv *env, jclass lookup, const char *name,
 982                                       jobject loader, const jbyte *buf, jsize len, jobject pd,
 983                                       int flags, jobject classData, TRAPS) {
 984   assert(THREAD->is_Java_thread(), "must be a JavaThread");
 985   JavaThread* jt = (JavaThread*) THREAD;
 986   ResourceMark rm(THREAD);
 987 
 988   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(lookup));
 989   // Lookup class must be a non-null instance
 990   if (k == NULL) {
 991     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
 992   }
 993   assert(k->is_instance_klass(), "Lookup class must be an instance klass");
 994 
 995   jboolean is_nestmate = (flags & NESTMATE) == NESTMATE;
 996   jboolean is_nonfindable = (flags & NONFINDABLE_CLASS) == NONFINDABLE_CLASS;
 997   jboolean is_weak = (flags & WEAK_CLASS) == WEAK_CLASS;
 998   jboolean vm_annotations = (flags & ACCESS_VM_ANNOTATIONS) == ACCESS_VM_ANNOTATIONS;
 999 
1000   InstanceKlass* host_class = NULL;
1001   if (is_nestmate) {
1002     // we need to find the true nest-host of the lookup class,
1003     // so any exceptions in nest validation must be thrown
1004     Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
1005     host_class = InstanceKlass::cast(k)->nest_host(icce, CHECK_NULL);
1006   }
1007 
1008   // classData (constant pool patching replacement) is only applicable for nonfindable classes
1009   if (classData != NULL && !is_nonfindable) {
1010     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "classData is only applicable for nonfindable classes");
1011   }
1012 
1013   // vm_annotations only allowed for nonfindable classes
1014   if (vm_annotations && !is_nonfindable) {
1015     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "vm annotations only allowed for weak nonfindable classes");
1016   }
1017 
1018   if (log_is_enabled(Info, class, nestmates)) {
1019     log_info(class, nestmates)("LookupDefineClass: %s - %s%s, %s, %s, %s",
1020                                name,
1021                                is_nestmate ? "with dynamic nest-host " : "non-nestmate",
1022                                is_nestmate ? host_class->external_name() : "",
1023                                is_nonfindable ? "non-findable" : "findable",
1024                                is_weak ? "weak" : "strong",
1025                                vm_annotations ? "with vm annotations" : "without vm annotation");
1026   }
1027 
1028   // Since exceptions can be thrown, class initialization can take place
1029   // if name is NULL no check for class name in .class stream has to be made.
1030   TempNewSymbol class_name = NULL;
1031   if (name != NULL) {
1032     const int str_len = (int)strlen(name);
1033     if (str_len > Symbol::max_length()) {
1034       // It's impossible to create this class;  the name cannot fit
1035       // into the constant pool.
1036       Exceptions::fthrow(THREAD_AND_LOCATION,
1037                          vmSymbols::java_lang_NoClassDefFoundError(),
1038                          "Class name exceeds maximum length of %d: %s",
1039                          Symbol::max_length(),
1040                          name);
1041       return 0;
1042     }
1043     class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL);
1044   }
1045 
1046   Handle class_loader (THREAD, JNIHandles::resolve(loader));
1047   Handle protection_domain (THREAD, JNIHandles::resolve(pd));
1048   const char* source = is_nestmate ? host_class->external_name() : "__JVM_LookupDefineClass__";
1049   ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
1050 
1051   if (!is_nonfindable) {
1052     k = SystemDictionary::resolve_from_stream(class_name,
1053                                               class_loader,
1054                                               protection_domain,
1055                                               &st,
1056                                               host_class,
1057                                               CHECK_NULL);
1058 
1059     if (log_is_enabled(Debug, class, resolve) && k != NULL) {
1060       trace_class_resolution(k);
1061     }
1062   } else { //nonfindable
1063     Handle classData_h(THREAD, JNIHandles::resolve(classData));
1064     k = SystemDictionary::parse_stream(class_name,
1065                                        class_loader,
1066                                        protection_domain,
1067                                        &st,
1068                                        NULL, // unsafe_anonymous_host
1069                                        NULL, // cp_patches
1070                                        is_nonfindable,
1071                                        is_weak,
1072                                        vm_annotations,
1073                                        host_class,
1074                                        classData_h,
1075                                        CHECK_NULL);
1076     if (k == NULL) {
1077       THROW_MSG_0(vmSymbols::java_lang_Error(), "Failure to define a nonfindable class");
1078     }
1079 
1080     // The nonfindable class loader data has been artificially been kept alive to
1081     // this point. The mirror and any instances of this class have to keep
1082     // it alive afterwards.
1083     InstanceKlass::cast(k)->class_loader_data()->dec_keep_alive();
1084   }
1085 
1086   if (is_nestmate && log_is_enabled(Debug, class, nestmates)) {
1087     InstanceKlass* ik = InstanceKlass::cast(k);
1088     ModuleEntry* module = ik->module();
1089     const char * module_name = module->is_named() ? module->name()->as_C_string() : UNNAMED_MODULE;
1090     log_debug(class, nestmates)("Dynamic nestmate: %s/%s, nest_host %s, %s",
1091                                 module_name,
1092                                 ik->external_name(),
1093                                 host_class->external_name(),
1094                                 ik->is_nonfindable() ? "is non-findable" : "is findable");
1095   }
1096 
1097   return (jclass) JNIHandles::make_local(env, k->java_mirror());
1098 }
1099 
1100 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
1101   JVMWrapper("JVM_DefineClass");
1102 
1103   return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, THREAD);
1104 JVM_END
1105 
1106 /*
1107  * Define a class with the specified lookup class.
1108  *  lookup:  Lookup class
1109  *  name:    the name of the class
1110  *  loader:  defining class loader
1111  *  buf:     class bytes
1112  *  len:     length of class bytes
1113  *  pd:      protection domain
1114  *  flags:   properties of the class
1115  *  classData: private static pre-initialized field
1116  */
1117 JVM_ENTRY(jclass, JVM_LookupDefineClass(JNIEnv *env, jclass lookup, const char *name, jobject loader,
1118                       const jbyte *buf, jsize len, jobject pd, int flags, jobject classData))
1119   JVMWrapper("JVM_LookupDefineClass");
1120 
1121   if (lookup == NULL) {
1122     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
1123   }
1124 
1125   assert(buf != NULL, "buf must not be NULL");
1126 
1127   return jvm_lookup_define_class(env, lookup, name, loader, buf, len, pd, flags, classData, THREAD);
1128 JVM_END
1129 
1130 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
1131   JVMWrapper("JVM_DefineClassWithSource");
1132 
1133   return jvm_define_class_common(env, name, loader, buf, len, pd, source, THREAD);
1134 JVM_END
1135 
1136 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
1137   JVMWrapper("JVM_FindLoadedClass");
1138   ResourceMark rm(THREAD);
1139 
1140   Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
1141   Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL);
1142 
1143   const char* str   = java_lang_String::as_utf8_string(string());
1144   // Sanity check, don't expect null
1145   if (str == NULL) return NULL;
1146 
1147   const int str_len = (int)strlen(str);
1148   if (str_len > Symbol::max_length()) {


1530     return (jobjectArray)JNIHandles::make_local(env, res);
1531   }
1532 
1533   return (jobjectArray)JNIHandles::make_local(env, result());
1534 JVM_END
1535 
1536 
1537 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1538 {
1539   // ofClass is a reference to a java_lang_Class object.
1540   if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1541       ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->is_instance_klass()) {
1542     return NULL;
1543   }
1544 
1545   bool inner_is_member = false;
1546   Klass* outer_klass
1547     = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))
1548                           )->compute_enclosing_class(&inner_is_member, CHECK_NULL);
1549   if (outer_klass == NULL)  return NULL;  // already a top-level class
1550   if (!inner_is_member)  return NULL;     // a nonfindable or unsafe anonymous class (inside a method)
1551   return (jclass) JNIHandles::make_local(env, outer_klass->java_mirror());
1552 }
1553 JVM_END
1554 
1555 JVM_ENTRY(jstring, JVM_GetSimpleBinaryName(JNIEnv *env, jclass cls))
1556 {
1557   oop mirror = JNIHandles::resolve_non_null(cls);
1558   if (java_lang_Class::is_primitive(mirror) ||
1559       !java_lang_Class::as_Klass(mirror)->is_instance_klass()) {
1560     return NULL;
1561   }
1562   InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(mirror));
1563   int ooff = 0, noff = 0;
1564   if (k->find_inner_classes_attr(&ooff, &noff, THREAD)) {
1565     if (noff != 0) {
1566       constantPoolHandle i_cp(thread, k->constants());
1567       Symbol* name = i_cp->symbol_at(noff);
1568       Handle str = java_lang_String::create_from_symbol(name, CHECK_NULL);
1569       return (jstring) JNIHandles::make_local(env, str());
1570     }


1975                                              length + 1, CHECK_NULL);
1976     objArrayHandle result (THREAD, r);
1977     result->obj_at_put(0, host->java_mirror());
1978     if (length != 0) {
1979       int i;
1980       for (i = 0; i < length; i++) {
1981          int cp_index = members->at(i);
1982          Klass* k = host->constants()->klass_at(cp_index, CHECK_NULL);
1983          if (k->is_instance_klass()) {
1984            InstanceKlass* nest_host_k =
1985              InstanceKlass::cast(k)->nest_host(icce, CHECK_NULL);
1986            if (nest_host_k == host) {
1987              result->obj_at_put(i+1, k->java_mirror());
1988            }
1989            else {
1990              // k's nest host is legal but it isn't our host so
1991              // throw ICCE
1992              ResourceMark rm(THREAD);
1993              Exceptions::fthrow(THREAD_AND_LOCATION,
1994                                 icce,
1995                                 "%s.getNestMembers: Nest member %s in %s declares a different nest host of %s",
1996                                 c->external_name(),
1997                                 k->external_name(),
1998                                 host->external_name(),
1999                                 nest_host_k->external_name()
2000                            );
2001              return NULL;
2002            }
2003          }
2004          else {
2005            // we have a bad nest member entry - throw ICCE
2006            ResourceMark rm(THREAD);
2007            Exceptions::fthrow(THREAD_AND_LOCATION,
2008                               icce,
2009                               "Class %s can not be a nest member of %s",
2010                               k->external_name(),
2011                               host->external_name()
2012                               );
2013            return NULL;
2014          }
2015       }
2016     }


< prev index next >