1 /* 2 * Copyright (c) 1997, 2012, 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 */ 24 25 #ifndef SHARE_VM_INTERPRETER_LINKRESOLVER_HPP 26 #define SHARE_VM_INTERPRETER_LINKRESOLVER_HPP 27 28 #include "oops/method.hpp" 29 #include "utilities/top.hpp" 30 31 // All the necessary definitions for run-time link resolution. 32 33 // LinkInfo & its subclasses provide all the information gathered 34 // for a particular link after resolving it. A link is any reference 35 // made from within the bytecodes of a method to an object outside of 36 // that method. If the info is invalid, the link has not been resolved 37 // successfully. 38 39 class LinkInfo VALUE_OBJ_CLASS_SPEC { 40 }; 41 42 43 // Link information for getfield/putfield & getstatic/putstatic bytecodes. 44 45 class FieldAccessInfo: public LinkInfo { 46 protected: 47 KlassHandle _klass; 48 Symbol* _name; 49 AccessFlags _access_flags; 50 int _field_index; // original index in the klass 51 int _field_offset; 52 BasicType _field_type; 53 54 public: 55 void set(KlassHandle klass, Symbol* name, int field_index, int field_offset, 56 BasicType field_type, AccessFlags access_flags); 57 KlassHandle klass() const { return _klass; } 58 Symbol* name() const { return _name; } 59 int field_index() const { return _field_index; } 60 int field_offset() const { return _field_offset; } 61 BasicType field_type() const { return _field_type; } 62 AccessFlags access_flags() const { return _access_flags; } 63 64 // debugging 65 void print() PRODUCT_RETURN; 66 }; 67 68 69 // Link information for all calls. 70 71 class CallInfo: public LinkInfo { 72 private: 73 KlassHandle _resolved_klass; // static receiver klass 74 KlassHandle _selected_klass; // dynamic receiver class (same as static, or subklass) 75 methodHandle _resolved_method; // static target method 76 methodHandle _selected_method; // dynamic (actual) target method 77 int _vtable_index; // vtable index of selected method 78 Handle _resolved_appendix; // extra argument in constant pool (if CPCE::has_appendix) 79 Handle _resolved_method_type; // MethodType (for invokedynamic and invokehandle call sites) 80 81 void set_static( KlassHandle resolved_klass, methodHandle resolved_method , TRAPS); 82 void set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method , TRAPS); 83 void set_virtual( KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index , TRAPS); 84 void set_handle( methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS); 85 void set_common( KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index , TRAPS); 86 87 friend class LinkResolver; 88 89 public: 90 KlassHandle resolved_klass() const { return _resolved_klass; } 91 KlassHandle selected_klass() const { return _selected_klass; } 92 methodHandle resolved_method() const { return _resolved_method; } 93 methodHandle selected_method() const { return _selected_method; } 94 Handle resolved_appendix() const { return _resolved_appendix; } 95 Handle resolved_method_type() const { return _resolved_method_type; } 96 97 BasicType result_type() const { return selected_method()->result_type(); } 98 bool has_vtable_index() const { return _vtable_index >= 0; } 99 bool is_statically_bound() const { return _vtable_index == Method::nonvirtual_vtable_index; } 100 int vtable_index() const { 101 // Even for interface calls the vtable index could be non-negative. 102 // See CallInfo::set_interface. 103 assert(has_vtable_index() || is_statically_bound(), ""); 104 return _vtable_index; 105 } 106 }; 107 108 109 // The LinkResolver is used to resolve constant-pool references at run-time. 110 // It does all necessary link-time checks & throws exceptions if necessary. 111 112 class LinkResolver: AllStatic { 113 private: 114 static void lookup_method_in_klasses (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 115 static void lookup_instance_method_in_klasses (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 116 static void lookup_method_in_interfaces (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 117 static void lookup_polymorphic_method (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, 118 KlassHandle current_klass, Handle *appendix_result_or_null, Handle *method_type_result, TRAPS); 119 120 static int vtable_index_of_miranda_method(KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 121 122 static void resolve_klass (KlassHandle& result, constantPoolHandle pool, int index, TRAPS); 123 static void resolve_klass_no_update (KlassHandle& result, constantPoolHandle pool, int index, TRAPS); // no update of constantPool entry 124 125 static void resolve_pool (KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature, KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS); 126 127 static void resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 128 static void resolve_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 129 130 static void linktime_resolve_static_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 131 static void linktime_resolve_special_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 132 static void linktime_resolve_virtual_method (methodHandle &resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature,KlassHandle current_klass, bool check_access, TRAPS); 133 static void linktime_resolve_interface_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 134 135 static void runtime_resolve_special_method (CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, KlassHandle current_klass, bool check_access, TRAPS); 136 static void runtime_resolve_virtual_method (CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS); 137 static void runtime_resolve_interface_method (CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS); 138 139 static void check_field_accessability (KlassHandle ref_klass, KlassHandle resolved_klass, KlassHandle sel_klass, fieldDescriptor& fd, TRAPS); 140 static void check_method_accessability (KlassHandle ref_klass, KlassHandle resolved_klass, KlassHandle sel_klass, methodHandle sel_method, TRAPS); 141 142 public: 143 // constant pool resolving 144 static void check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS); 145 146 // static resolving calls (will not run any Java code); used only from Bytecode_invoke::static_target 147 static void resolve_method_statically(methodHandle& method_result, KlassHandle& klass_result, 148 Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS); 149 150 // runtime/static resolving for fields 151 static void resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, TRAPS); 152 // takes an extra bool argument "update_pool" to decide whether to update the constantPool during klass resolution. 153 static void resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, bool update_pool, TRAPS); 154 155 // runtime resolving: 156 // resolved_klass = specified class (i.e., static receiver class) 157 // current_klass = sending method holder (i.e., class containing the method containing the call being resolved) 158 static void resolve_static_call (CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, bool initialize_klass, TRAPS); 159 static void resolve_special_call (CallInfo& result, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 160 static void resolve_virtual_call (CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, bool check_null_and_abstract, TRAPS); 161 static void resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, bool check_null_and_abstract, TRAPS); 162 static void resolve_handle_call (CallInfo& result, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, TRAPS); 163 static void resolve_dynamic_call (CallInfo& result, Handle bootstrap_specifier, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, TRAPS); 164 165 // same as above for compile-time resolution; but returns null handle instead of throwing an exception on error 166 // also, does not initialize klass (i.e., no side effects) 167 static methodHandle resolve_virtual_call_or_null (KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 168 static methodHandle resolve_interface_call_or_null(KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 169 static methodHandle resolve_static_call_or_null (KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 170 static methodHandle resolve_special_call_or_null (KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 171 172 // same as above for compile-time resolution; returns vtable_index if current_klass if linked 173 static int resolve_virtual_vtable_index (KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); | 1 /* 2 * Copyright (c) 1997, 2013, 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 */ 24 25 #ifndef SHARE_VM_INTERPRETER_LINKRESOLVER_HPP 26 #define SHARE_VM_INTERPRETER_LINKRESOLVER_HPP 27 28 #include "oops/method.hpp" 29 #include "utilities/top.hpp" 30 31 // All the necessary definitions for run-time link resolution. 32 33 // CallInfo provides all the information gathered for a particular 34 // linked call site after resolving it. A link is any reference 35 // made from within the bytecodes of a method to an object outside of 36 // that method. If the info is invalid, the link has not been resolved 37 // successfully. 38 39 class CallInfo VALUE_OBJ_CLASS_SPEC { 40 public: 41 // Ways that a method call might be selected (or not) based on receiver type. 42 // Note that an invokevirtual instruction might be linked with no_dispatch, 43 // and an invokeinterface instruction might be linked with any of the three options 44 enum CallKind { 45 direct_call, // jump into resolved_method (must be concrete) 46 vtable_call, // select recv.klass.method_at_vtable(index) 47 itable_call, // select recv.klass.method_at_itable(resolved_method.holder, index) 48 unknown_kind = -1 49 }; 50 private: 51 KlassHandle _resolved_klass; // static receiver klass, resolved from a symbolic reference 52 KlassHandle _selected_klass; // dynamic receiver class (same as static, or subklass) 53 methodHandle _resolved_method; // static target method 54 methodHandle _selected_method; // dynamic (actual) target method 55 CallKind _call_kind; // kind of call (static, vtable, itable) 56 int _call_index; // vtable or itable index of selected class method (if any) 57 Handle _resolved_appendix; // extra argument in constant pool (if CPCE::has_appendix) 58 Handle _resolved_method_type; // MethodType (for invokedynamic and invokehandle call sites) 59 60 void set_static( KlassHandle resolved_klass, methodHandle resolved_method , TRAPS); 61 void set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index , TRAPS); 62 void set_virtual( KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index , TRAPS); 63 void set_handle( methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS); 64 void set_common( KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, CallKind kind, int index, TRAPS); 65 66 friend class LinkResolver; 67 68 public: 69 CallInfo() { 70 #ifndef PRODUCT 71 _call_kind = CallInfo::unknown_kind; 72 _call_index = Method::garbage_vtable_index; 73 #endif //PRODUCT 74 } 75 76 // utility to extract an effective CallInfo from a method and an optional receiver limit 77 // does not queue the method for compilation 78 CallInfo(Method* resolved_method, Klass* resolved_klass = NULL); 79 80 KlassHandle resolved_klass() const { return _resolved_klass; } 81 KlassHandle selected_klass() const { return _selected_klass; } 82 methodHandle resolved_method() const { return _resolved_method; } 83 methodHandle selected_method() const { return _selected_method; } 84 Handle resolved_appendix() const { return _resolved_appendix; } 85 Handle resolved_method_type() const { return _resolved_method_type; } 86 87 BasicType result_type() const { return selected_method()->result_type(); } 88 CallKind call_kind() const { return _call_kind; } 89 int call_index() const { return _call_index; } 90 int vtable_index() const { 91 // Even for interface calls the vtable index could be non-negative. 92 // See CallInfo::set_interface. 93 assert(has_vtable_index() || is_statically_bound(), ""); 94 assert(call_kind() == vtable_call || call_kind() == direct_call, ""); 95 // The returned value is < 0 if the call is statically bound. 96 // But, the returned value may be >= 0 even if the kind is direct_call. 97 // It is up to the caller to decide which way to go. 98 return _call_index; 99 } 100 int itable_index() const { 101 assert(call_kind() == itable_call, ""); 102 // The returned value is always >= 0, a valid itable index. 103 return _call_index; 104 } 105 106 // debugging 107 #ifdef ASSERT 108 bool has_vtable_index() const { return _call_index >= 0 && _call_kind != CallInfo::itable_call; } 109 bool is_statically_bound() const { return _call_index == Method::nonvirtual_vtable_index; } 110 #endif //ASSERT 111 void verify() PRODUCT_RETURN; 112 void print() PRODUCT_RETURN; 113 }; 114 115 // Link information for getfield/putfield & getstatic/putstatic bytecodes 116 // is represented using a fieldDescriptor. 117 118 // The LinkResolver is used to resolve constant-pool references at run-time. 119 // It does all necessary link-time checks & throws exceptions if necessary. 120 121 class LinkResolver: AllStatic { 122 friend class klassVtable; 123 friend class klassItable; 124 125 private: 126 static void lookup_method_in_klasses (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 127 static void lookup_instance_method_in_klasses (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 128 static void lookup_method_in_interfaces (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 129 static void lookup_polymorphic_method (methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, 130 KlassHandle current_klass, Handle *appendix_result_or_null, Handle *method_type_result, TRAPS); 131 132 static int vtable_index_of_miranda_method(KlassHandle klass, Symbol* name, Symbol* signature, TRAPS); 133 134 static void resolve_klass (KlassHandle& result, constantPoolHandle pool, int index, TRAPS); 135 136 static void resolve_pool (KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature, KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS); 137 138 static void resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 139 static void resolve_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 140 141 static void linktime_resolve_static_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 142 static void linktime_resolve_special_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 143 static void linktime_resolve_virtual_method (methodHandle &resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature,KlassHandle current_klass, bool check_access, TRAPS); 144 static void linktime_resolve_interface_method (methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 145 146 static void runtime_resolve_special_method (CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, KlassHandle current_klass, bool check_access, TRAPS); 147 static void runtime_resolve_virtual_method (CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS); 148 static void runtime_resolve_interface_method (CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS); 149 150 static void check_field_accessability (KlassHandle ref_klass, KlassHandle resolved_klass, KlassHandle sel_klass, fieldDescriptor& fd, TRAPS); 151 static void check_method_accessability (KlassHandle ref_klass, KlassHandle resolved_klass, KlassHandle sel_klass, methodHandle sel_method, TRAPS); 152 153 public: 154 // constant pool resolving 155 static void check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS); 156 157 // static resolving calls (will not run any Java code); used only from Bytecode_invoke::static_target 158 static void resolve_method_statically(methodHandle& method_result, KlassHandle& klass_result, 159 Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS); 160 161 // runtime/static resolving for fields 162 static void resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS); 163 static void resolve_field(fieldDescriptor& result, KlassHandle resolved_klass, Symbol* field_name, Symbol* field_signature, 164 KlassHandle current_klass, Bytecodes::Code access_kind, bool check_access, bool initialize_class, TRAPS); 165 166 // source of access_kind codes: 167 static Bytecodes::Code field_access_kind(bool is_static, bool is_put) { 168 return (is_static 169 ? (is_put ? Bytecodes::_putstatic : Bytecodes::_getstatic) 170 : (is_put ? Bytecodes::_putfield : Bytecodes::_getfield )); 171 } 172 173 // runtime resolving: 174 // resolved_klass = specified class (i.e., static receiver class) 175 // current_klass = sending method holder (i.e., class containing the method containing the call being resolved) 176 static void resolve_static_call (CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, bool initialize_klass, TRAPS); 177 static void resolve_special_call (CallInfo& result, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS); 178 static void resolve_virtual_call (CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, bool check_null_and_abstract, TRAPS); 179 static void resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, bool check_access, bool check_null_and_abstract, TRAPS); 180 static void resolve_handle_call (CallInfo& result, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, TRAPS); 181 static void resolve_dynamic_call (CallInfo& result, Handle bootstrap_specifier, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, TRAPS); 182 183 // same as above for compile-time resolution; but returns null handle instead of throwing an exception on error 184 // also, does not initialize klass (i.e., no side effects) 185 static methodHandle resolve_virtual_call_or_null (KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 186 static methodHandle resolve_interface_call_or_null(KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 187 static methodHandle resolve_static_call_or_null (KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 188 static methodHandle resolve_special_call_or_null (KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); 189 190 // same as above for compile-time resolution; returns vtable_index if current_klass if linked 191 static int resolve_virtual_vtable_index (KlassHandle receiver_klass, KlassHandle resolved_klass, Symbol* method_name, Symbol* method_signature, KlassHandle current_klass); |