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