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