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