1 /*
  2  * Copyright (c) 2003, 2025, 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 "cds/cdsConfig.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/handles.inline.hpp"
 36 
 37 VerificationType VerificationType::from_tag(u1 tag) {
 38   switch (tag) {
 39     case ITEM_Top:     return bogus_type();
 40     case ITEM_Integer: return integer_type();
 41     case ITEM_Float:   return float_type();
 42     case ITEM_Double:  return double_type();
 43     case ITEM_Long:    return long_type();
 44     case ITEM_Null:    return null_type();
 45     default:
 46       ShouldNotReachHere();
 47       return bogus_type();
 48   }
 49 }
 50 
 51 bool VerificationType::resolve_and_check_assignability(InstanceKlass* klass, Symbol* name,
 52          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, TRAPS) {
 53   HandleMark hm(THREAD);
 54   Klass* this_class;
 55   if (klass->is_hidden() && klass->name() == name) {
 56     this_class = klass;
 57   } else {
 58     this_class = SystemDictionary::resolve_or_fail(
 59       name, Handle(THREAD, klass->class_loader()), true, CHECK_false);
 60     if (log_is_enabled(Debug, class, resolve)) {
 61       Verifier::trace_class_resolution(this_class, klass);
 62     }
 63   }
 64 
 65   if (this_class->is_interface() && (!from_field_is_protected ||
 66       from_name != vmSymbols::java_lang_Object())) {
 67     // If we are not trying to access a protected field or method in
 68     // java.lang.Object then, for arrays, we only allow assignability
 69     // to interfaces java.lang.Cloneable and java.io.Serializable.
 70     // Otherwise, we treat interfaces as java.lang.Object.
 71     return !from_is_array ||
 72       this_class == vmClasses::Cloneable_klass() ||
 73       this_class == vmClasses::Serializable_klass();
 74   } else if (from_is_object) {
 75     Klass* from_class;
 76     if (klass->is_hidden() && klass->name() == from_name) {
 77       from_class = klass;
 78     } else {
 79       from_class = SystemDictionary::resolve_or_fail(
 80         from_name, Handle(THREAD, klass->class_loader()), true, CHECK_false);
 81       if (log_is_enabled(Debug, class, resolve)) {
 82         Verifier::trace_class_resolution(from_class, klass);
 83       }
 84     }
 85     return from_class->is_subclass_of(this_class);
 86   }
 87 
 88   return false;
 89 }
 90 
 91 bool VerificationType::is_reference_assignable_from(
 92     const VerificationType& from, ClassVerifier* context,
 93     bool from_field_is_protected, TRAPS) const {
 94   InstanceKlass* klass = context->current_class();
 95   if (from.is_null()) {
 96     // null is assignable to any reference
 97     return true;
 98   } else if (is_null()) {
 99     return false;
100   } else if (name() == from.name()) {
101     return true;
102   } else if (is_object()) {
103     // We need check the class hierarchy to check assignability
104     if (name() == vmSymbols::java_lang_Object()) {
105       // any object or array is assignable to java.lang.Object
106       return true;
107     }
108 
109 #if INCLUDE_CDS
110     if (CDSConfig::is_dumping_archive()) {
111       bool skip_assignability_check = false;
112       SystemDictionaryShared::add_verification_constraint(klass,
113               name(), from.name(), from_field_is_protected, from.is_array(),
114               from.is_object(), &skip_assignability_check);
115       if (skip_assignability_check) {
116         // We are not able to resolve name() or from.name(). The actual assignability check
117         // will be delayed until runtime.
118         return true;
119       }
120     }
121 #endif
122     return resolve_and_check_assignability(klass, name(), from.name(),
123           from_field_is_protected, from.is_array(), from.is_object(), THREAD);
124   } else if (is_array() && from.is_array()) {
125     VerificationType comp_this = get_component(context);
126     VerificationType comp_from = from.get_component(context);
127     if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
128       return comp_this.is_component_assignable_from(comp_from, context,
129                                                     from_field_is_protected, THREAD);
130     }
131   }
132   return false;
133 }
134 
135 VerificationType VerificationType::get_component(ClassVerifier *context) const {
136   assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array");
137   SignatureStream ss(name(), false);
138   ss.skip_array_prefix(1);
139   switch (ss.type()) {
140     case T_BOOLEAN: return VerificationType(Boolean);
141     case T_BYTE:    return VerificationType(Byte);
142     case T_CHAR:    return VerificationType(Char);
143     case T_SHORT:   return VerificationType(Short);
144     case T_INT:     return VerificationType(Integer);
145     case T_LONG:    return VerificationType(Long);
146     case T_FLOAT:   return VerificationType(Float);
147     case T_DOUBLE:  return VerificationType(Double);
148     case T_ARRAY:
149     case T_OBJECT: {
150       guarantee(ss.is_reference(), "unchecked verifier input?");
151       Symbol* component = ss.as_symbol();
152       // Create another symbol to save as signature stream unreferences this symbol.
153       Symbol* component_copy = context->create_temporary_symbol(component);
154       assert(component_copy == component, "symbols don't match");
155       return VerificationType::reference_type(component_copy);
156    }
157    default:
158      // Met an invalid type signature, e.g. [X
159      return VerificationType::bogus_type();
160   }
161 }
162 
163 void VerificationType::print_on(outputStream* st) const {
164   switch (_u._data) {
165     case Bogus:            st->print("top"); break;
166     case Category1:        st->print("category1"); break;
167     case Category2:        st->print("category2"); break;
168     case Category2_2nd:    st->print("category2_2nd"); break;
169     case Boolean:          st->print("boolean"); break;
170     case Byte:             st->print("byte"); break;
171     case Short:            st->print("short"); break;
172     case Char:             st->print("char"); break;
173     case Integer:          st->print("integer"); break;
174     case Float:            st->print("float"); break;
175     case Long:             st->print("long"); break;
176     case Double:           st->print("double"); break;
177     case Long_2nd:         st->print("long_2nd"); break;
178     case Double_2nd:       st->print("double_2nd"); break;
179     case Null:             st->print("null"); break;
180     case ReferenceQuery:   st->print("reference type"); break;
181     case Category1Query:   st->print("category1 type"); break;
182     case Category2Query:   st->print("category2 type"); break;
183     case Category2_2ndQuery: st->print("category2_2nd type"); break;
184     default:
185       if (is_uninitialized_this()) {
186         st->print("uninitializedThis");
187       } else if (is_uninitialized()) {
188         st->print("uninitialized %d", bci());
189       } else {
190         if (name() != nullptr) {
191           name()->print_value_on(st);
192         } else {
193           st->print_cr("null");
194         }
195       }
196   }
197 }