Mon, 27 Mar 2017 11:31:27 -0700
Merge
duke@435 | 1 | /* |
dbuck@8716 | 2 | * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
kamg@4245 | 26 | #include "classfile/defaultMethods.hpp" |
stefank@2314 | 27 | #include "classfile/systemDictionary.hpp" |
stefank@2314 | 28 | #include "classfile/vmSymbols.hpp" |
stefank@2314 | 29 | #include "compiler/compileBroker.hpp" |
stefank@2314 | 30 | #include "gc_interface/collectedHeap.inline.hpp" |
stefank@2314 | 31 | #include "interpreter/bytecode.hpp" |
stefank@2314 | 32 | #include "interpreter/interpreterRuntime.hpp" |
stefank@2314 | 33 | #include "interpreter/linkResolver.hpp" |
stefank@2314 | 34 | #include "memory/resourceArea.hpp" |
stefank@2314 | 35 | #include "memory/universe.inline.hpp" |
stefank@2314 | 36 | #include "oops/instanceKlass.hpp" |
stefank@2314 | 37 | #include "oops/objArrayOop.hpp" |
stefank@2314 | 38 | #include "prims/methodHandles.hpp" |
stefank@2314 | 39 | #include "prims/nativeLookup.hpp" |
stefank@2314 | 40 | #include "runtime/compilationPolicy.hpp" |
stefank@2314 | 41 | #include "runtime/fieldDescriptor.hpp" |
stefank@2314 | 42 | #include "runtime/frame.inline.hpp" |
stefank@2314 | 43 | #include "runtime/handles.inline.hpp" |
stefank@2314 | 44 | #include "runtime/reflection.hpp" |
stefank@2314 | 45 | #include "runtime/signature.hpp" |
stefank@4299 | 46 | #include "runtime/thread.inline.hpp" |
stefank@2314 | 47 | #include "runtime/vmThread.hpp" |
duke@435 | 48 | |
duke@435 | 49 | |
duke@435 | 50 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 51 | // Implementation of CallInfo |
duke@435 | 52 | |
duke@435 | 53 | |
duke@435 | 54 | void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) { |
coleenp@4037 | 55 | int vtable_index = Method::nonvirtual_vtable_index; |
drchase@5732 | 56 | set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); |
duke@435 | 57 | } |
duke@435 | 58 | |
duke@435 | 59 | |
drchase@5732 | 60 | void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index, TRAPS) { |
duke@435 | 61 | // This is only called for interface methods. If the resolved_method |
duke@435 | 62 | // comes from java/lang/Object, it can be the subject of a virtual call, so |
duke@435 | 63 | // we should pick the vtable index from the resolved method. |
drchase@5732 | 64 | // In that case, the caller must call set_virtual instead of set_interface. |
drchase@5732 | 65 | assert(resolved_method->method_holder()->is_interface(), ""); |
drchase@5732 | 66 | assert(itable_index == resolved_method()->itable_index(), ""); |
drchase@5732 | 67 | set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK); |
duke@435 | 68 | } |
duke@435 | 69 | |
duke@435 | 70 | void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) { |
coleenp@4037 | 71 | assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index"); |
drchase@5732 | 72 | assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), ""); |
drchase@5732 | 73 | CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call); |
drchase@5732 | 74 | set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK); |
twisti@3969 | 75 | assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call"); |
duke@435 | 76 | } |
duke@435 | 77 | |
twisti@4133 | 78 | void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS) { |
twisti@3969 | 79 | if (resolved_method.is_null()) { |
twisti@3969 | 80 | THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null"); |
twisti@3969 | 81 | } |
coleenp@4037 | 82 | KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass(); |
twisti@3969 | 83 | assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic || |
twisti@3969 | 84 | resolved_method->is_compiled_lambda_form(), |
twisti@3969 | 85 | "linkMethod must return one of these"); |
coleenp@4037 | 86 | int vtable_index = Method::nonvirtual_vtable_index; |
drchase@5732 | 87 | assert(!resolved_method->has_vtable_index(), ""); |
drchase@5732 | 88 | set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); |
twisti@4133 | 89 | _resolved_appendix = resolved_appendix; |
twisti@4133 | 90 | _resolved_method_type = resolved_method_type; |
jrose@2015 | 91 | } |
jrose@2015 | 92 | |
drchase@5732 | 93 | void CallInfo::set_common(KlassHandle resolved_klass, |
drchase@5732 | 94 | KlassHandle selected_klass, |
drchase@5732 | 95 | methodHandle resolved_method, |
drchase@5732 | 96 | methodHandle selected_method, |
drchase@5732 | 97 | CallKind kind, |
drchase@5732 | 98 | int index, |
drchase@5732 | 99 | TRAPS) { |
duke@435 | 100 | assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond"); |
duke@435 | 101 | _resolved_klass = resolved_klass; |
duke@435 | 102 | _selected_klass = selected_klass; |
duke@435 | 103 | _resolved_method = resolved_method; |
duke@435 | 104 | _selected_method = selected_method; |
drchase@5732 | 105 | _call_kind = kind; |
drchase@5732 | 106 | _call_index = index; |
twisti@3969 | 107 | _resolved_appendix = Handle(); |
drchase@5732 | 108 | DEBUG_ONLY(verify()); // verify before making side effects |
drchase@5732 | 109 | |
iveresov@2138 | 110 | if (CompilationPolicy::must_be_compiled(selected_method)) { |
twisti@1570 | 111 | // This path is unusual, mostly used by the '-Xcomp' stress test mode. |
twisti@1570 | 112 | |
iveresov@2138 | 113 | // Note: with several active threads, the must_be_compiled may be true |
iveresov@2138 | 114 | // while can_be_compiled is false; remove assert |
iveresov@2138 | 115 | // assert(CompilationPolicy::can_be_compiled(selected_method), "cannot compile"); |
duke@435 | 116 | if (THREAD->is_Compiler_thread()) { |
duke@435 | 117 | // don't force compilation, resolve was on behalf of compiler |
duke@435 | 118 | return; |
duke@435 | 119 | } |
coleenp@4251 | 120 | if (selected_method->method_holder()->is_not_initialized()) { |
twisti@1570 | 121 | // 'is_not_initialized' means not only '!is_initialized', but also that |
twisti@1570 | 122 | // initialization has not been started yet ('!being_initialized') |
twisti@1570 | 123 | // Do not force compilation of methods in uninitialized classes. |
twisti@1570 | 124 | // Note that doing this would throw an assert later, |
twisti@1570 | 125 | // in CompileBroker::compile_method. |
twisti@1570 | 126 | // We sometimes use the link resolver to do reflective lookups |
twisti@1570 | 127 | // even before classes are initialized. |
twisti@1570 | 128 | return; |
twisti@1570 | 129 | } |
duke@435 | 130 | CompileBroker::compile_method(selected_method, InvocationEntryBci, |
iveresov@3035 | 131 | CompilationPolicy::policy()->initial_compile_level(), |
iveresov@2138 | 132 | methodHandle(), 0, "must_be_compiled", CHECK); |
duke@435 | 133 | } |
duke@435 | 134 | } |
duke@435 | 135 | |
drchase@5732 | 136 | // utility query for unreflecting a method |
drchase@5732 | 137 | CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) { |
drchase@5732 | 138 | Klass* resolved_method_holder = resolved_method->method_holder(); |
drchase@5732 | 139 | if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st |
drchase@5732 | 140 | resolved_klass = resolved_method_holder; |
drchase@5732 | 141 | } |
drchase@5732 | 142 | _resolved_klass = resolved_klass; |
drchase@5732 | 143 | _selected_klass = resolved_klass; |
drchase@5732 | 144 | _resolved_method = resolved_method; |
drchase@5732 | 145 | _selected_method = resolved_method; |
drchase@5732 | 146 | // classify: |
drchase@5732 | 147 | CallKind kind = CallInfo::unknown_kind; |
drchase@5732 | 148 | int index = resolved_method->vtable_index(); |
drchase@5732 | 149 | if (resolved_method->can_be_statically_bound()) { |
drchase@5732 | 150 | kind = CallInfo::direct_call; |
drchase@5732 | 151 | } else if (!resolved_method_holder->is_interface()) { |
drchase@5732 | 152 | // Could be an Object method inherited into an interface, but still a vtable call. |
drchase@5732 | 153 | kind = CallInfo::vtable_call; |
drchase@5732 | 154 | } else if (!resolved_klass->is_interface()) { |
acorn@6080 | 155 | // A default or miranda method. Compute the vtable index. |
drchase@5732 | 156 | ResourceMark rm; |
drchase@5732 | 157 | klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable(); |
acorn@6080 | 158 | index = LinkResolver::vtable_index_of_interface_method(resolved_klass, |
acorn@6080 | 159 | resolved_method); |
acorn@6080 | 160 | assert(index >= 0 , "we should have valid vtable index at this point"); |
acorn@6080 | 161 | |
drchase@5732 | 162 | kind = CallInfo::vtable_call; |
drchase@5924 | 163 | } else if (resolved_method->has_vtable_index()) { |
drchase@5924 | 164 | // Can occur if an interface redeclares a method of Object. |
drchase@5924 | 165 | |
drchase@5924 | 166 | #ifdef ASSERT |
drchase@5924 | 167 | // Ensure that this is really the case. |
drchase@5924 | 168 | KlassHandle object_klass = SystemDictionary::Object_klass(); |
drchase@5924 | 169 | Method * object_resolved_method = object_klass()->vtable()->method_at(index); |
drchase@5924 | 170 | assert(object_resolved_method->name() == resolved_method->name(), |
drchase@5924 | 171 | err_msg("Object and interface method names should match at vtable index %d, %s != %s", |
drchase@5924 | 172 | index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string())); |
drchase@5924 | 173 | assert(object_resolved_method->signature() == resolved_method->signature(), |
drchase@5924 | 174 | err_msg("Object and interface method signatures should match at vtable index %d, %s != %s", |
drchase@5924 | 175 | index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string())); |
drchase@5924 | 176 | #endif // ASSERT |
drchase@5924 | 177 | |
drchase@5924 | 178 | kind = CallInfo::vtable_call; |
drchase@5732 | 179 | } else { |
drchase@5732 | 180 | // A regular interface call. |
drchase@5732 | 181 | kind = CallInfo::itable_call; |
drchase@5732 | 182 | index = resolved_method->itable_index(); |
drchase@5732 | 183 | } |
drchase@5732 | 184 | assert(index == Method::nonvirtual_vtable_index || index >= 0, err_msg("bad index %d", index)); |
drchase@5732 | 185 | _call_kind = kind; |
drchase@5732 | 186 | _call_index = index; |
drchase@5732 | 187 | _resolved_appendix = Handle(); |
drchase@5732 | 188 | DEBUG_ONLY(verify()); |
drchase@5732 | 189 | } |
drchase@5732 | 190 | |
drchase@5732 | 191 | #ifdef ASSERT |
drchase@5732 | 192 | void CallInfo::verify() { |
drchase@5732 | 193 | switch (call_kind()) { // the meaning and allowed value of index depends on kind |
drchase@5732 | 194 | case CallInfo::direct_call: |
drchase@5732 | 195 | if (_call_index == Method::nonvirtual_vtable_index) break; |
drchase@5732 | 196 | // else fall through to check vtable index: |
drchase@5732 | 197 | case CallInfo::vtable_call: |
drchase@5732 | 198 | assert(resolved_klass()->verify_vtable_index(_call_index), ""); |
drchase@5732 | 199 | break; |
drchase@5732 | 200 | case CallInfo::itable_call: |
drchase@5732 | 201 | assert(resolved_method()->method_holder()->verify_itable_index(_call_index), ""); |
drchase@5732 | 202 | break; |
drchase@5732 | 203 | case CallInfo::unknown_kind: |
drchase@5732 | 204 | assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set"); |
drchase@5732 | 205 | break; |
drchase@5732 | 206 | default: |
drchase@5732 | 207 | fatal(err_msg_res("Unexpected call kind %d", call_kind())); |
drchase@5732 | 208 | } |
drchase@5732 | 209 | } |
drchase@5732 | 210 | #endif //ASSERT |
drchase@5732 | 211 | |
drchase@5732 | 212 | |
duke@435 | 213 | |
duke@435 | 214 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 215 | // Klass resolution |
duke@435 | 216 | |
duke@435 | 217 | void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) { |
coleenp@4037 | 218 | if (!Reflection::verify_class_access(ref_klass(), |
coleenp@4037 | 219 | sel_klass(), |
duke@435 | 220 | true)) { |
duke@435 | 221 | ResourceMark rm(THREAD); |
duke@435 | 222 | Exceptions::fthrow( |
duke@435 | 223 | THREAD_AND_LOCATION, |
coleenp@2497 | 224 | vmSymbols::java_lang_IllegalAccessError(), |
duke@435 | 225 | "tried to access class %s from class %s", |
duke@435 | 226 | sel_klass->external_name(), |
duke@435 | 227 | ref_klass->external_name() |
duke@435 | 228 | ); |
duke@435 | 229 | return; |
duke@435 | 230 | } |
duke@435 | 231 | } |
duke@435 | 232 | |
duke@435 | 233 | void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) { |
coleenp@4037 | 234 | Klass* result_oop = pool->klass_ref_at(index, CHECK); |
duke@435 | 235 | result = KlassHandle(THREAD, result_oop); |
duke@435 | 236 | } |
duke@435 | 237 | |
duke@435 | 238 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 239 | // Method resolution |
duke@435 | 240 | // |
duke@435 | 241 | // According to JVM spec. $5.4.3c & $5.4.3d |
duke@435 | 242 | |
acorn@5848 | 243 | // Look up method in klasses, including static methods |
acorn@5848 | 244 | // Then look up local default methods |
hseigel@6167 | 245 | void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, bool checkpolymorphism, bool in_imethod_resolve, TRAPS) { |
lfoltan@6632 | 246 | // Ignore overpasses so statics can be found during resolution |
lfoltan@6632 | 247 | Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::skip_overpass); |
hseigel@6167 | 248 | |
lfoltan@6818 | 249 | if (klass->oop_is_array()) { |
lfoltan@6818 | 250 | // Only consider klass and super klass for arrays |
lfoltan@6818 | 251 | result = methodHandle(THREAD, result_oop); |
lfoltan@6818 | 252 | return; |
lfoltan@6818 | 253 | } |
lfoltan@6818 | 254 | |
hseigel@6167 | 255 | // JDK 8, JVMS 5.4.3.4: Interface method resolution should |
hseigel@6167 | 256 | // ignore static and non-public methods of java.lang.Object, |
hseigel@6167 | 257 | // like clone, finalize, registerNatives. |
hseigel@6167 | 258 | if (in_imethod_resolve && |
hseigel@6167 | 259 | result_oop != NULL && |
hseigel@6167 | 260 | klass->is_interface() && |
hseigel@6167 | 261 | (result_oop->is_static() || !result_oop->is_public()) && |
hseigel@6167 | 262 | result_oop->method_holder() == SystemDictionary::Object_klass()) { |
hseigel@6167 | 263 | result_oop = NULL; |
hseigel@6167 | 264 | } |
hseigel@6167 | 265 | |
lfoltan@6632 | 266 | // Before considering default methods, check for an overpass in the |
lfoltan@6632 | 267 | // current class if a method has not been found. |
lfoltan@6632 | 268 | if (result_oop == NULL) { |
lfoltan@6632 | 269 | result_oop = InstanceKlass::cast(klass())->find_method(name, signature); |
lfoltan@6632 | 270 | } |
lfoltan@6632 | 271 | |
acorn@5848 | 272 | if (result_oop == NULL) { |
acorn@5848 | 273 | Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods(); |
acorn@5848 | 274 | if (default_methods != NULL) { |
acorn@5848 | 275 | result_oop = InstanceKlass::find_method(default_methods, name, signature); |
acorn@5848 | 276 | } |
acorn@5848 | 277 | } |
acorn@5848 | 278 | |
acorn@6144 | 279 | if (checkpolymorphism && EnableInvokeDynamic && result_oop != NULL) { |
twisti@3969 | 280 | vmIntrinsics::ID iid = result_oop->intrinsic_id(); |
twisti@3969 | 281 | if (MethodHandles::is_signature_polymorphic(iid)) { |
twisti@3969 | 282 | // Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method. |
jrose@1862 | 283 | return; |
jrose@1862 | 284 | } |
jrose@1862 | 285 | } |
duke@435 | 286 | result = methodHandle(THREAD, result_oop); |
duke@435 | 287 | } |
duke@435 | 288 | |
duke@435 | 289 | // returns first instance method |
acorn@5848 | 290 | // Looks up method in classes, then looks up local default methods |
coleenp@2497 | 291 | void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { |
dbuck@8716 | 292 | Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::find_overpass); |
duke@435 | 293 | result = methodHandle(THREAD, result_oop); |
hseigel@5959 | 294 | while (!result.is_null() && result->is_static() && result->method_holder()->super() != NULL) { |
acorn@6145 | 295 | KlassHandle super_klass = KlassHandle(THREAD, result->method_holder()->super()); |
dbuck@8716 | 296 | result = methodHandle(THREAD, super_klass->uncached_lookup_method(name, signature, Klass::find_overpass)); |
duke@435 | 297 | } |
acorn@5848 | 298 | |
lfoltan@6818 | 299 | if (klass->oop_is_array()) { |
lfoltan@6818 | 300 | // Only consider klass and super klass for arrays |
lfoltan@6818 | 301 | return; |
lfoltan@6818 | 302 | } |
lfoltan@6818 | 303 | |
acorn@5848 | 304 | if (result.is_null()) { |
acorn@5848 | 305 | Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods(); |
acorn@5848 | 306 | if (default_methods != NULL) { |
acorn@5848 | 307 | result = methodHandle(InstanceKlass::find_method(default_methods, name, signature)); |
acorn@5848 | 308 | assert(result.is_null() || !result->is_static(), "static defaults not allowed"); |
acorn@5848 | 309 | } |
acorn@5848 | 310 | } |
duke@435 | 311 | } |
duke@435 | 312 | |
acorn@5848 | 313 | int LinkResolver::vtable_index_of_interface_method(KlassHandle klass, |
acorn@6080 | 314 | methodHandle resolved_method) { |
duke@435 | 315 | |
acorn@5848 | 316 | int vtable_index = Method::invalid_vtable_index; |
acorn@5848 | 317 | Symbol* name = resolved_method->name(); |
acorn@5848 | 318 | Symbol* signature = resolved_method->signature(); |
acorn@5848 | 319 | |
acorn@5848 | 320 | // First check in default method array |
hseigel@6195 | 321 | if (!resolved_method->is_abstract() && |
acorn@5848 | 322 | (InstanceKlass::cast(klass())->default_methods() != NULL)) { |
dbuck@8716 | 323 | int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(), |
dbuck@8716 | 324 | name, signature, Klass::find_overpass, |
dbuck@8716 | 325 | Klass::find_static, Klass::find_private); |
acorn@5848 | 326 | if (index >= 0 ) { |
acorn@5848 | 327 | vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index); |
acorn@5848 | 328 | } |
acorn@5848 | 329 | } |
acorn@5848 | 330 | if (vtable_index == Method::invalid_vtable_index) { |
acorn@5848 | 331 | // get vtable_index for miranda methods |
acorn@6080 | 332 | ResourceMark rm; |
acorn@5848 | 333 | klassVtable *vt = InstanceKlass::cast(klass())->vtable(); |
acorn@5848 | 334 | vtable_index = vt->index_of_miranda(name, signature); |
acorn@5848 | 335 | } |
acorn@5848 | 336 | return vtable_index; |
duke@435 | 337 | } |
duke@435 | 338 | |
coleenp@2497 | 339 | void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) { |
coleenp@4037 | 340 | InstanceKlass *ik = InstanceKlass::cast(klass()); |
hseigel@6195 | 341 | |
hseigel@6195 | 342 | // Specify 'true' in order to skip default methods when searching the |
hseigel@6195 | 343 | // interfaces. Function lookup_method_in_klasses() already looked for |
hseigel@6195 | 344 | // the method in the default methods table. |
lfoltan@6632 | 345 | result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name, signature, Klass::skip_defaults)); |
duke@435 | 346 | } |
duke@435 | 347 | |
twisti@3969 | 348 | void LinkResolver::lookup_polymorphic_method(methodHandle& result, |
twisti@3969 | 349 | KlassHandle klass, Symbol* name, Symbol* full_signature, |
twisti@3969 | 350 | KlassHandle current_klass, |
twisti@4133 | 351 | Handle *appendix_result_or_null, |
twisti@4133 | 352 | Handle *method_type_result, |
twisti@3969 | 353 | TRAPS) { |
twisti@3969 | 354 | vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name); |
twisti@3969 | 355 | if (TraceMethodHandles) { |
kmo@4910 | 356 | ResourceMark rm(THREAD); |
twisti@3969 | 357 | tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s", |
twisti@3969 | 358 | vmIntrinsics::name_at(iid), klass->external_name(), |
twisti@3969 | 359 | name->as_C_string(), full_signature->as_C_string()); |
twisti@3969 | 360 | } |
twisti@2698 | 361 | if (EnableInvokeDynamic && |
jrose@1862 | 362 | klass() == SystemDictionary::MethodHandle_klass() && |
twisti@3969 | 363 | iid != vmIntrinsics::_none) { |
twisti@3969 | 364 | if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) { |
twisti@3969 | 365 | // Most of these do not need an up-call to Java to resolve, so can be done anywhere. |
twisti@3969 | 366 | // Do not erase last argument type (MemberName) if it is a static linkTo method. |
twisti@3969 | 367 | bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid); |
twisti@3969 | 368 | TempNewSymbol basic_signature = |
twisti@3969 | 369 | MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK); |
twisti@3969 | 370 | if (TraceMethodHandles) { |
kmo@4910 | 371 | ResourceMark rm(THREAD); |
twisti@3969 | 372 | tty->print_cr("lookup_polymorphic_method %s %s => basic %s", |
twisti@3969 | 373 | name->as_C_string(), |
twisti@3969 | 374 | full_signature->as_C_string(), |
twisti@3969 | 375 | basic_signature->as_C_string()); |
jrose@2015 | 376 | } |
twisti@3969 | 377 | result = SystemDictionary::find_method_handle_intrinsic(iid, |
twisti@3969 | 378 | basic_signature, |
twisti@3969 | 379 | CHECK); |
twisti@3969 | 380 | if (result.not_null()) { |
twisti@3969 | 381 | assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic"); |
twisti@3969 | 382 | assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this"); |
twisti@3969 | 383 | assert(basic_signature == result->signature(), "predict the result signature"); |
twisti@3969 | 384 | if (TraceMethodHandles) { |
twisti@3969 | 385 | tty->print("lookup_polymorphic_method => intrinsic "); |
twisti@3969 | 386 | result->print_on(tty); |
twisti@3969 | 387 | } |
twisti@3969 | 388 | return; |
twisti@3969 | 389 | } |
twisti@3969 | 390 | } else if (iid == vmIntrinsics::_invokeGeneric |
twisti@3969 | 391 | && !THREAD->is_Compiler_thread() |
twisti@3969 | 392 | && appendix_result_or_null != NULL) { |
twisti@3969 | 393 | // This is a method with type-checking semantics. |
twisti@3969 | 394 | // We will ask Java code to spin an adapter method for it. |
twisti@3969 | 395 | if (!MethodHandles::enabled()) { |
twisti@3969 | 396 | // Make sure the Java part of the runtime has been booted up. |
coleenp@4037 | 397 | Klass* natives = SystemDictionary::MethodHandleNatives_klass(); |
coleenp@4037 | 398 | if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) { |
twisti@3969 | 399 | SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(), |
twisti@3969 | 400 | Handle(), |
twisti@3969 | 401 | Handle(), |
twisti@3969 | 402 | true, |
twisti@3969 | 403 | CHECK); |
twisti@3969 | 404 | } |
twisti@3969 | 405 | } |
twisti@3969 | 406 | |
twisti@3969 | 407 | Handle appendix; |
twisti@4133 | 408 | Handle method_type; |
twisti@3969 | 409 | result = SystemDictionary::find_method_handle_invoker(name, |
twisti@3969 | 410 | full_signature, |
twisti@3969 | 411 | current_klass, |
twisti@3969 | 412 | &appendix, |
twisti@4133 | 413 | &method_type, |
twisti@3969 | 414 | CHECK); |
twisti@3969 | 415 | if (TraceMethodHandles) { |
twisti@3969 | 416 | tty->print("lookup_polymorphic_method => (via Java) "); |
twisti@3969 | 417 | result->print_on(tty); |
twisti@3969 | 418 | tty->print(" lookup_polymorphic_method => appendix = "); |
twisti@3969 | 419 | if (appendix.is_null()) tty->print_cr("(none)"); |
twisti@3969 | 420 | else appendix->print_on(tty); |
twisti@3969 | 421 | } |
twisti@3969 | 422 | if (result.not_null()) { |
twisti@3969 | 423 | #ifdef ASSERT |
kmo@4910 | 424 | ResourceMark rm(THREAD); |
kmo@4910 | 425 | |
twisti@3969 | 426 | TempNewSymbol basic_signature = |
twisti@3969 | 427 | MethodHandles::lookup_basic_type_signature(full_signature, CHECK); |
twisti@3969 | 428 | int actual_size_of_params = result->size_of_parameters(); |
twisti@3969 | 429 | int expected_size_of_params = ArgumentSizeComputer(basic_signature).size(); |
twisti@3969 | 430 | // +1 for MethodHandle.this, +1 for trailing MethodType |
twisti@3969 | 431 | if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1; |
twisti@3969 | 432 | if (appendix.not_null()) expected_size_of_params += 1; |
twisti@3969 | 433 | if (actual_size_of_params != expected_size_of_params) { |
twisti@3969 | 434 | tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string()); |
twisti@3969 | 435 | tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid)); |
twisti@3969 | 436 | result->print(); |
twisti@3969 | 437 | } |
twisti@3969 | 438 | assert(actual_size_of_params == expected_size_of_params, |
twisti@3969 | 439 | err_msg("%d != %d", actual_size_of_params, expected_size_of_params)); |
twisti@3969 | 440 | #endif //ASSERT |
twisti@3969 | 441 | |
twisti@3969 | 442 | assert(appendix_result_or_null != NULL, ""); |
twisti@3969 | 443 | (*appendix_result_or_null) = appendix; |
twisti@4133 | 444 | (*method_type_result) = method_type; |
twisti@3969 | 445 | return; |
twisti@3969 | 446 | } |
jrose@1145 | 447 | } |
jrose@1145 | 448 | } |
jrose@1145 | 449 | } |
jrose@1145 | 450 | |
duke@435 | 451 | void LinkResolver::check_method_accessability(KlassHandle ref_klass, |
duke@435 | 452 | KlassHandle resolved_klass, |
duke@435 | 453 | KlassHandle sel_klass, |
duke@435 | 454 | methodHandle sel_method, |
duke@435 | 455 | TRAPS) { |
duke@435 | 456 | |
duke@435 | 457 | AccessFlags flags = sel_method->access_flags(); |
duke@435 | 458 | |
hseigel@6167 | 459 | // Special case: arrays always override "clone". JVMS 2.15. |
duke@435 | 460 | // If the resolved klass is an array class, and the declaring class |
duke@435 | 461 | // is java.lang.Object and the method is "clone", set the flags |
duke@435 | 462 | // to public. |
duke@435 | 463 | // |
hseigel@6167 | 464 | // We'll check for the method name first, as that's most likely |
hseigel@6167 | 465 | // to be false (so we'll short-circuit out of these tests). |
hseigel@6167 | 466 | if (sel_method->name() == vmSymbols::clone_name() && |
hseigel@6167 | 467 | sel_klass() == SystemDictionary::Object_klass() && |
hseigel@6167 | 468 | resolved_klass->oop_is_array()) { |
duke@435 | 469 | // We need to change "protected" to "public". |
hseigel@6167 | 470 | assert(flags.is_protected(), "clone not protected?"); |
duke@435 | 471 | jint new_flags = flags.as_int(); |
duke@435 | 472 | new_flags = new_flags & (~JVM_ACC_PROTECTED); |
duke@435 | 473 | new_flags = new_flags | JVM_ACC_PUBLIC; |
duke@435 | 474 | flags.set_flags(new_flags); |
duke@435 | 475 | } |
twisti@3969 | 476 | // assert(extra_arg_result_or_null != NULL, "must be able to return extra argument"); |
duke@435 | 477 | |
coleenp@4037 | 478 | if (!Reflection::verify_field_access(ref_klass(), |
coleenp@4037 | 479 | resolved_klass(), |
coleenp@4037 | 480 | sel_klass(), |
duke@435 | 481 | flags, |
duke@435 | 482 | true)) { |
duke@435 | 483 | ResourceMark rm(THREAD); |
duke@435 | 484 | Exceptions::fthrow( |
duke@435 | 485 | THREAD_AND_LOCATION, |
coleenp@2497 | 486 | vmSymbols::java_lang_IllegalAccessError(), |
duke@435 | 487 | "tried to access method %s.%s%s from class %s", |
duke@435 | 488 | sel_klass->external_name(), |
duke@435 | 489 | sel_method->name()->as_C_string(), |
duke@435 | 490 | sel_method->signature()->as_C_string(), |
duke@435 | 491 | ref_klass->external_name() |
duke@435 | 492 | ); |
duke@435 | 493 | return; |
duke@435 | 494 | } |
duke@435 | 495 | } |
duke@435 | 496 | |
twisti@3969 | 497 | void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass, |
twisti@3969 | 498 | Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) { |
drchase@5732 | 499 | // This method is used only |
drchase@5732 | 500 | // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call), |
drchase@5732 | 501 | // and |
drchase@5732 | 502 | // (2) in Bytecode_invoke::static_target |
drchase@5732 | 503 | // It appears to fail when applied to an invokeinterface call site. |
drchase@5732 | 504 | // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points. |
duke@435 | 505 | // resolve klass |
twisti@3969 | 506 | if (code == Bytecodes::_invokedynamic) { |
coleenp@4037 | 507 | resolved_klass = SystemDictionary::MethodHandle_klass(); |
twisti@3969 | 508 | Symbol* method_name = vmSymbols::invoke_name(); |
twisti@3969 | 509 | Symbol* method_signature = pool->signature_ref_at(index); |
twisti@3969 | 510 | KlassHandle current_klass(THREAD, pool->pool_holder()); |
acorn@5897 | 511 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK); |
twisti@3969 | 512 | return; |
twisti@3969 | 513 | } |
twisti@3969 | 514 | |
duke@435 | 515 | resolve_klass(resolved_klass, pool, index, CHECK); |
duke@435 | 516 | |
coleenp@2497 | 517 | Symbol* method_name = pool->name_ref_at(index); |
coleenp@2497 | 518 | Symbol* method_signature = pool->signature_ref_at(index); |
duke@435 | 519 | KlassHandle current_klass(THREAD, pool->pool_holder()); |
duke@435 | 520 | |
jrose@2982 | 521 | if (pool->has_preresolution() |
jrose@2982 | 522 | || (resolved_klass() == SystemDictionary::MethodHandle_klass() && |
twisti@3969 | 523 | MethodHandles::is_signature_polymorphic_name(resolved_klass(), method_name))) { |
coleenp@4037 | 524 | Method* result_oop = ConstantPool::method_at_if_loaded(pool, index); |
jrose@2982 | 525 | if (result_oop != NULL) { |
jrose@2982 | 526 | resolved_method = methodHandle(THREAD, result_oop); |
jrose@2982 | 527 | return; |
jrose@2982 | 528 | } |
jrose@2982 | 529 | } |
jrose@2982 | 530 | |
twisti@3969 | 531 | if (code == Bytecodes::_invokeinterface) { |
acorn@6144 | 532 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
acorn@5897 | 533 | } else if (code == Bytecodes::_invokevirtual) { |
acorn@5897 | 534 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
acorn@6144 | 535 | } else if (!resolved_klass->is_interface()) { |
acorn@6144 | 536 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK); |
twisti@3969 | 537 | } else { |
acorn@6144 | 538 | bool nostatics = (code == Bytecodes::_invokestatic) ? false : true; |
acorn@6144 | 539 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, nostatics, CHECK); |
twisti@3969 | 540 | } |
duke@435 | 541 | } |
duke@435 | 542 | |
duke@435 | 543 | void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 544 | Symbol* method_name, Symbol* method_signature, |
acorn@5897 | 545 | KlassHandle current_klass, bool check_access, |
acorn@5897 | 546 | bool require_methodref, TRAPS) { |
duke@435 | 547 | |
twisti@3969 | 548 | Handle nested_exception; |
twisti@3969 | 549 | |
acorn@5897 | 550 | // 1. check if methodref required, that resolved_klass is not interfacemethodref |
acorn@5897 | 551 | if (require_methodref && resolved_klass->is_interface()) { |
acorn@5897 | 552 | ResourceMark rm(THREAD); |
acorn@5897 | 553 | char buf[200]; |
acorn@5897 | 554 | jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected", |
acorn@5897 | 555 | resolved_klass()->external_name()); |
acorn@5897 | 556 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
acorn@5897 | 557 | } |
acorn@5897 | 558 | |
acorn@5897 | 559 | // 2. lookup method in resolved klass and its super klasses |
hseigel@6167 | 560 | lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, true, false, CHECK); |
duke@435 | 561 | |
lfoltan@6818 | 562 | if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { // not found in the class hierarchy |
acorn@5897 | 563 | // 3. lookup method in all the interfaces implemented by the resolved klass |
duke@435 | 564 | lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK); |
duke@435 | 565 | |
duke@435 | 566 | if (resolved_method.is_null()) { |
twisti@3969 | 567 | // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc |
twisti@3969 | 568 | lookup_polymorphic_method(resolved_method, resolved_klass, method_name, method_signature, |
twisti@4133 | 569 | current_klass, (Handle*)NULL, (Handle*)NULL, THREAD); |
twisti@3969 | 570 | if (HAS_PENDING_EXCEPTION) { |
twisti@3969 | 571 | nested_exception = Handle(THREAD, PENDING_EXCEPTION); |
twisti@3969 | 572 | CLEAR_PENDING_EXCEPTION; |
twisti@3969 | 573 | } |
jrose@1145 | 574 | } |
lfoltan@6818 | 575 | } |
jrose@1145 | 576 | |
lfoltan@6818 | 577 | if (resolved_method.is_null()) { |
lfoltan@6818 | 578 | // 4. method lookup failed |
lfoltan@6818 | 579 | ResourceMark rm(THREAD); |
lfoltan@6818 | 580 | THROW_MSG_CAUSE(vmSymbols::java_lang_NoSuchMethodError(), |
lfoltan@6818 | 581 | Method::name_and_sig_as_C_string(resolved_klass(), |
lfoltan@6818 | 582 | method_name, |
lfoltan@6818 | 583 | method_signature), |
lfoltan@6818 | 584 | nested_exception); |
duke@435 | 585 | } |
duke@435 | 586 | |
ccheung@6308 | 587 | // 5. access checks, access checking may be turned off when calling from within the VM. |
duke@435 | 588 | if (check_access) { |
duke@435 | 589 | assert(current_klass.not_null() , "current_klass should not be null"); |
duke@435 | 590 | |
duke@435 | 591 | // check if method can be accessed by the referring class |
duke@435 | 592 | check_method_accessability(current_klass, |
duke@435 | 593 | resolved_klass, |
duke@435 | 594 | KlassHandle(THREAD, resolved_method->method_holder()), |
duke@435 | 595 | resolved_method, |
duke@435 | 596 | CHECK); |
duke@435 | 597 | |
duke@435 | 598 | // check loader constraints |
coleenp@4037 | 599 | Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader()); |
coleenp@4251 | 600 | Handle class_loader (THREAD, resolved_method->method_holder()->class_loader()); |
duke@435 | 601 | { |
duke@435 | 602 | ResourceMark rm(THREAD); |
acorn@4840 | 603 | Symbol* failed_type_symbol = |
duke@435 | 604 | SystemDictionary::check_signature_loaders(method_signature, loader, |
duke@435 | 605 | class_loader, true, CHECK); |
acorn@4840 | 606 | if (failed_type_symbol != NULL) { |
duke@435 | 607 | const char* msg = "loader constraint violation: when resolving method" |
duke@435 | 608 | " \"%s\" the class loader (instance of %s) of the current class, %s," |
acorn@4840 | 609 | " and the class loader (instance of %s) for the method's defining class, %s, have" |
duke@435 | 610 | " different Class objects for the type %s used in the signature"; |
hseigel@4278 | 611 | char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature); |
duke@435 | 612 | const char* loader1 = SystemDictionary::loader_name(loader()); |
coleenp@4037 | 613 | char* current = InstanceKlass::cast(current_klass())->name()->as_C_string(); |
duke@435 | 614 | const char* loader2 = SystemDictionary::loader_name(class_loader()); |
acorn@4840 | 615 | char* target = InstanceKlass::cast(resolved_method->method_holder()) |
acorn@4840 | 616 | ->name()->as_C_string(); |
acorn@4840 | 617 | char* failed_type_name = failed_type_symbol->as_C_string(); |
duke@435 | 618 | size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + |
acorn@4840 | 619 | strlen(current) + strlen(loader2) + strlen(target) + |
acorn@4840 | 620 | strlen(failed_type_name) + 1; |
duke@435 | 621 | char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); |
duke@435 | 622 | jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, |
acorn@4840 | 623 | target, failed_type_name); |
duke@435 | 624 | THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); |
duke@435 | 625 | } |
duke@435 | 626 | } |
duke@435 | 627 | } |
duke@435 | 628 | } |
duke@435 | 629 | |
duke@435 | 630 | void LinkResolver::resolve_interface_method(methodHandle& resolved_method, |
duke@435 | 631 | KlassHandle resolved_klass, |
coleenp@2497 | 632 | Symbol* method_name, |
coleenp@2497 | 633 | Symbol* method_signature, |
duke@435 | 634 | KlassHandle current_klass, |
acorn@6144 | 635 | bool check_access, |
acorn@6144 | 636 | bool nostatics, TRAPS) { |
duke@435 | 637 | |
hseigel@6195 | 638 | // check if klass is interface |
duke@435 | 639 | if (!resolved_klass->is_interface()) { |
jcoomes@2856 | 640 | ResourceMark rm(THREAD); |
duke@435 | 641 | char buf[200]; |
hseigel@4278 | 642 | jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name()); |
duke@435 | 643 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 644 | } |
duke@435 | 645 | |
duke@435 | 646 | // lookup method in this interface or its super, java.lang.Object |
acorn@6144 | 647 | // JDK8: also look for static methods |
hseigel@6167 | 648 | lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, false, true, CHECK); |
duke@435 | 649 | |
lfoltan@6818 | 650 | if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { |
duke@435 | 651 | // lookup method in all the super-interfaces |
duke@435 | 652 | lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK); |
lfoltan@6818 | 653 | } |
lfoltan@6818 | 654 | |
lfoltan@6818 | 655 | if (resolved_method.is_null()) { |
lfoltan@6818 | 656 | // no method found |
lfoltan@6818 | 657 | ResourceMark rm(THREAD); |
lfoltan@6818 | 658 | THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(), |
lfoltan@6818 | 659 | Method::name_and_sig_as_C_string(resolved_klass(), |
lfoltan@6818 | 660 | method_name, |
lfoltan@6818 | 661 | method_signature)); |
duke@435 | 662 | } |
duke@435 | 663 | |
duke@435 | 664 | if (check_access) { |
acorn@5786 | 665 | // JDK8 adds non-public interface methods, and accessability check requirement |
acorn@5786 | 666 | assert(current_klass.not_null() , "current_klass should not be null"); |
acorn@5786 | 667 | |
acorn@5786 | 668 | // check if method can be accessed by the referring class |
acorn@5786 | 669 | check_method_accessability(current_klass, |
acorn@5786 | 670 | resolved_klass, |
acorn@5786 | 671 | KlassHandle(THREAD, resolved_method->method_holder()), |
acorn@5786 | 672 | resolved_method, |
acorn@5786 | 673 | CHECK); |
acorn@5786 | 674 | |
duke@435 | 675 | HandleMark hm(THREAD); |
coleenp@4037 | 676 | Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader()); |
coleenp@4251 | 677 | Handle class_loader (THREAD, resolved_method->method_holder()->class_loader()); |
duke@435 | 678 | { |
duke@435 | 679 | ResourceMark rm(THREAD); |
acorn@4840 | 680 | Symbol* failed_type_symbol = |
duke@435 | 681 | SystemDictionary::check_signature_loaders(method_signature, loader, |
duke@435 | 682 | class_loader, true, CHECK); |
acorn@4840 | 683 | if (failed_type_symbol != NULL) { |
duke@435 | 684 | const char* msg = "loader constraint violation: when resolving " |
duke@435 | 685 | "interface method \"%s\" the class loader (instance of %s) of the " |
duke@435 | 686 | "current class, %s, and the class loader (instance of %s) for " |
acorn@4840 | 687 | "the method's defining class, %s, have different Class objects for the type %s " |
duke@435 | 688 | "used in the signature"; |
hseigel@4278 | 689 | char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature); |
duke@435 | 690 | const char* loader1 = SystemDictionary::loader_name(loader()); |
coleenp@4037 | 691 | char* current = InstanceKlass::cast(current_klass())->name()->as_C_string(); |
duke@435 | 692 | const char* loader2 = SystemDictionary::loader_name(class_loader()); |
acorn@4840 | 693 | char* target = InstanceKlass::cast(resolved_method->method_holder()) |
acorn@4840 | 694 | ->name()->as_C_string(); |
acorn@4840 | 695 | char* failed_type_name = failed_type_symbol->as_C_string(); |
duke@435 | 696 | size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) + |
acorn@4840 | 697 | strlen(current) + strlen(loader2) + strlen(target) + |
acorn@4840 | 698 | strlen(failed_type_name) + 1; |
duke@435 | 699 | char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); |
duke@435 | 700 | jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2, |
acorn@4840 | 701 | target, failed_type_name); |
duke@435 | 702 | THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); |
duke@435 | 703 | } |
duke@435 | 704 | } |
duke@435 | 705 | } |
acorn@5786 | 706 | |
hseigel@6232 | 707 | if (nostatics && resolved_method->is_static()) { |
hseigel@6232 | 708 | ResourceMark rm(THREAD); |
hseigel@6232 | 709 | char buf[200]; |
hseigel@6232 | 710 | jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s", |
hseigel@6232 | 711 | Method::name_and_sig_as_C_string(resolved_klass(), |
hseigel@6232 | 712 | resolved_method->name(), resolved_method->signature())); |
hseigel@6232 | 713 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
hseigel@6232 | 714 | } |
hseigel@6232 | 715 | |
acorn@5786 | 716 | if (TraceItables && Verbose) { |
acorn@5786 | 717 | ResourceMark rm(THREAD); |
acorn@5786 | 718 | tty->print("invokeinterface resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 719 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), |
acorn@5786 | 720 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 721 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 722 | resolved_method->name(), |
acorn@5786 | 723 | resolved_method->signature()), |
acorn@5786 | 724 | resolved_method->method_holder()->internal_name() |
acorn@5786 | 725 | ); |
acorn@5786 | 726 | resolved_method->access_flags().print_on(tty); |
acorn@5848 | 727 | if (resolved_method->is_default_method()) { |
acorn@6080 | 728 | tty->print("default "); |
acorn@5848 | 729 | } |
acorn@5848 | 730 | if (resolved_method->is_overpass()) { |
acorn@5848 | 731 | tty->print("overpass"); |
acorn@5848 | 732 | } |
acorn@5786 | 733 | tty->cr(); |
acorn@5786 | 734 | } |
duke@435 | 735 | } |
duke@435 | 736 | |
duke@435 | 737 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 738 | // Field resolution |
duke@435 | 739 | |
duke@435 | 740 | void LinkResolver::check_field_accessability(KlassHandle ref_klass, |
duke@435 | 741 | KlassHandle resolved_klass, |
duke@435 | 742 | KlassHandle sel_klass, |
duke@435 | 743 | fieldDescriptor& fd, |
duke@435 | 744 | TRAPS) { |
coleenp@4037 | 745 | if (!Reflection::verify_field_access(ref_klass(), |
coleenp@4037 | 746 | resolved_klass(), |
coleenp@4037 | 747 | sel_klass(), |
duke@435 | 748 | fd.access_flags(), |
duke@435 | 749 | true)) { |
duke@435 | 750 | ResourceMark rm(THREAD); |
duke@435 | 751 | Exceptions::fthrow( |
duke@435 | 752 | THREAD_AND_LOCATION, |
coleenp@2497 | 753 | vmSymbols::java_lang_IllegalAccessError(), |
duke@435 | 754 | "tried to access field %s.%s from class %s", |
duke@435 | 755 | sel_klass->external_name(), |
duke@435 | 756 | fd.name()->as_C_string(), |
duke@435 | 757 | ref_klass->external_name() |
duke@435 | 758 | ); |
duke@435 | 759 | return; |
duke@435 | 760 | } |
duke@435 | 761 | } |
duke@435 | 762 | |
drchase@5732 | 763 | void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) { |
drchase@5732 | 764 | // Load these early in case the resolve of the containing klass fails |
drchase@5732 | 765 | Symbol* field = pool->name_ref_at(index); |
drchase@5732 | 766 | Symbol* sig = pool->signature_ref_at(index); |
drchase@5732 | 767 | |
drchase@5732 | 768 | // resolve specified klass |
drchase@5732 | 769 | KlassHandle resolved_klass; |
drchase@5732 | 770 | resolve_klass(resolved_klass, pool, index, CHECK); |
drchase@5732 | 771 | |
drchase@5732 | 772 | KlassHandle current_klass(THREAD, pool->pool_holder()); |
drchase@5732 | 773 | resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK); |
duke@435 | 774 | } |
duke@435 | 775 | |
drchase@5732 | 776 | void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig, |
drchase@5732 | 777 | KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class, |
drchase@5732 | 778 | TRAPS) { |
duke@435 | 779 | assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || |
drchase@5732 | 780 | byte == Bytecodes::_getfield || byte == Bytecodes::_putfield || |
drchase@5732 | 781 | (byte == Bytecodes::_nop && !check_access), "bad field access bytecode"); |
duke@435 | 782 | |
duke@435 | 783 | bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); |
duke@435 | 784 | bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic); |
duke@435 | 785 | |
duke@435 | 786 | // Check if there's a resolved klass containing the field |
drchase@5732 | 787 | if (resolved_klass.is_null()) { |
duke@435 | 788 | ResourceMark rm(THREAD); |
duke@435 | 789 | THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
duke@435 | 790 | } |
duke@435 | 791 | |
duke@435 | 792 | // Resolve instance field |
lfoltan@6818 | 793 | KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd)); |
duke@435 | 794 | // check if field exists; i.e., if a klass containing the field def has been selected |
drchase@5732 | 795 | if (sel_klass.is_null()) { |
duke@435 | 796 | ResourceMark rm(THREAD); |
duke@435 | 797 | THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
duke@435 | 798 | } |
duke@435 | 799 | |
drchase@5732 | 800 | if (!check_access) |
drchase@5732 | 801 | // Access checking may be turned off when calling from within the VM. |
drchase@5732 | 802 | return; |
drchase@5732 | 803 | |
duke@435 | 804 | // check access |
drchase@5732 | 805 | check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); |
duke@435 | 806 | |
duke@435 | 807 | // check for errors |
duke@435 | 808 | if (is_static != fd.is_static()) { |
jcoomes@2856 | 809 | ResourceMark rm(THREAD); |
duke@435 | 810 | char msg[200]; |
hseigel@4278 | 811 | jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string()); |
duke@435 | 812 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); |
duke@435 | 813 | } |
duke@435 | 814 | |
duke@435 | 815 | // Final fields can only be accessed from its own class. |
drchase@5732 | 816 | if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) { |
duke@435 | 817 | THROW(vmSymbols::java_lang_IllegalAccessError()); |
duke@435 | 818 | } |
duke@435 | 819 | |
duke@435 | 820 | // initialize resolved_klass if necessary |
duke@435 | 821 | // note 1: the klass which declared the field must be initialized (i.e, sel_klass) |
duke@435 | 822 | // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) |
duke@435 | 823 | // |
duke@435 | 824 | // note 2: we don't want to force initialization if we are just checking |
duke@435 | 825 | // if the field access is legal; e.g., during compilation |
drchase@5732 | 826 | if (is_static && initialize_class) { |
duke@435 | 827 | sel_klass->initialize(CHECK); |
duke@435 | 828 | } |
duke@435 | 829 | |
drchase@5732 | 830 | if (sel_klass() != current_klass()) { |
duke@435 | 831 | HandleMark hm(THREAD); |
drchase@5732 | 832 | Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader()); |
coleenp@4037 | 833 | Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader()); |
duke@435 | 834 | { |
duke@435 | 835 | ResourceMark rm(THREAD); |
acorn@4840 | 836 | Symbol* failed_type_symbol = |
drchase@5732 | 837 | SystemDictionary::check_signature_loaders(sig, |
duke@435 | 838 | ref_loader, sel_loader, |
duke@435 | 839 | false, |
duke@435 | 840 | CHECK); |
acorn@4840 | 841 | if (failed_type_symbol != NULL) { |
duke@435 | 842 | const char* msg = "loader constraint violation: when resolving field" |
duke@435 | 843 | " \"%s\" the class loader (instance of %s) of the referring class, " |
duke@435 | 844 | "%s, and the class loader (instance of %s) for the field's resolved " |
duke@435 | 845 | "type, %s, have different Class objects for that type"; |
coleenp@2497 | 846 | char* field_name = field->as_C_string(); |
duke@435 | 847 | const char* loader1 = SystemDictionary::loader_name(ref_loader()); |
coleenp@4037 | 848 | char* sel = InstanceKlass::cast(sel_klass())->name()->as_C_string(); |
duke@435 | 849 | const char* loader2 = SystemDictionary::loader_name(sel_loader()); |
acorn@4840 | 850 | char* failed_type_name = failed_type_symbol->as_C_string(); |
duke@435 | 851 | size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) + |
acorn@4840 | 852 | strlen(sel) + strlen(loader2) + strlen(failed_type_name) + 1; |
duke@435 | 853 | char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); |
duke@435 | 854 | jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2, |
duke@435 | 855 | failed_type_name); |
duke@435 | 856 | THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); |
duke@435 | 857 | } |
duke@435 | 858 | } |
duke@435 | 859 | } |
duke@435 | 860 | |
duke@435 | 861 | // return information. note that the klass is set to the actual klass containing the |
duke@435 | 862 | // field, otherwise access of static fields in superclasses will not work. |
duke@435 | 863 | } |
duke@435 | 864 | |
duke@435 | 865 | |
duke@435 | 866 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 867 | // Invoke resolution |
duke@435 | 868 | // |
duke@435 | 869 | // Naming conventions: |
duke@435 | 870 | // |
duke@435 | 871 | // resolved_method the specified method (i.e., static receiver specified via constant pool index) |
duke@435 | 872 | // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class) |
duke@435 | 873 | // resolved_klass the specified klass (i.e., specified via constant pool index) |
duke@435 | 874 | // recv_klass the receiver klass |
duke@435 | 875 | |
duke@435 | 876 | |
coleenp@2497 | 877 | void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name, |
coleenp@2497 | 878 | Symbol* method_signature, KlassHandle current_klass, |
duke@435 | 879 | bool check_access, bool initialize_class, TRAPS) { |
duke@435 | 880 | methodHandle resolved_method; |
duke@435 | 881 | linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
hseigel@4278 | 882 | resolved_klass = KlassHandle(THREAD, resolved_method->method_holder()); |
duke@435 | 883 | |
duke@435 | 884 | // Initialize klass (this should only happen if everything is ok) |
duke@435 | 885 | if (initialize_class && resolved_klass->should_be_initialized()) { |
duke@435 | 886 | resolved_klass->initialize(CHECK); |
duke@435 | 887 | linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
duke@435 | 888 | } |
duke@435 | 889 | |
duke@435 | 890 | // setup result |
duke@435 | 891 | result.set_static(resolved_klass, resolved_method, CHECK); |
duke@435 | 892 | } |
duke@435 | 893 | |
duke@435 | 894 | // throws linktime exceptions |
duke@435 | 895 | void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 896 | Symbol* method_name, Symbol* method_signature, |
duke@435 | 897 | KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 898 | |
acorn@6144 | 899 | if (!resolved_klass->is_interface()) { |
acorn@6144 | 900 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK); |
acorn@6144 | 901 | } else { |
acorn@6144 | 902 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK); |
acorn@6144 | 903 | } |
duke@435 | 904 | assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier"); |
duke@435 | 905 | |
duke@435 | 906 | // check if static |
duke@435 | 907 | if (!resolved_method->is_static()) { |
jcoomes@2856 | 908 | ResourceMark rm(THREAD); |
duke@435 | 909 | char buf[200]; |
hseigel@4278 | 910 | jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 911 | resolved_method->name(), |
duke@435 | 912 | resolved_method->signature())); |
duke@435 | 913 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 914 | } |
duke@435 | 915 | } |
duke@435 | 916 | |
duke@435 | 917 | |
coleenp@8739 | 918 | void LinkResolver::resolve_special_call(CallInfo& result, Handle recv, KlassHandle resolved_klass, Symbol* method_name, |
coleenp@2497 | 919 | Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 920 | methodHandle resolved_method; |
duke@435 | 921 | linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
coleenp@8739 | 922 | runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, recv, check_access, CHECK); |
duke@435 | 923 | } |
duke@435 | 924 | |
duke@435 | 925 | // throws linktime exceptions |
duke@435 | 926 | void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 927 | Symbol* method_name, Symbol* method_signature, |
duke@435 | 928 | KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 929 | |
acorn@5786 | 930 | // Invokespecial is called for multiple special reasons: |
acorn@5786 | 931 | // <init> |
acorn@5786 | 932 | // local private method invocation, for classes and interfaces |
acorn@5786 | 933 | // superclass.method, which can also resolve to a default method |
acorn@5786 | 934 | // and the selected method is recalculated relative to the direct superclass |
acorn@5786 | 935 | // superinterface.method, which explicitly does not check shadowing |
kamg@4245 | 936 | |
acorn@6144 | 937 | if (!resolved_klass->is_interface()) { |
acorn@6144 | 938 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK); |
acorn@6144 | 939 | } else { |
acorn@6144 | 940 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK); |
acorn@6144 | 941 | } |
duke@435 | 942 | |
duke@435 | 943 | // check if method name is <init>, that it is found in same klass as static type |
duke@435 | 944 | if (resolved_method->name() == vmSymbols::object_initializer_name() && |
duke@435 | 945 | resolved_method->method_holder() != resolved_klass()) { |
duke@435 | 946 | ResourceMark rm(THREAD); |
duke@435 | 947 | Exceptions::fthrow( |
duke@435 | 948 | THREAD_AND_LOCATION, |
coleenp@2497 | 949 | vmSymbols::java_lang_NoSuchMethodError(), |
duke@435 | 950 | "%s: method %s%s not found", |
duke@435 | 951 | resolved_klass->external_name(), |
duke@435 | 952 | resolved_method->name()->as_C_string(), |
duke@435 | 953 | resolved_method->signature()->as_C_string() |
duke@435 | 954 | ); |
duke@435 | 955 | return; |
duke@435 | 956 | } |
duke@435 | 957 | |
hseigel@6132 | 958 | // check if invokespecial's interface method reference is in an indirect superinterface |
hseigel@6132 | 959 | if (!current_klass.is_null() && resolved_klass->is_interface()) { |
hseigel@6132 | 960 | Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ? |
hseigel@6132 | 961 | current_klass() : |
hseigel@6132 | 962 | InstanceKlass::cast(current_klass())->host_klass(); |
hseigel@6164 | 963 | // As of the fix for 4486457 we disable verification for all of the |
hseigel@6164 | 964 | // dynamically-generated bytecodes associated with the 1.4 |
hseigel@6164 | 965 | // reflection implementation, not just those associated with |
hseigel@6164 | 966 | // sun/reflect/SerializationConstructorAccessor. |
hseigel@6164 | 967 | bool is_reflect = JDK_Version::is_gte_jdk14x_version() && |
hseigel@6164 | 968 | UseNewReflection && |
hseigel@6164 | 969 | klass_to_check->is_subclass_of( |
hseigel@6164 | 970 | SystemDictionary::reflect_MagicAccessorImpl_klass()); |
hseigel@6132 | 971 | |
hseigel@6164 | 972 | if (!is_reflect && |
hseigel@6164 | 973 | !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) { |
hseigel@6132 | 974 | ResourceMark rm(THREAD); |
hseigel@6132 | 975 | char buf[200]; |
hseigel@6132 | 976 | jio_snprintf(buf, sizeof(buf), |
hseigel@6132 | 977 | "Interface method reference: %s, is in an indirect superinterface of %s", |
hseigel@6132 | 978 | Method::name_and_sig_as_C_string(resolved_klass(), |
hseigel@6132 | 979 | resolved_method->name(), |
hseigel@6132 | 980 | resolved_method->signature()), |
hseigel@6132 | 981 | current_klass->external_name()); |
hseigel@6132 | 982 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
hseigel@6132 | 983 | } |
hseigel@6132 | 984 | } |
hseigel@6132 | 985 | |
duke@435 | 986 | // check if not static |
duke@435 | 987 | if (resolved_method->is_static()) { |
jcoomes@2856 | 988 | ResourceMark rm(THREAD); |
duke@435 | 989 | char buf[200]; |
duke@435 | 990 | jio_snprintf(buf, sizeof(buf), |
duke@435 | 991 | "Expecting non-static method %s", |
hseigel@4278 | 992 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 993 | resolved_method->name(), |
duke@435 | 994 | resolved_method->signature())); |
duke@435 | 995 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 996 | } |
acorn@5848 | 997 | |
acorn@5786 | 998 | if (TraceItables && Verbose) { |
acorn@5786 | 999 | ResourceMark rm(THREAD); |
acorn@5786 | 1000 | tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 1001 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), |
acorn@5786 | 1002 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1003 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1004 | resolved_method->name(), |
acorn@5786 | 1005 | resolved_method->signature()), |
acorn@5786 | 1006 | resolved_method->method_holder()->internal_name() |
acorn@5786 | 1007 | ); |
acorn@5786 | 1008 | resolved_method->access_flags().print_on(tty); |
acorn@5848 | 1009 | if (resolved_method->is_default_method()) { |
acorn@6080 | 1010 | tty->print("default "); |
acorn@5786 | 1011 | } |
acorn@5786 | 1012 | if (resolved_method->is_overpass()) { |
acorn@5786 | 1013 | tty->print("overpass"); |
acorn@5786 | 1014 | } |
acorn@5786 | 1015 | tty->cr(); |
acorn@5786 | 1016 | } |
duke@435 | 1017 | } |
duke@435 | 1018 | |
duke@435 | 1019 | // throws runtime exceptions |
duke@435 | 1020 | void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, |
coleenp@8739 | 1021 | KlassHandle current_klass, Handle recv, bool check_access, TRAPS) { |
duke@435 | 1022 | |
duke@435 | 1023 | // resolved method is selected method unless we have an old-style lookup |
acorn@5786 | 1024 | // for a superclass method |
acorn@5786 | 1025 | // Invokespecial for a superinterface, resolved method is selected method, |
acorn@5786 | 1026 | // no checks for shadowing |
duke@435 | 1027 | methodHandle sel_method(THREAD, resolved_method()); |
duke@435 | 1028 | |
coleenp@8739 | 1029 | if (check_access && |
coleenp@8739 | 1030 | // check if the method is not <init> |
coleenp@8739 | 1031 | resolved_method->name() != vmSymbols::object_initializer_name()) { |
coleenp@8739 | 1032 | |
duke@435 | 1033 | // check if this is an old-style super call and do a new lookup if so |
duke@435 | 1034 | // a) check if ACC_SUPER flag is set for the current class |
coleenp@8739 | 1035 | if ((current_klass->is_super() || !AllowNonVirtualCalls) && |
acorn@5786 | 1036 | // b) check if the class of the resolved_klass is a superclass |
acorn@5786 | 1037 | // (not supertype in order to exclude interface classes) of the current class. |
acorn@5786 | 1038 | // This check is not performed for super.invoke for interface methods |
acorn@5786 | 1039 | // in super interfaces. |
acorn@5786 | 1040 | current_klass->is_subclass_of(resolved_klass()) && |
coleenp@8739 | 1041 | current_klass() != resolved_klass()) { |
duke@435 | 1042 | // Lookup super method |
duke@435 | 1043 | KlassHandle super_klass(THREAD, current_klass->super()); |
duke@435 | 1044 | lookup_instance_method_in_klasses(sel_method, super_klass, |
coleenp@2497 | 1045 | resolved_method->name(), |
coleenp@2497 | 1046 | resolved_method->signature(), CHECK); |
duke@435 | 1047 | // check if found |
duke@435 | 1048 | if (sel_method.is_null()) { |
duke@435 | 1049 | ResourceMark rm(THREAD); |
duke@435 | 1050 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1051 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1052 | resolved_method->name(), |
duke@435 | 1053 | resolved_method->signature())); |
duke@435 | 1054 | } |
duke@435 | 1055 | } |
coleenp@8739 | 1056 | |
coleenp@8739 | 1057 | // Check that the class of objectref (the receiver) is the current class or interface, |
coleenp@8739 | 1058 | // or a subtype of the current class or interface (the sender), otherwise invokespecial |
coleenp@8739 | 1059 | // throws IllegalAccessError. |
coleenp@8739 | 1060 | // The verifier checks that the sender is a subtype of the class in the I/MR operand. |
coleenp@8739 | 1061 | // The verifier also checks that the receiver is a subtype of the sender, if the sender is |
coleenp@8739 | 1062 | // a class. If the sender is an interface, the check has to be performed at runtime. |
coleenp@8739 | 1063 | InstanceKlass* sender = InstanceKlass::cast(current_klass()); |
coleenp@8739 | 1064 | sender = sender->is_anonymous() ? InstanceKlass::cast(sender->host_klass()) : sender; |
coleenp@8739 | 1065 | if (sender->is_interface() && recv.not_null()) { |
coleenp@8739 | 1066 | Klass* receiver_klass = recv->klass(); |
coleenp@8739 | 1067 | if (!receiver_klass->is_subtype_of(sender)) { |
coleenp@8739 | 1068 | ResourceMark rm(THREAD); |
coleenp@8739 | 1069 | char buf[500]; |
coleenp@8739 | 1070 | jio_snprintf(buf, sizeof(buf), |
coleenp@8739 | 1071 | "Receiver class %s must be the current class or a subtype of interface %s", |
coleenp@8739 | 1072 | receiver_klass->name()->as_C_string(), |
coleenp@8739 | 1073 | sender->name()->as_C_string()); |
coleenp@8739 | 1074 | THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf); |
coleenp@8739 | 1075 | } |
coleenp@8739 | 1076 | } |
duke@435 | 1077 | } |
duke@435 | 1078 | |
duke@435 | 1079 | // check if not static |
duke@435 | 1080 | if (sel_method->is_static()) { |
jcoomes@2856 | 1081 | ResourceMark rm(THREAD); |
duke@435 | 1082 | char buf[200]; |
hseigel@4278 | 1083 | jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1084 | resolved_method->name(), |
duke@435 | 1085 | resolved_method->signature())); |
duke@435 | 1086 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 1087 | } |
duke@435 | 1088 | |
duke@435 | 1089 | // check if abstract |
duke@435 | 1090 | if (sel_method->is_abstract()) { |
duke@435 | 1091 | ResourceMark rm(THREAD); |
duke@435 | 1092 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1093 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1094 | sel_method->name(), |
duke@435 | 1095 | sel_method->signature())); |
duke@435 | 1096 | } |
duke@435 | 1097 | |
acorn@5786 | 1098 | if (TraceItables && Verbose) { |
acorn@5786 | 1099 | ResourceMark rm(THREAD); |
acorn@5786 | 1100 | tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 1101 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1102 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1103 | sel_method->name(), |
acorn@5786 | 1104 | sel_method->signature()), |
acorn@5786 | 1105 | sel_method->method_holder()->internal_name() |
acorn@5786 | 1106 | ); |
acorn@5786 | 1107 | sel_method->access_flags().print_on(tty); |
acorn@5848 | 1108 | if (sel_method->is_default_method()) { |
acorn@6080 | 1109 | tty->print("default "); |
acorn@5786 | 1110 | } |
acorn@5848 | 1111 | if (sel_method->is_overpass()) { |
acorn@5848 | 1112 | tty->print("overpass"); |
acorn@5848 | 1113 | } |
acorn@5786 | 1114 | tty->cr(); |
acorn@5786 | 1115 | } |
acorn@5786 | 1116 | |
duke@435 | 1117 | // setup result |
duke@435 | 1118 | result.set_static(resolved_klass, sel_method, CHECK); |
duke@435 | 1119 | } |
duke@435 | 1120 | |
duke@435 | 1121 | void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass, |
coleenp@2497 | 1122 | Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, |
duke@435 | 1123 | bool check_access, bool check_null_and_abstract, TRAPS) { |
duke@435 | 1124 | methodHandle resolved_method; |
duke@435 | 1125 | linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
duke@435 | 1126 | runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK); |
duke@435 | 1127 | } |
duke@435 | 1128 | |
duke@435 | 1129 | // throws linktime exceptions |
duke@435 | 1130 | void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass, |
coleenp@2497 | 1131 | Symbol* method_name, Symbol* method_signature, |
duke@435 | 1132 | KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 1133 | // normal method resolution |
acorn@5897 | 1134 | resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK); |
duke@435 | 1135 | |
duke@435 | 1136 | assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); |
duke@435 | 1137 | assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); |
duke@435 | 1138 | |
acorn@5786 | 1139 | // check if private interface method |
acorn@5786 | 1140 | if (resolved_klass->is_interface() && resolved_method->is_private()) { |
acorn@5786 | 1141 | ResourceMark rm(THREAD); |
acorn@5786 | 1142 | char buf[200]; |
acorn@5786 | 1143 | jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s", |
acorn@5786 | 1144 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1145 | resolved_method->name(), |
acorn@5786 | 1146 | resolved_method->signature()), |
acorn@5786 | 1147 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name())); |
acorn@5786 | 1148 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
acorn@5786 | 1149 | } |
acorn@5786 | 1150 | |
duke@435 | 1151 | // check if not static |
duke@435 | 1152 | if (resolved_method->is_static()) { |
jcoomes@2856 | 1153 | ResourceMark rm(THREAD); |
duke@435 | 1154 | char buf[200]; |
hseigel@4278 | 1155 | jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1156 | resolved_method->name(), |
duke@435 | 1157 | resolved_method->signature())); |
duke@435 | 1158 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 1159 | } |
acorn@5786 | 1160 | |
acorn@5848 | 1161 | if (PrintVtables && Verbose) { |
acorn@5848 | 1162 | ResourceMark rm(THREAD); |
acorn@5848 | 1163 | tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5848 | 1164 | (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()), |
acorn@5848 | 1165 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5848 | 1166 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5848 | 1167 | resolved_method->name(), |
acorn@5848 | 1168 | resolved_method->signature()), |
acorn@5848 | 1169 | resolved_method->method_holder()->internal_name() |
acorn@5848 | 1170 | ); |
acorn@5848 | 1171 | resolved_method->access_flags().print_on(tty); |
acorn@5848 | 1172 | if (resolved_method->is_default_method()) { |
acorn@6080 | 1173 | tty->print("default "); |
acorn@5848 | 1174 | } |
acorn@5848 | 1175 | if (resolved_method->is_overpass()) { |
acorn@5848 | 1176 | tty->print("overpass"); |
acorn@5848 | 1177 | } |
acorn@5848 | 1178 | tty->cr(); |
acorn@5848 | 1179 | } |
duke@435 | 1180 | } |
duke@435 | 1181 | |
duke@435 | 1182 | // throws runtime exceptions |
duke@435 | 1183 | void LinkResolver::runtime_resolve_virtual_method(CallInfo& result, |
duke@435 | 1184 | methodHandle resolved_method, |
duke@435 | 1185 | KlassHandle resolved_klass, |
duke@435 | 1186 | Handle recv, |
duke@435 | 1187 | KlassHandle recv_klass, |
duke@435 | 1188 | bool check_null_and_abstract, |
duke@435 | 1189 | TRAPS) { |
duke@435 | 1190 | |
duke@435 | 1191 | // setup default return values |
coleenp@4037 | 1192 | int vtable_index = Method::invalid_vtable_index; |
duke@435 | 1193 | methodHandle selected_method; |
duke@435 | 1194 | |
duke@435 | 1195 | assert(recv.is_null() || recv->is_oop(), "receiver is not an oop"); |
duke@435 | 1196 | |
duke@435 | 1197 | // runtime method resolution |
duke@435 | 1198 | if (check_null_and_abstract && recv.is_null()) { // check if receiver exists |
duke@435 | 1199 | THROW(vmSymbols::java_lang_NullPointerException()); |
duke@435 | 1200 | } |
duke@435 | 1201 | |
coleenp@4037 | 1202 | // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s |
duke@435 | 1203 | // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since |
duke@435 | 1204 | // a missing receiver might result in a bogus lookup. |
coleenp@4251 | 1205 | assert(resolved_method->method_holder()->is_linked(), "must be linked"); |
duke@435 | 1206 | |
duke@435 | 1207 | // do lookup based on receiver klass using the vtable index |
dbuck@8716 | 1208 | if (resolved_method->method_holder()->is_interface()) { // default or miranda method |
acorn@5848 | 1209 | vtable_index = vtable_index_of_interface_method(resolved_klass, |
acorn@6080 | 1210 | resolved_method); |
duke@435 | 1211 | assert(vtable_index >= 0 , "we should have valid vtable index at this point"); |
duke@435 | 1212 | |
coleenp@4037 | 1213 | InstanceKlass* inst = InstanceKlass::cast(recv_klass()); |
duke@435 | 1214 | selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); |
duke@435 | 1215 | } else { |
duke@435 | 1216 | // at this point we are sure that resolved_method is virtual and not |
dbuck@8716 | 1217 | // a default or miranda method; therefore, it must have a valid vtable index. |
drchase@5732 | 1218 | assert(!resolved_method->has_itable_index(), ""); |
duke@435 | 1219 | vtable_index = resolved_method->vtable_index(); |
duke@435 | 1220 | // We could get a negative vtable_index for final methods, |
duke@435 | 1221 | // because as an optimization they are they are never put in the vtable, |
duke@435 | 1222 | // unless they override an existing method. |
duke@435 | 1223 | // If we do get a negative, it means the resolved method is the the selected |
duke@435 | 1224 | // method, and it can never be changed by an override. |
coleenp@4037 | 1225 | if (vtable_index == Method::nonvirtual_vtable_index) { |
duke@435 | 1226 | assert(resolved_method->can_be_statically_bound(), "cannot override this method"); |
duke@435 | 1227 | selected_method = resolved_method; |
duke@435 | 1228 | } else { |
duke@435 | 1229 | // recv_klass might be an arrayKlassOop but all vtables start at |
duke@435 | 1230 | // the same place. The cast is to avoid virtual call and assertion. |
coleenp@4037 | 1231 | InstanceKlass* inst = (InstanceKlass*)recv_klass(); |
duke@435 | 1232 | selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); |
duke@435 | 1233 | } |
duke@435 | 1234 | } |
duke@435 | 1235 | |
duke@435 | 1236 | // check if method exists |
duke@435 | 1237 | if (selected_method.is_null()) { |
duke@435 | 1238 | ResourceMark rm(THREAD); |
duke@435 | 1239 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1240 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1241 | resolved_method->name(), |
duke@435 | 1242 | resolved_method->signature())); |
duke@435 | 1243 | } |
duke@435 | 1244 | |
duke@435 | 1245 | // check if abstract |
duke@435 | 1246 | if (check_null_and_abstract && selected_method->is_abstract()) { |
duke@435 | 1247 | ResourceMark rm(THREAD); |
duke@435 | 1248 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1249 | Method::name_and_sig_as_C_string(resolved_klass(), |
duke@435 | 1250 | selected_method->name(), |
duke@435 | 1251 | selected_method->signature())); |
duke@435 | 1252 | } |
duke@435 | 1253 | |
acorn@5786 | 1254 | if (PrintVtables && Verbose) { |
acorn@5786 | 1255 | ResourceMark rm(THREAD); |
acorn@5786 | 1256 | tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ", |
acorn@5786 | 1257 | (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()), |
acorn@5786 | 1258 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1259 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1260 | resolved_method->name(), |
acorn@5786 | 1261 | resolved_method->signature()), |
acorn@5786 | 1262 | selected_method->method_holder()->internal_name(), |
acorn@5786 | 1263 | vtable_index |
acorn@5786 | 1264 | ); |
acorn@5786 | 1265 | selected_method->access_flags().print_on(tty); |
acorn@5848 | 1266 | if (selected_method->is_default_method()) { |
acorn@6080 | 1267 | tty->print("default "); |
acorn@5786 | 1268 | } |
acorn@5848 | 1269 | if (selected_method->is_overpass()) { |
acorn@5786 | 1270 | tty->print("overpass"); |
acorn@5786 | 1271 | } |
acorn@5786 | 1272 | tty->cr(); |
acorn@5786 | 1273 | } |
duke@435 | 1274 | // setup result |
duke@435 | 1275 | result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK); |
duke@435 | 1276 | } |
duke@435 | 1277 | |
duke@435 | 1278 | void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass, |
coleenp@2497 | 1279 | Symbol* method_name, Symbol* method_signature, KlassHandle current_klass, |
duke@435 | 1280 | bool check_access, bool check_null_and_abstract, TRAPS) { |
duke@435 | 1281 | methodHandle resolved_method; |
duke@435 | 1282 | linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK); |
duke@435 | 1283 | runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK); |
duke@435 | 1284 | } |
duke@435 | 1285 | |
duke@435 | 1286 | // throws linktime exceptions |
coleenp@2497 | 1287 | void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name, |
coleenp@2497 | 1288 | Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) { |
duke@435 | 1289 | // normal interface method resolution |
acorn@6144 | 1290 | resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK); |
duke@435 | 1291 | |
duke@435 | 1292 | assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); |
duke@435 | 1293 | assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); |
duke@435 | 1294 | } |
duke@435 | 1295 | |
duke@435 | 1296 | // throws runtime exceptions |
duke@435 | 1297 | void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass, |
duke@435 | 1298 | Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) { |
duke@435 | 1299 | // check if receiver exists |
duke@435 | 1300 | if (check_null_and_abstract && recv.is_null()) { |
duke@435 | 1301 | THROW(vmSymbols::java_lang_NullPointerException()); |
duke@435 | 1302 | } |
duke@435 | 1303 | |
acorn@5786 | 1304 | // check if private interface method |
acorn@5786 | 1305 | if (resolved_klass->is_interface() && resolved_method->is_private()) { |
acorn@5786 | 1306 | ResourceMark rm(THREAD); |
acorn@5786 | 1307 | char buf[200]; |
acorn@5786 | 1308 | jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s", |
acorn@5786 | 1309 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1310 | resolved_method->name(), |
acorn@5786 | 1311 | resolved_method->signature())); |
acorn@5786 | 1312 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
acorn@5786 | 1313 | } |
acorn@5786 | 1314 | |
duke@435 | 1315 | // check if receiver klass implements the resolved interface |
duke@435 | 1316 | if (!recv_klass->is_subtype_of(resolved_klass())) { |
jcoomes@2856 | 1317 | ResourceMark rm(THREAD); |
duke@435 | 1318 | char buf[200]; |
duke@435 | 1319 | jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s", |
hseigel@4278 | 1320 | recv_klass()->external_name(), |
hseigel@4278 | 1321 | resolved_klass()->external_name()); |
duke@435 | 1322 | THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); |
duke@435 | 1323 | } |
hseigel@6195 | 1324 | |
duke@435 | 1325 | // do lookup based on receiver klass |
duke@435 | 1326 | methodHandle sel_method; |
hseigel@6195 | 1327 | // This search must match the linktime preparation search for itable initialization |
hseigel@6195 | 1328 | // to correctly enforce loader constraints for interface method inheritance |
duke@435 | 1329 | lookup_instance_method_in_klasses(sel_method, recv_klass, |
coleenp@2497 | 1330 | resolved_method->name(), |
coleenp@2497 | 1331 | resolved_method->signature(), CHECK); |
drchase@5732 | 1332 | if (sel_method.is_null() && !check_null_and_abstract) { |
drchase@5732 | 1333 | // In theory this is a harmless placeholder value, but |
drchase@5732 | 1334 | // in practice leaving in null affects the nsk default method tests. |
drchase@5732 | 1335 | // This needs further study. |
drchase@5732 | 1336 | sel_method = resolved_method; |
drchase@5732 | 1337 | } |
duke@435 | 1338 | // check if method exists |
duke@435 | 1339 | if (sel_method.is_null()) { |
duke@435 | 1340 | ResourceMark rm(THREAD); |
duke@435 | 1341 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1342 | Method::name_and_sig_as_C_string(recv_klass(), |
duke@435 | 1343 | resolved_method->name(), |
duke@435 | 1344 | resolved_method->signature())); |
duke@435 | 1345 | } |
bharadwaj@4960 | 1346 | // check access |
acorn@5786 | 1347 | // Throw Illegal Access Error if sel_method is not public. |
acorn@5786 | 1348 | if (!sel_method->is_public()) { |
acorn@5786 | 1349 | ResourceMark rm(THREAD); |
acorn@5786 | 1350 | THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), |
acorn@5786 | 1351 | Method::name_and_sig_as_C_string(recv_klass(), |
acorn@5786 | 1352 | sel_method->name(), |
acorn@5786 | 1353 | sel_method->signature())); |
bharadwaj@4960 | 1354 | } |
duke@435 | 1355 | // check if abstract |
duke@435 | 1356 | if (check_null_and_abstract && sel_method->is_abstract()) { |
duke@435 | 1357 | ResourceMark rm(THREAD); |
duke@435 | 1358 | THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), |
hseigel@4278 | 1359 | Method::name_and_sig_as_C_string(recv_klass(), |
duke@435 | 1360 | sel_method->name(), |
duke@435 | 1361 | sel_method->signature())); |
duke@435 | 1362 | } |
acorn@5786 | 1363 | |
acorn@5786 | 1364 | if (TraceItables && Verbose) { |
acorn@5786 | 1365 | ResourceMark rm(THREAD); |
acorn@5786 | 1366 | tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ", |
acorn@5786 | 1367 | (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()), |
acorn@5786 | 1368 | (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), |
acorn@5786 | 1369 | Method::name_and_sig_as_C_string(resolved_klass(), |
acorn@5786 | 1370 | resolved_method->name(), |
acorn@5786 | 1371 | resolved_method->signature()), |
acorn@5786 | 1372 | sel_method->method_holder()->internal_name() |
acorn@5786 | 1373 | ); |
acorn@5786 | 1374 | sel_method->access_flags().print_on(tty); |
acorn@5848 | 1375 | if (sel_method->is_default_method()) { |
acorn@6080 | 1376 | tty->print("default "); |
acorn@5786 | 1377 | } |
acorn@5848 | 1378 | if (sel_method->is_overpass()) { |
acorn@5786 | 1379 | tty->print("overpass"); |
acorn@5786 | 1380 | } |
acorn@5786 | 1381 | tty->cr(); |
acorn@5786 | 1382 | } |
acorn@6080 | 1383 | // setup result |
acorn@6080 | 1384 | if (!resolved_method->has_itable_index()) { |
acorn@6080 | 1385 | int vtable_index = resolved_method->vtable_index(); |
acorn@6080 | 1386 | assert(vtable_index == sel_method->vtable_index(), "sanity check"); |
acorn@6080 | 1387 | result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK); |
acorn@6080 | 1388 | } else { |
acorn@6080 | 1389 | int itable_index = resolved_method()->itable_index(); |
acorn@6080 | 1390 | result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK); |
acorn@6080 | 1391 | } |
duke@435 | 1392 | } |
duke@435 | 1393 | |
duke@435 | 1394 | |
duke@435 | 1395 | methodHandle LinkResolver::linktime_resolve_interface_method_or_null( |
duke@435 | 1396 | KlassHandle resolved_klass, |
coleenp@2497 | 1397 | Symbol* method_name, |
coleenp@2497 | 1398 | Symbol* method_signature, |
duke@435 | 1399 | KlassHandle current_klass, |
duke@435 | 1400 | bool check_access) { |
duke@435 | 1401 | EXCEPTION_MARK; |
duke@435 | 1402 | methodHandle method_result; |
duke@435 | 1403 | linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD); |
duke@435 | 1404 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1405 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1406 | return methodHandle(); |
duke@435 | 1407 | } else { |
duke@435 | 1408 | return method_result; |
duke@435 | 1409 | } |
duke@435 | 1410 | } |
duke@435 | 1411 | |
duke@435 | 1412 | methodHandle LinkResolver::linktime_resolve_virtual_method_or_null( |
duke@435 | 1413 | KlassHandle resolved_klass, |
coleenp@2497 | 1414 | Symbol* method_name, |
coleenp@2497 | 1415 | Symbol* method_signature, |
duke@435 | 1416 | KlassHandle current_klass, |
duke@435 | 1417 | bool check_access) { |
duke@435 | 1418 | EXCEPTION_MARK; |
duke@435 | 1419 | methodHandle method_result; |
duke@435 | 1420 | linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD); |
duke@435 | 1421 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1422 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1423 | return methodHandle(); |
duke@435 | 1424 | } else { |
duke@435 | 1425 | return method_result; |
duke@435 | 1426 | } |
duke@435 | 1427 | } |
duke@435 | 1428 | |
duke@435 | 1429 | methodHandle LinkResolver::resolve_virtual_call_or_null( |
duke@435 | 1430 | KlassHandle receiver_klass, |
duke@435 | 1431 | KlassHandle resolved_klass, |
coleenp@2497 | 1432 | Symbol* name, |
coleenp@2497 | 1433 | Symbol* signature, |
vlivanov@7792 | 1434 | KlassHandle current_klass, |
vlivanov@7792 | 1435 | bool check_access) { |
duke@435 | 1436 | EXCEPTION_MARK; |
duke@435 | 1437 | CallInfo info; |
vlivanov@7792 | 1438 | resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD); |
duke@435 | 1439 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1440 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1441 | return methodHandle(); |
duke@435 | 1442 | } |
duke@435 | 1443 | return info.selected_method(); |
duke@435 | 1444 | } |
duke@435 | 1445 | |
duke@435 | 1446 | methodHandle LinkResolver::resolve_interface_call_or_null( |
duke@435 | 1447 | KlassHandle receiver_klass, |
duke@435 | 1448 | KlassHandle resolved_klass, |
coleenp@2497 | 1449 | Symbol* name, |
coleenp@2497 | 1450 | Symbol* signature, |
vlivanov@7792 | 1451 | KlassHandle current_klass, |
vlivanov@7792 | 1452 | bool check_access) { |
duke@435 | 1453 | EXCEPTION_MARK; |
duke@435 | 1454 | CallInfo info; |
vlivanov@7792 | 1455 | resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD); |
duke@435 | 1456 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1457 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1458 | return methodHandle(); |
duke@435 | 1459 | } |
duke@435 | 1460 | return info.selected_method(); |
duke@435 | 1461 | } |
duke@435 | 1462 | |
duke@435 | 1463 | int LinkResolver::resolve_virtual_vtable_index( |
duke@435 | 1464 | KlassHandle receiver_klass, |
duke@435 | 1465 | KlassHandle resolved_klass, |
coleenp@2497 | 1466 | Symbol* name, |
coleenp@2497 | 1467 | Symbol* signature, |
duke@435 | 1468 | KlassHandle current_klass) { |
duke@435 | 1469 | EXCEPTION_MARK; |
duke@435 | 1470 | CallInfo info; |
duke@435 | 1471 | resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD); |
duke@435 | 1472 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1473 | CLEAR_PENDING_EXCEPTION; |
coleenp@4037 | 1474 | return Method::invalid_vtable_index; |
duke@435 | 1475 | } |
duke@435 | 1476 | return info.vtable_index(); |
duke@435 | 1477 | } |
duke@435 | 1478 | |
duke@435 | 1479 | methodHandle LinkResolver::resolve_static_call_or_null( |
duke@435 | 1480 | KlassHandle resolved_klass, |
coleenp@2497 | 1481 | Symbol* name, |
coleenp@2497 | 1482 | Symbol* signature, |
vlivanov@7792 | 1483 | KlassHandle current_klass, |
vlivanov@7792 | 1484 | bool check_access) { |
duke@435 | 1485 | EXCEPTION_MARK; |
duke@435 | 1486 | CallInfo info; |
vlivanov@7792 | 1487 | resolve_static_call(info, resolved_klass, name, signature, current_klass, check_access, false, THREAD); |
duke@435 | 1488 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1489 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1490 | return methodHandle(); |
duke@435 | 1491 | } |
duke@435 | 1492 | return info.selected_method(); |
duke@435 | 1493 | } |
duke@435 | 1494 | |
vlivanov@7792 | 1495 | methodHandle LinkResolver::resolve_special_call_or_null( |
vlivanov@7792 | 1496 | KlassHandle resolved_klass, |
vlivanov@7792 | 1497 | Symbol* name, |
vlivanov@7792 | 1498 | Symbol* signature, |
vlivanov@7792 | 1499 | KlassHandle current_klass, |
vlivanov@7792 | 1500 | bool check_access) { |
duke@435 | 1501 | EXCEPTION_MARK; |
duke@435 | 1502 | CallInfo info; |
coleenp@8739 | 1503 | resolve_special_call(info, Handle(), resolved_klass, name, signature, current_klass, check_access, THREAD); |
duke@435 | 1504 | if (HAS_PENDING_EXCEPTION) { |
duke@435 | 1505 | CLEAR_PENDING_EXCEPTION; |
duke@435 | 1506 | return methodHandle(); |
duke@435 | 1507 | } |
duke@435 | 1508 | return info.selected_method(); |
duke@435 | 1509 | } |
duke@435 | 1510 | |
duke@435 | 1511 | |
duke@435 | 1512 | |
duke@435 | 1513 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 1514 | // ConstantPool entries |
duke@435 | 1515 | |
duke@435 | 1516 | void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) { |
duke@435 | 1517 | switch (byte) { |
duke@435 | 1518 | case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break; |
coleenp@8739 | 1519 | case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break; |
duke@435 | 1520 | case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break; |
twisti@3969 | 1521 | case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break; |
jrose@1161 | 1522 | case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break; |
duke@435 | 1523 | case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break; |
duke@435 | 1524 | } |
duke@435 | 1525 | return; |
duke@435 | 1526 | } |
duke@435 | 1527 | |
coleenp@2497 | 1528 | void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature, |
duke@435 | 1529 | KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1530 | // resolve klass |
duke@435 | 1531 | resolve_klass(resolved_klass, pool, index, CHECK); |
duke@435 | 1532 | |
duke@435 | 1533 | // Get name, signature, and static klass |
coleenp@2497 | 1534 | method_name = pool->name_ref_at(index); |
coleenp@2497 | 1535 | method_signature = pool->signature_ref_at(index); |
duke@435 | 1536 | current_klass = KlassHandle(THREAD, pool->pool_holder()); |
duke@435 | 1537 | } |
duke@435 | 1538 | |
duke@435 | 1539 | |
duke@435 | 1540 | void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1541 | KlassHandle resolved_klass; |
coleenp@2497 | 1542 | Symbol* method_name = NULL; |
coleenp@2497 | 1543 | Symbol* method_signature = NULL; |
duke@435 | 1544 | KlassHandle current_klass; |
duke@435 | 1545 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
duke@435 | 1546 | resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
duke@435 | 1547 | } |
duke@435 | 1548 | |
duke@435 | 1549 | |
coleenp@8739 | 1550 | void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1551 | KlassHandle resolved_klass; |
coleenp@2497 | 1552 | Symbol* method_name = NULL; |
coleenp@2497 | 1553 | Symbol* method_signature = NULL; |
duke@435 | 1554 | KlassHandle current_klass; |
duke@435 | 1555 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
coleenp@8739 | 1556 | resolve_special_call(result, recv, resolved_klass, method_name, method_signature, current_klass, true, CHECK); |
duke@435 | 1557 | } |
duke@435 | 1558 | |
duke@435 | 1559 | |
duke@435 | 1560 | void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv, |
duke@435 | 1561 | constantPoolHandle pool, int index, |
duke@435 | 1562 | TRAPS) { |
duke@435 | 1563 | |
duke@435 | 1564 | KlassHandle resolved_klass; |
coleenp@2497 | 1565 | Symbol* method_name = NULL; |
coleenp@2497 | 1566 | Symbol* method_signature = NULL; |
duke@435 | 1567 | KlassHandle current_klass; |
duke@435 | 1568 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
coleenp@4037 | 1569 | KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); |
duke@435 | 1570 | resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
duke@435 | 1571 | } |
duke@435 | 1572 | |
duke@435 | 1573 | |
duke@435 | 1574 | void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) { |
duke@435 | 1575 | KlassHandle resolved_klass; |
coleenp@2497 | 1576 | Symbol* method_name = NULL; |
coleenp@2497 | 1577 | Symbol* method_signature = NULL; |
duke@435 | 1578 | KlassHandle current_klass; |
duke@435 | 1579 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
coleenp@4037 | 1580 | KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); |
duke@435 | 1581 | resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK); |
duke@435 | 1582 | } |
duke@435 | 1583 | |
jrose@1161 | 1584 | |
twisti@3969 | 1585 | void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) { |
jrose@1161 | 1586 | assert(EnableInvokeDynamic, ""); |
twisti@3969 | 1587 | // This guy is reached from InterpreterRuntime::resolve_invokehandle. |
twisti@3969 | 1588 | KlassHandle resolved_klass; |
twisti@3969 | 1589 | Symbol* method_name = NULL; |
twisti@3969 | 1590 | Symbol* method_signature = NULL; |
twisti@3969 | 1591 | KlassHandle current_klass; |
twisti@3969 | 1592 | resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK); |
kmo@4910 | 1593 | if (TraceMethodHandles) { |
kmo@4910 | 1594 | ResourceMark rm(THREAD); |
twisti@3969 | 1595 | tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string()); |
kmo@4910 | 1596 | } |
twisti@3969 | 1597 | resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK); |
twisti@3969 | 1598 | } |
jrose@1161 | 1599 | |
twisti@3969 | 1600 | void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass, |
twisti@3969 | 1601 | Symbol* method_name, Symbol* method_signature, |
twisti@3969 | 1602 | KlassHandle current_klass, |
twisti@3969 | 1603 | TRAPS) { |
twisti@3969 | 1604 | // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar |
twisti@3969 | 1605 | assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), ""); |
twisti@3969 | 1606 | assert(MethodHandles::is_signature_polymorphic_name(method_name), ""); |
twisti@3969 | 1607 | methodHandle resolved_method; |
twisti@4133 | 1608 | Handle resolved_appendix; |
twisti@4133 | 1609 | Handle resolved_method_type; |
twisti@3969 | 1610 | lookup_polymorphic_method(resolved_method, resolved_klass, |
twisti@3969 | 1611 | method_name, method_signature, |
twisti@4133 | 1612 | current_klass, &resolved_appendix, &resolved_method_type, CHECK); |
twisti@4133 | 1613 | result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK); |
twisti@3969 | 1614 | } |
jrose@1161 | 1615 | |
aeriksso@7865 | 1616 | static void wrap_invokedynamic_exception(TRAPS) { |
aeriksso@7865 | 1617 | if (HAS_PENDING_EXCEPTION) { |
aeriksso@7865 | 1618 | if (TraceMethodHandles) { |
aeriksso@7865 | 1619 | tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION)); |
aeriksso@7865 | 1620 | PENDING_EXCEPTION->print(); |
aeriksso@7865 | 1621 | } |
aeriksso@7865 | 1622 | if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) { |
aeriksso@7865 | 1623 | // throw these guys, since they are already wrapped |
aeriksso@7865 | 1624 | return; |
aeriksso@7865 | 1625 | } |
aeriksso@7865 | 1626 | if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { |
aeriksso@7865 | 1627 | // intercept only LinkageErrors which might have failed to wrap |
aeriksso@7865 | 1628 | return; |
aeriksso@7865 | 1629 | } |
aeriksso@7865 | 1630 | // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS. |
aeriksso@7865 | 1631 | Handle nested_exception(THREAD, PENDING_EXCEPTION); |
aeriksso@7865 | 1632 | CLEAR_PENDING_EXCEPTION; |
aeriksso@7865 | 1633 | THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) |
aeriksso@7865 | 1634 | } |
aeriksso@7865 | 1635 | } |
jrose@1161 | 1636 | |
twisti@3969 | 1637 | void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) { |
twisti@3969 | 1638 | assert(EnableInvokeDynamic, ""); |
twisti@3969 | 1639 | |
twisti@4133 | 1640 | //resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK); |
twisti@3969 | 1641 | Symbol* method_name = pool->name_ref_at(index); |
twisti@3969 | 1642 | Symbol* method_signature = pool->signature_ref_at(index); |
twisti@3969 | 1643 | KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder()); |
twisti@3969 | 1644 | |
twisti@3969 | 1645 | // Resolve the bootstrap specifier (BSM + optional arguments). |
twisti@3969 | 1646 | Handle bootstrap_specifier; |
twisti@3969 | 1647 | // Check if CallSite has been bound already: |
coleenp@4037 | 1648 | ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index); |
twisti@3969 | 1649 | if (cpce->is_f1_null()) { |
coleenp@4037 | 1650 | int pool_index = cpce->constant_pool_index(); |
aeriksso@7865 | 1651 | oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD); |
aeriksso@7865 | 1652 | wrap_invokedynamic_exception(CHECK); |
twisti@3969 | 1653 | assert(bsm_info != NULL, ""); |
twisti@3969 | 1654 | // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic. |
twisti@3969 | 1655 | bootstrap_specifier = Handle(THREAD, bsm_info); |
twisti@3969 | 1656 | } |
twisti@3969 | 1657 | if (!cpce->is_f1_null()) { |
twisti@4133 | 1658 | methodHandle method( THREAD, cpce->f1_as_method()); |
twisti@4133 | 1659 | Handle appendix( THREAD, cpce->appendix_if_resolved(pool)); |
twisti@4133 | 1660 | Handle method_type(THREAD, cpce->method_type_if_resolved(pool)); |
aeriksso@7865 | 1661 | result.set_handle(method, appendix, method_type, THREAD); |
aeriksso@7865 | 1662 | wrap_invokedynamic_exception(CHECK); |
twisti@3969 | 1663 | return; |
twisti@3969 | 1664 | } |
twisti@3969 | 1665 | |
twisti@3969 | 1666 | if (TraceMethodHandles) { |
drchase@5732 | 1667 | ResourceMark rm(THREAD); |
drchase@5732 | 1668 | tty->print_cr("resolve_invokedynamic #%d %s %s", |
coleenp@4037 | 1669 | ConstantPool::decode_invokedynamic_index(index), |
twisti@3969 | 1670 | method_name->as_C_string(), method_signature->as_C_string()); |
twisti@3969 | 1671 | tty->print(" BSM info: "); bootstrap_specifier->print(); |
twisti@3969 | 1672 | } |
twisti@3969 | 1673 | |
twisti@3969 | 1674 | resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK); |
twisti@3969 | 1675 | } |
twisti@3969 | 1676 | |
twisti@3969 | 1677 | void LinkResolver::resolve_dynamic_call(CallInfo& result, |
twisti@3969 | 1678 | Handle bootstrap_specifier, |
twisti@3969 | 1679 | Symbol* method_name, Symbol* method_signature, |
twisti@3969 | 1680 | KlassHandle current_klass, |
twisti@3969 | 1681 | TRAPS) { |
twisti@3969 | 1682 | // JSR 292: this must resolve to an implicitly generated method MH.linkToCallSite(*...) |
twisti@3969 | 1683 | // The appendix argument is likely to be a freshly-created CallSite. |
twisti@3969 | 1684 | Handle resolved_appendix; |
twisti@4133 | 1685 | Handle resolved_method_type; |
twisti@3969 | 1686 | methodHandle resolved_method = |
twisti@3969 | 1687 | SystemDictionary::find_dynamic_call_site_invoker(current_klass, |
twisti@3969 | 1688 | bootstrap_specifier, |
twisti@3969 | 1689 | method_name, method_signature, |
twisti@3969 | 1690 | &resolved_appendix, |
twisti@4133 | 1691 | &resolved_method_type, |
twisti@4041 | 1692 | THREAD); |
aeriksso@7865 | 1693 | wrap_invokedynamic_exception(CHECK); |
aeriksso@7865 | 1694 | result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD); |
aeriksso@7865 | 1695 | wrap_invokedynamic_exception(CHECK); |
jrose@1161 | 1696 | } |
jrose@1161 | 1697 | |
duke@435 | 1698 | //------------------------------------------------------------------------------------------------------------------------ |
duke@435 | 1699 | #ifndef PRODUCT |
duke@435 | 1700 | |
drchase@5732 | 1701 | void CallInfo::print() { |
duke@435 | 1702 | ResourceMark rm; |
drchase@5732 | 1703 | const char* kindstr = "unknown"; |
drchase@5732 | 1704 | switch (_call_kind) { |
drchase@5732 | 1705 | case direct_call: kindstr = "direct"; break; |
drchase@5732 | 1706 | case vtable_call: kindstr = "vtable"; break; |
drchase@5732 | 1707 | case itable_call: kindstr = "itable"; break; |
drchase@5732 | 1708 | } |
drchase@5732 | 1709 | tty->print_cr("Call %s@%d %s", kindstr, _call_index, |
drchase@5732 | 1710 | _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string()); |
duke@435 | 1711 | } |
duke@435 | 1712 | |
duke@435 | 1713 | #endif |