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