1 /*
  2  * Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "classfile/symbolTable.hpp"
 27 #include "classfile/systemDictionary.hpp"
 28 #include "classfile/systemDictionaryShared.hpp"
 29 #include "classfile/verificationType.hpp"
 30 #include "classfile/verifier.hpp"
 31 #include "classfile/vmClasses.hpp"
 32 #include "classfile/vmSymbols.hpp"
 33 #include "logging/log.hpp"
 34 #include "oops/klass.inline.hpp"
 35 #include "runtime/arguments.hpp"
 36 #include "runtime/handles.inline.hpp"
 37 
 38 VerificationType VerificationType::from_tag(u1 tag) {
 39   switch (tag) {
 40     case ITEM_Top:     return bogus_type();
 41     case ITEM_Integer: return integer_type();
 42     case ITEM_Float:   return float_type();
 43     case ITEM_Double:  return double_type();
 44     case ITEM_Long:    return long_type();
 45     case ITEM_Null:    return null_type();
 46     default:
 47       ShouldNotReachHere();
 48       return bogus_type();
 49   }
 50 }
 51 
 52 bool VerificationType::resolve_and_check_assignability(InstanceKlass* klass, Symbol* name,
 53          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, TRAPS) {
 54   HandleMark hm(THREAD);
 55   Klass* this_class;
 56   if (klass->is_hidden() && klass->name() == name) {
 57     this_class = klass;
 58   } else {
 59     this_class = SystemDictionary::resolve_or_fail(
 60       name, Handle(THREAD, klass->class_loader()),
 61       Handle(THREAD, klass->protection_domain()), true, CHECK_false);
 62     if (log_is_enabled(Debug, class, resolve)) {
 63       Verifier::trace_class_resolution(this_class, klass);
 64     }
 65   }
 66 
 67   if (this_class->access_flags().is_inline_type()) return false;
 68   if (this_class->is_interface() && (!from_field_is_protected ||
 69       from_name != vmSymbols::java_lang_Object())) {
 70     // If we are not trying to access a protected field or method in
 71     // java.lang.Object then, for arrays, we only allow assignability
 72     // to interfaces java.lang.Cloneable, java.io.Serializable,
 73     // and java.lang.IdentityObject.
 74     // Otherwise, we treat interfaces as java.lang.Object.
 75     return !from_is_array ||
 76       this_class == vmClasses::Cloneable_klass() ||
 77       this_class == vmClasses::Serializable_klass() ||
 78       this_class == vmClasses::IdentityObject_klass();
 79   } else if (from_is_object) {
 80     Klass* from_class;
 81     if (klass->is_hidden() && klass->name() == from_name) {
 82       from_class = klass;
 83     } else {
 84       from_class = SystemDictionary::resolve_or_fail(
 85         from_name, Handle(THREAD, klass->class_loader()),
 86         Handle(THREAD, klass->protection_domain()), true, CHECK_false);
 87       if (log_is_enabled(Debug, class, resolve)) {
 88         Verifier::trace_class_resolution(from_class, klass);
 89       }
 90     }
 91     return from_class->is_subclass_of(this_class);
 92   }
 93 
 94   return false;
 95 }
 96 
 97 bool VerificationType::is_reference_assignable_from(
 98     const VerificationType& from, ClassVerifier* context,
 99     bool from_field_is_protected, TRAPS) const {
100   InstanceKlass* klass = context->current_class();
101   if (from.is_null()) {
102     // null is assignable to any reference
103     return true;
104   } else if (is_null()) {
105     return false;
106   } else if (name() == from.name()) {
107     return true;
108   } else if (is_object()) {
109     // We need check the class hierarchy to check assignability
110     if (name() == vmSymbols::java_lang_Object()) {
111       // any object or array is assignable to java.lang.Object
112       return true;
113     }
114 
115     if (Arguments::is_dumping_archive()) {
116       if (SystemDictionaryShared::add_verification_constraint(klass,
117               name(), from.name(), from_field_is_protected, from.is_array(),
118               from.is_object())) {
119         // If add_verification_constraint() returns true, the resolution/check should be
120         // delayed until runtime.
121         return true;
122       }
123     }
124 
125     return resolve_and_check_assignability(klass, name(), from.name(),
126           from_field_is_protected, from.is_array(), from.is_object(), THREAD);
127   } else if (is_array() && from.is_array()) {
128     VerificationType comp_this = get_component(context);
129     VerificationType comp_from = from.get_component(context);
130 
131 /*
132     // This code implements non-covariance between inline type arrays and both
133     // arrays of objects and arrays of interface types.  If covariance is
134     // supported for inline type arrays then this code should be removed.
135     if (comp_from.is_inline_type() && !comp_this.is_null() && comp_this.is_reference()) {
136       // An array of inline types is not assignable to an array of java.lang.Objects.
137       if (comp_this.name() == vmSymbols::java_lang_Object()) {
138         return false;
139       }
140 
141       // Need to load 'comp_this' to see if it is an interface.
142       InstanceKlass* klass = context->current_class();
143       {
144         HandleMark hm(THREAD);
145         Klass* comp_this_class = SystemDictionary::resolve_or_fail(
146             comp_this.name(), Handle(THREAD, klass->class_loader()),
147             Handle(THREAD, klass->protection_domain()), true, CHECK_false);
148         klass->class_loader_data()->record_dependency(comp_this_class);
149         if (log_is_enabled(Debug, class, resolve)) {
150           Verifier::trace_class_resolution(comp_this_class, klass);
151         }
152         // An array of inline types is not assignable to an array of interface types.
153         if (comp_this_class->is_interface()) {
154           return false;
155         }
156       }
157     }
158 */
159     if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
160       return comp_this.is_component_assignable_from(comp_from, context,
161                                                     from_field_is_protected, THREAD);
162     }
163   }
164   return false;
165 }
166 
167 bool VerificationType::is_inline_type_assignable_from(const VerificationType& from) const {
168   // Check that 'from' is not null, is an inline type, and is the same inline type.
169   assert(is_inline_type(), "called with a non-inline type");
170   assert(!is_null(), "inline type is not null");
171   return (!from.is_null() && from.is_inline_type() && name() == from.name());
172 }
173 
174 bool VerificationType::is_ref_assignable_from_inline_type(const VerificationType& from, ClassVerifier* context, TRAPS) const {
175   assert(!from.is_null(), "Inline type should not be null");
176   if (!is_null() && (name()->is_same_fundamental_type(from.name()) ||
177       name() == vmSymbols::java_lang_Object())) {
178     return true;
179   }
180 
181   // Need to load 'this' to see if it is an interface or supertype.
182   InstanceKlass* klass = context->current_class();
183   {
184     HandleMark hm(THREAD);
185     Klass* this_class = SystemDictionary::resolve_or_fail(
186         name(), Handle(THREAD, klass->class_loader()),
187         Handle(THREAD, klass->protection_domain()), true, CHECK_false);
188     klass->class_loader_data()->record_dependency(this_class);
189     if (log_is_enabled(Debug, class, resolve)) {
190       Verifier::trace_class_resolution(this_class, klass);
191     }
192     if (this_class->is_interface()) {
193       return true;
194     } else {
195       Klass* from_class = SystemDictionary::resolve_or_fail(
196         from.name(), Handle(THREAD, klass->class_loader()),
197         Handle(THREAD, klass->protection_domain()), true, CHECK_false);
198       if (log_is_enabled(Debug, class, resolve)) {
199         Verifier::trace_class_resolution(from_class, klass);
200       }
201       return from_class->is_subclass_of(this_class);
202     }
203   }
204 }
205 
206 VerificationType VerificationType::get_component(ClassVerifier *context) const {
207   assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array");
208   SignatureStream ss(name(), false);
209   ss.skip_array_prefix(1);
210   switch (ss.type()) {
211     case T_BOOLEAN: return VerificationType(Boolean);
212     case T_BYTE:    return VerificationType(Byte);
213     case T_CHAR:    return VerificationType(Char);
214     case T_SHORT:   return VerificationType(Short);
215     case T_INT:     return VerificationType(Integer);
216     case T_LONG:    return VerificationType(Long);
217     case T_FLOAT:   return VerificationType(Float);
218     case T_DOUBLE:  return VerificationType(Double);
219     case T_ARRAY:
220     case T_OBJECT:
221     case T_INLINE_TYPE: {
222       guarantee(ss.is_reference(), "unchecked verifier input?");
223       Symbol* component = ss.as_symbol();
224       // Create another symbol to save as signature stream unreferences this symbol.
225       Symbol* component_copy = context->create_temporary_symbol(component);
226       assert(component_copy == component, "symbols don't match");
227       return (ss.type() == T_INLINE_TYPE) ?
228         VerificationType::inline_type(component_copy) :
229         VerificationType::reference_type(component_copy);
230    }
231    default:
232      // Met an invalid type signature, e.g. [X
233      return VerificationType::bogus_type();
234   }
235 }
236 
237 void VerificationType::print_on(outputStream* st) const {
238   switch (_u._data) {
239     case Bogus:            st->print("top"); break;
240     case Category1:        st->print("category1"); break;
241     case Category2:        st->print("category2"); break;
242     case Category2_2nd:    st->print("category2_2nd"); break;
243     case Boolean:          st->print("boolean"); break;
244     case Byte:             st->print("byte"); break;
245     case Short:            st->print("short"); break;
246     case Char:             st->print("char"); break;
247     case Integer:          st->print("integer"); break;
248     case Float:            st->print("float"); break;
249     case Long:             st->print("long"); break;
250     case Double:           st->print("double"); break;
251     case Long_2nd:         st->print("long_2nd"); break;
252     case Double_2nd:       st->print("double_2nd"); break;
253     case Null:             st->print("null"); break;
254     case ReferenceQuery:   st->print("reference type"); break;
255     case InlineTypeQuery:  st->print("inline type"); break;
256     case NonScalarQuery:   st->print("reference or inline type"); break;
257     case Category1Query:   st->print("category1 type"); break;
258     case Category2Query:   st->print("category2 type"); break;
259     case Category2_2ndQuery: st->print("category2_2nd type"); break;
260     default:
261       if (is_uninitialized_this()) {
262         st->print("uninitializedThis");
263       } else if (is_uninitialized()) {
264         st->print("uninitialized %d", bci());
265       } else if (is_inline_type()) {
266         name()->print_Qvalue_on(st);
267       } else {
268         if (name() != NULL) {
269           name()->print_value_on(st);
270         } else {
271           st->print_cr("NULL");
272         }
273       }
274   }
275 }