annotate src/hotspot/share/classfile/verificationType.cpp @ 51997:9ce37fa2e179

8209138: Symbol constructor uses u1 as the element type of its name argument Summary: Maske u1 the type for Symbol values and add a function to return it as a char. Reviewed-by: dholmes, coleenp
author hseigel
date Wed, 03 Oct 2018 09:46:46 -0400
parents 0a8a11767c3d
children 2e72562697bf
rev   line source
duke@1 1 /*
simonis@49393 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
stefank@7397 26 #include "classfile/symbolTable.hpp"
iklam@39713 27 #include "classfile/systemDictionaryShared.hpp"
stefank@7397 28 #include "classfile/verificationType.hpp"
coleenp@8076 29 #include "classfile/verifier.hpp"
stefank@49594 30 #include "logging/log.hpp"
simonis@49393 31 #include "runtime/handles.inline.hpp"
duke@1 32
duke@1 33 VerificationType VerificationType::from_tag(u1 tag) {
duke@1 34 switch (tag) {
duke@1 35 case ITEM_Top: return bogus_type();
duke@1 36 case ITEM_Integer: return integer_type();
duke@1 37 case ITEM_Float: return float_type();
duke@1 38 case ITEM_Double: return double_type();
duke@1 39 case ITEM_Long: return long_type();
duke@1 40 case ITEM_Null: return null_type();
duke@1 41 default:
duke@1 42 ShouldNotReachHere();
duke@1 43 return bogus_type();
duke@1 44 }
duke@1 45 }
duke@1 46
coleenp@46329 47 bool VerificationType::resolve_and_check_assignability(InstanceKlass* klass, Symbol* name,
iklam@39713 48 Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, TRAPS) {
coleenp@46271 49 HandleMark hm(THREAD);
coleenp@46329 50 Klass* this_class = SystemDictionary::resolve_or_fail(
iklam@39713 51 name, Handle(THREAD, klass->class_loader()),
iklam@39713 52 Handle(THREAD, klass->protection_domain()), true, CHECK_false);
hseigel@49770 53 klass->class_loader_data()->record_dependency(this_class);
iklam@39713 54 if (log_is_enabled(Debug, class, resolve)) {
coleenp@46329 55 Verifier::trace_class_resolution(this_class, klass);
iklam@39713 56 }
iklam@39713 57
iklam@39713 58 if (this_class->is_interface() && (!from_field_is_protected ||
iklam@39713 59 from_name != vmSymbols::java_lang_Object())) {
iklam@39713 60 // If we are not trying to access a protected field or method in
iklam@39713 61 // java.lang.Object then, for arrays, we only allow assignability
iklam@39713 62 // to interfaces java.lang.Cloneable and java.io.Serializable.
iklam@39713 63 // Otherwise, we treat interfaces as java.lang.Object.
iklam@39713 64 return !from_is_array ||
iklam@39713 65 this_class == SystemDictionary::Cloneable_klass() ||
iklam@39713 66 this_class == SystemDictionary::Serializable_klass();
iklam@39713 67 } else if (from_is_object) {
iklam@39713 68 Klass* from_class = SystemDictionary::resolve_or_fail(
iklam@39713 69 from_name, Handle(THREAD, klass->class_loader()),
iklam@39713 70 Handle(THREAD, klass->protection_domain()), true, CHECK_false);
hseigel@49770 71 klass->class_loader_data()->record_dependency(from_class);
iklam@39713 72 if (log_is_enabled(Debug, class, resolve)) {
coleenp@46329 73 Verifier::trace_class_resolution(from_class, klass);
iklam@39713 74 }
coleenp@46329 75 return InstanceKlass::cast(from_class)->is_subclass_of(this_class);
iklam@39713 76 }
iklam@39713 77
iklam@39713 78 return false;
iklam@39713 79 }
iklam@39713 80
duke@1 81 bool VerificationType::is_reference_assignable_from(
hseigel@27022 82 const VerificationType& from, ClassVerifier* context,
hseigel@27022 83 bool from_field_is_protected, TRAPS) const {
coleenp@46329 84 InstanceKlass* klass = context->current_class();
duke@1 85 if (from.is_null()) {
duke@1 86 // null is assignable to any reference
duke@1 87 return true;
duke@1 88 } else if (is_null()) {
duke@1 89 return false;
duke@1 90 } else if (name() == from.name()) {
duke@1 91 return true;
duke@1 92 } else if (is_object()) {
duke@1 93 // We need check the class hierarchy to check assignability
duke@1 94 if (name() == vmSymbols::java_lang_Object()) {
duke@1 95 // any object or array is assignable to java.lang.Object
duke@1 96 return true;
duke@1 97 }
iklam@39713 98
coleenp@46329 99 if (DumpSharedSpaces && SystemDictionaryShared::add_verification_constraint(klass,
iklam@39713 100 name(), from.name(), from_field_is_protected, from.is_array(),
iklam@39713 101 from.is_object())) {
iklam@39713 102 // If add_verification_constraint() returns true, the resolution/check should be
iklam@39713 103 // delayed until runtime.
iklam@39713 104 return true;
hseigel@30616 105 }
hseigel@30616 106
coleenp@46329 107 return resolve_and_check_assignability(klass, name(), from.name(),
iklam@39713 108 from_field_is_protected, from.is_array(), from.is_object(), THREAD);
duke@1 109 } else if (is_array() && from.is_array()) {
coleenp@8076 110 VerificationType comp_this = get_component(context, CHECK_false);
coleenp@8076 111 VerificationType comp_from = from.get_component(context, CHECK_false);
apangin@6172 112 if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
hseigel@31966 113 return comp_this.is_component_assignable_from(comp_from, context,
hseigel@27022 114 from_field_is_protected, CHECK_false);
apangin@6172 115 }
duke@1 116 }
duke@1 117 return false;
duke@1 118 }
duke@1 119
coleenp@8076 120 VerificationType VerificationType::get_component(ClassVerifier *context, TRAPS) const {
duke@1 121 assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array");
coleenp@8076 122 Symbol* component;
hseigel@51997 123 switch (name()->char_at(1)) {
duke@1 124 case 'Z': return VerificationType(Boolean);
duke@1 125 case 'B': return VerificationType(Byte);
duke@1 126 case 'C': return VerificationType(Char);
duke@1 127 case 'S': return VerificationType(Short);
duke@1 128 case 'I': return VerificationType(Integer);
duke@1 129 case 'J': return VerificationType(Long);
duke@1 130 case 'F': return VerificationType(Float);
duke@1 131 case 'D': return VerificationType(Double);
duke@1 132 case '[':
coleenp@8076 133 component = context->create_temporary_symbol(
duke@1 134 name(), 1, name()->utf8_length(),
duke@1 135 CHECK_(VerificationType::bogus_type()));
duke@1 136 return VerificationType::reference_type(component);
duke@1 137 case 'L':
coleenp@8076 138 component = context->create_temporary_symbol(
duke@1 139 name(), 2, name()->utf8_length() - 1,
duke@1 140 CHECK_(VerificationType::bogus_type()));
duke@1 141 return VerificationType::reference_type(component);
duke@1 142 default:
apangin@6172 143 // Met an invalid type signature, e.g. [X
duke@1 144 return VerificationType::bogus_type();
duke@1 145 }
duke@1 146 }
duke@1 147
duke@1 148 void VerificationType::print_on(outputStream* st) const {
duke@1 149 switch (_u._data) {
kamg@13476 150 case Bogus: st->print("top"); break;
kamg@13476 151 case Category1: st->print("category1"); break;
kamg@13476 152 case Category2: st->print("category2"); break;
kamg@13476 153 case Category2_2nd: st->print("category2_2nd"); break;
kamg@13476 154 case Boolean: st->print("boolean"); break;
kamg@13476 155 case Byte: st->print("byte"); break;
kamg@13476 156 case Short: st->print("short"); break;
kamg@13476 157 case Char: st->print("char"); break;
kamg@13476 158 case Integer: st->print("integer"); break;
kamg@13476 159 case Float: st->print("float"); break;
kamg@13476 160 case Long: st->print("long"); break;
kamg@13476 161 case Double: st->print("double"); break;
kamg@13476 162 case Long_2nd: st->print("long_2nd"); break;
kamg@13476 163 case Double_2nd: st->print("double_2nd"); break;
kamg@13476 164 case Null: st->print("null"); break;
kamg@13476 165 case ReferenceQuery: st->print("reference type"); break;
kamg@13476 166 case Category1Query: st->print("category1 type"); break;
kamg@13476 167 case Category2Query: st->print("category2 type"); break;
kamg@13476 168 case Category2_2ndQuery: st->print("category2_2nd type"); break;
duke@1 169 default:
duke@1 170 if (is_uninitialized_this()) {
kamg@13476 171 st->print("uninitializedThis");
duke@1 172 } else if (is_uninitialized()) {
kamg@13476 173 st->print("uninitialized %d", bci());
duke@1 174 } else {
kamg@13476 175 name()->print_value_on(st);
duke@1 176 }
duke@1 177 }
duke@1 178 }