1 /* 2 * Copyright (c) 1997, 2018, 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 "jvm.h" 27 #include "classfile/defaultMethods.hpp" 28 #include "classfile/javaClasses.inline.hpp" 29 #include "classfile/resolutionErrors.hpp" 30 #include "classfile/symbolTable.hpp" 31 #include "classfile/systemDictionary.hpp" 32 #include "classfile/vmSymbols.hpp" 33 #include "compiler/compileBroker.hpp" 34 #include "gc/shared/collectedHeap.inline.hpp" 35 #include "interpreter/bytecode.hpp" 36 #include "interpreter/interpreterRuntime.hpp" 37 #include "interpreter/linkResolver.hpp" 38 #include "logging/log.hpp" 39 #include "logging/logStream.hpp" 40 #include "memory/oopFactory.hpp" 41 #include "memory/resourceArea.hpp" 42 #include "memory/universe.hpp" 43 #include "oops/cpCache.inline.hpp" 44 #include "oops/instanceKlass.hpp" 45 #include "oops/method.hpp" 46 #include "oops/objArrayKlass.hpp" 47 #include "oops/objArrayOop.hpp" 48 #include "oops/oop.inline.hpp" 49 #include "prims/methodHandles.hpp" 50 #include "prims/nativeLookup.hpp" 51 #include "runtime/compilationPolicy.hpp" 52 #include "runtime/fieldDescriptor.inline.hpp" 53 #include "runtime/frame.inline.hpp" 54 #include "runtime/handles.inline.hpp" 55 #include "runtime/reflection.hpp" 56 #include "runtime/safepointVerifiers.hpp" 57 #include "runtime/signature.hpp" 58 #include "runtime/thread.inline.hpp" 59 #include "runtime/vmThread.hpp" 60 61 //------------------------------------------------------------------------------------------------------------------------ 62 // Implementation of CallInfo 63 64 65 void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) { 66 int vtable_index = Method::nonvirtual_vtable_index; 67 set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); 68 } 69 70 71 void CallInfo::set_interface(Klass* resolved_klass, 72 Klass* selected_klass, 73 const methodHandle& resolved_method, 74 const methodHandle& selected_method, 75 int itable_index, TRAPS) { 76 // This is only called for interface methods. If the resolved_method 77 // comes from java/lang/Object, it can be the subject of a virtual call, so 78 // we should pick the vtable index from the resolved method. 79 // In that case, the caller must call set_virtual instead of set_interface. 80 assert(resolved_method->method_holder()->is_interface(), ""); 81 assert(itable_index == resolved_method()->itable_index(), ""); 82 set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK); 83 } 84 85 void CallInfo::set_virtual(Klass* resolved_klass, 86 Klass* selected_klass, 87 const methodHandle& resolved_method, 88 const methodHandle& selected_method, 89 int vtable_index, TRAPS) { 90 assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index"); 91 assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), ""); 92 CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call); 93 set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK); 94 assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call"); 95 } 96 97 void CallInfo::set_handle(const methodHandle& resolved_method, 98 Handle resolved_appendix, 99 bool has_local_sig, TRAPS) { 100 set_handle(SystemDictionary::MethodHandle_klass(), resolved_method, resolved_appendix, has_local_sig, CHECK); 101 } 102 103 void CallInfo::set_handle(Klass* resolved_klass, 104 const methodHandle& resolved_method, 105 Handle resolved_appendix, 106 bool has_local_sig, TRAPS) { 107 guarantee(resolved_method.not_null(), "resolved method is null"); 108 assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic || 109 resolved_method->is_compiled_lambda_form(), 110 "linkMethod must return one of these"); 111 int vtable_index = Method::nonvirtual_vtable_index; 112 assert(!resolved_method->has_vtable_index(), ""); 113 set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); 114 _resolved_appendix = resolved_appendix; 115 _has_local_sig = has_local_sig; 116 assert(has_local_sig, "MHs and indy are always sig-poly"); 117 } 118 119 void CallInfo::set_common(Klass* resolved_klass, 120 Klass* selected_klass, 121 const methodHandle& resolved_method, 122 const methodHandle& selected_method, 123 CallKind kind, 124 int index, 125 TRAPS) { 126 assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond"); 127 _resolved_klass = resolved_klass; 128 _selected_klass = selected_klass; 129 _resolved_method = resolved_method; 130 _selected_method = selected_method; 131 _call_kind = kind; 132 _call_index = index; 133 _resolved_appendix = Handle(); 134 _has_local_sig = false; 135 DEBUG_ONLY(verify()); // verify before making side effects 136 137 CompilationPolicy::compile_if_required(selected_method, THREAD); 138 } 139 140 // utility query for unreflecting a method 141 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) { 142 Klass* resolved_method_holder = resolved_method->method_holder(); 143 if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st 144 resolved_klass = resolved_method_holder; 145 } 146 _resolved_klass = resolved_klass; 147 _selected_klass = resolved_klass; 148 _resolved_method = resolved_method; 149 _selected_method = resolved_method; 150 // classify: 151 CallKind kind = CallInfo::unknown_kind; 152 int index = resolved_method->vtable_index(); 153 if (resolved_method->can_be_statically_bound()) { 154 kind = CallInfo::direct_call; 155 } else if (!resolved_method_holder->is_interface()) { 156 // Could be an Object method inherited into an interface, but still a vtable call. 157 kind = CallInfo::vtable_call; 158 } else if (!resolved_klass->is_interface()) { 159 // A default or miranda method. Compute the vtable index. 160 index = LinkResolver::vtable_index_of_interface_method(resolved_klass, 161 resolved_method); 162 assert(index >= 0 , "we should have valid vtable index at this point"); 163 164 kind = CallInfo::vtable_call; 165 } else if (resolved_method->has_vtable_index()) { 166 // Can occur if an interface redeclares a method of Object. 167 168 #ifdef ASSERT 169 // Ensure that this is really the case. 170 Klass* object_klass = SystemDictionary::Object_klass(); 171 Method * object_resolved_method = object_klass->vtable().method_at(index); 172 assert(object_resolved_method->name() == resolved_method->name(), 173 "Object and interface method names should match at vtable index %d, %s != %s", 174 index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string()); 175 assert(object_resolved_method->signature() == resolved_method->signature(), 176 "Object and interface method signatures should match at vtable index %d, %s != %s", 177 index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string()); 178 #endif // ASSERT 179 180 kind = CallInfo::vtable_call; 181 } else { 182 // A regular interface call. 183 kind = CallInfo::itable_call; 184 index = resolved_method->itable_index(); 185 } 186 assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index); 187 _call_kind = kind; 188 _call_index = index; 189 _resolved_appendix = Handle(); 190 _has_local_sig = false; 191 // Find or create a ResolvedMethod instance for this Method* 192 set_resolved_method_name(CHECK); 193 194 DEBUG_ONLY(verify()); 195 } 196 197 void CallInfo::set_resolved_method_name(TRAPS) { 198 Method* m = _resolved_method(); 199 assert(m != NULL, "Should already have a Method*"); 200 oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(m, CHECK); 201 _resolved_method_name = Handle(THREAD, rmethod_name); 202 } 203 204 #ifdef ASSERT 205 void CallInfo::verify() { 206 switch (call_kind()) { // the meaning and allowed value of index depends on kind 207 case CallInfo::direct_call: 208 if (_call_index == Method::nonvirtual_vtable_index) break; 209 // else fall through to check vtable index: 210 case CallInfo::vtable_call: 211 assert(resolved_klass()->verify_vtable_index(_call_index), ""); 212 break; 213 case CallInfo::itable_call: 214 assert(resolved_method()->method_holder()->verify_itable_index(_call_index), ""); 215 break; 216 case CallInfo::unknown_kind: 217 assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set"); 218 break; 219 default: 220 fatal("Unexpected call kind %d", call_kind()); 221 } 222 } 223 #endif // ASSERT 224 225 #ifndef PRODUCT 226 void CallInfo::print() { 227 ResourceMark rm; 228 const char* kindstr; 229 switch (_call_kind) { 230 case direct_call: kindstr = "direct"; break; 231 case vtable_call: kindstr = "vtable"; break; 232 case itable_call: kindstr = "itable"; break; 233 default : kindstr = "unknown"; break; 234 } 235 tty->print_cr("Call %s@%d %s", kindstr, _call_index, 236 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string()); 237 } 238 #endif 239 240 //------------------------------------------------------------------------------------------------------------------------ 241 // Implementation of BootstrapInfo 242 243 BootstrapInfo::BootstrapInfo(const constantPoolHandle& pool, int bss_index, int indy_index) 244 : _pool(pool), 245 _bss_index(bss_index), 246 _indy_index(indy_index), 247 _is_method_call(indy_index != 0), 248 // derived and eagerly cached: 249 _argc( pool->bootstrap_argument_count_at(bss_index) ), 250 _name( pool->uncached_name_ref_at(bss_index) ), 251 _signature( pool->uncached_signature_ref_at(bss_index) ) 252 { 253 _is_resolved = false; 254 assert(pool->tag_at(bss_index).has_bootstrap(), ""); 255 assert(indy_index == 0 || pool->invokedynamic_bootstrap_ref_index_at(indy_index) == bss_index, ""); 256 } 257 258 oop BootstrapInfo::arg_value(int i) const { 259 // safely pull oop from _arg_values 260 if (_arg_values.is_null()) return NULL; 261 if (i < 0 || i >= _arg_values->length()) return NULL; 262 return _arg_values->obj_at(i); 263 } 264 265 bool BootstrapInfo::resolve_previously_linked_invokedynamic(CallInfo& result, TRAPS) { 266 assert(_indy_index != 0, ""); 267 ConstantPoolCacheEntry* cpce = invokedynamic_cp_cache_entry(); 268 if (!cpce->is_f1_null()) { 269 methodHandle method( THREAD, cpce->f1_as_method()); 270 Handle appendix( THREAD, cpce->appendix_if_resolved(_pool)); 271 result.set_handle(method, appendix, true, THREAD); 272 Exceptions::wrap_dynamic_exception(CHECK_false); 273 return true; 274 } else if (cpce->indy_resolution_failed()) { 275 int encoded_index = ResolutionErrorTable::encode_cpcache_index(_indy_index); 276 ConstantPool::throw_resolution_error(_pool, encoded_index, CHECK_false); 277 return true; 278 } else { 279 return false; 280 } 281 } 282 283 Handle BootstrapInfo::resolve_bsm(TRAPS) { 284 if (_bsm.not_null()) return _bsm; 285 // Unpack the BSM by resolving the MH constant. 286 assert(_pool->tag_at(bsm_index()).is_method_handle(), "classfile structural constraint"); 287 oop bsm_oop = _pool->resolve_possibly_cached_constant_at(bsm_index(), THREAD); 288 Exceptions::wrap_dynamic_exception(CHECK_(_bsm)); 289 guarantee(java_lang_invoke_MethodHandle::is_instance(bsm_oop), 290 "classfile must supply a valid BSM"); 291 _bsm = Handle(THREAD, bsm_oop); 292 return _bsm; 293 } 294 295 // What kind of BSM is it? 296 // - If the BSM takes exactly two arguments (second non-array), then mode=bsci. 297 // - Otherwise, if it's indy or there's a leading Lookup argument, then mode=metadata. 298 // - Otherwise, mode=expression. 299 enum BootstrapInfo::BSMMode BootstrapInfo::bsm_mode() const { 300 assert(_bsm.not_null(), "resolve_bsm first"); 301 if (_bsm.is_null()) return _unknown; 302 oop bsm_type = java_lang_invoke_MethodHandle::type(_bsm()); 303 int arity = java_lang_invoke_MethodType::ptype_count(bsm_type); 304 Klass* ptype0 = NULL; 305 Klass* ptype1 = NULL; 306 if (arity > 0) { 307 ptype0 = java_lang_Class::as_Klass(java_lang_invoke_MethodType::ptype(bsm_type, 0)); 308 if (arity > 1) { 309 ptype1 = java_lang_Class::as_Klass(java_lang_invoke_MethodType::ptype(bsm_type, 1)); 310 } 311 } 312 if (arity == 2 && ptype1 != NULL && !ptype1->is_array_klass()) 313 return _bsci; 314 if (_indy_index != 0 || (ptype0 != NULL && ptype0->name() == vmSymbols::java_lang_invoke_MethodHandles_Lookup())) 315 return _metadata; 316 return _expression; 317 } 318 319 void BootstrapInfo::resolve_metadata(TRAPS) { 320 resolve_bsm(CHECK); 321 Symbol* name = this->name(); 322 Symbol* type = this->signature(); 323 _name_arg = java_lang_String::create_from_symbol(name, CHECK); 324 if (type->byte_at(0) == '(') { 325 _type_arg = SystemDictionary::find_method_handle_type(type, caller(), THREAD); 326 } else { 327 _type_arg = SystemDictionary::find_java_mirror_for_type(type, caller(), SignatureStream::NCDFError, true, THREAD); 328 } 329 if (HAS_PENDING_EXCEPTION) { 330 bool continue_after_type_resolution_exception = false; 331 if (PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass()) && bsm_mode() == _bsci) 332 continue_after_type_resolution_exception = true; 333 // The type argument can fail to resolve, if the BSM is ready to handle it. 334 if (!continue_after_type_resolution_exception) { 335 return; // keep throwing 336 } 337 CLEAR_PENDING_EXCEPTION; 338 _type_arg = java_lang_String::create_from_symbol(type, CHECK); 339 } 340 assert(_type_arg.not_null(), ""); 341 } 342 void BootstrapInfo::resolve_args(ConstantPool::BootstrapArgumentReferenceMode resolving, TRAPS) { 343 arrayHandle buf; 344 if (resolving == ConstantPool::R_SYMREF) { 345 assert(_arg_indexes.is_null(), ""); 346 _arg_indexes = oopFactory::new_intArray_handle(_argc, CHECK); 347 buf = arrayHandle(THREAD, _arg_indexes()); 348 } else { 349 if (_arg_values.is_null()) { 350 _arg_values = oopFactory::new_objArray_handle(SystemDictionary::Object_klass(), _argc, CHECK); 351 } 352 buf = arrayHandle(THREAD, _arg_values()); 353 } 354 bool skip_recursion = (indy_index() == 0); 355 // if it's a condy call, leave it to the BSM to resolve sub-BSM calls 356 _pool->copy_bootstrap_arguments_at(_bss_index, 0, _argc, buf, 0, 357 resolving, Handle(), Handle(), 358 false, skip_recursion, CHECK); 359 } 360 361 // there must be a LinkageError pending; try to save it and then throw 362 bool BootstrapInfo::save_and_throw_indy_exc(TRAPS) { 363 assert(HAS_PENDING_EXCEPTION, ""); 364 assert(_indy_index != 0, ""); 365 ConstantPoolCacheEntry* cpce = invokedynamic_cp_cache_entry(); 366 int encoded_index = ResolutionErrorTable::encode_cpcache_index(_indy_index); 367 bool recorded_res_status = cpce->save_and_throw_indy_exc(_pool, _bss_index, 368 encoded_index, 369 pool()->tag_at(_bss_index), 370 CHECK_false); 371 return recorded_res_status; 372 } 373 374 void BootstrapInfo::resolve_newly_linked_invokedynamic(CallInfo& result, TRAPS) { 375 assert(is_resolved(), ""); 376 result.set_handle(resolved_method(), resolved_appendix(), true, THREAD); 377 } 378 379 void BootstrapInfo::print_msg_on(outputStream* st, const char* msg) { 380 ResourceMark rm; 381 char what[20]; 382 if (_indy_index != 0) 383 sprintf(what, "indy#%d", decode_indy_index()); 384 else 385 sprintf(what, "condy"); 386 bool have_msg = (msg != NULL && strlen(msg) > 0); 387 tty->print_cr("%s%sBootstrap in %s %s@CP[%d] %s:%s%s BSMS[%d] BSM@CP[%d]%s argc=%d%s", 388 (have_msg ? msg : ""), (have_msg ? " " : ""), 389 caller()->name()->as_C_string(), 390 what, // "indy#42" or "condy" 391 _bss_index, 392 _name->as_C_string(), 393 _signature->as_C_string(), 394 (_type_arg.is_null() ? "" : "(resolved)"), 395 bsms_attr_index(), 396 bsm_index(), (_bsm.is_null() ? "" : "(resolved)"), 397 _argc, (_arg_values.is_null() ? "" : "(resolved)")); 398 if (_argc > 0) { 399 char argbuf[80]; 400 argbuf[0] = 0; 401 for (int i = 0; i < _argc; i++) { 402 int pos = strlen(argbuf); 403 if (pos + 20 > (int)sizeof(argbuf)) { 404 sprintf(argbuf + pos, "..."); 405 break; 406 } 407 if (i > 0) argbuf[pos++] = ','; 408 sprintf(argbuf+pos, "%d", arg_index(i)); 409 } 410 tty->print_cr(" argument indexes: {%s}", argbuf); 411 } 412 if (_bsm.not_null()) { 413 tty->print(" resolved BSM: "); _bsm->print(); 414 } 415 if (_arg_values.not_null()) { 416 int lines = 0; 417 for (int i = 0; i < _argc; i++) { 418 oop x = arg_value(i); 419 if (x != NULL) { 420 if (++lines > 6) { 421 tty->print_cr(" resolved arg[%d]: ...", i); 422 break; 423 } 424 tty->print(" resolved arg[%d]: ", i); x->print(); 425 } 426 } 427 } 428 } 429 430 //------------------------------------------------------------------------------------------------------------------------ 431 // Implementation of LinkInfo 432 433 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS) { 434 // resolve klass 435 _resolved_klass = pool->klass_ref_at(index, CHECK); 436 437 // Get name, signature, and static klass 438 _name = pool->name_ref_at(index); 439 _signature = pool->signature_ref_at(index); 440 _tag = pool->tag_ref_at(index); 441 _current_klass = pool->pool_holder(); 442 _current_method = current_method; 443 444 // Coming from the constant pool always checks access 445 _check_access = true; 446 } 447 448 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) { 449 // resolve klass 450 _resolved_klass = pool->klass_ref_at(index, CHECK); 451 452 // Get name, signature, and static klass 453 _name = pool->name_ref_at(index); 454 _signature = pool->signature_ref_at(index); 455 _tag = pool->tag_ref_at(index); 456 _current_klass = pool->pool_holder(); 457 _current_method = methodHandle(); 458 459 // Coming from the constant pool always checks access 460 _check_access = true; 461 } 462 463 char* LinkInfo::method_string() const { 464 return Method::name_and_sig_as_C_string(_resolved_klass, _name, _signature); 465 } 466 467 #ifndef PRODUCT 468 void LinkInfo::print() { 469 ResourceMark rm; 470 tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s", 471 _resolved_klass->name()->as_C_string(), 472 _name->as_C_string(), 473 _signature->as_C_string(), 474 _current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(), 475 _check_access ? "true" : "false"); 476 } 477 #endif // PRODUCT 478 //------------------------------------------------------------------------------------------------------------------------ 479 // Klass resolution 480 481 void LinkResolver::check_klass_accessability(Klass* ref_klass, Klass* sel_klass, 482 bool fold_type_to_class, TRAPS) { 483 Klass* base_klass = sel_klass; 484 if (fold_type_to_class) { 485 if (sel_klass->is_objArray_klass()) { 486 base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass(); 487 } 488 // The element type could be a typeArray - we only need the access 489 // check if it is a reference to another class. 490 if (!base_klass->is_instance_klass()) { 491 return; // no relevant check to do 492 } 493 } 494 Reflection::VerifyClassAccessResults vca_result = 495 Reflection::verify_class_access(ref_klass, InstanceKlass::cast(base_klass), true); 496 if (vca_result != Reflection::ACCESS_OK) { 497 ResourceMark rm(THREAD); 498 char* msg = Reflection::verify_class_access_msg(ref_klass, 499 InstanceKlass::cast(base_klass), 500 vca_result); 501 bool same_module = (base_klass->module() == ref_klass->module()); 502 if (msg == NULL) { 503 Exceptions::fthrow( 504 THREAD_AND_LOCATION, 505 vmSymbols::java_lang_IllegalAccessError(), 506 "failed to access class %s from class %s (%s%s%s)", 507 base_klass->external_name(), 508 ref_klass->external_name(), 509 (same_module) ? base_klass->joint_in_module_of_loader(ref_klass) : base_klass->class_in_module_of_loader(), 510 (same_module) ? "" : "; ", 511 (same_module) ? "" : ref_klass->class_in_module_of_loader()); 512 } else { 513 // Use module specific message returned by verify_class_access_msg(). 514 Exceptions::fthrow( 515 THREAD_AND_LOCATION, 516 vmSymbols::java_lang_IllegalAccessError(), 517 "%s", msg); 518 } 519 } 520 } 521 522 //------------------------------------------------------------------------------------------------------------------------ 523 // Method resolution 524 // 525 // According to JVM spec. $5.4.3c & $5.4.3d 526 527 // Look up method in klasses, including static methods 528 // Then look up local default methods 529 Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info, 530 bool checkpolymorphism, 531 bool in_imethod_resolve) { 532 NoSafepointVerifier nsv; // Method* returned may not be reclaimed 533 534 Klass* klass = link_info.resolved_klass(); 535 Symbol* name = link_info.name(); 536 Symbol* signature = link_info.signature(); 537 538 // Ignore overpasses so statics can be found during resolution 539 Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass); 540 541 if (klass->is_array_klass()) { 542 // Only consider klass and super klass for arrays 543 return result; 544 } 545 546 InstanceKlass* ik = InstanceKlass::cast(klass); 547 548 // JDK 8, JVMS 5.4.3.4: Interface method resolution should 549 // ignore static and non-public methods of java.lang.Object, 550 // like clone, finalize, registerNatives. 551 if (in_imethod_resolve && 552 result != NULL && 553 ik->is_interface() && 554 (result->is_static() || !result->is_public()) && 555 result->method_holder() == SystemDictionary::Object_klass()) { 556 result = NULL; 557 } 558 559 // Before considering default methods, check for an overpass in the 560 // current class if a method has not been found. 561 if (result == NULL) { 562 result = ik->find_method(name, signature); 563 } 564 565 if (result == NULL) { 566 Array<Method*>* default_methods = ik->default_methods(); 567 if (default_methods != NULL) { 568 result = InstanceKlass::find_method(default_methods, name, signature); 569 } 570 } 571 572 if (checkpolymorphism && result != NULL) { 573 vmIntrinsics::ID iid = result->intrinsic_id(); 574 if (MethodHandles::is_signature_polymorphic(iid)) { 575 // Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method. 576 return NULL; 577 } 578 } 579 return result; 580 } 581 582 // returns first instance method 583 // Looks up method in classes, then looks up local default methods 584 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass, 585 Symbol* name, 586 Symbol* signature, 587 Klass::PrivateLookupMode private_mode, TRAPS) { 588 Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode); 589 590 while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) { 591 Klass* super_klass = result->method_holder()->super(); 592 result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode); 593 } 594 595 if (klass->is_array_klass()) { 596 // Only consider klass and super klass for arrays 597 return methodHandle(THREAD, result); 598 } 599 600 if (result == NULL) { 601 Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods(); 602 if (default_methods != NULL) { 603 result = InstanceKlass::find_method(default_methods, name, signature); 604 assert(result == NULL || !result->is_static(), "static defaults not allowed"); 605 } 606 } 607 return methodHandle(THREAD, result); 608 } 609 610 int LinkResolver::vtable_index_of_interface_method(Klass* klass, 611 const methodHandle& resolved_method) { 612 613 int vtable_index = Method::invalid_vtable_index; 614 Symbol* name = resolved_method->name(); 615 Symbol* signature = resolved_method->signature(); 616 InstanceKlass* ik = InstanceKlass::cast(klass); 617 618 // First check in default method array 619 if (!resolved_method->is_abstract() && ik->default_methods() != NULL) { 620 int index = InstanceKlass::find_method_index(ik->default_methods(), 621 name, signature, Klass::find_overpass, 622 Klass::find_static, Klass::find_private); 623 if (index >= 0 ) { 624 vtable_index = ik->default_vtable_indices()->at(index); 625 } 626 } 627 if (vtable_index == Method::invalid_vtable_index) { 628 // get vtable_index for miranda methods 629 klassVtable vt = ik->vtable(); 630 vtable_index = vt.index_of_miranda(name, signature); 631 } 632 return vtable_index; 633 } 634 635 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) { 636 InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass()); 637 638 // Specify 'true' in order to skip default methods when searching the 639 // interfaces. Function lookup_method_in_klasses() already looked for 640 // the method in the default methods table. 641 return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::skip_defaults); 642 } 643 644 methodHandle LinkResolver::lookup_polymorphic_method( 645 const LinkInfo& link_info, 646 Handle *appendix_result_or_null, 647 TRAPS) { 648 Klass* klass = link_info.resolved_klass(); 649 Symbol* name = link_info.name(); 650 Symbol* full_signature = link_info.signature(); 651 652 vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name); 653 if (TraceMethodHandles) { 654 ResourceMark rm(THREAD); 655 tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s", 656 vmIntrinsics::name_at(iid), klass->external_name(), 657 name->as_C_string(), full_signature->as_C_string()); 658 } 659 if ((klass == SystemDictionary::MethodHandle_klass() || 660 klass == SystemDictionary::VarHandle_klass()) && 661 iid != vmIntrinsics::_none) { 662 if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) { 663 // Most of these do not need an up-call to Java to resolve, so can be done anywhere. 664 // Do not erase last argument type (MemberName) if it is a static linkTo method. 665 bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid); 666 TempNewSymbol basic_signature = 667 MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL); 668 if (TraceMethodHandles) { 669 ResourceMark rm(THREAD); 670 tty->print_cr("lookup_polymorphic_method %s %s => basic %s", 671 name->as_C_string(), 672 full_signature->as_C_string(), 673 basic_signature->as_C_string()); 674 } 675 methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid, 676 basic_signature, 677 CHECK_NULL); 678 if (result.not_null()) { 679 assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic"); 680 assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this"); 681 assert(basic_signature == result->signature(), "predict the result signature"); 682 if (TraceMethodHandles) { 683 ttyLocker ttyl; 684 tty->print("lookup_polymorphic_method => intrinsic "); 685 result->print_on(tty); 686 } 687 } 688 return result; 689 } else if (iid == vmIntrinsics::_invokeGeneric 690 && THREAD->can_call_java() 691 && appendix_result_or_null != NULL) { 692 // This is a method with type-checking semantics. 693 // We will ask Java code to spin an adapter method for it. 694 if (!MethodHandles::enabled()) { 695 // Make sure the Java part of the runtime has been booted up. 696 Klass* natives = SystemDictionary::MethodHandleNatives_klass(); 697 if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) { 698 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(), 699 Handle(), 700 Handle(), 701 true, 702 CHECK_NULL); 703 } 704 } 705 706 Handle appendix; 707 Handle method_type; 708 methodHandle result = SystemDictionary::find_method_handle_invoker( 709 klass, 710 name, 711 full_signature, 712 link_info.current_klass(), 713 &appendix, 714 CHECK_NULL); 715 if (TraceMethodHandles) { 716 ttyLocker ttyl; 717 tty->print("lookup_polymorphic_method => (via Java) "); 718 result->print_on(tty); 719 tty->print(" lookup_polymorphic_method => appendix = "); 720 if (appendix.is_null()) tty->print_cr("(none)"); 721 else appendix->print_on(tty); 722 } 723 if (result.not_null()) { 724 #ifdef ASSERT 725 ResourceMark rm(THREAD); 726 727 TempNewSymbol basic_signature = 728 MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL); 729 int actual_size_of_params = result->size_of_parameters(); 730 int expected_size_of_params = ArgumentSizeComputer(basic_signature).size(); 731 // +1 for MethodHandle.this, +1 for trailing MethodType 732 if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1; 733 if (appendix.not_null()) expected_size_of_params += 1; 734 if (actual_size_of_params != expected_size_of_params) { 735 tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string()); 736 tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid)); 737 result->print(); 738 } 739 assert(actual_size_of_params == expected_size_of_params, 740 "%d != %d", actual_size_of_params, expected_size_of_params); 741 #endif //ASSERT 742 743 assert(appendix_result_or_null != NULL, ""); 744 (*appendix_result_or_null) = appendix; 745 } 746 return result; 747 } 748 } 749 return NULL; 750 } 751 752 void LinkResolver::check_method_accessability(Klass* ref_klass, 753 Klass* resolved_klass, 754 Klass* sel_klass, 755 const methodHandle& sel_method, 756 TRAPS) { 757 758 AccessFlags flags = sel_method->access_flags(); 759 760 // Special case: arrays always override "clone". JVMS 2.15. 761 // If the resolved klass is an array class, and the declaring class 762 // is java.lang.Object and the method is "clone", set the flags 763 // to public. 764 // 765 // We'll check for the method name first, as that's most likely 766 // to be false (so we'll short-circuit out of these tests). 767 if (sel_method->name() == vmSymbols::clone_name() && 768 sel_klass == SystemDictionary::Object_klass() && 769 resolved_klass->is_array_klass()) { 770 // We need to change "protected" to "public". 771 assert(flags.is_protected(), "clone not protected?"); 772 jint new_flags = flags.as_int(); 773 new_flags = new_flags & (~JVM_ACC_PROTECTED); 774 new_flags = new_flags | JVM_ACC_PUBLIC; 775 flags.set_flags(new_flags); 776 } 777 // assert(extra_arg_result_or_null != NULL, "must be able to return extra argument"); 778 779 bool can_access = Reflection::verify_member_access(ref_klass, 780 resolved_klass, 781 sel_klass, 782 flags, 783 true, false, CHECK); 784 // Any existing exceptions that may have been thrown, for example LinkageErrors 785 // from nest-host resolution, have been allowed to propagate. 786 if (!can_access) { 787 ResourceMark rm(THREAD); 788 bool same_module = (sel_klass->module() == ref_klass->module()); 789 Exceptions::fthrow( 790 THREAD_AND_LOCATION, 791 vmSymbols::java_lang_IllegalAccessError(), 792 "class %s tried to access %s%s%smethod %s.%s%s (%s%s%s)", 793 ref_klass->external_name(), 794 sel_method->is_abstract() ? "abstract " : "", 795 sel_method->is_protected() ? "protected " : "", 796 sel_method->is_private() ? "private " : "", 797 sel_klass->external_name(), 798 sel_method->name()->as_C_string(), 799 sel_method->signature()->as_C_string(), 800 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(), 801 (same_module) ? "" : "; ", 802 (same_module) ? "" : sel_klass->class_in_module_of_loader() 803 ); 804 return; 805 } 806 } 807 808 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code, 809 const constantPoolHandle& pool, int index, TRAPS) { 810 // This method is used only 811 // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call), 812 // and 813 // (2) in Bytecode_invoke::static_target 814 // It appears to fail when applied to an invokeinterface call site. 815 // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points. 816 // resolve klass 817 if (code == Bytecodes::_invokedynamic) { 818 Klass* resolved_klass = SystemDictionary::MethodHandle_klass(); 819 Symbol* method_name = vmSymbols::invoke_name(); 820 Symbol* method_signature = pool->signature_ref_at(index); 821 Klass* current_klass = pool->pool_holder(); 822 LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass); 823 return resolve_method(link_info, code, THREAD); 824 } 825 826 LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL); 827 Klass* resolved_klass = link_info.resolved_klass(); 828 829 if (pool->has_preresolution() 830 || (resolved_klass == SystemDictionary::MethodHandle_klass() && 831 MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) { 832 Method* result = ConstantPool::method_at_if_loaded(pool, index); 833 if (result != NULL) { 834 return methodHandle(THREAD, result); 835 } 836 } 837 838 if (code == Bytecodes::_invokeinterface) { 839 return resolve_interface_method(link_info, code, THREAD); 840 } else if (code == Bytecodes::_invokevirtual) { 841 return resolve_method(link_info, code, THREAD); 842 } else if (!resolved_klass->is_interface()) { 843 return resolve_method(link_info, code, THREAD); 844 } else { 845 return resolve_interface_method(link_info, code, THREAD); 846 } 847 } 848 849 // Check and print a loader constraint violation message for method or interface method 850 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info, 851 const methodHandle& resolved_method, 852 const char* method_type, TRAPS) { 853 Handle current_loader(THREAD, link_info.current_klass()->class_loader()); 854 Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader()); 855 856 ResourceMark rm(THREAD); 857 Symbol* failed_type_symbol = 858 SystemDictionary::check_signature_loaders(link_info.signature(), current_loader, 859 resolved_loader, true, CHECK); 860 if (failed_type_symbol != NULL) { 861 Klass* current_class = link_info.current_klass(); 862 ClassLoaderData* current_loader_data = current_class->class_loader_data(); 863 assert(current_loader_data != NULL, "current class has no class loader data"); 864 Klass* resolved_method_class = resolved_method->method_holder(); 865 ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data(); 866 assert(target_loader_data != NULL, "resolved method's class has no class loader data"); 867 868 stringStream ss; 869 ss.print("loader constraint violation: when resolving %s" 870 " \"%s\" the class loader %s of the current class, %s," 871 " and the class loader %s for the method's defining class, %s, have" 872 " different Class objects for the type %s used in the signature (%s; %s)", 873 method_type, 874 link_info.method_string(), 875 current_loader_data->loader_name_and_id(), 876 current_class->name()->as_C_string(), 877 target_loader_data->loader_name_and_id(), 878 resolved_method_class->name()->as_C_string(), 879 failed_type_symbol->as_C_string(), 880 current_class->class_in_module_of_loader(false, true), 881 resolved_method_class->class_in_module_of_loader(false, true)); 882 THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string()); 883 } 884 } 885 886 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig, 887 Klass* current_klass, 888 Klass* sel_klass, TRAPS) { 889 Handle ref_loader(THREAD, current_klass->class_loader()); 890 Handle sel_loader(THREAD, sel_klass->class_loader()); 891 892 ResourceMark rm(THREAD); // needed for check_signature_loaders 893 Symbol* failed_type_symbol = 894 SystemDictionary::check_signature_loaders(sig, 895 ref_loader, sel_loader, 896 false, 897 CHECK); 898 if (failed_type_symbol != NULL) { 899 stringStream ss; 900 const char* failed_type_name = failed_type_symbol->as_klass_external_name(); 901 902 ss.print("loader constraint violation: when resolving field" 903 " \"%s\" of type %s, the class loader %s of the current class, " 904 "%s, and the class loader %s for the field's defining " 905 "type, %s, have different Class objects for type %s (%s; %s)", 906 field->as_C_string(), 907 failed_type_name, 908 current_klass->class_loader_data()->loader_name_and_id(), 909 current_klass->external_name(), 910 sel_klass->class_loader_data()->loader_name_and_id(), 911 sel_klass->external_name(), 912 failed_type_name, 913 current_klass->class_in_module_of_loader(false, true), 914 sel_klass->class_in_module_of_loader(false, true)); 915 THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string()); 916 } 917 } 918 919 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info, 920 Bytecodes::Code code, TRAPS) { 921 922 Handle nested_exception; 923 Klass* resolved_klass = link_info.resolved_klass(); 924 925 // 1. For invokevirtual, cannot call an interface method 926 if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) { 927 ResourceMark rm(THREAD); 928 char buf[200]; 929 jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected", 930 resolved_klass->external_name()); 931 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 932 } 933 934 // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref 935 if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) { 936 ResourceMark rm(THREAD); 937 char buf[200]; 938 jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string()); 939 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 940 } 941 942 // 3. lookup method in resolved klass and its super klasses 943 methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false)); 944 945 // 4. lookup method in all the interfaces implemented by the resolved klass 946 if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy 947 resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info)); 948 949 if (resolved_method.is_null()) { 950 // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc 951 resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD); 952 if (HAS_PENDING_EXCEPTION) { 953 nested_exception = Handle(THREAD, PENDING_EXCEPTION); 954 CLEAR_PENDING_EXCEPTION; 955 } 956 } 957 } 958 959 // 5. method lookup failed 960 if (resolved_method.is_null()) { 961 ResourceMark rm(THREAD); 962 THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(), 963 Method::name_and_sig_as_C_string(resolved_klass, 964 link_info.name(), 965 link_info.signature()), 966 nested_exception, NULL); 967 } 968 969 // 6. access checks, access checking may be turned off when calling from within the VM. 970 Klass* current_klass = link_info.current_klass(); 971 if (link_info.check_access()) { 972 assert(current_klass != NULL , "current_klass should not be null"); 973 974 // check if method can be accessed by the referring class 975 check_method_accessability(current_klass, 976 resolved_klass, 977 resolved_method->method_holder(), 978 resolved_method, 979 CHECK_NULL); 980 981 // check loader constraints 982 check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL); 983 } 984 985 // For private method invocation we should only find the method in the resolved class. 986 // If that is not the case then we have a found a supertype method that we have nestmate 987 // access to. 988 if (resolved_method->is_private() && resolved_method->method_holder() != resolved_klass) { 989 ResourceMark rm(THREAD); 990 DEBUG_ONLY(bool is_nestmate = InstanceKlass::cast(link_info.current_klass())->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD);) 991 assert(is_nestmate, "was only expecting nestmates to get here!"); 992 Exceptions::fthrow( 993 THREAD_AND_LOCATION, 994 vmSymbols::java_lang_NoSuchMethodError(), 995 "%s: method %s%s not found", 996 resolved_klass->external_name(), 997 resolved_method->name()->as_C_string(), 998 resolved_method->signature()->as_C_string() 999 ); 1000 return NULL; 1001 } 1002 1003 return resolved_method; 1004 } 1005 1006 static void trace_method_resolution(const char* prefix, 1007 Klass* klass, 1008 Klass* resolved_klass, 1009 const methodHandle& method, 1010 bool logitables, 1011 int index = -1) { 1012 #ifndef PRODUCT 1013 ResourceMark rm; 1014 Log(itables) logi; 1015 LogStream lsi(logi.trace()); 1016 Log(vtables) logv; 1017 LogStream lsv(logv.trace()); 1018 outputStream* st; 1019 if (logitables) { 1020 st = &lsi; 1021 } else { 1022 st = &lsv; 1023 } 1024 st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", 1025 prefix, 1026 (klass == NULL ? "<NULL>" : klass->internal_name()), 1027 (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()), 1028 Method::name_and_sig_as_C_string(resolved_klass, 1029 method->name(), 1030 method->signature()), 1031 method->method_holder()->internal_name()); 1032 method->print_linkage_flags(st); 1033 if (index != -1) { 1034 st->print("vtable_index:%d", index); 1035 } 1036 st->cr(); 1037 #endif // PRODUCT 1038 } 1039 1040 // Do linktime resolution of a method in the interface within the context of the specied bytecode. 1041 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) { 1042 1043 Klass* resolved_klass = link_info.resolved_klass(); 1044 1045 // check if klass is interface 1046 if (!resolved_klass->is_interface()) { 1047 ResourceMark rm(THREAD); 1048 char buf[200]; 1049 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name()); 1050 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1051 } 1052 1053 // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref 1054 if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) { 1055 ResourceMark rm(THREAD); 1056 char buf[200]; 1057 jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string()); 1058 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1059 } 1060 1061 // lookup method in this interface or its super, java.lang.Object 1062 // JDK8: also look for static methods 1063 methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true)); 1064 1065 if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { 1066 // lookup method in all the super-interfaces 1067 resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info)); 1068 } 1069 1070 if (resolved_method.is_null()) { 1071 // no method found 1072 ResourceMark rm(THREAD); 1073 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), 1074 Method::name_and_sig_as_C_string(resolved_klass, 1075 link_info.name(), 1076 link_info.signature())); 1077 } 1078 1079 if (link_info.check_access()) { 1080 // JDK8 adds non-public interface methods, and accessability check requirement 1081 Klass* current_klass = link_info.current_klass(); 1082 1083 assert(current_klass != NULL , "current_klass should not be null"); 1084 1085 // check if method can be accessed by the referring class 1086 check_method_accessability(current_klass, 1087 resolved_klass, 1088 resolved_method->method_holder(), 1089 resolved_method, 1090 CHECK_NULL); 1091 1092 check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL); 1093 } 1094 1095 if (code != Bytecodes::_invokestatic && resolved_method->is_static()) { 1096 ResourceMark rm(THREAD); 1097 char buf[200]; 1098 jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s", 1099 Method::name_and_sig_as_C_string(resolved_klass, 1100 resolved_method->name(), resolved_method->signature())); 1101 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1102 } 1103 1104 if (log_develop_is_enabled(Trace, itables)) { 1105 char buf[200]; 1106 jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:", 1107 Bytecodes::name(code)); 1108 trace_method_resolution(buf, link_info.current_klass(), resolved_klass, 1109 resolved_method, true); 1110 } 1111 1112 return resolved_method; 1113 } 1114 1115 //------------------------------------------------------------------------------------------------------------------------ 1116 // Field resolution 1117 1118 void LinkResolver::check_field_accessability(Klass* ref_klass, 1119 Klass* resolved_klass, 1120 Klass* sel_klass, 1121 const fieldDescriptor& fd, 1122 TRAPS) { 1123 bool can_access = Reflection::verify_member_access(ref_klass, 1124 resolved_klass, 1125 sel_klass, 1126 fd.access_flags(), 1127 true, false, CHECK); 1128 // Any existing exceptions that may have been thrown, for example LinkageErrors 1129 // from nest-host resolution, have been allowed to propagate. 1130 if (!can_access) { 1131 bool same_module = (sel_klass->module() == ref_klass->module()); 1132 ResourceMark rm(THREAD); 1133 Exceptions::fthrow( 1134 THREAD_AND_LOCATION, 1135 vmSymbols::java_lang_IllegalAccessError(), 1136 "class %s tried to access %s%sfield %s.%s (%s%s%s)", 1137 ref_klass->external_name(), 1138 fd.is_protected() ? "protected " : "", 1139 fd.is_private() ? "private " : "", 1140 sel_klass->external_name(), 1141 fd.name()->as_C_string(), 1142 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(), 1143 (same_module) ? "" : "; ", 1144 (same_module) ? "" : sel_klass->class_in_module_of_loader() 1145 ); 1146 return; 1147 } 1148 } 1149 1150 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) { 1151 LinkInfo link_info(pool, index, method, CHECK); 1152 resolve_field(fd, link_info, byte, true, CHECK); 1153 } 1154 1155 void LinkResolver::resolve_field(fieldDescriptor& fd, 1156 const LinkInfo& link_info, 1157 Bytecodes::Code byte, bool initialize_class, 1158 TRAPS) { 1159 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || 1160 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield || 1161 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield || 1162 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode"); 1163 1164 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); 1165 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield); 1166 // Check if there's a resolved klass containing the field 1167 Klass* resolved_klass = link_info.resolved_klass(); 1168 Symbol* field = link_info.name(); 1169 Symbol* sig = link_info.signature(); 1170 1171 if (resolved_klass == NULL) { 1172 ResourceMark rm(THREAD); 1173 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 1174 } 1175 1176 // Resolve instance field 1177 Klass* sel_klass = resolved_klass->find_field(field, sig, &fd); 1178 // check if field exists; i.e., if a klass containing the field def has been selected 1179 if (sel_klass == NULL) { 1180 ResourceMark rm(THREAD); 1181 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 1182 } 1183 1184 if (!link_info.check_access()) 1185 // Access checking may be turned off when calling from within the VM. 1186 return; 1187 1188 // check access 1189 Klass* current_klass = link_info.current_klass(); 1190 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); 1191 1192 // check for errors 1193 if (is_static != fd.is_static()) { 1194 ResourceMark rm(THREAD); 1195 char msg[200]; 1196 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string()); 1197 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); 1198 } 1199 1200 // A final field can be modified only 1201 // (1) by methods declared in the class declaring the field and 1202 // (2) by the <clinit> method (in case of a static field) 1203 // or by the <init> method (in case of an instance field). 1204 if (is_put && fd.access_flags().is_final()) { 1205 ResourceMark rm(THREAD); 1206 stringStream ss; 1207 1208 if (sel_klass != current_klass) { 1209 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class", 1210 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(), 1211 current_klass->external_name()); 1212 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); 1213 } 1214 1215 if (fd.constants()->pool_holder()->major_version() >= 53) { 1216 methodHandle m = link_info.current_method(); 1217 assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes"); 1218 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic && 1219 fd.is_static() && 1220 !m()->is_static_initializer()); 1221 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) && 1222 !fd.is_static() && 1223 !m->is_object_initializer()); 1224 1225 if (is_initialized_static_final_update || is_initialized_instance_final_update) { 1226 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ", 1227 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(), 1228 m()->name()->as_C_string(), 1229 is_static ? "<clinit>" : "<init>"); 1230 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string()); 1231 } 1232 } 1233 } 1234 1235 // initialize resolved_klass if necessary 1236 // note 1: the klass which declared the field must be initialized (i.e, sel_klass) 1237 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) 1238 // 1239 // note 2: we don't want to force initialization if we are just checking 1240 // if the field access is legal; e.g., during compilation 1241 if (is_static && initialize_class) { 1242 sel_klass->initialize(CHECK); 1243 } 1244 1245 if (sel_klass != current_klass) { 1246 check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK); 1247 } 1248 1249 // return information. note that the klass is set to the actual klass containing the 1250 // field, otherwise access of static fields in superclasses will not work. 1251 } 1252 1253 1254 //------------------------------------------------------------------------------------------------------------------------ 1255 // Invoke resolution 1256 // 1257 // Naming conventions: 1258 // 1259 // resolved_method the specified method (i.e., static receiver specified via constant pool index) 1260 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class) 1261 // resolved_klass the specified klass (i.e., specified via constant pool index) 1262 // recv_klass the receiver klass 1263 1264 1265 void LinkResolver::resolve_static_call(CallInfo& result, 1266 const LinkInfo& link_info, 1267 bool initialize_class, TRAPS) { 1268 methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK); 1269 1270 // The resolved class can change as a result of this resolution. 1271 Klass* resolved_klass = resolved_method->method_holder(); 1272 1273 // Initialize klass (this should only happen if everything is ok) 1274 if (initialize_class && resolved_klass->should_be_initialized()) { 1275 resolved_klass->initialize(CHECK); 1276 // Use updated LinkInfo to reresolve with resolved method holder 1277 LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(), 1278 link_info.current_klass(), 1279 link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check); 1280 resolved_method = linktime_resolve_static_method(new_info, CHECK); 1281 } 1282 1283 // setup result 1284 result.set_static(resolved_klass, resolved_method, CHECK); 1285 } 1286 1287 // throws linktime exceptions 1288 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) { 1289 1290 Klass* resolved_klass = link_info.resolved_klass(); 1291 methodHandle resolved_method; 1292 if (!resolved_klass->is_interface()) { 1293 resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL); 1294 } else { 1295 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL); 1296 } 1297 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier"); 1298 1299 // check if static 1300 if (!resolved_method->is_static()) { 1301 ResourceMark rm(THREAD); 1302 char buf[200]; 1303 jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass, 1304 resolved_method->name(), 1305 resolved_method->signature())); 1306 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1307 } 1308 return resolved_method; 1309 } 1310 1311 1312 void LinkResolver::resolve_special_call(CallInfo& result, 1313 Handle recv, 1314 const LinkInfo& link_info, 1315 TRAPS) { 1316 methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK); 1317 runtime_resolve_special_method(result, link_info, resolved_method, recv, CHECK); 1318 } 1319 1320 // throws linktime exceptions 1321 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, 1322 TRAPS) { 1323 1324 // Invokespecial is called for multiple special reasons: 1325 // <init> 1326 // local private method invocation, for classes and interfaces 1327 // superclass.method, which can also resolve to a default method 1328 // and the selected method is recalculated relative to the direct superclass 1329 // superinterface.method, which explicitly does not check shadowing 1330 Klass* resolved_klass = link_info.resolved_klass(); 1331 methodHandle resolved_method; 1332 1333 if (!resolved_klass->is_interface()) { 1334 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL); 1335 } else { 1336 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL); 1337 } 1338 1339 // check if method name is <init>, that it is found in same klass as static type 1340 if (resolved_method->name() == vmSymbols::object_initializer_name() && 1341 resolved_method->method_holder() != resolved_klass) { 1342 ResourceMark rm(THREAD); 1343 Exceptions::fthrow( 1344 THREAD_AND_LOCATION, 1345 vmSymbols::java_lang_NoSuchMethodError(), 1346 "%s: method %s%s not found", 1347 resolved_klass->external_name(), 1348 resolved_method->name()->as_C_string(), 1349 resolved_method->signature()->as_C_string() 1350 ); 1351 return NULL; 1352 } 1353 1354 // ensure that invokespecial's interface method reference is in 1355 // a direct superinterface, not an indirect superinterface 1356 Klass* current_klass = link_info.current_klass(); 1357 if (current_klass != NULL && resolved_klass->is_interface()) { 1358 InstanceKlass* ck = InstanceKlass::cast(current_klass); 1359 InstanceKlass *klass_to_check = !ck->is_unsafe_anonymous() ? 1360 ck : 1361 InstanceKlass::cast(ck->unsafe_anonymous_host()); 1362 // Disable verification for the dynamically-generated reflection bytecodes. 1363 bool is_reflect = klass_to_check->is_subclass_of( 1364 SystemDictionary::reflect_MagicAccessorImpl_klass()); 1365 1366 if (!is_reflect && 1367 !klass_to_check->is_same_or_direct_interface(resolved_klass)) { 1368 ResourceMark rm(THREAD); 1369 char buf[200]; 1370 jio_snprintf(buf, sizeof(buf), 1371 "Interface method reference: %s, is in an indirect superinterface of %s", 1372 Method::name_and_sig_as_C_string(resolved_klass, 1373 resolved_method->name(), 1374 resolved_method->signature()), 1375 current_klass->external_name()); 1376 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1377 } 1378 } 1379 1380 // check if not static 1381 if (resolved_method->is_static()) { 1382 ResourceMark rm(THREAD); 1383 char buf[200]; 1384 jio_snprintf(buf, sizeof(buf), 1385 "Expecting non-static method %s", 1386 Method::name_and_sig_as_C_string(resolved_klass, 1387 resolved_method->name(), 1388 resolved_method->signature())); 1389 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1390 } 1391 1392 if (log_develop_is_enabled(Trace, itables)) { 1393 trace_method_resolution("invokespecial resolved method: caller-class:", 1394 current_klass, resolved_klass, resolved_method, true); 1395 } 1396 1397 return resolved_method; 1398 } 1399 1400 // throws runtime exceptions 1401 void LinkResolver::runtime_resolve_special_method(CallInfo& result, 1402 const LinkInfo& link_info, 1403 const methodHandle& resolved_method, 1404 Handle recv, TRAPS) { 1405 1406 Klass* resolved_klass = link_info.resolved_klass(); 1407 1408 // resolved method is selected method unless we have an old-style lookup 1409 // for a superclass method 1410 // Invokespecial for a superinterface, resolved method is selected method, 1411 // no checks for shadowing 1412 methodHandle sel_method(THREAD, resolved_method()); 1413 1414 if (link_info.check_access() && 1415 // check if the method is not <init> 1416 resolved_method->name() != vmSymbols::object_initializer_name()) { 1417 1418 Klass* current_klass = link_info.current_klass(); 1419 1420 // Check if the class of the resolved_klass is a superclass 1421 // (not supertype in order to exclude interface classes) of the current class. 1422 // This check is not performed for super.invoke for interface methods 1423 // in super interfaces. 1424 if (current_klass->is_subclass_of(resolved_klass) && 1425 current_klass != resolved_klass) { 1426 // Lookup super method 1427 Klass* super_klass = current_klass->super(); 1428 sel_method = lookup_instance_method_in_klasses(super_klass, 1429 resolved_method->name(), 1430 resolved_method->signature(), 1431 Klass::find_private, CHECK); 1432 // check if found 1433 if (sel_method.is_null()) { 1434 ResourceMark rm(THREAD); 1435 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1436 Method::name_and_sig_as_C_string(resolved_klass, 1437 resolved_method->name(), 1438 resolved_method->signature())); 1439 // check loader constraints if found a different method 1440 } else if (sel_method() != resolved_method()) { 1441 check_method_loader_constraints(link_info, sel_method, "method", CHECK); 1442 } 1443 } 1444 1445 // Check that the class of objectref (the receiver) is the current class or interface, 1446 // or a subtype of the current class or interface (the sender), otherwise invokespecial 1447 // throws IllegalAccessError. 1448 // The verifier checks that the sender is a subtype of the class in the I/MR operand. 1449 // The verifier also checks that the receiver is a subtype of the sender, if the sender is 1450 // a class. If the sender is an interface, the check has to be performed at runtime. 1451 InstanceKlass* sender = InstanceKlass::cast(current_klass); 1452 sender = sender->is_unsafe_anonymous() ? sender->unsafe_anonymous_host() : sender; 1453 if (sender->is_interface() && recv.not_null()) { 1454 Klass* receiver_klass = recv->klass(); 1455 if (!receiver_klass->is_subtype_of(sender)) { 1456 ResourceMark rm(THREAD); 1457 char buf[500]; 1458 jio_snprintf(buf, sizeof(buf), 1459 "Receiver class %s must be the current class or a subtype of interface %s", 1460 receiver_klass->name()->as_C_string(), 1461 sender->name()->as_C_string()); 1462 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf); 1463 } 1464 } 1465 } 1466 1467 // check if not static 1468 if (sel_method->is_static()) { 1469 ResourceMark rm(THREAD); 1470 char buf[200]; 1471 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass, 1472 resolved_method->name(), 1473 resolved_method->signature())); 1474 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1475 } 1476 1477 // check if abstract 1478 if (sel_method->is_abstract()) { 1479 ResourceMark rm(THREAD); 1480 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1481 Method::name_and_sig_as_C_string(resolved_klass, 1482 sel_method->name(), 1483 sel_method->signature())); 1484 } 1485 1486 if (log_develop_is_enabled(Trace, itables)) { 1487 trace_method_resolution("invokespecial selected method: resolved-class:", 1488 resolved_klass, resolved_klass, sel_method, true); 1489 } 1490 1491 // setup result 1492 result.set_static(resolved_klass, sel_method, CHECK); 1493 } 1494 1495 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass, 1496 const LinkInfo& link_info, 1497 bool check_null_and_abstract, TRAPS) { 1498 methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK); 1499 runtime_resolve_virtual_method(result, resolved_method, 1500 link_info.resolved_klass(), 1501 recv, receiver_klass, 1502 check_null_and_abstract, CHECK); 1503 } 1504 1505 // throws linktime exceptions 1506 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info, 1507 TRAPS) { 1508 // normal method resolution 1509 methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL); 1510 1511 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1512 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1513 1514 // check if private interface method 1515 Klass* resolved_klass = link_info.resolved_klass(); 1516 Klass* current_klass = link_info.current_klass(); 1517 1518 // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method 1519 if (resolved_klass->is_interface() && resolved_method->is_private()) { 1520 ResourceMark rm(THREAD); 1521 char buf[200]; 1522 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s", 1523 Method::name_and_sig_as_C_string(resolved_klass, 1524 resolved_method->name(), 1525 resolved_method->signature()), 1526 (current_klass == NULL ? "<NULL>" : current_klass->internal_name())); 1527 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1528 } 1529 1530 // check if not static 1531 if (resolved_method->is_static()) { 1532 ResourceMark rm(THREAD); 1533 char buf[200]; 1534 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass, 1535 resolved_method->name(), 1536 resolved_method->signature())); 1537 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1538 } 1539 1540 if (log_develop_is_enabled(Trace, vtables)) { 1541 trace_method_resolution("invokevirtual resolved method: caller-class:", 1542 current_klass, resolved_klass, resolved_method, false); 1543 } 1544 1545 return resolved_method; 1546 } 1547 1548 // throws runtime exceptions 1549 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result, 1550 const methodHandle& resolved_method, 1551 Klass* resolved_klass, 1552 Handle recv, 1553 Klass* recv_klass, 1554 bool check_null_and_abstract, 1555 TRAPS) { 1556 1557 // setup default return values 1558 int vtable_index = Method::invalid_vtable_index; 1559 methodHandle selected_method; 1560 1561 // runtime method resolution 1562 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists 1563 THROW(vmSymbols::java_lang_NullPointerException()); 1564 } 1565 1566 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s 1567 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since 1568 // a missing receiver might result in a bogus lookup. 1569 assert(resolved_method->method_holder()->is_linked(), "must be linked"); 1570 1571 // do lookup based on receiver klass using the vtable index 1572 if (resolved_method->method_holder()->is_interface()) { // default or miranda method 1573 vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method); 1574 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); 1575 1576 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index)); 1577 } else { 1578 // at this point we are sure that resolved_method is virtual and not 1579 // a default or miranda method; therefore, it must have a valid vtable index. 1580 assert(!resolved_method->has_itable_index(), ""); 1581 vtable_index = resolved_method->vtable_index(); 1582 // We could get a negative vtable_index of nonvirtual_vtable_index for private 1583 // methods, or for final methods. Private methods never appear in the vtable 1584 // and never override other methods. As an optimization, final methods are 1585 // never put in the vtable, unless they override an existing method. 1586 // So if we do get nonvirtual_vtable_index, it means the selected method is the 1587 // resolved method, and it can never be changed by an override. 1588 if (vtable_index == Method::nonvirtual_vtable_index) { 1589 assert(resolved_method->can_be_statically_bound(), "cannot override this method"); 1590 selected_method = resolved_method; 1591 } else { 1592 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index)); 1593 } 1594 } 1595 1596 // check if method exists 1597 if (selected_method.is_null()) { 1598 throw_abstract_method_error(resolved_method, recv_klass, CHECK); 1599 } 1600 1601 // check if abstract 1602 if (check_null_and_abstract && selected_method->is_abstract()) { 1603 // Pass arguments for generating a verbose error message. 1604 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK); 1605 } 1606 1607 if (log_develop_is_enabled(Trace, vtables)) { 1608 trace_method_resolution("invokevirtual selected method: receiver-class:", 1609 recv_klass, resolved_klass, selected_method, 1610 false, vtable_index); 1611 } 1612 // setup result 1613 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK); 1614 } 1615 1616 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass, 1617 const LinkInfo& link_info, 1618 bool check_null_and_abstract, TRAPS) { 1619 // throws linktime exceptions 1620 methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK); 1621 runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(), 1622 recv, recv_klass, check_null_and_abstract, CHECK); 1623 } 1624 1625 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info, 1626 TRAPS) { 1627 // normal interface method resolution 1628 methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL); 1629 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1630 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1631 1632 return resolved_method; 1633 } 1634 1635 // throws runtime exceptions 1636 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, 1637 const methodHandle& resolved_method, 1638 Klass* resolved_klass, 1639 Handle recv, 1640 Klass* recv_klass, 1641 bool check_null_and_abstract, TRAPS) { 1642 1643 // check if receiver exists 1644 if (check_null_and_abstract && recv.is_null()) { 1645 THROW(vmSymbols::java_lang_NullPointerException()); 1646 } 1647 1648 // check if receiver klass implements the resolved interface 1649 if (!recv_klass->is_subtype_of(resolved_klass)) { 1650 ResourceMark rm(THREAD); 1651 char buf[200]; 1652 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s", 1653 recv_klass->external_name(), 1654 resolved_klass->external_name()); 1655 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1656 } 1657 1658 methodHandle selected_method = resolved_method; 1659 1660 // resolve the method in the receiver class, unless it is private 1661 if (!resolved_method()->is_private()) { 1662 // do lookup based on receiver klass 1663 // This search must match the linktime preparation search for itable initialization 1664 // to correctly enforce loader constraints for interface method inheritance. 1665 // Private methods are skipped as the resolved method was not private. 1666 selected_method = lookup_instance_method_in_klasses(recv_klass, 1667 resolved_method->name(), 1668 resolved_method->signature(), 1669 Klass::skip_private, CHECK); 1670 1671 if (selected_method.is_null() && !check_null_and_abstract) { 1672 // In theory this is a harmless placeholder value, but 1673 // in practice leaving in null affects the nsk default method tests. 1674 // This needs further study. 1675 selected_method = resolved_method; 1676 } 1677 // check if method exists 1678 if (selected_method.is_null()) { 1679 // Pass arguments for generating a verbose error message. 1680 throw_abstract_method_error(resolved_method, recv_klass, CHECK); 1681 } 1682 // check access 1683 // Throw Illegal Access Error if selected_method is not public. 1684 if (!selected_method->is_public()) { 1685 ResourceMark rm(THREAD); 1686 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), 1687 Method::name_and_sig_as_C_string(recv_klass, 1688 selected_method->name(), 1689 selected_method->signature())); 1690 } 1691 // check if abstract 1692 if (check_null_and_abstract && selected_method->is_abstract()) { 1693 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK); 1694 } 1695 } 1696 1697 if (log_develop_is_enabled(Trace, itables)) { 1698 trace_method_resolution("invokeinterface selected method: receiver-class:", 1699 recv_klass, resolved_klass, selected_method, true); 1700 } 1701 // setup result 1702 if (resolved_method->has_vtable_index()) { 1703 int vtable_index = resolved_method->vtable_index(); 1704 log_develop_trace(itables)(" -- vtable index: %d", vtable_index); 1705 assert(vtable_index == selected_method->vtable_index(), "sanity check"); 1706 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK); 1707 } else if (resolved_method->has_itable_index()) { 1708 int itable_index = resolved_method()->itable_index(); 1709 log_develop_trace(itables)(" -- itable index: %d", itable_index); 1710 result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK); 1711 } else { 1712 int index = resolved_method->vtable_index(); 1713 log_develop_trace(itables)(" -- non itable/vtable index: %d", index); 1714 assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!"); 1715 assert(resolved_method()->is_private() || 1716 (resolved_method()->is_final() && resolved_method->method_holder() == SystemDictionary::Object_klass()), 1717 "Should only have non-virtual invokeinterface for private or final-Object methods!"); 1718 assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!"); 1719 // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods) 1720 result.set_virtual(resolved_klass, resolved_klass, resolved_method, resolved_method, index, CHECK); 1721 } 1722 } 1723 1724 1725 methodHandle LinkResolver::linktime_resolve_interface_method_or_null( 1726 const LinkInfo& link_info) { 1727 EXCEPTION_MARK; 1728 methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD); 1729 if (HAS_PENDING_EXCEPTION) { 1730 CLEAR_PENDING_EXCEPTION; 1731 return methodHandle(); 1732 } else { 1733 return method_result; 1734 } 1735 } 1736 1737 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null( 1738 const LinkInfo& link_info) { 1739 EXCEPTION_MARK; 1740 methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD); 1741 if (HAS_PENDING_EXCEPTION) { 1742 CLEAR_PENDING_EXCEPTION; 1743 return methodHandle(); 1744 } else { 1745 return method_result; 1746 } 1747 } 1748 1749 methodHandle LinkResolver::resolve_virtual_call_or_null( 1750 Klass* receiver_klass, 1751 const LinkInfo& link_info) { 1752 EXCEPTION_MARK; 1753 CallInfo info; 1754 resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1755 if (HAS_PENDING_EXCEPTION) { 1756 CLEAR_PENDING_EXCEPTION; 1757 return methodHandle(); 1758 } 1759 return info.selected_method(); 1760 } 1761 1762 methodHandle LinkResolver::resolve_interface_call_or_null( 1763 Klass* receiver_klass, 1764 const LinkInfo& link_info) { 1765 EXCEPTION_MARK; 1766 CallInfo info; 1767 resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1768 if (HAS_PENDING_EXCEPTION) { 1769 CLEAR_PENDING_EXCEPTION; 1770 return methodHandle(); 1771 } 1772 return info.selected_method(); 1773 } 1774 1775 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass, 1776 const LinkInfo& link_info) { 1777 EXCEPTION_MARK; 1778 CallInfo info; 1779 resolve_virtual_call(info, Handle(), receiver_klass, link_info, 1780 /*check_null_or_abstract*/false, THREAD); 1781 if (HAS_PENDING_EXCEPTION) { 1782 CLEAR_PENDING_EXCEPTION; 1783 return Method::invalid_vtable_index; 1784 } 1785 return info.vtable_index(); 1786 } 1787 1788 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) { 1789 EXCEPTION_MARK; 1790 CallInfo info; 1791 resolve_static_call(info, link_info, /*initialize_class*/false, THREAD); 1792 if (HAS_PENDING_EXCEPTION) { 1793 CLEAR_PENDING_EXCEPTION; 1794 return methodHandle(); 1795 } 1796 return info.selected_method(); 1797 } 1798 1799 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) { 1800 EXCEPTION_MARK; 1801 CallInfo info; 1802 resolve_special_call(info, Handle(), link_info, THREAD); 1803 if (HAS_PENDING_EXCEPTION) { 1804 CLEAR_PENDING_EXCEPTION; 1805 return methodHandle(); 1806 } 1807 return info.selected_method(); 1808 } 1809 1810 1811 1812 //------------------------------------------------------------------------------------------------------------------------ 1813 // ConstantPool entries 1814 1815 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) { 1816 switch (byte) { 1817 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break; 1818 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break; 1819 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break; 1820 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break; 1821 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break; 1822 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break; 1823 default : break; 1824 } 1825 return; 1826 } 1827 1828 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv, 1829 const methodHandle& attached_method, 1830 Bytecodes::Code byte, TRAPS) { 1831 Klass* defc = attached_method->method_holder(); 1832 Symbol* name = attached_method->name(); 1833 Symbol* type = attached_method->signature(); 1834 LinkInfo link_info(defc, name, type); 1835 switch(byte) { 1836 case Bytecodes::_invokevirtual: 1837 resolve_virtual_call(result, recv, recv->klass(), link_info, 1838 /*check_null_and_abstract=*/true, CHECK); 1839 break; 1840 case Bytecodes::_invokeinterface: 1841 resolve_interface_call(result, recv, recv->klass(), link_info, 1842 /*check_null_and_abstract=*/true, CHECK); 1843 break; 1844 case Bytecodes::_invokestatic: 1845 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK); 1846 break; 1847 case Bytecodes::_invokespecial: 1848 resolve_special_call(result, recv, link_info, CHECK); 1849 break; 1850 default: 1851 fatal("bad call: %s", Bytecodes::name(byte)); 1852 break; 1853 } 1854 } 1855 1856 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1857 LinkInfo link_info(pool, index, CHECK); 1858 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK); 1859 } 1860 1861 1862 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv, 1863 const constantPoolHandle& pool, int index, TRAPS) { 1864 LinkInfo link_info(pool, index, CHECK); 1865 resolve_special_call(result, recv, link_info, CHECK); 1866 } 1867 1868 1869 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv, 1870 const constantPoolHandle& pool, int index, 1871 TRAPS) { 1872 1873 LinkInfo link_info(pool, index, CHECK); 1874 Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass(); 1875 resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK); 1876 } 1877 1878 1879 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) { 1880 LinkInfo link_info(pool, index, CHECK); 1881 Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass(); 1882 resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK); 1883 } 1884 1885 1886 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1887 // This guy is reached from InterpreterRuntime::resolve_invokehandle. 1888 LinkInfo link_info(pool, index, CHECK); 1889 if (TraceMethodHandles) { 1890 ResourceMark rm(THREAD); 1891 tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(), 1892 link_info.signature()->as_C_string()); 1893 } 1894 resolve_handle_call(result, link_info, CHECK); 1895 } 1896 1897 void LinkResolver::resolve_handle_call(CallInfo& result, 1898 const LinkInfo& link_info, 1899 TRAPS) { 1900 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar 1901 Klass* resolved_klass = link_info.resolved_klass(); 1902 assert(resolved_klass == SystemDictionary::MethodHandle_klass() || 1903 resolved_klass == SystemDictionary::VarHandle_klass(), ""); 1904 assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), ""); 1905 Handle resolved_appendix; 1906 methodHandle resolved_method = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK); 1907 result.set_handle(resolved_klass, resolved_method, resolved_appendix, true, CHECK); 1908 } 1909 1910 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) { 1911 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(indy_index); 1912 int pool_index = cpce->constant_pool_index(); 1913 1914 // Resolve the bootstrap specifier (BSM + optional arguments). 1915 BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index); 1916 1917 // Check if CallSite has been bound already or failed already, and short circuit: 1918 { 1919 bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK); 1920 if (is_done) return; 1921 } 1922 1923 // The initial step in Call Site Specifier Resolution is to resolve the symbolic 1924 // reference to a method handle which will be the bootstrap method for a dynamic 1925 // call site. If resolution for the java.lang.invoke.MethodHandle for the bootstrap 1926 // method falis, then a MethodHandleInError is stored at the corresponding bootstrap 1927 // method's CP index for the CONSTANT_MethodHandle_info. So, there is no need to 1928 // set the indy_rf flag since any subsequent invokedynamic instruction which shares 1929 // this bootstrap method will encounter the resolution of MethodHandleInError. 1930 if (TraceMethodHandles) { 1931 bootstrap_specifier.print_msg_on(tty, "resolve_invokedynamic"); 1932 } 1933 1934 resolve_dynamic_call(result, bootstrap_specifier, CHECK); 1935 1936 // The returned linkage result is provisional up to the moment 1937 // the interpreter or runtime performs a serialized check of 1938 // the relevent CPCE::f1 field. This is done by the caller 1939 // of this method, via CPCE::set_dynamic_call, which uses 1940 // an ObjectLocker to do the final serialization of updates 1941 // to CPCE state, including f1. 1942 } 1943 1944 void LinkResolver::resolve_dynamic_call(CallInfo& result, 1945 BootstrapInfo& bootstrap_specifier, 1946 TRAPS) { 1947 // JSR 292: this must resolve to an implicitly generated method 1948 // such as MH.linkToCallSite(*...) or some other call-site shape. 1949 // The appendix argument is likely to be a freshly-created CallSite. 1950 // It may also be a MethodHandle from an unwrapped ConstantCallSite, 1951 // or any other reference. The resolved_method DTRT with the appendix. 1952 SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD); 1953 Exceptions::wrap_dynamic_exception(THREAD); 1954 1955 if (HAS_PENDING_EXCEPTION) { 1956 if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { 1957 // Let any random low-level IE or SOE or OOME just bleed through. 1958 // Basically we pretend that the bootstrap method was never called, 1959 // if it fails this way: We neither record a successful linkage, 1960 // nor do we memoize a LE for posterity. 1961 return; 1962 } 1963 // JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve 1964 // a symbolic reference fails because an error is thrown that is an 1965 // instance of LinkageError (or a subclass), then subsequent attempts to 1966 // resolve the reference always fail with the same error that was thrown 1967 // as a result of the initial resolution attempt. 1968 bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK); 1969 if (!recorded_res_status) { 1970 // Another thread got here just before we did. So, either use the method 1971 // that it resolved or throw the LinkageError exception that it threw. 1972 bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK); 1973 if (is_done) return; 1974 } 1975 assert(bootstrap_specifier.invokedynamic_cp_cache_entry()->indy_resolution_failed(), 1976 "Resolution failure flag wasn't set"); 1977 } 1978 1979 bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK); 1980 // Exceptions::wrap_dynamic_exception not used because 1981 // set_handle doesn't throw linkage errors 1982 } 1983 1984 // Selected method is abstract. 1985 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method, 1986 const methodHandle& selected_method, 1987 Klass *recv_klass, TRAPS) { 1988 Klass *resolved_klass = resolved_method->method_holder(); 1989 ResourceMark rm(THREAD); 1990 stringStream ss; 1991 1992 if (recv_klass != NULL) { 1993 ss.print("Receiver class %s does not define or inherit an " 1994 "implementation of the", 1995 recv_klass->external_name()); 1996 } else { 1997 ss.print("Missing implementation of"); 1998 } 1999 2000 assert(resolved_method.not_null(), "Sanity"); 2001 ss.print(" resolved method %s%s%s%s of %s %s.", 2002 resolved_method->is_abstract() ? "abstract " : "", 2003 resolved_method->is_private() ? "private " : "", 2004 resolved_method->name()->as_C_string(), 2005 resolved_method->signature()->as_C_string(), 2006 resolved_klass->external_kind(), 2007 resolved_klass->external_name()); 2008 2009 if (selected_method.not_null() && !(resolved_method == selected_method)) { 2010 ss.print(" Selected method is %s%s%s.", 2011 selected_method->is_abstract() ? "abstract " : "", 2012 selected_method->is_private() ? "private " : "", 2013 selected_method->name_and_sig_as_C_string()); 2014 } 2015 2016 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string()); 2017 }