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