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