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