123 _resolved_klass = resolved_klass;
124 _selected_klass = selected_klass;
125 _resolved_method = resolved_method;
126 _selected_method = selected_method;
127 _call_kind = kind;
128 _call_index = index;
129 _resolved_appendix = Handle();
130 DEBUG_ONLY(verify()); // verify before making side effects
131
132 CompilationPolicy::compile_if_required(selected_method, THREAD);
133 }
134
135 // utility query for unreflecting a method
136 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
137 Klass* resolved_method_holder = resolved_method->method_holder();
138 if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
139 resolved_klass = resolved_method_holder;
140 }
141 _resolved_klass = resolved_klass;
142 _selected_klass = resolved_klass;
143 _resolved_method = resolved_method;
144 _selected_method = resolved_method;
145 // classify:
146 CallKind kind = CallInfo::unknown_kind;
147 int index = resolved_method->vtable_index();
148 if (resolved_method->can_be_statically_bound()) {
149 kind = CallInfo::direct_call;
150 } else if (!resolved_method_holder->is_interface()) {
151 // Could be an Object method inherited into an interface, but still a vtable call.
152 kind = CallInfo::vtable_call;
153 } else if (!resolved_klass->is_interface()) {
154 // A default or miranda method. Compute the vtable index.
155 index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
156 resolved_method);
157 assert(index >= 0 , "we should have valid vtable index at this point");
158
159 kind = CallInfo::vtable_call;
160 } else if (resolved_method->has_vtable_index()) {
161 // Can occur if an interface redeclares a method of Object.
162
163 #ifdef ASSERT
164 // Ensure that this is really the case.
165 Klass* object_klass = SystemDictionary::Object_klass();
166 Method * object_resolved_method = object_klass->vtable().method_at(index);
167 assert(object_resolved_method->name() == resolved_method->name(),
168 "Object and interface method names should match at vtable index %d, %s != %s",
169 index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
170 assert(object_resolved_method->signature() == resolved_method->signature(),
171 "Object and interface method signatures should match at vtable index %d, %s != %s",
172 index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
173 #endif // ASSERT
174
175 kind = CallInfo::vtable_call;
176 } else {
177 // A regular interface call.
178 kind = CallInfo::itable_call;
179 index = resolved_method->itable_index();
180 }
181 assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
182 _call_kind = kind;
183 _call_index = index;
184 _resolved_appendix = Handle();
185 // Find or create a ResolvedMethod instance for this Method*
186 set_resolved_method_name(CHECK);
187
188 DEBUG_ONLY(verify());
189 }
190
191 void CallInfo::set_resolved_method_name(TRAPS) {
192 Method* m = _resolved_method();
193 assert(m != NULL, "Should already have a Method*");
194 oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(m, CHECK);
195 _resolved_method_name = Handle(THREAD, rmethod_name);
196 }
197
198 #ifdef ASSERT
199 void CallInfo::verify() {
200 switch (call_kind()) { // the meaning and allowed value of index depends on kind
201 case CallInfo::direct_call:
202 if (_call_index == Method::nonvirtual_vtable_index) break;
203 // else fall through to check vtable index:
204 case CallInfo::vtable_call:
205 assert(resolved_klass()->verify_vtable_index(_call_index), "");
206 break;
207 case CallInfo::itable_call:
208 assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
209 break;
210 case CallInfo::unknown_kind:
211 assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
212 break;
213 default:
214 fatal("Unexpected call kind %d", call_kind());
364
365 if (result == NULL) {
366 Array<Method*>* default_methods = ik->default_methods();
367 if (default_methods != NULL) {
368 result = InstanceKlass::find_method(default_methods, name, signature);
369 }
370 }
371
372 if (checkpolymorphism && result != NULL) {
373 vmIntrinsics::ID iid = result->intrinsic_id();
374 if (MethodHandles::is_signature_polymorphic(iid)) {
375 // Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.
376 return NULL;
377 }
378 }
379 return result;
380 }
381
382 // returns first instance method
383 // Looks up method in classes, then looks up local default methods
384 methodHandle LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
385 Symbol* name,
386 Symbol* signature,
387 Klass::PrivateLookupMode private_mode, TRAPS) {
388 Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
389
390 while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
391 Klass* super_klass = result->method_holder()->super();
392 result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
393 }
394
395 if (klass->is_array_klass()) {
396 // Only consider klass and super klass for arrays
397 return methodHandle(THREAD, result);
398 }
399
400 if (result == NULL) {
401 Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
402 if (default_methods != NULL) {
403 result = InstanceKlass::find_method(default_methods, name, signature);
404 assert(result == NULL || !result->is_static(), "static defaults not allowed");
405 }
406 }
407 return methodHandle(THREAD, result);
408 }
409
410 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
411 const methodHandle& resolved_method) {
412
413 int vtable_index = Method::invalid_vtable_index;
414 Symbol* name = resolved_method->name();
415 Symbol* signature = resolved_method->signature();
416 InstanceKlass* ik = InstanceKlass::cast(klass);
417
418 // First check in default method array
419 if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
420 int index = InstanceKlass::find_method_index(ik->default_methods(),
421 name, signature, Klass::find_overpass,
422 Klass::find_static, Klass::find_private);
423 if (index >= 0 ) {
424 vtable_index = ik->default_vtable_indices()->at(index);
425 }
426 }
427 if (vtable_index == Method::invalid_vtable_index) {
428 // get vtable_index for miranda methods
429 klassVtable vt = ik->vtable();
430 vtable_index = vt.index_of_miranda(name, signature);
431 }
432 return vtable_index;
433 }
434
435 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
436 InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
437
438 // Specify 'true' in order to skip default methods when searching the
439 // interfaces. Function lookup_method_in_klasses() already looked for
440 // the method in the default methods table.
441 return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::skip_defaults);
442 }
443
444 methodHandle LinkResolver::lookup_polymorphic_method(
445 const LinkInfo& link_info,
446 Handle *appendix_result_or_null,
447 TRAPS) {
448 Klass* klass = link_info.resolved_klass();
449 Symbol* name = link_info.name();
450 Symbol* full_signature = link_info.signature();
451
452 vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
453 if (TraceMethodHandles) {
454 ResourceMark rm(THREAD);
455 tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
456 vmIntrinsics::name_at(iid), klass->external_name(),
457 name->as_C_string(), full_signature->as_C_string());
458 }
459 if ((klass == SystemDictionary::MethodHandle_klass() ||
460 klass == SystemDictionary::VarHandle_klass()) &&
461 iid != vmIntrinsics::_none) {
462 if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
463 // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
464 // Do not erase last argument type (MemberName) if it is a static linkTo method.
465 bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
466 TempNewSymbol basic_signature =
467 MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
468 if (TraceMethodHandles) {
469 ResourceMark rm(THREAD);
470 tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
471 name->as_C_string(),
472 full_signature->as_C_string(),
473 basic_signature->as_C_string());
474 }
475 methodHandle result = SystemDictionary::find_method_handle_intrinsic(iid,
476 basic_signature,
477 CHECK_NULL);
478 if (result.not_null()) {
479 assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
480 assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
481 assert(basic_signature == result->signature(), "predict the result signature");
482 if (TraceMethodHandles) {
483 ttyLocker ttyl;
484 tty->print("lookup_polymorphic_method => intrinsic ");
485 result->print_on(tty);
486 }
487 }
488 return result;
489 } else if (iid == vmIntrinsics::_invokeGeneric
490 && THREAD->can_call_java()
491 && appendix_result_or_null != NULL) {
492 // This is a method with type-checking semantics.
493 // We will ask Java code to spin an adapter method for it.
494 if (!MethodHandles::enabled()) {
495 // Make sure the Java part of the runtime has been booted up.
496 Klass* natives = SystemDictionary::MethodHandleNatives_klass();
497 if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
498 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
499 Handle(),
500 Handle(),
501 true,
502 CHECK_NULL);
503 }
504 }
505
506 Handle appendix;
507 Handle method_type;
508 methodHandle result = SystemDictionary::find_method_handle_invoker(
509 klass,
510 name,
511 full_signature,
512 link_info.current_klass(),
513 &appendix,
514 CHECK_NULL);
515 if (TraceMethodHandles) {
516 ttyLocker ttyl;
517 tty->print("lookup_polymorphic_method => (via Java) ");
518 result->print_on(tty);
519 tty->print(" lookup_polymorphic_method => appendix = ");
520 if (appendix.is_null()) tty->print_cr("(none)");
521 else appendix->print_on(tty);
522 }
523 if (result.not_null()) {
524 #ifdef ASSERT
525 ResourceMark rm(THREAD);
526
527 TempNewSymbol basic_signature =
528 MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL);
529 int actual_size_of_params = result->size_of_parameters();
530 int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
531 // +1 for MethodHandle.this, +1 for trailing MethodType
532 if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
533 if (appendix.not_null()) expected_size_of_params += 1;
534 if (actual_size_of_params != expected_size_of_params) {
535 tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
536 tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
537 result->print();
538 }
539 assert(actual_size_of_params == expected_size_of_params,
540 "%d != %d", actual_size_of_params, expected_size_of_params);
541 #endif //ASSERT
542
543 assert(appendix_result_or_null != NULL, "");
586 if (!can_access) {
587 ResourceMark rm(THREAD);
588 bool same_module = (sel_klass->module() == ref_klass->module());
589 Exceptions::fthrow(
590 THREAD_AND_LOCATION,
591 vmSymbols::java_lang_IllegalAccessError(),
592 "class %s tried to access %s%s%smethod '%s' (%s%s%s)",
593 ref_klass->external_name(),
594 sel_method->is_abstract() ? "abstract " : "",
595 sel_method->is_protected() ? "protected " : "",
596 sel_method->is_private() ? "private " : "",
597 sel_method->external_name(),
598 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
599 (same_module) ? "" : "; ",
600 (same_module) ? "" : sel_klass->class_in_module_of_loader()
601 );
602 return;
603 }
604 }
605
606 methodHandle LinkResolver::resolve_method_statically(Bytecodes::Code code,
607 const constantPoolHandle& pool, int index, TRAPS) {
608 // This method is used only
609 // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
610 // and
611 // (2) in Bytecode_invoke::static_target
612 // It appears to fail when applied to an invokeinterface call site.
613 // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
614 // resolve klass
615 if (code == Bytecodes::_invokedynamic) {
616 Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
617 Symbol* method_name = vmSymbols::invoke_name();
618 Symbol* method_signature = pool->signature_ref_at(index);
619 Klass* current_klass = pool->pool_holder();
620 LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
621 return resolve_method(link_info, code, THREAD);
622 }
623
624 LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
625 Klass* resolved_klass = link_info.resolved_klass();
626
627 if (pool->has_preresolution()
628 || (resolved_klass == SystemDictionary::MethodHandle_klass() &&
629 MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
630 Method* result = ConstantPool::method_at_if_loaded(pool, index);
631 if (result != NULL) {
632 return methodHandle(THREAD, result);
633 }
634 }
635
636 if (code == Bytecodes::_invokeinterface) {
637 return resolve_interface_method(link_info, code, THREAD);
638 } else if (code == Bytecodes::_invokevirtual) {
639 return resolve_method(link_info, code, THREAD);
640 } else if (!resolved_klass->is_interface()) {
641 return resolve_method(link_info, code, THREAD);
642 } else {
643 return resolve_interface_method(link_info, code, THREAD);
644 }
645 }
646
647 // Check and print a loader constraint violation message for method or interface method
648 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
649 const methodHandle& resolved_method,
650 const char* method_type, TRAPS) {
651 Handle current_loader(THREAD, link_info.current_klass()->class_loader());
652 Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
697 const char* failed_type_name = failed_type_symbol->as_klass_external_name();
698
699 ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
700 "the class loader %s of the current class, %s, "
701 "and the class loader %s for the field's defining %s, %s, "
702 "have different Class objects for type %s (%s; %s)",
703 field->as_C_string(),
704 failed_type_name,
705 current_klass->class_loader_data()->loader_name_and_id(),
706 current_klass->external_name(),
707 sel_klass->class_loader_data()->loader_name_and_id(),
708 sel_klass->external_kind(),
709 sel_klass->external_name(),
710 failed_type_name,
711 current_klass->class_in_module_of_loader(false, true),
712 sel_klass->class_in_module_of_loader(false, true));
713 THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
714 }
715 }
716
717 methodHandle LinkResolver::resolve_method(const LinkInfo& link_info,
718 Bytecodes::Code code, TRAPS) {
719
720 Handle nested_exception;
721 Klass* resolved_klass = link_info.resolved_klass();
722
723 // 1. For invokevirtual, cannot call an interface method
724 if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
725 ResourceMark rm(THREAD);
726 char buf[200];
727 jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
728 resolved_klass->external_name());
729 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
730 }
731
732 // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
733 if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
734 ResourceMark rm(THREAD);
735 stringStream ss;
736 ss.print("Method '");
737 Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
738 ss.print("' must be Methodref constant");
739 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
740 }
741
742 // 3. lookup method in resolved klass and its super klasses
743 methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
744
745 // 4. lookup method in all the interfaces implemented by the resolved klass
746 if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
747 resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
748
749 if (resolved_method.is_null()) {
750 // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
751 resolved_method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);
752 if (HAS_PENDING_EXCEPTION) {
753 nested_exception = Handle(THREAD, PENDING_EXCEPTION);
754 CLEAR_PENDING_EXCEPTION;
755 }
756 }
757 }
758
759 // 5. method lookup failed
760 if (resolved_method.is_null()) {
761 ResourceMark rm(THREAD);
762 stringStream ss;
763 ss.print("'");
764 Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
765 ss.print("'");
766 THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
767 ss.as_string(), nested_exception, NULL);
768 }
769
770 // 6. access checks, access checking may be turned off when calling from within the VM.
771 Klass* current_klass = link_info.current_klass();
772 if (link_info.check_access()) {
773 assert(current_klass != NULL , "current_klass should not be null");
774
775 // check if method can be accessed by the referring class
776 check_method_accessability(current_klass,
777 resolved_klass,
778 resolved_method->method_holder(),
779 resolved_method,
780 CHECK_NULL);
781
782 // check loader constraints
783 check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
784 }
785
786 return resolved_method;
787 }
788
789 static void trace_method_resolution(const char* prefix,
790 Klass* klass,
791 Klass* resolved_klass,
792 const methodHandle& method,
793 bool logitables,
794 int index = -1) {
795 #ifndef PRODUCT
796 ResourceMark rm;
797 Log(itables) logi;
798 LogStream lsi(logi.trace());
799 Log(vtables) logv;
800 LogStream lsv(logv.trace());
801 outputStream* st;
802 if (logitables) {
803 st = &lsi;
804 } else {
805 st = &lsv;
806 }
807 st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
808 prefix,
809 (klass == NULL ? "<NULL>" : klass->internal_name()),
810 (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
811 Method::name_and_sig_as_C_string(resolved_klass,
812 method->name(),
813 method->signature()),
814 method->method_holder()->internal_name());
815 method->print_linkage_flags(st);
816 if (index != -1) {
817 st->print("vtable_index:%d", index);
818 }
819 st->cr();
820 #endif // PRODUCT
821 }
822
823 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
824 methodHandle LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
825
826 Klass* resolved_klass = link_info.resolved_klass();
827
828 // check if klass is interface
829 if (!resolved_klass->is_interface()) {
830 ResourceMark rm(THREAD);
831 char buf[200];
832 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
833 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
834 }
835
836 // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
837 if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
838 ResourceMark rm(THREAD);
839 stringStream ss;
840 ss.print("Method '");
841 Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
842 ss.print("' must be InterfaceMethodref constant");
843 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
844 }
875 resolved_method,
876 CHECK_NULL);
877
878 check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
879 }
880
881 if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
882 ResourceMark rm(THREAD);
883 stringStream ss;
884 ss.print("Expected instance not static method '");
885 Method::print_external_name(&ss, resolved_klass,
886 resolved_method->name(), resolved_method->signature());
887 ss.print("'");
888 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
889 }
890
891 if (log_develop_is_enabled(Trace, itables)) {
892 char buf[200];
893 jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
894 Bytecodes::name(code));
895 trace_method_resolution(buf, link_info.current_klass(), resolved_klass,
896 resolved_method, true);
897 }
898
899 return resolved_method;
900 }
901
902 //------------------------------------------------------------------------------------------------------------------------
903 // Field resolution
904
905 void LinkResolver::check_field_accessability(Klass* ref_klass,
906 Klass* resolved_klass,
907 Klass* sel_klass,
908 const fieldDescriptor& fd,
909 TRAPS) {
910 bool can_access = Reflection::verify_member_access(ref_klass,
911 resolved_klass,
912 sel_klass,
913 fd.access_flags(),
914 true, false, CHECK);
915 // Any existing exceptions that may have been thrown, for example LinkageErrors
916 // from nest-host resolution, have been allowed to propagate.
917 if (!can_access) {
918 bool same_module = (sel_klass->module() == ref_klass->module());
919 ResourceMark rm(THREAD);
982 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
983 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
984 }
985
986 // A final field can be modified only
987 // (1) by methods declared in the class declaring the field and
988 // (2) by the <clinit> method (in case of a static field)
989 // or by the <init> method (in case of an instance field).
990 if (is_put && fd.access_flags().is_final()) {
991 ResourceMark rm(THREAD);
992 stringStream ss;
993
994 if (sel_klass != current_klass) {
995 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
996 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
997 current_klass->external_name());
998 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
999 }
1000
1001 if (fd.constants()->pool_holder()->major_version() >= 53) {
1002 methodHandle m = link_info.current_method();
1003 assert(!m.is_null(), "information about the current method must be available for 'put' bytecodes");
1004 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1005 fd.is_static() &&
1006 !m()->is_static_initializer());
1007 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1008 !fd.is_static() &&
1009 !m->is_object_initializer());
1010
1011 if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1012 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1013 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1014 m()->name()->as_C_string(),
1015 is_static ? "<clinit>" : "<init>");
1016 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1017 }
1018 }
1019 }
1020
1021 // initialize resolved_klass if necessary
1022 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1023 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1024 //
1025 // note 2: we don't want to force initialization if we are just checking
1026 // if the field access is legal; e.g., during compilation
1027 if (is_static && initialize_class) {
1028 sel_klass->initialize(CHECK);
1029 }
1030 }
1031
1032 if ((sel_klass != current_klass) && (current_klass != NULL)) {
1033 check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1034 }
1035
1036 // return information. note that the klass is set to the actual klass containing the
1037 // field, otherwise access of static fields in superclasses will not work.
1038 }
1039
1040
1041 //------------------------------------------------------------------------------------------------------------------------
1042 // Invoke resolution
1043 //
1044 // Naming conventions:
1045 //
1046 // resolved_method the specified method (i.e., static receiver specified via constant pool index)
1047 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)
1048 // resolved_klass the specified klass (i.e., specified via constant pool index)
1049 // recv_klass the receiver klass
1050
1051
1052 void LinkResolver::resolve_static_call(CallInfo& result,
1053 const LinkInfo& link_info,
1054 bool initialize_class, TRAPS) {
1055 methodHandle resolved_method = linktime_resolve_static_method(link_info, CHECK);
1056
1057 // The resolved class can change as a result of this resolution.
1058 Klass* resolved_klass = resolved_method->method_holder();
1059
1060 // Initialize klass (this should only happen if everything is ok)
1061 if (initialize_class && resolved_klass->should_be_initialized()) {
1062 resolved_klass->initialize(CHECK);
1063 // Use updated LinkInfo to reresolve with resolved method holder
1064 LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1065 link_info.current_klass(),
1066 link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1067 resolved_method = linktime_resolve_static_method(new_info, CHECK);
1068 }
1069
1070 // setup result
1071 result.set_static(resolved_klass, resolved_method, CHECK);
1072 }
1073
1074 // throws linktime exceptions
1075 methodHandle LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1076
1077 Klass* resolved_klass = link_info.resolved_klass();
1078 methodHandle resolved_method;
1079 if (!resolved_klass->is_interface()) {
1080 resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1081 } else {
1082 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1083 }
1084 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1085
1086 // check if static
1087 if (!resolved_method->is_static()) {
1088 ResourceMark rm(THREAD);
1089 stringStream ss;
1090 ss.print("Expected static method '");
1091 resolved_method()->print_external_name(&ss);
1092 ss.print("'");
1093 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1094 }
1095 return resolved_method;
1096 }
1097
1098
1099 void LinkResolver::resolve_special_call(CallInfo& result,
1100 Handle recv,
1101 const LinkInfo& link_info,
1102 TRAPS) {
1103 methodHandle resolved_method = linktime_resolve_special_method(link_info, CHECK);
1104 runtime_resolve_special_method(result, link_info, resolved_method, recv, CHECK);
1105 }
1106
1107 // throws linktime exceptions
1108 methodHandle LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info,
1109 TRAPS) {
1110
1111 // Invokespecial is called for multiple special reasons:
1112 // <init>
1113 // local private method invocation, for classes and interfaces
1114 // superclass.method, which can also resolve to a default method
1115 // and the selected method is recalculated relative to the direct superclass
1116 // superinterface.method, which explicitly does not check shadowing
1117 Klass* resolved_klass = link_info.resolved_klass();
1118 methodHandle resolved_method;
1119
1120 if (!resolved_klass->is_interface()) {
1121 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1122 } else {
1123 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1124 }
1125
1126 // check if method name is <init>, that it is found in same klass as static type
1127 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1128 resolved_method->method_holder() != resolved_klass) {
1129 ResourceMark rm(THREAD);
1130 stringStream ss;
1131 ss.print("%s: method '", resolved_klass->external_name());
1132 resolved_method->signature()->print_as_signature_external_return_type(&ss);
1133 ss.print(" %s(", resolved_method->name()->as_C_string());
1134 resolved_method->signature()->print_as_signature_external_parameters(&ss);
1135 ss.print(")' not found");
1136 Exceptions::fthrow(
1137 THREAD_AND_LOCATION,
1138 vmSymbols::java_lang_NoSuchMethodError(),
1193 // resolved method is selected method unless we have an old-style lookup
1194 // for a superclass method
1195 // Invokespecial for a superinterface, resolved method is selected method,
1196 // no checks for shadowing
1197 methodHandle sel_method(THREAD, resolved_method());
1198
1199 if (link_info.check_access() &&
1200 // check if the method is not <init>
1201 resolved_method->name() != vmSymbols::object_initializer_name()) {
1202
1203 Klass* current_klass = link_info.current_klass();
1204
1205 // Check if the class of the resolved_klass is a superclass
1206 // (not supertype in order to exclude interface classes) of the current class.
1207 // This check is not performed for super.invoke for interface methods
1208 // in super interfaces.
1209 if (current_klass->is_subclass_of(resolved_klass) &&
1210 current_klass != resolved_klass) {
1211 // Lookup super method
1212 Klass* super_klass = current_klass->super();
1213 sel_method = lookup_instance_method_in_klasses(super_klass,
1214 resolved_method->name(),
1215 resolved_method->signature(),
1216 Klass::find_private, CHECK);
1217 // check if found
1218 if (sel_method.is_null()) {
1219 ResourceMark rm(THREAD);
1220 stringStream ss;
1221 ss.print("'");
1222 resolved_method->print_external_name(&ss);
1223 ss.print("'");
1224 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1225 // check loader constraints if found a different method
1226 } else if (sel_method() != resolved_method()) {
1227 check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1228 }
1229 }
1230
1231 // Check that the class of objectref (the receiver) is the current class or interface,
1232 // or a subtype of the current class or interface (the sender), otherwise invokespecial
1233 // throws IllegalAccessError.
1234 // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1235 // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1236 // a class. If the sender is an interface, the check has to be performed at runtime.
1255 ResourceMark rm(THREAD);
1256 stringStream ss;
1257 ss.print("Expecting non-static method '");
1258 resolved_method->print_external_name(&ss);
1259 ss.print("'");
1260 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1261 }
1262
1263 // check if abstract
1264 if (sel_method->is_abstract()) {
1265 ResourceMark rm(THREAD);
1266 stringStream ss;
1267 ss.print("'");
1268 Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1269 ss.print("'");
1270 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1271 }
1272
1273 if (log_develop_is_enabled(Trace, itables)) {
1274 trace_method_resolution("invokespecial selected method: resolved-class:",
1275 resolved_klass, resolved_klass, sel_method, true);
1276 }
1277
1278 // setup result
1279 result.set_static(resolved_klass, sel_method, CHECK);
1280 }
1281
1282 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1283 const LinkInfo& link_info,
1284 bool check_null_and_abstract, TRAPS) {
1285 methodHandle resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1286 runtime_resolve_virtual_method(result, resolved_method,
1287 link_info.resolved_klass(),
1288 recv, receiver_klass,
1289 check_null_and_abstract, CHECK);
1290 }
1291
1292 // throws linktime exceptions
1293 methodHandle LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1294 TRAPS) {
1295 // normal method resolution
1296 methodHandle resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1297
1298 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1299 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1300
1301 // check if private interface method
1302 Klass* resolved_klass = link_info.resolved_klass();
1303 Klass* current_klass = link_info.current_klass();
1304
1305 // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1306 if (resolved_klass->is_interface() && resolved_method->is_private()) {
1307 ResourceMark rm(THREAD);
1308 stringStream ss;
1309 ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1310 resolved_method->print_external_name(&ss);
1311 ss.print("', caller-class: %s",
1312 (current_klass == NULL ? "<null>" : current_klass->internal_name()));
1313 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1314 }
1315
1316 // check if not static
1375 assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1376 selected_method = resolved_method;
1377 } else {
1378 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1379 }
1380 }
1381
1382 // check if method exists
1383 if (selected_method.is_null()) {
1384 throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1385 }
1386
1387 // check if abstract
1388 if (check_null_and_abstract && selected_method->is_abstract()) {
1389 // Pass arguments for generating a verbose error message.
1390 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1391 }
1392
1393 if (log_develop_is_enabled(Trace, vtables)) {
1394 trace_method_resolution("invokevirtual selected method: receiver-class:",
1395 recv_klass, resolved_klass, selected_method,
1396 false, vtable_index);
1397 }
1398 // setup result
1399 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1400 }
1401
1402 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1403 const LinkInfo& link_info,
1404 bool check_null_and_abstract, TRAPS) {
1405 // throws linktime exceptions
1406 methodHandle resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1407 runtime_resolve_interface_method(result, resolved_method,link_info.resolved_klass(),
1408 recv, recv_klass, check_null_and_abstract, CHECK);
1409 }
1410
1411 methodHandle LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1412 TRAPS) {
1413 // normal interface method resolution
1414 methodHandle resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1415 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1416 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1417
1418 return resolved_method;
1419 }
1420
1421 // throws runtime exceptions
1422 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1423 const methodHandle& resolved_method,
1424 Klass* resolved_klass,
1425 Handle recv,
1426 Klass* recv_klass,
1427 bool check_null_and_abstract, TRAPS) {
1428
1429 // check if receiver exists
1430 if (check_null_and_abstract && recv.is_null()) {
1431 THROW(vmSymbols::java_lang_NullPointerException());
1432 }
1433
1434 // check if receiver klass implements the resolved interface
1435 if (!recv_klass->is_subtype_of(resolved_klass)) {
1436 ResourceMark rm(THREAD);
1437 char buf[200];
1438 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1439 recv_klass->external_name(),
1440 resolved_klass->external_name());
1441 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1442 }
1443
1444 methodHandle selected_method = resolved_method;
1445
1446 // resolve the method in the receiver class, unless it is private
1447 if (!resolved_method()->is_private()) {
1448 // do lookup based on receiver klass
1449 // This search must match the linktime preparation search for itable initialization
1450 // to correctly enforce loader constraints for interface method inheritance.
1451 // Private methods are skipped as the resolved method was not private.
1452 selected_method = lookup_instance_method_in_klasses(recv_klass,
1453 resolved_method->name(),
1454 resolved_method->signature(),
1455 Klass::skip_private, CHECK);
1456
1457 if (selected_method.is_null() && !check_null_and_abstract) {
1458 // In theory this is a harmless placeholder value, but
1459 // in practice leaving in null affects the nsk default method tests.
1460 // This needs further study.
1461 selected_method = resolved_method;
1462 }
1463 // check if method exists
1464 if (selected_method.is_null()) {
1465 // Pass arguments for generating a verbose error message.
1466 throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1467 }
1468 // check access
1469 // Throw Illegal Access Error if selected_method is not public.
1470 if (!selected_method->is_public()) {
1471 ResourceMark rm(THREAD);
1472 stringStream ss;
1473 ss.print("'");
1474 Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1475 ss.print("'");
1476 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1477 }
1478 // check if abstract
1479 if (check_null_and_abstract && selected_method->is_abstract()) {
1480 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1481 }
1482 }
1483
1484 if (log_develop_is_enabled(Trace, itables)) {
1485 trace_method_resolution("invokeinterface selected method: receiver-class:",
1486 recv_klass, resolved_klass, selected_method, true);
1487 }
1488 // setup result
1489 if (resolved_method->has_vtable_index()) {
1490 int vtable_index = resolved_method->vtable_index();
1491 log_develop_trace(itables)(" -- vtable index: %d", vtable_index);
1492 assert(vtable_index == selected_method->vtable_index(), "sanity check");
1493 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1494 } else if (resolved_method->has_itable_index()) {
1495 int itable_index = resolved_method()->itable_index();
1496 log_develop_trace(itables)(" -- itable index: %d", itable_index);
1497 result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK);
1498 } else {
1499 int index = resolved_method->vtable_index();
1500 log_develop_trace(itables)(" -- non itable/vtable index: %d", index);
1501 assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1502 assert(resolved_method()->is_private() ||
1503 (resolved_method()->is_final() && resolved_method->method_holder() == SystemDictionary::Object_klass()),
1504 "Should only have non-virtual invokeinterface for private or final-Object methods!");
1505 assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1506 // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1507 result.set_virtual(resolved_klass, resolved_klass, resolved_method, resolved_method, index, CHECK);
1508 }
1509 }
1510
1511
1512 methodHandle LinkResolver::linktime_resolve_interface_method_or_null(
1513 const LinkInfo& link_info) {
1514 EXCEPTION_MARK;
1515 methodHandle method_result = linktime_resolve_interface_method(link_info, THREAD);
1516 if (HAS_PENDING_EXCEPTION) {
1517 CLEAR_PENDING_EXCEPTION;
1518 return methodHandle();
1519 } else {
1520 return method_result;
1521 }
1522 }
1523
1524 methodHandle LinkResolver::linktime_resolve_virtual_method_or_null(
1525 const LinkInfo& link_info) {
1526 EXCEPTION_MARK;
1527 methodHandle method_result = linktime_resolve_virtual_method(link_info, THREAD);
1528 if (HAS_PENDING_EXCEPTION) {
1529 CLEAR_PENDING_EXCEPTION;
1530 return methodHandle();
1531 } else {
1532 return method_result;
1533 }
1534 }
1535
1536 methodHandle LinkResolver::resolve_virtual_call_or_null(
1537 Klass* receiver_klass,
1538 const LinkInfo& link_info) {
1539 EXCEPTION_MARK;
1540 CallInfo info;
1541 resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1542 if (HAS_PENDING_EXCEPTION) {
1543 CLEAR_PENDING_EXCEPTION;
1544 return methodHandle();
1545 }
1546 return info.selected_method();
1547 }
1548
1549 methodHandle LinkResolver::resolve_interface_call_or_null(
1550 Klass* receiver_klass,
1551 const LinkInfo& link_info) {
1552 EXCEPTION_MARK;
1553 CallInfo info;
1554 resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1555 if (HAS_PENDING_EXCEPTION) {
1556 CLEAR_PENDING_EXCEPTION;
1557 return methodHandle();
1558 }
1559 return info.selected_method();
1560 }
1561
1562 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1563 const LinkInfo& link_info) {
1564 EXCEPTION_MARK;
1565 CallInfo info;
1566 resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1567 /*check_null_or_abstract*/false, THREAD);
1568 if (HAS_PENDING_EXCEPTION) {
1569 CLEAR_PENDING_EXCEPTION;
1570 return Method::invalid_vtable_index;
1571 }
1572 return info.vtable_index();
1573 }
1574
1575 methodHandle LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1576 EXCEPTION_MARK;
1577 CallInfo info;
1578 resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1579 if (HAS_PENDING_EXCEPTION) {
1580 CLEAR_PENDING_EXCEPTION;
1581 return methodHandle();
1582 }
1583 return info.selected_method();
1584 }
1585
1586 methodHandle LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1587 EXCEPTION_MARK;
1588 CallInfo info;
1589 resolve_special_call(info, Handle(), link_info, THREAD);
1590 if (HAS_PENDING_EXCEPTION) {
1591 CLEAR_PENDING_EXCEPTION;
1592 return methodHandle();
1593 }
1594 return info.selected_method();
1595 }
1596
1597
1598
1599 //------------------------------------------------------------------------------------------------------------------------
1600 // ConstantPool entries
1601
1602 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1603 switch (byte) {
1604 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1605 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1606 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1607 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1608 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1609 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1610 default : break;
1611 }
1612 return;
1673 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1674 // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1675 LinkInfo link_info(pool, index, CHECK);
1676 if (TraceMethodHandles) {
1677 ResourceMark rm(THREAD);
1678 tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1679 link_info.signature()->as_C_string());
1680 }
1681 resolve_handle_call(result, link_info, CHECK);
1682 }
1683
1684 void LinkResolver::resolve_handle_call(CallInfo& result,
1685 const LinkInfo& link_info,
1686 TRAPS) {
1687 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1688 Klass* resolved_klass = link_info.resolved_klass();
1689 assert(resolved_klass == SystemDictionary::MethodHandle_klass() ||
1690 resolved_klass == SystemDictionary::VarHandle_klass(), "");
1691 assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1692 Handle resolved_appendix;
1693 methodHandle resolved_method = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1694 result.set_handle(resolved_klass, resolved_method, resolved_appendix, CHECK);
1695 }
1696
1697 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1698 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(indy_index);
1699 int pool_index = cpce->constant_pool_index();
1700
1701 // Resolve the bootstrap specifier (BSM + optional arguments).
1702 BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
1703
1704 // Check if CallSite has been bound already or failed already, and short circuit:
1705 {
1706 bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1707 if (is_done) return;
1708 }
1709
1710 // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1711 // reference to a method handle which will be the bootstrap method for a dynamic
1712 // call site. If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1713 // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1714 // method's CP index for the CONSTANT_MethodHandle_info. So, there is no need to
|
123 _resolved_klass = resolved_klass;
124 _selected_klass = selected_klass;
125 _resolved_method = resolved_method;
126 _selected_method = selected_method;
127 _call_kind = kind;
128 _call_index = index;
129 _resolved_appendix = Handle();
130 DEBUG_ONLY(verify()); // verify before making side effects
131
132 CompilationPolicy::compile_if_required(selected_method, THREAD);
133 }
134
135 // utility query for unreflecting a method
136 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass, TRAPS) {
137 Klass* resolved_method_holder = resolved_method->method_holder();
138 if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st
139 resolved_klass = resolved_method_holder;
140 }
141 _resolved_klass = resolved_klass;
142 _selected_klass = resolved_klass;
143 _resolved_method = methodHandle(THREAD, resolved_method);
144 _selected_method = methodHandle(THREAD, resolved_method);
145 // classify:
146 CallKind kind = CallInfo::unknown_kind;
147 int index = resolved_method->vtable_index();
148 if (resolved_method->can_be_statically_bound()) {
149 kind = CallInfo::direct_call;
150 } else if (!resolved_method_holder->is_interface()) {
151 // Could be an Object method inherited into an interface, but still a vtable call.
152 kind = CallInfo::vtable_call;
153 } else if (!resolved_klass->is_interface()) {
154 // A default or miranda method. Compute the vtable index.
155 index = LinkResolver::vtable_index_of_interface_method(resolved_klass,
156 _resolved_method);
157 assert(index >= 0 , "we should have valid vtable index at this point");
158
159 kind = CallInfo::vtable_call;
160 } else if (resolved_method->has_vtable_index()) {
161 // Can occur if an interface redeclares a method of Object.
162
163 #ifdef ASSERT
164 // Ensure that this is really the case.
165 Klass* object_klass = SystemDictionary::Object_klass();
166 Method * object_resolved_method = object_klass->vtable().method_at(index);
167 assert(object_resolved_method->name() == resolved_method->name(),
168 "Object and interface method names should match at vtable index %d, %s != %s",
169 index, object_resolved_method->name()->as_C_string(), resolved_method->name()->as_C_string());
170 assert(object_resolved_method->signature() == resolved_method->signature(),
171 "Object and interface method signatures should match at vtable index %d, %s != %s",
172 index, object_resolved_method->signature()->as_C_string(), resolved_method->signature()->as_C_string());
173 #endif // ASSERT
174
175 kind = CallInfo::vtable_call;
176 } else {
177 // A regular interface call.
178 kind = CallInfo::itable_call;
179 index = resolved_method->itable_index();
180 }
181 assert(index == Method::nonvirtual_vtable_index || index >= 0, "bad index %d", index);
182 _call_kind = kind;
183 _call_index = index;
184 _resolved_appendix = Handle();
185 // Find or create a ResolvedMethod instance for this Method*
186 set_resolved_method_name(CHECK);
187
188 DEBUG_ONLY(verify());
189 }
190
191 void CallInfo::set_resolved_method_name(TRAPS) {
192 assert(_resolved_method() != NULL, "Should already have a Method*");
193 oop rmethod_name = java_lang_invoke_ResolvedMethodName::find_resolved_method(_resolved_method, CHECK);
194 _resolved_method_name = Handle(THREAD, rmethod_name);
195 }
196
197 #ifdef ASSERT
198 void CallInfo::verify() {
199 switch (call_kind()) { // the meaning and allowed value of index depends on kind
200 case CallInfo::direct_call:
201 if (_call_index == Method::nonvirtual_vtable_index) break;
202 // else fall through to check vtable index:
203 case CallInfo::vtable_call:
204 assert(resolved_klass()->verify_vtable_index(_call_index), "");
205 break;
206 case CallInfo::itable_call:
207 assert(resolved_method()->method_holder()->verify_itable_index(_call_index), "");
208 break;
209 case CallInfo::unknown_kind:
210 assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set");
211 break;
212 default:
213 fatal("Unexpected call kind %d", call_kind());
363
364 if (result == NULL) {
365 Array<Method*>* default_methods = ik->default_methods();
366 if (default_methods != NULL) {
367 result = InstanceKlass::find_method(default_methods, name, signature);
368 }
369 }
370
371 if (checkpolymorphism && result != NULL) {
372 vmIntrinsics::ID iid = result->intrinsic_id();
373 if (MethodHandles::is_signature_polymorphic(iid)) {
374 // Do not link directly to these. The VM must produce a synthetic one using lookup_polymorphic_method.
375 return NULL;
376 }
377 }
378 return result;
379 }
380
381 // returns first instance method
382 // Looks up method in classes, then looks up local default methods
383 Method* LinkResolver::lookup_instance_method_in_klasses(Klass* klass,
384 Symbol* name,
385 Symbol* signature,
386 Klass::PrivateLookupMode private_mode, TRAPS) {
387 Method* result = klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
388
389 while (result != NULL && result->is_static() && result->method_holder()->super() != NULL) {
390 Klass* super_klass = result->method_holder()->super();
391 result = super_klass->uncached_lookup_method(name, signature, Klass::find_overpass, private_mode);
392 }
393
394 if (klass->is_array_klass()) {
395 // Only consider klass and super klass for arrays
396 return result;
397 }
398
399 if (result == NULL) {
400 Array<Method*>* default_methods = InstanceKlass::cast(klass)->default_methods();
401 if (default_methods != NULL) {
402 result = InstanceKlass::find_method(default_methods, name, signature);
403 assert(result == NULL || !result->is_static(), "static defaults not allowed");
404 }
405 }
406 return result;
407 }
408
409 int LinkResolver::vtable_index_of_interface_method(Klass* klass,
410 const methodHandle& resolved_method) {
411
412 int vtable_index = Method::invalid_vtable_index;
413 Symbol* name = resolved_method->name();
414 Symbol* signature = resolved_method->signature();
415 InstanceKlass* ik = InstanceKlass::cast(klass);
416
417 // First check in default method array
418 if (!resolved_method->is_abstract() && ik->default_methods() != NULL) {
419 int index = InstanceKlass::find_method_index(ik->default_methods(),
420 name, signature, Klass::find_overpass,
421 Klass::find_static, Klass::find_private);
422 if (index >= 0 ) {
423 vtable_index = ik->default_vtable_indices()->at(index);
424 }
425 }
426 if (vtable_index == Method::invalid_vtable_index) {
427 // get vtable_index for miranda methods
428 klassVtable vt = ik->vtable();
429 vtable_index = vt.index_of_miranda(name, signature);
430 }
431 return vtable_index;
432 }
433
434 Method* LinkResolver::lookup_method_in_interfaces(const LinkInfo& cp_info) {
435 InstanceKlass *ik = InstanceKlass::cast(cp_info.resolved_klass());
436
437 // Specify 'true' in order to skip default methods when searching the
438 // interfaces. Function lookup_method_in_klasses() already looked for
439 // the method in the default methods table.
440 return ik->lookup_method_in_all_interfaces(cp_info.name(), cp_info.signature(), Klass::skip_defaults);
441 }
442
443 Method* LinkResolver::lookup_polymorphic_method(const LinkInfo& link_info,
444 Handle *appendix_result_or_null,
445 TRAPS) {
446 Klass* klass = link_info.resolved_klass();
447 Symbol* name = link_info.name();
448 Symbol* full_signature = link_info.signature();
449
450 vmIntrinsics::ID iid = MethodHandles::signature_polymorphic_name_id(name);
451 if (TraceMethodHandles) {
452 ResourceMark rm(THREAD);
453 tty->print_cr("lookup_polymorphic_method iid=%s %s.%s%s",
454 vmIntrinsics::name_at(iid), klass->external_name(),
455 name->as_C_string(), full_signature->as_C_string());
456 }
457 if ((klass == SystemDictionary::MethodHandle_klass() ||
458 klass == SystemDictionary::VarHandle_klass()) &&
459 iid != vmIntrinsics::_none) {
460 if (MethodHandles::is_signature_polymorphic_intrinsic(iid)) {
461 // Most of these do not need an up-call to Java to resolve, so can be done anywhere.
462 // Do not erase last argument type (MemberName) if it is a static linkTo method.
463 bool keep_last_arg = MethodHandles::is_signature_polymorphic_static(iid);
464 TempNewSymbol basic_signature =
465 MethodHandles::lookup_basic_type_signature(full_signature, keep_last_arg, CHECK_NULL);
466 if (TraceMethodHandles) {
467 ResourceMark rm(THREAD);
468 tty->print_cr("lookup_polymorphic_method %s %s => basic %s",
469 name->as_C_string(),
470 full_signature->as_C_string(),
471 basic_signature->as_C_string());
472 }
473 Method* result = SystemDictionary::find_method_handle_intrinsic(iid,
474 basic_signature,
475 CHECK_NULL);
476 if (result != NULL) {
477 assert(result->is_method_handle_intrinsic(), "MH.invokeBasic or MH.linkTo* intrinsic");
478 assert(result->intrinsic_id() != vmIntrinsics::_invokeGeneric, "wrong place to find this");
479 assert(basic_signature == result->signature(), "predict the result signature");
480 if (TraceMethodHandles) {
481 ttyLocker ttyl;
482 tty->print("lookup_polymorphic_method => intrinsic ");
483 result->print_on(tty);
484 }
485 }
486 return result;
487 } else if (iid == vmIntrinsics::_invokeGeneric
488 && THREAD->can_call_java()
489 && appendix_result_or_null != NULL) {
490 // This is a method with type-checking semantics.
491 // We will ask Java code to spin an adapter method for it.
492 if (!MethodHandles::enabled()) {
493 // Make sure the Java part of the runtime has been booted up.
494 Klass* natives = SystemDictionary::MethodHandleNatives_klass();
495 if (natives == NULL || InstanceKlass::cast(natives)->is_not_initialized()) {
496 SystemDictionary::resolve_or_fail(vmSymbols::java_lang_invoke_MethodHandleNatives(),
497 Handle(),
498 Handle(),
499 true,
500 CHECK_NULL);
501 }
502 }
503
504 Handle appendix;
505 Handle method_type;
506 Method* result = SystemDictionary::find_method_handle_invoker(
507 klass,
508 name,
509 full_signature,
510 link_info.current_klass(),
511 &appendix,
512 CHECK_NULL);
513 if (TraceMethodHandles) {
514 ttyLocker ttyl;
515 tty->print("lookup_polymorphic_method => (via Java) ");
516 result->print_on(tty);
517 tty->print(" lookup_polymorphic_method => appendix = ");
518 if (appendix.is_null()) tty->print_cr("(none)");
519 else appendix->print_on(tty);
520 }
521 if (result != NULL) {
522 #ifdef ASSERT
523 ResourceMark rm(THREAD);
524
525 TempNewSymbol basic_signature =
526 MethodHandles::lookup_basic_type_signature(full_signature, CHECK_NULL);
527 int actual_size_of_params = result->size_of_parameters();
528 int expected_size_of_params = ArgumentSizeComputer(basic_signature).size();
529 // +1 for MethodHandle.this, +1 for trailing MethodType
530 if (!MethodHandles::is_signature_polymorphic_static(iid)) expected_size_of_params += 1;
531 if (appendix.not_null()) expected_size_of_params += 1;
532 if (actual_size_of_params != expected_size_of_params) {
533 tty->print_cr("*** basic_signature=%s", basic_signature->as_C_string());
534 tty->print_cr("*** result for %s: ", vmIntrinsics::name_at(iid));
535 result->print();
536 }
537 assert(actual_size_of_params == expected_size_of_params,
538 "%d != %d", actual_size_of_params, expected_size_of_params);
539 #endif //ASSERT
540
541 assert(appendix_result_or_null != NULL, "");
584 if (!can_access) {
585 ResourceMark rm(THREAD);
586 bool same_module = (sel_klass->module() == ref_klass->module());
587 Exceptions::fthrow(
588 THREAD_AND_LOCATION,
589 vmSymbols::java_lang_IllegalAccessError(),
590 "class %s tried to access %s%s%smethod '%s' (%s%s%s)",
591 ref_klass->external_name(),
592 sel_method->is_abstract() ? "abstract " : "",
593 sel_method->is_protected() ? "protected " : "",
594 sel_method->is_private() ? "private " : "",
595 sel_method->external_name(),
596 (same_module) ? ref_klass->joint_in_module_of_loader(sel_klass) : ref_klass->class_in_module_of_loader(),
597 (same_module) ? "" : "; ",
598 (same_module) ? "" : sel_klass->class_in_module_of_loader()
599 );
600 return;
601 }
602 }
603
604 Method* LinkResolver::resolve_method_statically(Bytecodes::Code code,
605 const constantPoolHandle& pool, int index, TRAPS) {
606 // This method is used only
607 // (1) in C2 from InlineTree::ok_to_inline (via ciMethod::check_call),
608 // and
609 // (2) in Bytecode_invoke::static_target
610 // It appears to fail when applied to an invokeinterface call site.
611 // FIXME: Remove this method and ciMethod::check_call; refactor to use the other LinkResolver entry points.
612 // resolve klass
613 if (code == Bytecodes::_invokedynamic) {
614 Klass* resolved_klass = SystemDictionary::MethodHandle_klass();
615 Symbol* method_name = vmSymbols::invoke_name();
616 Symbol* method_signature = pool->signature_ref_at(index);
617 Klass* current_klass = pool->pool_holder();
618 LinkInfo link_info(resolved_klass, method_name, method_signature, current_klass);
619 return resolve_method(link_info, code, THREAD);
620 }
621
622 LinkInfo link_info(pool, index, methodHandle(), CHECK_NULL);
623 Klass* resolved_klass = link_info.resolved_klass();
624
625 if (pool->has_preresolution()
626 || (resolved_klass == SystemDictionary::MethodHandle_klass() &&
627 MethodHandles::is_signature_polymorphic_name(resolved_klass, link_info.name()))) {
628 Method* result = ConstantPool::method_at_if_loaded(pool, index);
629 if (result != NULL) {
630 return result;
631 }
632 }
633
634 if (code == Bytecodes::_invokeinterface) {
635 return resolve_interface_method(link_info, code, THREAD);
636 } else if (code == Bytecodes::_invokevirtual) {
637 return resolve_method(link_info, code, THREAD);
638 } else if (!resolved_klass->is_interface()) {
639 return resolve_method(link_info, code, THREAD);
640 } else {
641 return resolve_interface_method(link_info, code, THREAD);
642 }
643 }
644
645 // Check and print a loader constraint violation message for method or interface method
646 void LinkResolver::check_method_loader_constraints(const LinkInfo& link_info,
647 const methodHandle& resolved_method,
648 const char* method_type, TRAPS) {
649 Handle current_loader(THREAD, link_info.current_klass()->class_loader());
650 Handle resolved_loader(THREAD, resolved_method->method_holder()->class_loader());
695 const char* failed_type_name = failed_type_symbol->as_klass_external_name();
696
697 ss.print("loader constraint violation: when resolving field \"%s\" of type %s, "
698 "the class loader %s of the current class, %s, "
699 "and the class loader %s for the field's defining %s, %s, "
700 "have different Class objects for type %s (%s; %s)",
701 field->as_C_string(),
702 failed_type_name,
703 current_klass->class_loader_data()->loader_name_and_id(),
704 current_klass->external_name(),
705 sel_klass->class_loader_data()->loader_name_and_id(),
706 sel_klass->external_kind(),
707 sel_klass->external_name(),
708 failed_type_name,
709 current_klass->class_in_module_of_loader(false, true),
710 sel_klass->class_in_module_of_loader(false, true));
711 THROW_MSG(vmSymbols::java_lang_LinkageError(), ss.as_string());
712 }
713 }
714
715 Method* LinkResolver::resolve_method(const LinkInfo& link_info,
716 Bytecodes::Code code, TRAPS) {
717
718 Handle nested_exception;
719 Klass* resolved_klass = link_info.resolved_klass();
720
721 // 1. For invokevirtual, cannot call an interface method
722 if (code == Bytecodes::_invokevirtual && resolved_klass->is_interface()) {
723 ResourceMark rm(THREAD);
724 char buf[200];
725 jio_snprintf(buf, sizeof(buf), "Found interface %s, but class was expected",
726 resolved_klass->external_name());
727 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
728 }
729
730 // 2. check constant pool tag for called method - must be JVM_CONSTANT_Methodref
731 if (!link_info.tag().is_invalid() && !link_info.tag().is_method()) {
732 ResourceMark rm(THREAD);
733 stringStream ss;
734 ss.print("Method '");
735 Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
736 ss.print("' must be Methodref constant");
737 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
738 }
739
740 // 3. lookup method in resolved klass and its super klasses
741 methodHandle resolved_method(THREAD, lookup_method_in_klasses(link_info, true, false));
742
743 // 4. lookup method in all the interfaces implemented by the resolved klass
744 if (resolved_method.is_null() && !resolved_klass->is_array_klass()) { // not found in the class hierarchy
745 resolved_method = methodHandle(THREAD, lookup_method_in_interfaces(link_info));
746
747 if (resolved_method.is_null()) {
748 // JSR 292: see if this is an implicitly generated method MethodHandle.linkToVirtual(*...), etc
749 Method* method = lookup_polymorphic_method(link_info, (Handle*)NULL, THREAD);
750 resolved_method = methodHandle(THREAD, method);
751 if (HAS_PENDING_EXCEPTION) {
752 nested_exception = Handle(THREAD, PENDING_EXCEPTION);
753 CLEAR_PENDING_EXCEPTION;
754 }
755 }
756 }
757
758 // 5. method lookup failed
759 if (resolved_method.is_null()) {
760 ResourceMark rm(THREAD);
761 stringStream ss;
762 ss.print("'");
763 Method::print_external_name(&ss, resolved_klass, link_info.name(), link_info.signature());
764 ss.print("'");
765 THROW_MSG_CAUSE_(vmSymbols::java_lang_NoSuchMethodError(),
766 ss.as_string(), nested_exception, NULL);
767 }
768
769 // 6. access checks, access checking may be turned off when calling from within the VM.
770 Klass* current_klass = link_info.current_klass();
771 if (link_info.check_access()) {
772 assert(current_klass != NULL , "current_klass should not be null");
773
774 // check if method can be accessed by the referring class
775 check_method_accessability(current_klass,
776 resolved_klass,
777 resolved_method->method_holder(),
778 resolved_method,
779 CHECK_NULL);
780
781 // check loader constraints
782 check_method_loader_constraints(link_info, resolved_method, "method", CHECK_NULL);
783 }
784
785 return resolved_method();
786 }
787
788 static void trace_method_resolution(const char* prefix,
789 Klass* klass,
790 Klass* resolved_klass,
791 Method* method,
792 bool logitables,
793 int index = -1) {
794 #ifndef PRODUCT
795 ResourceMark rm;
796 Log(itables) logi;
797 LogStream lsi(logi.trace());
798 Log(vtables) logv;
799 LogStream lsv(logv.trace());
800 outputStream* st;
801 if (logitables) {
802 st = &lsi;
803 } else {
804 st = &lsv;
805 }
806 st->print("%s%s, compile-time-class:%s, method:%s, method_holder:%s, access_flags: ",
807 prefix,
808 (klass == NULL ? "<NULL>" : klass->internal_name()),
809 (resolved_klass == NULL ? "<NULL>" : resolved_klass->internal_name()),
810 Method::name_and_sig_as_C_string(resolved_klass,
811 method->name(),
812 method->signature()),
813 method->method_holder()->internal_name());
814 method->print_linkage_flags(st);
815 if (index != -1) {
816 st->print("vtable_index:%d", index);
817 }
818 st->cr();
819 #endif // PRODUCT
820 }
821
822 // Do linktime resolution of a method in the interface within the context of the specied bytecode.
823 Method* LinkResolver::resolve_interface_method(const LinkInfo& link_info, Bytecodes::Code code, TRAPS) {
824
825 Klass* resolved_klass = link_info.resolved_klass();
826
827 // check if klass is interface
828 if (!resolved_klass->is_interface()) {
829 ResourceMark rm(THREAD);
830 char buf[200];
831 jio_snprintf(buf, sizeof(buf), "Found class %s, but interface was expected", resolved_klass->external_name());
832 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
833 }
834
835 // check constant pool tag for called method - must be JVM_CONSTANT_InterfaceMethodref
836 if (!link_info.tag().is_invalid() && !link_info.tag().is_interface_method()) {
837 ResourceMark rm(THREAD);
838 stringStream ss;
839 ss.print("Method '");
840 Method::print_external_name(&ss, link_info.resolved_klass(), link_info.name(), link_info.signature());
841 ss.print("' must be InterfaceMethodref constant");
842 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
843 }
874 resolved_method,
875 CHECK_NULL);
876
877 check_method_loader_constraints(link_info, resolved_method, "interface method", CHECK_NULL);
878 }
879
880 if (code != Bytecodes::_invokestatic && resolved_method->is_static()) {
881 ResourceMark rm(THREAD);
882 stringStream ss;
883 ss.print("Expected instance not static method '");
884 Method::print_external_name(&ss, resolved_klass,
885 resolved_method->name(), resolved_method->signature());
886 ss.print("'");
887 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
888 }
889
890 if (log_develop_is_enabled(Trace, itables)) {
891 char buf[200];
892 jio_snprintf(buf, sizeof(buf), "%s resolved interface method: caller-class:",
893 Bytecodes::name(code));
894 trace_method_resolution(buf, link_info.current_klass(), resolved_klass, resolved_method(), true);
895 }
896
897 return resolved_method();
898 }
899
900 //------------------------------------------------------------------------------------------------------------------------
901 // Field resolution
902
903 void LinkResolver::check_field_accessability(Klass* ref_klass,
904 Klass* resolved_klass,
905 Klass* sel_klass,
906 const fieldDescriptor& fd,
907 TRAPS) {
908 bool can_access = Reflection::verify_member_access(ref_klass,
909 resolved_klass,
910 sel_klass,
911 fd.access_flags(),
912 true, false, CHECK);
913 // Any existing exceptions that may have been thrown, for example LinkageErrors
914 // from nest-host resolution, have been allowed to propagate.
915 if (!can_access) {
916 bool same_module = (sel_klass->module() == ref_klass->module());
917 ResourceMark rm(THREAD);
980 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string());
981 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg);
982 }
983
984 // A final field can be modified only
985 // (1) by methods declared in the class declaring the field and
986 // (2) by the <clinit> method (in case of a static field)
987 // or by the <init> method (in case of an instance field).
988 if (is_put && fd.access_flags().is_final()) {
989 ResourceMark rm(THREAD);
990 stringStream ss;
991
992 if (sel_klass != current_klass) {
993 ss.print("Update to %s final field %s.%s attempted from a different class (%s) than the field's declaring class",
994 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
995 current_klass->external_name());
996 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
997 }
998
999 if (fd.constants()->pool_holder()->major_version() >= 53) {
1000 Method* m = link_info.current_method();
1001 assert(m != NULL, "information about the current method must be available for 'put' bytecodes");
1002 bool is_initialized_static_final_update = (byte == Bytecodes::_putstatic &&
1003 fd.is_static() &&
1004 !m->is_static_initializer());
1005 bool is_initialized_instance_final_update = ((byte == Bytecodes::_putfield || byte == Bytecodes::_nofast_putfield) &&
1006 !fd.is_static() &&
1007 !m->is_object_initializer());
1008
1009 if (is_initialized_static_final_update || is_initialized_instance_final_update) {
1010 ss.print("Update to %s final field %s.%s attempted from a different method (%s) than the initializer method %s ",
1011 is_static ? "static" : "non-static", resolved_klass->external_name(), fd.name()->as_C_string(),
1012 m->name()->as_C_string(),
1013 is_static ? "<clinit>" : "<init>");
1014 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1015 }
1016 }
1017 }
1018
1019 // initialize resolved_klass if necessary
1020 // note 1: the klass which declared the field must be initialized (i.e, sel_klass)
1021 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99)
1022 //
1023 // note 2: we don't want to force initialization if we are just checking
1024 // if the field access is legal; e.g., during compilation
1025 if (is_static && initialize_class) {
1026 sel_klass->initialize(CHECK);
1027 }
1028 }
1029
1030 if ((sel_klass != current_klass) && (current_klass != NULL)) {
1031 check_field_loader_constraints(field, sig, current_klass, sel_klass, CHECK);
1032 }
1033
1034 // return information. note that the klass is set to the actual klass containing the
1035 // field, otherwise access of static fields in superclasses will not work.
1036 }
1037
1038
1039 //------------------------------------------------------------------------------------------------------------------------
1040 // Invoke resolution
1041 //
1042 // Naming conventions:
1043 //
1044 // resolved_method the specified method (i.e., static receiver specified via constant pool index)
1045 // sel_method the selected method (selected via run-time lookup; e.g., based on dynamic receiver class)
1046 // resolved_klass the specified klass (i.e., specified via constant pool index)
1047 // recv_klass the receiver klass
1048
1049
1050 void LinkResolver::resolve_static_call(CallInfo& result,
1051 const LinkInfo& link_info,
1052 bool initialize_class, TRAPS) {
1053 Method* resolved_method = linktime_resolve_static_method(link_info, CHECK);
1054
1055 // The resolved class can change as a result of this resolution.
1056 Klass* resolved_klass = resolved_method->method_holder();
1057
1058 // Initialize klass (this should only happen if everything is ok)
1059 if (initialize_class && resolved_klass->should_be_initialized()) {
1060 resolved_klass->initialize(CHECK);
1061 // Use updated LinkInfo to reresolve with resolved method holder
1062 LinkInfo new_info(resolved_klass, link_info.name(), link_info.signature(),
1063 link_info.current_klass(),
1064 link_info.check_access() ? LinkInfo::needs_access_check : LinkInfo::skip_access_check);
1065 resolved_method = linktime_resolve_static_method(new_info, CHECK);
1066 }
1067
1068 // setup result
1069 result.set_static(resolved_klass, methodHandle(THREAD, resolved_method), CHECK);
1070 }
1071
1072 // throws linktime exceptions
1073 Method* LinkResolver::linktime_resolve_static_method(const LinkInfo& link_info, TRAPS) {
1074
1075 Klass* resolved_klass = link_info.resolved_klass();
1076 Method* resolved_method;
1077 if (!resolved_klass->is_interface()) {
1078 resolved_method = resolve_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1079 } else {
1080 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokestatic, CHECK_NULL);
1081 }
1082 assert(resolved_method->name() != vmSymbols::class_initializer_name(), "should have been checked in verifier");
1083
1084 // check if static
1085 if (!resolved_method->is_static()) {
1086 ResourceMark rm(THREAD);
1087 stringStream ss;
1088 ss.print("Expected static method '");
1089 resolved_method->print_external_name(&ss);
1090 ss.print("'");
1091 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1092 }
1093 return resolved_method;
1094 }
1095
1096
1097 void LinkResolver::resolve_special_call(CallInfo& result,
1098 Handle recv,
1099 const LinkInfo& link_info,
1100 TRAPS) {
1101 Method* resolved_method = linktime_resolve_special_method(link_info, CHECK);
1102 runtime_resolve_special_method(result, link_info, methodHandle(THREAD, resolved_method), recv, CHECK);
1103 }
1104
1105 // throws linktime exceptions
1106 Method* LinkResolver::linktime_resolve_special_method(const LinkInfo& link_info, TRAPS) {
1107
1108 // Invokespecial is called for multiple special reasons:
1109 // <init>
1110 // local private method invocation, for classes and interfaces
1111 // superclass.method, which can also resolve to a default method
1112 // and the selected method is recalculated relative to the direct superclass
1113 // superinterface.method, which explicitly does not check shadowing
1114 Klass* resolved_klass = link_info.resolved_klass();
1115 Method* resolved_method;
1116
1117 if (!resolved_klass->is_interface()) {
1118 resolved_method = resolve_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1119 } else {
1120 resolved_method = resolve_interface_method(link_info, Bytecodes::_invokespecial, CHECK_NULL);
1121 }
1122
1123 // check if method name is <init>, that it is found in same klass as static type
1124 if (resolved_method->name() == vmSymbols::object_initializer_name() &&
1125 resolved_method->method_holder() != resolved_klass) {
1126 ResourceMark rm(THREAD);
1127 stringStream ss;
1128 ss.print("%s: method '", resolved_klass->external_name());
1129 resolved_method->signature()->print_as_signature_external_return_type(&ss);
1130 ss.print(" %s(", resolved_method->name()->as_C_string());
1131 resolved_method->signature()->print_as_signature_external_parameters(&ss);
1132 ss.print(")' not found");
1133 Exceptions::fthrow(
1134 THREAD_AND_LOCATION,
1135 vmSymbols::java_lang_NoSuchMethodError(),
1190 // resolved method is selected method unless we have an old-style lookup
1191 // for a superclass method
1192 // Invokespecial for a superinterface, resolved method is selected method,
1193 // no checks for shadowing
1194 methodHandle sel_method(THREAD, resolved_method());
1195
1196 if (link_info.check_access() &&
1197 // check if the method is not <init>
1198 resolved_method->name() != vmSymbols::object_initializer_name()) {
1199
1200 Klass* current_klass = link_info.current_klass();
1201
1202 // Check if the class of the resolved_klass is a superclass
1203 // (not supertype in order to exclude interface classes) of the current class.
1204 // This check is not performed for super.invoke for interface methods
1205 // in super interfaces.
1206 if (current_klass->is_subclass_of(resolved_klass) &&
1207 current_klass != resolved_klass) {
1208 // Lookup super method
1209 Klass* super_klass = current_klass->super();
1210 Method* instance_method = lookup_instance_method_in_klasses(super_klass,
1211 resolved_method->name(),
1212 resolved_method->signature(),
1213 Klass::find_private, CHECK);
1214 sel_method = methodHandle(THREAD, instance_method);
1215
1216 // check if found
1217 if (sel_method.is_null()) {
1218 ResourceMark rm(THREAD);
1219 stringStream ss;
1220 ss.print("'");
1221 resolved_method->print_external_name(&ss);
1222 ss.print("'");
1223 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1224 // check loader constraints if found a different method
1225 } else if (sel_method() != resolved_method()) {
1226 check_method_loader_constraints(link_info, sel_method, "method", CHECK);
1227 }
1228 }
1229
1230 // Check that the class of objectref (the receiver) is the current class or interface,
1231 // or a subtype of the current class or interface (the sender), otherwise invokespecial
1232 // throws IllegalAccessError.
1233 // The verifier checks that the sender is a subtype of the class in the I/MR operand.
1234 // The verifier also checks that the receiver is a subtype of the sender, if the sender is
1235 // a class. If the sender is an interface, the check has to be performed at runtime.
1254 ResourceMark rm(THREAD);
1255 stringStream ss;
1256 ss.print("Expecting non-static method '");
1257 resolved_method->print_external_name(&ss);
1258 ss.print("'");
1259 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1260 }
1261
1262 // check if abstract
1263 if (sel_method->is_abstract()) {
1264 ResourceMark rm(THREAD);
1265 stringStream ss;
1266 ss.print("'");
1267 Method::print_external_name(&ss, resolved_klass, sel_method->name(), sel_method->signature());
1268 ss.print("'");
1269 THROW_MSG(vmSymbols::java_lang_AbstractMethodError(), ss.as_string());
1270 }
1271
1272 if (log_develop_is_enabled(Trace, itables)) {
1273 trace_method_resolution("invokespecial selected method: resolved-class:",
1274 resolved_klass, resolved_klass, sel_method(), true);
1275 }
1276
1277 // setup result
1278 result.set_static(resolved_klass, sel_method, CHECK);
1279 }
1280
1281 void LinkResolver::resolve_virtual_call(CallInfo& result, Handle recv, Klass* receiver_klass,
1282 const LinkInfo& link_info,
1283 bool check_null_and_abstract, TRAPS) {
1284 Method* resolved_method = linktime_resolve_virtual_method(link_info, CHECK);
1285 runtime_resolve_virtual_method(result, methodHandle(THREAD, resolved_method),
1286 link_info.resolved_klass(),
1287 recv, receiver_klass,
1288 check_null_and_abstract, CHECK);
1289 }
1290
1291 // throws linktime exceptions
1292 Method* LinkResolver::linktime_resolve_virtual_method(const LinkInfo& link_info,
1293 TRAPS) {
1294 // normal method resolution
1295 Method* resolved_method = resolve_method(link_info, Bytecodes::_invokevirtual, CHECK_NULL);
1296
1297 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1298 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1299
1300 // check if private interface method
1301 Klass* resolved_klass = link_info.resolved_klass();
1302 Klass* current_klass = link_info.current_klass();
1303
1304 // This is impossible, if resolve_klass is an interface, we've thrown icce in resolve_method
1305 if (resolved_klass->is_interface() && resolved_method->is_private()) {
1306 ResourceMark rm(THREAD);
1307 stringStream ss;
1308 ss.print("private interface method requires invokespecial, not invokevirtual: method '");
1309 resolved_method->print_external_name(&ss);
1310 ss.print("', caller-class: %s",
1311 (current_klass == NULL ? "<null>" : current_klass->internal_name()));
1312 THROW_MSG_NULL(vmSymbols::java_lang_IncompatibleClassChangeError(), ss.as_string());
1313 }
1314
1315 // check if not static
1374 assert(resolved_method->can_be_statically_bound(), "cannot override this method");
1375 selected_method = resolved_method;
1376 } else {
1377 selected_method = methodHandle(THREAD, recv_klass->method_at_vtable(vtable_index));
1378 }
1379 }
1380
1381 // check if method exists
1382 if (selected_method.is_null()) {
1383 throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1384 }
1385
1386 // check if abstract
1387 if (check_null_and_abstract && selected_method->is_abstract()) {
1388 // Pass arguments for generating a verbose error message.
1389 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1390 }
1391
1392 if (log_develop_is_enabled(Trace, vtables)) {
1393 trace_method_resolution("invokevirtual selected method: receiver-class:",
1394 recv_klass, resolved_klass, selected_method(),
1395 false, vtable_index);
1396 }
1397 // setup result
1398 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1399 }
1400
1401 void LinkResolver::resolve_interface_call(CallInfo& result, Handle recv, Klass* recv_klass,
1402 const LinkInfo& link_info,
1403 bool check_null_and_abstract, TRAPS) {
1404 // throws linktime exceptions
1405 Method* resolved_method = linktime_resolve_interface_method(link_info, CHECK);
1406 methodHandle mh(THREAD, resolved_method);
1407 runtime_resolve_interface_method(result, mh, link_info.resolved_klass(),
1408 recv, recv_klass, check_null_and_abstract, CHECK);
1409 }
1410
1411 Method* LinkResolver::linktime_resolve_interface_method(const LinkInfo& link_info,
1412 TRAPS) {
1413 // normal interface method resolution
1414 Method* resolved_method = resolve_interface_method(link_info, Bytecodes::_invokeinterface, CHECK_NULL);
1415 assert(resolved_method->name() != vmSymbols::object_initializer_name(), "should have been checked in verifier");
1416 assert(resolved_method->name() != vmSymbols::class_initializer_name (), "should have been checked in verifier");
1417
1418 return resolved_method;
1419 }
1420
1421 // throws runtime exceptions
1422 void LinkResolver::runtime_resolve_interface_method(CallInfo& result,
1423 const methodHandle& resolved_method,
1424 Klass* resolved_klass,
1425 Handle recv,
1426 Klass* recv_klass,
1427 bool check_null_and_abstract, TRAPS) {
1428
1429 // check if receiver exists
1430 if (check_null_and_abstract && recv.is_null()) {
1431 THROW(vmSymbols::java_lang_NullPointerException());
1432 }
1433
1434 // check if receiver klass implements the resolved interface
1435 if (!recv_klass->is_subtype_of(resolved_klass)) {
1436 ResourceMark rm(THREAD);
1437 char buf[200];
1438 jio_snprintf(buf, sizeof(buf), "Class %s does not implement the requested interface %s",
1439 recv_klass->external_name(),
1440 resolved_klass->external_name());
1441 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), buf);
1442 }
1443
1444 methodHandle selected_method = resolved_method;
1445
1446 // resolve the method in the receiver class, unless it is private
1447 if (!resolved_method()->is_private()) {
1448 // do lookup based on receiver klass
1449 // This search must match the linktime preparation search for itable initialization
1450 // to correctly enforce loader constraints for interface method inheritance.
1451 // Private methods are skipped as the resolved method was not private.
1452 Method* method = lookup_instance_method_in_klasses(recv_klass,
1453 resolved_method->name(),
1454 resolved_method->signature(),
1455 Klass::skip_private, CHECK);
1456 selected_method = methodHandle(THREAD, method);
1457
1458 if (selected_method.is_null() && !check_null_and_abstract) {
1459 // In theory this is a harmless placeholder value, but
1460 // in practice leaving in null affects the nsk default method tests.
1461 // This needs further study.
1462 selected_method = resolved_method;
1463 }
1464 // check if method exists
1465 if (selected_method.is_null()) {
1466 // Pass arguments for generating a verbose error message.
1467 throw_abstract_method_error(resolved_method, recv_klass, CHECK);
1468 }
1469 // check access
1470 // Throw Illegal Access Error if selected_method is not public.
1471 if (!selected_method->is_public()) {
1472 ResourceMark rm(THREAD);
1473 stringStream ss;
1474 ss.print("'");
1475 Method::print_external_name(&ss, recv_klass, selected_method->name(), selected_method->signature());
1476 ss.print("'");
1477 THROW_MSG(vmSymbols::java_lang_IllegalAccessError(), ss.as_string());
1478 }
1479 // check if abstract
1480 if (check_null_and_abstract && selected_method->is_abstract()) {
1481 throw_abstract_method_error(resolved_method, selected_method, recv_klass, CHECK);
1482 }
1483 }
1484
1485 if (log_develop_is_enabled(Trace, itables)) {
1486 trace_method_resolution("invokeinterface selected method: receiver-class:",
1487 recv_klass, resolved_klass, selected_method(), true);
1488 }
1489 // setup result
1490 if (resolved_method->has_vtable_index()) {
1491 int vtable_index = resolved_method->vtable_index();
1492 log_develop_trace(itables)(" -- vtable index: %d", vtable_index);
1493 assert(vtable_index == selected_method->vtable_index(), "sanity check");
1494 result.set_virtual(resolved_klass, recv_klass, resolved_method, selected_method, vtable_index, CHECK);
1495 } else if (resolved_method->has_itable_index()) {
1496 int itable_index = resolved_method()->itable_index();
1497 log_develop_trace(itables)(" -- itable index: %d", itable_index);
1498 result.set_interface(resolved_klass, recv_klass, resolved_method, selected_method, itable_index, CHECK);
1499 } else {
1500 int index = resolved_method->vtable_index();
1501 log_develop_trace(itables)(" -- non itable/vtable index: %d", index);
1502 assert(index == Method::nonvirtual_vtable_index, "Oops hit another case!");
1503 assert(resolved_method()->is_private() ||
1504 (resolved_method()->is_final() && resolved_method->method_holder() == SystemDictionary::Object_klass()),
1505 "Should only have non-virtual invokeinterface for private or final-Object methods!");
1506 assert(resolved_method()->can_be_statically_bound(), "Should only have non-virtual invokeinterface for statically bound methods!");
1507 // This sets up the nonvirtual form of "virtual" call (as needed for final and private methods)
1508 result.set_virtual(resolved_klass, resolved_klass, resolved_method, resolved_method, index, CHECK);
1509 }
1510 }
1511
1512
1513 Method* LinkResolver::linktime_resolve_interface_method_or_null(
1514 const LinkInfo& link_info) {
1515 EXCEPTION_MARK;
1516 Method* method_result = linktime_resolve_interface_method(link_info, THREAD);
1517 if (HAS_PENDING_EXCEPTION) {
1518 CLEAR_PENDING_EXCEPTION;
1519 return NULL;
1520 } else {
1521 return method_result;
1522 }
1523 }
1524
1525 Method* LinkResolver::linktime_resolve_virtual_method_or_null(
1526 const LinkInfo& link_info) {
1527 EXCEPTION_MARK;
1528 Method* method_result = linktime_resolve_virtual_method(link_info, THREAD);
1529 if (HAS_PENDING_EXCEPTION) {
1530 CLEAR_PENDING_EXCEPTION;
1531 return NULL;
1532 } else {
1533 return method_result;
1534 }
1535 }
1536
1537 Method* LinkResolver::resolve_virtual_call_or_null(
1538 Klass* receiver_klass,
1539 const LinkInfo& link_info) {
1540 EXCEPTION_MARK;
1541 CallInfo info;
1542 resolve_virtual_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1543 if (HAS_PENDING_EXCEPTION) {
1544 CLEAR_PENDING_EXCEPTION;
1545 return NULL;
1546 }
1547 return info.selected_method();
1548 }
1549
1550 Method* LinkResolver::resolve_interface_call_or_null(
1551 Klass* receiver_klass,
1552 const LinkInfo& link_info) {
1553 EXCEPTION_MARK;
1554 CallInfo info;
1555 resolve_interface_call(info, Handle(), receiver_klass, link_info, false, THREAD);
1556 if (HAS_PENDING_EXCEPTION) {
1557 CLEAR_PENDING_EXCEPTION;
1558 return NULL;
1559 }
1560 return info.selected_method();
1561 }
1562
1563 int LinkResolver::resolve_virtual_vtable_index(Klass* receiver_klass,
1564 const LinkInfo& link_info) {
1565 EXCEPTION_MARK;
1566 CallInfo info;
1567 resolve_virtual_call(info, Handle(), receiver_klass, link_info,
1568 /*check_null_or_abstract*/false, THREAD);
1569 if (HAS_PENDING_EXCEPTION) {
1570 CLEAR_PENDING_EXCEPTION;
1571 return Method::invalid_vtable_index;
1572 }
1573 return info.vtable_index();
1574 }
1575
1576 Method* LinkResolver::resolve_static_call_or_null(const LinkInfo& link_info) {
1577 EXCEPTION_MARK;
1578 CallInfo info;
1579 resolve_static_call(info, link_info, /*initialize_class*/false, THREAD);
1580 if (HAS_PENDING_EXCEPTION) {
1581 CLEAR_PENDING_EXCEPTION;
1582 return NULL;
1583 }
1584 return info.selected_method();
1585 }
1586
1587 Method* LinkResolver::resolve_special_call_or_null(const LinkInfo& link_info) {
1588 EXCEPTION_MARK;
1589 CallInfo info;
1590 resolve_special_call(info, Handle(), link_info, THREAD);
1591 if (HAS_PENDING_EXCEPTION) {
1592 CLEAR_PENDING_EXCEPTION;
1593 return NULL;
1594 }
1595 return info.selected_method();
1596 }
1597
1598
1599
1600 //------------------------------------------------------------------------------------------------------------------------
1601 // ConstantPool entries
1602
1603 void LinkResolver::resolve_invoke(CallInfo& result, Handle recv, const constantPoolHandle& pool, int index, Bytecodes::Code byte, TRAPS) {
1604 switch (byte) {
1605 case Bytecodes::_invokestatic : resolve_invokestatic (result, pool, index, CHECK); break;
1606 case Bytecodes::_invokespecial : resolve_invokespecial (result, recv, pool, index, CHECK); break;
1607 case Bytecodes::_invokevirtual : resolve_invokevirtual (result, recv, pool, index, CHECK); break;
1608 case Bytecodes::_invokehandle : resolve_invokehandle (result, pool, index, CHECK); break;
1609 case Bytecodes::_invokedynamic : resolve_invokedynamic (result, pool, index, CHECK); break;
1610 case Bytecodes::_invokeinterface: resolve_invokeinterface(result, recv, pool, index, CHECK); break;
1611 default : break;
1612 }
1613 return;
1674 void LinkResolver::resolve_invokehandle(CallInfo& result, const constantPoolHandle& pool, int index, TRAPS) {
1675 // This guy is reached from InterpreterRuntime::resolve_invokehandle.
1676 LinkInfo link_info(pool, index, CHECK);
1677 if (TraceMethodHandles) {
1678 ResourceMark rm(THREAD);
1679 tty->print_cr("resolve_invokehandle %s %s", link_info.name()->as_C_string(),
1680 link_info.signature()->as_C_string());
1681 }
1682 resolve_handle_call(result, link_info, CHECK);
1683 }
1684
1685 void LinkResolver::resolve_handle_call(CallInfo& result,
1686 const LinkInfo& link_info,
1687 TRAPS) {
1688 // JSR 292: this must be an implicitly generated method MethodHandle.invokeExact(*...) or similar
1689 Klass* resolved_klass = link_info.resolved_klass();
1690 assert(resolved_klass == SystemDictionary::MethodHandle_klass() ||
1691 resolved_klass == SystemDictionary::VarHandle_klass(), "");
1692 assert(MethodHandles::is_signature_polymorphic_name(link_info.name()), "");
1693 Handle resolved_appendix;
1694 Method* resolved_method = lookup_polymorphic_method(link_info, &resolved_appendix, CHECK);
1695 result.set_handle(resolved_klass, methodHandle(THREAD, resolved_method), resolved_appendix, CHECK);
1696 }
1697
1698 void LinkResolver::resolve_invokedynamic(CallInfo& result, const constantPoolHandle& pool, int indy_index, TRAPS) {
1699 ConstantPoolCacheEntry* cpce = pool->invokedynamic_cp_cache_entry_at(indy_index);
1700 int pool_index = cpce->constant_pool_index();
1701
1702 // Resolve the bootstrap specifier (BSM + optional arguments).
1703 BootstrapInfo bootstrap_specifier(pool, pool_index, indy_index);
1704
1705 // Check if CallSite has been bound already or failed already, and short circuit:
1706 {
1707 bool is_done = bootstrap_specifier.resolve_previously_linked_invokedynamic(result, CHECK);
1708 if (is_done) return;
1709 }
1710
1711 // The initial step in Call Site Specifier Resolution is to resolve the symbolic
1712 // reference to a method handle which will be the bootstrap method for a dynamic
1713 // call site. If resolution for the java.lang.invoke.MethodHandle for the bootstrap
1714 // method fails, then a MethodHandleInError is stored at the corresponding bootstrap
1715 // method's CP index for the CONSTANT_MethodHandle_info. So, there is no need to
|