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