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