0N/A/*
3955N/A * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "classfile/symbolTable.hpp"
1879N/A#include "classfile/verificationType.hpp"
2062N/A#include "classfile/verifier.hpp"
0N/A
0N/AVerificationType VerificationType::from_tag(u1 tag) {
0N/A switch (tag) {
0N/A case ITEM_Top: return bogus_type();
0N/A case ITEM_Integer: return integer_type();
0N/A case ITEM_Float: return float_type();
0N/A case ITEM_Double: return double_type();
0N/A case ITEM_Long: return long_type();
0N/A case ITEM_Null: return null_type();
0N/A default:
0N/A ShouldNotReachHere();
0N/A return bogus_type();
0N/A }
0N/A}
0N/A
0N/Abool VerificationType::is_reference_assignable_from(
2062N/A const VerificationType& from, ClassVerifier* context, TRAPS) const {
2062N/A instanceKlassHandle klass = context->current_class();
0N/A if (from.is_null()) {
0N/A // null is assignable to any reference
0N/A return true;
0N/A } else if (is_null()) {
0N/A return false;
0N/A } else if (name() == from.name()) {
0N/A return true;
0N/A } else if (is_object()) {
0N/A // We need check the class hierarchy to check assignability
0N/A if (name() == vmSymbols::java_lang_Object()) {
0N/A // any object or array is assignable to java.lang.Object
0N/A return true;
0N/A }
1723N/A klassOop obj = SystemDictionary::resolve_or_fail(
2062N/A name(), Handle(THREAD, klass->class_loader()),
2062N/A Handle(THREAD, klass->protection_domain()), true, CHECK_false);
1723N/A KlassHandle this_class(THREAD, obj);
1723N/A
1723N/A if (this_class->is_interface()) {
0N/A // We treat interfaces as java.lang.Object, including
0N/A // java.lang.Cloneable and java.io.Serializable
0N/A return true;
0N/A } else if (from.is_object()) {
0N/A klassOop from_class = SystemDictionary::resolve_or_fail(
2062N/A from.name(), Handle(THREAD, klass->class_loader()),
2062N/A Handle(THREAD, klass->protection_domain()), true, CHECK_false);
1723N/A return instanceKlass::cast(from_class)->is_subclass_of(this_class());
0N/A }
0N/A } else if (is_array() && from.is_array()) {
2062N/A VerificationType comp_this = get_component(context, CHECK_false);
2062N/A VerificationType comp_from = from.get_component(context, CHECK_false);
1597N/A if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
1597N/A return comp_this.is_assignable_from(comp_from, context, CHECK_false);
1597N/A }
0N/A }
0N/A return false;
0N/A}
0N/A
2062N/AVerificationType VerificationType::get_component(ClassVerifier *context, TRAPS) const {
0N/A assert(is_array() && name()->utf8_length() >= 2, "Must be a valid array");
2062N/A Symbol* component;
0N/A switch (name()->byte_at(1)) {
0N/A case 'Z': return VerificationType(Boolean);
0N/A case 'B': return VerificationType(Byte);
0N/A case 'C': return VerificationType(Char);
0N/A case 'S': return VerificationType(Short);
0N/A case 'I': return VerificationType(Integer);
0N/A case 'J': return VerificationType(Long);
0N/A case 'F': return VerificationType(Float);
0N/A case 'D': return VerificationType(Double);
0N/A case '[':
2062N/A component = context->create_temporary_symbol(
0N/A name(), 1, name()->utf8_length(),
0N/A CHECK_(VerificationType::bogus_type()));
0N/A return VerificationType::reference_type(component);
0N/A case 'L':
2062N/A component = context->create_temporary_symbol(
0N/A name(), 2, name()->utf8_length() - 1,
0N/A CHECK_(VerificationType::bogus_type()));
0N/A return VerificationType::reference_type(component);
0N/A default:
1597N/A // Met an invalid type signature, e.g. [X
0N/A return VerificationType::bogus_type();
0N/A }
0N/A}
0N/A
0N/Avoid VerificationType::print_on(outputStream* st) const {
0N/A switch (_u._data) {
3955N/A case Bogus: st->print("top"); break;
3955N/A case Category1: st->print("category1"); break;
3955N/A case Category2: st->print("category2"); break;
3955N/A case Category2_2nd: st->print("category2_2nd"); break;
3955N/A case Boolean: st->print("boolean"); break;
3955N/A case Byte: st->print("byte"); break;
3955N/A case Short: st->print("short"); break;
3955N/A case Char: st->print("char"); break;
3955N/A case Integer: st->print("integer"); break;
3955N/A case Float: st->print("float"); break;
3955N/A case Long: st->print("long"); break;
3955N/A case Double: st->print("double"); break;
3955N/A case Long_2nd: st->print("long_2nd"); break;
3955N/A case Double_2nd: st->print("double_2nd"); break;
3955N/A case Null: st->print("null"); break;
3955N/A case ReferenceQuery: st->print("reference type"); break;
3955N/A case Category1Query: st->print("category1 type"); break;
3955N/A case Category2Query: st->print("category2 type"); break;
3955N/A case Category2_2ndQuery: st->print("category2_2nd type"); break;
0N/A default:
0N/A if (is_uninitialized_this()) {
3955N/A st->print("uninitializedThis");
0N/A } else if (is_uninitialized()) {
3955N/A st->print("uninitialized %d", bci());
0N/A } else {
3955N/A name()->print_value_on(st);
0N/A }
0N/A }
0N/A}