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