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::_withfield || 965 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield || 966 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode"); 967 968 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); 969 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || 970 byte == Bytecodes::_nofast_putfield || byte == Bytecodes::_withfield); 971 // Check if there's a resolved klass containing the field 972 Klass* resolved_klass = link_info.resolved_klass(); 973 Symbol* field = link_info.name(); 974 Symbol* sig = link_info.signature(); 975 976 977 if (byte == Bytecodes::_withfield && !resolved_klass->is_inline_klass()) { 978 ResourceMark rm(THREAD); 979 char msg[200]; 980 jio_snprintf(msg, sizeof(msg), "Bytecode withfield cannot be used on identity class %s", resolved_klass->external_name()); 981 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); 982 } 983 984 if (is_put && !is_static && byte != Bytecodes::_withfield && resolved_klass->is_inline_klass()) { 985 ResourceMark rm(THREAD); 986 char msg[200]; 987 jio_snprintf(msg, sizeof(msg), "Bytecode putfield cannot be used on primitive class %s", resolved_klass->external_name()); 988 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); 989 } 990 991 // Resolve instance field 992 Klass* sel_klass = resolved_klass->find_field(field, sig, &fd); 993 // check if field exists; i.e., if a klass containing the field def has been selected 994 if (sel_klass == nullptr) { 995 ResourceMark rm(THREAD); 996 stringStream ss; 997 ss.print("Class %s does not have member field '", resolved_klass->external_name()); 998 sig->print_as_field_external_type(&ss); 999 ss.print(" %s'", field->as_C_string()); 1000 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), ss.as_string()); 1001 } 1002 1003 // Access checking may be turned off when calling from within the VM. 1004 Klass* current_klass = link_info.current_klass(); 1005 if (link_info.check_access()) { 1006 1007 // check access 1008 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); 1009 1010 // check for errors 1011 if (is_static != fd.is_static()) { 1012 ResourceMark rm(THREAD); 1013 char msg[200]; 1014 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string()); 1015 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); 1016 } 1017 1018 // A final field can be modified only 1019 // (1) by methods declared in the class declaring the field and 1020 // (2) by the <clinit> method (in case of a static field) 1021 // or by the <init> method (in case of an instance field). 1022 // (3) by withfield when field is in a value type and the 1023 // selected class and current class are nest mates. 1024 if (is_put && fd.access_flags().is_final()) { 1025 1026 if (sel_klass != current_klass) { 1027 // If byte code is a withfield check if they are nestmates. 1028 bool are_nestmates = false; 1029 if (sel_klass->is_instance_klass() && 1030 InstanceKlass::cast(sel_klass)->is_inline_klass() && 1031 current_klass->is_instance_klass()) { 1032 are_nestmates = InstanceKlass::cast(current_klass)->has_nestmate_access_to(InstanceKlass::cast(sel_klass), THREAD); 1033 } 1034 if (!are_nestmates) { 1035 ResourceMark rm(THREAD); 1036 stringStream ss; 1037 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class", 1038 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(), 1039 current_klass->external_name()); 1040 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); 1041 } 1042 } 1043 1044 if (fd.constants()->pool_holder()->major_version() >= 53) { 1045 Method* m = link_info.current_method(); 1046 assert(m != nullptr, "information about the current method must be available for 'put' bytecodes"); 1047 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic && 1048 fd.is_static() && 1049 !m->is_class_initializer()); 1050 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) && 1051 !fd.is_static() && 1052 !m->is_object_constructor()); 1053 1054 if (is_initialized_static_final_update || is_initialized_instance_final_update) { 1055 ResourceMark rm(THREAD); 1056 stringStream ss; 1057 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ", 1058 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(), 1059 m->name()->as_C_string(), 1060 is_static ? "<clinit>" : "<init>"); 1061 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); 1062 } 1063 } 1064 } 1065 1066 // initialize resolved_klass if necessary 1067 // note 1: the klass which declared the field must be initialized (i.e, sel_klass) 1068 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) 1069 // 1070 // note 2: we don't want to force initialization if we are just checking 1071 // if the field access is legal; e.g., during compilation 1072 if (is_static && initialize_class) { 1073 sel_klass->initialize(CHECK); 1074 } 1075 } 1076 1077 if (link_info.check_loader_constraints() && (sel_klass != current_klass) && (current_klass != nullptr)) { 1078 check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK); 1079 } 1080 1081 // return information. note that the klass is set to the actual klass containing the 1082 // field, otherwise access of static fields in superclasses will not work. 1083 } 1084 1085 1086 //------------------------------------------------------------------------------------------------------------------------ 1087 // Invoke resolution 1088 // 1089 // Naming conventions: 1090 // 1091 // resolved_method the specified method (i.e., static receiver specified via constant pool index) 1092 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class) 1093 // resolved_klass the specified klass (i.e., specified via constant pool index) 1094 // recv_klass the receiver klass 1095 1096 1097 void LinkResolver::resolve_static_call(CallInfo& result, 1098 const LinkInfo& link_info, 1099 bool initialize_class, TRAPS) { 1100 Method* resolved_method = linktime_resolve_static_method(link_info, CHECK); 1101 1102 // The resolved class can change as a result of this resolution. 1103 Klass* resolved_klass = resolved_method->method_holder(); 1104 1105 // Initialize klass (this should only happen if everything is ok) 1106 if (initialize_class && resolved_klass->should_be_initialized()) { 1107 resolved_klass->initialize(CHECK); 1108 // Use updated LinkInfo to reresolve with resolved method holder 1109 LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(), 1110 link_info.current_klass(), 1111 link_info.check_access() ? LinkInfo::AccessCheck::required : LinkInfo::AccessCheck::skip, 1112 link_info.check_loader_constraints() ? LinkInfo::LoaderConstraintCheck::required : LinkInfo::LoaderConstraintCheck::skip); 1113 resolved_method = linktime_resolve_static_method(new_info, CHECK); 1114 } 1115 1116 if (resolved_method->is_continuation_native_intrinsic() 1117 && resolved_method->from_interpreted_entry() == nullptr) { // does a load_acquire 1118 methodHandle mh(THREAD, resolved_method); 1119 // Generate a compiled form of the enterSpecial intrinsic. 1120 AdapterHandlerLibrary::create_native_wrapper(mh); 1121 } 1122 1123 // setup result 1124 result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK); 1125 JFR_ONLY(Jfr::on_resolution(result, CHECK);) 1126 } 1127 1128 // throws linktime exceptions 1129 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) { 1130 1131 Klass* resolved_klass = link_info.resolved_klass(); 1132 Method* resolved_method; 1133 if (!resolved_klass->is_interface()) { 1134 resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL); 1135 } else { 1136 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL); 1137 } 1138 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier"); 1139 1140 // check if static 1141 if (!resolved_method->is_static()) { 1142 ResourceMark rm(THREAD); 1143 stringStream ss; 1144 ss.print("Expected static method '"); 1145 resolved_method->print_external_name(&ss); 1146 ss.print("'"); 1147 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string()); 1148 } 1149 return resolved_method; 1150 } 1151 1152 1153 void LinkResolver::resolve_special_call(CallInfo& result, 1154 Handle recv, 1155 const LinkInfo& link_info, 1156 TRAPS) { 1157 Method* resolved_method = linktime_resolve_special_method(link_info, CHECK); 1158 runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK); 1159 } 1160 1161 // throws linktime exceptions 1162 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) { 1163 1164 // Invokespecial is called for multiple special reasons: 1165 // <init> 1166 // local private method invocation, for classes and interfaces 1167 // superclass.method, which can also resolve to a default method 1168 // and the selected method is recalculated relative to the direct superclass 1169 // superinterface.method, which explicitly does not check shadowing 1170 Klass* resolved_klass = link_info.resolved_klass(); 1171 Method* resolved_method = nullptr; 1172 1173 if (!resolved_klass->is_interface()) { 1174 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL); 1175 } else { 1176 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL); 1177 } 1178 1179 // check if method name is <init>, that it is found in same klass as static type 1180 // Since this method is never inherited from a super, any appearance here under 1181 // the wrong class would be an error. 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 // Disable verification for the dynamically-generated reflection bytecodes 1204 // for serialization constructor accessor. 1205 bool is_reflect = klass_to_check->is_subclass_of( 1206 vmClasses::reflect_SerializationConstructorAccessorImpl_klass()); 1207 1208 if (!is_reflect && 1209 !klass_to_check->is_same_or_direct_interface(resolved_klass)) { 1210 ResourceMark rm(THREAD); 1211 stringStream ss; 1212 ss.print("Interface method reference: '"); 1213 resolved_method->print_external_name(&ss); 1214 ss.print("', is in an indirect superinterface of %s", 1215 current_klass->external_name()); 1216 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string()); 1217 } 1218 } 1219 1220 // check if not static 1221 if (resolved_method->is_static()) { 1222 ResourceMark rm(THREAD); 1223 stringStream ss; 1224 ss.print("Expecting non-static method '"); 1225 resolved_method->print_external_name(&ss); 1226 ss.print("'"); 1227 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string()); 1228 } 1229 1230 if (log_develop_is_enabled(Trace, itables)) { 1231 trace_method_resolution("invokespecial resolved method: caller-class:", 1232 current_klass, resolved_klass, resolved_method, true); 1233 } 1234 1235 return resolved_method; 1236 } 1237 1238 // throws runtime exceptions 1239 void LinkResolver::runtime_resolve_special_method(CallInfo& result, 1240 const LinkInfo& link_info, 1241 const methodHandle& resolved_method, 1242 Handle recv, TRAPS) { 1243 1244 Klass* resolved_klass = link_info.resolved_klass(); 1245 1246 // resolved method is selected method unless we have an old-style lookup 1247 // for a superclass method 1248 // Invokespecial for a superinterface, resolved method is selected method, 1249 // no checks for shadowing 1250 methodHandle sel_method(THREAD, resolved_method()); 1251 1252 if (link_info.check_access() && 1253 // check if the method is not <init>, which is never inherited 1254 resolved_method->name() != vmSymbols::object_initializer_name()) { 1255 1256 Klass* current_klass = link_info.current_klass(); 1257 1258 // Check if the class of the resolved_klass is a superclass 1259 // (not supertype in order to exclude interface classes) of the current class. 1260 // This check is not performed for super.invoke for interface methods 1261 // in super interfaces. 1262 if (current_klass->is_subclass_of(resolved_klass) && 1263 current_klass != resolved_klass) { 1264 // Lookup super method 1265 Klass* super_klass = current_klass->super(); 1266 Method* instance_method = lookup_instance_method_in_klasses(super_klass, 1267 resolved_method->name(), 1268 resolved_method->signature(), 1269 Klass::PrivateLookupMode::find); 1270 sel_method = methodHandle(THREAD, instance_method); 1271 1272 // check if found 1273 if (sel_method.is_null()) { 1274 ResourceMark rm(THREAD); 1275 stringStream ss; 1276 ss.print("'"); 1277 resolved_method->print_external_name(&ss); 1278 ss.print("'"); 1279 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string()); 1280 // check loader constraints if found a different method 1281 } else if (link_info.check_loader_constraints() && sel_method() != resolved_method()) { 1282 check_method_loader_constraints(link_info, sel_method, "method", CHECK); 1283 } 1284 } 1285 1286 // Check that the class of objectref (the receiver) is the current class or interface, 1287 // or a subtype of the current class or interface (the sender), otherwise invokespecial 1288 // throws IllegalAccessError. 1289 // The verifier checks that the sender is a subtype of the class in the I/MR operand. 1290 // The verifier also checks that the receiver is a subtype of the sender, if the sender is 1291 // a class. If the sender is an interface, the check has to be performed at runtime. 1292 InstanceKlass* sender = InstanceKlass::cast(current_klass); 1293 if (sender->is_interface() && recv.not_null()) { 1294 Klass* receiver_klass = recv->klass(); 1295 if (!receiver_klass->is_subtype_of(sender)) { 1296 ResourceMark rm(THREAD); 1297 char buf[500]; 1298 jio_snprintf(buf, sizeof(buf), 1299 "Receiver class %s must be the current class or a subtype of interface %s", 1300 receiver_klass->external_name(), 1301 sender->external_name()); 1302 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf); 1303 } 1304 } 1305 } 1306 1307 // check if not static 1308 if (sel_method->is_static()) { 1309 ResourceMark rm(THREAD); 1310 stringStream ss; 1311 ss.print("Expecting non-static method '"); 1312 resolved_method->print_external_name(&ss); 1313 ss.print("'"); 1314 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string()); 1315 } 1316 1317 // check if abstract 1318 if (sel_method->is_abstract()) { 1319 ResourceMark rm(THREAD); 1320 stringStream ss; 1321 ss.print("'"); 1322 Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature()); 1323 ss.print("'"); 1324 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string()); 1325 } 1326 1327 if (log_develop_is_enabled(Trace, itables)) { 1328 trace_method_resolution("invokespecial selected method: resolved-class:", 1329 resolved_klass, resolved_klass, sel_method(), true); 1330 } 1331 1332 // setup result 1333 result.set_static(resolved_klass, sel_method, CHECK); 1334 JFR_ONLY(Jfr::on_resolution(result, CHECK);) 1335 } 1336 1337 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass, 1338 const LinkInfo& link_info, 1339 bool check_null_and_abstract, TRAPS) { 1340 Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK); 1341 runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method), 1342 link_info.resolved_klass(), 1343 recv, receiver_klass, 1344 check_null_and_abstract, CHECK); 1345 } 1346 1347 // throws linktime exceptions 1348 Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info, 1349 TRAPS) { 1350 // normal method resolution 1351 Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL); 1352 1353 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1354 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1355 1356 // check if private interface method 1357 Klass* resolved_klass = link_info.resolved_klass(); 1358 Klass* current_klass = link_info.current_klass(); 1359 1360 // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method 1361 if (resolved_klass->is_interface() && resolved_method->is_private()) { 1362 ResourceMark rm(THREAD); 1363 stringStream ss; 1364 ss.print("private interface method requires invokespecial, not invokevirtual: method '"); 1365 resolved_method->print_external_name(&ss); 1366 ss.print("', caller-class: %s", 1367 (current_klass == nullptr ? "<null>" : current_klass->internal_name())); 1368 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string()); 1369 } 1370 1371 // check if not static 1372 if (resolved_method->is_static()) { 1373 ResourceMark rm(THREAD); 1374 stringStream ss; 1375 ss.print("Expecting non-static method '"); 1376 resolved_method->print_external_name(&ss); 1377 ss.print("'"); 1378 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string()); 1379 } 1380 1381 if (log_develop_is_enabled(Trace, vtables)) { 1382 trace_method_resolution("invokevirtual resolved method: caller-class:", 1383 current_klass, resolved_klass, resolved_method, false); 1384 } 1385 1386 return resolved_method; 1387 } 1388 1389 // throws runtime exceptions 1390 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result, 1391 const methodHandle& resolved_method, 1392 Klass* resolved_klass, 1393 Handle recv, 1394 Klass* recv_klass, 1395 bool check_null_and_abstract, 1396 TRAPS) { 1397 1398 // setup default return values 1399 int vtable_index = Method::invalid_vtable_index; 1400 methodHandle selected_method; 1401 1402 // runtime method resolution 1403 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists 1404 THROW(vmSymbols::java_lang_NullPointerException()); 1405 } 1406 1407 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s 1408 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since 1409 // a missing receiver might result in a bogus lookup. 1410 assert(resolved_method->method_holder()->is_linked(), "must be linked"); 1411 1412 // do lookup based on receiver klass using the vtable index 1413 if (resolved_method->method_holder()->is_interface()) { // default or miranda method 1414 vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method); 1415 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); 1416 1417 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index)); 1418 } else { 1419 // at this point we are sure that resolved_method is virtual and not 1420 // a default or miranda method; therefore, it must have a valid vtable index. 1421 assert(!resolved_method->has_itable_index(), ""); 1422 vtable_index = resolved_method->vtable_index(); 1423 // We could get a negative vtable_index of nonvirtual_vtable_index for private 1424 // methods, or for final methods. Private methods never appear in the vtable 1425 // and never override other methods. As an optimization, final methods are 1426 // never put in the vtable, unless they override an existing method. 1427 // So if we do get nonvirtual_vtable_index, it means the selected method is the 1428 // resolved method, and it can never be changed by an override. 1429 if (vtable_index == Method::nonvirtual_vtable_index) { 1430 assert(resolved_method->can_be_statically_bound(), "cannot override this method"); 1431 selected_method = resolved_method; 1432 } else { 1433 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index)); 1434 } 1435 } 1436 1437 // check if method exists 1438 if (selected_method.is_null()) { 1439 throw_abstract_method_error(resolved_method, recv_klass, CHECK); 1440 } 1441 1442 // check if abstract 1443 if (check_null_and_abstract && selected_method->is_abstract()) { 1444 // Pass arguments for generating a verbose error message. 1445 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK); 1446 } 1447 1448 if (log_develop_is_enabled(Trace, vtables)) { 1449 trace_method_resolution("invokevirtual selected method: receiver-class:", 1450 recv_klass, resolved_klass, selected_method(), 1451 false, vtable_index); 1452 } 1453 // setup result 1454 result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK); 1455 JFR_ONLY(Jfr::on_resolution(result, CHECK);) 1456 } 1457 1458 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass, 1459 const LinkInfo& link_info, 1460 bool check_null_and_abstract, TRAPS) { 1461 // throws linktime exceptions 1462 Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK); 1463 methodHandle mh(THREAD, resolved_method); 1464 runtime_resolve_interface_method(result, mh, link_info.resolved_klass(), 1465 recv, recv_klass, check_null_and_abstract, CHECK); 1466 } 1467 1468 Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info, 1469 TRAPS) { 1470 // normal interface method resolution 1471 Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL); 1472 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1473 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1474 1475 return resolved_method; 1476 } 1477 1478 // throws runtime exceptions 1479 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, 1480 const methodHandle& resolved_method, 1481 Klass* resolved_klass, 1482 Handle recv, 1483 Klass* recv_klass, 1484 bool check_null_and_abstract, TRAPS) { 1485 1486 // check if receiver exists 1487 if (check_null_and_abstract && recv.is_null()) { 1488 THROW(vmSymbols::java_lang_NullPointerException()); 1489 } 1490 1491 // check if receiver klass implements the resolved interface 1492 if (!recv_klass->is_subtype_of(resolved_klass)) { 1493 ResourceMark rm(THREAD); 1494 char buf[200]; 1495 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s", 1496 recv_klass->external_name(), 1497 resolved_klass->external_name()); 1498 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1499 } 1500 1501 methodHandle selected_method = resolved_method; 1502 1503 // resolve the method in the receiver class, unless it is private 1504 if (!resolved_method()->is_private()) { 1505 // do lookup based on receiver klass 1506 // This search must match the linktime preparation search for itable initialization 1507 // to correctly enforce loader constraints for interface method inheritance. 1508 // Private methods are skipped as the resolved method was not private. 1509 Method* method = lookup_instance_method_in_klasses(recv_klass, 1510 resolved_method->name(), 1511 resolved_method->signature(), 1512 Klass::PrivateLookupMode::skip); 1513 selected_method = methodHandle(THREAD, method); 1514 1515 if (selected_method.is_null() && !check_null_and_abstract) { 1516 // In theory this is a harmless placeholder value, but 1517 // in practice leaving in null affects the nsk default method tests. 1518 // This needs further study. 1519 selected_method = resolved_method; 1520 } 1521 // check if method exists 1522 if (selected_method.is_null()) { 1523 // Pass arguments for generating a verbose error message. 1524 throw_abstract_method_error(resolved_method, recv_klass, CHECK); 1525 } 1526 // check access 1527 // Throw Illegal Access Error if selected_method is not public. 1528 if (!selected_method->is_public()) { 1529 ResourceMark rm(THREAD); 1530 stringStream ss; 1531 ss.print("'"); 1532 Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature()); 1533 ss.print("'"); 1534 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); 1535 } 1536 // check if abstract 1537 if (check_null_and_abstract && selected_method->is_abstract()) { 1538 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK); 1539 } 1540 } 1541 1542 if (log_develop_is_enabled(Trace, itables)) { 1543 trace_method_resolution("invokeinterface selected method: receiver-class:", 1544 recv_klass, resolved_klass, selected_method(), true); 1545 } 1546 // setup result 1547 if (resolved_method->has_vtable_index()) { 1548 int vtable_index = resolved_method->vtable_index(); 1549 log_develop_trace(itables)(" -- vtable index: %d", vtable_index); 1550 assert(vtable_index == selected_method->vtable_index(), "sanity check"); 1551 result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK); 1552 } else if (resolved_method->has_itable_index()) { 1553 int itable_index = resolved_method()->itable_index(); 1554 log_develop_trace(itables)(" -- itable index: %d", itable_index); 1555 result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK); 1556 } else { 1557 int index = resolved_method->vtable_index(); 1558 log_develop_trace(itables)(" -- non itable/vtable index: %d", index); 1559 assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!"); 1560 assert(resolved_method()->is_private() || 1561 (resolved_method()->is_final() && resolved_method->method_holder() == vmClasses::Object_klass()), 1562 "Should only have non-virtual invokeinterface for private or final-Object methods!"); 1563 assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!"); 1564 // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods) 1565 result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK); 1566 } 1567 JFR_ONLY(Jfr::on_resolution(result, CHECK);) 1568 } 1569 1570 1571 Method* LinkResolver::linktime_resolve_interface_method_or_null( 1572 const LinkInfo& link_info) { 1573 EXCEPTION_MARK; 1574 Method* method_result = linktime_resolve_interface_method(link_info, THREAD); 1575 if (HAS_PENDING_EXCEPTION) { 1576 CLEAR_PENDING_EXCEPTION; 1577 return nullptr; 1578 } else { 1579 return method_result; 1580 } 1581 } 1582 1583 Method* LinkResolver::linktime_resolve_virtual_method_or_null( 1584 const LinkInfo& link_info) { 1585 EXCEPTION_MARK; 1586 Method* method_result = linktime_resolve_virtual_method(link_info, THREAD); 1587 if (HAS_PENDING_EXCEPTION) { 1588 CLEAR_PENDING_EXCEPTION; 1589 return nullptr; 1590 } else { 1591 return method_result; 1592 } 1593 } 1594 1595 Method* LinkResolver::resolve_virtual_call_or_null( 1596 Klass* receiver_klass, 1597 const LinkInfo& link_info) { 1598 EXCEPTION_MARK; 1599 CallInfo info; 1600 resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1601 if (HAS_PENDING_EXCEPTION) { 1602 CLEAR_PENDING_EXCEPTION; 1603 return nullptr; 1604 } 1605 return info.selected_method(); 1606 } 1607 1608 Method* LinkResolver::resolve_interface_call_or_null( 1609 Klass* receiver_klass, 1610 const LinkInfo& link_info) { 1611 EXCEPTION_MARK; 1612 CallInfo info; 1613 resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1614 if (HAS_PENDING_EXCEPTION) { 1615 CLEAR_PENDING_EXCEPTION; 1616 return nullptr; 1617 } 1618 return info.selected_method(); 1619 } 1620 1621 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass, 1622 const LinkInfo& link_info) { 1623 EXCEPTION_MARK; 1624 CallInfo info; 1625 resolve_virtual_call(info, Handle(), receiver_klass, link_info, 1626 /*check_null_or_abstract*/false, THREAD); 1627 if (HAS_PENDING_EXCEPTION) { 1628 CLEAR_PENDING_EXCEPTION; 1629 return Method::invalid_vtable_index; 1630 } 1631 return info.vtable_index(); 1632 } 1633 1634 Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) { 1635 EXCEPTION_MARK; 1636 CallInfo info; 1637 resolve_static_call(info, link_info, /*initialize_class*/false, THREAD); 1638 if (HAS_PENDING_EXCEPTION) { 1639 CLEAR_PENDING_EXCEPTION; 1640 return nullptr; 1641 } 1642 return info.selected_method(); 1643 } 1644 1645 Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) { 1646 EXCEPTION_MARK; 1647 CallInfo info; 1648 resolve_special_call(info, Handle(), link_info, THREAD); 1649 if (HAS_PENDING_EXCEPTION) { 1650 CLEAR_PENDING_EXCEPTION; 1651 return nullptr; 1652 } 1653 return info.selected_method(); 1654 } 1655 1656 1657 1658 //------------------------------------------------------------------------------------------------------------------------ 1659 // ConstantPool entries 1660 1661 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) { 1662 switch (byte) { 1663 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break; 1664 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break; 1665 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break; 1666 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break; 1667 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break; 1668 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break; 1669 default : break; 1670 } 1671 return; 1672 } 1673 1674 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv, 1675 const methodHandle& attached_method, 1676 Bytecodes::Code byte, bool check_null_and_abstract, TRAPS) { 1677 Klass* defc = attached_method->method_holder(); 1678 Symbol* name = attached_method->name(); 1679 Symbol* type = attached_method->signature(); 1680 LinkInfo link_info(defc, name, type); 1681 Klass* recv_klass = recv.is_null() ? defc : recv->klass(); 1682 switch(byte) { 1683 case Bytecodes::_invokevirtual: 1684 resolve_virtual_call(result, recv, recv_klass, link_info, 1685 check_null_and_abstract, CHECK); 1686 break; 1687 case Bytecodes::_invokeinterface: 1688 resolve_interface_call(result, recv, recv_klass, link_info, 1689 check_null_and_abstract, CHECK); 1690 break; 1691 case Bytecodes::_invokestatic: 1692 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK); 1693 break; 1694 case Bytecodes::_invokespecial: 1695 resolve_special_call(result, recv, link_info, CHECK); 1696 break; 1697 default: 1698 fatal("bad call: %s", Bytecodes::name(byte)); 1699 break; 1700 } 1701 } 1702 1703 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1704 LinkInfo link_info(pool, index, Bytecodes::_invokestatic, CHECK); 1705 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK); 1706 } 1707 1708 1709 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv, 1710 const constantPoolHandle& pool, int index, TRAPS) { 1711 LinkInfo link_info(pool, index, Bytecodes::_invokespecial, CHECK); 1712 resolve_special_call(result, recv, link_info, CHECK); 1713 } 1714 1715 1716 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv, 1717 const constantPoolHandle& pool, int index, 1718 TRAPS) { 1719 1720 LinkInfo link_info(pool, index, Bytecodes::_invokevirtual, CHECK); 1721 Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass(); 1722 resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK); 1723 } 1724 1725 1726 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) { 1727 LinkInfo link_info(pool, index, Bytecodes::_invokeinterface, CHECK); 1728 Klass* recvrKlass = recv.is_null() ? (Klass*)nullptr : recv->klass(); 1729 resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK); 1730 } 1731 1732 bool LinkResolver::resolve_previously_linked_invokehandle(CallInfo& result, const LinkInfo& link_info, const constantPoolHandle& pool, int index, TRAPS) { 1733 int cache_index = ConstantPool::decode_cpcache_index(index, true); 1734 ConstantPoolCacheEntry* cpce = pool->cache()->entry_at(cache_index); 1735 if (!cpce->is_f1_null()) { 1736 Klass* resolved_klass = link_info.resolved_klass(); 1737 methodHandle method(THREAD, cpce->f1_as_method()); 1738 Handle appendix(THREAD, cpce->appendix_if_resolved(pool)); 1739 result.set_handle(resolved_klass, method, appendix, CHECK_false); 1740 JFR_ONLY(Jfr::on_resolution(result, CHECK_false);) 1741 return true; 1742 } else { 1743 return false; 1744 } 1745 } 1746 1747 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1748 LinkInfo link_info(pool, index, Bytecodes::_invokehandle, CHECK); 1749 if (log_is_enabled(Info, methodhandles)) { 1750 ResourceMark rm(THREAD); 1751 log_info(methodhandles)("resolve_invokehandle %s %s", link_info.name()->as_C_string(), 1752 link_info.signature()->as_C_string()); 1753 } 1754 { // Check if the call site has been bound already, and short circuit: 1755 bool is_done = resolve_previously_linked_invokehandle(result, link_info, pool, index, CHECK); 1756 if (is_done) return; 1757 } 1758 resolve_handle_call(result, link_info, CHECK); 1759 } 1760 1761 void LinkResolver::resolve_handle_call(CallInfo& result, 1762 const LinkInfo& link_info, 1763 TRAPS) { 1764 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar 1765 Klass* resolved_klass = link_info.resolved_klass(); 1766 assert(resolved_klass == vmClasses::MethodHandle_klass() || 1767 resolved_klass == vmClasses::VarHandle_klass(), ""); 1768 assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), ""); 1769 Handle resolved_appendix; 1770 Method* m = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK); 1771 methodHandle resolved_method(THREAD, m); 1772 1773 if (link_info.check_access()) { 1774 Symbol* name = link_info.name(); 1775 vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name); 1776 if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) { 1777 // Check if method can be accessed by the referring class. 1778 // MH.linkTo* invocations are not rewritten to invokehandle. 1779 assert(iid == vmIntrinsicID::_invokeBasic, "%s", vmIntrinsics::name_at(iid)); 1780 1781 Klass* current_klass = link_info.current_klass(); 1782 assert(current_klass != nullptr , "current_klass should not be null"); 1783 check_method_accessability(current_klass, 1784 resolved_klass, 1785 resolved_method->method_holder(), 1786 resolved_method, 1787 CHECK); 1788 } else { 1789 // Java code is free to arbitrarily link signature-polymorphic invokers. 1790 assert(iid == vmIntrinsics::_invokeGeneric, "not an invoker: %s", vmIntrinsics::name_at(iid)); 1791 assert(MethodHandles::is_signature_polymorphic_public_name(resolved_klass, name), "not public"); 1792 } 1793 } 1794 result.set_handle(resolved_klass, resolved_method, resolved_appendix, CHECK); 1795 JFR_ONLY(Jfr::on_resolution(result, CHECK);) 1796 } 1797 1798 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) { 1799 int index = pool->decode_invokedynamic_index(indy_index); 1800 int pool_index = pool->resolved_indy_entry_at(index)->constant_pool_index(); 1801 1802 // Resolve the bootstrap specifier (BSM + optional arguments). 1803 BootstrapInfo bootstrap_specifier(pool, pool_index, index); 1804 1805 // Check if CallSite has been bound already or failed already, and short circuit: 1806 { 1807 bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK); 1808 if (is_done) return; 1809 } 1810 1811 // The initial step in Call Site Specifier Resolution is to resolve the symbolic 1812 // reference to a method handle which will be the bootstrap method for a dynamic 1813 // call site. If resolution for the java.lang.invoke.MethodHandle for the bootstrap 1814 // method fails, then a MethodHandleInError is stored at the corresponding bootstrap 1815 // method's CP index for the CONSTANT_MethodHandle_info. 1816 // Any subsequent invokedynamic instruction which shares 1817 // this bootstrap method will encounter the resolution of MethodHandleInError. 1818 1819 resolve_dynamic_call(result, bootstrap_specifier, CHECK); 1820 1821 LogTarget(Debug, methodhandles, indy) lt_indy; 1822 if (lt_indy.is_enabled()) { 1823 LogStream ls(lt_indy); 1824 bootstrap_specifier.print_msg_on(&ls, "resolve_invokedynamic"); 1825 } 1826 1827 // The returned linkage result is provisional up to the moment 1828 // the interpreter or runtime performs a serialized check of 1829 // the relevant ResolvedIndyEntry::method field. This is done by the caller 1830 // of this method, via CPC::set_dynamic_call, which uses 1831 // a lock to do the final serialization of updates 1832 // to ResolvedIndyEntry state, including method. 1833 1834 // Log dynamic info to CDS classlist. 1835 ArchiveUtils::log_to_classlist(&bootstrap_specifier, CHECK); 1836 } 1837 1838 void LinkResolver::resolve_dynamic_call(CallInfo& result, 1839 BootstrapInfo& bootstrap_specifier, 1840 TRAPS) { 1841 // JSR 292: this must resolve to an implicitly generated method 1842 // such as MH.linkToCallSite(*...) or some other call-site shape. 1843 // The appendix argument is likely to be a freshly-created CallSite. 1844 // It may also be a MethodHandle from an unwrapped ConstantCallSite, 1845 // or any other reference. The resolved_method as well as the appendix 1846 // are both recorded together via CallInfo::set_handle. 1847 SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD); 1848 Exceptions::wrap_dynamic_exception(/* is_indy */ true, THREAD); 1849 1850 if (HAS_PENDING_EXCEPTION) { 1851 if (!PENDING_EXCEPTION->is_a(vmClasses::LinkageError_klass())) { 1852 // Let any random low-level IE or SOE or OOME just bleed through. 1853 // Basically we pretend that the bootstrap method was never called, 1854 // if it fails this way: We neither record a successful linkage, 1855 // nor do we memorize a LE for posterity. 1856 return; 1857 } 1858 // JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve 1859 // a symbolic reference fails because an error is thrown that is an 1860 // instance of LinkageError (or a subclass), then subsequent attempts to 1861 // resolve the reference always fail with the same error that was thrown 1862 // as a result of the initial resolution attempt. 1863 bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK); 1864 if (!recorded_res_status) { 1865 // Another thread got here just before we did. So, either use the method 1866 // that it resolved or throw the LinkageError exception that it threw. 1867 bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK); 1868 if (is_done) return; 1869 } 1870 assert(bootstrap_specifier.pool()->resolved_indy_entry_at(bootstrap_specifier.indy_index())->resolution_failed(), 1871 "Resolution should have failed"); 1872 } 1873 1874 bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK); 1875 // Exceptions::wrap_dynamic_exception not used because 1876 // set_handle doesn't throw linkage errors 1877 JFR_ONLY(Jfr::on_resolution(result, CHECK);) 1878 } 1879 1880 // Selected method is abstract. 1881 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method, 1882 const methodHandle& selected_method, 1883 Klass *recv_klass, TRAPS) { 1884 Klass *resolved_klass = resolved_method->method_holder(); 1885 ResourceMark rm(THREAD); 1886 stringStream ss; 1887 1888 if (recv_klass != nullptr) { 1889 ss.print("Receiver class %s does not define or inherit an " 1890 "implementation of the", 1891 recv_klass->external_name()); 1892 } else { 1893 ss.print("Missing implementation of"); 1894 } 1895 1896 assert(resolved_method.not_null(), "Sanity"); 1897 ss.print(" resolved method '%s%s", 1898 resolved_method->is_abstract() ? "abstract " : "", 1899 resolved_method->is_private() ? "private " : ""); 1900 resolved_method->signature()->print_as_signature_external_return_type(&ss); 1901 ss.print(" %s(", resolved_method->name()->as_C_string()); 1902 resolved_method->signature()->print_as_signature_external_parameters(&ss); 1903 ss.print(")' of %s %s.", 1904 resolved_klass->external_kind(), 1905 resolved_klass->external_name()); 1906 1907 if (selected_method.not_null() && !(resolved_method == selected_method)) { 1908 ss.print(" Selected method is '%s%s", 1909 selected_method->is_abstract() ? "abstract " : "", 1910 selected_method->is_private() ? "private " : ""); 1911 selected_method->print_external_name(&ss); 1912 ss.print("'."); 1913 } 1914 1915 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string()); 1916 }