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