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