1 /*
   2  * Copyright (c) 2003, 2018, 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 "runtime/handles.inline.hpp"
  32 
  33 VerificationType VerificationType::from_tag(u1 tag) {
  34   switch (tag) {
  35     case ITEM_Top:     return bogus_type();
  36     case ITEM_Integer: return integer_type();
  37     case ITEM_Float:   return float_type();
  38     case ITEM_Double:  return double_type();
  39     case ITEM_Long:    return long_type();
  40     case ITEM_Null:    return null_type();
  41     default:
  42       ShouldNotReachHere();
  43       return bogus_type();
  44   }
  45 }
  46 
  47 bool VerificationType::resolve_and_check_assignability(InstanceKlass* klass, Symbol* name,
  48          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, TRAPS) {
  49   HandleMark hm(THREAD);
  50   Klass* this_class = SystemDictionary::resolve_or_fail(
  51       name, Handle(THREAD, klass->class_loader()),
  52       Handle(THREAD, klass->protection_domain()), true, CHECK_false);
  53   klass->class_loader_data()->record_dependency(this_class);
  54   if (log_is_enabled(Debug, class, resolve)) {
  55     Verifier::trace_class_resolution(this_class, klass);
  56   }
  57 
  58   if (this_class->access_flags().is_value_type()) return false;
  59   if (this_class->is_interface() && (!from_field_is_protected ||
  60       from_name != vmSymbols::java_lang_Object())) {
  61     // If we are not trying to access a protected field or method in
  62     // java.lang.Object then, for arrays, we only allow assignability
  63     // to interfaces java.lang.Cloneable and java.io.Serializable.
  64     // Otherwise, we treat interfaces as java.lang.Object.
  65     return !from_is_array ||
  66       this_class == SystemDictionary::Cloneable_klass() ||
  67       this_class == SystemDictionary::Serializable_klass();
  68   } else if (from_is_object) {
  69     Klass* from_class = SystemDictionary::resolve_or_fail(
  70         from_name, Handle(THREAD, klass->class_loader()),
  71         Handle(THREAD, klass->protection_domain()), true, CHECK_false);
  72     klass->class_loader_data()->record_dependency(from_class);
  73     if (log_is_enabled(Debug, class, resolve)) {
  74       Verifier::trace_class_resolution(from_class, klass);
  75     }
  76     return InstanceKlass::cast(from_class)->is_subclass_of(this_class);
  77   }
  78 
  79   return false;
  80 }
  81 
  82 bool VerificationType::is_reference_assignable_from(
  83     const VerificationType& from, ClassVerifier* context,
  84     bool from_field_is_protected, TRAPS) const {
  85   InstanceKlass* klass = context->current_class();
  86   if (from.is_null()) {
  87     // null is assignable to any reference
  88     return true;
  89   } else if (is_null()) {
  90     return false;
  91   } else if (name() == from.name()) {
  92     return true;
  93   } else if (is_object()) {
  94     // We need check the class hierarchy to check assignability
  95     if (name() == vmSymbols::java_lang_Object()) {
  96       // any object or array is assignable to java.lang.Object
  97       return true;
  98     }
  99 
 100     if (DumpSharedSpaces && SystemDictionaryShared::add_verification_constraint(klass,
 101               name(), from.name(), from_field_is_protected, from.is_array(),
 102               from.is_object())) {
 103       // If add_verification_constraint() returns true, the resolution/check should be
 104       // delayed until runtime.
 105       return true;
 106     }
 107 
 108     return resolve_and_check_assignability(klass, name(), from.name(),
 109           from_field_is_protected, from.is_array(), from.is_object(), THREAD);
 110   } else if (is_array() && from.is_array()) {
 111     VerificationType comp_this = get_component(context, CHECK_false);
 112     VerificationType comp_from = from.get_component(context, CHECK_false);
 113 
 114     if (comp_from.is_valuetype() && !comp_this.is_null() && comp_this.is_reference()) {
 115       // An array of value types is not assignable to an array of java.lang.Objects.
 116       if (comp_this.name() == vmSymbols::java_lang_Object()) {
 117         return false;
 118       }
 119 
 120       // Need to load 'comp_this' to see if it is an interface.
 121       InstanceKlass* klass = context->current_class();
 122       {
 123         HandleMark hm(THREAD);
 124         Klass* comp_this_class = SystemDictionary::resolve_or_fail(
 125             comp_this.name(), Handle(THREAD, klass->class_loader()),
 126             Handle(THREAD, klass->protection_domain()), true, CHECK_false);
 127         klass->class_loader_data()->record_dependency(comp_this_class);
 128         if (log_is_enabled(Debug, class, resolve)) {
 129           Verifier::trace_class_resolution(comp_this_class, klass);
 130         }
 131         // An array of value types is not assignable to an array of interface types.
 132         if (comp_this_class->is_interface()) {
 133           return false;
 134         }
 135       }
 136     }
 137 
 138     if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
 139       return comp_this.is_component_assignable_from(comp_from, context,
 140                                                     from_field_is_protected, THREAD);
 141     }
 142   }
 143   return false;
 144 }
 145 
 146 bool VerificationType::is_valuetype_assignable_from(const VerificationType& from) const {
 147   // Check that 'from' is not null, is a value type, and is the same value type.
 148   assert(is_valuetype(), "called with a non-valuetype type");
 149   assert(!is_null(), "valuetype is not null");
 150   assert(name() != vmSymbols::java_lang_Object(), "java.lang.Object is a value type?");
 151   return (!from.is_null() && from.is_valuetype() && name() == from.name());
 152 }
 153 
 154 bool VerificationType::is_ref_assignable_from_value_type(const VerificationType& from, ClassVerifier* context, TRAPS) const {
 155   assert(!from.is_null(), "Value type should not be null");
 156   if (!is_null() && (name()->is_same_fundamental_type(from.name()) ||
 157       name() == vmSymbols::java_lang_Object())) {
 158     return true;
 159   }
 160 
 161   // Need to load 'this' to see if it is an interface.
 162   InstanceKlass* klass = context->current_class();
 163   {
 164     HandleMark hm(THREAD);
 165     Klass* this_class = SystemDictionary::resolve_or_fail(
 166         name(), Handle(THREAD, klass->class_loader()),
 167         Handle(THREAD, klass->protection_domain()), true, CHECK_false);
 168     klass->class_loader_data()->record_dependency(this_class);
 169     if (log_is_enabled(Debug, class, resolve)) {
 170       Verifier::trace_class_resolution(this_class, klass);
 171     }
 172     return (this_class->is_interface());
 173   }
 174 }
 175 
 176 VerificationType VerificationType::get_component(ClassVerifier *context, TRAPS) const {
 177   assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array");
 178   Symbol* component;
 179   switch (name()->char_at(1)) {
 180     case 'Z': return VerificationType(Boolean);
 181     case 'B': return VerificationType(Byte);
 182     case 'C': return VerificationType(Char);
 183     case 'S': return VerificationType(Short);
 184     case 'I': return VerificationType(Integer);
 185     case 'J': return VerificationType(Long);
 186     case 'F': return VerificationType(Float);
 187     case 'D': return VerificationType(Double);
 188     case '[':
 189       component = context->create_temporary_symbol(
 190         name(), 1, name()->utf8_length(),
 191         CHECK_(VerificationType::bogus_type()));
 192       return VerificationType::reference_type(component);
 193     case 'L':
 194       component = context->create_temporary_symbol(
 195         name(), 2, name()->utf8_length() - 1,
 196         CHECK_(VerificationType::bogus_type()));
 197       return VerificationType::reference_type(component);
 198     case 'Q':
 199       component = context->create_temporary_symbol(
 200         name(), 2, name()->utf8_length() - 1,
 201         CHECK_(VerificationType::bogus_type()));
 202       return VerificationType::valuetype_type(component);
 203     default:
 204       // Met an invalid type signature, e.g. [X
 205       return VerificationType::bogus_type();
 206   }
 207 }
 208 
 209 void VerificationType::print_on(outputStream* st) const {
 210   switch (_u._data) {
 211     case Bogus:            st->print("top"); break;
 212     case Category1:        st->print("category1"); break;
 213     case Category2:        st->print("category2"); break;
 214     case Category2_2nd:    st->print("category2_2nd"); break;
 215     case Boolean:          st->print("boolean"); break;
 216     case Byte:             st->print("byte"); break;
 217     case Short:            st->print("short"); break;
 218     case Char:             st->print("char"); break;
 219     case Integer:          st->print("integer"); break;
 220     case Float:            st->print("float"); break;
 221     case Long:             st->print("long"); break;
 222     case Double:           st->print("double"); break;
 223     case Long_2nd:         st->print("long_2nd"); break;
 224     case Double_2nd:       st->print("double_2nd"); break;
 225     case Null:             st->print("null"); break;
 226     case ReferenceQuery:   st->print("reference type"); break;
 227     case ValueTypeQuery:   st->print("value type"); break;
 228     case NonScalarQuery:   st->print("reference or value type"); break;
 229     case Category1Query:   st->print("category1 type"); break;
 230     case Category2Query:   st->print("category2 type"); break;
 231     case Category2_2ndQuery: st->print("category2_2nd type"); break;
 232     default:
 233       if (is_uninitialized_this()) {
 234         st->print("uninitializedThis");
 235       } else if (is_uninitialized()) {
 236         st->print("uninitialized %d", bci());
 237       } else if (is_valuetype()) {
 238         name()->print_Qvalue_on(st);
 239       } else {
 240         if (name() != NULL) {
 241           name()->print_value_on(st);
 242         } else {
 243           st->print_cr("NULL");
 244         }
 245       }
 246   }
 247 }