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"
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" );
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) {
1320 assert(!mh->is_abstract(), "abstract methods do not have adapters");
1321 PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1322
1323 // Adapters for compiled code are made eagerly here. They are fairly
1324 // small (generally < 100 bytes) and quick to make (and cached and shared)
1325 // so making them eagerly shouldn't be too expensive.
1326 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1327 if (adapter == nullptr ) {
1328 if (!is_init_completed()) {
1329 // Don't throw exceptions during VM initialization because java.lang.* classes
1330 // might not have been initialized, causing problems when constructing the
1331 // Java exception object.
1332 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1333 } else {
1334 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
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"
151 method_data()->~MethodData();
152 }
153 }
154
155 address Method::get_i2c_entry() {
156 if (is_abstract()) {
157 return SharedRuntime::throw_AbstractMethodError_entry();
158 }
159 assert(adapter() != nullptr, "must have");
160 return adapter()->get_i2c_entry();
161 }
162
163 address Method::get_c2i_entry() {
164 if (is_abstract()) {
165 return SharedRuntime::get_handle_wrong_method_abstract_stub();
166 }
167 assert(adapter() != nullptr, "must have");
168 return adapter()->get_c2i_entry();
169 }
170
171 address Method::get_c2i_inline_entry() {
172 if (is_abstract()) {
173 return SharedRuntime::get_handle_wrong_method_abstract_stub();
174 }
175 assert(adapter() != nullptr, "must have");
176 return adapter()->get_c2i_inline_entry();
177 }
178
179 address Method::get_c2i_inline_ro_entry() {
180 if (is_abstract()) {
181 return SharedRuntime::get_handle_wrong_method_abstract_stub();
182 }
183 assert(adapter() != nullptr, "must have");
184 return adapter()->get_c2i_inline_ro_entry();
185 }
186
187 address Method::get_c2i_unverified_entry() {
188 if (is_abstract()) {
189 return SharedRuntime::get_handle_wrong_method_abstract_stub();
190 }
191 assert(adapter() != nullptr, "must have");
192 return adapter()->get_c2i_unverified_entry();
193 }
194
195 address Method::get_c2i_unverified_inline_entry() {
196 assert(adapter() != nullptr, "must have");
197 return adapter()->get_c2i_unverified_inline_entry();
198 }
199
200 address Method::get_c2i_no_clinit_check_entry() {
201 if (is_abstract()) {
202 return nullptr;
203 }
204 assert(VM_Version::supports_fast_class_init_checks(), "");
205 assert(adapter() != nullptr, "must have");
206 return adapter()->get_c2i_no_clinit_check_entry();
207 }
208
209 char* Method::name_and_sig_as_C_string() const {
210 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
211 }
212
213 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
214 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
215 }
216
217 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
218 const char* klass_name = klass->external_name();
219 int klass_name_len = (int)strlen(klass_name);
408 return code_base();
409 } else {
410 return bcp;
411 }
412 }
413
414 int Method::size(bool is_native) {
415 // If native, then include pointers for native_function and signature_handler
416 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
417 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
418 return align_metadata_size(header_size() + extra_words);
419 }
420
421 Symbol* Method::klass_name() const {
422 return method_holder()->name();
423 }
424
425 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
426 log_trace(aot)("Iter(Method): %p", this);
427
428 if (!method_holder()->is_rewritten() || Arguments::is_valhalla_enabled()) {
429 it->push(&_constMethod, MetaspaceClosure::_writable);
430 } else {
431 it->push(&_constMethod);
432 }
433 it->push(&_adapter);
434 it->push(&_method_data);
435 it->push(&_method_counters);
436 NOT_PRODUCT(it->push(&_name);)
437 }
438
439 #if INCLUDE_CDS
440 // Attempt to return method to original state. Clear any pointers
441 // (to objects outside the shared spaces). We won't be able to predict
442 // where they should point in a new JVM. Further initialize some
443 // entries now in order allow them to be write protected later.
444
445 void Method::remove_unshareable_info() {
446 unlink_method();
447 if (method_data() != nullptr) {
448 method_data()->remove_unshareable_info();
451 method_counters()->remove_unshareable_info();
452 }
453 if (CDSConfig::is_dumping_adapters() && _adapter != nullptr) {
454 _adapter->remove_unshareable_info();
455 _adapter = nullptr;
456 }
457 JFR_ONLY(REMOVE_METHOD_ID(this);)
458 }
459
460 void Method::restore_unshareable_info(TRAPS) {
461 assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
462 if (method_data() != nullptr) {
463 method_data()->restore_unshareable_info(CHECK);
464 }
465 if (method_counters() != nullptr) {
466 method_counters()->restore_unshareable_info(CHECK);
467 }
468 if (_adapter != nullptr) {
469 assert(_adapter->is_linked(), "must be");
470 _from_compiled_entry = _adapter->get_c2i_entry();
471 _from_compiled_inline_entry = _adapter->get_c2i_inline_entry();
472 _from_compiled_inline_ro_entry = _adapter->get_c2i_inline_ro_entry();
473 }
474 assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
475 }
476 #endif
477
478 void Method::set_vtable_index(int index) {
479 if (in_aot_cache() && !AOTMetaspace::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
480 // At runtime initialize_vtable is rerun as part of link_class_impl()
481 // for a shared class loaded by the non-boot loader to obtain the loader
482 // constraints based on the runtime classloaders' context.
483 return; // don't write into the shared class
484 } else {
485 _vtable_index = index;
486 }
487 }
488
489 void Method::set_itable_index(int index) {
490 if (in_aot_cache() && !AOTMetaspace::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
491 // At runtime initialize_itable is rerun as part of link_class_impl()
492 // for a shared class loaded by the non-boot loader to obtain the loader
742 bool Method::init_method_counters(MethodCounters* counters) {
743 // Try to install a pointer to MethodCounters, return true on success.
744 return AtomicAccess::replace_if_null(&_method_counters, counters);
745 }
746
747 void Method::set_exception_handler_entered(int handler_bci) {
748 if (ProfileExceptionHandlers) {
749 MethodData* mdo = method_data();
750 if (mdo != nullptr) {
751 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
752 handler_data.set_exception_handler_entered();
753 }
754 }
755 }
756
757 int Method::extra_stack_words() {
758 // not an inline function, to avoid a header dependency on Interpreter
759 return extra_stack_entries() * Interpreter::stackElementSize;
760 }
761
762 // InlineKlass the method is declared to return. This must not
763 // safepoint as it is called with references live on the stack at
764 // locations the GC is unaware of.
765 InlineKlass* Method::returns_inline_type() const {
766 assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
767 if (is_native()) {
768 return nullptr;
769 }
770 NoSafepointVerifier nsv;
771 SignatureStream ss(signature());
772 ss.skip_to_return_type();
773 return ss.as_inline_klass(method_holder());
774 }
775
776 bool Method::compute_has_loops_flag() {
777 BytecodeStream bcs(methodHandle(Thread::current(), this));
778 Bytecodes::Code bc;
779
780 while ((bc = bcs.next()) >= 0) {
781 switch (bc) {
782 case Bytecodes::_ifeq:
783 case Bytecodes::_ifnull:
784 case Bytecodes::_iflt:
785 case Bytecodes::_ifle:
786 case Bytecodes::_ifne:
787 case Bytecodes::_ifnonnull:
788 case Bytecodes::_ifgt:
789 case Bytecodes::_ifge:
790 case Bytecodes::_if_icmpeq:
791 case Bytecodes::_if_icmpne:
792 case Bytecodes::_if_icmplt:
793 case Bytecodes::_if_icmpgt:
794 case Bytecodes::_if_icmple:
795 case Bytecodes::_if_icmpge:
904
905 bool Method::is_accessor() const {
906 return is_getter() || is_setter();
907 }
908
909 bool Method::is_getter() const {
910 if (code_size() != 5) return false;
911 if (size_of_parameters() != 1) return false;
912 if (java_code_at(0) != Bytecodes::_aload_0) return false;
913 if (java_code_at(1) != Bytecodes::_getfield) return false;
914 switch (java_code_at(4)) {
915 case Bytecodes::_ireturn:
916 case Bytecodes::_lreturn:
917 case Bytecodes::_freturn:
918 case Bytecodes::_dreturn:
919 case Bytecodes::_areturn:
920 break;
921 default:
922 return false;
923 }
924 if (InlineTypeReturnedAsFields && returns_inline_type() != nullptr) {
925 // Don't treat this as (trivial) getter method because the
926 // inline type could be returned in a scalarized form.
927 return false;
928 }
929 return true;
930 }
931
932 bool Method::is_setter() const {
933 if (code_size() != 6) return false;
934 if (java_code_at(0) != Bytecodes::_aload_0) return false;
935 switch (java_code_at(1)) {
936 case Bytecodes::_iload_1:
937 case Bytecodes::_aload_1:
938 case Bytecodes::_fload_1:
939 if (size_of_parameters() != 2) return false;
940 break;
941 case Bytecodes::_dload_1:
942 case Bytecodes::_lload_1:
943 if (size_of_parameters() != 3) return false;
944 break;
945 default:
946 return false;
947 }
948 if (java_code_at(2) != Bytecodes::_putfield) return false;
949 if (java_code_at(5) != Bytecodes::_return) return false;
950 if (has_scalarized_args()) {
951 // Don't treat this as (trivial) setter method because the
952 // inline type argument should be passed in a scalarized form.
953 return false;
954 }
955 return true;
956 }
957
958 bool Method::is_constant_getter() const {
959 int last_index = code_size() - 1;
960 // Check if the first 1-3 bytecodes are a constant push
961 // and the last bytecode is a return.
962 return (2 <= code_size() && code_size() <= 4 &&
963 Bytecodes::is_const(java_code_at(0)) &&
964 Bytecodes::length_for(java_code_at(0)) == last_index &&
965 Bytecodes::is_return(java_code_at(last_index)) &&
966 !has_scalarized_args());
967 }
968
969 bool Method::is_class_initializer() const {
970 // For classfiles version 51 or greater, ensure that the clinit method is
971 // static. Non-static methods with the name "<clinit>" are not static
972 // initializers. (older classfiles exempted for backward compatibility)
973 return (name() == vmSymbols::class_initializer_name() &&
974 (is_static() ||
975 method_holder()->major_version() < 51));
976 }
977
978 // A method named <init>, is a classic object constructor.
979 bool Method::is_object_constructor() const {
980 return name() == vmSymbols::object_initializer_name();
981 }
982
983 bool Method::needs_clinit_barrier() const {
984 return is_static() && !method_holder()->is_initialized();
985 }
986
987 bool Method::is_object_wait0() const {
988 return klass_name() == vmSymbols::java_lang_Object()
989 && name() == vmSymbols::wait_name();
990 }
991
992 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
993 int length = method->checked_exceptions_length();
994 if (length == 0) { // common case
995 return objArrayHandle(THREAD, Universe::the_empty_class_array());
996 } else {
997 methodHandle h_this(THREAD, method);
998 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
999 objArrayHandle mirrors (THREAD, m_oop);
1000 for (int i = 0; i < length; i++) {
1023 // Not necessarily sorted and not necessarily one-to-one.
1024 CompressedLineNumberReadStream stream(compressed_linenumber_table());
1025 while (stream.read_pair()) {
1026 if (stream.bci() == bci) {
1027 // perfect match
1028 return stream.line();
1029 } else {
1030 // update best_bci/line
1031 if (stream.bci() < bci && stream.bci() >= best_bci) {
1032 best_bci = stream.bci();
1033 best_line = stream.line();
1034 }
1035 }
1036 }
1037 }
1038 return best_line;
1039 }
1040
1041
1042 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
1043 if( constants()->tag_at(klass_index).is_unresolved_klass()) {
1044 Thread *thread = Thread::current();
1045 Symbol* klass_name = constants()->klass_name_at(klass_index);
1046 Handle loader(thread, method_holder()->class_loader());
1047 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
1048 } else {
1049 return true;
1050 }
1051 }
1052
1053
1054 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1055 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
1056 if (must_be_resolved) {
1057 // Make sure klass is resolved in constantpool.
1058 if (constants()->tag_at(klass_index).is_unresolved_klass()) {
1059 return false;
1060 }
1061 }
1062 return is_klass_loaded_by_klass_index(klass_index);
1063 }
1064
1065
1066 void Method::set_native_function(address function, bool post_event_flag) {
1067 assert(function != nullptr, "use clear_native_function to unregister natives");
1068 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
1069 address* native_function = native_function_addr();
1070
1071 // We can see racers trying to place the same native function into place. Once
1072 // is plenty.
1073 address current = *native_function;
1074 if (current == function) return;
1075 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1076 function != nullptr) {
1077 // native_method_throw_unsatisfied_link_error_entry() should only
1078 // be passed when post_event_flag is false.
1079 assert(function !=
1080 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1208
1209 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1210 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1211 if (comp_level == CompLevel_all) {
1212 set_is_not_c1_osr_compilable();
1213 set_is_not_c2_osr_compilable();
1214 } else {
1215 if (is_c1_compile(comp_level))
1216 set_is_not_c1_osr_compilable();
1217 if (is_c2_compile(comp_level))
1218 set_is_not_c2_osr_compilable();
1219 }
1220 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1221 }
1222
1223 // Revert to using the interpreter and clear out the nmethod
1224 void Method::clear_code() {
1225 // this may be null if c2i adapters have not been made yet
1226 // Only should happen at allocate time.
1227 if (adapter() == nullptr) {
1228 _from_compiled_entry = nullptr;
1229 _from_compiled_inline_entry = nullptr;
1230 _from_compiled_inline_ro_entry = nullptr;
1231 } else {
1232 _from_compiled_entry = adapter()->get_c2i_entry();
1233 _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1234 _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1235 }
1236 OrderAccess::storestore();
1237 _from_interpreted_entry = _i2i_entry;
1238 OrderAccess::storestore();
1239 _code = nullptr;
1240 }
1241
1242 void Method::unlink_code(nmethod *compare) {
1243 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1244 // We need to check if either the _code or _from_compiled_code_entry_point
1245 // refer to this nmethod because there is a race in setting these two fields
1246 // in Method* as seen in bugid 4947125.
1247 if (code() == compare ||
1248 from_compiled_entry() == compare->verified_entry_point()) {
1249 clear_code();
1250 }
1251 }
1252
1253 void Method::unlink_code() {
1254 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1255 clear_code();
1256 }
1257
1258 #if INCLUDE_CDS
1259 // Called by class data sharing to remove any entry points (which are not shared)
1260 void Method::unlink_method() {
1261 assert(CDSConfig::is_dumping_archive(), "sanity");
1262 _code = nullptr;
1263 if (!CDSConfig::is_dumping_adapters()) {
1264 _adapter = nullptr;
1265 }
1266 _i2i_entry = nullptr;
1267 _from_compiled_entry = nullptr;
1268 _from_compiled_inline_entry = nullptr;
1269 _from_compiled_inline_ro_entry = nullptr;
1270 _from_interpreted_entry = nullptr;
1271
1272 if (is_native()) {
1273 *native_function_addr() = nullptr;
1274 set_signature_handler(nullptr);
1275 }
1276 NOT_PRODUCT(set_compiled_invocation_count(0);)
1277
1278 clear_method_data();
1279 clear_method_counters();
1280 clear_is_not_c1_compilable();
1281 clear_is_not_c1_osr_compilable();
1282 clear_is_not_c2_compilable();
1283 clear_is_not_c2_osr_compilable();
1284 clear_queued_for_compilation();
1285
1286 remove_unshareable_flags();
1287 }
1288
1289 void Method::remove_unshareable_flags() {
1290 // clear all the flags that shouldn't be in the archived version
1291 assert(!is_old(), "must be");
1292 assert(!is_obsolete(), "must be");
1293 assert(!is_deleted(), "must be");
1294
1295 set_is_prefixed_native(false);
1296 set_queued_for_compilation(false);
1297 set_is_not_c2_compilable(false);
1298 set_is_not_c1_compilable(false);
1299 set_is_not_c2_osr_compilable(false);
1300 set_on_stack_flag(false);
1301 set_has_scalarized_args(false);
1302 }
1303 #endif
1304
1305 // Called when the method_holder is getting linked. Setup entrypoints so the method
1306 // is ready to be called from interpreter, compiler, and vtables.
1307 void Method::link_method(const methodHandle& h_method, TRAPS) {
1308 if (log_is_enabled(Info, perf, class, link)) {
1309 ClassLoader::perf_ik_link_methods_count()->inc();
1310 }
1311
1312 // If the code cache is full, we may reenter this function for the
1313 // leftover methods that weren't linked.
1314 if (adapter() != nullptr) {
1315 if (adapter()->in_aot_cache()) {
1316 assert(adapter()->is_linked(), "Adapter is shared but not linked");
1317 } else {
1318 return;
1319 }
1320 }
1321 assert( _code == nullptr, "nothing compiled yet" );
1327 address entry = Interpreter::entry_for_method(h_method);
1328 assert(entry != nullptr, "interpreter entry must be non-null");
1329 // Sets both _i2i_entry and _from_interpreted_entry
1330 set_interpreter_entry(entry);
1331
1332 // Don't overwrite already registered native entries.
1333 if (is_native() && !has_native_function()) {
1334 set_native_function(
1335 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1336 !native_bind_event_is_interesting);
1337 }
1338
1339 // Setup compiler entrypoint. This is made eagerly, so we do not need
1340 // special handling of vtables. An alternative is to make adapters more
1341 // lazily by calling make_adapter() from from_compiled_entry() for the
1342 // normal calls. For vtable calls life gets more complicated. When a
1343 // call-site goes mega-morphic we need adapters in all methods which can be
1344 // called from the vtable. We need adapters on such methods that get loaded
1345 // later. Ditto for mega-morphic itable calls. If this proves to be a
1346 // problem we'll make these lazily later.
1347 // With the scalarized calling convention, create adapters for abstract
1348 // methods as well because the adapter is used to propagate the signature.
1349 if (_adapter == nullptr && (!h_method->is_abstract() || InlineTypePassFieldsAsArgs)) {
1350 make_adapters(h_method, CHECK);
1351 }
1352 h_method->_from_compiled_entry = h_method->get_c2i_entry();
1353 h_method->_from_compiled_inline_entry = h_method->get_c2i_inline_entry();
1354 h_method->_from_compiled_inline_ro_entry = h_method->get_c2i_inline_ro_entry();
1355
1356 // ONLY USE the h_method now as make_adapter may have blocked
1357
1358 if (h_method->is_continuation_native_intrinsic()) {
1359 _from_interpreted_entry = nullptr;
1360 _from_compiled_entry = nullptr;
1361 _i2i_entry = nullptr;
1362 if (Continuations::enabled()) {
1363 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1364 AdapterHandlerLibrary::create_native_wrapper(h_method);
1365 if (!h_method->has_compiled_code()) {
1366 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1367 }
1368 assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1369 }
1370 }
1371 }
1372
1373 void Method::make_adapters(const methodHandle& mh, TRAPS) {
1374 assert(!mh->is_abstract() || InlineTypePassFieldsAsArgs, "abstract methods do not have adapters");
1375 PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1376
1377 // Adapters for compiled code are made eagerly here. They are fairly
1378 // small (generally < 100 bytes) and quick to make (and cached and shared)
1379 // so making them eagerly shouldn't be too expensive.
1380 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1381 if (adapter == nullptr ) {
1382 if (!is_init_completed()) {
1383 // Don't throw exceptions during VM initialization because java.lang.* classes
1384 // might not have been initialized, causing problems when constructing the
1385 // Java exception object.
1386 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1387 } else {
1388 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1389 }
1390 }
1391
1392 assert(!mh->has_scalarized_args() || (adapter->get_sig_cc() != nullptr && adapter->get_sig_cc_ro() != nullptr), "should be initialized");
1393
1394 mh->set_adapter_entry(adapter);
1395 #ifndef ZERO
1396 assert(adapter->is_linked(), "Adapter must have been linked");
1397 #endif
1398 }
1399
1400 // The verified_code_entry() must be called when a invoke is resolved
1401 // on this method.
1402
1403 // It returns the compiled code entry point, after asserting not null.
1404 // This function is called after potential safepoints so that nmethod
1405 // or adapter that it points to is still live and valid.
1406 // This function must not hit a safepoint!
1407 address Method::verified_code_entry() {
1408 DEBUG_ONLY(NoSafepointVerifier nsv;)
1409 assert(_from_compiled_entry != nullptr, "must be set");
1410 return _from_compiled_entry;
1411 }
1412
1413 address Method::verified_inline_code_entry() {
1414 DEBUG_ONLY(NoSafepointVerifier nsv;)
1415 assert(_from_compiled_inline_entry != nullptr, "must be set");
1416 return _from_compiled_inline_entry;
1417 }
1418
1419 address Method::verified_inline_ro_code_entry() {
1420 DEBUG_ONLY(NoSafepointVerifier nsv;)
1421 assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1422 return _from_compiled_inline_ro_entry;
1423 }
1424
1425 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1426 // (could be racing a deopt).
1427 // Not inline to avoid circular ref.
1428 bool Method::check_code() const {
1429 // cached in a register or local. There's a race on the value of the field.
1430 nmethod *code = AtomicAccess::load_acquire(&_code);
1431 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1432 }
1433
1434 // Install compiled code. Instantly it can execute.
1435 void Method::set_code(const methodHandle& mh, nmethod *code) {
1436 assert_lock_strong(NMethodState_lock);
1437 assert( code, "use clear_code to remove code" );
1438 assert( mh->check_code(), "" );
1439
1440 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1441
1442 // These writes must happen in this order, because the interpreter will
1443 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1444 // which jumps to _from_compiled_entry.
1445 mh->_code = code; // Assign before allowing compiled code to exec
1446
1447 int comp_level = code->comp_level();
1448 // In theory there could be a race here. In practice it is unlikely
1449 // and not worth worrying about.
1450 if (comp_level > mh->highest_comp_level()) {
1451 mh->set_highest_comp_level(comp_level);
1452 }
1453
1454 OrderAccess::storestore();
1455 mh->_from_compiled_entry = code->verified_entry_point();
1456 mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1457 mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1458 OrderAccess::storestore();
1459
1460 if (mh->is_continuation_native_intrinsic()) {
1461 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1462
1463 if (mh->is_continuation_enter_intrinsic()) {
1464 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1465 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1466 } else if (mh->is_continuation_yield_intrinsic()) {
1467 mh->_i2i_entry = mh->get_i2c_entry();
1468 } else {
1469 guarantee(false, "Unknown Continuation native intrinsic");
1470 }
1471 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1472 AtomicAccess::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1473 } else if (!mh->is_method_handle_intrinsic()) {
1474 // Instantly compiled code can execute.
1475 mh->_from_interpreted_entry = mh->get_i2c_entry();
1476 }
1477 }
1630 assert(m->can_be_statically_bound(), "");
1631 m->set_vtable_index(Method::nonvirtual_vtable_index);
1632 m->link_method(m, CHECK_(empty));
1633
1634 if (iid == vmIntrinsics::_linkToNative) {
1635 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1636 }
1637 if (log_is_enabled(Debug, methodhandles)) {
1638 LogTarget(Debug, methodhandles) lt;
1639 LogStream ls(lt);
1640 m->print_on(&ls);
1641 }
1642
1643 return m;
1644 }
1645
1646 #if INCLUDE_CDS
1647 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1648 if (m->adapter() != nullptr) {
1649 m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1650 m->set_from_compiled_inline_entry(m->adapter()->get_c2i_inline_entry());
1651 m->set_from_compiled_inline_ro_entry(m->adapter()->get_c2i_inline_ro_entry());
1652 }
1653 m->link_method(m, CHECK);
1654
1655 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1656 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1657 }
1658 }
1659 #endif
1660
1661 Klass* Method::check_non_bcp_klass(Klass* klass) {
1662 if (klass != nullptr && klass->class_loader() != nullptr) {
1663 if (klass->is_objArray_klass())
1664 klass = ObjArrayKlass::cast(klass)->bottom_klass();
1665 return klass;
1666 }
1667 return nullptr;
1668 }
1669
1670
1671 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
2249 }
2250
2251 // Check that this pointer is valid by checking that the vtbl pointer matches
2252 bool Method::is_valid_method(const Method* m) {
2253 if (m == nullptr) {
2254 return false;
2255 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2256 // Quick sanity check on pointer.
2257 return false;
2258 } else if (!os::is_readable_range(m, m + 1)) {
2259 return false;
2260 } else if (m->in_aot_cache()) {
2261 return CppVtables::is_valid_shared_method(m);
2262 } else if (Metaspace::contains_non_shared(m)) {
2263 return has_method_vptr((const void*)m);
2264 } else {
2265 return false;
2266 }
2267 }
2268
2269 bool Method::is_scalarized_arg(int idx) const {
2270 if (!has_scalarized_args()) {
2271 return false;
2272 }
2273 // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2274 int depth = 0;
2275 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2276 for (int i = 0; i < sig->length(); i++) {
2277 BasicType bt = sig->at(i)._bt;
2278 if (bt == T_METADATA) {
2279 depth++;
2280 }
2281 if (idx == 0) {
2282 break; // Argument found
2283 }
2284 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2285 depth--;
2286 }
2287 if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2288 idx--; // Advance to next argument
2289 }
2290 }
2291 return depth != 0;
2292 }
2293
2294 bool Method::is_scalarized_buffer_arg(int idx) const {
2295 if (!has_scalarized_args()) {
2296 return false;
2297 }
2298 // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2299 int depth = 0;
2300 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2301 for (int i = 0; i < sig->length(); i++) {
2302 BasicType bt = sig->at(i)._bt;
2303 if (bt == T_METADATA) {
2304 depth++;
2305 continue;
2306 }
2307 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2308 depth--;
2309 continue;
2310 }
2311 if (idx == 0) {
2312 if (sig->at(i)._vt_oop) {
2313 assert(depth == 1, "only for root value");
2314 return true;
2315 }
2316 break; // Argument found
2317 }
2318 idx--; // Advance to next argument
2319 }
2320 return false;
2321 }
2322
2323 // Printing
2324
2325 #ifndef PRODUCT
2326
2327 void Method::print_on(outputStream* st) const {
2328 ResourceMark rm;
2329 assert(is_method(), "must be method");
2330 st->print_cr("%s", internal_name());
2331 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2332 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2333 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2334 constants()->print_value_on(st); st->cr();
2335 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2336 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2337 st->print (" - name: "); name()->print_value_on(st); st->cr();
2338 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2339 st->print_cr(" - max stack: %d", max_stack());
2340 st->print_cr(" - max locals: %d", max_locals());
2341 st->print_cr(" - size of params: %d", size_of_parameters());
2342 st->print_cr(" - method size: %d", method_size());
2343 if (intrinsic_id() != vmIntrinsics::_none)
2344 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2345 if (highest_comp_level() != CompLevel_none)
2346 st->print_cr(" - highest level: %d", highest_comp_level());
2347 st->print_cr(" - vtable index: %d", _vtable_index);
2348 #ifdef ASSERT
2349 if (valid_itable_index())
2350 st->print_cr(" - itable index: %d", itable_index());
2351 #endif
2352 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2353 st->print( " - adapters: ");
2354 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2355 if (a == nullptr)
2356 st->print_cr(PTR_FORMAT, p2i(a));
2357 else
2358 a->print_adapter_on(st);
2359 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2360 st->print_cr(" - compiled inline entry " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2361 st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2362 st->print_cr(" - code size: %d", code_size());
2363 if (code_size() != 0) {
2364 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2365 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2366 }
2367 if (method_data() != nullptr) {
2368 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2369 }
2370 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2371 if (checked_exceptions_length() > 0) {
2372 CheckedExceptionElement* table = checked_exceptions_start();
2373 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2374 if (Verbose) {
2375 for (int i = 0; i < checked_exceptions_length(); i++) {
2376 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2377 }
2378 }
2379 }
2380 if (has_linenumber_table()) {
2381 u_char* table = compressed_linenumber_table();
2411 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2412 }
2413 }
2414
2415 void Method::print_linkage_flags(outputStream* st) {
2416 access_flags().print_on(st);
2417 if (is_default_method()) {
2418 st->print("default ");
2419 }
2420 if (is_overpass()) {
2421 st->print("overpass ");
2422 }
2423 }
2424 #endif //PRODUCT
2425
2426 void Method::print_value_on(outputStream* st) const {
2427 assert(is_method(), "must be method");
2428 st->print("%s", internal_name());
2429 print_address_on(st);
2430 st->print(" ");
2431 if (WizardMode) access_flags().print_on(st);
2432 name()->print_value_on(st);
2433 st->print(" ");
2434 signature()->print_value_on(st);
2435 st->print(" in ");
2436 method_holder()->print_value_on(st);
2437 if (WizardMode) st->print("#%d", _vtable_index);
2438 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2439 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2440 }
2441
2442 // Verification
2443
2444 void Method::verify_on(outputStream* st) {
2445 guarantee(is_method(), "object must be method");
2446 guarantee(constants()->is_constantPool(), "should be constant pool");
2447 MethodData* md = method_data();
2448 guarantee(md == nullptr ||
2449 md->is_methodData(), "should be method data");
2450 }
|