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