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