< prev index next >

src/hotspot/share/oops/method.cpp

Print this page

  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/constMethod.hpp"
  55 #include "oops/constantPool.hpp"
  56 #include "oops/klass.inline.hpp"
  57 #include "oops/method.inline.hpp"
  58 #include "oops/methodData.hpp"
  59 #include "oops/objArrayKlass.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbol.hpp"

  63 #include "prims/jvmtiExport.hpp"
  64 #include "prims/methodHandles.hpp"
  65 #include "runtime/atomic.hpp"
  66 #include "runtime/arguments.hpp"
  67 #include "runtime/continuationEntry.hpp"
  68 #include "runtime/frame.inline.hpp"
  69 #include "runtime/handles.inline.hpp"
  70 #include "runtime/init.hpp"
  71 #include "runtime/java.hpp"
  72 #include "runtime/orderAccess.hpp"
  73 #include "runtime/perfData.hpp"
  74 #include "runtime/relocator.hpp"
  75 #include "runtime/safepointVerifiers.hpp"
  76 #include "runtime/sharedRuntime.hpp"
  77 #include "runtime/signature.hpp"
  78 #include "runtime/threads.hpp"
  79 #include "runtime/vm_version.hpp"
  80 #include "utilities/align.hpp"
  81 #include "utilities/quickSort.hpp"
  82 #include "utilities/vmError.hpp"

 103 }
 104 
 105 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
 106   NoSafepointVerifier no_safepoint;
 107   set_constMethod(xconst);
 108   set_access_flags(access_flags);
 109   set_intrinsic_id(vmIntrinsics::_none);
 110   clear_method_data();
 111   clear_method_counters();
 112   set_vtable_index(Method::garbage_vtable_index);
 113 
 114   // Fix and bury in Method*
 115   set_interpreter_entry(nullptr); // sets i2i entry and from_int
 116   set_adapter_entry(nullptr);
 117   Method::clear_code(); // from_c/from_i get set to c2i/i2i
 118 
 119   if (access_flags.is_native()) {
 120     clear_native_function();
 121     set_signature_handler(nullptr);
 122   }
 123 
 124   NOT_PRODUCT(set_compiled_invocation_count(0);)
 125   // Name is very useful for debugging.
 126   NOT_PRODUCT(_name = name;)
 127 }
 128 
 129 // Release Method*.  The nmethod will be gone when we get here because
 130 // we've walked the code cache.
 131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
 132   MetadataFactory::free_metadata(loader_data, constMethod());
 133   set_constMethod(nullptr);
 134   MetadataFactory::free_metadata(loader_data, method_data());
 135   clear_method_data();
 136   MetadataFactory::free_metadata(loader_data, method_counters());
 137   clear_method_counters();
 138   // The nmethod will be gone when we get here.
 139   if (code() != nullptr) _code = nullptr;
 140 }
 141 
 142 void Method::release_C_heap_structures() {
 143   if (method_data()) {
 144     method_data()->release_C_heap_structures();
 145 
 146     // Destroy MethodData embedded lock
 147     method_data()->~MethodData();
 148   }
 149 }
 150 
 151 address Method::get_i2c_entry() {
 152   assert(adapter() != nullptr, "must have");
 153   return adapter()->get_i2c_entry();
 154 }
 155 
 156 address Method::get_c2i_entry() {
 157   assert(adapter() != nullptr, "must have");
 158   return adapter()->get_c2i_entry();
 159 }
 160 





 161 address Method::get_c2i_unverified_entry() {
 162   assert(adapter() != nullptr, "must have");
 163   return adapter()->get_c2i_unverified_entry();
 164 }
 165 





 166 address Method::get_c2i_no_clinit_check_entry() {
 167   assert(VM_Version::supports_fast_class_init_checks(), "");
 168   assert(adapter() != nullptr, "must have");
 169   return adapter()->get_c2i_no_clinit_check_entry();
 170 }
 171 
 172 char* Method::name_and_sig_as_C_string() const {
 173   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 174 }
 175 
 176 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 177   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 178 }
 179 
 180 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 181   const char* klass_name = klass->external_name();
 182   int klass_name_len  = (int)strlen(klass_name);
 183   int method_name_len = method_name->utf8_length();
 184   int len             = klass_name_len + 1 + method_name_len + signature->utf8_length();
 185   char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);

 370     return code_base();
 371   } else {
 372     return bcp;
 373   }
 374 }
 375 
 376 int Method::size(bool is_native) {
 377   // If native, then include pointers for native_function and signature_handler
 378   int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
 379   int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
 380   return align_metadata_size(header_size() + extra_words);
 381 }
 382 
 383 Symbol* Method::klass_name() const {
 384   return method_holder()->name();
 385 }
 386 
 387 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
 388   log_trace(cds)("Iter(Method): %p", this);
 389 
 390   if (!method_holder()->is_rewritten()) {
 391     it->push(&_constMethod, MetaspaceClosure::_writable);
 392   } else {
 393     it->push(&_constMethod);
 394   }
 395   it->push(&_method_data);
 396   it->push(&_method_counters);
 397   NOT_PRODUCT(it->push(&_name);)
 398 }
 399 
 400 #if INCLUDE_CDS
 401 // Attempt to return method to original state.  Clear any pointers
 402 // (to objects outside the shared spaces).  We won't be able to predict
 403 // where they should point in a new JVM.  Further initialize some
 404 // entries now in order allow them to be write protected later.
 405 
 406 void Method::remove_unshareable_info() {
 407   unlink_method();
 408   JFR_ONLY(REMOVE_METHOD_ID(this);)
 409 }
 410 

 647 bool Method::init_method_counters(MethodCounters* counters) {
 648   // Try to install a pointer to MethodCounters, return true on success.
 649   return Atomic::replace_if_null(&_method_counters, counters);
 650 }
 651 
 652 void Method::set_exception_handler_entered(int handler_bci) {
 653   if (ProfileExceptionHandlers) {
 654     MethodData* mdo = method_data();
 655     if (mdo != nullptr) {
 656       BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
 657       handler_data.set_exception_handler_entered();
 658     }
 659   }
 660 }
 661 
 662 int Method::extra_stack_words() {
 663   // not an inline function, to avoid a header dependency on Interpreter
 664   return extra_stack_entries() * Interpreter::stackElementSize;
 665 }
 666 
















 667 bool Method::compute_has_loops_flag() {
 668   BytecodeStream bcs(methodHandle(Thread::current(), this));
 669   Bytecodes::Code bc;
 670 
 671   while ((bc = bcs.next()) >= 0) {
 672     switch (bc) {
 673       case Bytecodes::_ifeq:
 674       case Bytecodes::_ifnull:
 675       case Bytecodes::_iflt:
 676       case Bytecodes::_ifle:
 677       case Bytecodes::_ifne:
 678       case Bytecodes::_ifnonnull:
 679       case Bytecodes::_ifgt:
 680       case Bytecodes::_ifge:
 681       case Bytecodes::_if_icmpeq:
 682       case Bytecodes::_if_icmpne:
 683       case Bytecodes::_if_icmplt:
 684       case Bytecodes::_if_icmpgt:
 685       case Bytecodes::_if_icmple:
 686       case Bytecodes::_if_icmpge:

 795 
 796 bool Method::is_accessor() const {
 797   return is_getter() || is_setter();
 798 }
 799 
 800 bool Method::is_getter() const {
 801   if (code_size() != 5) return false;
 802   if (size_of_parameters() != 1) return false;
 803   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 804   if (java_code_at(1) != Bytecodes::_getfield) return false;
 805   switch (java_code_at(4)) {
 806     case Bytecodes::_ireturn:
 807     case Bytecodes::_lreturn:
 808     case Bytecodes::_freturn:
 809     case Bytecodes::_dreturn:
 810     case Bytecodes::_areturn:
 811       break;
 812     default:
 813       return false;
 814   }





 815   return true;
 816 }
 817 
 818 bool Method::is_setter() const {
 819   if (code_size() != 6) return false;
 820   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 821   switch (java_code_at(1)) {
 822     case Bytecodes::_iload_1:
 823     case Bytecodes::_aload_1:
 824     case Bytecodes::_fload_1:
 825       if (size_of_parameters() != 2) return false;
 826       break;
 827     case Bytecodes::_dload_1:
 828     case Bytecodes::_lload_1:
 829       if (size_of_parameters() != 3) return false;
 830       break;
 831     default:
 832       return false;
 833   }
 834   if (java_code_at(2) != Bytecodes::_putfield) return false;
 835   if (java_code_at(5) != Bytecodes::_return)   return false;





 836   return true;
 837 }
 838 
 839 bool Method::is_constant_getter() const {
 840   int last_index = code_size() - 1;
 841   // Check if the first 1-3 bytecodes are a constant push
 842   // and the last bytecode is a return.
 843   return (2 <= code_size() && code_size() <= 4 &&
 844           Bytecodes::is_const(java_code_at(0)) &&
 845           Bytecodes::length_for(java_code_at(0)) == last_index &&
 846           Bytecodes::is_return(java_code_at(last_index)));

 847 }
 848 
 849 bool Method::has_valid_initializer_flags() const {
 850   return (is_static() ||
 851           method_holder()->major_version() < 51);
 852 }
 853 
 854 bool Method::is_static_initializer() const {
 855   // For classfiles version 51 or greater, ensure that the clinit method is
 856   // static.  Non-static methods with the name "<clinit>" are not static
 857   // initializers. (older classfiles exempted for backward compatibility)
 858   return name() == vmSymbols::class_initializer_name() &&
 859          has_valid_initializer_flags();

 860 }
 861 
 862 bool Method::is_object_initializer() const {
 863    return name() == vmSymbols::object_initializer_name();

 864 }
 865 
 866 bool Method::needs_clinit_barrier() const {
 867   return is_static() && !method_holder()->is_initialized();
 868 }
 869 
 870 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 871   int length = method->checked_exceptions_length();
 872   if (length == 0) {  // common case
 873     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 874   } else {
 875     methodHandle h_this(THREAD, method);
 876     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 877     objArrayHandle mirrors (THREAD, m_oop);
 878     for (int i = 0; i < length; i++) {
 879       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 880       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 881       if (log_is_enabled(Warning, exceptions) &&
 882           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 883         ResourceMark rm(THREAD);

 901     // Not necessarily sorted and not necessarily one-to-one.
 902     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 903     while (stream.read_pair()) {
 904       if (stream.bci() == bci) {
 905         // perfect match
 906         return stream.line();
 907       } else {
 908         // update best_bci/line
 909         if (stream.bci() < bci && stream.bci() >= best_bci) {
 910           best_bci  = stream.bci();
 911           best_line = stream.line();
 912         }
 913       }
 914     }
 915   }
 916   return best_line;
 917 }
 918 
 919 
 920 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
 921   if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
 922     Thread *thread = Thread::current();
 923     Symbol* klass_name = constants()->klass_name_at(klass_index);
 924     Handle loader(thread, method_holder()->class_loader());
 925     Handle prot  (thread, method_holder()->protection_domain());
 926     return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != nullptr;
 927   } else {
 928     return true;
 929   }
 930 }
 931 
 932 
 933 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
 934   int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
 935   if (must_be_resolved) {
 936     // Make sure klass is resolved in constantpool.
 937     if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;


 938   }
 939   return is_klass_loaded_by_klass_index(klass_index);
 940 }
 941 
 942 
 943 void Method::set_native_function(address function, bool post_event_flag) {
 944   assert(function != nullptr, "use clear_native_function to unregister natives");
 945   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
 946   address* native_function = native_function_addr();
 947 
 948   // We can see racers trying to place the same native function into place. Once
 949   // is plenty.
 950   address current = *native_function;
 951   if (current == function) return;
 952   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
 953       function != nullptr) {
 954     // native_method_throw_unsatisfied_link_error_entry() should only
 955     // be passed when post_event_flag is false.
 956     assert(function !=
 957       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1086 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1087   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1088   if (comp_level == CompLevel_all) {
1089     set_is_not_c1_osr_compilable();
1090     set_is_not_c2_osr_compilable();
1091   } else {
1092     if (is_c1_compile(comp_level))
1093       set_is_not_c1_osr_compilable();
1094     if (is_c2_compile(comp_level))
1095       set_is_not_c2_osr_compilable();
1096   }
1097   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1098 }
1099 
1100 // Revert to using the interpreter and clear out the nmethod
1101 void Method::clear_code() {
1102   // this may be null if c2i adapters have not been made yet
1103   // Only should happen at allocate time.
1104   if (adapter() == nullptr) {
1105     _from_compiled_entry    = nullptr;


1106   } else {
1107     _from_compiled_entry    = adapter()->get_c2i_entry();


1108   }
1109   OrderAccess::storestore();
1110   _from_interpreted_entry = _i2i_entry;
1111   OrderAccess::storestore();
1112   _code = nullptr;
1113 }
1114 
1115 void Method::unlink_code(nmethod *compare) {
1116   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1117   // We need to check if either the _code or _from_compiled_code_entry_point
1118   // refer to this nmethod because there is a race in setting these two fields
1119   // in Method* as seen in bugid 4947125.
1120   if (code() == compare ||
1121       from_compiled_entry() == compare->verified_entry_point()) {
1122     clear_code();
1123   }
1124 }
1125 
1126 void Method::unlink_code() {
1127   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1128   clear_code();
1129 }
1130 
1131 #if INCLUDE_CDS
1132 // Called by class data sharing to remove any entry points (which are not shared)
1133 void Method::unlink_method() {
1134   assert(CDSConfig::is_dumping_archive(), "sanity");
1135   _code = nullptr;
1136   _adapter = nullptr;
1137   _i2i_entry = nullptr;
1138   _from_compiled_entry = nullptr;


1139   _from_interpreted_entry = nullptr;
1140 
1141   if (is_native()) {
1142     *native_function_addr() = nullptr;
1143     set_signature_handler(nullptr);
1144   }
1145   NOT_PRODUCT(set_compiled_invocation_count(0);)
1146 
1147   clear_method_data();
1148   clear_method_counters();
1149   remove_unshareable_flags();
1150 }
1151 
1152 void Method::remove_unshareable_flags() {
1153   // clear all the flags that shouldn't be in the archived version
1154   assert(!is_old(), "must be");
1155   assert(!is_obsolete(), "must be");
1156   assert(!is_deleted(), "must be");
1157 
1158   set_is_prefixed_native(false);
1159   set_queued_for_compilation(false);
1160   set_is_not_c2_compilable(false);
1161   set_is_not_c1_compilable(false);
1162   set_is_not_c2_osr_compilable(false);
1163   set_on_stack_flag(false);


1164 }
1165 #endif
1166 
1167 // Called when the method_holder is getting linked. Setup entrypoints so the method
1168 // is ready to be called from interpreter, compiler, and vtables.
1169 void Method::link_method(const methodHandle& h_method, TRAPS) {
1170   if (log_is_enabled(Info, perf, class, link)) {
1171     ClassLoader::perf_ik_link_methods_count()->inc();
1172   }
1173 
1174   // If the code cache is full, we may reenter this function for the
1175   // leftover methods that weren't linked.
1176   if (adapter() != nullptr) {
1177     return;
1178   }
1179   assert( _code == nullptr, "nothing compiled yet" );
1180 
1181   // Setup interpreter entrypoint
1182   assert(this == h_method(), "wrong h_method()" );
1183 
1184   assert(adapter() == nullptr, "init'd to null");
1185   address entry = Interpreter::entry_for_method(h_method);
1186   assert(entry != nullptr, "interpreter entry must be non-null");
1187   // Sets both _i2i_entry and _from_interpreted_entry
1188   set_interpreter_entry(entry);
1189 
1190   // Don't overwrite already registered native entries.
1191   if (is_native() && !has_native_function()) {
1192     set_native_function(
1193       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1194       !native_bind_event_is_interesting);
1195   }



1196 
1197   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1198   // special handling of vtables.  An alternative is to make adapters more
1199   // lazily by calling make_adapter() from from_compiled_entry() for the
1200   // normal calls.  For vtable calls life gets more complicated.  When a
1201   // call-site goes mega-morphic we need adapters in all methods which can be
1202   // called from the vtable.  We need adapters on such methods that get loaded
1203   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1204   // problem we'll make these lazily later.
1205   (void) make_adapters(h_method, CHECK);
1206 
1207   // ONLY USE the h_method now as make_adapter may have blocked
1208 
1209   if (h_method->is_continuation_native_intrinsic()) {
1210     _from_interpreted_entry = nullptr;
1211     _from_compiled_entry = nullptr;
1212     _i2i_entry = nullptr;
1213     if (Continuations::enabled()) {
1214       assert(!Threads::is_vm_complete(), "should only be called during vm init");
1215       AdapterHandlerLibrary::create_native_wrapper(h_method);

1224 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1225   PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1226 
1227   // Adapters for compiled code are made eagerly here.  They are fairly
1228   // small (generally < 100 bytes) and quick to make (and cached and shared)
1229   // so making them eagerly shouldn't be too expensive.
1230   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1231   if (adapter == nullptr ) {
1232     if (!is_init_completed()) {
1233       // Don't throw exceptions during VM initialization because java.lang.* classes
1234       // might not have been initialized, causing problems when constructing the
1235       // Java exception object.
1236       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1237     } else {
1238       THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1239     }
1240   }
1241 
1242   mh->set_adapter_entry(adapter);
1243   mh->_from_compiled_entry = adapter->get_c2i_entry();


1244   return adapter->get_c2i_entry();
1245 }
1246 
1247 // The verified_code_entry() must be called when a invoke is resolved
1248 // on this method.
1249 
1250 // It returns the compiled code entry point, after asserting not null.
1251 // This function is called after potential safepoints so that nmethod
1252 // or adapter that it points to is still live and valid.
1253 // This function must not hit a safepoint!
1254 address Method::verified_code_entry() {
1255   debug_only(NoSafepointVerifier nsv;)
1256   assert(_from_compiled_entry != nullptr, "must be set");
1257   return _from_compiled_entry;
1258 }
1259 












1260 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1261 // (could be racing a deopt).
1262 // Not inline to avoid circular ref.
1263 bool Method::check_code() const {
1264   // cached in a register or local.  There's a race on the value of the field.
1265   nmethod *code = Atomic::load_acquire(&_code);
1266   return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1267 }
1268 
1269 // Install compiled code.  Instantly it can execute.
1270 void Method::set_code(const methodHandle& mh, nmethod *code) {
1271   assert_lock_strong(NMethodState_lock);
1272   assert( code, "use clear_code to remove code" );
1273   assert( mh->check_code(), "" );
1274 
1275   guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1276 
1277   // These writes must happen in this order, because the interpreter will
1278   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1279   // which jumps to _from_compiled_entry.
1280   mh->_code = code;             // Assign before allowing compiled code to exec
1281 
1282   int comp_level = code->comp_level();
1283   // In theory there could be a race here. In practice it is unlikely
1284   // and not worth worrying about.
1285   if (comp_level > mh->highest_comp_level()) {
1286     mh->set_highest_comp_level(comp_level);
1287   }
1288 
1289   OrderAccess::storestore();
1290   mh->_from_compiled_entry = code->verified_entry_point();


1291   OrderAccess::storestore();
1292 
1293   if (mh->is_continuation_native_intrinsic()) {
1294     assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1295 
1296     if (mh->is_continuation_enter_intrinsic()) {
1297       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1298       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1299     } else if (mh->is_continuation_yield_intrinsic()) {
1300       mh->_i2i_entry = mh->get_i2c_entry();
1301     } else {
1302       guarantee(false, "Unknown Continuation native intrinsic");
1303     }
1304     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1305     Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1306   } else if (!mh->is_method_handle_intrinsic()) {
1307     // Instantly compiled code can execute.
1308     mh->_from_interpreted_entry = mh->get_i2c_entry();
1309   }
1310 }

2248 }
2249 
2250 // Check that this pointer is valid by checking that the vtbl pointer matches
2251 bool Method::is_valid_method(const Method* m) {
2252   if (m == nullptr) {
2253     return false;
2254   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2255     // Quick sanity check on pointer.
2256     return false;
2257   } else if (!os::is_readable_range(m, m + 1)) {
2258     return false;
2259   } else if (m->is_shared()) {
2260     return CppVtables::is_valid_shared_method(m);
2261   } else if (Metaspace::contains_non_shared(m)) {
2262     return has_method_vptr((const void*)m);
2263   } else {
2264     return false;
2265   }
2266 }
2267 

























2268 #ifndef PRODUCT
2269 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2270   out->print("%d", loader_data->jmethod_ids()->count_methods());
2271 }
2272 #endif // PRODUCT
2273 
2274 
2275 // Printing
2276 
2277 #ifndef PRODUCT
2278 
2279 void Method::print_on(outputStream* st) const {
2280   ResourceMark rm;
2281   assert(is_method(), "must be method");
2282   st->print_cr("%s", internal_name());
2283   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2284   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2285   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2286   constants()->print_value_on(st); st->cr();
2287   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2288   st->print   (" - flags:             0x%x  ", _flags.as_int()); _flags.print_on(st); st->cr();
2289   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2290   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2291   st->print_cr(" - max stack:         %d",   max_stack());
2292   st->print_cr(" - max locals:        %d",   max_locals());
2293   st->print_cr(" - size of params:    %d",   size_of_parameters());
2294   st->print_cr(" - method size:       %d",   method_size());
2295   if (intrinsic_id() != vmIntrinsics::_none)
2296     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2297   if (highest_comp_level() != CompLevel_none)
2298     st->print_cr(" - highest level:     %d", highest_comp_level());
2299   st->print_cr(" - vtable index:      %d",   _vtable_index);




2300   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2301   st->print(   " - adapters:          ");
2302   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2303   if (a == nullptr)
2304     st->print_cr(PTR_FORMAT, p2i(a));
2305   else
2306     a->print_adapter_on(st);
2307   st->print_cr(" - compiled entry     " PTR_FORMAT, p2i(from_compiled_entry()));


2308   st->print_cr(" - code size:         %d",   code_size());
2309   if (code_size() != 0) {
2310     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2311     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2312   }
2313   if (method_data() != nullptr) {
2314     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2315   }
2316   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2317   if (checked_exceptions_length() > 0) {
2318     CheckedExceptionElement* table = checked_exceptions_start();
2319     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2320     if (Verbose) {
2321       for (int i = 0; i < checked_exceptions_length(); i++) {
2322         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2323       }
2324     }
2325   }
2326   if (has_linenumber_table()) {
2327     u_char* table = compressed_linenumber_table();

2357     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2358   }
2359 }
2360 
2361 void Method::print_linkage_flags(outputStream* st) {
2362   access_flags().print_on(st);
2363   if (is_default_method()) {
2364     st->print("default ");
2365   }
2366   if (is_overpass()) {
2367     st->print("overpass ");
2368   }
2369 }
2370 #endif //PRODUCT
2371 
2372 void Method::print_value_on(outputStream* st) const {
2373   assert(is_method(), "must be method");
2374   st->print("%s", internal_name());
2375   print_address_on(st);
2376   st->print(" ");

2377   name()->print_value_on(st);
2378   st->print(" ");
2379   signature()->print_value_on(st);
2380   st->print(" in ");
2381   method_holder()->print_value_on(st);
2382   if (WizardMode) st->print("#%d", _vtable_index);
2383   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2384   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2385 }
2386 
2387 // Verification
2388 
2389 void Method::verify_on(outputStream* st) {
2390   guarantee(is_method(), "object must be method");
2391   guarantee(constants()->is_constantPool(), "should be constant pool");
2392   MethodData* md = method_data();
2393   guarantee(md == nullptr ||
2394       md->is_methodData(), "should be method data");
2395 }

  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/constMethod.hpp"
  55 #include "oops/constantPool.hpp"
  56 #include "oops/klass.inline.hpp"
  57 #include "oops/method.inline.hpp"
  58 #include "oops/methodData.hpp"
  59 #include "oops/objArrayKlass.hpp"
  60 #include "oops/objArrayOop.inline.hpp"
  61 #include "oops/oop.inline.hpp"
  62 #include "oops/symbol.hpp"
  63 #include "oops/inlineKlass.inline.hpp"
  64 #include "prims/jvmtiExport.hpp"
  65 #include "prims/methodHandles.hpp"
  66 #include "runtime/atomic.hpp"
  67 #include "runtime/arguments.hpp"
  68 #include "runtime/continuationEntry.hpp"
  69 #include "runtime/frame.inline.hpp"
  70 #include "runtime/handles.inline.hpp"
  71 #include "runtime/init.hpp"
  72 #include "runtime/java.hpp"
  73 #include "runtime/orderAccess.hpp"
  74 #include "runtime/perfData.hpp"
  75 #include "runtime/relocator.hpp"
  76 #include "runtime/safepointVerifiers.hpp"
  77 #include "runtime/sharedRuntime.hpp"
  78 #include "runtime/signature.hpp"
  79 #include "runtime/threads.hpp"
  80 #include "runtime/vm_version.hpp"
  81 #include "utilities/align.hpp"
  82 #include "utilities/quickSort.hpp"
  83 #include "utilities/vmError.hpp"

 104 }
 105 
 106 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
 107   NoSafepointVerifier no_safepoint;
 108   set_constMethod(xconst);
 109   set_access_flags(access_flags);
 110   set_intrinsic_id(vmIntrinsics::_none);
 111   clear_method_data();
 112   clear_method_counters();
 113   set_vtable_index(Method::garbage_vtable_index);
 114 
 115   // Fix and bury in Method*
 116   set_interpreter_entry(nullptr); // sets i2i entry and from_int
 117   set_adapter_entry(nullptr);
 118   Method::clear_code(); // from_c/from_i get set to c2i/i2i
 119 
 120   if (access_flags.is_native()) {
 121     clear_native_function();
 122     set_signature_handler(nullptr);
 123   }

 124   NOT_PRODUCT(set_compiled_invocation_count(0);)
 125   // Name is very useful for debugging.
 126   NOT_PRODUCT(_name = name;)
 127 }
 128 
 129 // Release Method*.  The nmethod will be gone when we get here because
 130 // we've walked the code cache.
 131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
 132   MetadataFactory::free_metadata(loader_data, constMethod());
 133   set_constMethod(nullptr);
 134   MetadataFactory::free_metadata(loader_data, method_data());
 135   clear_method_data();
 136   MetadataFactory::free_metadata(loader_data, method_counters());
 137   clear_method_counters();
 138   // The nmethod will be gone when we get here.
 139   if (code() != nullptr) _code = nullptr;
 140 }
 141 
 142 void Method::release_C_heap_structures() {
 143   if (method_data()) {
 144     method_data()->release_C_heap_structures();
 145 
 146     // Destroy MethodData embedded lock
 147     method_data()->~MethodData();
 148   }
 149 }
 150 
 151 address Method::get_i2c_entry() {
 152   assert(adapter() != nullptr, "must have");
 153   return adapter()->get_i2c_entry();
 154 }
 155 
 156 address Method::get_c2i_entry() {
 157   assert(adapter() != nullptr, "must have");
 158   return adapter()->get_c2i_entry();
 159 }
 160 
 161 address Method::get_c2i_inline_entry() {
 162   assert(adapter() != nullptr, "must have");
 163   return adapter()->get_c2i_inline_entry();
 164 }
 165 
 166 address Method::get_c2i_unverified_entry() {
 167   assert(adapter() != nullptr, "must have");
 168   return adapter()->get_c2i_unverified_entry();
 169 }
 170 
 171 address Method::get_c2i_unverified_inline_entry() {
 172   assert(adapter() != nullptr, "must have");
 173   return adapter()->get_c2i_unverified_inline_entry();
 174 }
 175 
 176 address Method::get_c2i_no_clinit_check_entry() {
 177   assert(VM_Version::supports_fast_class_init_checks(), "");
 178   assert(adapter() != nullptr, "must have");
 179   return adapter()->get_c2i_no_clinit_check_entry();
 180 }
 181 
 182 char* Method::name_and_sig_as_C_string() const {
 183   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
 184 }
 185 
 186 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
 187   return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
 188 }
 189 
 190 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
 191   const char* klass_name = klass->external_name();
 192   int klass_name_len  = (int)strlen(klass_name);
 193   int method_name_len = method_name->utf8_length();
 194   int len             = klass_name_len + 1 + method_name_len + signature->utf8_length();
 195   char* dest          = NEW_RESOURCE_ARRAY(char, len + 1);

 380     return code_base();
 381   } else {
 382     return bcp;
 383   }
 384 }
 385 
 386 int Method::size(bool is_native) {
 387   // If native, then include pointers for native_function and signature_handler
 388   int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
 389   int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
 390   return align_metadata_size(header_size() + extra_words);
 391 }
 392 
 393 Symbol* Method::klass_name() const {
 394   return method_holder()->name();
 395 }
 396 
 397 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
 398   log_trace(cds)("Iter(Method): %p", this);
 399 
 400   if (!method_holder()->is_rewritten() || CDSConfig::is_valhalla_preview()) {
 401     it->push(&_constMethod, MetaspaceClosure::_writable);
 402   } else {
 403     it->push(&_constMethod);
 404   }
 405   it->push(&_method_data);
 406   it->push(&_method_counters);
 407   NOT_PRODUCT(it->push(&_name);)
 408 }
 409 
 410 #if INCLUDE_CDS
 411 // Attempt to return method to original state.  Clear any pointers
 412 // (to objects outside the shared spaces).  We won't be able to predict
 413 // where they should point in a new JVM.  Further initialize some
 414 // entries now in order allow them to be write protected later.
 415 
 416 void Method::remove_unshareable_info() {
 417   unlink_method();
 418   JFR_ONLY(REMOVE_METHOD_ID(this);)
 419 }
 420 

 657 bool Method::init_method_counters(MethodCounters* counters) {
 658   // Try to install a pointer to MethodCounters, return true on success.
 659   return Atomic::replace_if_null(&_method_counters, counters);
 660 }
 661 
 662 void Method::set_exception_handler_entered(int handler_bci) {
 663   if (ProfileExceptionHandlers) {
 664     MethodData* mdo = method_data();
 665     if (mdo != nullptr) {
 666       BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
 667       handler_data.set_exception_handler_entered();
 668     }
 669   }
 670 }
 671 
 672 int Method::extra_stack_words() {
 673   // not an inline function, to avoid a header dependency on Interpreter
 674   return extra_stack_entries() * Interpreter::stackElementSize;
 675 }
 676 
 677 // InlineKlass the method is declared to return. This must not
 678 // safepoint as it is called with references live on the stack at
 679 // locations the GC is unaware of.
 680 InlineKlass* Method::returns_inline_type(Thread* thread) const {
 681   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
 682   if (is_native()) {
 683     return nullptr;
 684   }
 685   NoSafepointVerifier nsv;
 686   SignatureStream ss(signature());
 687   while (!ss.at_return_type()) {
 688     ss.next();
 689   }
 690   return ss.as_inline_klass(method_holder());
 691 }
 692 
 693 bool Method::compute_has_loops_flag() {
 694   BytecodeStream bcs(methodHandle(Thread::current(), this));
 695   Bytecodes::Code bc;
 696 
 697   while ((bc = bcs.next()) >= 0) {
 698     switch (bc) {
 699       case Bytecodes::_ifeq:
 700       case Bytecodes::_ifnull:
 701       case Bytecodes::_iflt:
 702       case Bytecodes::_ifle:
 703       case Bytecodes::_ifne:
 704       case Bytecodes::_ifnonnull:
 705       case Bytecodes::_ifgt:
 706       case Bytecodes::_ifge:
 707       case Bytecodes::_if_icmpeq:
 708       case Bytecodes::_if_icmpne:
 709       case Bytecodes::_if_icmplt:
 710       case Bytecodes::_if_icmpgt:
 711       case Bytecodes::_if_icmple:
 712       case Bytecodes::_if_icmpge:

 821 
 822 bool Method::is_accessor() const {
 823   return is_getter() || is_setter();
 824 }
 825 
 826 bool Method::is_getter() const {
 827   if (code_size() != 5) return false;
 828   if (size_of_parameters() != 1) return false;
 829   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 830   if (java_code_at(1) != Bytecodes::_getfield) return false;
 831   switch (java_code_at(4)) {
 832     case Bytecodes::_ireturn:
 833     case Bytecodes::_lreturn:
 834     case Bytecodes::_freturn:
 835     case Bytecodes::_dreturn:
 836     case Bytecodes::_areturn:
 837       break;
 838     default:
 839       return false;
 840   }
 841   if (has_scalarized_return()) {
 842     // Don't treat this as (trivial) getter method because the
 843     // inline type should be returned in a scalarized form.
 844     return false;
 845   }
 846   return true;
 847 }
 848 
 849 bool Method::is_setter() const {
 850   if (code_size() != 6) return false;
 851   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 852   switch (java_code_at(1)) {
 853     case Bytecodes::_iload_1:
 854     case Bytecodes::_aload_1:
 855     case Bytecodes::_fload_1:
 856       if (size_of_parameters() != 2) return false;
 857       break;
 858     case Bytecodes::_dload_1:
 859     case Bytecodes::_lload_1:
 860       if (size_of_parameters() != 3) return false;
 861       break;
 862     default:
 863       return false;
 864   }
 865   if (java_code_at(2) != Bytecodes::_putfield) return false;
 866   if (java_code_at(5) != Bytecodes::_return)   return false;
 867   if (has_scalarized_args()) {
 868     // Don't treat this as (trivial) setter method because the
 869     // inline type argument should be passed in a scalarized form.
 870     return false;
 871   }
 872   return true;
 873 }
 874 
 875 bool Method::is_constant_getter() const {
 876   int last_index = code_size() - 1;
 877   // Check if the first 1-3 bytecodes are a constant push
 878   // and the last bytecode is a return.
 879   return (2 <= code_size() && code_size() <= 4 &&
 880           Bytecodes::is_const(java_code_at(0)) &&
 881           Bytecodes::length_for(java_code_at(0)) == last_index &&
 882           Bytecodes::is_return(java_code_at(last_index)) &&
 883           !has_scalarized_args());
 884 }
 885 
 886 bool Method::is_class_initializer() const {





 887   // For classfiles version 51 or greater, ensure that the clinit method is
 888   // static.  Non-static methods with the name "<clinit>" are not static
 889   // initializers. (older classfiles exempted for backward compatibility)
 890   return (name() == vmSymbols::class_initializer_name() &&
 891           (is_static() ||
 892            method_holder()->major_version() < 51));
 893 }
 894 
 895 // A method named <init>, is a classic object constructor.
 896 bool Method::is_object_constructor() const {
 897   return name() == vmSymbols::object_initializer_name();
 898 }
 899 
 900 bool Method::needs_clinit_barrier() const {
 901   return is_static() && !method_holder()->is_initialized();
 902 }
 903 
 904 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 905   int length = method->checked_exceptions_length();
 906   if (length == 0) {  // common case
 907     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 908   } else {
 909     methodHandle h_this(THREAD, method);
 910     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 911     objArrayHandle mirrors (THREAD, m_oop);
 912     for (int i = 0; i < length; i++) {
 913       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 914       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 915       if (log_is_enabled(Warning, exceptions) &&
 916           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 917         ResourceMark rm(THREAD);

 935     // Not necessarily sorted and not necessarily one-to-one.
 936     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 937     while (stream.read_pair()) {
 938       if (stream.bci() == bci) {
 939         // perfect match
 940         return stream.line();
 941       } else {
 942         // update best_bci/line
 943         if (stream.bci() < bci && stream.bci() >= best_bci) {
 944           best_bci  = stream.bci();
 945           best_line = stream.line();
 946         }
 947       }
 948     }
 949   }
 950   return best_line;
 951 }
 952 
 953 
 954 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
 955   if( constants()->tag_at(klass_index).is_unresolved_klass()) {
 956     Thread *thread = Thread::current();
 957     Symbol* klass_name = constants()->klass_name_at(klass_index);
 958     Handle loader(thread, method_holder()->class_loader());
 959     Handle prot  (thread, method_holder()->protection_domain());
 960     return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != nullptr;
 961   } else {
 962     return true;
 963   }
 964 }
 965 
 966 
 967 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
 968   int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
 969   if (must_be_resolved) {
 970     // Make sure klass is resolved in constantpool.
 971     if (constants()->tag_at(klass_index).is_unresolved_klass()) {
 972       return false;
 973     }
 974   }
 975   return is_klass_loaded_by_klass_index(klass_index);
 976 }
 977 
 978 
 979 void Method::set_native_function(address function, bool post_event_flag) {
 980   assert(function != nullptr, "use clear_native_function to unregister natives");
 981   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
 982   address* native_function = native_function_addr();
 983 
 984   // We can see racers trying to place the same native function into place. Once
 985   // is plenty.
 986   address current = *native_function;
 987   if (current == function) return;
 988   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
 989       function != nullptr) {
 990     // native_method_throw_unsatisfied_link_error_entry() should only
 991     // be passed when post_event_flag is false.
 992     assert(function !=
 993       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1122 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1123   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1124   if (comp_level == CompLevel_all) {
1125     set_is_not_c1_osr_compilable();
1126     set_is_not_c2_osr_compilable();
1127   } else {
1128     if (is_c1_compile(comp_level))
1129       set_is_not_c1_osr_compilable();
1130     if (is_c2_compile(comp_level))
1131       set_is_not_c2_osr_compilable();
1132   }
1133   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1134 }
1135 
1136 // Revert to using the interpreter and clear out the nmethod
1137 void Method::clear_code() {
1138   // this may be null if c2i adapters have not been made yet
1139   // Only should happen at allocate time.
1140   if (adapter() == nullptr) {
1141     _from_compiled_entry    = nullptr;
1142     _from_compiled_inline_entry = nullptr;
1143     _from_compiled_inline_ro_entry = nullptr;
1144   } else {
1145     _from_compiled_entry    = adapter()->get_c2i_entry();
1146     _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1147     _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1148   }
1149   OrderAccess::storestore();
1150   _from_interpreted_entry = _i2i_entry;
1151   OrderAccess::storestore();
1152   _code = nullptr;
1153 }
1154 
1155 void Method::unlink_code(nmethod *compare) {
1156   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1157   // We need to check if either the _code or _from_compiled_code_entry_point
1158   // refer to this nmethod because there is a race in setting these two fields
1159   // in Method* as seen in bugid 4947125.
1160   if (code() == compare ||
1161       from_compiled_entry() == compare->verified_entry_point()) {
1162     clear_code();
1163   }
1164 }
1165 
1166 void Method::unlink_code() {
1167   ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1168   clear_code();
1169 }
1170 
1171 #if INCLUDE_CDS
1172 // Called by class data sharing to remove any entry points (which are not shared)
1173 void Method::unlink_method() {
1174   assert(CDSConfig::is_dumping_archive(), "sanity");
1175   _code = nullptr;
1176   _adapter = nullptr;
1177   _i2i_entry = nullptr;
1178   _from_compiled_entry = nullptr;
1179   _from_compiled_inline_entry = nullptr;
1180   _from_compiled_inline_ro_entry = nullptr;
1181   _from_interpreted_entry = nullptr;
1182 
1183   if (is_native()) {
1184     *native_function_addr() = nullptr;
1185     set_signature_handler(nullptr);
1186   }
1187   NOT_PRODUCT(set_compiled_invocation_count(0);)
1188 
1189   clear_method_data();
1190   clear_method_counters();
1191   remove_unshareable_flags();
1192 }
1193 
1194 void Method::remove_unshareable_flags() {
1195   // clear all the flags that shouldn't be in the archived version
1196   assert(!is_old(), "must be");
1197   assert(!is_obsolete(), "must be");
1198   assert(!is_deleted(), "must be");
1199 
1200   set_is_prefixed_native(false);
1201   set_queued_for_compilation(false);
1202   set_is_not_c2_compilable(false);
1203   set_is_not_c1_compilable(false);
1204   set_is_not_c2_osr_compilable(false);
1205   set_on_stack_flag(false);
1206   set_has_scalarized_args(false);
1207   set_has_scalarized_return(false);
1208 }
1209 #endif
1210 
1211 // Called when the method_holder is getting linked. Setup entrypoints so the method
1212 // is ready to be called from interpreter, compiler, and vtables.
1213 void Method::link_method(const methodHandle& h_method, TRAPS) {
1214   if (log_is_enabled(Info, perf, class, link)) {
1215     ClassLoader::perf_ik_link_methods_count()->inc();
1216   }
1217 
1218   // If the code cache is full, we may reenter this function for the
1219   // leftover methods that weren't linked.
1220   if (adapter() != nullptr) {
1221     return;
1222   }
1223   assert( _code == nullptr, "nothing compiled yet" );
1224 
1225   // Setup interpreter entrypoint
1226   assert(this == h_method(), "wrong h_method()" );
1227 
1228   assert(adapter() == nullptr, "init'd to null");
1229   address entry = Interpreter::entry_for_method(h_method);
1230   assert(entry != nullptr, "interpreter entry must be non-null");
1231   // Sets both _i2i_entry and _from_interpreted_entry
1232   set_interpreter_entry(entry);
1233 
1234   // Don't overwrite already registered native entries.
1235   if (is_native() && !has_native_function()) {
1236     set_native_function(
1237       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1238       !native_bind_event_is_interesting);
1239   }
1240   if (InlineTypeReturnedAsFields && returns_inline_type(THREAD) && !has_scalarized_return()) {
1241     set_has_scalarized_return();
1242   }
1243 
1244   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1245   // special handling of vtables.  An alternative is to make adapters more
1246   // lazily by calling make_adapter() from from_compiled_entry() for the
1247   // normal calls.  For vtable calls life gets more complicated.  When a
1248   // call-site goes mega-morphic we need adapters in all methods which can be
1249   // called from the vtable.  We need adapters on such methods that get loaded
1250   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1251   // problem we'll make these lazily later.
1252   (void) make_adapters(h_method, CHECK);
1253 
1254   // ONLY USE the h_method now as make_adapter may have blocked
1255 
1256   if (h_method->is_continuation_native_intrinsic()) {
1257     _from_interpreted_entry = nullptr;
1258     _from_compiled_entry = nullptr;
1259     _i2i_entry = nullptr;
1260     if (Continuations::enabled()) {
1261       assert(!Threads::is_vm_complete(), "should only be called during vm init");
1262       AdapterHandlerLibrary::create_native_wrapper(h_method);

1271 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1272   PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1273 
1274   // Adapters for compiled code are made eagerly here.  They are fairly
1275   // small (generally < 100 bytes) and quick to make (and cached and shared)
1276   // so making them eagerly shouldn't be too expensive.
1277   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1278   if (adapter == nullptr ) {
1279     if (!is_init_completed()) {
1280       // Don't throw exceptions during VM initialization because java.lang.* classes
1281       // might not have been initialized, causing problems when constructing the
1282       // Java exception object.
1283       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1284     } else {
1285       THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1286     }
1287   }
1288 
1289   mh->set_adapter_entry(adapter);
1290   mh->_from_compiled_entry = adapter->get_c2i_entry();
1291   mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1292   mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1293   return adapter->get_c2i_entry();
1294 }
1295 
1296 // The verified_code_entry() must be called when a invoke is resolved
1297 // on this method.
1298 
1299 // It returns the compiled code entry point, after asserting not null.
1300 // This function is called after potential safepoints so that nmethod
1301 // or adapter that it points to is still live and valid.
1302 // This function must not hit a safepoint!
1303 address Method::verified_code_entry() {
1304   debug_only(NoSafepointVerifier nsv;)
1305   assert(_from_compiled_entry != nullptr, "must be set");
1306   return _from_compiled_entry;
1307 }
1308 
1309 address Method::verified_inline_code_entry() {
1310   debug_only(NoSafepointVerifier nsv;)
1311   assert(_from_compiled_inline_entry != nullptr, "must be set");
1312   return _from_compiled_inline_entry;
1313 }
1314 
1315 address Method::verified_inline_ro_code_entry() {
1316   debug_only(NoSafepointVerifier nsv;)
1317   assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1318   return _from_compiled_inline_ro_entry;
1319 }
1320 
1321 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1322 // (could be racing a deopt).
1323 // Not inline to avoid circular ref.
1324 bool Method::check_code() const {
1325   // cached in a register or local.  There's a race on the value of the field.
1326   nmethod *code = Atomic::load_acquire(&_code);
1327   return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1328 }
1329 
1330 // Install compiled code.  Instantly it can execute.
1331 void Method::set_code(const methodHandle& mh, nmethod *code) {
1332   assert_lock_strong(NMethodState_lock);
1333   assert( code, "use clear_code to remove code" );
1334   assert( mh->check_code(), "" );
1335 
1336   guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1337 
1338   // These writes must happen in this order, because the interpreter will
1339   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1340   // which jumps to _from_compiled_entry.
1341   mh->_code = code;             // Assign before allowing compiled code to exec
1342 
1343   int comp_level = code->comp_level();
1344   // In theory there could be a race here. In practice it is unlikely
1345   // and not worth worrying about.
1346   if (comp_level > mh->highest_comp_level()) {
1347     mh->set_highest_comp_level(comp_level);
1348   }
1349 
1350   OrderAccess::storestore();
1351   mh->_from_compiled_entry = code->verified_entry_point();
1352   mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1353   mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1354   OrderAccess::storestore();
1355 
1356   if (mh->is_continuation_native_intrinsic()) {
1357     assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1358 
1359     if (mh->is_continuation_enter_intrinsic()) {
1360       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1361       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1362     } else if (mh->is_continuation_yield_intrinsic()) {
1363       mh->_i2i_entry = mh->get_i2c_entry();
1364     } else {
1365       guarantee(false, "Unknown Continuation native intrinsic");
1366     }
1367     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1368     Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1369   } else if (!mh->is_method_handle_intrinsic()) {
1370     // Instantly compiled code can execute.
1371     mh->_from_interpreted_entry = mh->get_i2c_entry();
1372   }
1373 }

2311 }
2312 
2313 // Check that this pointer is valid by checking that the vtbl pointer matches
2314 bool Method::is_valid_method(const Method* m) {
2315   if (m == nullptr) {
2316     return false;
2317   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2318     // Quick sanity check on pointer.
2319     return false;
2320   } else if (!os::is_readable_range(m, m + 1)) {
2321     return false;
2322   } else if (m->is_shared()) {
2323     return CppVtables::is_valid_shared_method(m);
2324   } else if (Metaspace::contains_non_shared(m)) {
2325     return has_method_vptr((const void*)m);
2326   } else {
2327     return false;
2328   }
2329 }
2330 
2331 bool Method::is_scalarized_arg(int idx) const {
2332   if (!has_scalarized_args()) {
2333     return false;
2334   }
2335   // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2336   int depth = 0;
2337   const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2338   for (int i = 0; i < sig->length(); i++) {
2339     BasicType bt = sig->at(i)._bt;
2340     if (bt == T_METADATA) {
2341       depth++;
2342     }
2343     if (idx == 0) {
2344       break; // Argument found
2345     }
2346     if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2347       depth--;
2348     }
2349     if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2350       idx--; // Advance to next argument
2351     }
2352   }
2353   return depth != 0;
2354 }
2355 
2356 #ifndef PRODUCT
2357 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2358   out->print("%d", loader_data->jmethod_ids()->count_methods());
2359 }
2360 #endif // PRODUCT
2361 
2362 
2363 // Printing
2364 
2365 #ifndef PRODUCT
2366 
2367 void Method::print_on(outputStream* st) const {
2368   ResourceMark rm;
2369   assert(is_method(), "must be method");
2370   st->print_cr("%s", internal_name());
2371   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2372   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2373   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2374   constants()->print_value_on(st); st->cr();
2375   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2376   st->print   (" - flags:             0x%x  ", _flags.as_int()); _flags.print_on(st); st->cr();
2377   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2378   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2379   st->print_cr(" - max stack:         %d",   max_stack());
2380   st->print_cr(" - max locals:        %d",   max_locals());
2381   st->print_cr(" - size of params:    %d",   size_of_parameters());
2382   st->print_cr(" - method size:       %d",   method_size());
2383   if (intrinsic_id() != vmIntrinsics::_none)
2384     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2385   if (highest_comp_level() != CompLevel_none)
2386     st->print_cr(" - highest level:     %d", highest_comp_level());
2387   st->print_cr(" - vtable index:      %d",   _vtable_index);
2388 #ifdef ASSERT
2389   if (valid_itable_index())
2390     st->print_cr(" - itable index:      %d",   itable_index());
2391 #endif
2392   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2393   st->print(   " - adapters:          ");
2394   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2395   if (a == nullptr)
2396     st->print_cr(PTR_FORMAT, p2i(a));
2397   else
2398     a->print_adapter_on(st);
2399   st->print_cr(" - compiled entry           " PTR_FORMAT, p2i(from_compiled_entry()));
2400   st->print_cr(" - compiled inline entry    " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2401   st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2402   st->print_cr(" - code size:         %d",   code_size());
2403   if (code_size() != 0) {
2404     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2405     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2406   }
2407   if (method_data() != nullptr) {
2408     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2409   }
2410   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2411   if (checked_exceptions_length() > 0) {
2412     CheckedExceptionElement* table = checked_exceptions_start();
2413     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2414     if (Verbose) {
2415       for (int i = 0; i < checked_exceptions_length(); i++) {
2416         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2417       }
2418     }
2419   }
2420   if (has_linenumber_table()) {
2421     u_char* table = compressed_linenumber_table();

2451     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2452   }
2453 }
2454 
2455 void Method::print_linkage_flags(outputStream* st) {
2456   access_flags().print_on(st);
2457   if (is_default_method()) {
2458     st->print("default ");
2459   }
2460   if (is_overpass()) {
2461     st->print("overpass ");
2462   }
2463 }
2464 #endif //PRODUCT
2465 
2466 void Method::print_value_on(outputStream* st) const {
2467   assert(is_method(), "must be method");
2468   st->print("%s", internal_name());
2469   print_address_on(st);
2470   st->print(" ");
2471   if (WizardMode) access_flags().print_on(st);
2472   name()->print_value_on(st);
2473   st->print(" ");
2474   signature()->print_value_on(st);
2475   st->print(" in ");
2476   method_holder()->print_value_on(st);
2477   if (WizardMode) st->print("#%d", _vtable_index);
2478   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2479   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2480 }
2481 
2482 // Verification
2483 
2484 void Method::verify_on(outputStream* st) {
2485   guarantee(is_method(), "object must be method");
2486   guarantee(constants()->is_constantPool(), "should be constant pool");
2487   MethodData* md = method_data();
2488   guarantee(md == nullptr ||
2489       md->is_methodData(), "should be method data");
2490 }
< prev index next >