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, const 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 Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 319                                                bool checkpolymorphism,
 320                                                bool in_imethod_resolve) {
 321   NoSafepointVerifier nsv;  // Method* returned may not be reclaimed
 322 
 323   Klass* klass = link_info.resolved_klass();
 324   Symbol* name = link_info.name();
 325   Symbol* signature = link_info.signature();
 326 
 327   // Ignore overpasses so statics can be found during resolution
 328   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 329 
 330   if (klass->is_array_klass()) {
 331     // Only consider klass and super klass for arrays
 332     return result;
 333   }
 334 
 335   InstanceKlass* ik = InstanceKlass::cast(klass);
 336 
 337    // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 338   // ignore static and non-public methods of java.lang.Object,
 339   // like clone, finalize, registerNatives.
 340   if (in_imethod_resolve &&
 341       result != NULL &&
 342       ik->is_interface() &&
 343       (result->is_static() || !result->is_public()) &&
 344       result->method_holder() == SystemDictionary::Object_klass()) {
 345     result = NULL;
 346   }
 347 
 348   // Before considering default methods, check for an overpass in the
 349   // current class if a method has not been found.
 350   if (result == NULL) {
 351     result = ik->find_method(name, signature);
 352   }
 353 
 354   if (result == NULL) {
 355     Array<Method*>* default_methods = ik->default_methods();
 356     if (default_methods != NULL) {
 357       result = InstanceKlass::find_method(default_methods, name, signature);
 358     }
 359   }
 360 
 361   if (checkpolymorphism && result != NULL) {
 362     vmIntrinsics::ID iid = result->intrinsic_id();
 363     if (MethodHandles::is_signature_polymorphic(iid)) {
 364       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 365       return NULL;
 366     }
 367   }
 368   return result;
 369 }
 370 
 371 // returns first instance method
 372 // Looks up method in classes, then looks up local default methods
 373 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 374                                                              Symbol* name,
 375                                                              Symbol* signature, TRAPS) {
 376   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 377 
 378   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 379     Klass* super_klass = result->method_holder()->super();
 380     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 381   }
 382 
 383   if (klass->is_array_klass()) {
 384     // Only consider klass and super klass for arrays
 385     return methodHandle(THREAD, result);
 386   }
 387 
 388   if (result == NULL) {
 389     Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
 390     if (default_methods != NULL) {
 391       result = InstanceKlass::find_method(default_methods, name, signature);
 392       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 393     }
 394   }
 395   return methodHandle(THREAD, result);
 396 }
 397 
 398 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
 399                                                    const methodHandle& resolved_method) {
 400 
 401   int vtable_index = Method::invalid_vtable_index;
 402   Symbol* name = resolved_method->name();
 403   Symbol* signature = resolved_method->signature();
 404   InstanceKlass* ik = InstanceKlass::cast(klass);
 405 
 406   // First check in default method array
 407   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 408     int index = InstanceKlass::find_method_index(ik->default_methods(),
 409                                                  name, signature, Klass::find_overpass,
 410                                                  Klass::find_static, Klass::find_private);
 411     if (index >= 0 ) {
 412       vtable_index = ik->default_vtable_indices()->at(index);
 413     }
 414   }
 415   if (vtable_index == Method::invalid_vtable_index) {
 416     // get vtable_index for miranda methods
 417     klassVtable vt = ik->vtable();
 418     vtable_index = vt.index_of_miranda(name, signature);
 419   }
 420   return vtable_index;
 421 }
 422 
 423 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
 424   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
 425 
 426   // Specify 'true' in order to skip default methods when searching the
 427   // interfaces.  Function lookup_method_in_klasses() already looked for
 428   // the method in the default methods table.
 429   return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), 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   // 3. lookup method in resolved klass and its super klasses
 717   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 718 
 719   // 4. lookup method in all the interfaces implemented by the resolved klass
 720   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 721     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 722 
 723     if (resolved_method.is_null()) {
 724       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 725       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 726       if (HAS_PENDING_EXCEPTION) {
 727         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 728         CLEAR_PENDING_EXCEPTION;
 729       }
 730     }
 731   }
 732 
 733   // 5. method lookup failed
 734   if (resolved_method.is_null()) {
 735     ResourceMark rm(THREAD);
 736     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 737                     Method::name_and_sig_as_C_string(resolved_klass,
 738                                                      link_info.name(),
 739                                                      link_info.signature()),
 740                     nested_exception, NULL);
 741   }
 742 
 743   // 6. access checks, access checking may be turned off when calling from within the VM.
 744   Klass* current_klass = link_info.current_klass();
 745   if (link_info.check_access()) {
 746     assert(current_klass != NULL , "current_klass should not be null");
 747 
 748     // check if method can be accessed by the referring class
 749     check_method_accessability(current_klass,
 750                                resolved_klass,
 751                                resolved_method->method_holder(),
 752                                resolved_method,
 753                                CHECK_NULL);
 754 
 755     // check loader constraints
 756     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 757   }
 758 
 759   // For private method invocation we should only find the method in the resolved class.
 760   // If that is not the case then we have a found a supertype method that we have nestmate
 761   // access to.
 762   // FIXME: the "ignoring xxx" part is for debugging only
 763   if (resolved_method->is_private() && resolved_method->method_holder() != resolved_klass) {
 764     ResourceMark rm(THREAD);
 765    DEBUG_ONLY(bool is_nestmate = InstanceKlass::cast(link_info.current_klass())->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD);)
 766     assert(is_nestmate, "was only expecting nestmates to get here!");
 767     Exceptions::fthrow(
 768       THREAD_AND_LOCATION,
 769       vmSymbols::java_lang_NoSuchMethodError(),
 770       "%s: method %s%s not found (ignoring %s)",
 771       resolved_klass->external_name(),
 772       resolved_method->name()->as_C_string(),
 773       resolved_method->signature()->as_C_string(),
 774       resolved_method->method_holder()->external_name()
 775     );
 776     return NULL;
 777   }
 778 
 779   return resolved_method;
 780 }
 781 
 782 static void trace_method_resolution(const char* prefix,
 783                                     Klass* klass,
 784                                     Klass* resolved_klass,
 785                                     const methodHandle& method,
 786                                     bool logitables,
 787                                     int index = -1) {
 788 #ifndef PRODUCT
 789   ResourceMark rm;
 790   Log(itables) logi;
 791   LogStream lsi(logi.trace());
 792   Log(vtables) logv;
 793   LogStream lsv(logv.trace());
 794   outputStream* st;
 795   if (logitables) {
 796     st = &lsi;
 797   } else {
 798     st = &lsv;
 799   }
 800   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 801             prefix,
 802             (klass == NULL ? "<NULL>" : klass->internal_name()),
 803             (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
 804             Method::name_and_sig_as_C_string(resolved_klass,
 805                                              method->name(),
 806                                              method->signature()),
 807             method->method_holder()->internal_name());
 808   method->print_linkage_flags(st);
 809   if (index != -1) {
 810     st->print("vtable_index:%d", index);
 811   }
 812   st->cr();
 813 #endif // PRODUCT
 814 }
 815 
 816 // FIXME: update to correct version
 817 #define VIRTUAL_PRIVATE_ACCESS_VERSION 53
 818 
 819 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 820 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 821 
 822   Klass* resolved_klass = link_info.resolved_klass();
 823 
 824   // check if klass is interface
 825   if (!resolved_klass->is_interface()) {
 826     ResourceMark rm(THREAD);
 827     char buf[200];
 828     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 829     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 830   }
 831 
 832   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 833   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 834     ResourceMark rm(THREAD);
 835     char buf[200];
 836     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 837     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 838   }
 839 
 840   // lookup method in this interface or its super, java.lang.Object
 841   // JDK8: also look for static methods
 842   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 843 
 844   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 845     // lookup method in all the super-interfaces
 846     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 847   }
 848 
 849   if (resolved_method.is_null()) {
 850     // no method found
 851     ResourceMark rm(THREAD);
 852     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 853                    Method::name_and_sig_as_C_string(resolved_klass,
 854                                                     link_info.name(),
 855                                                     link_info.signature()));
 856   }
 857 
 858   if (link_info.check_access()) {
 859     // JDK8 adds non-public interface methods, and accessability check requirement
 860     Klass* current_klass = link_info.current_klass();
 861 
 862     assert(current_klass != NULL , "current_klass should not be null");
 863 
 864     // check if method can be accessed by the referring class
 865     check_method_accessability(current_klass,
 866                                resolved_klass,
 867                                resolved_method->method_holder(),
 868                                resolved_method,
 869                                CHECK_NULL);
 870 
 871     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 872   }
 873 
 874   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 875     ResourceMark rm(THREAD);
 876     char buf[200];
 877     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 878                  Method::name_and_sig_as_C_string(resolved_klass,
 879                  resolved_method->name(), resolved_method->signature()));
 880     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 881   }
 882 
 883   if (code == Bytecodes::_invokeinterface && resolved_method->is_private()) {
 884     Klass* current_klass = link_info.current_klass();
 885     assert(current_klass != NULL, "current_klass should not be null for invokeinterface");
 886     if (InstanceKlass::cast(current_klass)->major_version() < VIRTUAL_PRIVATE_ACCESS_VERSION) {
 887       ResourceMark rm(THREAD);
 888       char buf[200];
 889       jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s, caller-class:%s",
 890                    Method::name_and_sig_as_C_string(resolved_klass,
 891                                                     resolved_method->name(),
 892                                                     resolved_method->signature()),
 893                    current_klass->internal_name());
 894       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 895     }
 896   }
 897 
 898   if (log_develop_is_enabled(Trace, itables)) {
 899     char buf[200];
 900     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 901                  Bytecodes::name(code));
 902     trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
 903                             resolved_method, true);
 904   }
 905 
 906   return resolved_method;
 907 }
 908 
 909 //------------------------------------------------------------------------------------------------------------------------
 910 // Field resolution
 911 
 912 void LinkResolver::check_field_accessability(Klass* ref_klass,
 913                                              Klass* resolved_klass,
 914                                              Klass* sel_klass,
 915                                              const fieldDescriptor& fd,
 916                                              TRAPS) {
 917   if (!Reflection::verify_field_access(ref_klass,
 918                                        resolved_klass,
 919                                        sel_klass,
 920                                        fd.access_flags(),
 921                                        true)) {
 922     ResourceMark rm(THREAD);
 923     Exceptions::fthrow(
 924       THREAD_AND_LOCATION,
 925       vmSymbols::java_lang_IllegalAccessError(),
 926       "tried to access field %s.%s from class %s",
 927       sel_klass->external_name(),
 928       fd.name()->as_C_string(),
 929       ref_klass->external_name()
 930     );
 931     return;
 932   }
 933 }
 934 
 935 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 936   LinkInfo link_info(pool, index, method, CHECK);
 937   resolve_field(fd, link_info, byte, true, CHECK);
 938 }
 939 
 940 void LinkResolver::resolve_field(fieldDescriptor& fd,
 941                                  const LinkInfo& link_info,
 942                                  Bytecodes::Code byte, bool initialize_class,
 943                                  TRAPS) {
 944   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 945          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 946          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 947          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 948 
 949   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 950   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 951   // Check if there's a resolved klass containing the field
 952   Klass* resolved_klass = link_info.resolved_klass();
 953   Symbol* field = link_info.name();
 954   Symbol* sig = link_info.signature();
 955 
 956   if (resolved_klass == NULL) {
 957     ResourceMark rm(THREAD);
 958     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 959   }
 960 
 961   // Resolve instance field
 962   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 963   // check if field exists; i.e., if a klass containing the field def has been selected
 964   if (sel_klass == NULL) {
 965     ResourceMark rm(THREAD);
 966     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 967   }
 968 
 969   if (!link_info.check_access())
 970     // Access checking may be turned off when calling from within the VM.
 971     return;
 972 
 973   // check access
 974   Klass* current_klass = link_info.current_klass();
 975   check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
 976 
 977   // check for errors
 978   if (is_static != fd.is_static()) {
 979     ResourceMark rm(THREAD);
 980     char msg[200];
 981     jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
 982     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
 983   }
 984 
 985   // A final field can be modified only
 986   // (1) by methods declared in the class declaring the field and
 987   // (2) by the <clinit> method (in case of a static field)
 988   //     or by the <init> method (in case of an instance field).
 989   if (is_put && fd.access_flags().is_final()) {
 990     ResourceMark rm(THREAD);
 991     stringStream ss;
 992 
 993     if (sel_klass != current_klass) {
 994       ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
 995                 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
 996                 current_klass->external_name());
 997       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
 998     }
 999 
1000     if (fd.constants()->pool_holder()->major_version() >= 53) {
1001       methodHandle m = link_info.current_method();
1002       assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
1003       bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1004                                                  fd.is_static() &&
1005                                                  !m()->is_static_initializer());
1006       bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1007                                                    !fd.is_static() &&
1008                                                    !m->is_object_initializer());
1009 
1010       if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1011         ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1012                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1013                  m()->name()->as_C_string(),
1014                  is_static ? "<clinit>" : "<init>");
1015         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1016       }
1017     }
1018   }
1019 
1020   // initialize resolved_klass if necessary
1021   // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1022   //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1023   //
1024   // note 2: we don't want to force initialization if we are just checking
1025   //         if the field access is legal; e.g., during compilation
1026   if (is_static && initialize_class) {
1027     sel_klass->initialize(CHECK);
1028   }
1029 
1030   if (sel_klass != current_klass) {
1031     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1032   }
1033 
1034   // return information. note that the klass is set to the actual klass containing the
1035   // field, otherwise access of static fields in superclasses will not work.
1036 }
1037 
1038 
1039 //------------------------------------------------------------------------------------------------------------------------
1040 // Invoke resolution
1041 //
1042 // Naming conventions:
1043 //
1044 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1045 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1046 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1047 // recv_klass         the receiver klass
1048 
1049 
1050 void LinkResolver::resolve_static_call(CallInfo& result,
1051                                        const LinkInfo& link_info,
1052                                        bool initialize_class, TRAPS) {
1053   methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
1054 
1055   // The resolved class can change as a result of this resolution.
1056   Klass* resolved_klass = resolved_method->method_holder();
1057 
1058   // Initialize klass (this should only happen if everything is ok)
1059   if (initialize_class && resolved_klass->should_be_initialized()) {
1060     resolved_klass->initialize(CHECK);
1061     // Use updated LinkInfo to reresolve with resolved method holder
1062     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1063                       link_info.current_klass(),
1064                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1065     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1066   }
1067 
1068   // setup result
1069   result.set_static(resolved_klass, resolved_method, CHECK);
1070 }
1071 
1072 // throws linktime exceptions
1073 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1074 
1075   Klass* resolved_klass = link_info.resolved_klass();
1076   methodHandle resolved_method;
1077   if (!resolved_klass->is_interface()) {
1078     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1079   } else {
1080     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1081   }
1082   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1083 
1084   // check if static
1085   if (!resolved_method->is_static()) {
1086     ResourceMark rm(THREAD);
1087     char buf[200];
1088     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1089                                                       resolved_method->name(),
1090                                                       resolved_method->signature()));
1091     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1092   }
1093   return resolved_method;
1094 }
1095 
1096 
1097 void LinkResolver::resolve_special_call(CallInfo& result,
1098                                         Handle recv,
1099                                         const LinkInfo& link_info,
1100                                         TRAPS) {
1101   methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1102   runtime_resolve_special_method(result, resolved_method,
1103                                  link_info.resolved_klass(),
1104                                  link_info.current_klass(),
1105                                  recv,
1106                                  link_info.check_access(), CHECK);
1107 }
1108 
1109 // throws linktime exceptions
1110 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1111                                                            TRAPS) {
1112 
1113   // Invokespecial is called for multiple special reasons:
1114   // <init>
1115   // local private method invocation, for classes and interfaces
1116   // superclass.method, which can also resolve to a default method
1117   // and the selected method is recalculated relative to the direct superclass
1118   // superinterface.method, which explicitly does not check shadowing
1119   Klass* resolved_klass = link_info.resolved_klass();
1120   methodHandle resolved_method;
1121 
1122   if (!resolved_klass->is_interface()) {
1123     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1124   } else {
1125     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1126   }
1127 
1128   // check if method name is <init>, that it is found in same klass as static type
1129   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1130       resolved_method->method_holder() != resolved_klass) {
1131     ResourceMark rm(THREAD);
1132     Exceptions::fthrow(
1133       THREAD_AND_LOCATION,
1134       vmSymbols::java_lang_NoSuchMethodError(),
1135       "%s: method %s%s not found",
1136       resolved_klass->external_name(),
1137       resolved_method->name()->as_C_string(),
1138       resolved_method->signature()->as_C_string()
1139     );
1140     return NULL;
1141   }
1142 
1143   // check that invokespecial's interface method reference is in a direct superinterface,
1144   // unless we are dealing with nestmates.
1145   Klass* current_klass = link_info.current_klass();
1146   if (current_klass != NULL && resolved_klass->is_interface()) {
1147     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1148     InstanceKlass *klass_to_check = !ck->is_anonymous() ?
1149                                     ck :
1150                                     InstanceKlass::cast(ck->host_klass());
1151     // Disable verification for the dynamically-generated reflection bytecodes.
1152     bool is_reflect = klass_to_check->is_subclass_of(
1153                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1154 
1155     if (!is_reflect &&
1156         !klass_to_check->is_same_or_direct_interface(resolved_klass) &&
1157         (ck == klass_to_check && // don't check nestmate access for anonymous classes
1158          !klass_to_check->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD))) {
1159       ResourceMark rm(THREAD);
1160       char buf[200];
1161       jio_snprintf(buf, sizeof(buf),
1162                    "Interface method reference: %s, is not in a direct superinterface of %s",
1163                    Method::name_and_sig_as_C_string(resolved_klass,
1164                                                                            resolved_method->name(),
1165                                                                            resolved_method->signature()),
1166                    current_klass->external_name());
1167       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1168     }
1169   }
1170 
1171   // check if not static
1172   if (resolved_method->is_static()) {
1173     ResourceMark rm(THREAD);
1174     char buf[200];
1175     jio_snprintf(buf, sizeof(buf),
1176                  "Expecting non-static method %s",
1177                  Method::name_and_sig_as_C_string(resolved_klass,
1178                                                   resolved_method->name(),
1179                                                   resolved_method->signature()));
1180     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1181   }
1182 
1183   // For private method invocation we should only find the method in the resolved class.
1184   // If that is not the case then we have a found a supertype method that we have nestmate
1185   // access to.
1186   // FIXME: the "ignoring xxx" part is for debugging only
1187   if (resolved_method->is_private() && resolved_method->method_holder() != resolved_klass) {
1188     ResourceMark rm(THREAD);
1189     DEBUG_ONLY(bool is_nestmate = InstanceKlass::cast(link_info.current_klass())->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD);)
1190     assert(is_nestmate, "was only expecting nestmates here!");
1191     Exceptions::fthrow(
1192       THREAD_AND_LOCATION,
1193       vmSymbols::java_lang_NoSuchMethodError(),
1194       "%s: method %s%s not found (ignoring %s)",
1195       resolved_klass->external_name(),
1196       resolved_method->name()->as_C_string(),
1197       resolved_method->signature()->as_C_string(),
1198       resolved_method->method_holder()->external_name()
1199     );
1200     return NULL;
1201   }
1202 
1203   if (log_develop_is_enabled(Trace, itables)) {
1204     trace_method_resolution("invokespecial resolved method: caller-class:",
1205                             current_klass, resolved_klass, resolved_method, true);
1206   }
1207 
1208   return resolved_method;
1209 }
1210 
1211 // throws runtime exceptions
1212 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1213                                                   const methodHandle& resolved_method,
1214                                                   Klass* resolved_klass,
1215                                                   Klass* current_klass,
1216                                                   Handle recv,
1217                                                   bool check_access, TRAPS) {
1218 
1219   // resolved method is selected method unless we have an old-style lookup
1220   // for a superclass method
1221   // Invokespecial for a superinterface, resolved method is selected method,
1222   // no checks for shadowing
1223   methodHandle sel_method(THREAD, resolved_method());
1224 
1225   if (check_access &&
1226       // check if the method is not <init>
1227       resolved_method->name() != vmSymbols::object_initializer_name()
1228       ) {
1229     // check if this is an old-style super call and do a new lookup if so
1230     // a) check if ACC_SUPER flag is set for the current class
1231     if ((current_klass->is_super() || !AllowNonVirtualCalls) &&
1232         // b) check if the class of the resolved_klass is a superclass
1233         // (not supertype in order to exclude interface classes) of the current class.
1234         // This check is not performed for super.invoke for interface methods
1235         // in super interfaces.
1236         current_klass->is_subclass_of(resolved_klass) &&
1237         current_klass != resolved_klass &&
1238         // c) check the method is not private - we don't re-resolve private methods
1239         !resolved_method->is_private()
1240         ) {
1241       // Lookup super method
1242       Klass* super_klass = current_klass->super();
1243       sel_method = lookup_instance_method_in_klasses(super_klass,
1244                            resolved_method->name(),
1245                            resolved_method->signature(), CHECK);
1246       // check if found
1247       if (sel_method.is_null()) {
1248         ResourceMark rm(THREAD);
1249         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1250                   Method::name_and_sig_as_C_string(resolved_klass,
1251                                             resolved_method->name(),
1252                                             resolved_method->signature()));
1253       }
1254     }
1255 
1256     // Check that the class of objectref (the receiver) is the current class or interface,
1257     // or a subtype of the current class or interface (the sender), or else they are
1258     // nestmates, otherwise invokespecial throws IllegalAccessError.
1259     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1260     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1261     // a class.  If the sender is an interface, the check has to be performed at runtime.
1262     InstanceKlass* cur_ik = InstanceKlass::cast(current_klass);
1263     InstanceKlass* sender = cur_ik->is_anonymous() ? cur_ik->host_klass() : cur_ik;
1264     if (sender->is_interface() && recv.not_null()) {
1265       Klass* receiver_klass = recv->klass();
1266       if (!receiver_klass->is_subtype_of(sender) &&
1267           (cur_ik == sender && // don't check nestmate access for anonymous classes
1268            // Note: we check the resolved class here, not the receiver class - the latter
1269            // may be a subtype of the resolved class defined outside the nest.
1270            !sender->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD))) {
1271         ResourceMark rm(THREAD);
1272         char buf[500];
1273         jio_snprintf(buf, sizeof(buf),
1274                      "Receiver class %s must be the current class or a subtype of interface %s",
1275                      receiver_klass->name()->as_C_string(),
1276                      sender->name()->as_C_string());
1277         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1278       }
1279     }
1280   }
1281 
1282   // check if not static
1283   if (sel_method->is_static()) {
1284     ResourceMark rm(THREAD);
1285     char buf[200];
1286     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1287                                                                                       resolved_method->name(),
1288                                                                                       resolved_method->signature()));
1289     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1290   }
1291 
1292   // check if abstract
1293   if (sel_method->is_abstract()) {
1294     ResourceMark rm(THREAD);
1295     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1296               Method::name_and_sig_as_C_string(resolved_klass,
1297                                                sel_method->name(),
1298                                                sel_method->signature()));
1299   }
1300 
1301   if (log_develop_is_enabled(Trace, itables)) {
1302     trace_method_resolution("invokespecial selected method: resolved-class:",
1303                             resolved_klass, resolved_klass, sel_method, true);
1304   }
1305 
1306   // setup result
1307   result.set_static(resolved_klass, sel_method, CHECK);
1308 }
1309 
1310 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1311                                         const LinkInfo& link_info,
1312                                         bool check_null_and_abstract, TRAPS) {
1313   methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1314   runtime_resolve_virtual_method(result, resolved_method,
1315                                  link_info.resolved_klass(),
1316                                  recv, receiver_klass,
1317                                  check_null_and_abstract, CHECK);
1318 }
1319 
1320 // throws linktime exceptions
1321 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1322                                                            TRAPS) {
1323   // normal method resolution
1324   methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1325 
1326   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1327   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1328 
1329   // check if private interface method
1330   Klass* resolved_klass = link_info.resolved_klass();
1331   Klass* current_klass = link_info.current_klass();
1332 
1333   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1334   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1335     ResourceMark rm(THREAD);
1336     char buf[200];
1337     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1338                  Method::name_and_sig_as_C_string(resolved_klass,
1339                                                   resolved_method->name(),
1340                                                   resolved_method->signature()),
1341                    (current_klass == NULL ? "<NULL>" : current_klass->internal_name()));
1342     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1343   }
1344 
1345   // check if not static
1346   if (resolved_method->is_static()) {
1347     ResourceMark rm(THREAD);
1348     char buf[200];
1349     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass,
1350                                                                                            resolved_method->name(),
1351                                                                                            resolved_method->signature()));
1352     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1353   }
1354 
1355   if (log_develop_is_enabled(Trace, vtables)) {
1356     trace_method_resolution("invokevirtual resolved method: caller-class:",
1357                             current_klass, resolved_klass, resolved_method, false);
1358   }
1359 
1360   return resolved_method;
1361 }
1362 
1363 // throws runtime exceptions
1364 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1365                                                   const methodHandle& resolved_method,
1366                                                   Klass* resolved_klass,
1367                                                   Handle recv,
1368                                                   Klass* recv_klass,
1369                                                   bool check_null_and_abstract,
1370                                                   TRAPS) {
1371 
1372   // setup default return values
1373   int vtable_index = Method::invalid_vtable_index;
1374   methodHandle selected_method;
1375 
1376   // runtime method resolution
1377   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1378     THROW(vmSymbols::java_lang_NullPointerException());
1379   }
1380 
1381   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1382   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1383   // a missing receiver might result in a bogus lookup.
1384   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1385 
1386   // do lookup based on receiver klass using the vtable index
1387   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1388     vtable_index = vtable_index_of_interface_method(resolved_klass,
1389                            resolved_method);
1390     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1391 
1392     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1393   } else {
1394     // at this point we are sure that resolved_method is virtual and not
1395     // a default or miranda method; therefore, it must have a valid vtable index.
1396     assert(!resolved_method->has_itable_index(), "");
1397     vtable_index = resolved_method->vtable_index();
1398     // We could get a negative vtable_index for final methods,
1399     // because as an optimization they are they are never put in the vtable,
1400     // unless they override an existing method.
1401     // If we do get a negative, it means the resolved method is the the selected
1402     // method, and it can never be changed by an override.
1403     if (vtable_index == Method::nonvirtual_vtable_index) {
1404       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1405       selected_method = resolved_method;
1406     } else {
1407       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1408     }
1409   }
1410 
1411   // check if method exists
1412   if (selected_method.is_null()) {
1413     ResourceMark rm(THREAD);
1414     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1415               Method::name_and_sig_as_C_string(resolved_klass,
1416                                                resolved_method->name(),
1417                                                resolved_method->signature()));
1418   }
1419 
1420   // check if abstract
1421   if (check_null_and_abstract && selected_method->is_abstract()) {
1422     ResourceMark rm(THREAD);
1423     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1424               Method::name_and_sig_as_C_string(resolved_klass,
1425                                                selected_method->name(),
1426                                                selected_method->signature()));
1427   }
1428 
1429   if (log_develop_is_enabled(Trace, vtables)) {
1430     trace_method_resolution("invokevirtual selected method: receiver-class:",
1431                             recv_klass, resolved_klass, selected_method,
1432                             false, vtable_index);
1433   }
1434   // setup result
1435   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1436 }
1437 
1438 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1439                                           const LinkInfo& link_info,
1440                                           bool check_null_and_abstract, TRAPS) {
1441   // throws linktime exceptions
1442   methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1443   runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1444                                    recv, recv_klass, check_null_and_abstract, CHECK);
1445 }
1446 
1447 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1448                                                              TRAPS) {
1449   // normal interface method resolution
1450   methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1451   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1452   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1453 
1454   return resolved_method;
1455 }
1456 
1457 // throws runtime exceptions
1458 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1459                                                     const methodHandle& resolved_method,
1460                                                     Klass* resolved_klass,
1461                                                     Handle recv,
1462                                                     Klass* recv_klass,
1463                                                     bool check_null_and_abstract, TRAPS) {
1464   // check if receiver exists
1465   if (check_null_and_abstract && recv.is_null()) {
1466     THROW(vmSymbols::java_lang_NullPointerException());
1467   }
1468 
1469   // check if receiver klass implements the resolved interface
1470   if (!recv_klass->is_subtype_of(resolved_klass)) {
1471     ResourceMark rm(THREAD);
1472     char buf[200];
1473     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1474                  recv_klass->external_name(),
1475                  resolved_klass->external_name());
1476     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1477   }
1478 
1479   methodHandle sel_method = resolved_method;
1480 
1481   // resolve the method in the receiver class, unless it is private
1482   if (!resolved_method()->is_private()) {
1483     // do lookup based on receiver klass
1484     // This search must match the linktime preparation search for itable initialization
1485     // to correctly enforce loader constraints for interface method inheritance
1486     sel_method = lookup_instance_method_in_klasses(recv_klass,
1487                                                    resolved_method->name(),
1488                                                    resolved_method->signature(), CHECK);
1489 
1490     if (sel_method.is_null() && !check_null_and_abstract) {
1491       // In theory this is a harmless placeholder value, but
1492       // in practice leaving in null affects the nsk default method tests.
1493       // This needs further study.
1494       sel_method = resolved_method;
1495     }
1496     // check if method exists
1497     if (sel_method.is_null()) {
1498       ResourceMark rm(THREAD);
1499       THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1500                 Method::name_and_sig_as_C_string(recv_klass,
1501                                                  resolved_method->name(),
1502                                                  resolved_method->signature()));
1503     }
1504     // check access
1505     // Throw Illegal Access Error if sel_method is not public.
1506     if (!sel_method->is_public()) {
1507       ResourceMark rm(THREAD);
1508       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1509                 Method::name_and_sig_as_C_string(recv_klass,
1510                                                  sel_method->name(),
1511                                                  sel_method->signature()));
1512     }
1513     // check if abstract
1514     if (check_null_and_abstract && sel_method->is_abstract()) {
1515       ResourceMark rm(THREAD);
1516       THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1517                 Method::name_and_sig_as_C_string(recv_klass,
1518                                                  sel_method->name(),
1519                                                  sel_method->signature()));
1520     }
1521   }
1522 
1523   if (log_develop_is_enabled(Trace, itables)) {
1524     trace_method_resolution("invokeinterface selected method: receiver-class:",
1525                             recv_klass, resolved_klass, sel_method, true);
1526   }
1527   // setup result
1528   if (!resolved_method->has_itable_index()) {
1529     int vtable_index = resolved_method->vtable_index();
1530     assert(vtable_index == sel_method->vtable_index(), "sanity check");
1531     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1532   } else {
1533     int itable_index = resolved_method()->itable_index();
1534     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1535   }
1536 }
1537 
1538 
1539 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1540                                                  const LinkInfo& link_info) {
1541   EXCEPTION_MARK;
1542   methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1543   if (HAS_PENDING_EXCEPTION) {
1544     CLEAR_PENDING_EXCEPTION;
1545     return methodHandle();
1546   } else {
1547     return method_result;
1548   }
1549 }
1550 
1551 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1552                                                  const LinkInfo& link_info) {
1553   EXCEPTION_MARK;
1554   methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1555   if (HAS_PENDING_EXCEPTION) {
1556     CLEAR_PENDING_EXCEPTION;
1557     return methodHandle();
1558   } else {
1559     return method_result;
1560   }
1561 }
1562 
1563 methodHandle LinkResolver::resolve_virtual_call_or_null(
1564                                                  Klass* receiver_klass,
1565                                                  const LinkInfo& link_info) {
1566   EXCEPTION_MARK;
1567   CallInfo info;
1568   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1569   if (HAS_PENDING_EXCEPTION) {
1570     CLEAR_PENDING_EXCEPTION;
1571     return methodHandle();
1572   }
1573   return info.selected_method();
1574 }
1575 
1576 methodHandle LinkResolver::resolve_interface_call_or_null(
1577                                                  Klass* receiver_klass,
1578                                                  const LinkInfo& link_info) {
1579   EXCEPTION_MARK;
1580   CallInfo info;
1581   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1582   if (HAS_PENDING_EXCEPTION) {
1583     CLEAR_PENDING_EXCEPTION;
1584     return methodHandle();
1585   }
1586   return info.selected_method();
1587 }
1588 
1589 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1590                                                const LinkInfo& link_info) {
1591   EXCEPTION_MARK;
1592   CallInfo info;
1593   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1594                        /*check_null_or_abstract*/false, THREAD);
1595   if (HAS_PENDING_EXCEPTION) {
1596     CLEAR_PENDING_EXCEPTION;
1597     return Method::invalid_vtable_index;
1598   }
1599   return info.vtable_index();
1600 }
1601 
1602 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1603   EXCEPTION_MARK;
1604   CallInfo info;
1605   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1606   if (HAS_PENDING_EXCEPTION) {
1607     CLEAR_PENDING_EXCEPTION;
1608     return methodHandle();
1609   }
1610   return info.selected_method();
1611 }
1612 
1613 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1614   EXCEPTION_MARK;
1615   CallInfo info;
1616   resolve_special_call(info, Handle(), link_info, THREAD);
1617   if (HAS_PENDING_EXCEPTION) {
1618     CLEAR_PENDING_EXCEPTION;
1619     return methodHandle();
1620   }
1621   return info.selected_method();
1622 }
1623 
1624 
1625 
1626 //------------------------------------------------------------------------------------------------------------------------
1627 // ConstantPool entries
1628 
1629 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1630   switch (byte) {
1631     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1632     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1633     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1634     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1635     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1636     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1637     default                         :                                                            break;
1638   }
1639   return;
1640 }
1641 
1642 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1643                              const methodHandle& attached_method,
1644                              Bytecodes::Code byte, TRAPS) {
1645   Klass* defc = attached_method->method_holder();
1646   Symbol* name = attached_method->name();
1647   Symbol* type = attached_method->signature();
1648   LinkInfo link_info(defc, name, type);
1649   switch(byte) {
1650     case Bytecodes::_invokevirtual:
1651       resolve_virtual_call(result, recv, recv->klass(), link_info,
1652                            /*check_null_and_abstract=*/true, CHECK);
1653       break;
1654     case Bytecodes::_invokeinterface:
1655       resolve_interface_call(result, recv, recv->klass(), link_info,
1656                              /*check_null_and_abstract=*/true, CHECK);
1657       break;
1658     case Bytecodes::_invokestatic:
1659       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1660       break;
1661     case Bytecodes::_invokespecial:
1662       resolve_special_call(result, recv, link_info, CHECK);
1663       break;
1664     default:
1665       fatal("bad call: %s", Bytecodes::name(byte));
1666       break;
1667   }
1668 }
1669 
1670 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1671   LinkInfo link_info(pool, index, CHECK);
1672   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1673 }
1674 
1675 
1676 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1677                                          const constantPoolHandle& pool, int index, TRAPS) {
1678   LinkInfo link_info(pool, index, CHECK);
1679   resolve_special_call(result, recv, link_info, CHECK);
1680 }
1681 
1682 
1683 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1684                                           const constantPoolHandle& pool, int index,
1685                                           TRAPS) {
1686 
1687   LinkInfo link_info(pool, index, CHECK);
1688   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1689   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1690 }
1691 
1692 
1693 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1694   LinkInfo link_info(pool, index, CHECK);
1695   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1696   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1697 }
1698 
1699 
1700 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1701   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1702   LinkInfo link_info(pool, index, CHECK);
1703   if (TraceMethodHandles) {
1704     ResourceMark rm(THREAD);
1705     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1706                   link_info.signature()->as_C_string());
1707   }
1708   resolve_handle_call(result, link_info, CHECK);
1709 }
1710 
1711 void LinkResolver::resolve_handle_call(CallInfo& result,
1712                                        const LinkInfo& link_info,
1713                                        TRAPS) {
1714   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1715   Klass* resolved_klass = link_info.resolved_klass();
1716   assert(resolved_klass == SystemDictionary::MethodHandle_klass() ||
1717          resolved_klass == SystemDictionary::VarHandle_klass(), "");
1718   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1719   Handle       resolved_appendix;
1720   Handle       resolved_method_type;
1721   methodHandle resolved_method = lookup_polymorphic_method(link_info,
1722                                        &resolved_appendix, &resolved_method_type, CHECK);
1723   result.set_handle(resolved_klass, resolved_method, resolved_appendix, resolved_method_type, CHECK);
1724 }
1725 
1726 static void wrap_invokedynamic_exception(TRAPS) {
1727   if (HAS_PENDING_EXCEPTION) {
1728     // See the "Linking Exceptions" section for the invokedynamic instruction
1729     // in JVMS 6.5.
1730     if (PENDING_EXCEPTION->is_a(SystemDictionary::Error_klass())) {
1731       // Pass through an Error, including BootstrapMethodError, any other form
1732       // of linkage error, or say ThreadDeath/OutOfMemoryError
1733       if (TraceMethodHandles) {
1734         tty->print_cr("invokedynamic passes through an Error for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1735         PENDING_EXCEPTION->print();
1736       }
1737       return;
1738     }
1739 
1740     // Otherwise wrap the exception in a BootstrapMethodError
1741     if (TraceMethodHandles) {
1742       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
1743       PENDING_EXCEPTION->print();
1744     }
1745     Handle nested_exception(THREAD, PENDING_EXCEPTION);
1746     CLEAR_PENDING_EXCEPTION;
1747     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1748   }
1749 }
1750 
1751 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1752   Symbol* method_name       = pool->name_ref_at(index);
1753   Symbol* method_signature  = pool->signature_ref_at(index);
1754   Klass* current_klass = pool->pool_holder();
1755 
1756   // Resolve the bootstrap specifier (BSM + optional arguments).
1757   Handle bootstrap_specifier;
1758   // Check if CallSite has been bound already:
1759   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1760   if (cpce->is_f1_null()) {
1761     int pool_index = cpce->constant_pool_index();
1762     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1763     wrap_invokedynamic_exception(CHECK);
1764     assert(bsm_info != NULL, "");
1765     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1766     bootstrap_specifier = Handle(THREAD, bsm_info);
1767   }
1768   if (!cpce->is_f1_null()) {
1769     methodHandle method(     THREAD, cpce->f1_as_method());
1770     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1771     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1772     result.set_handle(method, appendix, method_type, THREAD);
1773     wrap_invokedynamic_exception(CHECK);
1774     return;
1775   }
1776 
1777   if (TraceMethodHandles) {
1778     ResourceMark rm(THREAD);
1779     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1780                   ConstantPool::decode_invokedynamic_index(index),
1781                   method_name->as_C_string(), method_signature->as_C_string(),
1782                   current_klass->name()->as_C_string());
1783     tty->print("  BSM info: "); bootstrap_specifier->print();
1784   }
1785 
1786   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1787 }
1788 
1789 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1790                                         Handle bootstrap_specifier,
1791                                         Symbol* method_name, Symbol* method_signature,
1792                                         Klass* current_klass,
1793                                         TRAPS) {
1794   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1795   // The appendix argument is likely to be a freshly-created CallSite.
1796   Handle       resolved_appendix;
1797   Handle       resolved_method_type;
1798   methodHandle resolved_method =
1799     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1800                                                      bootstrap_specifier,
1801                                                      method_name, method_signature,
1802                                                      &resolved_appendix,
1803                                                      &resolved_method_type,
1804                                                      THREAD);
1805   wrap_invokedynamic_exception(CHECK);
1806   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1807   wrap_invokedynamic_exception(CHECK);
1808 }