971 ClassLoader::sync_JVMDefineClassLockFreeCounter(),
972 THREAD);
973 }
974 Handle protection_domain (THREAD, JNIHandles::resolve(pd));
975 Klass* k = SystemDictionary::resolve_from_stream(class_name,
976 class_loader,
977 protection_domain,
978 &st,
979 CHECK_NULL);
980
981 if (log_is_enabled(Debug, class, resolve) && k != NULL) {
982 trace_class_resolution(k);
983 }
984
985 return (jclass) JNIHandles::make_local(env, k->java_mirror());
986 }
987
988 enum {
989 NESTMATE = java_lang_invoke_MemberName::MN_NESTMATE_CLASS,
990 HIDDEN_CLASS = java_lang_invoke_MemberName::MN_HIDDEN_CLASS,
991 WEAK_CLASS = java_lang_invoke_MemberName::MN_WEAK_CLASS,
992 ACCESS_VM_ANNOTATIONS = java_lang_invoke_MemberName::MN_ACCESS_VM_ANNOTATIONS
993 };
994
995 /*
996 * Define a class with the specified flags that indicates if it's a nestmate,
997 * hidden, or weakly referenced from class loader.
998 */
999 static jclass jvm_lookup_define_class(JNIEnv *env, jclass lookup, const char *name,
1000 jobject loader, const jbyte *buf, jsize len, jobject pd,
1001 jboolean init, int flags, jobject classData, TRAPS) {
1002 assert(THREAD->is_Java_thread(), "must be a JavaThread");
1003 JavaThread* jt = (JavaThread*) THREAD;
1004 ResourceMark rm(THREAD);
1005
1006 Klass* lookup_k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(lookup));
1007 // Lookup class must be a non-null instance
1008 if (lookup_k == NULL) {
1009 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
1010 }
1011 assert(lookup_k->is_instance_klass(), "Lookup class must be an instance klass");
1012
1013 jboolean is_nestmate = (flags & NESTMATE) == NESTMATE;
1014 jboolean is_hidden = (flags & HIDDEN_CLASS) == HIDDEN_CLASS;
1015 jboolean is_weak = (flags & WEAK_CLASS) == WEAK_CLASS;
1016 jboolean vm_annotations = (flags & ACCESS_VM_ANNOTATIONS) == ACCESS_VM_ANNOTATIONS;
1017
1018 InstanceKlass* host_class = NULL;
1019 if (is_nestmate) {
1020 host_class = InstanceKlass::cast(lookup_k)->nest_host(CHECK_NULL);
1021 }
1022
1023 if (log_is_enabled(Info, class, nestmates)) {
1024 log_info(class, nestmates)("LookupDefineClass: %s - %s%s, %s, %s, %s",
1025 name,
1026 is_nestmate ? "with dynamic nest-host " : "non-nestmate",
1027 is_nestmate ? host_class->external_name() : "",
1028 is_hidden ? "hidden" : "not hidden",
1029 is_weak ? "weak" : "strong",
1030 vm_annotations ? "with vm annotations" : "without vm annotation");
1031 }
1032
1033 if (!is_hidden) {
1034 // classData is only applicable for hidden classes
1035 if (classData != NULL) {
1036 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "classData is only applicable for hidden classes");
1037 }
1038 if (is_nestmate) {
1039 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "dynamic nestmate is only applicable for hidden classes");
1040 }
1041 if (is_weak) {
1042 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "weak class is only applicable for hidden classes");
1043 }
1044 if (vm_annotations) {
1045 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "vm annotations only allowed for hidden classes");
1046 }
1047 if (flags) {
1048 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1049 err_msg("flag 0x%x can only be set for hidden classes", flags));
1050 }
1051 }
1052
1053
1054 // Since exceptions can be thrown, class initialization can take place
1055 // if name is NULL no check for class name in .class stream has to be made.
1056 TempNewSymbol class_name = NULL;
1057 if (name != NULL) {
1058 const int str_len = (int)strlen(name);
1059 if (str_len > Symbol::max_length()) {
1060 // It's impossible to create this class; the name cannot fit
1061 // into the constant pool.
1062 Exceptions::fthrow(THREAD_AND_LOCATION,
1063 vmSymbols::java_lang_NoClassDefFoundError(),
1064 "Class name exceeds maximum length of %d: %s",
1065 Symbol::max_length(),
1066 name);
1067 return 0;
1068 }
1069 class_name = SymbolTable::new_symbol(name, str_len);
1076
1077 Klass* defined_k;
1078 if (!is_hidden) {
1079 defined_k = SystemDictionary::resolve_from_stream(class_name,
1080 class_loader,
1081 protection_domain,
1082 &st,
1083 CHECK_NULL);
1084
1085 if (log_is_enabled(Debug, class, resolve) && defined_k != NULL) {
1086 trace_class_resolution(defined_k);
1087 }
1088 } else { // hidden
1089 Handle classData_h(THREAD, JNIHandles::resolve(classData));
1090 ClassLoadInfo cl_info(protection_domain,
1091 NULL, // unsafe_anonymous_host
1092 NULL, // cp_patches
1093 host_class,
1094 classData_h,
1095 is_hidden,
1096 is_weak,
1097 vm_annotations);
1098 defined_k = SystemDictionary::parse_stream(class_name,
1099 class_loader,
1100 &st,
1101 cl_info,
1102 CHECK_NULL);
1103 if (defined_k == NULL) {
1104 THROW_MSG_0(vmSymbols::java_lang_Error(), "Failure to define a hidden class");
1105 }
1106
1107 // The hidden class loader data has been artificially been kept alive to
1108 // this point. The mirror and any instances of this class have to keep
1109 // it alive afterwards.
1110 InstanceKlass::cast(defined_k)->class_loader_data()->dec_keep_alive();
1111
1112 if (is_nestmate && log_is_enabled(Debug, class, nestmates)) {
1113 InstanceKlass* ik = InstanceKlass::cast(defined_k);
1114 ModuleEntry* module = ik->module();
1115 const char * module_name = module->is_named() ? module->name()->as_C_string() : UNNAMED_MODULE;
1116 log_debug(class, nestmates)("Dynamic nestmate: %s/%s, nest_host %s, %s",
|
971 ClassLoader::sync_JVMDefineClassLockFreeCounter(),
972 THREAD);
973 }
974 Handle protection_domain (THREAD, JNIHandles::resolve(pd));
975 Klass* k = SystemDictionary::resolve_from_stream(class_name,
976 class_loader,
977 protection_domain,
978 &st,
979 CHECK_NULL);
980
981 if (log_is_enabled(Debug, class, resolve) && k != NULL) {
982 trace_class_resolution(k);
983 }
984
985 return (jclass) JNIHandles::make_local(env, k->java_mirror());
986 }
987
988 enum {
989 NESTMATE = java_lang_invoke_MemberName::MN_NESTMATE_CLASS,
990 HIDDEN_CLASS = java_lang_invoke_MemberName::MN_HIDDEN_CLASS,
991 STRONG_LOADER_LINK = java_lang_invoke_MemberName::MN_STRONG_LOADER_LINK,
992 ACCESS_VM_ANNOTATIONS = java_lang_invoke_MemberName::MN_ACCESS_VM_ANNOTATIONS
993 };
994
995 /*
996 * Define a class with the specified flags that indicates if it's a nestmate,
997 * hidden, or strongly referenced from class loader.
998 */
999 static jclass jvm_lookup_define_class(JNIEnv *env, jclass lookup, const char *name,
1000 jobject loader, const jbyte *buf, jsize len, jobject pd,
1001 jboolean init, int flags, jobject classData, TRAPS) {
1002 assert(THREAD->is_Java_thread(), "must be a JavaThread");
1003 JavaThread* jt = (JavaThread*) THREAD;
1004 ResourceMark rm(THREAD);
1005
1006 Klass* lookup_k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(lookup));
1007 // Lookup class must be a non-null instance
1008 if (lookup_k == NULL) {
1009 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Lookup class is null");
1010 }
1011 assert(lookup_k->is_instance_klass(), "Lookup class must be an instance klass");
1012
1013 jboolean is_nestmate = (flags & NESTMATE) == NESTMATE;
1014 jboolean is_hidden = (flags & HIDDEN_CLASS) == HIDDEN_CLASS;
1015 jboolean is_strong = (flags & STRONG_LOADER_LINK) == STRONG_LOADER_LINK;
1016 jboolean vm_annotations = (flags & ACCESS_VM_ANNOTATIONS) == ACCESS_VM_ANNOTATIONS;
1017
1018 InstanceKlass* host_class = NULL;
1019 if (is_nestmate) {
1020 host_class = InstanceKlass::cast(lookup_k)->nest_host(CHECK_NULL);
1021 }
1022
1023 if (log_is_enabled(Info, class, nestmates)) {
1024 log_info(class, nestmates)("LookupDefineClass: %s - %s%s, %s, %s, %s",
1025 name,
1026 is_nestmate ? "with dynamic nest-host " : "non-nestmate",
1027 is_nestmate ? host_class->external_name() : "",
1028 is_hidden ? "hidden" : "not hidden",
1029 is_strong ? "strong" : "weak",
1030 vm_annotations ? "with vm annotations" : "without vm annotation");
1031 }
1032
1033 if (!is_hidden) {
1034 // classData is only applicable for hidden classes
1035 if (classData != NULL) {
1036 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "classData is only applicable for hidden classes");
1037 }
1038 if (is_nestmate) {
1039 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "dynamic nestmate is only applicable for hidden classes");
1040 }
1041 if (!is_strong) {
1042 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "an ordinary class must be strongly referenced by its defining loader");
1043 }
1044 if (vm_annotations) {
1045 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "vm annotations only allowed for hidden classes");
1046 }
1047 if (flags != STRONG_LOADER_LINK) {
1048 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
1049 err_msg("invalid flag 0x%x", flags));
1050 }
1051 }
1052
1053
1054 // Since exceptions can be thrown, class initialization can take place
1055 // if name is NULL no check for class name in .class stream has to be made.
1056 TempNewSymbol class_name = NULL;
1057 if (name != NULL) {
1058 const int str_len = (int)strlen(name);
1059 if (str_len > Symbol::max_length()) {
1060 // It's impossible to create this class; the name cannot fit
1061 // into the constant pool.
1062 Exceptions::fthrow(THREAD_AND_LOCATION,
1063 vmSymbols::java_lang_NoClassDefFoundError(),
1064 "Class name exceeds maximum length of %d: %s",
1065 Symbol::max_length(),
1066 name);
1067 return 0;
1068 }
1069 class_name = SymbolTable::new_symbol(name, str_len);
1076
1077 Klass* defined_k;
1078 if (!is_hidden) {
1079 defined_k = SystemDictionary::resolve_from_stream(class_name,
1080 class_loader,
1081 protection_domain,
1082 &st,
1083 CHECK_NULL);
1084
1085 if (log_is_enabled(Debug, class, resolve) && defined_k != NULL) {
1086 trace_class_resolution(defined_k);
1087 }
1088 } else { // hidden
1089 Handle classData_h(THREAD, JNIHandles::resolve(classData));
1090 ClassLoadInfo cl_info(protection_domain,
1091 NULL, // unsafe_anonymous_host
1092 NULL, // cp_patches
1093 host_class,
1094 classData_h,
1095 is_hidden,
1096 !is_strong,
1097 vm_annotations);
1098 defined_k = SystemDictionary::parse_stream(class_name,
1099 class_loader,
1100 &st,
1101 cl_info,
1102 CHECK_NULL);
1103 if (defined_k == NULL) {
1104 THROW_MSG_0(vmSymbols::java_lang_Error(), "Failure to define a hidden class");
1105 }
1106
1107 // The hidden class loader data has been artificially been kept alive to
1108 // this point. The mirror and any instances of this class have to keep
1109 // it alive afterwards.
1110 InstanceKlass::cast(defined_k)->class_loader_data()->dec_keep_alive();
1111
1112 if (is_nestmate && log_is_enabled(Debug, class, nestmates)) {
1113 InstanceKlass* ik = InstanceKlass::cast(defined_k);
1114 ModuleEntry* module = ik->module();
1115 const char * module_name = module->is_named() ? module->name()->as_C_string() : UNNAMED_MODULE;
1116 log_debug(class, nestmates)("Dynamic nestmate: %s/%s, nest_host %s, %s",
|