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