< prev index next >

src/hotspot/share/prims/jvm.cpp

Print this page




 953  * Define a class with the specified flags that indicates if it's a nestmate,
 954  * not findable, or weakly reachable from class loader.
 955  *
 956  * Same class may be defined by multiple threads at the same time.
 957  * Should the VM keep the classData (the one successfully defined the class)
 958  * as if a private static field is declared in the class?
 959  */
 960 static jclass jvm_lookup_define_class(JNIEnv *env, jclass lookup, const char *name,
 961                                       jobject loader, const jbyte *buf, jsize len, jobject pd,
 962                                       int flags, jobject classData, TRAPS) {
 963   assert(THREAD->is_Java_thread(), "must be a JavaThread");
 964   JavaThread* jt = (JavaThread*) THREAD;
 965   ResourceMark rm(THREAD);
 966 
 967   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(lookup));
 968   // Lookup class must be a non-null instance
 969   if (k == NULL) {
 970     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
 971   }
 972   assert(k->is_instance_klass(), "Lookup class must be an instance klass");
 973   InstanceKlass* lookup_class = InstanceKlass::cast(k);
 974 
 975   jboolean is_nestmate = (flags & NESTMATE) == NESTMATE;
 976   jboolean is_nonfindable = (flags & NONFINDABLE_CLASS) == NONFINDABLE_CLASS;
 977   jboolean is_weak = (flags & WEAK_CLASS) == WEAK_CLASS;
 978   jboolean vm_annotations = (flags & ACCESS_VM_ANNOTATIONS) == ACCESS_VM_ANNOTATIONS;
 979 








 980   // classData (constant pool patching replacement) is only applicable for nonfindable classes
 981   if (classData != NULL && !is_nonfindable) {
 982     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "classData is only applicable for nonfindable classes");
 983   }
 984 
 985   // vm_annotations only allowed for nonfindable classes
 986   if (vm_annotations && !is_nonfindable) {
 987     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "vm annotations only allowed for weak nonfindable classes");
 988   }
 989 
 990   if (log_is_enabled(Info, class, nestmates)) {
 991     log_info(class, nestmates)("Lookup define class %s is_nestmate %d is_nonfindable %d is_weak %d vm annotations %d name %s",
 992                                 lookup_class->external_name(), is_nestmate, is_nonfindable, is_weak, vm_annotations, name);





 993   }
 994 
 995   // Since exceptions can be thrown, class initialization can take place
 996   // if name is NULL no check for class name in .class stream has to be made.
 997   TempNewSymbol class_name = NULL;
 998   if (name != NULL) {
 999     const int str_len = (int)strlen(name);
1000     if (str_len > Symbol::max_length()) {
1001       // It's impossible to create this class;  the name cannot fit
1002       // into the constant pool.
1003       Exceptions::fthrow(THREAD_AND_LOCATION,
1004                          vmSymbols::java_lang_NoClassDefFoundError(),
1005                          "Class name exceeds maximum length of %d: %s",
1006                          Symbol::max_length(),
1007                          name);
1008       return 0;
1009     }
1010     class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL);
1011   }
1012 
1013   Handle class_loader (THREAD, JNIHandles::resolve(loader));
1014   Handle protection_domain (THREAD, JNIHandles::resolve(pd));
1015   const char* source = is_nestmate ? lookup_class->external_name() : "__JVM_LookupDefineClass__";
1016   ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
1017 
1018   if (!is_nonfindable) {
1019     k = SystemDictionary::resolve_from_stream(class_name,
1020                                               class_loader,
1021                                               protection_domain,
1022                                               &st,
1023                                               is_nestmate ? lookup_class : NULL,
1024                                               CHECK_NULL);
1025 
1026     if (log_is_enabled(Debug, class, resolve) && k != NULL) {
1027       trace_class_resolution(k);
1028     }
1029   } else { //nonfindable
1030     k = SystemDictionary::parse_stream(class_name,
1031                                        class_loader,
1032                                        protection_domain,
1033                                        &st,
1034                                        NULL, // unsafe_anonymous_host
1035                                        NULL, // cp_patches
1036                                        is_nonfindable,
1037                                        true, // is_weak - workaround to allow access to VM annotations
1038                                        is_nestmate ? lookup_class : NULL,
1039                                        CHECK_NULL);
1040     if (k == NULL) {
1041       THROW_MSG_0(vmSymbols::java_lang_Error(), "Failure to define a nonfindable class");
1042     }
1043 
1044     // The nonfindable class loader data has been artificially been kept alive to
1045     // this point. The mirror and any instances of this class have to keep
1046     // it alive afterwards.
1047     InstanceKlass::cast(k)->class_loader_data()->dec_keep_alive();
1048   }
1049 
1050   if (is_nestmate && log_is_enabled(Debug, class, nestmates)) {
1051     InstanceKlass* ik = InstanceKlass::cast(k);
1052     ModuleEntry* module = ik->module();
1053     const char * module_name = module->is_named() ? module->name()->as_C_string() : UNNAMED_MODULE;
1054     log_debug(class, nestmates)("Dynamic nestmate: %s/%s, nest_host %s, %s",
1055                                 module_name,
1056                                 ik->external_name(),
1057                                 lookup_class->external_name(),
1058                                 ik->is_nonfindable() ? "is non-findable" : "is findable");
1059   }
1060 
1061   return (jclass) JNIHandles::make_local(env, k->java_mirror());
1062 }
1063 
1064 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
1065   JVMWrapper("JVM_DefineClass");
1066 
1067   return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, THREAD);
1068 JVM_END
1069 
1070 /*
1071  * Define a class with the specified lookup class.
1072  *  lookup:  Lookup class
1073  *  name:    the name of the class
1074  *  loader:  defining class loader
1075  *  buf:     class bytes
1076  *  len:     length of class bytes
1077  *  pd:      protection domain


2084                                              length + 1, CHECK_NULL);
2085     objArrayHandle result (THREAD, r);
2086     result->obj_at_put(0, host->java_mirror());
2087     if (length != 0) {
2088       int i;
2089       for (i = 0; i < length; i++) {
2090          int cp_index = members->at(i);
2091          Klass* k = host->constants()->klass_at(cp_index, CHECK_NULL);
2092          if (k->is_instance_klass()) {
2093            InstanceKlass* nest_host_k =
2094              InstanceKlass::cast(k)->nest_host(icce, CHECK_NULL);
2095            if (nest_host_k == host) {
2096              result->obj_at_put(i+1, k->java_mirror());
2097            }
2098            else {
2099              // k's nest host is legal but it isn't our host so
2100              // throw ICCE
2101              ResourceMark rm(THREAD);
2102              Exceptions::fthrow(THREAD_AND_LOCATION,
2103                                 icce,
2104                                 "Nest member %s in %s declares a different nest host of %s",

2105                                 k->external_name(),
2106                                 host->external_name(),
2107                                 nest_host_k->external_name()
2108                            );
2109              return NULL;
2110            }
2111          }
2112          else {
2113            // we have a bad nest member entry - throw ICCE
2114            ResourceMark rm(THREAD);
2115            Exceptions::fthrow(THREAD_AND_LOCATION,
2116                               icce,
2117                               "Class %s can not be a nest member of %s",
2118                               k->external_name(),
2119                               host->external_name()
2120                               );
2121            return NULL;
2122          }
2123       }
2124     }




 953  * Define a class with the specified flags that indicates if it's a nestmate,
 954  * not findable, or weakly reachable from class loader.
 955  *
 956  * Same class may be defined by multiple threads at the same time.
 957  * Should the VM keep the classData (the one successfully defined the class)
 958  * as if a private static field is declared in the class?
 959  */
 960 static jclass jvm_lookup_define_class(JNIEnv *env, jclass lookup, const char *name,
 961                                       jobject loader, const jbyte *buf, jsize len, jobject pd,
 962                                       int flags, jobject classData, TRAPS) {
 963   assert(THREAD->is_Java_thread(), "must be a JavaThread");
 964   JavaThread* jt = (JavaThread*) THREAD;
 965   ResourceMark rm(THREAD);
 966 
 967   Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(lookup));
 968   // Lookup class must be a non-null instance
 969   if (k == NULL) {
 970     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
 971   }
 972   assert(k->is_instance_klass(), "Lookup class must be an instance klass");

 973 
 974   jboolean is_nestmate = (flags & NESTMATE) == NESTMATE;
 975   jboolean is_nonfindable = (flags & NONFINDABLE_CLASS) == NONFINDABLE_CLASS;
 976   jboolean is_weak = (flags & WEAK_CLASS) == WEAK_CLASS;
 977   jboolean vm_annotations = (flags & ACCESS_VM_ANNOTATIONS) == ACCESS_VM_ANNOTATIONS;
 978 
 979   InstanceKlass* host_class = NULL;
 980   if (is_nestmate) {
 981     // we need to find the true nest-host of the lookup class,
 982     // so any exceptions in nest validation must be thrown
 983     Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
 984     host_class = InstanceKlass::cast(k)->nest_host(icce, CHECK_NULL);
 985   }
 986 
 987   // classData (constant pool patching replacement) is only applicable for nonfindable classes
 988   if (classData != NULL && !is_nonfindable) {
 989     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "classData is only applicable for nonfindable classes");
 990   }
 991 
 992   // vm_annotations only allowed for nonfindable classes
 993   if (vm_annotations && !is_nonfindable) {
 994     THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "vm annotations only allowed for weak nonfindable classes");
 995   }
 996 
 997   if (log_is_enabled(Info, class, nestmates)) {
 998     log_info(class, nestmates)("LookupDefineClass: %s - %s%s, %s, %s, %s",
 999                                name,
1000                                is_nestmate ? "with dynamic nest-host " : "non-nestmate",
1001                                is_nestmate ? host_class->external_name() : "",
1002                                is_nonfindable ? "non-findable" : "findable",
1003                                is_weak ? "weak" : "strong",
1004                                vm_annotations ? "with vm annotations" : "without vm annotation");
1005   }
1006 
1007   // Since exceptions can be thrown, class initialization can take place
1008   // if name is NULL no check for class name in .class stream has to be made.
1009   TempNewSymbol class_name = NULL;
1010   if (name != NULL) {
1011     const int str_len = (int)strlen(name);
1012     if (str_len > Symbol::max_length()) {
1013       // It's impossible to create this class;  the name cannot fit
1014       // into the constant pool.
1015       Exceptions::fthrow(THREAD_AND_LOCATION,
1016                          vmSymbols::java_lang_NoClassDefFoundError(),
1017                          "Class name exceeds maximum length of %d: %s",
1018                          Symbol::max_length(),
1019                          name);
1020       return 0;
1021     }
1022     class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL);
1023   }
1024 
1025   Handle class_loader (THREAD, JNIHandles::resolve(loader));
1026   Handle protection_domain (THREAD, JNIHandles::resolve(pd));
1027   const char* source = is_nestmate ? host_class->external_name() : "__JVM_LookupDefineClass__";
1028   ClassFileStream st((u1*)buf, len, source, ClassFileStream::verify);
1029 
1030   if (!is_nonfindable) {
1031     k = SystemDictionary::resolve_from_stream(class_name,
1032                                               class_loader,
1033                                               protection_domain,
1034                                               &st,
1035                                               host_class,
1036                                               CHECK_NULL);
1037 
1038     if (log_is_enabled(Debug, class, resolve) && k != NULL) {
1039       trace_class_resolution(k);
1040     }
1041   } else { //nonfindable
1042     k = SystemDictionary::parse_stream(class_name,
1043                                        class_loader,
1044                                        protection_domain,
1045                                        &st,
1046                                        NULL, // unsafe_anonymous_host
1047                                        NULL, // cp_patches
1048                                        is_nonfindable,
1049                                        true, // is_weak - workaround to allow access to VM annotations
1050                                        host_class,
1051                                        CHECK_NULL);
1052     if (k == NULL) {
1053       THROW_MSG_0(vmSymbols::java_lang_Error(), "Failure to define a nonfindable class");
1054     }
1055 
1056     // The nonfindable class loader data has been artificially been kept alive to
1057     // this point. The mirror and any instances of this class have to keep
1058     // it alive afterwards.
1059     InstanceKlass::cast(k)->class_loader_data()->dec_keep_alive();
1060   }
1061 
1062   if (is_nestmate && log_is_enabled(Debug, class, nestmates)) {
1063     InstanceKlass* ik = InstanceKlass::cast(k);
1064     ModuleEntry* module = ik->module();
1065     const char * module_name = module->is_named() ? module->name()->as_C_string() : UNNAMED_MODULE;
1066     log_debug(class, nestmates)("Dynamic nestmate: %s/%s, nest_host %s, %s",
1067                                 module_name,
1068                                 ik->external_name(),
1069                                 host_class->external_name(),
1070                                 ik->is_nonfindable() ? "is non-findable" : "is findable");
1071   }
1072 
1073   return (jclass) JNIHandles::make_local(env, k->java_mirror());
1074 }
1075 
1076 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
1077   JVMWrapper("JVM_DefineClass");
1078 
1079   return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, THREAD);
1080 JVM_END
1081 
1082 /*
1083  * Define a class with the specified lookup class.
1084  *  lookup:  Lookup class
1085  *  name:    the name of the class
1086  *  loader:  defining class loader
1087  *  buf:     class bytes
1088  *  len:     length of class bytes
1089  *  pd:      protection domain


2096                                              length + 1, CHECK_NULL);
2097     objArrayHandle result (THREAD, r);
2098     result->obj_at_put(0, host->java_mirror());
2099     if (length != 0) {
2100       int i;
2101       for (i = 0; i < length; i++) {
2102          int cp_index = members->at(i);
2103          Klass* k = host->constants()->klass_at(cp_index, CHECK_NULL);
2104          if (k->is_instance_klass()) {
2105            InstanceKlass* nest_host_k =
2106              InstanceKlass::cast(k)->nest_host(icce, CHECK_NULL);
2107            if (nest_host_k == host) {
2108              result->obj_at_put(i+1, k->java_mirror());
2109            }
2110            else {
2111              // k's nest host is legal but it isn't our host so
2112              // throw ICCE
2113              ResourceMark rm(THREAD);
2114              Exceptions::fthrow(THREAD_AND_LOCATION,
2115                                 icce,
2116                                 "%s.getNestMembers: Nest member %s in %s declares a different nest host of %s",
2117                                 c->external_name(),
2118                                 k->external_name(),
2119                                 host->external_name(),
2120                                 nest_host_k->external_name()
2121                            );
2122              return NULL;
2123            }
2124          }
2125          else {
2126            // we have a bad nest member entry - throw ICCE
2127            ResourceMark rm(THREAD);
2128            Exceptions::fthrow(THREAD_AND_LOCATION,
2129                               icce,
2130                               "Class %s can not be a nest member of %s",
2131                               k->external_name(),
2132                               host->external_name()
2133                               );
2134            return NULL;
2135          }
2136       }
2137     }


< prev index next >