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