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