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