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