src/share/vm/interpreter/linkResolver.cpp

Mon, 14 May 2018 09:16:44 -0400

author
hseigel
date
Mon, 14 May 2018 09:16:44 -0400
changeset 9403
3af740792979
parent 9008
432f92e99174
child 9448
73d689add964
permissions
-rw-r--r--

8199226: Improve field accesses
Reviewed-by: acorn, ahgross, rhalade
Contributed-by: harold.seigel@oracle.com

     1 /*
     2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    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"
    50 //------------------------------------------------------------------------------------------------------------------------
    51 // Implementation of CallInfo
    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 }
    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 }
    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 }
    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 }
    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
   110   if (CompilationPolicy::must_be_compiled(selected_method)) {
   111     // This path is unusual, mostly used by the '-Xcomp' stress test mode.
   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 }
   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");
   162     kind = CallInfo::vtable_call;
   163   } else if (resolved_method->has_vtable_index()) {
   164     // Can occur if an interface redeclares a method of Object.
   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
   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 }
   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
   214 //------------------------------------------------------------------------------------------------------------------------
   215 // Klass resolution
   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 }
   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 }
   238 //------------------------------------------------------------------------------------------------------------------------
   239 // Method resolution
   240 //
   241 // According to JVM spec. $5.4.3c & $5.4.3d
   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   // Ignore overpasses so statics can be found during resolution
   247   Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
   249   if (klass->oop_is_array()) {
   250     // Only consider klass and super klass for arrays
   251     result = methodHandle(THREAD, result_oop);
   252     return;
   253   }
   255   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
   256   // ignore static and non-public methods of java.lang.Object,
   257   // like clone, finalize, registerNatives.
   258   if (in_imethod_resolve &&
   259       result_oop != NULL &&
   260       klass->is_interface() &&
   261       (result_oop->is_static() || !result_oop->is_public()) &&
   262       result_oop->method_holder() == SystemDictionary::Object_klass()) {
   263     result_oop = NULL;
   264   }
   266   // Before considering default methods, check for an overpass in the
   267   // current class if a method has not been found.
   268   if (result_oop == NULL) {
   269     result_oop = InstanceKlass::cast(klass())->find_method(name, signature);
   270   }
   272   if (result_oop == NULL) {
   273     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
   274     if (default_methods != NULL) {
   275       result_oop = InstanceKlass::find_method(default_methods, name, signature);
   276     }
   277   }
   279   if (checkpolymorphism && EnableInvokeDynamic && result_oop != NULL) {
   280     vmIntrinsics::ID iid = result_oop->intrinsic_id();
   281     if (MethodHandles::is_signature_polymorphic(iid)) {
   282       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
   283       return;
   284     }
   285   }
   286   result = methodHandle(THREAD, result_oop);
   287 }
   289 // returns first instance method
   290 // Looks up method in classes, then looks up local default methods
   291 void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
   292   Method* result_oop = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
   293   result = methodHandle(THREAD, result_oop);
   294   while (!result.is_null() && result->is_static() && result->method_holder()->super() != NULL) {
   295     KlassHandle super_klass = KlassHandle(THREAD, result->method_holder()->super());
   296     result = methodHandle(THREAD, super_klass->uncached_lookup_method(name, signature, Klass::find_overpass));
   297   }
   299   if (klass->oop_is_array()) {
   300     // Only consider klass and super klass for arrays
   301     return;
   302   }
   304   if (result.is_null()) {
   305     Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
   306     if (default_methods != NULL) {
   307       result = methodHandle(InstanceKlass::find_method(default_methods, name, signature));
   308       assert(result.is_null() || !result->is_static(), "static defaults not allowed");
   309     }
   310   }
   311 }
   313 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
   314                                           methodHandle resolved_method) {
   316   int vtable_index = Method::invalid_vtable_index;
   317   Symbol* name = resolved_method->name();
   318   Symbol* signature = resolved_method->signature();
   320   // First check in default method array
   321   if (!resolved_method->is_abstract() &&
   322     (InstanceKlass::cast(klass())->default_methods() != NULL)) {
   323     int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(),
   324                                                  name, signature, Klass::find_overpass,
   325                                                  Klass::find_static, Klass::find_private);
   326     if (index >= 0 ) {
   327       vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index);
   328     }
   329   }
   330   if (vtable_index == Method::invalid_vtable_index) {
   331     // get vtable_index for miranda methods
   332     ResourceMark rm;
   333     klassVtable *vt = InstanceKlass::cast(klass())->vtable();
   334     vtable_index = vt->index_of_miranda(name, signature);
   335   }
   336   return vtable_index;
   337 }
   339 void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
   340   InstanceKlass *ik = InstanceKlass::cast(klass());
   342   // Specify 'true' in order to skip default methods when searching the
   343   // interfaces.  Function lookup_method_in_klasses() already looked for
   344   // the method in the default methods table.
   345   result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name, signature, Klass::skip_defaults));
   346 }
   348 void LinkResolver::lookup_polymorphic_method(methodHandle& result,
   349                                              KlassHandle klass, Symbol* name, Symbol* full_signature,
   350                                              KlassHandle current_klass,
   351                                              Handle *appendix_result_or_null,
   352                                              Handle *method_type_result,
   353                                              TRAPS) {
   354   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
   355   if (TraceMethodHandles) {
   356     ResourceMark rm(THREAD);
   357     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
   358                   vmIntrinsics::name_at(iid), klass->external_name(),
   359                   name->as_C_string(), full_signature->as_C_string());
   360   }
   361   if (EnableInvokeDynamic &&
   362       klass() == SystemDictionary::MethodHandle_klass() &&
   363       iid != vmIntrinsics::_none) {
   364     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
   365       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
   366       // Do not erase last argument type (MemberName) if it is a static linkTo method.
   367       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
   368       TempNewSymbol basic_signature =
   369         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK);
   370       if (TraceMethodHandles) {
   371         ResourceMark rm(THREAD);
   372         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
   373                       name->as_C_string(),
   374                       full_signature->as_C_string(),
   375                       basic_signature->as_C_string());
   376       }
   377       result = SystemDictionary::find_method_handle_intrinsic(iid,
   378                                                               basic_signature,
   379                                                               CHECK);
   380       if (result.not_null()) {
   381         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
   382         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
   383         assert(basic_signature == result->signature(), "predict the result signature");
   384         if (TraceMethodHandles) {
   385           tty->print("lookup_polymorphic_method => intrinsic ");
   386           result->print_on(tty);
   387         }
   388         return;
   389       }
   390     } else if (iid == vmIntrinsics::_invokeGeneric
   391                && !THREAD->is_Compiler_thread()
   392                && appendix_result_or_null != NULL) {
   393       // This is a method with type-checking semantics.
   394       // We will ask Java code to spin an adapter method for it.
   395       if (!MethodHandles::enabled()) {
   396         // Make sure the Java part of the runtime has been booted up.
   397         Klass* natives = SystemDictionary::MethodHandleNatives_klass();
   398         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
   399           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
   400                                             Handle(),
   401                                             Handle(),
   402                                             true,
   403                                             CHECK);
   404         }
   405       }
   407       Handle appendix;
   408       Handle method_type;
   409       result = SystemDictionary::find_method_handle_invoker(name,
   410                                                             full_signature,
   411                                                             current_klass,
   412                                                             &appendix,
   413                                                             &method_type,
   414                                                             CHECK);
   415       if (TraceMethodHandles) {
   416         tty->print("lookup_polymorphic_method => (via Java) ");
   417         result->print_on(tty);
   418         tty->print("  lookup_polymorphic_method => appendix = ");
   419         if (appendix.is_null())  tty->print_cr("(none)");
   420         else                     appendix->print_on(tty);
   421       }
   422       if (result.not_null()) {
   423 #ifdef ASSERT
   424         ResourceMark rm(THREAD);
   426         TempNewSymbol basic_signature =
   427           MethodHandles::lookup_basic_type_signature(full_signature, CHECK);
   428         int actual_size_of_params = result->size_of_parameters();
   429         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
   430         // +1 for MethodHandle.this, +1 for trailing MethodType
   431         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
   432         if (appendix.not_null())                                   expected_size_of_params += 1;
   433         if (actual_size_of_params != expected_size_of_params) {
   434           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
   435           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
   436           result->print();
   437         }
   438         assert(actual_size_of_params == expected_size_of_params,
   439                err_msg("%d != %d", actual_size_of_params, expected_size_of_params));
   440 #endif //ASSERT
   442         assert(appendix_result_or_null != NULL, "");
   443         (*appendix_result_or_null) = appendix;
   444         (*method_type_result)      = method_type;
   445         return;
   446       }
   447     }
   448   }
   449 }
   451 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
   452                                               KlassHandle resolved_klass,
   453                                               KlassHandle sel_klass,
   454                                               methodHandle sel_method,
   455                                               TRAPS) {
   457   AccessFlags flags = sel_method->access_flags();
   459   // Special case:  arrays always override "clone". JVMS 2.15.
   460   // If the resolved klass is an array class, and the declaring class
   461   // is java.lang.Object and the method is "clone", set the flags
   462   // to public.
   463   //
   464   // We'll check for the method name first, as that's most likely
   465   // to be false (so we'll short-circuit out of these tests).
   466   if (sel_method->name() == vmSymbols::clone_name() &&
   467       sel_klass() == SystemDictionary::Object_klass() &&
   468       resolved_klass->oop_is_array()) {
   469     // We need to change "protected" to "public".
   470     assert(flags.is_protected(), "clone not protected?");
   471     jint new_flags = flags.as_int();
   472     new_flags = new_flags & (~JVM_ACC_PROTECTED);
   473     new_flags = new_flags | JVM_ACC_PUBLIC;
   474     flags.set_flags(new_flags);
   475   }
   476 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
   478   if (!Reflection::verify_field_access(ref_klass(),
   479                                        resolved_klass(),
   480                                        sel_klass(),
   481                                        flags,
   482                                        true)) {
   483     ResourceMark rm(THREAD);
   484     Exceptions::fthrow(
   485       THREAD_AND_LOCATION,
   486       vmSymbols::java_lang_IllegalAccessError(),
   487       "tried to access method %s.%s%s from class %s",
   488       sel_klass->external_name(),
   489       sel_method->name()->as_C_string(),
   490       sel_method->signature()->as_C_string(),
   491       ref_klass->external_name()
   492     );
   493     return;
   494   }
   495 }
   497 void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass,
   498                                              Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) {
   499   // This method is used only
   500   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
   501   // and
   502   // (2) in Bytecode_invoke::static_target
   503   // It appears to fail when applied to an invokeinterface call site.
   504   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
   505   // resolve klass
   506   if (code == Bytecodes::_invokedynamic) {
   507     resolved_klass = SystemDictionary::MethodHandle_klass();
   508     Symbol* method_name = vmSymbols::invoke_name();
   509     Symbol* method_signature = pool->signature_ref_at(index);
   510     KlassHandle  current_klass(THREAD, pool->pool_holder());
   511     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
   512     return;
   513   }
   515   resolve_klass(resolved_klass, pool, index, CHECK);
   517   Symbol*  method_name       = pool->name_ref_at(index);
   518   Symbol*  method_signature  = pool->signature_ref_at(index);
   519   KlassHandle  current_klass(THREAD, pool->pool_holder());
   521   if (pool->has_preresolution()
   522       || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
   523           MethodHandles::is_signature_polymorphic_name(resolved_klass(), method_name))) {
   524     Method* result_oop = ConstantPool::method_at_if_loaded(pool, index);
   525     if (result_oop != NULL) {
   526       resolved_method = methodHandle(THREAD, result_oop);
   527       return;
   528     }
   529   }
   531   if (code == Bytecodes::_invokeinterface) {
   532     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
   533   } else if (code == Bytecodes::_invokevirtual) {
   534     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
   535   } else if (!resolved_klass->is_interface()) {
   536     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
   537   } else {
   538     bool nostatics = (code == Bytecodes::_invokestatic) ? false : true;
   539     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, nostatics, CHECK);
   540   }
   541 }
   543 void LinkResolver::check_method_loader_constraints(methodHandle& resolved_method,
   544                                                    KlassHandle resolved_klass,
   545                                                    Symbol* method_name,
   546                                                    Symbol* method_signature,
   547                                                    KlassHandle current_klass,
   548                                                    const char* method_type, TRAPS) {
   549   Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
   550   Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());
   551   {
   552     ResourceMark rm(THREAD);
   553     Symbol* failed_type_symbol =
   554       SystemDictionary::check_signature_loaders(method_signature, loader,
   555                                                 class_loader, true, CHECK);
   556     if (failed_type_symbol != NULL) {
   557       const char* msg = "loader constraint violation: when resolving %s"
   558         " \"%s\" the class loader (instance of %s) of the current class, %s,"
   559         " and the class loader (instance of %s) for the method's defining class, %s, have"
   560         " different Class objects for the type %s used in the signature";
   561       char* sig = Method::name_and_sig_as_C_string(resolved_klass(), method_name, method_signature);
   562       const char* loader1 = SystemDictionary::loader_name(loader());
   563       char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();
   564       const char* loader2 = SystemDictionary::loader_name(class_loader());
   565       char* target = InstanceKlass::cast(resolved_method->method_holder())
   566                      ->name()->as_C_string();
   567       char* failed_type_name = failed_type_symbol->as_C_string();
   568       size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
   569         strlen(current) + strlen(loader2) + strlen(target) +
   570         strlen(failed_type_name) + strlen(method_type) + 1;
   571       char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
   572       jio_snprintf(buf, buflen, msg, method_type, sig, loader1, current, loader2,
   573                    target, failed_type_name);
   574       THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
   575     }
   576   }
   577 }
   579 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass,
   580                                   Symbol* method_name, Symbol* method_signature,
   581                                   KlassHandle current_klass, bool check_access,
   582                                   bool require_methodref, TRAPS) {
   584   Handle nested_exception;
   586   // 1. check if methodref required, that resolved_klass is not interfacemethodref
   587   if (require_methodref && resolved_klass->is_interface()) {
   588     ResourceMark rm(THREAD);
   589     char buf[200];
   590     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
   591         resolved_klass()->external_name());
   592     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   593   }
   595   // 2. lookup method in resolved klass and its super klasses
   596   lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, true, false, CHECK);
   598   if (resolved_method.is_null() && !resolved_klass->oop_is_array()) { // not found in the class hierarchy
   599     // 3. lookup method in all the interfaces implemented by the resolved klass
   600     lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
   602     if (resolved_method.is_null()) {
   603       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
   604       lookup_polymorphic_method(resolved_method, resolved_klass, method_name, method_signature,
   605                                 current_klass, (Handle*)NULL, (Handle*)NULL, THREAD);
   606       if (HAS_PENDING_EXCEPTION) {
   607         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
   608         CLEAR_PENDING_EXCEPTION;
   609       }
   610     }
   611   }
   613   if (resolved_method.is_null()) {
   614     // 4. method lookup failed
   615     ResourceMark rm(THREAD);
   616     THROW_MSG_CAUSE(vmSymbols::java_lang_NoSuchMethodError(),
   617                     Method::name_and_sig_as_C_string(resolved_klass(),
   618                                                             method_name,
   619                                                             method_signature),
   620                     nested_exception);
   621   }
   623   // 5. access checks, access checking may be turned off when calling from within the VM.
   624   if (check_access) {
   625     assert(current_klass.not_null() , "current_klass should not be null");
   627     // check if method can be accessed by the referring class
   628     check_method_accessability(current_klass,
   629                                resolved_klass,
   630                                KlassHandle(THREAD, resolved_method->method_holder()),
   631                                resolved_method,
   632                                CHECK);
   634     // check loader constraints
   635     check_method_loader_constraints(resolved_method, resolved_klass, method_name,
   636                                     method_signature, current_klass, "method", CHECK);
   637   }
   638 }
   640 void LinkResolver::resolve_interface_method(methodHandle& resolved_method,
   641                                             KlassHandle resolved_klass,
   642                                             Symbol* method_name,
   643                                             Symbol* method_signature,
   644                                             KlassHandle current_klass,
   645                                             bool check_access,
   646                                             bool nostatics, TRAPS) {
   648   // check if klass is interface
   649   if (!resolved_klass->is_interface()) {
   650     ResourceMark rm(THREAD);
   651     char buf[200];
   652     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
   653     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   654   }
   656   // lookup method in this interface or its super, java.lang.Object
   657   // JDK8: also look for static methods
   658   lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, false, true, CHECK);
   660   if (resolved_method.is_null() && !resolved_klass->oop_is_array()) {
   661     // lookup method in all the super-interfaces
   662     lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
   663   }
   665   if (resolved_method.is_null()) {
   666     // no method found
   667     ResourceMark rm(THREAD);
   668     THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
   669               Method::name_and_sig_as_C_string(resolved_klass(),
   670                                                       method_name,
   671                                                       method_signature));
   672   }
   674   if (check_access) {
   675     // JDK8 adds non-public interface methods, and accessability check requirement
   676     assert(current_klass.not_null() , "current_klass should not be null");
   678     // check if method can be accessed by the referring class
   679     check_method_accessability(current_klass,
   680                                resolved_klass,
   681                                KlassHandle(THREAD, resolved_method->method_holder()),
   682                                resolved_method,
   683                                CHECK);
   685     check_method_loader_constraints(resolved_method, resolved_klass, method_name,
   686                                     method_signature, current_klass, "interface method", CHECK);
   687   }
   689   if (nostatics && resolved_method->is_static()) {
   690     ResourceMark rm(THREAD);
   691     char buf[200];
   692     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
   693                  Method::name_and_sig_as_C_string(resolved_klass(),
   694                  resolved_method->name(), resolved_method->signature()));
   695     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   696   }
   698   if (TraceItables && Verbose) {
   699     ResourceMark rm(THREAD);
   700     tty->print("invokeinterface resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
   701                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
   702                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
   703                    Method::name_and_sig_as_C_string(resolved_klass(),
   704                                                     resolved_method->name(),
   705                                                     resolved_method->signature()),
   706                    resolved_method->method_holder()->internal_name()
   707                   );
   708     resolved_method->access_flags().print_on(tty);
   709     if (resolved_method->is_default_method()) {
   710       tty->print("default ");
   711     }
   712     if (resolved_method->is_overpass()) {
   713       tty->print("overpass");
   714     }
   715     tty->cr();
   716   }
   717 }
   719 //------------------------------------------------------------------------------------------------------------------------
   720 // Field resolution
   722 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
   723                                              KlassHandle resolved_klass,
   724                                              KlassHandle sel_klass,
   725                                              fieldDescriptor& fd,
   726                                              TRAPS) {
   727   if (!Reflection::verify_field_access(ref_klass(),
   728                                        resolved_klass(),
   729                                        sel_klass(),
   730                                        fd.access_flags(),
   731                                        true)) {
   732     ResourceMark rm(THREAD);
   733     Exceptions::fthrow(
   734       THREAD_AND_LOCATION,
   735       vmSymbols::java_lang_IllegalAccessError(),
   736       "tried to access field %s.%s from class %s",
   737       sel_klass->external_name(),
   738       fd.name()->as_C_string(),
   739       ref_klass->external_name()
   740     );
   741     return;
   742   }
   743 }
   745 void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
   746   // Load these early in case the resolve of the containing klass fails
   747   Symbol* field = pool->name_ref_at(index);
   748   Symbol* sig   = pool->signature_ref_at(index);
   750   // resolve specified klass
   751   KlassHandle resolved_klass;
   752   resolve_klass(resolved_klass, pool, index, CHECK);
   754   KlassHandle  current_klass(THREAD, pool->pool_holder());
   755   resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK);
   756 }
   758 void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig,
   759                                  KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class,
   760                                  TRAPS) {
   761   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
   762          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
   763          (byte == Bytecodes::_nop && !check_access), "bad field access bytecode");
   765   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
   766   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic);
   768   // Check if there's a resolved klass containing the field
   769   if (resolved_klass.is_null()) {
   770     ResourceMark rm(THREAD);
   771     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
   772   }
   774   // Resolve instance field
   775   KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd));
   776   // check if field exists; i.e., if a klass containing the field def has been selected
   777   if (sel_klass.is_null()) {
   778     ResourceMark rm(THREAD);
   779     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
   780   }
   782   // Access checking may be turned off when calling from within the VM.
   783   if (check_access) {
   785     // check access
   786     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
   788     // check for errors
   789     if (is_static != fd.is_static()) {
   790       ResourceMark rm(THREAD);
   791       char msg[200];
   792       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
   793       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
   794     }
   796     // Final fields can only be accessed from its own class.
   797     if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {
   798       THROW(vmSymbols::java_lang_IllegalAccessError());
   799     }
   801     // initialize resolved_klass if necessary
   802     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
   803     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
   804     //
   805     // note 2: we don't want to force initialization if we are just checking
   806     //         if the field access is legal; e.g., during compilation
   807     if (is_static && initialize_class) {
   808       sel_klass->initialize(CHECK);
   809     }
   810   }
   812   if (sel_klass() != current_klass() && !current_klass.is_null()) {
   813     HandleMark hm(THREAD);
   814     Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
   815     Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader());
   816     {
   817       ResourceMark rm(THREAD);
   818       Symbol* failed_type_symbol =
   819         SystemDictionary::check_signature_loaders(sig,
   820                                                   ref_loader, sel_loader,
   821                                                   false,
   822                                                   CHECK);
   823       if (failed_type_symbol != NULL) {
   824         const char* msg = "loader constraint violation: when resolving field"
   825           " \"%s\" the class loader (instance of %s) of the referring class, "
   826           "%s, and the class loader (instance of %s) for the field's resolved "
   827           "type, %s, have different Class objects for that type";
   828         char* field_name = field->as_C_string();
   829         const char* loader1 = SystemDictionary::loader_name(ref_loader());
   830         char* sel = InstanceKlass::cast(sel_klass())->name()->as_C_string();
   831         const char* loader2 = SystemDictionary::loader_name(sel_loader());
   832         char* failed_type_name = failed_type_symbol->as_C_string();
   833         size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) +
   834           strlen(sel) + strlen(loader2) + strlen(failed_type_name) + 1;
   835         char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
   836         jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2,
   837                      failed_type_name);
   838         THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
   839       }
   840     }
   841   }
   843   // return information. note that the klass is set to the actual klass containing the
   844   // field, otherwise access of static fields in superclasses will not work.
   845 }
   848 //------------------------------------------------------------------------------------------------------------------------
   849 // Invoke resolution
   850 //
   851 // Naming conventions:
   852 //
   853 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
   854 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
   855 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
   856 // recv_klass         the receiver klass
   859 void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name,
   860                                        Symbol* method_signature, KlassHandle current_klass,
   861                                        bool check_access, bool initialize_class, TRAPS) {
   862   methodHandle resolved_method;
   863   linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
   864   resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());
   866   // Initialize klass (this should only happen if everything is ok)
   867   if (initialize_class && resolved_klass->should_be_initialized()) {
   868     resolved_klass->initialize(CHECK);
   869     linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
   870   }
   872   // setup result
   873   result.set_static(resolved_klass, resolved_method, CHECK);
   874 }
   876 // throws linktime exceptions
   877 void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass,
   878                                                   Symbol* method_name, Symbol* method_signature,
   879                                                   KlassHandle current_klass, bool check_access, TRAPS) {
   881   if (!resolved_klass->is_interface()) {
   882     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
   883   } else {
   884     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
   885   }
   886   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
   888   // check if static
   889   if (!resolved_method->is_static()) {
   890     ResourceMark rm(THREAD);
   891     char buf[200];
   892     jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
   893                                                       resolved_method->name(),
   894                                                       resolved_method->signature()));
   895     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   896   }
   897 }
   900 void LinkResolver::resolve_special_call(CallInfo& result, Handle recv, KlassHandle resolved_klass, Symbol* method_name,
   901                                         Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
   902   methodHandle resolved_method;
   903   linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
   904   runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, recv, check_access, CHECK);
   905 }
   907 // throws linktime exceptions
   908 void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass,
   909                                                    Symbol* method_name, Symbol* method_signature,
   910                                                    KlassHandle current_klass, bool check_access, TRAPS) {
   912   // Invokespecial is called for multiple special reasons:
   913   // <init>
   914   // local private method invocation, for classes and interfaces
   915   // superclass.method, which can also resolve to a default method
   916   // and the selected method is recalculated relative to the direct superclass
   917   // superinterface.method, which explicitly does not check shadowing
   919   if (!resolved_klass->is_interface()) {
   920     resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
   921   } else {
   922     resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
   923   }
   925   // check if method name is <init>, that it is found in same klass as static type
   926   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
   927       resolved_method->method_holder() != resolved_klass()) {
   928     ResourceMark rm(THREAD);
   929     Exceptions::fthrow(
   930       THREAD_AND_LOCATION,
   931       vmSymbols::java_lang_NoSuchMethodError(),
   932       "%s: method %s%s not found",
   933       resolved_klass->external_name(),
   934       resolved_method->name()->as_C_string(),
   935       resolved_method->signature()->as_C_string()
   936     );
   937     return;
   938   }
   940   // check if invokespecial's interface method reference is in an indirect superinterface
   941   if (!current_klass.is_null() && resolved_klass->is_interface()) {
   942     Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ?
   943                                   current_klass() :
   944                                   InstanceKlass::cast(current_klass())->host_klass();
   945     // As of the fix for 4486457 we disable verification for all of the
   946     // dynamically-generated bytecodes associated with the 1.4
   947     // reflection implementation, not just those associated with
   948     // sun/reflect/SerializationConstructorAccessor.
   949     bool is_reflect = JDK_Version::is_gte_jdk14x_version() &&
   950                       UseNewReflection &&
   951                       klass_to_check->is_subclass_of(
   952                         SystemDictionary::reflect_MagicAccessorImpl_klass());
   954     if (!is_reflect &&
   955         !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) {
   956       ResourceMark rm(THREAD);
   957       char buf[200];
   958       jio_snprintf(buf, sizeof(buf),
   959                    "Interface method reference: %s, is in an indirect superinterface of %s",
   960                    Method::name_and_sig_as_C_string(resolved_klass(),
   961                                                          resolved_method->name(),
   962                                                          resolved_method->signature()),
   963                    current_klass->external_name());
   964       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   965     }
   966   }
   968   // check if not static
   969   if (resolved_method->is_static()) {
   970     ResourceMark rm(THREAD);
   971     char buf[200];
   972     jio_snprintf(buf, sizeof(buf),
   973                  "Expecting non-static method %s",
   974                  Method::name_and_sig_as_C_string(resolved_klass(),
   975                                                          resolved_method->name(),
   976                                                          resolved_method->signature()));
   977     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
   978   }
   980   if (TraceItables && Verbose) {
   981     ResourceMark rm(THREAD);
   982     tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
   983                 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
   984                 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
   985                 Method::name_and_sig_as_C_string(resolved_klass(),
   986                                                  resolved_method->name(),
   987                                                  resolved_method->signature()),
   988                 resolved_method->method_holder()->internal_name()
   989                );
   990     resolved_method->access_flags().print_on(tty);
   991     if (resolved_method->is_default_method()) {
   992       tty->print("default ");
   993     }
   994     if (resolved_method->is_overpass()) {
   995       tty->print("overpass");
   996     }
   997     tty->cr();
   998   }
   999 }
  1001 // throws runtime exceptions
  1002 void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
  1003                                                   KlassHandle current_klass, Handle recv, bool check_access, TRAPS) {
  1005   // resolved method is selected method unless we have an old-style lookup
  1006   // for a superclass method
  1007   // Invokespecial for a superinterface, resolved method is selected method,
  1008   // no checks for shadowing
  1009   methodHandle sel_method(THREAD, resolved_method());
  1011   if (check_access &&
  1012       // check if the method is not <init>
  1013       resolved_method->name() != vmSymbols::object_initializer_name()) {
  1015   // check if this is an old-style super call and do a new lookup if so
  1016         // a) check if ACC_SUPER flag is set for the current class
  1017     if ((current_klass->is_super() || !AllowNonVirtualCalls) &&
  1018         // b) check if the class of the resolved_klass is a superclass
  1019         // (not supertype in order to exclude interface classes) of the current class.
  1020         // This check is not performed for super.invoke for interface methods
  1021         // in super interfaces.
  1022         current_klass->is_subclass_of(resolved_klass()) &&
  1023         current_klass() != resolved_klass()) {
  1024       // Lookup super method
  1025       KlassHandle super_klass(THREAD, current_klass->super());
  1026       lookup_instance_method_in_klasses(sel_method, super_klass,
  1027                            resolved_method->name(),
  1028                            resolved_method->signature(), CHECK);
  1029       // check if found
  1030       if (sel_method.is_null()) {
  1031         ResourceMark rm(THREAD);
  1032         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1033                   Method::name_and_sig_as_C_string(resolved_klass(),
  1034                                             resolved_method->name(),
  1035                                             resolved_method->signature()));
  1036       } else if (sel_method() != resolved_method()) {
  1037         check_method_loader_constraints(sel_method, resolved_klass,
  1038                                         sel_method->name(), sel_method->signature(),
  1039                                         current_klass, "method", CHECK);
  1043     // Check that the class of objectref (the receiver) is the current class or interface,
  1044     // or a subtype of the current class or interface (the sender), otherwise invokespecial
  1045     // throws IllegalAccessError.
  1046     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
  1047     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
  1048     // a class.  If the sender is an interface, the check has to be performed at runtime.
  1049     InstanceKlass* sender = InstanceKlass::cast(current_klass());
  1050     sender = sender->is_anonymous() ? InstanceKlass::cast(sender->host_klass()) : sender;
  1051     if (sender->is_interface() && recv.not_null()) {
  1052       Klass* receiver_klass = recv->klass();
  1053       if (!receiver_klass->is_subtype_of(sender)) {
  1054         ResourceMark rm(THREAD);
  1055         char buf[500];
  1056         jio_snprintf(buf, sizeof(buf),
  1057                      "Receiver class %s must be the current class or a subtype of interface %s",
  1058                      receiver_klass->name()->as_C_string(),
  1059                      sender->name()->as_C_string());
  1060         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
  1065   // check if not static
  1066   if (sel_method->is_static()) {
  1067     ResourceMark rm(THREAD);
  1068     char buf[200];
  1069     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
  1070                                                                                                              resolved_method->name(),
  1071                                                                                                              resolved_method->signature()));
  1072     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1075   // check if abstract
  1076   if (sel_method->is_abstract()) {
  1077     ResourceMark rm(THREAD);
  1078     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1079               Method::name_and_sig_as_C_string(resolved_klass(),
  1080                                                       sel_method->name(),
  1081                                                       sel_method->signature()));
  1084   if (TraceItables && Verbose) {
  1085     ResourceMark rm(THREAD);
  1086     tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
  1087                  (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
  1088                  Method::name_and_sig_as_C_string(resolved_klass(),
  1089                                                   sel_method->name(),
  1090                                                   sel_method->signature()),
  1091                  sel_method->method_holder()->internal_name()
  1092                 );
  1093     sel_method->access_flags().print_on(tty);
  1094     if (sel_method->is_default_method()) {
  1095       tty->print("default ");
  1097     if (sel_method->is_overpass()) {
  1098       tty->print("overpass");
  1100     tty->cr();
  1103   // setup result
  1104   result.set_static(resolved_klass, sel_method, CHECK);
  1107 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,
  1108                                         Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
  1109                                         bool check_access, bool check_null_and_abstract, TRAPS) {
  1110   methodHandle resolved_method;
  1111   linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
  1112   runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);
  1115 // throws linktime exceptions
  1116 void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,
  1117                                                    Symbol* method_name, Symbol* method_signature,
  1118                                                    KlassHandle current_klass, bool check_access, TRAPS) {
  1119   // normal method resolution
  1120   resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
  1122   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
  1123   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
  1125   // check if private interface method
  1126   if (resolved_klass->is_interface() && resolved_method->is_private()) {
  1127     ResourceMark rm(THREAD);
  1128     char buf[200];
  1129     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
  1130                  Method::name_and_sig_as_C_string(resolved_klass(),
  1131                                                   resolved_method->name(),
  1132                                                   resolved_method->signature()),
  1133                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
  1134     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1137   // check if not static
  1138   if (resolved_method->is_static()) {
  1139     ResourceMark rm(THREAD);
  1140     char buf[200];
  1141     jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
  1142                                                                                                              resolved_method->name(),
  1143                                                                                                              resolved_method->signature()));
  1144     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1147   if (PrintVtables && Verbose) {
  1148     ResourceMark rm(THREAD);
  1149     tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
  1150                    (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
  1151                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
  1152                    Method::name_and_sig_as_C_string(resolved_klass(),
  1153                                                     resolved_method->name(),
  1154                                                     resolved_method->signature()),
  1155                    resolved_method->method_holder()->internal_name()
  1156                   );
  1157     resolved_method->access_flags().print_on(tty);
  1158     if (resolved_method->is_default_method()) {
  1159       tty->print("default ");
  1161     if (resolved_method->is_overpass()) {
  1162       tty->print("overpass");
  1164     tty->cr();
  1168 // throws runtime exceptions
  1169 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
  1170                                                   methodHandle resolved_method,
  1171                                                   KlassHandle resolved_klass,
  1172                                                   Handle recv,
  1173                                                   KlassHandle recv_klass,
  1174                                                   bool check_null_and_abstract,
  1175                                                   TRAPS) {
  1177   // setup default return values
  1178   int vtable_index = Method::invalid_vtable_index;
  1179   methodHandle selected_method;
  1181   assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
  1183   // runtime method resolution
  1184   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
  1185     THROW(vmSymbols::java_lang_NullPointerException());
  1188   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
  1189   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
  1190   // a missing receiver might result in a bogus lookup.
  1191   assert(resolved_method->method_holder()->is_linked(), "must be linked");
  1193   // do lookup based on receiver klass using the vtable index
  1194   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
  1195     vtable_index = vtable_index_of_interface_method(resolved_klass,
  1196                            resolved_method);
  1197     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
  1199     InstanceKlass* inst = InstanceKlass::cast(recv_klass());
  1200     selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
  1201   } else {
  1202     // at this point we are sure that resolved_method is virtual and not
  1203     // a default or miranda method; therefore, it must have a valid vtable index.
  1204     assert(!resolved_method->has_itable_index(), "");
  1205     vtable_index = resolved_method->vtable_index();
  1206     // We could get a negative vtable_index for final methods,
  1207     // because as an optimization they are they are never put in the vtable,
  1208     // unless they override an existing method.
  1209     // If we do get a negative, it means the resolved method is the the selected
  1210     // method, and it can never be changed by an override.
  1211     if (vtable_index == Method::nonvirtual_vtable_index) {
  1212       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
  1213       selected_method = resolved_method;
  1214     } else {
  1215       // recv_klass might be an arrayKlassOop but all vtables start at
  1216       // the same place. The cast is to avoid virtual call and assertion.
  1217       InstanceKlass* inst = (InstanceKlass*)recv_klass();
  1218       selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
  1222   // check if method exists
  1223   if (selected_method.is_null()) {
  1224     ResourceMark rm(THREAD);
  1225     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1226               Method::name_and_sig_as_C_string(resolved_klass(),
  1227                                                       resolved_method->name(),
  1228                                                       resolved_method->signature()));
  1231   // check if abstract
  1232   if (check_null_and_abstract && selected_method->is_abstract()) {
  1233     ResourceMark rm(THREAD);
  1234     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1235               Method::name_and_sig_as_C_string(resolved_klass(),
  1236                                                       selected_method->name(),
  1237                                                       selected_method->signature()));
  1240   if (PrintVtables && Verbose) {
  1241     ResourceMark rm(THREAD);
  1242     tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ",
  1243                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
  1244                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
  1245                    Method::name_and_sig_as_C_string(resolved_klass(),
  1246                                                     resolved_method->name(),
  1247                                                     resolved_method->signature()),
  1248                    selected_method->method_holder()->internal_name(),
  1249                    vtable_index
  1250                   );
  1251     selected_method->access_flags().print_on(tty);
  1252     if (selected_method->is_default_method()) {
  1253       tty->print("default ");
  1255     if (selected_method->is_overpass()) {
  1256       tty->print("overpass");
  1258     tty->cr();
  1260   // setup result
  1261   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
  1264 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,
  1265                                           Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
  1266                                           bool check_access, bool check_null_and_abstract, TRAPS) {
  1267   methodHandle resolved_method;
  1268   linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
  1269   runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);
  1272 // throws linktime exceptions
  1273 void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name,
  1274                                                      Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
  1275   // normal interface method resolution
  1276   resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
  1278   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
  1279   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
  1282 // throws runtime exceptions
  1283 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
  1284                                                     Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {
  1285   // check if receiver exists
  1286   if (check_null_and_abstract && recv.is_null()) {
  1287     THROW(vmSymbols::java_lang_NullPointerException());
  1290   // check if private interface method
  1291   if (resolved_klass->is_interface() && resolved_method->is_private()) {
  1292     ResourceMark rm(THREAD);
  1293     char buf[200];
  1294     jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s",
  1295                  Method::name_and_sig_as_C_string(resolved_klass(),
  1296                                                   resolved_method->name(),
  1297                                                   resolved_method->signature()));
  1298     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1301   // check if receiver klass implements the resolved interface
  1302   if (!recv_klass->is_subtype_of(resolved_klass())) {
  1303     ResourceMark rm(THREAD);
  1304     char buf[200];
  1305     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
  1306                  recv_klass()->external_name(),
  1307                  resolved_klass()->external_name());
  1308     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
  1311   // do lookup based on receiver klass
  1312   methodHandle sel_method;
  1313   // This search must match the linktime preparation search for itable initialization
  1314   // to correctly enforce loader constraints for interface method inheritance
  1315   lookup_instance_method_in_klasses(sel_method, recv_klass,
  1316             resolved_method->name(),
  1317             resolved_method->signature(), CHECK);
  1318   if (sel_method.is_null() && !check_null_and_abstract) {
  1319     // In theory this is a harmless placeholder value, but
  1320     // in practice leaving in null affects the nsk default method tests.
  1321     // This needs further study.
  1322     sel_method = resolved_method;
  1324   // check if method exists
  1325   if (sel_method.is_null()) {
  1326     ResourceMark rm(THREAD);
  1327     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1328               Method::name_and_sig_as_C_string(recv_klass(),
  1329                                                       resolved_method->name(),
  1330                                                       resolved_method->signature()));
  1332   // check access
  1333   // Throw Illegal Access Error if sel_method is not public.
  1334   if (!sel_method->is_public()) {
  1335     ResourceMark rm(THREAD);
  1336     THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
  1337               Method::name_and_sig_as_C_string(recv_klass(),
  1338                                                sel_method->name(),
  1339                                                sel_method->signature()));
  1341   // check if abstract
  1342   if (check_null_and_abstract && sel_method->is_abstract()) {
  1343     ResourceMark rm(THREAD);
  1344     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
  1345               Method::name_and_sig_as_C_string(recv_klass(),
  1346                                                       sel_method->name(),
  1347                                                       sel_method->signature()));
  1350   if (TraceItables && Verbose) {
  1351     ResourceMark rm(THREAD);
  1352     tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
  1353                    (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
  1354                    (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
  1355                    Method::name_and_sig_as_C_string(resolved_klass(),
  1356                                                     resolved_method->name(),
  1357                                                     resolved_method->signature()),
  1358                    sel_method->method_holder()->internal_name()
  1359                   );
  1360     sel_method->access_flags().print_on(tty);
  1361     if (sel_method->is_default_method()) {
  1362       tty->print("default ");
  1364     if (sel_method->is_overpass()) {
  1365       tty->print("overpass");
  1367     tty->cr();
  1369   // setup result
  1370   if (!resolved_method->has_itable_index()) {
  1371     int vtable_index = resolved_method->vtable_index();
  1372     assert(vtable_index == sel_method->vtable_index(), "sanity check");
  1373     result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
  1374   } else {
  1375     int itable_index = resolved_method()->itable_index();
  1376     result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
  1381 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
  1382                                                  KlassHandle resolved_klass,
  1383                                                  Symbol* method_name,
  1384                                                  Symbol* method_signature,
  1385                                                  KlassHandle current_klass,
  1386                                                  bool check_access) {
  1387   EXCEPTION_MARK;
  1388   methodHandle method_result;
  1389   linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
  1390   if (HAS_PENDING_EXCEPTION) {
  1391     CLEAR_PENDING_EXCEPTION;
  1392     return methodHandle();
  1393   } else {
  1394     return method_result;
  1398 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
  1399                                                  KlassHandle resolved_klass,
  1400                                                  Symbol* method_name,
  1401                                                  Symbol* method_signature,
  1402                                                  KlassHandle current_klass,
  1403                                                  bool check_access) {
  1404   EXCEPTION_MARK;
  1405   methodHandle method_result;
  1406   linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
  1407   if (HAS_PENDING_EXCEPTION) {
  1408     CLEAR_PENDING_EXCEPTION;
  1409     return methodHandle();
  1410   } else {
  1411     return method_result;
  1415 methodHandle LinkResolver::resolve_virtual_call_or_null(
  1416                                                  KlassHandle receiver_klass,
  1417                                                  KlassHandle resolved_klass,
  1418                                                  Symbol* name,
  1419                                                  Symbol* signature,
  1420                                                  KlassHandle current_klass,
  1421                                                  bool check_access) {
  1422   EXCEPTION_MARK;
  1423   CallInfo info;
  1424   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD);
  1425   if (HAS_PENDING_EXCEPTION) {
  1426     CLEAR_PENDING_EXCEPTION;
  1427     return methodHandle();
  1429   return info.selected_method();
  1432 methodHandle LinkResolver::resolve_interface_call_or_null(
  1433                                                  KlassHandle receiver_klass,
  1434                                                  KlassHandle resolved_klass,
  1435                                                  Symbol* name,
  1436                                                  Symbol* signature,
  1437                                                  KlassHandle current_klass,
  1438                                                  bool check_access) {
  1439   EXCEPTION_MARK;
  1440   CallInfo info;
  1441   resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, check_access, false, THREAD);
  1442   if (HAS_PENDING_EXCEPTION) {
  1443     CLEAR_PENDING_EXCEPTION;
  1444     return methodHandle();
  1446   return info.selected_method();
  1449 int LinkResolver::resolve_virtual_vtable_index(
  1450                                                KlassHandle receiver_klass,
  1451                                                KlassHandle resolved_klass,
  1452                                                Symbol* name,
  1453                                                Symbol* signature,
  1454                                                KlassHandle current_klass) {
  1455   EXCEPTION_MARK;
  1456   CallInfo info;
  1457   resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
  1458   if (HAS_PENDING_EXCEPTION) {
  1459     CLEAR_PENDING_EXCEPTION;
  1460     return Method::invalid_vtable_index;
  1462   return info.vtable_index();
  1465 methodHandle LinkResolver::resolve_static_call_or_null(
  1466                                                   KlassHandle resolved_klass,
  1467                                                   Symbol* name,
  1468                                                   Symbol* signature,
  1469                                                   KlassHandle current_klass,
  1470                                                   bool check_access) {
  1471   EXCEPTION_MARK;
  1472   CallInfo info;
  1473   resolve_static_call(info, resolved_klass, name, signature, current_klass, check_access, false, THREAD);
  1474   if (HAS_PENDING_EXCEPTION) {
  1475     CLEAR_PENDING_EXCEPTION;
  1476     return methodHandle();
  1478   return info.selected_method();
  1481 methodHandle LinkResolver::resolve_special_call_or_null(
  1482                                                   KlassHandle resolved_klass,
  1483                                                   Symbol* name,
  1484                                                   Symbol* signature,
  1485                                                   KlassHandle current_klass,
  1486                                                   bool check_access) {
  1487   EXCEPTION_MARK;
  1488   CallInfo info;
  1489   resolve_special_call(info, Handle(), resolved_klass, name, signature, current_klass, check_access, THREAD);
  1490   if (HAS_PENDING_EXCEPTION) {
  1491     CLEAR_PENDING_EXCEPTION;
  1492     return methodHandle();
  1494   return info.selected_method();
  1499 //------------------------------------------------------------------------------------------------------------------------
  1500 // ConstantPool entries
  1502 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
  1503   switch (byte) {
  1504     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
  1505     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
  1506     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
  1507     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
  1508     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
  1509     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
  1511   return;
  1514 void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature,
  1515                                 KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {
  1516    // resolve klass
  1517   resolve_klass(resolved_klass, pool, index, CHECK);
  1519   // Get name, signature, and static klass
  1520   method_name      = pool->name_ref_at(index);
  1521   method_signature = pool->signature_ref_at(index);
  1522   current_klass    = KlassHandle(THREAD, pool->pool_holder());
  1526 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1527   KlassHandle  resolved_klass;
  1528   Symbol* method_name = NULL;
  1529   Symbol* method_signature = NULL;
  1530   KlassHandle  current_klass;
  1531   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
  1532   resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
  1536 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
  1537   KlassHandle  resolved_klass;
  1538   Symbol* method_name = NULL;
  1539   Symbol* method_signature = NULL;
  1540   KlassHandle  current_klass;
  1541   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
  1542   resolve_special_call(result, recv, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
  1546 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
  1547                                           constantPoolHandle pool, int index,
  1548                                           TRAPS) {
  1550   KlassHandle  resolved_klass;
  1551   Symbol* method_name = NULL;
  1552   Symbol* method_signature = NULL;
  1553   KlassHandle  current_klass;
  1554   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
  1555   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
  1556   resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
  1560 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
  1561   KlassHandle  resolved_klass;
  1562   Symbol* method_name = NULL;
  1563   Symbol* method_signature = NULL;
  1564   KlassHandle  current_klass;
  1565   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
  1566   KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
  1567   resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
  1571 void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1572   assert(EnableInvokeDynamic, "");
  1573   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
  1574   KlassHandle  resolved_klass;
  1575   Symbol* method_name = NULL;
  1576   Symbol* method_signature = NULL;
  1577   KlassHandle  current_klass;
  1578   resolve_pool(resolved_klass, method_name,  method_signature, current_klass, pool, index, CHECK);
  1579   if (TraceMethodHandles) {
  1580     ResourceMark rm(THREAD);
  1581     tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string());
  1583   resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK);
  1586 void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass,
  1587                                        Symbol* method_name, Symbol* method_signature,
  1588                                        KlassHandle current_klass,
  1589                                        TRAPS) {
  1590   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
  1591   assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), "");
  1592   assert(MethodHandles::is_signature_polymorphic_name(method_name), "");
  1593   methodHandle resolved_method;
  1594   Handle       resolved_appendix;
  1595   Handle       resolved_method_type;
  1596   lookup_polymorphic_method(resolved_method, resolved_klass,
  1597                             method_name, method_signature,
  1598                             current_klass, &resolved_appendix, &resolved_method_type, CHECK);
  1599   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
  1602 static void wrap_invokedynamic_exception(TRAPS) {
  1603   if (HAS_PENDING_EXCEPTION) {
  1604     if (TraceMethodHandles) {
  1605       tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION));
  1606       PENDING_EXCEPTION->print();
  1608     if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
  1609       // throw these guys, since they are already wrapped
  1610       return;
  1612     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
  1613       // intercept only LinkageErrors which might have failed to wrap
  1614       return;
  1616     // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
  1617     Handle nested_exception(THREAD, PENDING_EXCEPTION);
  1618     CLEAR_PENDING_EXCEPTION;
  1619     THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
  1623 void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
  1624   assert(EnableInvokeDynamic, "");
  1626   //resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK);
  1627   Symbol* method_name       = pool->name_ref_at(index);
  1628   Symbol* method_signature  = pool->signature_ref_at(index);
  1629   KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
  1631   // Resolve the bootstrap specifier (BSM + optional arguments).
  1632   Handle bootstrap_specifier;
  1633   // Check if CallSite has been bound already:
  1634   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
  1635   if (cpce->is_f1_null()) {
  1636     int pool_index = cpce->constant_pool_index();
  1637     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
  1638     wrap_invokedynamic_exception(CHECK);
  1639     assert(bsm_info != NULL, "");
  1640     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
  1641     bootstrap_specifier = Handle(THREAD, bsm_info);
  1643   if (!cpce->is_f1_null()) {
  1644     methodHandle method(     THREAD, cpce->f1_as_method());
  1645     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
  1646     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
  1647     result.set_handle(method, appendix, method_type, THREAD);
  1648     wrap_invokedynamic_exception(CHECK);
  1649     return;
  1652   if (TraceMethodHandles) {
  1653       ResourceMark rm(THREAD);
  1654       tty->print_cr("resolve_invokedynamic #%d %s %s",
  1655                   ConstantPool::decode_invokedynamic_index(index),
  1656                   method_name->as_C_string(), method_signature->as_C_string());
  1657     tty->print("  BSM info: "); bootstrap_specifier->print();
  1660   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
  1663 void LinkResolver::resolve_dynamic_call(CallInfo& result,
  1664                                         Handle bootstrap_specifier,
  1665                                         Symbol* method_name, Symbol* method_signature,
  1666                                         KlassHandle current_klass,
  1667                                         TRAPS) {
  1668   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
  1669   // The appendix argument is likely to be a freshly-created CallSite.
  1670   Handle       resolved_appendix;
  1671   Handle       resolved_method_type;
  1672   methodHandle resolved_method =
  1673     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
  1674                                                      bootstrap_specifier,
  1675                                                      method_name, method_signature,
  1676                                                      &resolved_appendix,
  1677                                                      &resolved_method_type,
  1678                                                      THREAD);
  1679   wrap_invokedynamic_exception(CHECK);
  1680   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
  1681   wrap_invokedynamic_exception(CHECK);
  1684 //------------------------------------------------------------------------------------------------------------------------
  1685 #ifndef PRODUCT
  1687 void CallInfo::print() {
  1688   ResourceMark rm;
  1689   const char* kindstr = "unknown";
  1690   switch (_call_kind) {
  1691   case direct_call: kindstr = "direct"; break;
  1692   case vtable_call: kindstr = "vtable"; break;
  1693   case itable_call: kindstr = "itable"; break;
  1695   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
  1696                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
  1699 #endif

mercurial