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