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