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