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   _tag           = pool->tag_ref_at(index);
 235   _current_klass = KlassHandle(THREAD, pool->pool_holder());
 236   _current_method = current_method;
 237 
 238   // Coming from the constant pool always checks access
 239   _check_access  = true;
 240 }
 241 
 242 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 243    // resolve klass
 244   Klass* result = pool->klass_ref_at(index, CHECK);
 245   _resolved_klass = KlassHandle(THREAD, result);
 246 
 247   // Get name, signature, and static klass
 248   _name          = pool->name_ref_at(index);
 249   _signature     = pool->signature_ref_at(index);
 250   _tag           = pool->tag_ref_at(index);
 251   _current_klass = KlassHandle(THREAD, pool->pool_holder());
 252   _current_method = methodHandle();
 253 
 254   // Coming from the constant pool always checks access
 255   _check_access  = true;
 256 }
 257 
 258 char* LinkInfo::method_string() const {
 259   return Method::name_and_sig_as_C_string(_resolved_klass(), _name, _signature);
 260 }
 261 
 262 #ifndef PRODUCT
 263 void LinkInfo::print() {
 264   ResourceMark rm;
 265   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 266                 _resolved_klass->name()->as_C_string(),
 267                 _name->as_C_string(),
 268                 _signature->as_C_string(),
 269                 _current_klass.is_null() ? "(none)" : _current_klass->name()->as_C_string(),
 270                 _check_access ? "true" : "false");
 271 }
 272 #endif // PRODUCT
 273 //------------------------------------------------------------------------------------------------------------------------
 274 // Klass resolution
 275 
 276 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
 277   Reflection::VerifyClassAccessResults vca_result =
 278     Reflection::verify_class_access(ref_klass(), sel_klass(), true);
 279   if (vca_result != Reflection::ACCESS_OK) {
 280     ResourceMark rm(THREAD);
 281     char* msg = Reflection::verify_class_access_msg(ref_klass(), sel_klass(), vca_result);
 282     if (msg == NULL) {
 283       Exceptions::fthrow(
 284         THREAD_AND_LOCATION,
 285         vmSymbols::java_lang_IllegalAccessError(),
 286         "failed to access class %s from class %s",
 287         sel_klass->external_name(),
 288         ref_klass->external_name());
 289     } else {
 290       // Use module specific message returned by verify_class_access_msg().
 291       Exceptions::fthrow(
 292         THREAD_AND_LOCATION,
 293         vmSymbols::java_lang_IllegalAccessError(),
 294         "%s", msg);
 295     }
 296   }
 297 }
 298 
 299 //------------------------------------------------------------------------------------------------------------------------
 300 // Method resolution
 301 //
 302 // According to JVM spec. $5.4.3c & $5.4.3d
 303 
 304 // Look up method in klasses, including static methods
 305 // Then look up local default methods
 306 methodHandle LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 307                                                     bool checkpolymorphism,
 308                                                     bool in_imethod_resolve, TRAPS) {
 309   KlassHandle klass = link_info.resolved_klass();
 310   Symbol* name = link_info.name();
 311   Symbol* signature = link_info.signature();
 312 
 313   // Ignore overpasses so statics can be found during resolution
 314   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 315 
 316   if (klass->is_array_klass()) {
 317     // Only consider klass and super klass for arrays
 318     return methodHandle(THREAD, result);
 319   }
 320 
 321   InstanceKlass* ik = InstanceKlass::cast(klass());
 322 
 323   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 324   // ignore static and non-public methods of java.lang.Object,
 325   // like clone, finalize, registerNatives.
 326   if (in_imethod_resolve &&
 327       result != NULL &&
 328       ik->is_interface() &&
 329       (result->is_static() || !result->is_public()) &&
 330       result->method_holder() == SystemDictionary::Object_klass()) {
 331     result = NULL;
 332   }
 333 
 334   // Before considering default methods, check for an overpass in the
 335   // current class if a method has not been found.
 336   if (result == NULL) {
 337     result = ik->find_method(name, signature);
 338   }
 339 
 340   if (result == NULL) {
 341     Array<Method*>* default_methods = ik->default_methods();
 342     if (default_methods != NULL) {
 343       result = InstanceKlass::find_method(default_methods, name, signature);
 344     }
 345   }
 346 
 347   if (checkpolymorphism && result != NULL) {
 348     vmIntrinsics::ID iid = result->intrinsic_id();
 349     if (MethodHandles::is_signature_polymorphic(iid)) {
 350       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 351       return NULL;
 352     }
 353   }
 354   return methodHandle(THREAD, result);
 355 }
 356 
 357 // returns first instance method
 358 // Looks up method in classes, then looks up local default methods
 359 methodHandle LinkResolver::lookup_instance_method_in_klasses(KlassHandle klass,
 360                                                              Symbol* name,
 361                                                              Symbol* signature, TRAPS) {
 362   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 363 
 364   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 365     Klass* super_klass = result->method_holder()->super();
 366     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 367   }
 368 
 369   if (klass->is_array_klass()) {
 370     // Only consider klass and super klass for arrays
 371     return methodHandle(THREAD, result);
 372   }
 373 
 374   if (result == NULL) {
 375     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
 376     if (default_methods != NULL) {
 377       result = InstanceKlass::find_method(default_methods, name, signature);
 378       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 379     }
 380   }
 381   return methodHandle(THREAD, result);
 382 }
 383 
 384 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
 385                                                    const methodHandle& resolved_method) {
 386 
 387   int vtable_index = Method::invalid_vtable_index;
 388   Symbol* name = resolved_method->name();
 389   Symbol* signature = resolved_method->signature();
 390   InstanceKlass* ik = InstanceKlass::cast(klass());
 391 
 392   // First check in default method array
 393   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 394     int index = InstanceKlass::find_method_index(ik->default_methods(),
 395                                                  name, signature, Klass::find_overpass,
 396                                                  Klass::find_static, Klass::find_private);
 397     if (index >= 0 ) {
 398       vtable_index = ik->default_vtable_indices()->at(index);
 399     }
 400   }
 401   if (vtable_index == Method::invalid_vtable_index) {
 402     // get vtable_index for miranda methods
 403     ResourceMark rm;
 404     klassVtable *vt = ik->vtable();
 405     vtable_index = vt->index_of_miranda(name, signature);
 406   }
 407   return vtable_index;
 408 }
 409 
 410 methodHandle LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info, TRAPS) {
 411   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass()());
 412 
 413   // Specify 'true' in order to skip default methods when searching the
 414   // interfaces.  Function lookup_method_in_klasses() already looked for
 415   // the method in the default methods table.
 416   return methodHandle(THREAD,
 417     ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(),
 418     Klass::skip_defaults));
 419 }
 420 
 421 methodHandle LinkResolver::lookup_polymorphic_method(
 422                                              const LinkInfo& link_info,
 423                                              Handle *appendix_result_or_null,
 424                                              Handle *method_type_result,
 425                                              TRAPS) {
 426   KlassHandle klass = link_info.resolved_klass();
 427   Symbol* name = link_info.name();
 428   Symbol* full_signature = link_info.signature();
 429 
 430   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 431   if (TraceMethodHandles) {
 432     ResourceMark rm(THREAD);
 433     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 434                   vmIntrinsics::name_at(iid), klass->external_name(),
 435                   name->as_C_string(), full_signature->as_C_string());
 436   }
 437   if ((klass() == SystemDictionary::MethodHandle_klass() ||
 438        klass() == SystemDictionary::VarHandle_klass()) &&
 439       iid != vmIntrinsics::_none) {
 440     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 441       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 442       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 443       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 444       TempNewSymbol basic_signature =
 445         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
 446       if (TraceMethodHandles) {
 447         ResourceMark rm(THREAD);
 448         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
 449                       name->as_C_string(),
 450                       full_signature->as_C_string(),
 451                       basic_signature->as_C_string());
 452       }
 453       methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid,
 454                                                               basic_signature,
 455                                                               CHECK_NULL);
 456       if (result.not_null()) {
 457         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 458         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
 459         assert(basic_signature == result->signature(), "predict the result signature");
 460         if (TraceMethodHandles) {
 461           ttyLocker ttyl;
 462           tty->print("lookup_polymorphic_method => intrinsic ");
 463           result->print_on(tty);
 464         }
 465       }
 466       return result;
 467     } else if (iid == vmIntrinsics::_invokeGeneric
 468                && THREAD->can_call_java()
 469                && appendix_result_or_null != NULL) {
 470       // This is a method with type-checking semantics.
 471       // We will ask Java code to spin an adapter method for it.
 472       if (!MethodHandles::enabled()) {
 473         // Make sure the Java part of the runtime has been booted up.
 474         Klass* natives = SystemDictionary::MethodHandleNatives_klass();
 475         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
 476           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
 477                                             Handle(),
 478                                             Handle(),
 479                                             true,
 480                                             CHECK_NULL);
 481         }
 482       }
 483 
 484       Handle appendix;
 485       Handle method_type;
 486       methodHandle result = SystemDictionary::find_method_handle_invoker(
 487                                                             klass,
 488                                                             name,
 489                                                             full_signature,
 490                                                             link_info.current_klass(),
 491                                                             &appendix,
 492                                                             &method_type,
 493                                                             CHECK_NULL);
 494       if (TraceMethodHandles) {
 495         ttyLocker ttyl;
 496         tty->print("lookup_polymorphic_method => (via Java) ");
 497         result->print_on(tty);
 498         tty->print("  lookup_polymorphic_method => appendix = ");
 499         if (appendix.is_null())  tty->print_cr("(none)");
 500         else                     appendix->print_on(tty);
 501       }
 502       if (result.not_null()) {
 503 #ifdef ASSERT
 504         ResourceMark rm(THREAD);
 505 
 506         TempNewSymbol basic_signature =
 507           MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL);
 508         int actual_size_of_params = result->size_of_parameters();
 509         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
 510         // +1 for MethodHandle.this, +1 for trailing MethodType
 511         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
 512         if (appendix.not_null())                                   expected_size_of_params += 1;
 513         if (actual_size_of_params != expected_size_of_params) {
 514           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 515           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 516           result->print();
 517         }
 518         assert(actual_size_of_params == expected_size_of_params,
 519                "%d != %d", actual_size_of_params, expected_size_of_params);
 520 #endif //ASSERT
 521 
 522         assert(appendix_result_or_null != NULL, "");
 523         (*appendix_result_or_null) = appendix;
 524         (*method_type_result)      = method_type;
 525       }
 526       return result;
 527     }
 528   }
 529   return NULL;
 530 }
 531 
 532 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
 533                                               KlassHandle resolved_klass,
 534                                               KlassHandle sel_klass,
 535                                               const methodHandle& sel_method,
 536                                               TRAPS) {
 537 
 538   AccessFlags flags = sel_method->access_flags();
 539 
 540   // Special case:  arrays always override "clone". JVMS 2.15.
 541   // If the resolved klass is an array class, and the declaring class
 542   // is java.lang.Object and the method is "clone", set the flags
 543   // to public.
 544   //
 545   // We'll check for the method name first, as that's most likely
 546   // to be false (so we'll short-circuit out of these tests).
 547   if (sel_method->name() == vmSymbols::clone_name() &&
 548       sel_klass() == SystemDictionary::Object_klass() &&
 549       resolved_klass->is_array_klass()) {
 550     // We need to change "protected" to "public".
 551     assert(flags.is_protected(), "clone not protected?");
 552     jint new_flags = flags.as_int();
 553     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 554     new_flags = new_flags | JVM_ACC_PUBLIC;
 555     flags.set_flags(new_flags);
 556   }
 557 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 558 
 559   if (!Reflection::verify_field_access(ref_klass(),
 560                                        resolved_klass(),
 561                                        sel_klass(),
 562                                        flags,
 563                                        true)) {
 564     ResourceMark rm(THREAD);
 565     Exceptions::fthrow(
 566       THREAD_AND_LOCATION,
 567       vmSymbols::java_lang_IllegalAccessError(),
 568       "tried to access method %s.%s%s from class %s",
 569       sel_klass->external_name(),
 570       sel_method->name()->as_C_string(),
 571       sel_method->signature()->as_C_string(),
 572       ref_klass->external_name()
 573     );
 574     return;
 575   }
 576 }
 577 
 578 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 579                                                      const constantPoolHandle& pool, int index, TRAPS) {
 580   // This method is used only
 581   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 582   // and
 583   // (2) in Bytecode_invoke::static_target
 584   // It appears to fail when applied to an invokeinterface call site.
 585   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 586   // resolve klass
 587   KlassHandle resolved_klass;
 588   if (code == Bytecodes::_invokedynamic) {
 589     resolved_klass = SystemDictionary::MethodHandle_klass();
 590     Symbol* method_name = vmSymbols::invoke_name();
 591     Symbol* method_signature = pool->signature_ref_at(index);
 592     KlassHandle  current_klass(THREAD, pool->pool_holder());
 593     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 594     return resolve_method(link_info, code, THREAD);
 595   }
 596 
 597   LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
 598   resolved_klass = link_info.resolved_klass();
 599 
 600   if (pool->has_preresolution()
 601       || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
 602           MethodHandles::is_signature_polymorphic_name(resolved_klass(), link_info.name()))) {
 603     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 604     if (result != NULL) {
 605       return methodHandle(THREAD, result);
 606     }
 607   }
 608 
 609   if (code == Bytecodes::_invokeinterface) {
 610     return resolve_interface_method(link_info, code, THREAD);
 611   } else if (code == Bytecodes::_invokevirtual) {
 612     return resolve_method(link_info, code, THREAD);
 613   } else if (!resolved_klass->is_interface()) {
 614     return resolve_method(link_info, code, THREAD);
 615   } else {
 616     return resolve_interface_method(link_info, code, THREAD);
 617   }
 618 }
 619 
 620 // Check and print a loader constraint violation message for method or interface method
 621 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 622                                                    const methodHandle& resolved_method,
 623                                                    const char* method_type, TRAPS) {
 624   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 625   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 626 
 627   ResourceMark rm(THREAD);
 628   Symbol* failed_type_symbol =
 629     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 630                                               resolved_loader, true, CHECK);
 631   if (failed_type_symbol != NULL) {
 632     const char* msg = "loader constraint violation: when resolving %s"
 633       " \"%s\" the class loader (instance of %s) of the current class, %s,"
 634       " and the class loader (instance of %s) for the method's defining class, %s, have"
 635       " different Class objects for the type %s used in the signature";
 636     char* sig = link_info.method_string();
 637     const char* loader1_name = SystemDictionary::loader_name(current_loader());
 638     char* current = link_info.current_klass()->name()->as_C_string();
 639     const char* loader2_name = SystemDictionary::loader_name(resolved_loader());
 640     char* target = resolved_method->method_holder()->name()->as_C_string();
 641     char* failed_type_name = failed_type_symbol->as_C_string();
 642     size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) +
 643       strlen(current) + strlen(loader2_name) + strlen(target) +
 644       strlen(failed_type_name) + strlen(method_type) + 1;
 645     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 646     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
 647                  target, failed_type_name);
 648     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 649   }
 650 }
 651 
 652 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 653                                                   KlassHandle current_klass,
 654                                                   KlassHandle sel_klass, TRAPS) {
 655   Handle ref_loader(THREAD, current_klass->class_loader());
 656   Handle sel_loader(THREAD, sel_klass->class_loader());
 657 
 658   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 659   Symbol* failed_type_symbol =
 660     SystemDictionary::check_signature_loaders(sig,
 661                                               ref_loader, sel_loader,
 662                                               false,
 663                                               CHECK);
 664   if (failed_type_symbol != NULL) {
 665     const char* msg = "loader constraint violation: when resolving field"
 666       " \"%s\" the class loader (instance of %s) of the referring class, "
 667       "%s, and the class loader (instance of %s) for the field's resolved "
 668       "type, %s, have different Class objects for that type";
 669     char* field_name = field->as_C_string();
 670     const char* loader1_name = SystemDictionary::loader_name(ref_loader());
 671     char* sel = sel_klass->name()->as_C_string();
 672     const char* loader2_name = SystemDictionary::loader_name(sel_loader());
 673     char* failed_type_name = failed_type_symbol->as_C_string();
 674     size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) +
 675                     strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1;
 676     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 677     jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name,
 678                      failed_type_name);
 679     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 680   }
 681 }
 682 
 683 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 684                                           Bytecodes::Code code, TRAPS) {
 685 
 686   Handle nested_exception;
 687   KlassHandle resolved_klass = link_info.resolved_klass();
 688 
 689   // 1. For invokevirtual, cannot call an interface method
 690   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 691     ResourceMark rm(THREAD);
 692     char buf[200];
 693     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 694         resolved_klass()->external_name());
 695     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 696   }
 697 
 698   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 699   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 700     ResourceMark rm(THREAD);
 701     char buf[200];
 702     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 703     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 704   }
 705 
 706 
 707   // 3. lookup method in resolved klass and its super klasses
 708   methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL);
 709 
 710   // 4. lookup method in all the interfaces implemented by the resolved klass
 711   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 712     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 713 
 714     if (resolved_method.is_null()) {
 715       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 716       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 717       if (HAS_PENDING_EXCEPTION) {
 718         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 719         CLEAR_PENDING_EXCEPTION;
 720       }
 721     }
 722   }
 723 
 724   // 5. method lookup failed
 725   if (resolved_method.is_null()) {
 726     ResourceMark rm(THREAD);
 727     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 728                     Method::name_and_sig_as_C_string(resolved_klass(),
 729                                                      link_info.name(),
 730                                                      link_info.signature()),
 731                     nested_exception, NULL);
 732   }
 733 
 734   // 6. access checks, access checking may be turned off when calling from within the VM.
 735   KlassHandle current_klass = link_info.current_klass();
 736   if (link_info.check_access()) {
 737     assert(current_klass.not_null() , "current_klass should not be null");
 738 
 739     // check if method can be accessed by the referring class
 740     check_method_accessability(current_klass,
 741                                resolved_klass,
 742                                KlassHandle(THREAD, resolved_method->method_holder()),
 743                                resolved_method,
 744                                CHECK_NULL);
 745 
 746     // check loader constraints
 747     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 748   }
 749 
 750   return resolved_method;
 751 }
 752 
 753 static void trace_method_resolution(const char* prefix,
 754                                     KlassHandle klass,
 755                                     KlassHandle resolved_klass,
 756                                     const methodHandle& method,
 757                                     bool logitables,
 758                                     int index = -1) {
 759 #ifndef PRODUCT
 760   ResourceMark rm;
 761   outputStream* st;
 762   if (logitables) {
 763     st = Log(itables)::trace_stream();
 764   } else {
 765     st = Log(vtables)::trace_stream();
 766   }
 767   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 768             prefix,
 769             (klass.is_null() ? "<NULL>" : klass->internal_name()),
 770             (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
 771             Method::name_and_sig_as_C_string(resolved_klass(),
 772                                              method->name(),
 773                                              method->signature()),
 774             method->method_holder()->internal_name());
 775   method->print_linkage_flags(st);
 776   if (index != -1) {
 777     st->print("vtable_index:%d", index);
 778   }
 779   st->cr();
 780 #endif // PRODUCT
 781 }
 782 
 783 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 784 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 785 
 786   KlassHandle resolved_klass = link_info.resolved_klass();
 787 
 788   // check if klass is interface
 789   if (!resolved_klass->is_interface()) {
 790     ResourceMark rm(THREAD);
 791     char buf[200];
 792     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
 793     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 794   }
 795 
 796   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 797   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 798     ResourceMark rm(THREAD);
 799     char buf[200];
 800     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 801     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 802   }
 803 
 804   // lookup method in this interface or its super, java.lang.Object
 805   // JDK8: also look for static methods
 806   methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL);
 807 
 808   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 809     // lookup method in all the super-interfaces
 810     resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL);
 811   }
 812 
 813   if (resolved_method.is_null()) {
 814     // no method found
 815     ResourceMark rm(THREAD);
 816     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 817                    Method::name_and_sig_as_C_string(resolved_klass(),
 818                                                     link_info.name(),
 819                                                     link_info.signature()));
 820   }
 821 
 822   if (link_info.check_access()) {
 823     // JDK8 adds non-public interface methods, and accessability check requirement
 824     KlassHandle current_klass = link_info.current_klass();
 825 
 826     assert(current_klass.not_null() , "current_klass should not be null");
 827 
 828     // check if method can be accessed by the referring class
 829     check_method_accessability(current_klass,
 830                                resolved_klass,
 831                                KlassHandle(THREAD, resolved_method->method_holder()),
 832                                resolved_method,
 833                                CHECK_NULL);
 834 
 835     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 836   }
 837 
 838   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 839     ResourceMark rm(THREAD);
 840     char buf[200];
 841     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 842                  Method::name_and_sig_as_C_string(resolved_klass(),
 843                  resolved_method->name(), resolved_method->signature()));
 844     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 845   }
 846 
 847   if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) {
 848     ResourceMark rm(THREAD);
 849     char buf[200];
 850 
 851     KlassHandle current_klass = link_info.current_klass();
 852     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s",
 853                  Method::name_and_sig_as_C_string(resolved_klass(),
 854                                                   resolved_method->name(),
 855                                                   resolved_method->signature()),
 856                                                   (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
 857      THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 858   }
 859 
 860   if (log_develop_is_enabled(Trace, itables)) {
 861     trace_method_resolution("invokeinterface resolved method: caller-class",
 862                             link_info.current_klass(), resolved_klass,
 863                             resolved_method, true);
 864   }
 865 
 866   return resolved_method;
 867 }
 868 
 869 //------------------------------------------------------------------------------------------------------------------------
 870 // Field resolution
 871 
 872 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
 873                                              KlassHandle resolved_klass,
 874                                              KlassHandle sel_klass,
 875                                              const fieldDescriptor& fd,
 876                                              TRAPS) {
 877   if (!Reflection::verify_field_access(ref_klass(),
 878                                        resolved_klass(),
 879                                        sel_klass(),
 880                                        fd.access_flags(),
 881                                        true)) {
 882     ResourceMark rm(THREAD);
 883     Exceptions::fthrow(
 884       THREAD_AND_LOCATION,
 885       vmSymbols::java_lang_IllegalAccessError(),
 886       "tried to access field %s.%s from class %s",
 887       sel_klass->external_name(),
 888       fd.name()->as_C_string(),
 889       ref_klass->external_name()
 890     );
 891     return;
 892   }
 893 }
 894 
 895 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 896   LinkInfo link_info(pool, index, method, CHECK);
 897   resolve_field(fd, link_info, byte, true, CHECK);
 898 }
 899 
 900 void LinkResolver::resolve_field(fieldDescriptor& fd,
 901                                  const LinkInfo& link_info,
 902                                  Bytecodes::Code byte, bool initialize_class,
 903                                  TRAPS) {
 904   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 905          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 906          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 907          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 908 
 909   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 910   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 911   // Check if there's a resolved klass containing the field
 912   KlassHandle resolved_klass = link_info.resolved_klass();
 913   Symbol* field = link_info.name();
 914   Symbol* sig = link_info.signature();
 915 
 916   if (resolved_klass.is_null()) {
 917     ResourceMark rm(THREAD);
 918     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 919   }
 920 
 921   // Resolve instance field
 922   KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd));
 923   // check if field exists; i.e., if a klass containing the field def has been selected
 924   if (sel_klass.is_null()) {
 925     ResourceMark rm(THREAD);
 926     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 927   }
 928 
 929   if (!link_info.check_access())
 930     // Access checking may be turned off when calling from within the VM.
 931     return;
 932 
 933   // check access
 934   KlassHandle current_klass = link_info.current_klass();
 935   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 936 
 937   // check for errors
 938   if (is_static != fd.is_static()) {
 939     ResourceMark rm(THREAD);
 940     char msg[200];
 941     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
 942     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 943   }
 944 
 945   // A final field can be modified only
 946   // (1) by methods declared in the class declaring the field and
 947   // (2) by the <clinit> method (in case of a static field)
 948   //     or by the <init> method (in case of an instance field).
 949   if (is_put && fd.access_flags().is_final()) {
 950     ResourceMark rm(THREAD);
 951     stringStream ss;
 952 
 953     if (sel_klass() != current_klass()) {
 954       ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
 955                 is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string(),
 956                 current_klass()->external_name());
 957       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 958     }
 959 
 960     if (CheckFinalFieldModifications &&
 961         fd.constants()->pool_holder()->major_version() >= 53) {
 962       methodHandle m = link_info.current_method();
 963       assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
 964       bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
 965                                                  fd.is_static() &&
 966                                                  !m()->is_static_initializer());
 967       bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
 968                                                    !fd.is_static() &&
 969                                                    !m->is_object_initializer());
 970 
 971       if (is_initialized_static_final_update || is_initialized_instance_final_update) {
 972         ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
 973                  is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string(),
 974                  current_klass()->external_name(),
 975                  is_static ? "<clinit>" : "<init>");
 976         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 977       }
 978     }
 979   }
 980 
 981   // initialize resolved_klass if necessary
 982   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
 983   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
 984   //
 985   // note 2: we don't want to force initialization if we are just checking
 986   //         if the field access is legal; e.g., during compilation
 987   if (is_static && initialize_class) {
 988     sel_klass->initialize(CHECK);
 989   }
 990 
 991   if (sel_klass() != current_klass()) {
 992     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
 993   }
 994 
 995   // return information. note that the klass is set to the actual klass containing the
 996   // field, otherwise access of static fields in superclasses will not work.
 997 }
 998 
 999 
1000 //------------------------------------------------------------------------------------------------------------------------
1001 // Invoke resolution
1002 //
1003 // Naming conventions:
1004 //
1005 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1006 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1007 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1008 // recv_klass         the receiver klass
1009 
1010 
1011 void LinkResolver::resolve_static_call(CallInfo& result,
1012                                        const LinkInfo& link_info,
1013                                        bool initialize_class, TRAPS) {
1014   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
1015 
1016   // The resolved class can change as a result of this resolution.
1017   KlassHandle resolved_klass(THREAD, resolved_method->method_holder());
1018 
1019   // Initialize klass (this should only happen if everything is ok)
1020   if (initialize_class && resolved_klass->should_be_initialized()) {
1021     resolved_klass->initialize(CHECK);
1022     // Use updated LinkInfo to reresolve with resolved method holder
1023     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1024                       link_info.current_klass(),
1025                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1026     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1027   }
1028 
1029   // setup result
1030   result.set_static(resolved_klass, resolved_method, CHECK);
1031 }
1032 
1033 // throws linktime exceptions
1034 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1035 
1036   KlassHandle resolved_klass = link_info.resolved_klass();
1037   methodHandle resolved_method;
1038   if (!resolved_klass->is_interface()) {
1039     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1040   } else {
1041     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1042   }
1043   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1044 
1045   // check if static
1046   if (!resolved_method->is_static()) {
1047     ResourceMark rm(THREAD);
1048     char buf[200];
1049     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1050                                                       resolved_method->name(),
1051                                                       resolved_method->signature()));
1052     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1053   }
1054   return resolved_method;
1055 }
1056 
1057 
1058 void LinkResolver::resolve_special_call(CallInfo& result,
1059                                         const LinkInfo& link_info,
1060                                         TRAPS) {
1061   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1062   runtime_resolve_special_method(result, resolved_method,
1063                                  link_info.resolved_klass(),
1064                                  link_info.current_klass(),
1065                                  link_info.check_access(), CHECK);
1066 }
1067 
1068 // throws linktime exceptions
1069 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1070                                                            TRAPS) {
1071 
1072   // Invokespecial is called for multiple special reasons:
1073   // <init>
1074   // local private method invocation, for classes and interfaces
1075   // superclass.method, which can also resolve to a default method
1076   // and the selected method is recalculated relative to the direct superclass
1077   // superinterface.method, which explicitly does not check shadowing
1078   KlassHandle resolved_klass = link_info.resolved_klass();
1079   methodHandle resolved_method;
1080 
1081   if (!resolved_klass->is_interface()) {
1082     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1083   } else {
1084     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1085   }
1086 
1087   // check if method name is <init>, that it is found in same klass as static type
1088   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1089       resolved_method->method_holder() != resolved_klass()) {
1090     ResourceMark rm(THREAD);
1091     Exceptions::fthrow(
1092       THREAD_AND_LOCATION,
1093       vmSymbols::java_lang_NoSuchMethodError(),
1094       "%s: method %s%s not found",
1095       resolved_klass->external_name(),
1096       resolved_method->name()->as_C_string(),
1097       resolved_method->signature()->as_C_string()
1098     );
1099     return NULL;
1100   }
1101 
1102   // check if invokespecial's interface method reference is in an indirect superinterface
1103   KlassHandle current_klass = link_info.current_klass();
1104   if (!current_klass.is_null() && resolved_klass->is_interface()) {
1105     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
1106                                   current_klass() :
1107                                   InstanceKlass::cast(current_klass())->host_klass();
1108     // Disable verification for the dynamically-generated reflection bytecodes.
1109     bool is_reflect = klass_to_check->is_subclass_of(
1110                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1111 
1112     if (!is_reflect &&
1113         !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {
1114       ResourceMark rm(THREAD);
1115       char buf[200];
1116       jio_snprintf(buf, sizeof(buf),
1117                    "Interface method reference: %s, is in an indirect superinterface of %s",
1118                    Method::name_and_sig_as_C_string(resolved_klass(),
1119                                                          resolved_method->name(),
1120                                                          resolved_method->signature()),
1121                    current_klass->external_name());
1122       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1123     }
1124   }
1125 
1126   // check if not static
1127   if (resolved_method->is_static()) {
1128     ResourceMark rm(THREAD);
1129     char buf[200];
1130     jio_snprintf(buf, sizeof(buf),
1131                  "Expecting non-static method %s",
1132                  Method::name_and_sig_as_C_string(resolved_klass(),
1133                                                   resolved_method->name(),
1134                                                   resolved_method->signature()));
1135     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1136   }
1137 
1138   if (log_develop_is_enabled(Trace, itables)) {
1139     trace_method_resolution("invokespecial resolved method: caller-class:",
1140                             current_klass, resolved_klass, resolved_method, true);
1141   }
1142 
1143   return resolved_method;
1144 }
1145 
1146 // throws runtime exceptions
1147 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1148                                                   const methodHandle& resolved_method,
1149                                                   KlassHandle resolved_klass,
1150                                                   KlassHandle current_klass,
1151                                                   bool check_access, TRAPS) {
1152 
1153   // resolved method is selected method unless we have an old-style lookup
1154   // for a superclass method
1155   // Invokespecial for a superinterface, resolved method is selected method,
1156   // no checks for shadowing
1157   methodHandle sel_method(THREAD, resolved_method());
1158 
1159   // check if this is an old-style super call and do a new lookup if so
1160   { KlassHandle method_klass  = KlassHandle(THREAD,
1161                                             resolved_method->method_holder());
1162 
1163     if (check_access &&
1164         // a) check if ACC_SUPER flag is set for the current class
1165         (current_klass->is_super() || !AllowNonVirtualCalls) &&
1166         // b) check if the class of the resolved_klass is a superclass
1167         // (not supertype in order to exclude interface classes) of the current class.
1168         // This check is not performed for super.invoke for interface methods
1169         // in super interfaces.
1170         current_klass->is_subclass_of(resolved_klass()) &&
1171         current_klass() != resolved_klass() &&
1172         // c) check if the method is not <init>
1173         resolved_method->name() != vmSymbols::object_initializer_name()) {
1174       // Lookup super method
1175       KlassHandle super_klass(THREAD, current_klass->super());
1176       sel_method = lookup_instance_method_in_klasses(super_klass,
1177                            resolved_method->name(),
1178                            resolved_method->signature(), CHECK);
1179       // check if found
1180       if (sel_method.is_null()) {
1181         ResourceMark rm(THREAD);
1182         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1183                   Method::name_and_sig_as_C_string(resolved_klass(),
1184                                             resolved_method->name(),
1185                                             resolved_method->signature()));
1186       }
1187     }
1188   }
1189 
1190   // check if not static
1191   if (sel_method->is_static()) {
1192     ResourceMark rm(THREAD);
1193     char buf[200];
1194     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1195                                                                                                              resolved_method->name(),
1196                                                                                                              resolved_method->signature()));
1197     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1198   }
1199 
1200   // check if abstract
1201   if (sel_method->is_abstract()) {
1202     ResourceMark rm(THREAD);
1203     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1204               Method::name_and_sig_as_C_string(resolved_klass(),
1205                                                sel_method->name(),
1206                                                sel_method->signature()));
1207   }
1208 
1209   if (log_develop_is_enabled(Trace, itables)) {
1210     trace_method_resolution("invokespecial selected method: resolved-class:",
1211                             resolved_klass, resolved_klass, sel_method, true);
1212   }
1213 
1214   // setup result
1215   result.set_static(resolved_klass, sel_method, CHECK);
1216 }
1217 
1218 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass,
1219                                         const LinkInfo& link_info,
1220                                         bool check_null_and_abstract, TRAPS) {
1221   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1222   runtime_resolve_virtual_method(result, resolved_method,
1223                                  link_info.resolved_klass(),
1224                                  recv, receiver_klass,
1225                                  check_null_and_abstract, CHECK);
1226 }
1227 
1228 // throws linktime exceptions
1229 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1230                                                            TRAPS) {
1231   // normal method resolution
1232   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1233 
1234   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1235   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1236 
1237   // check if private interface method
1238   KlassHandle resolved_klass = link_info.resolved_klass();
1239   KlassHandle current_klass = link_info.current_klass();
1240 
1241   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1242   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1243     ResourceMark rm(THREAD);
1244     char buf[200];
1245     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1246                  Method::name_and_sig_as_C_string(resolved_klass(),
1247                                                   resolved_method->name(),
1248                                                   resolved_method->signature()),
1249                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1250     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1251   }
1252 
1253   // check if not static
1254   if (resolved_method->is_static()) {
1255     ResourceMark rm(THREAD);
1256     char buf[200];
1257     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1258                                                                                                              resolved_method->name(),
1259                                                                                                              resolved_method->signature()));
1260     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1261   }
1262 
1263   if (log_develop_is_enabled(Trace, vtables)) {
1264     trace_method_resolution("invokevirtual resolved method: caller-class:",
1265                             current_klass, resolved_klass, resolved_method, false);
1266   }
1267 
1268   return resolved_method;
1269 }
1270 
1271 // throws runtime exceptions
1272 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1273                                                   const methodHandle& resolved_method,
1274                                                   KlassHandle resolved_klass,
1275                                                   Handle recv,
1276                                                   KlassHandle recv_klass,
1277                                                   bool check_null_and_abstract,
1278                                                   TRAPS) {
1279 
1280   // setup default return values
1281   int vtable_index = Method::invalid_vtable_index;
1282   methodHandle selected_method;
1283 
1284   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1285 
1286   // runtime method resolution
1287   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1288     THROW(vmSymbols::java_lang_NullPointerException());
1289   }
1290 
1291   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1292   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1293   // a missing receiver might result in a bogus lookup.
1294   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1295 
1296   // do lookup based on receiver klass using the vtable index
1297   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1298     vtable_index = vtable_index_of_interface_method(resolved_klass,
1299                            resolved_method);
1300     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1301 
1302     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1303   } else {
1304     // at this point we are sure that resolved_method is virtual and not
1305     // a default or miranda method; therefore, it must have a valid vtable index.
1306     assert(!resolved_method->has_itable_index(), "");
1307     vtable_index = resolved_method->vtable_index();
1308     // We could get a negative vtable_index for final methods,
1309     // because as an optimization they are they are never put in the vtable,
1310     // unless they override an existing method.
1311     // If we do get a negative, it means the resolved method is the the selected
1312     // method, and it can never be changed by an override.
1313     if (vtable_index == Method::nonvirtual_vtable_index) {
1314       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1315       selected_method = resolved_method;
1316     } else {
1317       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1318     }
1319   }
1320 
1321   // check if method exists
1322   if (selected_method.is_null()) {
1323     ResourceMark rm(THREAD);
1324     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1325               Method::name_and_sig_as_C_string(resolved_klass(),
1326                                                       resolved_method->name(),
1327                                                       resolved_method->signature()));
1328   }
1329 
1330   // check if abstract
1331   if (check_null_and_abstract && selected_method->is_abstract()) {
1332     ResourceMark rm(THREAD);
1333     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1334               Method::name_and_sig_as_C_string(resolved_klass(),
1335                                                       selected_method->name(),
1336                                                       selected_method->signature()));
1337   }
1338 
1339   if (log_develop_is_enabled(Trace, vtables)) {
1340     trace_method_resolution("invokevirtual selected method: receiver-class:",
1341                             recv_klass, resolved_klass, selected_method,
1342                             false, vtable_index);
1343   }
1344   // setup result
1345   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1346 }
1347 
1348 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass,
1349                                           const LinkInfo& link_info,
1350                                           bool check_null_and_abstract, TRAPS) {
1351   // throws linktime exceptions
1352   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1353   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1354                                    recv, recv_klass, check_null_and_abstract, CHECK);
1355 }
1356 
1357 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1358                                                              TRAPS) {
1359   // normal interface method resolution
1360   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1361   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1362   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1363 
1364   return resolved_method;
1365 }
1366 
1367 // throws runtime exceptions
1368 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1369                                                     const methodHandle& resolved_method,
1370                                                     KlassHandle resolved_klass,
1371                                                     Handle recv,
1372                                                     KlassHandle recv_klass,
1373                                                     bool check_null_and_abstract, TRAPS) {
1374   // check if receiver exists
1375   if (check_null_and_abstract && recv.is_null()) {
1376     THROW(vmSymbols::java_lang_NullPointerException());
1377   }
1378 
1379   // check if receiver klass implements the resolved interface
1380   if (!recv_klass->is_subtype_of(resolved_klass())) {
1381     ResourceMark rm(THREAD);
1382     char buf[200];
1383     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1384                  recv_klass()->external_name(),
1385                  resolved_klass()->external_name());
1386     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1387   }
1388 
1389   // do lookup based on receiver klass
1390   // This search must match the linktime preparation search for itable initialization
1391   // to correctly enforce loader constraints for interface method inheritance
1392   methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass,
1393                                                   resolved_method->name(),
1394                                                   resolved_method->signature(), CHECK);
1395   if (sel_method.is_null() && !check_null_and_abstract) {
1396     // In theory this is a harmless placeholder value, but
1397     // in practice leaving in null affects the nsk default method tests.
1398     // This needs further study.
1399     sel_method = resolved_method;
1400   }
1401   // check if method exists
1402   if (sel_method.is_null()) {
1403     ResourceMark rm(THREAD);
1404     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1405                    Method::name_and_sig_as_C_string(recv_klass(),
1406                                                     resolved_method->name(),
1407                                                     resolved_method->signature()));
1408   }
1409   // check access
1410   // Throw Illegal Access Error if sel_method is not public.
1411   if (!sel_method->is_public()) {
1412     ResourceMark rm(THREAD);
1413     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1414               Method::name_and_sig_as_C_string(recv_klass(),
1415                                                sel_method->name(),
1416                                                sel_method->signature()));
1417   }
1418   // check if abstract
1419   if (check_null_and_abstract && sel_method->is_abstract()) {
1420     ResourceMark rm(THREAD);
1421     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1422               Method::name_and_sig_as_C_string(recv_klass(),
1423                                                       sel_method->name(),
1424                                                       sel_method->signature()));
1425   }
1426 
1427   if (log_develop_is_enabled(Trace, itables)) {
1428     trace_method_resolution("invokeinterface selected method: receiver-class",
1429                             recv_klass, resolved_klass, sel_method, true);
1430   }
1431   // setup result
1432   if (!resolved_method->has_itable_index()) {
1433     int vtable_index = resolved_method->vtable_index();
1434     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1435     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1436   } else {
1437     int itable_index = resolved_method()->itable_index();
1438     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1439   }
1440 }
1441 
1442 
1443 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1444                                                  const LinkInfo& link_info) {
1445   EXCEPTION_MARK;
1446   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1447   if (HAS_PENDING_EXCEPTION) {
1448     CLEAR_PENDING_EXCEPTION;
1449     return methodHandle();
1450   } else {
1451     return method_result;
1452   }
1453 }
1454 
1455 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1456                                                  const LinkInfo& link_info) {
1457   EXCEPTION_MARK;
1458   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1459   if (HAS_PENDING_EXCEPTION) {
1460     CLEAR_PENDING_EXCEPTION;
1461     return methodHandle();
1462   } else {
1463     return method_result;
1464   }
1465 }
1466 
1467 methodHandle LinkResolver::resolve_virtual_call_or_null(
1468                                                  KlassHandle receiver_klass,
1469                                                  const LinkInfo& link_info) {
1470   EXCEPTION_MARK;
1471   CallInfo info;
1472   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1473   if (HAS_PENDING_EXCEPTION) {
1474     CLEAR_PENDING_EXCEPTION;
1475     return methodHandle();
1476   }
1477   return info.selected_method();
1478 }
1479 
1480 methodHandle LinkResolver::resolve_interface_call_or_null(
1481                                                  KlassHandle receiver_klass,
1482                                                  const LinkInfo& link_info) {
1483   EXCEPTION_MARK;
1484   CallInfo info;
1485   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1486   if (HAS_PENDING_EXCEPTION) {
1487     CLEAR_PENDING_EXCEPTION;
1488     return methodHandle();
1489   }
1490   return info.selected_method();
1491 }
1492 
1493 int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass,
1494                                                const LinkInfo& link_info) {
1495   EXCEPTION_MARK;
1496   CallInfo info;
1497   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1498                        /*check_null_or_abstract*/false, THREAD);
1499   if (HAS_PENDING_EXCEPTION) {
1500     CLEAR_PENDING_EXCEPTION;
1501     return Method::invalid_vtable_index;
1502   }
1503   return info.vtable_index();
1504 }
1505 
1506 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1507   EXCEPTION_MARK;
1508   CallInfo info;
1509   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1510   if (HAS_PENDING_EXCEPTION) {
1511     CLEAR_PENDING_EXCEPTION;
1512     return methodHandle();
1513   }
1514   return info.selected_method();
1515 }
1516 
1517 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1518   EXCEPTION_MARK;
1519   CallInfo info;
1520   resolve_special_call(info, link_info, THREAD);
1521   if (HAS_PENDING_EXCEPTION) {
1522     CLEAR_PENDING_EXCEPTION;
1523     return methodHandle();
1524   }
1525   return info.selected_method();
1526 }
1527 
1528 
1529 
1530 //------------------------------------------------------------------------------------------------------------------------
1531 // ConstantPool entries
1532 
1533 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1534   switch (byte) {
1535     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1536     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1537     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1538     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1539     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1540     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1541   }
1542   return;
1543 }
1544 
1545 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1546                              const methodHandle& attached_method,
1547                              Bytecodes::Code byte, TRAPS) {
1548   KlassHandle defc = attached_method->method_holder();
1549   Symbol* name = attached_method->name();
1550   Symbol* type = attached_method->signature();
1551   LinkInfo link_info(defc, name, type);
1552   switch(byte) {
1553     case Bytecodes::_invokevirtual:
1554       resolve_virtual_call(result, recv, recv->klass(), link_info,
1555                            /*check_null_and_abstract=*/true, CHECK);
1556       break;
1557     case Bytecodes::_invokeinterface:
1558       resolve_interface_call(result, recv, recv->klass(), link_info,
1559                              /*check_null_and_abstract=*/true, CHECK);
1560       break;
1561     case Bytecodes::_invokestatic:
1562       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1563       break;
1564     case Bytecodes::_invokespecial:
1565       resolve_special_call(result, link_info, CHECK);
1566       break;
1567     default:
1568       fatal("bad call: %s", Bytecodes::name(byte));
1569   }
1570 }
1571 
1572 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1573   LinkInfo link_info(pool, index, CHECK);
1574   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1575 }
1576 
1577 
1578 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1579   LinkInfo link_info(pool, index, CHECK);
1580   resolve_special_call(result, link_info, CHECK);
1581 }
1582 
1583 
1584 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1585                                           const constantPoolHandle& pool, int index,
1586                                           TRAPS) {
1587 
1588   LinkInfo link_info(pool, index, CHECK);
1589   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1590   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1591 }
1592 
1593 
1594 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1595   LinkInfo link_info(pool, index, CHECK);
1596   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1597   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1598 }
1599 
1600 
1601 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1602   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1603   LinkInfo link_info(pool, index, CHECK);
1604   if (TraceMethodHandles) {
1605     ResourceMark rm(THREAD);
1606     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1607                   link_info.signature()->as_C_string());
1608   }
1609   resolve_handle_call(result, link_info, CHECK);
1610 }
1611 
1612 void LinkResolver::resolve_handle_call(CallInfo& result,
1613                                        const LinkInfo& link_info,
1614                                        TRAPS) {
1615   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1616   KlassHandle resolved_klass = link_info.resolved_klass();
1617   assert(resolved_klass() == SystemDictionary::MethodHandle_klass() ||
1618          resolved_klass() == SystemDictionary::VarHandle_klass(), "");
1619   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1620   Handle       resolved_appendix;
1621   Handle       resolved_method_type;
1622   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1623                                        &resolved_appendix, &resolved_method_type, CHECK);
1624   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1625 }
1626 
1627 static void wrap_invokedynamic_exception(TRAPS) {
1628   if (HAS_PENDING_EXCEPTION) {
1629     if (TraceMethodHandles) {
1630       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1631       PENDING_EXCEPTION->print();
1632     }
1633     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1634       // throw these guys, since they are already wrapped
1635       return;
1636     }
1637     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1638       // intercept only LinkageErrors which might have failed to wrap
1639       return;
1640     }
1641     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1642     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1643     CLEAR_PENDING_EXCEPTION;
1644     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1645   }
1646 }
1647 
1648 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1649   Symbol* method_name       = pool->name_ref_at(index);
1650   Symbol* method_signature  = pool->signature_ref_at(index);
1651   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1652 
1653   // Resolve the bootstrap specifier (BSM + optional arguments).
1654   Handle bootstrap_specifier;
1655   // Check if CallSite has been bound already:
1656   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1657   if (cpce->is_f1_null()) {
1658     int pool_index = cpce->constant_pool_index();
1659     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1660     wrap_invokedynamic_exception(CHECK);
1661     assert(bsm_info != NULL, "");
1662     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1663     bootstrap_specifier = Handle(THREAD, bsm_info);
1664   }
1665   if (!cpce->is_f1_null()) {
1666     methodHandle method(     THREAD, cpce->f1_as_method());
1667     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1668     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1669     result.set_handle(method, appendix, method_type, THREAD);
1670     wrap_invokedynamic_exception(CHECK);
1671     return;
1672   }
1673 
1674   if (TraceMethodHandles) {
1675     ResourceMark rm(THREAD);
1676     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1677                   ConstantPool::decode_invokedynamic_index(index),
1678                   method_name->as_C_string(), method_signature->as_C_string(),
1679                   current_klass->name()->as_C_string());
1680     tty->print("  BSM info: "); bootstrap_specifier->print();
1681   }
1682 
1683   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1684 }
1685 
1686 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1687                                         Handle bootstrap_specifier,
1688                                         Symbol* method_name, Symbol* method_signature,
1689                                         KlassHandle current_klass,
1690                                         TRAPS) {
1691   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1692   // The appendix argument is likely to be a freshly-created CallSite.
1693   Handle       resolved_appendix;
1694   Handle       resolved_method_type;
1695   methodHandle resolved_method =
1696     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1697                                                      bootstrap_specifier,
1698                                                      method_name, method_signature,
1699                                                      &resolved_appendix,
1700                                                      &resolved_method_type,
1701                                                      THREAD);
1702   wrap_invokedynamic_exception(CHECK);
1703   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1704   wrap_invokedynamic_exception(CHECK);
1705 }