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