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