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