annotate src/hotspot/share/interpreter/linkResolver.cpp @ 51450:a73848f8d0ad

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