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 static void print_nest_host_error_on(stringStream* ss, Klass* ref_klass, Klass* sel_klass, TRAPS) {
 544   assert(ref_klass->is_instance_klass(), "must be");
 545   assert(sel_klass->is_instance_klass(), "must be");
 546   InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
 547   InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
 548   const char* nest_host_error_1 = ref_ik->nest_host_error(THREAD);
 549   const char* nest_host_error_2 = sel_ik->nest_host_error(THREAD);
 550   if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) {
 551     ss->print(", (%s%s%s)",
 552               (nest_host_error_1 != NULL) ? nest_host_error_1 : "",
 553               (nest_host_error_1 != NULL && nest_host_error_2 != NULL) ? ", " : "",
 554               (nest_host_error_2 != NULL) ? nest_host_error_2 : "");
 555   }
 556 }
 557 
 558 void LinkResolver::check_method_accessability(Klass* ref_klass,
 559                                               Klass* resolved_klass,
 560                                               Klass* sel_klass,
 561                                               const methodHandle& sel_method,
 562                                               TRAPS) {
 563 
 564   AccessFlags flags = sel_method->access_flags();
 565 
 566   // Special case:  arrays always override "clone". JVMS 2.15.
 567   // If the resolved klass is an array class, and the declaring class
 568   // is java.lang.Object and the method is "clone", set the flags
 569   // to public.
 570   //
 571   // We'll check for the method name first, as that's most likely
 572   // to be false (so we'll short-circuit out of these tests).
 573   if (sel_method->name() == vmSymbols::clone_name() &&
 574       sel_klass == SystemDictionary::Object_klass() &&
 575       resolved_klass->is_array_klass()) {
 576     // We need to change "protected" to "public".
 577     assert(flags.is_protected(), "clone not protected?");
 578     jint new_flags = flags.as_int();
 579     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 580     new_flags = new_flags | JVM_ACC_PUBLIC;
 581     flags.set_flags(new_flags);
 582   }
 583 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 584 
 585   bool can_access = Reflection::verify_member_access(ref_klass,
 586                                                      resolved_klass,
 587                                                      sel_klass,
 588                                                      flags,
 589                                                      true, false, CHECK);
 590   // Any existing exceptions that may have been thrown
 591   // have been allowed to propagate.
 592   if (!can_access) {
 593     ResourceMark rm(THREAD);
 594     stringStream ss;
 595     bool same_module = (sel_klass->module() == ref_klass->module());
 596     ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",
 597              ref_klass->external_name(),
 598              sel_method->is_abstract()  ? "abstract "  : "",
 599              sel_method->is_protected() ? "protected " : "",
 600              sel_method->is_private()   ? "private "   : "",
 601              sel_method->external_name(),
 602              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 603              (same_module) ? "" : "; ",
 604              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 605              );
 606 
 607     // For private access see if there was a problem with nest host
 608     // resolution, and if so report that as part of the message.
 609     if (sel_method->is_private()) {
 610       print_nest_host_error_on(&ss, ref_klass, sel_klass, THREAD);
 611     }
 612 
 613     Exceptions::fthrow(THREAD_AND_LOCATION,
 614                        vmSymbols::java_lang_IllegalAccessError(),
 615                        "%s",
 616                        ss.as_string()
 617                        );
 618     return;
 619   }
 620 }
 621 
 622 Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
 623                                                 const constantPoolHandle& pool, int index, TRAPS) {
 624   // This method is used only
 625   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 626   // and
 627   // (2) in Bytecode_invoke::static_target
 628   // It appears to fail when applied to an invokeinterface call site.
 629   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 630   // resolve klass
 631   if (code == Bytecodes::_invokedynamic) {
 632     Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
 633     Symbol* method_name = vmSymbols::invoke_name();
 634     Symbol* method_signature = pool->signature_ref_at(index);
 635     Klass*  current_klass = pool->pool_holder();
 636     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 637     return resolve_method(link_info, code, THREAD);
 638   }
 639 
 640   LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
 641   Klass* resolved_klass = link_info.resolved_klass();
 642 
 643   if (pool->has_preresolution()
 644       || (resolved_klass == SystemDictionary::MethodHandle_klass() &&
 645           MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
 646     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 647     if (result != NULL) {
 648       return result;
 649     }
 650   }
 651 
 652   if (code == Bytecodes::_invokeinterface) {
 653     return resolve_interface_method(link_info, code, THREAD);
 654   } else if (code == Bytecodes::_invokevirtual) {
 655     return resolve_method(link_info, code, THREAD);
 656   } else if (!resolved_klass->is_interface()) {
 657     return resolve_method(link_info, code, THREAD);
 658   } else {
 659     return resolve_interface_method(link_info, code, THREAD);
 660   }
 661 }
 662 
 663 // Check and print a loader constraint violation message for method or interface method
 664 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 665                                                    const methodHandle& resolved_method,
 666                                                    const char* method_type, TRAPS) {
 667   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 668   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 669 
 670   ResourceMark rm(THREAD);
 671   Symbol* failed_type_symbol =
 672     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 673                                               resolved_loader, true, CHECK);
 674   if (failed_type_symbol != NULL) {
 675     Klass* current_class = link_info.current_klass();
 676     ClassLoaderData* current_loader_data = current_class->class_loader_data();
 677     assert(current_loader_data != NULL, "current class has no class loader data");
 678     Klass* resolved_method_class = resolved_method->method_holder();
 679     ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
 680     assert(target_loader_data != NULL, "resolved method's class has no class loader data");
 681 
 682     stringStream ss;
 683     ss.print("loader constraint violation: when resolving %s '", method_type);
 684     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 685     ss.print("' the class loader %s of the current class, %s,"
 686              " and the class loader %s for the method's defining class, %s, have"
 687              " different Class objects for the type %s used in the signature (%s; %s)",
 688              current_loader_data->loader_name_and_id(),
 689              current_class->name()->as_C_string(),
 690              target_loader_data->loader_name_and_id(),
 691              resolved_method_class->name()->as_C_string(),
 692              failed_type_symbol->as_C_string(),
 693              current_class->class_in_module_of_loader(false, true),
 694              resolved_method_class->class_in_module_of_loader(false, true));
 695     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 696   }
 697 }
 698 
 699 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 700                                                   Klass* current_klass,
 701                                                   Klass* sel_klass, TRAPS) {
 702   Handle ref_loader(THREAD, current_klass->class_loader());
 703   Handle sel_loader(THREAD, sel_klass->class_loader());
 704 
 705   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 706   Symbol* failed_type_symbol =
 707     SystemDictionary::check_signature_loaders(sig,
 708                                               ref_loader, sel_loader,
 709                                               false,
 710                                               CHECK);
 711   if (failed_type_symbol != NULL) {
 712     stringStream ss;
 713     const char* failed_type_name = failed_type_symbol->as_klass_external_name();
 714 
 715     ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
 716              "the class loader %s of the current class, %s, "
 717              "and the class loader %s for the field's defining %s, %s, "
 718              "have different Class objects for type %s (%s; %s)",
 719              field->as_C_string(),
 720              failed_type_name,
 721              current_klass->class_loader_data()->loader_name_and_id(),
 722              current_klass->external_name(),
 723              sel_klass->class_loader_data()->loader_name_and_id(),
 724              sel_klass->external_kind(),
 725              sel_klass->external_name(),
 726              failed_type_name,
 727              current_klass->class_in_module_of_loader(false, true),
 728              sel_klass->class_in_module_of_loader(false, true));
 729     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 730   }
 731 }
 732 
 733 Method* LinkResolver::resolve_method(const LinkInfo& link_info,
 734                                      Bytecodes::Code code, TRAPS) {
 735 
 736   Handle nested_exception;
 737   Klass* resolved_klass = link_info.resolved_klass();
 738 
 739   // 1. For invokevirtual, cannot call an interface method
 740   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 741     ResourceMark rm(THREAD);
 742     char buf[200];
 743     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 744         resolved_klass->external_name());
 745     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 746   }
 747 
 748   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 749   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 750     ResourceMark rm(THREAD);
 751     stringStream ss;
 752     ss.print("Method '");
 753     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 754     ss.print("' must be Methodref constant");
 755     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 756   }
 757 
 758   // 3. lookup method in resolved klass and its super klasses
 759   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 760 
 761   // 4. lookup method in all the interfaces implemented by the resolved klass
 762   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 763     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 764 
 765     if (resolved_method.is_null()) {
 766       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 767       Method* method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);
 768       resolved_method = methodHandle(THREAD, method);
 769       if (HAS_PENDING_EXCEPTION) {
 770         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 771         CLEAR_PENDING_EXCEPTION;
 772       }
 773     }
 774   }
 775 
 776   // 5. method lookup failed
 777   if (resolved_method.is_null()) {
 778     ResourceMark rm(THREAD);
 779     stringStream ss;
 780     ss.print("'");
 781     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 782     ss.print("'");
 783     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 784                      ss.as_string(), nested_exception, NULL);
 785   }
 786 
 787   // 6. access checks, access checking may be turned off when calling from within the VM.
 788   Klass* current_klass = link_info.current_klass();
 789   if (link_info.check_access()) {
 790     assert(current_klass != NULL , "current_klass should not be null");
 791 
 792     // check if method can be accessed by the referring class
 793     check_method_accessability(current_klass,
 794                                resolved_klass,
 795                                resolved_method->method_holder(),
 796                                resolved_method,
 797                                CHECK_NULL);
 798 
 799     // check loader constraints
 800     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 801   }
 802 
 803   return resolved_method();
 804 }
 805 
 806 static void trace_method_resolution(const char* prefix,
 807                                     Klass* klass,
 808                                     Klass* resolved_klass,
 809                                     Method* method,
 810                                     bool logitables,
 811                                     int index = -1) {
 812 #ifndef PRODUCT
 813   ResourceMark rm;
 814   Log(itables) logi;
 815   LogStream lsi(logi.trace());
 816   Log(vtables) logv;
 817   LogStream lsv(logv.trace());
 818   outputStream* st;
 819   if (logitables) {
 820     st = &lsi;
 821   } else {
 822     st = &lsv;
 823   }
 824   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 825             prefix,
 826             (klass == NULL ? "<NULL>" : klass->internal_name()),
 827             (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
 828             Method::name_and_sig_as_C_string(resolved_klass,
 829                                              method->name(),
 830                                              method->signature()),
 831             method->method_holder()->internal_name());
 832   method->print_linkage_flags(st);
 833   if (index != -1) {
 834     st->print("vtable_index:%d", index);
 835   }
 836   st->cr();
 837 #endif // PRODUCT
 838 }
 839 
 840 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 841 Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 842 
 843   Klass* resolved_klass = link_info.resolved_klass();
 844 
 845   // check if klass is interface
 846   if (!resolved_klass->is_interface()) {
 847     ResourceMark rm(THREAD);
 848     char buf[200];
 849     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 850     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 851   }
 852 
 853   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 854   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 855     ResourceMark rm(THREAD);
 856     stringStream ss;
 857     ss.print("Method '");
 858     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 859     ss.print("' must be InterfaceMethodref constant");
 860     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 861   }
 862 
 863   // lookup method in this interface or its super, java.lang.Object
 864   // JDK8: also look for static methods
 865   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 866 
 867   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 868     // lookup method in all the super-interfaces
 869     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 870   }
 871 
 872   if (resolved_method.is_null()) {
 873     // no method found
 874     ResourceMark rm(THREAD);
 875     stringStream ss;
 876     ss.print("'");
 877     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 878     ss.print("'");
 879     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
 880   }
 881 
 882   if (link_info.check_access()) {
 883     // JDK8 adds non-public interface methods, and accessability check requirement
 884     Klass* current_klass = link_info.current_klass();
 885 
 886     assert(current_klass != NULL , "current_klass should not be null");
 887 
 888     // check if method can be accessed by the referring class
 889     check_method_accessability(current_klass,
 890                                resolved_klass,
 891                                resolved_method->method_holder(),
 892                                resolved_method,
 893                                CHECK_NULL);
 894 
 895     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 896   }
 897 
 898   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 899     ResourceMark rm(THREAD);
 900     stringStream ss;
 901     ss.print("Expected instance not static method '");
 902     Method::print_external_name(&ss, resolved_klass,
 903                                 resolved_method->name(), resolved_method->signature());
 904     ss.print("'");
 905     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 906   }
 907 
 908   if (log_develop_is_enabled(Trace, itables)) {
 909     char buf[200];
 910     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 911                  Bytecodes::name(code));
 912     trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);
 913   }
 914 
 915   return resolved_method();
 916 }
 917 
 918 //------------------------------------------------------------------------------------------------------------------------
 919 // Field resolution
 920 
 921 void LinkResolver::check_field_accessability(Klass* ref_klass,
 922                                              Klass* resolved_klass,
 923                                              Klass* sel_klass,
 924                                              const fieldDescriptor& fd,
 925                                              TRAPS) {
 926   bool can_access = Reflection::verify_member_access(ref_klass,
 927                                                      resolved_klass,
 928                                                      sel_klass,
 929                                                      fd.access_flags(),
 930                                                      true, false, CHECK);
 931   // Any existing exceptions that may have been thrown, for example LinkageErrors
 932   // from nest-host resolution, have been allowed to propagate.
 933   if (!can_access) {
 934     bool same_module = (sel_klass->module() == ref_klass->module());
 935     ResourceMark rm(THREAD);
 936     stringStream ss;
 937     ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",
 938              ref_klass->external_name(),
 939              fd.is_protected() ? "protected " : "",
 940              fd.is_private()   ? "private "   : "",
 941              sel_klass->external_name(),
 942              fd.name()->as_C_string(),
 943              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 944              (same_module) ? "" : "; ",
 945              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 946              );
 947     // For private access see if there was a problem with nest host
 948     // resolution, and if so report that as part of the message.
 949     if (fd.is_private()) {
 950       print_nest_host_error_on(&ss, ref_klass, sel_klass, THREAD);
 951     }
 952     Exceptions::fthrow(THREAD_AND_LOCATION,
 953                        vmSymbols::java_lang_IllegalAccessError(),
 954                        "%s",
 955                        ss.as_string()
 956                        );
 957     return;
 958   }
 959 }
 960 
 961 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 962   LinkInfo link_info(pool, index, method, CHECK);
 963   resolve_field(fd, link_info, byte, true, CHECK);
 964 }
 965 
 966 void LinkResolver::resolve_field(fieldDescriptor& fd,
 967                                  const LinkInfo& link_info,
 968                                  Bytecodes::Code byte, bool initialize_class,
 969                                  TRAPS) {
 970   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 971          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 972          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 973          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 974 
 975   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 976   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 977   // Check if there's a resolved klass containing the field
 978   Klass* resolved_klass = link_info.resolved_klass();
 979   Symbol* field = link_info.name();
 980   Symbol* sig = link_info.signature();
 981 
 982   if (resolved_klass == NULL) {
 983     ResourceMark rm(THREAD);
 984     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 985   }
 986 
 987   // Resolve instance field
 988   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
 989   // check if field exists; i.e., if a klass containing the field def has been selected
 990   if (sel_klass == NULL) {
 991     ResourceMark rm(THREAD);
 992     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 993   }
 994 
 995   // Access checking may be turned off when calling from within the VM.
 996   Klass* current_klass = link_info.current_klass();
 997   if (link_info.check_access()) {
 998 
 999     // check access
1000     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1001 
1002     // check for errors
1003     if (is_static != fd.is_static()) {
1004       ResourceMark rm(THREAD);
1005       char msg[200];
1006       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1007       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1008     }
1009 
1010     // A final field can be modified only
1011     // (1) by methods declared in the class declaring the field and
1012     // (2) by the <clinit> method (in case of a static field)
1013     //     or by the <init> method (in case of an instance field).
1014     if (is_put && fd.access_flags().is_final()) {
1015 
1016       if (sel_klass != current_klass) {
1017         ResourceMark rm(THREAD);
1018         stringStream ss;
1019         ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1020                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1021                 current_klass->external_name());
1022         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1023       }
1024 
1025       if (fd.constants()->pool_holder()->major_version() >= 53) {
1026         Method* m = link_info.current_method();
1027         assert(m != NULL, "information about the current method must be available for 'put' bytecodes");
1028         bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1029                                                    fd.is_static() &&
1030                                                    !m->is_static_initializer());
1031         bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1032                                                      !fd.is_static() &&
1033                                                      !m->is_object_initializer());
1034 
1035         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1036           ResourceMark rm(THREAD);
1037           stringStream ss;
1038           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1039                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1040                    m->name()->as_C_string(),
1041                    is_static ? "<clinit>" : "<init>");
1042           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1043         }
1044       }
1045     }
1046 
1047     // initialize resolved_klass if necessary
1048     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1049     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1050     //
1051     // note 2: we don't want to force initialization if we are just checking
1052     //         if the field access is legal; e.g., during compilation
1053     if (is_static && initialize_class) {
1054       sel_klass->initialize(CHECK);
1055     }
1056   }
1057 
1058   if ((sel_klass != current_klass) && (current_klass != NULL)) {
1059     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1060   }
1061 
1062   // return information. note that the klass is set to the actual klass containing the
1063   // field, otherwise access of static fields in superclasses will not work.
1064 }
1065 
1066 
1067 //------------------------------------------------------------------------------------------------------------------------
1068 // Invoke resolution
1069 //
1070 // Naming conventions:
1071 //
1072 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1073 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1074 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1075 // recv_klass         the receiver klass
1076 
1077 
1078 void LinkResolver::resolve_static_call(CallInfo& result,
1079                                        const LinkInfo& link_info,
1080                                        bool initialize_class, TRAPS) {
1081   Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1082 
1083   // The resolved class can change as a result of this resolution.
1084   Klass* resolved_klass = resolved_method->method_holder();
1085 
1086   // Initialize klass (this should only happen if everything is ok)
1087   if (initialize_class && resolved_klass->should_be_initialized()) {
1088     resolved_klass->initialize(CHECK);
1089     // Use updated LinkInfo to reresolve with resolved method holder
1090     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1091                       link_info.current_klass(),
1092                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1093     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1094   }
1095 
1096   // setup result
1097   result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1098 }
1099 
1100 // throws linktime exceptions
1101 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1102 
1103   Klass* resolved_klass = link_info.resolved_klass();
1104   Method* resolved_method;
1105   if (!resolved_klass->is_interface()) {
1106     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1107   } else {
1108     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1109   }
1110   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1111 
1112   // check if static
1113   if (!resolved_method->is_static()) {
1114     ResourceMark rm(THREAD);
1115     stringStream ss;
1116     ss.print("Expected static method '");
1117     resolved_method->print_external_name(&ss);
1118     ss.print("'");
1119     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1120   }
1121   return resolved_method;
1122 }
1123 
1124 
1125 void LinkResolver::resolve_special_call(CallInfo& result,
1126                                         Handle recv,
1127                                         const LinkInfo& link_info,
1128                                         TRAPS) {
1129   Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1130   runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1131 }
1132 
1133 // throws linktime exceptions
1134 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1135 
1136   // Invokespecial is called for multiple special reasons:
1137   // <init>
1138   // local private method invocation, for classes and interfaces
1139   // superclass.method, which can also resolve to a default method
1140   // and the selected method is recalculated relative to the direct superclass
1141   // superinterface.method, which explicitly does not check shadowing
1142   Klass* resolved_klass = link_info.resolved_klass();
1143   Method* resolved_method;
1144 
1145   if (!resolved_klass->is_interface()) {
1146     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1147   } else {
1148     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1149   }
1150 
1151   // check if method name is <init>, that it is found in same klass as static type
1152   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1153       resolved_method->method_holder() != resolved_klass) {
1154     ResourceMark rm(THREAD);
1155     stringStream ss;
1156     ss.print("%s: method '", resolved_klass->external_name());
1157     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1158     ss.print(" %s(", resolved_method->name()->as_C_string());
1159     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1160     ss.print(")' not found");
1161     Exceptions::fthrow(
1162       THREAD_AND_LOCATION,
1163       vmSymbols::java_lang_NoSuchMethodError(),
1164       "%s", ss.as_string());
1165     return NULL;
1166   }
1167 
1168   // ensure that invokespecial's interface method reference is in
1169   // a direct superinterface, not an indirect superinterface
1170   Klass* current_klass = link_info.current_klass();
1171   if (current_klass != NULL && resolved_klass->is_interface()) {
1172     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1173     InstanceKlass *klass_to_check = !ck->is_unsafe_anonymous() ?
1174                                     ck :
1175                                     ck->unsafe_anonymous_host();
1176     // Disable verification for the dynamically-generated reflection bytecodes.
1177     bool is_reflect = klass_to_check->is_subclass_of(
1178                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1179 
1180     if (!is_reflect &&
1181         !klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1182       ResourceMark rm(THREAD);
1183       stringStream ss;
1184       ss.print("Interface method reference: '");
1185       resolved_method->print_external_name(&ss);
1186       ss.print("', is in an indirect superinterface of %s",
1187                current_klass->external_name());
1188       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1189     }
1190   }
1191 
1192   // check if not static
1193   if (resolved_method->is_static()) {
1194     ResourceMark rm(THREAD);
1195     stringStream ss;
1196     ss.print("Expecting non-static method '");
1197     resolved_method->print_external_name(&ss);
1198     ss.print("'");
1199     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1200   }
1201 
1202   if (log_develop_is_enabled(Trace, itables)) {
1203     trace_method_resolution("invokespecial resolved method: caller-class:",
1204                             current_klass, resolved_klass, resolved_method, true);
1205   }
1206 
1207   return resolved_method;
1208 }
1209 
1210 // throws runtime exceptions
1211 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1212                                                   const LinkInfo& link_info,
1213                                                   const methodHandle& resolved_method,
1214                                                   Handle recv, TRAPS) {
1215 
1216   Klass* resolved_klass = link_info.resolved_klass();
1217 
1218   // resolved method is selected method unless we have an old-style lookup
1219   // for a superclass method
1220   // Invokespecial for a superinterface, resolved method is selected method,
1221   // no checks for shadowing
1222   methodHandle sel_method(THREAD, resolved_method());
1223 
1224   if (link_info.check_access() &&
1225       // check if the method is not <init>
1226       resolved_method->name() != vmSymbols::object_initializer_name()) {
1227 
1228     Klass* current_klass = link_info.current_klass();
1229 
1230     // Check if the class of the resolved_klass is a superclass
1231     // (not supertype in order to exclude interface classes) of the current class.
1232     // This check is not performed for super.invoke for interface methods
1233     // in super interfaces.
1234     if (current_klass->is_subclass_of(resolved_klass) &&
1235         current_klass != resolved_klass) {
1236       // Lookup super method
1237       Klass* super_klass = current_klass->super();
1238       Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1239                                                      resolved_method->name(),
1240                                                      resolved_method->signature(),
1241                                                      Klass::find_private, CHECK);
1242       sel_method = methodHandle(THREAD, instance_method);
1243 
1244       // check if found
1245       if (sel_method.is_null()) {
1246         ResourceMark rm(THREAD);
1247         stringStream ss;
1248         ss.print("'");
1249         resolved_method->print_external_name(&ss);
1250         ss.print("'");
1251         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1252       // check loader constraints if found a different method
1253       } else if (sel_method() != resolved_method()) {
1254         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1255       }
1256     }
1257 
1258     // Check that the class of objectref (the receiver) is the current class or interface,
1259     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1260     // throws IllegalAccessError.
1261     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1262     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1263     // a class.  If the sender is an interface, the check has to be performed at runtime.
1264     InstanceKlass* sender = InstanceKlass::cast(current_klass);
1265     sender = sender->is_unsafe_anonymous() ? sender->unsafe_anonymous_host() : sender;
1266     if (sender->is_interface() && recv.not_null()) {
1267       Klass* receiver_klass = recv->klass();
1268       if (!receiver_klass->is_subtype_of(sender)) {
1269         ResourceMark rm(THREAD);
1270         char buf[500];
1271         jio_snprintf(buf, sizeof(buf),
1272                      "Receiver class %s must be the current class or a subtype of interface %s",
1273                      receiver_klass->external_name(),
1274                      sender->external_name());
1275         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1276       }
1277     }
1278   }
1279 
1280   // check if not static
1281   if (sel_method->is_static()) {
1282     ResourceMark rm(THREAD);
1283     stringStream ss;
1284     ss.print("Expecting non-static method '");
1285     resolved_method->print_external_name(&ss);
1286     ss.print("'");
1287     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1288   }
1289 
1290   // check if abstract
1291   if (sel_method->is_abstract()) {
1292     ResourceMark rm(THREAD);
1293     stringStream ss;
1294     ss.print("'");
1295     Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1296     ss.print("'");
1297     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1298   }
1299 
1300   if (log_develop_is_enabled(Trace, itables)) {
1301     trace_method_resolution("invokespecial selected method: resolved-class:",
1302                             resolved_klass, resolved_klass, sel_method(), true);
1303   }
1304 
1305   // setup result
1306   result.set_static(resolved_klass, sel_method, CHECK);
1307 }
1308 
1309 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1310                                         const LinkInfo& link_info,
1311                                         bool check_null_and_abstract, TRAPS) {
1312   Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1313   runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1314                                  link_info.resolved_klass(),
1315                                  recv, receiver_klass,
1316                                  check_null_and_abstract, CHECK);
1317 }
1318 
1319 // throws linktime exceptions
1320 Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1321                                                            TRAPS) {
1322   // normal method resolution
1323   Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1324 
1325   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1326   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1327 
1328   // check if private interface method
1329   Klass* resolved_klass = link_info.resolved_klass();
1330   Klass* current_klass = link_info.current_klass();
1331 
1332   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1333   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1334     ResourceMark rm(THREAD);
1335     stringStream ss;
1336     ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1337     resolved_method->print_external_name(&ss);
1338     ss.print("', caller-class: %s",
1339              (current_klass == NULL ? "<null>" : current_klass->internal_name()));
1340     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1341   }
1342 
1343   // check if not static
1344   if (resolved_method->is_static()) {
1345     ResourceMark rm(THREAD);
1346     stringStream ss;
1347     ss.print("Expecting non-static method '");
1348     resolved_method->print_external_name(&ss);
1349     ss.print("'");
1350     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1351   }
1352 
1353   if (log_develop_is_enabled(Trace, vtables)) {
1354     trace_method_resolution("invokevirtual resolved method: caller-class:",
1355                             current_klass, resolved_klass, resolved_method, false);
1356   }
1357 
1358   return resolved_method;
1359 }
1360 
1361 // throws runtime exceptions
1362 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1363                                                   const methodHandle& resolved_method,
1364                                                   Klass* resolved_klass,
1365                                                   Handle recv,
1366                                                   Klass* recv_klass,
1367                                                   bool check_null_and_abstract,
1368                                                   TRAPS) {
1369 
1370   // setup default return values
1371   int vtable_index = Method::invalid_vtable_index;
1372   methodHandle selected_method;
1373 
1374   // runtime method resolution
1375   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1376     THROW(vmSymbols::java_lang_NullPointerException());
1377   }
1378 
1379   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1380   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1381   // a missing receiver might result in a bogus lookup.
1382   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1383 
1384   // do lookup based on receiver klass using the vtable index
1385   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1386     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1387     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1388 
1389     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1390   } else {
1391     // at this point we are sure that resolved_method is virtual and not
1392     // a default or miranda method; therefore, it must have a valid vtable index.
1393     assert(!resolved_method->has_itable_index(), "");
1394     vtable_index = resolved_method->vtable_index();
1395     // We could get a negative vtable_index of nonvirtual_vtable_index for private
1396     // methods, or for final methods. Private methods never appear in the vtable
1397     // and never override other methods. As an optimization, final methods are
1398     // never put in the vtable, unless they override an existing method.
1399     // So if we do get nonvirtual_vtable_index, it means the selected method is the
1400     // resolved method, and it can never be changed by an override.
1401     if (vtable_index == Method::nonvirtual_vtable_index) {
1402       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1403       selected_method = resolved_method;
1404     } else {
1405       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1406     }
1407   }
1408 
1409   // check if method exists
1410   if (selected_method.is_null()) {
1411     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1412   }
1413 
1414   // check if abstract
1415   if (check_null_and_abstract && selected_method->is_abstract()) {
1416     // Pass arguments for generating a verbose error message.
1417     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1418   }
1419 
1420   if (log_develop_is_enabled(Trace, vtables)) {
1421     trace_method_resolution("invokevirtual selected method: receiver-class:",
1422                             recv_klass, resolved_klass, selected_method(),
1423                             false, vtable_index);
1424   }
1425   // setup result
1426   result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1427 }
1428 
1429 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1430                                           const LinkInfo& link_info,
1431                                           bool check_null_and_abstract, TRAPS) {
1432   // throws linktime exceptions
1433   Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1434   methodHandle mh(THREAD, resolved_method);
1435   runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1436                                    recv, recv_klass, check_null_and_abstract, CHECK);
1437 }
1438 
1439 Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1440                                                              TRAPS) {
1441   // normal interface method resolution
1442   Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1443   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1444   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1445 
1446   return resolved_method;
1447 }
1448 
1449 // throws runtime exceptions
1450 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1451                                                     const methodHandle& resolved_method,
1452                                                     Klass* resolved_klass,
1453                                                     Handle recv,
1454                                                     Klass* recv_klass,
1455                                                     bool check_null_and_abstract, TRAPS) {
1456 
1457   // check if receiver exists
1458   if (check_null_and_abstract && recv.is_null()) {
1459     THROW(vmSymbols::java_lang_NullPointerException());
1460   }
1461 
1462   // check if receiver klass implements the resolved interface
1463   if (!recv_klass->is_subtype_of(resolved_klass)) {
1464     ResourceMark rm(THREAD);
1465     char buf[200];
1466     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1467                  recv_klass->external_name(),
1468                  resolved_klass->external_name());
1469     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1470   }
1471 
1472   methodHandle selected_method = resolved_method;
1473 
1474   // resolve the method in the receiver class, unless it is private
1475   if (!resolved_method()->is_private()) {
1476     // do lookup based on receiver klass
1477     // This search must match the linktime preparation search for itable initialization
1478     // to correctly enforce loader constraints for interface method inheritance.
1479     // Private methods are skipped as the resolved method was not private.
1480     Method* method = lookup_instance_method_in_klasses(recv_klass,
1481                                                        resolved_method->name(),
1482                                                        resolved_method->signature(),
1483                                                        Klass::skip_private, CHECK);
1484     selected_method = methodHandle(THREAD, method);
1485 
1486     if (selected_method.is_null() && !check_null_and_abstract) {
1487       // In theory this is a harmless placeholder value, but
1488       // in practice leaving in null affects the nsk default method tests.
1489       // This needs further study.
1490       selected_method = resolved_method;
1491     }
1492     // check if method exists
1493     if (selected_method.is_null()) {
1494       // Pass arguments for generating a verbose error message.
1495       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1496     }
1497     // check access
1498     // Throw Illegal Access Error if selected_method is not public.
1499     if (!selected_method->is_public()) {
1500       ResourceMark rm(THREAD);
1501       stringStream ss;
1502       ss.print("'");
1503       Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1504       ss.print("'");
1505       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1506     }
1507     // check if abstract
1508     if (check_null_and_abstract && selected_method->is_abstract()) {
1509       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1510     }
1511   }
1512 
1513   if (log_develop_is_enabled(Trace, itables)) {
1514     trace_method_resolution("invokeinterface selected method: receiver-class:",
1515                             recv_klass, resolved_klass, selected_method(), true);
1516   }
1517   // setup result
1518   if (resolved_method->has_vtable_index()) {
1519     int vtable_index = resolved_method->vtable_index();
1520     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1521     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1522     result.set_virtual(resolved_klass, resolved_method, selected_method, vtable_index, CHECK);
1523   } else if (resolved_method->has_itable_index()) {
1524     int itable_index = resolved_method()->itable_index();
1525     log_develop_trace(itables)("  -- itable index: %d", itable_index);
1526     result.set_interface(resolved_klass, resolved_method, selected_method, itable_index, CHECK);
1527   } else {
1528     int index = resolved_method->vtable_index();
1529     log_develop_trace(itables)("  -- non itable/vtable index: %d", index);
1530     assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1531     assert(resolved_method()->is_private() ||
1532            (resolved_method()->is_final() && resolved_method->method_holder() == SystemDictionary::Object_klass()),
1533            "Should only have non-virtual invokeinterface for private or final-Object methods!");
1534     assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1535     // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1536     result.set_virtual(resolved_klass, resolved_method, resolved_method, index, CHECK);
1537   }
1538 }
1539 
1540 
1541 Method* LinkResolver::linktime_resolve_interface_method_or_null(
1542                                                  const LinkInfo& link_info) {
1543   EXCEPTION_MARK;
1544   Method* method_result = linktime_resolve_interface_method(link_info, THREAD);
1545   if (HAS_PENDING_EXCEPTION) {
1546     CLEAR_PENDING_EXCEPTION;
1547     return NULL;
1548   } else {
1549     return method_result;
1550   }
1551 }
1552 
1553 Method* LinkResolver::linktime_resolve_virtual_method_or_null(
1554                                                  const LinkInfo& link_info) {
1555   EXCEPTION_MARK;
1556   Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1557   if (HAS_PENDING_EXCEPTION) {
1558     CLEAR_PENDING_EXCEPTION;
1559     return NULL;
1560   } else {
1561     return method_result;
1562   }
1563 }
1564 
1565 Method* LinkResolver::resolve_virtual_call_or_null(
1566                                                  Klass* receiver_klass,
1567                                                  const LinkInfo& link_info) {
1568   EXCEPTION_MARK;
1569   CallInfo info;
1570   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1571   if (HAS_PENDING_EXCEPTION) {
1572     CLEAR_PENDING_EXCEPTION;
1573     return NULL;
1574   }
1575   return info.selected_method();
1576 }
1577 
1578 Method* LinkResolver::resolve_interface_call_or_null(
1579                                                  Klass* receiver_klass,
1580                                                  const LinkInfo& link_info) {
1581   EXCEPTION_MARK;
1582   CallInfo info;
1583   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1584   if (HAS_PENDING_EXCEPTION) {
1585     CLEAR_PENDING_EXCEPTION;
1586     return NULL;
1587   }
1588   return info.selected_method();
1589 }
1590 
1591 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1592                                                const LinkInfo& link_info) {
1593   EXCEPTION_MARK;
1594   CallInfo info;
1595   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1596                        /*check_null_or_abstract*/false, THREAD);
1597   if (HAS_PENDING_EXCEPTION) {
1598     CLEAR_PENDING_EXCEPTION;
1599     return Method::invalid_vtable_index;
1600   }
1601   return info.vtable_index();
1602 }
1603 
1604 Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1605   EXCEPTION_MARK;
1606   CallInfo info;
1607   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1608   if (HAS_PENDING_EXCEPTION) {
1609     CLEAR_PENDING_EXCEPTION;
1610     return NULL;
1611   }
1612   return info.selected_method();
1613 }
1614 
1615 Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1616   EXCEPTION_MARK;
1617   CallInfo info;
1618   resolve_special_call(info, Handle(), link_info, THREAD);
1619   if (HAS_PENDING_EXCEPTION) {
1620     CLEAR_PENDING_EXCEPTION;
1621     return NULL;
1622   }
1623   return info.selected_method();
1624 }
1625 
1626 
1627 
1628 //------------------------------------------------------------------------------------------------------------------------
1629 // ConstantPool entries
1630 
1631 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1632   switch (byte) {
1633     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1634     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1635     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1636     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1637     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1638     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1639     default                         :                                                            break;
1640   }
1641   return;
1642 }
1643 
1644 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1645                              const methodHandle& attached_method,
1646                              Bytecodes::Code byte, TRAPS) {
1647   Klass* defc = attached_method->method_holder();
1648   Symbol* name = attached_method->name();
1649   Symbol* type = attached_method->signature();
1650   LinkInfo link_info(defc, name, type);
1651   switch(byte) {
1652     case Bytecodes::_invokevirtual:
1653       resolve_virtual_call(result, recv, recv->klass(), link_info,
1654                            /*check_null_and_abstract=*/true, CHECK);
1655       break;
1656     case Bytecodes::_invokeinterface:
1657       resolve_interface_call(result, recv, recv->klass(), link_info,
1658                              /*check_null_and_abstract=*/true, CHECK);
1659       break;
1660     case Bytecodes::_invokestatic:
1661       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1662       break;
1663     case Bytecodes::_invokespecial:
1664       resolve_special_call(result, recv, link_info, CHECK);
1665       break;
1666     default:
1667       fatal("bad call: %s", Bytecodes::name(byte));
1668       break;
1669   }
1670 }
1671 
1672 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1673   LinkInfo link_info(pool, index, CHECK);
1674   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1675 }
1676 
1677 
1678 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1679                                          const constantPoolHandle& pool, int index, TRAPS) {
1680   LinkInfo link_info(pool, index, CHECK);
1681   resolve_special_call(result, recv, link_info, CHECK);
1682 }
1683 
1684 
1685 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1686                                           const constantPoolHandle& pool, int index,
1687                                           TRAPS) {
1688 
1689   LinkInfo link_info(pool, index, CHECK);
1690   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1691   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1692 }
1693 
1694 
1695 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1696   LinkInfo link_info(pool, index, CHECK);
1697   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1698   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1699 }
1700 
1701 
1702 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1703   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1704   LinkInfo link_info(pool, index, CHECK);
1705   if (TraceMethodHandles) {
1706     ResourceMark rm(THREAD);
1707     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1708                   link_info.signature()->as_C_string());
1709   }
1710   resolve_handle_call(result, link_info, CHECK);
1711 }
1712 
1713 void LinkResolver::resolve_handle_call(CallInfo& result,
1714                                        const LinkInfo& link_info,
1715                                        TRAPS) {
1716   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1717   Klass* resolved_klass = link_info.resolved_klass();
1718   assert(resolved_klass == SystemDictionary::MethodHandle_klass() ||
1719          resolved_klass == SystemDictionary::VarHandle_klass(), "");
1720   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1721   Handle       resolved_appendix;
1722   Method* resolved_method = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1723   result.set_handle(resolved_klass, methodHandle(THREAD, resolved_method), resolved_appendix, CHECK);
1724 }
1725 
1726 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1727   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(indy_index);
1728   int pool_index = cpce->constant_pool_index();
1729 
1730   // Resolve the bootstrap specifier (BSM + optional arguments).
1731   BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
1732 
1733   // Check if CallSite has been bound already or failed already, and short circuit:
1734   {
1735     bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1736     if (is_done) return;
1737   }
1738 
1739   // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1740   // reference to a method handle which will be the bootstrap method for a dynamic
1741   // call site.  If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1742   // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1743   // method's CP index for the CONSTANT_MethodHandle_info.  So, there is no need to
1744   // set the indy_rf flag since any subsequent invokedynamic instruction which shares
1745   // this bootstrap method will encounter the resolution of MethodHandleInError.
1746 
1747   resolve_dynamic_call(result, bootstrap_specifier, CHECK);
1748 
1749   if (TraceMethodHandles) {
1750     bootstrap_specifier.print_msg_on(tty, "resolve_invokedynamic");
1751   }
1752 
1753   // The returned linkage result is provisional up to the moment
1754   // the interpreter or runtime performs a serialized check of
1755   // the relevant CPCE::f1 field.  This is done by the caller
1756   // of this method, via CPCE::set_dynamic_call, which uses
1757   // an ObjectLocker to do the final serialization of updates
1758   // to CPCE state, including f1.
1759 }
1760 
1761 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1762                                         BootstrapInfo& bootstrap_specifier,
1763                                         TRAPS) {
1764   // JSR 292:  this must resolve to an implicitly generated method
1765   // such as MH.linkToCallSite(*...) or some other call-site shape.
1766   // The appendix argument is likely to be a freshly-created CallSite.
1767   // It may also be a MethodHandle from an unwrapped ConstantCallSite,
1768   // or any other reference.  The resolved_method as well as the appendix
1769   // are both recorded together via CallInfo::set_handle.
1770   SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1771   Exceptions::wrap_dynamic_exception(THREAD);
1772 
1773   if (HAS_PENDING_EXCEPTION) {
1774     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1775       // Let any random low-level IE or SOE or OOME just bleed through.
1776       // Basically we pretend that the bootstrap method was never called,
1777       // if it fails this way:  We neither record a successful linkage,
1778       // nor do we memorize a LE for posterity.
1779       return;
1780     }
1781     // JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve
1782     // a symbolic reference fails because an error is thrown that is an
1783     // instance of LinkageError (or a subclass), then subsequent attempts to
1784     // resolve the reference always fail with the same error that was thrown
1785     // as a result of the initial resolution attempt.
1786      bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK);
1787      if (!recorded_res_status) {
1788        // Another thread got here just before we did.  So, either use the method
1789        // that it resolved or throw the LinkageError exception that it threw.
1790        bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1791        if (is_done) return;
1792      }
1793      assert(bootstrap_specifier.invokedynamic_cp_cache_entry()->indy_resolution_failed(),
1794             "Resolution failure flag wasn't set");
1795   }
1796 
1797   bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK);
1798   // Exceptions::wrap_dynamic_exception not used because
1799   // set_handle doesn't throw linkage errors
1800 }
1801 
1802 // Selected method is abstract.
1803 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1804                                                const methodHandle& selected_method,
1805                                                Klass *recv_klass, TRAPS) {
1806   Klass *resolved_klass = resolved_method->method_holder();
1807   ResourceMark rm(THREAD);
1808   stringStream ss;
1809 
1810   if (recv_klass != NULL) {
1811     ss.print("Receiver class %s does not define or inherit an "
1812              "implementation of the",
1813              recv_klass->external_name());
1814   } else {
1815     ss.print("Missing implementation of");
1816   }
1817 
1818   assert(resolved_method.not_null(), "Sanity");
1819   ss.print(" resolved method '%s%s",
1820            resolved_method->is_abstract() ? "abstract " : "",
1821            resolved_method->is_private()  ? "private "  : "");
1822   resolved_method->signature()->print_as_signature_external_return_type(&ss);
1823   ss.print(" %s(", resolved_method->name()->as_C_string());
1824   resolved_method->signature()->print_as_signature_external_parameters(&ss);
1825   ss.print(")' of %s %s.",
1826            resolved_klass->external_kind(),
1827            resolved_klass->external_name());
1828 
1829   if (selected_method.not_null() && !(resolved_method == selected_method)) {
1830     ss.print(" Selected method is '%s%s",
1831              selected_method->is_abstract() ? "abstract " : "",
1832              selected_method->is_private()  ? "private "  : "");
1833     selected_method->print_external_name(&ss);
1834     ss.print("'.");
1835   }
1836 
1837   THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1838 }