1 /*
   2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/defaultMethods.hpp"
  27 #include "classfile/symbolTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "classfile/vmSymbols.hpp"
  30 #include "compiler/compileBroker.hpp"
  31 #include "gc/shared/collectedHeap.inline.hpp"
  32 #include "interpreter/bytecode.hpp"
  33 #include "interpreter/interpreterRuntime.hpp"
  34 #include "interpreter/linkResolver.hpp"
  35 #include "logging/log.hpp"
  36 #include "memory/resourceArea.hpp"
  37 #include "memory/universe.inline.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "oops/method.hpp"
  40 #include "oops/objArrayOop.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "prims/methodHandles.hpp"
  43 #include "prims/nativeLookup.hpp"
  44 #include "runtime/compilationPolicy.hpp"
  45 #include "runtime/fieldDescriptor.hpp"
  46 #include "runtime/frame.inline.hpp"
  47 #include "runtime/handles.inline.hpp"
  48 #include "runtime/reflection.hpp"
  49 #include "runtime/signature.hpp"
  50 #include "runtime/thread.inline.hpp"
  51 #include "runtime/vmThread.hpp"
  52 
  53 
  54 //------------------------------------------------------------------------------------------------------------------------
  55 // Implementation of CallInfo
  56 
  57 
  58 void CallInfo::set_static(KlassHandle resolved_klass, const methodHandle& resolved_method, TRAPS) {
  59   int vtable_index = Method::nonvirtual_vtable_index;
  60   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  61 }
  62 
  63 
  64 void CallInfo::set_interface(KlassHandle resolved_klass,
  65                              KlassHandle selected_klass,
  66                              const methodHandle& resolved_method,
  67                              const methodHandle& selected_method,
  68                              int itable_index, TRAPS) {
  69   // This is only called for interface methods. If the resolved_method
  70   // comes from java/lang/Object, it can be the subject of a virtual call, so
  71   // we should pick the vtable index from the resolved method.
  72   // In that case, the caller must call set_virtual instead of set_interface.
  73   assert(resolved_method->method_holder()->is_interface(), "");
  74   assert(itable_index == resolved_method()->itable_index(), "");
  75   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
  76 }
  77 
  78 void CallInfo::set_virtual(KlassHandle resolved_klass,
  79                            KlassHandle selected_klass,
  80                            const methodHandle& resolved_method,
  81                            const methodHandle& selected_method,
  82                            int vtable_index, TRAPS) {
  83   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
  84   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
  85   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
  86   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
  87   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
  88 }
  89 
  90 void CallInfo::set_handle(const methodHandle& resolved_method,
  91                           Handle resolved_appendix,
  92                           Handle resolved_method_type, TRAPS) {
  93   set_handle(SystemDictionary::MethodHandle_klass(), resolved_method, resolved_appendix, resolved_method_type, CHECK);
  94 }
  95 
  96 void CallInfo::set_handle(KlassHandle resolved_klass,
  97                           const methodHandle& resolved_method,
  98                           Handle resolved_appendix,
  99                           Handle resolved_method_type, TRAPS) {
 100   if (resolved_method.is_null()) {
 101     THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");
 102   }
 103   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 104          resolved_method->is_compiled_lambda_form(),
 105          "linkMethod must return one of these");
 106   int vtable_index = Method::nonvirtual_vtable_index;
 107   assert(!resolved_method->has_vtable_index(), "");
 108   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 109   _resolved_appendix    = resolved_appendix;
 110   _resolved_method_type = resolved_method_type;
 111 }
 112 
 113 void CallInfo::set_common(KlassHandle resolved_klass,
 114                           KlassHandle selected_klass,
 115                           const methodHandle& resolved_method,
 116                           const methodHandle& selected_method,
 117                           CallKind kind,
 118                           int index,
 119                           TRAPS) {
 120   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 121   _resolved_klass  = resolved_klass;
 122   _selected_klass  = selected_klass;
 123   _resolved_method = resolved_method;
 124   _selected_method = selected_method;
 125   _call_kind       = kind;
 126   _call_index      = index;
 127   _resolved_appendix = Handle();
 128   DEBUG_ONLY(verify());  // verify before making side effects
 129 
 130   CompilationPolicy::compile_if_required(selected_method, THREAD);
 131 }
 132 
 133 // utility query for unreflecting a method
 134 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) {
 135   Klass* resolved_method_holder = resolved_method->method_holder();
 136   if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
 137     resolved_klass = resolved_method_holder;
 138   }
 139   _resolved_klass  = resolved_klass;
 140   _selected_klass  = resolved_klass;
 141   _resolved_method = resolved_method;
 142   _selected_method = resolved_method;
 143   // classify:
 144   CallKind kind = CallInfo::unknown_kind;
 145   int index = resolved_method->vtable_index();
 146   if (resolved_method->can_be_statically_bound()) {
 147     kind = CallInfo::direct_call;
 148   } else if (!resolved_method_holder->is_interface()) {
 149     // Could be an Object method inherited into an interface, but still a vtable call.
 150     kind = CallInfo::vtable_call;
 151   } else if (!resolved_klass->is_interface()) {
 152     // A default or miranda method.  Compute the vtable index.
 153     ResourceMark rm;
 154     klassVtable* vt = resolved_klass->vtable();
 155     index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
 156                            resolved_method);
 157     assert(index >= 0 , "we should have valid vtable index at this point");
 158 
 159     kind = CallInfo::vtable_call;
 160   } else if (resolved_method->has_vtable_index()) {
 161     // Can occur if an interface redeclares a method of Object.
 162 
 163 #ifdef ASSERT
 164     // Ensure that this is really the case.
 165     KlassHandle object_klass = SystemDictionary::Object_klass();
 166     Method * object_resolved_method = object_klass()->vtable()->method_at(index);
 167     assert(object_resolved_method->name() == resolved_method->name(),
 168       "Object and interface method names should match at vtable index %d, %s != %s",
 169       index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
 170     assert(object_resolved_method->signature() == resolved_method->signature(),
 171       "Object and interface method signatures should match at vtable index %d, %s != %s",
 172       index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
 173 #endif // ASSERT
 174 
 175     kind = CallInfo::vtable_call;
 176   } else {
 177     // A regular interface call.
 178     kind = CallInfo::itable_call;
 179     index = resolved_method->itable_index();
 180   }
 181   assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
 182   _call_kind  = kind;
 183   _call_index = index;
 184   _resolved_appendix = Handle();
 185   DEBUG_ONLY(verify());
 186 }
 187 
 188 #ifdef ASSERT
 189 void CallInfo::verify() {
 190   switch (call_kind()) {  // the meaning and allowed value of index depends on kind
 191   case CallInfo::direct_call:
 192     if (_call_index == Method::nonvirtual_vtable_index)  break;
 193     // else fall through to check vtable index:
 194   case CallInfo::vtable_call:
 195     assert(resolved_klass()->verify_vtable_index(_call_index), "");
 196     break;
 197   case CallInfo::itable_call:
 198     assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
 199     break;
 200   case CallInfo::unknown_kind:
 201     assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
 202     break;
 203   default:
 204     fatal("Unexpected call kind %d", call_kind());
 205   }
 206 }
 207 #endif //ASSERT
 208 
 209 #ifndef PRODUCT
 210 void CallInfo::print() {
 211   ResourceMark rm;
 212   const char* kindstr = "unknown";
 213   switch (_call_kind) {
 214   case direct_call: kindstr = "direct"; break;
 215   case vtable_call: kindstr = "vtable"; break;
 216   case itable_call: kindstr = "itable"; break;
 217   }
 218   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 219                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 220 }
 221 #endif
 222 
 223 //------------------------------------------------------------------------------------------------------------------------
 224 // Implementation of LinkInfo
 225 
 226 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, methodHandle current_method, TRAPS) {
 227    // resolve klass
 228   Klass* result = pool->klass_ref_at(index, CHECK);
 229   _resolved_klass = KlassHandle(THREAD, result);
 230 
 231   // Get name, signature, and static klass
 232   _name          = pool->name_ref_at(index);
 233   _signature     = pool->signature_ref_at(index);
 234   _current_klass = KlassHandle(THREAD, pool->pool_holder());
 235   _current_method = current_method;
 236 
 237   // Coming from the constant pool always checks access
 238   _check_access  = true;
 239 }
 240 
 241 char* LinkInfo::method_string() const {
 242   return Method::name_and_sig_as_C_string(_resolved_klass(), _name, _signature);
 243 }
 244 
 245 #ifndef PRODUCT
 246 void LinkInfo::print() {
 247   ResourceMark rm;
 248   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 249                 _resolved_klass->name()->as_C_string(),
 250                 _name->as_C_string(),
 251                 _signature->as_C_string(),
 252                 _current_klass.is_null() ? "(none)" : _current_klass->name()->as_C_string(),
 253                 _check_access ? "true" : "false");
 254 }
 255 #endif // PRODUCT
 256 //------------------------------------------------------------------------------------------------------------------------
 257 // Klass resolution
 258 
 259 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
 260   Reflection::VerifyClassAccessResults vca_result =
 261     Reflection::verify_class_access(ref_klass(), sel_klass(), true);
 262   if (vca_result != Reflection::ACCESS_OK) {
 263     ResourceMark rm(THREAD);
 264     char* msg = Reflection::verify_class_access_msg(ref_klass(), sel_klass(), vca_result);
 265     if (msg == NULL) {
 266       Exceptions::fthrow(
 267         THREAD_AND_LOCATION,
 268         vmSymbols::java_lang_IllegalAccessError(),
 269         "failed to access class %s from class %s",
 270         sel_klass->external_name(),
 271         ref_klass->external_name());
 272     } else {
 273       // Use module specific message returned by verify_class_access_msg().
 274       Exceptions::fthrow(
 275         THREAD_AND_LOCATION,
 276         vmSymbols::java_lang_IllegalAccessError(),
 277         "%s", msg);
 278     }
 279   }
 280 }
 281 
 282 //------------------------------------------------------------------------------------------------------------------------
 283 // Method resolution
 284 //
 285 // According to JVM spec. $5.4.3c & $5.4.3d
 286 
 287 // Look up method in klasses, including static methods
 288 // Then look up local default methods
 289 methodHandle LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 290                                                     bool checkpolymorphism,
 291                                                     bool in_imethod_resolve, TRAPS) {
 292   KlassHandle klass = link_info.resolved_klass();
 293   Symbol* name = link_info.name();
 294   Symbol* signature = link_info.signature();
 295 
 296   // Ignore overpasses so statics can be found during resolution
 297   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 298 
 299   if (klass->is_array_klass()) {
 300     // Only consider klass and super klass for arrays
 301     return methodHandle(THREAD, result);
 302   }
 303 
 304   InstanceKlass* ik = InstanceKlass::cast(klass());
 305 
 306   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 307   // ignore static and non-public methods of java.lang.Object,
 308   // like clone, finalize, registerNatives.
 309   if (in_imethod_resolve &&
 310       result != NULL &&
 311       ik->is_interface() &&
 312       (result->is_static() || !result->is_public()) &&
 313       result->method_holder() == SystemDictionary::Object_klass()) {
 314     result = NULL;
 315   }
 316 
 317   // Before considering default methods, check for an overpass in the
 318   // current class if a method has not been found.
 319   if (result == NULL) {
 320     result = ik->find_method(name, signature);
 321   }
 322 
 323   if (result == NULL) {
 324     Array<Method*>* default_methods = ik->default_methods();
 325     if (default_methods != NULL) {
 326       result = InstanceKlass::find_method(default_methods, name, signature);
 327     }
 328   }
 329 
 330   if (checkpolymorphism && result != NULL) {
 331     vmIntrinsics::ID iid = result->intrinsic_id();
 332     if (MethodHandles::is_signature_polymorphic(iid)) {
 333       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 334       return NULL;
 335     }
 336   }
 337   return methodHandle(THREAD, result);
 338 }
 339 
 340 // returns first instance method
 341 // Looks up method in classes, then looks up local default methods
 342 methodHandle LinkResolver::lookup_instance_method_in_klasses(KlassHandle klass,
 343                                                              Symbol* name,
 344                                                              Symbol* signature, TRAPS) {
 345   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 346 
 347   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 348     Klass* super_klass = result->method_holder()->super();
 349     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 350   }
 351 
 352   if (klass->is_array_klass()) {
 353     // Only consider klass and super klass for arrays
 354     return methodHandle(THREAD, result);
 355   }
 356 
 357   if (result == NULL) {
 358     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
 359     if (default_methods != NULL) {
 360       result = InstanceKlass::find_method(default_methods, name, signature);
 361       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 362     }
 363   }
 364   return methodHandle(THREAD, result);
 365 }
 366 
 367 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
 368                                                    const methodHandle& resolved_method) {
 369 
 370   int vtable_index = Method::invalid_vtable_index;
 371   Symbol* name = resolved_method->name();
 372   Symbol* signature = resolved_method->signature();
 373   InstanceKlass* ik = InstanceKlass::cast(klass());
 374 
 375   // First check in default method array
 376   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 377     int index = InstanceKlass::find_method_index(ik->default_methods(),
 378                                                  name, signature, Klass::find_overpass,
 379                                                  Klass::find_static, Klass::find_private);
 380     if (index >= 0 ) {
 381       vtable_index = ik->default_vtable_indices()->at(index);
 382     }
 383   }
 384   if (vtable_index == Method::invalid_vtable_index) {
 385     // get vtable_index for miranda methods
 386     ResourceMark rm;
 387     klassVtable *vt = ik->vtable();
 388     vtable_index = vt->index_of_miranda(name, signature);
 389   }
 390   return vtable_index;
 391 }
 392 
 393 methodHandle LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info, TRAPS) {
 394   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass()());
 395 
 396   // Specify 'true' in order to skip default methods when searching the
 397   // interfaces.  Function lookup_method_in_klasses() already looked for
 398   // the method in the default methods table.
 399   return methodHandle(THREAD,
 400     ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(),
 401     Klass::skip_defaults));
 402 }
 403 
 404 methodHandle LinkResolver::lookup_polymorphic_method(
 405                                              const LinkInfo& link_info,
 406                                              Handle *appendix_result_or_null,
 407                                              Handle *method_type_result,
 408                                              TRAPS) {
 409   KlassHandle klass = link_info.resolved_klass();
 410   Symbol* name = link_info.name();
 411   Symbol* full_signature = link_info.signature();
 412 
 413   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 414   if (TraceMethodHandles) {
 415     ResourceMark rm(THREAD);
 416     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 417                   vmIntrinsics::name_at(iid), klass->external_name(),
 418                   name->as_C_string(), full_signature->as_C_string());
 419   }
 420   if ((klass() == SystemDictionary::MethodHandle_klass() ||
 421        klass() == SystemDictionary::VarHandle_klass()) &&
 422       iid != vmIntrinsics::_none) {
 423     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 424       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 425       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 426       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 427       TempNewSymbol basic_signature =
 428         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
 429       if (TraceMethodHandles) {
 430         ResourceMark rm(THREAD);
 431         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
 432                       name->as_C_string(),
 433                       full_signature->as_C_string(),
 434                       basic_signature->as_C_string());
 435       }
 436       methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid,
 437                                                               basic_signature,
 438                                                               CHECK_NULL);
 439       if (result.not_null()) {
 440         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 441         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
 442         assert(basic_signature == result->signature(), "predict the result signature");
 443         if (TraceMethodHandles) {
 444           ttyLocker ttyl;
 445           tty->print("lookup_polymorphic_method => intrinsic ");
 446           result->print_on(tty);
 447         }
 448       }
 449       return result;
 450     } else if (iid == vmIntrinsics::_invokeGeneric
 451                && THREAD->can_call_java()
 452                && appendix_result_or_null != NULL) {
 453       // This is a method with type-checking semantics.
 454       // We will ask Java code to spin an adapter method for it.
 455       if (!MethodHandles::enabled()) {
 456         // Make sure the Java part of the runtime has been booted up.
 457         Klass* natives = SystemDictionary::MethodHandleNatives_klass();
 458         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
 459           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
 460                                             Handle(),
 461                                             Handle(),
 462                                             true,
 463                                             CHECK_NULL);
 464         }
 465       }
 466 
 467       Handle appendix;
 468       Handle method_type;
 469       methodHandle result = SystemDictionary::find_method_handle_invoker(
 470                                                             klass,
 471                                                             name,
 472                                                             full_signature,
 473                                                             link_info.current_klass(),
 474                                                             &appendix,
 475                                                             &method_type,
 476                                                             CHECK_NULL);
 477       if (TraceMethodHandles) {
 478         ttyLocker ttyl;
 479         tty->print("lookup_polymorphic_method => (via Java) ");
 480         result->print_on(tty);
 481         tty->print("  lookup_polymorphic_method => appendix = ");
 482         if (appendix.is_null())  tty->print_cr("(none)");
 483         else                     appendix->print_on(tty);
 484       }
 485       if (result.not_null()) {
 486 #ifdef ASSERT
 487         ResourceMark rm(THREAD);
 488 
 489         TempNewSymbol basic_signature =
 490           MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL);
 491         int actual_size_of_params = result->size_of_parameters();
 492         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
 493         // +1 for MethodHandle.this, +1 for trailing MethodType
 494         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
 495         if (appendix.not_null())                                   expected_size_of_params += 1;
 496         if (actual_size_of_params != expected_size_of_params) {
 497           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 498           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 499           result->print();
 500         }
 501         assert(actual_size_of_params == expected_size_of_params,
 502                "%d != %d", actual_size_of_params, expected_size_of_params);
 503 #endif //ASSERT
 504 
 505         assert(appendix_result_or_null != NULL, "");
 506         (*appendix_result_or_null) = appendix;
 507         (*method_type_result)      = method_type;
 508       }
 509       return result;
 510     }
 511   }
 512   return NULL;
 513 }
 514 
 515 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
 516                                               KlassHandle resolved_klass,
 517                                               KlassHandle sel_klass,
 518                                               const methodHandle& sel_method,
 519                                               TRAPS) {
 520 
 521   AccessFlags flags = sel_method->access_flags();
 522 
 523   // Special case:  arrays always override "clone". JVMS 2.15.
 524   // If the resolved klass is an array class, and the declaring class
 525   // is java.lang.Object and the method is "clone", set the flags
 526   // to public.
 527   //
 528   // We'll check for the method name first, as that's most likely
 529   // to be false (so we'll short-circuit out of these tests).
 530   if (sel_method->name() == vmSymbols::clone_name() &&
 531       sel_klass() == SystemDictionary::Object_klass() &&
 532       resolved_klass->is_array_klass()) {
 533     // We need to change "protected" to "public".
 534     assert(flags.is_protected(), "clone not protected?");
 535     jint new_flags = flags.as_int();
 536     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 537     new_flags = new_flags | JVM_ACC_PUBLIC;
 538     flags.set_flags(new_flags);
 539   }
 540 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 541 
 542   if (!Reflection::verify_field_access(ref_klass(),
 543                                        resolved_klass(),
 544                                        sel_klass(),
 545                                        flags,
 546                                        true)) {
 547     ResourceMark rm(THREAD);
 548     Exceptions::fthrow(
 549       THREAD_AND_LOCATION,
 550       vmSymbols::java_lang_IllegalAccessError(),
 551       "tried to access method %s.%s%s from class %s",
 552       sel_klass->external_name(),
 553       sel_method->name()->as_C_string(),
 554       sel_method->signature()->as_C_string(),
 555       ref_klass->external_name()
 556     );
 557     return;
 558   }
 559 }
 560 
 561 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 562                                                      const constantPoolHandle& pool, int index, TRAPS) {
 563   // This method is used only
 564   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 565   // and
 566   // (2) in Bytecode_invoke::static_target
 567   // It appears to fail when applied to an invokeinterface call site.
 568   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 569   // resolve klass
 570   KlassHandle resolved_klass;
 571   if (code == Bytecodes::_invokedynamic) {
 572     resolved_klass = SystemDictionary::MethodHandle_klass();
 573     Symbol* method_name = vmSymbols::invoke_name();
 574     Symbol* method_signature = pool->signature_ref_at(index);
 575     KlassHandle  current_klass(THREAD, pool->pool_holder());
 576     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass, NULL);
 577     return resolve_method(link_info, /*require_methodref*/false, THREAD);
 578   }
 579 
 580   LinkInfo link_info(pool, index, NULL, CHECK_NULL);
 581   resolved_klass = link_info.resolved_klass();
 582 
 583   if (pool->has_preresolution()
 584       || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
 585           MethodHandles::is_signature_polymorphic_name(resolved_klass(), link_info.name()))) {
 586     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 587     if (result != NULL) {
 588       return methodHandle(THREAD, result);
 589     }
 590   }
 591 
 592   if (code == Bytecodes::_invokeinterface) {
 593     return resolve_interface_method(link_info, code, THREAD);
 594   } else if (code == Bytecodes::_invokevirtual) {
 595     return resolve_method(link_info, /*require_methodref*/true, THREAD);
 596   } else if (!resolved_klass->is_interface()) {
 597     return resolve_method(link_info, /*require_methodref*/false, THREAD);
 598   } else {
 599     return resolve_interface_method(link_info, code, THREAD);
 600   }
 601 }
 602 
 603 // Check and print a loader constraint violation message for method or interface method
 604 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 605                                                    const methodHandle& resolved_method,
 606                                                    const char* method_type, TRAPS) {
 607   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 608   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 609 
 610   ResourceMark rm(THREAD);
 611   Symbol* failed_type_symbol =
 612     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 613                                               resolved_loader, true, CHECK);
 614   if (failed_type_symbol != NULL) {
 615     const char* msg = "loader constraint violation: when resolving %s"
 616       " \"%s\" the class loader (instance of %s) of the current class, %s,"
 617       " and the class loader (instance of %s) for the method's defining class, %s, have"
 618       " different Class objects for the type %s used in the signature";
 619     char* sig = link_info.method_string();
 620     const char* loader1_name = SystemDictionary::loader_name(current_loader());
 621     char* current = link_info.current_klass()->name()->as_C_string();
 622     const char* loader2_name = SystemDictionary::loader_name(resolved_loader());
 623     char* target = resolved_method->method_holder()->name()->as_C_string();
 624     char* failed_type_name = failed_type_symbol->as_C_string();
 625     size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) +
 626       strlen(current) + strlen(loader2_name) + strlen(target) +
 627       strlen(failed_type_name) + strlen(method_type) + 1;
 628     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 629     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
 630                  target, failed_type_name);
 631     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 632   }
 633 }
 634 
 635 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 636                                                   KlassHandle current_klass,
 637                                                   KlassHandle sel_klass, TRAPS) {
 638   Handle ref_loader(THREAD, current_klass->class_loader());
 639   Handle sel_loader(THREAD, sel_klass->class_loader());
 640 
 641   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 642   Symbol* failed_type_symbol =
 643     SystemDictionary::check_signature_loaders(sig,
 644                                               ref_loader, sel_loader,
 645                                               false,
 646                                               CHECK);
 647   if (failed_type_symbol != NULL) {
 648     const char* msg = "loader constraint violation: when resolving field"
 649       " \"%s\" the class loader (instance of %s) of the referring class, "
 650       "%s, and the class loader (instance of %s) for the field's resolved "
 651       "type, %s, have different Class objects for that type";
 652     char* field_name = field->as_C_string();
 653     const char* loader1_name = SystemDictionary::loader_name(ref_loader());
 654     char* sel = sel_klass->name()->as_C_string();
 655     const char* loader2_name = SystemDictionary::loader_name(sel_loader());
 656     char* failed_type_name = failed_type_symbol->as_C_string();
 657     size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) +
 658                     strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1;
 659     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 660     jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name,
 661                      failed_type_name);
 662     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 663   }
 664 }
 665 
 666 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 667                                           bool require_methodref, TRAPS) {
 668 
 669   Handle nested_exception;
 670   KlassHandle resolved_klass = link_info.resolved_klass();
 671 
 672   // 1. check if methodref required, that resolved_klass is not interfacemethodref
 673   if (require_methodref && resolved_klass->is_interface()) {
 674     ResourceMark rm(THREAD);
 675     char buf[200];
 676     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 677         resolved_klass()->external_name());
 678     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 679   }
 680 
 681   // 2. lookup method in resolved klass and its super klasses
 682   methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL);
 683 
 684   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 685     // 3. lookup method in all the interfaces implemented by the resolved klass
 686     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 687 
 688     if (resolved_method.is_null()) {
 689       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 690       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 691       if (HAS_PENDING_EXCEPTION) {
 692         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 693         CLEAR_PENDING_EXCEPTION;
 694       }
 695     }
 696   }
 697 
 698   if (resolved_method.is_null()) {
 699     // 4. method lookup failed
 700     ResourceMark rm(THREAD);
 701     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 702                     Method::name_and_sig_as_C_string(resolved_klass(),
 703                                                      link_info.name(),
 704                                                      link_info.signature()),
 705                     nested_exception, NULL);
 706   }
 707 
 708   // 5. access checks, access checking may be turned off when calling from within the VM.
 709   KlassHandle current_klass = link_info.current_klass();
 710   if (link_info.check_access()) {
 711     assert(current_klass.not_null() , "current_klass should not be null");
 712 
 713     // check if method can be accessed by the referring class
 714     check_method_accessability(current_klass,
 715                                resolved_klass,
 716                                KlassHandle(THREAD, resolved_method->method_holder()),
 717                                resolved_method,
 718                                CHECK_NULL);
 719 
 720     // check loader constraints
 721     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 722   }
 723 
 724   return resolved_method;
 725 }
 726 
 727 static void trace_method_resolution(const char* prefix,
 728                                     KlassHandle klass,
 729                                     KlassHandle resolved_klass,
 730                                     const methodHandle& method,
 731                                     bool logitables,
 732                                     int index = -1) {
 733 #ifndef PRODUCT
 734   ResourceMark rm;
 735   outputStream* st;
 736   if (logitables) {
 737     st = Log(itables)::trace_stream();
 738   } else {
 739     st = Log(vtables)::trace_stream();
 740   }
 741   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 742             prefix,
 743             (klass.is_null() ? "<NULL>" : klass->internal_name()),
 744             (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
 745             Method::name_and_sig_as_C_string(resolved_klass(),
 746                                              method->name(),
 747                                              method->signature()),
 748             method->method_holder()->internal_name());
 749   method->print_linkage_flags(st);
 750   if (index != -1) {
 751     st->print("vtable_index:%d", index);
 752   }
 753   st->cr();
 754 #endif // PRODUCT
 755 }
 756 
 757 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 758 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 759 
 760   KlassHandle resolved_klass = link_info.resolved_klass();
 761 
 762   // check if klass is interface
 763   if (!resolved_klass->is_interface()) {
 764     ResourceMark rm(THREAD);
 765     char buf[200];
 766     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
 767     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 768   }
 769 
 770   // lookup method in this interface or its super, java.lang.Object
 771   // JDK8: also look for static methods
 772   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
 773 
 774   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 775     // lookup method in all the super-interfaces
 776     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 777   }
 778 
 779   if (resolved_method.is_null()) {
 780     // no method found
 781     ResourceMark rm(THREAD);
 782     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 783                    Method::name_and_sig_as_C_string(resolved_klass(),
 784                                                     link_info.name(),
 785                                                     link_info.signature()));
 786   }
 787 
 788   if (link_info.check_access()) {
 789     // JDK8 adds non-public interface methods, and accessability check requirement
 790     KlassHandle current_klass = link_info.current_klass();
 791 
 792     assert(current_klass.not_null() , "current_klass should not be null");
 793 
 794     // check if method can be accessed by the referring class
 795     check_method_accessability(current_klass,
 796                                resolved_klass,
 797                                KlassHandle(THREAD, resolved_method->method_holder()),
 798                                resolved_method,
 799                                CHECK_NULL);
 800 
 801     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 802   }
 803 
 804   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 805     ResourceMark rm(THREAD);
 806     char buf[200];
 807     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 808                  Method::name_and_sig_as_C_string(resolved_klass(),
 809                  resolved_method->name(), resolved_method->signature()));
 810     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 811   }
 812 
 813   if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) {
 814     ResourceMark rm(THREAD);
 815     char buf[200];
 816 
 817     KlassHandle current_klass = link_info.current_klass();
 818     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s",
 819                  Method::name_and_sig_as_C_string(resolved_klass(),
 820                                                   resolved_method->name(),
 821                                                   resolved_method->signature()),
 822                                                   (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
 823      THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 824   }
 825 
 826   if (log_develop_is_enabled(Trace, itables)) {
 827     trace_method_resolution("invokeinterface resolved method: caller-class",
 828                             link_info.current_klass(), resolved_klass,
 829                             resolved_method, true);
 830   }
 831 
 832   return resolved_method;
 833 }
 834 
 835 //------------------------------------------------------------------------------------------------------------------------
 836 // Field resolution
 837 
 838 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
 839                                              KlassHandle resolved_klass,
 840                                              KlassHandle sel_klass,
 841                                              const fieldDescriptor& fd,
 842                                              TRAPS) {
 843   if (!Reflection::verify_field_access(ref_klass(),
 844                                        resolved_klass(),
 845                                        sel_klass(),
 846                                        fd.access_flags(),
 847                                        true)) {
 848     ResourceMark rm(THREAD);
 849     Exceptions::fthrow(
 850       THREAD_AND_LOCATION,
 851       vmSymbols::java_lang_IllegalAccessError(),
 852       "tried to access field %s.%s from class %s",
 853       sel_klass->external_name(),
 854       fd.name()->as_C_string(),
 855       ref_klass->external_name()
 856     );
 857     return;
 858   }
 859 }
 860 
 861 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 862   LinkInfo link_info(pool, index, method, CHECK);
 863   resolve_field(fd, link_info, byte, true, CHECK);
 864 }
 865 
 866 void LinkResolver::resolve_field(fieldDescriptor& fd,
 867                                  const LinkInfo& link_info,
 868                                  Bytecodes::Code byte, bool initialize_class,
 869                                  TRAPS) {
 870   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 871          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 872          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 873          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 874 
 875   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 876   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 877   // Check if there's a resolved klass containing the field
 878   KlassHandle resolved_klass = link_info.resolved_klass();
 879   Symbol* field = link_info.name();
 880   Symbol* sig = link_info.signature();
 881 
 882   if (resolved_klass.is_null()) {
 883     ResourceMark rm(THREAD);
 884     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 885   }
 886 
 887   // Resolve instance field
 888   KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd));
 889   // check if field exists; i.e., if a klass containing the field def has been selected
 890   if (sel_klass.is_null()) {
 891     ResourceMark rm(THREAD);
 892     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 893   }
 894 
 895   if (!link_info.check_access())
 896     // Access checking may be turned off when calling from within the VM.
 897     return;
 898 
 899   // check access
 900   KlassHandle current_klass = link_info.current_klass();
 901   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 902 
 903   // check for errors
 904   if (is_static != fd.is_static()) {
 905     ResourceMark rm(THREAD);
 906     char msg[200];
 907     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
 908     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 909   }
 910 
 911   // A final field can be modified only
 912   // (1) by methods declared in the class declaring the field and
 913   // (2) by the <clinit> method (in case of a static field)
 914   //     or by the <init> method (in case of an instance field).
 915   if (is_put && fd.access_flags().is_final()) {
 916     methodHandle m = link_info.current_method();
 917     assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
 918     Symbol* method_name = m->name();
 919     if (sel_klass() != current_klass() ||
 920         (byte == Bytecodes::_putstatic && fd.is_static() && method_name != vmSymbols::class_initializer_name()) ||
 921         ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) && !fd.is_static() && method_name != vmSymbols::object_initializer_name())
 922       ) {
 923       THROW(vmSymbols::java_lang_IllegalAccessError());
 924     }
 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(), NULL, 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, Bytecodes::_invokestatic, 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, Bytecodes::_invokespecial, 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, Bytecodes::_invokeinterface, 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 receiver klass implements the resolved interface
1326   if (!recv_klass->is_subtype_of(resolved_klass())) {
1327     ResourceMark rm(THREAD);
1328     char buf[200];
1329     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1330                  recv_klass()->external_name(),
1331                  resolved_klass()->external_name());
1332     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1333   }
1334 
1335   // do lookup based on receiver klass
1336   // This search must match the linktime preparation search for itable initialization
1337   // to correctly enforce loader constraints for interface method inheritance
1338   methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass,
1339                                                   resolved_method->name(),
1340                                                   resolved_method->signature(), CHECK);
1341   if (sel_method.is_null() && !check_null_and_abstract) {
1342     // In theory this is a harmless placeholder value, but
1343     // in practice leaving in null affects the nsk default method tests.
1344     // This needs further study.
1345     sel_method = resolved_method;
1346   }
1347   // check if method exists
1348   if (sel_method.is_null()) {
1349     ResourceMark rm(THREAD);
1350     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1351                    Method::name_and_sig_as_C_string(recv_klass(),
1352                                                     resolved_method->name(),
1353                                                     resolved_method->signature()));
1354   }
1355   // check access
1356   // Throw Illegal Access Error if sel_method is not public.
1357   if (!sel_method->is_public()) {
1358     ResourceMark rm(THREAD);
1359     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1360               Method::name_and_sig_as_C_string(recv_klass(),
1361                                                sel_method->name(),
1362                                                sel_method->signature()));
1363   }
1364   // check if abstract
1365   if (check_null_and_abstract && sel_method->is_abstract()) {
1366     ResourceMark rm(THREAD);
1367     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1368               Method::name_and_sig_as_C_string(recv_klass(),
1369                                                       sel_method->name(),
1370                                                       sel_method->signature()));
1371   }
1372 
1373   if (log_develop_is_enabled(Trace, itables)) {
1374     trace_method_resolution("invokeinterface selected method: receiver-class",
1375                             recv_klass, resolved_klass, sel_method, true);
1376   }
1377   // setup result
1378   if (!resolved_method->has_itable_index()) {
1379     int vtable_index = resolved_method->vtable_index();
1380     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1381     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1382   } else {
1383     int itable_index = resolved_method()->itable_index();
1384     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1385   }
1386 }
1387 
1388 
1389 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1390                                                  const LinkInfo& link_info) {
1391   EXCEPTION_MARK;
1392   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1393   if (HAS_PENDING_EXCEPTION) {
1394     CLEAR_PENDING_EXCEPTION;
1395     return methodHandle();
1396   } else {
1397     return method_result;
1398   }
1399 }
1400 
1401 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1402                                                  const LinkInfo& link_info) {
1403   EXCEPTION_MARK;
1404   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1405   if (HAS_PENDING_EXCEPTION) {
1406     CLEAR_PENDING_EXCEPTION;
1407     return methodHandle();
1408   } else {
1409     return method_result;
1410   }
1411 }
1412 
1413 methodHandle LinkResolver::resolve_virtual_call_or_null(
1414                                                  KlassHandle receiver_klass,
1415                                                  const LinkInfo& link_info) {
1416   EXCEPTION_MARK;
1417   CallInfo info;
1418   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1419   if (HAS_PENDING_EXCEPTION) {
1420     CLEAR_PENDING_EXCEPTION;
1421     return methodHandle();
1422   }
1423   return info.selected_method();
1424 }
1425 
1426 methodHandle LinkResolver::resolve_interface_call_or_null(
1427                                                  KlassHandle receiver_klass,
1428                                                  const LinkInfo& link_info) {
1429   EXCEPTION_MARK;
1430   CallInfo info;
1431   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1432   if (HAS_PENDING_EXCEPTION) {
1433     CLEAR_PENDING_EXCEPTION;
1434     return methodHandle();
1435   }
1436   return info.selected_method();
1437 }
1438 
1439 int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass,
1440                                                const LinkInfo& link_info) {
1441   EXCEPTION_MARK;
1442   CallInfo info;
1443   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1444                        /*check_null_or_abstract*/false, THREAD);
1445   if (HAS_PENDING_EXCEPTION) {
1446     CLEAR_PENDING_EXCEPTION;
1447     return Method::invalid_vtable_index;
1448   }
1449   return info.vtable_index();
1450 }
1451 
1452 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1453   EXCEPTION_MARK;
1454   CallInfo info;
1455   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1456   if (HAS_PENDING_EXCEPTION) {
1457     CLEAR_PENDING_EXCEPTION;
1458     return methodHandle();
1459   }
1460   return info.selected_method();
1461 }
1462 
1463 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1464   EXCEPTION_MARK;
1465   CallInfo info;
1466   resolve_special_call(info, link_info, THREAD);
1467   if (HAS_PENDING_EXCEPTION) {
1468     CLEAR_PENDING_EXCEPTION;
1469     return methodHandle();
1470   }
1471   return info.selected_method();
1472 }
1473 
1474 
1475 
1476 //------------------------------------------------------------------------------------------------------------------------
1477 // ConstantPool entries
1478 
1479 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1480   switch (byte) {
1481     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1482     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1483     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1484     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1485     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1486     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1487   }
1488   return;
1489 }
1490 
1491 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1492                              const methodHandle& attached_method,
1493                              Bytecodes::Code byte, TRAPS) {
1494   KlassHandle defc = attached_method->method_holder();
1495   Symbol* name = attached_method->name();
1496   Symbol* type = attached_method->signature();
1497   LinkInfo link_info(defc, name, type, KlassHandle(), NULL, /*check_access=*/false);
1498   switch(byte) {
1499     case Bytecodes::_invokevirtual:
1500       resolve_virtual_call(result, recv, recv->klass(), link_info,
1501                            /*check_null_and_abstract=*/true, CHECK);
1502       break;
1503     case Bytecodes::_invokeinterface:
1504       resolve_interface_call(result, recv, recv->klass(), link_info,
1505                              /*check_null_and_abstract=*/true, CHECK);
1506       break;
1507     case Bytecodes::_invokestatic:
1508       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1509       break;
1510     case Bytecodes::_invokespecial:
1511       resolve_special_call(result, link_info, CHECK);
1512       break;
1513     default:
1514       fatal("bad call: %s", Bytecodes::name(byte));
1515   }
1516 }
1517 
1518 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1519   LinkInfo link_info(pool, index, NULL, CHECK);
1520   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1521 }
1522 
1523 
1524 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1525   LinkInfo link_info(pool, index, NULL, CHECK);
1526   resolve_special_call(result, link_info, CHECK);
1527 }
1528 
1529 
1530 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1531                                           const constantPoolHandle& pool, int index,
1532                                           TRAPS) {
1533 
1534   LinkInfo link_info(pool, index, NULL, CHECK);
1535   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1536   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1537 }
1538 
1539 
1540 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1541   LinkInfo link_info(pool, index, NULL, CHECK);
1542   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1543   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1544 }
1545 
1546 
1547 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1548   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1549   LinkInfo link_info(pool, index, NULL, CHECK);
1550   if (TraceMethodHandles) {
1551     ResourceMark rm(THREAD);
1552     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1553                   link_info.signature()->as_C_string());
1554   }
1555   resolve_handle_call(result, link_info, CHECK);
1556 }
1557 
1558 void LinkResolver::resolve_handle_call(CallInfo& result,
1559                                        const LinkInfo& link_info,
1560                                        TRAPS) {
1561   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1562   KlassHandle resolved_klass = link_info.resolved_klass();
1563   assert(resolved_klass() == SystemDictionary::MethodHandle_klass() ||
1564          resolved_klass() == SystemDictionary::VarHandle_klass(), "");
1565   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1566   Handle       resolved_appendix;
1567   Handle       resolved_method_type;
1568   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1569                                        &resolved_appendix, &resolved_method_type, CHECK);
1570   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1571 }
1572 
1573 static void wrap_invokedynamic_exception(TRAPS) {
1574   if (HAS_PENDING_EXCEPTION) {
1575     if (TraceMethodHandles) {
1576       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1577       PENDING_EXCEPTION->print();
1578     }
1579     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1580       // throw these guys, since they are already wrapped
1581       return;
1582     }
1583     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1584       // intercept only LinkageErrors which might have failed to wrap
1585       return;
1586     }
1587     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1588     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1589     CLEAR_PENDING_EXCEPTION;
1590     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1591   }
1592 }
1593 
1594 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1595   Symbol* method_name       = pool->name_ref_at(index);
1596   Symbol* method_signature  = pool->signature_ref_at(index);
1597   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1598 
1599   // Resolve the bootstrap specifier (BSM + optional arguments).
1600   Handle bootstrap_specifier;
1601   // Check if CallSite has been bound already:
1602   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1603   if (cpce->is_f1_null()) {
1604     int pool_index = cpce->constant_pool_index();
1605     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1606     wrap_invokedynamic_exception(CHECK);
1607     assert(bsm_info != NULL, "");
1608     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1609     bootstrap_specifier = Handle(THREAD, bsm_info);
1610   }
1611   if (!cpce->is_f1_null()) {
1612     methodHandle method(     THREAD, cpce->f1_as_method());
1613     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1614     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1615     result.set_handle(method, appendix, method_type, THREAD);
1616     wrap_invokedynamic_exception(CHECK);
1617     return;
1618   }
1619 
1620   if (TraceMethodHandles) {
1621     ResourceMark rm(THREAD);
1622     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1623                   ConstantPool::decode_invokedynamic_index(index),
1624                   method_name->as_C_string(), method_signature->as_C_string(),
1625                   current_klass->name()->as_C_string());
1626     tty->print("  BSM info: "); bootstrap_specifier->print();
1627   }
1628 
1629   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1630 }
1631 
1632 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1633                                         Handle bootstrap_specifier,
1634                                         Symbol* method_name, Symbol* method_signature,
1635                                         KlassHandle current_klass,
1636                                         TRAPS) {
1637   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1638   // The appendix argument is likely to be a freshly-created CallSite.
1639   Handle       resolved_appendix;
1640   Handle       resolved_method_type;
1641   methodHandle resolved_method =
1642     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1643                                                      bootstrap_specifier,
1644                                                      method_name, method_signature,
1645                                                      &resolved_appendix,
1646                                                      &resolved_method_type,
1647                                                      THREAD);
1648   wrap_invokedynamic_exception(CHECK);
1649   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1650   wrap_invokedynamic_exception(CHECK);
1651 }