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 (fd.constants()->pool_holder()->major_version() >= 53) {
 961       methodHandle m = link_info.current_method();
 962       assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
 963       bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
 964                                                  fd.is_static() &&
 965                                                  !m()->is_static_initializer());
 966       bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
 967                                                    !fd.is_static() &&
 968                                                    !m->is_object_initializer());
 969 
 970       if (is_initialized_static_final_update || is_initialized_instance_final_update) {
 971         ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
 972                  is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string(),
 973                  m()->name()->as_C_string(),
 974                  is_static ? "<clinit>" : "<init>");
 975         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 976       }
 977     }
 978   }
 979 
 980   // initialize resolved_klass if necessary
 981   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
 982   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
 983   //
 984   // note 2: we don't want to force initialization if we are just checking
 985   //         if the field access is legal; e.g., during compilation
 986   if (is_static && initialize_class) {
 987     sel_klass->initialize(CHECK);
 988   }
 989 
 990   if (sel_klass() != current_klass()) {
 991     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
 992   }
 993 
 994   // return information. note that the klass is set to the actual klass containing the
 995   // field, otherwise access of static fields in superclasses will not work.
 996 }
 997 
 998 
 999 //------------------------------------------------------------------------------------------------------------------------
1000 // Invoke resolution
1001 //
1002 // Naming conventions:
1003 //
1004 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1005 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1006 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1007 // recv_klass         the receiver klass
1008 
1009 
1010 void LinkResolver::resolve_static_call(CallInfo& result,
1011                                        const LinkInfo& link_info,
1012                                        bool initialize_class, TRAPS) {
1013   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
1014 
1015   // The resolved class can change as a result of this resolution.
1016   KlassHandle resolved_klass(THREAD, resolved_method->method_holder());
1017 
1018   // Initialize klass (this should only happen if everything is ok)
1019   if (initialize_class && resolved_klass->should_be_initialized()) {
1020     resolved_klass->initialize(CHECK);
1021     // Use updated LinkInfo to reresolve with resolved method holder
1022     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1023                       link_info.current_klass(),
1024                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1025     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1026   }
1027 
1028   // setup result
1029   result.set_static(resolved_klass, resolved_method, CHECK);
1030 }
1031 
1032 // throws linktime exceptions
1033 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1034 
1035   KlassHandle resolved_klass = link_info.resolved_klass();
1036   methodHandle resolved_method;
1037   if (!resolved_klass->is_interface()) {
1038     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1039   } else {
1040     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1041   }
1042   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1043 
1044   // check if static
1045   if (!resolved_method->is_static()) {
1046     ResourceMark rm(THREAD);
1047     char buf[200];
1048     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1049                                                       resolved_method->name(),
1050                                                       resolved_method->signature()));
1051     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1052   }
1053   return resolved_method;
1054 }
1055 
1056 
1057 void LinkResolver::resolve_special_call(CallInfo& result,
1058                                         const LinkInfo& link_info,
1059                                         TRAPS) {
1060   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1061   runtime_resolve_special_method(result, resolved_method,
1062                                  link_info.resolved_klass(),
1063                                  link_info.current_klass(),
1064                                  link_info.check_access(), CHECK);
1065 }
1066 
1067 // throws linktime exceptions
1068 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1069                                                            TRAPS) {
1070 
1071   // Invokespecial is called for multiple special reasons:
1072   // <init>
1073   // local private method invocation, for classes and interfaces
1074   // superclass.method, which can also resolve to a default method
1075   // and the selected method is recalculated relative to the direct superclass
1076   // superinterface.method, which explicitly does not check shadowing
1077   KlassHandle resolved_klass = link_info.resolved_klass();
1078   methodHandle resolved_method;
1079 
1080   if (!resolved_klass->is_interface()) {
1081     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1082   } else {
1083     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1084   }
1085 
1086   // check if method name is <init>, that it is found in same klass as static type
1087   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1088       resolved_method->method_holder() != resolved_klass()) {
1089     ResourceMark rm(THREAD);
1090     Exceptions::fthrow(
1091       THREAD_AND_LOCATION,
1092       vmSymbols::java_lang_NoSuchMethodError(),
1093       "%s: method %s%s not found",
1094       resolved_klass->external_name(),
1095       resolved_method->name()->as_C_string(),
1096       resolved_method->signature()->as_C_string()
1097     );
1098     return NULL;
1099   }
1100 
1101   // check if invokespecial's interface method reference is in an indirect superinterface
1102   KlassHandle current_klass = link_info.current_klass();
1103   if (!current_klass.is_null() && resolved_klass->is_interface()) {
1104     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
1105                                   current_klass() :
1106                                   InstanceKlass::cast(current_klass())->host_klass();
1107     // Disable verification for the dynamically-generated reflection bytecodes.
1108     bool is_reflect = klass_to_check->is_subclass_of(
1109                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1110 
1111     if (!is_reflect &&
1112         !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {
1113       ResourceMark rm(THREAD);
1114       char buf[200];
1115       jio_snprintf(buf, sizeof(buf),
1116                    "Interface method reference: %s, is in an indirect superinterface of %s",
1117                    Method::name_and_sig_as_C_string(resolved_klass(),
1118                                                          resolved_method->name(),
1119                                                          resolved_method->signature()),
1120                    current_klass->external_name());
1121       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1122     }
1123   }
1124 
1125   // check if not static
1126   if (resolved_method->is_static()) {
1127     ResourceMark rm(THREAD);
1128     char buf[200];
1129     jio_snprintf(buf, sizeof(buf),
1130                  "Expecting non-static method %s",
1131                  Method::name_and_sig_as_C_string(resolved_klass(),
1132                                                   resolved_method->name(),
1133                                                   resolved_method->signature()));
1134     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1135   }
1136 
1137   if (log_develop_is_enabled(Trace, itables)) {
1138     trace_method_resolution("invokespecial resolved method: caller-class:",
1139                             current_klass, resolved_klass, resolved_method, true);
1140   }
1141 
1142   return resolved_method;
1143 }
1144 
1145 // throws runtime exceptions
1146 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1147                                                   const methodHandle& resolved_method,
1148                                                   KlassHandle resolved_klass,
1149                                                   KlassHandle current_klass,
1150                                                   bool check_access, TRAPS) {
1151 
1152   // resolved method is selected method unless we have an old-style lookup
1153   // for a superclass method
1154   // Invokespecial for a superinterface, resolved method is selected method,
1155   // no checks for shadowing
1156   methodHandle sel_method(THREAD, resolved_method());
1157 
1158   // check if this is an old-style super call and do a new lookup if so
1159   { KlassHandle method_klass  = KlassHandle(THREAD,
1160                                             resolved_method->method_holder());
1161 
1162     if (check_access &&
1163         // a) check if ACC_SUPER flag is set for the current class
1164         (current_klass->is_super() || !AllowNonVirtualCalls) &&
1165         // b) check if the class of the resolved_klass is a superclass
1166         // (not supertype in order to exclude interface classes) of the current class.
1167         // This check is not performed for super.invoke for interface methods
1168         // in super interfaces.
1169         current_klass->is_subclass_of(resolved_klass()) &&
1170         current_klass() != resolved_klass() &&
1171         // c) check if the method is not <init>
1172         resolved_method->name() != vmSymbols::object_initializer_name()) {
1173       // Lookup super method
1174       KlassHandle super_klass(THREAD, current_klass->super());
1175       sel_method = lookup_instance_method_in_klasses(super_klass,
1176                            resolved_method->name(),
1177                            resolved_method->signature(), CHECK);
1178       // check if found
1179       if (sel_method.is_null()) {
1180         ResourceMark rm(THREAD);
1181         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1182                   Method::name_and_sig_as_C_string(resolved_klass(),
1183                                             resolved_method->name(),
1184                                             resolved_method->signature()));
1185       }
1186     }
1187   }
1188 
1189   // check if not static
1190   if (sel_method->is_static()) {
1191     ResourceMark rm(THREAD);
1192     char buf[200];
1193     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1194                                                                                                              resolved_method->name(),
1195                                                                                                              resolved_method->signature()));
1196     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1197   }
1198 
1199   // check if abstract
1200   if (sel_method->is_abstract()) {
1201     ResourceMark rm(THREAD);
1202     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1203               Method::name_and_sig_as_C_string(resolved_klass(),
1204                                                sel_method->name(),
1205                                                sel_method->signature()));
1206   }
1207 
1208   if (log_develop_is_enabled(Trace, itables)) {
1209     trace_method_resolution("invokespecial selected method: resolved-class:",
1210                             resolved_klass, resolved_klass, sel_method, true);
1211   }
1212 
1213   // setup result
1214   result.set_static(resolved_klass, sel_method, CHECK);
1215 }
1216 
1217 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass,
1218                                         const LinkInfo& link_info,
1219                                         bool check_null_and_abstract, TRAPS) {
1220   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1221   runtime_resolve_virtual_method(result, resolved_method,
1222                                  link_info.resolved_klass(),
1223                                  recv, receiver_klass,
1224                                  check_null_and_abstract, CHECK);
1225 }
1226 
1227 // throws linktime exceptions
1228 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1229                                                            TRAPS) {
1230   // normal method resolution
1231   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1232 
1233   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1234   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1235 
1236   // check if private interface method
1237   KlassHandle resolved_klass = link_info.resolved_klass();
1238   KlassHandle current_klass = link_info.current_klass();
1239 
1240   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1241   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1242     ResourceMark rm(THREAD);
1243     char buf[200];
1244     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1245                  Method::name_and_sig_as_C_string(resolved_klass(),
1246                                                   resolved_method->name(),
1247                                                   resolved_method->signature()),
1248                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1249     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1250   }
1251 
1252   // check if not static
1253   if (resolved_method->is_static()) {
1254     ResourceMark rm(THREAD);
1255     char buf[200];
1256     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1257                                                                                                              resolved_method->name(),
1258                                                                                                              resolved_method->signature()));
1259     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1260   }
1261 
1262   if (log_develop_is_enabled(Trace, vtables)) {
1263     trace_method_resolution("invokevirtual resolved method: caller-class:",
1264                             current_klass, resolved_klass, resolved_method, false);
1265   }
1266 
1267   return resolved_method;
1268 }
1269 
1270 // throws runtime exceptions
1271 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1272                                                   const methodHandle& resolved_method,
1273                                                   KlassHandle resolved_klass,
1274                                                   Handle recv,
1275                                                   KlassHandle recv_klass,
1276                                                   bool check_null_and_abstract,
1277                                                   TRAPS) {
1278 
1279   // setup default return values
1280   int vtable_index = Method::invalid_vtable_index;
1281   methodHandle selected_method;
1282 
1283   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1284 
1285   // runtime method resolution
1286   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1287     THROW(vmSymbols::java_lang_NullPointerException());
1288   }
1289 
1290   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1291   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1292   // a missing receiver might result in a bogus lookup.
1293   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1294 
1295   // do lookup based on receiver klass using the vtable index
1296   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1297     vtable_index = vtable_index_of_interface_method(resolved_klass,
1298                            resolved_method);
1299     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1300 
1301     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1302   } else {
1303     // at this point we are sure that resolved_method is virtual and not
1304     // a default or miranda method; therefore, it must have a valid vtable index.
1305     assert(!resolved_method->has_itable_index(), "");
1306     vtable_index = resolved_method->vtable_index();
1307     // We could get a negative vtable_index for final methods,
1308     // because as an optimization they are they are never put in the vtable,
1309     // unless they override an existing method.
1310     // If we do get a negative, it means the resolved method is the the selected
1311     // method, and it can never be changed by an override.
1312     if (vtable_index == Method::nonvirtual_vtable_index) {
1313       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1314       selected_method = resolved_method;
1315     } else {
1316       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1317     }
1318   }
1319 
1320   // check if method exists
1321   if (selected_method.is_null()) {
1322     ResourceMark rm(THREAD);
1323     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1324               Method::name_and_sig_as_C_string(resolved_klass(),
1325                                                       resolved_method->name(),
1326                                                       resolved_method->signature()));
1327   }
1328 
1329   // check if abstract
1330   if (check_null_and_abstract && selected_method->is_abstract()) {
1331     ResourceMark rm(THREAD);
1332     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1333               Method::name_and_sig_as_C_string(resolved_klass(),
1334                                                       selected_method->name(),
1335                                                       selected_method->signature()));
1336   }
1337 
1338   if (log_develop_is_enabled(Trace, vtables)) {
1339     trace_method_resolution("invokevirtual selected method: receiver-class:",
1340                             recv_klass, resolved_klass, selected_method,
1341                             false, vtable_index);
1342   }
1343   // setup result
1344   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1345 }
1346 
1347 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass,
1348                                           const LinkInfo& link_info,
1349                                           bool check_null_and_abstract, TRAPS) {
1350   // throws linktime exceptions
1351   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1352   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1353                                    recv, recv_klass, check_null_and_abstract, CHECK);
1354 }
1355 
1356 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1357                                                              TRAPS) {
1358   // normal interface method resolution
1359   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1360   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1361   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1362 
1363   return resolved_method;
1364 }
1365 
1366 // throws runtime exceptions
1367 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1368                                                     const methodHandle& resolved_method,
1369                                                     KlassHandle resolved_klass,
1370                                                     Handle recv,
1371                                                     KlassHandle recv_klass,
1372                                                     bool check_null_and_abstract, TRAPS) {
1373   // check if receiver exists
1374   if (check_null_and_abstract && recv.is_null()) {
1375     THROW(vmSymbols::java_lang_NullPointerException());
1376   }
1377 
1378   // check if receiver klass implements the resolved interface
1379   if (!recv_klass->is_subtype_of(resolved_klass())) {
1380     ResourceMark rm(THREAD);
1381     char buf[200];
1382     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1383                  recv_klass()->external_name(),
1384                  resolved_klass()->external_name());
1385     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1386   }
1387 
1388   // do lookup based on receiver klass
1389   // This search must match the linktime preparation search for itable initialization
1390   // to correctly enforce loader constraints for interface method inheritance
1391   methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass,
1392                                                   resolved_method->name(),
1393                                                   resolved_method->signature(), CHECK);
1394   if (sel_method.is_null() && !check_null_and_abstract) {
1395     // In theory this is a harmless placeholder value, but
1396     // in practice leaving in null affects the nsk default method tests.
1397     // This needs further study.
1398     sel_method = resolved_method;
1399   }
1400   // check if method exists
1401   if (sel_method.is_null()) {
1402     ResourceMark rm(THREAD);
1403     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1404                    Method::name_and_sig_as_C_string(recv_klass(),
1405                                                     resolved_method->name(),
1406                                                     resolved_method->signature()));
1407   }
1408   // check access
1409   // Throw Illegal Access Error if sel_method is not public.
1410   if (!sel_method->is_public()) {
1411     ResourceMark rm(THREAD);
1412     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1413               Method::name_and_sig_as_C_string(recv_klass(),
1414                                                sel_method->name(),
1415                                                sel_method->signature()));
1416   }
1417   // check if abstract
1418   if (check_null_and_abstract && sel_method->is_abstract()) {
1419     ResourceMark rm(THREAD);
1420     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1421               Method::name_and_sig_as_C_string(recv_klass(),
1422                                                       sel_method->name(),
1423                                                       sel_method->signature()));
1424   }
1425 
1426   if (log_develop_is_enabled(Trace, itables)) {
1427     trace_method_resolution("invokeinterface selected method: receiver-class",
1428                             recv_klass, resolved_klass, sel_method, true);
1429   }
1430   // setup result
1431   if (!resolved_method->has_itable_index()) {
1432     int vtable_index = resolved_method->vtable_index();
1433     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1434     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1435   } else {
1436     int itable_index = resolved_method()->itable_index();
1437     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1438   }
1439 }
1440 
1441 
1442 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1443                                                  const LinkInfo& link_info) {
1444   EXCEPTION_MARK;
1445   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1446   if (HAS_PENDING_EXCEPTION) {
1447     CLEAR_PENDING_EXCEPTION;
1448     return methodHandle();
1449   } else {
1450     return method_result;
1451   }
1452 }
1453 
1454 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1455                                                  const LinkInfo& link_info) {
1456   EXCEPTION_MARK;
1457   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1458   if (HAS_PENDING_EXCEPTION) {
1459     CLEAR_PENDING_EXCEPTION;
1460     return methodHandle();
1461   } else {
1462     return method_result;
1463   }
1464 }
1465 
1466 methodHandle LinkResolver::resolve_virtual_call_or_null(
1467                                                  KlassHandle receiver_klass,
1468                                                  const LinkInfo& link_info) {
1469   EXCEPTION_MARK;
1470   CallInfo info;
1471   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1472   if (HAS_PENDING_EXCEPTION) {
1473     CLEAR_PENDING_EXCEPTION;
1474     return methodHandle();
1475   }
1476   return info.selected_method();
1477 }
1478 
1479 methodHandle LinkResolver::resolve_interface_call_or_null(
1480                                                  KlassHandle receiver_klass,
1481                                                  const LinkInfo& link_info) {
1482   EXCEPTION_MARK;
1483   CallInfo info;
1484   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1485   if (HAS_PENDING_EXCEPTION) {
1486     CLEAR_PENDING_EXCEPTION;
1487     return methodHandle();
1488   }
1489   return info.selected_method();
1490 }
1491 
1492 int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass,
1493                                                const LinkInfo& link_info) {
1494   EXCEPTION_MARK;
1495   CallInfo info;
1496   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1497                        /*check_null_or_abstract*/false, THREAD);
1498   if (HAS_PENDING_EXCEPTION) {
1499     CLEAR_PENDING_EXCEPTION;
1500     return Method::invalid_vtable_index;
1501   }
1502   return info.vtable_index();
1503 }
1504 
1505 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1506   EXCEPTION_MARK;
1507   CallInfo info;
1508   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1509   if (HAS_PENDING_EXCEPTION) {
1510     CLEAR_PENDING_EXCEPTION;
1511     return methodHandle();
1512   }
1513   return info.selected_method();
1514 }
1515 
1516 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1517   EXCEPTION_MARK;
1518   CallInfo info;
1519   resolve_special_call(info, link_info, THREAD);
1520   if (HAS_PENDING_EXCEPTION) {
1521     CLEAR_PENDING_EXCEPTION;
1522     return methodHandle();
1523   }
1524   return info.selected_method();
1525 }
1526 
1527 
1528 
1529 //------------------------------------------------------------------------------------------------------------------------
1530 // ConstantPool entries
1531 
1532 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1533   switch (byte) {
1534     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1535     case Bytecodes::_invokespecial  : resolve_invokespecial  (result,       pool, index, CHECK); break;
1536     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1537     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1538     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1539     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1540   }
1541   return;
1542 }
1543 
1544 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1545                              const methodHandle& attached_method,
1546                              Bytecodes::Code byte, TRAPS) {
1547   KlassHandle defc = attached_method->method_holder();
1548   Symbol* name = attached_method->name();
1549   Symbol* type = attached_method->signature();
1550   LinkInfo link_info(defc, name, type);
1551   switch(byte) {
1552     case Bytecodes::_invokevirtual:
1553       resolve_virtual_call(result, recv, recv->klass(), link_info,
1554                            /*check_null_and_abstract=*/true, CHECK);
1555       break;
1556     case Bytecodes::_invokeinterface:
1557       resolve_interface_call(result, recv, recv->klass(), link_info,
1558                              /*check_null_and_abstract=*/true, CHECK);
1559       break;
1560     case Bytecodes::_invokestatic:
1561       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1562       break;
1563     case Bytecodes::_invokespecial:
1564       resolve_special_call(result, link_info, CHECK);
1565       break;
1566     default:
1567       fatal("bad call: %s", Bytecodes::name(byte));
1568   }
1569 }
1570 
1571 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1572   LinkInfo link_info(pool, index, CHECK);
1573   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1574 }
1575 
1576 
1577 void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1578   LinkInfo link_info(pool, index, CHECK);
1579   resolve_special_call(result, link_info, CHECK);
1580 }
1581 
1582 
1583 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1584                                           const constantPoolHandle& pool, int index,
1585                                           TRAPS) {
1586 
1587   LinkInfo link_info(pool, index, CHECK);
1588   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1589   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1590 }
1591 
1592 
1593 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1594   LinkInfo link_info(pool, index, CHECK);
1595   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1596   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1597 }
1598 
1599 
1600 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1601   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1602   LinkInfo link_info(pool, index, CHECK);
1603   if (TraceMethodHandles) {
1604     ResourceMark rm(THREAD);
1605     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1606                   link_info.signature()->as_C_string());
1607   }
1608   resolve_handle_call(result, link_info, CHECK);
1609 }
1610 
1611 void LinkResolver::resolve_handle_call(CallInfo& result,
1612                                        const LinkInfo& link_info,
1613                                        TRAPS) {
1614   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1615   KlassHandle resolved_klass = link_info.resolved_klass();
1616   assert(resolved_klass() == SystemDictionary::MethodHandle_klass() ||
1617          resolved_klass() == SystemDictionary::VarHandle_klass(), "");
1618   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1619   Handle       resolved_appendix;
1620   Handle       resolved_method_type;
1621   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1622                                        &resolved_appendix, &resolved_method_type, CHECK);
1623   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1624 }
1625 
1626 static void wrap_invokedynamic_exception(TRAPS) {
1627   if (HAS_PENDING_EXCEPTION) {
1628     if (TraceMethodHandles) {
1629       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1630       PENDING_EXCEPTION->print();
1631     }
1632     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1633       // throw these guys, since they are already wrapped
1634       return;
1635     }
1636     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1637       // intercept only LinkageErrors which might have failed to wrap
1638       return;
1639     }
1640     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1641     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1642     CLEAR_PENDING_EXCEPTION;
1643     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1644   }
1645 }
1646 
1647 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1648   Symbol* method_name       = pool->name_ref_at(index);
1649   Symbol* method_signature  = pool->signature_ref_at(index);
1650   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1651 
1652   // Resolve the bootstrap specifier (BSM + optional arguments).
1653   Handle bootstrap_specifier;
1654   // Check if CallSite has been bound already:
1655   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1656   if (cpce->is_f1_null()) {
1657     int pool_index = cpce->constant_pool_index();
1658     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1659     wrap_invokedynamic_exception(CHECK);
1660     assert(bsm_info != NULL, "");
1661     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1662     bootstrap_specifier = Handle(THREAD, bsm_info);
1663   }
1664   if (!cpce->is_f1_null()) {
1665     methodHandle method(     THREAD, cpce->f1_as_method());
1666     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1667     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1668     result.set_handle(method, appendix, method_type, THREAD);
1669     wrap_invokedynamic_exception(CHECK);
1670     return;
1671   }
1672 
1673   if (TraceMethodHandles) {
1674     ResourceMark rm(THREAD);
1675     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1676                   ConstantPool::decode_invokedynamic_index(index),
1677                   method_name->as_C_string(), method_signature->as_C_string(),
1678                   current_klass->name()->as_C_string());
1679     tty->print("  BSM info: "); bootstrap_specifier->print();
1680   }
1681 
1682   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1683 }
1684 
1685 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1686                                         Handle bootstrap_specifier,
1687                                         Symbol* method_name, Symbol* method_signature,
1688                                         KlassHandle current_klass,
1689                                         TRAPS) {
1690   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1691   // The appendix argument is likely to be a freshly-created CallSite.
1692   Handle       resolved_appendix;
1693   Handle       resolved_method_type;
1694   methodHandle resolved_method =
1695     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1696                                                      bootstrap_specifier,
1697                                                      method_name, method_signature,
1698                                                      &resolved_appendix,
1699                                                      &resolved_method_type,
1700                                                      THREAD);
1701   wrap_invokedynamic_exception(CHECK);
1702   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1703   wrap_invokedynamic_exception(CHECK);
1704 }