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