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