src/share/vm/classfile/verificationType.cpp

Thu, 24 May 2018 18:41:44 +0800

author
aoqi
date
Thu, 24 May 2018 18:41:44 +0800
changeset 8856
ac27a9c85bea
parent 8604
04d83ba48607
child 9122
024be04bb151
permissions
-rw-r--r--

Merge

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

mercurial