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