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