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