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   bool can_access = Reflection::verify_member_access(ref_klass,
 587                                                      resolved_klass,
 588                                                      sel_klass,
 589                                                      flags,
 590                                                      true, false, CHECK);
 591   // Any existing exceptions that may have been thrown, for example LinkageErrors
 592   // from nest-host resolution, have been allowed to propagate.
 593   if (!can_access) {
 594     ResourceMark rm(THREAD);
 595     bool same_module = (sel_klass->module() == ref_klass->module());
 596     Exceptions::fthrow(
 597       THREAD_AND_LOCATION,
 598       vmSymbols::java_lang_IllegalAccessError(),
 599       "class %s tried to access method %s.%s%s (%s%s%s)",
 600       ref_klass->external_name(),
 601       sel_klass->external_name(),
 602       sel_method->name()->as_C_string(),
 603       sel_method->signature()->as_C_string(),
 604       (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 605       (same_module) ? "" : "; ",
 606       (same_module) ? "" : sel_klass->class_in_module_of_loader()
 607     );
 608     return;
 609   }
 610 }
 611 
 612 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 613                                                      const constantPoolHandle& pool, int index, TRAPS) {
 614   // This method is used only
 615   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 616   // and
 617   // (2) in Bytecode_invoke::static_target
 618   // It appears to fail when applied to an invokeinterface call site.
 619   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 620   // resolve klass
 621   if (code == Bytecodes::_invokedynamic) {
 622     Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
 623     Symbol* method_name = vmSymbols::invoke_name();
 624     Symbol* method_signature = pool->signature_ref_at(index);
 625     Klass*  current_klass = pool->pool_holder();
 626     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 627     return resolve_method(link_info, code, THREAD);
 628   }
 629 
 630   LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
 631   Klass* resolved_klass = link_info.resolved_klass();
 632 
 633   if (pool->has_preresolution()
 634       || (resolved_klass == SystemDictionary::MethodHandle_klass() &&
 635           MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
 636     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 637     if (result != NULL) {
 638       return methodHandle(THREAD, result);
 639     }
 640   }
 641 
 642   if (code == Bytecodes::_invokeinterface) {
 643     return resolve_interface_method(link_info, code, THREAD);
 644   } else if (code == Bytecodes::_invokevirtual) {
 645     return resolve_method(link_info, code, THREAD);
 646   } else if (!resolved_klass->is_interface()) {
 647     return resolve_method(link_info, code, THREAD);
 648   } else {
 649     return resolve_interface_method(link_info, code, THREAD);
 650   }
 651 }
 652 
 653 // Check and print a loader constraint violation message for method or interface method
 654 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 655                                                    const methodHandle& resolved_method,
 656                                                    const char* method_type, TRAPS) {
 657   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 658   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 659 
 660   ResourceMark rm(THREAD);
 661   Symbol* failed_type_symbol =
 662     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 663                                               resolved_loader, true, CHECK);
 664   if (failed_type_symbol != NULL) {
 665     Klass* current_class = link_info.current_klass();
 666     ClassLoaderData* current_loader_data = current_class->class_loader_data();
 667     assert(current_loader_data != NULL, "current class has no class loader data");
 668     Klass* resolved_method_class = resolved_method->method_holder();
 669     ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
 670     assert(target_loader_data != NULL, "resolved method's class has no class loader data");
 671 
 672     const char* msg = "loader constraint violation: when resolving %s"
 673       " \"%s\" the class loader %s of the current class, %s,"
 674       " and the class loader %s for the method's defining class, %s, have"
 675       " different Class objects for the type %s used in the signature (%s; %s)";
 676     char* sig = link_info.method_string();
 677     const char* loader1_name = current_loader_data->loader_name_and_id();
 678     char* current = current_class->name()->as_C_string();
 679     const char* current_class_description = current_class->class_in_module_of_loader(false,true);
 680     const char* loader2_name = target_loader_data->loader_name_and_id();
 681     char* target = resolved_method_class->name()->as_C_string();
 682     const char* target_class_description = resolved_method_class->class_in_module_of_loader(false,true);
 683     char* failed_type_name = failed_type_symbol->as_C_string();
 684     size_t buflen = strlen(msg) + strlen(sig) +
 685                     strlen(loader1_name) + strlen(current) + strlen(current_class_description) +
 686                     strlen(loader2_name) + strlen(target) + strlen(target_class_description) +
 687                     strlen(failed_type_name) + strlen(method_type) + 1;
 688     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 689     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
 690                  target, failed_type_name, current_class_description, target_class_description);
 691     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 692   }
 693 }
 694 
 695 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 696                                                   Klass* current_klass,
 697                                                   Klass* sel_klass, TRAPS) {
 698   Handle ref_loader(THREAD, current_klass->class_loader());
 699   Handle sel_loader(THREAD, sel_klass->class_loader());
 700 
 701   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 702   Symbol* failed_type_symbol =
 703     SystemDictionary::check_signature_loaders(sig,
 704                                               ref_loader, sel_loader,
 705                                               false,
 706                                               CHECK);
 707   if (failed_type_symbol != NULL) {
 708     const char* msg = "loader constraint violation: when resolving field"
 709       " \"%s\" of type %s, the class loader %s of the current class, "
 710       "%s, and the class loader %s for the field's defining "
 711       "type, %s, have different Class objects for type %s (%s; %s)";
 712     const char* field_name = field->as_C_string();
 713     const char* loader1_name = current_klass->class_loader_data()->loader_name_and_id();
 714     const char* curr_name = current_klass->external_name();
 715     const char* curr_class_description = current_klass->class_in_module_of_loader(false,true);
 716     const char* loader2_name = sel_klass->class_loader_data()->loader_name_and_id();
 717     const char* sel_name = sel_klass->external_name();
 718     const char* sel_class_description = sel_klass->class_in_module_of_loader(false,true);
 719     const char* failed_type_name = failed_type_symbol->as_klass_external_name();
 720     size_t buflen = strlen(msg) + strlen(field_name) + 2 * strlen(failed_type_name) +
 721                     strlen(loader1_name) + strlen(curr_name) + strlen(curr_class_description) +
 722                     strlen(loader2_name) + strlen(sel_name) + strlen(sel_class_description) + 1;
 723     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 724     jio_snprintf(buf, buflen, msg, field_name, failed_type_name, loader1_name,
 725                  curr_name, loader2_name, sel_name, failed_type_name,
 726                  curr_class_description, sel_class_description);
 727     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 728   }
 729 }
 730 
 731 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 732                                           Bytecodes::Code code, TRAPS) {
 733 
 734   Handle nested_exception;
 735   Klass* resolved_klass = link_info.resolved_klass();
 736 
 737   // 1. For invokevirtual, cannot call an interface method
 738   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 739     ResourceMark rm(THREAD);
 740     char buf[200];
 741     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 742         resolved_klass->external_name());
 743     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 744   }
 745 
 746   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 747   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 748     ResourceMark rm(THREAD);
 749     char buf[200];
 750     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 751     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 752   }
 753 
 754   // 3. lookup method in resolved klass and its super klasses
 755   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 756 
 757   // 4. lookup method in all the interfaces implemented by the resolved klass
 758   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 759     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 760 
 761     if (resolved_method.is_null()) {
 762       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 763       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 764       if (HAS_PENDING_EXCEPTION) {
 765         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 766         CLEAR_PENDING_EXCEPTION;
 767       }
 768     }
 769   }
 770 
 771   // 5. method lookup failed
 772   if (resolved_method.is_null()) {
 773     ResourceMark rm(THREAD);
 774     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 775                     Method::name_and_sig_as_C_string(resolved_klass,
 776                                                      link_info.name(),
 777                                                      link_info.signature()),
 778                     nested_exception, NULL);
 779   }
 780 
 781   // 6. access checks, access checking may be turned off when calling from within the VM.
 782   Klass* current_klass = link_info.current_klass();
 783   if (link_info.check_access()) {
 784     assert(current_klass != NULL , "current_klass should not be null");
 785 
 786     // check if method can be accessed by the referring class
 787     check_method_accessability(current_klass,
 788                                resolved_klass,
 789                                resolved_method->method_holder(),
 790                                resolved_method,
 791                                CHECK_NULL);
 792 
 793     // check loader constraints
 794     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 795   }
 796 
 797   // For private method invocation we should only find the method in the resolved class.
 798   // If that is not the case then we have a found a supertype method that we have nestmate
 799   // access to.
 800   if (resolved_method->is_private() && resolved_method->method_holder() != resolved_klass) {
 801     ResourceMark rm(THREAD);
 802     DEBUG_ONLY(bool is_nestmate = InstanceKlass::cast(link_info.current_klass())->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD);)
 803     assert(is_nestmate, "was only expecting nestmates to get here!");
 804     Exceptions::fthrow(
 805       THREAD_AND_LOCATION,
 806       vmSymbols::java_lang_NoSuchMethodError(),
 807       "%s: method %s%s not found",
 808       resolved_klass->external_name(),
 809       resolved_method->name()->as_C_string(),
 810       resolved_method->signature()->as_C_string()
 811     );
 812     return NULL;
 813   }
 814 
 815   return resolved_method;
 816 }
 817 
 818 static void trace_method_resolution(const char* prefix,
 819                                     Klass* klass,
 820                                     Klass* resolved_klass,
 821                                     const methodHandle& method,
 822                                     bool logitables,
 823                                     int index = -1) {
 824 #ifndef PRODUCT
 825   ResourceMark rm;
 826   Log(itables) logi;
 827   LogStream lsi(logi.trace());
 828   Log(vtables) logv;
 829   LogStream lsv(logv.trace());
 830   outputStream* st;
 831   if (logitables) {
 832     st = &lsi;
 833   } else {
 834     st = &lsv;
 835   }
 836   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 837             prefix,
 838             (klass == NULL ? "<NULL>" : klass->internal_name()),
 839             (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
 840             Method::name_and_sig_as_C_string(resolved_klass,
 841                                              method->name(),
 842                                              method->signature()),
 843             method->method_holder()->internal_name());
 844   method->print_linkage_flags(st);
 845   if (index != -1) {
 846     st->print("vtable_index:%d", index);
 847   }
 848   st->cr();
 849 #endif // PRODUCT
 850 }
 851 
 852 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 853 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 854 
 855   Klass* resolved_klass = link_info.resolved_klass();
 856 
 857   // check if klass is interface
 858   if (!resolved_klass->is_interface()) {
 859     ResourceMark rm(THREAD);
 860     char buf[200];
 861     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 862     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 863   }
 864 
 865   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 866   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 867     ResourceMark rm(THREAD);
 868     char buf[200];
 869     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 870     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 871   }
 872 
 873   // lookup method in this interface or its super, java.lang.Object
 874   // JDK8: also look for static methods
 875   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 876 
 877   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 878     // lookup method in all the super-interfaces
 879     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 880   }
 881 
 882   if (resolved_method.is_null()) {
 883     // no method found
 884     ResourceMark rm(THREAD);
 885     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 886                    Method::name_and_sig_as_C_string(resolved_klass,
 887                                                     link_info.name(),
 888                                                     link_info.signature()));
 889   }
 890 
 891   if (link_info.check_access()) {
 892     // JDK8 adds non-public interface methods, and accessability check requirement
 893     Klass* current_klass = link_info.current_klass();
 894 
 895     assert(current_klass != NULL , "current_klass should not be null");
 896 
 897     // check if method can be accessed by the referring class
 898     check_method_accessability(current_klass,
 899                                resolved_klass,
 900                                resolved_method->method_holder(),
 901                                resolved_method,
 902                                CHECK_NULL);
 903 
 904     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 905   }
 906 
 907   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 908     ResourceMark rm(THREAD);
 909     char buf[200];
 910     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 911                  Method::name_and_sig_as_C_string(resolved_klass,
 912                  resolved_method->name(), resolved_method->signature()));
 913     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 914   }
 915 
 916   if (log_develop_is_enabled(Trace, itables)) {
 917     char buf[200];
 918     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 919                  Bytecodes::name(code));
 920     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 921                             resolved_method, true);
 922   }
 923 
 924   return resolved_method;
 925 }
 926 
 927 //------------------------------------------------------------------------------------------------------------------------
 928 // Field resolution
 929 
 930 void LinkResolver::check_field_accessability(Klass* ref_klass,
 931                                              Klass* resolved_klass,
 932                                              Klass* sel_klass,
 933                                              const fieldDescriptor& fd,
 934                                              TRAPS) {
 935   bool can_access = Reflection::verify_member_access(ref_klass,
 936                                                      resolved_klass,
 937                                                      sel_klass,
 938                                                      fd.access_flags(),
 939                                                      true, false, CHECK);
 940   // Any existing exceptions that may have been thrown, for example LinkageErrors
 941   // from nest-host resolution, have been allowed to propagate.
 942   if (!can_access) {
 943     ResourceMark rm(THREAD);
 944     Exceptions::fthrow(
 945       THREAD_AND_LOCATION,
 946       vmSymbols::java_lang_IllegalAccessError(),
 947       "tried to access field %s.%s from class %s",
 948       sel_klass->external_name(),
 949       fd.name()->as_C_string(),
 950       ref_klass->external_name()
 951     );
 952     return;
 953   }
 954 }
 955 
 956 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 957   LinkInfo link_info(pool, index, method, CHECK);
 958   resolve_field(fd, link_info, byte, true, CHECK);
 959 }
 960 
 961 void LinkResolver::resolve_field(fieldDescriptor& fd,
 962                                  const LinkInfo& link_info,
 963                                  Bytecodes::Code byte, bool initialize_class,
 964                                  TRAPS) {
 965   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 966          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 967          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 968          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 969 
 970   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 971   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 972   // Check if there's a resolved klass containing the field
 973   Klass* resolved_klass = link_info.resolved_klass();
 974   Symbol* field = link_info.name();
 975   Symbol* sig = link_info.signature();
 976 
 977   if (resolved_klass == NULL) {
 978     ResourceMark rm(THREAD);
 979     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 980   }
 981 
 982   // Resolve instance field
 983   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 984   // check if field exists; i.e., if a klass containing the field def has been selected
 985   if (sel_klass == NULL) {
 986     ResourceMark rm(THREAD);
 987     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 988   }
 989 
 990   if (!link_info.check_access())
 991     // Access checking may be turned off when calling from within the VM.
 992     return;
 993 
 994   // check access
 995   Klass* current_klass = link_info.current_klass();
 996   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 997 
 998   // check for errors
 999   if (is_static != fd.is_static()) {
1000     ResourceMark rm(THREAD);
1001     char msg[200];
1002     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1003     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1004   }
1005 
1006   // A final field can be modified only
1007   // (1) by methods declared in the class declaring the field and
1008   // (2) by the <clinit> method (in case of a static field)
1009   //     or by the <init> method (in case of an instance field).
1010   if (is_put && fd.access_flags().is_final()) {
1011     ResourceMark rm(THREAD);
1012     stringStream ss;
1013 
1014     if (sel_klass != current_klass) {
1015       ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1016                 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1017                 current_klass->external_name());
1018       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1019     }
1020 
1021     if (fd.constants()->pool_holder()->major_version() >= 53) {
1022       methodHandle m = link_info.current_method();
1023       assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
1024       bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1025                                                  fd.is_static() &&
1026                                                  !m()->is_static_initializer());
1027       bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1028                                                    !fd.is_static() &&
1029                                                    !m->is_object_initializer());
1030 
1031       if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1032         ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1033                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1034                  m()->name()->as_C_string(),
1035                  is_static ? "<clinit>" : "<init>");
1036         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1037       }
1038     }
1039   }
1040 
1041   // initialize resolved_klass if necessary
1042   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1043   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1044   //
1045   // note 2: we don't want to force initialization if we are just checking
1046   //         if the field access is legal; e.g., during compilation
1047   if (is_static && initialize_class) {
1048     sel_klass->initialize(CHECK);
1049   }
1050 
1051   if (sel_klass != current_klass) {
1052     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1053   }
1054 
1055   // return information. note that the klass is set to the actual klass containing the
1056   // field, otherwise access of static fields in superclasses will not work.
1057 }
1058 
1059 
1060 //------------------------------------------------------------------------------------------------------------------------
1061 // Invoke resolution
1062 //
1063 // Naming conventions:
1064 //
1065 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1066 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1067 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1068 // recv_klass         the receiver klass
1069 
1070 
1071 void LinkResolver::resolve_static_call(CallInfo& result,
1072                                        const LinkInfo& link_info,
1073                                        bool initialize_class, TRAPS) {
1074   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
1075 
1076   // The resolved class can change as a result of this resolution.
1077   Klass* resolved_klass = resolved_method->method_holder();
1078 
1079   // Initialize klass (this should only happen if everything is ok)
1080   if (initialize_class && resolved_klass->should_be_initialized()) {
1081     resolved_klass->initialize(CHECK);
1082     // Use updated LinkInfo to reresolve with resolved method holder
1083     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1084                       link_info.current_klass(),
1085                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1086     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1087   }
1088 
1089   // setup result
1090   result.set_static(resolved_klass, resolved_method, CHECK);
1091 }
1092 
1093 // throws linktime exceptions
1094 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1095 
1096   Klass* resolved_klass = link_info.resolved_klass();
1097   methodHandle resolved_method;
1098   if (!resolved_klass->is_interface()) {
1099     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1100   } else {
1101     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1102   }
1103   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1104 
1105   // check if static
1106   if (!resolved_method->is_static()) {
1107     ResourceMark rm(THREAD);
1108     char buf[200];
1109     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1110                                                       resolved_method->name(),
1111                                                       resolved_method->signature()));
1112     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1113   }
1114   return resolved_method;
1115 }
1116 
1117 
1118 void LinkResolver::resolve_special_call(CallInfo& result,
1119                                         Handle recv,
1120                                         const LinkInfo& link_info,
1121                                         TRAPS) {
1122   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1123   runtime_resolve_special_method(result, link_info, resolved_method, recv, CHECK);
1124 }
1125 
1126 // throws linktime exceptions
1127 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1128                                                            TRAPS) {
1129 
1130   // Invokespecial is called for multiple special reasons:
1131   // <init>
1132   // local private method invocation, for classes and interfaces
1133   // superclass.method, which can also resolve to a default method
1134   // and the selected method is recalculated relative to the direct superclass
1135   // superinterface.method, which explicitly does not check shadowing
1136   Klass* resolved_klass = link_info.resolved_klass();
1137   methodHandle resolved_method;
1138 
1139   if (!resolved_klass->is_interface()) {
1140     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1141   } else {
1142     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1143   }
1144 
1145   // check if method name is <init>, that it is found in same klass as static type
1146   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1147       resolved_method->method_holder() != resolved_klass) {
1148     ResourceMark rm(THREAD);
1149     Exceptions::fthrow(
1150       THREAD_AND_LOCATION,
1151       vmSymbols::java_lang_NoSuchMethodError(),
1152       "%s: method %s%s not found",
1153       resolved_klass->external_name(),
1154       resolved_method->name()->as_C_string(),
1155       resolved_method->signature()->as_C_string()
1156     );
1157     return NULL;
1158   }
1159 
1160   // ensure that invokespecial's interface method reference is in
1161   // a direct superinterface, not an indirect superinterface
1162   Klass* current_klass = link_info.current_klass();
1163   if (current_klass != NULL && resolved_klass->is_interface()) {
1164     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1165     InstanceKlass *klass_to_check = !ck->is_anonymous() ?
1166                                     ck :
1167                                     InstanceKlass::cast(ck->host_klass());
1168     // Disable verification for the dynamically-generated reflection bytecodes.
1169     bool is_reflect = klass_to_check->is_subclass_of(
1170                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1171 
1172     if (!is_reflect &&
1173         !klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1174       ResourceMark rm(THREAD);
1175       char buf[200];
1176       jio_snprintf(buf, sizeof(buf),
1177                    "Interface method reference: %s, is in an indirect superinterface of %s",
1178                    Method::name_and_sig_as_C_string(resolved_klass,
1179                                                                            resolved_method->name(),
1180                                                                            resolved_method->signature()),
1181                    current_klass->external_name());
1182       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1183     }
1184   }
1185 
1186   // check if not static
1187   if (resolved_method->is_static()) {
1188     ResourceMark rm(THREAD);
1189     char buf[200];
1190     jio_snprintf(buf, sizeof(buf),
1191                  "Expecting non-static method %s",
1192                  Method::name_and_sig_as_C_string(resolved_klass,
1193                                                   resolved_method->name(),
1194                                                   resolved_method->signature()));
1195     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1196   }
1197 
1198   if (log_develop_is_enabled(Trace, itables)) {
1199     trace_method_resolution("invokespecial resolved method: caller-class:",
1200                             current_klass, resolved_klass, resolved_method, true);
1201   }
1202 
1203   return resolved_method;
1204 }
1205 
1206 // throws runtime exceptions
1207 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1208                                                   const LinkInfo& link_info,
1209                                                   const methodHandle& resolved_method,
1210                                                   Handle recv, TRAPS) {
1211 
1212   Klass* resolved_klass = link_info.resolved_klass();
1213 
1214   // resolved method is selected method unless we have an old-style lookup
1215   // for a superclass method
1216   // Invokespecial for a superinterface, resolved method is selected method,
1217   // no checks for shadowing
1218   methodHandle sel_method(THREAD, resolved_method());
1219 
1220   if (link_info.check_access() &&
1221       // check if the method is not <init>
1222       resolved_method->name() != vmSymbols::object_initializer_name()) {
1223 
1224      // check if this is an old-style super call and do a new lookup if so
1225      // a) check if ACC_SUPER flag is set for the current class
1226     Klass* current_klass = link_info.current_klass();
1227     if ((current_klass->is_super() || !AllowNonVirtualCalls) &&
1228         // b) check if the class of the resolved_klass is a superclass
1229         // (not supertype in order to exclude interface classes) of the current class.
1230         // This check is not performed for super.invoke for interface methods
1231         // in super interfaces.
1232         current_klass->is_subclass_of(resolved_klass) &&
1233         current_klass != resolved_klass
1234         ) {
1235       // Lookup super method
1236       Klass* super_klass = current_klass->super();
1237       sel_method = lookup_instance_method_in_klasses(super_klass,
1238                                                      resolved_method->name(),
1239                                                      resolved_method->signature(),
1240                                                      Klass::find_private, CHECK);
1241       // check if found
1242       if (sel_method.is_null()) {
1243         ResourceMark rm(THREAD);
1244         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1245                   Method::name_and_sig_as_C_string(resolved_klass,
1246                                             resolved_method->name(),
1247                                             resolved_method->signature()));
1248       // check loader constraints if found a different method
1249       } else if (sel_method() != resolved_method()) {
1250         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1251       }
1252     }
1253 
1254     // Check that the class of objectref (the receiver) is the current class or interface,
1255     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1256     // throws IllegalAccessError.
1257     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1258     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1259     // a class.  If the sender is an interface, the check has to be performed at runtime.
1260     InstanceKlass* sender = InstanceKlass::cast(current_klass);
1261     sender = sender->is_anonymous() ? sender->host_klass() : sender;
1262     if (sender->is_interface() && recv.not_null()) {
1263       Klass* receiver_klass = recv->klass();
1264       if (!receiver_klass->is_subtype_of(sender)) {
1265         ResourceMark rm(THREAD);
1266         char buf[500];
1267         jio_snprintf(buf, sizeof(buf),
1268                      "Receiver class %s must be the current class or a subtype of interface %s",
1269                      receiver_klass->name()->as_C_string(),
1270                      sender->name()->as_C_string());
1271         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1272       }
1273     }
1274   }
1275 
1276   // check if not static
1277   if (sel_method->is_static()) {
1278     ResourceMark rm(THREAD);
1279     char buf[200];
1280     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1281                                                                                       resolved_method->name(),
1282                                                                                       resolved_method->signature()));
1283     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1284   }
1285 
1286   // check if abstract
1287   if (sel_method->is_abstract()) {
1288     ResourceMark rm(THREAD);
1289     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1290               Method::name_and_sig_as_C_string(resolved_klass,
1291                                                sel_method->name(),
1292                                                sel_method->signature()));
1293   }
1294 
1295   if (log_develop_is_enabled(Trace, itables)) {
1296     trace_method_resolution("invokespecial selected method: resolved-class:",
1297                             resolved_klass, resolved_klass, sel_method, true);
1298   }
1299 
1300   // setup result
1301   result.set_static(resolved_klass, sel_method, CHECK);
1302 }
1303 
1304 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1305                                         const LinkInfo& link_info,
1306                                         bool check_null_and_abstract, TRAPS) {
1307   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1308   runtime_resolve_virtual_method(result, resolved_method,
1309                                  link_info.resolved_klass(),
1310                                  recv, receiver_klass,
1311                                  check_null_and_abstract, CHECK);
1312 }
1313 
1314 // throws linktime exceptions
1315 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1316                                                            TRAPS) {
1317   // normal method resolution
1318   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1319 
1320   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1321   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1322 
1323   // check if private interface method
1324   Klass* resolved_klass = link_info.resolved_klass();
1325   Klass* current_klass = link_info.current_klass();
1326 
1327   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1328   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1329     ResourceMark rm(THREAD);
1330     char buf[200];
1331     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1332                  Method::name_and_sig_as_C_string(resolved_klass,
1333                                                   resolved_method->name(),
1334                                                   resolved_method->signature()),
1335                    (current_klass == NULL ? "<NULL>" : current_klass->internal_name()));
1336     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1337   }
1338 
1339   // check if not static
1340   if (resolved_method->is_static()) {
1341     ResourceMark rm(THREAD);
1342     char buf[200];
1343     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1344                                                                                            resolved_method->name(),
1345                                                                                            resolved_method->signature()));
1346     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1347   }
1348 
1349   if (log_develop_is_enabled(Trace, vtables)) {
1350     trace_method_resolution("invokevirtual resolved method: caller-class:",
1351                             current_klass, resolved_klass, resolved_method, false);
1352   }
1353 
1354   return resolved_method;
1355 }
1356 
1357 // throws runtime exceptions
1358 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1359                                                   const methodHandle& resolved_method,
1360                                                   Klass* resolved_klass,
1361                                                   Handle recv,
1362                                                   Klass* recv_klass,
1363                                                   bool check_null_and_abstract,
1364                                                   TRAPS) {
1365 
1366   // setup default return values
1367   int vtable_index = Method::invalid_vtable_index;
1368   methodHandle selected_method;
1369 
1370   // runtime method resolution
1371   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1372     THROW(vmSymbols::java_lang_NullPointerException());
1373   }
1374 
1375   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1376   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1377   // a missing receiver might result in a bogus lookup.
1378   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1379 
1380   // do lookup based on receiver klass using the vtable index
1381   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1382     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1383     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1384 
1385     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1386   } else {
1387     // at this point we are sure that resolved_method is virtual and not
1388     // a default or miranda method; therefore, it must have a valid vtable index.
1389     assert(!resolved_method->has_itable_index(), "");
1390     vtable_index = resolved_method->vtable_index();
1391     // We could get a negative vtable_index of nonvirtual_vtable_index for private
1392     // methods, or for final methods. Private methods never appear in the vtable
1393     // and never override other methods. As an optimization, final methods are
1394     // never put in the vtable, unless they override an existing method.
1395     // So if we do get nonvirtual_vtable_index, it means the selected method is the
1396     // resolved method, and it can never be changed by an override.
1397     if (vtable_index == Method::nonvirtual_vtable_index) {
1398       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1399       selected_method = resolved_method;
1400     } else {
1401       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1402     }
1403   }
1404 
1405   // check if method exists
1406   if (selected_method.is_null()) {
1407     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1408   }
1409 
1410   // check if abstract
1411   if (check_null_and_abstract && selected_method->is_abstract()) {
1412     // Pass arguments for generating a verbose error message.
1413     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1414   }
1415 
1416   if (log_develop_is_enabled(Trace, vtables)) {
1417     trace_method_resolution("invokevirtual selected method: receiver-class:",
1418                             recv_klass, resolved_klass, selected_method,
1419                             false, vtable_index);
1420   }
1421   // setup result
1422   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1423 }
1424 
1425 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1426                                           const LinkInfo& link_info,
1427                                           bool check_null_and_abstract, TRAPS) {
1428   // throws linktime exceptions
1429   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1430   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1431                                    recv, recv_klass, check_null_and_abstract, CHECK);
1432 }
1433 
1434 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1435                                                              TRAPS) {
1436   // normal interface method resolution
1437   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1438   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1439   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1440 
1441   return resolved_method;
1442 }
1443 
1444 // throws runtime exceptions
1445 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1446                                                     const methodHandle& resolved_method,
1447                                                     Klass* resolved_klass,
1448                                                     Handle recv,
1449                                                     Klass* recv_klass,
1450                                                     bool check_null_and_abstract, TRAPS) {
1451 
1452   // check if receiver exists
1453   if (check_null_and_abstract && recv.is_null()) {
1454     THROW(vmSymbols::java_lang_NullPointerException());
1455   }
1456 
1457   // check if receiver klass implements the resolved interface
1458   if (!recv_klass->is_subtype_of(resolved_klass)) {
1459     ResourceMark rm(THREAD);
1460     char buf[200];
1461     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1462                  recv_klass->external_name(),
1463                  resolved_klass->external_name());
1464     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1465   }
1466 
1467   methodHandle selected_method = resolved_method;
1468 
1469   // resolve the method in the receiver class, unless it is private
1470   if (!resolved_method()->is_private()) {
1471     // do lookup based on receiver klass
1472     // This search must match the linktime preparation search for itable initialization
1473     // to correctly enforce loader constraints for interface method inheritance.
1474     // Private methods are skipped as the resolved method was not private.
1475     selected_method = lookup_instance_method_in_klasses(recv_klass,
1476                                                         resolved_method->name(),
1477                                                         resolved_method->signature(),
1478                                                         Klass::skip_private, CHECK);
1479 
1480     if (selected_method.is_null() && !check_null_and_abstract) {
1481       // In theory this is a harmless placeholder value, but
1482       // in practice leaving in null affects the nsk default method tests.
1483       // This needs further study.
1484       selected_method = resolved_method;
1485     }
1486     // check if method exists
1487     if (selected_method.is_null()) {
1488       // Pass arguments for generating a verbose error message.
1489       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1490     }
1491     // check access
1492     // Throw Illegal Access Error if selected_method is not public.
1493     if (!selected_method->is_public()) {
1494       ResourceMark rm(THREAD);
1495       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1496                 Method::name_and_sig_as_C_string(recv_klass,
1497                                                  selected_method->name(),
1498                                                  selected_method->signature()));
1499     }
1500     // check if abstract
1501     if (check_null_and_abstract && selected_method->is_abstract()) {
1502       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1503     }
1504   }
1505 
1506   if (log_develop_is_enabled(Trace, itables)) {
1507     trace_method_resolution("invokeinterface selected method: receiver-class:",
1508                             recv_klass, resolved_klass, selected_method, true);
1509   }
1510   // setup result
1511   if (resolved_method->has_vtable_index()) {
1512     int vtable_index = resolved_method->vtable_index();
1513     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1514     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1515     result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1516   } else if (resolved_method->has_itable_index()) {
1517     int itable_index = resolved_method()->itable_index();
1518     log_develop_trace(itables)("  -- itable index: %d", itable_index);
1519     result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK);
1520   } else {
1521     int index = resolved_method->vtable_index();
1522     log_develop_trace(itables)("  -- non itable/vtable index: %d", index);
1523     assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1524     assert(resolved_method()->is_private() ||
1525            (resolved_method()->is_final() && resolved_method->method_holder() == SystemDictionary::Object_klass()),
1526            "Should only have non-virtual invokeinterface for private or final-Object methods!");
1527     assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1528     // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1529     result.set_virtual(resolved_klass, resolved_klass, resolved_method, resolved_method, index, CHECK);
1530   }
1531 }
1532 
1533 
1534 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1535                                                  const LinkInfo& link_info) {
1536   EXCEPTION_MARK;
1537   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1538   if (HAS_PENDING_EXCEPTION) {
1539     CLEAR_PENDING_EXCEPTION;
1540     return methodHandle();
1541   } else {
1542     return method_result;
1543   }
1544 }
1545 
1546 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1547                                                  const LinkInfo& link_info) {
1548   EXCEPTION_MARK;
1549   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1550   if (HAS_PENDING_EXCEPTION) {
1551     CLEAR_PENDING_EXCEPTION;
1552     return methodHandle();
1553   } else {
1554     return method_result;
1555   }
1556 }
1557 
1558 methodHandle LinkResolver::resolve_virtual_call_or_null(
1559                                                  Klass* receiver_klass,
1560                                                  const LinkInfo& link_info) {
1561   EXCEPTION_MARK;
1562   CallInfo info;
1563   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1564   if (HAS_PENDING_EXCEPTION) {
1565     CLEAR_PENDING_EXCEPTION;
1566     return methodHandle();
1567   }
1568   return info.selected_method();
1569 }
1570 
1571 methodHandle LinkResolver::resolve_interface_call_or_null(
1572                                                  Klass* receiver_klass,
1573                                                  const LinkInfo& link_info) {
1574   EXCEPTION_MARK;
1575   CallInfo info;
1576   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1577   if (HAS_PENDING_EXCEPTION) {
1578     CLEAR_PENDING_EXCEPTION;
1579     return methodHandle();
1580   }
1581   return info.selected_method();
1582 }
1583 
1584 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1585                                                const LinkInfo& link_info) {
1586   EXCEPTION_MARK;
1587   CallInfo info;
1588   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1589                        /*check_null_or_abstract*/false, THREAD);
1590   if (HAS_PENDING_EXCEPTION) {
1591     CLEAR_PENDING_EXCEPTION;
1592     return Method::invalid_vtable_index;
1593   }
1594   return info.vtable_index();
1595 }
1596 
1597 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1598   EXCEPTION_MARK;
1599   CallInfo info;
1600   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1601   if (HAS_PENDING_EXCEPTION) {
1602     CLEAR_PENDING_EXCEPTION;
1603     return methodHandle();
1604   }
1605   return info.selected_method();
1606 }
1607 
1608 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1609   EXCEPTION_MARK;
1610   CallInfo info;
1611   resolve_special_call(info, Handle(), link_info, THREAD);
1612   if (HAS_PENDING_EXCEPTION) {
1613     CLEAR_PENDING_EXCEPTION;
1614     return methodHandle();
1615   }
1616   return info.selected_method();
1617 }
1618 
1619 
1620 
1621 //------------------------------------------------------------------------------------------------------------------------
1622 // ConstantPool entries
1623 
1624 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1625   switch (byte) {
1626     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1627     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1628     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1629     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1630     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1631     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1632     default                         :                                                            break;
1633   }
1634   return;
1635 }
1636 
1637 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1638                              const methodHandle& attached_method,
1639                              Bytecodes::Code byte, TRAPS) {
1640   Klass* defc = attached_method->method_holder();
1641   Symbol* name = attached_method->name();
1642   Symbol* type = attached_method->signature();
1643   LinkInfo link_info(defc, name, type);
1644   switch(byte) {
1645     case Bytecodes::_invokevirtual:
1646       resolve_virtual_call(result, recv, recv->klass(), link_info,
1647                            /*check_null_and_abstract=*/true, CHECK);
1648       break;
1649     case Bytecodes::_invokeinterface:
1650       resolve_interface_call(result, recv, recv->klass(), link_info,
1651                              /*check_null_and_abstract=*/true, CHECK);
1652       break;
1653     case Bytecodes::_invokestatic:
1654       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1655       break;
1656     case Bytecodes::_invokespecial:
1657       resolve_special_call(result, recv, link_info, CHECK);
1658       break;
1659     default:
1660       fatal("bad call: %s", Bytecodes::name(byte));
1661       break;
1662   }
1663 }
1664 
1665 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1666   LinkInfo link_info(pool, index, CHECK);
1667   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1668 }
1669 
1670 
1671 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1672                                          const constantPoolHandle& pool, int index, TRAPS) {
1673   LinkInfo link_info(pool, index, CHECK);
1674   resolve_special_call(result, recv, link_info, CHECK);
1675 }
1676 
1677 
1678 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1679                                           const constantPoolHandle& pool, int index,
1680                                           TRAPS) {
1681 
1682   LinkInfo link_info(pool, index, CHECK);
1683   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1684   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1685 }
1686 
1687 
1688 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1689   LinkInfo link_info(pool, index, CHECK);
1690   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1691   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1692 }
1693 
1694 
1695 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1696   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1697   LinkInfo link_info(pool, index, CHECK);
1698   if (TraceMethodHandles) {
1699     ResourceMark rm(THREAD);
1700     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1701                   link_info.signature()->as_C_string());
1702   }
1703   resolve_handle_call(result, link_info, CHECK);
1704 }
1705 
1706 void LinkResolver::resolve_handle_call(CallInfo& result,
1707                                        const LinkInfo& link_info,
1708                                        TRAPS) {
1709   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1710   Klass* resolved_klass = link_info.resolved_klass();
1711   assert(resolved_klass == SystemDictionary::MethodHandle_klass() ||
1712          resolved_klass == SystemDictionary::VarHandle_klass(), "");
1713   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1714   Handle       resolved_appendix;
1715   Handle       resolved_method_type;
1716   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1717                                        &resolved_appendix, &resolved_method_type, CHECK);
1718   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1719 }
1720 
1721 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1722   Symbol* method_name       = pool->name_ref_at(index);
1723   Symbol* method_signature  = pool->signature_ref_at(index);
1724   Klass* current_klass = pool->pool_holder();
1725 
1726   // Resolve the bootstrap specifier (BSM + optional arguments).
1727   Handle bootstrap_specifier;
1728   // Check if CallSite has been bound already:
1729   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1730   int pool_index = cpce->constant_pool_index();
1731 
1732   if (cpce->is_f1_null()) {
1733     if (cpce->indy_resolution_failed()) {
1734       ConstantPool::throw_resolution_error(pool,
1735                                            ResolutionErrorTable::encode_cpcache_index(index),
1736                                            CHECK);
1737     }
1738 
1739     // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1740     // reference to a method handle which will be the bootstrap method for a dynamic
1741     // call site.  If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1742     // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1743     // method's CP index for the CONSTANT_MethodHandle_info.  So, there is no need to
1744     // set the indy_rf flag since any subsequent invokedynamic instruction which shares
1745     // this bootstrap method will encounter the resolution of MethodHandleInError.
1746     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1747     Exceptions::wrap_dynamic_exception(CHECK);
1748     assert(bsm_info != NULL, "");
1749     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1750     bootstrap_specifier = Handle(THREAD, bsm_info);
1751   }
1752   if (!cpce->is_f1_null()) {
1753     methodHandle method(     THREAD, cpce->f1_as_method());
1754     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1755     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1756     result.set_handle(method, appendix, method_type, THREAD);
1757     Exceptions::wrap_dynamic_exception(CHECK);
1758     return;
1759   }
1760 
1761   if (TraceMethodHandles) {
1762     ResourceMark rm(THREAD);
1763     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1764                   ConstantPool::decode_invokedynamic_index(index),
1765                   method_name->as_C_string(), method_signature->as_C_string(),
1766                   current_klass->name()->as_C_string());
1767     tty->print("  BSM info: "); bootstrap_specifier->print();
1768   }
1769 
1770   resolve_dynamic_call(result, pool_index, bootstrap_specifier, method_name,
1771                        method_signature, current_klass, THREAD);
1772   if (HAS_PENDING_EXCEPTION && PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1773     int encoded_index = ResolutionErrorTable::encode_cpcache_index(index);
1774     bool recorded_res_status = cpce->save_and_throw_indy_exc(pool, pool_index,
1775                                                              encoded_index,
1776                                                              pool()->tag_at(pool_index),
1777                                                              CHECK);
1778     if (!recorded_res_status) {
1779       // Another thread got here just before we did.  So, either use the method
1780       // that it resolved or throw the LinkageError exception that it threw.
1781       if (!cpce->is_f1_null()) {
1782         methodHandle method(     THREAD, cpce->f1_as_method());
1783         Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1784         Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1785         result.set_handle(method, appendix, method_type, THREAD);
1786         Exceptions::wrap_dynamic_exception(CHECK);
1787       } else {
1788         assert(cpce->indy_resolution_failed(), "Resolution failure flag not set");
1789         ConstantPool::throw_resolution_error(pool, encoded_index, CHECK);
1790       }
1791       return;
1792     }
1793     assert(cpce->indy_resolution_failed(), "Resolution failure flag wasn't set");
1794   }
1795 }
1796 
1797 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1798                                         int pool_index,
1799                                         Handle bootstrap_specifier,
1800                                         Symbol* method_name, Symbol* method_signature,
1801                                         Klass* current_klass,
1802                                         TRAPS) {
1803   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1804   // The appendix argument is likely to be a freshly-created CallSite.
1805   Handle       resolved_appendix;
1806   Handle       resolved_method_type;
1807   methodHandle resolved_method =
1808     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1809                                                      pool_index,
1810                                                      bootstrap_specifier,
1811                                                      method_name, method_signature,
1812                                                      &resolved_appendix,
1813                                                      &resolved_method_type,
1814                                                      THREAD);
1815   Exceptions::wrap_dynamic_exception(CHECK);
1816   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1817   Exceptions::wrap_dynamic_exception(CHECK);
1818 }
1819 
1820 // Selected method is abstract.
1821 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1822                                                const methodHandle& selected_method,
1823                                                Klass *recv_klass, TRAPS) {
1824   Klass *resolved_klass = resolved_method->method_holder();
1825   ResourceMark rm(THREAD);
1826   stringStream ss;
1827 
1828   if (recv_klass != NULL) {
1829     ss.print("Receiver class %s does not define or inherit an "
1830              "implementation of the",
1831              recv_klass->external_name());
1832   } else {
1833     ss.print("Missing implementation of");
1834   }
1835 
1836   assert(resolved_method.not_null(), "Sanity");
1837   ss.print(" resolved method %s%s%s%s of %s %s.",
1838            resolved_method->is_abstract() ? "abstract " : "",
1839            resolved_method->is_private()  ? "private "  : "",
1840            resolved_method->name()->as_C_string(),
1841            resolved_method->signature()->as_C_string(),
1842            resolved_klass->external_kind(),
1843            resolved_klass->external_name());
1844 
1845   if (selected_method.not_null() && !(resolved_method == selected_method)) {
1846     ss.print(" Selected method is %s%s%s.",
1847              selected_method->is_abstract() ? "abstract " : "",
1848              selected_method->is_private()  ? "private "  : "",
1849              selected_method->name_and_sig_as_C_string());
1850   }
1851 
1852   THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1853 }