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