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