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