rev 59277 : [mq]: v3

   1 /*
   2  * Copyright (c) 1997, 2019, 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_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  68 }
  69 
  70 
  71 void CallInfo::set_interface(Klass* resolved_klass,
  72                              Klass* selected_klass,
  73                              const methodHandle& resolved_method,
  74                              const methodHandle& selected_method,
  75                              int itable_index, TRAPS) {
  76   // This is only called for interface methods. If the resolved_method
  77   // comes from java/lang/Object, it can be the subject of a virtual call, so
  78   // we should pick the vtable index from the resolved method.
  79   // In that case, the caller must call set_virtual instead of set_interface.
  80   assert(resolved_method->method_holder()->is_interface(), "");
  81   assert(itable_index == resolved_method()->itable_index(), "");
  82   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
  83 }
  84 
  85 void CallInfo::set_virtual(Klass* resolved_klass,
  86                            Klass* selected_klass,
  87                            const methodHandle& resolved_method,
  88                            const methodHandle& selected_method,
  89                            int vtable_index, TRAPS) {
  90   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
  91   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
  92   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
  93   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
  94   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
  95 }
  96 
  97 void CallInfo::set_handle(const methodHandle& resolved_method,
  98                           Handle resolved_appendix, TRAPS) {
  99   set_handle(SystemDictionary::MethodHandle_klass(), resolved_method, resolved_appendix, CHECK);
 100 }
 101 
 102 void CallInfo::set_handle(Klass* resolved_klass,
 103                           const methodHandle& resolved_method,
 104                           Handle resolved_appendix, TRAPS) {
 105   guarantee(resolved_method.not_null(), "resolved method is null");
 106   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 107          resolved_method->is_compiled_lambda_form(),
 108          "linkMethod must return one of these");
 109   int vtable_index = Method::nonvirtual_vtable_index;
 110   assert(!resolved_method->has_vtable_index(), "");
 111   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 112   _resolved_appendix = resolved_appendix;
 113 }
 114 
 115 void CallInfo::set_common(Klass* resolved_klass,
 116                           Klass* selected_klass,
 117                           const methodHandle& resolved_method,
 118                           const methodHandle& selected_method,
 119                           CallKind kind,
 120                           int index,
 121                           TRAPS) {
 122   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 123   _resolved_klass  = resolved_klass;
 124   _selected_klass  = selected_klass;
 125   _resolved_method = resolved_method;
 126   _selected_method = selected_method;
 127   _call_kind       = kind;
 128   _call_index      = index;
 129   _resolved_appendix = Handle();
 130   DEBUG_ONLY(verify());  // verify before making side effects
 131 
 132   CompilationPolicy::compile_if_required(selected_method, THREAD);
 133 }
 134 
 135 // utility query for unreflecting a method
 136 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
 137   Klass* resolved_method_holder = resolved_method->method_holder();
 138   if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
 139     resolved_klass = resolved_method_holder;
 140   }
 141   _resolved_klass  = resolved_klass;
 142   _selected_klass  = resolved_klass;
 143   _resolved_method = methodHandle(THREAD, resolved_method);
 144   _selected_method = methodHandle(THREAD, resolved_method);
 145   // classify:
 146   CallKind kind = CallInfo::unknown_kind;
 147   int index = resolved_method->vtable_index();
 148   if (resolved_method->can_be_statically_bound()) {
 149     kind = CallInfo::direct_call;
 150   } else if (!resolved_method_holder->is_interface()) {
 151     // Could be an Object method inherited into an interface, but still a vtable call.
 152     kind = CallInfo::vtable_call;
 153   } else if (!resolved_klass->is_interface()) {
 154     // A default or miranda method.  Compute the vtable index.
 155     index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
 156                            _resolved_method);
 157     assert(index >= 0 , "we should have valid vtable index at this point");
 158 
 159     kind = CallInfo::vtable_call;
 160   } else if (resolved_method->has_vtable_index()) {
 161     // Can occur if an interface redeclares a method of Object.
 162 
 163 #ifdef ASSERT
 164     // Ensure that this is really the case.
 165     Klass* object_klass = SystemDictionary::Object_klass();
 166     Method * object_resolved_method = object_klass->vtable().method_at(index);
 167     assert(object_resolved_method->name() == resolved_method->name(),
 168       "Object and interface method names should match at vtable index %d, %s != %s",
 169       index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
 170     assert(object_resolved_method->signature() == resolved_method->signature(),
 171       "Object and interface method signatures should match at vtable index %d, %s != %s",
 172       index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
 173 #endif // ASSERT
 174 
 175     kind = CallInfo::vtable_call;
 176   } else {
 177     // A regular interface call.
 178     kind = CallInfo::itable_call;
 179     index = resolved_method->itable_index();
 180   }
 181   assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
 182   _call_kind  = kind;
 183   _call_index = index;
 184   _resolved_appendix = Handle();
 185   // Find or create a ResolvedMethod instance for this Method*
 186   set_resolved_method_name(CHECK);
 187 
 188   DEBUG_ONLY(verify());
 189 }
 190 
 191 void CallInfo::set_resolved_method_name(TRAPS) {
 192   assert(_resolved_method() != NULL, "Should already have a Method*");
 193   oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(_resolved_method, 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;
 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   default         : kindstr = "unknown"; break;
 227   }
 228   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 229                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 230 }
 231 #endif
 232 
 233 //------------------------------------------------------------------------------------------------------------------------
 234 // Implementation of LinkInfo
 235 
 236 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS) {
 237    // resolve klass
 238   _resolved_klass = pool->klass_ref_at(index, CHECK);
 239 
 240   // Get name, signature, and static klass
 241   _name          = pool->name_ref_at(index);
 242   _signature     = pool->signature_ref_at(index);
 243   _tag           = pool->tag_ref_at(index);
 244   _current_klass = pool->pool_holder();
 245   _current_method = current_method;
 246 
 247   // Coming from the constant pool always checks access
 248   _check_access  = true;
 249 }
 250 
 251 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 252    // resolve klass
 253   _resolved_klass = pool->klass_ref_at(index, CHECK);
 254 
 255   // Get name, signature, and static klass
 256   _name          = pool->name_ref_at(index);
 257   _signature     = pool->signature_ref_at(index);
 258   _tag           = pool->tag_ref_at(index);
 259   _current_klass = pool->pool_holder();
 260   _current_method = methodHandle();
 261 
 262   // Coming from the constant pool always checks access
 263   _check_access  = true;
 264 }
 265 
 266 #ifndef PRODUCT
 267 void LinkInfo::print() {
 268   ResourceMark rm;
 269   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 270                 _resolved_klass->name()->as_C_string(),
 271                 _name->as_C_string(),
 272                 _signature->as_C_string(),
 273                 _current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(),
 274                 _check_access ? "true" : "false");
 275 }
 276 #endif // PRODUCT
 277 //------------------------------------------------------------------------------------------------------------------------
 278 // Klass resolution
 279 
 280 void LinkResolver::check_klass_accessability(Klass* ref_klass, Klass* sel_klass,
 281                                              bool fold_type_to_class, TRAPS) {
 282   Klass* base_klass = sel_klass;
 283   if (fold_type_to_class) {
 284     if (sel_klass->is_objArray_klass()) {
 285       base_klass = ObjArrayKlass::cast(sel_klass)->bottom_klass();
 286     }
 287     // The element type could be a typeArray - we only need the access
 288     // check if it is a reference to another class.
 289     if (!base_klass->is_instance_klass()) {
 290       return;  // no relevant check to do
 291     }
 292   }
 293   Reflection::VerifyClassAccessResults vca_result =
 294     Reflection::verify_class_access(ref_klass, InstanceKlass::cast(base_klass), true);
 295   if (vca_result != Reflection::ACCESS_OK) {
 296     ResourceMark rm(THREAD);
 297     char* msg = Reflection::verify_class_access_msg(ref_klass,
 298                                                     InstanceKlass::cast(base_klass),
 299                                                     vca_result);
 300     bool same_module = (base_klass->module() == ref_klass->module());
 301     if (msg == NULL) {
 302       Exceptions::fthrow(
 303         THREAD_AND_LOCATION,
 304         vmSymbols::java_lang_IllegalAccessError(),
 305         "failed to access class %s from class %s (%s%s%s)",
 306         base_klass->external_name(),
 307         ref_klass->external_name(),
 308         (same_module) ? base_klass->joint_in_module_of_loader(ref_klass) : base_klass->class_in_module_of_loader(),
 309         (same_module) ? "" : "; ",
 310         (same_module) ? "" : ref_klass->class_in_module_of_loader());
 311     } else {
 312       // Use module specific message returned by verify_class_access_msg().
 313       Exceptions::fthrow(
 314         THREAD_AND_LOCATION,
 315         vmSymbols::java_lang_IllegalAccessError(),
 316         "%s", msg);
 317     }
 318   }
 319 }
 320 
 321 //------------------------------------------------------------------------------------------------------------------------
 322 // Method resolution
 323 //
 324 // According to JVM spec. $5.4.3c & $5.4.3d
 325 
 326 // Look up method in klasses, including static methods
 327 // Then look up local default methods
 328 Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 329                                                bool checkpolymorphism,
 330                                                bool in_imethod_resolve) {
 331   NoSafepointVerifier nsv;  // Method* returned may not be reclaimed
 332 
 333   Klass* klass = link_info.resolved_klass();
 334   Symbol* name = link_info.name();
 335   Symbol* signature = link_info.signature();
 336 
 337   // Ignore overpasses so statics can be found during resolution
 338   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 339 
 340   if (klass->is_array_klass()) {
 341     // Only consider klass and super klass for arrays
 342     return result;
 343   }
 344 
 345   InstanceKlass* ik = InstanceKlass::cast(klass);
 346 
 347   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 348   // ignore static and non-public methods of java.lang.Object,
 349   // like clone, finalize, registerNatives.
 350   if (in_imethod_resolve &&
 351       result != NULL &&
 352       ik->is_interface() &&
 353       (result->is_static() || !result->is_public()) &&
 354       result->method_holder() == SystemDictionary::Object_klass()) {
 355     result = NULL;
 356   }
 357 
 358   // Before considering default methods, check for an overpass in the
 359   // current class if a method has not been found.
 360   if (result == NULL) {
 361     result = ik->find_method(name, signature);
 362   }
 363 
 364   if (result == NULL) {
 365     Array<Method*>* default_methods = ik->default_methods();
 366     if (default_methods != NULL) {
 367       result = InstanceKlass::find_method(default_methods, name, signature);
 368     }
 369   }
 370 
 371   if (checkpolymorphism && result != NULL) {
 372     vmIntrinsics::ID iid = result->intrinsic_id();
 373     if (MethodHandles::is_signature_polymorphic(iid)) {
 374       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 375       return NULL;
 376     }
 377   }
 378   return result;
 379 }
 380 
 381 // returns first instance method
 382 // Looks up method in classes, then looks up local default methods
 383 Method* LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 384                                                         Symbol* name,
 385                                                         Symbol* signature,
 386                                                         Klass::PrivateLookupMode private_mode, TRAPS) {
 387   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
 388 
 389   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 390     Klass* super_klass = result->method_holder()->super();
 391     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
 392   }
 393 
 394   if (klass->is_array_klass()) {
 395     // Only consider klass and super klass for arrays
 396     return result;
 397   }
 398 
 399   if (result == NULL) {
 400     Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
 401     if (default_methods != NULL) {
 402       result = InstanceKlass::find_method(default_methods, name, signature);
 403       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 404     }
 405   }
 406   return result;
 407 }
 408 
 409 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
 410                                                    const methodHandle& resolved_method) {
 411 
 412   int vtable_index = Method::invalid_vtable_index;
 413   Symbol* name = resolved_method->name();
 414   Symbol* signature = resolved_method->signature();
 415   InstanceKlass* ik = InstanceKlass::cast(klass);
 416 
 417   // First check in default method array
 418   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 419     int index = InstanceKlass::find_method_index(ik->default_methods(),
 420                                                  name, signature, Klass::find_overpass,
 421                                                  Klass::find_static, Klass::find_private);
 422     if (index >= 0 ) {
 423       vtable_index = ik->default_vtable_indices()->at(index);
 424     }
 425   }
 426   if (vtable_index == Method::invalid_vtable_index) {
 427     // get vtable_index for miranda methods
 428     klassVtable vt = ik->vtable();
 429     vtable_index = vt.index_of_miranda(name, signature);
 430   }
 431   return vtable_index;
 432 }
 433 
 434 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
 435   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
 436 
 437   // Specify 'true' in order to skip default methods when searching the
 438   // interfaces.  Function lookup_method_in_klasses() already looked for
 439   // the method in the default methods table.
 440   return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::skip_defaults);
 441 }
 442 
 443 Method* LinkResolver::lookup_polymorphic_method(const LinkInfo& link_info,
 444                                                 Handle *appendix_result_or_null,
 445                                                 TRAPS) {
 446   Klass* klass = link_info.resolved_klass();
 447   Symbol* name = link_info.name();
 448   Symbol* full_signature = link_info.signature();
 449 
 450   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 451   if (TraceMethodHandles) {
 452     ResourceMark rm(THREAD);
 453     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 454                   vmIntrinsics::name_at(iid), klass->external_name(),
 455                   name->as_C_string(), full_signature->as_C_string());
 456   }
 457   if ((klass == SystemDictionary::MethodHandle_klass() ||
 458        klass == SystemDictionary::VarHandle_klass()) &&
 459       iid != vmIntrinsics::_none) {
 460     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 461       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 462       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 463       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 464       TempNewSymbol basic_signature =
 465         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
 466       if (TraceMethodHandles) {
 467         ResourceMark rm(THREAD);
 468         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
 469                       name->as_C_string(),
 470                       full_signature->as_C_string(),
 471                       basic_signature->as_C_string());
 472       }
 473       Method* result = SystemDictionary::find_method_handle_intrinsic(iid,
 474                                                               basic_signature,
 475                                                               CHECK_NULL);
 476       if (result != NULL) {
 477         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 478         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
 479         assert(basic_signature == result->signature(), "predict the result signature");
 480         if (TraceMethodHandles) {
 481           ttyLocker ttyl;
 482           tty->print("lookup_polymorphic_method => intrinsic ");
 483           result->print_on(tty);
 484         }
 485       }
 486       return result;
 487     } else if (iid == vmIntrinsics::_invokeGeneric
 488                && THREAD->can_call_java()
 489                && appendix_result_or_null != NULL) {
 490       // This is a method with type-checking semantics.
 491       // We will ask Java code to spin an adapter method for it.
 492       if (!MethodHandles::enabled()) {
 493         // Make sure the Java part of the runtime has been booted up.
 494         Klass* natives = SystemDictionary::MethodHandleNatives_klass();
 495         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
 496           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
 497                                             Handle(),
 498                                             Handle(),
 499                                             true,
 500                                             CHECK_NULL);
 501         }
 502       }
 503 
 504       Handle appendix;
 505       Handle method_type;
 506       Method* result = SystemDictionary::find_method_handle_invoker(
 507                                                             klass,
 508                                                             name,
 509                                                             full_signature,
 510                                                             link_info.current_klass(),
 511                                                             &appendix,
 512                                                             CHECK_NULL);
 513       if (TraceMethodHandles) {
 514         ttyLocker ttyl;
 515         tty->print("lookup_polymorphic_method => (via Java) ");
 516         result->print_on(tty);
 517         tty->print("  lookup_polymorphic_method => appendix = ");
 518         if (appendix.is_null())  tty->print_cr("(none)");
 519         else                     appendix->print_on(tty);
 520       }
 521       if (result != NULL) {
 522 #ifdef ASSERT
 523         ResourceMark rm(THREAD);
 524 
 525         TempNewSymbol basic_signature =
 526           MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL);
 527         int actual_size_of_params = result->size_of_parameters();
 528         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
 529         // +1 for MethodHandle.this, +1 for trailing MethodType
 530         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
 531         if (appendix.not_null())                                   expected_size_of_params += 1;
 532         if (actual_size_of_params != expected_size_of_params) {
 533           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 534           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 535           result->print();
 536         }
 537         assert(actual_size_of_params == expected_size_of_params,
 538                "%d != %d", actual_size_of_params, expected_size_of_params);
 539 #endif //ASSERT
 540 
 541         assert(appendix_result_or_null != NULL, "");
 542         (*appendix_result_or_null) = appendix;
 543       }
 544       return result;
 545     }
 546   }
 547   return NULL;
 548 }
 549 
 550 void LinkResolver::check_method_accessability(Klass* ref_klass,
 551                                               Klass* resolved_klass,
 552                                               Klass* sel_klass,
 553                                               const methodHandle& sel_method,
 554                                               TRAPS) {
 555 
 556   AccessFlags flags = sel_method->access_flags();
 557 
 558   // Special case:  arrays always override "clone". JVMS 2.15.
 559   // If the resolved klass is an array class, and the declaring class
 560   // is java.lang.Object and the method is "clone", set the flags
 561   // to public.
 562   //
 563   // We'll check for the method name first, as that's most likely
 564   // to be false (so we'll short-circuit out of these tests).
 565   if (sel_method->name() == vmSymbols::clone_name() &&
 566       sel_klass == SystemDictionary::Object_klass() &&
 567       resolved_klass->is_array_klass()) {
 568     // We need to change "protected" to "public".
 569     assert(flags.is_protected(), "clone not protected?");
 570     jint new_flags = flags.as_int();
 571     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 572     new_flags = new_flags | JVM_ACC_PUBLIC;
 573     flags.set_flags(new_flags);
 574   }
 575 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 576 
 577   bool can_access = Reflection::verify_member_access(ref_klass,
 578                                                      resolved_klass,
 579                                                      sel_klass,
 580                                                      flags,
 581                                                      true, false, CHECK);
 582   // Any existing exceptions that may have been thrown
 583   // have been allowed to propagate.
 584   if (!can_access) {
 585     ResourceMark rm(THREAD);
 586     stringStream ss;
 587     bool same_module = (sel_klass->module() == ref_klass->module());
 588     ss.print("class %s tried to access %s%s%smethod '%s' (%s%s%s)",



 589              ref_klass->external_name(),
 590              sel_method->is_abstract()  ? "abstract "  : "",
 591              sel_method->is_protected() ? "protected " : "",
 592              sel_method->is_private()   ? "private "   : "",
 593              sel_method->external_name(),
 594              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 595              (same_module) ? "" : "; ",
 596              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 597              );
 598 
 599     // For private access check if there was a problem with nest host
 600     // resolution, and if so report that as part of the message.
 601     if (sel_method->is_private()) {
 602       assert(ref_klass->is_instance_klass(), "must be");
 603       assert(sel_klass->is_instance_klass(), "must be");
 604       InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
 605       InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
 606       const char* nest_host_error_1 = ref_ik->nest_host_resolution_error();
 607       const char* nest_host_error_2 = sel_ik->nest_host_resolution_error();
 608       if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) {
 609         ss.print(", (%s%s%s)",
 610                  (nest_host_error_1 != NULL) ? nest_host_error_1 : "",
 611                  (nest_host_error_1 != NULL && nest_host_error_2 != NULL) ? ", " : "",
 612                  (nest_host_error_2 != NULL) ? nest_host_error_2 : "");
 613       }
 614     }
 615 
 616     Exceptions::fthrow(THREAD_AND_LOCATION,
 617                        vmSymbols::java_lang_IllegalAccessError(),
 618                        "%s",
 619                        ss.as_string()
 620                        );
 621     return;
 622   }
 623 }
 624 
 625 Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
 626                                                 const constantPoolHandle& pool, int index, TRAPS) {
 627   // This method is used only
 628   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 629   // and
 630   // (2) in Bytecode_invoke::static_target
 631   // It appears to fail when applied to an invokeinterface call site.
 632   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 633   // resolve klass
 634   if (code == Bytecodes::_invokedynamic) {
 635     Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
 636     Symbol* method_name = vmSymbols::invoke_name();
 637     Symbol* method_signature = pool->signature_ref_at(index);
 638     Klass*  current_klass = pool->pool_holder();
 639     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 640     return resolve_method(link_info, code, THREAD);
 641   }
 642 
 643   LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
 644   Klass* resolved_klass = link_info.resolved_klass();
 645 
 646   if (pool->has_preresolution()
 647       || (resolved_klass == SystemDictionary::MethodHandle_klass() &&
 648           MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
 649     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 650     if (result != NULL) {
 651       return result;
 652     }
 653   }
 654 
 655   if (code == Bytecodes::_invokeinterface) {
 656     return resolve_interface_method(link_info, code, THREAD);
 657   } else if (code == Bytecodes::_invokevirtual) {
 658     return resolve_method(link_info, code, THREAD);
 659   } else if (!resolved_klass->is_interface()) {
 660     return resolve_method(link_info, code, THREAD);
 661   } else {
 662     return resolve_interface_method(link_info, code, THREAD);
 663   }
 664 }
 665 
 666 // Check and print a loader constraint violation message for method or interface method
 667 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 668                                                    const methodHandle& resolved_method,
 669                                                    const char* method_type, TRAPS) {
 670   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 671   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 672 
 673   ResourceMark rm(THREAD);
 674   Symbol* failed_type_symbol =
 675     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 676                                               resolved_loader, true, CHECK);
 677   if (failed_type_symbol != NULL) {
 678     Klass* current_class = link_info.current_klass();
 679     ClassLoaderData* current_loader_data = current_class->class_loader_data();
 680     assert(current_loader_data != NULL, "current class has no class loader data");
 681     Klass* resolved_method_class = resolved_method->method_holder();
 682     ClassLoaderData* target_loader_data = resolved_method_class->class_loader_data();
 683     assert(target_loader_data != NULL, "resolved method's class has no class loader data");
 684 
 685     stringStream ss;
 686     ss.print("loader constraint violation: when resolving %s '", method_type);
 687     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 688     ss.print("' the class loader %s of the current class, %s,"
 689              " and the class loader %s for the method's defining class, %s, have"
 690              " different Class objects for the type %s used in the signature (%s; %s)",
 691              current_loader_data->loader_name_and_id(),
 692              current_class->name()->as_C_string(),
 693              target_loader_data->loader_name_and_id(),
 694              resolved_method_class->name()->as_C_string(),
 695              failed_type_symbol->as_C_string(),
 696              current_class->class_in_module_of_loader(false, true),
 697              resolved_method_class->class_in_module_of_loader(false, true));
 698     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 699   }
 700 }
 701 
 702 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 703                                                   Klass* current_klass,
 704                                                   Klass* sel_klass, TRAPS) {
 705   Handle ref_loader(THREAD, current_klass->class_loader());
 706   Handle sel_loader(THREAD, sel_klass->class_loader());
 707 
 708   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 709   Symbol* failed_type_symbol =
 710     SystemDictionary::check_signature_loaders(sig,
 711                                               ref_loader, sel_loader,
 712                                               false,
 713                                               CHECK);
 714   if (failed_type_symbol != NULL) {
 715     stringStream ss;
 716     const char* failed_type_name = failed_type_symbol->as_klass_external_name();
 717 
 718     ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
 719              "the class loader %s of the current class, %s, "
 720              "and the class loader %s for the field's defining %s, %s, "
 721              "have different Class objects for type %s (%s; %s)",
 722              field->as_C_string(),
 723              failed_type_name,
 724              current_klass->class_loader_data()->loader_name_and_id(),
 725              current_klass->external_name(),
 726              sel_klass->class_loader_data()->loader_name_and_id(),
 727              sel_klass->external_kind(),
 728              sel_klass->external_name(),
 729              failed_type_name,
 730              current_klass->class_in_module_of_loader(false, true),
 731              sel_klass->class_in_module_of_loader(false, true));
 732     THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
 733   }
 734 }
 735 
 736 Method* LinkResolver::resolve_method(const LinkInfo& link_info,
 737                                      Bytecodes::Code code, TRAPS) {
 738 
 739   Handle nested_exception;
 740   Klass* resolved_klass = link_info.resolved_klass();
 741 
 742   // 1. For invokevirtual, cannot call an interface method
 743   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 744     ResourceMark rm(THREAD);
 745     char buf[200];
 746     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 747         resolved_klass->external_name());
 748     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 749   }
 750 
 751   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 752   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 753     ResourceMark rm(THREAD);
 754     stringStream ss;
 755     ss.print("Method '");
 756     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 757     ss.print("' must be Methodref constant");
 758     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 759   }
 760 
 761   // 3. lookup method in resolved klass and its super klasses
 762   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 763 
 764   // 4. lookup method in all the interfaces implemented by the resolved klass
 765   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 766     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 767 
 768     if (resolved_method.is_null()) {
 769       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 770       Method* method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);
 771       resolved_method = methodHandle(THREAD, method);
 772       if (HAS_PENDING_EXCEPTION) {
 773         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 774         CLEAR_PENDING_EXCEPTION;
 775       }
 776     }
 777   }
 778 
 779   // 5. method lookup failed
 780   if (resolved_method.is_null()) {
 781     ResourceMark rm(THREAD);
 782     stringStream ss;
 783     ss.print("'");
 784     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 785     ss.print("'");
 786     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 787                      ss.as_string(), nested_exception, NULL);
 788   }
 789 
 790   // 6. access checks, access checking may be turned off when calling from within the VM.
 791   Klass* current_klass = link_info.current_klass();
 792   if (link_info.check_access()) {
 793     assert(current_klass != NULL , "current_klass should not be null");
 794 
 795     // check if method can be accessed by the referring class
 796     check_method_accessability(current_klass,
 797                                resolved_klass,
 798                                resolved_method->method_holder(),
 799                                resolved_method,
 800                                CHECK_NULL);
 801 
 802     // check loader constraints
 803     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 804   }
 805 
 806   return resolved_method();
 807 }
 808 
 809 static void trace_method_resolution(const char* prefix,
 810                                     Klass* klass,
 811                                     Klass* resolved_klass,
 812                                     Method* method,
 813                                     bool logitables,
 814                                     int index = -1) {
 815 #ifndef PRODUCT
 816   ResourceMark rm;
 817   Log(itables) logi;
 818   LogStream lsi(logi.trace());
 819   Log(vtables) logv;
 820   LogStream lsv(logv.trace());
 821   outputStream* st;
 822   if (logitables) {
 823     st = &lsi;
 824   } else {
 825     st = &lsv;
 826   }
 827   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 828             prefix,
 829             (klass == NULL ? "<NULL>" : klass->internal_name()),
 830             (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
 831             Method::name_and_sig_as_C_string(resolved_klass,
 832                                              method->name(),
 833                                              method->signature()),
 834             method->method_holder()->internal_name());
 835   method->print_linkage_flags(st);
 836   if (index != -1) {
 837     st->print("vtable_index:%d", index);
 838   }
 839   st->cr();
 840 #endif // PRODUCT
 841 }
 842 
 843 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 844 Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 845 
 846   Klass* resolved_klass = link_info.resolved_klass();
 847 
 848   // check if klass is interface
 849   if (!resolved_klass->is_interface()) {
 850     ResourceMark rm(THREAD);
 851     char buf[200];
 852     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 853     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 854   }
 855 
 856   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 857   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 858     ResourceMark rm(THREAD);
 859     stringStream ss;
 860     ss.print("Method '");
 861     Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
 862     ss.print("' must be InterfaceMethodref constant");
 863     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 864   }
 865 
 866   // lookup method in this interface or its super, java.lang.Object
 867   // JDK8: also look for static methods
 868   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 869 
 870   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 871     // lookup method in all the super-interfaces
 872     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 873   }
 874 
 875   if (resolved_method.is_null()) {
 876     // no method found
 877     ResourceMark rm(THREAD);
 878     stringStream ss;
 879     ss.print("'");
 880     Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
 881     ss.print("'");
 882     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), ss.as_string());
 883   }
 884 
 885   if (link_info.check_access()) {
 886     // JDK8 adds non-public interface methods, and accessability check requirement
 887     Klass* current_klass = link_info.current_klass();
 888 
 889     assert(current_klass != NULL , "current_klass should not be null");
 890 
 891     // check if method can be accessed by the referring class
 892     check_method_accessability(current_klass,
 893                                resolved_klass,
 894                                resolved_method->method_holder(),
 895                                resolved_method,
 896                                CHECK_NULL);
 897 
 898     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 899   }
 900 
 901   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 902     ResourceMark rm(THREAD);
 903     stringStream ss;
 904     ss.print("Expected instance not static method '");
 905     Method::print_external_name(&ss, resolved_klass,
 906                                 resolved_method->name(), resolved_method->signature());
 907     ss.print("'");
 908     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
 909   }
 910 
 911   if (log_develop_is_enabled(Trace, itables)) {
 912     char buf[200];
 913     jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
 914                  Bytecodes::name(code));
 915     trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);
 916   }
 917 
 918   return resolved_method();
 919 }
 920 
 921 //------------------------------------------------------------------------------------------------------------------------
 922 // Field resolution
 923 
 924 void LinkResolver::check_field_accessability(Klass* ref_klass,
 925                                              Klass* resolved_klass,
 926                                              Klass* sel_klass,
 927                                              const fieldDescriptor& fd,
 928                                              TRAPS) {
 929   bool can_access = Reflection::verify_member_access(ref_klass,
 930                                                      resolved_klass,
 931                                                      sel_klass,
 932                                                      fd.access_flags(),
 933                                                      true, false, CHECK);
 934   // Any existing exceptions that may have been thrown, for example LinkageErrors
 935   // from nest-host resolution, have been allowed to propagate.
 936   if (!can_access) {
 937     bool same_module = (sel_klass->module() == ref_klass->module());
 938     ResourceMark rm(THREAD);
 939     stringStream ss;
 940     ss.print("class %s tried to access %s%sfield %s.%s (%s%s%s)",


 941              ref_klass->external_name(),
 942              fd.is_protected() ? "protected " : "",
 943              fd.is_private()   ? "private "   : "",
 944              sel_klass->external_name(),
 945              fd.name()->as_C_string(),
 946              (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
 947              (same_module) ? "" : "; ",
 948              (same_module) ? "" : sel_klass->class_in_module_of_loader()
 949              );
 950     // For private access check if there was a problem with nest host
 951     // resolution, and if so report that as part of the message.
 952     if (fd.is_private()) {
 953       assert(ref_klass->is_instance_klass(), "must be");
 954       assert(sel_klass->is_instance_klass(), "must be");
 955       InstanceKlass* ref_ik = InstanceKlass::cast(ref_klass);
 956       InstanceKlass* sel_ik = InstanceKlass::cast(sel_klass);
 957       const char* nest_host_error_1 = ref_ik->nest_host_resolution_error();
 958       const char* nest_host_error_2 = sel_ik->nest_host_resolution_error();
 959       if (nest_host_error_1 != NULL || nest_host_error_2 != NULL) {
 960         ss.print(", (%s%s%s)",
 961                  (nest_host_error_1 != NULL) ? nest_host_error_1 : "",
 962                  (nest_host_error_1 != NULL && nest_host_error_2 != NULL) ? ", " : "",
 963                  (nest_host_error_2 != NULL) ? nest_host_error_2 : "");
 964       }
 965     }
 966     Exceptions::fthrow(THREAD_AND_LOCATION,
 967                        vmSymbols::java_lang_IllegalAccessError(),
 968                        "%s",
 969                        ss.as_string()
 970                        );
 971     return;
 972   }
 973 }
 974 
 975 void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, const methodHandle& method, Bytecodes::Code byte, TRAPS) {
 976   LinkInfo link_info(pool, index, method, CHECK);
 977   resolve_field(fd, link_info, byte, true, CHECK);
 978 }
 979 
 980 void LinkResolver::resolve_field(fieldDescriptor& fd,
 981                                  const LinkInfo& link_info,
 982                                  Bytecodes::Code byte, bool initialize_class,
 983                                  TRAPS) {
 984   assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
 985          byte == Bytecodes::_getfield  || byte == Bytecodes::_putfield  ||
 986          byte == Bytecodes::_nofast_getfield  || byte == Bytecodes::_nofast_putfield  ||
 987          (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode");
 988 
 989   bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
 990   bool is_put    = (byte == Bytecodes::_putfield  || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield);
 991   // Check if there's a resolved klass containing the field
 992   Klass* resolved_klass = link_info.resolved_klass();
 993   Symbol* field = link_info.name();
 994   Symbol* sig = link_info.signature();
 995 
 996   if (resolved_klass == NULL) {
 997     ResourceMark rm(THREAD);
 998     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
 999   }
1000 
1001   // Resolve instance field
1002   Klass* sel_klass = resolved_klass->find_field(field, sig, &fd);
1003   // check if field exists; i.e., if a klass containing the field def has been selected
1004   if (sel_klass == NULL) {
1005     ResourceMark rm(THREAD);
1006     THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
1007   }
1008 
1009   // Access checking may be turned off when calling from within the VM.
1010   Klass* current_klass = link_info.current_klass();
1011   if (link_info.check_access()) {
1012 
1013     // check access
1014     check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
1015 
1016     // check for errors
1017     if (is_static != fd.is_static()) {
1018       ResourceMark rm(THREAD);
1019       char msg[200];
1020       jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
1021       THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
1022     }
1023 
1024     // A final field can be modified only
1025     // (1) by methods declared in the class declaring the field and
1026     // (2) by the <clinit> method (in case of a static field)
1027     //     or by the <init> method (in case of an instance field).
1028     if (is_put && fd.access_flags().is_final()) {
1029       ResourceMark rm(THREAD);
1030       stringStream ss;
1031 
1032       if (sel_klass != current_klass) {
1033         ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
1034                  is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1035                 current_klass->external_name());
1036         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1037       }
1038 
1039       if (fd.constants()->pool_holder()->major_version() >= 53) {
1040         Method* m = link_info.current_method();
1041         assert(m != NULL, "information about the current method must be available for 'put' bytecodes");
1042         bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1043                                                    fd.is_static() &&
1044                                                    !m->is_static_initializer());
1045         bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1046                                                      !fd.is_static() &&
1047                                                      !m->is_object_initializer());
1048 
1049         if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1050           ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1051                    is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1052                    m->name()->as_C_string(),
1053                    is_static ? "<clinit>" : "<init>");
1054           THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1055         }
1056       }
1057     }
1058 
1059     // initialize resolved_klass if necessary
1060     // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1061     //         according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1062     //
1063     // note 2: we don't want to force initialization if we are just checking
1064     //         if the field access is legal; e.g., during compilation
1065     if (is_static && initialize_class) {
1066       sel_klass->initialize(CHECK);
1067     }
1068   }
1069 
1070   if ((sel_klass != current_klass) && (current_klass != NULL)) {
1071     check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1072   }
1073 
1074   // return information. note that the klass is set to the actual klass containing the
1075   // field, otherwise access of static fields in superclasses will not work.
1076 }
1077 
1078 
1079 //------------------------------------------------------------------------------------------------------------------------
1080 // Invoke resolution
1081 //
1082 // Naming conventions:
1083 //
1084 // resolved_method    the specified method (i.e., static receiver specified via constant pool index)
1085 // sel_method         the selected method  (selected via run-time lookup; e.g., based on dynamic receiver class)
1086 // resolved_klass     the specified klass  (i.e., specified via constant pool index)
1087 // recv_klass         the receiver klass
1088 
1089 
1090 void LinkResolver::resolve_static_call(CallInfo& result,
1091                                        const LinkInfo& link_info,
1092                                        bool initialize_class, TRAPS) {
1093   Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1094 
1095   // The resolved class can change as a result of this resolution.
1096   Klass* resolved_klass = resolved_method->method_holder();
1097 
1098   // Initialize klass (this should only happen if everything is ok)
1099   if (initialize_class && resolved_klass->should_be_initialized()) {
1100     resolved_klass->initialize(CHECK);
1101     // Use updated LinkInfo to reresolve with resolved method holder
1102     LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1103                       link_info.current_klass(),
1104                       link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1105     resolved_method = linktime_resolve_static_method(new_info, CHECK);
1106   }
1107 
1108   // setup result
1109   result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1110 }
1111 
1112 // throws linktime exceptions
1113 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1114 
1115   Klass* resolved_klass = link_info.resolved_klass();
1116   Method* resolved_method;
1117   if (!resolved_klass->is_interface()) {
1118     resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1119   } else {
1120     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1121   }
1122   assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1123 
1124   // check if static
1125   if (!resolved_method->is_static()) {
1126     ResourceMark rm(THREAD);
1127     stringStream ss;
1128     ss.print("Expected static method '");
1129     resolved_method->print_external_name(&ss);
1130     ss.print("'");
1131     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1132   }
1133   return resolved_method;
1134 }
1135 
1136 
1137 void LinkResolver::resolve_special_call(CallInfo& result,
1138                                         Handle recv,
1139                                         const LinkInfo& link_info,
1140                                         TRAPS) {
1141   Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1142   runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1143 }
1144 
1145 // throws linktime exceptions
1146 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1147 
1148   // Invokespecial is called for multiple special reasons:
1149   // <init>
1150   // local private method invocation, for classes and interfaces
1151   // superclass.method, which can also resolve to a default method
1152   // and the selected method is recalculated relative to the direct superclass
1153   // superinterface.method, which explicitly does not check shadowing
1154   Klass* resolved_klass = link_info.resolved_klass();
1155   Method* resolved_method;
1156 
1157   if (!resolved_klass->is_interface()) {
1158     resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1159   } else {
1160     resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1161   }
1162 
1163   // check if method name is <init>, that it is found in same klass as static type
1164   if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1165       resolved_method->method_holder() != resolved_klass) {
1166     ResourceMark rm(THREAD);
1167     stringStream ss;
1168     ss.print("%s: method '", resolved_klass->external_name());
1169     resolved_method->signature()->print_as_signature_external_return_type(&ss);
1170     ss.print(" %s(", resolved_method->name()->as_C_string());
1171     resolved_method->signature()->print_as_signature_external_parameters(&ss);
1172     ss.print(")' not found");
1173     Exceptions::fthrow(
1174       THREAD_AND_LOCATION,
1175       vmSymbols::java_lang_NoSuchMethodError(),
1176       "%s", ss.as_string());
1177     return NULL;
1178   }
1179 
1180   // ensure that invokespecial's interface method reference is in
1181   // a direct superinterface, not an indirect superinterface
1182   Klass* current_klass = link_info.current_klass();
1183   if (current_klass != NULL && resolved_klass->is_interface()) {
1184     InstanceKlass* ck = InstanceKlass::cast(current_klass);
1185     InstanceKlass *klass_to_check = !ck->is_unsafe_anonymous() ?
1186                                     ck :
1187                                     ck->unsafe_anonymous_host();
1188     // Disable verification for the dynamically-generated reflection bytecodes.
1189     bool is_reflect = klass_to_check->is_subclass_of(
1190                         SystemDictionary::reflect_MagicAccessorImpl_klass());
1191 
1192     if (!is_reflect &&
1193         !klass_to_check->is_same_or_direct_interface(resolved_klass)) {
1194       ResourceMark rm(THREAD);
1195       stringStream ss;
1196       ss.print("Interface method reference: '");
1197       resolved_method->print_external_name(&ss);
1198       ss.print("', is in an indirect superinterface of %s",
1199                current_klass->external_name());
1200       THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1201     }
1202   }
1203 
1204   // check if not static
1205   if (resolved_method->is_static()) {
1206     ResourceMark rm(THREAD);
1207     stringStream ss;
1208     ss.print("Expecting non-static method '");
1209     resolved_method->print_external_name(&ss);
1210     ss.print("'");
1211     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1212   }
1213 
1214   if (log_develop_is_enabled(Trace, itables)) {
1215     trace_method_resolution("invokespecial resolved method: caller-class:",
1216                             current_klass, resolved_klass, resolved_method, true);
1217   }
1218 
1219   return resolved_method;
1220 }
1221 
1222 // throws runtime exceptions
1223 void LinkResolver::runtime_resolve_special_method(CallInfo& result,
1224                                                   const LinkInfo& link_info,
1225                                                   const methodHandle& resolved_method,
1226                                                   Handle recv, TRAPS) {
1227 
1228   Klass* resolved_klass = link_info.resolved_klass();
1229 
1230   // resolved method is selected method unless we have an old-style lookup
1231   // for a superclass method
1232   // Invokespecial for a superinterface, resolved method is selected method,
1233   // no checks for shadowing
1234   methodHandle sel_method(THREAD, resolved_method());
1235 
1236   if (link_info.check_access() &&
1237       // check if the method is not <init>
1238       resolved_method->name() != vmSymbols::object_initializer_name()) {
1239 
1240     Klass* current_klass = link_info.current_klass();
1241 
1242     // Check if the class of the resolved_klass is a superclass
1243     // (not supertype in order to exclude interface classes) of the current class.
1244     // This check is not performed for super.invoke for interface methods
1245     // in super interfaces.
1246     if (current_klass->is_subclass_of(resolved_klass) &&
1247         current_klass != resolved_klass) {
1248       // Lookup super method
1249       Klass* super_klass = current_klass->super();
1250       Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1251                                                      resolved_method->name(),
1252                                                      resolved_method->signature(),
1253                                                      Klass::find_private, CHECK);
1254       sel_method = methodHandle(THREAD, instance_method);
1255 
1256       // check if found
1257       if (sel_method.is_null()) {
1258         ResourceMark rm(THREAD);
1259         stringStream ss;
1260         ss.print("'");
1261         resolved_method->print_external_name(&ss);
1262         ss.print("'");
1263         THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1264       // check loader constraints if found a different method
1265       } else if (sel_method() != resolved_method()) {
1266         check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1267       }
1268     }
1269 
1270     // Check that the class of objectref (the receiver) is the current class or interface,
1271     // or a subtype of the current class or interface (the sender), otherwise invokespecial
1272     // throws IllegalAccessError.
1273     // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1274     // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1275     // a class.  If the sender is an interface, the check has to be performed at runtime.
1276     InstanceKlass* sender = InstanceKlass::cast(current_klass);
1277     sender = sender->is_unsafe_anonymous() ? sender->unsafe_anonymous_host() : sender;
1278     if (sender->is_interface() && recv.not_null()) {
1279       Klass* receiver_klass = recv->klass();
1280       if (!receiver_klass->is_subtype_of(sender)) {
1281         ResourceMark rm(THREAD);
1282         char buf[500];
1283         jio_snprintf(buf, sizeof(buf),
1284                      "Receiver class %s must be the current class or a subtype of interface %s",
1285                      receiver_klass->external_name(),
1286                      sender->external_name());
1287         THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), buf);
1288       }
1289     }
1290   }
1291 
1292   // check if not static
1293   if (sel_method->is_static()) {
1294     ResourceMark rm(THREAD);
1295     stringStream ss;
1296     ss.print("Expecting non-static method '");
1297     resolved_method->print_external_name(&ss);
1298     ss.print("'");
1299     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1300   }
1301 
1302   // check if abstract
1303   if (sel_method->is_abstract()) {
1304     ResourceMark rm(THREAD);
1305     stringStream ss;
1306     ss.print("'");
1307     Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1308     ss.print("'");
1309     THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1310   }
1311 
1312   if (log_develop_is_enabled(Trace, itables)) {
1313     trace_method_resolution("invokespecial selected method: resolved-class:",
1314                             resolved_klass, resolved_klass, sel_method(), true);
1315   }
1316 
1317   // setup result
1318   result.set_static(resolved_klass, sel_method, CHECK);
1319 }
1320 
1321 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1322                                         const LinkInfo& link_info,
1323                                         bool check_null_and_abstract, TRAPS) {
1324   Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1325   runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1326                                  link_info.resolved_klass(),
1327                                  recv, receiver_klass,
1328                                  check_null_and_abstract, CHECK);
1329 }
1330 
1331 // throws linktime exceptions
1332 Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1333                                                            TRAPS) {
1334   // normal method resolution
1335   Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1336 
1337   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1338   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1339 
1340   // check if private interface method
1341   Klass* resolved_klass = link_info.resolved_klass();
1342   Klass* current_klass = link_info.current_klass();
1343 
1344   // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1345   if (resolved_klass->is_interface() && resolved_method->is_private()) {
1346     ResourceMark rm(THREAD);
1347     stringStream ss;
1348     ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1349     resolved_method->print_external_name(&ss);
1350     ss.print("', caller-class: %s",
1351              (current_klass == NULL ? "<null>" : current_klass->internal_name()));
1352     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1353   }
1354 
1355   // check if not static
1356   if (resolved_method->is_static()) {
1357     ResourceMark rm(THREAD);
1358     stringStream ss;
1359     ss.print("Expecting non-static method '");
1360     resolved_method->print_external_name(&ss);
1361     ss.print("'");
1362     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1363   }
1364 
1365   if (log_develop_is_enabled(Trace, vtables)) {
1366     trace_method_resolution("invokevirtual resolved method: caller-class:",
1367                             current_klass, resolved_klass, resolved_method, false);
1368   }
1369 
1370   return resolved_method;
1371 }
1372 
1373 // throws runtime exceptions
1374 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1375                                                   const methodHandle& resolved_method,
1376                                                   Klass* resolved_klass,
1377                                                   Handle recv,
1378                                                   Klass* recv_klass,
1379                                                   bool check_null_and_abstract,
1380                                                   TRAPS) {
1381 
1382   // setup default return values
1383   int vtable_index = Method::invalid_vtable_index;
1384   methodHandle selected_method;
1385 
1386   // runtime method resolution
1387   if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1388     THROW(vmSymbols::java_lang_NullPointerException());
1389   }
1390 
1391   // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1392   // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1393   // a missing receiver might result in a bogus lookup.
1394   assert(resolved_method->method_holder()->is_linked(), "must be linked");
1395 
1396   // do lookup based on receiver klass using the vtable index
1397   if (resolved_method->method_holder()->is_interface()) { // default or miranda method
1398     vtable_index = vtable_index_of_interface_method(resolved_klass, resolved_method);
1399     assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1400 
1401     selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1402   } else {
1403     // at this point we are sure that resolved_method is virtual and not
1404     // a default or miranda method; therefore, it must have a valid vtable index.
1405     assert(!resolved_method->has_itable_index(), "");
1406     vtable_index = resolved_method->vtable_index();
1407     // We could get a negative vtable_index of nonvirtual_vtable_index for private
1408     // methods, or for final methods. Private methods never appear in the vtable
1409     // and never override other methods. As an optimization, final methods are
1410     // never put in the vtable, unless they override an existing method.
1411     // So if we do get nonvirtual_vtable_index, it means the selected method is the
1412     // resolved method, and it can never be changed by an override.
1413     if (vtable_index == Method::nonvirtual_vtable_index) {
1414       assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1415       selected_method = resolved_method;
1416     } else {
1417       selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1418     }
1419   }
1420 
1421   // check if method exists
1422   if (selected_method.is_null()) {
1423     throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1424   }
1425 
1426   // check if abstract
1427   if (check_null_and_abstract && selected_method->is_abstract()) {
1428     // Pass arguments for generating a verbose error message.
1429     throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1430   }
1431 
1432   if (log_develop_is_enabled(Trace, vtables)) {
1433     trace_method_resolution("invokevirtual selected method: receiver-class:",
1434                             recv_klass, resolved_klass, selected_method(),
1435                             false, vtable_index);
1436   }
1437   // setup result
1438   result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1439 }
1440 
1441 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1442                                           const LinkInfo& link_info,
1443                                           bool check_null_and_abstract, TRAPS) {
1444   // throws linktime exceptions
1445   Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1446   methodHandle mh(THREAD, resolved_method);
1447   runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1448                                    recv, recv_klass, check_null_and_abstract, CHECK);
1449 }
1450 
1451 Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1452                                                              TRAPS) {
1453   // normal interface method resolution
1454   Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1455   assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1456   assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1457 
1458   return resolved_method;
1459 }
1460 
1461 // throws runtime exceptions
1462 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1463                                                     const methodHandle& resolved_method,
1464                                                     Klass* resolved_klass,
1465                                                     Handle recv,
1466                                                     Klass* recv_klass,
1467                                                     bool check_null_and_abstract, TRAPS) {
1468 
1469   // check if receiver exists
1470   if (check_null_and_abstract && recv.is_null()) {
1471     THROW(vmSymbols::java_lang_NullPointerException());
1472   }
1473 
1474   // check if receiver klass implements the resolved interface
1475   if (!recv_klass->is_subtype_of(resolved_klass)) {
1476     ResourceMark rm(THREAD);
1477     char buf[200];
1478     jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1479                  recv_klass->external_name(),
1480                  resolved_klass->external_name());
1481     THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1482   }
1483 
1484   methodHandle selected_method = resolved_method;
1485 
1486   // resolve the method in the receiver class, unless it is private
1487   if (!resolved_method()->is_private()) {
1488     // do lookup based on receiver klass
1489     // This search must match the linktime preparation search for itable initialization
1490     // to correctly enforce loader constraints for interface method inheritance.
1491     // Private methods are skipped as the resolved method was not private.
1492     Method* method = lookup_instance_method_in_klasses(recv_klass,
1493                                                        resolved_method->name(),
1494                                                        resolved_method->signature(),
1495                                                        Klass::skip_private, CHECK);
1496     selected_method = methodHandle(THREAD, method);
1497 
1498     if (selected_method.is_null() && !check_null_and_abstract) {
1499       // In theory this is a harmless placeholder value, but
1500       // in practice leaving in null affects the nsk default method tests.
1501       // This needs further study.
1502       selected_method = resolved_method;
1503     }
1504     // check if method exists
1505     if (selected_method.is_null()) {
1506       // Pass arguments for generating a verbose error message.
1507       throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1508     }
1509     // check access
1510     // Throw Illegal Access Error if selected_method is not public.
1511     if (!selected_method->is_public()) {
1512       ResourceMark rm(THREAD);
1513       stringStream ss;
1514       ss.print("'");
1515       Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1516       ss.print("'");
1517       THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1518     }
1519     // check if abstract
1520     if (check_null_and_abstract && selected_method->is_abstract()) {
1521       throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1522     }
1523   }
1524 
1525   if (log_develop_is_enabled(Trace, itables)) {
1526     trace_method_resolution("invokeinterface selected method: receiver-class:",
1527                             recv_klass, resolved_klass, selected_method(), true);
1528   }
1529   // setup result
1530   if (resolved_method->has_vtable_index()) {
1531     int vtable_index = resolved_method->vtable_index();
1532     log_develop_trace(itables)("  -- vtable index: %d", vtable_index);
1533     assert(vtable_index == selected_method->vtable_index(), "sanity check");
1534     result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1535   } else if (resolved_method->has_itable_index()) {
1536     int itable_index = resolved_method()->itable_index();
1537     log_develop_trace(itables)("  -- itable index: %d", itable_index);
1538     result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK);
1539   } else {
1540     int index = resolved_method->vtable_index();
1541     log_develop_trace(itables)("  -- non itable/vtable index: %d", index);
1542     assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1543     assert(resolved_method()->is_private() ||
1544            (resolved_method()->is_final() && resolved_method->method_holder() == SystemDictionary::Object_klass()),
1545            "Should only have non-virtual invokeinterface for private or final-Object methods!");
1546     assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1547     // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1548     result.set_virtual(resolved_klass, resolved_klass, resolved_method, resolved_method, index, CHECK);
1549   }
1550 }
1551 
1552 
1553 Method* LinkResolver::linktime_resolve_interface_method_or_null(
1554                                                  const LinkInfo& link_info) {
1555   EXCEPTION_MARK;
1556   Method* method_result = linktime_resolve_interface_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::linktime_resolve_virtual_method_or_null(
1566                                                  const LinkInfo& link_info) {
1567   EXCEPTION_MARK;
1568   Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1569   if (HAS_PENDING_EXCEPTION) {
1570     CLEAR_PENDING_EXCEPTION;
1571     return NULL;
1572   } else {
1573     return method_result;
1574   }
1575 }
1576 
1577 Method* LinkResolver::resolve_virtual_call_or_null(
1578                                                  Klass* receiver_klass,
1579                                                  const LinkInfo& link_info) {
1580   EXCEPTION_MARK;
1581   CallInfo info;
1582   resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1583   if (HAS_PENDING_EXCEPTION) {
1584     CLEAR_PENDING_EXCEPTION;
1585     return NULL;
1586   }
1587   return info.selected_method();
1588 }
1589 
1590 Method* LinkResolver::resolve_interface_call_or_null(
1591                                                  Klass* receiver_klass,
1592                                                  const LinkInfo& link_info) {
1593   EXCEPTION_MARK;
1594   CallInfo info;
1595   resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1596   if (HAS_PENDING_EXCEPTION) {
1597     CLEAR_PENDING_EXCEPTION;
1598     return NULL;
1599   }
1600   return info.selected_method();
1601 }
1602 
1603 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1604                                                const LinkInfo& link_info) {
1605   EXCEPTION_MARK;
1606   CallInfo info;
1607   resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1608                        /*check_null_or_abstract*/false, THREAD);
1609   if (HAS_PENDING_EXCEPTION) {
1610     CLEAR_PENDING_EXCEPTION;
1611     return Method::invalid_vtable_index;
1612   }
1613   return info.vtable_index();
1614 }
1615 
1616 Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1617   EXCEPTION_MARK;
1618   CallInfo info;
1619   resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1620   if (HAS_PENDING_EXCEPTION) {
1621     CLEAR_PENDING_EXCEPTION;
1622     return NULL;
1623   }
1624   return info.selected_method();
1625 }
1626 
1627 Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1628   EXCEPTION_MARK;
1629   CallInfo info;
1630   resolve_special_call(info, Handle(), link_info, THREAD);
1631   if (HAS_PENDING_EXCEPTION) {
1632     CLEAR_PENDING_EXCEPTION;
1633     return NULL;
1634   }
1635   return info.selected_method();
1636 }
1637 
1638 
1639 
1640 //------------------------------------------------------------------------------------------------------------------------
1641 // ConstantPool entries
1642 
1643 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1644   switch (byte) {
1645     case Bytecodes::_invokestatic   : resolve_invokestatic   (result,       pool, index, CHECK); break;
1646     case Bytecodes::_invokespecial  : resolve_invokespecial  (result, recv, pool, index, CHECK); break;
1647     case Bytecodes::_invokevirtual  : resolve_invokevirtual  (result, recv, pool, index, CHECK); break;
1648     case Bytecodes::_invokehandle   : resolve_invokehandle   (result,       pool, index, CHECK); break;
1649     case Bytecodes::_invokedynamic  : resolve_invokedynamic  (result,       pool, index, CHECK); break;
1650     case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1651     default                         :                                                            break;
1652   }
1653   return;
1654 }
1655 
1656 void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv,
1657                              const methodHandle& attached_method,
1658                              Bytecodes::Code byte, TRAPS) {
1659   Klass* defc = attached_method->method_holder();
1660   Symbol* name = attached_method->name();
1661   Symbol* type = attached_method->signature();
1662   LinkInfo link_info(defc, name, type);
1663   switch(byte) {
1664     case Bytecodes::_invokevirtual:
1665       resolve_virtual_call(result, recv, recv->klass(), link_info,
1666                            /*check_null_and_abstract=*/true, CHECK);
1667       break;
1668     case Bytecodes::_invokeinterface:
1669       resolve_interface_call(result, recv, recv->klass(), link_info,
1670                              /*check_null_and_abstract=*/true, CHECK);
1671       break;
1672     case Bytecodes::_invokestatic:
1673       resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK);
1674       break;
1675     case Bytecodes::_invokespecial:
1676       resolve_special_call(result, recv, link_info, CHECK);
1677       break;
1678     default:
1679       fatal("bad call: %s", Bytecodes::name(byte));
1680       break;
1681   }
1682 }
1683 
1684 void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1685   LinkInfo link_info(pool, index, CHECK);
1686   resolve_static_call(result, link_info, /*initialize_class*/true, CHECK);
1687 }
1688 
1689 
1690 void LinkResolver::resolve_invokespecial(CallInfo& result, Handle recv,
1691                                          const constantPoolHandle& pool, int index, TRAPS) {
1692   LinkInfo link_info(pool, index, CHECK);
1693   resolve_special_call(result, recv, link_info, CHECK);
1694 }
1695 
1696 
1697 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1698                                           const constantPoolHandle& pool, int index,
1699                                           TRAPS) {
1700 
1701   LinkInfo link_info(pool, index, CHECK);
1702   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1703   resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK);
1704 }
1705 
1706 
1707 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) {
1708   LinkInfo link_info(pool, index, CHECK);
1709   Klass* recvrKlass = recv.is_null() ? (Klass*)NULL : recv->klass();
1710   resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK);
1711 }
1712 
1713 
1714 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1715   // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1716   LinkInfo link_info(pool, index, CHECK);
1717   if (TraceMethodHandles) {
1718     ResourceMark rm(THREAD);
1719     tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1720                   link_info.signature()->as_C_string());
1721   }
1722   resolve_handle_call(result, link_info, CHECK);
1723 }
1724 
1725 void LinkResolver::resolve_handle_call(CallInfo& result,
1726                                        const LinkInfo& link_info,
1727                                        TRAPS) {
1728   // JSR 292:  this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1729   Klass* resolved_klass = link_info.resolved_klass();
1730   assert(resolved_klass == SystemDictionary::MethodHandle_klass() ||
1731          resolved_klass == SystemDictionary::VarHandle_klass(), "");
1732   assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1733   Handle       resolved_appendix;
1734   Method* resolved_method = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1735   result.set_handle(resolved_klass, methodHandle(THREAD, resolved_method), resolved_appendix, CHECK);
1736 }
1737 
1738 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1739   ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(indy_index);
1740   int pool_index = cpce->constant_pool_index();
1741 
1742   // Resolve the bootstrap specifier (BSM + optional arguments).
1743   BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
1744 
1745   // Check if CallSite has been bound already or failed already, and short circuit:
1746   {
1747     bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1748     if (is_done) return;
1749   }
1750 
1751   // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1752   // reference to a method handle which will be the bootstrap method for a dynamic
1753   // call site.  If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1754   // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1755   // method's CP index for the CONSTANT_MethodHandle_info.  So, there is no need to
1756   // set the indy_rf flag since any subsequent invokedynamic instruction which shares
1757   // this bootstrap method will encounter the resolution of MethodHandleInError.
1758 
1759   resolve_dynamic_call(result, bootstrap_specifier, CHECK);
1760 
1761   if (TraceMethodHandles) {
1762     bootstrap_specifier.print_msg_on(tty, "resolve_invokedynamic");
1763   }
1764 
1765   // The returned linkage result is provisional up to the moment
1766   // the interpreter or runtime performs a serialized check of
1767   // the relevant CPCE::f1 field.  This is done by the caller
1768   // of this method, via CPCE::set_dynamic_call, which uses
1769   // an ObjectLocker to do the final serialization of updates
1770   // to CPCE state, including f1.
1771 }
1772 
1773 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1774                                         BootstrapInfo& bootstrap_specifier,
1775                                         TRAPS) {
1776   // JSR 292:  this must resolve to an implicitly generated method
1777   // such as MH.linkToCallSite(*...) or some other call-site shape.
1778   // The appendix argument is likely to be a freshly-created CallSite.
1779   // It may also be a MethodHandle from an unwrapped ConstantCallSite,
1780   // or any other reference.  The resolved_method as well as the appendix
1781   // are both recorded together via CallInfo::set_handle.
1782   SystemDictionary::invoke_bootstrap_method(bootstrap_specifier, THREAD);
1783   Exceptions::wrap_dynamic_exception(THREAD);
1784 
1785   if (HAS_PENDING_EXCEPTION) {
1786     if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1787       // Let any random low-level IE or SOE or OOME just bleed through.
1788       // Basically we pretend that the bootstrap method was never called,
1789       // if it fails this way:  We neither record a successful linkage,
1790       // nor do we memorize a LE for posterity.
1791       return;
1792     }
1793     // JVMS 5.4.3 says: If an attempt by the Java Virtual Machine to resolve
1794     // a symbolic reference fails because an error is thrown that is an
1795     // instance of LinkageError (or a subclass), then subsequent attempts to
1796     // resolve the reference always fail with the same error that was thrown
1797     // as a result of the initial resolution attempt.
1798      bool recorded_res_status = bootstrap_specifier.save_and_throw_indy_exc(CHECK);
1799      if (!recorded_res_status) {
1800        // Another thread got here just before we did.  So, either use the method
1801        // that it resolved or throw the LinkageError exception that it threw.
1802        bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1803        if (is_done) return;
1804      }
1805      assert(bootstrap_specifier.invokedynamic_cp_cache_entry()->indy_resolution_failed(),
1806             "Resolution failure flag wasn't set");
1807   }
1808 
1809   bootstrap_specifier.resolve_newly_linked_invokedynamic(result, CHECK);
1810   // Exceptions::wrap_dynamic_exception not used because
1811   // set_handle doesn't throw linkage errors
1812 }
1813 
1814 // Selected method is abstract.
1815 void LinkResolver::throw_abstract_method_error(const methodHandle& resolved_method,
1816                                                const methodHandle& selected_method,
1817                                                Klass *recv_klass, TRAPS) {
1818   Klass *resolved_klass = resolved_method->method_holder();
1819   ResourceMark rm(THREAD);
1820   stringStream ss;
1821 
1822   if (recv_klass != NULL) {
1823     ss.print("Receiver class %s does not define or inherit an "
1824              "implementation of the",
1825              recv_klass->external_name());
1826   } else {
1827     ss.print("Missing implementation of");
1828   }
1829 
1830   assert(resolved_method.not_null(), "Sanity");
1831   ss.print(" resolved method '%s%s",
1832            resolved_method->is_abstract() ? "abstract " : "",
1833            resolved_method->is_private()  ? "private "  : "");
1834   resolved_method->signature()->print_as_signature_external_return_type(&ss);
1835   ss.print(" %s(", resolved_method->name()->as_C_string());
1836   resolved_method->signature()->print_as_signature_external_parameters(&ss);
1837   ss.print(")' of %s %s.",
1838            resolved_klass->external_kind(),
1839            resolved_klass->external_name());
1840 
1841   if (selected_method.not_null() && !(resolved_method == selected_method)) {
1842     ss.print(" Selected method is '%s%s",
1843              selected_method->is_abstract() ? "abstract " : "",
1844              selected_method->is_private()  ? "private "  : "");
1845     selected_method->print_external_name(&ss);
1846     ss.print("'.");
1847   }
1848 
1849   THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1850 }
--- EOF ---