hotspot/src/share/vm/classfile/verificationType.cpp
changeset 39713 29ece76096cb
parent 38151 fffedc5e5cf8
child 46271 979ebd346ecf
equal deleted inserted replaced
39712:dccb9af07ee1 39713:29ece76096cb
     1 /*
     1 /*
     2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  *
    22  *
    23  */
    23  */
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
    26 #include "classfile/symbolTable.hpp"
    26 #include "classfile/symbolTable.hpp"
       
    27 #include "classfile/systemDictionaryShared.hpp"
    27 #include "classfile/verificationType.hpp"
    28 #include "classfile/verificationType.hpp"
    28 #include "classfile/verifier.hpp"
    29 #include "classfile/verifier.hpp"
    29 
    30 
    30 VerificationType VerificationType::from_tag(u1 tag) {
    31 VerificationType VerificationType::from_tag(u1 tag) {
    31   switch (tag) {
    32   switch (tag) {
    37     case ITEM_Null:    return null_type();
    38     case ITEM_Null:    return null_type();
    38     default:
    39     default:
    39       ShouldNotReachHere();
    40       ShouldNotReachHere();
    40       return bogus_type();
    41       return bogus_type();
    41   }
    42   }
       
    43 }
       
    44 
       
    45 bool VerificationType::resolve_and_check_assignability(instanceKlassHandle klass, Symbol* name,
       
    46          Symbol* from_name, bool from_field_is_protected, bool from_is_array, bool from_is_object, TRAPS) {
       
    47   Klass* obj = SystemDictionary::resolve_or_fail(
       
    48       name, Handle(THREAD, klass->class_loader()),
       
    49       Handle(THREAD, klass->protection_domain()), true, CHECK_false);
       
    50   if (log_is_enabled(Debug, class, resolve)) {
       
    51     Verifier::trace_class_resolution(obj, klass());
       
    52   }
       
    53 
       
    54   KlassHandle this_class(THREAD, obj);
       
    55 
       
    56   if (this_class->is_interface() && (!from_field_is_protected ||
       
    57       from_name != vmSymbols::java_lang_Object())) {
       
    58     // If we are not trying to access a protected field or method in
       
    59     // java.lang.Object then, for arrays, we only allow assignability
       
    60     // to interfaces java.lang.Cloneable and java.io.Serializable.
       
    61     // Otherwise, we treat interfaces as java.lang.Object.
       
    62     return !from_is_array ||
       
    63       this_class == SystemDictionary::Cloneable_klass() ||
       
    64       this_class == SystemDictionary::Serializable_klass();
       
    65   } else if (from_is_object) {
       
    66     Klass* from_class = SystemDictionary::resolve_or_fail(
       
    67         from_name, Handle(THREAD, klass->class_loader()),
       
    68         Handle(THREAD, klass->protection_domain()), true, CHECK_false);
       
    69     if (log_is_enabled(Debug, class, resolve)) {
       
    70       Verifier::trace_class_resolution(from_class, klass());
       
    71     }
       
    72     return InstanceKlass::cast(from_class)->is_subclass_of(this_class());
       
    73   }
       
    74 
       
    75   return false;
    42 }
    76 }
    43 
    77 
    44 bool VerificationType::is_reference_assignable_from(
    78 bool VerificationType::is_reference_assignable_from(
    45     const VerificationType& from, ClassVerifier* context,
    79     const VerificationType& from, ClassVerifier* context,
    46     bool from_field_is_protected, TRAPS) const {
    80     bool from_field_is_protected, TRAPS) const {
    56     // We need check the class hierarchy to check assignability
    90     // We need check the class hierarchy to check assignability
    57     if (name() == vmSymbols::java_lang_Object()) {
    91     if (name() == vmSymbols::java_lang_Object()) {
    58       // any object or array is assignable to java.lang.Object
    92       // any object or array is assignable to java.lang.Object
    59       return true;
    93       return true;
    60     }
    94     }
    61     Klass* obj = SystemDictionary::resolve_or_fail(
    95 
    62         name(), Handle(THREAD, klass->class_loader()),
    96     if (DumpSharedSpaces && SystemDictionaryShared::add_verification_constraint(klass(),
    63         Handle(THREAD, klass->protection_domain()), true, CHECK_false);
    97               name(), from.name(), from_field_is_protected, from.is_array(),
    64     if (log_is_enabled(Debug, class, resolve)) {
    98               from.is_object())) {
    65       Verifier::trace_class_resolution(obj, klass());
    99       // If add_verification_constraint() returns true, the resolution/check should be
       
   100       // delayed until runtime.
       
   101       return true;
    66     }
   102     }
    67 
   103 
    68     KlassHandle this_class(THREAD, obj);
   104     return resolve_and_check_assignability(klass(), name(), from.name(),
    69 
   105           from_field_is_protected, from.is_array(), from.is_object(), THREAD);
    70     if (this_class->is_interface() && (!from_field_is_protected ||
       
    71         from.name() != vmSymbols::java_lang_Object())) {
       
    72       // If we are not trying to access a protected field or method in
       
    73       // java.lang.Object then, for arrays, we only allow assignability
       
    74       // to interfaces java.lang.Cloneable and java.io.Serializable.
       
    75       // Otherwise, we treat interfaces as java.lang.Object.
       
    76       return !from.is_array() ||
       
    77         this_class == SystemDictionary::Cloneable_klass() ||
       
    78         this_class == SystemDictionary::Serializable_klass();
       
    79     } else if (from.is_object()) {
       
    80       Klass* from_class = SystemDictionary::resolve_or_fail(
       
    81           from.name(), Handle(THREAD, klass->class_loader()),
       
    82           Handle(THREAD, klass->protection_domain()), true, CHECK_false);
       
    83       if (log_is_enabled(Debug, class, resolve)) {
       
    84         Verifier::trace_class_resolution(from_class, klass());
       
    85       }
       
    86       return InstanceKlass::cast(from_class)->is_subclass_of(this_class());
       
    87     }
       
    88   } else if (is_array() && from.is_array()) {
   106   } else if (is_array() && from.is_array()) {
    89     VerificationType comp_this = get_component(context, CHECK_false);
   107     VerificationType comp_this = get_component(context, CHECK_false);
    90     VerificationType comp_from = from.get_component(context, CHECK_false);
   108     VerificationType comp_from = from.get_component(context, CHECK_false);
    91     if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
   109     if (!comp_this.is_bogus() && !comp_from.is_bogus()) {
    92       return comp_this.is_component_assignable_from(comp_from, context,
   110       return comp_this.is_component_assignable_from(comp_from, context,