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 "memory/resourceArea.hpp" 38 #include "memory/universe.inline.hpp" 39 #include "oops/instanceKlass.hpp" 40 #include "oops/method.hpp" 41 #include "oops/objArrayOop.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "prims/jvm.h" 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 outputStream* st; 770 if (logitables) { 771 st = Log(itables)::trace_stream(); 772 } else { 773 st = Log(vtables)::trace_stream(); 774 } 775 st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", 776 prefix, 777 (klass == NULL ? "<NULL>" : klass->internal_name()), 778 (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()), 779 Method::name_and_sig_as_C_string(resolved_klass, 780 method->name(), 781 method->signature()), 782 method->method_holder()->internal_name()); 783 method->print_linkage_flags(st); 784 if (index != -1) { 785 st->print("vtable_index:%d", index); 786 } 787 st->cr(); 788 #endif // PRODUCT 789 } 790 791 // Do linktime resolution of a method in the interface within the context of the specied bytecode. 792 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) { 793 794 Klass* resolved_klass = link_info.resolved_klass(); 795 796 // check if klass is interface 797 if (!resolved_klass->is_interface()) { 798 ResourceMark rm(THREAD); 799 char buf[200]; 800 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name()); 801 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 802 } 803 804 // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref 805 if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) { 806 ResourceMark rm(THREAD); 807 char buf[200]; 808 jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string()); 809 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 810 } 811 812 // lookup method in this interface or its super, java.lang.Object 813 // JDK8: also look for static methods 814 methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL); 815 816 if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { 817 // lookup method in all the super-interfaces 818 resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL); 819 } 820 821 if (resolved_method.is_null()) { 822 // no method found 823 ResourceMark rm(THREAD); 824 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), 825 Method::name_and_sig_as_C_string(resolved_klass, 826 link_info.name(), 827 link_info.signature())); 828 } 829 830 if (link_info.check_access()) { 831 // JDK8 adds non-public interface methods, and accessability check requirement 832 Klass* current_klass = link_info.current_klass(); 833 834 assert(current_klass != NULL , "current_klass should not be null"); 835 836 // check if method can be accessed by the referring class 837 check_method_accessability(current_klass, 838 resolved_klass, 839 resolved_method->method_holder(), 840 resolved_method, 841 CHECK_NULL); 842 843 check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL); 844 } 845 846 if (code != Bytecodes::_invokestatic && resolved_method->is_static()) { 847 ResourceMark rm(THREAD); 848 char buf[200]; 849 jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s", 850 Method::name_and_sig_as_C_string(resolved_klass, 851 resolved_method->name(), resolved_method->signature())); 852 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 853 } 854 855 if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) { 856 ResourceMark rm(THREAD); 857 char buf[200]; 858 859 Klass* current_klass = link_info.current_klass(); 860 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s", 861 Method::name_and_sig_as_C_string(resolved_klass, 862 resolved_method->name(), 863 resolved_method->signature()), 864 (current_klass == NULL ? "<NULL>" : current_klass->internal_name())); 865 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 866 } 867 868 if (log_develop_is_enabled(Trace, itables)) { 869 char buf[200]; 870 jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:", 871 Bytecodes::name(code)); 872 trace_method_resolution(buf, link_info.current_klass(), resolved_klass, 873 resolved_method, true); 874 } 875 876 return resolved_method; 877 } 878 879 //------------------------------------------------------------------------------------------------------------------------ 880 // Field resolution 881 882 void LinkResolver::check_field_accessability(Klass* ref_klass, 883 Klass* resolved_klass, 884 Klass* sel_klass, 885 const fieldDescriptor& fd, 886 TRAPS) { 887 if (!Reflection::verify_field_access(ref_klass, 888 resolved_klass, 889 sel_klass, 890 fd.access_flags(), 891 true)) { 892 ResourceMark rm(THREAD); 893 Exceptions::fthrow( 894 THREAD_AND_LOCATION, 895 vmSymbols::java_lang_IllegalAccessError(), 896 "tried to access field %s.%s from class %s", 897 sel_klass->external_name(), 898 fd.name()->as_C_string(), 899 ref_klass->external_name() 900 ); 901 return; 902 } 903 } 904 905 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) { 906 LinkInfo link_info(pool, index, method, CHECK); 907 resolve_field(fd, link_info, byte, true, CHECK); 908 } 909 910 void LinkResolver::resolve_field(fieldDescriptor& fd, 911 const LinkInfo& link_info, 912 Bytecodes::Code byte, bool initialize_class, 913 TRAPS) { 914 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || 915 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield || 916 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield || 917 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode"); 918 919 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); 920 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield); 921 // Check if there's a resolved klass containing the field 922 Klass* resolved_klass = link_info.resolved_klass(); 923 Symbol* field = link_info.name(); 924 Symbol* sig = link_info.signature(); 925 926 if (resolved_klass == NULL) { 927 ResourceMark rm(THREAD); 928 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 929 } 930 931 // Resolve instance field 932 Klass* sel_klass = resolved_klass->find_field(field, sig, &fd); 933 // check if field exists; i.e., if a klass containing the field def has been selected 934 if (sel_klass == NULL) { 935 ResourceMark rm(THREAD); 936 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 937 } 938 939 if (!link_info.check_access()) 940 // Access checking may be turned off when calling from within the VM. 941 return; 942 943 // check access 944 Klass* current_klass = link_info.current_klass(); 945 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); 946 947 // check for errors 948 if (is_static != fd.is_static()) { 949 ResourceMark rm(THREAD); 950 char msg[200]; 951 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string()); 952 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); 953 } 954 955 // A final field can be modified only 956 // (1) by methods declared in the class declaring the field and 957 // (2) by the <clinit> method (in case of a static field) 958 // or by the <init> method (in case of an instance field). 959 if (is_put && fd.access_flags().is_final()) { 960 ResourceMark rm(THREAD); 961 stringStream ss; 962 963 if (sel_klass != current_klass) { 964 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class", 965 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(), 966 current_klass->external_name()); 967 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); 968 } 969 970 if (fd.constants()->pool_holder()->major_version() >= 53) { 971 methodHandle m = link_info.current_method(); 972 assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes"); 973 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic && 974 fd.is_static() && 975 !m()->is_static_initializer()); 976 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) && 977 !fd.is_static() && 978 !m->is_object_initializer()); 979 980 if (is_initialized_static_final_update || is_initialized_instance_final_update) { 981 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ", 982 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(), 983 m()->name()->as_C_string(), 984 is_static ? "<clinit>" : "<init>"); 985 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); 986 } 987 } 988 } 989 990 // initialize resolved_klass if necessary 991 // note 1: the klass which declared the field must be initialized (i.e, sel_klass) 992 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) 993 // 994 // note 2: we don't want to force initialization if we are just checking 995 // if the field access is legal; e.g., during compilation 996 if (is_static && initialize_class) { 997 sel_klass->initialize(CHECK); 998 } 999 1000 if (sel_klass != current_klass) { 1001 check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK); 1002 } 1003 1004 // return information. note that the klass is set to the actual klass containing the 1005 // field, otherwise access of static fields in superclasses will not work. 1006 } 1007 1008 1009 //------------------------------------------------------------------------------------------------------------------------ 1010 // Invoke resolution 1011 // 1012 // Naming conventions: 1013 // 1014 // resolved_method the specified method (i.e., static receiver specified via constant pool index) 1015 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class) 1016 // resolved_klass the specified klass (i.e., specified via constant pool index) 1017 // recv_klass the receiver klass 1018 1019 1020 void LinkResolver::resolve_static_call(CallInfo& result, 1021 const LinkInfo& link_info, 1022 bool initialize_class, TRAPS) { 1023 methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK); 1024 1025 // The resolved class can change as a result of this resolution. 1026 Klass* resolved_klass = resolved_method->method_holder(); 1027 1028 // Initialize klass (this should only happen if everything is ok) 1029 if (initialize_class && resolved_klass->should_be_initialized()) { 1030 resolved_klass->initialize(CHECK); 1031 // Use updated LinkInfo to reresolve with resolved method holder 1032 LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(), 1033 link_info.current_klass(), 1034 link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check); 1035 resolved_method = linktime_resolve_static_method(new_info, CHECK); 1036 } 1037 1038 // setup result 1039 result.set_static(resolved_klass, resolved_method, CHECK); 1040 } 1041 1042 // throws linktime exceptions 1043 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) { 1044 1045 Klass* resolved_klass = link_info.resolved_klass(); 1046 methodHandle resolved_method; 1047 if (!resolved_klass->is_interface()) { 1048 resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL); 1049 } else { 1050 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL); 1051 } 1052 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier"); 1053 1054 // check if static 1055 if (!resolved_method->is_static()) { 1056 ResourceMark rm(THREAD); 1057 char buf[200]; 1058 jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass, 1059 resolved_method->name(), 1060 resolved_method->signature())); 1061 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1062 } 1063 return resolved_method; 1064 } 1065 1066 1067 void LinkResolver::resolve_special_call(CallInfo& result, 1068 Handle recv, 1069 const LinkInfo& link_info, 1070 TRAPS) { 1071 methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK); 1072 runtime_resolve_special_method(result, resolved_method, 1073 link_info.resolved_klass(), 1074 link_info.current_klass(), 1075 recv, 1076 link_info.check_access(), CHECK); 1077 } 1078 1079 // throws linktime exceptions 1080 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, 1081 TRAPS) { 1082 1083 // Invokespecial is called for multiple special reasons: 1084 // <init> 1085 // local private method invocation, for classes and interfaces 1086 // superclass.method, which can also resolve to a default method 1087 // and the selected method is recalculated relative to the direct superclass 1088 // superinterface.method, which explicitly does not check shadowing 1089 Klass* resolved_klass = link_info.resolved_klass(); 1090 methodHandle resolved_method; 1091 1092 if (!resolved_klass->is_interface()) { 1093 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL); 1094 } else { 1095 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL); 1096 } 1097 1098 // check if method name is <init>, that it is found in same klass as static type 1099 if (resolved_method->name() == vmSymbols::object_initializer_name() && 1100 resolved_method->method_holder() != resolved_klass) { 1101 ResourceMark rm(THREAD); 1102 Exceptions::fthrow( 1103 THREAD_AND_LOCATION, 1104 vmSymbols::java_lang_NoSuchMethodError(), 1105 "%s: method %s%s not found", 1106 resolved_klass->external_name(), 1107 resolved_method->name()->as_C_string(), 1108 resolved_method->signature()->as_C_string() 1109 ); 1110 return NULL; 1111 } 1112 1113 // check if invokespecial's interface method reference is in an indirect superinterface 1114 Klass* current_klass = link_info.current_klass(); 1115 if (current_klass != NULL && resolved_klass->is_interface()) { 1116 InstanceKlass* ck = InstanceKlass::cast(current_klass); 1117 InstanceKlass *klass_to_check = !ck->is_anonymous() ? 1118 ck : 1119 InstanceKlass::cast(ck->host_klass()); 1120 // Disable verification for the dynamically-generated reflection bytecodes. 1121 bool is_reflect = klass_to_check->is_subclass_of( 1122 SystemDictionary::reflect_MagicAccessorImpl_klass()); 1123 1124 if (!is_reflect && 1125 !klass_to_check->is_same_or_direct_interface(resolved_klass)) { 1126 ResourceMark rm(THREAD); 1127 char buf[200]; 1128 jio_snprintf(buf, sizeof(buf), 1129 "Interface method reference: %s, is in an indirect superinterface of %s", 1130 Method::name_and_sig_as_C_string(resolved_klass, 1131 resolved_method->name(), 1132 resolved_method->signature()), 1133 current_klass->external_name()); 1134 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1135 } 1136 } 1137 1138 // check if not static 1139 if (resolved_method->is_static()) { 1140 ResourceMark rm(THREAD); 1141 char buf[200]; 1142 jio_snprintf(buf, sizeof(buf), 1143 "Expecting non-static method %s", 1144 Method::name_and_sig_as_C_string(resolved_klass, 1145 resolved_method->name(), 1146 resolved_method->signature())); 1147 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1148 } 1149 1150 if (log_develop_is_enabled(Trace, itables)) { 1151 trace_method_resolution("invokespecial resolved method: caller-class:", 1152 current_klass, resolved_klass, resolved_method, true); 1153 } 1154 1155 return resolved_method; 1156 } 1157 1158 // throws runtime exceptions 1159 void LinkResolver::runtime_resolve_special_method(CallInfo& result, 1160 const methodHandle& resolved_method, 1161 Klass* resolved_klass, 1162 Klass* current_klass, 1163 Handle recv, 1164 bool check_access, TRAPS) { 1165 1166 // resolved method is selected method unless we have an old-style lookup 1167 // for a superclass method 1168 // Invokespecial for a superinterface, resolved method is selected method, 1169 // no checks for shadowing 1170 methodHandle sel_method(THREAD, resolved_method()); 1171 1172 if (check_access && 1173 // check if the method is not <init> 1174 resolved_method->name() != vmSymbols::object_initializer_name()) { 1175 1176 // check if this is an old-style super call and do a new lookup if so 1177 // a) check if ACC_SUPER flag is set for the current class 1178 if ((current_klass->is_super() || !AllowNonVirtualCalls) && 1179 // b) check if the class of the resolved_klass is a superclass 1180 // (not supertype in order to exclude interface classes) of the current class. 1181 // This check is not performed for super.invoke for interface methods 1182 // in super interfaces. 1183 current_klass->is_subclass_of(resolved_klass) && 1184 current_klass != resolved_klass) { 1185 // Lookup super method 1186 Klass* super_klass = current_klass->super(); 1187 sel_method = lookup_instance_method_in_klasses(super_klass, 1188 resolved_method->name(), 1189 resolved_method->signature(), CHECK); 1190 // check if found 1191 if (sel_method.is_null()) { 1192 ResourceMark rm(THREAD); 1193 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1194 Method::name_and_sig_as_C_string(resolved_klass, 1195 resolved_method->name(), 1196 resolved_method->signature())); 1197 } 1198 } 1199 1200 // Check that the class of objectref (the receiver) is the current class or interface, 1201 // or a subtype of the current class or interface (the sender), otherwise invokespecial 1202 // throws IllegalAccessError. 1203 // The verifier checks that the sender is a subtype of the class in the I/MR operand. 1204 // The verifier also checks that the receiver is a subtype of the sender, if the sender is 1205 // a class. If the sender is an interface, the check has to be performed at runtime. 1206 InstanceKlass* sender = InstanceKlass::cast(current_klass); 1207 sender = sender->is_anonymous() ? sender->host_klass() : sender; 1208 if (sender->is_interface() && recv.not_null()) { 1209 Klass* receiver_klass = recv->klass(); 1210 if (!receiver_klass->is_subtype_of(sender)) { 1211 ResourceMark rm(THREAD); 1212 char buf[500]; 1213 jio_snprintf(buf, sizeof(buf), 1214 "Receiver class %s must be the current class or a subtype of interface %s", 1215 receiver_klass->name()->as_C_string(), 1216 sender->name()->as_C_string()); 1217 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf); 1218 } 1219 } 1220 } 1221 1222 // check if not static 1223 if (sel_method->is_static()) { 1224 ResourceMark rm(THREAD); 1225 char buf[200]; 1226 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass, 1227 resolved_method->name(), 1228 resolved_method->signature())); 1229 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1230 } 1231 1232 // check if abstract 1233 if (sel_method->is_abstract()) { 1234 ResourceMark rm(THREAD); 1235 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1236 Method::name_and_sig_as_C_string(resolved_klass, 1237 sel_method->name(), 1238 sel_method->signature())); 1239 } 1240 1241 if (log_develop_is_enabled(Trace, itables)) { 1242 trace_method_resolution("invokespecial selected method: resolved-class:", 1243 resolved_klass, resolved_klass, sel_method, true); 1244 } 1245 1246 // setup result 1247 result.set_static(resolved_klass, sel_method, CHECK); 1248 } 1249 1250 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass, 1251 const LinkInfo& link_info, 1252 bool check_null_and_abstract, TRAPS) { 1253 methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK); 1254 runtime_resolve_virtual_method(result, resolved_method, 1255 link_info.resolved_klass(), 1256 recv, receiver_klass, 1257 check_null_and_abstract, CHECK); 1258 } 1259 1260 // throws linktime exceptions 1261 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info, 1262 TRAPS) { 1263 // normal method resolution 1264 methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL); 1265 1266 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1267 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1268 1269 // check if private interface method 1270 Klass* resolved_klass = link_info.resolved_klass(); 1271 Klass* current_klass = link_info.current_klass(); 1272 1273 // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method 1274 if (resolved_klass->is_interface() && resolved_method->is_private()) { 1275 ResourceMark rm(THREAD); 1276 char buf[200]; 1277 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s", 1278 Method::name_and_sig_as_C_string(resolved_klass, 1279 resolved_method->name(), 1280 resolved_method->signature()), 1281 (current_klass == NULL ? "<NULL>" : current_klass->internal_name())); 1282 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1283 } 1284 1285 // check if not static 1286 if (resolved_method->is_static()) { 1287 ResourceMark rm(THREAD); 1288 char buf[200]; 1289 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass, 1290 resolved_method->name(), 1291 resolved_method->signature())); 1292 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1293 } 1294 1295 if (log_develop_is_enabled(Trace, vtables)) { 1296 trace_method_resolution("invokevirtual resolved method: caller-class:", 1297 current_klass, resolved_klass, resolved_method, false); 1298 } 1299 1300 return resolved_method; 1301 } 1302 1303 // throws runtime exceptions 1304 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result, 1305 const methodHandle& resolved_method, 1306 Klass* resolved_klass, 1307 Handle recv, 1308 Klass* recv_klass, 1309 bool check_null_and_abstract, 1310 TRAPS) { 1311 1312 // setup default return values 1313 int vtable_index = Method::invalid_vtable_index; 1314 methodHandle selected_method; 1315 1316 assert(recv.is_null() || recv->is_oop(), "receiver is not an oop"); 1317 1318 // runtime method resolution 1319 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists 1320 THROW(vmSymbols::java_lang_NullPointerException()); 1321 } 1322 1323 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s 1324 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since 1325 // a missing receiver might result in a bogus lookup. 1326 assert(resolved_method->method_holder()->is_linked(), "must be linked"); 1327 1328 // do lookup based on receiver klass using the vtable index 1329 if (resolved_method->method_holder()->is_interface()) { // default or miranda method 1330 vtable_index = vtable_index_of_interface_method(resolved_klass, 1331 resolved_method); 1332 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); 1333 1334 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index)); 1335 } else { 1336 // at this point we are sure that resolved_method is virtual and not 1337 // a default or miranda method; therefore, it must have a valid vtable index. 1338 assert(!resolved_method->has_itable_index(), ""); 1339 vtable_index = resolved_method->vtable_index(); 1340 // We could get a negative vtable_index for final methods, 1341 // because as an optimization they are they are never put in the vtable, 1342 // unless they override an existing method. 1343 // If we do get a negative, it means the resolved method is the the selected 1344 // method, and it can never be changed by an override. 1345 if (vtable_index == Method::nonvirtual_vtable_index) { 1346 assert(resolved_method->can_be_statically_bound(), "cannot override this method"); 1347 selected_method = resolved_method; 1348 } else { 1349 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index)); 1350 } 1351 } 1352 1353 // check if method exists 1354 if (selected_method.is_null()) { 1355 ResourceMark rm(THREAD); 1356 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1357 Method::name_and_sig_as_C_string(resolved_klass, 1358 resolved_method->name(), 1359 resolved_method->signature())); 1360 } 1361 1362 // check if abstract 1363 if (check_null_and_abstract && selected_method->is_abstract()) { 1364 ResourceMark rm(THREAD); 1365 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1366 Method::name_and_sig_as_C_string(resolved_klass, 1367 selected_method->name(), 1368 selected_method->signature())); 1369 } 1370 1371 if (log_develop_is_enabled(Trace, vtables)) { 1372 trace_method_resolution("invokevirtual selected method: receiver-class:", 1373 recv_klass, resolved_klass, selected_method, 1374 false, vtable_index); 1375 } 1376 // setup result 1377 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK); 1378 } 1379 1380 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass, 1381 const LinkInfo& link_info, 1382 bool check_null_and_abstract, TRAPS) { 1383 // throws linktime exceptions 1384 methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK); 1385 runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(), 1386 recv, recv_klass, check_null_and_abstract, CHECK); 1387 } 1388 1389 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info, 1390 TRAPS) { 1391 // normal interface method resolution 1392 methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL); 1393 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1394 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1395 1396 return resolved_method; 1397 } 1398 1399 // throws runtime exceptions 1400 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, 1401 const methodHandle& resolved_method, 1402 Klass* resolved_klass, 1403 Handle recv, 1404 Klass* recv_klass, 1405 bool check_null_and_abstract, TRAPS) { 1406 // check if receiver exists 1407 if (check_null_and_abstract && recv.is_null()) { 1408 THROW(vmSymbols::java_lang_NullPointerException()); 1409 } 1410 1411 // check if receiver klass implements the resolved interface 1412 if (!recv_klass->is_subtype_of(resolved_klass)) { 1413 ResourceMark rm(THREAD); 1414 char buf[200]; 1415 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s", 1416 recv_klass->external_name(), 1417 resolved_klass->external_name()); 1418 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1419 } 1420 1421 // do lookup based on receiver klass 1422 // This search must match the linktime preparation search for itable initialization 1423 // to correctly enforce loader constraints for interface method inheritance 1424 methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass, 1425 resolved_method->name(), 1426 resolved_method->signature(), CHECK); 1427 if (sel_method.is_null() && !check_null_and_abstract) { 1428 // In theory this is a harmless placeholder value, but 1429 // in practice leaving in null affects the nsk default method tests. 1430 // This needs further study. 1431 sel_method = resolved_method; 1432 } 1433 // check if method exists 1434 if (sel_method.is_null()) { 1435 ResourceMark rm(THREAD); 1436 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1437 Method::name_and_sig_as_C_string(recv_klass, 1438 resolved_method->name(), 1439 resolved_method->signature())); 1440 } 1441 // check access 1442 // Throw Illegal Access Error if sel_method is not public. 1443 if (!sel_method->is_public()) { 1444 ResourceMark rm(THREAD); 1445 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), 1446 Method::name_and_sig_as_C_string(recv_klass, 1447 sel_method->name(), 1448 sel_method->signature())); 1449 } 1450 // check if abstract 1451 if (check_null_and_abstract && sel_method->is_abstract()) { 1452 ResourceMark rm(THREAD); 1453 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1454 Method::name_and_sig_as_C_string(recv_klass, 1455 sel_method->name(), 1456 sel_method->signature())); 1457 } 1458 1459 if (log_develop_is_enabled(Trace, itables)) { 1460 trace_method_resolution("invokeinterface selected method: receiver-class:", 1461 recv_klass, resolved_klass, sel_method, true); 1462 } 1463 // setup result 1464 if (!resolved_method->has_itable_index()) { 1465 int vtable_index = resolved_method->vtable_index(); 1466 assert(vtable_index == sel_method->vtable_index(), "sanity check"); 1467 result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK); 1468 } else { 1469 int itable_index = resolved_method()->itable_index(); 1470 result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK); 1471 } 1472 } 1473 1474 1475 methodHandle LinkResolver::linktime_resolve_interface_method_or_null( 1476 const LinkInfo& link_info) { 1477 EXCEPTION_MARK; 1478 methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD); 1479 if (HAS_PENDING_EXCEPTION) { 1480 CLEAR_PENDING_EXCEPTION; 1481 return methodHandle(); 1482 } else { 1483 return method_result; 1484 } 1485 } 1486 1487 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null( 1488 const LinkInfo& link_info) { 1489 EXCEPTION_MARK; 1490 methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD); 1491 if (HAS_PENDING_EXCEPTION) { 1492 CLEAR_PENDING_EXCEPTION; 1493 return methodHandle(); 1494 } else { 1495 return method_result; 1496 } 1497 } 1498 1499 methodHandle LinkResolver::resolve_virtual_call_or_null( 1500 Klass* receiver_klass, 1501 const LinkInfo& link_info) { 1502 EXCEPTION_MARK; 1503 CallInfo info; 1504 resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1505 if (HAS_PENDING_EXCEPTION) { 1506 CLEAR_PENDING_EXCEPTION; 1507 return methodHandle(); 1508 } 1509 return info.selected_method(); 1510 } 1511 1512 methodHandle LinkResolver::resolve_interface_call_or_null( 1513 Klass* receiver_klass, 1514 const LinkInfo& link_info) { 1515 EXCEPTION_MARK; 1516 CallInfo info; 1517 resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1518 if (HAS_PENDING_EXCEPTION) { 1519 CLEAR_PENDING_EXCEPTION; 1520 return methodHandle(); 1521 } 1522 return info.selected_method(); 1523 } 1524 1525 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass, 1526 const LinkInfo& link_info) { 1527 EXCEPTION_MARK; 1528 CallInfo info; 1529 resolve_virtual_call(info, Handle(), receiver_klass, link_info, 1530 /*check_null_or_abstract*/false, THREAD); 1531 if (HAS_PENDING_EXCEPTION) { 1532 CLEAR_PENDING_EXCEPTION; 1533 return Method::invalid_vtable_index; 1534 } 1535 return info.vtable_index(); 1536 } 1537 1538 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) { 1539 EXCEPTION_MARK; 1540 CallInfo info; 1541 resolve_static_call(info, link_info, /*initialize_class*/false, THREAD); 1542 if (HAS_PENDING_EXCEPTION) { 1543 CLEAR_PENDING_EXCEPTION; 1544 return methodHandle(); 1545 } 1546 return info.selected_method(); 1547 } 1548 1549 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) { 1550 EXCEPTION_MARK; 1551 CallInfo info; 1552 resolve_special_call(info, Handle(), link_info, THREAD); 1553 if (HAS_PENDING_EXCEPTION) { 1554 CLEAR_PENDING_EXCEPTION; 1555 return methodHandle(); 1556 } 1557 return info.selected_method(); 1558 } 1559 1560 1561 1562 //------------------------------------------------------------------------------------------------------------------------ 1563 // ConstantPool entries 1564 1565 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) { 1566 switch (byte) { 1567 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break; 1568 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break; 1569 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break; 1570 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break; 1571 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break; 1572 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break; 1573 } 1574 return; 1575 } 1576 1577 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv, 1578 const methodHandle& attached_method, 1579 Bytecodes::Code byte, TRAPS) { 1580 Klass* defc = attached_method->method_holder(); 1581 Symbol* name = attached_method->name(); 1582 Symbol* type = attached_method->signature(); 1583 LinkInfo link_info(defc, name, type); 1584 switch(byte) { 1585 case Bytecodes::_invokevirtual: 1586 resolve_virtual_call(result, recv, recv->klass(), link_info, 1587 /*check_null_and_abstract=*/true, CHECK); 1588 break; 1589 case Bytecodes::_invokeinterface: 1590 resolve_interface_call(result, recv, recv->klass(), link_info, 1591 /*check_null_and_abstract=*/true, CHECK); 1592 break; 1593 case Bytecodes::_invokestatic: 1594 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK); 1595 break; 1596 case Bytecodes::_invokespecial: 1597 resolve_special_call(result, recv, link_info, CHECK); 1598 break; 1599 default: 1600 fatal("bad call: %s", Bytecodes::name(byte)); 1601 } 1602 } 1603 1604 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1605 LinkInfo link_info(pool, index, CHECK); 1606 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK); 1607 } 1608 1609 1610 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv, 1611 const constantPoolHandle& pool, int index, TRAPS) { 1612 LinkInfo link_info(pool, index, CHECK); 1613 resolve_special_call(result, recv, link_info, CHECK); 1614 } 1615 1616 1617 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv, 1618 const constantPoolHandle& pool, int index, 1619 TRAPS) { 1620 1621 LinkInfo link_info(pool, index, CHECK); 1622 Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass(); 1623 resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK); 1624 } 1625 1626 1627 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) { 1628 LinkInfo link_info(pool, index, CHECK); 1629 Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass(); 1630 resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK); 1631 } 1632 1633 1634 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1635 // This guy is reached from InterpreterRuntime::resolve_invokehandle. 1636 LinkInfo link_info(pool, index, CHECK); 1637 if (TraceMethodHandles) { 1638 ResourceMark rm(THREAD); 1639 tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(), 1640 link_info.signature()->as_C_string()); 1641 } 1642 resolve_handle_call(result, link_info, CHECK); 1643 } 1644 1645 void LinkResolver::resolve_handle_call(CallInfo& result, 1646 const LinkInfo& link_info, 1647 TRAPS) { 1648 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar 1649 Klass* resolved_klass = link_info.resolved_klass(); 1650 assert(resolved_klass == SystemDictionary::MethodHandle_klass() || 1651 resolved_klass == SystemDictionary::VarHandle_klass(), ""); 1652 assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), ""); 1653 Handle resolved_appendix; 1654 Handle resolved_method_type; 1655 methodHandle resolved_method = lookup_polymorphic_method(link_info, 1656 &resolved_appendix, &resolved_method_type, CHECK); 1657 result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK); 1658 } 1659 1660 static void wrap_invokedynamic_exception(TRAPS) { 1661 if (HAS_PENDING_EXCEPTION) { 1662 // See the "Linking Exceptions" section for the invokedynamic instruction 1663 // in JVMS 6.5. 1664 if (PENDING_EXCEPTION->is_a(SystemDictionary::Error_klass())) { 1665 // Pass through an Error, including BootstrapMethodError, any other form 1666 // of linkage error, or say ThreadDeath/OutOfMemoryError 1667 if (TraceMethodHandles) { 1668 tty->print_cr("invokedynamic passes through an Error for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION)); 1669 PENDING_EXCEPTION->print(); 1670 } 1671 return; 1672 } 1673 1674 // Otherwise wrap the exception in a BootstrapMethodError 1675 if (TraceMethodHandles) { 1676 tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION)); 1677 PENDING_EXCEPTION->print(); 1678 } 1679 Handle nested_exception(THREAD, PENDING_EXCEPTION); 1680 CLEAR_PENDING_EXCEPTION; 1681 THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) 1682 } 1683 } 1684 1685 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1686 Symbol* method_name = pool->name_ref_at(index); 1687 Symbol* method_signature = pool->signature_ref_at(index); 1688 Klass* current_klass = pool->pool_holder(); 1689 1690 // Resolve the bootstrap specifier (BSM + optional arguments). 1691 Handle bootstrap_specifier; 1692 // Check if CallSite has been bound already: 1693 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index); 1694 if (cpce->is_f1_null()) { 1695 int pool_index = cpce->constant_pool_index(); 1696 oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD); 1697 wrap_invokedynamic_exception(CHECK); 1698 assert(bsm_info != NULL, ""); 1699 // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic. 1700 bootstrap_specifier = Handle(THREAD, bsm_info); 1701 } 1702 if (!cpce->is_f1_null()) { 1703 methodHandle method( THREAD, cpce->f1_as_method()); 1704 Handle appendix( THREAD, cpce->appendix_if_resolved(pool)); 1705 Handle method_type(THREAD, cpce->method_type_if_resolved(pool)); 1706 result.set_handle(method, appendix, method_type, THREAD); 1707 wrap_invokedynamic_exception(CHECK); 1708 return; 1709 } 1710 1711 if (TraceMethodHandles) { 1712 ResourceMark rm(THREAD); 1713 tty->print_cr("resolve_invokedynamic #%d %s %s in %s", 1714 ConstantPool::decode_invokedynamic_index(index), 1715 method_name->as_C_string(), method_signature->as_C_string(), 1716 current_klass->name()->as_C_string()); 1717 tty->print(" BSM info: "); bootstrap_specifier->print(); 1718 } 1719 1720 resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK); 1721 } 1722 1723 void LinkResolver::resolve_dynamic_call(CallInfo& result, 1724 Handle bootstrap_specifier, 1725 Symbol* method_name, Symbol* method_signature, 1726 Klass* current_klass, 1727 TRAPS) { 1728 // JSR 292: this must resolve to an implicitly generated method MH.linkToCallSite(*...) 1729 // The appendix argument is likely to be a freshly-created CallSite. 1730 Handle resolved_appendix; 1731 Handle resolved_method_type; 1732 methodHandle resolved_method = 1733 SystemDictionary::find_dynamic_call_site_invoker(current_klass, 1734 bootstrap_specifier, 1735 method_name, method_signature, 1736 &resolved_appendix, 1737 &resolved_method_type, 1738 THREAD); 1739 wrap_invokedynamic_exception(CHECK); 1740 result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD); 1741 wrap_invokedynamic_exception(CHECK); 1742 }