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