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 = Log(itables)::trace_stream();
 760   } else {
 761     st = Log(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 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 780 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 781 
 782   KlassHandle resolved_klass = link_info.resolved_klass();
 783 
 784   // check if klass is interface
 785   if (!resolved_klass->is_interface()) {
 786     ResourceMark rm(THREAD);
 787     char buf[200];
 788     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
 789     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 790   }
 791 
 792   // lookup method in this interface or its super, java.lang.Object
 793   // JDK8: also look for static methods
 794   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
 795 
 796   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 797     // lookup method in all the super-interfaces
 798     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 799   }
 800 
 801   if (resolved_method.is_null()) {
 802     // no method found
 803     ResourceMark rm(THREAD);
 804     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 805                    Method::name_and_sig_as_C_string(resolved_klass(),
 806                                                     link_info.name(),
 807                                                     link_info.signature()));
 808   }
 809 
 810   if (link_info.check_access()) {
 811     // JDK8 adds non-public interface methods, and accessability check requirement
 812     KlassHandle current_klass = link_info.current_klass();
 813 
 814     assert(current_klass.not_null() , "current_klass should not be null");
 815 
 816     // check if method can be accessed by the referring class
 817     check_method_accessability(current_klass,
 818                                resolved_klass,
 819                                KlassHandle(THREAD, resolved_method->method_holder()),
 820                                resolved_method,
 821                                CHECK_NULL);
 822 
 823     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 824   }
 825 
 826   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 827     ResourceMark rm(THREAD);
 828     char buf[200];
 829     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 830                  Method::name_and_sig_as_C_string(resolved_klass(),
 831                  resolved_method->name(), resolved_method->signature()));
 832     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 833   }
 834 
 835   if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) {
 836     ResourceMark rm(THREAD);
 837     char buf[200];
 838 
 839     KlassHandle current_klass = link_info.current_klass();
 840     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s",
 841                  Method::name_and_sig_as_C_string(resolved_klass(),
 842                                                   resolved_method->name(),
 843                                                   resolved_method->signature()),
 844                                                   (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
 845      THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 846   }
 847 
 848   if (log_develop_is_enabled(Trace, itables)) {
 849     trace_method_resolution("invokeinterface resolved method: caller-class",
 850                             link_info.current_klass(), resolved_klass,
 851                             resolved_method, true);
 852   }
 853 
 854   return resolved_method;
 855 }
 856 
 857 //------------------------------------------------------------------------------------------------------------------------
 858 // Field resolution
 859 
 860 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
 861                                              KlassHandle resolved_klass,
 862                                              KlassHandle sel_klass,
 863                                              const fieldDescriptor& fd,
 864                                              TRAPS) {
 865   if (!Reflection::verify_field_access(ref_klass(),
 866                                        resolved_klass(),
 867                                        sel_klass(),
 868                                        fd.access_flags(),
 869                                        true)) {
 870     ResourceMark rm(THREAD);
 871     Exceptions::fthrow(
 872       THREAD_AND_LOCATION,
 873       vmSymbols::java_lang_IllegalAccessError(),
 874       "tried to access field %s.%s from class %s",
 875       sel_klass->external_name(),
 876       fd.name()->as_C_string(),
 877       ref_klass->external_name()
 878     );
 879     return;
 880   }
 881 }
 882 
 883 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
 884   LinkInfo link_info(pool, index, CHECK);
 885   resolve_field(fd, link_info, byte, true, CHECK);
 886 }
 887 
 888 void LinkResolver::resolve_field(fieldDescriptor& fd,
 889                                  const LinkInfo& link_info,
 890                                  Bytecodes::Code byte, bool initialize_class,
 891                                  TRAPS) {
 892   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 893          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 894          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 895          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 896 
 897   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 898   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 899   // Check if there's a resolved klass containing the field
 900   KlassHandle resolved_klass = link_info.resolved_klass();
 901   Symbol* field = link_info.name();
 902   Symbol* sig = link_info.signature();
 903 
 904   if (resolved_klass.is_null()) {
 905     ResourceMark rm(THREAD);
 906     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 907   }
 908 
 909   // Resolve instance field
 910   KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd));
 911   // check if field exists; i.e., if a klass containing the field def has been selected
 912   if (sel_klass.is_null()) {
 913     ResourceMark rm(THREAD);
 914     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 915   }
 916 
 917   if (!link_info.check_access())
 918     // Access checking may be turned off when calling from within the VM.
 919     return;
 920 
 921   // check access
 922   KlassHandle current_klass = link_info.current_klass();
 923   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 924 
 925   // check for errors
 926   if (is_static != fd.is_static()) {
 927     ResourceMark rm(THREAD);
 928     char msg[200];
 929     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
 930     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 931   }
 932 
 933   // Final fields can only be accessed from its own class.
 934   if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {
 935     THROW(vmSymbols::java_lang_IllegalAccessError());
 936   }
 937 
 938   // initialize resolved_klass if necessary
 939   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
 940   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
 941   //
 942   // note 2: we don't want to force initialization if we are just checking
 943   //         if the field access is legal; e.g., during compilation
 944   if (is_static && initialize_class) {
 945     sel_klass->initialize(CHECK);
 946   }
 947 
 948   if (sel_klass() != current_klass()) {
 949     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
 950   }
 951 
 952   // return information. note that the klass is set to the actual klass containing the
 953   // field, otherwise access of static fields in superclasses will not work.
 954 }
 955 
 956 
 957 //------------------------------------------------------------------------------------------------------------------------
 958 // Invoke resolution
 959 //
 960 // Naming conventions:
 961 //
 962 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
 963 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
 964 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
 965 // recv_klass         the receiver klass
 966 
 967 
 968 void LinkResolver::resolve_static_call(CallInfo& result,
 969                                        const LinkInfo& link_info,
 970                                        bool initialize_class, TRAPS) {
 971   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
 972 
 973   // The resolved class can change as a result of this resolution.
 974   KlassHandle resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());
 975 
 976   Method* save_resolved_method = resolved_method();
 977   // Initialize klass (this should only happen if everything is ok)
 978   if (initialize_class && resolved_klass->should_be_initialized()) {
 979     resolved_klass->initialize(CHECK);
 980     // Use updated LinkInfo (to reresolve with resolved_klass as method_holder?)
 981     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
 982                       link_info.current_klass(), link_info.check_access());
 983     resolved_method = linktime_resolve_static_method(new_info, CHECK);
 984   }
 985 
 986   assert(save_resolved_method == resolved_method(), "does this change?");
 987   // setup result
 988   result.set_static(resolved_klass, resolved_method, CHECK);
 989 }
 990 
 991 // throws linktime exceptions
 992 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
 993 
 994   KlassHandle resolved_klass = link_info.resolved_klass();
 995   methodHandle resolved_method;
 996   if (!resolved_klass->is_interface()) {
 997     resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL);
 998   } else {
 999     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1000   }
1001   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1002 
1003   // check if static
1004   if (!resolved_method->is_static()) {
1005     ResourceMark rm(THREAD);
1006     char buf[200];
1007     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1008                                                       resolved_method->name(),
1009                                                       resolved_method->signature()));
1010     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1011   }
1012   return resolved_method;
1013 }
1014 
1015 
1016 void LinkResolver::resolve_special_call(CallInfo& result,
1017                                         const LinkInfo& link_info,
1018                                         TRAPS) {
1019   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1020   runtime_resolve_special_method(result, resolved_method,
1021                                  link_info.resolved_klass(),
1022                                  link_info.current_klass(),
1023                                  link_info.check_access(), CHECK);
1024 }
1025 
1026 // throws linktime exceptions
1027 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1028                                                            TRAPS) {
1029 
1030   // Invokespecial is called for multiple special reasons:
1031   // <init>
1032   // local private method invocation, for classes and interfaces
1033   // superclass.method, which can also resolve to a default method
1034   // and the selected method is recalculated relative to the direct superclass
1035   // superinterface.method, which explicitly does not check shadowing
1036   KlassHandle resolved_klass = link_info.resolved_klass();
1037   methodHandle resolved_method;
1038 
1039   if (!resolved_klass->is_interface()) {
1040     resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL);
1041   } else {
1042     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1043   }
1044 
1045   // check if method name is <init>, that it is found in same klass as static type
1046   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1047       resolved_method->method_holder() != resolved_klass()) {
1048     ResourceMark rm(THREAD);
1049     Exceptions::fthrow(
1050       THREAD_AND_LOCATION,
1051       vmSymbols::java_lang_NoSuchMethodError(),
1052       "%s: method %s%s not found",
1053       resolved_klass->external_name(),
1054       resolved_method->name()->as_C_string(),
1055       resolved_method->signature()->as_C_string()
1056     );
1057     return NULL;
1058   }
1059 
1060   // check if invokespecial's interface method reference is in an indirect superinterface
1061   KlassHandle current_klass = link_info.current_klass();
1062   if (!current_klass.is_null() && resolved_klass->is_interface()) {
1063     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
1064                                   current_klass() :
1065                                   InstanceKlass::cast(current_klass())->host_klass();
1066     // Disable verification for the dynamically-generated reflection bytecodes.
1067     bool is_reflect = klass_to_check->is_subclass_of(
1068                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1069 
1070     if (!is_reflect &&
1071         !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {
1072       ResourceMark rm(THREAD);
1073       char buf[200];
1074       jio_snprintf(buf, sizeof(buf),
1075                    "Interface method reference: %s, is in an indirect superinterface of %s",
1076                    Method::name_and_sig_as_C_string(resolved_klass(),
1077                                                          resolved_method->name(),
1078                                                          resolved_method->signature()),
1079                    current_klass->external_name());
1080       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1081     }
1082   }
1083 
1084   // check if not static
1085   if (resolved_method->is_static()) {
1086     ResourceMark rm(THREAD);
1087     char buf[200];
1088     jio_snprintf(buf, sizeof(buf),
1089                  "Expecting non-static method %s",
1090                  Method::name_and_sig_as_C_string(resolved_klass(),
1091                                                   resolved_method->name(),
1092                                                   resolved_method->signature()));
1093     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1094   }
1095 
1096   if (log_develop_is_enabled(Trace, itables)) {
1097     trace_method_resolution("invokespecial resolved method: caller-class:",
1098                             current_klass, resolved_klass, resolved_method, true);
1099   }
1100 
1101   return resolved_method;
1102 }
1103 
1104 // throws runtime exceptions
1105 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1106                                                   const methodHandle& resolved_method,
1107                                                   KlassHandle resolved_klass,
1108                                                   KlassHandle current_klass,
1109                                                   bool check_access, TRAPS) {
1110 
1111   // resolved method is selected method unless we have an old-style lookup
1112   // for a superclass method
1113   // Invokespecial for a superinterface, resolved method is selected method,
1114   // no checks for shadowing
1115   methodHandle sel_method(THREAD, resolved_method());
1116 
1117   // check if this is an old-style super call and do a new lookup if so
1118   { KlassHandle method_klass  = KlassHandle(THREAD,
1119                                             resolved_method->method_holder());
1120 
1121     if (check_access &&
1122         // a) check if ACC_SUPER flag is set for the current class
1123         (current_klass->is_super() || !AllowNonVirtualCalls) &&
1124         // b) check if the class of the resolved_klass is a superclass
1125         // (not supertype in order to exclude interface classes) of the current class.
1126         // This check is not performed for super.invoke for interface methods
1127         // in super interfaces.
1128         current_klass->is_subclass_of(resolved_klass()) &&
1129         current_klass() != resolved_klass() &&
1130         // c) check if the method is not <init>
1131         resolved_method->name() != vmSymbols::object_initializer_name()) {
1132       // Lookup super method
1133       KlassHandle super_klass(THREAD, current_klass->super());
1134       sel_method = lookup_instance_method_in_klasses(super_klass,
1135                            resolved_method->name(),
1136                            resolved_method->signature(), CHECK);
1137       // check if found
1138       if (sel_method.is_null()) {
1139         ResourceMark rm(THREAD);
1140         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1141                   Method::name_and_sig_as_C_string(resolved_klass(),
1142                                             resolved_method->name(),
1143                                             resolved_method->signature()));
1144       }
1145     }
1146   }
1147 
1148   // check if not static
1149   if (sel_method->is_static()) {
1150     ResourceMark rm(THREAD);
1151     char buf[200];
1152     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1153                                                                                                              resolved_method->name(),
1154                                                                                                              resolved_method->signature()));
1155     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1156   }
1157 
1158   // check if abstract
1159   if (sel_method->is_abstract()) {
1160     ResourceMark rm(THREAD);
1161     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1162               Method::name_and_sig_as_C_string(resolved_klass(),
1163                                                sel_method->name(),
1164                                                sel_method->signature()));
1165   }
1166 
1167   if (log_develop_is_enabled(Trace, itables)) {
1168     trace_method_resolution("invokespecial selected method: resolved-class:",
1169                             resolved_klass, resolved_klass, sel_method, true);
1170   }
1171 
1172   // setup result
1173   result.set_static(resolved_klass, sel_method, CHECK);
1174 }
1175 
1176 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass,
1177                                         const LinkInfo& link_info,
1178                                         bool check_null_and_abstract, TRAPS) {
1179   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1180   runtime_resolve_virtual_method(result, resolved_method,
1181                                  link_info.resolved_klass(),
1182                                  recv, receiver_klass,
1183                                  check_null_and_abstract, CHECK);
1184 }
1185 
1186 // throws linktime exceptions
1187 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1188                                                            TRAPS) {
1189   // normal method resolution
1190   methodHandle resolved_method = resolve_method(link_info, /*require_methodref*/true, CHECK_NULL);
1191 
1192   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1193   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1194 
1195   // check if private interface method
1196   KlassHandle resolved_klass = link_info.resolved_klass();
1197   KlassHandle current_klass = link_info.current_klass();
1198 
1199   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1200     ResourceMark rm(THREAD);
1201     char buf[200];
1202     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1203                  Method::name_and_sig_as_C_string(resolved_klass(),
1204                                                   resolved_method->name(),
1205                                                   resolved_method->signature()),
1206                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1207     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1208   }
1209 
1210   // check if not static
1211   if (resolved_method->is_static()) {
1212     ResourceMark rm(THREAD);
1213     char buf[200];
1214     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1215                                                                                                              resolved_method->name(),
1216                                                                                                              resolved_method->signature()));
1217     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1218   }
1219 
1220   if (log_develop_is_enabled(Trace, vtables)) {
1221     trace_method_resolution("invokevirtual resolved method: caller-class:",
1222                             current_klass, resolved_klass, resolved_method, false);
1223   }
1224 
1225   return resolved_method;
1226 }
1227 
1228 // throws runtime exceptions
1229 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1230                                                   const methodHandle& resolved_method,
1231                                                   KlassHandle resolved_klass,
1232                                                   Handle recv,
1233                                                   KlassHandle recv_klass,
1234                                                   bool check_null_and_abstract,
1235                                                   TRAPS) {
1236 
1237   // setup default return values
1238   int vtable_index = Method::invalid_vtable_index;
1239   methodHandle selected_method;
1240 
1241   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1242 
1243   // runtime method resolution
1244   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1245     THROW(vmSymbols::java_lang_NullPointerException());
1246   }
1247 
1248   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1249   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1250   // a missing receiver might result in a bogus lookup.
1251   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1252 
1253   // do lookup based on receiver klass using the vtable index
1254   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1255     vtable_index = vtable_index_of_interface_method(resolved_klass,
1256                            resolved_method);
1257     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1258 
1259     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1260   } else {
1261     // at this point we are sure that resolved_method is virtual and not
1262     // a default or miranda method; therefore, it must have a valid vtable index.
1263     assert(!resolved_method->has_itable_index(), "");
1264     vtable_index = resolved_method->vtable_index();
1265     // We could get a negative vtable_index for final methods,
1266     // because as an optimization they are they are never put in the vtable,
1267     // unless they override an existing method.
1268     // If we do get a negative, it means the resolved method is the the selected
1269     // method, and it can never be changed by an override.
1270     if (vtable_index == Method::nonvirtual_vtable_index) {
1271       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1272       selected_method = resolved_method;
1273     } else {
1274       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1275     }
1276   }
1277 
1278   // check if method exists
1279   if (selected_method.is_null()) {
1280     ResourceMark rm(THREAD);
1281     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1282               Method::name_and_sig_as_C_string(resolved_klass(),
1283                                                       resolved_method->name(),
1284                                                       resolved_method->signature()));
1285   }
1286 
1287   // check if abstract
1288   if (check_null_and_abstract && selected_method->is_abstract()) {
1289     ResourceMark rm(THREAD);
1290     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1291               Method::name_and_sig_as_C_string(resolved_klass(),
1292                                                       selected_method->name(),
1293                                                       selected_method->signature()));
1294   }
1295 
1296   if (log_develop_is_enabled(Trace, vtables)) {
1297     trace_method_resolution("invokevirtual selected method: receiver-class:",
1298                             recv_klass, resolved_klass, selected_method,
1299                             false, vtable_index);
1300   }
1301   // setup result
1302   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1303 }
1304 
1305 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass,
1306                                           const LinkInfo& link_info,
1307                                           bool check_null_and_abstract, TRAPS) {
1308   // throws linktime exceptions
1309   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1310   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1311                                    recv, recv_klass, check_null_and_abstract, CHECK);
1312 }
1313 
1314 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1315                                                              TRAPS) {
1316   // normal interface method resolution
1317   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1318   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1319   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1320 
1321   return resolved_method;
1322 }
1323 
1324 // throws runtime exceptions
1325 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1326                                                     const methodHandle& resolved_method,
1327                                                     KlassHandle resolved_klass,
1328                                                     Handle recv,
1329                                                     KlassHandle recv_klass,
1330                                                     bool check_null_and_abstract, TRAPS) {
1331   // check if receiver exists
1332   if (check_null_and_abstract && recv.is_null()) {
1333     THROW(vmSymbols::java_lang_NullPointerException());
1334   }
1335 
1336   // check if receiver klass implements the resolved interface
1337   if (!recv_klass->is_subtype_of(resolved_klass())) {
1338     ResourceMark rm(THREAD);
1339     char buf[200];
1340     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1341                  recv_klass()->external_name(),
1342                  resolved_klass()->external_name());
1343     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1344   }
1345 
1346   // do lookup based on receiver klass
1347   // This search must match the linktime preparation search for itable initialization
1348   // to correctly enforce loader constraints for interface method inheritance
1349   methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass,
1350                                                   resolved_method->name(),
1351                                                   resolved_method->signature(), CHECK);
1352   if (sel_method.is_null() && !check_null_and_abstract) {
1353     // In theory this is a harmless placeholder value, but
1354     // in practice leaving in null affects the nsk default method tests.
1355     // This needs further study.
1356     sel_method = resolved_method;
1357   }
1358   // check if method exists
1359   if (sel_method.is_null()) {
1360     ResourceMark rm(THREAD);
1361     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1362                    Method::name_and_sig_as_C_string(recv_klass(),
1363                                                     resolved_method->name(),
1364                                                     resolved_method->signature()));
1365   }
1366   // check access
1367   // Throw Illegal Access Error if sel_method is not public.
1368   if (!sel_method->is_public()) {
1369     ResourceMark rm(THREAD);
1370     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1371               Method::name_and_sig_as_C_string(recv_klass(),
1372                                                sel_method->name(),
1373                                                sel_method->signature()));
1374   }
1375   // check if abstract
1376   if (check_null_and_abstract && sel_method->is_abstract()) {
1377     ResourceMark rm(THREAD);
1378     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1379               Method::name_and_sig_as_C_string(recv_klass(),
1380                                                       sel_method->name(),
1381                                                       sel_method->signature()));
1382   }
1383 
1384   if (log_develop_is_enabled(Trace, itables)) {
1385     trace_method_resolution("invokeinterface selected method: receiver-class",
1386                             recv_klass, resolved_klass, sel_method, true);
1387   }
1388   // setup result
1389   if (!resolved_method->has_itable_index()) {
1390     int vtable_index = resolved_method->vtable_index();
1391     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1392     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1393   } else {
1394     int itable_index = resolved_method()->itable_index();
1395     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1396   }
1397 }
1398 
1399 
1400 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1401                                                  const LinkInfo& link_info) {
1402   EXCEPTION_MARK;
1403   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1404   if (HAS_PENDING_EXCEPTION) {
1405     CLEAR_PENDING_EXCEPTION;
1406     return methodHandle();
1407   } else {
1408     return method_result;
1409   }
1410 }
1411 
1412 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1413                                                  const LinkInfo& link_info) {
1414   EXCEPTION_MARK;
1415   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1416   if (HAS_PENDING_EXCEPTION) {
1417     CLEAR_PENDING_EXCEPTION;
1418     return methodHandle();
1419   } else {
1420     return method_result;
1421   }
1422 }
1423 
1424 methodHandle LinkResolver::resolve_virtual_call_or_null(
1425                                                  KlassHandle receiver_klass,
1426                                                  const LinkInfo& link_info) {
1427   EXCEPTION_MARK;
1428   CallInfo info;
1429   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1430   if (HAS_PENDING_EXCEPTION) {
1431     CLEAR_PENDING_EXCEPTION;
1432     return methodHandle();
1433   }
1434   return info.selected_method();
1435 }
1436 
1437 methodHandle LinkResolver::resolve_interface_call_or_null(
1438                                                  KlassHandle receiver_klass,
1439                                                  const LinkInfo& link_info) {
1440   EXCEPTION_MARK;
1441   CallInfo info;
1442   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1443   if (HAS_PENDING_EXCEPTION) {
1444     CLEAR_PENDING_EXCEPTION;
1445     return methodHandle();
1446   }
1447   return info.selected_method();
1448 }
1449 
1450 int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass,
1451                                                const LinkInfo& link_info) {
1452   EXCEPTION_MARK;
1453   CallInfo info;
1454   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1455                        /*check_null_or_abstract*/false, THREAD);
1456   if (HAS_PENDING_EXCEPTION) {
1457     CLEAR_PENDING_EXCEPTION;
1458     return Method::invalid_vtable_index;
1459   }
1460   return info.vtable_index();
1461 }
1462 
1463 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1464   EXCEPTION_MARK;
1465   CallInfo info;
1466   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1467   if (HAS_PENDING_EXCEPTION) {
1468     CLEAR_PENDING_EXCEPTION;
1469     return methodHandle();
1470   }
1471   return info.selected_method();
1472 }
1473 
1474 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1475   EXCEPTION_MARK;
1476   CallInfo info;
1477   resolve_special_call(info, link_info, THREAD);
1478   if (HAS_PENDING_EXCEPTION) {
1479     CLEAR_PENDING_EXCEPTION;
1480     return methodHandle();
1481   }
1482   return info.selected_method();
1483 }
1484 
1485 
1486 
1487 //------------------------------------------------------------------------------------------------------------------------
1488 // ConstantPool entries
1489 
1490 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1491   switch (byte) {
1492     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1493     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1494     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1495     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1496     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1497     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1498   }
1499   return;
1500 }
1501 
1502 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1503                              const methodHandle& attached_method,
1504                              Bytecodes::Code byte, TRAPS) {
1505   KlassHandle defc = attached_method->method_holder();
1506   Symbol* name = attached_method->name();
1507   Symbol* type = attached_method->signature();
1508   LinkInfo link_info(defc, name, type, KlassHandle(), /*check_access=*/false);
1509   switch(byte) {
1510     case Bytecodes::_invokevirtual:
1511       resolve_virtual_call(result, recv, recv->klass(), link_info,
1512                            /*check_null_and_abstract=*/true, CHECK);
1513       break;
1514     case Bytecodes::_invokeinterface:
1515       resolve_interface_call(result, recv, recv->klass(), link_info,
1516                              /*check_null_and_abstract=*/true, CHECK);
1517       break;
1518     case Bytecodes::_invokestatic:
1519       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1520       break;
1521     case Bytecodes::_invokespecial:
1522       resolve_special_call(result, link_info, CHECK);
1523       break;
1524     default:
1525       fatal("bad call: %s", Bytecodes::name(byte));
1526   }
1527 }
1528 
1529 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1530   LinkInfo link_info(pool, index, CHECK);
1531   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1532 }
1533 
1534 
1535 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1536   LinkInfo link_info(pool, index, CHECK);
1537   resolve_special_call(result, link_info, CHECK);
1538 }
1539 
1540 
1541 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1542                                           const constantPoolHandle& pool, int index,
1543                                           TRAPS) {
1544 
1545   LinkInfo link_info(pool, index, CHECK);
1546   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1547   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1548 }
1549 
1550 
1551 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1552   LinkInfo link_info(pool, index, CHECK);
1553   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1554   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1555 }
1556 
1557 
1558 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1559   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1560   LinkInfo link_info(pool, index, CHECK);
1561   if (TraceMethodHandles) {
1562     ResourceMark rm(THREAD);
1563     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1564                   link_info.signature()->as_C_string());
1565   }
1566   resolve_handle_call(result, link_info, CHECK);
1567 }
1568 
1569 void LinkResolver::resolve_handle_call(CallInfo& result,
1570                                        const LinkInfo& link_info,
1571                                        TRAPS) {
1572   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1573   KlassHandle resolved_klass = link_info.resolved_klass();
1574   assert(resolved_klass() == SystemDictionary::MethodHandle_klass() ||
1575          resolved_klass() == SystemDictionary::VarHandle_klass(), "");
1576   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1577   Handle       resolved_appendix;
1578   Handle       resolved_method_type;
1579   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1580                                        &resolved_appendix, &resolved_method_type, CHECK);
1581   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1582 }
1583 
1584 static void wrap_invokedynamic_exception(TRAPS) {
1585   if (HAS_PENDING_EXCEPTION) {
1586     if (TraceMethodHandles) {
1587       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1588       PENDING_EXCEPTION->print();
1589     }
1590     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1591       // throw these guys, since they are already wrapped
1592       return;
1593     }
1594     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1595       // intercept only LinkageErrors which might have failed to wrap
1596       return;
1597     }
1598     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1599     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1600     CLEAR_PENDING_EXCEPTION;
1601     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1602   }
1603 }
1604 
1605 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1606   Symbol* method_name       = pool->name_ref_at(index);
1607   Symbol* method_signature  = pool->signature_ref_at(index);
1608   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1609 
1610   // Resolve the bootstrap specifier (BSM + optional arguments).
1611   Handle bootstrap_specifier;
1612   // Check if CallSite has been bound already:
1613   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1614   if (cpce->is_f1_null()) {
1615     int pool_index = cpce->constant_pool_index();
1616     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1617     wrap_invokedynamic_exception(CHECK);
1618     assert(bsm_info != NULL, "");
1619     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1620     bootstrap_specifier = Handle(THREAD, bsm_info);
1621   }
1622   if (!cpce->is_f1_null()) {
1623     methodHandle method(     THREAD, cpce->f1_as_method());
1624     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1625     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1626     result.set_handle(method, appendix, method_type, THREAD);
1627     wrap_invokedynamic_exception(CHECK);
1628     return;
1629   }
1630 
1631   if (TraceMethodHandles) {
1632     ResourceMark rm(THREAD);
1633     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1634                   ConstantPool::decode_invokedynamic_index(index),
1635                   method_name->as_C_string(), method_signature->as_C_string(),
1636                   current_klass->name()->as_C_string());
1637     tty->print("  BSM info: "); bootstrap_specifier->print();
1638   }
1639 
1640   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1641 }
1642 
1643 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1644                                         Handle bootstrap_specifier,
1645                                         Symbol* method_name, Symbol* method_signature,
1646                                         KlassHandle current_klass,
1647                                         TRAPS) {
1648   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1649   // The appendix argument is likely to be a freshly-created CallSite.
1650   Handle       resolved_appendix;
1651   Handle       resolved_method_type;
1652   methodHandle resolved_method =
1653     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1654                                                      bootstrap_specifier,
1655                                                      method_name, method_signature,
1656                                                      &resolved_appendix,
1657                                                      &resolved_method_type,
1658                                                      THREAD);
1659   wrap_invokedynamic_exception(CHECK);
1660   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1661   wrap_invokedynamic_exception(CHECK);
1662 }