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