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, true, 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     bool nostatics = (code == Bytecodes::_invokestatic) ? false : true;
 622     return resolve_interface_method(link_info, nostatics, THREAD);
 623   }
 624 }
 625 
 626 // Check and print a loader constraint violation message for method or interface method
 627 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 628                                                    const methodHandle& resolved_method,
 629                                                    const char* method_type, TRAPS) {
 630   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 631   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 632 
 633   ResourceMark rm(THREAD);
 634   Symbol* failed_type_symbol =
 635     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 636                                               resolved_loader, true, CHECK);
 637   if (failed_type_symbol != NULL) {
 638     const char* msg = "loader constraint violation: when resolving %s"
 639       " \"%s\" the class loader (instance of %s) of the current class, %s,"
 640       " and the class loader (instance of %s) for the method's defining class, %s, have"
 641       " different Class objects for the type %s used in the signature";
 642     char* sig = link_info.method_string();
 643     const char* loader1_name = SystemDictionary::loader_name(current_loader());
 644     char* current = link_info.current_klass()->name()->as_C_string();
 645     const char* loader2_name = SystemDictionary::loader_name(resolved_loader());
 646     char* target = resolved_method->method_holder()->name()->as_C_string();
 647     char* failed_type_name = failed_type_symbol->as_C_string();
 648     size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) +
 649       strlen(current) + strlen(loader2_name) + strlen(target) +
 650       strlen(failed_type_name) + strlen(method_type) + 1;
 651     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 652     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
 653                  target, failed_type_name);
 654     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 655   }
 656 }
 657 
 658 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 659                                                   KlassHandle current_klass,
 660                                                   KlassHandle sel_klass, TRAPS) {
 661   Handle ref_loader(THREAD, current_klass->class_loader());
 662   Handle sel_loader(THREAD, sel_klass->class_loader());
 663 
 664   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 665   Symbol* failed_type_symbol =
 666     SystemDictionary::check_signature_loaders(sig,
 667                                               ref_loader, sel_loader,
 668                                               false,
 669                                               CHECK);
 670   if (failed_type_symbol != NULL) {
 671     const char* msg = "loader constraint violation: when resolving field"
 672       " \"%s\" the class loader (instance of %s) of the referring class, "
 673       "%s, and the class loader (instance of %s) for the field's resolved "
 674       "type, %s, have different Class objects for that type";
 675     char* field_name = field->as_C_string();
 676     const char* loader1_name = SystemDictionary::loader_name(ref_loader());
 677     char* sel = sel_klass->name()->as_C_string();
 678     const char* loader2_name = SystemDictionary::loader_name(sel_loader());
 679     char* failed_type_name = failed_type_symbol->as_C_string();
 680     size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) +
 681                     strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1;
 682     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 683     jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name,
 684                      failed_type_name);
 685     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 686   }
 687 }
 688 
 689 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 690                                           bool require_methodref, TRAPS) {
 691 
 692   Handle nested_exception;
 693   KlassHandle resolved_klass = link_info.resolved_klass();
 694 
 695   // 1. check if methodref required, that resolved_klass is not interfacemethodref
 696   if (require_methodref && resolved_klass->is_interface()) {
 697     ResourceMark rm(THREAD);
 698     char buf[200];
 699     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 700         resolved_klass()->external_name());
 701     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 702   }
 703 
 704   // 2. lookup method in resolved klass and its super klasses
 705   methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL);
 706 
 707   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 708     // 3. lookup method in all the interfaces implemented by the resolved klass
 709     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 710 
 711     if (resolved_method.is_null()) {
 712       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 713       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 714       if (HAS_PENDING_EXCEPTION) {
 715         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 716         CLEAR_PENDING_EXCEPTION;
 717       }
 718     }
 719   }
 720 
 721   if (resolved_method.is_null()) {
 722     // 4. method lookup failed
 723     ResourceMark rm(THREAD);
 724     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 725                     Method::name_and_sig_as_C_string(resolved_klass(),
 726                                                      link_info.name(),
 727                                                      link_info.signature()),
 728                     nested_exception, NULL);
 729   }
 730 
 731   // 5. access checks, access checking may be turned off when calling from within the VM.
 732   KlassHandle current_klass = link_info.current_klass();
 733   if (link_info.check_access()) {
 734     assert(current_klass.not_null() , "current_klass should not be null");
 735 
 736     // check if method can be accessed by the referring class
 737     check_method_accessability(current_klass,
 738                                resolved_klass,
 739                                KlassHandle(THREAD, resolved_method->method_holder()),
 740                                resolved_method,
 741                                CHECK_NULL);
 742 
 743     // check loader constraints
 744     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 745   }
 746 
 747   return resolved_method;
 748 }
 749 
 750 static void trace_method_resolution(const char* prefix,
 751                                     KlassHandle klass,
 752                                     KlassHandle resolved_klass,
 753                                     const methodHandle& method,
 754                                     bool logitables,
 755                                     int index = -1) {
 756 #ifndef PRODUCT
 757   ResourceMark rm;
 758   outputStream* st;
 759   if (logitables) {
 760     st = LogHandle(itables)::trace_stream();
 761   } else {
 762     st = LogHandle(vtables)::trace_stream();
 763   }
 764   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 765             prefix,
 766             (klass.is_null() ? "<NULL>" : klass->internal_name()),
 767             (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
 768             Method::name_and_sig_as_C_string(resolved_klass(),
 769                                              method->name(),
 770                                              method->signature()),
 771             method->method_holder()->internal_name());
 772   method->print_linkage_flags(st);
 773   if (index != -1) {
 774     st->print("vtable_index:%d", index);
 775   }
 776   st->cr();
 777 #endif // PRODUCT
 778 }
 779 
 780 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info,
 781                                                     bool nostatics, TRAPS) {
 782 
 783   KlassHandle resolved_klass = link_info.resolved_klass();
 784 
 785   // check if klass is interface
 786   if (!resolved_klass->is_interface()) {
 787     ResourceMark rm(THREAD);
 788     char buf[200];
 789     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
 790     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 791   }
 792 
 793   // lookup method in this interface or its super, java.lang.Object
 794   // JDK8: also look for static methods
 795   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
 796 
 797   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 798     // lookup method in all the super-interfaces
 799     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 800   }
 801 
 802   if (resolved_method.is_null()) {
 803     // no method found
 804     ResourceMark rm(THREAD);
 805     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 806                    Method::name_and_sig_as_C_string(resolved_klass(),
 807                                                     link_info.name(),
 808                                                     link_info.signature()));
 809   }
 810 
 811   if (link_info.check_access()) {
 812     // JDK8 adds non-public interface methods, and accessability check requirement
 813     KlassHandle current_klass = link_info.current_klass();
 814 
 815     assert(current_klass.not_null() , "current_klass should not be null");
 816 
 817     // check if method can be accessed by the referring class
 818     check_method_accessability(current_klass,
 819                                resolved_klass,
 820                                KlassHandle(THREAD, resolved_method->method_holder()),
 821                                resolved_method,
 822                                CHECK_NULL);
 823 
 824     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 825   }
 826 
 827   if (nostatics && resolved_method->is_static()) {
 828     ResourceMark rm(THREAD);
 829     char buf[200];
 830     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 831                  Method::name_and_sig_as_C_string(resolved_klass(),
 832                  resolved_method->name(), resolved_method->signature()));
 833     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 834   }
 835 
 836   if (log_develop_is_enabled(Trace, itables)) {
 837     trace_method_resolution("invokeinterface resolved method: caller-class",
 838                             link_info.current_klass(), resolved_klass,
 839                             resolved_method, true);
 840   }
 841 
 842   return resolved_method;
 843 }
 844 
 845 //------------------------------------------------------------------------------------------------------------------------
 846 // Field resolution
 847 
 848 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
 849                                              KlassHandle resolved_klass,
 850                                              KlassHandle sel_klass,
 851                                              const fieldDescriptor& fd,
 852                                              TRAPS) {
 853   if (!Reflection::verify_field_access(ref_klass(),
 854                                        resolved_klass(),
 855                                        sel_klass(),
 856                                        fd.access_flags(),
 857                                        true)) {
 858     ResourceMark rm(THREAD);
 859     Exceptions::fthrow(
 860       THREAD_AND_LOCATION,
 861       vmSymbols::java_lang_IllegalAccessError(),
 862       "tried to access field %s.%s from class %s",
 863       sel_klass->external_name(),
 864       fd.name()->as_C_string(),
 865       ref_klass->external_name()
 866     );
 867     return;
 868   }
 869 }
 870 
 871 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
 872   LinkInfo link_info(pool, index, CHECK);
 873   resolve_field(fd, link_info, byte, true, CHECK);
 874 }
 875 
 876 void LinkResolver::resolve_field(fieldDescriptor& fd,
 877                                  const LinkInfo& link_info,
 878                                  Bytecodes::Code byte, bool initialize_class,
 879                                  TRAPS) {
 880   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 881          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 882          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 883          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 884 
 885   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 886   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 887   // Check if there's a resolved klass containing the field
 888   KlassHandle resolved_klass = link_info.resolved_klass();
 889   Symbol* field = link_info.name();
 890   Symbol* sig = link_info.signature();
 891 
 892   if (resolved_klass.is_null()) {
 893     ResourceMark rm(THREAD);
 894     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 895   }
 896 
 897   // Resolve instance field
 898   KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd));
 899   // check if field exists; i.e., if a klass containing the field def has been selected
 900   if (sel_klass.is_null()) {
 901     ResourceMark rm(THREAD);
 902     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 903   }
 904 
 905   if (!link_info.check_access())
 906     // Access checking may be turned off when calling from within the VM.
 907     return;
 908 
 909   // check access
 910   KlassHandle current_klass = link_info.current_klass();
 911   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 912 
 913   // check for errors
 914   if (is_static != fd.is_static()) {
 915     ResourceMark rm(THREAD);
 916     char msg[200];
 917     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
 918     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 919   }
 920 
 921   // Final fields can only be accessed from its own class.
 922   if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {
 923     THROW(vmSymbols::java_lang_IllegalAccessError());
 924   }
 925 
 926   // initialize resolved_klass if necessary
 927   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
 928   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
 929   //
 930   // note 2: we don't want to force initialization if we are just checking
 931   //         if the field access is legal; e.g., during compilation
 932   if (is_static && initialize_class) {
 933     sel_klass->initialize(CHECK);
 934   }
 935 
 936   if (sel_klass() != current_klass()) {
 937     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
 938   }
 939 
 940   // return information. note that the klass is set to the actual klass containing the
 941   // field, otherwise access of static fields in superclasses will not work.
 942 }
 943 
 944 
 945 //------------------------------------------------------------------------------------------------------------------------
 946 // Invoke resolution
 947 //
 948 // Naming conventions:
 949 //
 950 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
 951 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
 952 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
 953 // recv_klass         the receiver klass
 954 
 955 
 956 void LinkResolver::resolve_static_call(CallInfo& result,
 957                                        const LinkInfo& link_info,
 958                                        bool initialize_class, TRAPS) {
 959   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
 960 
 961   // The resolved class can change as a result of this resolution.
 962   KlassHandle resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());
 963 
 964   Method* save_resolved_method = resolved_method();
 965   // Initialize klass (this should only happen if everything is ok)
 966   if (initialize_class && resolved_klass->should_be_initialized()) {
 967     resolved_klass->initialize(CHECK);
 968     // Use updated LinkInfo (to reresolve with resolved_klass as method_holder?)
 969     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
 970                       link_info.current_klass(), link_info.check_access());
 971     resolved_method = linktime_resolve_static_method(new_info, CHECK);
 972   }
 973 
 974   assert(save_resolved_method == resolved_method(), "does this change?");
 975   // setup result
 976   result.set_static(resolved_klass, resolved_method, CHECK);
 977 }
 978 
 979 // throws linktime exceptions
 980 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
 981 
 982   KlassHandle resolved_klass = link_info.resolved_klass();
 983   methodHandle resolved_method;
 984   if (!resolved_klass->is_interface()) {
 985     resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL);
 986   } else {
 987     resolved_method = resolve_interface_method(link_info, /*nostatics*/false, CHECK_NULL);
 988   }
 989   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
 990 
 991   // check if static
 992   if (!resolved_method->is_static()) {
 993     ResourceMark rm(THREAD);
 994     char buf[200];
 995     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
 996                                                       resolved_method->name(),
 997                                                       resolved_method->signature()));
 998     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 999   }
1000   return resolved_method;
1001 }
1002 
1003 
1004 void LinkResolver::resolve_special_call(CallInfo& result,
1005                                         const LinkInfo& link_info,
1006                                         TRAPS) {
1007   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1008   runtime_resolve_special_method(result, resolved_method,
1009                                  link_info.resolved_klass(),
1010                                  link_info.current_klass(),
1011                                  link_info.check_access(), CHECK);
1012 }
1013 
1014 // throws linktime exceptions
1015 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1016                                                            TRAPS) {
1017 
1018   // Invokespecial is called for multiple special reasons:
1019   // <init>
1020   // local private method invocation, for classes and interfaces
1021   // superclass.method, which can also resolve to a default method
1022   // and the selected method is recalculated relative to the direct superclass
1023   // superinterface.method, which explicitly does not check shadowing
1024   KlassHandle resolved_klass = link_info.resolved_klass();
1025   methodHandle resolved_method;
1026 
1027   if (!resolved_klass->is_interface()) {
1028     resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL);
1029   } else {
1030     resolved_method = resolve_interface_method(link_info, /*nostatics*/true, CHECK_NULL);
1031   }
1032 
1033   // check if method name is <init>, that it is found in same klass as static type
1034   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1035       resolved_method->method_holder() != resolved_klass()) {
1036     ResourceMark rm(THREAD);
1037     Exceptions::fthrow(
1038       THREAD_AND_LOCATION,
1039       vmSymbols::java_lang_NoSuchMethodError(),
1040       "%s: method %s%s not found",
1041       resolved_klass->external_name(),
1042       resolved_method->name()->as_C_string(),
1043       resolved_method->signature()->as_C_string()
1044     );
1045     return NULL;
1046   }
1047 
1048   // check if invokespecial's interface method reference is in an indirect superinterface
1049   KlassHandle current_klass = link_info.current_klass();
1050   if (!current_klass.is_null() && resolved_klass->is_interface()) {
1051     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
1052                                   current_klass() :
1053                                   InstanceKlass::cast(current_klass())->host_klass();
1054     // Disable verification for the dynamically-generated reflection bytecodes.
1055     bool is_reflect = klass_to_check->is_subclass_of(
1056                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1057 
1058     if (!is_reflect &&
1059         !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {
1060       ResourceMark rm(THREAD);
1061       char buf[200];
1062       jio_snprintf(buf, sizeof(buf),
1063                    "Interface method reference: %s, is in an indirect superinterface of %s",
1064                    Method::name_and_sig_as_C_string(resolved_klass(),
1065                                                          resolved_method->name(),
1066                                                          resolved_method->signature()),
1067                    current_klass->external_name());
1068       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1069     }
1070   }
1071 
1072   // check if not static
1073   if (resolved_method->is_static()) {
1074     ResourceMark rm(THREAD);
1075     char buf[200];
1076     jio_snprintf(buf, sizeof(buf),
1077                  "Expecting non-static method %s",
1078                  Method::name_and_sig_as_C_string(resolved_klass(),
1079                                                   resolved_method->name(),
1080                                                   resolved_method->signature()));
1081     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1082   }
1083 
1084   if (log_develop_is_enabled(Trace, itables)) {
1085     trace_method_resolution("invokespecial resolved method: caller-class:",
1086                             current_klass, resolved_klass, resolved_method, true);
1087   }
1088 
1089   return resolved_method;
1090 }
1091 
1092 // throws runtime exceptions
1093 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1094                                                   const methodHandle& resolved_method,
1095                                                   KlassHandle resolved_klass,
1096                                                   KlassHandle current_klass,
1097                                                   bool check_access, TRAPS) {
1098 
1099   // resolved method is selected method unless we have an old-style lookup
1100   // for a superclass method
1101   // Invokespecial for a superinterface, resolved method is selected method,
1102   // no checks for shadowing
1103   methodHandle sel_method(THREAD, resolved_method());
1104 
1105   // check if this is an old-style super call and do a new lookup if so
1106   { KlassHandle method_klass  = KlassHandle(THREAD,
1107                                             resolved_method->method_holder());
1108 
1109     if (check_access &&
1110         // a) check if ACC_SUPER flag is set for the current class
1111         (current_klass->is_super() || !AllowNonVirtualCalls) &&
1112         // b) check if the class of the resolved_klass is a superclass
1113         // (not supertype in order to exclude interface classes) of the current class.
1114         // This check is not performed for super.invoke for interface methods
1115         // in super interfaces.
1116         current_klass->is_subclass_of(resolved_klass()) &&
1117         current_klass() != resolved_klass() &&
1118         // c) check if the method is not <init>
1119         resolved_method->name() != vmSymbols::object_initializer_name()) {
1120       // Lookup super method
1121       KlassHandle super_klass(THREAD, current_klass->super());
1122       sel_method = lookup_instance_method_in_klasses(super_klass,
1123                            resolved_method->name(),
1124                            resolved_method->signature(), CHECK);
1125       // check if found
1126       if (sel_method.is_null()) {
1127         ResourceMark rm(THREAD);
1128         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1129                   Method::name_and_sig_as_C_string(resolved_klass(),
1130                                             resolved_method->name(),
1131                                             resolved_method->signature()));
1132       }
1133     }
1134   }
1135 
1136   // check if not static
1137   if (sel_method->is_static()) {
1138     ResourceMark rm(THREAD);
1139     char buf[200];
1140     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1141                                                                                                              resolved_method->name(),
1142                                                                                                              resolved_method->signature()));
1143     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1144   }
1145 
1146   // check if abstract
1147   if (sel_method->is_abstract()) {
1148     ResourceMark rm(THREAD);
1149     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1150               Method::name_and_sig_as_C_string(resolved_klass(),
1151                                                sel_method->name(),
1152                                                sel_method->signature()));
1153   }
1154 
1155   if (log_develop_is_enabled(Trace, itables)) {
1156     trace_method_resolution("invokespecial selected method: resolved-class:",
1157                             resolved_klass, resolved_klass, sel_method, true);
1158   }
1159 
1160   // setup result
1161   result.set_static(resolved_klass, sel_method, CHECK);
1162 }
1163 
1164 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass,
1165                                         const LinkInfo& link_info,
1166                                         bool check_null_and_abstract, TRAPS) {
1167   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1168   runtime_resolve_virtual_method(result, resolved_method,
1169                                  link_info.resolved_klass(),
1170                                  recv, receiver_klass,
1171                                  check_null_and_abstract, CHECK);
1172 }
1173 
1174 // throws linktime exceptions
1175 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1176                                                            TRAPS) {
1177   // normal method resolution
1178   methodHandle resolved_method = resolve_method(link_info, /*require_methodref*/true, CHECK_NULL);
1179 
1180   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1181   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1182 
1183   // check if private interface method
1184   KlassHandle resolved_klass = link_info.resolved_klass();
1185   KlassHandle current_klass = link_info.current_klass();
1186 
1187   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1188     ResourceMark rm(THREAD);
1189     char buf[200];
1190     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1191                  Method::name_and_sig_as_C_string(resolved_klass(),
1192                                                   resolved_method->name(),
1193                                                   resolved_method->signature()),
1194                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1195     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1196   }
1197 
1198   // check if not static
1199   if (resolved_method->is_static()) {
1200     ResourceMark rm(THREAD);
1201     char buf[200];
1202     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1203                                                                                                              resolved_method->name(),
1204                                                                                                              resolved_method->signature()));
1205     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1206   }
1207 
1208   if (log_develop_is_enabled(Trace, vtables)) {
1209     trace_method_resolution("invokevirtual resolved method: caller-class:",
1210                             current_klass, resolved_klass, resolved_method, false);
1211   }
1212 
1213   return resolved_method;
1214 }
1215 
1216 // throws runtime exceptions
1217 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1218                                                   const methodHandle& resolved_method,
1219                                                   KlassHandle resolved_klass,
1220                                                   Handle recv,
1221                                                   KlassHandle recv_klass,
1222                                                   bool check_null_and_abstract,
1223                                                   TRAPS) {
1224 
1225   // setup default return values
1226   int vtable_index = Method::invalid_vtable_index;
1227   methodHandle selected_method;
1228 
1229   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1230 
1231   // runtime method resolution
1232   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1233     THROW(vmSymbols::java_lang_NullPointerException());
1234   }
1235 
1236   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1237   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1238   // a missing receiver might result in a bogus lookup.
1239   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1240 
1241   // do lookup based on receiver klass using the vtable index
1242   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1243     vtable_index = vtable_index_of_interface_method(resolved_klass,
1244                            resolved_method);
1245     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1246 
1247     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1248   } else {
1249     // at this point we are sure that resolved_method is virtual and not
1250     // a default or miranda method; therefore, it must have a valid vtable index.
1251     assert(!resolved_method->has_itable_index(), "");
1252     vtable_index = resolved_method->vtable_index();
1253     // We could get a negative vtable_index for final methods,
1254     // because as an optimization they are they are never put in the vtable,
1255     // unless they override an existing method.
1256     // If we do get a negative, it means the resolved method is the the selected
1257     // method, and it can never be changed by an override.
1258     if (vtable_index == Method::nonvirtual_vtable_index) {
1259       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1260       selected_method = resolved_method;
1261     } else {
1262       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1263     }
1264   }
1265 
1266   // check if method exists
1267   if (selected_method.is_null()) {
1268     ResourceMark rm(THREAD);
1269     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1270               Method::name_and_sig_as_C_string(resolved_klass(),
1271                                                       resolved_method->name(),
1272                                                       resolved_method->signature()));
1273   }
1274 
1275   // check if abstract
1276   if (check_null_and_abstract && selected_method->is_abstract()) {
1277     ResourceMark rm(THREAD);
1278     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1279               Method::name_and_sig_as_C_string(resolved_klass(),
1280                                                       selected_method->name(),
1281                                                       selected_method->signature()));
1282   }
1283 
1284   if (log_develop_is_enabled(Trace, vtables)) {
1285     trace_method_resolution("invokevirtual selected method: receiver-class:",
1286                             recv_klass, resolved_klass, selected_method,
1287                             false, vtable_index);
1288   }
1289   // setup result
1290   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1291 }
1292 
1293 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass,
1294                                           const LinkInfo& link_info,
1295                                           bool check_null_and_abstract, TRAPS) {
1296   // throws linktime exceptions
1297   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1298   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1299                                    recv, recv_klass, check_null_and_abstract, CHECK);
1300 }
1301 
1302 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1303                                                              TRAPS) {
1304   // normal interface method resolution
1305   methodHandle resolved_method = resolve_interface_method(link_info, true, CHECK_NULL);
1306   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1307   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1308 
1309   return resolved_method;
1310 }
1311 
1312 // throws runtime exceptions
1313 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1314                                                     const methodHandle& resolved_method,
1315                                                     KlassHandle resolved_klass,
1316                                                     Handle recv,
1317                                                     KlassHandle recv_klass,
1318                                                     bool check_null_and_abstract, TRAPS) {
1319   // check if receiver exists
1320   if (check_null_and_abstract && recv.is_null()) {
1321     THROW(vmSymbols::java_lang_NullPointerException());
1322   }
1323 
1324   // check if private interface method
1325   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1326     ResourceMark rm(THREAD);
1327     char buf[200];
1328     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s",
1329                  Method::name_and_sig_as_C_string(resolved_klass(),
1330                                                   resolved_method->name(),
1331                                                   resolved_method->signature()));
1332     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
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 }