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

1693   int pool_index = cpce->constant_pool_index();
1694   if (pool()->tag_at(pool_index).is_invoke_dynamic_in_error()) {
1695     // Throw saved exception.
1696     ConstantPool::throw_resolution_error(pool, pool_index, CHECK);
1697     return;
1698   }
1699 
1700   if (cpce->is_f1_null()) {
1701     oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD);
1702     wrap_invokedynamic_exception(CHECK);
1703     assert(bsm_info != NULL, "");
1704     // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1705     bootstrap_specifier = Handle(THREAD, bsm_info);
1706   }
1707   if (!cpce->is_f1_null()) {
1708     methodHandle method(     THREAD, cpce->f1_as_method());
1709     Handle       appendix(   THREAD, cpce->appendix_if_resolved(pool));
1710     Handle       method_type(THREAD, cpce->method_type_if_resolved(pool));
1711     result.set_handle(method, appendix, method_type, THREAD);
1712     wrap_invokedynamic_exception(CHECK);
1713     return;
1714   }
1715 
1716   if (TraceMethodHandles) {
1717     ResourceMark rm(THREAD);
1718     tty->print_cr("resolve_invokedynamic #%d %s %s in %s",
1719                   ConstantPool::decode_invokedynamic_index(index),
1720                   method_name->as_C_string(), method_signature->as_C_string(),
1721                   current_klass->name()->as_C_string());
1722     tty->print("  BSM info: "); bootstrap_specifier->print();
1723   }
1724 
1725   resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, THREAD);
1726   if (HAS_PENDING_EXCEPTION) {
1727     if (pool()->tag_at(pool_index).is_invoke_dynamic()&&
1728         PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1729       // Record the exception then throw it.
1730       ConstantPool::save_and_throw_exception(pool, pool_index, pool()->tag_at(pool_index), CHECK);
1731     } else if (pool()->tag_at(pool_index).is_invoke_dynamic_in_error()) {
1732       // Throw saved exception.
1733       ConstantPool::throw_resolution_error(pool, pool_index, CHECK);
1734     } // else return the original exception.
1735   }
1736 }
1737 
1738 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1739                                         Handle bootstrap_specifier,
1740                                         Symbol* method_name, Symbol* method_signature,
1741                                         Klass* current_klass,
1742                                         TRAPS) {
1743   // JSR 292:  this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1744   // The appendix argument is likely to be a freshly-created CallSite.
1745   Handle       resolved_appendix;
1746   Handle       resolved_method_type;
1747   methodHandle resolved_method =
1748     SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1749                                                      bootstrap_specifier,
1750                                                      method_name, method_signature,
1751                                                      &resolved_appendix,
1752                                                      &resolved_method_type,
1753                                                      THREAD);
1754   wrap_invokedynamic_exception(CHECK);
1755   result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD);
1756   wrap_invokedynamic_exception(CHECK);
1757 }
--- EOF ---