1 /*
   2  * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "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/compileBroker.hpp"
  34 #include "gc/shared/collectedHeap.inline.hpp"
  35 #include "interpreter/bytecode.hpp"
  36 #include "interpreter/interpreterRuntime.hpp"
  37 #include "interpreter/linkResolver.hpp"
  38 #include "logging/log.hpp"
  39 #include "logging/logStream.hpp"
  40 #include "memory/resourceArea.hpp"
  41 #include "memory/universe.inline.hpp"
  42 #include "oops/instanceKlass.hpp"
  43 #include "oops/method.hpp"
  44 #include "oops/objArrayOop.hpp"
  45 #include "oops/oop.inline.hpp"
  46 #include "prims/methodHandles.hpp"
  47 #include "prims/nativeLookup.hpp"
  48 #include "runtime/compilationPolicy.hpp"
  49 #include "runtime/fieldDescriptor.hpp"
  50 #include "runtime/frame.inline.hpp"
  51 #include "runtime/handles.inline.hpp"
  52 #include "runtime/reflection.hpp"
  53 #include "runtime/signature.hpp"
  54 #include "runtime/thread.inline.hpp"
  55 #include "runtime/vmThread.hpp"
  56 
  57 
  58 //------------------------------------------------------------------------------------------------------------------------
  59 // Implementation of CallInfo
  60 
  61 
  62 void CallInfo::set_static(Klass* resolved_klass, const methodHandle& resolved_method, TRAPS) {
  63   int vtable_index = Method::nonvirtual_vtable_index;
  64   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
  65 }
  66 
  67 
  68 void CallInfo::set_interface(Klass* resolved_klass,
  69                              Klass* selected_klass,
  70                              const methodHandle& resolved_method,
  71                              const methodHandle& selected_method,
  72                              int itable_index, TRAPS) {
  73   // This is only called for interface methods. If the resolved_method
  74   // comes from java/lang/Object, it can be the subject of a virtual call, so
  75   // we should pick the vtable index from the resolved method.
  76   // In that case, the caller must call set_virtual instead of set_interface.
  77   assert(resolved_method->method_holder()->is_interface(), "");
  78   assert(itable_index == resolved_method()->itable_index(), "");
  79   set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK);
  80 }
  81 
  82 void CallInfo::set_virtual(Klass* resolved_klass,
  83                            Klass* selected_klass,
  84                            const methodHandle& resolved_method,
  85                            const methodHandle& selected_method,
  86                            int vtable_index, TRAPS) {
  87   assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index");
  88   assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), "");
  89   CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call);
  90   set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK);
  91   assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call");
  92 }
  93 
  94 void CallInfo::set_handle(const methodHandle& resolved_method,
  95                           Handle resolved_appendix,
  96                           Handle resolved_method_type, TRAPS) {
  97   set_handle(SystemDictionary::MethodHandle_klass(), resolved_method, resolved_appendix, resolved_method_type, CHECK);
  98 }
  99 
 100 void CallInfo::set_handle(Klass* resolved_klass,
 101                           const methodHandle& resolved_method,
 102                           Handle resolved_appendix,
 103                           Handle resolved_method_type, TRAPS) {
 104   if (resolved_method.is_null()) {
 105     THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null");
 106   }
 107   assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic ||
 108          resolved_method->is_compiled_lambda_form(),
 109          "linkMethod must return one of these");
 110   int vtable_index = Method::nonvirtual_vtable_index;
 111   assert(!resolved_method->has_vtable_index(), "");
 112   set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK);
 113   _resolved_appendix    = resolved_appendix;
 114   _resolved_method_type = resolved_method_type;
 115 }
 116 
 117 void CallInfo::set_common(Klass* resolved_klass,
 118                           Klass* selected_klass,
 119                           const methodHandle& resolved_method,
 120                           const methodHandle& selected_method,
 121                           CallKind kind,
 122                           int index,
 123                           TRAPS) {
 124   assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond");
 125   _resolved_klass  = resolved_klass;
 126   _selected_klass  = selected_klass;
 127   _resolved_method = resolved_method;
 128   _selected_method = selected_method;
 129   _call_kind       = kind;
 130   _call_index      = index;
 131   _resolved_appendix = Handle();
 132   DEBUG_ONLY(verify());  // verify before making side effects
 133 
 134   CompilationPolicy::compile_if_required(selected_method, THREAD);
 135 }
 136 
 137 // utility query for unreflecting a method
 138 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
 139   Klass* resolved_method_holder = resolved_method->method_holder();
 140   if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
 141     resolved_klass = resolved_method_holder;
 142   }
 143   _resolved_klass  = resolved_klass;
 144   _selected_klass  = resolved_klass;
 145   _resolved_method = resolved_method;
 146   _selected_method = resolved_method;
 147   // classify:
 148   CallKind kind = CallInfo::unknown_kind;
 149   int index = resolved_method->vtable_index();
 150   if (resolved_method->can_be_statically_bound()) {
 151     kind = CallInfo::direct_call;
 152   } else if (!resolved_method_holder->is_interface()) {
 153     // Could be an Object method inherited into an interface, but still a vtable call.
 154     kind = CallInfo::vtable_call;
 155   } else if (!resolved_klass->is_interface()) {
 156     // A default or miranda method.  Compute the vtable index.
 157     index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
 158                            resolved_method);
 159     assert(index >= 0 , "we should have valid vtable index at this point");
 160 
 161     kind = CallInfo::vtable_call;
 162   } else if (resolved_method->has_vtable_index()) {
 163     // Can occur if an interface redeclares a method of Object.
 164 
 165 #ifdef ASSERT
 166     // Ensure that this is really the case.
 167     Klass* object_klass = SystemDictionary::Object_klass();
 168     Method * object_resolved_method = object_klass->vtable().method_at(index);
 169     assert(object_resolved_method->name() == resolved_method->name(),
 170       "Object and interface method names should match at vtable index %d, %s != %s",
 171       index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
 172     assert(object_resolved_method->signature() == resolved_method->signature(),
 173       "Object and interface method signatures should match at vtable index %d, %s != %s",
 174       index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
 175 #endif // ASSERT
 176 
 177     kind = CallInfo::vtable_call;
 178   } else {
 179     // A regular interface call.
 180     kind = CallInfo::itable_call;
 181     index = resolved_method->itable_index();
 182   }
 183   assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
 184   _call_kind  = kind;
 185   _call_index = index;
 186   _resolved_appendix = Handle();
 187   // Find or create a ResolvedMethod instance for this Method*
 188   set_resolved_method_name(CHECK);
 189 
 190   DEBUG_ONLY(verify());
 191 }
 192 
 193 void CallInfo::set_resolved_method_name(TRAPS) {
 194   Method* m = _resolved_method();
 195   assert(m != NULL, "Should already have a Method*");
 196   oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(m, CHECK);
 197   _resolved_method_name = Handle(THREAD, rmethod_name);
 198 }
 199 
 200 #ifdef ASSERT
 201 void CallInfo::verify() {
 202   switch (call_kind()) {  // the meaning and allowed value of index depends on kind
 203   case CallInfo::direct_call:
 204     if (_call_index == Method::nonvirtual_vtable_index)  break;
 205     // else fall through to check vtable index:
 206   case CallInfo::vtable_call:
 207     assert(resolved_klass()->verify_vtable_index(_call_index), "");
 208     break;
 209   case CallInfo::itable_call:
 210     assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
 211     break;
 212   case CallInfo::unknown_kind:
 213     assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
 214     break;
 215   default:
 216     fatal("Unexpected call kind %d", call_kind());
 217   }
 218 }
 219 #endif //ASSERT
 220 
 221 #ifndef PRODUCT
 222 void CallInfo::print() {
 223   ResourceMark rm;
 224   const char* kindstr;
 225   switch (_call_kind) {
 226   case direct_call: kindstr = "direct";  break;
 227   case vtable_call: kindstr = "vtable";  break;
 228   case itable_call: kindstr = "itable";  break;
 229   default         : kindstr = "unknown"; break;
 230   }
 231   tty->print_cr("Call %s@%d %s", kindstr, _call_index,
 232                 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
 233 }
 234 #endif
 235 
 236 //------------------------------------------------------------------------------------------------------------------------
 237 // Implementation of LinkInfo
 238 
 239 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, const methodHandle& current_method, TRAPS) {
 240    // resolve klass
 241   _resolved_klass = pool->klass_ref_at(index, CHECK);
 242 
 243   // Get name, signature, and static klass
 244   _name          = pool->name_ref_at(index);
 245   _signature     = pool->signature_ref_at(index);
 246   _tag           = pool->tag_ref_at(index);
 247   _current_klass = pool->pool_holder();
 248   _current_method = current_method;
 249 
 250   // Coming from the constant pool always checks access
 251   _check_access  = true;
 252 }
 253 
 254 LinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) {
 255    // resolve klass
 256   _resolved_klass = pool->klass_ref_at(index, CHECK);
 257 
 258   // Get name, signature, and static klass
 259   _name          = pool->name_ref_at(index);
 260   _signature     = pool->signature_ref_at(index);
 261   _tag           = pool->tag_ref_at(index);
 262   _current_klass = pool->pool_holder();
 263   _current_method = methodHandle();
 264 
 265   // Coming from the constant pool always checks access
 266   _check_access  = true;
 267 }
 268 
 269 char* LinkInfo::method_string() const {
 270   return Method::name_and_sig_as_C_string(_resolved_klass, _name, _signature);
 271 }
 272 
 273 #ifndef PRODUCT
 274 void LinkInfo::print() {
 275   ResourceMark rm;
 276   tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s",
 277                 _resolved_klass->name()->as_C_string(),
 278                 _name->as_C_string(),
 279                 _signature->as_C_string(),
 280                 _current_klass == NULL ? "(none)" : _current_klass->name()->as_C_string(),
 281                 _check_access ? "true" : "false");
 282 }
 283 #endif // PRODUCT
 284 //------------------------------------------------------------------------------------------------------------------------
 285 // Klass resolution
 286 
 287 void LinkResolver::check_klass_accessability(Klass* ref_klass, Klass* sel_klass, TRAPS) {
 288   Reflection::VerifyClassAccessResults vca_result =
 289     Reflection::verify_class_access(ref_klass, InstanceKlass::cast(sel_klass), true);
 290   if (vca_result != Reflection::ACCESS_OK) {
 291     ResourceMark rm(THREAD);
 292     char* msg = Reflection::verify_class_access_msg(ref_klass,
 293                                                     InstanceKlass::cast(sel_klass),
 294                                                     vca_result);
 295     if (msg == NULL) {
 296       Exceptions::fthrow(
 297         THREAD_AND_LOCATION,
 298         vmSymbols::java_lang_IllegalAccessError(),
 299         "failed to access class %s from class %s",
 300         sel_klass->external_name(),
 301         ref_klass->external_name());
 302     } else {
 303       // Use module specific message returned by verify_class_access_msg().
 304       Exceptions::fthrow(
 305         THREAD_AND_LOCATION,
 306         vmSymbols::java_lang_IllegalAccessError(),
 307         "%s", msg);
 308     }
 309   }
 310 }
 311 
 312 //------------------------------------------------------------------------------------------------------------------------
 313 // Method resolution
 314 //
 315 // According to JVM spec. $5.4.3c & $5.4.3d
 316 
 317 // Look up method in klasses, including static methods
 318 // Then look up local default methods
 319 Method* LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info,
 320                                                bool checkpolymorphism,
 321                                                bool in_imethod_resolve) {
 322   NoSafepointVerifier nsv;  // Method* returned may not be reclaimed
 323 
 324   Klass* klass = link_info.resolved_klass();
 325   Symbol* name = link_info.name();
 326   Symbol* signature = link_info.signature();
 327 
 328   // Ignore overpasses so statics can be found during resolution
 329   Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass);
 330 
 331   if (klass->is_array_klass()) {
 332     // Only consider klass and super klass for arrays
 333     return result;
 334   }
 335 
 336   InstanceKlass* ik = InstanceKlass::cast(klass);
 337 
 338   // JDK 8, JVMS 5.4.3.4: Interface method resolution should
 339   // ignore static and non-public methods of java.lang.Object,
 340   // like clone, finalize, registerNatives.
 341   if (in_imethod_resolve &&
 342       result != NULL &&
 343       ik->is_interface() &&
 344       (result->is_static() || !result->is_public()) &&
 345       result->method_holder() == SystemDictionary::Object_klass()) {
 346     result = NULL;
 347   }
 348 
 349   // Before considering default methods, check for an overpass in the
 350   // current class if a method has not been found.
 351   if (result == NULL) {
 352     result = ik->find_method(name, signature);
 353   }
 354 
 355   if (result == NULL) {
 356     Array<Method*>* default_methods = ik->default_methods();
 357     if (default_methods != NULL) {
 358       result = InstanceKlass::find_method(default_methods, name, signature);
 359     }
 360   }
 361 
 362   if (checkpolymorphism && result != NULL) {
 363     vmIntrinsics::ID iid = result->intrinsic_id();
 364     if (MethodHandles::is_signature_polymorphic(iid)) {
 365       // Do not link directly to these.  The VM must produce a synthetic one using lookup_polymorphic_method.
 366       return NULL;
 367     }
 368   }
 369   return result;
 370 }
 371 
 372 // returns first instance method
 373 // Looks up method in classes, then looks up local default methods
 374 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
 375                                                              Symbol* name,
 376                                                              Symbol* signature, TRAPS) {
 377   Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 378 
 379   while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
 380     Klass* super_klass = result->method_holder()->super();
 381     result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass);
 382   }
 383 
 384   if (klass->is_array_klass()) {
 385     // Only consider klass and super klass for arrays
 386     return methodHandle(THREAD, result);
 387   }
 388 
 389   if (result == NULL) {
 390     Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
 391     if (default_methods != NULL) {
 392       result = InstanceKlass::find_method(default_methods, name, signature);
 393       assert(result == NULL || !result->is_static(), "static defaults not allowed");
 394     }
 395   }
 396   return methodHandle(THREAD, result);
 397 }
 398 
 399 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
 400                                                    const methodHandle& resolved_method) {
 401 
 402   int vtable_index = Method::invalid_vtable_index;
 403   Symbol* name = resolved_method->name();
 404   Symbol* signature = resolved_method->signature();
 405   InstanceKlass* ik = InstanceKlass::cast(klass);
 406 
 407   // First check in default method array
 408   if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
 409     int index = InstanceKlass::find_method_index(ik->default_methods(),
 410                                                  name, signature, Klass::find_overpass,
 411                                                  Klass::find_static, Klass::find_private);
 412     if (index >= 0 ) {
 413       vtable_index = ik->default_vtable_indices()->at(index);
 414     }
 415   }
 416   if (vtable_index == Method::invalid_vtable_index) {
 417     // get vtable_index for miranda methods
 418     klassVtable vt = ik->vtable();
 419     vtable_index = vt.index_of_miranda(name, signature);
 420   }
 421   return vtable_index;
 422 }
 423 
 424 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
 425   InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
 426 
 427   // Specify 'true' in order to skip default methods when searching the
 428   // interfaces.  Function lookup_method_in_klasses() already looked for
 429   // the method in the default methods table.
 430   return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::skip_defaults);
 431 }
 432 
 433 methodHandle LinkResolver::lookup_polymorphic_method(
 434                                              const LinkInfo& link_info,
 435                                              Handle *appendix_result_or_null,
 436                                              Handle *method_type_result,
 437                                              TRAPS) {
 438   Klass* klass = link_info.resolved_klass();
 439   Symbol* name = link_info.name();
 440   Symbol* full_signature = link_info.signature();
 441 
 442   vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
 443   if (TraceMethodHandles) {
 444     ResourceMark rm(THREAD);
 445     tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
 446                   vmIntrinsics::name_at(iid), klass->external_name(),
 447                   name->as_C_string(), full_signature->as_C_string());
 448   }
 449   if ((klass == SystemDictionary::MethodHandle_klass() ||
 450        klass == SystemDictionary::VarHandle_klass()) &&
 451       iid != vmIntrinsics::_none) {
 452     if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
 453       // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
 454       // Do not erase last argument type (MemberName) if it is a static linkTo method.
 455       bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
 456       TempNewSymbol basic_signature =
 457         MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
 458       if (TraceMethodHandles) {
 459         ResourceMark rm(THREAD);
 460         tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
 461                       name->as_C_string(),
 462                       full_signature->as_C_string(),
 463                       basic_signature->as_C_string());
 464       }
 465       methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid,
 466                                                               basic_signature,
 467                                                               CHECK_NULL);
 468       if (result.not_null()) {
 469         assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
 470         assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
 471         assert(basic_signature == result->signature(), "predict the result signature");
 472         if (TraceMethodHandles) {
 473           ttyLocker ttyl;
 474           tty->print("lookup_polymorphic_method => intrinsic ");
 475           result->print_on(tty);
 476         }
 477       }
 478       return result;
 479     } else if (iid == vmIntrinsics::_invokeGeneric
 480                && THREAD->can_call_java()
 481                && appendix_result_or_null != NULL) {
 482       // This is a method with type-checking semantics.
 483       // We will ask Java code to spin an adapter method for it.
 484       if (!MethodHandles::enabled()) {
 485         // Make sure the Java part of the runtime has been booted up.
 486         Klass* natives = SystemDictionary::MethodHandleNatives_klass();
 487         if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
 488           SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
 489                                             Handle(),
 490                                             Handle(),
 491                                             true,
 492                                             CHECK_NULL);
 493         }
 494       }
 495 
 496       Handle appendix;
 497       Handle method_type;
 498       methodHandle result = SystemDictionary::find_method_handle_invoker(
 499                                                             klass,
 500                                                             name,
 501                                                             full_signature,
 502                                                             link_info.current_klass(),
 503                                                             &appendix,
 504                                                             &method_type,
 505                                                             CHECK_NULL);
 506       if (TraceMethodHandles) {
 507         ttyLocker ttyl;
 508         tty->print("lookup_polymorphic_method => (via Java) ");
 509         result->print_on(tty);
 510         tty->print("  lookup_polymorphic_method => appendix = ");
 511         if (appendix.is_null())  tty->print_cr("(none)");
 512         else                     appendix->print_on(tty);
 513       }
 514       if (result.not_null()) {
 515 #ifdef ASSERT
 516         ResourceMark rm(THREAD);
 517 
 518         TempNewSymbol basic_signature =
 519           MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL);
 520         int actual_size_of_params = result->size_of_parameters();
 521         int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
 522         // +1 for MethodHandle.this, +1 for trailing MethodType
 523         if (!MethodHandles::is_signature_polymorphic_static(iid))  expected_size_of_params += 1;
 524         if (appendix.not_null())                                   expected_size_of_params += 1;
 525         if (actual_size_of_params != expected_size_of_params) {
 526           tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
 527           tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
 528           result->print();
 529         }
 530         assert(actual_size_of_params == expected_size_of_params,
 531                "%d != %d", actual_size_of_params, expected_size_of_params);
 532 #endif //ASSERT
 533 
 534         assert(appendix_result_or_null != NULL, "");
 535         (*appendix_result_or_null) = appendix;
 536         (*method_type_result)      = method_type;
 537       }
 538       return result;
 539     }
 540   }
 541   return NULL;
 542 }
 543 
 544 void LinkResolver::check_method_accessability(Klass* ref_klass,
 545                                               Klass* resolved_klass,
 546                                               Klass* sel_klass,
 547                                               const methodHandle& sel_method,
 548                                               TRAPS) {
 549 
 550   AccessFlags flags = sel_method->access_flags();
 551 
 552   // Special case:  arrays always override "clone". JVMS 2.15.
 553   // If the resolved klass is an array class, and the declaring class
 554   // is java.lang.Object and the method is "clone", set the flags
 555   // to public.
 556   //
 557   // We'll check for the method name first, as that's most likely
 558   // to be false (so we'll short-circuit out of these tests).
 559   if (sel_method->name() == vmSymbols::clone_name() &&
 560       sel_klass == SystemDictionary::Object_klass() &&
 561       resolved_klass->is_array_klass()) {
 562     // We need to change "protected" to "public".
 563     assert(flags.is_protected(), "clone not protected?");
 564     jint new_flags = flags.as_int();
 565     new_flags = new_flags & (~JVM_ACC_PROTECTED);
 566     new_flags = new_flags | JVM_ACC_PUBLIC;
 567     flags.set_flags(new_flags);
 568   }
 569 //  assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
 570 
 571   if (!Reflection::verify_field_access(ref_klass,
 572                                        resolved_klass,
 573                                        sel_klass,
 574                                        flags,
 575                                        true)) {
 576     // Propagate any existing exceptions that may have been thrown
 577     if (HAS_PENDING_EXCEPTION) {
 578       return;
 579     }
 580     ResourceMark rm(THREAD);
 581     Exceptions::fthrow(
 582       THREAD_AND_LOCATION,
 583       vmSymbols::java_lang_IllegalAccessError(),
 584       "tried to access method %s.%s%s from class %s",
 585       sel_klass->external_name(),
 586       sel_method->name()->as_C_string(),
 587       sel_method->signature()->as_C_string(),
 588       ref_klass->external_name()
 589     );
 590     return;
 591   }
 592 }
 593 
 594 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
 595                                                      const constantPoolHandle& pool, int index, TRAPS) {
 596   // This method is used only
 597   // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
 598   // and
 599   // (2) in Bytecode_invoke::static_target
 600   // It appears to fail when applied to an invokeinterface call site.
 601   // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
 602   // resolve klass
 603   if (code == Bytecodes::_invokedynamic) {
 604     Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
 605     Symbol* method_name = vmSymbols::invoke_name();
 606     Symbol* method_signature = pool->signature_ref_at(index);
 607     Klass*  current_klass = pool->pool_holder();
 608     LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
 609     return resolve_method(link_info, code, THREAD);
 610   }
 611 
 612   LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
 613   Klass* resolved_klass = link_info.resolved_klass();
 614 
 615   if (pool->has_preresolution()
 616       || (resolved_klass == SystemDictionary::MethodHandle_klass() &&
 617           MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
 618     Method* result = ConstantPool::method_at_if_loaded(pool, index);
 619     if (result != NULL) {
 620       return methodHandle(THREAD, result);
 621     }
 622   }
 623 
 624   if (code == Bytecodes::_invokeinterface) {
 625     return resolve_interface_method(link_info, code, THREAD);
 626   } else if (code == Bytecodes::_invokevirtual) {
 627     return resolve_method(link_info, code, THREAD);
 628   } else if (!resolved_klass->is_interface()) {
 629     return resolve_method(link_info, code, THREAD);
 630   } else {
 631     return resolve_interface_method(link_info, code, THREAD);
 632   }
 633 }
 634 
 635 // Check and print a loader constraint violation message for method or interface method
 636 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
 637                                                    const methodHandle& resolved_method,
 638                                                    const char* method_type, TRAPS) {
 639   Handle current_loader(THREAD, link_info.current_klass()->class_loader());
 640   Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
 641 
 642   ResourceMark rm(THREAD);
 643   Symbol* failed_type_symbol =
 644     SystemDictionary::check_signature_loaders(link_info.signature(), current_loader,
 645                                               resolved_loader, true, CHECK);
 646   if (failed_type_symbol != NULL) {
 647     const char* msg = "loader constraint violation: when resolving %s"
 648       " \"%s\" the class loader (instance of %s) of the current class, %s,"
 649       " and the class loader (instance of %s) for the method's defining class, %s, have"
 650       " different Class objects for the type %s used in the signature";
 651     char* sig = link_info.method_string();
 652     const char* loader1_name = SystemDictionary::loader_name(current_loader());
 653     char* current = link_info.current_klass()->name()->as_C_string();
 654     const char* loader2_name = SystemDictionary::loader_name(resolved_loader());
 655     char* target = resolved_method->method_holder()->name()->as_C_string();
 656     char* failed_type_name = failed_type_symbol->as_C_string();
 657     size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) +
 658       strlen(current) + strlen(loader2_name) + strlen(target) +
 659       strlen(failed_type_name) + strlen(method_type) + 1;
 660     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 661     jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name,
 662                  target, failed_type_name);
 663     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 664   }
 665 }
 666 
 667 void LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig,
 668                                                   Klass* current_klass,
 669                                                   Klass* sel_klass, TRAPS) {
 670   Handle ref_loader(THREAD, current_klass->class_loader());
 671   Handle sel_loader(THREAD, sel_klass->class_loader());
 672 
 673   ResourceMark rm(THREAD);  // needed for check_signature_loaders
 674   Symbol* failed_type_symbol =
 675     SystemDictionary::check_signature_loaders(sig,
 676                                               ref_loader, sel_loader,
 677                                               false,
 678                                               CHECK);
 679   if (failed_type_symbol != NULL) {
 680     const char* msg = "loader constraint violation: when resolving field"
 681       " \"%s\" the class loader (instance of %s) of the referring class, "
 682       "%s, and the class loader (instance of %s) for the field's resolved "
 683       "type, %s, have different Class objects for that type";
 684     char* field_name = field->as_C_string();
 685     const char* loader1_name = SystemDictionary::loader_name(ref_loader());
 686     char* sel = sel_klass->name()->as_C_string();
 687     const char* loader2_name = SystemDictionary::loader_name(sel_loader());
 688     char* failed_type_name = failed_type_symbol->as_C_string();
 689     size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) +
 690                     strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1;
 691     char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
 692     jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name,
 693                      failed_type_name);
 694     THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
 695   }
 696 }
 697 
 698 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
 699                                           Bytecodes::Code code, TRAPS) {
 700 
 701   Handle nested_exception;
 702   Klass* resolved_klass = link_info.resolved_klass();
 703 
 704   // 1. For invokevirtual, cannot call an interface method
 705   if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
 706     ResourceMark rm(THREAD);
 707     char buf[200];
 708     jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
 709         resolved_klass->external_name());
 710     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 711   }
 712 
 713   // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
 714   if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
 715     ResourceMark rm(THREAD);
 716     char buf[200];
 717     jio_snprintf(buf, sizeof(buf), "Method %s must be Methodref constant", link_info.method_string());
 718     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 719   }
 720 
 721   // 3. lookup method in resolved klass and its super klasses
 722   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
 723 
 724   // 4. lookup method in all the interfaces implemented by the resolved klass
 725   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
 726     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 727 
 728     if (resolved_method.is_null()) {
 729       // JSR 292:  see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
 730       resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD);
 731       if (HAS_PENDING_EXCEPTION) {
 732         nested_exception = Handle(THREAD, PENDING_EXCEPTION);
 733         CLEAR_PENDING_EXCEPTION;
 734       }
 735     }
 736   }
 737 
 738   // 5. method lookup failed
 739   if (resolved_method.is_null()) {
 740     ResourceMark rm(THREAD);
 741     THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
 742                     Method::name_and_sig_as_C_string(resolved_klass,
 743                                                      link_info.name(),
 744                                                      link_info.signature()),
 745                     nested_exception, NULL);
 746   }
 747 
 748   // 6. access checks, access checking may be turned off when calling from within the VM.
 749   Klass* current_klass = link_info.current_klass();
 750   if (link_info.check_access()) {
 751     assert(current_klass != NULL , "current_klass should not be null");
 752 
 753     // check if method can be accessed by the referring class
 754     check_method_accessability(current_klass,
 755                                resolved_klass,
 756                                resolved_method->method_holder(),
 757                                resolved_method,
 758                                CHECK_NULL);
 759 
 760     // check loader constraints
 761     check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
 762   }
 763 
 764   // For private method invocation we should only find the method in the resolved class.
 765   // If that is not the case then we have a found a supertype method that we have nestmate
 766   // access to.
 767   // FIXME: the "ignoring xxx" part is for debugging only
 768   if (resolved_method->is_private() && resolved_method->method_holder() != resolved_klass) {
 769     ResourceMark rm(THREAD);
 770    DEBUG_ONLY(bool is_nestmate = InstanceKlass::cast(link_info.current_klass())->has_nestmate_access_to(InstanceKlass::cast(resolved_klass), THREAD);)
 771     assert(is_nestmate, "was only expecting nestmates to get here!");
 772     Exceptions::fthrow(
 773       THREAD_AND_LOCATION,
 774       vmSymbols::java_lang_NoSuchMethodError(),
 775       "%s: method %s%s not found (ignoring %s)",
 776       resolved_klass->external_name(),
 777       resolved_method->name()->as_C_string(),
 778       resolved_method->signature()->as_C_string(),
 779       resolved_method->method_holder()->external_name()
 780     );
 781     return NULL;
 782   }
 783 
 784   return resolved_method;
 785 }
 786 
 787 static void trace_method_resolution(const char* prefix,
 788                                     Klass* klass,
 789                                     Klass* resolved_klass,
 790                                     const methodHandle& method,
 791                                     bool logitables,
 792                                     int index = -1) {
 793 #ifndef PRODUCT
 794   ResourceMark rm;
 795   Log(itables) logi;
 796   LogStream lsi(logi.trace());
 797   Log(vtables) logv;
 798   LogStream lsv(logv.trace());
 799   outputStream* st;
 800   if (logitables) {
 801     st = &lsi;
 802   } else {
 803     st = &lsv;
 804   }
 805   st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
 806             prefix,
 807             (klass == NULL ? "<NULL>" : klass->internal_name()),
 808             (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
 809             Method::name_and_sig_as_C_string(resolved_klass,
 810                                              method->name(),
 811                                              method->signature()),
 812             method->method_holder()->internal_name());
 813   method->print_linkage_flags(st);
 814   if (index != -1) {
 815     st->print("vtable_index:%d", index);
 816   }
 817   st->cr();
 818 #endif // PRODUCT
 819 }
 820 



 821 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
 822 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
 823 
 824   Klass* resolved_klass = link_info.resolved_klass();
 825 
 826   // check if klass is interface
 827   if (!resolved_klass->is_interface()) {
 828     ResourceMark rm(THREAD);
 829     char buf[200];
 830     jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
 831     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 832   }
 833 
 834   // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
 835   if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
 836     ResourceMark rm(THREAD);
 837     char buf[200];
 838     jio_snprintf(buf, sizeof(buf), "Method %s must be InterfaceMethodref constant", link_info.method_string());
 839     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
 840   }
 841 
 842   // lookup method in this interface or its super, java.lang.Object
 843   // JDK8: also look for static methods
 844   methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, false, true));
 845 
 846   if (resolved_method.is_null() && !resolved_klass->is_array_klass()) {
 847     // lookup method in all the super-interfaces
 848     resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
 849   }
 850 
 851   if (resolved_method.is_null()) {
 852     // no method found
 853     ResourceMark rm(THREAD);
 854     THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(),
 855                    Method::name_and_sig_as_C_string(resolved_klass,
 856                                                     link_info.name(),
 857                                                     link_info.signature()));
 858   }
 859 
 860   if (link_info.check_access()) {
 861     // JDK8 adds non-public interface methods, and accessability check requirement
 862     Klass* current_klass = link_info.current_klass();
 863 
 864     assert(current_klass != NULL , "current_klass should not be null");
 865 
 866     // check if method can be accessed by the referring class
 867     check_method_accessability(current_klass,
 868                                resolved_klass,
 869                                resolved_method->method_holder(),
 870                                resolved_method,
 871                                CHECK_NULL);
 872 
 873     check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
 874   }
 875 
 876   if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
 877     ResourceMark rm(THREAD);
 878     char buf[200];
 879     jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s",
 880                  Method::name_and_sig_as_C_string(resolved_klass,
 881                  resolved_method->name(), resolved_method->signature()));
 882     THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);















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