< prev index next >

src/hotspot/share/oops/method.cpp

Print this page

  42 #include "interpreter/oopMapCache.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "logging/logTag.hpp"
  46 #include "memory/allocation.inline.hpp"
  47 #include "memory/metadataFactory.hpp"
  48 #include "memory/metaspaceClosure.hpp"
  49 #include "memory/oopFactory.hpp"
  50 #include "memory/resourceArea.hpp"
  51 #include "memory/universe.hpp"
  52 #include "nmt/memTracker.hpp"
  53 #include "oops/constMethod.hpp"
  54 #include "oops/constantPool.hpp"
  55 #include "oops/klass.inline.hpp"
  56 #include "oops/method.inline.hpp"
  57 #include "oops/methodData.hpp"
  58 #include "oops/objArrayKlass.hpp"
  59 #include "oops/objArrayOop.inline.hpp"
  60 #include "oops/oop.inline.hpp"
  61 #include "oops/symbol.hpp"

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

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





 157 address Method::get_c2i_unverified_entry() {
 158   assert(adapter() != nullptr, "must have");
 159   return adapter()->get_c2i_unverified_entry();
 160 }
 161 





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

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

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
















 666 bool Method::is_vanilla_constructor() const {
 667   // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
 668   // which only calls the superclass vanilla constructor and possibly does stores of
 669   // zero constants to local fields:
 670   //
 671   //   aload_0
 672   //   invokespecial
 673   //   indexbyte1
 674   //   indexbyte2
 675   //
 676   // followed by an (optional) sequence of:
 677   //
 678   //   aload_0
 679   //   aconst_null / iconst_0 / fconst_0 / dconst_0
 680   //   putfield
 681   //   indexbyte1
 682   //   indexbyte2
 683   //
 684   // followed by:
 685   //
 686   //   return
 687 
 688   assert(name() == vmSymbols::object_initializer_name(),    "Should only be called for default constructors");
 689   assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
 690   int size = code_size();
 691   // Check if size match
 692   if (size == 0 || size % 5 != 0) return false;
 693   address cb = code_base();
 694   int last = size - 1;
 695   if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {

 696     // Does not call superclass default constructor
 697     return false;
 698   }
 699   // Check optional sequence
 700   for (int i = 4; i < last; i += 5) {
 701     if (cb[i] != Bytecodes::_aload_0) return false;
 702     if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
 703     if (cb[i+2] != Bytecodes::_putfield) return false;
 704   }
 705   return true;
 706 }
 707 
 708 
 709 bool Method::compute_has_loops_flag() {
 710   BytecodeStream bcs(methodHandle(Thread::current(), this));
 711   Bytecodes::Code bc;
 712 
 713   while ((bc = bcs.next()) >= 0) {
 714     switch (bc) {
 715       case Bytecodes::_ifeq:

 837 
 838 bool Method::is_accessor() const {
 839   return is_getter() || is_setter();
 840 }
 841 
 842 bool Method::is_getter() const {
 843   if (code_size() != 5) return false;
 844   if (size_of_parameters() != 1) return false;
 845   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 846   if (java_code_at(1) != Bytecodes::_getfield) return false;
 847   switch (java_code_at(4)) {
 848     case Bytecodes::_ireturn:
 849     case Bytecodes::_lreturn:
 850     case Bytecodes::_freturn:
 851     case Bytecodes::_dreturn:
 852     case Bytecodes::_areturn:
 853       break;
 854     default:
 855       return false;
 856   }





 857   return true;
 858 }
 859 
 860 bool Method::is_setter() const {
 861   if (code_size() != 6) return false;
 862   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 863   switch (java_code_at(1)) {
 864     case Bytecodes::_iload_1:
 865     case Bytecodes::_aload_1:
 866     case Bytecodes::_fload_1:
 867       if (size_of_parameters() != 2) return false;
 868       break;
 869     case Bytecodes::_dload_1:
 870     case Bytecodes::_lload_1:
 871       if (size_of_parameters() != 3) return false;
 872       break;
 873     default:
 874       return false;
 875   }
 876   if (java_code_at(2) != Bytecodes::_putfield) return false;
 877   if (java_code_at(5) != Bytecodes::_return)   return false;





 878   return true;
 879 }
 880 
 881 bool Method::is_constant_getter() const {
 882   int last_index = code_size() - 1;
 883   // Check if the first 1-3 bytecodes are a constant push
 884   // and the last bytecode is a return.
 885   return (2 <= code_size() && code_size() <= 4 &&
 886           Bytecodes::is_const(java_code_at(0)) &&
 887           Bytecodes::length_for(java_code_at(0)) == last_index &&
 888           Bytecodes::is_return(java_code_at(last_index)));
 889 }
 890 
 891 bool Method::is_initializer() const {
 892   return is_object_initializer() || is_static_initializer();
 893 }
 894 
 895 bool Method::has_valid_initializer_flags() const {
 896   return (is_static() ||
 897           method_holder()->major_version() < 51);
 898 }
 899 
 900 bool Method::is_static_initializer() const {
 901   // For classfiles version 51 or greater, ensure that the clinit method is
 902   // static.  Non-static methods with the name "<clinit>" are not static
 903   // initializers. (older classfiles exempted for backward compatibility)
 904   return name() == vmSymbols::class_initializer_name() &&
 905          has_valid_initializer_flags();

 906 }
 907 
 908 bool Method::is_object_initializer() const {
 909    return name() == vmSymbols::object_initializer_name();

 910 }
 911 
 912 bool Method::needs_clinit_barrier() const {
 913   return is_static() && !method_holder()->is_initialized();
 914 }
 915 
 916 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 917   int length = method->checked_exceptions_length();
 918   if (length == 0) {  // common case
 919     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 920   } else {
 921     methodHandle h_this(THREAD, method);
 922     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 923     objArrayHandle mirrors (THREAD, m_oop);
 924     for (int i = 0; i < length; i++) {
 925       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 926       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 927       if (log_is_enabled(Warning, exceptions) &&
 928           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 929         ResourceMark rm(THREAD);

 947     // Not necessarily sorted and not necessarily one-to-one.
 948     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 949     while (stream.read_pair()) {
 950       if (stream.bci() == bci) {
 951         // perfect match
 952         return stream.line();
 953       } else {
 954         // update best_bci/line
 955         if (stream.bci() < bci && stream.bci() >= best_bci) {
 956           best_bci  = stream.bci();
 957           best_line = stream.line();
 958         }
 959       }
 960     }
 961   }
 962   return best_line;
 963 }
 964 
 965 
 966 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
 967   if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
 968     Thread *thread = Thread::current();
 969     Symbol* klass_name = constants()->klass_name_at(klass_index);
 970     Handle loader(thread, method_holder()->class_loader());
 971     Handle prot  (thread, method_holder()->protection_domain());
 972     return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != nullptr;
 973   } else {
 974     return true;
 975   }
 976 }
 977 
 978 
 979 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
 980   int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
 981   if (must_be_resolved) {
 982     // Make sure klass is resolved in constantpool.
 983     if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;


 984   }
 985   return is_klass_loaded_by_klass_index(klass_index);
 986 }
 987 
 988 
 989 void Method::set_native_function(address function, bool post_event_flag) {
 990   assert(function != nullptr, "use clear_native_function to unregister natives");
 991   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
 992   address* native_function = native_function_addr();
 993 
 994   // We can see racers trying to place the same native function into place. Once
 995   // is plenty.
 996   address current = *native_function;
 997   if (current == function) return;
 998   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
 999       function != nullptr) {
1000     // native_method_throw_unsatisfied_link_error_entry() should only
1001     // be passed when post_event_flag is false.
1002     assert(function !=
1003       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1132 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1133   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1134   if (comp_level == CompLevel_all) {
1135     set_is_not_c1_osr_compilable();
1136     set_is_not_c2_osr_compilable();
1137   } else {
1138     if (is_c1_compile(comp_level))
1139       set_is_not_c1_osr_compilable();
1140     if (is_c2_compile(comp_level))
1141       set_is_not_c2_osr_compilable();
1142   }
1143   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1144 }
1145 
1146 // Revert to using the interpreter and clear out the nmethod
1147 void Method::clear_code() {
1148   // this may be null if c2i adapters have not been made yet
1149   // Only should happen at allocate time.
1150   if (adapter() == nullptr) {
1151     _from_compiled_entry    = nullptr;


1152   } else {
1153     _from_compiled_entry    = adapter()->get_c2i_entry();


1154   }
1155   OrderAccess::storestore();
1156   _from_interpreted_entry = _i2i_entry;
1157   OrderAccess::storestore();
1158   _code = nullptr;
1159 }
1160 
1161 void Method::unlink_code(CompiledMethod *compare) {
1162   ConditionalMutexLocker ml(CompiledMethod_lock, !CompiledMethod_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1163   // We need to check if either the _code or _from_compiled_code_entry_point
1164   // refer to this nmethod because there is a race in setting these two fields
1165   // in Method* as seen in bugid 4947125.
1166   if (code() == compare ||
1167       from_compiled_entry() == compare->verified_entry_point()) {
1168     clear_code();
1169   }
1170 }
1171 
1172 void Method::unlink_code() {
1173   ConditionalMutexLocker ml(CompiledMethod_lock, !CompiledMethod_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1174   clear_code();
1175 }
1176 
1177 #if INCLUDE_CDS
1178 // Called by class data sharing to remove any entry points (which are not shared)
1179 void Method::unlink_method() {
1180   assert(CDSConfig::is_dumping_archive(), "sanity");
1181   _code = nullptr;
1182   _adapter = nullptr;
1183   _i2i_entry = nullptr;
1184   _from_compiled_entry = nullptr;


1185   _from_interpreted_entry = nullptr;
1186 
1187   if (is_native()) {
1188     *native_function_addr() = nullptr;
1189     set_signature_handler(nullptr);
1190   }
1191   NOT_PRODUCT(set_compiled_invocation_count(0);)
1192 
1193   clear_method_data();
1194   clear_method_counters();
1195   remove_unshareable_flags();
1196 }
1197 
1198 void Method::remove_unshareable_flags() {
1199   // clear all the flags that shouldn't be in the archived version
1200   assert(!is_old(), "must be");
1201   assert(!is_obsolete(), "must be");
1202   assert(!is_deleted(), "must be");
1203 
1204   set_is_prefixed_native(false);

1218   if (adapter() != nullptr) {
1219     return;
1220   }
1221   assert( _code == nullptr, "nothing compiled yet" );
1222 
1223   // Setup interpreter entrypoint
1224   assert(this == h_method(), "wrong h_method()" );
1225 
1226   assert(adapter() == nullptr, "init'd to null");
1227   address entry = Interpreter::entry_for_method(h_method);
1228   assert(entry != nullptr, "interpreter entry must be non-null");
1229   // Sets both _i2i_entry and _from_interpreted_entry
1230   set_interpreter_entry(entry);
1231 
1232   // Don't overwrite already registered native entries.
1233   if (is_native() && !has_native_function()) {
1234     set_native_function(
1235       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1236       !native_bind_event_is_interesting);
1237   }



1238 
1239   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1240   // special handling of vtables.  An alternative is to make adapters more
1241   // lazily by calling make_adapter() from from_compiled_entry() for the
1242   // normal calls.  For vtable calls life gets more complicated.  When a
1243   // call-site goes mega-morphic we need adapters in all methods which can be
1244   // called from the vtable.  We need adapters on such methods that get loaded
1245   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1246   // problem we'll make these lazily later.
1247   (void) make_adapters(h_method, CHECK);
1248 
1249   // ONLY USE the h_method now as make_adapter may have blocked
1250 
1251   if (h_method->is_continuation_native_intrinsic()) {
1252     // the entry points to this method will be set in set_code, called when first resolving this method
1253     _from_interpreted_entry = nullptr;
1254     _from_compiled_entry = nullptr;
1255     _i2i_entry = nullptr;
1256   }
1257 }
1258 
1259 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1260   // Adapters for compiled code are made eagerly here.  They are fairly
1261   // small (generally < 100 bytes) and quick to make (and cached and shared)
1262   // so making them eagerly shouldn't be too expensive.
1263   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1264   if (adapter == nullptr ) {
1265     if (!is_init_completed()) {
1266       // Don't throw exceptions during VM initialization because java.lang.* classes
1267       // might not have been initialized, causing problems when constructing the
1268       // Java exception object.
1269       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1270     } else {
1271       THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
1272     }
1273   }
1274 
1275   mh->set_adapter_entry(adapter);
1276   mh->_from_compiled_entry = adapter->get_c2i_entry();


1277   return adapter->get_c2i_entry();
1278 }
1279 
1280 // The verified_code_entry() must be called when a invoke is resolved
1281 // on this method.
1282 
1283 // It returns the compiled code entry point, after asserting not null.
1284 // This function is called after potential safepoints so that nmethod
1285 // or adapter that it points to is still live and valid.
1286 // This function must not hit a safepoint!
1287 address Method::verified_code_entry() {
1288   debug_only(NoSafepointVerifier nsv;)
1289   assert(_from_compiled_entry != nullptr, "must be set");
1290   return _from_compiled_entry;
1291 }
1292 












1293 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1294 // (could be racing a deopt).
1295 // Not inline to avoid circular ref.
1296 bool Method::check_code() const {
1297   // cached in a register or local.  There's a race on the value of the field.
1298   CompiledMethod *code = Atomic::load_acquire(&_code);
1299   return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1300 }
1301 
1302 // Install compiled code.  Instantly it can execute.
1303 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1304   assert_lock_strong(CompiledMethod_lock);
1305   assert( code, "use clear_code to remove code" );
1306   assert( mh->check_code(), "" );
1307 
1308   guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1309 
1310   // These writes must happen in this order, because the interpreter will
1311   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1312   // which jumps to _from_compiled_entry.
1313   mh->_code = code;             // Assign before allowing compiled code to exec
1314 
1315   int comp_level = code->comp_level();
1316   // In theory there could be a race here. In practice it is unlikely
1317   // and not worth worrying about.
1318   if (comp_level > mh->highest_comp_level()) {
1319     mh->set_highest_comp_level(comp_level);
1320   }
1321 
1322   OrderAccess::storestore();
1323   mh->_from_compiled_entry = code->verified_entry_point();


1324   OrderAccess::storestore();
1325 
1326   if (mh->is_continuation_native_intrinsic()) {
1327     assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1328 
1329     if (mh->is_continuation_enter_intrinsic()) {
1330       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1331       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1332     } else if (mh->is_continuation_yield_intrinsic()) {
1333       mh->_i2i_entry = mh->get_i2c_entry();
1334     } else {
1335       guarantee(false, "Unknown Continuation native intrinsic");
1336     }
1337     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1338     Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1339   } else if (!mh->is_method_handle_intrinsic()) {
1340     // Instantly compiled code can execute.
1341     mh->_from_interpreted_entry = mh->get_i2c_entry();
1342   }
1343 }

2294 }
2295 
2296 // Check that this pointer is valid by checking that the vtbl pointer matches
2297 bool Method::is_valid_method(const Method* m) {
2298   if (m == nullptr) {
2299     return false;
2300   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2301     // Quick sanity check on pointer.
2302     return false;
2303   } else if (!os::is_readable_range(m, m + 1)) {
2304     return false;
2305   } else if (m->is_shared()) {
2306     return CppVtables::is_valid_shared_method(m);
2307   } else if (Metaspace::contains_non_shared(m)) {
2308     return has_method_vptr((const void*)m);
2309   } else {
2310     return false;
2311   }
2312 }
2313 

























2314 #ifndef PRODUCT
2315 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2316   out->print("%d", loader_data->jmethod_ids()->count_methods());
2317 }
2318 #endif // PRODUCT
2319 
2320 
2321 // Printing
2322 
2323 #ifndef PRODUCT
2324 
2325 void Method::print_on(outputStream* st) const {
2326   ResourceMark rm;
2327   assert(is_method(), "must be method");
2328   st->print_cr("%s", internal_name());
2329   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2330   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2331   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2332   constants()->print_value_on(st); st->cr();
2333   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2334   st->print   (" - flags:             0x%x  ", _flags.as_int()); _flags.print_on(st); st->cr();
2335   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2336   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2337   st->print_cr(" - max stack:         %d",   max_stack());
2338   st->print_cr(" - max locals:        %d",   max_locals());
2339   st->print_cr(" - size of params:    %d",   size_of_parameters());
2340   st->print_cr(" - method size:       %d",   method_size());
2341   if (intrinsic_id() != vmIntrinsics::_none)
2342     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2343   if (highest_comp_level() != CompLevel_none)
2344     st->print_cr(" - highest level:     %d", highest_comp_level());
2345   st->print_cr(" - vtable index:      %d",   _vtable_index);




2346   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2347   st->print(   " - adapters:          ");
2348   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2349   if (a == nullptr)
2350     st->print_cr(PTR_FORMAT, p2i(a));
2351   else
2352     a->print_adapter_on(st);
2353   st->print_cr(" - compiled entry     " PTR_FORMAT, p2i(from_compiled_entry()));


2354   st->print_cr(" - code size:         %d",   code_size());
2355   if (code_size() != 0) {
2356     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2357     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2358   }
2359   if (method_data() != nullptr) {
2360     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2361   }
2362   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2363   if (checked_exceptions_length() > 0) {
2364     CheckedExceptionElement* table = checked_exceptions_start();
2365     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2366     if (Verbose) {
2367       for (int i = 0; i < checked_exceptions_length(); i++) {
2368         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2369       }
2370     }
2371   }
2372   if (has_linenumber_table()) {
2373     u_char* table = compressed_linenumber_table();

2403     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2404   }
2405 }
2406 
2407 void Method::print_linkage_flags(outputStream* st) {
2408   access_flags().print_on(st);
2409   if (is_default_method()) {
2410     st->print("default ");
2411   }
2412   if (is_overpass()) {
2413     st->print("overpass ");
2414   }
2415 }
2416 #endif //PRODUCT
2417 
2418 void Method::print_value_on(outputStream* st) const {
2419   assert(is_method(), "must be method");
2420   st->print("%s", internal_name());
2421   print_address_on(st);
2422   st->print(" ");

2423   name()->print_value_on(st);
2424   st->print(" ");
2425   signature()->print_value_on(st);
2426   st->print(" in ");
2427   method_holder()->print_value_on(st);
2428   if (WizardMode) st->print("#%d", _vtable_index);
2429   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2430   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2431 }
2432 
2433 // Verification
2434 
2435 void Method::verify_on(outputStream* st) {
2436   guarantee(is_method(), "object must be method");
2437   guarantee(constants()->is_constantPool(), "should be constant pool");
2438   MethodData* md = method_data();
2439   guarantee(md == nullptr ||
2440       md->is_methodData(), "should be method data");
2441 }

  42 #include "interpreter/oopMapCache.hpp"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "logging/logTag.hpp"
  46 #include "memory/allocation.inline.hpp"
  47 #include "memory/metadataFactory.hpp"
  48 #include "memory/metaspaceClosure.hpp"
  49 #include "memory/oopFactory.hpp"
  50 #include "memory/resourceArea.hpp"
  51 #include "memory/universe.hpp"
  52 #include "nmt/memTracker.hpp"
  53 #include "oops/constMethod.hpp"
  54 #include "oops/constantPool.hpp"
  55 #include "oops/klass.inline.hpp"
  56 #include "oops/method.inline.hpp"
  57 #include "oops/methodData.hpp"
  58 #include "oops/objArrayKlass.hpp"
  59 #include "oops/objArrayOop.inline.hpp"
  60 #include "oops/oop.inline.hpp"
  61 #include "oops/symbol.hpp"
  62 #include "oops/inlineKlass.inline.hpp"
  63 #include "prims/jvmtiExport.hpp"
  64 #include "prims/methodHandles.hpp"
  65 #include "runtime/atomic.hpp"
  66 #include "runtime/continuationEntry.hpp"
  67 #include "runtime/frame.inline.hpp"
  68 #include "runtime/handles.inline.hpp"
  69 #include "runtime/init.hpp"
  70 #include "runtime/java.hpp"
  71 #include "runtime/orderAccess.hpp"
  72 #include "runtime/relocator.hpp"
  73 #include "runtime/safepointVerifiers.hpp"
  74 #include "runtime/sharedRuntime.hpp"
  75 #include "runtime/signature.hpp"
  76 #include "runtime/vm_version.hpp"
  77 #include "utilities/align.hpp"
  78 #include "utilities/quickSort.hpp"
  79 #include "utilities/vmError.hpp"
  80 #include "utilities/xmlstream.hpp"
  81 
  82 // Implementation of Method

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

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

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

 656 bool Method::init_method_counters(MethodCounters* counters) {
 657   // Try to install a pointer to MethodCounters, return true on success.
 658   return Atomic::replace_if_null(&_method_counters, counters);
 659 }
 660 
 661 void Method::set_exception_handler_entered(int handler_bci) {
 662   if (ProfileExceptionHandlers) {
 663     MethodData* mdo = method_data();
 664     if (mdo != nullptr) {
 665       BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
 666       handler_data.set_exception_handler_entered();
 667     }
 668   }
 669 }
 670 
 671 int Method::extra_stack_words() {
 672   // not an inline function, to avoid a header dependency on Interpreter
 673   return extra_stack_entries() * Interpreter::stackElementSize;
 674 }
 675 
 676 // InlineKlass the method is declared to return. This must not
 677 // safepoint as it is called with references live on the stack at
 678 // locations the GC is unaware of.
 679 InlineKlass* Method::returns_inline_type(Thread* thread) const {
 680   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
 681   if (is_native()) {
 682     return nullptr;
 683   }
 684   NoSafepointVerifier nsv;
 685   SignatureStream ss(signature());
 686   while (!ss.at_return_type()) {
 687     ss.next();
 688   }
 689   return ss.as_inline_klass(method_holder());
 690 }
 691 
 692 bool Method::is_vanilla_constructor() const {
 693   // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
 694   // which only calls the superclass vanilla constructor and possibly does stores of
 695   // zero constants to local fields:
 696   //
 697   //   aload_0, _fast_aload_0, or _nofast_aload_0
 698   //   invokespecial
 699   //   indexbyte1
 700   //   indexbyte2
 701   //
 702   // followed by an (optional) sequence of:
 703   //
 704   //   aload_0
 705   //   aconst_null / iconst_0 / fconst_0 / dconst_0
 706   //   putfield
 707   //   indexbyte1
 708   //   indexbyte2
 709   //
 710   // followed by:
 711   //
 712   //   return
 713 
 714   assert(name() == vmSymbols::object_initializer_name(),    "Should only be called for default constructors");
 715   assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
 716   int size = code_size();
 717   // Check if size match
 718   if (size == 0 || size % 5 != 0) return false;
 719   address cb = code_base();
 720   int last = size - 1;
 721   if ((cb[0] != Bytecodes::_aload_0 && cb[0] != Bytecodes::_fast_aload_0 && cb[0] != Bytecodes::_nofast_aload_0) ||
 722        cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
 723     // Does not call superclass default constructor
 724     return false;
 725   }
 726   // Check optional sequence
 727   for (int i = 4; i < last; i += 5) {
 728     if (cb[i] != Bytecodes::_aload_0) return false;
 729     if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
 730     if (cb[i+2] != Bytecodes::_putfield) return false;
 731   }
 732   return true;
 733 }
 734 
 735 
 736 bool Method::compute_has_loops_flag() {
 737   BytecodeStream bcs(methodHandle(Thread::current(), this));
 738   Bytecodes::Code bc;
 739 
 740   while ((bc = bcs.next()) >= 0) {
 741     switch (bc) {
 742       case Bytecodes::_ifeq:

 864 
 865 bool Method::is_accessor() const {
 866   return is_getter() || is_setter();
 867 }
 868 
 869 bool Method::is_getter() const {
 870   if (code_size() != 5) return false;
 871   if (size_of_parameters() != 1) return false;
 872   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 873   if (java_code_at(1) != Bytecodes::_getfield) return false;
 874   switch (java_code_at(4)) {
 875     case Bytecodes::_ireturn:
 876     case Bytecodes::_lreturn:
 877     case Bytecodes::_freturn:
 878     case Bytecodes::_dreturn:
 879     case Bytecodes::_areturn:
 880       break;
 881     default:
 882       return false;
 883   }
 884   if (has_scalarized_return()) {
 885     // Don't treat this as (trivial) getter method because the
 886     // inline type should be returned in a scalarized form.
 887     return false;
 888   }
 889   return true;
 890 }
 891 
 892 bool Method::is_setter() const {
 893   if (code_size() != 6) return false;
 894   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 895   switch (java_code_at(1)) {
 896     case Bytecodes::_iload_1:
 897     case Bytecodes::_aload_1:
 898     case Bytecodes::_fload_1:
 899       if (size_of_parameters() != 2) return false;
 900       break;
 901     case Bytecodes::_dload_1:
 902     case Bytecodes::_lload_1:
 903       if (size_of_parameters() != 3) return false;
 904       break;
 905     default:
 906       return false;
 907   }
 908   if (java_code_at(2) != Bytecodes::_putfield) return false;
 909   if (java_code_at(5) != Bytecodes::_return)   return false;
 910   if (has_scalarized_args()) {
 911     // Don't treat this as (trivial) setter method because the
 912     // inline type argument should be passed in a scalarized form.
 913     return false;
 914   }
 915   return true;
 916 }
 917 
 918 bool Method::is_constant_getter() const {
 919   int last_index = code_size() - 1;
 920   // Check if the first 1-3 bytecodes are a constant push
 921   // and the last bytecode is a return.
 922   return (2 <= code_size() && code_size() <= 4 &&
 923           Bytecodes::is_const(java_code_at(0)) &&
 924           Bytecodes::length_for(java_code_at(0)) == last_index &&
 925           Bytecodes::is_return(java_code_at(last_index)) &&
 926           !has_scalarized_args());








 927 }
 928 
 929 bool Method::is_class_initializer() const {
 930   // For classfiles version 51 or greater, ensure that the clinit method is
 931   // static.  Non-static methods with the name "<clinit>" are not static
 932   // initializers. (older classfiles exempted for backward compatibility)
 933   return (name() == vmSymbols::class_initializer_name() &&
 934           (is_static() ||
 935            method_holder()->major_version() < 51));
 936 }
 937 
 938 // A method named <init>, is a classic object constructor.
 939 bool Method::is_object_constructor() const {
 940   return name() == vmSymbols::object_initializer_name();
 941 }
 942 
 943 bool Method::needs_clinit_barrier() const {
 944   return is_static() && !method_holder()->is_initialized();
 945 }
 946 
 947 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
 948   int length = method->checked_exceptions_length();
 949   if (length == 0) {  // common case
 950     return objArrayHandle(THREAD, Universe::the_empty_class_array());
 951   } else {
 952     methodHandle h_this(THREAD, method);
 953     objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
 954     objArrayHandle mirrors (THREAD, m_oop);
 955     for (int i = 0; i < length; i++) {
 956       CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
 957       Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
 958       if (log_is_enabled(Warning, exceptions) &&
 959           !k->is_subclass_of(vmClasses::Throwable_klass())) {
 960         ResourceMark rm(THREAD);

 978     // Not necessarily sorted and not necessarily one-to-one.
 979     CompressedLineNumberReadStream stream(compressed_linenumber_table());
 980     while (stream.read_pair()) {
 981       if (stream.bci() == bci) {
 982         // perfect match
 983         return stream.line();
 984       } else {
 985         // update best_bci/line
 986         if (stream.bci() < bci && stream.bci() >= best_bci) {
 987           best_bci  = stream.bci();
 988           best_line = stream.line();
 989         }
 990       }
 991     }
 992   }
 993   return best_line;
 994 }
 995 
 996 
 997 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
 998   if( constants()->tag_at(klass_index).is_unresolved_klass()) {
 999     Thread *thread = Thread::current();
1000     Symbol* klass_name = constants()->klass_name_at(klass_index);
1001     Handle loader(thread, method_holder()->class_loader());
1002     Handle prot  (thread, method_holder()->protection_domain());
1003     return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != nullptr;
1004   } else {
1005     return true;
1006   }
1007 }
1008 
1009 
1010 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1011   int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
1012   if (must_be_resolved) {
1013     // Make sure klass is resolved in constantpool.
1014     if (constants()->tag_at(klass_index).is_unresolved_klass()) {
1015       return false;
1016     }
1017   }
1018   return is_klass_loaded_by_klass_index(klass_index);
1019 }
1020 
1021 
1022 void Method::set_native_function(address function, bool post_event_flag) {
1023   assert(function != nullptr, "use clear_native_function to unregister natives");
1024   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
1025   address* native_function = native_function_addr();
1026 
1027   // We can see racers trying to place the same native function into place. Once
1028   // is plenty.
1029   address current = *native_function;
1030   if (current == function) return;
1031   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1032       function != nullptr) {
1033     // native_method_throw_unsatisfied_link_error_entry() should only
1034     // be passed when post_event_flag is false.
1035     assert(function !=
1036       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1165 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1166   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1167   if (comp_level == CompLevel_all) {
1168     set_is_not_c1_osr_compilable();
1169     set_is_not_c2_osr_compilable();
1170   } else {
1171     if (is_c1_compile(comp_level))
1172       set_is_not_c1_osr_compilable();
1173     if (is_c2_compile(comp_level))
1174       set_is_not_c2_osr_compilable();
1175   }
1176   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1177 }
1178 
1179 // Revert to using the interpreter and clear out the nmethod
1180 void Method::clear_code() {
1181   // this may be null if c2i adapters have not been made yet
1182   // Only should happen at allocate time.
1183   if (adapter() == nullptr) {
1184     _from_compiled_entry    = nullptr;
1185     _from_compiled_inline_entry = nullptr;
1186     _from_compiled_inline_ro_entry = nullptr;
1187   } else {
1188     _from_compiled_entry    = adapter()->get_c2i_entry();
1189     _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1190     _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1191   }
1192   OrderAccess::storestore();
1193   _from_interpreted_entry = _i2i_entry;
1194   OrderAccess::storestore();
1195   _code = nullptr;
1196 }
1197 
1198 void Method::unlink_code(CompiledMethod *compare) {
1199   ConditionalMutexLocker ml(CompiledMethod_lock, !CompiledMethod_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1200   // We need to check if either the _code or _from_compiled_code_entry_point
1201   // refer to this nmethod because there is a race in setting these two fields
1202   // in Method* as seen in bugid 4947125.
1203   if (code() == compare ||
1204       from_compiled_entry() == compare->verified_entry_point()) {
1205     clear_code();
1206   }
1207 }
1208 
1209 void Method::unlink_code() {
1210   ConditionalMutexLocker ml(CompiledMethod_lock, !CompiledMethod_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1211   clear_code();
1212 }
1213 
1214 #if INCLUDE_CDS
1215 // Called by class data sharing to remove any entry points (which are not shared)
1216 void Method::unlink_method() {
1217   assert(CDSConfig::is_dumping_archive(), "sanity");
1218   _code = nullptr;
1219   _adapter = nullptr;
1220   _i2i_entry = nullptr;
1221   _from_compiled_entry = nullptr;
1222   _from_compiled_inline_entry = nullptr;
1223   _from_compiled_inline_ro_entry = nullptr;
1224   _from_interpreted_entry = nullptr;
1225 
1226   if (is_native()) {
1227     *native_function_addr() = nullptr;
1228     set_signature_handler(nullptr);
1229   }
1230   NOT_PRODUCT(set_compiled_invocation_count(0);)
1231 
1232   clear_method_data();
1233   clear_method_counters();
1234   remove_unshareable_flags();
1235 }
1236 
1237 void Method::remove_unshareable_flags() {
1238   // clear all the flags that shouldn't be in the archived version
1239   assert(!is_old(), "must be");
1240   assert(!is_obsolete(), "must be");
1241   assert(!is_deleted(), "must be");
1242 
1243   set_is_prefixed_native(false);

1257   if (adapter() != nullptr) {
1258     return;
1259   }
1260   assert( _code == nullptr, "nothing compiled yet" );
1261 
1262   // Setup interpreter entrypoint
1263   assert(this == h_method(), "wrong h_method()" );
1264 
1265   assert(adapter() == nullptr, "init'd to null");
1266   address entry = Interpreter::entry_for_method(h_method);
1267   assert(entry != nullptr, "interpreter entry must be non-null");
1268   // Sets both _i2i_entry and _from_interpreted_entry
1269   set_interpreter_entry(entry);
1270 
1271   // Don't overwrite already registered native entries.
1272   if (is_native() && !has_native_function()) {
1273     set_native_function(
1274       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1275       !native_bind_event_is_interesting);
1276   }
1277   if (InlineTypeReturnedAsFields && returns_inline_type(THREAD) && !has_scalarized_return()) {
1278     set_has_scalarized_return();
1279   }
1280 
1281   // Setup compiler entrypoint.  This is made eagerly, so we do not need
1282   // special handling of vtables.  An alternative is to make adapters more
1283   // lazily by calling make_adapter() from from_compiled_entry() for the
1284   // normal calls.  For vtable calls life gets more complicated.  When a
1285   // call-site goes mega-morphic we need adapters in all methods which can be
1286   // called from the vtable.  We need adapters on such methods that get loaded
1287   // later.  Ditto for mega-morphic itable calls.  If this proves to be a
1288   // problem we'll make these lazily later.
1289   (void) make_adapters(h_method, CHECK);
1290 
1291   // ONLY USE the h_method now as make_adapter may have blocked
1292 
1293   if (h_method->is_continuation_native_intrinsic()) {
1294     // the entry points to this method will be set in set_code, called when first resolving this method
1295     _from_interpreted_entry = nullptr;
1296     _from_compiled_entry = nullptr;
1297     _i2i_entry = nullptr;
1298   }
1299 }
1300 
1301 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1302   // Adapters for compiled code are made eagerly here.  They are fairly
1303   // small (generally < 100 bytes) and quick to make (and cached and shared)
1304   // so making them eagerly shouldn't be too expensive.
1305   AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1306   if (adapter == nullptr ) {
1307     if (!is_init_completed()) {
1308       // Don't throw exceptions during VM initialization because java.lang.* classes
1309       // might not have been initialized, causing problems when constructing the
1310       // Java exception object.
1311       vm_exit_during_initialization("Out of space in CodeCache for adapters");
1312     } else {
1313       THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
1314     }
1315   }
1316 
1317   mh->set_adapter_entry(adapter);
1318   mh->_from_compiled_entry = adapter->get_c2i_entry();
1319   mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1320   mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1321   return adapter->get_c2i_entry();
1322 }
1323 
1324 // The verified_code_entry() must be called when a invoke is resolved
1325 // on this method.
1326 
1327 // It returns the compiled code entry point, after asserting not null.
1328 // This function is called after potential safepoints so that nmethod
1329 // or adapter that it points to is still live and valid.
1330 // This function must not hit a safepoint!
1331 address Method::verified_code_entry() {
1332   debug_only(NoSafepointVerifier nsv;)
1333   assert(_from_compiled_entry != nullptr, "must be set");
1334   return _from_compiled_entry;
1335 }
1336 
1337 address Method::verified_inline_code_entry() {
1338   debug_only(NoSafepointVerifier nsv;)
1339   assert(_from_compiled_inline_entry != nullptr, "must be set");
1340   return _from_compiled_inline_entry;
1341 }
1342 
1343 address Method::verified_inline_ro_code_entry() {
1344   debug_only(NoSafepointVerifier nsv;)
1345   assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1346   return _from_compiled_inline_ro_entry;
1347 }
1348 
1349 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1350 // (could be racing a deopt).
1351 // Not inline to avoid circular ref.
1352 bool Method::check_code() const {
1353   // cached in a register or local.  There's a race on the value of the field.
1354   CompiledMethod *code = Atomic::load_acquire(&_code);
1355   return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1356 }
1357 
1358 // Install compiled code.  Instantly it can execute.
1359 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1360   assert_lock_strong(CompiledMethod_lock);
1361   assert( code, "use clear_code to remove code" );
1362   assert( mh->check_code(), "" );
1363 
1364   guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1365 
1366   // These writes must happen in this order, because the interpreter will
1367   // directly jump to from_interpreted_entry which jumps to an i2c adapter
1368   // which jumps to _from_compiled_entry.
1369   mh->_code = code;             // Assign before allowing compiled code to exec
1370 
1371   int comp_level = code->comp_level();
1372   // In theory there could be a race here. In practice it is unlikely
1373   // and not worth worrying about.
1374   if (comp_level > mh->highest_comp_level()) {
1375     mh->set_highest_comp_level(comp_level);
1376   }
1377 
1378   OrderAccess::storestore();
1379   mh->_from_compiled_entry = code->verified_entry_point();
1380   mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1381   mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1382   OrderAccess::storestore();
1383 
1384   if (mh->is_continuation_native_intrinsic()) {
1385     assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1386 
1387     if (mh->is_continuation_enter_intrinsic()) {
1388       // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1389       mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1390     } else if (mh->is_continuation_yield_intrinsic()) {
1391       mh->_i2i_entry = mh->get_i2c_entry();
1392     } else {
1393       guarantee(false, "Unknown Continuation native intrinsic");
1394     }
1395     // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1396     Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1397   } else if (!mh->is_method_handle_intrinsic()) {
1398     // Instantly compiled code can execute.
1399     mh->_from_interpreted_entry = mh->get_i2c_entry();
1400   }
1401 }

2352 }
2353 
2354 // Check that this pointer is valid by checking that the vtbl pointer matches
2355 bool Method::is_valid_method(const Method* m) {
2356   if (m == nullptr) {
2357     return false;
2358   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2359     // Quick sanity check on pointer.
2360     return false;
2361   } else if (!os::is_readable_range(m, m + 1)) {
2362     return false;
2363   } else if (m->is_shared()) {
2364     return CppVtables::is_valid_shared_method(m);
2365   } else if (Metaspace::contains_non_shared(m)) {
2366     return has_method_vptr((const void*)m);
2367   } else {
2368     return false;
2369   }
2370 }
2371 
2372 bool Method::is_scalarized_arg(int idx) const {
2373   if (!has_scalarized_args()) {
2374     return false;
2375   }
2376   // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2377   int depth = 0;
2378   const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2379   for (int i = 0; i < sig->length(); i++) {
2380     BasicType bt = sig->at(i)._bt;
2381     if (bt == T_METADATA) {
2382       depth++;
2383     }
2384     if (idx == 0) {
2385       break; // Argument found
2386     }
2387     if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2388       depth--;
2389     }
2390     if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2391       idx--; // Advance to next argument
2392     }
2393   }
2394   return depth != 0;
2395 }
2396 
2397 #ifndef PRODUCT
2398 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2399   out->print("%d", loader_data->jmethod_ids()->count_methods());
2400 }
2401 #endif // PRODUCT
2402 
2403 
2404 // Printing
2405 
2406 #ifndef PRODUCT
2407 
2408 void Method::print_on(outputStream* st) const {
2409   ResourceMark rm;
2410   assert(is_method(), "must be method");
2411   st->print_cr("%s", internal_name());
2412   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2413   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2414   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2415   constants()->print_value_on(st); st->cr();
2416   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2417   st->print   (" - flags:             0x%x  ", _flags.as_int()); _flags.print_on(st); st->cr();
2418   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2419   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2420   st->print_cr(" - max stack:         %d",   max_stack());
2421   st->print_cr(" - max locals:        %d",   max_locals());
2422   st->print_cr(" - size of params:    %d",   size_of_parameters());
2423   st->print_cr(" - method size:       %d",   method_size());
2424   if (intrinsic_id() != vmIntrinsics::_none)
2425     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2426   if (highest_comp_level() != CompLevel_none)
2427     st->print_cr(" - highest level:     %d", highest_comp_level());
2428   st->print_cr(" - vtable index:      %d",   _vtable_index);
2429 #ifdef ASSERT
2430   if (valid_itable_index())
2431     st->print_cr(" - itable index:      %d",   itable_index());
2432 #endif
2433   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2434   st->print(   " - adapters:          ");
2435   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2436   if (a == nullptr)
2437     st->print_cr(PTR_FORMAT, p2i(a));
2438   else
2439     a->print_adapter_on(st);
2440   st->print_cr(" - compiled entry           " PTR_FORMAT, p2i(from_compiled_entry()));
2441   st->print_cr(" - compiled inline entry    " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2442   st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2443   st->print_cr(" - code size:         %d",   code_size());
2444   if (code_size() != 0) {
2445     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2446     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2447   }
2448   if (method_data() != nullptr) {
2449     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2450   }
2451   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2452   if (checked_exceptions_length() > 0) {
2453     CheckedExceptionElement* table = checked_exceptions_start();
2454     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2455     if (Verbose) {
2456       for (int i = 0; i < checked_exceptions_length(); i++) {
2457         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2458       }
2459     }
2460   }
2461   if (has_linenumber_table()) {
2462     u_char* table = compressed_linenumber_table();

2492     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2493   }
2494 }
2495 
2496 void Method::print_linkage_flags(outputStream* st) {
2497   access_flags().print_on(st);
2498   if (is_default_method()) {
2499     st->print("default ");
2500   }
2501   if (is_overpass()) {
2502     st->print("overpass ");
2503   }
2504 }
2505 #endif //PRODUCT
2506 
2507 void Method::print_value_on(outputStream* st) const {
2508   assert(is_method(), "must be method");
2509   st->print("%s", internal_name());
2510   print_address_on(st);
2511   st->print(" ");
2512   if (WizardMode) access_flags().print_on(st);
2513   name()->print_value_on(st);
2514   st->print(" ");
2515   signature()->print_value_on(st);
2516   st->print(" in ");
2517   method_holder()->print_value_on(st);
2518   if (WizardMode) st->print("#%d", _vtable_index);
2519   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2520   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2521 }
2522 
2523 // Verification
2524 
2525 void Method::verify_on(outputStream* st) {
2526   guarantee(is_method(), "object must be method");
2527   guarantee(constants()->is_constantPool(), "should be constant pool");
2528   MethodData* md = method_data();
2529   guarantee(md == nullptr ||
2530       md->is_methodData(), "should be method data");
2531 }
< prev index next >