36 #include "code/debugInfoRec.hpp"
37 #include "compiler/compilationPolicy.hpp"
38 #include "gc/shared/collectedHeap.inline.hpp"
39 #include "interpreter/bytecodes.hpp"
40 #include "interpreter/bytecodeStream.hpp"
41 #include "interpreter/bytecodeTracer.hpp"
42 #include "interpreter/interpreter.hpp"
43 #include "interpreter/oopMapCache.hpp"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "logging/logTag.hpp"
47 #include "memory/allocation.inline.hpp"
48 #include "memory/metadataFactory.hpp"
49 #include "memory/metaspaceClosure.hpp"
50 #include "memory/oopFactory.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "nmt/memTracker.hpp"
54 #include "oops/constantPool.hpp"
55 #include "oops/constMethod.hpp"
56 #include "oops/jmethodIDTable.hpp"
57 #include "oops/klass.inline.hpp"
58 #include "oops/method.inline.hpp"
59 #include "oops/methodData.hpp"
60 #include "oops/objArrayKlass.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oop.inline.hpp"
63 #include "oops/symbol.hpp"
64 #include "oops/trainingData.hpp"
65 #include "prims/jvmtiExport.hpp"
66 #include "prims/methodHandles.hpp"
67 #include "runtime/arguments.hpp"
68 #include "runtime/atomic.hpp"
69 #include "runtime/continuationEntry.hpp"
70 #include "runtime/frame.inline.hpp"
71 #include "runtime/handles.inline.hpp"
72 #include "runtime/init.hpp"
73 #include "runtime/java.hpp"
74 #include "runtime/orderAccess.hpp"
75 #include "runtime/perfData.hpp"
105 }
106
107 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
108 NoSafepointVerifier no_safepoint;
109 set_constMethod(xconst);
110 set_access_flags(access_flags);
111 set_intrinsic_id(vmIntrinsics::_none);
112 clear_method_data();
113 clear_method_counters();
114 set_vtable_index(Method::garbage_vtable_index);
115
116 // Fix and bury in Method*
117 set_interpreter_entry(nullptr); // sets i2i entry and from_int
118 set_adapter_entry(nullptr);
119 Method::clear_code(); // from_c/from_i get set to c2i/i2i
120
121 if (access_flags.is_native()) {
122 clear_native_function();
123 set_signature_handler(nullptr);
124 }
125
126 NOT_PRODUCT(set_compiled_invocation_count(0);)
127 // Name is very useful for debugging.
128 NOT_PRODUCT(_name = name;)
129 }
130
131 // Release Method*. The nmethod will be gone when we get here because
132 // we've walked the code cache.
133 void Method::deallocate_contents(ClassLoaderData* loader_data) {
134 MetadataFactory::free_metadata(loader_data, constMethod());
135 set_constMethod(nullptr);
136 MetadataFactory::free_metadata(loader_data, method_data());
137 clear_method_data();
138 MetadataFactory::free_metadata(loader_data, method_counters());
139 clear_method_counters();
140 set_adapter_entry(nullptr);
141 // The nmethod will be gone when we get here.
142 if (code() != nullptr) _code = nullptr;
143 }
144
145 void Method::release_C_heap_structures() {
150 method_data()->~MethodData();
151 }
152 }
153
154 address Method::get_i2c_entry() {
155 if (is_abstract()) {
156 return SharedRuntime::throw_AbstractMethodError_entry();
157 }
158 assert(adapter() != nullptr, "must have");
159 return adapter()->get_i2c_entry();
160 }
161
162 address Method::get_c2i_entry() {
163 if (is_abstract()) {
164 return SharedRuntime::get_handle_wrong_method_abstract_stub();
165 }
166 assert(adapter() != nullptr, "must have");
167 return adapter()->get_c2i_entry();
168 }
169
170 address Method::get_c2i_unverified_entry() {
171 assert(adapter() != nullptr, "must have");
172 return adapter()->get_c2i_unverified_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(aot)("Iter(Method): %p", this);
399
400 if (!method_holder()->is_rewritten()) {
401 it->push(&_constMethod, MetaspaceClosure::_writable);
402 } else {
403 it->push(&_constMethod);
404 }
405 it->push(&_adapter);
406 it->push(&_method_data);
407 it->push(&_method_counters);
408 NOT_PRODUCT(it->push(&_name);)
409 }
410
411 #if INCLUDE_CDS
412 // Attempt to return method to original state. Clear any pointers
413 // (to objects outside the shared spaces). We won't be able to predict
414 // where they should point in a new JVM. Further initialize some
415 // entries now in order allow them to be write protected later.
416
417 void Method::remove_unshareable_info() {
418 unlink_method();
419 if (method_data() != nullptr) {
420 method_data()->remove_unshareable_info();
423 method_counters()->remove_unshareable_info();
424 }
425 if (CDSConfig::is_dumping_adapters() && _adapter != nullptr) {
426 _adapter->remove_unshareable_info();
427 _adapter = nullptr;
428 }
429 JFR_ONLY(REMOVE_METHOD_ID(this);)
430 }
431
432 void Method::restore_unshareable_info(TRAPS) {
433 assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
434 if (method_data() != nullptr) {
435 method_data()->restore_unshareable_info(CHECK);
436 }
437 if (method_counters() != nullptr) {
438 method_counters()->restore_unshareable_info(CHECK);
439 }
440 if (_adapter != nullptr) {
441 assert(_adapter->is_linked(), "must be");
442 _from_compiled_entry = _adapter->get_c2i_entry();
443 }
444 assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
445 }
446 #endif
447
448 void Method::set_vtable_index(int index) {
449 if (in_aot_cache() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
450 // At runtime initialize_vtable is rerun as part of link_class_impl()
451 // for a shared class loaded by the non-boot loader to obtain the loader
452 // constraints based on the runtime classloaders' context.
453 return; // don't write into the shared class
454 } else {
455 _vtable_index = index;
456 }
457 }
458
459 void Method::set_itable_index(int index) {
460 if (in_aot_cache() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
461 // At runtime initialize_itable is rerun as part of link_class_impl()
462 // for a shared class loaded by the non-boot loader to obtain the loader
712 bool Method::init_method_counters(MethodCounters* counters) {
713 // Try to install a pointer to MethodCounters, return true on success.
714 return Atomic::replace_if_null(&_method_counters, counters);
715 }
716
717 void Method::set_exception_handler_entered(int handler_bci) {
718 if (ProfileExceptionHandlers) {
719 MethodData* mdo = method_data();
720 if (mdo != nullptr) {
721 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
722 handler_data.set_exception_handler_entered();
723 }
724 }
725 }
726
727 int Method::extra_stack_words() {
728 // not an inline function, to avoid a header dependency on Interpreter
729 return extra_stack_entries() * Interpreter::stackElementSize;
730 }
731
732 bool Method::compute_has_loops_flag() {
733 BytecodeStream bcs(methodHandle(Thread::current(), this));
734 Bytecodes::Code bc;
735
736 while ((bc = bcs.next()) >= 0) {
737 switch (bc) {
738 case Bytecodes::_ifeq:
739 case Bytecodes::_ifnull:
740 case Bytecodes::_iflt:
741 case Bytecodes::_ifle:
742 case Bytecodes::_ifne:
743 case Bytecodes::_ifnonnull:
744 case Bytecodes::_ifgt:
745 case Bytecodes::_ifge:
746 case Bytecodes::_if_icmpeq:
747 case Bytecodes::_if_icmpne:
748 case Bytecodes::_if_icmplt:
749 case Bytecodes::_if_icmpgt:
750 case Bytecodes::_if_icmple:
751 case Bytecodes::_if_icmpge:
860
861 bool Method::is_accessor() const {
862 return is_getter() || is_setter();
863 }
864
865 bool Method::is_getter() const {
866 if (code_size() != 5) return false;
867 if (size_of_parameters() != 1) return false;
868 if (java_code_at(0) != Bytecodes::_aload_0) return false;
869 if (java_code_at(1) != Bytecodes::_getfield) return false;
870 switch (java_code_at(4)) {
871 case Bytecodes::_ireturn:
872 case Bytecodes::_lreturn:
873 case Bytecodes::_freturn:
874 case Bytecodes::_dreturn:
875 case Bytecodes::_areturn:
876 break;
877 default:
878 return false;
879 }
880 return true;
881 }
882
883 bool Method::is_setter() const {
884 if (code_size() != 6) return false;
885 if (java_code_at(0) != Bytecodes::_aload_0) return false;
886 switch (java_code_at(1)) {
887 case Bytecodes::_iload_1:
888 case Bytecodes::_aload_1:
889 case Bytecodes::_fload_1:
890 if (size_of_parameters() != 2) return false;
891 break;
892 case Bytecodes::_dload_1:
893 case Bytecodes::_lload_1:
894 if (size_of_parameters() != 3) return false;
895 break;
896 default:
897 return false;
898 }
899 if (java_code_at(2) != Bytecodes::_putfield) return false;
900 if (java_code_at(5) != Bytecodes::_return) return false;
901 return true;
902 }
903
904 bool Method::is_constant_getter() const {
905 int last_index = code_size() - 1;
906 // Check if the first 1-3 bytecodes are a constant push
907 // and the last bytecode is a return.
908 return (2 <= code_size() && code_size() <= 4 &&
909 Bytecodes::is_const(java_code_at(0)) &&
910 Bytecodes::length_for(java_code_at(0)) == last_index &&
911 Bytecodes::is_return(java_code_at(last_index)));
912 }
913
914 bool Method::has_valid_initializer_flags() const {
915 return (is_static() ||
916 method_holder()->major_version() < 51);
917 }
918
919 bool Method::is_static_initializer() const {
920 // For classfiles version 51 or greater, ensure that the clinit method is
921 // static. Non-static methods with the name "<clinit>" are not static
922 // initializers. (older classfiles exempted for backward compatibility)
923 return name() == vmSymbols::class_initializer_name() &&
924 has_valid_initializer_flags();
925 }
926
927 bool Method::is_object_initializer() const {
928 return name() == vmSymbols::object_initializer_name();
929 }
930
931 bool Method::needs_clinit_barrier() const {
932 return is_static() && !method_holder()->is_initialized();
933 }
934
935 bool Method::is_object_wait0() const {
936 return klass_name() == vmSymbols::java_lang_Object()
937 && name() == vmSymbols::wait_name();
938 }
939
940 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
941 int length = method->checked_exceptions_length();
942 if (length == 0) { // common case
943 return objArrayHandle(THREAD, Universe::the_empty_class_array());
944 } else {
945 methodHandle h_this(THREAD, method);
946 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
947 objArrayHandle mirrors (THREAD, m_oop);
948 for (int i = 0; i < length; i++) {
971 // Not necessarily sorted and not necessarily one-to-one.
972 CompressedLineNumberReadStream stream(compressed_linenumber_table());
973 while (stream.read_pair()) {
974 if (stream.bci() == bci) {
975 // perfect match
976 return stream.line();
977 } else {
978 // update best_bci/line
979 if (stream.bci() < bci && stream.bci() >= best_bci) {
980 best_bci = stream.bci();
981 best_line = stream.line();
982 }
983 }
984 }
985 }
986 return best_line;
987 }
988
989
990 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
991 if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
992 Thread *thread = Thread::current();
993 Symbol* klass_name = constants()->klass_name_at(klass_index);
994 Handle loader(thread, method_holder()->class_loader());
995 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
996 } else {
997 return true;
998 }
999 }
1000
1001
1002 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1003 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
1004 if (must_be_resolved) {
1005 // Make sure klass is resolved in constantpool.
1006 if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;
1007 }
1008 return is_klass_loaded_by_klass_index(klass_index);
1009 }
1010
1011
1012 void Method::set_native_function(address function, bool post_event_flag) {
1013 assert(function != nullptr, "use clear_native_function to unregister natives");
1014 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
1015 address* native_function = native_function_addr();
1016
1017 // We can see racers trying to place the same native function into place. Once
1018 // is plenty.
1019 address current = *native_function;
1020 if (current == function) return;
1021 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1022 function != nullptr) {
1023 // native_method_throw_unsatisfied_link_error_entry() should only
1024 // be passed when post_event_flag is false.
1025 assert(function !=
1026 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1154
1155 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1156 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1157 if (comp_level == CompLevel_all) {
1158 set_is_not_c1_osr_compilable();
1159 set_is_not_c2_osr_compilable();
1160 } else {
1161 if (is_c1_compile(comp_level))
1162 set_is_not_c1_osr_compilable();
1163 if (is_c2_compile(comp_level))
1164 set_is_not_c2_osr_compilable();
1165 }
1166 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1167 }
1168
1169 // Revert to using the interpreter and clear out the nmethod
1170 void Method::clear_code() {
1171 // this may be null if c2i adapters have not been made yet
1172 // Only should happen at allocate time.
1173 if (adapter() == nullptr) {
1174 _from_compiled_entry = nullptr;
1175 } else {
1176 _from_compiled_entry = adapter()->get_c2i_entry();
1177 }
1178 OrderAccess::storestore();
1179 _from_interpreted_entry = _i2i_entry;
1180 OrderAccess::storestore();
1181 _code = nullptr;
1182 }
1183
1184 void Method::unlink_code(nmethod *compare) {
1185 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1186 // We need to check if either the _code or _from_compiled_code_entry_point
1187 // refer to this nmethod because there is a race in setting these two fields
1188 // in Method* as seen in bugid 4947125.
1189 if (code() == compare ||
1190 from_compiled_entry() == compare->verified_entry_point()) {
1191 clear_code();
1192 }
1193 }
1194
1195 void Method::unlink_code() {
1196 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1197 clear_code();
1198 }
1199
1200 #if INCLUDE_CDS
1201 // Called by class data sharing to remove any entry points (which are not shared)
1202 void Method::unlink_method() {
1203 assert(CDSConfig::is_dumping_archive(), "sanity");
1204 _code = nullptr;
1205 if (!CDSConfig::is_dumping_adapters()) {
1206 _adapter = nullptr;
1207 }
1208 _i2i_entry = nullptr;
1209 _from_compiled_entry = nullptr;
1210 _from_interpreted_entry = nullptr;
1211
1212 if (is_native()) {
1213 *native_function_addr() = nullptr;
1214 set_signature_handler(nullptr);
1215 }
1216 NOT_PRODUCT(set_compiled_invocation_count(0);)
1217
1218 clear_method_data();
1219 clear_method_counters();
1220 clear_is_not_c1_compilable();
1221 clear_is_not_c1_osr_compilable();
1222 clear_is_not_c2_compilable();
1223 clear_is_not_c2_osr_compilable();
1224 clear_queued_for_compilation();
1225
1226 remove_unshareable_flags();
1227 }
1228
1229 void Method::remove_unshareable_flags() {
1230 // clear all the flags that shouldn't be in the archived version
1231 assert(!is_old(), "must be");
1232 assert(!is_obsolete(), "must be");
1233 assert(!is_deleted(), "must be");
1234
1235 set_is_prefixed_native(false);
1236 set_queued_for_compilation(false);
1237 set_is_not_c2_compilable(false);
1238 set_is_not_c1_compilable(false);
1239 set_is_not_c2_osr_compilable(false);
1240 set_on_stack_flag(false);
1241 }
1242 #endif
1243
1244 // Called when the method_holder is getting linked. Setup entrypoints so the method
1245 // is ready to be called from interpreter, compiler, and vtables.
1246 void Method::link_method(const methodHandle& h_method, TRAPS) {
1247 if (log_is_enabled(Info, perf, class, link)) {
1248 ClassLoader::perf_ik_link_methods_count()->inc();
1249 }
1250
1251 // If the code cache is full, we may reenter this function for the
1252 // leftover methods that weren't linked.
1253 if (adapter() != nullptr) {
1254 if (adapter()->in_aot_cache()) {
1255 assert(adapter()->is_linked(), "Adapter is shared but not linked");
1256 } else {
1257 return;
1258 }
1259 }
1260 assert( _code == nullptr, "nothing compiled yet" );
1261
1262 // Setup interpreter entrypoint
1263 assert(this == h_method(), "wrong h_method()" );
1264
1265 assert(adapter() == nullptr || adapter()->is_linked(), "init'd to null or restored from cache");
1266 address entry = Interpreter::entry_for_method(h_method);
1267 assert(entry != nullptr, "interpreter entry must be non-null");
1268 // Sets both _i2i_entry and _from_interpreted_entry
1269 set_interpreter_entry(entry);
1270
1271 // Don't overwrite already registered native entries.
1272 if (is_native() && !has_native_function()) {
1273 set_native_function(
1274 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1275 !native_bind_event_is_interesting);
1276 }
1277
1278 // Setup compiler entrypoint. This is made eagerly, so we do not need
1279 // special handling of vtables. An alternative is to make adapters more
1280 // lazily by calling make_adapter() from from_compiled_entry() for the
1281 // normal calls. For vtable calls life gets more complicated. When a
1282 // call-site goes mega-morphic we need adapters in all methods which can be
1283 // called from the vtable. We need adapters on such methods that get loaded
1284 // later. Ditto for mega-morphic itable calls. If this proves to be a
1285 // problem we'll make these lazily later.
1286 if (is_abstract()) {
1287 h_method->_from_compiled_entry = SharedRuntime::get_handle_wrong_method_abstract_stub();
1288 } else if (_adapter == nullptr) {
1289 (void) make_adapters(h_method, CHECK);
1290 assert(adapter()->is_linked(), "Adapter must have been linked");
1291 h_method->_from_compiled_entry = adapter()->get_c2i_entry();
1292 }
1293
1294 // ONLY USE the h_method now as make_adapter may have blocked
1295
1296 if (h_method->is_continuation_native_intrinsic()) {
1297 _from_interpreted_entry = nullptr;
1298 _from_compiled_entry = nullptr;
1299 _i2i_entry = nullptr;
1300 if (Continuations::enabled()) {
1301 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1302 AdapterHandlerLibrary::create_native_wrapper(h_method);
1303 if (!h_method->has_compiled_code()) {
1304 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1305 }
1306 assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1307 }
1308 }
1309 }
1310
1311 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1327 }
1328 }
1329
1330 mh->set_adapter_entry(adapter);
1331 return adapter->get_c2i_entry();
1332 }
1333
1334 // The verified_code_entry() must be called when a invoke is resolved
1335 // on this method.
1336
1337 // It returns the compiled code entry point, after asserting not null.
1338 // This function is called after potential safepoints so that nmethod
1339 // or adapter that it points to is still live and valid.
1340 // This function must not hit a safepoint!
1341 address Method::verified_code_entry() {
1342 DEBUG_ONLY(NoSafepointVerifier nsv;)
1343 assert(_from_compiled_entry != nullptr, "must be set");
1344 return _from_compiled_entry;
1345 }
1346
1347 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1348 // (could be racing a deopt).
1349 // Not inline to avoid circular ref.
1350 bool Method::check_code() const {
1351 // cached in a register or local. There's a race on the value of the field.
1352 nmethod *code = Atomic::load_acquire(&_code);
1353 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1354 }
1355
1356 // Install compiled code. Instantly it can execute.
1357 void Method::set_code(const methodHandle& mh, nmethod *code) {
1358 assert_lock_strong(NMethodState_lock);
1359 assert( code, "use clear_code to remove code" );
1360 assert( mh->check_code(), "" );
1361
1362 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1363
1364 // These writes must happen in this order, because the interpreter will
1365 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1366 // which jumps to _from_compiled_entry.
1367 mh->_code = code; // Assign before allowing compiled code to exec
1368
1369 int comp_level = code->comp_level();
1370 // In theory there could be a race here. In practice it is unlikely
1371 // and not worth worrying about.
1372 if (comp_level > mh->highest_comp_level()) {
1373 mh->set_highest_comp_level(comp_level);
1374 }
1375
1376 OrderAccess::storestore();
1377 mh->_from_compiled_entry = code->verified_entry_point();
1378 OrderAccess::storestore();
1379
1380 if (mh->is_continuation_native_intrinsic()) {
1381 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1382
1383 if (mh->is_continuation_enter_intrinsic()) {
1384 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1385 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1386 } else if (mh->is_continuation_yield_intrinsic()) {
1387 mh->_i2i_entry = mh->get_i2c_entry();
1388 } else {
1389 guarantee(false, "Unknown Continuation native intrinsic");
1390 }
1391 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1392 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1393 } else if (!mh->is_method_handle_intrinsic()) {
1394 // Instantly compiled code can execute.
1395 mh->_from_interpreted_entry = mh->get_i2c_entry();
1396 }
1397 }
1550 assert(m->can_be_statically_bound(), "");
1551 m->set_vtable_index(Method::nonvirtual_vtable_index);
1552 m->link_method(m, CHECK_(empty));
1553
1554 if (iid == vmIntrinsics::_linkToNative) {
1555 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1556 }
1557 if (log_is_enabled(Debug, methodhandles)) {
1558 LogTarget(Debug, methodhandles) lt;
1559 LogStream ls(lt);
1560 m->print_on(&ls);
1561 }
1562
1563 return m;
1564 }
1565
1566 #if INCLUDE_CDS
1567 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1568 if (m->adapter() != nullptr) {
1569 m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1570 }
1571 m->link_method(m, CHECK);
1572
1573 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1574 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1575 }
1576 }
1577 #endif
1578
1579 Klass* Method::check_non_bcp_klass(Klass* klass) {
1580 if (klass != nullptr && klass->class_loader() != nullptr) {
1581 if (klass->is_objArray_klass())
1582 klass = ObjArrayKlass::cast(klass)->bottom_klass();
1583 return klass;
1584 }
1585 return nullptr;
1586 }
1587
1588
1589 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
2167 }
2168
2169 // Check that this pointer is valid by checking that the vtbl pointer matches
2170 bool Method::is_valid_method(const Method* m) {
2171 if (m == nullptr) {
2172 return false;
2173 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2174 // Quick sanity check on pointer.
2175 return false;
2176 } else if (!os::is_readable_range(m, m + 1)) {
2177 return false;
2178 } else if (m->in_aot_cache()) {
2179 return CppVtables::is_valid_shared_method(m);
2180 } else if (Metaspace::contains_non_shared(m)) {
2181 return has_method_vptr((const void*)m);
2182 } else {
2183 return false;
2184 }
2185 }
2186
2187 // Printing
2188
2189 #ifndef PRODUCT
2190
2191 void Method::print_on(outputStream* st) const {
2192 ResourceMark rm;
2193 assert(is_method(), "must be method");
2194 st->print_cr("%s", internal_name());
2195 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2196 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2197 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2198 constants()->print_value_on(st); st->cr();
2199 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2200 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2201 st->print (" - name: "); name()->print_value_on(st); st->cr();
2202 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2203 st->print_cr(" - max stack: %d", max_stack());
2204 st->print_cr(" - max locals: %d", max_locals());
2205 st->print_cr(" - size of params: %d", size_of_parameters());
2206 st->print_cr(" - method size: %d", method_size());
2207 if (intrinsic_id() != vmIntrinsics::_none)
2208 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2209 if (highest_comp_level() != CompLevel_none)
2210 st->print_cr(" - highest level: %d", highest_comp_level());
2211 st->print_cr(" - vtable index: %d", _vtable_index);
2212 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2213 st->print( " - adapters: ");
2214 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2215 if (a == nullptr)
2216 st->print_cr(PTR_FORMAT, p2i(a));
2217 else
2218 a->print_adapter_on(st);
2219 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2220 st->print_cr(" - code size: %d", code_size());
2221 if (code_size() != 0) {
2222 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2223 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2224 }
2225 if (method_data() != nullptr) {
2226 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2227 }
2228 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2229 if (checked_exceptions_length() > 0) {
2230 CheckedExceptionElement* table = checked_exceptions_start();
2231 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2232 if (Verbose) {
2233 for (int i = 0; i < checked_exceptions_length(); i++) {
2234 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2235 }
2236 }
2237 }
2238 if (has_linenumber_table()) {
2239 u_char* table = compressed_linenumber_table();
2269 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2270 }
2271 }
2272
2273 void Method::print_linkage_flags(outputStream* st) {
2274 access_flags().print_on(st);
2275 if (is_default_method()) {
2276 st->print("default ");
2277 }
2278 if (is_overpass()) {
2279 st->print("overpass ");
2280 }
2281 }
2282 #endif //PRODUCT
2283
2284 void Method::print_value_on(outputStream* st) const {
2285 assert(is_method(), "must be method");
2286 st->print("%s", internal_name());
2287 print_address_on(st);
2288 st->print(" ");
2289 name()->print_value_on(st);
2290 st->print(" ");
2291 signature()->print_value_on(st);
2292 st->print(" in ");
2293 method_holder()->print_value_on(st);
2294 if (WizardMode) st->print("#%d", _vtable_index);
2295 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2296 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2297 }
2298
2299 // Verification
2300
2301 void Method::verify_on(outputStream* st) {
2302 guarantee(is_method(), "object must be method");
2303 guarantee(constants()->is_constantPool(), "should be constant pool");
2304 MethodData* md = method_data();
2305 guarantee(md == nullptr ||
2306 md->is_methodData(), "should be method data");
2307 }
|
36 #include "code/debugInfoRec.hpp"
37 #include "compiler/compilationPolicy.hpp"
38 #include "gc/shared/collectedHeap.inline.hpp"
39 #include "interpreter/bytecodes.hpp"
40 #include "interpreter/bytecodeStream.hpp"
41 #include "interpreter/bytecodeTracer.hpp"
42 #include "interpreter/interpreter.hpp"
43 #include "interpreter/oopMapCache.hpp"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "logging/logTag.hpp"
47 #include "memory/allocation.inline.hpp"
48 #include "memory/metadataFactory.hpp"
49 #include "memory/metaspaceClosure.hpp"
50 #include "memory/oopFactory.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "nmt/memTracker.hpp"
54 #include "oops/constantPool.hpp"
55 #include "oops/constMethod.hpp"
56 #include "oops/inlineKlass.inline.hpp"
57 #include "oops/jmethodIDTable.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/method.inline.hpp"
60 #include "oops/methodData.hpp"
61 #include "oops/objArrayKlass.hpp"
62 #include "oops/objArrayOop.inline.hpp"
63 #include "oops/oop.inline.hpp"
64 #include "oops/symbol.hpp"
65 #include "oops/trainingData.hpp"
66 #include "prims/jvmtiExport.hpp"
67 #include "prims/methodHandles.hpp"
68 #include "runtime/arguments.hpp"
69 #include "runtime/atomic.hpp"
70 #include "runtime/continuationEntry.hpp"
71 #include "runtime/frame.inline.hpp"
72 #include "runtime/handles.inline.hpp"
73 #include "runtime/init.hpp"
74 #include "runtime/java.hpp"
75 #include "runtime/orderAccess.hpp"
76 #include "runtime/perfData.hpp"
106 }
107
108 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
109 NoSafepointVerifier no_safepoint;
110 set_constMethod(xconst);
111 set_access_flags(access_flags);
112 set_intrinsic_id(vmIntrinsics::_none);
113 clear_method_data();
114 clear_method_counters();
115 set_vtable_index(Method::garbage_vtable_index);
116
117 // Fix and bury in Method*
118 set_interpreter_entry(nullptr); // sets i2i entry and from_int
119 set_adapter_entry(nullptr);
120 Method::clear_code(); // from_c/from_i get set to c2i/i2i
121
122 if (access_flags.is_native()) {
123 clear_native_function();
124 set_signature_handler(nullptr);
125 }
126 NOT_PRODUCT(set_compiled_invocation_count(0);)
127 // Name is very useful for debugging.
128 NOT_PRODUCT(_name = name;)
129 }
130
131 // Release Method*. The nmethod will be gone when we get here because
132 // we've walked the code cache.
133 void Method::deallocate_contents(ClassLoaderData* loader_data) {
134 MetadataFactory::free_metadata(loader_data, constMethod());
135 set_constMethod(nullptr);
136 MetadataFactory::free_metadata(loader_data, method_data());
137 clear_method_data();
138 MetadataFactory::free_metadata(loader_data, method_counters());
139 clear_method_counters();
140 set_adapter_entry(nullptr);
141 // The nmethod will be gone when we get here.
142 if (code() != nullptr) _code = nullptr;
143 }
144
145 void Method::release_C_heap_structures() {
150 method_data()->~MethodData();
151 }
152 }
153
154 address Method::get_i2c_entry() {
155 if (is_abstract()) {
156 return SharedRuntime::throw_AbstractMethodError_entry();
157 }
158 assert(adapter() != nullptr, "must have");
159 return adapter()->get_i2c_entry();
160 }
161
162 address Method::get_c2i_entry() {
163 if (is_abstract()) {
164 return SharedRuntime::get_handle_wrong_method_abstract_stub();
165 }
166 assert(adapter() != nullptr, "must have");
167 return adapter()->get_c2i_entry();
168 }
169
170 address Method::get_c2i_inline_entry() {
171 assert(adapter() != nullptr, "must have");
172 return adapter()->get_c2i_inline_entry();
173 }
174
175 address Method::get_c2i_unverified_entry() {
176 assert(adapter() != nullptr, "must have");
177 return adapter()->get_c2i_unverified_entry();
178 }
179
180 address Method::get_c2i_unverified_inline_entry() {
181 assert(adapter() != nullptr, "must have");
182 return adapter()->get_c2i_unverified_inline_entry();
183 }
184
185 address Method::get_c2i_no_clinit_check_entry() {
186 assert(VM_Version::supports_fast_class_init_checks(), "");
187 assert(adapter() != nullptr, "must have");
188 return adapter()->get_c2i_no_clinit_check_entry();
189 }
190
191 char* Method::name_and_sig_as_C_string() const {
192 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
193 }
194
195 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
196 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
197 }
198
199 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
200 const char* klass_name = klass->external_name();
201 int klass_name_len = (int)strlen(klass_name);
202 int method_name_len = method_name->utf8_length();
203 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
204 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
390 return code_base();
391 } else {
392 return bcp;
393 }
394 }
395
396 int Method::size(bool is_native) {
397 // If native, then include pointers for native_function and signature_handler
398 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
399 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
400 return align_metadata_size(header_size() + extra_words);
401 }
402
403 Symbol* Method::klass_name() const {
404 return method_holder()->name();
405 }
406
407 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
408 log_trace(aot)("Iter(Method): %p", this);
409
410 if (!method_holder()->is_rewritten() || CDSConfig::is_valhalla_preview()) {
411 it->push(&_constMethod, MetaspaceClosure::_writable);
412 } else {
413 it->push(&_constMethod);
414 }
415 it->push(&_adapter);
416 it->push(&_method_data);
417 it->push(&_method_counters);
418 NOT_PRODUCT(it->push(&_name);)
419 }
420
421 #if INCLUDE_CDS
422 // Attempt to return method to original state. Clear any pointers
423 // (to objects outside the shared spaces). We won't be able to predict
424 // where they should point in a new JVM. Further initialize some
425 // entries now in order allow them to be write protected later.
426
427 void Method::remove_unshareable_info() {
428 unlink_method();
429 if (method_data() != nullptr) {
430 method_data()->remove_unshareable_info();
433 method_counters()->remove_unshareable_info();
434 }
435 if (CDSConfig::is_dumping_adapters() && _adapter != nullptr) {
436 _adapter->remove_unshareable_info();
437 _adapter = nullptr;
438 }
439 JFR_ONLY(REMOVE_METHOD_ID(this);)
440 }
441
442 void Method::restore_unshareable_info(TRAPS) {
443 assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
444 if (method_data() != nullptr) {
445 method_data()->restore_unshareable_info(CHECK);
446 }
447 if (method_counters() != nullptr) {
448 method_counters()->restore_unshareable_info(CHECK);
449 }
450 if (_adapter != nullptr) {
451 assert(_adapter->is_linked(), "must be");
452 _from_compiled_entry = _adapter->get_c2i_entry();
453 _from_compiled_inline_entry = _adapter->get_c2i_inline_entry();
454 _from_compiled_inline_ro_entry = _adapter->get_c2i_inline_ro_entry();
455 }
456 assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
457 }
458 #endif
459
460 void Method::set_vtable_index(int index) {
461 if (in_aot_cache() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
462 // At runtime initialize_vtable is rerun as part of link_class_impl()
463 // for a shared class loaded by the non-boot loader to obtain the loader
464 // constraints based on the runtime classloaders' context.
465 return; // don't write into the shared class
466 } else {
467 _vtable_index = index;
468 }
469 }
470
471 void Method::set_itable_index(int index) {
472 if (in_aot_cache() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
473 // At runtime initialize_itable is rerun as part of link_class_impl()
474 // for a shared class loaded by the non-boot loader to obtain the loader
724 bool Method::init_method_counters(MethodCounters* counters) {
725 // Try to install a pointer to MethodCounters, return true on success.
726 return Atomic::replace_if_null(&_method_counters, counters);
727 }
728
729 void Method::set_exception_handler_entered(int handler_bci) {
730 if (ProfileExceptionHandlers) {
731 MethodData* mdo = method_data();
732 if (mdo != nullptr) {
733 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
734 handler_data.set_exception_handler_entered();
735 }
736 }
737 }
738
739 int Method::extra_stack_words() {
740 // not an inline function, to avoid a header dependency on Interpreter
741 return extra_stack_entries() * Interpreter::stackElementSize;
742 }
743
744 // InlineKlass the method is declared to return. This must not
745 // safepoint as it is called with references live on the stack at
746 // locations the GC is unaware of.
747 InlineKlass* Method::returns_inline_type() const {
748 assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
749 if (is_native()) {
750 return nullptr;
751 }
752 NoSafepointVerifier nsv;
753 SignatureStream ss(signature());
754 ss.skip_to_return_type();
755 return ss.as_inline_klass(method_holder());
756 }
757
758 bool Method::compute_has_loops_flag() {
759 BytecodeStream bcs(methodHandle(Thread::current(), this));
760 Bytecodes::Code bc;
761
762 while ((bc = bcs.next()) >= 0) {
763 switch (bc) {
764 case Bytecodes::_ifeq:
765 case Bytecodes::_ifnull:
766 case Bytecodes::_iflt:
767 case Bytecodes::_ifle:
768 case Bytecodes::_ifne:
769 case Bytecodes::_ifnonnull:
770 case Bytecodes::_ifgt:
771 case Bytecodes::_ifge:
772 case Bytecodes::_if_icmpeq:
773 case Bytecodes::_if_icmpne:
774 case Bytecodes::_if_icmplt:
775 case Bytecodes::_if_icmpgt:
776 case Bytecodes::_if_icmple:
777 case Bytecodes::_if_icmpge:
886
887 bool Method::is_accessor() const {
888 return is_getter() || is_setter();
889 }
890
891 bool Method::is_getter() const {
892 if (code_size() != 5) return false;
893 if (size_of_parameters() != 1) return false;
894 if (java_code_at(0) != Bytecodes::_aload_0) return false;
895 if (java_code_at(1) != Bytecodes::_getfield) return false;
896 switch (java_code_at(4)) {
897 case Bytecodes::_ireturn:
898 case Bytecodes::_lreturn:
899 case Bytecodes::_freturn:
900 case Bytecodes::_dreturn:
901 case Bytecodes::_areturn:
902 break;
903 default:
904 return false;
905 }
906 if (has_scalarized_return()) {
907 // Don't treat this as (trivial) getter method because the
908 // inline type should be returned in a scalarized form.
909 return false;
910 }
911 return true;
912 }
913
914 bool Method::is_setter() const {
915 if (code_size() != 6) return false;
916 if (java_code_at(0) != Bytecodes::_aload_0) return false;
917 switch (java_code_at(1)) {
918 case Bytecodes::_iload_1:
919 case Bytecodes::_aload_1:
920 case Bytecodes::_fload_1:
921 if (size_of_parameters() != 2) return false;
922 break;
923 case Bytecodes::_dload_1:
924 case Bytecodes::_lload_1:
925 if (size_of_parameters() != 3) return false;
926 break;
927 default:
928 return false;
929 }
930 if (java_code_at(2) != Bytecodes::_putfield) return false;
931 if (java_code_at(5) != Bytecodes::_return) return false;
932 if (has_scalarized_args()) {
933 // Don't treat this as (trivial) setter method because the
934 // inline type argument should be passed in a scalarized form.
935 return false;
936 }
937 return true;
938 }
939
940 bool Method::is_constant_getter() const {
941 int last_index = code_size() - 1;
942 // Check if the first 1-3 bytecodes are a constant push
943 // and the last bytecode is a return.
944 return (2 <= code_size() && code_size() <= 4 &&
945 Bytecodes::is_const(java_code_at(0)) &&
946 Bytecodes::length_for(java_code_at(0)) == last_index &&
947 Bytecodes::is_return(java_code_at(last_index)) &&
948 !has_scalarized_args());
949 }
950
951 bool Method::is_class_initializer() const {
952 // For classfiles version 51 or greater, ensure that the clinit method is
953 // static. Non-static methods with the name "<clinit>" are not static
954 // initializers. (older classfiles exempted for backward compatibility)
955 return (name() == vmSymbols::class_initializer_name() &&
956 (is_static() ||
957 method_holder()->major_version() < 51));
958 }
959
960 // A method named <init>, is a classic object constructor.
961 bool Method::is_object_constructor() const {
962 return name() == vmSymbols::object_initializer_name();
963 }
964
965 bool Method::needs_clinit_barrier() const {
966 return is_static() && !method_holder()->is_initialized();
967 }
968
969 bool Method::is_object_wait0() const {
970 return klass_name() == vmSymbols::java_lang_Object()
971 && name() == vmSymbols::wait_name();
972 }
973
974 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
975 int length = method->checked_exceptions_length();
976 if (length == 0) { // common case
977 return objArrayHandle(THREAD, Universe::the_empty_class_array());
978 } else {
979 methodHandle h_this(THREAD, method);
980 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
981 objArrayHandle mirrors (THREAD, m_oop);
982 for (int i = 0; i < length; i++) {
1005 // Not necessarily sorted and not necessarily one-to-one.
1006 CompressedLineNumberReadStream stream(compressed_linenumber_table());
1007 while (stream.read_pair()) {
1008 if (stream.bci() == bci) {
1009 // perfect match
1010 return stream.line();
1011 } else {
1012 // update best_bci/line
1013 if (stream.bci() < bci && stream.bci() >= best_bci) {
1014 best_bci = stream.bci();
1015 best_line = stream.line();
1016 }
1017 }
1018 }
1019 }
1020 return best_line;
1021 }
1022
1023
1024 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
1025 if( constants()->tag_at(klass_index).is_unresolved_klass()) {
1026 Thread *thread = Thread::current();
1027 Symbol* klass_name = constants()->klass_name_at(klass_index);
1028 Handle loader(thread, method_holder()->class_loader());
1029 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
1030 } else {
1031 return true;
1032 }
1033 }
1034
1035
1036 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1037 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
1038 if (must_be_resolved) {
1039 // Make sure klass is resolved in constantpool.
1040 if (constants()->tag_at(klass_index).is_unresolved_klass()) {
1041 return false;
1042 }
1043 }
1044 return is_klass_loaded_by_klass_index(klass_index);
1045 }
1046
1047
1048 void Method::set_native_function(address function, bool post_event_flag) {
1049 assert(function != nullptr, "use clear_native_function to unregister natives");
1050 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
1051 address* native_function = native_function_addr();
1052
1053 // We can see racers trying to place the same native function into place. Once
1054 // is plenty.
1055 address current = *native_function;
1056 if (current == function) return;
1057 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1058 function != nullptr) {
1059 // native_method_throw_unsatisfied_link_error_entry() should only
1060 // be passed when post_event_flag is false.
1061 assert(function !=
1062 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1190
1191 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1192 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1193 if (comp_level == CompLevel_all) {
1194 set_is_not_c1_osr_compilable();
1195 set_is_not_c2_osr_compilable();
1196 } else {
1197 if (is_c1_compile(comp_level))
1198 set_is_not_c1_osr_compilable();
1199 if (is_c2_compile(comp_level))
1200 set_is_not_c2_osr_compilable();
1201 }
1202 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1203 }
1204
1205 // Revert to using the interpreter and clear out the nmethod
1206 void Method::clear_code() {
1207 // this may be null if c2i adapters have not been made yet
1208 // Only should happen at allocate time.
1209 if (adapter() == nullptr) {
1210 _from_compiled_entry = nullptr;
1211 _from_compiled_inline_entry = nullptr;
1212 _from_compiled_inline_ro_entry = nullptr;
1213 } else {
1214 _from_compiled_entry = adapter()->get_c2i_entry();
1215 _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1216 _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1217 }
1218 OrderAccess::storestore();
1219 _from_interpreted_entry = _i2i_entry;
1220 OrderAccess::storestore();
1221 _code = nullptr;
1222 }
1223
1224 void Method::unlink_code(nmethod *compare) {
1225 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1226 // We need to check if either the _code or _from_compiled_code_entry_point
1227 // refer to this nmethod because there is a race in setting these two fields
1228 // in Method* as seen in bugid 4947125.
1229 if (code() == compare ||
1230 from_compiled_entry() == compare->verified_entry_point()) {
1231 clear_code();
1232 }
1233 }
1234
1235 void Method::unlink_code() {
1236 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1237 clear_code();
1238 }
1239
1240 #if INCLUDE_CDS
1241 // Called by class data sharing to remove any entry points (which are not shared)
1242 void Method::unlink_method() {
1243 assert(CDSConfig::is_dumping_archive(), "sanity");
1244 _code = nullptr;
1245 if (!CDSConfig::is_dumping_adapters()) {
1246 _adapter = nullptr;
1247 }
1248 _i2i_entry = nullptr;
1249 _from_compiled_entry = nullptr;
1250 _from_compiled_inline_entry = nullptr;
1251 _from_compiled_inline_ro_entry = nullptr;
1252 _from_interpreted_entry = nullptr;
1253
1254 if (is_native()) {
1255 *native_function_addr() = nullptr;
1256 set_signature_handler(nullptr);
1257 }
1258 NOT_PRODUCT(set_compiled_invocation_count(0);)
1259
1260 clear_method_data();
1261 clear_method_counters();
1262 clear_is_not_c1_compilable();
1263 clear_is_not_c1_osr_compilable();
1264 clear_is_not_c2_compilable();
1265 clear_is_not_c2_osr_compilable();
1266 clear_queued_for_compilation();
1267
1268 remove_unshareable_flags();
1269 }
1270
1271 void Method::remove_unshareable_flags() {
1272 // clear all the flags that shouldn't be in the archived version
1273 assert(!is_old(), "must be");
1274 assert(!is_obsolete(), "must be");
1275 assert(!is_deleted(), "must be");
1276
1277 set_is_prefixed_native(false);
1278 set_queued_for_compilation(false);
1279 set_is_not_c2_compilable(false);
1280 set_is_not_c1_compilable(false);
1281 set_is_not_c2_osr_compilable(false);
1282 set_on_stack_flag(false);
1283 set_has_scalarized_args(false);
1284 set_has_scalarized_return(false);
1285 }
1286 #endif
1287
1288 // Called when the method_holder is getting linked. Setup entrypoints so the method
1289 // is ready to be called from interpreter, compiler, and vtables.
1290 void Method::link_method(const methodHandle& h_method, TRAPS) {
1291 if (log_is_enabled(Info, perf, class, link)) {
1292 ClassLoader::perf_ik_link_methods_count()->inc();
1293 }
1294
1295 // If the code cache is full, we may reenter this function for the
1296 // leftover methods that weren't linked.
1297 if (adapter() != nullptr) {
1298 if (adapter()->in_aot_cache()) {
1299 assert(adapter()->is_linked(), "Adapter is shared but not linked");
1300 } else {
1301 return;
1302 }
1303 }
1304 assert( _code == nullptr, "nothing compiled yet" );
1305
1306 // Setup interpreter entrypoint
1307 assert(this == h_method(), "wrong h_method()" );
1308
1309 assert(adapter() == nullptr || adapter()->is_linked(), "init'd to null or restored from cache");
1310 address entry = Interpreter::entry_for_method(h_method);
1311 assert(entry != nullptr, "interpreter entry must be non-null");
1312 // Sets both _i2i_entry and _from_interpreted_entry
1313 set_interpreter_entry(entry);
1314
1315 // Don't overwrite already registered native entries.
1316 if (is_native() && !has_native_function()) {
1317 set_native_function(
1318 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1319 !native_bind_event_is_interesting);
1320 }
1321 if (InlineTypeReturnedAsFields && returns_inline_type() && !has_scalarized_return()) {
1322 set_has_scalarized_return();
1323 }
1324
1325 // Setup compiler entrypoint. This is made eagerly, so we do not need
1326 // special handling of vtables. An alternative is to make adapters more
1327 // lazily by calling make_adapter() from from_compiled_entry() for the
1328 // normal calls. For vtable calls life gets more complicated. When a
1329 // call-site goes mega-morphic we need adapters in all methods which can be
1330 // called from the vtable. We need adapters on such methods that get loaded
1331 // later. Ditto for mega-morphic itable calls. If this proves to be a
1332 // problem we'll make these lazily later.
1333 if (is_abstract()) {
1334 address wrong_method_abstract = SharedRuntime::get_handle_wrong_method_abstract_stub();
1335 h_method->_from_compiled_entry = wrong_method_abstract;
1336 h_method->_from_compiled_inline_entry = wrong_method_abstract;
1337 h_method->_from_compiled_inline_ro_entry = wrong_method_abstract;
1338 } else if (_adapter == nullptr) {
1339 (void) make_adapters(h_method, CHECK);
1340 assert(adapter()->is_linked(), "Adapter must have been linked");
1341 h_method->_from_compiled_entry = adapter()->get_c2i_entry();
1342 h_method->_from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1343 h_method->_from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1344 }
1345
1346 // ONLY USE the h_method now as make_adapter may have blocked
1347
1348 if (h_method->is_continuation_native_intrinsic()) {
1349 _from_interpreted_entry = nullptr;
1350 _from_compiled_entry = nullptr;
1351 _i2i_entry = nullptr;
1352 if (Continuations::enabled()) {
1353 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1354 AdapterHandlerLibrary::create_native_wrapper(h_method);
1355 if (!h_method->has_compiled_code()) {
1356 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1357 }
1358 assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1359 }
1360 }
1361 }
1362
1363 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1379 }
1380 }
1381
1382 mh->set_adapter_entry(adapter);
1383 return adapter->get_c2i_entry();
1384 }
1385
1386 // The verified_code_entry() must be called when a invoke is resolved
1387 // on this method.
1388
1389 // It returns the compiled code entry point, after asserting not null.
1390 // This function is called after potential safepoints so that nmethod
1391 // or adapter that it points to is still live and valid.
1392 // This function must not hit a safepoint!
1393 address Method::verified_code_entry() {
1394 DEBUG_ONLY(NoSafepointVerifier nsv;)
1395 assert(_from_compiled_entry != nullptr, "must be set");
1396 return _from_compiled_entry;
1397 }
1398
1399 address Method::verified_inline_code_entry() {
1400 DEBUG_ONLY(NoSafepointVerifier nsv;)
1401 assert(_from_compiled_inline_entry != nullptr, "must be set");
1402 return _from_compiled_inline_entry;
1403 }
1404
1405 address Method::verified_inline_ro_code_entry() {
1406 DEBUG_ONLY(NoSafepointVerifier nsv;)
1407 assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1408 return _from_compiled_inline_ro_entry;
1409 }
1410
1411 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1412 // (could be racing a deopt).
1413 // Not inline to avoid circular ref.
1414 bool Method::check_code() const {
1415 // cached in a register or local. There's a race on the value of the field.
1416 nmethod *code = Atomic::load_acquire(&_code);
1417 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1418 }
1419
1420 // Install compiled code. Instantly it can execute.
1421 void Method::set_code(const methodHandle& mh, nmethod *code) {
1422 assert_lock_strong(NMethodState_lock);
1423 assert( code, "use clear_code to remove code" );
1424 assert( mh->check_code(), "" );
1425
1426 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1427
1428 // These writes must happen in this order, because the interpreter will
1429 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1430 // which jumps to _from_compiled_entry.
1431 mh->_code = code; // Assign before allowing compiled code to exec
1432
1433 int comp_level = code->comp_level();
1434 // In theory there could be a race here. In practice it is unlikely
1435 // and not worth worrying about.
1436 if (comp_level > mh->highest_comp_level()) {
1437 mh->set_highest_comp_level(comp_level);
1438 }
1439
1440 OrderAccess::storestore();
1441 mh->_from_compiled_entry = code->verified_entry_point();
1442 mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1443 mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1444 OrderAccess::storestore();
1445
1446 if (mh->is_continuation_native_intrinsic()) {
1447 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1448
1449 if (mh->is_continuation_enter_intrinsic()) {
1450 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1451 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1452 } else if (mh->is_continuation_yield_intrinsic()) {
1453 mh->_i2i_entry = mh->get_i2c_entry();
1454 } else {
1455 guarantee(false, "Unknown Continuation native intrinsic");
1456 }
1457 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1458 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1459 } else if (!mh->is_method_handle_intrinsic()) {
1460 // Instantly compiled code can execute.
1461 mh->_from_interpreted_entry = mh->get_i2c_entry();
1462 }
1463 }
1616 assert(m->can_be_statically_bound(), "");
1617 m->set_vtable_index(Method::nonvirtual_vtable_index);
1618 m->link_method(m, CHECK_(empty));
1619
1620 if (iid == vmIntrinsics::_linkToNative) {
1621 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1622 }
1623 if (log_is_enabled(Debug, methodhandles)) {
1624 LogTarget(Debug, methodhandles) lt;
1625 LogStream ls(lt);
1626 m->print_on(&ls);
1627 }
1628
1629 return m;
1630 }
1631
1632 #if INCLUDE_CDS
1633 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1634 if (m->adapter() != nullptr) {
1635 m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1636 m->set_from_compiled_inline_entry(m->adapter()->get_c2i_inline_entry());
1637 m->set_from_compiled_inline_ro_entry(m->adapter()->get_c2i_inline_ro_entry());
1638 }
1639 m->link_method(m, CHECK);
1640
1641 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1642 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1643 }
1644 }
1645 #endif
1646
1647 Klass* Method::check_non_bcp_klass(Klass* klass) {
1648 if (klass != nullptr && klass->class_loader() != nullptr) {
1649 if (klass->is_objArray_klass())
1650 klass = ObjArrayKlass::cast(klass)->bottom_klass();
1651 return klass;
1652 }
1653 return nullptr;
1654 }
1655
1656
1657 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
2235 }
2236
2237 // Check that this pointer is valid by checking that the vtbl pointer matches
2238 bool Method::is_valid_method(const Method* m) {
2239 if (m == nullptr) {
2240 return false;
2241 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2242 // Quick sanity check on pointer.
2243 return false;
2244 } else if (!os::is_readable_range(m, m + 1)) {
2245 return false;
2246 } else if (m->in_aot_cache()) {
2247 return CppVtables::is_valid_shared_method(m);
2248 } else if (Metaspace::contains_non_shared(m)) {
2249 return has_method_vptr((const void*)m);
2250 } else {
2251 return false;
2252 }
2253 }
2254
2255 bool Method::is_scalarized_arg(int idx) const {
2256 if (!has_scalarized_args()) {
2257 return false;
2258 }
2259 // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2260 int depth = 0;
2261 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2262 for (int i = 0; i < sig->length(); i++) {
2263 BasicType bt = sig->at(i)._bt;
2264 if (bt == T_METADATA) {
2265 depth++;
2266 }
2267 if (idx == 0) {
2268 break; // Argument found
2269 }
2270 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2271 depth--;
2272 }
2273 if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2274 idx--; // Advance to next argument
2275 }
2276 }
2277 return depth != 0;
2278 }
2279
2280 // Printing
2281
2282 #ifndef PRODUCT
2283
2284 void Method::print_on(outputStream* st) const {
2285 ResourceMark rm;
2286 assert(is_method(), "must be method");
2287 st->print_cr("%s", internal_name());
2288 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2289 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2290 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2291 constants()->print_value_on(st); st->cr();
2292 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2293 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2294 st->print (" - name: "); name()->print_value_on(st); st->cr();
2295 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2296 st->print_cr(" - max stack: %d", max_stack());
2297 st->print_cr(" - max locals: %d", max_locals());
2298 st->print_cr(" - size of params: %d", size_of_parameters());
2299 st->print_cr(" - method size: %d", method_size());
2300 if (intrinsic_id() != vmIntrinsics::_none)
2301 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2302 if (highest_comp_level() != CompLevel_none)
2303 st->print_cr(" - highest level: %d", highest_comp_level());
2304 st->print_cr(" - vtable index: %d", _vtable_index);
2305 #ifdef ASSERT
2306 if (valid_itable_index())
2307 st->print_cr(" - itable index: %d", itable_index());
2308 #endif
2309 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2310 st->print( " - adapters: ");
2311 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2312 if (a == nullptr)
2313 st->print_cr(PTR_FORMAT, p2i(a));
2314 else
2315 a->print_adapter_on(st);
2316 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2317 st->print_cr(" - compiled inline entry " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2318 st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2319 st->print_cr(" - code size: %d", code_size());
2320 if (code_size() != 0) {
2321 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2322 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2323 }
2324 if (method_data() != nullptr) {
2325 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2326 }
2327 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2328 if (checked_exceptions_length() > 0) {
2329 CheckedExceptionElement* table = checked_exceptions_start();
2330 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2331 if (Verbose) {
2332 for (int i = 0; i < checked_exceptions_length(); i++) {
2333 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2334 }
2335 }
2336 }
2337 if (has_linenumber_table()) {
2338 u_char* table = compressed_linenumber_table();
2368 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2369 }
2370 }
2371
2372 void Method::print_linkage_flags(outputStream* st) {
2373 access_flags().print_on(st);
2374 if (is_default_method()) {
2375 st->print("default ");
2376 }
2377 if (is_overpass()) {
2378 st->print("overpass ");
2379 }
2380 }
2381 #endif //PRODUCT
2382
2383 void Method::print_value_on(outputStream* st) const {
2384 assert(is_method(), "must be method");
2385 st->print("%s", internal_name());
2386 print_address_on(st);
2387 st->print(" ");
2388 if (WizardMode) access_flags().print_on(st);
2389 name()->print_value_on(st);
2390 st->print(" ");
2391 signature()->print_value_on(st);
2392 st->print(" in ");
2393 method_holder()->print_value_on(st);
2394 if (WizardMode) st->print("#%d", _vtable_index);
2395 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2396 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2397 }
2398
2399 // Verification
2400
2401 void Method::verify_on(outputStream* st) {
2402 guarantee(is_method(), "object must be method");
2403 guarantee(constants()->is_constantPool(), "should be constant pool");
2404 MethodData* md = method_data();
2405 guarantee(md == nullptr ||
2406 md->is_methodData(), "should be method data");
2407 }
|