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