Tue, 19 Nov 2013 11:53:58 -0800
8028514: PPC64: Fix C++ Interpreter after '7195622: CheckUnhandledOops has limited usefulness now'
Summary: fix CPP-interpreter after CheckUnhandledOops was re-enabled in the fastdebug build
Reviewed-by: kvn, dholmes, lfoltan
1 /*
2 * Copyright (c) 1997, 2013, 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 miranda method. Compute the vtable index.
156 ResourceMark rm;
157 klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable();
158 index = vt->index_of_miranda(resolved_method->name(),
159 resolved_method->signature());
160 kind = CallInfo::vtable_call;
161 } else if (resolved_method->has_vtable_index()) {
162 // Can occur if an interface redeclares a method of Object.
164 #ifdef ASSERT
165 // Ensure that this is really the case.
166 KlassHandle object_klass = SystemDictionary::Object_klass();
167 Method * object_resolved_method = object_klass()->vtable()->method_at(index);
168 assert(object_resolved_method->name() == resolved_method->name(),
169 err_msg("Object and interface method names should match at vtable index %d, %s != %s",
170 index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string()));
171 assert(object_resolved_method->signature() == resolved_method->signature(),
172 err_msg("Object and interface method signatures should match at vtable index %d, %s != %s",
173 index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string()));
174 #endif // ASSERT
176 kind = CallInfo::vtable_call;
177 } else {
178 // A regular interface call.
179 kind = CallInfo::itable_call;
180 index = resolved_method->itable_index();
181 }
182 assert(index == Method::nonvirtual_vtable_index || index >= 0, err_msg("bad index %d", index));
183 _call_kind = kind;
184 _call_index = index;
185 _resolved_appendix = Handle();
186 DEBUG_ONLY(verify());
187 }
189 #ifdef ASSERT
190 void CallInfo::verify() {
191 switch (call_kind()) { // the meaning and allowed value of index depends on kind
192 case CallInfo::direct_call:
193 if (_call_index == Method::nonvirtual_vtable_index) break;
194 // else fall through to check vtable index:
195 case CallInfo::vtable_call:
196 assert(resolved_klass()->verify_vtable_index(_call_index), "");
197 break;
198 case CallInfo::itable_call:
199 assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
200 break;
201 case CallInfo::unknown_kind:
202 assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
203 break;
204 default:
205 fatal(err_msg_res("Unexpected call kind %d", call_kind()));
206 }
207 }
208 #endif //ASSERT
212 //------------------------------------------------------------------------------------------------------------------------
213 // Klass resolution
215 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) {
216 if (!Reflection::verify_class_access(ref_klass(),
217 sel_klass(),
218 true)) {
219 ResourceMark rm(THREAD);
220 Exceptions::fthrow(
221 THREAD_AND_LOCATION,
222 vmSymbols::java_lang_IllegalAccessError(),
223 "tried to access class %s from class %s",
224 sel_klass->external_name(),
225 ref_klass->external_name()
226 );
227 return;
228 }
229 }
231 void LinkResolver::resolve_klass(KlassHandle& result, constantPoolHandle pool, int index, TRAPS) {
232 Klass* result_oop = pool->klass_ref_at(index, CHECK);
233 result = KlassHandle(THREAD, result_oop);
234 }
236 //------------------------------------------------------------------------------------------------------------------------
237 // Method resolution
238 //
239 // According to JVM spec. $5.4.3c & $5.4.3d
241 // Look up method in klasses, including static methods
242 // Then look up local default methods
243 void LinkResolver::lookup_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
244 Method* result_oop = klass->uncached_lookup_method(name, signature);
245 if (result_oop == NULL) {
246 Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
247 if (default_methods != NULL) {
248 result_oop = InstanceKlass::find_method(default_methods, name, signature);
249 }
250 }
252 if (EnableInvokeDynamic && result_oop != NULL) {
253 vmIntrinsics::ID iid = result_oop->intrinsic_id();
254 if (MethodHandles::is_signature_polymorphic(iid)) {
255 // Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.
256 return;
257 }
258 }
259 result = methodHandle(THREAD, result_oop);
260 }
262 // returns first instance method
263 // Looks up method in classes, then looks up local default methods
264 void LinkResolver::lookup_instance_method_in_klasses(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
265 Method* result_oop = klass->uncached_lookup_method(name, signature);
266 result = methodHandle(THREAD, result_oop);
267 while (!result.is_null() && result->is_static() && result->method_holder()->super() != NULL) {
268 klass = KlassHandle(THREAD, result->method_holder()->super());
269 result = methodHandle(THREAD, klass->uncached_lookup_method(name, signature));
270 }
272 if (result.is_null()) {
273 Array<Method*>* default_methods = InstanceKlass::cast(klass())->default_methods();
274 if (default_methods != NULL) {
275 result = methodHandle(InstanceKlass::find_method(default_methods, name, signature));
276 assert(result.is_null() || !result->is_static(), "static defaults not allowed");
277 }
278 }
279 }
281 int LinkResolver::vtable_index_of_interface_method(KlassHandle klass,
282 methodHandle resolved_method, TRAPS) {
284 int vtable_index = Method::invalid_vtable_index;
285 Symbol* name = resolved_method->name();
286 Symbol* signature = resolved_method->signature();
288 // First check in default method array
289 if (!resolved_method->is_abstract() &&
290 (InstanceKlass::cast(klass())->default_methods() != NULL)) {
291 int index = InstanceKlass::find_method_index(InstanceKlass::cast(klass())->default_methods(), name, signature);
292 if (index >= 0 ) {
293 vtable_index = InstanceKlass::cast(klass())->default_vtable_indices()->at(index);
294 }
295 }
296 if (vtable_index == Method::invalid_vtable_index) {
297 // get vtable_index for miranda methods
298 ResourceMark rm(THREAD);
299 klassVtable *vt = InstanceKlass::cast(klass())->vtable();
300 vtable_index = vt->index_of_miranda(name, signature);
301 }
302 return vtable_index;
303 }
305 void LinkResolver::lookup_method_in_interfaces(methodHandle& result, KlassHandle klass, Symbol* name, Symbol* signature, TRAPS) {
306 InstanceKlass *ik = InstanceKlass::cast(klass());
307 result = methodHandle(THREAD, ik->lookup_method_in_all_interfaces(name, signature));
308 }
310 void LinkResolver::lookup_polymorphic_method(methodHandle& result,
311 KlassHandle klass, Symbol* name, Symbol* full_signature,
312 KlassHandle current_klass,
313 Handle *appendix_result_or_null,
314 Handle *method_type_result,
315 TRAPS) {
316 vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
317 if (TraceMethodHandles) {
318 ResourceMark rm(THREAD);
319 tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
320 vmIntrinsics::name_at(iid), klass->external_name(),
321 name->as_C_string(), full_signature->as_C_string());
322 }
323 if (EnableInvokeDynamic &&
324 klass() == SystemDictionary::MethodHandle_klass() &&
325 iid != vmIntrinsics::_none) {
326 if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
327 // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
328 // Do not erase last argument type (MemberName) if it is a static linkTo method.
329 bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
330 TempNewSymbol basic_signature =
331 MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK);
332 if (TraceMethodHandles) {
333 ResourceMark rm(THREAD);
334 tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
335 name->as_C_string(),
336 full_signature->as_C_string(),
337 basic_signature->as_C_string());
338 }
339 result = SystemDictionary::find_method_handle_intrinsic(iid,
340 basic_signature,
341 CHECK);
342 if (result.not_null()) {
343 assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
344 assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
345 assert(basic_signature == result->signature(), "predict the result signature");
346 if (TraceMethodHandles) {
347 tty->print("lookup_polymorphic_method => intrinsic ");
348 result->print_on(tty);
349 }
350 return;
351 }
352 } else if (iid == vmIntrinsics::_invokeGeneric
353 && !THREAD->is_Compiler_thread()
354 && appendix_result_or_null != NULL) {
355 // This is a method with type-checking semantics.
356 // We will ask Java code to spin an adapter method for it.
357 if (!MethodHandles::enabled()) {
358 // Make sure the Java part of the runtime has been booted up.
359 Klass* natives = SystemDictionary::MethodHandleNatives_klass();
360 if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
361 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
362 Handle(),
363 Handle(),
364 true,
365 CHECK);
366 }
367 }
369 Handle appendix;
370 Handle method_type;
371 result = SystemDictionary::find_method_handle_invoker(name,
372 full_signature,
373 current_klass,
374 &appendix,
375 &method_type,
376 CHECK);
377 if (TraceMethodHandles) {
378 tty->print("lookup_polymorphic_method => (via Java) ");
379 result->print_on(tty);
380 tty->print(" lookup_polymorphic_method => appendix = ");
381 if (appendix.is_null()) tty->print_cr("(none)");
382 else appendix->print_on(tty);
383 }
384 if (result.not_null()) {
385 #ifdef ASSERT
386 ResourceMark rm(THREAD);
388 TempNewSymbol basic_signature =
389 MethodHandles::lookup_basic_type_signature(full_signature, CHECK);
390 int actual_size_of_params = result->size_of_parameters();
391 int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
392 // +1 for MethodHandle.this, +1 for trailing MethodType
393 if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
394 if (appendix.not_null()) expected_size_of_params += 1;
395 if (actual_size_of_params != expected_size_of_params) {
396 tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
397 tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
398 result->print();
399 }
400 assert(actual_size_of_params == expected_size_of_params,
401 err_msg("%d != %d", actual_size_of_params, expected_size_of_params));
402 #endif //ASSERT
404 assert(appendix_result_or_null != NULL, "");
405 (*appendix_result_or_null) = appendix;
406 (*method_type_result) = method_type;
407 return;
408 }
409 }
410 }
411 }
413 void LinkResolver::check_method_accessability(KlassHandle ref_klass,
414 KlassHandle resolved_klass,
415 KlassHandle sel_klass,
416 methodHandle sel_method,
417 TRAPS) {
419 AccessFlags flags = sel_method->access_flags();
421 // Special case #1: arrays always override "clone". JVMS 2.15.
422 // If the resolved klass is an array class, and the declaring class
423 // is java.lang.Object and the method is "clone", set the flags
424 // to public.
425 // Special case #2: If the resolved klass is an interface, and
426 // the declaring class is java.lang.Object, and the method is
427 // "clone" or "finalize", set the flags to public. If the
428 // resolved interface does not contain "clone" or "finalize"
429 // methods, the method/interface method resolution looks to
430 // the interface's super class, java.lang.Object. With JDK 8
431 // interface accessability check requirement, special casing
432 // this scenario is necessary to avoid an IAE.
433 //
434 // We'll check for each method name first and then java.lang.Object
435 // to best short-circuit out of these tests.
436 if (((sel_method->name() == vmSymbols::clone_name() &&
437 (resolved_klass->oop_is_array() || resolved_klass->is_interface())) ||
438 (sel_method->name() == vmSymbols::finalize_method_name() &&
439 resolved_klass->is_interface())) &&
440 sel_klass() == SystemDictionary::Object_klass()) {
441 // We need to change "protected" to "public".
442 assert(flags.is_protected(), "clone or finalize not protected?");
443 jint new_flags = flags.as_int();
444 new_flags = new_flags & (~JVM_ACC_PROTECTED);
445 new_flags = new_flags | JVM_ACC_PUBLIC;
446 flags.set_flags(new_flags);
447 }
448 // assert(extra_arg_result_or_null != NULL, "must be able to return extra argument");
450 if (!Reflection::verify_field_access(ref_klass(),
451 resolved_klass(),
452 sel_klass(),
453 flags,
454 true)) {
455 ResourceMark rm(THREAD);
456 Exceptions::fthrow(
457 THREAD_AND_LOCATION,
458 vmSymbols::java_lang_IllegalAccessError(),
459 "tried to access method %s.%s%s from class %s",
460 sel_klass->external_name(),
461 sel_method->name()->as_C_string(),
462 sel_method->signature()->as_C_string(),
463 ref_klass->external_name()
464 );
465 return;
466 }
467 }
469 void LinkResolver::resolve_method_statically(methodHandle& resolved_method, KlassHandle& resolved_klass,
470 Bytecodes::Code code, constantPoolHandle pool, int index, TRAPS) {
471 // This method is used only
472 // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
473 // and
474 // (2) in Bytecode_invoke::static_target
475 // It appears to fail when applied to an invokeinterface call site.
476 // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
477 // resolve klass
478 if (code == Bytecodes::_invokedynamic) {
479 resolved_klass = SystemDictionary::MethodHandle_klass();
480 Symbol* method_name = vmSymbols::invoke_name();
481 Symbol* method_signature = pool->signature_ref_at(index);
482 KlassHandle current_klass(THREAD, pool->pool_holder());
483 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
484 return;
485 }
487 resolve_klass(resolved_klass, pool, index, CHECK);
489 Symbol* method_name = pool->name_ref_at(index);
490 Symbol* method_signature = pool->signature_ref_at(index);
491 KlassHandle current_klass(THREAD, pool->pool_holder());
493 if (pool->has_preresolution()
494 || (resolved_klass() == SystemDictionary::MethodHandle_klass() &&
495 MethodHandles::is_signature_polymorphic_name(resolved_klass(), method_name))) {
496 Method* result_oop = ConstantPool::method_at_if_loaded(pool, index);
497 if (result_oop != NULL) {
498 resolved_method = methodHandle(THREAD, result_oop);
499 return;
500 }
501 }
503 if (code == Bytecodes::_invokeinterface) {
504 resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
505 } else if (code == Bytecodes::_invokevirtual) {
506 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
507 } else {
508 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, true, false, CHECK);
509 }
510 }
512 void LinkResolver::resolve_method(methodHandle& resolved_method, KlassHandle resolved_klass,
513 Symbol* method_name, Symbol* method_signature,
514 KlassHandle current_klass, bool check_access,
515 bool require_methodref, TRAPS) {
517 Handle nested_exception;
519 // 1. check if methodref required, that resolved_klass is not interfacemethodref
520 if (require_methodref && resolved_klass->is_interface()) {
521 ResourceMark rm(THREAD);
522 char buf[200];
523 jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
524 resolved_klass()->external_name());
525 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
526 }
528 // 2. lookup method in resolved klass and its super klasses
529 lookup_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
531 if (resolved_method.is_null()) { // not found in the class hierarchy
532 // 3. lookup method in all the interfaces implemented by the resolved klass
533 lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
535 if (resolved_method.is_null()) {
536 // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
537 lookup_polymorphic_method(resolved_method, resolved_klass, method_name, method_signature,
538 current_klass, (Handle*)NULL, (Handle*)NULL, THREAD);
539 if (HAS_PENDING_EXCEPTION) {
540 nested_exception = Handle(THREAD, PENDING_EXCEPTION);
541 CLEAR_PENDING_EXCEPTION;
542 }
543 }
545 if (resolved_method.is_null()) {
546 // 4. method lookup failed
547 ResourceMark rm(THREAD);
548 THROW_MSG_CAUSE(vmSymbols::java_lang_NoSuchMethodError(),
549 Method::name_and_sig_as_C_string(resolved_klass(),
550 method_name,
551 method_signature),
552 nested_exception);
553 }
554 }
556 // 5. check if method is concrete
557 if (resolved_method->is_abstract() && !resolved_klass->is_abstract()) {
558 ResourceMark rm(THREAD);
559 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
560 Method::name_and_sig_as_C_string(resolved_klass(),
561 method_name,
562 method_signature));
563 }
565 // 6. access checks, access checking may be turned off when calling from within the VM.
566 if (check_access) {
567 assert(current_klass.not_null() , "current_klass should not be null");
569 // check if method can be accessed by the referring class
570 check_method_accessability(current_klass,
571 resolved_klass,
572 KlassHandle(THREAD, resolved_method->method_holder()),
573 resolved_method,
574 CHECK);
576 // check loader constraints
577 Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
578 Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());
579 {
580 ResourceMark rm(THREAD);
581 Symbol* failed_type_symbol =
582 SystemDictionary::check_signature_loaders(method_signature, loader,
583 class_loader, true, CHECK);
584 if (failed_type_symbol != NULL) {
585 const char* msg = "loader constraint violation: when resolving method"
586 " \"%s\" the class loader (instance of %s) of the current class, %s,"
587 " and the class loader (instance of %s) for the method's defining class, %s, have"
588 " different Class objects for the type %s used in the signature";
589 char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature);
590 const char* loader1 = SystemDictionary::loader_name(loader());
591 char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();
592 const char* loader2 = SystemDictionary::loader_name(class_loader());
593 char* target = InstanceKlass::cast(resolved_method->method_holder())
594 ->name()->as_C_string();
595 char* failed_type_name = failed_type_symbol->as_C_string();
596 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
597 strlen(current) + strlen(loader2) + strlen(target) +
598 strlen(failed_type_name) + 1;
599 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
600 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
601 target, failed_type_name);
602 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
603 }
604 }
605 }
606 }
608 void LinkResolver::resolve_interface_method(methodHandle& resolved_method,
609 KlassHandle resolved_klass,
610 Symbol* method_name,
611 Symbol* method_signature,
612 KlassHandle current_klass,
613 bool check_access, TRAPS) {
615 // check if klass is interface
616 if (!resolved_klass->is_interface()) {
617 ResourceMark rm(THREAD);
618 char buf[200];
619 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass()->external_name());
620 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
621 }
623 // lookup method in this interface or its super, java.lang.Object
624 lookup_instance_method_in_klasses(resolved_method, resolved_klass, method_name, method_signature, CHECK);
626 if (resolved_method.is_null()) {
627 // lookup method in all the super-interfaces
628 lookup_method_in_interfaces(resolved_method, resolved_klass, method_name, method_signature, CHECK);
629 if (resolved_method.is_null()) {
630 // no method found
631 ResourceMark rm(THREAD);
632 THROW_MSG(vmSymbols::java_lang_NoSuchMethodError(),
633 Method::name_and_sig_as_C_string(resolved_klass(),
634 method_name,
635 method_signature));
636 }
637 }
639 if (check_access) {
640 // JDK8 adds non-public interface methods, and accessability check requirement
641 assert(current_klass.not_null() , "current_klass should not be null");
643 // check if method can be accessed by the referring class
644 check_method_accessability(current_klass,
645 resolved_klass,
646 KlassHandle(THREAD, resolved_method->method_holder()),
647 resolved_method,
648 CHECK);
650 HandleMark hm(THREAD);
651 Handle loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
652 Handle class_loader (THREAD, resolved_method->method_holder()->class_loader());
653 {
654 ResourceMark rm(THREAD);
655 Symbol* failed_type_symbol =
656 SystemDictionary::check_signature_loaders(method_signature, loader,
657 class_loader, true, CHECK);
658 if (failed_type_symbol != NULL) {
659 const char* msg = "loader constraint violation: when resolving "
660 "interface method \"%s\" the class loader (instance of %s) of the "
661 "current class, %s, and the class loader (instance of %s) for "
662 "the method's defining class, %s, have different Class objects for the type %s "
663 "used in the signature";
664 char* sig = Method::name_and_sig_as_C_string(resolved_klass(),method_name,method_signature);
665 const char* loader1 = SystemDictionary::loader_name(loader());
666 char* current = InstanceKlass::cast(current_klass())->name()->as_C_string();
667 const char* loader2 = SystemDictionary::loader_name(class_loader());
668 char* target = InstanceKlass::cast(resolved_method->method_holder())
669 ->name()->as_C_string();
670 char* failed_type_name = failed_type_symbol->as_C_string();
671 size_t buflen = strlen(msg) + strlen(sig) + strlen(loader1) +
672 strlen(current) + strlen(loader2) + strlen(target) +
673 strlen(failed_type_name) + 1;
674 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
675 jio_snprintf(buf, buflen, msg, sig, loader1, current, loader2,
676 target, failed_type_name);
677 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
678 }
679 }
680 }
682 if (TraceItables && Verbose) {
683 ResourceMark rm(THREAD);
684 tty->print("invokeinterface resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
685 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
686 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
687 Method::name_and_sig_as_C_string(resolved_klass(),
688 resolved_method->name(),
689 resolved_method->signature()),
690 resolved_method->method_holder()->internal_name()
691 );
692 resolved_method->access_flags().print_on(tty);
693 if (resolved_method->is_default_method()) {
694 tty->print("default");
695 }
696 if (resolved_method->is_overpass()) {
697 tty->print("overpass");
698 }
699 tty->cr();
700 }
701 }
703 //------------------------------------------------------------------------------------------------------------------------
704 // Field resolution
706 void LinkResolver::check_field_accessability(KlassHandle ref_klass,
707 KlassHandle resolved_klass,
708 KlassHandle sel_klass,
709 fieldDescriptor& fd,
710 TRAPS) {
711 if (!Reflection::verify_field_access(ref_klass(),
712 resolved_klass(),
713 sel_klass(),
714 fd.access_flags(),
715 true)) {
716 ResourceMark rm(THREAD);
717 Exceptions::fthrow(
718 THREAD_AND_LOCATION,
719 vmSymbols::java_lang_IllegalAccessError(),
720 "tried to access field %s.%s from class %s",
721 sel_klass->external_name(),
722 fd.name()->as_C_string(),
723 ref_klass->external_name()
724 );
725 return;
726 }
727 }
729 void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
730 // Load these early in case the resolve of the containing klass fails
731 Symbol* field = pool->name_ref_at(index);
732 Symbol* sig = pool->signature_ref_at(index);
734 // resolve specified klass
735 KlassHandle resolved_klass;
736 resolve_klass(resolved_klass, pool, index, CHECK);
738 KlassHandle current_klass(THREAD, pool->pool_holder());
739 resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK);
740 }
742 void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig,
743 KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class,
744 TRAPS) {
745 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic ||
746 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield ||
747 (byte == Bytecodes::_nop && !check_access), "bad field access bytecode");
749 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic);
750 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic);
752 // Check if there's a resolved klass containing the field
753 if (resolved_klass.is_null()) {
754 ResourceMark rm(THREAD);
755 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
756 }
758 // Resolve instance field
759 KlassHandle sel_klass(THREAD, InstanceKlass::cast(resolved_klass())->find_field(field, sig, &fd));
760 // check if field exists; i.e., if a klass containing the field def has been selected
761 if (sel_klass.is_null()) {
762 ResourceMark rm(THREAD);
763 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string());
764 }
766 if (!check_access)
767 // Access checking may be turned off when calling from within the VM.
768 return;
770 // check access
771 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK);
773 // check for errors
774 if (is_static != fd.is_static()) {
775 ResourceMark rm(THREAD);
776 char msg[200];
777 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string());
778 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
779 }
781 // Final fields can only be accessed from its own class.
782 if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) {
783 THROW(vmSymbols::java_lang_IllegalAccessError());
784 }
786 // initialize resolved_klass if necessary
787 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
788 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
789 //
790 // note 2: we don't want to force initialization if we are just checking
791 // if the field access is legal; e.g., during compilation
792 if (is_static && initialize_class) {
793 sel_klass->initialize(CHECK);
794 }
796 if (sel_klass() != current_klass()) {
797 HandleMark hm(THREAD);
798 Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader());
799 Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader());
800 {
801 ResourceMark rm(THREAD);
802 Symbol* failed_type_symbol =
803 SystemDictionary::check_signature_loaders(sig,
804 ref_loader, sel_loader,
805 false,
806 CHECK);
807 if (failed_type_symbol != NULL) {
808 const char* msg = "loader constraint violation: when resolving field"
809 " \"%s\" the class loader (instance of %s) of the referring class, "
810 "%s, and the class loader (instance of %s) for the field's resolved "
811 "type, %s, have different Class objects for that type";
812 char* field_name = field->as_C_string();
813 const char* loader1 = SystemDictionary::loader_name(ref_loader());
814 char* sel = InstanceKlass::cast(sel_klass())->name()->as_C_string();
815 const char* loader2 = SystemDictionary::loader_name(sel_loader());
816 char* failed_type_name = failed_type_symbol->as_C_string();
817 size_t buflen = strlen(msg) + strlen(field_name) + strlen(loader1) +
818 strlen(sel) + strlen(loader2) + strlen(failed_type_name) + 1;
819 char* buf = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, buflen);
820 jio_snprintf(buf, buflen, msg, field_name, loader1, sel, loader2,
821 failed_type_name);
822 THROW_MSG(vmSymbols::java_lang_LinkageError(), buf);
823 }
824 }
825 }
827 // return information. note that the klass is set to the actual klass containing the
828 // field, otherwise access of static fields in superclasses will not work.
829 }
832 //------------------------------------------------------------------------------------------------------------------------
833 // Invoke resolution
834 //
835 // Naming conventions:
836 //
837 // resolved_method the specified method (i.e., static receiver specified via constant pool index)
838 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)
839 // resolved_klass the specified klass (i.e., specified via constant pool index)
840 // recv_klass the receiver klass
843 void LinkResolver::resolve_static_call(CallInfo& result, KlassHandle& resolved_klass, Symbol* method_name,
844 Symbol* method_signature, KlassHandle current_klass,
845 bool check_access, bool initialize_class, TRAPS) {
846 methodHandle resolved_method;
847 linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
848 resolved_klass = KlassHandle(THREAD, resolved_method->method_holder());
850 // Initialize klass (this should only happen if everything is ok)
851 if (initialize_class && resolved_klass->should_be_initialized()) {
852 resolved_klass->initialize(CHECK);
853 linktime_resolve_static_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
854 }
856 // setup result
857 result.set_static(resolved_klass, resolved_method, CHECK);
858 }
860 // throws linktime exceptions
861 void LinkResolver::linktime_resolve_static_method(methodHandle& resolved_method, KlassHandle resolved_klass,
862 Symbol* method_name, Symbol* method_signature,
863 KlassHandle current_klass, bool check_access, TRAPS) {
865 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
866 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
868 // check if static
869 if (!resolved_method->is_static()) {
870 ResourceMark rm(THREAD);
871 char buf[200];
872 jio_snprintf(buf, sizeof(buf), "Expected static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
873 resolved_method->name(),
874 resolved_method->signature()));
875 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
876 }
877 }
880 void LinkResolver::resolve_special_call(CallInfo& result, KlassHandle resolved_klass, Symbol* method_name,
881 Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
882 methodHandle resolved_method;
883 linktime_resolve_special_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
884 runtime_resolve_special_method(result, resolved_method, resolved_klass, current_klass, check_access, CHECK);
885 }
887 // throws linktime exceptions
888 void LinkResolver::linktime_resolve_special_method(methodHandle& resolved_method, KlassHandle resolved_klass,
889 Symbol* method_name, Symbol* method_signature,
890 KlassHandle current_klass, bool check_access, TRAPS) {
892 // Invokespecial is called for multiple special reasons:
893 // <init>
894 // local private method invocation, for classes and interfaces
895 // superclass.method, which can also resolve to a default method
896 // and the selected method is recalculated relative to the direct superclass
897 // superinterface.method, which explicitly does not check shadowing
899 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, false, CHECK);
901 // check if method name is <init>, that it is found in same klass as static type
902 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
903 resolved_method->method_holder() != resolved_klass()) {
904 ResourceMark rm(THREAD);
905 Exceptions::fthrow(
906 THREAD_AND_LOCATION,
907 vmSymbols::java_lang_NoSuchMethodError(),
908 "%s: method %s%s not found",
909 resolved_klass->external_name(),
910 resolved_method->name()->as_C_string(),
911 resolved_method->signature()->as_C_string()
912 );
913 return;
914 }
916 // check if not static
917 if (resolved_method->is_static()) {
918 ResourceMark rm(THREAD);
919 char buf[200];
920 jio_snprintf(buf, sizeof(buf),
921 "Expecting non-static method %s",
922 Method::name_and_sig_as_C_string(resolved_klass(),
923 resolved_method->name(),
924 resolved_method->signature()));
925 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
926 }
928 if (TraceItables && Verbose) {
929 ResourceMark rm(THREAD);
930 tty->print("invokespecial resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
931 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
932 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
933 Method::name_and_sig_as_C_string(resolved_klass(),
934 resolved_method->name(),
935 resolved_method->signature()),
936 resolved_method->method_holder()->internal_name()
937 );
938 resolved_method->access_flags().print_on(tty);
939 if (resolved_method->is_default_method()) {
940 tty->print("default");
941 }
942 if (resolved_method->is_overpass()) {
943 tty->print("overpass");
944 }
945 tty->cr();
946 }
947 }
949 // throws runtime exceptions
950 void LinkResolver::runtime_resolve_special_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
951 KlassHandle current_klass, bool check_access, TRAPS) {
953 // resolved method is selected method unless we have an old-style lookup
954 // for a superclass method
955 // Invokespecial for a superinterface, resolved method is selected method,
956 // no checks for shadowing
957 methodHandle sel_method(THREAD, resolved_method());
959 // check if this is an old-style super call and do a new lookup if so
960 { KlassHandle method_klass = KlassHandle(THREAD,
961 resolved_method->method_holder());
963 if (check_access &&
964 // a) check if ACC_SUPER flag is set for the current class
965 (current_klass->is_super() || !AllowNonVirtualCalls) &&
966 // b) check if the class of the resolved_klass is a superclass
967 // (not supertype in order to exclude interface classes) of the current class.
968 // This check is not performed for super.invoke for interface methods
969 // in super interfaces.
970 current_klass->is_subclass_of(resolved_klass()) &&
971 current_klass() != resolved_klass() &&
972 // c) check if the method is not <init>
973 resolved_method->name() != vmSymbols::object_initializer_name()) {
974 // Lookup super method
975 KlassHandle super_klass(THREAD, current_klass->super());
976 lookup_instance_method_in_klasses(sel_method, super_klass,
977 resolved_method->name(),
978 resolved_method->signature(), CHECK);
979 // check if found
980 if (sel_method.is_null()) {
981 ResourceMark rm(THREAD);
982 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
983 Method::name_and_sig_as_C_string(resolved_klass(),
984 resolved_method->name(),
985 resolved_method->signature()));
986 }
987 }
988 }
990 // check if not static
991 if (sel_method->is_static()) {
992 ResourceMark rm(THREAD);
993 char buf[200];
994 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
995 resolved_method->name(),
996 resolved_method->signature()));
997 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
998 }
1000 // check if abstract
1001 if (sel_method->is_abstract()) {
1002 ResourceMark rm(THREAD);
1003 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1004 Method::name_and_sig_as_C_string(resolved_klass(),
1005 sel_method->name(),
1006 sel_method->signature()));
1007 }
1009 if (TraceItables && Verbose) {
1010 ResourceMark rm(THREAD);
1011 tty->print("invokespecial selected method: resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
1012 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1013 Method::name_and_sig_as_C_string(resolved_klass(),
1014 sel_method->name(),
1015 sel_method->signature()),
1016 sel_method->method_holder()->internal_name()
1017 );
1018 sel_method->access_flags().print_on(tty);
1019 if (sel_method->is_default_method()) {
1020 tty->print("default");
1021 }
1022 if (sel_method->is_overpass()) {
1023 tty->print("overpass");
1024 }
1025 tty->cr();
1026 }
1028 // setup result
1029 result.set_static(resolved_klass, sel_method, CHECK);
1030 }
1032 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, KlassHandle receiver_klass, KlassHandle resolved_klass,
1033 Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
1034 bool check_access, bool check_null_and_abstract, TRAPS) {
1035 methodHandle resolved_method;
1036 linktime_resolve_virtual_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1037 runtime_resolve_virtual_method(result, resolved_method, resolved_klass, recv, receiver_klass, check_null_and_abstract, CHECK);
1038 }
1040 // throws linktime exceptions
1041 void LinkResolver::linktime_resolve_virtual_method(methodHandle &resolved_method, KlassHandle resolved_klass,
1042 Symbol* method_name, Symbol* method_signature,
1043 KlassHandle current_klass, bool check_access, TRAPS) {
1044 // normal method resolution
1045 resolve_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, true, CHECK);
1047 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1048 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1050 // check if private interface method
1051 if (resolved_klass->is_interface() && resolved_method->is_private()) {
1052 ResourceMark rm(THREAD);
1053 char buf[200];
1054 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokevirtual: method %s, caller-class:%s",
1055 Method::name_and_sig_as_C_string(resolved_klass(),
1056 resolved_method->name(),
1057 resolved_method->signature()),
1058 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()));
1059 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1060 }
1062 // check if not static
1063 if (resolved_method->is_static()) {
1064 ResourceMark rm(THREAD);
1065 char buf[200];
1066 jio_snprintf(buf, sizeof(buf), "Expecting non-static method %s", Method::name_and_sig_as_C_string(resolved_klass(),
1067 resolved_method->name(),
1068 resolved_method->signature()));
1069 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1070 }
1072 if (PrintVtables && Verbose) {
1073 ResourceMark rm(THREAD);
1074 tty->print("invokevirtual resolved method: caller-class:%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
1075 (current_klass.is_null() ? "<NULL>" : current_klass->internal_name()),
1076 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1077 Method::name_and_sig_as_C_string(resolved_klass(),
1078 resolved_method->name(),
1079 resolved_method->signature()),
1080 resolved_method->method_holder()->internal_name()
1081 );
1082 resolved_method->access_flags().print_on(tty);
1083 if (resolved_method->is_default_method()) {
1084 tty->print("default");
1085 }
1086 if (resolved_method->is_overpass()) {
1087 tty->print("overpass");
1088 }
1089 tty->cr();
1090 }
1091 }
1093 // throws runtime exceptions
1094 void LinkResolver::runtime_resolve_virtual_method(CallInfo& result,
1095 methodHandle resolved_method,
1096 KlassHandle resolved_klass,
1097 Handle recv,
1098 KlassHandle recv_klass,
1099 bool check_null_and_abstract,
1100 TRAPS) {
1102 // setup default return values
1103 int vtable_index = Method::invalid_vtable_index;
1104 methodHandle selected_method;
1106 assert(recv.is_null() || recv->is_oop(), "receiver is not an oop");
1108 // runtime method resolution
1109 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists
1110 THROW(vmSymbols::java_lang_NullPointerException());
1111 }
1113 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s
1114 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since
1115 // a missing receiver might result in a bogus lookup.
1116 assert(resolved_method->method_holder()->is_linked(), "must be linked");
1118 // do lookup based on receiver klass using the vtable index
1119 if (resolved_method->method_holder()->is_interface()) { // miranda method
1120 vtable_index = vtable_index_of_interface_method(resolved_klass,
1121 resolved_method, CHECK);
1122 assert(vtable_index >= 0 , "we should have valid vtable index at this point");
1124 InstanceKlass* inst = InstanceKlass::cast(recv_klass());
1125 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
1126 } else {
1127 // at this point we are sure that resolved_method is virtual and not
1128 // a miranda method; therefore, it must have a valid vtable index.
1129 assert(!resolved_method->has_itable_index(), "");
1130 vtable_index = resolved_method->vtable_index();
1131 // We could get a negative vtable_index for final methods,
1132 // because as an optimization they are they are never put in the vtable,
1133 // unless they override an existing method.
1134 // If we do get a negative, it means the resolved method is the the selected
1135 // method, and it can never be changed by an override.
1136 if (vtable_index == Method::nonvirtual_vtable_index) {
1137 assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1138 selected_method = resolved_method;
1139 } else {
1140 // recv_klass might be an arrayKlassOop but all vtables start at
1141 // the same place. The cast is to avoid virtual call and assertion.
1142 InstanceKlass* inst = (InstanceKlass*)recv_klass();
1143 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index));
1144 }
1145 }
1147 // check if method exists
1148 if (selected_method.is_null()) {
1149 ResourceMark rm(THREAD);
1150 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1151 Method::name_and_sig_as_C_string(resolved_klass(),
1152 resolved_method->name(),
1153 resolved_method->signature()));
1154 }
1156 // check if abstract
1157 if (check_null_and_abstract && selected_method->is_abstract()) {
1158 ResourceMark rm(THREAD);
1159 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1160 Method::name_and_sig_as_C_string(resolved_klass(),
1161 selected_method->name(),
1162 selected_method->signature()));
1163 }
1165 if (PrintVtables && Verbose) {
1166 ResourceMark rm(THREAD);
1167 tty->print("invokevirtual selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, vtable_index:%d, access_flags: ",
1168 (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
1169 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1170 Method::name_and_sig_as_C_string(resolved_klass(),
1171 resolved_method->name(),
1172 resolved_method->signature()),
1173 selected_method->method_holder()->internal_name(),
1174 vtable_index
1175 );
1176 selected_method->access_flags().print_on(tty);
1177 if (selected_method->is_default_method()) {
1178 tty->print("default");
1179 }
1180 if (selected_method->is_overpass()) {
1181 tty->print("overpass");
1182 }
1183 tty->cr();
1184 }
1185 // setup result
1186 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1187 }
1189 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, KlassHandle recv_klass, KlassHandle resolved_klass,
1190 Symbol* method_name, Symbol* method_signature, KlassHandle current_klass,
1191 bool check_access, bool check_null_and_abstract, TRAPS) {
1192 methodHandle resolved_method;
1193 linktime_resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1194 runtime_resolve_interface_method(result, resolved_method, resolved_klass, recv, recv_klass, check_null_and_abstract, CHECK);
1195 }
1197 // throws linktime exceptions
1198 void LinkResolver::linktime_resolve_interface_method(methodHandle& resolved_method, KlassHandle resolved_klass, Symbol* method_name,
1199 Symbol* method_signature, KlassHandle current_klass, bool check_access, TRAPS) {
1200 // normal interface method resolution
1201 resolve_interface_method(resolved_method, resolved_klass, method_name, method_signature, current_klass, check_access, CHECK);
1203 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1204 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1205 }
1207 // throws runtime exceptions
1208 void LinkResolver::runtime_resolve_interface_method(CallInfo& result, methodHandle resolved_method, KlassHandle resolved_klass,
1209 Handle recv, KlassHandle recv_klass, bool check_null_and_abstract, TRAPS) {
1210 // check if receiver exists
1211 if (check_null_and_abstract && recv.is_null()) {
1212 THROW(vmSymbols::java_lang_NullPointerException());
1213 }
1215 // check if private interface method
1216 if (resolved_klass->is_interface() && resolved_method->is_private()) {
1217 ResourceMark rm(THREAD);
1218 char buf[200];
1219 jio_snprintf(buf, sizeof(buf), "private interface method requires invokespecial, not invokeinterface: method %s",
1220 Method::name_and_sig_as_C_string(resolved_klass(),
1221 resolved_method->name(),
1222 resolved_method->signature()));
1223 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1224 }
1226 // check if receiver klass implements the resolved interface
1227 if (!recv_klass->is_subtype_of(resolved_klass())) {
1228 ResourceMark rm(THREAD);
1229 char buf[200];
1230 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1231 recv_klass()->external_name(),
1232 resolved_klass()->external_name());
1233 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1234 }
1235 // do lookup based on receiver klass
1236 methodHandle sel_method;
1237 lookup_instance_method_in_klasses(sel_method, recv_klass,
1238 resolved_method->name(),
1239 resolved_method->signature(), CHECK);
1240 if (sel_method.is_null() && !check_null_and_abstract) {
1241 // In theory this is a harmless placeholder value, but
1242 // in practice leaving in null affects the nsk default method tests.
1243 // This needs further study.
1244 sel_method = resolved_method;
1245 }
1246 // check if method exists
1247 if (sel_method.is_null()) {
1248 ResourceMark rm(THREAD);
1249 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1250 Method::name_and_sig_as_C_string(recv_klass(),
1251 resolved_method->name(),
1252 resolved_method->signature()));
1253 }
1254 // check access
1255 // Throw Illegal Access Error if sel_method is not public.
1256 if (!sel_method->is_public()) {
1257 ResourceMark rm(THREAD);
1258 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(),
1259 Method::name_and_sig_as_C_string(recv_klass(),
1260 sel_method->name(),
1261 sel_method->signature()));
1262 }
1263 // check if abstract
1264 if (check_null_and_abstract && sel_method->is_abstract()) {
1265 ResourceMark rm(THREAD);
1266 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(),
1267 Method::name_and_sig_as_C_string(recv_klass(),
1268 sel_method->name(),
1269 sel_method->signature()));
1270 }
1271 // setup result
1272 if (!resolved_method->has_itable_index()) {
1273 int vtable_index = resolved_method->vtable_index();
1274 assert(vtable_index == sel_method->vtable_index(), "sanity check");
1275 result.set_virtual(resolved_klass, recv_klass, resolved_method, sel_method, vtable_index, CHECK);
1276 return;
1277 }
1278 int itable_index = resolved_method()->itable_index();
1280 if (TraceItables && Verbose) {
1281 ResourceMark rm(THREAD);
1282 tty->print("invokeinterface selected method: receiver-class:%s, resolved-class:%s, method:%s, method_holder:%s, access_flags: ",
1283 (recv_klass.is_null() ? "<NULL>" : recv_klass->internal_name()),
1284 (resolved_klass.is_null() ? "<NULL>" : resolved_klass->internal_name()),
1285 Method::name_and_sig_as_C_string(resolved_klass(),
1286 resolved_method->name(),
1287 resolved_method->signature()),
1288 sel_method->method_holder()->internal_name()
1289 );
1290 sel_method->access_flags().print_on(tty);
1291 if (sel_method->is_default_method()) {
1292 tty->print("default");
1293 }
1294 if (sel_method->is_overpass()) {
1295 tty->print("overpass");
1296 }
1297 tty->cr();
1298 }
1299 result.set_interface(resolved_klass, recv_klass, resolved_method, sel_method, itable_index, CHECK);
1300 }
1303 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1304 KlassHandle resolved_klass,
1305 Symbol* method_name,
1306 Symbol* method_signature,
1307 KlassHandle current_klass,
1308 bool check_access) {
1309 EXCEPTION_MARK;
1310 methodHandle method_result;
1311 linktime_resolve_interface_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
1312 if (HAS_PENDING_EXCEPTION) {
1313 CLEAR_PENDING_EXCEPTION;
1314 return methodHandle();
1315 } else {
1316 return method_result;
1317 }
1318 }
1320 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1321 KlassHandle resolved_klass,
1322 Symbol* method_name,
1323 Symbol* method_signature,
1324 KlassHandle current_klass,
1325 bool check_access) {
1326 EXCEPTION_MARK;
1327 methodHandle method_result;
1328 linktime_resolve_virtual_method(method_result, resolved_klass, method_name, method_signature, current_klass, check_access, THREAD);
1329 if (HAS_PENDING_EXCEPTION) {
1330 CLEAR_PENDING_EXCEPTION;
1331 return methodHandle();
1332 } else {
1333 return method_result;
1334 }
1335 }
1337 methodHandle LinkResolver::resolve_virtual_call_or_null(
1338 KlassHandle receiver_klass,
1339 KlassHandle resolved_klass,
1340 Symbol* name,
1341 Symbol* signature,
1342 KlassHandle current_klass) {
1343 EXCEPTION_MARK;
1344 CallInfo info;
1345 resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1346 if (HAS_PENDING_EXCEPTION) {
1347 CLEAR_PENDING_EXCEPTION;
1348 return methodHandle();
1349 }
1350 return info.selected_method();
1351 }
1353 methodHandle LinkResolver::resolve_interface_call_or_null(
1354 KlassHandle receiver_klass,
1355 KlassHandle resolved_klass,
1356 Symbol* name,
1357 Symbol* signature,
1358 KlassHandle current_klass) {
1359 EXCEPTION_MARK;
1360 CallInfo info;
1361 resolve_interface_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1362 if (HAS_PENDING_EXCEPTION) {
1363 CLEAR_PENDING_EXCEPTION;
1364 return methodHandle();
1365 }
1366 return info.selected_method();
1367 }
1369 int LinkResolver::resolve_virtual_vtable_index(
1370 KlassHandle receiver_klass,
1371 KlassHandle resolved_klass,
1372 Symbol* name,
1373 Symbol* signature,
1374 KlassHandle current_klass) {
1375 EXCEPTION_MARK;
1376 CallInfo info;
1377 resolve_virtual_call(info, Handle(), receiver_klass, resolved_klass, name, signature, current_klass, true, false, THREAD);
1378 if (HAS_PENDING_EXCEPTION) {
1379 CLEAR_PENDING_EXCEPTION;
1380 return Method::invalid_vtable_index;
1381 }
1382 return info.vtable_index();
1383 }
1385 methodHandle LinkResolver::resolve_static_call_or_null(
1386 KlassHandle resolved_klass,
1387 Symbol* name,
1388 Symbol* signature,
1389 KlassHandle current_klass) {
1390 EXCEPTION_MARK;
1391 CallInfo info;
1392 resolve_static_call(info, resolved_klass, name, signature, current_klass, true, false, THREAD);
1393 if (HAS_PENDING_EXCEPTION) {
1394 CLEAR_PENDING_EXCEPTION;
1395 return methodHandle();
1396 }
1397 return info.selected_method();
1398 }
1400 methodHandle LinkResolver::resolve_special_call_or_null(KlassHandle resolved_klass, Symbol* name, Symbol* signature,
1401 KlassHandle current_klass) {
1402 EXCEPTION_MARK;
1403 CallInfo info;
1404 resolve_special_call(info, resolved_klass, name, signature, current_klass, true, THREAD);
1405 if (HAS_PENDING_EXCEPTION) {
1406 CLEAR_PENDING_EXCEPTION;
1407 return methodHandle();
1408 }
1409 return info.selected_method();
1410 }
1414 //------------------------------------------------------------------------------------------------------------------------
1415 // ConstantPool entries
1417 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) {
1418 switch (byte) {
1419 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1420 case Bytecodes::_invokespecial : resolve_invokespecial (result, pool, index, CHECK); break;
1421 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1422 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1423 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1424 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1425 }
1426 return;
1427 }
1429 void LinkResolver::resolve_pool(KlassHandle& resolved_klass, Symbol*& method_name, Symbol*& method_signature,
1430 KlassHandle& current_klass, constantPoolHandle pool, int index, TRAPS) {
1431 // resolve klass
1432 resolve_klass(resolved_klass, pool, index, CHECK);
1434 // Get name, signature, and static klass
1435 method_name = pool->name_ref_at(index);
1436 method_signature = pool->signature_ref_at(index);
1437 current_klass = KlassHandle(THREAD, pool->pool_holder());
1438 }
1441 void LinkResolver::resolve_invokestatic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1442 KlassHandle resolved_klass;
1443 Symbol* method_name = NULL;
1444 Symbol* method_signature = NULL;
1445 KlassHandle current_klass;
1446 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
1447 resolve_static_call(result, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1448 }
1451 void LinkResolver::resolve_invokespecial(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1452 KlassHandle resolved_klass;
1453 Symbol* method_name = NULL;
1454 Symbol* method_signature = NULL;
1455 KlassHandle current_klass;
1456 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
1457 resolve_special_call(result, resolved_klass, method_name, method_signature, current_klass, true, CHECK);
1458 }
1461 void LinkResolver::resolve_invokevirtual(CallInfo& result, Handle recv,
1462 constantPoolHandle pool, int index,
1463 TRAPS) {
1465 KlassHandle resolved_klass;
1466 Symbol* method_name = NULL;
1467 Symbol* method_signature = NULL;
1468 KlassHandle current_klass;
1469 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
1470 KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1471 resolve_virtual_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1472 }
1475 void LinkResolver::resolve_invokeinterface(CallInfo& result, Handle recv, constantPoolHandle pool, int index, TRAPS) {
1476 KlassHandle resolved_klass;
1477 Symbol* method_name = NULL;
1478 Symbol* method_signature = NULL;
1479 KlassHandle current_klass;
1480 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
1481 KlassHandle recvrKlass (THREAD, recv.is_null() ? (Klass*)NULL : recv->klass());
1482 resolve_interface_call(result, recv, recvrKlass, resolved_klass, method_name, method_signature, current_klass, true, true, CHECK);
1483 }
1486 void LinkResolver::resolve_invokehandle(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1487 assert(EnableInvokeDynamic, "");
1488 // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1489 KlassHandle resolved_klass;
1490 Symbol* method_name = NULL;
1491 Symbol* method_signature = NULL;
1492 KlassHandle current_klass;
1493 resolve_pool(resolved_klass, method_name, method_signature, current_klass, pool, index, CHECK);
1494 if (TraceMethodHandles) {
1495 ResourceMark rm(THREAD);
1496 tty->print_cr("resolve_invokehandle %s %s", method_name->as_C_string(), method_signature->as_C_string());
1497 }
1498 resolve_handle_call(result, resolved_klass, method_name, method_signature, current_klass, CHECK);
1499 }
1501 void LinkResolver::resolve_handle_call(CallInfo& result, KlassHandle resolved_klass,
1502 Symbol* method_name, Symbol* method_signature,
1503 KlassHandle current_klass,
1504 TRAPS) {
1505 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1506 assert(resolved_klass() == SystemDictionary::MethodHandle_klass(), "");
1507 assert(MethodHandles::is_signature_polymorphic_name(method_name), "");
1508 methodHandle resolved_method;
1509 Handle resolved_appendix;
1510 Handle resolved_method_type;
1511 lookup_polymorphic_method(resolved_method, resolved_klass,
1512 method_name, method_signature,
1513 current_klass, &resolved_appendix, &resolved_method_type, CHECK);
1514 result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
1515 }
1518 void LinkResolver::resolve_invokedynamic(CallInfo& result, constantPoolHandle pool, int index, TRAPS) {
1519 assert(EnableInvokeDynamic, "");
1521 //resolve_pool(<resolved_klass>, method_name, method_signature, current_klass, pool, index, CHECK);
1522 Symbol* method_name = pool->name_ref_at(index);
1523 Symbol* method_signature = pool->signature_ref_at(index);
1524 KlassHandle current_klass = KlassHandle(THREAD, pool->pool_holder());
1526 // Resolve the bootstrap specifier (BSM + optional arguments).
1527 Handle bootstrap_specifier;
1528 // Check if CallSite has been bound already:
1529 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(index);
1530 if (cpce->is_f1_null()) {
1531 int pool_index = cpce->constant_pool_index();
1532 oop bsm_info = pool->resolve_bootstrap_specifier_at(pool_index, CHECK);
1533 assert(bsm_info != NULL, "");
1534 // FIXME: Cache this once per BootstrapMethods entry, not once per CONSTANT_InvokeDynamic.
1535 bootstrap_specifier = Handle(THREAD, bsm_info);
1536 }
1537 if (!cpce->is_f1_null()) {
1538 methodHandle method( THREAD, cpce->f1_as_method());
1539 Handle appendix( THREAD, cpce->appendix_if_resolved(pool));
1540 Handle method_type(THREAD, cpce->method_type_if_resolved(pool));
1541 result.set_handle(method, appendix, method_type, CHECK);
1542 return;
1543 }
1545 if (TraceMethodHandles) {
1546 ResourceMark rm(THREAD);
1547 tty->print_cr("resolve_invokedynamic #%d %s %s",
1548 ConstantPool::decode_invokedynamic_index(index),
1549 method_name->as_C_string(), method_signature->as_C_string());
1550 tty->print(" BSM info: "); bootstrap_specifier->print();
1551 }
1553 resolve_dynamic_call(result, bootstrap_specifier, method_name, method_signature, current_klass, CHECK);
1554 }
1556 void LinkResolver::resolve_dynamic_call(CallInfo& result,
1557 Handle bootstrap_specifier,
1558 Symbol* method_name, Symbol* method_signature,
1559 KlassHandle current_klass,
1560 TRAPS) {
1561 // JSR 292: this must resolve to an implicitly generated method MH.linkToCallSite(*...)
1562 // The appendix argument is likely to be a freshly-created CallSite.
1563 Handle resolved_appendix;
1564 Handle resolved_method_type;
1565 methodHandle resolved_method =
1566 SystemDictionary::find_dynamic_call_site_invoker(current_klass,
1567 bootstrap_specifier,
1568 method_name, method_signature,
1569 &resolved_appendix,
1570 &resolved_method_type,
1571 THREAD);
1572 if (HAS_PENDING_EXCEPTION) {
1573 if (TraceMethodHandles) {
1574 tty->print_cr("invokedynamic throws BSME for "INTPTR_FORMAT, (void *)PENDING_EXCEPTION);
1575 PENDING_EXCEPTION->print();
1576 }
1577 if (PENDING_EXCEPTION->is_a(SystemDictionary::BootstrapMethodError_klass())) {
1578 // throw these guys, since they are already wrapped
1579 return;
1580 }
1581 if (!PENDING_EXCEPTION->is_a(SystemDictionary::LinkageError_klass())) {
1582 // intercept only LinkageErrors which might have failed to wrap
1583 return;
1584 }
1585 // See the "Linking Exceptions" section for the invokedynamic instruction in the JVMS.
1586 Handle nested_exception(THREAD, PENDING_EXCEPTION);
1587 CLEAR_PENDING_EXCEPTION;
1588 THROW_CAUSE(vmSymbols::java_lang_BootstrapMethodError(), nested_exception)
1589 }
1590 result.set_handle(resolved_method, resolved_appendix, resolved_method_type, CHECK);
1591 }
1593 //------------------------------------------------------------------------------------------------------------------------
1594 #ifndef PRODUCT
1596 void CallInfo::print() {
1597 ResourceMark rm;
1598 const char* kindstr = "unknown";
1599 switch (_call_kind) {
1600 case direct_call: kindstr = "direct"; break;
1601 case vtable_call: kindstr = "vtable"; break;
1602 case itable_call: kindstr = "itable"; break;
1603 }
1604 tty->print_cr("Call %s@%d %s", kindstr, _call_index,
1605 _resolved_method.is_null() ? "(none)" : _resolved_method->name_and_sig_as_C_string());
1606 }
1608 #endif