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 "logging/log.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 _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 // 2. lookup method in resolved klass and its super klasses 686 methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL); 687 688 if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy 689 // 3. lookup method in all the interfaces implemented by the resolved klass 690 resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL); 691 692 if (resolved_method.is_null()) { 693 // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc 694 resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD); 695 if (HAS_PENDING_EXCEPTION) { 696 nested_exception = Handle(THREAD, PENDING_EXCEPTION); 697 CLEAR_PENDING_EXCEPTION; 698 } 699 } 700 } 701 702 if (resolved_method.is_null()) { 703 // 4. method lookup failed 704 ResourceMark rm(THREAD); 705 THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(), 706 Method::name_and_sig_as_C_string(resolved_klass(), 707 link_info.name(), 708 link_info.signature()), 709 nested_exception, NULL); 710 } 711 712 // 5. access checks, access checking may be turned off when calling from within the VM. 713 KlassHandle current_klass = link_info.current_klass(); 714 if (link_info.check_access()) { 715 assert(current_klass.not_null() , "current_klass should not be null"); 716 717 // check if method can be accessed by the referring class 718 check_method_accessability(current_klass, 719 resolved_klass, 720 KlassHandle(THREAD, resolved_method->method_holder()), 721 resolved_method, 722 CHECK_NULL); 723 724 // check loader constraints 725 check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL); 726 } 727 728 return resolved_method; 729 } 730 731 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, 732 bool nostatics, TRAPS) { 733 734 KlassHandle resolved_klass = link_info.resolved_klass(); 735 736 // check if klass is interface 737 if (!resolved_klass->is_interface()) { 738 ResourceMark rm(THREAD); 739 char buf[200]; 740 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name()); 741 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 742 } 743 744 // lookup method in this interface or its super, java.lang.Object 745 // JDK8: also look for static methods 746 methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL); 747 748 if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { 749 // lookup method in all the super-interfaces 750 resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL); 751 } 752 753 if (resolved_method.is_null()) { 754 // no method found 755 ResourceMark rm(THREAD); 756 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), 757 Method::name_and_sig_as_C_string(resolved_klass(), 758 link_info.name(), 759 link_info.signature())); 760 } 761 762 if (link_info.check_access()) { 763 // JDK8 adds non-public interface methods, and accessability check requirement 764 KlassHandle current_klass = link_info.current_klass(); 765 766 assert(current_klass.not_null() , "current_klass should not be null"); 767 768 // check if method can be accessed by the referring class 769 check_method_accessability(current_klass, 770 resolved_klass, 771 KlassHandle(THREAD, resolved_method->method_holder()), 772 resolved_method, 773 CHECK_NULL); 774 775 check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL); 776 } 777 778 if (nostatics && resolved_method->is_static()) { 779 ResourceMark rm(THREAD); 780 char buf[200]; 781 jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s", 782 Method::name_and_sig_as_C_string(resolved_klass(), 783 resolved_method->name(), resolved_method->signature())); 784 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 785 } 786 787 if (develop_log_is_enabled(Trace, itables)) { 788 ResourceMark rm; 789 outputStream* logst = LogHandle(itables)::trace_stream(); 790 trace_method_resolution(logst, "invokeinterface resolved method: caller-class", 791 link_info.current_klass(), resolved_klass, resolved_method); 792 logst->cr(); 793 } 794 795 return resolved_method; 796 } 797 798 //------------------------------------------------------------------------------------------------------------------------ 799 // Field resolution 800 801 void LinkResolver::check_field_accessability(KlassHandle ref_klass, 802 KlassHandle resolved_klass, 803 KlassHandle sel_klass, 804 const fieldDescriptor& fd, 805 TRAPS) { 806 if (!Reflection::verify_field_access(ref_klass(), 807 resolved_klass(), 808 sel_klass(), 809 fd.access_flags(), 810 true)) { 811 ResourceMark rm(THREAD); 812 Exceptions::fthrow( 813 THREAD_AND_LOCATION, 814 vmSymbols::java_lang_IllegalAccessError(), 815 "tried to access field %s.%s from class %s", 816 sel_klass->external_name(), 817 fd.name()->as_C_string(), 818 ref_klass->external_name() 819 ); 820 return; 821 } 822 } 823 824 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) { 825 LinkInfo link_info(pool, index, CHECK); 826 resolve_field(fd, link_info, byte, true, CHECK); 827 } 828 829 void LinkResolver::resolve_field(fieldDescriptor& fd, 830 const LinkInfo& link_info, 831 Bytecodes::Code byte, bool initialize_class, 832 TRAPS) { 833 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || 834 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield || 835 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield || 836 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode"); 837 838 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); 839 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield); 840 // Check if there's a resolved klass containing the field 841 KlassHandle resolved_klass = link_info.resolved_klass(); 842 Symbol* field = link_info.name(); 843 Symbol* sig = link_info.signature(); 844 845 if (resolved_klass.is_null()) { 846 ResourceMark rm(THREAD); 847 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 848 } 849 850 // Resolve instance field 851 KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd)); 852 // check if field exists; i.e., if a klass containing the field def has been selected 853 if (sel_klass.is_null()) { 854 ResourceMark rm(THREAD); 855 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 856 } 857 858 if (!link_info.check_access()) 859 // Access checking may be turned off when calling from within the VM. 860 return; 861 862 // check access 863 KlassHandle current_klass = link_info.current_klass(); 864 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); 865 866 // check for errors 867 if (is_static != fd.is_static()) { 868 ResourceMark rm(THREAD); 869 char msg[200]; 870 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string()); 871 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); 872 } 873 874 // Final fields can only be accessed from its own class. 875 if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) { 876 THROW(vmSymbols::java_lang_IllegalAccessError()); 877 } 878 879 // initialize resolved_klass if necessary 880 // note 1: the klass which declared the field must be initialized (i.e, sel_klass) 881 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) 882 // 883 // note 2: we don't want to force initialization if we are just checking 884 // if the field access is legal; e.g., during compilation 885 if (is_static && initialize_class) { 886 sel_klass->initialize(CHECK); 887 } 888 889 if (sel_klass() != current_klass()) { 890 check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK); 891 } 892 893 // return information. note that the klass is set to the actual klass containing the 894 // field, otherwise access of static fields in superclasses will not work. 895 } 896 897 898 //------------------------------------------------------------------------------------------------------------------------ 899 // Invoke resolution 900 // 901 // Naming conventions: 902 // 903 // resolved_method the specified method (i.e., static receiver specified via constant pool index) 904 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class) 905 // resolved_klass the specified klass (i.e., specified via constant pool index) 906 // recv_klass the receiver klass 907 908 909 void LinkResolver::resolve_static_call(CallInfo& result, 910 const LinkInfo& link_info, 911 bool initialize_class, TRAPS) { 912 methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK); 913 914 // The resolved class can change as a result of this resolution. 915 KlassHandle resolved_klass = KlassHandle(THREAD, resolved_method->method_holder()); 916 917 Method* save_resolved_method = resolved_method(); 918 // Initialize klass (this should only happen if everything is ok) 919 if (initialize_class && resolved_klass->should_be_initialized()) { 920 resolved_klass->initialize(CHECK); 921 // Use updated LinkInfo (to reresolve with resolved_klass as method_holder?) 922 LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(), 923 link_info.current_klass(), link_info.check_access()); 924 resolved_method = linktime_resolve_static_method(new_info, CHECK); 925 } 926 927 assert(save_resolved_method == resolved_method(), "does this change?"); 928 // setup result 929 result.set_static(resolved_klass, resolved_method, CHECK); 930 } 931 932 // throws linktime exceptions 933 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) { 934 935 KlassHandle resolved_klass = link_info.resolved_klass(); 936 methodHandle resolved_method; 937 if (!resolved_klass->is_interface()) { 938 resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL); 939 } else { 940 resolved_method = resolve_interface_method(link_info, /*nostatics*/false, CHECK_NULL); 941 } 942 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier"); 943 944 // check if static 945 if (!resolved_method->is_static()) { 946 ResourceMark rm(THREAD); 947 char buf[200]; 948 jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(), 949 resolved_method->name(), 950 resolved_method->signature())); 951 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 952 } 953 return resolved_method; 954 } 955 956 957 void LinkResolver::resolve_special_call(CallInfo& result, 958 const LinkInfo& link_info, 959 TRAPS) { 960 methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK); 961 runtime_resolve_special_method(result, resolved_method, 962 link_info.resolved_klass(), 963 link_info.current_klass(), 964 link_info.check_access(), CHECK); 965 } 966 967 // throws linktime exceptions 968 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, 969 TRAPS) { 970 971 // Invokespecial is called for multiple special reasons: 972 // <init> 973 // local private method invocation, for classes and interfaces 974 // superclass.method, which can also resolve to a default method 975 // and the selected method is recalculated relative to the direct superclass 976 // superinterface.method, which explicitly does not check shadowing 977 KlassHandle resolved_klass = link_info.resolved_klass(); 978 methodHandle resolved_method; 979 980 if (!resolved_klass->is_interface()) { 981 resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL); 982 } else { 983 resolved_method = resolve_interface_method(link_info, /*nostatics*/true, CHECK_NULL); 984 } 985 986 // check if method name is <init>, that it is found in same klass as static type 987 if (resolved_method->name() == vmSymbols::object_initializer_name() && 988 resolved_method->method_holder() != resolved_klass()) { 989 ResourceMark rm(THREAD); 990 Exceptions::fthrow( 991 THREAD_AND_LOCATION, 992 vmSymbols::java_lang_NoSuchMethodError(), 993 "%s: method %s%s not found", 994 resolved_klass->external_name(), 995 resolved_method->name()->as_C_string(), 996 resolved_method->signature()->as_C_string() 997 ); 998 return NULL; 999 } 1000 1001 // check if invokespecial's interface method reference is in an indirect superinterface 1002 KlassHandle current_klass = link_info.current_klass(); 1003 if (!current_klass.is_null() && resolved_klass->is_interface()) { 1004 Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ? 1005 current_klass() : 1006 InstanceKlass::cast(current_klass())->host_klass(); 1007 // Disable verification for the dynamically-generated reflection bytecodes. 1008 bool is_reflect = klass_to_check->is_subclass_of( 1009 SystemDictionary::reflect_MagicAccessorImpl_klass()); 1010 1011 if (!is_reflect && 1012 !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) { 1013 ResourceMark rm(THREAD); 1014 char buf[200]; 1015 jio_snprintf(buf, sizeof(buf), 1016 "Interface method reference: %s, is in an indirect superinterface of %s", 1017 Method::name_and_sig_as_C_string(resolved_klass(), 1018 resolved_method->name(), 1019 resolved_method->signature()), 1020 current_klass->external_name()); 1021 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1022 } 1023 } 1024 1025 // check if not static 1026 if (resolved_method->is_static()) { 1027 ResourceMark rm(THREAD); 1028 char buf[200]; 1029 jio_snprintf(buf, sizeof(buf), 1030 "Expecting non-static method %s", 1031 Method::name_and_sig_as_C_string(resolved_klass(), 1032 resolved_method->name(), 1033 resolved_method->signature())); 1034 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1035 } 1036 1037 if (develop_log_is_enabled(Trace, itables)) { 1038 ResourceMark rm; 1039 outputStream* logst = LogHandle(itables)::trace_stream(); 1040 trace_method_resolution(logst, "invokespecial resolved method: caller-class:", 1041 current_klass, resolved_klass, resolved_method); 1042 logst->cr(); 1043 } 1044 1045 return resolved_method; 1046 } 1047 1048 // throws runtime exceptions 1049 void LinkResolver::runtime_resolve_special_method(CallInfo& result, 1050 const methodHandle& resolved_method, 1051 KlassHandle resolved_klass, 1052 KlassHandle current_klass, 1053 bool check_access, TRAPS) { 1054 1055 // resolved method is selected method unless we have an old-style lookup 1056 // for a superclass method 1057 // Invokespecial for a superinterface, resolved method is selected method, 1058 // no checks for shadowing 1059 methodHandle sel_method(THREAD, resolved_method()); 1060 1061 // check if this is an old-style super call and do a new lookup if so 1062 { KlassHandle method_klass = KlassHandle(THREAD, 1063 resolved_method->method_holder()); 1064 1065 if (check_access && 1066 // a) check if ACC_SUPER flag is set for the current class 1067 (current_klass->is_super() || !AllowNonVirtualCalls) && 1068 // b) check if the class of the resolved_klass is a superclass 1069 // (not supertype in order to exclude interface classes) of the current class. 1070 // This check is not performed for super.invoke for interface methods 1071 // in super interfaces. 1072 current_klass->is_subclass_of(resolved_klass()) && 1073 current_klass() != resolved_klass() && 1074 // c) check if the method is not <init> 1075 resolved_method->name() != vmSymbols::object_initializer_name()) { 1076 // Lookup super method 1077 KlassHandle super_klass(THREAD, current_klass->super()); 1078 sel_method = lookup_instance_method_in_klasses(super_klass, 1079 resolved_method->name(), 1080 resolved_method->signature(), CHECK); 1081 // check if found 1082 if (sel_method.is_null()) { 1083 ResourceMark rm(THREAD); 1084 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1085 Method::name_and_sig_as_C_string(resolved_klass(), 1086 resolved_method->name(), 1087 resolved_method->signature())); 1088 } 1089 } 1090 } 1091 1092 // check if not static 1093 if (sel_method->is_static()) { 1094 ResourceMark rm(THREAD); 1095 char buf[200]; 1096 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), 1097 resolved_method->name(), 1098 resolved_method->signature())); 1099 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1100 } 1101 1102 // check if abstract 1103 if (sel_method->is_abstract()) { 1104 ResourceMark rm(THREAD); 1105 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1106 Method::name_and_sig_as_C_string(resolved_klass(), 1107 sel_method->name(), 1108 sel_method->signature())); 1109 } 1110 1111 if (develop_log_is_enabled(Trace, itables)) { 1112 ResourceMark rm; 1113 outputStream* logst = LogHandle(itables)::trace_stream(); 1114 trace_method_resolution(logst, "invokespecial selected method: resolved-class:", 1115 resolved_klass, resolved_klass, sel_method); 1116 logst->cr(); 1117 } 1118 1119 // setup result 1120 result.set_static(resolved_klass, sel_method, CHECK); 1121 } 1122 1123 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, 1124 const LinkInfo& link_info, 1125 bool check_null_and_abstract, TRAPS) { 1126 methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK); 1127 runtime_resolve_virtual_method(result, resolved_method, 1128 link_info.resolved_klass(), 1129 recv, receiver_klass, 1130 check_null_and_abstract, CHECK); 1131 } 1132 1133 // throws linktime exceptions 1134 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info, 1135 TRAPS) { 1136 // normal method resolution 1137 methodHandle resolved_method = resolve_method(link_info, /*require_methodref*/true, CHECK_NULL); 1138 1139 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1140 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1141 1142 // check if private interface method 1143 KlassHandle resolved_klass = link_info.resolved_klass(); 1144 KlassHandle current_klass = link_info.current_klass(); 1145 1146 if (resolved_klass->is_interface() && resolved_method->is_private()) { 1147 ResourceMark rm(THREAD); 1148 char buf[200]; 1149 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s", 1150 Method::name_and_sig_as_C_string(resolved_klass(), 1151 resolved_method->name(), 1152 resolved_method->signature()), 1153 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name())); 1154 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1155 } 1156 1157 // check if not static 1158 if (resolved_method->is_static()) { 1159 ResourceMark rm(THREAD); 1160 char buf[200]; 1161 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), 1162 resolved_method->name(), 1163 resolved_method->signature())); 1164 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1165 } 1166 1167 if (develop_log_is_enabled(Trace, vtables)) { 1168 ResourceMark rm(THREAD); 1169 outputStream* logst = LogHandle(vtables)::trace_stream(); 1170 trace_method_resolution(logst, "invokevirtual resolved method: caller-class:", 1171 current_klass, resolved_klass, resolved_method); 1172 logst->cr(); 1173 } 1174 1175 return resolved_method; 1176 } 1177 1178 // throws runtime exceptions 1179 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result, 1180 const methodHandle& resolved_method, 1181 KlassHandle resolved_klass, 1182 Handle recv, 1183 KlassHandle recv_klass, 1184 bool check_null_and_abstract, 1185 TRAPS) { 1186 1187 // setup default return values 1188 int vtable_index = Method::invalid_vtable_index; 1189 methodHandle selected_method; 1190 1191 assert(recv.is_null() || recv->is_oop(), "receiver is not an oop"); 1192 1193 // runtime method resolution 1194 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists 1195 THROW(vmSymbols::java_lang_NullPointerException()); 1196 } 1197 1198 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s 1199 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since 1200 // a missing receiver might result in a bogus lookup. 1201 assert(resolved_method->method_holder()->is_linked(), "must be linked"); 1202 1203 // do lookup based on receiver klass using the vtable index 1204 if (resolved_method->method_holder()->is_interface()) { // default or miranda method 1205 vtable_index = vtable_index_of_interface_method(resolved_klass, 1206 resolved_method); 1207 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); 1208 1209 InstanceKlass* inst = InstanceKlass::cast(recv_klass()); 1210 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); 1211 } else { 1212 // at this point we are sure that resolved_method is virtual and not 1213 // a default or miranda method; therefore, it must have a valid vtable index. 1214 assert(!resolved_method->has_itable_index(), ""); 1215 vtable_index = resolved_method->vtable_index(); 1216 // We could get a negative vtable_index for final methods, 1217 // because as an optimization they are they are never put in the vtable, 1218 // unless they override an existing method. 1219 // If we do get a negative, it means the resolved method is the the selected 1220 // method, and it can never be changed by an override. 1221 if (vtable_index == Method::nonvirtual_vtable_index) { 1222 assert(resolved_method->can_be_statically_bound(), "cannot override this method"); 1223 selected_method = resolved_method; 1224 } else { 1225 // recv_klass might be an arrayKlassOop but all vtables start at 1226 // the same place. The cast is to avoid virtual call and assertion. 1227 InstanceKlass* inst = (InstanceKlass*)recv_klass(); 1228 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); 1229 } 1230 } 1231 1232 // check if method exists 1233 if (selected_method.is_null()) { 1234 ResourceMark rm(THREAD); 1235 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1236 Method::name_and_sig_as_C_string(resolved_klass(), 1237 resolved_method->name(), 1238 resolved_method->signature())); 1239 } 1240 1241 // check if abstract 1242 if (check_null_and_abstract && selected_method->is_abstract()) { 1243 ResourceMark rm(THREAD); 1244 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1245 Method::name_and_sig_as_C_string(resolved_klass(), 1246 selected_method->name(), 1247 selected_method->signature())); 1248 } 1249 1250 if (develop_log_is_enabled(Trace, vtables)) { 1251 ResourceMark rm; 1252 outputStream* logst = LogHandle(vtables)::trace_stream(); 1253 trace_method_resolution(logst, "invokevirtual selected method: receiver-class:", 1254 recv_klass, resolved_klass, selected_method); 1255 logst->print_cr("vtable_index:%d", vtable_index); 1256 } 1257 // setup result 1258 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK); 1259 } 1260 1261 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, 1262 const LinkInfo& link_info, 1263 bool check_null_and_abstract, TRAPS) { 1264 // throws linktime exceptions 1265 methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK); 1266 runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(), 1267 recv, recv_klass, check_null_and_abstract, CHECK); 1268 } 1269 1270 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info, 1271 TRAPS) { 1272 // normal interface method resolution 1273 methodHandle resolved_method = resolve_interface_method(link_info, true, CHECK_NULL); 1274 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1275 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1276 1277 return resolved_method; 1278 } 1279 1280 // throws runtime exceptions 1281 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, 1282 const methodHandle& resolved_method, 1283 KlassHandle resolved_klass, 1284 Handle recv, 1285 KlassHandle recv_klass, 1286 bool check_null_and_abstract, TRAPS) { 1287 // check if receiver exists 1288 if (check_null_and_abstract && recv.is_null()) { 1289 THROW(vmSymbols::java_lang_NullPointerException()); 1290 } 1291 1292 // check if private interface method 1293 if (resolved_klass->is_interface() && resolved_method->is_private()) { 1294 ResourceMark rm(THREAD); 1295 char buf[200]; 1296 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s", 1297 Method::name_and_sig_as_C_string(resolved_klass(), 1298 resolved_method->name(), 1299 resolved_method->signature())); 1300 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1301 } 1302 1303 // check if receiver klass implements the resolved interface 1304 if (!recv_klass->is_subtype_of(resolved_klass())) { 1305 ResourceMark rm(THREAD); 1306 char buf[200]; 1307 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s", 1308 recv_klass()->external_name(), 1309 resolved_klass()->external_name()); 1310 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1311 } 1312 1313 // do lookup based on receiver klass 1314 // This search must match the linktime preparation search for itable initialization 1315 // to correctly enforce loader constraints for interface method inheritance 1316 methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass, 1317 resolved_method->name(), 1318 resolved_method->signature(), CHECK); 1319 if (sel_method.is_null() && !check_null_and_abstract) { 1320 // In theory this is a harmless placeholder value, but 1321 // in practice leaving in null affects the nsk default method tests. 1322 // This needs further study. 1323 sel_method = resolved_method; 1324 } 1325 // check if method exists 1326 if (sel_method.is_null()) { 1327 ResourceMark rm(THREAD); 1328 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1329 Method::name_and_sig_as_C_string(recv_klass(), 1330 resolved_method->name(), 1331 resolved_method->signature())); 1332 } 1333 // check access 1334 // Throw Illegal Access Error if sel_method is not public. 1335 if (!sel_method->is_public()) { 1336 ResourceMark rm(THREAD); 1337 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), 1338 Method::name_and_sig_as_C_string(recv_klass(), 1339 sel_method->name(), 1340 sel_method->signature())); 1341 } 1342 // check if abstract 1343 if (check_null_and_abstract && sel_method->is_abstract()) { 1344 ResourceMark rm(THREAD); 1345 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1346 Method::name_and_sig_as_C_string(recv_klass(), 1347 sel_method->name(), 1348 sel_method->signature())); 1349 } 1350 1351 if (develop_log_is_enabled(Trace, itables)) { 1352 ResourceMark rm; 1353 outputStream* logst = LogHandle(itables)::trace_stream(); 1354 trace_method_resolution(logst, "invokeinterface selected method: receiver-class", 1355 recv_klass, resolved_klass, sel_method); 1356 logst->cr(); 1357 } 1358 // setup result 1359 if (!resolved_method->has_itable_index()) { 1360 int vtable_index = resolved_method->vtable_index(); 1361 assert(vtable_index == sel_method->vtable_index(), "sanity check"); 1362 result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK); 1363 } else { 1364 int itable_index = resolved_method()->itable_index(); 1365 result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK); 1366 } 1367 } 1368 1369 1370 methodHandle LinkResolver::linktime_resolve_interface_method_or_null( 1371 const LinkInfo& link_info) { 1372 EXCEPTION_MARK; 1373 methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD); 1374 if (HAS_PENDING_EXCEPTION) { 1375 CLEAR_PENDING_EXCEPTION; 1376 return methodHandle(); 1377 } else { 1378 return method_result; 1379 } 1380 } 1381 1382 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null( 1383 const LinkInfo& link_info) { 1384 EXCEPTION_MARK; 1385 methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD); 1386 if (HAS_PENDING_EXCEPTION) { 1387 CLEAR_PENDING_EXCEPTION; 1388 return methodHandle(); 1389 } else { 1390 return method_result; 1391 } 1392 } 1393 1394 methodHandle LinkResolver::resolve_virtual_call_or_null( 1395 KlassHandle receiver_klass, 1396 const LinkInfo& link_info) { 1397 EXCEPTION_MARK; 1398 CallInfo info; 1399 resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1400 if (HAS_PENDING_EXCEPTION) { 1401 CLEAR_PENDING_EXCEPTION; 1402 return methodHandle(); 1403 } 1404 return info.selected_method(); 1405 } 1406 1407 methodHandle LinkResolver::resolve_interface_call_or_null( 1408 KlassHandle receiver_klass, 1409 const LinkInfo& link_info) { 1410 EXCEPTION_MARK; 1411 CallInfo info; 1412 resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1413 if (HAS_PENDING_EXCEPTION) { 1414 CLEAR_PENDING_EXCEPTION; 1415 return methodHandle(); 1416 } 1417 return info.selected_method(); 1418 } 1419 1420 int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass, 1421 const LinkInfo& link_info) { 1422 EXCEPTION_MARK; 1423 CallInfo info; 1424 resolve_virtual_call(info, Handle(), receiver_klass, link_info, 1425 /*check_null_or_abstract*/false, THREAD); 1426 if (HAS_PENDING_EXCEPTION) { 1427 CLEAR_PENDING_EXCEPTION; 1428 return Method::invalid_vtable_index; 1429 } 1430 return info.vtable_index(); 1431 } 1432 1433 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) { 1434 EXCEPTION_MARK; 1435 CallInfo info; 1436 resolve_static_call(info, link_info, /*initialize_class*/false, THREAD); 1437 if (HAS_PENDING_EXCEPTION) { 1438 CLEAR_PENDING_EXCEPTION; 1439 return methodHandle(); 1440 } 1441 return info.selected_method(); 1442 } 1443 1444 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) { 1445 EXCEPTION_MARK; 1446 CallInfo info; 1447 resolve_special_call(info, link_info, THREAD); 1448 if (HAS_PENDING_EXCEPTION) { 1449 CLEAR_PENDING_EXCEPTION; 1450 return methodHandle(); 1451 } 1452 return info.selected_method(); 1453 } 1454 1455 1456 1457 //------------------------------------------------------------------------------------------------------------------------ 1458 // ConstantPool entries 1459 1460 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) { 1461 switch (byte) { 1462 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break; 1463 case Bytecodes::_invokespecial : resolve_invokespecial (result, pool, index, CHECK); break; 1464 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break; 1465 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break; 1466 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break; 1467 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break; 1468 } 1469 return; 1470 } 1471 1472 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1473 LinkInfo link_info(pool, index, CHECK); 1474 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK); 1475 } 1476 1477 1478 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1479 LinkInfo link_info(pool, index, CHECK); 1480 resolve_special_call(result, link_info, CHECK); 1481 } 1482 1483 1484 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv, 1485 const constantPoolHandle& pool, int index, 1486 TRAPS) { 1487 1488 LinkInfo link_info(pool, index, CHECK); 1489 KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); 1490 resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK); 1491 } 1492 1493 1494 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) { 1495 LinkInfo link_info(pool, index, CHECK); 1496 KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); 1497 resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK); 1498 } 1499 1500 1501 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1502 // This guy is reached from InterpreterRuntime::resolve_invokehandle. 1503 LinkInfo link_info(pool, index, CHECK); 1504 if (TraceMethodHandles) { 1505 ResourceMark rm(THREAD); 1506 tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(), 1507 link_info.signature()->as_C_string()); 1508 } 1509 resolve_handle_call(result, link_info, CHECK); 1510 } 1511 1512 void LinkResolver::resolve_handle_call(CallInfo& result, 1513 const LinkInfo& link_info, 1514 TRAPS) { 1515 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar 1516 assert(link_info.resolved_klass()() == SystemDictionary::MethodHandle_klass(), ""); 1517 assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), ""); 1518 Handle resolved_appendix; 1519 Handle resolved_method_type; 1520 methodHandle resolved_method = lookup_polymorphic_method(link_info, 1521 &resolved_appendix, &resolved_method_type, CHECK); 1522 result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK); 1523 } 1524 1525 static void wrap_invokedynamic_exception(TRAPS) { 1526 if (HAS_PENDING_EXCEPTION) { 1527 if (TraceMethodHandles) { 1528 tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION)); 1529 PENDING_EXCEPTION->print(); 1530 } 1531 if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) { 1532 // throw these guys, since they are already wrapped 1533 return; 1534 } 1535 if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { 1536 // intercept only LinkageErrors which might have failed to wrap 1537 return; 1538 } 1539 // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS. 1540 Handle nested_exception(THREAD, PENDING_EXCEPTION); 1541 CLEAR_PENDING_EXCEPTION; 1542 THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) 1543 } 1544 } 1545 1546 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1547 Symbol* method_name = pool->name_ref_at(index); 1548 Symbol* method_signature = pool->signature_ref_at(index); 1549 KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder()); 1550 1551 // Resolve the bootstrap specifier (BSM + optional arguments). 1552 Handle bootstrap_specifier; 1553 // Check if CallSite has been bound already: 1554 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index); 1555 if (cpce->is_f1_null()) { 1556 int pool_index = cpce->constant_pool_index(); 1557 oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD); 1558 wrap_invokedynamic_exception(CHECK); 1559 assert(bsm_info != NULL, ""); 1560 // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic. 1561 bootstrap_specifier = Handle(THREAD, bsm_info); 1562 } 1563 if (!cpce->is_f1_null()) { 1564 methodHandle method( THREAD, cpce->f1_as_method()); 1565 Handle appendix( THREAD, cpce->appendix_if_resolved(pool)); 1566 Handle method_type(THREAD, cpce->method_type_if_resolved(pool)); 1567 result.set_handle(method, appendix, method_type, THREAD); 1568 wrap_invokedynamic_exception(CHECK); 1569 return; 1570 } 1571 1572 if (TraceMethodHandles) { 1573 ResourceMark rm(THREAD); 1574 tty->print_cr("resolve_invokedynamic #%d %s %s", 1575 ConstantPool::decode_invokedynamic_index(index), 1576 method_name->as_C_string(), method_signature->as_C_string()); 1577 tty->print(" BSM info: "); bootstrap_specifier->print(); 1578 } 1579 1580 resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK); 1581 } 1582 1583 void LinkResolver::resolve_dynamic_call(CallInfo& result, 1584 Handle bootstrap_specifier, 1585 Symbol* method_name, Symbol* method_signature, 1586 KlassHandle current_klass, 1587 TRAPS) { 1588 // JSR 292: this must resolve to an implicitly generated method MH.linkToCallSite(*...) 1589 // The appendix argument is likely to be a freshly-created CallSite. 1590 Handle resolved_appendix; 1591 Handle resolved_method_type; 1592 methodHandle resolved_method = 1593 SystemDictionary::find_dynamic_call_site_invoker(current_klass, 1594 bootstrap_specifier, 1595 method_name, method_signature, 1596 &resolved_appendix, 1597 &resolved_method_type, 1598 THREAD); 1599 wrap_invokedynamic_exception(CHECK); 1600 result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD); 1601 wrap_invokedynamic_exception(CHECK); 1602 } 1603 1604 #ifndef PRODUCT 1605 // Must call with a ResourceMark 1606 void LinkResolver::trace_method_resolution(outputStream* st, 1607 const char* prefix, 1608 KlassHandle klass, 1609 KlassHandle resolved_klass, 1610 const methodHandle& method) { 1611 st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", 1612 prefix, 1613 (klass.is_null() ? "<NULL>" : klass->internal_name()), 1614 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), 1615 Method::name_and_sig_as_C_string(resolved_klass(), 1616 method->name(), 1617 method->signature()), 1618 method->method_holder()->internal_name() 1619 ); 1620 method->access_flags().print_on(st); 1621 if (method->is_default_method()) { 1622 st->print("default "); 1623 } 1624 if (method->is_overpass()) { 1625 st->print("overpass "); 1626 } 1627 } 1628 #endif // PRODUCT