< prev index next >

src/hotspot/share/oops/method.cpp

Print this page

  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) {
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"

 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_inline_ro_entry() {
 176   assert(adapter() != nullptr, "must have");
 177   return adapter()->get_c2i_inline_ro_entry();
 178 }
 179 
 180 address Method::get_c2i_unverified_entry() {
 181   if (is_abstract()) {
 182     return SharedRuntime::get_handle_wrong_method_abstract_stub();
 183   }
 184   assert(adapter() != nullptr, "must have");
 185   return adapter()->get_c2i_unverified_entry();
 186 }
 187 
 188 address Method::get_c2i_unverified_inline_entry() {
 189   assert(adapter() != nullptr, "must have");
 190   return adapter()->get_c2i_unverified_inline_entry();
 191 }
 192 
 193 address Method::get_c2i_no_clinit_check_entry() {
 194   if (is_abstract()) {
 195     return nullptr;
 196   }
 197   assert(VM_Version::supports_fast_class_init_checks(), "");
 198   assert(adapter() != nullptr, "must have");
 199   return adapter()->get_c2i_no_clinit_check_entry();
 200 }
 201 
 202 char* Method::name_and_sig_as_C_string() const {
 203   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 204 }
 205 
 206 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 207   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 208 }
 209 
 210 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 211   const char* klass_name = klass->external_name();
 212   int klass_name_len  = (int)strlen(klass_name);

 401     return code_base();
 402   } else {
 403     return bcp;
 404   }
 405 }
 406 
 407 int Method::size(bool is_native) {
 408   // If native, then include pointers for native_function and signature_handler
 409   int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
 410   int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
 411   return align_metadata_size(header_size() + extra_words);
 412 }
 413 
 414 Symbol* Method::klass_name() const {
 415   return method_holder()->name();
 416 }
 417 
 418 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
 419   log_trace(aot)("Iter(Method): %p", this);
 420 
 421   if (!method_holder()->is_rewritten() || Arguments::is_valhalla_enabled()) {
 422     it->push(&_constMethod, MetaspaceClosure::_writable);
 423   } else {
 424     it->push(&_constMethod);
 425   }
 426   it->push(&_adapter);
 427   it->push(&_method_data);
 428   it->push(&_method_counters);
 429   NOT_PRODUCT(it->push(&_name);)
 430 }
 431 
 432 #if INCLUDE_CDS
 433 // Attempt to return method to original state.  Clear any pointers
 434 // (to objects outside the shared spaces).  We won't be able to predict
 435 // where they should point in a new JVM.  Further initialize some
 436 // entries now in order allow them to be write protected later.
 437 
 438 void Method::remove_unshareable_info() {
 439   unlink_method();
 440   if (method_data() != nullptr) {
 441     method_data()->remove_unshareable_info();

 444     method_counters()->remove_unshareable_info();
 445   }
 446   if (CDSConfig::is_dumping_adapters() && _adapter != nullptr) {
 447     _adapter->remove_unshareable_info();
 448     _adapter = nullptr;
 449   }
 450   JFR_ONLY(REMOVE_METHOD_ID(this);)
 451 }
 452 
 453 void Method::restore_unshareable_info(TRAPS) {
 454   assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
 455   if (method_data() != nullptr) {
 456     method_data()->restore_unshareable_info(CHECK);
 457   }
 458   if (method_counters() != nullptr) {
 459     method_counters()->restore_unshareable_info(CHECK);
 460   }
 461   if (_adapter != nullptr) {
 462     assert(_adapter->is_linked(), "must be");
 463     _from_compiled_entry = _adapter->get_c2i_entry();
 464     _from_compiled_inline_entry = _adapter->get_c2i_inline_entry();
 465     _from_compiled_inline_ro_entry = _adapter->get_c2i_inline_ro_entry();
 466   }
 467   assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
 468 }
 469 #endif
 470 
 471 void Method::set_vtable_index(int index) {
 472   if (in_aot_cache() && !AOTMetaspace::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 473     // At runtime initialize_vtable is rerun as part of link_class_impl()
 474     // for a shared class loaded by the non-boot loader to obtain the loader
 475     // constraints based on the runtime classloaders' context.
 476     return; // don't write into the shared class
 477   } else {
 478     _vtable_index = index;
 479   }
 480 }
 481 
 482 void Method::set_itable_index(int index) {
 483   if (in_aot_cache() && !AOTMetaspace::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
 484     // At runtime initialize_itable is rerun as part of link_class_impl()
 485     // for a shared class loaded by the non-boot loader to obtain the loader

 735 bool Method::init_method_counters(MethodCounters* counters) {
 736   // Try to install a pointer to MethodCounters, return true on success.
 737   return AtomicAccess::replace_if_null(&_method_counters, counters);
 738 }
 739 
 740 void Method::set_exception_handler_entered(int handler_bci) {
 741   if (ProfileExceptionHandlers) {
 742     MethodData* mdo = method_data();
 743     if (mdo != nullptr) {
 744       BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
 745       handler_data.set_exception_handler_entered();
 746     }
 747   }
 748 }
 749 
 750 int Method::extra_stack_words() {
 751   // not an inline function, to avoid a header dependency on Interpreter
 752   return extra_stack_entries() * Interpreter::stackElementSize;
 753 }
 754 
 755 // InlineKlass the method is declared to return. This must not
 756 // safepoint as it is called with references live on the stack at
 757 // locations the GC is unaware of.
 758 InlineKlass* Method::returns_inline_type() const {
 759   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
 760   if (is_native()) {
 761     return nullptr;
 762   }
 763   NoSafepointVerifier nsv;
 764   SignatureStream ss(signature());
 765   ss.skip_to_return_type();
 766   return ss.as_inline_klass(method_holder());
 767 }
 768 
 769 bool Method::compute_has_loops_flag() {
 770   BytecodeStream bcs(methodHandle(Thread::current(), this));
 771   Bytecodes::Code bc;
 772 
 773   while ((bc = bcs.next()) >= 0) {
 774     switch (bc) {
 775       case Bytecodes::_ifeq:
 776       case Bytecodes::_ifnull:
 777       case Bytecodes::_iflt:
 778       case Bytecodes::_ifle:
 779       case Bytecodes::_ifne:
 780       case Bytecodes::_ifnonnull:
 781       case Bytecodes::_ifgt:
 782       case Bytecodes::_ifge:
 783       case Bytecodes::_if_icmpeq:
 784       case Bytecodes::_if_icmpne:
 785       case Bytecodes::_if_icmplt:
 786       case Bytecodes::_if_icmpgt:
 787       case Bytecodes::_if_icmple:
 788       case Bytecodes::_if_icmpge:

 897 
 898 bool Method::is_accessor() const {
 899   return is_getter() || is_setter();
 900 }
 901 
 902 bool Method::is_getter() const {
 903   if (code_size() != 5) return false;
 904   if (size_of_parameters() != 1) return false;
 905   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 906   if (java_code_at(1) != Bytecodes::_getfield) return false;
 907   switch (java_code_at(4)) {
 908     case Bytecodes::_ireturn:
 909     case Bytecodes::_lreturn:
 910     case Bytecodes::_freturn:
 911     case Bytecodes::_dreturn:
 912     case Bytecodes::_areturn:
 913       break;
 914     default:
 915       return false;
 916   }
 917   if (has_scalarized_return()) {
 918     // Don't treat this as (trivial) getter method because the
 919     // inline type should be returned in a scalarized form.
 920     return false;
 921   }
 922   return true;
 923 }
 924 
 925 bool Method::is_setter() const {
 926   if (code_size() != 6) return false;
 927   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 928   switch (java_code_at(1)) {
 929     case Bytecodes::_iload_1:
 930     case Bytecodes::_aload_1:
 931     case Bytecodes::_fload_1:
 932       if (size_of_parameters() != 2) return false;
 933       break;
 934     case Bytecodes::_dload_1:
 935     case Bytecodes::_lload_1:
 936       if (size_of_parameters() != 3) return false;
 937       break;
 938     default:
 939       return false;
 940   }
 941   if (java_code_at(2) != Bytecodes::_putfield) return false;
 942   if (java_code_at(5) != Bytecodes::_return)   return false;
 943   if (has_scalarized_args()) {
 944     // Don't treat this as (trivial) setter method because the
 945     // inline type argument should be passed in a scalarized form.
 946     return false;
 947   }
 948   return true;
 949 }
 950 
 951 bool Method::is_constant_getter() const {
 952   int last_index = code_size() - 1;
 953   // Check if the first 1-3 bytecodes are a constant push
 954   // and the last bytecode is a return.
 955   return (2 <= code_size() && code_size() <= 4 &&
 956           Bytecodes::is_const(java_code_at(0)) &&
 957           Bytecodes::length_for(java_code_at(0)) == last_index &&
 958           Bytecodes::is_return(java_code_at(last_index)) &&
 959           !has_scalarized_args());
 960 }
 961 
 962 bool Method::is_class_initializer() const {





 963   // For classfiles version 51 or greater, ensure that the clinit method is
 964   // static.  Non-static methods with the name "<clinit>" are not static
 965   // initializers. (older classfiles exempted for backward compatibility)
 966   return (name() == vmSymbols::class_initializer_name() &&
 967           (is_static() ||
 968            method_holder()->major_version() < 51));
 969 }
 970 
 971 // A method named <init>, is a classic object constructor.
 972 bool Method::is_object_constructor() const {
 973   return name() == vmSymbols::object_initializer_name();
 974 }
 975 
 976 bool Method::needs_clinit_barrier() const {
 977   return is_static() && !method_holder()->is_initialized();
 978 }
 979 
 980 bool Method::is_object_wait0() const {
 981   return klass_name() == vmSymbols::java_lang_Object()
 982          && name() == vmSymbols::wait_name();
 983 }
 984 
 985 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 986   int length = method->checked_exceptions_length();
 987   if (length == 0) {  // common case
 988     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 989   } else {
 990     methodHandle h_this(THREAD, method);
 991     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 992     objArrayHandle mirrors (THREAD, m_oop);
 993     for (int i = 0; i < length; i++) {

1016     // Not necessarily sorted and not necessarily one-to-one.
1017     CompressedLineNumberReadStream stream(compressed_linenumber_table());
1018     while (stream.read_pair()) {
1019       if (stream.bci() == bci) {
1020         // perfect match
1021         return stream.line();
1022       } else {
1023         // update best_bci/line
1024         if (stream.bci() < bci && stream.bci() >= best_bci) {
1025           best_bci  = stream.bci();
1026           best_line = stream.line();
1027         }
1028       }
1029     }
1030   }
1031   return best_line;
1032 }
1033 
1034 
1035 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
1036   if( constants()->tag_at(klass_index).is_unresolved_klass()) {
1037     Thread *thread = Thread::current();
1038     Symbol* klass_name = constants()->klass_name_at(klass_index);
1039     Handle loader(thread, method_holder()->class_loader());
1040     return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
1041   } else {
1042     return true;
1043   }
1044 }
1045 
1046 
1047 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1048   int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
1049   if (must_be_resolved) {
1050     // Make sure klass is resolved in constantpool.
1051     if (constants()->tag_at(klass_index).is_unresolved_klass()) {
1052       return false;
1053     }
1054   }
1055   return is_klass_loaded_by_klass_index(klass_index);
1056 }
1057 
1058 
1059 void Method::set_native_function(address function, bool post_event_flag) {
1060   assert(function != nullptr, "use clear_native_function to unregister natives");
1061   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
1062   address* native_function = native_function_addr();
1063 
1064   // We can see racers trying to place the same native function into place. Once
1065   // is plenty.
1066   address current = *native_function;
1067   if (current == function) return;
1068   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1069       function != nullptr) {
1070     // native_method_throw_unsatisfied_link_error_entry() should only
1071     // be passed when post_event_flag is false.
1072     assert(function !=
1073       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1201 
1202 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1203   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1204   if (comp_level == CompLevel_all) {
1205     set_is_not_c1_osr_compilable();
1206     set_is_not_c2_osr_compilable();
1207   } else {
1208     if (is_c1_compile(comp_level))
1209       set_is_not_c1_osr_compilable();
1210     if (is_c2_compile(comp_level))
1211       set_is_not_c2_osr_compilable();
1212   }
1213   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1214 }
1215 
1216 // Revert to using the interpreter and clear out the nmethod
1217 void Method::clear_code() {
1218   // this may be null if c2i adapters have not been made yet
1219   // Only should happen at allocate time.
1220   if (adapter() == nullptr) {
1221     _from_compiled_entry    = nullptr;
1222     _from_compiled_inline_entry = nullptr;
1223     _from_compiled_inline_ro_entry = nullptr;
1224   } else {
1225     _from_compiled_entry    = adapter()->get_c2i_entry();
1226     _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1227     _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1228   }
1229   OrderAccess::storestore();
1230   _from_interpreted_entry = _i2i_entry;
1231   OrderAccess::storestore();
1232   _code = nullptr;
1233 }
1234 
1235 void Method::unlink_code(nmethod *compare) {
1236   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1237   // We need to check if either the _code or _from_compiled_code_entry_point
1238   // refer to this nmethod because there is a race in setting these two fields
1239   // in Method* as seen in bugid 4947125.
1240   if (code() == compare ||
1241       from_compiled_entry() == compare->verified_entry_point()) {
1242     clear_code();
1243   }
1244 }
1245 
1246 void Method::unlink_code() {
1247   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1248   clear_code();
1249 }
1250 
1251 #if INCLUDE_CDS
1252 // Called by class data sharing to remove any entry points (which are not shared)
1253 void Method::unlink_method() {
1254   assert(CDSConfig::is_dumping_archive(), "sanity");
1255   _code = nullptr;
1256   if (!CDSConfig::is_dumping_adapters()) {
1257     _adapter = nullptr;
1258   }
1259   _i2i_entry = nullptr;
1260   _from_compiled_entry = nullptr;
1261   _from_compiled_inline_entry = nullptr;
1262   _from_compiled_inline_ro_entry = nullptr;
1263   _from_interpreted_entry = nullptr;
1264 
1265   if (is_native()) {
1266     *native_function_addr() = nullptr;
1267     set_signature_handler(nullptr);
1268   }
1269   NOT_PRODUCT(set_compiled_invocation_count(0);)
1270 
1271   clear_method_data();
1272   clear_method_counters();
1273   clear_is_not_c1_compilable();
1274   clear_is_not_c1_osr_compilable();
1275   clear_is_not_c2_compilable();
1276   clear_is_not_c2_osr_compilable();
1277   clear_queued_for_compilation();
1278 
1279   remove_unshareable_flags();
1280 }
1281 
1282 void Method::remove_unshareable_flags() {
1283   // clear all the flags that shouldn't be in the archived version
1284   assert(!is_old(), "must be");
1285   assert(!is_obsolete(), "must be");
1286   assert(!is_deleted(), "must be");
1287 
1288   set_is_prefixed_native(false);
1289   set_queued_for_compilation(false);
1290   set_is_not_c2_compilable(false);
1291   set_is_not_c1_compilable(false);
1292   set_is_not_c2_osr_compilable(false);
1293   set_on_stack_flag(false);
1294   set_has_scalarized_args(false);
1295   set_has_scalarized_return(false);
1296 }
1297 #endif
1298 
1299 // Called when the method_holder is getting linked. Setup entrypoints so the method
1300 // is ready to be called from interpreter, compiler, and vtables.
1301 void Method::link_method(const methodHandle& h_method, TRAPS) {
1302   if (log_is_enabled(Info, perf, class, link)) {
1303     ClassLoader::perf_ik_link_methods_count()->inc();
1304   }
1305 
1306   // If the code cache is full, we may reenter this function for the
1307   // leftover methods that weren't linked.
1308   if (adapter() != nullptr) {
1309     if (adapter()->in_aot_cache()) {
1310       assert(adapter()->is_linked(), "Adapter is shared but not linked");
1311     } else {
1312       return;
1313     }
1314   }
1315   assert( _code == nullptr, "nothing compiled yet" );
1316 
1317   // Setup interpreter entrypoint
1318   assert(this == h_method(), "wrong h_method()" );
1319 
1320   assert(adapter() == nullptr || adapter()->is_linked(), "init'd to null or restored from cache");
1321   address entry = Interpreter::entry_for_method(h_method);
1322   assert(entry != nullptr, "interpreter entry must be non-null");
1323   // Sets both _i2i_entry and _from_interpreted_entry
1324   set_interpreter_entry(entry);
1325 
1326   // Don't overwrite already registered native entries.
1327   if (is_native() && !has_native_function()) {
1328     set_native_function(
1329       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1330       !native_bind_event_is_interesting);
1331   }
1332   if (InlineTypeReturnedAsFields && returns_inline_type() && !has_scalarized_return()) {
1333     set_has_scalarized_return();
1334   }
1335 
1336   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1337   // special handling of vtables.  An alternative is to make adapters more
1338   // lazily by calling make_adapter() from from_compiled_entry() for the
1339   // normal calls.  For vtable calls life gets more complicated.  When a
1340   // call-site goes mega-morphic we need adapters in all methods which can be
1341   // called from the vtable.  We need adapters on such methods that get loaded
1342   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1343   // problem we'll make these lazily later.
1344   if (is_abstract()) {
1345     address wrong_method_abstract = SharedRuntime::get_handle_wrong_method_abstract_stub();
1346     h_method->_from_compiled_entry = wrong_method_abstract;
1347     h_method->_from_compiled_inline_entry = wrong_method_abstract;
1348     h_method->_from_compiled_inline_ro_entry = wrong_method_abstract;
1349   } else if (_adapter == nullptr) {
1350     (void) make_adapters(h_method, CHECK);
1351 #ifndef ZERO
1352     assert(adapter()->is_linked(), "Adapter must have been linked");
1353 #endif
1354     h_method->_from_compiled_entry = adapter()->get_c2i_entry();
1355     h_method->_from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1356     h_method->_from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1357   }
1358 
1359   // ONLY USE the h_method now as make_adapter may have blocked
1360 
1361   if (h_method->is_continuation_native_intrinsic()) {
1362     _from_interpreted_entry = nullptr;
1363     _from_compiled_entry = nullptr;
1364     _i2i_entry = nullptr;
1365     if (Continuations::enabled()) {
1366       assert(!Threads::is_vm_complete(), "should only be called during vm init");
1367       AdapterHandlerLibrary::create_native_wrapper(h_method);
1368       if (!h_method->has_compiled_code()) {
1369         THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), "Initial size of CodeCache is too small");
1370       }
1371       assert(_from_interpreted_entry == get_i2c_entry(), "invariant");
1372     }
1373   }
1374 }
1375 
1376 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1377   assert(!mh->is_abstract(), "abstract methods do not have adapters");
1378   PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1379 
1380   // Adapters for compiled code are made eagerly here.  They are fairly
1381   // small (generally < 100 bytes) and quick to make (and cached and shared)
1382   // so making them eagerly shouldn't be too expensive.
1383   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1384   if (adapter == nullptr ) {
1385     if (!is_init_completed()) {
1386       // Don't throw exceptions during VM initialization because java.lang.* classes
1387       // might not have been initialized, causing problems when constructing the
1388       // Java exception object.
1389       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1390     } else {
1391       THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1392     }
1393   }
1394 
1395   assert(!mh->has_scalarized_args() || adapter->get_sig_cc() != nullptr, "sigcc should not be null here");
1396 
1397   mh->set_adapter_entry(adapter);
1398   return adapter->get_c2i_entry();
1399 }
1400 
1401 // The verified_code_entry() must be called when a invoke is resolved
1402 // on this method.
1403 
1404 // It returns the compiled code entry point, after asserting not null.
1405 // This function is called after potential safepoints so that nmethod
1406 // or adapter that it points to is still live and valid.
1407 // This function must not hit a safepoint!
1408 address Method::verified_code_entry() {
1409   DEBUG_ONLY(NoSafepointVerifier nsv;)
1410   assert(_from_compiled_entry != nullptr, "must be set");
1411   return _from_compiled_entry;
1412 }
1413 
1414 address Method::verified_inline_code_entry() {
1415   DEBUG_ONLY(NoSafepointVerifier nsv;)
1416   assert(_from_compiled_inline_entry != nullptr, "must be set");
1417   return _from_compiled_inline_entry;
1418 }
1419 
1420 address Method::verified_inline_ro_code_entry() {
1421   DEBUG_ONLY(NoSafepointVerifier nsv;)
1422   assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1423   return _from_compiled_inline_ro_entry;
1424 }
1425 
1426 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1427 // (could be racing a deopt).
1428 // Not inline to avoid circular ref.
1429 bool Method::check_code() const {
1430   // cached in a register or local.  There's a race on the value of the field.
1431   nmethod *code = AtomicAccess::load_acquire(&_code);
1432   return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1433 }
1434 
1435 // Install compiled code.  Instantly it can execute.
1436 void Method::set_code(const methodHandle& mh, nmethod *code) {
1437   assert_lock_strong(NMethodState_lock);
1438   assert( code, "use clear_code to remove code" );
1439   assert( mh->check_code(), "" );
1440 
1441   guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1442 
1443   // These writes must happen in this order, because the interpreter will
1444   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1445   // which jumps to _from_compiled_entry.
1446   mh->_code = code;             // Assign before allowing compiled code to exec
1447 
1448   int comp_level = code->comp_level();
1449   // In theory there could be a race here. In practice it is unlikely
1450   // and not worth worrying about.
1451   if (comp_level > mh->highest_comp_level()) {
1452     mh->set_highest_comp_level(comp_level);
1453   }
1454 
1455   OrderAccess::storestore();
1456   mh->_from_compiled_entry = code->verified_entry_point();
1457   mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1458   mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1459   OrderAccess::storestore();
1460 
1461   if (mh->is_continuation_native_intrinsic()) {
1462     assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1463 
1464     if (mh->is_continuation_enter_intrinsic()) {
1465       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1466       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1467     } else if (mh->is_continuation_yield_intrinsic()) {
1468       mh->_i2i_entry = mh->get_i2c_entry();
1469     } else {
1470       guarantee(false, "Unknown Continuation native intrinsic");
1471     }
1472     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1473     AtomicAccess::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1474   } else if (!mh->is_method_handle_intrinsic()) {
1475     // Instantly compiled code can execute.
1476     mh->_from_interpreted_entry = mh->get_i2c_entry();
1477   }
1478 }

1631   assert(m->can_be_statically_bound(), "");
1632   m->set_vtable_index(Method::nonvirtual_vtable_index);
1633   m->link_method(m, CHECK_(empty));
1634 
1635   if (iid == vmIntrinsics::_linkToNative) {
1636     m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1637   }
1638   if (log_is_enabled(Debug, methodhandles)) {
1639     LogTarget(Debug, methodhandles) lt;
1640     LogStream ls(lt);
1641     m->print_on(&ls);
1642   }
1643 
1644   return m;
1645 }
1646 
1647 #if INCLUDE_CDS
1648 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1649   if (m->adapter() != nullptr) {
1650     m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1651     m->set_from_compiled_inline_entry(m->adapter()->get_c2i_inline_entry());
1652     m->set_from_compiled_inline_ro_entry(m->adapter()->get_c2i_inline_ro_entry());
1653   }
1654   m->link_method(m, CHECK);
1655 
1656   if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1657     m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1658   }
1659 }
1660 #endif
1661 
1662 Klass* Method::check_non_bcp_klass(Klass* klass) {
1663   if (klass != nullptr && klass->class_loader() != nullptr) {
1664     if (klass->is_objArray_klass())
1665       klass = ObjArrayKlass::cast(klass)->bottom_klass();
1666     return klass;
1667   }
1668   return nullptr;
1669 }
1670 
1671 
1672 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,

2250 }
2251 
2252 // Check that this pointer is valid by checking that the vtbl pointer matches
2253 bool Method::is_valid_method(const Method* m) {
2254   if (m == nullptr) {
2255     return false;
2256   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2257     // Quick sanity check on pointer.
2258     return false;
2259   } else if (!os::is_readable_range(m, m + 1)) {
2260     return false;
2261   } else if (m->in_aot_cache()) {
2262     return CppVtables::is_valid_shared_method(m);
2263   } else if (Metaspace::contains_non_shared(m)) {
2264     return has_method_vptr((const void*)m);
2265   } else {
2266     return false;
2267   }
2268 }
2269 
2270 bool Method::is_scalarized_arg(int idx) const {
2271   if (!has_scalarized_args()) {
2272     return false;
2273   }
2274   // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2275   int depth = 0;
2276   const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2277   for (int i = 0; i < sig->length(); i++) {
2278     BasicType bt = sig->at(i)._bt;
2279     if (bt == T_METADATA) {
2280       depth++;
2281     }
2282     if (idx == 0) {
2283       break; // Argument found
2284     }
2285     if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2286       depth--;
2287     }
2288     if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2289       idx--; // Advance to next argument
2290     }
2291   }
2292   return depth != 0;
2293 }
2294 
2295 // Printing
2296 
2297 #ifndef PRODUCT
2298 
2299 void Method::print_on(outputStream* st) const {
2300   ResourceMark rm;
2301   assert(is_method(), "must be method");
2302   st->print_cr("%s", internal_name());
2303   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2304   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2305   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2306   constants()->print_value_on(st); st->cr();
2307   st->print   (" - access:            0x%x  ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2308   st->print   (" - flags:             0x%x  ", _flags.as_int()); _flags.print_on(st); st->cr();
2309   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2310   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2311   st->print_cr(" - max stack:         %d",   max_stack());
2312   st->print_cr(" - max locals:        %d",   max_locals());
2313   st->print_cr(" - size of params:    %d",   size_of_parameters());
2314   st->print_cr(" - method size:       %d",   method_size());
2315   if (intrinsic_id() != vmIntrinsics::_none)
2316     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2317   if (highest_comp_level() != CompLevel_none)
2318     st->print_cr(" - highest level:     %d", highest_comp_level());
2319   st->print_cr(" - vtable index:      %d",   _vtable_index);
2320 #ifdef ASSERT
2321   if (valid_itable_index())
2322     st->print_cr(" - itable index:      %d",   itable_index());
2323 #endif
2324   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2325   st->print(   " - adapters:          ");
2326   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2327   if (a == nullptr)
2328     st->print_cr(PTR_FORMAT, p2i(a));
2329   else
2330     a->print_adapter_on(st);
2331   st->print_cr(" - compiled entry           " PTR_FORMAT, p2i(from_compiled_entry()));
2332   st->print_cr(" - compiled inline entry    " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2333   st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2334   st->print_cr(" - code size:         %d",   code_size());
2335   if (code_size() != 0) {
2336     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2337     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2338   }
2339   if (method_data() != nullptr) {
2340     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2341   }
2342   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2343   if (checked_exceptions_length() > 0) {
2344     CheckedExceptionElement* table = checked_exceptions_start();
2345     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2346     if (Verbose) {
2347       for (int i = 0; i < checked_exceptions_length(); i++) {
2348         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2349       }
2350     }
2351   }
2352   if (has_linenumber_table()) {
2353     u_char* table = compressed_linenumber_table();

2383     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2384   }
2385 }
2386 
2387 void Method::print_linkage_flags(outputStream* st) {
2388   access_flags().print_on(st);
2389   if (is_default_method()) {
2390     st->print("default ");
2391   }
2392   if (is_overpass()) {
2393     st->print("overpass ");
2394   }
2395 }
2396 #endif //PRODUCT
2397 
2398 void Method::print_value_on(outputStream* st) const {
2399   assert(is_method(), "must be method");
2400   st->print("%s", internal_name());
2401   print_address_on(st);
2402   st->print(" ");
2403   if (WizardMode) access_flags().print_on(st);
2404   name()->print_value_on(st);
2405   st->print(" ");
2406   signature()->print_value_on(st);
2407   st->print(" in ");
2408   method_holder()->print_value_on(st);
2409   if (WizardMode) st->print("#%d", _vtable_index);
2410   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2411   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2412 }
2413 
2414 // Verification
2415 
2416 void Method::verify_on(outputStream* st) {
2417   guarantee(is_method(), "object must be method");
2418   guarantee(constants()->is_constantPool(), "should be constant pool");
2419   MethodData* md = method_data();
2420   guarantee(md == nullptr ||
2421       md->is_methodData(), "should be method data");
2422 }
< prev index next >