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