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/atomicAccess.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() && !AOTMetaspace::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() && !AOTMetaspace::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 AtomicAccess::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 #ifndef ZERO
1291 assert(adapter()->is_linked(), "Adapter must have been linked");
1292 #endif
1293 h_method->_from_compiled_entry = adapter()->get_c2i_entry();
1294 }
1295
1296 // ONLY USE the h_method now as make_adapter may have blocked
1297
1298 if (h_method->is_continuation_native_intrinsic()) {
1299 _from_interpreted_entry = nullptr;
1300 _from_compiled_entry = nullptr;
1301 _i2i_entry = nullptr;
1302 if (Continuations::enabled()) {
1303 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1304 AdapterHandlerLibrary::create_native_wrapper(h_method);
1305 if (!h_method->has_compiled_code()) {
1306 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1307 }
1308 assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1309 }
1310 }
1311 }
1312
1313 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1329 }
1330 }
1331
1332 mh->set_adapter_entry(adapter);
1333 return adapter->get_c2i_entry();
1334 }
1335
1336 // The verified_code_entry() must be called when a invoke is resolved
1337 // on this method.
1338
1339 // It returns the compiled code entry point, after asserting not null.
1340 // This function is called after potential safepoints so that nmethod
1341 // or adapter that it points to is still live and valid.
1342 // This function must not hit a safepoint!
1343 address Method::verified_code_entry() {
1344 DEBUG_ONLY(NoSafepointVerifier nsv;)
1345 assert(_from_compiled_entry != nullptr, "must be set");
1346 return _from_compiled_entry;
1347 }
1348
1349 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1350 // (could be racing a deopt).
1351 // Not inline to avoid circular ref.
1352 bool Method::check_code() const {
1353 // cached in a register or local. There's a race on the value of the field.
1354 nmethod *code = AtomicAccess::load_acquire(&_code);
1355 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1356 }
1357
1358 // Install compiled code. Instantly it can execute.
1359 void Method::set_code(const methodHandle& mh, nmethod *code) {
1360 assert_lock_strong(NMethodState_lock);
1361 assert( code, "use clear_code to remove code" );
1362 assert( mh->check_code(), "" );
1363
1364 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1365
1366 // These writes must happen in this order, because the interpreter will
1367 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1368 // which jumps to _from_compiled_entry.
1369 mh->_code = code; // Assign before allowing compiled code to exec
1370
1371 int comp_level = code->comp_level();
1372 // In theory there could be a race here. In practice it is unlikely
1373 // and not worth worrying about.
1374 if (comp_level > mh->highest_comp_level()) {
1375 mh->set_highest_comp_level(comp_level);
1376 }
1377
1378 OrderAccess::storestore();
1379 mh->_from_compiled_entry = code->verified_entry_point();
1380 OrderAccess::storestore();
1381
1382 if (mh->is_continuation_native_intrinsic()) {
1383 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1384
1385 if (mh->is_continuation_enter_intrinsic()) {
1386 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1387 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1388 } else if (mh->is_continuation_yield_intrinsic()) {
1389 mh->_i2i_entry = mh->get_i2c_entry();
1390 } else {
1391 guarantee(false, "Unknown Continuation native intrinsic");
1392 }
1393 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1394 AtomicAccess::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1395 } else if (!mh->is_method_handle_intrinsic()) {
1396 // Instantly compiled code can execute.
1397 mh->_from_interpreted_entry = mh->get_i2c_entry();
1398 }
1399 }
1552 assert(m->can_be_statically_bound(), "");
1553 m->set_vtable_index(Method::nonvirtual_vtable_index);
1554 m->link_method(m, CHECK_(empty));
1555
1556 if (iid == vmIntrinsics::_linkToNative) {
1557 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1558 }
1559 if (log_is_enabled(Debug, methodhandles)) {
1560 LogTarget(Debug, methodhandles) lt;
1561 LogStream ls(lt);
1562 m->print_on(&ls);
1563 }
1564
1565 return m;
1566 }
1567
1568 #if INCLUDE_CDS
1569 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1570 if (m->adapter() != nullptr) {
1571 m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1572 }
1573 m->link_method(m, CHECK);
1574
1575 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1576 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1577 }
1578 }
1579 #endif
1580
1581 Klass* Method::check_non_bcp_klass(Klass* klass) {
1582 if (klass != nullptr && klass->class_loader() != nullptr) {
1583 if (klass->is_objArray_klass())
1584 klass = ObjArrayKlass::cast(klass)->bottom_klass();
1585 return klass;
1586 }
1587 return nullptr;
1588 }
1589
1590
1591 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
2169 }
2170
2171 // Check that this pointer is valid by checking that the vtbl pointer matches
2172 bool Method::is_valid_method(const Method* m) {
2173 if (m == nullptr) {
2174 return false;
2175 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2176 // Quick sanity check on pointer.
2177 return false;
2178 } else if (!os::is_readable_range(m, m + 1)) {
2179 return false;
2180 } else if (m->in_aot_cache()) {
2181 return CppVtables::is_valid_shared_method(m);
2182 } else if (Metaspace::contains_non_shared(m)) {
2183 return has_method_vptr((const void*)m);
2184 } else {
2185 return false;
2186 }
2187 }
2188
2189 // Printing
2190
2191 #ifndef PRODUCT
2192
2193 void Method::print_on(outputStream* st) const {
2194 ResourceMark rm;
2195 assert(is_method(), "must be method");
2196 st->print_cr("%s", internal_name());
2197 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2198 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2199 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2200 constants()->print_value_on(st); st->cr();
2201 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2202 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2203 st->print (" - name: "); name()->print_value_on(st); st->cr();
2204 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2205 st->print_cr(" - max stack: %d", max_stack());
2206 st->print_cr(" - max locals: %d", max_locals());
2207 st->print_cr(" - size of params: %d", size_of_parameters());
2208 st->print_cr(" - method size: %d", method_size());
2209 if (intrinsic_id() != vmIntrinsics::_none)
2210 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2211 if (highest_comp_level() != CompLevel_none)
2212 st->print_cr(" - highest level: %d", highest_comp_level());
2213 st->print_cr(" - vtable index: %d", _vtable_index);
2214 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2215 st->print( " - adapters: ");
2216 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2217 if (a == nullptr)
2218 st->print_cr(PTR_FORMAT, p2i(a));
2219 else
2220 a->print_adapter_on(st);
2221 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2222 st->print_cr(" - code size: %d", code_size());
2223 if (code_size() != 0) {
2224 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2225 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2226 }
2227 if (method_data() != nullptr) {
2228 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2229 }
2230 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2231 if (checked_exceptions_length() > 0) {
2232 CheckedExceptionElement* table = checked_exceptions_start();
2233 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2234 if (Verbose) {
2235 for (int i = 0; i < checked_exceptions_length(); i++) {
2236 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2237 }
2238 }
2239 }
2240 if (has_linenumber_table()) {
2241 u_char* table = compressed_linenumber_table();
2271 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2272 }
2273 }
2274
2275 void Method::print_linkage_flags(outputStream* st) {
2276 access_flags().print_on(st);
2277 if (is_default_method()) {
2278 st->print("default ");
2279 }
2280 if (is_overpass()) {
2281 st->print("overpass ");
2282 }
2283 }
2284 #endif //PRODUCT
2285
2286 void Method::print_value_on(outputStream* st) const {
2287 assert(is_method(), "must be method");
2288 st->print("%s", internal_name());
2289 print_address_on(st);
2290 st->print(" ");
2291 name()->print_value_on(st);
2292 st->print(" ");
2293 signature()->print_value_on(st);
2294 st->print(" in ");
2295 method_holder()->print_value_on(st);
2296 if (WizardMode) st->print("#%d", _vtable_index);
2297 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2298 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2299 }
2300
2301 // Verification
2302
2303 void Method::verify_on(outputStream* st) {
2304 guarantee(is_method(), "object must be method");
2305 guarantee(constants()->is_constantPool(), "should be constant pool");
2306 MethodData* md = method_data();
2307 guarantee(md == nullptr ||
2308 md->is_methodData(), "should be method data");
2309 }
|
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/atomicAccess.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() && !AOTMetaspace::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() && !AOTMetaspace::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 AtomicAccess::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 #ifndef ZERO
1341 assert(adapter()->is_linked(), "Adapter must have been linked");
1342 #endif
1343 h_method->_from_compiled_entry = adapter()->get_c2i_entry();
1344 h_method->_from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1345 h_method->_from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1346 }
1347
1348 // ONLY USE the h_method now as make_adapter may have blocked
1349
1350 if (h_method->is_continuation_native_intrinsic()) {
1351 _from_interpreted_entry = nullptr;
1352 _from_compiled_entry = nullptr;
1353 _i2i_entry = nullptr;
1354 if (Continuations::enabled()) {
1355 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1356 AdapterHandlerLibrary::create_native_wrapper(h_method);
1357 if (!h_method->has_compiled_code()) {
1358 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1359 }
1360 assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1361 }
1362 }
1363 }
1364
1365 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1381 }
1382 }
1383
1384 mh->set_adapter_entry(adapter);
1385 return adapter->get_c2i_entry();
1386 }
1387
1388 // The verified_code_entry() must be called when a invoke is resolved
1389 // on this method.
1390
1391 // It returns the compiled code entry point, after asserting not null.
1392 // This function is called after potential safepoints so that nmethod
1393 // or adapter that it points to is still live and valid.
1394 // This function must not hit a safepoint!
1395 address Method::verified_code_entry() {
1396 DEBUG_ONLY(NoSafepointVerifier nsv;)
1397 assert(_from_compiled_entry != nullptr, "must be set");
1398 return _from_compiled_entry;
1399 }
1400
1401 address Method::verified_inline_code_entry() {
1402 DEBUG_ONLY(NoSafepointVerifier nsv;)
1403 assert(_from_compiled_inline_entry != nullptr, "must be set");
1404 return _from_compiled_inline_entry;
1405 }
1406
1407 address Method::verified_inline_ro_code_entry() {
1408 DEBUG_ONLY(NoSafepointVerifier nsv;)
1409 assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1410 return _from_compiled_inline_ro_entry;
1411 }
1412
1413 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1414 // (could be racing a deopt).
1415 // Not inline to avoid circular ref.
1416 bool Method::check_code() const {
1417 // cached in a register or local. There's a race on the value of the field.
1418 nmethod *code = AtomicAccess::load_acquire(&_code);
1419 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1420 }
1421
1422 // Install compiled code. Instantly it can execute.
1423 void Method::set_code(const methodHandle& mh, nmethod *code) {
1424 assert_lock_strong(NMethodState_lock);
1425 assert( code, "use clear_code to remove code" );
1426 assert( mh->check_code(), "" );
1427
1428 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1429
1430 // These writes must happen in this order, because the interpreter will
1431 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1432 // which jumps to _from_compiled_entry.
1433 mh->_code = code; // Assign before allowing compiled code to exec
1434
1435 int comp_level = code->comp_level();
1436 // In theory there could be a race here. In practice it is unlikely
1437 // and not worth worrying about.
1438 if (comp_level > mh->highest_comp_level()) {
1439 mh->set_highest_comp_level(comp_level);
1440 }
1441
1442 OrderAccess::storestore();
1443 mh->_from_compiled_entry = code->verified_entry_point();
1444 mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1445 mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1446 OrderAccess::storestore();
1447
1448 if (mh->is_continuation_native_intrinsic()) {
1449 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1450
1451 if (mh->is_continuation_enter_intrinsic()) {
1452 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1453 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1454 } else if (mh->is_continuation_yield_intrinsic()) {
1455 mh->_i2i_entry = mh->get_i2c_entry();
1456 } else {
1457 guarantee(false, "Unknown Continuation native intrinsic");
1458 }
1459 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1460 AtomicAccess::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1461 } else if (!mh->is_method_handle_intrinsic()) {
1462 // Instantly compiled code can execute.
1463 mh->_from_interpreted_entry = mh->get_i2c_entry();
1464 }
1465 }
1618 assert(m->can_be_statically_bound(), "");
1619 m->set_vtable_index(Method::nonvirtual_vtable_index);
1620 m->link_method(m, CHECK_(empty));
1621
1622 if (iid == vmIntrinsics::_linkToNative) {
1623 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1624 }
1625 if (log_is_enabled(Debug, methodhandles)) {
1626 LogTarget(Debug, methodhandles) lt;
1627 LogStream ls(lt);
1628 m->print_on(&ls);
1629 }
1630
1631 return m;
1632 }
1633
1634 #if INCLUDE_CDS
1635 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1636 if (m->adapter() != nullptr) {
1637 m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1638 m->set_from_compiled_inline_entry(m->adapter()->get_c2i_inline_entry());
1639 m->set_from_compiled_inline_ro_entry(m->adapter()->get_c2i_inline_ro_entry());
1640 }
1641 m->link_method(m, CHECK);
1642
1643 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1644 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1645 }
1646 }
1647 #endif
1648
1649 Klass* Method::check_non_bcp_klass(Klass* klass) {
1650 if (klass != nullptr && klass->class_loader() != nullptr) {
1651 if (klass->is_objArray_klass())
1652 klass = ObjArrayKlass::cast(klass)->bottom_klass();
1653 return klass;
1654 }
1655 return nullptr;
1656 }
1657
1658
1659 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
2237 }
2238
2239 // Check that this pointer is valid by checking that the vtbl pointer matches
2240 bool Method::is_valid_method(const Method* m) {
2241 if (m == nullptr) {
2242 return false;
2243 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2244 // Quick sanity check on pointer.
2245 return false;
2246 } else if (!os::is_readable_range(m, m + 1)) {
2247 return false;
2248 } else if (m->in_aot_cache()) {
2249 return CppVtables::is_valid_shared_method(m);
2250 } else if (Metaspace::contains_non_shared(m)) {
2251 return has_method_vptr((const void*)m);
2252 } else {
2253 return false;
2254 }
2255 }
2256
2257 bool Method::is_scalarized_arg(int idx) const {
2258 if (!has_scalarized_args()) {
2259 return false;
2260 }
2261 // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2262 int depth = 0;
2263 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2264 for (int i = 0; i < sig->length(); i++) {
2265 BasicType bt = sig->at(i)._bt;
2266 if (bt == T_METADATA) {
2267 depth++;
2268 }
2269 if (idx == 0) {
2270 break; // Argument found
2271 }
2272 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2273 depth--;
2274 }
2275 if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2276 idx--; // Advance to next argument
2277 }
2278 }
2279 return depth != 0;
2280 }
2281
2282 // Printing
2283
2284 #ifndef PRODUCT
2285
2286 void Method::print_on(outputStream* st) const {
2287 ResourceMark rm;
2288 assert(is_method(), "must be method");
2289 st->print_cr("%s", internal_name());
2290 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2291 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2292 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2293 constants()->print_value_on(st); st->cr();
2294 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2295 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2296 st->print (" - name: "); name()->print_value_on(st); st->cr();
2297 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2298 st->print_cr(" - max stack: %d", max_stack());
2299 st->print_cr(" - max locals: %d", max_locals());
2300 st->print_cr(" - size of params: %d", size_of_parameters());
2301 st->print_cr(" - method size: %d", method_size());
2302 if (intrinsic_id() != vmIntrinsics::_none)
2303 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2304 if (highest_comp_level() != CompLevel_none)
2305 st->print_cr(" - highest level: %d", highest_comp_level());
2306 st->print_cr(" - vtable index: %d", _vtable_index);
2307 #ifdef ASSERT
2308 if (valid_itable_index())
2309 st->print_cr(" - itable index: %d", itable_index());
2310 #endif
2311 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2312 st->print( " - adapters: ");
2313 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2314 if (a == nullptr)
2315 st->print_cr(PTR_FORMAT, p2i(a));
2316 else
2317 a->print_adapter_on(st);
2318 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2319 st->print_cr(" - compiled inline entry " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2320 st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2321 st->print_cr(" - code size: %d", code_size());
2322 if (code_size() != 0) {
2323 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2324 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2325 }
2326 if (method_data() != nullptr) {
2327 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2328 }
2329 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2330 if (checked_exceptions_length() > 0) {
2331 CheckedExceptionElement* table = checked_exceptions_start();
2332 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2333 if (Verbose) {
2334 for (int i = 0; i < checked_exceptions_length(); i++) {
2335 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2336 }
2337 }
2338 }
2339 if (has_linenumber_table()) {
2340 u_char* table = compressed_linenumber_table();
2370 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2371 }
2372 }
2373
2374 void Method::print_linkage_flags(outputStream* st) {
2375 access_flags().print_on(st);
2376 if (is_default_method()) {
2377 st->print("default ");
2378 }
2379 if (is_overpass()) {
2380 st->print("overpass ");
2381 }
2382 }
2383 #endif //PRODUCT
2384
2385 void Method::print_value_on(outputStream* st) const {
2386 assert(is_method(), "must be method");
2387 st->print("%s", internal_name());
2388 print_address_on(st);
2389 st->print(" ");
2390 if (WizardMode) access_flags().print_on(st);
2391 name()->print_value_on(st);
2392 st->print(" ");
2393 signature()->print_value_on(st);
2394 st->print(" in ");
2395 method_holder()->print_value_on(st);
2396 if (WizardMode) st->print("#%d", _vtable_index);
2397 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2398 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2399 }
2400
2401 // Verification
2402
2403 void Method::verify_on(outputStream* st) {
2404 guarantee(is_method(), "object must be method");
2405 guarantee(constants()->is_constantPool(), "should be constant pool");
2406 MethodData* md = method_data();
2407 guarantee(md == nullptr ||
2408 md->is_methodData(), "should be method data");
2409 }
|