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