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