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