linkResolver.cpp revision 10061:197538942788
155714Skris/* 255714Skris * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved. 355714Skris * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 455714Skris * 555714Skris * This code is free software; you can redistribute it and/or modify it 655714Skris * under the terms of the GNU General Public License version 2 only, as 755714Skris * published by the Free Software Foundation. 8280304Sjkim * 955714Skris * This code is distributed in the hope that it will be useful, but WITHOUT 1055714Skris * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1155714Skris * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1255714Skris * version 2 for more details (a copy is included in the LICENSE file that 1355714Skris * accompanied this code). 1455714Skris * 15280304Sjkim * You should have received a copy of the GNU General Public License version 1655714Skris * 2 along with this work; if not, write to the Free Software Foundation, 1755714Skris * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1855714Skris * 1955714Skris * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2055714Skris * or visit www.oracle.com if you need additional information or have any 2155714Skris * questions. 22280304Sjkim * 2355714Skris */ 2455714Skris 2555714Skris#include "precompiled.hpp" 2655714Skris#include "classfile/defaultMethods.hpp" 2755714Skris#include "classfile/symbolTable.hpp" 2855714Skris#include "classfile/systemDictionary.hpp" 2955714Skris#include "classfile/vmSymbols.hpp" 3055714Skris#include "compiler/compileBroker.hpp" 3155714Skris#include "gc/shared/collectedHeap.inline.hpp" 3255714Skris#include "interpreter/bytecode.hpp" 3355714Skris#include "interpreter/interpreterRuntime.hpp" 3455714Skris#include "interpreter/linkResolver.hpp" 3555714Skris#include "logging/log.hpp" 3655714Skris#include "memory/resourceArea.hpp" 37280304Sjkim#include "memory/universe.inline.hpp" 3855714Skris#include "oops/instanceKlass.hpp" 3955714Skris#include "oops/method.hpp" 40280304Sjkim#include "oops/objArrayOop.hpp" 4155714Skris#include "oops/oop.inline.hpp" 4255714Skris#include "prims/methodHandles.hpp" 4355714Skris#include "prims/nativeLookup.hpp" 4455714Skris#include "runtime/compilationPolicy.hpp" 4555714Skris#include "runtime/fieldDescriptor.hpp" 4655714Skris#include "runtime/frame.inline.hpp" 4755714Skris#include "runtime/handles.inline.hpp" 4855714Skris#include "runtime/reflection.hpp" 4955714Skris#include "runtime/signature.hpp" 5055714Skris#include "runtime/thread.inline.hpp" 5155714Skris#include "runtime/vmThread.hpp" 52280304Sjkim 5355714Skris 5455714Skris//------------------------------------------------------------------------------------------------------------------------ 5555714Skris// Implementation of CallInfo 5655714Skris 5755714Skris 58160814Ssimonvoid CallInfo::set_static(KlassHandle resolved_klass, const methodHandle& resolved_method, TRAPS) { 59238405Sjkim int vtable_index = Method::nonvirtual_vtable_index; 60238405Sjkim set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); 61238405Sjkim} 62238405Sjkim 63238405Sjkim 64238405Sjkimvoid CallInfo::set_interface(KlassHandle resolved_klass, 65238405Sjkim KlassHandle selected_klass, 66280304Sjkim const methodHandle& resolved_method, 67238405Sjkim const methodHandle& selected_method, 68238405Sjkim int itable_index, TRAPS) { 69238405Sjkim // This is only called for interface methods. If the resolved_method 70238405Sjkim // comes from java/lang/Object, it can be the subject of a virtual call, so 71238405Sjkim // we should pick the vtable index from the resolved method. 72238405Sjkim // In that case, the caller must call set_virtual instead of set_interface. 73238405Sjkim assert(resolved_method->method_holder()->is_interface(), ""); 74238405Sjkim assert(itable_index == resolved_method()->itable_index(), ""); 75238405Sjkim set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK); 76238405Sjkim} 77238405Sjkim 78238405Sjkimvoid CallInfo::set_virtual(KlassHandle resolved_klass, 79238405Sjkim KlassHandle selected_klass, 80238405Sjkim const methodHandle& resolved_method, 81238405Sjkim const methodHandle& selected_method, 82238405Sjkim int vtable_index, TRAPS) { 83238405Sjkim assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index"); 84238405Sjkim assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), ""); 85238405Sjkim CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call); 86238405Sjkim set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK); 87238405Sjkim assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call"); 88238405Sjkim} 89238405Sjkim 90238405Sjkimvoid CallInfo::set_handle(const methodHandle& resolved_method, 91238405Sjkim Handle resolved_appendix, 92238405Sjkim Handle resolved_method_type, TRAPS) { 93238405Sjkim if (resolved_method.is_null()) { 94238405Sjkim THROW_MSG(vmSymbols::java_lang_InternalError(), "resolved method is null"); 95238405Sjkim } 96238405Sjkim KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass(); 97238405Sjkim assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic || 98238405Sjkim resolved_method->is_compiled_lambda_form(), 99238405Sjkim "linkMethod must return one of these"); 100238405Sjkim int vtable_index = Method::nonvirtual_vtable_index; 101238405Sjkim assert(!resolved_method->has_vtable_index(), ""); 102238405Sjkim set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); 103238405Sjkim _resolved_appendix = resolved_appendix; 104238405Sjkim _resolved_method_type = resolved_method_type; 105238405Sjkim} 106238405Sjkim 107238405Sjkimvoid CallInfo::set_common(KlassHandle resolved_klass, 108238405Sjkim KlassHandle selected_klass, 109238405Sjkim const methodHandle& resolved_method, 110238405Sjkim const methodHandle& selected_method, 111238405Sjkim CallKind kind, 112160814Ssimon int index, 113160814Ssimon TRAPS) { 114280304Sjkim assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond"); 115160814Ssimon _resolved_klass = resolved_klass; 116160814Ssimon _selected_klass = selected_klass; 117160814Ssimon _resolved_method = resolved_method; 118160814Ssimon _selected_method = selected_method; 119160814Ssimon _call_kind = kind; 120160814Ssimon _call_index = index; 121160814Ssimon _resolved_appendix = Handle(); 122160814Ssimon DEBUG_ONLY(verify()); // verify before making side effects 123160814Ssimon 124238405Sjkim if (CompilationPolicy::must_be_compiled(selected_method)) { 125238405Sjkim // This path is unusual, mostly used by the '-Xcomp' stress test mode. 126238405Sjkim 127238405Sjkim // Note: with several active threads, the must_be_compiled may be true 128238405Sjkim // while can_be_compiled is false; remove assert 129238405Sjkim // assert(CompilationPolicy::can_be_compiled(selected_method), "cannot compile"); 130238405Sjkim if (!THREAD->can_call_java()) { 131238405Sjkim // don't force compilation, resolve was on behalf of compiler 132238405Sjkim return; 133238405Sjkim } 134238405Sjkim if (selected_method->method_holder()->is_not_initialized()) { 135238405Sjkim // 'is_not_initialized' means not only '!is_initialized', but also that 136238405Sjkim // initialization has not been started yet ('!being_initialized') 137238405Sjkim // Do not force compilation of methods in uninitialized classes. 138238405Sjkim // Note that doing this would throw an assert later, 139238405Sjkim // in CompileBroker::compile_method. 140238405Sjkim // We sometimes use the link resolver to do reflective lookups 141238405Sjkim // even before classes are initialized. 142238405Sjkim return; 143238405Sjkim } 144238405Sjkim CompileBroker::compile_method(selected_method, InvocationEntryBci, 145238405Sjkim CompilationPolicy::policy()->initial_compile_level(), 146238405Sjkim methodHandle(), 0, "must_be_compiled", CHECK); 147238405Sjkim } 148238405Sjkim} 149238405Sjkim 15055714Skris// utility query for unreflecting a method 151280304SjkimCallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) { 152280304Sjkim Klass* resolved_method_holder = resolved_method->method_holder(); 15355714Skris if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st 154280304Sjkim resolved_klass = resolved_method_holder; 15555714Skris } 15655714Skris _resolved_klass = resolved_klass; 15755714Skris _selected_klass = resolved_klass; 15855714Skris _resolved_method = resolved_method; 15955714Skris _selected_method = resolved_method; 160280304Sjkim // classify: 16155714Skris CallKind kind = CallInfo::unknown_kind; 162280304Sjkim int index = resolved_method->vtable_index(); 163280304Sjkim if (resolved_method->can_be_statically_bound()) { 164280304Sjkim kind = CallInfo::direct_call; 165280304Sjkim } else if (!resolved_method_holder->is_interface()) { 166238405Sjkim // Could be an Object method inherited into an interface, but still a vtable call. 167280304Sjkim kind = CallInfo::vtable_call; 168280304Sjkim } else if (!resolved_klass->is_interface()) { 169273149Sjkim // A default or miranda method. Compute the vtable index. 170280304Sjkim ResourceMark rm; 171280304Sjkim klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable(); 172238405Sjkim index = LinkResolver::vtable_index_of_interface_method(resolved_klass, 173280304Sjkim resolved_method); 174280304Sjkim assert(index >= 0 , "we should have valid vtable index at this point"); 17555714Skris 176280304Sjkim kind = CallInfo::vtable_call; 177280304Sjkim } else if (resolved_method->has_vtable_index()) { 178238405Sjkim // Can occur if an interface redeclares a method of Object. 179280304Sjkim 180280304Sjkim#ifdef ASSERT 181238405Sjkim // Ensure that this is really the case. 182280304Sjkim KlassHandle object_klass = SystemDictionary::Object_klass(); 183280304Sjkim Method * object_resolved_method = object_klass()->vtable()->method_at(index); 184280304Sjkim assert(object_resolved_method->name() == resolved_method->name(), 185280304Sjkim "Object and interface method names should match at vtable index %d, %s != %s", 186280304Sjkim index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string()); 187280304Sjkim assert(object_resolved_method->signature() == resolved_method->signature(), 188280304Sjkim "Object and interface method signatures should match at vtable index %d, %s != %s", 189280304Sjkim index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string()); 190280304Sjkim#endif // ASSERT 191280304Sjkim 192280304Sjkim kind = CallInfo::vtable_call; 193280304Sjkim } else { 194280304Sjkim // A regular interface call. 195194206Ssimon kind = CallInfo::itable_call; 196280304Sjkim index = resolved_method->itable_index(); 197280304Sjkim } 198280304Sjkim assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index); 199280304Sjkim _call_kind = kind; 200280304Sjkim _call_index = index; 201280304Sjkim _resolved_appendix = Handle(); 20255714Skris DEBUG_ONLY(verify()); 203238405Sjkim} 204280304Sjkim 205280304Sjkim#ifdef ASSERT 206280304Sjkimvoid CallInfo::verify() { 207280304Sjkim switch (call_kind()) { // the meaning and allowed value of index depends on kind 208280304Sjkim case CallInfo::direct_call: 209280304Sjkim if (_call_index == Method::nonvirtual_vtable_index) break; 210238405Sjkim // else fall through to check vtable index: 211280304Sjkim case CallInfo::vtable_call: 212238405Sjkim assert(resolved_klass()->verify_vtable_index(_call_index), ""); 213238405Sjkim break; 214280304Sjkim case CallInfo::itable_call: 215280304Sjkim assert(resolved_method()->method_holder()->verify_itable_index(_call_index), ""); 216238405Sjkim break; 217238405Sjkim case CallInfo::unknown_kind: 218280304Sjkim assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set"); 219238405Sjkim break; 220238405Sjkim default: 221280304Sjkim fatal("Unexpected call kind %d", call_kind()); 222280304Sjkim } 223238405Sjkim} 224238405Sjkim#endif //ASSERT 225280304Sjkim 226238405Sjkim#ifndef PRODUCT 227238405Sjkimvoid CallInfo::print() { 228280304Sjkim ResourceMark rm; 229238405Sjkim const char* kindstr = "unknown"; 230238405Sjkim switch (_call_kind) { 231280304Sjkim case direct_call: kindstr = "direct"; break; 232238405Sjkim case vtable_call: kindstr = "vtable"; break; 233238405Sjkim case itable_call: kindstr = "itable"; break; 234280304Sjkim } 235238405Sjkim tty->print_cr("Call %s@%d %s", kindstr, _call_index, 236280304Sjkim _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string()); 237280304Sjkim} 238291721Sjkim#endif 239264331Sjkim 240280304Sjkim//------------------------------------------------------------------------------------------------------------------------ 241264331Sjkim// Implementation of LinkInfo 242238405Sjkim 243280304SjkimLinkInfo::LinkInfo(const constantPoolHandle& pool, int index, TRAPS) { 244194206Ssimon // resolve klass 245238405Sjkim Klass* result = pool->klass_ref_at(index, CHECK); 246280304Sjkim _resolved_klass = KlassHandle(THREAD, result); 247280304Sjkim 248280304Sjkim // Get name, signature, and static klass 249280304Sjkim _name = pool->name_ref_at(index); 250280304Sjkim _signature = pool->signature_ref_at(index); 251280304Sjkim _current_klass = KlassHandle(THREAD, pool->pool_holder()); 252280304Sjkim 253280304Sjkim // Coming from the constant pool always checks access 254238405Sjkim _check_access = true; 255205128Ssimon} 256280304Sjkim 257205128Ssimonchar* LinkInfo::method_string() const { 258280304Sjkim return Method::name_and_sig_as_C_string(_resolved_klass(), _name, _signature); 259238405Sjkim} 260280304Sjkim 261280304Sjkim#ifndef PRODUCT 262238405Sjkimvoid LinkInfo::print() { 263291721Sjkim ResourceMark rm; 264280304Sjkim tty->print_cr("Link resolved_klass=%s name=%s signature=%s current_klass=%s check_access=%s", 265291721Sjkim _resolved_klass->name()->as_C_string(), 266280304Sjkim _name->as_C_string(), 267194206Ssimon _signature->as_C_string(), 268291721Sjkim _current_klass.is_null() ? "(none)" : _current_klass->name()->as_C_string(), 269280304Sjkim _check_access ? "true" : "false"); 270280304Sjkim} 271280304Sjkim#endif // PRODUCT 272280304Sjkim//------------------------------------------------------------------------------------------------------------------------ 273280304Sjkim// Klass resolution 274238405Sjkim 275291721Sjkimvoid LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) { 276280304Sjkim if (!Reflection::verify_class_access(ref_klass(), 277280304Sjkim sel_klass(), 278280304Sjkim true)) { 279280304Sjkim ResourceMark rm(THREAD); 280238405Sjkim Exceptions::fthrow( 281280304Sjkim THREAD_AND_LOCATION, 282280304Sjkim vmSymbols::java_lang_IllegalAccessError(), 283280304Sjkim "tried to access class %s from class %s", 284280304Sjkim sel_klass->external_name(), 285280304Sjkim ref_klass->external_name() 286280304Sjkim ); 287280304Sjkim return; 288238405Sjkim } 289280304Sjkim} 290194206Ssimon 291280304Sjkim//------------------------------------------------------------------------------------------------------------------------ 292194206Ssimon// Method resolution 293238405Sjkim// 294238405Sjkim// According to JVM spec. $5.4.3c & $5.4.3d 295280304Sjkim 296280304Sjkim// Look up method in klasses, including static methods 297238405Sjkim// Then look up local default methods 298238405SjkimmethodHandle LinkResolver::lookup_method_in_klasses(const LinkInfo& link_info, 299280304Sjkim bool checkpolymorphism, 300280304Sjkim bool in_imethod_resolve, TRAPS) { 301238405Sjkim KlassHandle klass = link_info.resolved_klass(); 302238405Sjkim Symbol* name = link_info.name(); 303280304Sjkim Symbol* signature = link_info.signature(); 304280304Sjkim 305280304Sjkim // Ignore overpasses so statics can be found during resolution 306194206Ssimon Method* result = klass->uncached_lookup_method(name, signature, Klass::skip_overpass); 307280304Sjkim 308194206Ssimon if (klass->is_array_klass()) { 309194206Ssimon // Only consider klass and super klass for arrays 310280304Sjkim return methodHandle(THREAD, result); 311194206Ssimon } 312194206Ssimon 313280304Sjkim InstanceKlass* ik = InstanceKlass::cast(klass()); 314194206Ssimon 315194206Ssimon // JDK 8, JVMS 5.4.3.4: Interface method resolution should 316280304Sjkim // ignore static and non-public methods of java.lang.Object, 317194206Ssimon // like clone, finalize, registerNatives. 318194206Ssimon if (in_imethod_resolve && 319280304Sjkim result != NULL && 320194206Ssimon ik->is_interface() && 321194206Ssimon (result->is_static() || !result->is_public()) && 322280304Sjkim result->method_holder() == SystemDictionary::Object_klass()) { 323194206Ssimon result = NULL; 324194206Ssimon } 325280304Sjkim 326194206Ssimon // Before considering default methods, check for an overpass in the 327194206Ssimon // current class if a method has not been found. 328280304Sjkim if (result == NULL) { 329194206Ssimon result = ik->find_method(name, signature); 330194206Ssimon } 331280304Sjkim 332194206Ssimon if (result == NULL) { 333194206Ssimon Array<Method*>* default_methods = ik->default_methods(); 334280304Sjkim if (default_methods != NULL) { 335194206Ssimon result = InstanceKlass::find_method(default_methods, name, signature); 336194206Ssimon } 337280304Sjkim } 338194206Ssimon 339194206Ssimon if (checkpolymorphism && result != NULL) { 340280304Sjkim vmIntrinsics::ID iid = result->intrinsic_id(); 341280304Sjkim if (MethodHandles::is_signature_polymorphic(iid)) { 342280304Sjkim // Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method. 343280304Sjkim return NULL; 344194206Ssimon } 345280304Sjkim } 346194206Ssimon return methodHandle(THREAD, result); 347194206Ssimon} 348280304Sjkim 349280304Sjkim// returns first instance method 350280304Sjkim// Looks up method in classes, then looks up local default methods 351280304SjkimmethodHandle LinkResolver::lookup_instance_method_in_klasses(KlassHandle klass, 352194206Ssimon Symbol* name, 353280304Sjkim Symbol* signature, TRAPS) { 354194206Ssimon Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass); 355194206Ssimon 356280304Sjkim while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) { 357194206Ssimon Klass* super_klass = result->method_holder()->super(); 358194206Ssimon result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass); 359280304Sjkim } 360238405Sjkim 361280304Sjkim if (klass->is_array_klass()) { 362238405Sjkim // Only consider klass and super klass for arrays 363280304Sjkim return methodHandle(THREAD, result); 364238405Sjkim } 365238405Sjkim 366280304Sjkim if (result == NULL) { 367194206Ssimon Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods(); 368194206Ssimon if (default_methods != NULL) { 369280304Sjkim result = InstanceKlass::find_method(default_methods, name, signature); 370280304Sjkim assert(result == NULL || !result->is_static(), "static defaults not allowed"); 371280304Sjkim } 372280304Sjkim } 373238405Sjkim return methodHandle(THREAD, result); 374280304Sjkim} 375238405Sjkim 376280304Sjkimint LinkResolver::vtable_index_of_interface_method(KlassHandle klass, 377238405Sjkim const methodHandle& resolved_method) { 378280304Sjkim 379280304Sjkim int vtable_index = Method::invalid_vtable_index; 380194206Ssimon Symbol* name = resolved_method->name(); 381238405Sjkim Symbol* signature = resolved_method->signature(); 382280304Sjkim InstanceKlass* ik = InstanceKlass::cast(klass()); 383280304Sjkim 384280304Sjkim // First check in default method array 385280304Sjkim if (!resolved_method->is_abstract() && ik->default_methods() != NULL) { 386238405Sjkim int index = InstanceKlass::find_method_index(ik->default_methods(), 387280304Sjkim name, signature, Klass::find_overpass, 388280304Sjkim Klass::find_static, Klass::find_private); 389280304Sjkim if (index >= 0 ) { 390280304Sjkim vtable_index = ik->default_vtable_indices()->at(index); 391280304Sjkim } 392280304Sjkim } 393280304Sjkim if (vtable_index == Method::invalid_vtable_index) { 394280304Sjkim // get vtable_index for miranda methods 395280304Sjkim ResourceMark rm; 396280304Sjkim klassVtable *vt = ik->vtable(); 397280304Sjkim vtable_index = vt->index_of_miranda(name, signature); 398280304Sjkim } 399280304Sjkim return vtable_index; 400280304Sjkim} 401280304Sjkim 402280304SjkimmethodHandle LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info, TRAPS) { 40355714Skris InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass()()); 404109998Smarkm 405280304Sjkim // Specify 'true' in order to skip default methods when searching the 406280304Sjkim // interfaces. Function lookup_method_in_klasses() already looked for 407280304Sjkim // the method in the default methods table. 408280304Sjkim return methodHandle(THREAD, 409280304Sjkim ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), 410280304Sjkim Klass::skip_defaults)); 411109998Smarkm} 412280304Sjkim 413280304SjkimmethodHandle LinkResolver::lookup_polymorphic_method( 414280304Sjkim const LinkInfo& link_info, 415280304Sjkim Handle *appendix_result_or_null, 416280304Sjkim Handle *method_type_result, 417280304Sjkim TRAPS) { 418109998Smarkm KlassHandle klass = link_info.resolved_klass(); 419238405Sjkim Symbol* name = link_info.name(); 420280304Sjkim Symbol* full_signature = link_info.signature(); 421280304Sjkim 422280304Sjkim vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name); 423280304Sjkim if (TraceMethodHandles) { 424280304Sjkim ResourceMark rm(THREAD); 425280304Sjkim tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s", 426238405Sjkim vmIntrinsics::name_at(iid), klass->external_name(), 427162911Ssimon name->as_C_string(), full_signature->as_C_string()); 428280304Sjkim } 429280304Sjkim if (klass() == SystemDictionary::MethodHandle_klass() && 430280304Sjkim iid != vmIntrinsics::_none) { 431280304Sjkim if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) { 432280304Sjkim // Most of these do not need an up-call to Java to resolve, so can be done anywhere. 433280304Sjkim // Do not erase last argument type (MemberName) if it is a static linkTo method. 434162911Ssimon bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid); 435238405Sjkim TempNewSymbol basic_signature = 436280304Sjkim MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL); 437280304Sjkim if (TraceMethodHandles) { 438280304Sjkim ResourceMark rm(THREAD); 439280304Sjkim tty->print_cr("lookup_polymorphic_method %s %s => basic %s", 440280304Sjkim name->as_C_string(), 441280304Sjkim full_signature->as_C_string(), 442280304Sjkim basic_signature->as_C_string()); 443238405Sjkim } 444238405Sjkim methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid, 445280304Sjkim basic_signature, 446280304Sjkim CHECK_NULL); 447280304Sjkim if (result.not_null()) { 448280304Sjkim assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic"); 449280304Sjkim assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this"); 450280304Sjkim assert(basic_signature == result->signature(), "predict the result signature"); 451162911Ssimon if (TraceMethodHandles) { 452194206Ssimon ttyLocker ttyl; 453280304Sjkim tty->print("lookup_polymorphic_method => intrinsic "); 454280304Sjkim result->print_on(tty); 455280304Sjkim } 456280304Sjkim } 457280304Sjkim return result; 458280304Sjkim } else if (iid == vmIntrinsics::_invokeGeneric 459194206Ssimon && THREAD->can_call_java() 460238405Sjkim && appendix_result_or_null != NULL) { 461280304Sjkim // This is a method with type-checking semantics. 462280304Sjkim // We will ask Java code to spin an adapter method for it. 463280304Sjkim if (!MethodHandles::enabled()) { 464280304Sjkim // Make sure the Java part of the runtime has been booted up. 465280304Sjkim Klass* natives = SystemDictionary::MethodHandleNatives_klass(); 466280304Sjkim if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) { 467280304Sjkim SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(), 468280304Sjkim Handle(), 469280304Sjkim Handle(), 470280304Sjkim true, 471280304Sjkim CHECK_NULL); 472280304Sjkim } 473238405Sjkim } 474280304Sjkim 475280304Sjkim Handle appendix; 476280304Sjkim Handle method_type; 477280304Sjkim methodHandle result = SystemDictionary::find_method_handle_invoker( 478280304Sjkim name, 479280304Sjkim full_signature, 480280304Sjkim link_info.current_klass(), 481280304Sjkim &appendix, 482280304Sjkim &method_type, 483160814Ssimon CHECK_NULL); 484280304Sjkim if (TraceMethodHandles) { 485280304Sjkim ttyLocker ttyl; 486280304Sjkim tty->print("lookup_polymorphic_method => (via Java) "); 487280304Sjkim result->print_on(tty); 488280304Sjkim tty->print(" lookup_polymorphic_method => appendix = "); 489160814Ssimon if (appendix.is_null()) tty->print_cr("(none)"); 490280304Sjkim else appendix->print_on(tty); 491280304Sjkim } 492280304Sjkim if (result.not_null()) { 493280304Sjkim#ifdef ASSERT 494280304Sjkim ResourceMark rm(THREAD); 495160814Ssimon 496280304Sjkim TempNewSymbol basic_signature = 497280304Sjkim MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL); 498280304Sjkim int actual_size_of_params = result->size_of_parameters(); 499280304Sjkim int expected_size_of_params = ArgumentSizeComputer(basic_signature).size(); 500280304Sjkim // +1 for MethodHandle.this, +1 for trailing MethodType 501160814Ssimon if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1; 502280304Sjkim if (appendix.not_null()) expected_size_of_params += 1; 503280304Sjkim if (actual_size_of_params != expected_size_of_params) { 504280304Sjkim tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string()); 505280304Sjkim tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid)); 506280304Sjkim result->print(); 507160814Ssimon } 508238405Sjkim assert(actual_size_of_params == expected_size_of_params, 509280304Sjkim "%d != %d", actual_size_of_params, expected_size_of_params); 510280304Sjkim#endif //ASSERT 511280304Sjkim 512280304Sjkim assert(appendix_result_or_null != NULL, ""); 513280304Sjkim (*appendix_result_or_null) = appendix; 514280304Sjkim (*method_type_result) = method_type; 515280304Sjkim } 516280304Sjkim return result; 517280304Sjkim } 518238405Sjkim } 519238405Sjkim return NULL; 520238405Sjkim} 521280304Sjkim 522280304Sjkimvoid LinkResolver::check_method_accessability(KlassHandle ref_klass, 523280304Sjkim KlassHandle resolved_klass, 524280304Sjkim KlassHandle sel_klass, 525280304Sjkim const methodHandle& sel_method, 526280304Sjkim TRAPS) { 527280304Sjkim 528280304Sjkim AccessFlags flags = sel_method->access_flags(); 529238405Sjkim 530238405Sjkim // Special case: arrays always override "clone". JVMS 2.15. 531280304Sjkim // If the resolved klass is an array class, and the declaring class 532280304Sjkim // is java.lang.Object and the method is "clone", set the flags 533280304Sjkim // to public. 534280304Sjkim // 535280304Sjkim // We'll check for the method name first, as that's most likely 536280304Sjkim // to be false (so we'll short-circuit out of these tests). 537280304Sjkim if (sel_method->name() == vmSymbols::clone_name() && 538280304Sjkim sel_klass() == SystemDictionary::Object_klass() && 539238405Sjkim resolved_klass->is_array_klass()) { 540280304Sjkim // We need to change "protected" to "public". 541280304Sjkim assert(flags.is_protected(), "clone not protected?"); 542280304Sjkim jint new_flags = flags.as_int(); 543280304Sjkim new_flags = new_flags & (~JVM_ACC_PROTECTED); 544280304Sjkim new_flags = new_flags | JVM_ACC_PUBLIC; 545280304Sjkim flags.set_flags(new_flags); 546280304Sjkim } 547280304Sjkim// assert(extra_arg_result_or_null != NULL, "must be able to return extra argument"); 548280304Sjkim 549280304Sjkim if (!Reflection::verify_field_access(ref_klass(), 550280304Sjkim resolved_klass(), 551280304Sjkim sel_klass(), 552280304Sjkim flags, 553280304Sjkim true)) { 55455714Skris ResourceMark rm(THREAD); 555109998Smarkm Exceptions::fthrow( 556280304Sjkim THREAD_AND_LOCATION, 557280304Sjkim vmSymbols::java_lang_IllegalAccessError(), 558280304Sjkim "tried to access method %s.%s%s from class %s", 559280304Sjkim sel_klass->external_name(), 560280304Sjkim sel_method->name()->as_C_string(), 561280304Sjkim sel_method->signature()->as_C_string(), 56255714Skris ref_klass->external_name() 563280304Sjkim ); 564280304Sjkim return; 565280304Sjkim } 566280304Sjkim} 567280304Sjkim 568280304SjkimmethodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code, 569109998Smarkm const constantPoolHandle& pool, int index, TRAPS) { 570291721Sjkim // This method is used only 571280304Sjkim // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call), 572280304Sjkim // and 573280304Sjkim // (2) in Bytecode_invoke::static_target 574280304Sjkim // It appears to fail when applied to an invokeinterface call site. 575280304Sjkim // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points. 576109998Smarkm // resolve klass 577280304Sjkim KlassHandle resolved_klass; 578280304Sjkim if (code == Bytecodes::_invokedynamic) { 579280304Sjkim resolved_klass = SystemDictionary::MethodHandle_klass(); 580280304Sjkim Symbol* method_name = vmSymbols::invoke_name(); 581280304Sjkim Symbol* method_signature = pool->signature_ref_at(index); 582160814Ssimon KlassHandle current_klass(THREAD, pool->pool_holder()); 583280304Sjkim LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass); 584280304Sjkim return resolve_method(link_info, /*require_methodref*/false, THREAD); 585280304Sjkim } 586280304Sjkim 587280304Sjkim LinkInfo link_info(pool, index, CHECK_NULL); 588160814Ssimon resolved_klass = link_info.resolved_klass(); 589280304Sjkim 590280304Sjkim if (pool->has_preresolution() 591280304Sjkim || (resolved_klass() == SystemDictionary::MethodHandle_klass() && 592280304Sjkim MethodHandles::is_signature_polymorphic_name(resolved_klass(), link_info.name()))) { 593280304Sjkim Method* result = ConstantPool::method_at_if_loaded(pool, index); 594160814Ssimon if (result != NULL) { 595280304Sjkim return methodHandle(THREAD, result); 596280304Sjkim } 597280304Sjkim } 598280304Sjkim 599280304Sjkim if (code == Bytecodes::_invokeinterface) { 600160814Ssimon return resolve_interface_method(link_info, true, THREAD); 601238405Sjkim } else if (code == Bytecodes::_invokevirtual) { 602280304Sjkim return resolve_method(link_info, /*require_methodref*/true, THREAD); 603280304Sjkim } else if (!resolved_klass->is_interface()) { 604280304Sjkim return resolve_method(link_info, /*require_methodref*/false, THREAD); 605280304Sjkim } else { 606238405Sjkim bool nostatics = (code == Bytecodes::_invokestatic) ? false : true; 607238405Sjkim return resolve_interface_method(link_info, nostatics, THREAD); 608280304Sjkim } 609280304Sjkim} 610280304Sjkim 611280304Sjkim// Check and print a loader constraint violation message for method or interface method 612280304Sjkimvoid LinkResolver::check_method_loader_constraints(const LinkInfo& link_info, 613280304Sjkim const methodHandle& resolved_method, 614280304Sjkim const char* method_type, TRAPS) { 615280304Sjkim Handle current_loader(THREAD, link_info.current_klass()->class_loader()); 616280304Sjkim Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader()); 617238405Sjkim 618194206Ssimon ResourceMark rm(THREAD); 619280304Sjkim Symbol* failed_type_symbol = 620280304Sjkim SystemDictionary::check_signature_loaders(link_info.signature(), current_loader, 621280304Sjkim resolved_loader, true, CHECK); 622280304Sjkim if (failed_type_symbol != NULL) { 623280304Sjkim const char* msg = "loader constraint violation: when resolving %s" 624280304Sjkim " \"%s\" the class loader (instance of %s) of the current class, %s," 625162911Ssimon " and the class loader (instance of %s) for the method's defining class, %s, have" 626280304Sjkim " different Class objects for the type %s used in the signature"; 627280304Sjkim char* sig = link_info.method_string(); 628280304Sjkim const char* loader1_name = SystemDictionary::loader_name(current_loader()); 629280304Sjkim char* current = link_info.current_klass()->name()->as_C_string(); 630280304Sjkim const char* loader2_name = SystemDictionary::loader_name(resolved_loader()); 631280304Sjkim char* target = resolved_method->method_holder()->name()->as_C_string(); 632162911Ssimon char* failed_type_name = failed_type_symbol->as_C_string(); 633194206Ssimon size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1_name) + 634280304Sjkim strlen(current) + strlen(loader2_name) + strlen(target) + 635280304Sjkim strlen(failed_type_name) + strlen(method_type) + 1; 636280304Sjkim char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); 637280304Sjkim jio_snprintf(buf, buflen, msg, method_type, sig, loader1_name, current, loader2_name, 638280304Sjkim target, failed_type_name); 639280304Sjkim THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); 640162911Ssimon } 641238405Sjkim} 642280304Sjkim 643280304Sjkimvoid LinkResolver::check_field_loader_constraints(Symbol* field, Symbol* sig, 644280304Sjkim KlassHandle current_klass, 645280304Sjkim KlassHandle sel_klass, TRAPS) { 646280304Sjkim Handle ref_loader(THREAD, current_klass->class_loader()); 647280304Sjkim Handle sel_loader(THREAD, sel_klass->class_loader()); 648280304Sjkim 649280304Sjkim ResourceMark rm(THREAD); // needed for check_signature_loaders 650280304Sjkim Symbol* failed_type_symbol = 651280304Sjkim SystemDictionary::check_signature_loaders(sig, 652280304Sjkim ref_loader, sel_loader, 653280304Sjkim false, 654280304Sjkim CHECK); 655238405Sjkim if (failed_type_symbol != NULL) { 656238405Sjkim const char* msg = "loader constraint violation: when resolving field" 657280304Sjkim " \"%s\" the class loader (instance of %s) of the referring class, " 658280304Sjkim "%s, and the class loader (instance of %s) for the field's resolved " 659280304Sjkim "type, %s, have different Class objects for that type"; 660280304Sjkim char* field_name = field->as_C_string(); 661280304Sjkim const char* loader1_name = SystemDictionary::loader_name(ref_loader()); 662280304Sjkim char* sel = sel_klass->name()->as_C_string(); 663280304Sjkim const char* loader2_name = SystemDictionary::loader_name(sel_loader()); 664280304Sjkim char* failed_type_name = failed_type_symbol->as_C_string(); 665280304Sjkim size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1_name) + 666280304Sjkim strlen(sel) + strlen(loader2_name) + strlen(failed_type_name) + 1; 667280304Sjkim char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen); 668280304Sjkim jio_snprintf(buf, buflen, msg, field_name, loader1_name, sel, loader2_name, 669238405Sjkim failed_type_name); 670238405Sjkim THROW_MSG(vmSymbols::java_lang_LinkageError(), buf); 671238405Sjkim } 672280304Sjkim} 673280304Sjkim 674280304SjkimmethodHandle LinkResolver::resolve_method(const LinkInfo& link_info, 675280304Sjkim bool require_methodref, TRAPS) { 676280304Sjkim 677280304Sjkim Handle nested_exception; 678280304Sjkim KlassHandle resolved_klass = link_info.resolved_klass(); 679280304Sjkim 680238405Sjkim // 1. check if methodref required, that resolved_klass is not interfacemethodref 681238405Sjkim if (require_methodref && resolved_klass->is_interface()) { 682280304Sjkim ResourceMark rm(THREAD); 683280304Sjkim char buf[200]; 684280304Sjkim jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected", 685280304Sjkim resolved_klass()->external_name()); 686280304Sjkim THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 687280304Sjkim } 688280304Sjkim 689280304Sjkim // 2. lookup method in resolved klass and its super klasses 690238405Sjkim methodHandle resolved_method = lookup_method_in_klasses(link_info, true, false, CHECK_NULL); 691280304Sjkim 692280304Sjkim if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy 693280304Sjkim // 3. lookup method in all the interfaces implemented by the resolved klass 694280304Sjkim resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL); 695280304Sjkim 696280304Sjkim if (resolved_method.is_null()) { 697280304Sjkim // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc 698280304Sjkim resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, (Handle*)NULL, THREAD); 699280304Sjkim if (HAS_PENDING_EXCEPTION) { 700280304Sjkim nested_exception = Handle(THREAD, PENDING_EXCEPTION); 701280304Sjkim CLEAR_PENDING_EXCEPTION; 702280304Sjkim } 703280304Sjkim } 704280304Sjkim } 70555714Skris 706280304Sjkim if (resolved_method.is_null()) { 70755714Skris // 4. method lookup failed 708280304Sjkim ResourceMark rm(THREAD); 709280304Sjkim THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(), 710280304Sjkim Method::name_and_sig_as_C_string(resolved_klass(), 711280304Sjkim link_info.name(), 712280304Sjkim link_info.signature()), 713280304Sjkim nested_exception, NULL); 714280304Sjkim } 715280304Sjkim 716280304Sjkim // 5. access checks, access checking may be turned off when calling from within the VM. 717280304Sjkim KlassHandle current_klass = link_info.current_klass(); 718280304Sjkim if (link_info.check_access()) { 719280304Sjkim assert(current_klass.not_null() , "current_klass should not be null"); 720280304Sjkim 721280304Sjkim // check if method can be accessed by the referring class 722280304Sjkim check_method_accessability(current_klass, 723280304Sjkim resolved_klass, 724280304Sjkim KlassHandle(THREAD, resolved_method->method_holder()), 72555714Skris resolved_method, 726280304Sjkim CHECK_NULL); 727280304Sjkim 728280304Sjkim // check loader constraints 729280304Sjkim check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL); 730280304Sjkim } 731280304Sjkim 73255714Skris return resolved_method; 733280304Sjkim} 734280304Sjkim 735280304Sjkimstatic void trace_method_resolution(const char* prefix, 736280304Sjkim KlassHandle klass, 737280304Sjkim KlassHandle resolved_klass, 738280304Sjkim const methodHandle& method, 739280304Sjkim bool logitables, 740280304Sjkim int index = -1) { 741280304Sjkim#ifndef PRODUCT 742280304Sjkim ResourceMark rm; 743280304Sjkim outputStream* st; 744280304Sjkim if (logitables) { 745280304Sjkim st = LogHandle(itables)::trace_stream(); 746280304Sjkim } else { 747280304Sjkim st = LogHandle(vtables)::trace_stream(); 748280304Sjkim } 749280304Sjkim st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ", 750280304Sjkim prefix, 751280304Sjkim (klass.is_null() ? "<NULL>" : klass->internal_name()), 752280304Sjkim (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()), 753280304Sjkim Method::name_and_sig_as_C_string(resolved_klass(), 754280304Sjkim method->name(), 755280304Sjkim method->signature()), 756280304Sjkim method->method_holder()->internal_name()); 757280304Sjkim method->print_linkage_flags(st); 758280304Sjkim if (index != -1) { 759280304Sjkim st->print("vtable_index:%d", index); 760280304Sjkim } 761280304Sjkim st->cr(); 762280304Sjkim#endif // PRODUCT 763280304Sjkim} 764280304Sjkim 765280304SjkimmethodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, 766280304Sjkim bool nostatics, TRAPS) { 767280304Sjkim 768280304Sjkim KlassHandle resolved_klass = link_info.resolved_klass(); 769280304Sjkim 770280304Sjkim // check if klass is interface 771280304Sjkim if (!resolved_klass->is_interface()) { 772280304Sjkim ResourceMark rm(THREAD); 773280304Sjkim char buf[200]; 774280304Sjkim jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name()); 775280304Sjkim THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 776238405Sjkim } 777280304Sjkim 778280304Sjkim // lookup method in this interface or its super, java.lang.Object 779280304Sjkim // JDK8: also look for static methods 780280304Sjkim methodHandle resolved_method = lookup_method_in_klasses(link_info, false, true, CHECK_NULL); 781238405Sjkim 78255714Skris if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { 78355714Skris // lookup method in all the super-interfaces 78455714Skris resolved_method = lookup_method_in_interfaces(link_info, CHECK_NULL); 78555714Skris } 786 787 if (resolved_method.is_null()) { 788 // no method found 789 ResourceMark rm(THREAD); 790 THROW_MSG_NULL(vmSymbols::java_lang_NoSuchMethodError(), 791 Method::name_and_sig_as_C_string(resolved_klass(), 792 link_info.name(), 793 link_info.signature())); 794 } 795 796 if (link_info.check_access()) { 797 // JDK8 adds non-public interface methods, and accessability check requirement 798 KlassHandle current_klass = link_info.current_klass(); 799 800 assert(current_klass.not_null() , "current_klass should not be null"); 801 802 // check if method can be accessed by the referring class 803 check_method_accessability(current_klass, 804 resolved_klass, 805 KlassHandle(THREAD, resolved_method->method_holder()), 806 resolved_method, 807 CHECK_NULL); 808 809 check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL); 810 } 811 812 if (nostatics && resolved_method->is_static()) { 813 ResourceMark rm(THREAD); 814 char buf[200]; 815 jio_snprintf(buf, sizeof(buf), "Expected instance not static method %s", 816 Method::name_and_sig_as_C_string(resolved_klass(), 817 resolved_method->name(), resolved_method->signature())); 818 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 819 } 820 821 if (develop_log_is_enabled(Trace, itables)) { 822 trace_method_resolution("invokeinterface resolved method: caller-class", 823 link_info.current_klass(), resolved_klass, 824 resolved_method, true); 825 } 826 827 return resolved_method; 828} 829 830//------------------------------------------------------------------------------------------------------------------------ 831// Field resolution 832 833void LinkResolver::check_field_accessability(KlassHandle ref_klass, 834 KlassHandle resolved_klass, 835 KlassHandle sel_klass, 836 const fieldDescriptor& fd, 837 TRAPS) { 838 if (!Reflection::verify_field_access(ref_klass(), 839 resolved_klass(), 840 sel_klass(), 841 fd.access_flags(), 842 true)) { 843 ResourceMark rm(THREAD); 844 Exceptions::fthrow( 845 THREAD_AND_LOCATION, 846 vmSymbols::java_lang_IllegalAccessError(), 847 "tried to access field %s.%s from class %s", 848 sel_klass->external_name(), 849 fd.name()->as_C_string(), 850 ref_klass->external_name() 851 ); 852 return; 853 } 854} 855 856void LinkResolver::resolve_field_access(fieldDescriptor& fd, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) { 857 LinkInfo link_info(pool, index, CHECK); 858 resolve_field(fd, link_info, byte, true, CHECK); 859} 860 861void LinkResolver::resolve_field(fieldDescriptor& fd, 862 const LinkInfo& link_info, 863 Bytecodes::Code byte, bool initialize_class, 864 TRAPS) { 865 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || 866 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield || 867 byte == Bytecodes::_nofast_getfield || byte == Bytecodes::_nofast_putfield || 868 (byte == Bytecodes::_nop && !link_info.check_access()), "bad field access bytecode"); 869 870 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); 871 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic || byte == Bytecodes::_nofast_putfield); 872 // Check if there's a resolved klass containing the field 873 KlassHandle resolved_klass = link_info.resolved_klass(); 874 Symbol* field = link_info.name(); 875 Symbol* sig = link_info.signature(); 876 877 if (resolved_klass.is_null()) { 878 ResourceMark rm(THREAD); 879 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 880 } 881 882 // Resolve instance field 883 KlassHandle sel_klass(THREAD, resolved_klass->find_field(field, sig, &fd)); 884 // check if field exists; i.e., if a klass containing the field def has been selected 885 if (sel_klass.is_null()) { 886 ResourceMark rm(THREAD); 887 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); 888 } 889 890 if (!link_info.check_access()) 891 // Access checking may be turned off when calling from within the VM. 892 return; 893 894 // check access 895 KlassHandle current_klass = link_info.current_klass(); 896 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); 897 898 // check for errors 899 if (is_static != fd.is_static()) { 900 ResourceMark rm(THREAD); 901 char msg[200]; 902 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string()); 903 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); 904 } 905 906 // Final fields can only be accessed from its own class. 907 if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) { 908 THROW(vmSymbols::java_lang_IllegalAccessError()); 909 } 910 911 // initialize resolved_klass if necessary 912 // note 1: the klass which declared the field must be initialized (i.e, sel_klass) 913 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) 914 // 915 // note 2: we don't want to force initialization if we are just checking 916 // if the field access is legal; e.g., during compilation 917 if (is_static && initialize_class) { 918 sel_klass->initialize(CHECK); 919 } 920 921 if (sel_klass() != current_klass()) { 922 check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK); 923 } 924 925 // return information. note that the klass is set to the actual klass containing the 926 // field, otherwise access of static fields in superclasses will not work. 927} 928 929 930//------------------------------------------------------------------------------------------------------------------------ 931// Invoke resolution 932// 933// Naming conventions: 934// 935// resolved_method the specified method (i.e., static receiver specified via constant pool index) 936// sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class) 937// resolved_klass the specified klass (i.e., specified via constant pool index) 938// recv_klass the receiver klass 939 940 941void LinkResolver::resolve_static_call(CallInfo& result, 942 const LinkInfo& link_info, 943 bool initialize_class, TRAPS) { 944 methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK); 945 946 // The resolved class can change as a result of this resolution. 947 KlassHandle resolved_klass = KlassHandle(THREAD, resolved_method->method_holder()); 948 949 Method* save_resolved_method = resolved_method(); 950 // Initialize klass (this should only happen if everything is ok) 951 if (initialize_class && resolved_klass->should_be_initialized()) { 952 resolved_klass->initialize(CHECK); 953 // Use updated LinkInfo (to reresolve with resolved_klass as method_holder?) 954 LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(), 955 link_info.current_klass(), link_info.check_access()); 956 resolved_method = linktime_resolve_static_method(new_info, CHECK); 957 } 958 959 assert(save_resolved_method == resolved_method(), "does this change?"); 960 // setup result 961 result.set_static(resolved_klass, resolved_method, CHECK); 962} 963 964// throws linktime exceptions 965methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) { 966 967 KlassHandle resolved_klass = link_info.resolved_klass(); 968 methodHandle resolved_method; 969 if (!resolved_klass->is_interface()) { 970 resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL); 971 } else { 972 resolved_method = resolve_interface_method(link_info, /*nostatics*/false, CHECK_NULL); 973 } 974 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier"); 975 976 // check if static 977 if (!resolved_method->is_static()) { 978 ResourceMark rm(THREAD); 979 char buf[200]; 980 jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(), 981 resolved_method->name(), 982 resolved_method->signature())); 983 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 984 } 985 return resolved_method; 986} 987 988 989void LinkResolver::resolve_special_call(CallInfo& result, 990 const LinkInfo& link_info, 991 TRAPS) { 992 methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK); 993 runtime_resolve_special_method(result, resolved_method, 994 link_info.resolved_klass(), 995 link_info.current_klass(), 996 link_info.check_access(), CHECK); 997} 998 999// throws linktime exceptions 1000methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, 1001 TRAPS) { 1002 1003 // Invokespecial is called for multiple special reasons: 1004 // <init> 1005 // local private method invocation, for classes and interfaces 1006 // superclass.method, which can also resolve to a default method 1007 // and the selected method is recalculated relative to the direct superclass 1008 // superinterface.method, which explicitly does not check shadowing 1009 KlassHandle resolved_klass = link_info.resolved_klass(); 1010 methodHandle resolved_method; 1011 1012 if (!resolved_klass->is_interface()) { 1013 resolved_method = resolve_method(link_info, /*require_methodref*/false, CHECK_NULL); 1014 } else { 1015 resolved_method = resolve_interface_method(link_info, /*nostatics*/true, CHECK_NULL); 1016 } 1017 1018 // check if method name is <init>, that it is found in same klass as static type 1019 if (resolved_method->name() == vmSymbols::object_initializer_name() && 1020 resolved_method->method_holder() != resolved_klass()) { 1021 ResourceMark rm(THREAD); 1022 Exceptions::fthrow( 1023 THREAD_AND_LOCATION, 1024 vmSymbols::java_lang_NoSuchMethodError(), 1025 "%s: method %s%s not found", 1026 resolved_klass->external_name(), 1027 resolved_method->name()->as_C_string(), 1028 resolved_method->signature()->as_C_string() 1029 ); 1030 return NULL; 1031 } 1032 1033 // check if invokespecial's interface method reference is in an indirect superinterface 1034 KlassHandle current_klass = link_info.current_klass(); 1035 if (!current_klass.is_null() && resolved_klass->is_interface()) { 1036 Klass *klass_to_check = !InstanceKlass::cast(current_klass())->is_anonymous() ? 1037 current_klass() : 1038 InstanceKlass::cast(current_klass())->host_klass(); 1039 // Disable verification for the dynamically-generated reflection bytecodes. 1040 bool is_reflect = klass_to_check->is_subclass_of( 1041 SystemDictionary::reflect_MagicAccessorImpl_klass()); 1042 1043 if (!is_reflect && 1044 !InstanceKlass::cast(klass_to_check)->is_same_or_direct_interface(resolved_klass())) { 1045 ResourceMark rm(THREAD); 1046 char buf[200]; 1047 jio_snprintf(buf, sizeof(buf), 1048 "Interface method reference: %s, is in an indirect superinterface of %s", 1049 Method::name_and_sig_as_C_string(resolved_klass(), 1050 resolved_method->name(), 1051 resolved_method->signature()), 1052 current_klass->external_name()); 1053 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1054 } 1055 } 1056 1057 // check if not static 1058 if (resolved_method->is_static()) { 1059 ResourceMark rm(THREAD); 1060 char buf[200]; 1061 jio_snprintf(buf, sizeof(buf), 1062 "Expecting non-static method %s", 1063 Method::name_and_sig_as_C_string(resolved_klass(), 1064 resolved_method->name(), 1065 resolved_method->signature())); 1066 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1067 } 1068 1069 if (develop_log_is_enabled(Trace, itables)) { 1070 trace_method_resolution("invokespecial resolved method: caller-class:", 1071 current_klass, resolved_klass, resolved_method, true); 1072 } 1073 1074 return resolved_method; 1075} 1076 1077// throws runtime exceptions 1078void LinkResolver::runtime_resolve_special_method(CallInfo& result, 1079 const methodHandle& resolved_method, 1080 KlassHandle resolved_klass, 1081 KlassHandle current_klass, 1082 bool check_access, TRAPS) { 1083 1084 // resolved method is selected method unless we have an old-style lookup 1085 // for a superclass method 1086 // Invokespecial for a superinterface, resolved method is selected method, 1087 // no checks for shadowing 1088 methodHandle sel_method(THREAD, resolved_method()); 1089 1090 // check if this is an old-style super call and do a new lookup if so 1091 { KlassHandle method_klass = KlassHandle(THREAD, 1092 resolved_method->method_holder()); 1093 1094 if (check_access && 1095 // a) check if ACC_SUPER flag is set for the current class 1096 (current_klass->is_super() || !AllowNonVirtualCalls) && 1097 // b) check if the class of the resolved_klass is a superclass 1098 // (not supertype in order to exclude interface classes) of the current class. 1099 // This check is not performed for super.invoke for interface methods 1100 // in super interfaces. 1101 current_klass->is_subclass_of(resolved_klass()) && 1102 current_klass() != resolved_klass() && 1103 // c) check if the method is not <init> 1104 resolved_method->name() != vmSymbols::object_initializer_name()) { 1105 // Lookup super method 1106 KlassHandle super_klass(THREAD, current_klass->super()); 1107 sel_method = lookup_instance_method_in_klasses(super_klass, 1108 resolved_method->name(), 1109 resolved_method->signature(), CHECK); 1110 // check if found 1111 if (sel_method.is_null()) { 1112 ResourceMark rm(THREAD); 1113 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1114 Method::name_and_sig_as_C_string(resolved_klass(), 1115 resolved_method->name(), 1116 resolved_method->signature())); 1117 } 1118 } 1119 } 1120 1121 // check if not static 1122 if (sel_method->is_static()) { 1123 ResourceMark rm(THREAD); 1124 char buf[200]; 1125 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), 1126 resolved_method->name(), 1127 resolved_method->signature())); 1128 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1129 } 1130 1131 // check if abstract 1132 if (sel_method->is_abstract()) { 1133 ResourceMark rm(THREAD); 1134 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1135 Method::name_and_sig_as_C_string(resolved_klass(), 1136 sel_method->name(), 1137 sel_method->signature())); 1138 } 1139 1140 if (develop_log_is_enabled(Trace, itables)) { 1141 trace_method_resolution("invokespecial selected method: resolved-class:", 1142 resolved_klass, resolved_klass, sel_method, true); 1143 } 1144 1145 // setup result 1146 result.set_static(resolved_klass, sel_method, CHECK); 1147} 1148 1149void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, 1150 const LinkInfo& link_info, 1151 bool check_null_and_abstract, TRAPS) { 1152 methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK); 1153 runtime_resolve_virtual_method(result, resolved_method, 1154 link_info.resolved_klass(), 1155 recv, receiver_klass, 1156 check_null_and_abstract, CHECK); 1157} 1158 1159// throws linktime exceptions 1160methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info, 1161 TRAPS) { 1162 // normal method resolution 1163 methodHandle resolved_method = resolve_method(link_info, /*require_methodref*/true, CHECK_NULL); 1164 1165 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1166 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1167 1168 // check if private interface method 1169 KlassHandle resolved_klass = link_info.resolved_klass(); 1170 KlassHandle current_klass = link_info.current_klass(); 1171 1172 if (resolved_klass->is_interface() && resolved_method->is_private()) { 1173 ResourceMark rm(THREAD); 1174 char buf[200]; 1175 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s", 1176 Method::name_and_sig_as_C_string(resolved_klass(), 1177 resolved_method->name(), 1178 resolved_method->signature()), 1179 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name())); 1180 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1181 } 1182 1183 // check if not static 1184 if (resolved_method->is_static()) { 1185 ResourceMark rm(THREAD); 1186 char buf[200]; 1187 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(), 1188 resolved_method->name(), 1189 resolved_method->signature())); 1190 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1191 } 1192 1193 if (develop_log_is_enabled(Trace, vtables)) { 1194 trace_method_resolution("invokevirtual resolved method: caller-class:", 1195 current_klass, resolved_klass, resolved_method, false); 1196 } 1197 1198 return resolved_method; 1199} 1200 1201// throws runtime exceptions 1202void LinkResolver::runtime_resolve_virtual_method(CallInfo& result, 1203 const methodHandle& resolved_method, 1204 KlassHandle resolved_klass, 1205 Handle recv, 1206 KlassHandle recv_klass, 1207 bool check_null_and_abstract, 1208 TRAPS) { 1209 1210 // setup default return values 1211 int vtable_index = Method::invalid_vtable_index; 1212 methodHandle selected_method; 1213 1214 assert(recv.is_null() || recv->is_oop(), "receiver is not an oop"); 1215 1216 // runtime method resolution 1217 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists 1218 THROW(vmSymbols::java_lang_NullPointerException()); 1219 } 1220 1221 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s 1222 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since 1223 // a missing receiver might result in a bogus lookup. 1224 assert(resolved_method->method_holder()->is_linked(), "must be linked"); 1225 1226 // do lookup based on receiver klass using the vtable index 1227 if (resolved_method->method_holder()->is_interface()) { // default or miranda method 1228 vtable_index = vtable_index_of_interface_method(resolved_klass, 1229 resolved_method); 1230 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); 1231 1232 InstanceKlass* inst = InstanceKlass::cast(recv_klass()); 1233 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); 1234 } else { 1235 // at this point we are sure that resolved_method is virtual and not 1236 // a default or miranda method; therefore, it must have a valid vtable index. 1237 assert(!resolved_method->has_itable_index(), ""); 1238 vtable_index = resolved_method->vtable_index(); 1239 // We could get a negative vtable_index for final methods, 1240 // because as an optimization they are they are never put in the vtable, 1241 // unless they override an existing method. 1242 // If we do get a negative, it means the resolved method is the the selected 1243 // method, and it can never be changed by an override. 1244 if (vtable_index == Method::nonvirtual_vtable_index) { 1245 assert(resolved_method->can_be_statically_bound(), "cannot override this method"); 1246 selected_method = resolved_method; 1247 } else { 1248 // recv_klass might be an arrayKlassOop but all vtables start at 1249 // the same place. The cast is to avoid virtual call and assertion. 1250 InstanceKlass* inst = (InstanceKlass*)recv_klass(); 1251 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); 1252 } 1253 } 1254 1255 // check if method exists 1256 if (selected_method.is_null()) { 1257 ResourceMark rm(THREAD); 1258 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1259 Method::name_and_sig_as_C_string(resolved_klass(), 1260 resolved_method->name(), 1261 resolved_method->signature())); 1262 } 1263 1264 // check if abstract 1265 if (check_null_and_abstract && selected_method->is_abstract()) { 1266 ResourceMark rm(THREAD); 1267 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1268 Method::name_and_sig_as_C_string(resolved_klass(), 1269 selected_method->name(), 1270 selected_method->signature())); 1271 } 1272 1273 if (develop_log_is_enabled(Trace, vtables)) { 1274 trace_method_resolution("invokevirtual selected method: receiver-class:", 1275 recv_klass, resolved_klass, selected_method, 1276 false, vtable_index); 1277 } 1278 // setup result 1279 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK); 1280} 1281 1282void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, 1283 const LinkInfo& link_info, 1284 bool check_null_and_abstract, TRAPS) { 1285 // throws linktime exceptions 1286 methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK); 1287 runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(), 1288 recv, recv_klass, check_null_and_abstract, CHECK); 1289} 1290 1291methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info, 1292 TRAPS) { 1293 // normal interface method resolution 1294 methodHandle resolved_method = resolve_interface_method(link_info, true, CHECK_NULL); 1295 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier"); 1296 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier"); 1297 1298 return resolved_method; 1299} 1300 1301// throws runtime exceptions 1302void LinkResolver::runtime_resolve_interface_method(CallInfo& result, 1303 const methodHandle& resolved_method, 1304 KlassHandle resolved_klass, 1305 Handle recv, 1306 KlassHandle recv_klass, 1307 bool check_null_and_abstract, TRAPS) { 1308 // check if receiver exists 1309 if (check_null_and_abstract && recv.is_null()) { 1310 THROW(vmSymbols::java_lang_NullPointerException()); 1311 } 1312 1313 // check if private interface method 1314 if (resolved_klass->is_interface() && resolved_method->is_private()) { 1315 ResourceMark rm(THREAD); 1316 char buf[200]; 1317 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s", 1318 Method::name_and_sig_as_C_string(resolved_klass(), 1319 resolved_method->name(), 1320 resolved_method->signature())); 1321 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1322 } 1323 1324 // check if receiver klass implements the resolved interface 1325 if (!recv_klass->is_subtype_of(resolved_klass())) { 1326 ResourceMark rm(THREAD); 1327 char buf[200]; 1328 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s", 1329 recv_klass()->external_name(), 1330 resolved_klass()->external_name()); 1331 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf); 1332 } 1333 1334 // do lookup based on receiver klass 1335 // This search must match the linktime preparation search for itable initialization 1336 // to correctly enforce loader constraints for interface method inheritance 1337 methodHandle sel_method = lookup_instance_method_in_klasses(recv_klass, 1338 resolved_method->name(), 1339 resolved_method->signature(), CHECK); 1340 if (sel_method.is_null() && !check_null_and_abstract) { 1341 // In theory this is a harmless placeholder value, but 1342 // in practice leaving in null affects the nsk default method tests. 1343 // This needs further study. 1344 sel_method = resolved_method; 1345 } 1346 // check if method exists 1347 if (sel_method.is_null()) { 1348 ResourceMark rm(THREAD); 1349 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1350 Method::name_and_sig_as_C_string(recv_klass(), 1351 resolved_method->name(), 1352 resolved_method->signature())); 1353 } 1354 // check access 1355 // Throw Illegal Access Error if sel_method is not public. 1356 if (!sel_method->is_public()) { 1357 ResourceMark rm(THREAD); 1358 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), 1359 Method::name_and_sig_as_C_string(recv_klass(), 1360 sel_method->name(), 1361 sel_method->signature())); 1362 } 1363 // check if abstract 1364 if (check_null_and_abstract && sel_method->is_abstract()) { 1365 ResourceMark rm(THREAD); 1366 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), 1367 Method::name_and_sig_as_C_string(recv_klass(), 1368 sel_method->name(), 1369 sel_method->signature())); 1370 } 1371 1372 if (develop_log_is_enabled(Trace, itables)) { 1373 trace_method_resolution("invokeinterface selected method: receiver-class", 1374 recv_klass, resolved_klass, sel_method, true); 1375 } 1376 // setup result 1377 if (!resolved_method->has_itable_index()) { 1378 int vtable_index = resolved_method->vtable_index(); 1379 assert(vtable_index == sel_method->vtable_index(), "sanity check"); 1380 result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK); 1381 } else { 1382 int itable_index = resolved_method()->itable_index(); 1383 result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK); 1384 } 1385} 1386 1387 1388methodHandle LinkResolver::linktime_resolve_interface_method_or_null( 1389 const LinkInfo& link_info) { 1390 EXCEPTION_MARK; 1391 methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD); 1392 if (HAS_PENDING_EXCEPTION) { 1393 CLEAR_PENDING_EXCEPTION; 1394 return methodHandle(); 1395 } else { 1396 return method_result; 1397 } 1398} 1399 1400methodHandle LinkResolver::linktime_resolve_virtual_method_or_null( 1401 const LinkInfo& link_info) { 1402 EXCEPTION_MARK; 1403 methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD); 1404 if (HAS_PENDING_EXCEPTION) { 1405 CLEAR_PENDING_EXCEPTION; 1406 return methodHandle(); 1407 } else { 1408 return method_result; 1409 } 1410} 1411 1412methodHandle LinkResolver::resolve_virtual_call_or_null( 1413 KlassHandle receiver_klass, 1414 const LinkInfo& link_info) { 1415 EXCEPTION_MARK; 1416 CallInfo info; 1417 resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1418 if (HAS_PENDING_EXCEPTION) { 1419 CLEAR_PENDING_EXCEPTION; 1420 return methodHandle(); 1421 } 1422 return info.selected_method(); 1423} 1424 1425methodHandle LinkResolver::resolve_interface_call_or_null( 1426 KlassHandle receiver_klass, 1427 const LinkInfo& link_info) { 1428 EXCEPTION_MARK; 1429 CallInfo info; 1430 resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD); 1431 if (HAS_PENDING_EXCEPTION) { 1432 CLEAR_PENDING_EXCEPTION; 1433 return methodHandle(); 1434 } 1435 return info.selected_method(); 1436} 1437 1438int LinkResolver::resolve_virtual_vtable_index(KlassHandle receiver_klass, 1439 const LinkInfo& link_info) { 1440 EXCEPTION_MARK; 1441 CallInfo info; 1442 resolve_virtual_call(info, Handle(), receiver_klass, link_info, 1443 /*check_null_or_abstract*/false, THREAD); 1444 if (HAS_PENDING_EXCEPTION) { 1445 CLEAR_PENDING_EXCEPTION; 1446 return Method::invalid_vtable_index; 1447 } 1448 return info.vtable_index(); 1449} 1450 1451methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) { 1452 EXCEPTION_MARK; 1453 CallInfo info; 1454 resolve_static_call(info, link_info, /*initialize_class*/false, THREAD); 1455 if (HAS_PENDING_EXCEPTION) { 1456 CLEAR_PENDING_EXCEPTION; 1457 return methodHandle(); 1458 } 1459 return info.selected_method(); 1460} 1461 1462methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) { 1463 EXCEPTION_MARK; 1464 CallInfo info; 1465 resolve_special_call(info, link_info, THREAD); 1466 if (HAS_PENDING_EXCEPTION) { 1467 CLEAR_PENDING_EXCEPTION; 1468 return methodHandle(); 1469 } 1470 return info.selected_method(); 1471} 1472 1473 1474 1475//------------------------------------------------------------------------------------------------------------------------ 1476// ConstantPool entries 1477 1478void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) { 1479 switch (byte) { 1480 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break; 1481 case Bytecodes::_invokespecial : resolve_invokespecial (result, pool, index, CHECK); break; 1482 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break; 1483 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break; 1484 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break; 1485 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break; 1486 } 1487 return; 1488} 1489 1490void LinkResolver::resolve_invoke(CallInfo& result, Handle& recv, 1491 const methodHandle& attached_method, 1492 Bytecodes::Code byte, TRAPS) { 1493 KlassHandle defc = attached_method->method_holder(); 1494 Symbol* name = attached_method->name(); 1495 Symbol* type = attached_method->signature(); 1496 LinkInfo link_info(defc, name, type, KlassHandle(), /*check_access=*/false); 1497 switch(byte) { 1498 case Bytecodes::_invokevirtual: 1499 resolve_virtual_call(result, recv, recv->klass(), link_info, 1500 /*check_null_and_abstract=*/true, CHECK); 1501 break; 1502 case Bytecodes::_invokeinterface: 1503 resolve_interface_call(result, recv, recv->klass(), link_info, 1504 /*check_null_and_abstract=*/true, CHECK); 1505 break; 1506 case Bytecodes::_invokestatic: 1507 resolve_static_call(result, link_info, /*initialize_class=*/false, CHECK); 1508 break; 1509 case Bytecodes::_invokespecial: 1510 resolve_special_call(result, link_info, CHECK); 1511 break; 1512 default: 1513 fatal("bad call: %s", Bytecodes::name(byte)); 1514 } 1515} 1516 1517void LinkResolver::resolve_invokestatic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1518 LinkInfo link_info(pool, index, CHECK); 1519 resolve_static_call(result, link_info, /*initialize_class*/true, CHECK); 1520} 1521 1522 1523void LinkResolver::resolve_invokespecial(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1524 LinkInfo link_info(pool, index, CHECK); 1525 resolve_special_call(result, link_info, CHECK); 1526} 1527 1528 1529void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv, 1530 const constantPoolHandle& pool, int index, 1531 TRAPS) { 1532 1533 LinkInfo link_info(pool, index, CHECK); 1534 KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); 1535 resolve_virtual_call(result, recv, recvrKlass, link_info, /*check_null_or_abstract*/true, CHECK); 1536} 1537 1538 1539void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, TRAPS) { 1540 LinkInfo link_info(pool, index, CHECK); 1541 KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass()); 1542 resolve_interface_call(result, recv, recvrKlass, link_info, true, CHECK); 1543} 1544 1545 1546void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1547 // This guy is reached from InterpreterRuntime::resolve_invokehandle. 1548 LinkInfo link_info(pool, index, CHECK); 1549 if (TraceMethodHandles) { 1550 ResourceMark rm(THREAD); 1551 tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(), 1552 link_info.signature()->as_C_string()); 1553 } 1554 resolve_handle_call(result, link_info, CHECK); 1555} 1556 1557void LinkResolver::resolve_handle_call(CallInfo& result, 1558 const LinkInfo& link_info, 1559 TRAPS) { 1560 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar 1561 assert(link_info.resolved_klass()() == SystemDictionary::MethodHandle_klass(), ""); 1562 assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), ""); 1563 Handle resolved_appendix; 1564 Handle resolved_method_type; 1565 methodHandle resolved_method = lookup_polymorphic_method(link_info, 1566 &resolved_appendix, &resolved_method_type, CHECK); 1567 result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK); 1568} 1569 1570static void wrap_invokedynamic_exception(TRAPS) { 1571 if (HAS_PENDING_EXCEPTION) { 1572 if (TraceMethodHandles) { 1573 tty->print_cr("invokedynamic throws BSME for " INTPTR_FORMAT, p2i((void *)PENDING_EXCEPTION)); 1574 PENDING_EXCEPTION->print(); 1575 } 1576 if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) { 1577 // throw these guys, since they are already wrapped 1578 return; 1579 } 1580 if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) { 1581 // intercept only LinkageErrors which might have failed to wrap 1582 return; 1583 } 1584 // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS. 1585 Handle nested_exception(THREAD, PENDING_EXCEPTION); 1586 CLEAR_PENDING_EXCEPTION; 1587 THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception) 1588 } 1589} 1590 1591void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) { 1592 Symbol* method_name = pool->name_ref_at(index); 1593 Symbol* method_signature = pool->signature_ref_at(index); 1594 KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder()); 1595 1596 // Resolve the bootstrap specifier (BSM + optional arguments). 1597 Handle bootstrap_specifier; 1598 // Check if CallSite has been bound already: 1599 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index); 1600 if (cpce->is_f1_null()) { 1601 int pool_index = cpce->constant_pool_index(); 1602 oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, THREAD); 1603 wrap_invokedynamic_exception(CHECK); 1604 assert(bsm_info != NULL, ""); 1605 // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic. 1606 bootstrap_specifier = Handle(THREAD, bsm_info); 1607 } 1608 if (!cpce->is_f1_null()) { 1609 methodHandle method( THREAD, cpce->f1_as_method()); 1610 Handle appendix( THREAD, cpce->appendix_if_resolved(pool)); 1611 Handle method_type(THREAD, cpce->method_type_if_resolved(pool)); 1612 result.set_handle(method, appendix, method_type, THREAD); 1613 wrap_invokedynamic_exception(CHECK); 1614 return; 1615 } 1616 1617 if (TraceMethodHandles) { 1618 ResourceMark rm(THREAD); 1619 tty->print_cr("resolve_invokedynamic #%d %s %s in %s", 1620 ConstantPool::decode_invokedynamic_index(index), 1621 method_name->as_C_string(), method_signature->as_C_string(), 1622 current_klass->name()->as_C_string()); 1623 tty->print(" BSM info: "); bootstrap_specifier->print(); 1624 } 1625 1626 resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK); 1627} 1628 1629void LinkResolver::resolve_dynamic_call(CallInfo& result, 1630 Handle bootstrap_specifier, 1631 Symbol* method_name, Symbol* method_signature, 1632 KlassHandle current_klass, 1633 TRAPS) { 1634 // JSR 292: this must resolve to an implicitly generated method MH.linkToCallSite(*...) 1635 // The appendix argument is likely to be a freshly-created CallSite. 1636 Handle resolved_appendix; 1637 Handle resolved_method_type; 1638 methodHandle resolved_method = 1639 SystemDictionary::find_dynamic_call_site_invoker(current_klass, 1640 bootstrap_specifier, 1641 method_name, method_signature, 1642 &resolved_appendix, 1643 &resolved_method_type, 1644 THREAD); 1645 wrap_invokedynamic_exception(CHECK); 1646 result.set_handle(resolved_method, resolved_appendix, resolved_method_type, THREAD); 1647 wrap_invokedynamic_exception(CHECK); 1648} 1649