42 #include "interpreter/oopMapCache.hpp"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "logging/logTag.hpp"
46 #include "memory/allocation.inline.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/metaspaceClosure.hpp"
49 #include "memory/oopFactory.hpp"
50 #include "memory/resourceArea.hpp"
51 #include "memory/universe.hpp"
52 #include "nmt/memTracker.hpp"
53 #include "oops/constMethod.hpp"
54 #include "oops/constantPool.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/method.inline.hpp"
57 #include "oops/methodData.hpp"
58 #include "oops/objArrayKlass.hpp"
59 #include "oops/objArrayOop.inline.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "oops/symbol.hpp"
62 #include "prims/jvmtiExport.hpp"
63 #include "prims/methodHandles.hpp"
64 #include "runtime/atomic.hpp"
65 #include "runtime/arguments.hpp"
66 #include "runtime/continuationEntry.hpp"
67 #include "runtime/frame.inline.hpp"
68 #include "runtime/handles.inline.hpp"
69 #include "runtime/init.hpp"
70 #include "runtime/java.hpp"
71 #include "runtime/orderAccess.hpp"
72 #include "runtime/perfData.hpp"
73 #include "runtime/relocator.hpp"
74 #include "runtime/safepointVerifiers.hpp"
75 #include "runtime/sharedRuntime.hpp"
76 #include "runtime/signature.hpp"
77 #include "runtime/threads.hpp"
78 #include "runtime/vm_version.hpp"
79 #include "utilities/align.hpp"
80 #include "utilities/quickSort.hpp"
81 #include "utilities/vmError.hpp"
102 }
103
104 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
105 NoSafepointVerifier no_safepoint;
106 set_constMethod(xconst);
107 set_access_flags(access_flags);
108 set_intrinsic_id(vmIntrinsics::_none);
109 clear_method_data();
110 clear_method_counters();
111 set_vtable_index(Method::garbage_vtable_index);
112
113 // Fix and bury in Method*
114 set_interpreter_entry(nullptr); // sets i2i entry and from_int
115 set_adapter_entry(nullptr);
116 Method::clear_code(); // from_c/from_i get set to c2i/i2i
117
118 if (access_flags.is_native()) {
119 clear_native_function();
120 set_signature_handler(nullptr);
121 }
122
123 NOT_PRODUCT(set_compiled_invocation_count(0);)
124 // Name is very useful for debugging.
125 NOT_PRODUCT(_name = name;)
126 }
127
128 // Release Method*. The nmethod will be gone when we get here because
129 // we've walked the code cache.
130 void Method::deallocate_contents(ClassLoaderData* loader_data) {
131 MetadataFactory::free_metadata(loader_data, constMethod());
132 set_constMethod(nullptr);
133 MetadataFactory::free_metadata(loader_data, method_data());
134 clear_method_data();
135 MetadataFactory::free_metadata(loader_data, method_counters());
136 clear_method_counters();
137 // The nmethod will be gone when we get here.
138 if (code() != nullptr) _code = nullptr;
139 }
140
141 void Method::release_C_heap_structures() {
142 if (method_data()) {
143 method_data()->release_C_heap_structures();
144
145 // Destroy MethodData embedded lock
146 method_data()->~MethodData();
147 }
148 }
149
150 address Method::get_i2c_entry() {
151 assert(adapter() != nullptr, "must have");
152 return adapter()->get_i2c_entry();
153 }
154
155 address Method::get_c2i_entry() {
156 assert(adapter() != nullptr, "must have");
157 return adapter()->get_c2i_entry();
158 }
159
160 address Method::get_c2i_unverified_entry() {
161 assert(adapter() != nullptr, "must have");
162 return adapter()->get_c2i_unverified_entry();
163 }
164
165 address Method::get_c2i_no_clinit_check_entry() {
166 assert(VM_Version::supports_fast_class_init_checks(), "");
167 assert(adapter() != nullptr, "must have");
168 return adapter()->get_c2i_no_clinit_check_entry();
169 }
170
171 char* Method::name_and_sig_as_C_string() const {
172 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
173 }
174
175 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
176 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
177 }
178
179 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
180 const char* klass_name = klass->external_name();
181 int klass_name_len = (int)strlen(klass_name);
182 int method_name_len = method_name->utf8_length();
183 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
184 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
370 return code_base();
371 } else {
372 return bcp;
373 }
374 }
375
376 int Method::size(bool is_native) {
377 // If native, then include pointers for native_function and signature_handler
378 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
379 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
380 return align_metadata_size(header_size() + extra_words);
381 }
382
383 Symbol* Method::klass_name() const {
384 return method_holder()->name();
385 }
386
387 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
388 log_trace(cds)("Iter(Method): %p", this);
389
390 if (!method_holder()->is_rewritten()) {
391 it->push(&_constMethod, MetaspaceClosure::_writable);
392 } else {
393 it->push(&_constMethod);
394 }
395 it->push(&_method_data);
396 it->push(&_method_counters);
397 NOT_PRODUCT(it->push(&_name);)
398 }
399
400 #if INCLUDE_CDS
401 // Attempt to return method to original state. Clear any pointers
402 // (to objects outside the shared spaces). We won't be able to predict
403 // where they should point in a new JVM. Further initialize some
404 // entries now in order allow them to be write protected later.
405
406 void Method::remove_unshareable_info() {
407 unlink_method();
408 JFR_ONLY(REMOVE_METHOD_ID(this);)
409 }
410
647 bool Method::init_method_counters(MethodCounters* counters) {
648 // Try to install a pointer to MethodCounters, return true on success.
649 return Atomic::replace_if_null(&_method_counters, counters);
650 }
651
652 void Method::set_exception_handler_entered(int handler_bci) {
653 if (ProfileExceptionHandlers) {
654 MethodData* mdo = method_data();
655 if (mdo != nullptr) {
656 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
657 handler_data.set_exception_handler_entered();
658 }
659 }
660 }
661
662 int Method::extra_stack_words() {
663 // not an inline function, to avoid a header dependency on Interpreter
664 return extra_stack_entries() * Interpreter::stackElementSize;
665 }
666
667 bool Method::compute_has_loops_flag() {
668 BytecodeStream bcs(methodHandle(Thread::current(), this));
669 Bytecodes::Code bc;
670
671 while ((bc = bcs.next()) >= 0) {
672 switch (bc) {
673 case Bytecodes::_ifeq:
674 case Bytecodes::_ifnull:
675 case Bytecodes::_iflt:
676 case Bytecodes::_ifle:
677 case Bytecodes::_ifne:
678 case Bytecodes::_ifnonnull:
679 case Bytecodes::_ifgt:
680 case Bytecodes::_ifge:
681 case Bytecodes::_if_icmpeq:
682 case Bytecodes::_if_icmpne:
683 case Bytecodes::_if_icmplt:
684 case Bytecodes::_if_icmpgt:
685 case Bytecodes::_if_icmple:
686 case Bytecodes::_if_icmpge:
795
796 bool Method::is_accessor() const {
797 return is_getter() || is_setter();
798 }
799
800 bool Method::is_getter() const {
801 if (code_size() != 5) return false;
802 if (size_of_parameters() != 1) return false;
803 if (java_code_at(0) != Bytecodes::_aload_0) return false;
804 if (java_code_at(1) != Bytecodes::_getfield) return false;
805 switch (java_code_at(4)) {
806 case Bytecodes::_ireturn:
807 case Bytecodes::_lreturn:
808 case Bytecodes::_freturn:
809 case Bytecodes::_dreturn:
810 case Bytecodes::_areturn:
811 break;
812 default:
813 return false;
814 }
815 return true;
816 }
817
818 bool Method::is_setter() const {
819 if (code_size() != 6) return false;
820 if (java_code_at(0) != Bytecodes::_aload_0) return false;
821 switch (java_code_at(1)) {
822 case Bytecodes::_iload_1:
823 case Bytecodes::_aload_1:
824 case Bytecodes::_fload_1:
825 if (size_of_parameters() != 2) return false;
826 break;
827 case Bytecodes::_dload_1:
828 case Bytecodes::_lload_1:
829 if (size_of_parameters() != 3) return false;
830 break;
831 default:
832 return false;
833 }
834 if (java_code_at(2) != Bytecodes::_putfield) return false;
835 if (java_code_at(5) != Bytecodes::_return) return false;
836 return true;
837 }
838
839 bool Method::is_constant_getter() const {
840 int last_index = code_size() - 1;
841 // Check if the first 1-3 bytecodes are a constant push
842 // and the last bytecode is a return.
843 return (2 <= code_size() && code_size() <= 4 &&
844 Bytecodes::is_const(java_code_at(0)) &&
845 Bytecodes::length_for(java_code_at(0)) == last_index &&
846 Bytecodes::is_return(java_code_at(last_index)));
847 }
848
849 bool Method::has_valid_initializer_flags() const {
850 return (is_static() ||
851 method_holder()->major_version() < 51);
852 }
853
854 bool Method::is_static_initializer() const {
855 // For classfiles version 51 or greater, ensure that the clinit method is
856 // static. Non-static methods with the name "<clinit>" are not static
857 // initializers. (older classfiles exempted for backward compatibility)
858 return name() == vmSymbols::class_initializer_name() &&
859 has_valid_initializer_flags();
860 }
861
862 bool Method::is_object_initializer() const {
863 return name() == vmSymbols::object_initializer_name();
864 }
865
866 bool Method::needs_clinit_barrier() const {
867 return is_static() && !method_holder()->is_initialized();
868 }
869
870 bool Method::is_object_wait0() const {
871 return klass_name() == vmSymbols::java_lang_Object()
872 && name() == vmSymbols::wait_name();
873 }
874
875 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
876 int length = method->checked_exceptions_length();
877 if (length == 0) { // common case
878 return objArrayHandle(THREAD, Universe::the_empty_class_array());
879 } else {
880 methodHandle h_this(THREAD, method);
881 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
882 objArrayHandle mirrors (THREAD, m_oop);
883 for (int i = 0; i < length; i++) {
906 // Not necessarily sorted and not necessarily one-to-one.
907 CompressedLineNumberReadStream stream(compressed_linenumber_table());
908 while (stream.read_pair()) {
909 if (stream.bci() == bci) {
910 // perfect match
911 return stream.line();
912 } else {
913 // update best_bci/line
914 if (stream.bci() < bci && stream.bci() >= best_bci) {
915 best_bci = stream.bci();
916 best_line = stream.line();
917 }
918 }
919 }
920 }
921 return best_line;
922 }
923
924
925 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
926 if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
927 Thread *thread = Thread::current();
928 Symbol* klass_name = constants()->klass_name_at(klass_index);
929 Handle loader(thread, method_holder()->class_loader());
930 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
931 } else {
932 return true;
933 }
934 }
935
936
937 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
938 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
939 if (must_be_resolved) {
940 // Make sure klass is resolved in constantpool.
941 if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;
942 }
943 return is_klass_loaded_by_klass_index(klass_index);
944 }
945
946
947 void Method::set_native_function(address function, bool post_event_flag) {
948 assert(function != nullptr, "use clear_native_function to unregister natives");
949 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
950 address* native_function = native_function_addr();
951
952 // We can see racers trying to place the same native function into place. Once
953 // is plenty.
954 address current = *native_function;
955 if (current == function) return;
956 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
957 function != nullptr) {
958 // native_method_throw_unsatisfied_link_error_entry() should only
959 // be passed when post_event_flag is false.
960 assert(function !=
961 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1090 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1091 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1092 if (comp_level == CompLevel_all) {
1093 set_is_not_c1_osr_compilable();
1094 set_is_not_c2_osr_compilable();
1095 } else {
1096 if (is_c1_compile(comp_level))
1097 set_is_not_c1_osr_compilable();
1098 if (is_c2_compile(comp_level))
1099 set_is_not_c2_osr_compilable();
1100 }
1101 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1102 }
1103
1104 // Revert to using the interpreter and clear out the nmethod
1105 void Method::clear_code() {
1106 // this may be null if c2i adapters have not been made yet
1107 // Only should happen at allocate time.
1108 if (adapter() == nullptr) {
1109 _from_compiled_entry = nullptr;
1110 } else {
1111 _from_compiled_entry = adapter()->get_c2i_entry();
1112 }
1113 OrderAccess::storestore();
1114 _from_interpreted_entry = _i2i_entry;
1115 OrderAccess::storestore();
1116 _code = nullptr;
1117 }
1118
1119 void Method::unlink_code(nmethod *compare) {
1120 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1121 // We need to check if either the _code or _from_compiled_code_entry_point
1122 // refer to this nmethod because there is a race in setting these two fields
1123 // in Method* as seen in bugid 4947125.
1124 if (code() == compare ||
1125 from_compiled_entry() == compare->verified_entry_point()) {
1126 clear_code();
1127 }
1128 }
1129
1130 void Method::unlink_code() {
1131 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1132 clear_code();
1133 }
1134
1135 #if INCLUDE_CDS
1136 // Called by class data sharing to remove any entry points (which are not shared)
1137 void Method::unlink_method() {
1138 assert(CDSConfig::is_dumping_archive(), "sanity");
1139 _code = nullptr;
1140 _adapter = nullptr;
1141 _i2i_entry = nullptr;
1142 _from_compiled_entry = nullptr;
1143 _from_interpreted_entry = nullptr;
1144
1145 if (is_native()) {
1146 *native_function_addr() = nullptr;
1147 set_signature_handler(nullptr);
1148 }
1149 NOT_PRODUCT(set_compiled_invocation_count(0);)
1150
1151 clear_method_data();
1152 clear_method_counters();
1153 remove_unshareable_flags();
1154 }
1155
1156 void Method::remove_unshareable_flags() {
1157 // clear all the flags that shouldn't be in the archived version
1158 assert(!is_old(), "must be");
1159 assert(!is_obsolete(), "must be");
1160 assert(!is_deleted(), "must be");
1161
1162 set_is_prefixed_native(false);
1163 set_queued_for_compilation(false);
1164 set_is_not_c2_compilable(false);
1165 set_is_not_c1_compilable(false);
1166 set_is_not_c2_osr_compilable(false);
1167 set_on_stack_flag(false);
1168 }
1169 #endif
1170
1171 // Called when the method_holder is getting linked. Setup entrypoints so the method
1172 // is ready to be called from interpreter, compiler, and vtables.
1173 void Method::link_method(const methodHandle& h_method, TRAPS) {
1174 if (log_is_enabled(Info, perf, class, link)) {
1175 ClassLoader::perf_ik_link_methods_count()->inc();
1176 }
1177
1178 // If the code cache is full, we may reenter this function for the
1179 // leftover methods that weren't linked.
1180 if (adapter() != nullptr) {
1181 return;
1182 }
1183 assert( _code == nullptr, "nothing compiled yet" );
1184
1185 // Setup interpreter entrypoint
1186 assert(this == h_method(), "wrong h_method()" );
1187
1188 assert(adapter() == nullptr, "init'd to null");
1189 address entry = Interpreter::entry_for_method(h_method);
1190 assert(entry != nullptr, "interpreter entry must be non-null");
1191 // Sets both _i2i_entry and _from_interpreted_entry
1192 set_interpreter_entry(entry);
1193
1194 // Don't overwrite already registered native entries.
1195 if (is_native() && !has_native_function()) {
1196 set_native_function(
1197 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1198 !native_bind_event_is_interesting);
1199 }
1200
1201 // Setup compiler entrypoint. This is made eagerly, so we do not need
1202 // special handling of vtables. An alternative is to make adapters more
1203 // lazily by calling make_adapter() from from_compiled_entry() for the
1204 // normal calls. For vtable calls life gets more complicated. When a
1205 // call-site goes mega-morphic we need adapters in all methods which can be
1206 // called from the vtable. We need adapters on such methods that get loaded
1207 // later. Ditto for mega-morphic itable calls. If this proves to be a
1208 // problem we'll make these lazily later.
1209 (void) make_adapters(h_method, CHECK);
1210
1211 // ONLY USE the h_method now as make_adapter may have blocked
1212
1213 if (h_method->is_continuation_native_intrinsic()) {
1214 _from_interpreted_entry = nullptr;
1215 _from_compiled_entry = nullptr;
1216 _i2i_entry = nullptr;
1217 if (Continuations::enabled()) {
1218 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1219 AdapterHandlerLibrary::create_native_wrapper(h_method);
1228 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1229 PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1230
1231 // Adapters for compiled code are made eagerly here. They are fairly
1232 // small (generally < 100 bytes) and quick to make (and cached and shared)
1233 // so making them eagerly shouldn't be too expensive.
1234 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1235 if (adapter == nullptr ) {
1236 if (!is_init_completed()) {
1237 // Don't throw exceptions during VM initialization because java.lang.* classes
1238 // might not have been initialized, causing problems when constructing the
1239 // Java exception object.
1240 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1241 } else {
1242 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1243 }
1244 }
1245
1246 mh->set_adapter_entry(adapter);
1247 mh->_from_compiled_entry = adapter->get_c2i_entry();
1248 return adapter->get_c2i_entry();
1249 }
1250
1251 // The verified_code_entry() must be called when a invoke is resolved
1252 // on this method.
1253
1254 // It returns the compiled code entry point, after asserting not null.
1255 // This function is called after potential safepoints so that nmethod
1256 // or adapter that it points to is still live and valid.
1257 // This function must not hit a safepoint!
1258 address Method::verified_code_entry() {
1259 debug_only(NoSafepointVerifier nsv;)
1260 assert(_from_compiled_entry != nullptr, "must be set");
1261 return _from_compiled_entry;
1262 }
1263
1264 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1265 // (could be racing a deopt).
1266 // Not inline to avoid circular ref.
1267 bool Method::check_code() const {
1268 // cached in a register or local. There's a race on the value of the field.
1269 nmethod *code = Atomic::load_acquire(&_code);
1270 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1271 }
1272
1273 // Install compiled code. Instantly it can execute.
1274 void Method::set_code(const methodHandle& mh, nmethod *code) {
1275 assert_lock_strong(NMethodState_lock);
1276 assert( code, "use clear_code to remove code" );
1277 assert( mh->check_code(), "" );
1278
1279 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1280
1281 // These writes must happen in this order, because the interpreter will
1282 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1283 // which jumps to _from_compiled_entry.
1284 mh->_code = code; // Assign before allowing compiled code to exec
1285
1286 int comp_level = code->comp_level();
1287 // In theory there could be a race here. In practice it is unlikely
1288 // and not worth worrying about.
1289 if (comp_level > mh->highest_comp_level()) {
1290 mh->set_highest_comp_level(comp_level);
1291 }
1292
1293 OrderAccess::storestore();
1294 mh->_from_compiled_entry = code->verified_entry_point();
1295 OrderAccess::storestore();
1296
1297 if (mh->is_continuation_native_intrinsic()) {
1298 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1299
1300 if (mh->is_continuation_enter_intrinsic()) {
1301 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1302 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1303 } else if (mh->is_continuation_yield_intrinsic()) {
1304 mh->_i2i_entry = mh->get_i2c_entry();
1305 } else {
1306 guarantee(false, "Unknown Continuation native intrinsic");
1307 }
1308 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1309 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1310 } else if (!mh->is_method_handle_intrinsic()) {
1311 // Instantly compiled code can execute.
1312 mh->_from_interpreted_entry = mh->get_i2c_entry();
1313 }
1314 }
2230 }
2231
2232 // Check that this pointer is valid by checking that the vtbl pointer matches
2233 bool Method::is_valid_method(const Method* m) {
2234 if (m == nullptr) {
2235 return false;
2236 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2237 // Quick sanity check on pointer.
2238 return false;
2239 } else if (!os::is_readable_range(m, m + 1)) {
2240 return false;
2241 } else if (m->is_shared()) {
2242 return CppVtables::is_valid_shared_method(m);
2243 } else if (Metaspace::contains_non_shared(m)) {
2244 return has_method_vptr((const void*)m);
2245 } else {
2246 return false;
2247 }
2248 }
2249
2250 #ifndef PRODUCT
2251 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2252 out->print("%d", loader_data->jmethod_ids()->count_methods());
2253 }
2254 #endif // PRODUCT
2255
2256
2257 // Printing
2258
2259 #ifndef PRODUCT
2260
2261 void Method::print_on(outputStream* st) const {
2262 ResourceMark rm;
2263 assert(is_method(), "must be method");
2264 st->print_cr("%s", internal_name());
2265 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2266 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2267 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2268 constants()->print_value_on(st); st->cr();
2269 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2270 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2271 st->print (" - name: "); name()->print_value_on(st); st->cr();
2272 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2273 st->print_cr(" - max stack: %d", max_stack());
2274 st->print_cr(" - max locals: %d", max_locals());
2275 st->print_cr(" - size of params: %d", size_of_parameters());
2276 st->print_cr(" - method size: %d", method_size());
2277 if (intrinsic_id() != vmIntrinsics::_none)
2278 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2279 if (highest_comp_level() != CompLevel_none)
2280 st->print_cr(" - highest level: %d", highest_comp_level());
2281 st->print_cr(" - vtable index: %d", _vtable_index);
2282 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2283 st->print( " - adapters: ");
2284 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2285 if (a == nullptr)
2286 st->print_cr(PTR_FORMAT, p2i(a));
2287 else
2288 a->print_adapter_on(st);
2289 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2290 st->print_cr(" - code size: %d", code_size());
2291 if (code_size() != 0) {
2292 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2293 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2294 }
2295 if (method_data() != nullptr) {
2296 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2297 }
2298 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2299 if (checked_exceptions_length() > 0) {
2300 CheckedExceptionElement* table = checked_exceptions_start();
2301 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2302 if (Verbose) {
2303 for (int i = 0; i < checked_exceptions_length(); i++) {
2304 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2305 }
2306 }
2307 }
2308 if (has_linenumber_table()) {
2309 u_char* table = compressed_linenumber_table();
2339 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2340 }
2341 }
2342
2343 void Method::print_linkage_flags(outputStream* st) {
2344 access_flags().print_on(st);
2345 if (is_default_method()) {
2346 st->print("default ");
2347 }
2348 if (is_overpass()) {
2349 st->print("overpass ");
2350 }
2351 }
2352 #endif //PRODUCT
2353
2354 void Method::print_value_on(outputStream* st) const {
2355 assert(is_method(), "must be method");
2356 st->print("%s", internal_name());
2357 print_address_on(st);
2358 st->print(" ");
2359 name()->print_value_on(st);
2360 st->print(" ");
2361 signature()->print_value_on(st);
2362 st->print(" in ");
2363 method_holder()->print_value_on(st);
2364 if (WizardMode) st->print("#%d", _vtable_index);
2365 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2366 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2367 }
2368
2369 // Verification
2370
2371 void Method::verify_on(outputStream* st) {
2372 guarantee(is_method(), "object must be method");
2373 guarantee(constants()->is_constantPool(), "should be constant pool");
2374 MethodData* md = method_data();
2375 guarantee(md == nullptr ||
2376 md->is_methodData(), "should be method data");
2377 }
|
42 #include "interpreter/oopMapCache.hpp"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "logging/logTag.hpp"
46 #include "memory/allocation.inline.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/metaspaceClosure.hpp"
49 #include "memory/oopFactory.hpp"
50 #include "memory/resourceArea.hpp"
51 #include "memory/universe.hpp"
52 #include "nmt/memTracker.hpp"
53 #include "oops/constMethod.hpp"
54 #include "oops/constantPool.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/method.inline.hpp"
57 #include "oops/methodData.hpp"
58 #include "oops/objArrayKlass.hpp"
59 #include "oops/objArrayOop.inline.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "oops/symbol.hpp"
62 #include "oops/inlineKlass.inline.hpp"
63 #include "prims/jvmtiExport.hpp"
64 #include "prims/methodHandles.hpp"
65 #include "runtime/atomic.hpp"
66 #include "runtime/arguments.hpp"
67 #include "runtime/continuationEntry.hpp"
68 #include "runtime/frame.inline.hpp"
69 #include "runtime/handles.inline.hpp"
70 #include "runtime/init.hpp"
71 #include "runtime/java.hpp"
72 #include "runtime/orderAccess.hpp"
73 #include "runtime/perfData.hpp"
74 #include "runtime/relocator.hpp"
75 #include "runtime/safepointVerifiers.hpp"
76 #include "runtime/sharedRuntime.hpp"
77 #include "runtime/signature.hpp"
78 #include "runtime/threads.hpp"
79 #include "runtime/vm_version.hpp"
80 #include "utilities/align.hpp"
81 #include "utilities/quickSort.hpp"
82 #include "utilities/vmError.hpp"
103 }
104
105 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
106 NoSafepointVerifier no_safepoint;
107 set_constMethod(xconst);
108 set_access_flags(access_flags);
109 set_intrinsic_id(vmIntrinsics::_none);
110 clear_method_data();
111 clear_method_counters();
112 set_vtable_index(Method::garbage_vtable_index);
113
114 // Fix and bury in Method*
115 set_interpreter_entry(nullptr); // sets i2i entry and from_int
116 set_adapter_entry(nullptr);
117 Method::clear_code(); // from_c/from_i get set to c2i/i2i
118
119 if (access_flags.is_native()) {
120 clear_native_function();
121 set_signature_handler(nullptr);
122 }
123 NOT_PRODUCT(set_compiled_invocation_count(0);)
124 // Name is very useful for debugging.
125 NOT_PRODUCT(_name = name;)
126 }
127
128 // Release Method*. The nmethod will be gone when we get here because
129 // we've walked the code cache.
130 void Method::deallocate_contents(ClassLoaderData* loader_data) {
131 MetadataFactory::free_metadata(loader_data, constMethod());
132 set_constMethod(nullptr);
133 MetadataFactory::free_metadata(loader_data, method_data());
134 clear_method_data();
135 MetadataFactory::free_metadata(loader_data, method_counters());
136 clear_method_counters();
137 // The nmethod will be gone when we get here.
138 if (code() != nullptr) _code = nullptr;
139 }
140
141 void Method::release_C_heap_structures() {
142 if (method_data()) {
143 method_data()->release_C_heap_structures();
144
145 // Destroy MethodData embedded lock
146 method_data()->~MethodData();
147 }
148 }
149
150 address Method::get_i2c_entry() {
151 assert(adapter() != nullptr, "must have");
152 return adapter()->get_i2c_entry();
153 }
154
155 address Method::get_c2i_entry() {
156 assert(adapter() != nullptr, "must have");
157 return adapter()->get_c2i_entry();
158 }
159
160 address Method::get_c2i_inline_entry() {
161 assert(adapter() != nullptr, "must have");
162 return adapter()->get_c2i_inline_entry();
163 }
164
165 address Method::get_c2i_unverified_entry() {
166 assert(adapter() != nullptr, "must have");
167 return adapter()->get_c2i_unverified_entry();
168 }
169
170 address Method::get_c2i_unverified_inline_entry() {
171 assert(adapter() != nullptr, "must have");
172 return adapter()->get_c2i_unverified_inline_entry();
173 }
174
175 address Method::get_c2i_no_clinit_check_entry() {
176 assert(VM_Version::supports_fast_class_init_checks(), "");
177 assert(adapter() != nullptr, "must have");
178 return adapter()->get_c2i_no_clinit_check_entry();
179 }
180
181 char* Method::name_and_sig_as_C_string() const {
182 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
183 }
184
185 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
186 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
187 }
188
189 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
190 const char* klass_name = klass->external_name();
191 int klass_name_len = (int)strlen(klass_name);
192 int method_name_len = method_name->utf8_length();
193 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
194 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
380 return code_base();
381 } else {
382 return bcp;
383 }
384 }
385
386 int Method::size(bool is_native) {
387 // If native, then include pointers for native_function and signature_handler
388 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
389 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
390 return align_metadata_size(header_size() + extra_words);
391 }
392
393 Symbol* Method::klass_name() const {
394 return method_holder()->name();
395 }
396
397 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
398 log_trace(cds)("Iter(Method): %p", this);
399
400 if (!method_holder()->is_rewritten() || CDSConfig::is_valhalla_preview()) {
401 it->push(&_constMethod, MetaspaceClosure::_writable);
402 } else {
403 it->push(&_constMethod);
404 }
405 it->push(&_method_data);
406 it->push(&_method_counters);
407 NOT_PRODUCT(it->push(&_name);)
408 }
409
410 #if INCLUDE_CDS
411 // Attempt to return method to original state. Clear any pointers
412 // (to objects outside the shared spaces). We won't be able to predict
413 // where they should point in a new JVM. Further initialize some
414 // entries now in order allow them to be write protected later.
415
416 void Method::remove_unshareable_info() {
417 unlink_method();
418 JFR_ONLY(REMOVE_METHOD_ID(this);)
419 }
420
657 bool Method::init_method_counters(MethodCounters* counters) {
658 // Try to install a pointer to MethodCounters, return true on success.
659 return Atomic::replace_if_null(&_method_counters, counters);
660 }
661
662 void Method::set_exception_handler_entered(int handler_bci) {
663 if (ProfileExceptionHandlers) {
664 MethodData* mdo = method_data();
665 if (mdo != nullptr) {
666 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
667 handler_data.set_exception_handler_entered();
668 }
669 }
670 }
671
672 int Method::extra_stack_words() {
673 // not an inline function, to avoid a header dependency on Interpreter
674 return extra_stack_entries() * Interpreter::stackElementSize;
675 }
676
677 // InlineKlass the method is declared to return. This must not
678 // safepoint as it is called with references live on the stack at
679 // locations the GC is unaware of.
680 InlineKlass* Method::returns_inline_type(Thread* thread) const {
681 assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
682 if (is_native()) {
683 return nullptr;
684 }
685 NoSafepointVerifier nsv;
686 SignatureStream ss(signature());
687 while (!ss.at_return_type()) {
688 ss.next();
689 }
690 return ss.as_inline_klass(method_holder());
691 }
692
693 bool Method::compute_has_loops_flag() {
694 BytecodeStream bcs(methodHandle(Thread::current(), this));
695 Bytecodes::Code bc;
696
697 while ((bc = bcs.next()) >= 0) {
698 switch (bc) {
699 case Bytecodes::_ifeq:
700 case Bytecodes::_ifnull:
701 case Bytecodes::_iflt:
702 case Bytecodes::_ifle:
703 case Bytecodes::_ifne:
704 case Bytecodes::_ifnonnull:
705 case Bytecodes::_ifgt:
706 case Bytecodes::_ifge:
707 case Bytecodes::_if_icmpeq:
708 case Bytecodes::_if_icmpne:
709 case Bytecodes::_if_icmplt:
710 case Bytecodes::_if_icmpgt:
711 case Bytecodes::_if_icmple:
712 case Bytecodes::_if_icmpge:
821
822 bool Method::is_accessor() const {
823 return is_getter() || is_setter();
824 }
825
826 bool Method::is_getter() const {
827 if (code_size() != 5) return false;
828 if (size_of_parameters() != 1) return false;
829 if (java_code_at(0) != Bytecodes::_aload_0) return false;
830 if (java_code_at(1) != Bytecodes::_getfield) return false;
831 switch (java_code_at(4)) {
832 case Bytecodes::_ireturn:
833 case Bytecodes::_lreturn:
834 case Bytecodes::_freturn:
835 case Bytecodes::_dreturn:
836 case Bytecodes::_areturn:
837 break;
838 default:
839 return false;
840 }
841 if (has_scalarized_return()) {
842 // Don't treat this as (trivial) getter method because the
843 // inline type should be returned in a scalarized form.
844 return false;
845 }
846 return true;
847 }
848
849 bool Method::is_setter() const {
850 if (code_size() != 6) return false;
851 if (java_code_at(0) != Bytecodes::_aload_0) return false;
852 switch (java_code_at(1)) {
853 case Bytecodes::_iload_1:
854 case Bytecodes::_aload_1:
855 case Bytecodes::_fload_1:
856 if (size_of_parameters() != 2) return false;
857 break;
858 case Bytecodes::_dload_1:
859 case Bytecodes::_lload_1:
860 if (size_of_parameters() != 3) return false;
861 break;
862 default:
863 return false;
864 }
865 if (java_code_at(2) != Bytecodes::_putfield) return false;
866 if (java_code_at(5) != Bytecodes::_return) return false;
867 if (has_scalarized_args()) {
868 // Don't treat this as (trivial) setter method because the
869 // inline type argument should be passed in a scalarized form.
870 return false;
871 }
872 return true;
873 }
874
875 bool Method::is_constant_getter() const {
876 int last_index = code_size() - 1;
877 // Check if the first 1-3 bytecodes are a constant push
878 // and the last bytecode is a return.
879 return (2 <= code_size() && code_size() <= 4 &&
880 Bytecodes::is_const(java_code_at(0)) &&
881 Bytecodes::length_for(java_code_at(0)) == last_index &&
882 Bytecodes::is_return(java_code_at(last_index)) &&
883 !has_scalarized_args());
884 }
885
886 bool Method::is_class_initializer() const {
887 // For classfiles version 51 or greater, ensure that the clinit method is
888 // static. Non-static methods with the name "<clinit>" are not static
889 // initializers. (older classfiles exempted for backward compatibility)
890 return (name() == vmSymbols::class_initializer_name() &&
891 (is_static() ||
892 method_holder()->major_version() < 51));
893 }
894
895 // A method named <init>, is a classic object constructor.
896 bool Method::is_object_constructor() const {
897 return name() == vmSymbols::object_initializer_name();
898 }
899
900 bool Method::needs_clinit_barrier() const {
901 return is_static() && !method_holder()->is_initialized();
902 }
903
904 bool Method::is_object_wait0() const {
905 return klass_name() == vmSymbols::java_lang_Object()
906 && name() == vmSymbols::wait_name();
907 }
908
909 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
910 int length = method->checked_exceptions_length();
911 if (length == 0) { // common case
912 return objArrayHandle(THREAD, Universe::the_empty_class_array());
913 } else {
914 methodHandle h_this(THREAD, method);
915 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
916 objArrayHandle mirrors (THREAD, m_oop);
917 for (int i = 0; i < length; i++) {
940 // Not necessarily sorted and not necessarily one-to-one.
941 CompressedLineNumberReadStream stream(compressed_linenumber_table());
942 while (stream.read_pair()) {
943 if (stream.bci() == bci) {
944 // perfect match
945 return stream.line();
946 } else {
947 // update best_bci/line
948 if (stream.bci() < bci && stream.bci() >= best_bci) {
949 best_bci = stream.bci();
950 best_line = stream.line();
951 }
952 }
953 }
954 }
955 return best_line;
956 }
957
958
959 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
960 if( constants()->tag_at(klass_index).is_unresolved_klass()) {
961 Thread *thread = Thread::current();
962 Symbol* klass_name = constants()->klass_name_at(klass_index);
963 Handle loader(thread, method_holder()->class_loader());
964 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
965 } else {
966 return true;
967 }
968 }
969
970
971 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
972 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
973 if (must_be_resolved) {
974 // Make sure klass is resolved in constantpool.
975 if (constants()->tag_at(klass_index).is_unresolved_klass()) {
976 return false;
977 }
978 }
979 return is_klass_loaded_by_klass_index(klass_index);
980 }
981
982
983 void Method::set_native_function(address function, bool post_event_flag) {
984 assert(function != nullptr, "use clear_native_function to unregister natives");
985 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
986 address* native_function = native_function_addr();
987
988 // We can see racers trying to place the same native function into place. Once
989 // is plenty.
990 address current = *native_function;
991 if (current == function) return;
992 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
993 function != nullptr) {
994 // native_method_throw_unsatisfied_link_error_entry() should only
995 // be passed when post_event_flag is false.
996 assert(function !=
997 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1126 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1127 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1128 if (comp_level == CompLevel_all) {
1129 set_is_not_c1_osr_compilable();
1130 set_is_not_c2_osr_compilable();
1131 } else {
1132 if (is_c1_compile(comp_level))
1133 set_is_not_c1_osr_compilable();
1134 if (is_c2_compile(comp_level))
1135 set_is_not_c2_osr_compilable();
1136 }
1137 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1138 }
1139
1140 // Revert to using the interpreter and clear out the nmethod
1141 void Method::clear_code() {
1142 // this may be null if c2i adapters have not been made yet
1143 // Only should happen at allocate time.
1144 if (adapter() == nullptr) {
1145 _from_compiled_entry = nullptr;
1146 _from_compiled_inline_entry = nullptr;
1147 _from_compiled_inline_ro_entry = nullptr;
1148 } else {
1149 _from_compiled_entry = adapter()->get_c2i_entry();
1150 _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1151 _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1152 }
1153 OrderAccess::storestore();
1154 _from_interpreted_entry = _i2i_entry;
1155 OrderAccess::storestore();
1156 _code = nullptr;
1157 }
1158
1159 void Method::unlink_code(nmethod *compare) {
1160 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1161 // We need to check if either the _code or _from_compiled_code_entry_point
1162 // refer to this nmethod because there is a race in setting these two fields
1163 // in Method* as seen in bugid 4947125.
1164 if (code() == compare ||
1165 from_compiled_entry() == compare->verified_entry_point()) {
1166 clear_code();
1167 }
1168 }
1169
1170 void Method::unlink_code() {
1171 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1172 clear_code();
1173 }
1174
1175 #if INCLUDE_CDS
1176 // Called by class data sharing to remove any entry points (which are not shared)
1177 void Method::unlink_method() {
1178 assert(CDSConfig::is_dumping_archive(), "sanity");
1179 _code = nullptr;
1180 _adapter = nullptr;
1181 _i2i_entry = nullptr;
1182 _from_compiled_entry = nullptr;
1183 _from_compiled_inline_entry = nullptr;
1184 _from_compiled_inline_ro_entry = nullptr;
1185 _from_interpreted_entry = nullptr;
1186
1187 if (is_native()) {
1188 *native_function_addr() = nullptr;
1189 set_signature_handler(nullptr);
1190 }
1191 NOT_PRODUCT(set_compiled_invocation_count(0);)
1192
1193 clear_method_data();
1194 clear_method_counters();
1195 remove_unshareable_flags();
1196 }
1197
1198 void Method::remove_unshareable_flags() {
1199 // clear all the flags that shouldn't be in the archived version
1200 assert(!is_old(), "must be");
1201 assert(!is_obsolete(), "must be");
1202 assert(!is_deleted(), "must be");
1203
1204 set_is_prefixed_native(false);
1205 set_queued_for_compilation(false);
1206 set_is_not_c2_compilable(false);
1207 set_is_not_c1_compilable(false);
1208 set_is_not_c2_osr_compilable(false);
1209 set_on_stack_flag(false);
1210 set_has_scalarized_args(false);
1211 set_has_scalarized_return(false);
1212 }
1213 #endif
1214
1215 // Called when the method_holder is getting linked. Setup entrypoints so the method
1216 // is ready to be called from interpreter, compiler, and vtables.
1217 void Method::link_method(const methodHandle& h_method, TRAPS) {
1218 if (log_is_enabled(Info, perf, class, link)) {
1219 ClassLoader::perf_ik_link_methods_count()->inc();
1220 }
1221
1222 // If the code cache is full, we may reenter this function for the
1223 // leftover methods that weren't linked.
1224 if (adapter() != nullptr) {
1225 return;
1226 }
1227 assert( _code == nullptr, "nothing compiled yet" );
1228
1229 // Setup interpreter entrypoint
1230 assert(this == h_method(), "wrong h_method()" );
1231
1232 assert(adapter() == nullptr, "init'd to null");
1233 address entry = Interpreter::entry_for_method(h_method);
1234 assert(entry != nullptr, "interpreter entry must be non-null");
1235 // Sets both _i2i_entry and _from_interpreted_entry
1236 set_interpreter_entry(entry);
1237
1238 // Don't overwrite already registered native entries.
1239 if (is_native() && !has_native_function()) {
1240 set_native_function(
1241 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1242 !native_bind_event_is_interesting);
1243 }
1244 if (InlineTypeReturnedAsFields && returns_inline_type(THREAD) && !has_scalarized_return()) {
1245 set_has_scalarized_return();
1246 }
1247
1248 // Setup compiler entrypoint. This is made eagerly, so we do not need
1249 // special handling of vtables. An alternative is to make adapters more
1250 // lazily by calling make_adapter() from from_compiled_entry() for the
1251 // normal calls. For vtable calls life gets more complicated. When a
1252 // call-site goes mega-morphic we need adapters in all methods which can be
1253 // called from the vtable. We need adapters on such methods that get loaded
1254 // later. Ditto for mega-morphic itable calls. If this proves to be a
1255 // problem we'll make these lazily later.
1256 (void) make_adapters(h_method, CHECK);
1257
1258 // ONLY USE the h_method now as make_adapter may have blocked
1259
1260 if (h_method->is_continuation_native_intrinsic()) {
1261 _from_interpreted_entry = nullptr;
1262 _from_compiled_entry = nullptr;
1263 _i2i_entry = nullptr;
1264 if (Continuations::enabled()) {
1265 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1266 AdapterHandlerLibrary::create_native_wrapper(h_method);
1275 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1276 PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1277
1278 // Adapters for compiled code are made eagerly here. They are fairly
1279 // small (generally < 100 bytes) and quick to make (and cached and shared)
1280 // so making them eagerly shouldn't be too expensive.
1281 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1282 if (adapter == nullptr ) {
1283 if (!is_init_completed()) {
1284 // Don't throw exceptions during VM initialization because java.lang.* classes
1285 // might not have been initialized, causing problems when constructing the
1286 // Java exception object.
1287 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1288 } else {
1289 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1290 }
1291 }
1292
1293 mh->set_adapter_entry(adapter);
1294 mh->_from_compiled_entry = adapter->get_c2i_entry();
1295 mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1296 mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1297 return adapter->get_c2i_entry();
1298 }
1299
1300 // The verified_code_entry() must be called when a invoke is resolved
1301 // on this method.
1302
1303 // It returns the compiled code entry point, after asserting not null.
1304 // This function is called after potential safepoints so that nmethod
1305 // or adapter that it points to is still live and valid.
1306 // This function must not hit a safepoint!
1307 address Method::verified_code_entry() {
1308 debug_only(NoSafepointVerifier nsv;)
1309 assert(_from_compiled_entry != nullptr, "must be set");
1310 return _from_compiled_entry;
1311 }
1312
1313 address Method::verified_inline_code_entry() {
1314 debug_only(NoSafepointVerifier nsv;)
1315 assert(_from_compiled_inline_entry != nullptr, "must be set");
1316 return _from_compiled_inline_entry;
1317 }
1318
1319 address Method::verified_inline_ro_code_entry() {
1320 debug_only(NoSafepointVerifier nsv;)
1321 assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1322 return _from_compiled_inline_ro_entry;
1323 }
1324
1325 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1326 // (could be racing a deopt).
1327 // Not inline to avoid circular ref.
1328 bool Method::check_code() const {
1329 // cached in a register or local. There's a race on the value of the field.
1330 nmethod *code = Atomic::load_acquire(&_code);
1331 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1332 }
1333
1334 // Install compiled code. Instantly it can execute.
1335 void Method::set_code(const methodHandle& mh, nmethod *code) {
1336 assert_lock_strong(NMethodState_lock);
1337 assert( code, "use clear_code to remove code" );
1338 assert( mh->check_code(), "" );
1339
1340 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1341
1342 // These writes must happen in this order, because the interpreter will
1343 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1344 // which jumps to _from_compiled_entry.
1345 mh->_code = code; // Assign before allowing compiled code to exec
1346
1347 int comp_level = code->comp_level();
1348 // In theory there could be a race here. In practice it is unlikely
1349 // and not worth worrying about.
1350 if (comp_level > mh->highest_comp_level()) {
1351 mh->set_highest_comp_level(comp_level);
1352 }
1353
1354 OrderAccess::storestore();
1355 mh->_from_compiled_entry = code->verified_entry_point();
1356 mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1357 mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1358 OrderAccess::storestore();
1359
1360 if (mh->is_continuation_native_intrinsic()) {
1361 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1362
1363 if (mh->is_continuation_enter_intrinsic()) {
1364 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1365 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1366 } else if (mh->is_continuation_yield_intrinsic()) {
1367 mh->_i2i_entry = mh->get_i2c_entry();
1368 } else {
1369 guarantee(false, "Unknown Continuation native intrinsic");
1370 }
1371 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1372 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1373 } else if (!mh->is_method_handle_intrinsic()) {
1374 // Instantly compiled code can execute.
1375 mh->_from_interpreted_entry = mh->get_i2c_entry();
1376 }
1377 }
2293 }
2294
2295 // Check that this pointer is valid by checking that the vtbl pointer matches
2296 bool Method::is_valid_method(const Method* m) {
2297 if (m == nullptr) {
2298 return false;
2299 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2300 // Quick sanity check on pointer.
2301 return false;
2302 } else if (!os::is_readable_range(m, m + 1)) {
2303 return false;
2304 } else if (m->is_shared()) {
2305 return CppVtables::is_valid_shared_method(m);
2306 } else if (Metaspace::contains_non_shared(m)) {
2307 return has_method_vptr((const void*)m);
2308 } else {
2309 return false;
2310 }
2311 }
2312
2313 bool Method::is_scalarized_arg(int idx) const {
2314 if (!has_scalarized_args()) {
2315 return false;
2316 }
2317 // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2318 int depth = 0;
2319 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2320 for (int i = 0; i < sig->length(); i++) {
2321 BasicType bt = sig->at(i)._bt;
2322 if (bt == T_METADATA) {
2323 depth++;
2324 }
2325 if (idx == 0) {
2326 break; // Argument found
2327 }
2328 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2329 depth--;
2330 }
2331 if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2332 idx--; // Advance to next argument
2333 }
2334 }
2335 return depth != 0;
2336 }
2337
2338 #ifndef PRODUCT
2339 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2340 out->print("%d", loader_data->jmethod_ids()->count_methods());
2341 }
2342 #endif // PRODUCT
2343
2344
2345 // Printing
2346
2347 #ifndef PRODUCT
2348
2349 void Method::print_on(outputStream* st) const {
2350 ResourceMark rm;
2351 assert(is_method(), "must be method");
2352 st->print_cr("%s", internal_name());
2353 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2354 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2355 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2356 constants()->print_value_on(st); st->cr();
2357 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2358 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2359 st->print (" - name: "); name()->print_value_on(st); st->cr();
2360 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2361 st->print_cr(" - max stack: %d", max_stack());
2362 st->print_cr(" - max locals: %d", max_locals());
2363 st->print_cr(" - size of params: %d", size_of_parameters());
2364 st->print_cr(" - method size: %d", method_size());
2365 if (intrinsic_id() != vmIntrinsics::_none)
2366 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2367 if (highest_comp_level() != CompLevel_none)
2368 st->print_cr(" - highest level: %d", highest_comp_level());
2369 st->print_cr(" - vtable index: %d", _vtable_index);
2370 #ifdef ASSERT
2371 if (valid_itable_index())
2372 st->print_cr(" - itable index: %d", itable_index());
2373 #endif
2374 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2375 st->print( " - adapters: ");
2376 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2377 if (a == nullptr)
2378 st->print_cr(PTR_FORMAT, p2i(a));
2379 else
2380 a->print_adapter_on(st);
2381 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2382 st->print_cr(" - compiled inline entry " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2383 st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2384 st->print_cr(" - code size: %d", code_size());
2385 if (code_size() != 0) {
2386 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2387 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2388 }
2389 if (method_data() != nullptr) {
2390 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2391 }
2392 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2393 if (checked_exceptions_length() > 0) {
2394 CheckedExceptionElement* table = checked_exceptions_start();
2395 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2396 if (Verbose) {
2397 for (int i = 0; i < checked_exceptions_length(); i++) {
2398 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2399 }
2400 }
2401 }
2402 if (has_linenumber_table()) {
2403 u_char* table = compressed_linenumber_table();
2433 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2434 }
2435 }
2436
2437 void Method::print_linkage_flags(outputStream* st) {
2438 access_flags().print_on(st);
2439 if (is_default_method()) {
2440 st->print("default ");
2441 }
2442 if (is_overpass()) {
2443 st->print("overpass ");
2444 }
2445 }
2446 #endif //PRODUCT
2447
2448 void Method::print_value_on(outputStream* st) const {
2449 assert(is_method(), "must be method");
2450 st->print("%s", internal_name());
2451 print_address_on(st);
2452 st->print(" ");
2453 if (WizardMode) access_flags().print_on(st);
2454 name()->print_value_on(st);
2455 st->print(" ");
2456 signature()->print_value_on(st);
2457 st->print(" in ");
2458 method_holder()->print_value_on(st);
2459 if (WizardMode) st->print("#%d", _vtable_index);
2460 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2461 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2462 }
2463
2464 // Verification
2465
2466 void Method::verify_on(outputStream* st) {
2467 guarantee(is_method(), "object must be method");
2468 guarantee(constants()->is_constantPool(), "should be constant pool");
2469 MethodData* md = method_data();
2470 guarantee(md == nullptr ||
2471 md->is_methodData(), "should be method data");
2472 }
|