86 int size = Method::size(access_flags.is_native());
87 return new (loader_data, size, MetaspaceObj::MethodType, THREAD) Method(cm, access_flags);
88 }
89
90 Method::Method(ConstMethod* xconst, AccessFlags access_flags) {
91 NoSafepointVerifier no_safepoint;
92 set_constMethod(xconst);
93 set_access_flags(access_flags);
94 set_intrinsic_id(vmIntrinsics::_none);
95 set_force_inline(false);
96 set_hidden(false);
97 set_dont_inline(false);
98 set_has_injected_profile(false);
99 set_method_data(NULL);
100 clear_method_counters();
101 set_vtable_index(Method::garbage_vtable_index);
102
103 // Fix and bury in Method*
104 set_interpreter_entry(NULL); // sets i2i entry and from_int
105 set_adapter_entry(NULL);
106 clear_code(false /* don't need a lock */); // from_c/from_i get set to c2i/i2i
107
108 if (access_flags.is_native()) {
109 clear_native_function();
110 set_signature_handler(NULL);
111 }
112
113 NOT_PRODUCT(set_compiled_invocation_count(0);)
114 }
115
116 // Release Method*. The nmethod will be gone when we get here because
117 // we've walked the code cache.
118 void Method::deallocate_contents(ClassLoaderData* loader_data) {
119 MetadataFactory::free_metadata(loader_data, constMethod());
120 set_constMethod(NULL);
121 #if INCLUDE_JVMCI
122 if (method_data()) {
123 FailedSpeculation::free_failed_speculations(method_data()->get_failed_speculations_address());
124 }
125 #endif
126 MetadataFactory::free_metadata(loader_data, method_data());
798 CompiledMethod* nm = code(); // Put it into local variable to guard against concurrent updates
799 if (nm != NULL) {
800 nm->make_not_entrant();
801 }
802 }
803
804
805 bool Method::has_native_function() const {
806 if (is_method_handle_intrinsic())
807 return false; // special-cased in SharedRuntime::generate_native_wrapper
808 address func = native_function();
809 return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
810 }
811
812
813 void Method::clear_native_function() {
814 // Note: is_method_handle_intrinsic() is allowed here.
815 set_native_function(
816 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
817 !native_bind_event_is_interesting);
818 clear_code();
819 }
820
821 address Method::critical_native_function() {
822 methodHandle mh(this);
823 return NativeLookup::lookup_critical_entry(mh);
824 }
825
826
827 void Method::set_signature_handler(address handler) {
828 address* signature_handler = signature_handler_addr();
829 *signature_handler = handler;
830 }
831
832
833 void Method::print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason) {
834 if (PrintCompilation && report) {
835 ttyLocker ttyl;
836 tty->print("made not %scompilable on ", is_osr ? "OSR " : "");
837 if (comp_level == CompLevel_all) {
838 tty->print("all levels ");
921 return is_not_c2_osr_compilable();
922 return false;
923 }
924
925 void Method::set_not_osr_compilable(int comp_level, bool report, const char* reason) {
926 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
927 if (comp_level == CompLevel_all) {
928 set_not_c1_osr_compilable();
929 set_not_c2_osr_compilable();
930 } else {
931 if (is_c1_compile(comp_level))
932 set_not_c1_osr_compilable();
933 if (is_c2_compile(comp_level))
934 set_not_c2_osr_compilable();
935 }
936 CompilationPolicy::policy()->disable_compilation(this);
937 assert(!CompilationPolicy::can_be_osr_compiled(this, comp_level), "sanity check");
938 }
939
940 // Revert to using the interpreter and clear out the nmethod
941 void Method::clear_code(bool acquire_lock /* = true */) {
942 MutexLocker pl(acquire_lock ? Patching_lock : NULL, Mutex::_no_safepoint_check_flag);
943 // this may be NULL if c2i adapters have not been made yet
944 // Only should happen at allocate time.
945 if (adapter() == NULL) {
946 _from_compiled_entry = NULL;
947 } else {
948 _from_compiled_entry = adapter()->get_c2i_entry();
949 }
950 OrderAccess::storestore();
951 _from_interpreted_entry = _i2i_entry;
952 OrderAccess::storestore();
953 _code = NULL;
954 }
955
956 #if INCLUDE_CDS
957 // Called by class data sharing to remove any entry points (which are not shared)
958 void Method::unlink_method() {
959 _code = NULL;
960
961 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
962 // Set the values to what they should be at run time. Note that
963 // this Method can no longer be executed during dump time.
964 _i2i_entry = Interpreter::entry_for_cds_method(this);
965 _from_interpreted_entry = _i2i_entry;
966
967 if (DynamicDumpSharedSpaces) {
968 assert(_from_compiled_entry != NULL, "sanity");
969 } else {
970 // TODO: Simplify the adapter trampoline allocation for static archiving.
971 // Remove the use of CDSAdapterHandlerEntry.
972 CDSAdapterHandlerEntry* cds_adapter = (CDSAdapterHandlerEntry*)adapter();
973 constMethod()->set_adapter_trampoline(cds_adapter->get_adapter_trampoline());
974 _from_compiled_entry = cds_adapter->get_c2i_entry_trampoline();
975 assert(*((int*)_from_compiled_entry) == 0,
1162 // This function is called after potential safepoints so that nmethod
1163 // or adapter that it points to is still live and valid.
1164 // This function must not hit a safepoint!
1165 address Method::verified_code_entry() {
1166 debug_only(NoSafepointVerifier nsv;)
1167 assert(_from_compiled_entry != NULL, "must be set");
1168 return _from_compiled_entry;
1169 }
1170
1171 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1172 // (could be racing a deopt).
1173 // Not inline to avoid circular ref.
1174 bool Method::check_code() const {
1175 // cached in a register or local. There's a race on the value of the field.
1176 CompiledMethod *code = OrderAccess::load_acquire(&_code);
1177 return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
1178 }
1179
1180 // Install compiled code. Instantly it can execute.
1181 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1182 MutexLocker pl(Patching_lock, Mutex::_no_safepoint_check_flag);
1183 assert( code, "use clear_code to remove code" );
1184 assert( mh->check_code(), "" );
1185
1186 guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
1187
1188 // These writes must happen in this order, because the interpreter will
1189 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1190 // which jumps to _from_compiled_entry.
1191 mh->_code = code; // Assign before allowing compiled code to exec
1192
1193 int comp_level = code->comp_level();
1194 // In theory there could be a race here. In practice it is unlikely
1195 // and not worth worrying about.
1196 if (comp_level > mh->highest_comp_level()) {
1197 mh->set_highest_comp_level(comp_level);
1198 }
1199
1200 OrderAccess::storestore();
1201 mh->_from_compiled_entry = code->verified_entry_point();
1202 OrderAccess::storestore();
|
86 int size = Method::size(access_flags.is_native());
87 return new (loader_data, size, MetaspaceObj::MethodType, THREAD) Method(cm, access_flags);
88 }
89
90 Method::Method(ConstMethod* xconst, AccessFlags access_flags) {
91 NoSafepointVerifier no_safepoint;
92 set_constMethod(xconst);
93 set_access_flags(access_flags);
94 set_intrinsic_id(vmIntrinsics::_none);
95 set_force_inline(false);
96 set_hidden(false);
97 set_dont_inline(false);
98 set_has_injected_profile(false);
99 set_method_data(NULL);
100 clear_method_counters();
101 set_vtable_index(Method::garbage_vtable_index);
102
103 // Fix and bury in Method*
104 set_interpreter_entry(NULL); // sets i2i entry and from_int
105 set_adapter_entry(NULL);
106 Method::clear_code(); // from_c/from_i get set to c2i/i2i
107
108 if (access_flags.is_native()) {
109 clear_native_function();
110 set_signature_handler(NULL);
111 }
112
113 NOT_PRODUCT(set_compiled_invocation_count(0);)
114 }
115
116 // Release Method*. The nmethod will be gone when we get here because
117 // we've walked the code cache.
118 void Method::deallocate_contents(ClassLoaderData* loader_data) {
119 MetadataFactory::free_metadata(loader_data, constMethod());
120 set_constMethod(NULL);
121 #if INCLUDE_JVMCI
122 if (method_data()) {
123 FailedSpeculation::free_failed_speculations(method_data()->get_failed_speculations_address());
124 }
125 #endif
126 MetadataFactory::free_metadata(loader_data, method_data());
798 CompiledMethod* nm = code(); // Put it into local variable to guard against concurrent updates
799 if (nm != NULL) {
800 nm->make_not_entrant();
801 }
802 }
803
804
805 bool Method::has_native_function() const {
806 if (is_method_handle_intrinsic())
807 return false; // special-cased in SharedRuntime::generate_native_wrapper
808 address func = native_function();
809 return (func != NULL && func != SharedRuntime::native_method_throw_unsatisfied_link_error_entry());
810 }
811
812
813 void Method::clear_native_function() {
814 // Note: is_method_handle_intrinsic() is allowed here.
815 set_native_function(
816 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
817 !native_bind_event_is_interesting);
818 this->unlink_code();
819 }
820
821 address Method::critical_native_function() {
822 methodHandle mh(this);
823 return NativeLookup::lookup_critical_entry(mh);
824 }
825
826
827 void Method::set_signature_handler(address handler) {
828 address* signature_handler = signature_handler_addr();
829 *signature_handler = handler;
830 }
831
832
833 void Method::print_made_not_compilable(int comp_level, bool is_osr, bool report, const char* reason) {
834 if (PrintCompilation && report) {
835 ttyLocker ttyl;
836 tty->print("made not %scompilable on ", is_osr ? "OSR " : "");
837 if (comp_level == CompLevel_all) {
838 tty->print("all levels ");
921 return is_not_c2_osr_compilable();
922 return false;
923 }
924
925 void Method::set_not_osr_compilable(int comp_level, bool report, const char* reason) {
926 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
927 if (comp_level == CompLevel_all) {
928 set_not_c1_osr_compilable();
929 set_not_c2_osr_compilable();
930 } else {
931 if (is_c1_compile(comp_level))
932 set_not_c1_osr_compilable();
933 if (is_c2_compile(comp_level))
934 set_not_c2_osr_compilable();
935 }
936 CompilationPolicy::policy()->disable_compilation(this);
937 assert(!CompilationPolicy::can_be_osr_compiled(this, comp_level), "sanity check");
938 }
939
940 // Revert to using the interpreter and clear out the nmethod
941 void Method::clear_code() {
942 // this may be NULL if c2i adapters have not been made yet
943 // Only should happen at allocate time.
944 if (adapter() == NULL) {
945 _from_compiled_entry = NULL;
946 } else {
947 _from_compiled_entry = adapter()->get_c2i_entry();
948 }
949 OrderAccess::storestore();
950 _from_interpreted_entry = _i2i_entry;
951 OrderAccess::storestore();
952 _code = NULL;
953 }
954
955 void Method::unlink_code(CompiledMethod *compare) {
956 MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
957 // We need to check if either the _code or _from_compiled_code_entry_point
958 // refer to this nmethod because there is a race in setting these two fields
959 // in Method* as seen in bugid 4947125.
960 // If the vep() points to the zombie nmethod, the memory for the nmethod
961 // could be flushed and the compiler and vtable stubs could still call
962 // through it.
963 if (code() == compare ||
964 from_compiled_entry() == compare->verified_entry_point()) {
965 clear_code();
966 }
967 }
968
969 void Method::unlink_code() {
970 MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
971 clear_code();
972 }
973
974 #if INCLUDE_CDS
975 // Called by class data sharing to remove any entry points (which are not shared)
976 void Method::unlink_method() {
977 _code = NULL;
978
979 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
980 // Set the values to what they should be at run time. Note that
981 // this Method can no longer be executed during dump time.
982 _i2i_entry = Interpreter::entry_for_cds_method(this);
983 _from_interpreted_entry = _i2i_entry;
984
985 if (DynamicDumpSharedSpaces) {
986 assert(_from_compiled_entry != NULL, "sanity");
987 } else {
988 // TODO: Simplify the adapter trampoline allocation for static archiving.
989 // Remove the use of CDSAdapterHandlerEntry.
990 CDSAdapterHandlerEntry* cds_adapter = (CDSAdapterHandlerEntry*)adapter();
991 constMethod()->set_adapter_trampoline(cds_adapter->get_adapter_trampoline());
992 _from_compiled_entry = cds_adapter->get_c2i_entry_trampoline();
993 assert(*((int*)_from_compiled_entry) == 0,
1180 // This function is called after potential safepoints so that nmethod
1181 // or adapter that it points to is still live and valid.
1182 // This function must not hit a safepoint!
1183 address Method::verified_code_entry() {
1184 debug_only(NoSafepointVerifier nsv;)
1185 assert(_from_compiled_entry != NULL, "must be set");
1186 return _from_compiled_entry;
1187 }
1188
1189 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1190 // (could be racing a deopt).
1191 // Not inline to avoid circular ref.
1192 bool Method::check_code() const {
1193 // cached in a register or local. There's a race on the value of the field.
1194 CompiledMethod *code = OrderAccess::load_acquire(&_code);
1195 return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
1196 }
1197
1198 // Install compiled code. Instantly it can execute.
1199 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1200 MutexLocker pl(CompiledMethod_lock, Mutex::_no_safepoint_check_flag);
1201 assert( code, "use clear_code to remove code" );
1202 assert( mh->check_code(), "" );
1203
1204 guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
1205
1206 // These writes must happen in this order, because the interpreter will
1207 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1208 // which jumps to _from_compiled_entry.
1209 mh->_code = code; // Assign before allowing compiled code to exec
1210
1211 int comp_level = code->comp_level();
1212 // In theory there could be a race here. In practice it is unlikely
1213 // and not worth worrying about.
1214 if (comp_level > mh->highest_comp_level()) {
1215 mh->set_highest_comp_level(comp_level);
1216 }
1217
1218 OrderAccess::storestore();
1219 mh->_from_compiled_entry = code->verified_entry_point();
1220 OrderAccess::storestore();
|