< prev index next >

src/share/vm/interpreter/linkResolver.cpp

Print this page




  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/method.hpp"
  40 #include "oops/objArrayOop.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "prims/methodHandles.hpp"
  43 #include "prims/nativeLookup.hpp"
  44 #include "runtime/compilationPolicy.hpp"
  45 #include "runtime/fieldDescriptor.hpp"
  46 #include "runtime/frame.inline.hpp"
  47 #include "runtime/handles.inline.hpp"
  48 #include "runtime/reflection.hpp"
  49 #include "runtime/signature.hpp"
  50 #include "runtime/thread.inline.hpp"
  51 #include "runtime/vmThread.hpp"
  52 
  53 
  54 //------------------------------------------------------------------------------------------------------------------------
  55 // Implementation of CallInfo
  56 
  57 
  58 void CallInfo::set_static(KlassHandle resolved_klass, const methodHandle& resolved_method, TRAPS) {
  59   int vtable_index = Method::nonvirtual_vtable_index;
  60   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  61 }
  62 
  63 
  64 void CallInfo::set_interface(KlassHandle resolved_klass,
  65                              KlassHandle selected_klass,
  66                              const methodHandle& resolved_method,
  67                              const methodHandle& selected_method,
  68                              int itable_index, TRAPS) {
  69   // This is only called for interface methods. If the resolved_method
  70   // comes from java/lang/Object, it can be the subject of a virtual call, so
  71   // we should pick the vtable index from the resolved method.
  72   // In that case, the caller must call set_virtual instead of set_interface.
  73   assert(resolved_method->method_holder()->is_interface(), "");
  74   assert(itable_index == resolved_method()->itable_index(), "");
  75   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
  76 }
  77 
  78 void CallInfo::set_virtual(KlassHandle resolved_klass,
  79                            KlassHandle selected_klass,
  80                            const methodHandle& resolved_method,
  81                            const methodHandle& selected_method,
  82                            int vtable_index, TRAPS) {
  83   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
  84   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
  85   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
  86   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
  87   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
  88 }
  89 
  90 void CallInfo::set_handle(const methodHandle& resolved_method,
  91                           Handle resolved_appendix,
  92                           Handle resolved_method_type, TRAPS) {
  93   set_handle(SystemDictionary::MethodHandle_klass(), resolved_method, resolved_appendix, resolved_method_type, CHECK);
  94 }
  95 
  96 void CallInfo::set_handle(KlassHandle resolved_klass,
  97                           const methodHandle& resolved_method,
  98                           Handle resolved_appendix,
  99                           Handle resolved_method_type, TRAPS) {
 100   if (resolved_method.is_null()) {
 101     THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");
 102   }
 103   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 104          resolved_method->is_compiled_lambda_form(),
 105          "linkMethod must return one of these");
 106   int vtable_index = Method::nonvirtual_vtable_index;
 107   assert(!resolved_method->has_vtable_index(), "");
 108   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 109   _resolved_appendix    = resolved_appendix;
 110   _resolved_method_type = resolved_method_type;
 111 }
 112 
 113 void CallInfo::set_common(KlassHandle resolved_klass,
 114                           KlassHandle selected_klass,
 115                           const methodHandle& resolved_method,
 116                           const methodHandle& selected_method,
 117                           CallKind kind,
 118                           int index,
 119                           TRAPS) {
 120   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 121   _resolved_klass  = resolved_klass;
 122   _selected_klass  = selected_klass;
 123   _resolved_method = resolved_method;
 124   _selected_method = selected_method;
 125   _call_kind       = kind;
 126   _call_index      = index;
 127   _resolved_appendix = Handle();
 128   DEBUG_ONLY(verify());  // verify before making side effects
 129 
 130   CompilationPolicy::compile_if_required(selected_method, THREAD);
 131 }
 132 
 133 // utility query for unreflecting a method
 134 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) {


 145   int index = resolved_method->vtable_index();
 146   if (resolved_method->can_be_statically_bound()) {
 147     kind = CallInfo::direct_call;
 148   } else if (!resolved_method_holder->is_interface()) {
 149     // Could be an Object method inherited into an interface, but still a vtable call.
 150     kind = CallInfo::vtable_call;
 151   } else if (!resolved_klass->is_interface()) {
 152     // A default or miranda method.  Compute the vtable index.
 153     ResourceMark rm;
 154     klassVtable* vt = resolved_klass->vtable();
 155     index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
 156                            resolved_method);
 157     assert(index >= 0 , "we should have valid vtable index at this point");
 158 
 159     kind = CallInfo::vtable_call;
 160   } else if (resolved_method->has_vtable_index()) {
 161     // Can occur if an interface redeclares a method of Object.
 162 
 163 #ifdef ASSERT
 164     // Ensure that this is really the case.
 165     KlassHandle object_klass = SystemDictionary::Object_klass();
 166     Method * object_resolved_method = object_klass()->vtable()->method_at(index);
 167     assert(object_resolved_method->name() == resolved_method->name(),
 168       "Object and interface method names should match at vtable index %d, %s != %s",
 169       index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
 170     assert(object_resolved_method->signature() == resolved_method->signature(),
 171       "Object and interface method signatures should match at vtable index %d, %s != %s",
 172       index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
 173 #endif // ASSERT
 174 
 175     kind = CallInfo::vtable_call;
 176   } else {
 177     // A regular interface call.
 178     kind = CallInfo::itable_call;
 179     index = resolved_method->itable_index();
 180   }
 181   assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
 182   _call_kind  = kind;
 183   _call_index = index;
 184   _resolved_appendix = Handle();
 185   DEBUG_ONLY(verify());
 186 }


 208 
 209 #ifndef PRODUCT
 210 void CallInfo::print() {
 211   ResourceMark rm;
 212   const char* kindstr = "unknown";
 213   switch (_call_kind) {
 214   case direct_call: kindstr = "direct"; break;
 215   case vtable_call: kindstr = "vtable"; break;
 216   case itable_call: kindstr = "itable"; break;
 217   }
 218   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 219                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 220 }
 221 #endif
 222 
 223 //------------------------------------------------------------------------------------------------------------------------
 224 // Implementation of LinkInfo
 225 
 226 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, methodHandle current_method, TRAPS) {
 227    // resolve klass
 228   Klass* result = pool->klass_ref_at(index, CHECK);
 229   _resolved_klass = KlassHandle(THREAD, result);
 230 
 231   // Get name, signature, and static klass
 232   _name          = pool->name_ref_at(index);
 233   _signature     = pool->signature_ref_at(index);
 234   _tag           = pool->tag_ref_at(index);
 235   _current_klass = KlassHandle(THREAD, pool->pool_holder());
 236   _current_method = current_method;
 237 
 238   // Coming from the constant pool always checks access
 239   _check_access  = true;
 240 }
 241 
 242 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 243    // resolve klass
 244   Klass* result = pool->klass_ref_at(index, CHECK);
 245   _resolved_klass = KlassHandle(THREAD, result);
 246 
 247   // Get name, signature, and static klass
 248   _name          = pool->name_ref_at(index);
 249   _signature     = pool->signature_ref_at(index);
 250   _tag           = pool->tag_ref_at(index);
 251   _current_klass = KlassHandle(THREAD, pool->pool_holder());
 252   _current_method = methodHandle();
 253 
 254   // Coming from the constant pool always checks access
 255   _check_access  = true;
 256 }
 257 
 258 char* LinkInfo::method_string() const {
 259   return Method::name_and_sig_as_C_string(_resolved_klass(), _name, _signature);
 260 }
 261 
 262 #ifndef PRODUCT
 263 void LinkInfo::print() {
 264   ResourceMark rm;
 265   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 266                 _resolved_klass->name()->as_C_string(),
 267                 _name->as_C_string(),
 268                 _signature->as_C_string(),
 269                 _current_klass.is_null() ? "(none)" : _current_klass->name()->as_C_string(),
 270                 _check_access ? "true" : "false");
 271 }
 272 #endif // PRODUCT
 273 //------------------------------------------------------------------------------------------------------------------------
 274 // Klass resolution
 275 
 276 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
 277   Reflection::VerifyClassAccessResults vca_result =
 278     Reflection::verify_class_access(ref_klass(), InstanceKlass::cast(sel_klass()), true);
 279   if (vca_result != Reflection::ACCESS_OK) {
 280     ResourceMark rm(THREAD);
 281     char* msg = Reflection::verify_class_access_msg(ref_klass(),
 282                                                     InstanceKlass::cast(sel_klass()),
 283                                                     vca_result);
 284     if (msg == NULL) {
 285       Exceptions::fthrow(
 286         THREAD_AND_LOCATION,
 287         vmSymbols::java_lang_IllegalAccessError(),
 288         "failed to access class %s from class %s",
 289         sel_klass->external_name(),
 290         ref_klass->external_name());
 291     } else {
 292       // Use module specific message returned by verify_class_access_msg().
 293       Exceptions::fthrow(
 294         THREAD_AND_LOCATION,
 295         vmSymbols::java_lang_IllegalAccessError(),
 296         "%s", msg);
 297     }
 298   }
 299 }
 300 
 301 //------------------------------------------------------------------------------------------------------------------------
 302 // Method resolution
 303 //
 304 // According to JVM spec. $5.4.3c & $5.4.3d
 305 
 306 // Look up method in klasses, including static methods
 307 // Then look up local default methods
 308 methodHandle LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 309                                                     bool checkpolymorphism,
 310                                                     bool in_imethod_resolve, TRAPS) {
 311   KlassHandle klass = link_info.resolved_klass();
 312   Symbol* name = link_info.name();
 313   Symbol* signature = link_info.signature();
 314 
 315   // Ignore overpasses so statics can be found during resolution
 316   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 317 
 318   if (klass->is_array_klass()) {
 319     // Only consider klass and super klass for arrays
 320     return methodHandle(THREAD, result);
 321   }
 322 
 323   InstanceKlass* ik = InstanceKlass::cast(klass());
 324 
 325   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 326   // ignore static and non-public methods of java.lang.Object,
 327   // like clone, finalize, registerNatives.
 328   if (in_imethod_resolve &&
 329       result != NULL &&
 330       ik->is_interface() &&
 331       (result->is_static() || !result->is_public()) &&
 332       result->method_holder() == SystemDictionary::Object_klass()) {
 333     result = NULL;
 334   }
 335 
 336   // Before considering default methods, check for an overpass in the
 337   // current class if a method has not been found.
 338   if (result == NULL) {
 339     result = ik->find_method(name, signature);
 340   }
 341 
 342   if (result == NULL) {
 343     Array<Method*>* default_methods = ik->default_methods();
 344     if (default_methods != NULL) {
 345       result = InstanceKlass::find_method(default_methods, name, signature);
 346     }
 347   }
 348 
 349   if (checkpolymorphism && result != NULL) {
 350     vmIntrinsics::ID iid = result->intrinsic_id();
 351     if (MethodHandles::is_signature_polymorphic(iid)) {
 352       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 353       return NULL;
 354     }
 355   }
 356   return methodHandle(THREAD, result);
 357 }
 358 
 359 // returns first instance method
 360 // Looks up method in classes, then looks up local default methods
 361 methodHandle LinkResolver::lookup_instance_method_in_klasses(KlassHandle klass,
 362                                                              Symbol* name,
 363                                                              Symbol* signature, TRAPS) {
 364   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 365 
 366   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 367     Klass* super_klass = result->method_holder()->super();
 368     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 369   }
 370 
 371   if (klass->is_array_klass()) {
 372     // Only consider klass and super klass for arrays
 373     return methodHandle(THREAD, result);
 374   }
 375 
 376   if (result == NULL) {
 377     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
 378     if (default_methods != NULL) {
 379       result = InstanceKlass::find_method(default_methods, name, signature);
 380       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 381     }
 382   }
 383   return methodHandle(THREAD, result);
 384 }
 385 
 386 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
 387                                                    const methodHandle& resolved_method) {
 388 
 389   int vtable_index = Method::invalid_vtable_index;
 390   Symbol* name = resolved_method->name();
 391   Symbol* signature = resolved_method->signature();
 392   InstanceKlass* ik = InstanceKlass::cast(klass());
 393 
 394   // First check in default method array
 395   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 396     int index = InstanceKlass::find_method_index(ik->default_methods(),
 397                                                  name, signature, Klass::find_overpass,
 398                                                  Klass::find_static, Klass::find_private);
 399     if (index >= 0 ) {
 400       vtable_index = ik->default_vtable_indices()->at(index);
 401     }
 402   }
 403   if (vtable_index == Method::invalid_vtable_index) {
 404     // get vtable_index for miranda methods
 405     ResourceMark rm;
 406     klassVtable *vt = ik->vtable();
 407     vtable_index = vt->index_of_miranda(name, signature);
 408   }
 409   return vtable_index;
 410 }
 411 
 412 methodHandle LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info, TRAPS) {
 413   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass()());
 414 
 415   // Specify 'true' in order to skip default methods when searching the
 416   // interfaces.  Function lookup_method_in_klasses() already looked for
 417   // the method in the default methods table.
 418   return methodHandle(THREAD,
 419     ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(),
 420     Klass::skip_defaults));
 421 }
 422 
 423 methodHandle LinkResolver::lookup_polymorphic_method(
 424                                              const LinkInfo& link_info,
 425                                              Handle *appendix_result_or_null,
 426                                              Handle *method_type_result,
 427                                              TRAPS) {
 428   KlassHandle klass = link_info.resolved_klass();
 429   Symbol* name = link_info.name();
 430   Symbol* full_signature = link_info.signature();
 431 
 432   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 433   if (TraceMethodHandles) {
 434     ResourceMark rm(THREAD);
 435     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 436                   vmIntrinsics::name_at(iid), klass->external_name(),
 437                   name->as_C_string(), full_signature->as_C_string());
 438   }
 439   if ((klass() == SystemDictionary::MethodHandle_klass() ||
 440        klass() == SystemDictionary::VarHandle_klass()) &&
 441       iid != vmIntrinsics::_none) {
 442     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 443       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 444       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 445       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 446       TempNewSymbol basic_signature =
 447         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
 448       if (TraceMethodHandles) {
 449         ResourceMark rm(THREAD);
 450         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
 451                       name->as_C_string(),
 452                       full_signature->as_C_string(),
 453                       basic_signature->as_C_string());
 454       }
 455       methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid,
 456                                                               basic_signature,
 457                                                               CHECK_NULL);
 458       if (result.not_null()) {
 459         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 460         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");


 514         if (appendix.not_null())                                   expected_size_of_params += 1;
 515         if (actual_size_of_params != expected_size_of_params) {
 516           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 517           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 518           result->print();
 519         }
 520         assert(actual_size_of_params == expected_size_of_params,
 521                "%d != %d", actual_size_of_params, expected_size_of_params);
 522 #endif //ASSERT
 523 
 524         assert(appendix_result_or_null != NULL, "");
 525         (*appendix_result_or_null) = appendix;
 526         (*method_type_result)      = method_type;
 527       }
 528       return result;
 529     }
 530   }
 531   return NULL;
 532 }
 533 
 534 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
 535                                               KlassHandle resolved_klass,
 536                                               KlassHandle sel_klass,
 537                                               const methodHandle& sel_method,
 538                                               TRAPS) {
 539 
 540   AccessFlags flags = sel_method->access_flags();
 541 
 542   // Special case:  arrays always override "clone". JVMS 2.15.
 543   // If the resolved klass is an array class, and the declaring class
 544   // is java.lang.Object and the method is "clone", set the flags
 545   // to public.
 546   //
 547   // We'll check for the method name first, as that's most likely
 548   // to be false (so we'll short-circuit out of these tests).
 549   if (sel_method->name() == vmSymbols::clone_name() &&
 550       sel_klass() == SystemDictionary::Object_klass() &&
 551       resolved_klass->is_array_klass()) {
 552     // We need to change "protected" to "public".
 553     assert(flags.is_protected(), "clone not protected?");
 554     jint new_flags = flags.as_int();
 555     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 556     new_flags = new_flags | JVM_ACC_PUBLIC;
 557     flags.set_flags(new_flags);
 558   }
 559 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 560 
 561   if (!Reflection::verify_field_access(ref_klass(),
 562                                        resolved_klass(),
 563                                        sel_klass(),
 564                                        flags,
 565                                        true)) {
 566     ResourceMark rm(THREAD);
 567     Exceptions::fthrow(
 568       THREAD_AND_LOCATION,
 569       vmSymbols::java_lang_IllegalAccessError(),
 570       "tried to access method %s.%s%s from class %s",
 571       sel_klass->external_name(),
 572       sel_method->name()->as_C_string(),
 573       sel_method->signature()->as_C_string(),
 574       ref_klass->external_name()
 575     );
 576     return;
 577   }
 578 }
 579 
 580 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 581                                                      const constantPoolHandle& pool, int index, TRAPS) {
 582   // This method is used only
 583   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 584   // and
 585   // (2) in Bytecode_invoke::static_target
 586   // It appears to fail when applied to an invokeinterface call site.
 587   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 588   // resolve klass
 589   KlassHandle resolved_klass;
 590   if (code == Bytecodes::_invokedynamic) {
 591     resolved_klass = SystemDictionary::MethodHandle_klass();
 592     Symbol* method_name = vmSymbols::invoke_name();
 593     Symbol* method_signature = pool->signature_ref_at(index);
 594     KlassHandle  current_klass(THREAD, pool->pool_holder());
 595     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 596     return resolve_method(link_info, code, THREAD);
 597   }
 598 
 599   LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
 600   resolved_klass = link_info.resolved_klass();
 601 
 602   if (pool->has_preresolution()
 603       || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
 604           MethodHandles::is_signature_polymorphic_name(resolved_klass(), link_info.name()))) {
 605     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 606     if (result != NULL) {
 607       return methodHandle(THREAD, result);
 608     }
 609   }
 610 
 611   if (code == Bytecodes::_invokeinterface) {
 612     return resolve_interface_method(link_info, code, THREAD);
 613   } else if (code == Bytecodes::_invokevirtual) {
 614     return resolve_method(link_info, code, THREAD);
 615   } else if (!resolved_klass->is_interface()) {
 616     return resolve_method(link_info, code, THREAD);
 617   } else {
 618     return resolve_interface_method(link_info, code, THREAD);
 619   }
 620 }
 621 
 622 // Check and print a loader constraint violation message for method or interface method
 623 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 624                                                    const methodHandle& resolved_method,


 635       " \"%s\" the class loader (instance of %s) of the current class, %s,"
 636       " and the class loader (instance of %s) for the method's defining class, %s, have"
 637       " different Class objects for the type %s used in the signature";
 638     char* sig = link_info.method_string();
 639     const char* loader1_name = SystemDictionary::loader_name(current_loader());
 640     char* current = link_info.current_klass()->name()->as_C_string();
 641     const char* loader2_name = SystemDictionary::loader_name(resolved_loader());
 642     char* target = resolved_method->method_holder()->name()->as_C_string();
 643     char* failed_type_name = failed_type_symbol->as_C_string();
 644     size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) +
 645       strlen(current) + strlen(loader2_name) + strlen(target) +
 646       strlen(failed_type_name) + strlen(method_type) + 1;
 647     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 648     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
 649                  target, failed_type_name);
 650     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 651   }
 652 }
 653 
 654 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 655                                                   KlassHandle current_klass,
 656                                                   KlassHandle sel_klass, TRAPS) {
 657   Handle ref_loader(THREAD, current_klass->class_loader());
 658   Handle sel_loader(THREAD, sel_klass->class_loader());
 659 
 660   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 661   Symbol* failed_type_symbol =
 662     SystemDictionary::check_signature_loaders(sig,
 663                                               ref_loader, sel_loader,
 664                                               false,
 665                                               CHECK);
 666   if (failed_type_symbol != NULL) {
 667     const char* msg = "loader constraint violation: when resolving field"
 668       " \"%s\" the class loader (instance of %s) of the referring class, "
 669       "%s, and the class loader (instance of %s) for the field's resolved "
 670       "type, %s, have different Class objects for that type";
 671     char* field_name = field->as_C_string();
 672     const char* loader1_name = SystemDictionary::loader_name(ref_loader());
 673     char* sel = sel_klass->name()->as_C_string();
 674     const char* loader2_name = SystemDictionary::loader_name(sel_loader());
 675     char* failed_type_name = failed_type_symbol->as_C_string();
 676     size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) +
 677                     strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1;
 678     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 679     jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name,
 680                      failed_type_name);
 681     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 682   }
 683 }
 684 
 685 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 686                                           Bytecodes::Code code, TRAPS) {
 687 
 688   Handle nested_exception;
 689   KlassHandle resolved_klass = link_info.resolved_klass();
 690 
 691   // 1. For invokevirtual, cannot call an interface method
 692   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 693     ResourceMark rm(THREAD);
 694     char buf[200];
 695     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 696         resolved_klass()->external_name());
 697     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 698   }
 699 
 700   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 701   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 702     ResourceMark rm(THREAD);
 703     char buf[200];
 704     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 705     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 706   }
 707 
 708 
 709   // 3. lookup method in resolved klass and its super klasses
 710   methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL);
 711 
 712   // 4. lookup method in all the interfaces implemented by the resolved klass
 713   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 714     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 715 
 716     if (resolved_method.is_null()) {
 717       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 718       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 719       if (HAS_PENDING_EXCEPTION) {
 720         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 721         CLEAR_PENDING_EXCEPTION;
 722       }
 723     }
 724   }
 725 
 726   // 5. method lookup failed
 727   if (resolved_method.is_null()) {
 728     ResourceMark rm(THREAD);
 729     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 730                     Method::name_and_sig_as_C_string(resolved_klass(),
 731                                                      link_info.name(),
 732                                                      link_info.signature()),
 733                     nested_exception, NULL);
 734   }
 735 
 736   // 6. access checks, access checking may be turned off when calling from within the VM.
 737   KlassHandle current_klass = link_info.current_klass();
 738   if (link_info.check_access()) {
 739     assert(current_klass.not_null() , "current_klass should not be null");
 740 
 741     // check if method can be accessed by the referring class
 742     check_method_accessability(current_klass,
 743                                resolved_klass,
 744                                KlassHandle(THREAD, resolved_method->method_holder()),
 745                                resolved_method,
 746                                CHECK_NULL);
 747 
 748     // check loader constraints
 749     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 750   }
 751 
 752   return resolved_method;
 753 }
 754 
 755 static void trace_method_resolution(const char* prefix,
 756                                     KlassHandle klass,
 757                                     KlassHandle resolved_klass,
 758                                     const methodHandle& method,
 759                                     bool logitables,
 760                                     int index = -1) {
 761 #ifndef PRODUCT
 762   ResourceMark rm;
 763   outputStream* st;
 764   if (logitables) {
 765     st = Log(itables)::trace_stream();
 766   } else {
 767     st = Log(vtables)::trace_stream();
 768   }
 769   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 770             prefix,
 771             (klass.is_null() ? "<NULL>" : klass->internal_name()),
 772             (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
 773             Method::name_and_sig_as_C_string(resolved_klass(),
 774                                              method->name(),
 775                                              method->signature()),
 776             method->method_holder()->internal_name());
 777   method->print_linkage_flags(st);
 778   if (index != -1) {
 779     st->print("vtable_index:%d", index);
 780   }
 781   st->cr();
 782 #endif // PRODUCT
 783 }
 784 
 785 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 786 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 787 
 788   KlassHandle resolved_klass = link_info.resolved_klass();
 789 
 790   // check if klass is interface
 791   if (!resolved_klass->is_interface()) {
 792     ResourceMark rm(THREAD);
 793     char buf[200];
 794     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
 795     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 796   }
 797 
 798   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 799   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 800     ResourceMark rm(THREAD);
 801     char buf[200];
 802     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 803     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 804   }
 805 
 806   // lookup method in this interface or its super, java.lang.Object
 807   // JDK8: also look for static methods
 808   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
 809 
 810   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 811     // lookup method in all the super-interfaces
 812     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 813   }
 814 
 815   if (resolved_method.is_null()) {
 816     // no method found
 817     ResourceMark rm(THREAD);
 818     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 819                    Method::name_and_sig_as_C_string(resolved_klass(),
 820                                                     link_info.name(),
 821                                                     link_info.signature()));
 822   }
 823 
 824   if (link_info.check_access()) {
 825     // JDK8 adds non-public interface methods, and accessability check requirement
 826     KlassHandle current_klass = link_info.current_klass();
 827 
 828     assert(current_klass.not_null() , "current_klass should not be null");
 829 
 830     // check if method can be accessed by the referring class
 831     check_method_accessability(current_klass,
 832                                resolved_klass,
 833                                KlassHandle(THREAD, resolved_method->method_holder()),
 834                                resolved_method,
 835                                CHECK_NULL);
 836 
 837     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 838   }
 839 
 840   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 841     ResourceMark rm(THREAD);
 842     char buf[200];
 843     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 844                  Method::name_and_sig_as_C_string(resolved_klass(),
 845                  resolved_method->name(), resolved_method->signature()));
 846     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 847   }
 848 
 849   if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) {
 850     ResourceMark rm(THREAD);
 851     char buf[200];
 852 
 853     KlassHandle current_klass = link_info.current_klass();
 854     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s",
 855                  Method::name_and_sig_as_C_string(resolved_klass(),
 856                                                   resolved_method->name(),
 857                                                   resolved_method->signature()),
 858                                                   (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
 859      THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 860   }
 861 
 862   if (log_develop_is_enabled(Trace, itables)) {
 863     char buf[200];
 864     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 865                  Bytecodes::name(code));
 866     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 867                             resolved_method, true);
 868   }
 869 
 870   return resolved_method;
 871 }
 872 
 873 //------------------------------------------------------------------------------------------------------------------------
 874 // Field resolution
 875 
 876 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
 877                                              KlassHandle resolved_klass,
 878                                              KlassHandle sel_klass,
 879                                              const fieldDescriptor& fd,
 880                                              TRAPS) {
 881   if (!Reflection::verify_field_access(ref_klass(),
 882                                        resolved_klass(),
 883                                        sel_klass(),
 884                                        fd.access_flags(),
 885                                        true)) {
 886     ResourceMark rm(THREAD);
 887     Exceptions::fthrow(
 888       THREAD_AND_LOCATION,
 889       vmSymbols::java_lang_IllegalAccessError(),
 890       "tried to access field %s.%s from class %s",
 891       sel_klass->external_name(),
 892       fd.name()->as_C_string(),
 893       ref_klass->external_name()
 894     );
 895     return;
 896   }
 897 }
 898 
 899 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 900   LinkInfo link_info(pool, index, method, CHECK);
 901   resolve_field(fd, link_info, byte, true, CHECK);
 902 }
 903 
 904 void LinkResolver::resolve_field(fieldDescriptor& fd,
 905                                  const LinkInfo& link_info,
 906                                  Bytecodes::Code byte, bool initialize_class,
 907                                  TRAPS) {
 908   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 909          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 910          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 911          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 912 
 913   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 914   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 915   // Check if there's a resolved klass containing the field
 916   KlassHandle resolved_klass = link_info.resolved_klass();
 917   Symbol* field = link_info.name();
 918   Symbol* sig = link_info.signature();
 919 
 920   if (resolved_klass.is_null()) {
 921     ResourceMark rm(THREAD);
 922     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 923   }
 924 
 925   // Resolve instance field
 926   KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd));
 927   // check if field exists; i.e., if a klass containing the field def has been selected
 928   if (sel_klass.is_null()) {
 929     ResourceMark rm(THREAD);
 930     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 931   }
 932 
 933   if (!link_info.check_access())
 934     // Access checking may be turned off when calling from within the VM.
 935     return;
 936 
 937   // check access
 938   KlassHandle current_klass = link_info.current_klass();
 939   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 940 
 941   // check for errors
 942   if (is_static != fd.is_static()) {
 943     ResourceMark rm(THREAD);
 944     char msg[200];
 945     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
 946     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 947   }
 948 
 949   // A final field can be modified only
 950   // (1) by methods declared in the class declaring the field and
 951   // (2) by the <clinit> method (in case of a static field)
 952   //     or by the <init> method (in case of an instance field).
 953   if (is_put && fd.access_flags().is_final()) {
 954     ResourceMark rm(THREAD);
 955     stringStream ss;
 956 
 957     if (sel_klass() != current_klass()) {
 958       ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
 959                 is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string(),
 960                 current_klass()->external_name());
 961       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 962     }
 963 
 964     if (fd.constants()->pool_holder()->major_version() >= 53) {
 965       methodHandle m = link_info.current_method();
 966       assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
 967       bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
 968                                                  fd.is_static() &&
 969                                                  !m()->is_static_initializer());
 970       bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
 971                                                    !fd.is_static() &&
 972                                                    !m->is_object_initializer());
 973 
 974       if (is_initialized_static_final_update || is_initialized_instance_final_update) {
 975         ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
 976                  is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string(),
 977                  m()->name()->as_C_string(),
 978                  is_static ? "<clinit>" : "<init>");
 979         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 980       }
 981     }
 982   }
 983 
 984   // initialize resolved_klass if necessary
 985   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
 986   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
 987   //
 988   // note 2: we don't want to force initialization if we are just checking
 989   //         if the field access is legal; e.g., during compilation
 990   if (is_static && initialize_class) {
 991     sel_klass->initialize(CHECK);
 992   }
 993 
 994   if (sel_klass() != current_klass()) {
 995     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
 996   }
 997 
 998   // return information. note that the klass is set to the actual klass containing the
 999   // field, otherwise access of static fields in superclasses will not work.
1000 }
1001 
1002 
1003 //------------------------------------------------------------------------------------------------------------------------
1004 // Invoke resolution
1005 //
1006 // Naming conventions:
1007 //
1008 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1009 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1010 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1011 // recv_klass         the receiver klass
1012 
1013 
1014 void LinkResolver::resolve_static_call(CallInfo& result,
1015                                        const LinkInfo& link_info,
1016                                        bool initialize_class, TRAPS) {
1017   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
1018 
1019   // The resolved class can change as a result of this resolution.
1020   KlassHandle resolved_klass(THREAD, resolved_method->method_holder());
1021 
1022   // Initialize klass (this should only happen if everything is ok)
1023   if (initialize_class && resolved_klass->should_be_initialized()) {
1024     resolved_klass->initialize(CHECK);
1025     // Use updated LinkInfo to reresolve with resolved method holder
1026     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1027                       link_info.current_klass(),
1028                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1029     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1030   }
1031 
1032   // setup result
1033   result.set_static(resolved_klass, resolved_method, CHECK);
1034 }
1035 
1036 // throws linktime exceptions
1037 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1038 
1039   KlassHandle resolved_klass = link_info.resolved_klass();
1040   methodHandle resolved_method;
1041   if (!resolved_klass->is_interface()) {
1042     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1043   } else {
1044     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1045   }
1046   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1047 
1048   // check if static
1049   if (!resolved_method->is_static()) {
1050     ResourceMark rm(THREAD);
1051     char buf[200];
1052     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1053                                                       resolved_method->name(),
1054                                                       resolved_method->signature()));
1055     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1056   }
1057   return resolved_method;
1058 }
1059 
1060 
1061 void LinkResolver::resolve_special_call(CallInfo& result,
1062                                         const LinkInfo& link_info,
1063                                         TRAPS) {
1064   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1065   runtime_resolve_special_method(result, resolved_method,
1066                                  link_info.resolved_klass(),
1067                                  link_info.current_klass(),
1068                                  link_info.check_access(), CHECK);
1069 }
1070 
1071 // throws linktime exceptions
1072 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1073                                                            TRAPS) {
1074 
1075   // Invokespecial is called for multiple special reasons:
1076   // <init>
1077   // local private method invocation, for classes and interfaces
1078   // superclass.method, which can also resolve to a default method
1079   // and the selected method is recalculated relative to the direct superclass
1080   // superinterface.method, which explicitly does not check shadowing
1081   KlassHandle resolved_klass = link_info.resolved_klass();
1082   methodHandle resolved_method;
1083 
1084   if (!resolved_klass->is_interface()) {
1085     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1086   } else {
1087     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1088   }
1089 
1090   // check if method name is <init>, that it is found in same klass as static type
1091   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1092       resolved_method->method_holder() != resolved_klass()) {
1093     ResourceMark rm(THREAD);
1094     Exceptions::fthrow(
1095       THREAD_AND_LOCATION,
1096       vmSymbols::java_lang_NoSuchMethodError(),
1097       "%s: method %s%s not found",
1098       resolved_klass->external_name(),
1099       resolved_method->name()->as_C_string(),
1100       resolved_method->signature()->as_C_string()
1101     );
1102     return NULL;
1103   }
1104 
1105   // check if invokespecial's interface method reference is in an indirect superinterface
1106   KlassHandle current_klass = link_info.current_klass();
1107   if (!current_klass.is_null() && resolved_klass->is_interface()) {
1108     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
1109                                   current_klass() :
1110                                   InstanceKlass::cast(current_klass())->host_klass();

1111     // Disable verification for the dynamically-generated reflection bytecodes.
1112     bool is_reflect = klass_to_check->is_subclass_of(
1113                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1114 
1115     if (!is_reflect &&
1116         !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {
1117       ResourceMark rm(THREAD);
1118       char buf[200];
1119       jio_snprintf(buf, sizeof(buf),
1120                    "Interface method reference: %s, is in an indirect superinterface of %s",
1121                    Method::name_and_sig_as_C_string(resolved_klass(),
1122                                                          resolved_method->name(),
1123                                                          resolved_method->signature()),
1124                    current_klass->external_name());
1125       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1126     }
1127   }
1128 
1129   // check if not static
1130   if (resolved_method->is_static()) {
1131     ResourceMark rm(THREAD);
1132     char buf[200];
1133     jio_snprintf(buf, sizeof(buf),
1134                  "Expecting non-static method %s",
1135                  Method::name_and_sig_as_C_string(resolved_klass(),
1136                                                   resolved_method->name(),
1137                                                   resolved_method->signature()));
1138     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1139   }
1140 
1141   if (log_develop_is_enabled(Trace, itables)) {
1142     trace_method_resolution("invokespecial resolved method: caller-class:",
1143                             current_klass, resolved_klass, resolved_method, true);
1144   }
1145 
1146   return resolved_method;
1147 }
1148 
1149 // throws runtime exceptions
1150 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1151                                                   const methodHandle& resolved_method,
1152                                                   KlassHandle resolved_klass,
1153                                                   KlassHandle current_klass,
1154                                                   bool check_access, TRAPS) {
1155 
1156   // resolved method is selected method unless we have an old-style lookup
1157   // for a superclass method
1158   // Invokespecial for a superinterface, resolved method is selected method,
1159   // no checks for shadowing
1160   methodHandle sel_method(THREAD, resolved_method());
1161 
1162   // check if this is an old-style super call and do a new lookup if so
1163   { KlassHandle method_klass  = KlassHandle(THREAD,
1164                                             resolved_method->method_holder());
1165 
1166     if (check_access &&
1167         // a) check if ACC_SUPER flag is set for the current class
1168         (current_klass->is_super() || !AllowNonVirtualCalls) &&
1169         // b) check if the class of the resolved_klass is a superclass
1170         // (not supertype in order to exclude interface classes) of the current class.
1171         // This check is not performed for super.invoke for interface methods
1172         // in super interfaces.
1173         current_klass->is_subclass_of(resolved_klass()) &&
1174         current_klass() != resolved_klass() &&
1175         // c) check if the method is not <init>
1176         resolved_method->name() != vmSymbols::object_initializer_name()) {
1177       // Lookup super method
1178       KlassHandle super_klass(THREAD, current_klass->super());
1179       sel_method = lookup_instance_method_in_klasses(super_klass,
1180                            resolved_method->name(),
1181                            resolved_method->signature(), CHECK);
1182       // check if found
1183       if (sel_method.is_null()) {
1184         ResourceMark rm(THREAD);
1185         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1186                   Method::name_and_sig_as_C_string(resolved_klass(),
1187                                             resolved_method->name(),
1188                                             resolved_method->signature()));
1189       }
1190     }
1191   }
1192 
1193   // check if not static
1194   if (sel_method->is_static()) {
1195     ResourceMark rm(THREAD);
1196     char buf[200];
1197     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1198                                                                                                              resolved_method->name(),
1199                                                                                                              resolved_method->signature()));
1200     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1201   }
1202 
1203   // check if abstract
1204   if (sel_method->is_abstract()) {
1205     ResourceMark rm(THREAD);
1206     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1207               Method::name_and_sig_as_C_string(resolved_klass(),
1208                                                sel_method->name(),
1209                                                sel_method->signature()));
1210   }
1211 
1212   if (log_develop_is_enabled(Trace, itables)) {
1213     trace_method_resolution("invokespecial selected method: resolved-class:",
1214                             resolved_klass, resolved_klass, sel_method, true);
1215   }
1216 
1217   // setup result
1218   result.set_static(resolved_klass, sel_method, CHECK);
1219 }
1220 
1221 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass,
1222                                         const LinkInfo& link_info,
1223                                         bool check_null_and_abstract, TRAPS) {
1224   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1225   runtime_resolve_virtual_method(result, resolved_method,
1226                                  link_info.resolved_klass(),
1227                                  recv, receiver_klass,
1228                                  check_null_and_abstract, CHECK);
1229 }
1230 
1231 // throws linktime exceptions
1232 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1233                                                            TRAPS) {
1234   // normal method resolution
1235   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1236 
1237   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1238   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1239 
1240   // check if private interface method
1241   KlassHandle resolved_klass = link_info.resolved_klass();
1242   KlassHandle current_klass = link_info.current_klass();
1243 
1244   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1245   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1246     ResourceMark rm(THREAD);
1247     char buf[200];
1248     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1249                  Method::name_and_sig_as_C_string(resolved_klass(),
1250                                                   resolved_method->name(),
1251                                                   resolved_method->signature()),
1252                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1253     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1254   }
1255 
1256   // check if not static
1257   if (resolved_method->is_static()) {
1258     ResourceMark rm(THREAD);
1259     char buf[200];
1260     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1261                                                                                                              resolved_method->name(),
1262                                                                                                              resolved_method->signature()));
1263     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1264   }
1265 
1266   if (log_develop_is_enabled(Trace, vtables)) {
1267     trace_method_resolution("invokevirtual resolved method: caller-class:",
1268                             current_klass, resolved_klass, resolved_method, false);
1269   }
1270 
1271   return resolved_method;
1272 }
1273 
1274 // throws runtime exceptions
1275 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1276                                                   const methodHandle& resolved_method,
1277                                                   KlassHandle resolved_klass,
1278                                                   Handle recv,
1279                                                   KlassHandle recv_klass,
1280                                                   bool check_null_and_abstract,
1281                                                   TRAPS) {
1282 
1283   // setup default return values
1284   int vtable_index = Method::invalid_vtable_index;
1285   methodHandle selected_method;
1286 
1287   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1288 
1289   // runtime method resolution
1290   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1291     THROW(vmSymbols::java_lang_NullPointerException());
1292   }
1293 
1294   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1295   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1296   // a missing receiver might result in a bogus lookup.
1297   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1298 
1299   // do lookup based on receiver klass using the vtable index


1308     // a default or miranda method; therefore, it must have a valid vtable index.
1309     assert(!resolved_method->has_itable_index(), "");
1310     vtable_index = resolved_method->vtable_index();
1311     // We could get a negative vtable_index for final methods,
1312     // because as an optimization they are they are never put in the vtable,
1313     // unless they override an existing method.
1314     // If we do get a negative, it means the resolved method is the the selected
1315     // method, and it can never be changed by an override.
1316     if (vtable_index == Method::nonvirtual_vtable_index) {
1317       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1318       selected_method = resolved_method;
1319     } else {
1320       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1321     }
1322   }
1323 
1324   // check if method exists
1325   if (selected_method.is_null()) {
1326     ResourceMark rm(THREAD);
1327     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1328               Method::name_and_sig_as_C_string(resolved_klass(),
1329                                                       resolved_method->name(),
1330                                                       resolved_method->signature()));
1331   }
1332 
1333   // check if abstract
1334   if (check_null_and_abstract && selected_method->is_abstract()) {
1335     ResourceMark rm(THREAD);
1336     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1337               Method::name_and_sig_as_C_string(resolved_klass(),
1338                                                       selected_method->name(),
1339                                                       selected_method->signature()));
1340   }
1341 
1342   if (log_develop_is_enabled(Trace, vtables)) {
1343     trace_method_resolution("invokevirtual selected method: receiver-class:",
1344                             recv_klass, resolved_klass, selected_method,
1345                             false, vtable_index);
1346   }
1347   // setup result
1348   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1349 }
1350 
1351 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass,
1352                                           const LinkInfo& link_info,
1353                                           bool check_null_and_abstract, TRAPS) {
1354   // throws linktime exceptions
1355   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1356   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1357                                    recv, recv_klass, check_null_and_abstract, CHECK);
1358 }
1359 
1360 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1361                                                              TRAPS) {
1362   // normal interface method resolution
1363   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1364   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1365   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1366 
1367   return resolved_method;
1368 }
1369 
1370 // throws runtime exceptions
1371 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1372                                                     const methodHandle& resolved_method,
1373                                                     KlassHandle resolved_klass,
1374                                                     Handle recv,
1375                                                     KlassHandle recv_klass,
1376                                                     bool check_null_and_abstract, TRAPS) {
1377   // check if receiver exists
1378   if (check_null_and_abstract && recv.is_null()) {
1379     THROW(vmSymbols::java_lang_NullPointerException());
1380   }
1381 
1382   // check if receiver klass implements the resolved interface
1383   if (!recv_klass->is_subtype_of(resolved_klass())) {
1384     ResourceMark rm(THREAD);
1385     char buf[200];
1386     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1387                  recv_klass()->external_name(),
1388                  resolved_klass()->external_name());
1389     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1390   }
1391 
1392   // do lookup based on receiver klass
1393   // This search must match the linktime preparation search for itable initialization
1394   // to correctly enforce loader constraints for interface method inheritance
1395   methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass,
1396                                                   resolved_method->name(),
1397                                                   resolved_method->signature(), CHECK);
1398   if (sel_method.is_null() && !check_null_and_abstract) {
1399     // In theory this is a harmless placeholder value, but
1400     // in practice leaving in null affects the nsk default method tests.
1401     // This needs further study.
1402     sel_method = resolved_method;
1403   }
1404   // check if method exists
1405   if (sel_method.is_null()) {
1406     ResourceMark rm(THREAD);
1407     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1408                    Method::name_and_sig_as_C_string(recv_klass(),
1409                                                     resolved_method->name(),
1410                                                     resolved_method->signature()));
1411   }
1412   // check access
1413   // Throw Illegal Access Error if sel_method is not public.
1414   if (!sel_method->is_public()) {
1415     ResourceMark rm(THREAD);
1416     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1417               Method::name_and_sig_as_C_string(recv_klass(),
1418                                                sel_method->name(),
1419                                                sel_method->signature()));
1420   }
1421   // check if abstract
1422   if (check_null_and_abstract && sel_method->is_abstract()) {
1423     ResourceMark rm(THREAD);
1424     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1425               Method::name_and_sig_as_C_string(recv_klass(),
1426                                                       sel_method->name(),
1427                                                       sel_method->signature()));
1428   }
1429 
1430   if (log_develop_is_enabled(Trace, itables)) {
1431     trace_method_resolution("invokeinterface selected method: receiver-class:",
1432                             recv_klass, resolved_klass, sel_method, true);
1433   }
1434   // setup result
1435   if (!resolved_method->has_itable_index()) {
1436     int vtable_index = resolved_method->vtable_index();
1437     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1438     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1439   } else {
1440     int itable_index = resolved_method()->itable_index();
1441     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1442   }
1443 }
1444 
1445 


1451     CLEAR_PENDING_EXCEPTION;
1452     return methodHandle();
1453   } else {
1454     return method_result;
1455   }
1456 }
1457 
1458 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1459                                                  const LinkInfo& link_info) {
1460   EXCEPTION_MARK;
1461   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1462   if (HAS_PENDING_EXCEPTION) {
1463     CLEAR_PENDING_EXCEPTION;
1464     return methodHandle();
1465   } else {
1466     return method_result;
1467   }
1468 }
1469 
1470 methodHandle LinkResolver::resolve_virtual_call_or_null(
1471                                                  KlassHandle receiver_klass,
1472                                                  const LinkInfo& link_info) {
1473   EXCEPTION_MARK;
1474   CallInfo info;
1475   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1476   if (HAS_PENDING_EXCEPTION) {
1477     CLEAR_PENDING_EXCEPTION;
1478     return methodHandle();
1479   }
1480   return info.selected_method();
1481 }
1482 
1483 methodHandle LinkResolver::resolve_interface_call_or_null(
1484                                                  KlassHandle receiver_klass,
1485                                                  const LinkInfo& link_info) {
1486   EXCEPTION_MARK;
1487   CallInfo info;
1488   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1489   if (HAS_PENDING_EXCEPTION) {
1490     CLEAR_PENDING_EXCEPTION;
1491     return methodHandle();
1492   }
1493   return info.selected_method();
1494 }
1495 
1496 int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass,
1497                                                const LinkInfo& link_info) {
1498   EXCEPTION_MARK;
1499   CallInfo info;
1500   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1501                        /*check_null_or_abstract*/false, THREAD);
1502   if (HAS_PENDING_EXCEPTION) {
1503     CLEAR_PENDING_EXCEPTION;
1504     return Method::invalid_vtable_index;
1505   }
1506   return info.vtable_index();
1507 }
1508 
1509 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1510   EXCEPTION_MARK;
1511   CallInfo info;
1512   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1513   if (HAS_PENDING_EXCEPTION) {
1514     CLEAR_PENDING_EXCEPTION;
1515     return methodHandle();
1516   }


1531 
1532 
1533 //------------------------------------------------------------------------------------------------------------------------
1534 // ConstantPool entries
1535 
1536 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1537   switch (byte) {
1538     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1539     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1540     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1541     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1542     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1543     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1544   }
1545   return;
1546 }
1547 
1548 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1549                              const methodHandle& attached_method,
1550                              Bytecodes::Code byte, TRAPS) {
1551   KlassHandle defc = attached_method->method_holder();
1552   Symbol* name = attached_method->name();
1553   Symbol* type = attached_method->signature();
1554   LinkInfo link_info(defc, name, type);
1555   switch(byte) {
1556     case Bytecodes::_invokevirtual:
1557       resolve_virtual_call(result, recv, recv->klass(), link_info,
1558                            /*check_null_and_abstract=*/true, CHECK);
1559       break;
1560     case Bytecodes::_invokeinterface:
1561       resolve_interface_call(result, recv, recv->klass(), link_info,
1562                              /*check_null_and_abstract=*/true, CHECK);
1563       break;
1564     case Bytecodes::_invokestatic:
1565       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1566       break;
1567     case Bytecodes::_invokespecial:
1568       resolve_special_call(result, link_info, CHECK);
1569       break;
1570     default:
1571       fatal("bad call: %s", Bytecodes::name(byte));
1572   }
1573 }
1574 
1575 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1576   LinkInfo link_info(pool, index, CHECK);
1577   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1578 }
1579 
1580 
1581 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1582   LinkInfo link_info(pool, index, CHECK);
1583   resolve_special_call(result, link_info, CHECK);
1584 }
1585 
1586 
1587 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1588                                           const constantPoolHandle& pool, int index,
1589                                           TRAPS) {
1590 
1591   LinkInfo link_info(pool, index, CHECK);
1592   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1593   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1594 }
1595 
1596 
1597 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1598   LinkInfo link_info(pool, index, CHECK);
1599   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1600   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1601 }
1602 
1603 
1604 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1605   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1606   LinkInfo link_info(pool, index, CHECK);
1607   if (TraceMethodHandles) {
1608     ResourceMark rm(THREAD);
1609     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1610                   link_info.signature()->as_C_string());
1611   }
1612   resolve_handle_call(result, link_info, CHECK);
1613 }
1614 
1615 void LinkResolver::resolve_handle_call(CallInfo& result,
1616                                        const LinkInfo& link_info,
1617                                        TRAPS) {
1618   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1619   KlassHandle resolved_klass = link_info.resolved_klass();
1620   assert(resolved_klass() == SystemDictionary::MethodHandle_klass() ||
1621          resolved_klass() == SystemDictionary::VarHandle_klass(), "");
1622   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1623   Handle       resolved_appendix;
1624   Handle       resolved_method_type;
1625   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1626                                        &resolved_appendix, &resolved_method_type, CHECK);
1627   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1628 }
1629 
1630 static void wrap_invokedynamic_exception(TRAPS) {
1631   if (HAS_PENDING_EXCEPTION) {
1632     // See the "Linking Exceptions" section for the invokedynamic instruction
1633     // in JVMS 6.5.
1634     if (PENDING_EXCEPTION->is_a(SystemDictionary::Error_klass())) {
1635       // Pass through an Error, including BootstrapMethodError, any other form
1636       // of linkage error, or say ThreadDeath/OutOfMemoryError
1637       if (TraceMethodHandles) {
1638         tty->print_cr("invokedynamic passes through an Error for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1639         PENDING_EXCEPTION->print();
1640       }
1641       return;
1642     }
1643 
1644     // Otherwise wrap the exception in a BootstrapMethodError
1645     if (TraceMethodHandles) {
1646       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1647       PENDING_EXCEPTION->print();
1648     }
1649     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1650     CLEAR_PENDING_EXCEPTION;
1651     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1652   }
1653 }
1654 
1655 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1656   Symbol* method_name       = pool->name_ref_at(index);
1657   Symbol* method_signature  = pool->signature_ref_at(index);
1658   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1659 
1660   // Resolve the bootstrap specifier (BSM + optional arguments).
1661   Handle bootstrap_specifier;
1662   // Check if CallSite has been bound already:
1663   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1664   if (cpce->is_f1_null()) {
1665     int pool_index = cpce->constant_pool_index();
1666     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1667     wrap_invokedynamic_exception(CHECK);
1668     assert(bsm_info != NULL, "");
1669     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1670     bootstrap_specifier = Handle(THREAD, bsm_info);
1671   }
1672   if (!cpce->is_f1_null()) {
1673     methodHandle method(     THREAD, cpce->f1_as_method());
1674     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1675     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1676     result.set_handle(method, appendix, method_type, THREAD);
1677     wrap_invokedynamic_exception(CHECK);
1678     return;
1679   }
1680 
1681   if (TraceMethodHandles) {
1682     ResourceMark rm(THREAD);
1683     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1684                   ConstantPool::decode_invokedynamic_index(index),
1685                   method_name->as_C_string(), method_signature->as_C_string(),
1686                   current_klass->name()->as_C_string());
1687     tty->print("  BSM info: "); bootstrap_specifier->print();
1688   }
1689 
1690   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1691 }
1692 
1693 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1694                                         Handle bootstrap_specifier,
1695                                         Symbol* method_name, Symbol* method_signature,
1696                                         KlassHandle current_klass,
1697                                         TRAPS) {
1698   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1699   // The appendix argument is likely to be a freshly-created CallSite.
1700   Handle       resolved_appendix;
1701   Handle       resolved_method_type;
1702   methodHandle resolved_method =
1703     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1704                                                      bootstrap_specifier,
1705                                                      method_name, method_signature,
1706                                                      &resolved_appendix,
1707                                                      &resolved_method_type,
1708                                                      THREAD);
1709   wrap_invokedynamic_exception(CHECK);
1710   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1711   wrap_invokedynamic_exception(CHECK);
1712 }


  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/method.hpp"
  40 #include "oops/objArrayOop.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "prims/methodHandles.hpp"
  43 #include "prims/nativeLookup.hpp"
  44 #include "runtime/compilationPolicy.hpp"
  45 #include "runtime/fieldDescriptor.hpp"
  46 #include "runtime/frame.inline.hpp"
  47 #include "runtime/handles.inline.hpp"
  48 #include "runtime/reflection.hpp"
  49 #include "runtime/signature.hpp"
  50 #include "runtime/thread.inline.hpp"
  51 #include "runtime/vmThread.hpp"
  52 
  53 
  54 //------------------------------------------------------------------------------------------------------------------------
  55 // Implementation of CallInfo
  56 
  57 
  58 void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
  59   int vtable_index = Method::nonvirtual_vtable_index;
  60   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  61 }
  62 
  63 
  64 void CallInfo::set_interface(Klass* resolved_klass,
  65                              Klass* selected_klass,
  66                              const methodHandle& resolved_method,
  67                              const methodHandle& selected_method,
  68                              int itable_index, TRAPS) {
  69   // This is only called for interface methods. If the resolved_method
  70   // comes from java/lang/Object, it can be the subject of a virtual call, so
  71   // we should pick the vtable index from the resolved method.
  72   // In that case, the caller must call set_virtual instead of set_interface.
  73   assert(resolved_method->method_holder()->is_interface(), "");
  74   assert(itable_index == resolved_method()->itable_index(), "");
  75   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
  76 }
  77 
  78 void CallInfo::set_virtual(Klass* resolved_klass,
  79                            Klass* selected_klass,
  80                            const methodHandle& resolved_method,
  81                            const methodHandle& selected_method,
  82                            int vtable_index, TRAPS) {
  83   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
  84   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
  85   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
  86   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
  87   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
  88 }
  89 
  90 void CallInfo::set_handle(const methodHandle& resolved_method,
  91                           Handle resolved_appendix,
  92                           Handle resolved_method_type, TRAPS) {
  93   set_handle(SystemDictionary::MethodHandle_klass(), resolved_method, resolved_appendix, resolved_method_type, CHECK);
  94 }
  95 
  96 void CallInfo::set_handle(Klass* resolved_klass,
  97                           const methodHandle& resolved_method,
  98                           Handle resolved_appendix,
  99                           Handle resolved_method_type, TRAPS) {
 100   if (resolved_method.is_null()) {
 101     THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");
 102   }
 103   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 104          resolved_method->is_compiled_lambda_form(),
 105          "linkMethod must return one of these");
 106   int vtable_index = Method::nonvirtual_vtable_index;
 107   assert(!resolved_method->has_vtable_index(), "");
 108   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 109   _resolved_appendix    = resolved_appendix;
 110   _resolved_method_type = resolved_method_type;
 111 }
 112 
 113 void CallInfo::set_common(Klass* resolved_klass,
 114                           Klass* selected_klass,
 115                           const methodHandle& resolved_method,
 116                           const methodHandle& selected_method,
 117                           CallKind kind,
 118                           int index,
 119                           TRAPS) {
 120   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 121   _resolved_klass  = resolved_klass;
 122   _selected_klass  = selected_klass;
 123   _resolved_method = resolved_method;
 124   _selected_method = selected_method;
 125   _call_kind       = kind;
 126   _call_index      = index;
 127   _resolved_appendix = Handle();
 128   DEBUG_ONLY(verify());  // verify before making side effects
 129 
 130   CompilationPolicy::compile_if_required(selected_method, THREAD);
 131 }
 132 
 133 // utility query for unreflecting a method
 134 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) {


 145   int index = resolved_method->vtable_index();
 146   if (resolved_method->can_be_statically_bound()) {
 147     kind = CallInfo::direct_call;
 148   } else if (!resolved_method_holder->is_interface()) {
 149     // Could be an Object method inherited into an interface, but still a vtable call.
 150     kind = CallInfo::vtable_call;
 151   } else if (!resolved_klass->is_interface()) {
 152     // A default or miranda method.  Compute the vtable index.
 153     ResourceMark rm;
 154     klassVtable* vt = resolved_klass->vtable();
 155     index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
 156                            resolved_method);
 157     assert(index >= 0 , "we should have valid vtable index at this point");
 158 
 159     kind = CallInfo::vtable_call;
 160   } else if (resolved_method->has_vtable_index()) {
 161     // Can occur if an interface redeclares a method of Object.
 162 
 163 #ifdef ASSERT
 164     // Ensure that this is really the case.
 165     Klass* object_klass = SystemDictionary::Object_klass();
 166     Method * object_resolved_method = object_klass->vtable()->method_at(index);
 167     assert(object_resolved_method->name() == resolved_method->name(),
 168       "Object and interface method names should match at vtable index %d, %s != %s",
 169       index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
 170     assert(object_resolved_method->signature() == resolved_method->signature(),
 171       "Object and interface method signatures should match at vtable index %d, %s != %s",
 172       index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
 173 #endif // ASSERT
 174 
 175     kind = CallInfo::vtable_call;
 176   } else {
 177     // A regular interface call.
 178     kind = CallInfo::itable_call;
 179     index = resolved_method->itable_index();
 180   }
 181   assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
 182   _call_kind  = kind;
 183   _call_index = index;
 184   _resolved_appendix = Handle();
 185   DEBUG_ONLY(verify());
 186 }


 208 
 209 #ifndef PRODUCT
 210 void CallInfo::print() {
 211   ResourceMark rm;
 212   const char* kindstr = "unknown";
 213   switch (_call_kind) {
 214   case direct_call: kindstr = "direct"; break;
 215   case vtable_call: kindstr = "vtable"; break;
 216   case itable_call: kindstr = "itable"; break;
 217   }
 218   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 219                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 220 }
 221 #endif
 222 
 223 //------------------------------------------------------------------------------------------------------------------------
 224 // Implementation of LinkInfo
 225 
 226 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, methodHandle current_method, TRAPS) {
 227    // resolve klass
 228   _resolved_klass = pool->klass_ref_at(index, CHECK);

 229 
 230   // Get name, signature, and static klass
 231   _name          = pool->name_ref_at(index);
 232   _signature     = pool->signature_ref_at(index);
 233   _tag           = pool->tag_ref_at(index);
 234   _current_klass = pool->pool_holder();
 235   _current_method = current_method;
 236 
 237   // Coming from the constant pool always checks access
 238   _check_access  = true;
 239 }
 240 
 241 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 242    // resolve klass
 243   _resolved_klass = pool->klass_ref_at(index, CHECK);

 244 
 245   // Get name, signature, and static klass
 246   _name          = pool->name_ref_at(index);
 247   _signature     = pool->signature_ref_at(index);
 248   _tag           = pool->tag_ref_at(index);
 249   _current_klass = pool->pool_holder();
 250   _current_method = methodHandle();
 251 
 252   // Coming from the constant pool always checks access
 253   _check_access  = true;
 254 }
 255 
 256 char* LinkInfo::method_string() const {
 257   return Method::name_and_sig_as_C_string(_resolved_klass, _name, _signature);
 258 }
 259 
 260 #ifndef PRODUCT
 261 void LinkInfo::print() {
 262   ResourceMark rm;
 263   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 264                 _resolved_klass->name()->as_C_string(),
 265                 _name->as_C_string(),
 266                 _signature->as_C_string(),
 267                 _current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(),
 268                 _check_access ? "true" : "false");
 269 }
 270 #endif // PRODUCT
 271 //------------------------------------------------------------------------------------------------------------------------
 272 // Klass resolution
 273 
 274 void LinkResolver::check_klass_accessability(Klass* ref_klass, Klass* sel_klass, TRAPS) {
 275   Reflection::VerifyClassAccessResults vca_result =
 276     Reflection::verify_class_access(ref_klass, InstanceKlass::cast(sel_klass), true);
 277   if (vca_result != Reflection::ACCESS_OK) {
 278     ResourceMark rm(THREAD);
 279     char* msg = Reflection::verify_class_access_msg(ref_klass,
 280                                                     InstanceKlass::cast(sel_klass),
 281                                                     vca_result);
 282     if (msg == NULL) {
 283       Exceptions::fthrow(
 284         THREAD_AND_LOCATION,
 285         vmSymbols::java_lang_IllegalAccessError(),
 286         "failed to access class %s from class %s",
 287         sel_klass->external_name(),
 288         ref_klass->external_name());
 289     } else {
 290       // Use module specific message returned by verify_class_access_msg().
 291       Exceptions::fthrow(
 292         THREAD_AND_LOCATION,
 293         vmSymbols::java_lang_IllegalAccessError(),
 294         "%s", msg);
 295     }
 296   }
 297 }
 298 
 299 //------------------------------------------------------------------------------------------------------------------------
 300 // Method resolution
 301 //
 302 // According to JVM spec. $5.4.3c & $5.4.3d
 303 
 304 // Look up method in klasses, including static methods
 305 // Then look up local default methods
 306 methodHandle LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 307                                                     bool checkpolymorphism,
 308                                                     bool in_imethod_resolve, TRAPS) {
 309   Klass* klass = link_info.resolved_klass();
 310   Symbol* name = link_info.name();
 311   Symbol* signature = link_info.signature();
 312 
 313   // Ignore overpasses so statics can be found during resolution
 314   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 315 
 316   if (klass->is_array_klass()) {
 317     // Only consider klass and super klass for arrays
 318     return methodHandle(THREAD, result);
 319   }
 320 
 321   InstanceKlass* ik = InstanceKlass::cast(klass);
 322 
 323   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 324   // ignore static and non-public methods of java.lang.Object,
 325   // like clone, finalize, registerNatives.
 326   if (in_imethod_resolve &&
 327       result != NULL &&
 328       ik->is_interface() &&
 329       (result->is_static() || !result->is_public()) &&
 330       result->method_holder() == SystemDictionary::Object_klass()) {
 331     result = NULL;
 332   }
 333 
 334   // Before considering default methods, check for an overpass in the
 335   // current class if a method has not been found.
 336   if (result == NULL) {
 337     result = ik->find_method(name, signature);
 338   }
 339 
 340   if (result == NULL) {
 341     Array<Method*>* default_methods = ik->default_methods();
 342     if (default_methods != NULL) {
 343       result = InstanceKlass::find_method(default_methods, name, signature);
 344     }
 345   }
 346 
 347   if (checkpolymorphism && result != NULL) {
 348     vmIntrinsics::ID iid = result->intrinsic_id();
 349     if (MethodHandles::is_signature_polymorphic(iid)) {
 350       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 351       return NULL;
 352     }
 353   }
 354   return methodHandle(THREAD, result);
 355 }
 356 
 357 // returns first instance method
 358 // Looks up method in classes, then looks up local default methods
 359 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 360                                                              Symbol* name,
 361                                                              Symbol* signature, TRAPS) {
 362   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 363 
 364   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 365     Klass* super_klass = result->method_holder()->super();
 366     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 367   }
 368 
 369   if (klass->is_array_klass()) {
 370     // Only consider klass and super klass for arrays
 371     return methodHandle(THREAD, result);
 372   }
 373 
 374   if (result == NULL) {
 375     Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
 376     if (default_methods != NULL) {
 377       result = InstanceKlass::find_method(default_methods, name, signature);
 378       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 379     }
 380   }
 381   return methodHandle(THREAD, result);
 382 }
 383 
 384 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
 385                                                    const methodHandle& resolved_method) {
 386 
 387   int vtable_index = Method::invalid_vtable_index;
 388   Symbol* name = resolved_method->name();
 389   Symbol* signature = resolved_method->signature();
 390   InstanceKlass* ik = InstanceKlass::cast(klass);
 391 
 392   // First check in default method array
 393   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 394     int index = InstanceKlass::find_method_index(ik->default_methods(),
 395                                                  name, signature, Klass::find_overpass,
 396                                                  Klass::find_static, Klass::find_private);
 397     if (index >= 0 ) {
 398       vtable_index = ik->default_vtable_indices()->at(index);
 399     }
 400   }
 401   if (vtable_index == Method::invalid_vtable_index) {
 402     // get vtable_index for miranda methods
 403     ResourceMark rm;
 404     klassVtable *vt = ik->vtable();
 405     vtable_index = vt->index_of_miranda(name, signature);
 406   }
 407   return vtable_index;
 408 }
 409 
 410 methodHandle LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info, TRAPS) {
 411   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
 412 
 413   // Specify 'true' in order to skip default methods when searching the
 414   // interfaces.  Function lookup_method_in_klasses() already looked for
 415   // the method in the default methods table.
 416   return methodHandle(THREAD,
 417     ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(),
 418     Klass::skip_defaults));
 419 }
 420 
 421 methodHandle LinkResolver::lookup_polymorphic_method(
 422                                              const LinkInfo& link_info,
 423                                              Handle *appendix_result_or_null,
 424                                              Handle *method_type_result,
 425                                              TRAPS) {
 426   Klass* klass = link_info.resolved_klass();
 427   Symbol* name = link_info.name();
 428   Symbol* full_signature = link_info.signature();
 429 
 430   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 431   if (TraceMethodHandles) {
 432     ResourceMark rm(THREAD);
 433     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 434                   vmIntrinsics::name_at(iid), klass->external_name(),
 435                   name->as_C_string(), full_signature->as_C_string());
 436   }
 437   if ((klass == SystemDictionary::MethodHandle_klass() ||
 438        klass == SystemDictionary::VarHandle_klass()) &&
 439       iid != vmIntrinsics::_none) {
 440     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 441       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 442       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 443       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 444       TempNewSymbol basic_signature =
 445         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
 446       if (TraceMethodHandles) {
 447         ResourceMark rm(THREAD);
 448         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
 449                       name->as_C_string(),
 450                       full_signature->as_C_string(),
 451                       basic_signature->as_C_string());
 452       }
 453       methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid,
 454                                                               basic_signature,
 455                                                               CHECK_NULL);
 456       if (result.not_null()) {
 457         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 458         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");


 512         if (appendix.not_null())                                   expected_size_of_params += 1;
 513         if (actual_size_of_params != expected_size_of_params) {
 514           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 515           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 516           result->print();
 517         }
 518         assert(actual_size_of_params == expected_size_of_params,
 519                "%d != %d", actual_size_of_params, expected_size_of_params);
 520 #endif //ASSERT
 521 
 522         assert(appendix_result_or_null != NULL, "");
 523         (*appendix_result_or_null) = appendix;
 524         (*method_type_result)      = method_type;
 525       }
 526       return result;
 527     }
 528   }
 529   return NULL;
 530 }
 531 
 532 void LinkResolver::check_method_accessability(Klass* ref_klass,
 533                                               Klass* resolved_klass,
 534                                               Klass* sel_klass,
 535                                               const methodHandle& sel_method,
 536                                               TRAPS) {
 537 
 538   AccessFlags flags = sel_method->access_flags();
 539 
 540   // Special case:  arrays always override "clone". JVMS 2.15.
 541   // If the resolved klass is an array class, and the declaring class
 542   // is java.lang.Object and the method is "clone", set the flags
 543   // to public.
 544   //
 545   // We'll check for the method name first, as that's most likely
 546   // to be false (so we'll short-circuit out of these tests).
 547   if (sel_method->name() == vmSymbols::clone_name() &&
 548       sel_klass == SystemDictionary::Object_klass() &&
 549       resolved_klass->is_array_klass()) {
 550     // We need to change "protected" to "public".
 551     assert(flags.is_protected(), "clone not protected?");
 552     jint new_flags = flags.as_int();
 553     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 554     new_flags = new_flags | JVM_ACC_PUBLIC;
 555     flags.set_flags(new_flags);
 556   }
 557 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 558 
 559   if (!Reflection::verify_field_access(ref_klass,
 560                                        resolved_klass,
 561                                        sel_klass,
 562                                        flags,
 563                                        true)) {
 564     ResourceMark rm(THREAD);
 565     Exceptions::fthrow(
 566       THREAD_AND_LOCATION,
 567       vmSymbols::java_lang_IllegalAccessError(),
 568       "tried to access method %s.%s%s from class %s",
 569       sel_klass->external_name(),
 570       sel_method->name()->as_C_string(),
 571       sel_method->signature()->as_C_string(),
 572       ref_klass->external_name()
 573     );
 574     return;
 575   }
 576 }
 577 
 578 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 579                                                      const constantPoolHandle& pool, int index, TRAPS) {
 580   // This method is used only
 581   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 582   // and
 583   // (2) in Bytecode_invoke::static_target
 584   // It appears to fail when applied to an invokeinterface call site.
 585   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 586   // resolve klass

 587   if (code == Bytecodes::_invokedynamic) {
 588     Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
 589     Symbol* method_name = vmSymbols::invoke_name();
 590     Symbol* method_signature = pool->signature_ref_at(index);
 591     Klass*  current_klass = pool->pool_holder();
 592     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 593     return resolve_method(link_info, code, THREAD);
 594   }
 595 
 596   LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
 597   Klass* resolved_klass = link_info.resolved_klass();
 598 
 599   if (pool->has_preresolution()
 600       || (resolved_klass == SystemDictionary::MethodHandle_klass() &&
 601           MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
 602     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 603     if (result != NULL) {
 604       return methodHandle(THREAD, result);
 605     }
 606   }
 607 
 608   if (code == Bytecodes::_invokeinterface) {
 609     return resolve_interface_method(link_info, code, THREAD);
 610   } else if (code == Bytecodes::_invokevirtual) {
 611     return resolve_method(link_info, code, THREAD);
 612   } else if (!resolved_klass->is_interface()) {
 613     return resolve_method(link_info, code, THREAD);
 614   } else {
 615     return resolve_interface_method(link_info, code, THREAD);
 616   }
 617 }
 618 
 619 // Check and print a loader constraint violation message for method or interface method
 620 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 621                                                    const methodHandle& resolved_method,


 632       " \"%s\" the class loader (instance of %s) of the current class, %s,"
 633       " and the class loader (instance of %s) for the method's defining class, %s, have"
 634       " different Class objects for the type %s used in the signature";
 635     char* sig = link_info.method_string();
 636     const char* loader1_name = SystemDictionary::loader_name(current_loader());
 637     char* current = link_info.current_klass()->name()->as_C_string();
 638     const char* loader2_name = SystemDictionary::loader_name(resolved_loader());
 639     char* target = resolved_method->method_holder()->name()->as_C_string();
 640     char* failed_type_name = failed_type_symbol->as_C_string();
 641     size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) +
 642       strlen(current) + strlen(loader2_name) + strlen(target) +
 643       strlen(failed_type_name) + strlen(method_type) + 1;
 644     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 645     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
 646                  target, failed_type_name);
 647     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 648   }
 649 }
 650 
 651 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 652                                                   Klass* current_klass,
 653                                                   Klass* sel_klass, TRAPS) {
 654   Handle ref_loader(THREAD, current_klass->class_loader());
 655   Handle sel_loader(THREAD, sel_klass->class_loader());
 656 
 657   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 658   Symbol* failed_type_symbol =
 659     SystemDictionary::check_signature_loaders(sig,
 660                                               ref_loader, sel_loader,
 661                                               false,
 662                                               CHECK);
 663   if (failed_type_symbol != NULL) {
 664     const char* msg = "loader constraint violation: when resolving field"
 665       " \"%s\" the class loader (instance of %s) of the referring class, "
 666       "%s, and the class loader (instance of %s) for the field's resolved "
 667       "type, %s, have different Class objects for that type";
 668     char* field_name = field->as_C_string();
 669     const char* loader1_name = SystemDictionary::loader_name(ref_loader());
 670     char* sel = sel_klass->name()->as_C_string();
 671     const char* loader2_name = SystemDictionary::loader_name(sel_loader());
 672     char* failed_type_name = failed_type_symbol->as_C_string();
 673     size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) +
 674                     strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1;
 675     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 676     jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name,
 677                      failed_type_name);
 678     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 679   }
 680 }
 681 
 682 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 683                                           Bytecodes::Code code, TRAPS) {
 684 
 685   Handle nested_exception;
 686   Klass* resolved_klass = link_info.resolved_klass();
 687 
 688   // 1. For invokevirtual, cannot call an interface method
 689   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 690     ResourceMark rm(THREAD);
 691     char buf[200];
 692     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 693         resolved_klass->external_name());
 694     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 695   }
 696 
 697   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 698   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 699     ResourceMark rm(THREAD);
 700     char buf[200];
 701     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 702     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 703   }
 704 
 705 
 706   // 3. lookup method in resolved klass and its super klasses
 707   methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL);
 708 
 709   // 4. lookup method in all the interfaces implemented by the resolved klass
 710   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 711     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 712 
 713     if (resolved_method.is_null()) {
 714       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 715       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 716       if (HAS_PENDING_EXCEPTION) {
 717         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 718         CLEAR_PENDING_EXCEPTION;
 719       }
 720     }
 721   }
 722 
 723   // 5. method lookup failed
 724   if (resolved_method.is_null()) {
 725     ResourceMark rm(THREAD);
 726     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 727                     Method::name_and_sig_as_C_string(resolved_klass,
 728                                                      link_info.name(),
 729                                                      link_info.signature()),
 730                     nested_exception, NULL);
 731   }
 732 
 733   // 5. access checks, access checking may be turned off when calling from within the VM.
 734   Klass* current_klass = link_info.current_klass();
 735   if (link_info.check_access()) {
 736     assert(current_klass != NULL , "current_klass should not be null");
 737 
 738     // check if method can be accessed by the referring class
 739     check_method_accessability(current_klass,
 740                                resolved_klass,
 741                                resolved_method->method_holder(),
 742                                resolved_method,
 743                                CHECK_NULL);
 744 
 745     // check loader constraints
 746     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 747   }
 748 
 749   return resolved_method;
 750 }
 751 
 752 static void trace_method_resolution(const char* prefix,
 753                                     Klass* klass,
 754                                     Klass* resolved_klass,
 755                                     const methodHandle& method,
 756                                     bool logitables,
 757                                     int index = -1) {
 758 #ifndef PRODUCT
 759   ResourceMark rm;
 760   outputStream* st;
 761   if (logitables) {
 762     st = Log(itables)::trace_stream();
 763   } else {
 764     st = Log(vtables)::trace_stream();
 765   }
 766   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 767             prefix,
 768             (klass == NULL ? "<NULL>" : klass->internal_name()),
 769             (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
 770             Method::name_and_sig_as_C_string(resolved_klass,
 771                                              method->name(),
 772                                              method->signature()),
 773             method->method_holder()->internal_name());
 774   method->print_linkage_flags(st);
 775   if (index != -1) {
 776     st->print("vtable_index:%d", index);
 777   }
 778   st->cr();
 779 #endif // PRODUCT
 780 }
 781 
 782 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 783 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 784 
 785   Klass* resolved_klass = link_info.resolved_klass();
 786 
 787   // check if klass is interface
 788   if (!resolved_klass->is_interface()) {
 789     ResourceMark rm(THREAD);
 790     char buf[200];
 791     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 792     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 793   }
 794 
 795   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 796   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 797     ResourceMark rm(THREAD);
 798     char buf[200];
 799     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 800     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 801   }
 802 
 803   // lookup method in this interface or its super, java.lang.Object
 804   // JDK8: also look for static methods
 805   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
 806 
 807   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 808     // lookup method in all the super-interfaces
 809     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 810   }
 811 
 812   if (resolved_method.is_null()) {
 813     // no method found
 814     ResourceMark rm(THREAD);
 815     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 816                    Method::name_and_sig_as_C_string(resolved_klass,
 817                                                     link_info.name(),
 818                                                     link_info.signature()));
 819   }
 820 
 821   if (link_info.check_access()) {
 822     // JDK8 adds non-public interface methods, and accessability check requirement
 823     Klass* current_klass = link_info.current_klass();
 824 
 825     assert(current_klass != NULL , "current_klass should not be null");
 826 
 827     // check if method can be accessed by the referring class
 828     check_method_accessability(current_klass,
 829                                resolved_klass,
 830                                resolved_method->method_holder(),
 831                                resolved_method,
 832                                CHECK_NULL);
 833 
 834     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 835   }
 836 
 837   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 838     ResourceMark rm(THREAD);
 839     char buf[200];
 840     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 841                  Method::name_and_sig_as_C_string(resolved_klass,
 842                  resolved_method->name(), resolved_method->signature()));
 843     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 844   }
 845 
 846   if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) {
 847     ResourceMark rm(THREAD);
 848     char buf[200];
 849 
 850     Klass* current_klass = link_info.current_klass();
 851     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s",
 852                  Method::name_and_sig_as_C_string(resolved_klass,
 853                                                   resolved_method->name(),
 854                                                   resolved_method->signature()),
 855                                                   (current_klass == NULL ? "<NULL>" : current_klass->internal_name()));
 856      THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 857   }
 858 
 859   if (log_develop_is_enabled(Trace, itables)) {
 860     char buf[200];
 861     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 862                  Bytecodes::name(code));
 863     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 864                             resolved_method, true);
 865   }
 866 
 867   return resolved_method;
 868 }
 869 
 870 //------------------------------------------------------------------------------------------------------------------------
 871 // Field resolution
 872 
 873 void LinkResolver::check_field_accessability(Klass* ref_klass,
 874                                              Klass* resolved_klass,
 875                                              Klass* sel_klass,
 876                                              const fieldDescriptor& fd,
 877                                              TRAPS) {
 878   if (!Reflection::verify_field_access(ref_klass,
 879                                        resolved_klass,
 880                                        sel_klass,
 881                                        fd.access_flags(),
 882                                        true)) {
 883     ResourceMark rm(THREAD);
 884     Exceptions::fthrow(
 885       THREAD_AND_LOCATION,
 886       vmSymbols::java_lang_IllegalAccessError(),
 887       "tried to access field %s.%s from class %s",
 888       sel_klass->external_name(),
 889       fd.name()->as_C_string(),
 890       ref_klass->external_name()
 891     );
 892     return;
 893   }
 894 }
 895 
 896 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 897   LinkInfo link_info(pool, index, method, CHECK);
 898   resolve_field(fd, link_info, byte, true, CHECK);
 899 }
 900 
 901 void LinkResolver::resolve_field(fieldDescriptor& fd,
 902                                  const LinkInfo& link_info,
 903                                  Bytecodes::Code byte, bool initialize_class,
 904                                  TRAPS) {
 905   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 906          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 907          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 908          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 909 
 910   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 911   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 912   // Check if there's a resolved klass containing the field
 913   Klass* resolved_klass = link_info.resolved_klass();
 914   Symbol* field = link_info.name();
 915   Symbol* sig = link_info.signature();
 916 
 917   if (resolved_klass == NULL) {
 918     ResourceMark rm(THREAD);
 919     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 920   }
 921 
 922   // Resolve instance field
 923   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 924   // check if field exists; i.e., if a klass containing the field def has been selected
 925   if (sel_klass == NULL) {
 926     ResourceMark rm(THREAD);
 927     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 928   }
 929 
 930   if (!link_info.check_access())
 931     // Access checking may be turned off when calling from within the VM.
 932     return;
 933 
 934   // check access
 935   Klass* current_klass = link_info.current_klass();
 936   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 937 
 938   // check for errors
 939   if (is_static != fd.is_static()) {
 940     ResourceMark rm(THREAD);
 941     char msg[200];
 942     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
 943     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 944   }
 945 
 946   // A final field can be modified only
 947   // (1) by methods declared in the class declaring the field and
 948   // (2) by the <clinit> method (in case of a static field)
 949   //     or by the <init> method (in case of an instance field).
 950   if (is_put && fd.access_flags().is_final()) {
 951     ResourceMark rm(THREAD);
 952     stringStream ss;
 953 
 954     if (sel_klass != current_klass) {
 955       ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
 956                 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
 957                 current_klass->external_name());
 958       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 959     }
 960 
 961     if (fd.constants()->pool_holder()->major_version() >= 53) {
 962       methodHandle m = link_info.current_method();
 963       assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
 964       bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
 965                                                  fd.is_static() &&
 966                                                  !m()->is_static_initializer());
 967       bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
 968                                                    !fd.is_static() &&
 969                                                    !m->is_object_initializer());
 970 
 971       if (is_initialized_static_final_update || is_initialized_instance_final_update) {
 972         ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
 973                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
 974                  m()->name()->as_C_string(),
 975                  is_static ? "<clinit>" : "<init>");
 976         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 977       }
 978     }
 979   }
 980 
 981   // initialize resolved_klass if necessary
 982   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
 983   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
 984   //
 985   // note 2: we don't want to force initialization if we are just checking
 986   //         if the field access is legal; e.g., during compilation
 987   if (is_static && initialize_class) {
 988     sel_klass->initialize(CHECK);
 989   }
 990 
 991   if (sel_klass != current_klass) {
 992     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
 993   }
 994 
 995   // return information. note that the klass is set to the actual klass containing the
 996   // field, otherwise access of static fields in superclasses will not work.
 997 }
 998 
 999 
1000 //------------------------------------------------------------------------------------------------------------------------
1001 // Invoke resolution
1002 //
1003 // Naming conventions:
1004 //
1005 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1006 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1007 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1008 // recv_klass         the receiver klass
1009 
1010 
1011 void LinkResolver::resolve_static_call(CallInfo& result,
1012                                        const LinkInfo& link_info,
1013                                        bool initialize_class, TRAPS) {
1014   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
1015 
1016   // The resolved class can change as a result of this resolution.
1017   Klass* resolved_klass = resolved_method->method_holder();
1018 
1019   // Initialize klass (this should only happen if everything is ok)
1020   if (initialize_class && resolved_klass->should_be_initialized()) {
1021     resolved_klass->initialize(CHECK);
1022     // Use updated LinkInfo to reresolve with resolved method holder
1023     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1024                       link_info.current_klass(),
1025                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1026     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1027   }
1028 
1029   // setup result
1030   result.set_static(resolved_klass, resolved_method, CHECK);
1031 }
1032 
1033 // throws linktime exceptions
1034 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1035 
1036   Klass* resolved_klass = link_info.resolved_klass();
1037   methodHandle resolved_method;
1038   if (!resolved_klass->is_interface()) {
1039     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1040   } else {
1041     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1042   }
1043   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1044 
1045   // check if static
1046   if (!resolved_method->is_static()) {
1047     ResourceMark rm(THREAD);
1048     char buf[200];
1049     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1050                                                       resolved_method->name(),
1051                                                       resolved_method->signature()));
1052     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1053   }
1054   return resolved_method;
1055 }
1056 
1057 
1058 void LinkResolver::resolve_special_call(CallInfo& result,
1059                                         const LinkInfo& link_info,
1060                                         TRAPS) {
1061   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1062   runtime_resolve_special_method(result, resolved_method,
1063                                  link_info.resolved_klass(),
1064                                  link_info.current_klass(),
1065                                  link_info.check_access(), CHECK);
1066 }
1067 
1068 // throws linktime exceptions
1069 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1070                                                            TRAPS) {
1071 
1072   // Invokespecial is called for multiple special reasons:
1073   // <init>
1074   // local private method invocation, for classes and interfaces
1075   // superclass.method, which can also resolve to a default method
1076   // and the selected method is recalculated relative to the direct superclass
1077   // superinterface.method, which explicitly does not check shadowing
1078   Klass* resolved_klass = link_info.resolved_klass();
1079   methodHandle resolved_method;
1080 
1081   if (!resolved_klass->is_interface()) {
1082     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1083   } else {
1084     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1085   }
1086 
1087   // check if method name is <init>, that it is found in same klass as static type
1088   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1089       resolved_method->method_holder() != resolved_klass) {
1090     ResourceMark rm(THREAD);
1091     Exceptions::fthrow(
1092       THREAD_AND_LOCATION,
1093       vmSymbols::java_lang_NoSuchMethodError(),
1094       "%s: method %s%s not found",
1095       resolved_klass->external_name(),
1096       resolved_method->name()->as_C_string(),
1097       resolved_method->signature()->as_C_string()
1098     );
1099     return NULL;
1100   }
1101 
1102   // check if invokespecial's interface method reference is in an indirect superinterface
1103   Klass* current_klass = link_info.current_klass();
1104   if (current_klass != NULL && resolved_klass->is_interface()) {
1105     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1106     InstanceKlass *klass_to_check = !ck->is_anonymous() ?
1107                                     ck :
1108                                     InstanceKlass::cast(ck->host_klass());
1109     // Disable verification for the dynamically-generated reflection bytecodes.
1110     bool is_reflect = klass_to_check->is_subclass_of(
1111                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1112 
1113     if (!is_reflect &&
1114         !klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1115       ResourceMark rm(THREAD);
1116       char buf[200];
1117       jio_snprintf(buf, sizeof(buf),
1118                    "Interface method reference: %s, is in an indirect superinterface of %s",
1119                    Method::name_and_sig_as_C_string(resolved_klass,
1120                                                     resolved_method->name(),
1121                                                     resolved_method->signature()),
1122                    current_klass->external_name());
1123       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1124     }
1125   }
1126 
1127   // check if not static
1128   if (resolved_method->is_static()) {
1129     ResourceMark rm(THREAD);
1130     char buf[200];
1131     jio_snprintf(buf, sizeof(buf),
1132                  "Expecting non-static method %s",
1133                  Method::name_and_sig_as_C_string(resolved_klass,
1134                                                   resolved_method->name(),
1135                                                   resolved_method->signature()));
1136     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1137   }
1138 
1139   if (log_develop_is_enabled(Trace, itables)) {
1140     trace_method_resolution("invokespecial resolved method: caller-class:",
1141                             current_klass, resolved_klass, resolved_method, true);
1142   }
1143 
1144   return resolved_method;
1145 }
1146 
1147 // throws runtime exceptions
1148 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1149                                                   const methodHandle& resolved_method,
1150                                                   Klass* resolved_klass,
1151                                                   Klass* current_klass,
1152                                                   bool check_access, TRAPS) {
1153 
1154   // resolved method is selected method unless we have an old-style lookup
1155   // for a superclass method
1156   // Invokespecial for a superinterface, resolved method is selected method,
1157   // no checks for shadowing
1158   methodHandle sel_method(THREAD, resolved_method());
1159 
1160   // check if this is an old-style super call and do a new lookup if so



1161   if (check_access &&
1162       // a) check if ACC_SUPER flag is set for the current class
1163       (current_klass->is_super() || !AllowNonVirtualCalls) &&
1164       // b) check if the class of the resolved_klass is a superclass
1165       // (not supertype in order to exclude interface classes) of the current class.
1166       // This check is not performed for super.invoke for interface methods
1167       // in super interfaces.
1168       current_klass->is_subclass_of(resolved_klass) &&
1169       current_klass != resolved_klass &&
1170       // c) check if the method is not <init>
1171       resolved_method->name() != vmSymbols::object_initializer_name()) {
1172     // Lookup super method
1173     Klass* super_klass = current_klass->super();
1174     sel_method = lookup_instance_method_in_klasses(super_klass,
1175                          resolved_method->name(),
1176                          resolved_method->signature(), CHECK);
1177     // check if found
1178     if (sel_method.is_null()) {
1179       ResourceMark rm(THREAD);
1180       THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1181                 Method::name_and_sig_as_C_string(resolved_klass,
1182                                           resolved_method->name(),
1183                                           resolved_method->signature()));
1184     }
1185   }

1186 
1187   // check if not static
1188   if (sel_method->is_static()) {
1189     ResourceMark rm(THREAD);
1190     char buf[200];
1191     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1192                                                                                       resolved_method->name(),
1193                                                                                       resolved_method->signature()));
1194     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1195   }
1196 
1197   // check if abstract
1198   if (sel_method->is_abstract()) {
1199     ResourceMark rm(THREAD);
1200     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1201               Method::name_and_sig_as_C_string(resolved_klass,
1202                                                sel_method->name(),
1203                                                sel_method->signature()));
1204   }
1205 
1206   if (log_develop_is_enabled(Trace, itables)) {
1207     trace_method_resolution("invokespecial selected method: resolved-class:",
1208                             resolved_klass, resolved_klass, sel_method, true);
1209   }
1210 
1211   // setup result
1212   result.set_static(resolved_klass, sel_method, CHECK);
1213 }
1214 
1215 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1216                                         const LinkInfo& link_info,
1217                                         bool check_null_and_abstract, TRAPS) {
1218   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1219   runtime_resolve_virtual_method(result, resolved_method,
1220                                  link_info.resolved_klass(),
1221                                  recv, receiver_klass,
1222                                  check_null_and_abstract, CHECK);
1223 }
1224 
1225 // throws linktime exceptions
1226 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1227                                                            TRAPS) {
1228   // normal method resolution
1229   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1230 
1231   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1232   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1233 
1234   // check if private interface method
1235   Klass* resolved_klass = link_info.resolved_klass();
1236   Klass* current_klass = link_info.current_klass();
1237 
1238   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1239   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1240     ResourceMark rm(THREAD);
1241     char buf[200];
1242     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1243                  Method::name_and_sig_as_C_string(resolved_klass,
1244                                                   resolved_method->name(),
1245                                                   resolved_method->signature()),
1246                    (current_klass == NULL ? "<NULL>" : current_klass->internal_name()));
1247     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1248   }
1249 
1250   // check if not static
1251   if (resolved_method->is_static()) {
1252     ResourceMark rm(THREAD);
1253     char buf[200];
1254     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1255                                                                                            resolved_method->name(),
1256                                                                                            resolved_method->signature()));
1257     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1258   }
1259 
1260   if (log_develop_is_enabled(Trace, vtables)) {
1261     trace_method_resolution("invokevirtual resolved method: caller-class:",
1262                             current_klass, resolved_klass, resolved_method, false);
1263   }
1264 
1265   return resolved_method;
1266 }
1267 
1268 // throws runtime exceptions
1269 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1270                                                   const methodHandle& resolved_method,
1271                                                   Klass* resolved_klass,
1272                                                   Handle recv,
1273                                                   Klass* recv_klass,
1274                                                   bool check_null_and_abstract,
1275                                                   TRAPS) {
1276 
1277   // setup default return values
1278   int vtable_index = Method::invalid_vtable_index;
1279   methodHandle selected_method;
1280 
1281   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1282 
1283   // runtime method resolution
1284   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1285     THROW(vmSymbols::java_lang_NullPointerException());
1286   }
1287 
1288   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1289   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1290   // a missing receiver might result in a bogus lookup.
1291   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1292 
1293   // do lookup based on receiver klass using the vtable index


1302     // a default or miranda method; therefore, it must have a valid vtable index.
1303     assert(!resolved_method->has_itable_index(), "");
1304     vtable_index = resolved_method->vtable_index();
1305     // We could get a negative vtable_index for final methods,
1306     // because as an optimization they are they are never put in the vtable,
1307     // unless they override an existing method.
1308     // If we do get a negative, it means the resolved method is the the selected
1309     // method, and it can never be changed by an override.
1310     if (vtable_index == Method::nonvirtual_vtable_index) {
1311       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1312       selected_method = resolved_method;
1313     } else {
1314       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1315     }
1316   }
1317 
1318   // check if method exists
1319   if (selected_method.is_null()) {
1320     ResourceMark rm(THREAD);
1321     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1322               Method::name_and_sig_as_C_string(resolved_klass,
1323                                                resolved_method->name(),
1324                                                resolved_method->signature()));
1325   }
1326 
1327   // check if abstract
1328   if (check_null_and_abstract && selected_method->is_abstract()) {
1329     ResourceMark rm(THREAD);
1330     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1331               Method::name_and_sig_as_C_string(resolved_klass,
1332                                                selected_method->name(),
1333                                                selected_method->signature()));
1334   }
1335 
1336   if (log_develop_is_enabled(Trace, vtables)) {
1337     trace_method_resolution("invokevirtual selected method: receiver-class:",
1338                             recv_klass, resolved_klass, selected_method,
1339                             false, vtable_index);
1340   }
1341   // setup result
1342   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1343 }
1344 
1345 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1346                                           const LinkInfo& link_info,
1347                                           bool check_null_and_abstract, TRAPS) {
1348   // throws linktime exceptions
1349   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1350   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1351                                    recv, recv_klass, check_null_and_abstract, CHECK);
1352 }
1353 
1354 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1355                                                              TRAPS) {
1356   // normal interface method resolution
1357   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1358   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1359   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1360 
1361   return resolved_method;
1362 }
1363 
1364 // throws runtime exceptions
1365 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1366                                                     const methodHandle& resolved_method,
1367                                                     Klass* resolved_klass,
1368                                                     Handle recv,
1369                                                     Klass* recv_klass,
1370                                                     bool check_null_and_abstract, TRAPS) {
1371   // check if receiver exists
1372   if (check_null_and_abstract && recv.is_null()) {
1373     THROW(vmSymbols::java_lang_NullPointerException());
1374   }
1375 
1376   // check if receiver klass implements the resolved interface
1377   if (!recv_klass->is_subtype_of(resolved_klass)) {
1378     ResourceMark rm(THREAD);
1379     char buf[200];
1380     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1381                  recv_klass->external_name(),
1382                  resolved_klass->external_name());
1383     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1384   }
1385 
1386   // do lookup based on receiver klass
1387   // This search must match the linktime preparation search for itable initialization
1388   // to correctly enforce loader constraints for interface method inheritance
1389   methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass,
1390                                                   resolved_method->name(),
1391                                                   resolved_method->signature(), CHECK);
1392   if (sel_method.is_null() && !check_null_and_abstract) {
1393     // In theory this is a harmless placeholder value, but
1394     // in practice leaving in null affects the nsk default method tests.
1395     // This needs further study.
1396     sel_method = resolved_method;
1397   }
1398   // check if method exists
1399   if (sel_method.is_null()) {
1400     ResourceMark rm(THREAD);
1401     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1402                    Method::name_and_sig_as_C_string(recv_klass,
1403                                                     resolved_method->name(),
1404                                                     resolved_method->signature()));
1405   }
1406   // check access
1407   // Throw Illegal Access Error if sel_method is not public.
1408   if (!sel_method->is_public()) {
1409     ResourceMark rm(THREAD);
1410     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1411               Method::name_and_sig_as_C_string(recv_klass,
1412                                                sel_method->name(),
1413                                                sel_method->signature()));
1414   }
1415   // check if abstract
1416   if (check_null_and_abstract && sel_method->is_abstract()) {
1417     ResourceMark rm(THREAD);
1418     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1419               Method::name_and_sig_as_C_string(recv_klass,
1420                                                sel_method->name(),
1421                                                sel_method->signature()));
1422   }
1423 
1424   if (log_develop_is_enabled(Trace, itables)) {
1425     trace_method_resolution("invokeinterface selected method: receiver-class:",
1426                             recv_klass, resolved_klass, sel_method, true);
1427   }
1428   // setup result
1429   if (!resolved_method->has_itable_index()) {
1430     int vtable_index = resolved_method->vtable_index();
1431     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1432     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1433   } else {
1434     int itable_index = resolved_method()->itable_index();
1435     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1436   }
1437 }
1438 
1439 


1445     CLEAR_PENDING_EXCEPTION;
1446     return methodHandle();
1447   } else {
1448     return method_result;
1449   }
1450 }
1451 
1452 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1453                                                  const LinkInfo& link_info) {
1454   EXCEPTION_MARK;
1455   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1456   if (HAS_PENDING_EXCEPTION) {
1457     CLEAR_PENDING_EXCEPTION;
1458     return methodHandle();
1459   } else {
1460     return method_result;
1461   }
1462 }
1463 
1464 methodHandle LinkResolver::resolve_virtual_call_or_null(
1465                                                  Klass* receiver_klass,
1466                                                  const LinkInfo& link_info) {
1467   EXCEPTION_MARK;
1468   CallInfo info;
1469   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1470   if (HAS_PENDING_EXCEPTION) {
1471     CLEAR_PENDING_EXCEPTION;
1472     return methodHandle();
1473   }
1474   return info.selected_method();
1475 }
1476 
1477 methodHandle LinkResolver::resolve_interface_call_or_null(
1478                                                  Klass* receiver_klass,
1479                                                  const LinkInfo& link_info) {
1480   EXCEPTION_MARK;
1481   CallInfo info;
1482   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1483   if (HAS_PENDING_EXCEPTION) {
1484     CLEAR_PENDING_EXCEPTION;
1485     return methodHandle();
1486   }
1487   return info.selected_method();
1488 }
1489 
1490 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1491                                                const LinkInfo& link_info) {
1492   EXCEPTION_MARK;
1493   CallInfo info;
1494   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1495                        /*check_null_or_abstract*/false, THREAD);
1496   if (HAS_PENDING_EXCEPTION) {
1497     CLEAR_PENDING_EXCEPTION;
1498     return Method::invalid_vtable_index;
1499   }
1500   return info.vtable_index();
1501 }
1502 
1503 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1504   EXCEPTION_MARK;
1505   CallInfo info;
1506   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1507   if (HAS_PENDING_EXCEPTION) {
1508     CLEAR_PENDING_EXCEPTION;
1509     return methodHandle();
1510   }


1525 
1526 
1527 //------------------------------------------------------------------------------------------------------------------------
1528 // ConstantPool entries
1529 
1530 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1531   switch (byte) {
1532     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1533     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1534     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1535     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1536     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1537     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1538   }
1539   return;
1540 }
1541 
1542 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1543                              const methodHandle& attached_method,
1544                              Bytecodes::Code byte, TRAPS) {
1545   Klass* defc = attached_method->method_holder();
1546   Symbol* name = attached_method->name();
1547   Symbol* type = attached_method->signature();
1548   LinkInfo link_info(defc, name, type);
1549   switch(byte) {
1550     case Bytecodes::_invokevirtual:
1551       resolve_virtual_call(result, recv, recv->klass(), link_info,
1552                            /*check_null_and_abstract=*/true, CHECK);
1553       break;
1554     case Bytecodes::_invokeinterface:
1555       resolve_interface_call(result, recv, recv->klass(), link_info,
1556                              /*check_null_and_abstract=*/true, CHECK);
1557       break;
1558     case Bytecodes::_invokestatic:
1559       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1560       break;
1561     case Bytecodes::_invokespecial:
1562       resolve_special_call(result, link_info, CHECK);
1563       break;
1564     default:
1565       fatal("bad call: %s", Bytecodes::name(byte));
1566   }
1567 }
1568 
1569 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1570   LinkInfo link_info(pool, index, CHECK);
1571   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1572 }
1573 
1574 
1575 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1576   LinkInfo link_info(pool, index, CHECK);
1577   resolve_special_call(result, link_info, CHECK);
1578 }
1579 
1580 
1581 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1582                                           const constantPoolHandle& pool, int index,
1583                                           TRAPS) {
1584 
1585   LinkInfo link_info(pool, index, CHECK);
1586   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1587   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1588 }
1589 
1590 
1591 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1592   LinkInfo link_info(pool, index, CHECK);
1593   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1594   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1595 }
1596 
1597 
1598 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1599   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1600   LinkInfo link_info(pool, index, CHECK);
1601   if (TraceMethodHandles) {
1602     ResourceMark rm(THREAD);
1603     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1604                   link_info.signature()->as_C_string());
1605   }
1606   resolve_handle_call(result, link_info, CHECK);
1607 }
1608 
1609 void LinkResolver::resolve_handle_call(CallInfo& result,
1610                                        const LinkInfo& link_info,
1611                                        TRAPS) {
1612   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1613   Klass* resolved_klass = link_info.resolved_klass();
1614   assert(resolved_klass == SystemDictionary::MethodHandle_klass() ||
1615          resolved_klass == SystemDictionary::VarHandle_klass(), "");
1616   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1617   Handle       resolved_appendix;
1618   Handle       resolved_method_type;
1619   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1620                                        &resolved_appendix, &resolved_method_type, CHECK);
1621   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1622 }
1623 
1624 static void wrap_invokedynamic_exception(TRAPS) {
1625   if (HAS_PENDING_EXCEPTION) {
1626     // See the "Linking Exceptions" section for the invokedynamic instruction
1627     // in JVMS 6.5.
1628     if (PENDING_EXCEPTION->is_a(SystemDictionary::Error_klass())) {
1629       // Pass through an Error, including BootstrapMethodError, any other form
1630       // of linkage error, or say ThreadDeath/OutOfMemoryError
1631       if (TraceMethodHandles) {
1632         tty->print_cr("invokedynamic passes through an Error for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1633         PENDING_EXCEPTION->print();
1634       }
1635       return;
1636     }
1637 
1638     // Otherwise wrap the exception in a BootstrapMethodError
1639     if (TraceMethodHandles) {
1640       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1641       PENDING_EXCEPTION->print();
1642     }
1643     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1644     CLEAR_PENDING_EXCEPTION;
1645     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1646   }
1647 }
1648 
1649 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1650   Symbol* method_name       = pool->name_ref_at(index);
1651   Symbol* method_signature  = pool->signature_ref_at(index);
1652   Klass* current_klass = pool->pool_holder();
1653 
1654   // Resolve the bootstrap specifier (BSM + optional arguments).
1655   Handle bootstrap_specifier;
1656   // Check if CallSite has been bound already:
1657   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1658   if (cpce->is_f1_null()) {
1659     int pool_index = cpce->constant_pool_index();
1660     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1661     wrap_invokedynamic_exception(CHECK);
1662     assert(bsm_info != NULL, "");
1663     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1664     bootstrap_specifier = Handle(THREAD, bsm_info);
1665   }
1666   if (!cpce->is_f1_null()) {
1667     methodHandle method(     THREAD, cpce->f1_as_method());
1668     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1669     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1670     result.set_handle(method, appendix, method_type, THREAD);
1671     wrap_invokedynamic_exception(CHECK);
1672     return;
1673   }
1674 
1675   if (TraceMethodHandles) {
1676     ResourceMark rm(THREAD);
1677     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1678                   ConstantPool::decode_invokedynamic_index(index),
1679                   method_name->as_C_string(), method_signature->as_C_string(),
1680                   current_klass->name()->as_C_string());
1681     tty->print("  BSM info: "); bootstrap_specifier->print();
1682   }
1683 
1684   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1685 }
1686 
1687 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1688                                         Handle bootstrap_specifier,
1689                                         Symbol* method_name, Symbol* method_signature,
1690                                         Klass* current_klass,
1691                                         TRAPS) {
1692   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1693   // The appendix argument is likely to be a freshly-created CallSite.
1694   Handle       resolved_appendix;
1695   Handle       resolved_method_type;
1696   methodHandle resolved_method =
1697     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1698                                                      bootstrap_specifier,
1699                                                      method_name, method_signature,
1700                                                      &resolved_appendix,
1701                                                      &resolved_method_type,
1702                                                      THREAD);
1703   wrap_invokedynamic_exception(CHECK);
1704   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1705   wrap_invokedynamic_exception(CHECK);
1706 }
< prev index next >