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