25 #include "precompiled.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "classfile/systemDictionaryShared.hpp" 28 #include "classfile/verificationType.hpp" 29 #include "classfile/verifier.hpp" 30 31 VerificationType VerificationType::from_tag(u1 tag) { 32 switch (tag) { 33 case ITEM_Top: return bogus_type(); 34 case ITEM_Integer: return integer_type(); 35 case ITEM_Float: return float_type(); 36 case ITEM_Double: return double_type(); 37 case ITEM_Long: return long_type(); 38 case ITEM_Null: return null_type(); 39 default: 40 ShouldNotReachHere(); 41 return bogus_type(); 42 } 43 } 44 45 bool VerificationType::resolve_and_check_assignability(instanceKlassHandle klass, Symbol* name, 46 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, TRAPS) { 47 HandleMark hm(THREAD); 48 Klass* obj = SystemDictionary::resolve_or_fail( 49 name, Handle(THREAD, klass->class_loader()), 50 Handle(THREAD, klass->protection_domain()), true, CHECK_false); 51 if (log_is_enabled(Debug, class, resolve)) { 52 Verifier::trace_class_resolution(obj, klass()); 53 } 54 55 KlassHandle this_class(THREAD, obj); 56 57 if (this_class->is_interface() && (!from_field_is_protected || 58 from_name != vmSymbols::java_lang_Object())) { 59 // If we are not trying to access a protected field or method in 60 // java.lang.Object then, for arrays, we only allow assignability 61 // to interfaces java.lang.Cloneable and java.io.Serializable. 62 // Otherwise, we treat interfaces as java.lang.Object. 63 return !from_is_array || 64 this_class == SystemDictionary::Cloneable_klass() || 65 this_class == SystemDictionary::Serializable_klass(); 66 } else if (from_is_object) { 67 Klass* from_class = SystemDictionary::resolve_or_fail( 68 from_name, Handle(THREAD, klass->class_loader()), 69 Handle(THREAD, klass->protection_domain()), true, CHECK_false); 70 if (log_is_enabled(Debug, class, resolve)) { 71 Verifier::trace_class_resolution(from_class, klass()); 72 } 73 return InstanceKlass::cast(from_class)->is_subclass_of(this_class()); 74 } 75 76 return false; 77 } 78 79 bool VerificationType::is_reference_assignable_from( 80 const VerificationType& from, ClassVerifier* context, 81 bool from_field_is_protected, TRAPS) const { 82 instanceKlassHandle klass = context->current_class(); 83 if (from.is_null()) { 84 // null is assignable to any reference 85 return true; 86 } else if (is_null()) { 87 return false; 88 } else if (name() == from.name()) { 89 return true; 90 } else if (is_object()) { 91 // We need check the class hierarchy to check assignability 92 if (name() == vmSymbols::java_lang_Object()) { 93 // any object or array is assignable to java.lang.Object 94 return true; 95 } 96 97 if (DumpSharedSpaces && SystemDictionaryShared::add_verification_constraint(klass(), 98 name(), from.name(), from_field_is_protected, from.is_array(), 99 from.is_object())) { 100 // If add_verification_constraint() returns true, the resolution/check should be 101 // delayed until runtime. 102 return true; 103 } 104 105 return resolve_and_check_assignability(klass(), name(), from.name(), 106 from_field_is_protected, from.is_array(), from.is_object(), THREAD); 107 } else if (is_array() && from.is_array()) { 108 VerificationType comp_this = get_component(context, CHECK_false); 109 VerificationType comp_from = from.get_component(context, CHECK_false); 110 if (!comp_this.is_bogus() && !comp_from.is_bogus()) { 111 return comp_this.is_component_assignable_from(comp_from, context, 112 from_field_is_protected, CHECK_false); 113 } 114 } 115 return false; 116 } 117 118 VerificationType VerificationType::get_component(ClassVerifier *context, TRAPS) const { 119 assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array"); 120 Symbol* component; 121 switch (name()->byte_at(1)) { 122 case 'Z': return VerificationType(Boolean); 123 case 'B': return VerificationType(Byte); 124 case 'C': return VerificationType(Char); 125 case 'S': return VerificationType(Short); | 25 #include "precompiled.hpp" 26 #include "classfile/symbolTable.hpp" 27 #include "classfile/systemDictionaryShared.hpp" 28 #include "classfile/verificationType.hpp" 29 #include "classfile/verifier.hpp" 30 31 VerificationType VerificationType::from_tag(u1 tag) { 32 switch (tag) { 33 case ITEM_Top: return bogus_type(); 34 case ITEM_Integer: return integer_type(); 35 case ITEM_Float: return float_type(); 36 case ITEM_Double: return double_type(); 37 case ITEM_Long: return long_type(); 38 case ITEM_Null: return null_type(); 39 default: 40 ShouldNotReachHere(); 41 return bogus_type(); 42 } 43 } 44 45 bool VerificationType::resolve_and_check_assignability(InstanceKlass* klass, Symbol* name, 46 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, TRAPS) { 47 HandleMark hm(THREAD); 48 Klass* this_class = SystemDictionary::resolve_or_fail( 49 name, Handle(THREAD, klass->class_loader()), 50 Handle(THREAD, klass->protection_domain()), true, CHECK_false); 51 if (log_is_enabled(Debug, class, resolve)) { 52 Verifier::trace_class_resolution(this_class, klass); 53 } 54 55 if (this_class->is_interface() && (!from_field_is_protected || 56 from_name != vmSymbols::java_lang_Object())) { 57 // If we are not trying to access a protected field or method in 58 // java.lang.Object then, for arrays, we only allow assignability 59 // to interfaces java.lang.Cloneable and java.io.Serializable. 60 // Otherwise, we treat interfaces as java.lang.Object. 61 return !from_is_array || 62 this_class == SystemDictionary::Cloneable_klass() || 63 this_class == SystemDictionary::Serializable_klass(); 64 } else if (from_is_object) { 65 Klass* from_class = SystemDictionary::resolve_or_fail( 66 from_name, Handle(THREAD, klass->class_loader()), 67 Handle(THREAD, klass->protection_domain()), true, CHECK_false); 68 if (log_is_enabled(Debug, class, resolve)) { 69 Verifier::trace_class_resolution(from_class, klass); 70 } 71 return InstanceKlass::cast(from_class)->is_subclass_of(this_class); 72 } 73 74 return false; 75 } 76 77 bool VerificationType::is_reference_assignable_from( 78 const VerificationType& from, ClassVerifier* context, 79 bool from_field_is_protected, TRAPS) const { 80 InstanceKlass* klass = context->current_class(); 81 if (from.is_null()) { 82 // null is assignable to any reference 83 return true; 84 } else if (is_null()) { 85 return false; 86 } else if (name() == from.name()) { 87 return true; 88 } else if (is_object()) { 89 // We need check the class hierarchy to check assignability 90 if (name() == vmSymbols::java_lang_Object()) { 91 // any object or array is assignable to java.lang.Object 92 return true; 93 } 94 95 if (DumpSharedSpaces && SystemDictionaryShared::add_verification_constraint(klass, 96 name(), from.name(), from_field_is_protected, from.is_array(), 97 from.is_object())) { 98 // If add_verification_constraint() returns true, the resolution/check should be 99 // delayed until runtime. 100 return true; 101 } 102 103 return resolve_and_check_assignability(klass, name(), from.name(), 104 from_field_is_protected, from.is_array(), from.is_object(), THREAD); 105 } else if (is_array() && from.is_array()) { 106 VerificationType comp_this = get_component(context, CHECK_false); 107 VerificationType comp_from = from.get_component(context, CHECK_false); 108 if (!comp_this.is_bogus() && !comp_from.is_bogus()) { 109 return comp_this.is_component_assignable_from(comp_from, context, 110 from_field_is_protected, CHECK_false); 111 } 112 } 113 return false; 114 } 115 116 VerificationType VerificationType::get_component(ClassVerifier *context, TRAPS) const { 117 assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array"); 118 Symbol* component; 119 switch (name()->byte_at(1)) { 120 case 'Z': return VerificationType(Boolean); 121 case 'B': return VerificationType(Byte); 122 case 'C': return VerificationType(Char); 123 case 'S': return VerificationType(Short); |