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

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

mercurial