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