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