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