20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/metadataOnStackMark.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "code/codeCache.hpp"
29 #include "code/debugInfoRec.hpp"
30 #include "gc/shared/collectedHeap.inline.hpp"
31 #include "gc/shared/gcLocker.hpp"
32 #include "gc/shared/generation.hpp"
33 #include "interpreter/bytecodeStream.hpp"
34 #include "interpreter/bytecodeTracer.hpp"
35 #include "interpreter/bytecodes.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "interpreter/oopMapCache.hpp"
38 #include "memory/heapInspection.hpp"
39 #include "memory/metadataFactory.hpp"
40 #include "memory/oopFactory.hpp"
41 #include "oops/constMethod.hpp"
42 #include "oops/method.hpp"
43 #include "oops/methodData.hpp"
44 #include "oops/objArrayOop.inline.hpp"
45 #include "oops/oop.inline.hpp"
46 #include "oops/symbol.hpp"
47 #include "prims/jvmtiExport.hpp"
48 #include "prims/methodHandles.hpp"
49 #include "prims/nativeLookup.hpp"
50 #include "runtime/arguments.hpp"
51 #include "runtime/compilationPolicy.hpp"
52 #include "runtime/frame.inline.hpp"
53 #include "runtime/handles.inline.hpp"
54 #include "runtime/orderAccess.inline.hpp"
55 #include "runtime/relocator.hpp"
56 #include "runtime/sharedRuntime.hpp"
57 #include "runtime/signature.hpp"
58 #include "utilities/quickSort.hpp"
59 #include "utilities/xmlstream.hpp"
104 set_signature_handler(NULL);
105 }
106
107 NOT_PRODUCT(set_compiled_invocation_count(0);)
108 }
109
110 // Release Method*. The nmethod will be gone when we get here because
111 // we've walked the code cache.
112 void Method::deallocate_contents(ClassLoaderData* loader_data) {
113 MetadataFactory::free_metadata(loader_data, constMethod());
114 set_constMethod(NULL);
115 MetadataFactory::free_metadata(loader_data, method_data());
116 set_method_data(NULL);
117 MetadataFactory::free_metadata(loader_data, method_counters());
118 clear_method_counters();
119 // The nmethod will be gone when we get here.
120 if (code() != NULL) _code = NULL;
121 }
122
123 address Method::get_i2c_entry() {
124 assert(_adapter != NULL, "must have");
125 return _adapter->get_i2c_entry();
126 }
127
128 address Method::get_c2i_entry() {
129 assert(_adapter != NULL, "must have");
130 return _adapter->get_c2i_entry();
131 }
132
133 address Method::get_c2i_unverified_entry() {
134 assert(_adapter != NULL, "must have");
135 return _adapter->get_c2i_unverified_entry();
136 }
137
138 char* Method::name_and_sig_as_C_string() const {
139 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
140 }
141
142 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
143 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
144 }
145
146 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
147 const char* klass_name = klass->external_name();
148 int klass_name_len = (int)strlen(klass_name);
149 int method_name_len = method_name->utf8_length();
150 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
151 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
152 strcpy(dest, klass_name);
153 dest[klass_name_len] = '.';
154 strcpy(&dest[klass_name_len + 1], method_name->as_C_string());
155 strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string());
873 void Method::set_not_osr_compilable(int comp_level, bool report, const char* reason) {
874 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
875 if (comp_level == CompLevel_all) {
876 set_not_c1_osr_compilable();
877 set_not_c2_osr_compilable();
878 } else {
879 if (is_c1_compile(comp_level))
880 set_not_c1_osr_compilable();
881 if (is_c2_compile(comp_level))
882 set_not_c2_osr_compilable();
883 }
884 CompilationPolicy::policy()->disable_compilation(this);
885 assert(!CompilationPolicy::can_be_osr_compiled(this, comp_level), "sanity check");
886 }
887
888 // Revert to using the interpreter and clear out the nmethod
889 void Method::clear_code() {
890
891 // this may be NULL if c2i adapters have not been made yet
892 // Only should happen at allocate time.
893 if (_adapter == NULL) {
894 _from_compiled_entry = NULL;
895 } else {
896 _from_compiled_entry = _adapter->get_c2i_entry();
897 }
898 OrderAccess::storestore();
899 _from_interpreted_entry = _i2i_entry;
900 OrderAccess::storestore();
901 _code = NULL;
902 }
903
904 // Called by class data sharing to remove any entry points (which are not shared)
905 void Method::unlink_method() {
906 _code = NULL;
907 _i2i_entry = NULL;
908 _from_interpreted_entry = NULL;
909 if (is_native()) {
910 *native_function_addr() = NULL;
911 set_signature_handler(NULL);
912 }
913 NOT_PRODUCT(set_compiled_invocation_count(0);)
914 _adapter = NULL;
915 _from_compiled_entry = NULL;
916
917 // In case of DumpSharedSpaces, _method_data should always be NULL.
918 //
919 // During runtime (!DumpSharedSpaces), when we are cleaning a
920 // shared class that failed to load, this->link_method() may
921 // have already been called (before an exception happened), so
922 // this->_method_data may not be NULL.
923 assert(!DumpSharedSpaces || _method_data == NULL, "unexpected method data?");
924
925 set_method_data(NULL);
926 clear_method_counters();
927 }
928
929 // Called when the method_holder is getting linked. Setup entrypoints so the method
930 // is ready to be called from interpreter, compiler, and vtables.
931 void Method::link_method(const methodHandle& h_method, TRAPS) {
932 // If the code cache is full, we may reenter this function for the
933 // leftover methods that weren't linked.
934 if (_i2i_entry != NULL) return;
935
936 assert(_adapter == NULL, "init'd to NULL" );
937 assert( _code == NULL, "nothing compiled yet" );
938
939 // Setup interpreter entrypoint
940 assert(this == h_method(), "wrong h_method()" );
941 address entry = Interpreter::entry_for_method(h_method);
942 assert(entry != NULL, "interpreter entry must be non-null");
943 // Sets both _i2i_entry and _from_interpreted_entry
944 set_interpreter_entry(entry);
945
946 // Don't overwrite already registered native entries.
947 if (is_native() && !has_native_function()) {
948 set_native_function(
949 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
950 !native_bind_event_is_interesting);
951 }
952
953 // Setup compiler entrypoint. This is made eagerly, so we do not need
954 // special handling of vtables. An alternative is to make adapters more
955 // lazily by calling make_adapter() from from_compiled_entry() for the
956 // normal calls. For vtable calls life gets more complicated. When a
957 // call-site goes mega-morphic we need adapters in all methods which can be
958 // called from the vtable. We need adapters on such methods that get loaded
959 // later. Ditto for mega-morphic itable calls. If this proves to be a
960 // problem we'll make these lazily later.
961 (void) make_adapters(h_method, CHECK);
962
963 // ONLY USE the h_method now as make_adapter may have blocked
964
965 }
966
967 address Method::make_adapters(methodHandle mh, TRAPS) {
968 // Adapters for compiled code are made eagerly here. They are fairly
969 // small (generally < 100 bytes) and quick to make (and cached and shared)
970 // so making them eagerly shouldn't be too expensive.
971 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
972 if (adapter == NULL ) {
973 THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
974 }
975
976 mh->set_adapter_entry(adapter);
977 mh->_from_compiled_entry = adapter->get_c2i_entry();
978 return adapter->get_c2i_entry();
979 }
980
981 void Method::restore_unshareable_info(TRAPS) {
982 // Since restore_unshareable_info can be called more than once for a method, don't
983 // redo any work. If this field is restored, there is nothing to do.
984 if (_from_compiled_entry == NULL) {
985 // restore method's vtable by calling a virtual function
986 restore_vtable();
987
988 methodHandle mh(THREAD, this);
989 link_method(mh, CHECK);
990 }
991 }
992
993
994 // The verified_code_entry() must be called when a invoke is resolved
995 // on this method.
996
997 // It returns the compiled code entry point, after asserting not null.
998 // This function is called after potential safepoints so that nmethod
999 // or adapter that it points to is still live and valid.
1000 // This function must not hit a safepoint!
1001 address Method::verified_code_entry() {
1002 debug_only(NoSafepointVerifier nsv;)
1003 assert(_from_compiled_entry != NULL, "must be set");
1004 return _from_compiled_entry;
1005 }
1006
1007 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1008 // (could be racing a deopt).
1009 // Not inline to avoid circular ref.
1010 bool Method::check_code() const {
1011 // cached in a register or local. There's a race on the value of the field.
1012 nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code);
|
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/metadataOnStackMark.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "code/codeCache.hpp"
29 #include "code/debugInfoRec.hpp"
30 #include "gc/shared/collectedHeap.inline.hpp"
31 #include "gc/shared/gcLocker.hpp"
32 #include "gc/shared/generation.hpp"
33 #include "interpreter/bytecodeStream.hpp"
34 #include "interpreter/bytecodeTracer.hpp"
35 #include "interpreter/bytecodes.hpp"
36 #include "interpreter/interpreter.hpp"
37 #include "interpreter/oopMapCache.hpp"
38 #include "memory/heapInspection.hpp"
39 #include "memory/metadataFactory.hpp"
40 #include "memory/metaspaceShared.hpp"
41 #include "memory/oopFactory.hpp"
42 #include "oops/constMethod.hpp"
43 #include "oops/method.hpp"
44 #include "oops/methodData.hpp"
45 #include "oops/objArrayOop.inline.hpp"
46 #include "oops/oop.inline.hpp"
47 #include "oops/symbol.hpp"
48 #include "prims/jvmtiExport.hpp"
49 #include "prims/methodHandles.hpp"
50 #include "prims/nativeLookup.hpp"
51 #include "runtime/arguments.hpp"
52 #include "runtime/compilationPolicy.hpp"
53 #include "runtime/frame.inline.hpp"
54 #include "runtime/handles.inline.hpp"
55 #include "runtime/orderAccess.inline.hpp"
56 #include "runtime/relocator.hpp"
57 #include "runtime/sharedRuntime.hpp"
58 #include "runtime/signature.hpp"
59 #include "utilities/quickSort.hpp"
60 #include "utilities/xmlstream.hpp"
105 set_signature_handler(NULL);
106 }
107
108 NOT_PRODUCT(set_compiled_invocation_count(0);)
109 }
110
111 // Release Method*. The nmethod will be gone when we get here because
112 // we've walked the code cache.
113 void Method::deallocate_contents(ClassLoaderData* loader_data) {
114 MetadataFactory::free_metadata(loader_data, constMethod());
115 set_constMethod(NULL);
116 MetadataFactory::free_metadata(loader_data, method_data());
117 set_method_data(NULL);
118 MetadataFactory::free_metadata(loader_data, method_counters());
119 clear_method_counters();
120 // The nmethod will be gone when we get here.
121 if (code() != NULL) _code = NULL;
122 }
123
124 address Method::get_i2c_entry() {
125 assert(adapter() != NULL, "must have");
126 return adapter()->get_i2c_entry();
127 }
128
129 address Method::get_c2i_entry() {
130 assert(adapter() != NULL, "must have");
131 return adapter()->get_c2i_entry();
132 }
133
134 address Method::get_c2i_unverified_entry() {
135 assert(adapter() != NULL, "must have");
136 return adapter()->get_c2i_unverified_entry();
137 }
138
139 char* Method::name_and_sig_as_C_string() const {
140 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
141 }
142
143 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
144 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
145 }
146
147 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
148 const char* klass_name = klass->external_name();
149 int klass_name_len = (int)strlen(klass_name);
150 int method_name_len = method_name->utf8_length();
151 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
152 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
153 strcpy(dest, klass_name);
154 dest[klass_name_len] = '.';
155 strcpy(&dest[klass_name_len + 1], method_name->as_C_string());
156 strcpy(&dest[klass_name_len + 1 + method_name_len], signature->as_C_string());
874 void Method::set_not_osr_compilable(int comp_level, bool report, const char* reason) {
875 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
876 if (comp_level == CompLevel_all) {
877 set_not_c1_osr_compilable();
878 set_not_c2_osr_compilable();
879 } else {
880 if (is_c1_compile(comp_level))
881 set_not_c1_osr_compilable();
882 if (is_c2_compile(comp_level))
883 set_not_c2_osr_compilable();
884 }
885 CompilationPolicy::policy()->disable_compilation(this);
886 assert(!CompilationPolicy::can_be_osr_compiled(this, comp_level), "sanity check");
887 }
888
889 // Revert to using the interpreter and clear out the nmethod
890 void Method::clear_code() {
891
892 // this may be NULL if c2i adapters have not been made yet
893 // Only should happen at allocate time.
894 if (adapter() == NULL) {
895 _from_compiled_entry = NULL;
896 } else {
897 _from_compiled_entry = adapter()->get_c2i_entry();
898 }
899 OrderAccess::storestore();
900 _from_interpreted_entry = _i2i_entry;
901 OrderAccess::storestore();
902 _code = NULL;
903 }
904
905 // Called by class data sharing to remove any entry points (which are not shared)
906 void Method::unlink_method() {
907 _code = NULL;
908 if (!DumpSharedSpaces) {
909 _i2i_entry = NULL;
910 _from_interpreted_entry = NULL;
911 } else {
912 _i2i_entry = Interpreter::entry_for_cds_method(this);
913 _from_interpreted_entry = _i2i_entry;
914 }
915
916 if (is_native()) {
917 *native_function_addr() = NULL;
918 set_signature_handler(NULL);
919 }
920 NOT_PRODUCT(set_compiled_invocation_count(0);)
921 if (!DumpSharedSpaces) {
922 set_adapter_entry(NULL);
923 _from_compiled_entry = NULL;
924 } else {
925 CDSAdapterHandlerEntry* cds_adapter = (CDSAdapterHandlerEntry*)adapter();
926 constMethod()->set_adapter_trampoline(cds_adapter->get_adapter_trampoline());
927 _from_compiled_entry = cds_adapter->get_c2i_entry_trampoline();
928 assert(*((int*)_from_compiled_entry) == 0, "must be NULL during dump time, to be initialized at run time");
929 }
930
931 // In case of DumpSharedSpaces, _method_data should always be NULL.
932 //
933 // During runtime (!DumpSharedSpaces), when we are cleaning a
934 // shared class that failed to load, this->link_method() may
935 // have already been called (before an exception happened), so
936 // this->_method_data may not be NULL.
937 assert(!DumpSharedSpaces || _method_data == NULL, "unexpected method data?");
938
939 set_method_data(NULL);
940 clear_method_counters();
941 }
942
943 // Called when the method_holder is getting linked. Setup entrypoints so the method
944 // is ready to be called from interpreter, compiler, and vtables.
945 void Method::link_method(const methodHandle& h_method, TRAPS) {
946 // If the code cache is full, we may reenter this function for the
947 // leftover methods that weren't linked.
948 if (is_shared()) {
949 if (adapter() != NULL) return;
950 } else {
951 if (_i2i_entry != NULL) return;
952
953 assert(adapter() == NULL, "init'd to NULL" );
954 }
955 assert( _code == NULL, "nothing compiled yet" );
956
957 // Setup interpreter entrypoint
958 assert(this == h_method(), "wrong h_method()" );
959 address entry;
960
961 if (this->is_shared()) {
962 entry = Interpreter::entry_for_cds_method(h_method);
963 } else {
964 entry = Interpreter::entry_for_method(h_method);
965 }
966 assert(entry != NULL, "interpreter entry must be non-null");
967 // Sets both _i2i_entry and _from_interpreted_entry
968 set_interpreter_entry(entry);
969
970 // Don't overwrite already registered native entries.
971 if (is_native() && !has_native_function()) {
972 set_native_function(
973 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
974 !native_bind_event_is_interesting);
975 }
976
977 // Setup compiler entrypoint. This is made eagerly, so we do not need
978 // special handling of vtables. An alternative is to make adapters more
979 // lazily by calling make_adapter() from from_compiled_entry() for the
980 // normal calls. For vtable calls life gets more complicated. When a
981 // call-site goes mega-morphic we need adapters in all methods which can be
982 // called from the vtable. We need adapters on such methods that get loaded
983 // later. Ditto for mega-morphic itable calls. If this proves to be a
984 // problem we'll make these lazily later.
985 (void) make_adapters(h_method, CHECK);
986
987 // ONLY USE the h_method now as make_adapter may have blocked
988
989 }
990
991 address Method::make_adapters(methodHandle mh, TRAPS) {
992 // Adapters for compiled code are made eagerly here. They are fairly
993 // small (generally < 100 bytes) and quick to make (and cached and shared)
994 // so making them eagerly shouldn't be too expensive.
995 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
996 if (adapter == NULL ) {
997 THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
998 }
999
1000 if (mh->is_shared()) {
1001 assert(mh->adapter() == adapter, "must be");
1002 assert(mh->_from_compiled_entry != NULL, "must be"); // FIXME, the instructions also not NULL
1003 } else {
1004 mh->set_adapter_entry(adapter);
1005 mh->_from_compiled_entry = adapter->get_c2i_entry();
1006 }
1007 return adapter->get_c2i_entry();
1008 }
1009
1010 void Method::restore_unshareable_info(TRAPS) {
1011 // Since restore_unshareable_info can be called more than once for a method, don't
1012 // redo any work. If this field is restored, there is nothing to do.
1013 if (_from_compiled_entry == NULL) {
1014 // restore method's vtable by calling a virtual function
1015 restore_vtable();
1016
1017 methodHandle mh(THREAD, this);
1018 link_method(mh, CHECK);
1019 }
1020 }
1021
1022 volatile address Method::from_compiled_entry_no_trampoline() const {
1023 nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code);
1024 if (code) {
1025 return code->verified_entry_point();
1026 } else {
1027 return adapter()->get_c2i_entry();
1028 }
1029 }
1030
1031 // The verified_code_entry() must be called when a invoke is resolved
1032 // on this method.
1033
1034 // It returns the compiled code entry point, after asserting not null.
1035 // This function is called after potential safepoints so that nmethod
1036 // or adapter that it points to is still live and valid.
1037 // This function must not hit a safepoint!
1038 address Method::verified_code_entry() {
1039 debug_only(NoSafepointVerifier nsv;)
1040 assert(_from_compiled_entry != NULL, "must be set");
1041 return _from_compiled_entry;
1042 }
1043
1044 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1045 // (could be racing a deopt).
1046 // Not inline to avoid circular ref.
1047 bool Method::check_code() const {
1048 // cached in a register or local. There's a race on the value of the field.
1049 nmethod *code = (nmethod *)OrderAccess::load_ptr_acquire(&_code);
|