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