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