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