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 ||
 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::_invokedirect   : resolve_invokespecial  (result,       pool, index, CHECK); break; // temp hack
1495     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1496     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1497     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1498   }
1499   return;
1500 }
1501 
1502 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1503                              const methodHandle& attached_method,
1504                              Bytecodes::Code byte, TRAPS) {
1505   KlassHandle defc = attached_method->method_holder();
1506   Symbol* name = attached_method->name();
1507   Symbol* type = attached_method->signature();
1508   LinkInfo link_info(defc, name, type, KlassHandle(), /*check_access=*/false);
1509   switch(byte) {
1510     case Bytecodes::_invokevirtual:
1511       resolve_virtual_call(result, recv, recv->klass(), link_info,
1512                            /*check_null_and_abstract=*/true, CHECK);
1513       break;
1514     case Bytecodes::_invokeinterface:
1515       resolve_interface_call(result, recv, recv->klass(), link_info,
1516                              /*check_null_and_abstract=*/true, CHECK);
1517       break;
1518     case Bytecodes::_invokestatic:
1519       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1520       break;
1521     case Bytecodes::_invokespecial:
1522       resolve_special_call(result, link_info, CHECK);
1523       break;
1524     default:
1525       fatal("bad call: %s", Bytecodes::name(byte));
1526   }
1527 }
1528 
1529 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1530   LinkInfo link_info(pool, index, CHECK);
1531   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1532 }
1533 
1534 
1535 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1536   LinkInfo link_info(pool, index, CHECK);
1537   resolve_special_call(result, link_info, CHECK);
1538 }
1539 
1540 
1541 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1542                                           const constantPoolHandle& pool, int index,
1543                                           TRAPS) {
1544 
1545   LinkInfo link_info(pool, index, CHECK);
1546   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1547   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1548 }
1549 
1550 
1551 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1552   LinkInfo link_info(pool, index, CHECK);
1553   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1554   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1555 }
1556 
1557 
1558 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1559   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1560   LinkInfo link_info(pool, index, CHECK);
1561   if (TraceMethodHandles) {
1562     ResourceMark rm(THREAD);
1563     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1564                   link_info.signature()->as_C_string());
1565   }
1566   resolve_handle_call(result, link_info, CHECK);
1567 }
1568 
1569 void LinkResolver::resolve_handle_call(CallInfo& result,
1570                                        const LinkInfo& link_info,
1571                                        TRAPS) {
1572   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1573   assert(link_info.resolved_klass()() == SystemDictionary::MethodHandle_klass() ||
1574          link_info.resolved_klass()() == SystemDictionary::VarHandle_klass() ||
1575          link_info.resolved_klass()() == SystemDictionary::FieldHandle_klass() ||
1576          link_info.resolved_klass()() == SystemDictionary::ArrayHandle_klass(), "");
1577   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1578   Handle       resolved_appendix;
1579   Handle       resolved_method_type;
1580   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1581                                        &resolved_appendix, &resolved_method_type, CHECK);
1582   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
1583 }
1584 
1585 static void wrap_invokedynamic_exception(TRAPS) {
1586   if (HAS_PENDING_EXCEPTION) {
1587     if (TraceMethodHandles) {
1588       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1589       PENDING_EXCEPTION->print();
1590     }
1591     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1592       // throw these guys, since they are already wrapped
1593       return;
1594     }
1595     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1596       // intercept only LinkageErrors which might have failed to wrap
1597       return;
1598     }
1599     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1600     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1601     CLEAR_PENDING_EXCEPTION;
1602     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1603   }
1604 }
1605 
1606 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1607   Symbol* method_name       = pool->name_ref_at(index);
1608   Symbol* method_signature  = pool->signature_ref_at(index);
1609   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1610 
1611   // Resolve the bootstrap specifier (BSM + optional arguments).
1612   Handle bootstrap_specifier;
1613   // Check if CallSite has been bound already:
1614   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1615   if (cpce->is_f1_null()) {
1616     int pool_index = cpce->constant_pool_index();
1617     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1618     wrap_invokedynamic_exception(CHECK);
1619     assert(bsm_info != NULL, "");
1620     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1621     bootstrap_specifier = Handle(THREAD, bsm_info);
1622   }
1623   if (!cpce->is_f1_null()) {
1624     methodHandle method(     THREAD, cpce->f1_as_method());
1625     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1626     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1627     result.set_handle(method, appendix, method_type, THREAD);
1628     wrap_invokedynamic_exception(CHECK);
1629     return;
1630   }
1631 
1632   if (TraceMethodHandles) {
1633     ResourceMark rm(THREAD);
1634     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1635                   ConstantPool::decode_invokedynamic_index(index),
1636                   method_name->as_C_string(), method_signature->as_C_string(),
1637                   current_klass->name()->as_C_string());
1638     tty->print("  BSM info: "); bootstrap_specifier->print();
1639   }
1640 
1641   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1642 }
1643 
1644 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1645                                         Handle bootstrap_specifier,
1646                                         Symbol* method_name, Symbol* method_signature,
1647                                         KlassHandle current_klass,
1648                                         TRAPS) {
1649   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1650   // The appendix argument is likely to be a freshly-created CallSite.
1651   Handle       resolved_appendix;
1652   Handle       resolved_method_type;
1653   methodHandle resolved_method =
1654     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1655                                                      bootstrap_specifier,
1656                                                      method_name, method_signature,
1657                                                      &resolved_appendix,
1658                                                      &resolved_method_type,
1659                                                      THREAD);
1660   wrap_invokedynamic_exception(CHECK);
1661   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1662   wrap_invokedynamic_exception(CHECK);
1663 }