src/share/vm/classfile/verificationType.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
child 8604
04d83ba48607
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/symbolTable.hpp"
aoqi@0 27 #include "classfile/verificationType.hpp"
aoqi@0 28 #include "classfile/verifier.hpp"
aoqi@0 29
aoqi@0 30 VerificationType VerificationType::from_tag(u1 tag) {
aoqi@0 31 switch (tag) {
aoqi@0 32 case ITEM_Top: return bogus_type();
aoqi@0 33 case ITEM_Integer: return integer_type();
aoqi@0 34 case ITEM_Float: return float_type();
aoqi@0 35 case ITEM_Double: return double_type();
aoqi@0 36 case ITEM_Long: return long_type();
aoqi@0 37 case ITEM_Null: return null_type();
aoqi@0 38 default:
aoqi@0 39 ShouldNotReachHere();
aoqi@0 40 return bogus_type();
aoqi@0 41 }
aoqi@0 42 }
aoqi@0 43
aoqi@0 44 bool VerificationType::is_reference_assignable_from(
aoqi@0 45 const VerificationType& from, ClassVerifier* context,
aoqi@0 46 bool from_field_is_protected, TRAPS) const {
aoqi@0 47 instanceKlassHandle klass = context->current_class();
aoqi@0 48 if (from.is_null()) {
aoqi@0 49 // null is assignable to any reference
aoqi@0 50 return true;
aoqi@0 51 } else if (is_null()) {
aoqi@0 52 return false;
aoqi@0 53 } else if (name() == from.name()) {
aoqi@0 54 return true;
aoqi@0 55 } else if (is_object()) {
aoqi@0 56 // We need check the class hierarchy to check assignability
aoqi@0 57 if (name() == vmSymbols::java_lang_Object()) {
aoqi@0 58 // any object or array is assignable to java.lang.Object
aoqi@0 59 return true;
aoqi@0 60 }
aoqi@0 61 Klass* obj = SystemDictionary::resolve_or_fail(
aoqi@0 62 name(), Handle(THREAD, klass->class_loader()),
aoqi@0 63 Handle(THREAD, klass->protection_domain()), true, CHECK_false);
aoqi@0 64 KlassHandle this_class(THREAD, obj);
aoqi@0 65
aoqi@0 66 if (this_class->is_interface() && (!from_field_is_protected ||
aoqi@0 67 from.name() != vmSymbols::java_lang_Object())) {
aoqi@0 68 // If we are not trying to access a protected field or method in
aoqi@0 69 // java.lang.Object then we treat interfaces as java.lang.Object,
aoqi@0 70 // including java.lang.Cloneable and java.io.Serializable.
aoqi@0 71 return true;
aoqi@0 72 } else if (from.is_object()) {
aoqi@0 73 Klass* from_class = SystemDictionary::resolve_or_fail(
aoqi@0 74 from.name(), Handle(THREAD, klass->class_loader()),
aoqi@0 75 Handle(THREAD, klass->protection_domain()), true, CHECK_false);
aoqi@0 76 return InstanceKlass::cast(from_class)->is_subclass_of(this_class());
aoqi@0 77 }
aoqi@0 78 } else if (is_array() && from.is_array()) {
aoqi@0 79 VerificationType comp_this = get_component(context, CHECK_false);
aoqi@0 80 VerificationType comp_from = from.get_component(context, CHECK_false);
aoqi@0 81 if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
aoqi@0 82 return comp_this.is_assignable_from(comp_from, context,
aoqi@0 83 from_field_is_protected, CHECK_false);
aoqi@0 84 }
aoqi@0 85 }
aoqi@0 86 return false;
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 VerificationType VerificationType::get_component(ClassVerifier *context, TRAPS) const {
aoqi@0 90 assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array");
aoqi@0 91 Symbol* component;
aoqi@0 92 switch (name()->byte_at(1)) {
aoqi@0 93 case 'Z': return VerificationType(Boolean);
aoqi@0 94 case 'B': return VerificationType(Byte);
aoqi@0 95 case 'C': return VerificationType(Char);
aoqi@0 96 case 'S': return VerificationType(Short);
aoqi@0 97 case 'I': return VerificationType(Integer);
aoqi@0 98 case 'J': return VerificationType(Long);
aoqi@0 99 case 'F': return VerificationType(Float);
aoqi@0 100 case 'D': return VerificationType(Double);
aoqi@0 101 case '[':
aoqi@0 102 component = context->create_temporary_symbol(
aoqi@0 103 name(), 1, name()->utf8_length(),
aoqi@0 104 CHECK_(VerificationType::bogus_type()));
aoqi@0 105 return VerificationType::reference_type(component);
aoqi@0 106 case 'L':
aoqi@0 107 component = context->create_temporary_symbol(
aoqi@0 108 name(), 2, name()->utf8_length() - 1,
aoqi@0 109 CHECK_(VerificationType::bogus_type()));
aoqi@0 110 return VerificationType::reference_type(component);
aoqi@0 111 default:
aoqi@0 112 // Met an invalid type signature, e.g. [X
aoqi@0 113 return VerificationType::bogus_type();
aoqi@0 114 }
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 void VerificationType::print_on(outputStream* st) const {
aoqi@0 118 switch (_u._data) {
aoqi@0 119 case Bogus: st->print("top"); break;
aoqi@0 120 case Category1: st->print("category1"); break;
aoqi@0 121 case Category2: st->print("category2"); break;
aoqi@0 122 case Category2_2nd: st->print("category2_2nd"); break;
aoqi@0 123 case Boolean: st->print("boolean"); break;
aoqi@0 124 case Byte: st->print("byte"); break;
aoqi@0 125 case Short: st->print("short"); break;
aoqi@0 126 case Char: st->print("char"); break;
aoqi@0 127 case Integer: st->print("integer"); break;
aoqi@0 128 case Float: st->print("float"); break;
aoqi@0 129 case Long: st->print("long"); break;
aoqi@0 130 case Double: st->print("double"); break;
aoqi@0 131 case Long_2nd: st->print("long_2nd"); break;
aoqi@0 132 case Double_2nd: st->print("double_2nd"); break;
aoqi@0 133 case Null: st->print("null"); break;
aoqi@0 134 case ReferenceQuery: st->print("reference type"); break;
aoqi@0 135 case Category1Query: st->print("category1 type"); break;
aoqi@0 136 case Category2Query: st->print("category2 type"); break;
aoqi@0 137 case Category2_2ndQuery: st->print("category2_2nd type"); break;
aoqi@0 138 default:
aoqi@0 139 if (is_uninitialized_this()) {
aoqi@0 140 st->print("uninitializedThis");
aoqi@0 141 } else if (is_uninitialized()) {
aoqi@0 142 st->print("uninitialized %d", bci());
aoqi@0 143 } else {
aoqi@0 144 name()->print_value_on(st);
aoqi@0 145 }
aoqi@0 146 }
aoqi@0 147 }

mercurial