44 #include "runtime/reflection.hpp" |
44 #include "runtime/reflection.hpp" |
45 #include "runtime/signature.hpp" |
45 #include "runtime/signature.hpp" |
46 #include "runtime/thread.inline.hpp" |
46 #include "runtime/thread.inline.hpp" |
47 #include "runtime/vmThread.hpp" |
47 #include "runtime/vmThread.hpp" |
48 |
48 |
49 //------------------------------------------------------------------------------------------------------------------------ |
|
50 // Implementation of FieldAccessInfo |
|
51 |
|
52 void FieldAccessInfo::set(KlassHandle klass, Symbol* name, int field_index, int field_offset, |
|
53 BasicType field_type, AccessFlags access_flags) { |
|
54 _klass = klass; |
|
55 _name = name; |
|
56 _field_index = field_index; |
|
57 _field_offset = field_offset; |
|
58 _field_type = field_type; |
|
59 _access_flags = access_flags; |
|
60 } |
|
61 |
|
62 |
49 |
63 //------------------------------------------------------------------------------------------------------------------------ |
50 //------------------------------------------------------------------------------------------------------------------------ |
64 // Implementation of CallInfo |
51 // Implementation of CallInfo |
65 |
52 |
66 |
53 |
67 void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) { |
54 void CallInfo::set_static(KlassHandle resolved_klass, methodHandle resolved_method, TRAPS) { |
68 int vtable_index = Method::nonvirtual_vtable_index; |
55 int vtable_index = Method::nonvirtual_vtable_index; |
69 set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, vtable_index, CHECK); |
56 set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); |
70 } |
57 } |
71 |
58 |
72 |
59 |
73 void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, TRAPS) { |
60 void CallInfo::set_interface(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int itable_index, TRAPS) { |
74 // This is only called for interface methods. If the resolved_method |
61 // This is only called for interface methods. If the resolved_method |
75 // comes from java/lang/Object, it can be the subject of a virtual call, so |
62 // comes from java/lang/Object, it can be the subject of a virtual call, so |
76 // we should pick the vtable index from the resolved method. |
63 // we should pick the vtable index from the resolved method. |
77 // Other than that case, there is no valid vtable index to specify. |
64 // In that case, the caller must call set_virtual instead of set_interface. |
78 int vtable_index = Method::invalid_vtable_index; |
65 assert(resolved_method->method_holder()->is_interface(), ""); |
79 if (resolved_method->method_holder() == SystemDictionary::Object_klass()) { |
66 assert(itable_index == resolved_method()->itable_index(), ""); |
80 assert(resolved_method->vtable_index() == selected_method->vtable_index(), "sanity check"); |
67 set_common(resolved_klass, selected_klass, resolved_method, selected_method, CallInfo::itable_call, itable_index, CHECK); |
81 vtable_index = resolved_method->vtable_index(); |
|
82 } |
|
83 set_common(resolved_klass, selected_klass, resolved_method, selected_method, vtable_index, CHECK); |
|
84 } |
68 } |
85 |
69 |
86 void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) { |
70 void CallInfo::set_virtual(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) { |
87 assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index"); |
71 assert(vtable_index >= 0 || vtable_index == Method::nonvirtual_vtable_index, "valid index"); |
88 set_common(resolved_klass, selected_klass, resolved_method, selected_method, vtable_index, CHECK); |
72 assert(vtable_index < 0 || !resolved_method->has_vtable_index() || vtable_index == resolved_method->vtable_index(), ""); |
|
73 CallKind kind = (vtable_index >= 0 && !resolved_method->can_be_statically_bound() ? CallInfo::vtable_call : CallInfo::direct_call); |
|
74 set_common(resolved_klass, selected_klass, resolved_method, selected_method, kind, vtable_index, CHECK); |
89 assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call"); |
75 assert(!resolved_method->is_compiled_lambda_form(), "these must be handled via an invokehandle call"); |
90 } |
76 } |
91 |
77 |
92 void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS) { |
78 void CallInfo::set_handle(methodHandle resolved_method, Handle resolved_appendix, Handle resolved_method_type, TRAPS) { |
93 if (resolved_method.is_null()) { |
79 if (resolved_method.is_null()) { |
96 KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass(); |
82 KlassHandle resolved_klass = SystemDictionary::MethodHandle_klass(); |
97 assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic || |
83 assert(resolved_method->intrinsic_id() == vmIntrinsics::_invokeBasic || |
98 resolved_method->is_compiled_lambda_form(), |
84 resolved_method->is_compiled_lambda_form(), |
99 "linkMethod must return one of these"); |
85 "linkMethod must return one of these"); |
100 int vtable_index = Method::nonvirtual_vtable_index; |
86 int vtable_index = Method::nonvirtual_vtable_index; |
101 assert(resolved_method->vtable_index() == vtable_index, ""); |
87 assert(!resolved_method->has_vtable_index(), ""); |
102 set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, vtable_index, CHECK); |
88 set_common(resolved_klass, resolved_klass, resolved_method, resolved_method, CallInfo::direct_call, vtable_index, CHECK); |
103 _resolved_appendix = resolved_appendix; |
89 _resolved_appendix = resolved_appendix; |
104 _resolved_method_type = resolved_method_type; |
90 _resolved_method_type = resolved_method_type; |
105 } |
91 } |
106 |
92 |
107 void CallInfo::set_common(KlassHandle resolved_klass, KlassHandle selected_klass, methodHandle resolved_method, methodHandle selected_method, int vtable_index, TRAPS) { |
93 void CallInfo::set_common(KlassHandle resolved_klass, |
|
94 KlassHandle selected_klass, |
|
95 methodHandle resolved_method, |
|
96 methodHandle selected_method, |
|
97 CallKind kind, |
|
98 int index, |
|
99 TRAPS) { |
108 assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond"); |
100 assert(resolved_method->signature() == selected_method->signature(), "signatures must correspond"); |
109 _resolved_klass = resolved_klass; |
101 _resolved_klass = resolved_klass; |
110 _selected_klass = selected_klass; |
102 _selected_klass = selected_klass; |
111 _resolved_method = resolved_method; |
103 _resolved_method = resolved_method; |
112 _selected_method = selected_method; |
104 _selected_method = selected_method; |
113 _vtable_index = vtable_index; |
105 _call_kind = kind; |
|
106 _call_index = index; |
114 _resolved_appendix = Handle(); |
107 _resolved_appendix = Handle(); |
|
108 DEBUG_ONLY(verify()); // verify before making side effects |
|
109 |
115 if (CompilationPolicy::must_be_compiled(selected_method)) { |
110 if (CompilationPolicy::must_be_compiled(selected_method)) { |
116 // This path is unusual, mostly used by the '-Xcomp' stress test mode. |
111 // This path is unusual, mostly used by the '-Xcomp' stress test mode. |
117 |
112 |
118 // Note: with several active threads, the must_be_compiled may be true |
113 // Note: with several active threads, the must_be_compiled may be true |
119 // while can_be_compiled is false; remove assert |
114 // while can_be_compiled is false; remove assert |
136 CompilationPolicy::policy()->initial_compile_level(), |
131 CompilationPolicy::policy()->initial_compile_level(), |
137 methodHandle(), 0, "must_be_compiled", CHECK); |
132 methodHandle(), 0, "must_be_compiled", CHECK); |
138 } |
133 } |
139 } |
134 } |
140 |
135 |
|
136 // utility query for unreflecting a method |
|
137 CallInfo::CallInfo(Method* resolved_method, Klass* resolved_klass) { |
|
138 Klass* resolved_method_holder = resolved_method->method_holder(); |
|
139 if (resolved_klass == NULL) { // 2nd argument defaults to holder of 1st |
|
140 resolved_klass = resolved_method_holder; |
|
141 } |
|
142 _resolved_klass = resolved_klass; |
|
143 _selected_klass = resolved_klass; |
|
144 _resolved_method = resolved_method; |
|
145 _selected_method = resolved_method; |
|
146 // classify: |
|
147 CallKind kind = CallInfo::unknown_kind; |
|
148 int index = resolved_method->vtable_index(); |
|
149 if (resolved_method->can_be_statically_bound()) { |
|
150 kind = CallInfo::direct_call; |
|
151 } else if (!resolved_method_holder->is_interface()) { |
|
152 // Could be an Object method inherited into an interface, but still a vtable call. |
|
153 kind = CallInfo::vtable_call; |
|
154 } else if (!resolved_klass->is_interface()) { |
|
155 // A miranda method. Compute the vtable index. |
|
156 ResourceMark rm; |
|
157 klassVtable* vt = InstanceKlass::cast(resolved_klass)->vtable(); |
|
158 index = vt->index_of_miranda(resolved_method->name(), |
|
159 resolved_method->signature()); |
|
160 kind = CallInfo::vtable_call; |
|
161 } else { |
|
162 // A regular interface call. |
|
163 kind = CallInfo::itable_call; |
|
164 index = resolved_method->itable_index(); |
|
165 } |
|
166 assert(index == Method::nonvirtual_vtable_index || index >= 0, err_msg("bad index %d", index)); |
|
167 _call_kind = kind; |
|
168 _call_index = index; |
|
169 _resolved_appendix = Handle(); |
|
170 DEBUG_ONLY(verify()); |
|
171 } |
|
172 |
|
173 #ifdef ASSERT |
|
174 void CallInfo::verify() { |
|
175 switch (call_kind()) { // the meaning and allowed value of index depends on kind |
|
176 case CallInfo::direct_call: |
|
177 if (_call_index == Method::nonvirtual_vtable_index) break; |
|
178 // else fall through to check vtable index: |
|
179 case CallInfo::vtable_call: |
|
180 assert(resolved_klass()->verify_vtable_index(_call_index), ""); |
|
181 break; |
|
182 case CallInfo::itable_call: |
|
183 assert(resolved_method()->method_holder()->verify_itable_index(_call_index), ""); |
|
184 break; |
|
185 case CallInfo::unknown_kind: |
|
186 assert(call_kind() != CallInfo::unknown_kind, "CallInfo must be set"); |
|
187 break; |
|
188 default: |
|
189 fatal(err_msg_res("Unexpected call kind %d", call_kind())); |
|
190 } |
|
191 } |
|
192 #endif //ASSERT |
|
193 |
|
194 |
141 |
195 |
142 //------------------------------------------------------------------------------------------------------------------------ |
196 //------------------------------------------------------------------------------------------------------------------------ |
143 // Klass resolution |
197 // Klass resolution |
144 |
198 |
145 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) { |
199 void LinkResolver::check_klass_accessability(KlassHandle ref_klass, KlassHandle sel_klass, TRAPS) { |
578 ); |
630 ); |
579 return; |
631 return; |
580 } |
632 } |
581 } |
633 } |
582 |
634 |
583 void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, TRAPS) { |
635 void LinkResolver::resolve_field_access(fieldDescriptor& result, constantPoolHandle pool, int index, Bytecodes::Code byte, TRAPS) { |
584 resolve_field(result, pool, index, byte, check_only, true, CHECK); |
|
585 } |
|
586 |
|
587 void LinkResolver::resolve_field(FieldAccessInfo& result, constantPoolHandle pool, int index, Bytecodes::Code byte, bool check_only, bool update_pool, TRAPS) { |
|
588 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || |
|
589 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield, "bad bytecode"); |
|
590 |
|
591 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); |
|
592 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic); |
|
593 |
|
594 // resolve specified klass |
|
595 KlassHandle resolved_klass; |
|
596 if (update_pool) { |
|
597 resolve_klass(resolved_klass, pool, index, CHECK); |
|
598 } else { |
|
599 resolve_klass_no_update(resolved_klass, pool, index, CHECK); |
|
600 } |
|
601 // Load these early in case the resolve of the containing klass fails |
636 // Load these early in case the resolve of the containing klass fails |
602 Symbol* field = pool->name_ref_at(index); |
637 Symbol* field = pool->name_ref_at(index); |
603 Symbol* sig = pool->signature_ref_at(index); |
638 Symbol* sig = pool->signature_ref_at(index); |
|
639 |
|
640 // resolve specified klass |
|
641 KlassHandle resolved_klass; |
|
642 resolve_klass(resolved_klass, pool, index, CHECK); |
|
643 |
|
644 KlassHandle current_klass(THREAD, pool->pool_holder()); |
|
645 resolve_field(result, resolved_klass, field, sig, current_klass, byte, true, true, CHECK); |
|
646 } |
|
647 |
|
648 void LinkResolver::resolve_field(fieldDescriptor& fd, KlassHandle resolved_klass, Symbol* field, Symbol* sig, |
|
649 KlassHandle current_klass, Bytecodes::Code byte, bool check_access, bool initialize_class, |
|
650 TRAPS) { |
|
651 assert(byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic || |
|
652 byte == Bytecodes::_getfield || byte == Bytecodes::_putfield || |
|
653 (byte == Bytecodes::_nop && !check_access), "bad field access bytecode"); |
|
654 |
|
655 bool is_static = (byte == Bytecodes::_getstatic || byte == Bytecodes::_putstatic); |
|
656 bool is_put = (byte == Bytecodes::_putfield || byte == Bytecodes::_putstatic); |
|
657 |
604 // Check if there's a resolved klass containing the field |
658 // Check if there's a resolved klass containing the field |
605 if( resolved_klass.is_null() ) { |
659 if (resolved_klass.is_null()) { |
606 ResourceMark rm(THREAD); |
660 ResourceMark rm(THREAD); |
607 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
661 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
608 } |
662 } |
609 |
663 |
610 // Resolve instance field |
664 // Resolve instance field |
611 fieldDescriptor fd; // find_field initializes fd if found |
|
612 KlassHandle sel_klass(THREAD, InstanceKlass::cast(resolved_klass())->find_field(field, sig, &fd)); |
665 KlassHandle sel_klass(THREAD, InstanceKlass::cast(resolved_klass())->find_field(field, sig, &fd)); |
613 // check if field exists; i.e., if a klass containing the field def has been selected |
666 // check if field exists; i.e., if a klass containing the field def has been selected |
614 if (sel_klass.is_null()){ |
667 if (sel_klass.is_null()) { |
615 ResourceMark rm(THREAD); |
668 ResourceMark rm(THREAD); |
616 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
669 THROW_MSG(vmSymbols::java_lang_NoSuchFieldError(), field->as_C_string()); |
617 } |
670 } |
618 |
671 |
|
672 if (!check_access) |
|
673 // Access checking may be turned off when calling from within the VM. |
|
674 return; |
|
675 |
619 // check access |
676 // check access |
620 KlassHandle ref_klass(THREAD, pool->pool_holder()); |
677 check_field_accessability(current_klass, resolved_klass, sel_klass, fd, CHECK); |
621 check_field_accessability(ref_klass, resolved_klass, sel_klass, fd, CHECK); |
|
622 |
678 |
623 // check for errors |
679 // check for errors |
624 if (is_static != fd.is_static()) { |
680 if (is_static != fd.is_static()) { |
625 ResourceMark rm(THREAD); |
681 ResourceMark rm(THREAD); |
626 char msg[200]; |
682 char msg[200]; |
627 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string()); |
683 jio_snprintf(msg, sizeof(msg), "Expected %s field %s.%s", is_static ? "static" : "non-static", resolved_klass()->external_name(), fd.name()->as_C_string()); |
628 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); |
684 THROW_MSG(vmSymbols::java_lang_IncompatibleClassChangeError(), msg); |
629 } |
685 } |
630 |
686 |
631 // Final fields can only be accessed from its own class. |
687 // Final fields can only be accessed from its own class. |
632 if (is_put && fd.access_flags().is_final() && sel_klass() != pool->pool_holder()) { |
688 if (is_put && fd.access_flags().is_final() && sel_klass() != current_klass()) { |
633 THROW(vmSymbols::java_lang_IllegalAccessError()); |
689 THROW(vmSymbols::java_lang_IllegalAccessError()); |
634 } |
690 } |
635 |
691 |
636 // initialize resolved_klass if necessary |
692 // initialize resolved_klass if necessary |
637 // note 1: the klass which declared the field must be initialized (i.e, sel_klass) |
693 // note 1: the klass which declared the field must be initialized (i.e, sel_klass) |
638 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) |
694 // according to the newest JVM spec (5.5, p.170) - was bug (gri 7/28/99) |
639 // |
695 // |
640 // note 2: we don't want to force initialization if we are just checking |
696 // note 2: we don't want to force initialization if we are just checking |
641 // if the field access is legal; e.g., during compilation |
697 // if the field access is legal; e.g., during compilation |
642 if (is_static && !check_only) { |
698 if (is_static && initialize_class) { |
643 sel_klass->initialize(CHECK); |
699 sel_klass->initialize(CHECK); |
644 } |
700 } |
645 |
701 |
646 { |
702 if (sel_klass() != current_klass()) { |
647 HandleMark hm(THREAD); |
703 HandleMark hm(THREAD); |
648 Handle ref_loader (THREAD, InstanceKlass::cast(ref_klass())->class_loader()); |
704 Handle ref_loader (THREAD, InstanceKlass::cast(current_klass())->class_loader()); |
649 Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader()); |
705 Handle sel_loader (THREAD, InstanceKlass::cast(sel_klass())->class_loader()); |
650 Symbol* signature_ref = pool->signature_ref_at(index); |
|
651 { |
706 { |
652 ResourceMark rm(THREAD); |
707 ResourceMark rm(THREAD); |
653 Symbol* failed_type_symbol = |
708 Symbol* failed_type_symbol = |
654 SystemDictionary::check_signature_loaders(signature_ref, |
709 SystemDictionary::check_signature_loaders(sig, |
655 ref_loader, sel_loader, |
710 ref_loader, sel_loader, |
656 false, |
711 false, |
657 CHECK); |
712 CHECK); |
658 if (failed_type_symbol != NULL) { |
713 if (failed_type_symbol != NULL) { |
659 const char* msg = "loader constraint violation: when resolving field" |
714 const char* msg = "loader constraint violation: when resolving field" |
905 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists |
957 if (check_null_and_abstract && recv.is_null()) { // check if receiver exists |
906 THROW(vmSymbols::java_lang_NullPointerException()); |
958 THROW(vmSymbols::java_lang_NullPointerException()); |
907 } |
959 } |
908 |
960 |
909 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s |
961 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s |
910 // has not been rewritten, and the vtable initialized. |
|
911 assert(resolved_method->method_holder()->is_linked(), "must be linked"); |
|
912 |
|
913 // Virtual methods cannot be resolved before its klass has been linked, for otherwise the Method*'s |
|
914 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since |
962 // has not been rewritten, and the vtable initialized. Make sure to do this after the nullcheck, since |
915 // a missing receiver might result in a bogus lookup. |
963 // a missing receiver might result in a bogus lookup. |
916 assert(resolved_method->method_holder()->is_linked(), "must be linked"); |
964 assert(resolved_method->method_holder()->is_linked(), "must be linked"); |
917 |
965 |
918 // do lookup based on receiver klass using the vtable index |
966 // do lookup based on receiver klass using the vtable index |
919 if (resolved_method->method_holder()->is_interface()) { // miranda method |
967 if (resolved_method->method_holder()->is_interface()) { // miranda method |
920 vtable_index = vtable_index_of_miranda_method(resolved_klass, |
968 vtable_index = vtable_index_of_miranda_method(resolved_klass, |
921 resolved_method->name(), |
969 resolved_method->name(), |
922 resolved_method->signature(), CHECK); |
970 resolved_method->signature(), CHECK); |
|
971 |
923 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); |
972 assert(vtable_index >= 0 , "we should have valid vtable index at this point"); |
924 |
973 |
925 InstanceKlass* inst = InstanceKlass::cast(recv_klass()); |
974 InstanceKlass* inst = InstanceKlass::cast(recv_klass()); |
926 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); |
975 selected_method = methodHandle(THREAD, inst->method_at_vtable(vtable_index)); |
927 } else { |
976 } else { |
928 // at this point we are sure that resolved_method is virtual and not |
977 // at this point we are sure that resolved_method is virtual and not |
929 // a miranda method; therefore, it must have a valid vtable index. |
978 // a miranda method; therefore, it must have a valid vtable index. |
|
979 assert(!resolved_method->has_itable_index(), ""); |
930 vtable_index = resolved_method->vtable_index(); |
980 vtable_index = resolved_method->vtable_index(); |
931 // We could get a negative vtable_index for final methods, |
981 // We could get a negative vtable_index for final methods, |
932 // because as an optimization they are they are never put in the vtable, |
982 // because as an optimization they are they are never put in the vtable, |
933 // unless they override an existing method. |
983 // unless they override an existing method. |
934 // If we do get a negative, it means the resolved method is the the selected |
984 // If we do get a negative, it means the resolved method is the the selected |