annotate src/share/vm/interpreter/linkResolver.cpp @ 5489:8df6f123d35e

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