< 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);

 636     return nullptr;
 637   }
 638 
 639   if (!mh->init_method_counters(counters)) {
 640     MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters);
 641   }
 642 
 643   return mh->method_counters();
 644 }
 645 
 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 int Method::extra_stack_words() {
 652   // not an inline function, to avoid a header dependency on Interpreter
 653   return extra_stack_entries() * Interpreter::stackElementSize;
 654 }
 655 













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

 686     // Does not call superclass default constructor
 687     return false;
 688   }
 689   // Check optional sequence
 690   for (int i = 4; i < last; i += 5) {
 691     if (cb[i] != Bytecodes::_aload_0) return false;
 692     if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
 693     if (cb[i+2] != Bytecodes::_putfield) return false;
 694   }
 695   return true;
 696 }
 697 
 698 
 699 bool Method::compute_has_loops_flag() {
 700   BytecodeStream bcs(methodHandle(Thread::current(), this));
 701   Bytecodes::Code bc;
 702 
 703   while ((bc = bcs.next()) >= 0) {
 704     switch (bc) {
 705       case Bytecodes::_ifeq:

 827 
 828 bool Method::is_accessor() const {
 829   return is_getter() || is_setter();
 830 }
 831 
 832 bool Method::is_getter() const {
 833   if (code_size() != 5) return false;
 834   if (size_of_parameters() != 1) return false;
 835   if (java_code_at(0) != Bytecodes::_aload_0)  return false;
 836   if (java_code_at(1) != Bytecodes::_getfield) return false;
 837   switch (java_code_at(4)) {
 838     case Bytecodes::_ireturn:
 839     case Bytecodes::_lreturn:
 840     case Bytecodes::_freturn:
 841     case Bytecodes::_dreturn:
 842     case Bytecodes::_areturn:
 843       break;
 844     default:
 845       return false;
 846   }





 847   return true;
 848 }
 849 
 850 bool Method::is_setter() const {
 851   if (code_size() != 6) return false;
 852   if (java_code_at(0) != Bytecodes::_aload_0) return false;
 853   switch (java_code_at(1)) {
 854     case Bytecodes::_iload_1:
 855     case Bytecodes::_aload_1:
 856     case Bytecodes::_fload_1:
 857       if (size_of_parameters() != 2) return false;
 858       break;
 859     case Bytecodes::_dload_1:
 860     case Bytecodes::_lload_1:
 861       if (size_of_parameters() != 3) return false;
 862       break;
 863     default:
 864       return false;
 865   }
 866   if (java_code_at(2) != Bytecodes::_putfield) return false;
 867   if (java_code_at(5) != Bytecodes::_return)   return false;





 868   return true;
 869 }
 870 
 871 bool Method::is_constant_getter() const {
 872   int last_index = code_size() - 1;
 873   // Check if the first 1-3 bytecodes are a constant push
 874   // and the last bytecode is a return.
 875   return (2 <= code_size() && code_size() <= 4 &&
 876           Bytecodes::is_const(java_code_at(0)) &&
 877           Bytecodes::length_for(java_code_at(0)) == last_index &&
 878           Bytecodes::is_return(java_code_at(last_index)));

 879 }
 880 
 881 bool Method::is_initializer() const {
 882   return is_object_initializer() || is_static_initializer();
 883 }
 884 
 885 bool Method::has_valid_initializer_flags() const {
 886   return (is_static() ||
 887           method_holder()->major_version() < 51);
 888 }
 889 
 890 bool Method::is_static_initializer() const {
 891   // For classfiles version 51 or greater, ensure that the clinit method is
 892   // static.  Non-static methods with the name "<clinit>" are not static
 893   // initializers. (older classfiles exempted for backward compatibility)
 894   return name() == vmSymbols::class_initializer_name() &&
 895          has_valid_initializer_flags();

 896 }
 897 
 898 bool Method::is_object_initializer() const {
 899    return name() == vmSymbols::object_initializer_name();

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

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


 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   } else {
1143     _from_compiled_entry    = adapter()->get_c2i_entry();


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


1175   _from_interpreted_entry = nullptr;
1176 
1177   if (is_native()) {
1178     *native_function_addr() = nullptr;
1179     set_signature_handler(nullptr);
1180   }
1181   NOT_PRODUCT(set_compiled_invocation_count(0);)
1182 
1183   clear_method_data();
1184   clear_method_counters();
1185   remove_unshareable_flags();
1186 }
1187 
1188 void Method::remove_unshareable_flags() {
1189   // clear all the flags that shouldn't be in the archived version
1190   assert(!is_old(), "must be");
1191   assert(!is_obsolete(), "must be");
1192   assert(!is_deleted(), "must be");
1193 
1194   set_is_prefixed_native(false);

1208   if (adapter() != nullptr) {
1209     return;
1210   }
1211   assert( _code == nullptr, "nothing compiled yet" );
1212 
1213   // Setup interpreter entrypoint
1214   assert(this == h_method(), "wrong h_method()" );
1215 
1216   assert(adapter() == nullptr, "init'd to null");
1217   address entry = Interpreter::entry_for_method(h_method);
1218   assert(entry != nullptr, "interpreter entry must be non-null");
1219   // Sets both _i2i_entry and _from_interpreted_entry
1220   set_interpreter_entry(entry);
1221 
1222   // Don't overwrite already registered native entries.
1223   if (is_native() && !has_native_function()) {
1224     set_native_function(
1225       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1226       !native_bind_event_is_interesting);
1227   }




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


1267   return adapter->get_c2i_entry();
1268 }
1269 
1270 // The verified_code_entry() must be called when a invoke is resolved
1271 // on this method.
1272 
1273 // It returns the compiled code entry point, after asserting not null.
1274 // This function is called after potential safepoints so that nmethod
1275 // or adapter that it points to is still live and valid.
1276 // This function must not hit a safepoint!
1277 address Method::verified_code_entry() {
1278   debug_only(NoSafepointVerifier nsv;)
1279   assert(_from_compiled_entry != nullptr, "must be set");
1280   return _from_compiled_entry;
1281 }
1282 












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


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

2270 }
2271 
2272 // Check that this pointer is valid by checking that the vtbl pointer matches
2273 bool Method::is_valid_method(const Method* m) {
2274   if (m == nullptr) {
2275     return false;
2276   } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2277     // Quick sanity check on pointer.
2278     return false;
2279   } else if (!os::is_readable_range(m, m + 1)) {
2280     return false;
2281   } else if (m->is_shared()) {
2282     return CppVtables::is_valid_shared_method(m);
2283   } else if (Metaspace::contains_non_shared(m)) {
2284     return has_method_vptr((const void*)m);
2285   } else {
2286     return false;
2287   }
2288 }
2289 

























2290 #ifndef PRODUCT
2291 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2292   out->print("%d", loader_data->jmethod_ids()->count_methods());
2293 }
2294 #endif // PRODUCT
2295 
2296 
2297 // Printing
2298 
2299 #ifndef PRODUCT
2300 
2301 void Method::print_on(outputStream* st) const {
2302   ResourceMark rm;
2303   assert(is_method(), "must be method");
2304   st->print_cr("%s", internal_name());
2305   st->print_cr(" - this oop:          " PTR_FORMAT, p2i(this));
2306   st->print   (" - method holder:     "); method_holder()->print_value_on(st); st->cr();
2307   st->print   (" - constants:         " PTR_FORMAT " ", p2i(constants()));
2308   constants()->print_value_on(st); st->cr();
2309   st->print   (" - access:            0x%x  ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2310   st->print   (" - flags:             0x%x  ", _flags.as_int()); _flags.print_on(st); st->cr();
2311   st->print   (" - name:              ");    name()->print_value_on(st); st->cr();
2312   st->print   (" - signature:         ");    signature()->print_value_on(st); st->cr();
2313   st->print_cr(" - max stack:         %d",   max_stack());
2314   st->print_cr(" - max locals:        %d",   max_locals());
2315   st->print_cr(" - size of params:    %d",   size_of_parameters());
2316   st->print_cr(" - method size:       %d",   method_size());
2317   if (intrinsic_id() != vmIntrinsics::_none)
2318     st->print_cr(" - intrinsic id:      %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2319   if (highest_comp_level() != CompLevel_none)
2320     st->print_cr(" - highest level:     %d", highest_comp_level());
2321   st->print_cr(" - vtable index:      %d",   _vtable_index);




2322   st->print_cr(" - i2i entry:         " PTR_FORMAT, p2i(interpreter_entry()));
2323   st->print(   " - adapters:          ");
2324   AdapterHandlerEntry* a = ((Method*)this)->adapter();
2325   if (a == nullptr)
2326     st->print_cr(PTR_FORMAT, p2i(a));
2327   else
2328     a->print_adapter_on(st);
2329   st->print_cr(" - compiled entry     " PTR_FORMAT, p2i(from_compiled_entry()));


2330   st->print_cr(" - code size:         %d",   code_size());
2331   if (code_size() != 0) {
2332     st->print_cr(" - code start:        " PTR_FORMAT, p2i(code_base()));
2333     st->print_cr(" - code end (excl):   " PTR_FORMAT, p2i(code_base() + code_size()));
2334   }
2335   if (method_data() != nullptr) {
2336     st->print_cr(" - method data:       " PTR_FORMAT, p2i(method_data()));
2337   }
2338   st->print_cr(" - checked ex length: %d",   checked_exceptions_length());
2339   if (checked_exceptions_length() > 0) {
2340     CheckedExceptionElement* table = checked_exceptions_start();
2341     st->print_cr(" - checked ex start:  " PTR_FORMAT, p2i(table));
2342     if (Verbose) {
2343       for (int i = 0; i < checked_exceptions_length(); i++) {
2344         st->print_cr("   - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2345       }
2346     }
2347   }
2348   if (has_linenumber_table()) {
2349     u_char* table = compressed_linenumber_table();

2379     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2380   }
2381 }
2382 
2383 void Method::print_linkage_flags(outputStream* st) {
2384   access_flags().print_on(st);
2385   if (is_default_method()) {
2386     st->print("default ");
2387   }
2388   if (is_overpass()) {
2389     st->print("overpass ");
2390   }
2391 }
2392 #endif //PRODUCT
2393 
2394 void Method::print_value_on(outputStream* st) const {
2395   assert(is_method(), "must be method");
2396   st->print("%s", internal_name());
2397   print_address_on(st);
2398   st->print(" ");

2399   name()->print_value_on(st);
2400   st->print(" ");
2401   signature()->print_value_on(st);
2402   st->print(" in ");
2403   method_holder()->print_value_on(st);
2404   if (WizardMode) st->print("#%d", _vtable_index);
2405   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2406   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2407 }
2408 
2409 // Verification
2410 
2411 void Method::verify_on(outputStream* st) {
2412   guarantee(is_method(), "object must be method");
2413   guarantee(constants()->is_constantPool(), "should be constant pool");
2414   MethodData* md = method_data();
2415   guarantee(md == nullptr ||
2416       md->is_methodData(), "should be method data");
2417 }

  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);

 646     return nullptr;
 647   }
 648 
 649   if (!mh->init_method_counters(counters)) {
 650     MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters);
 651   }
 652 
 653   return mh->method_counters();
 654 }
 655 
 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 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 // InlineKlass the method is declared to return. This must not
 667 // safepoint as it is called with references live on the stack at
 668 // locations the GC is unaware of.
 669 InlineKlass* Method::returns_inline_type(Thread* thread) const {
 670   assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
 671   NoSafepointVerifier nsv;
 672   SignatureStream ss(signature());
 673   while (!ss.at_return_type()) {
 674     ss.next();
 675   }
 676   return ss.as_inline_klass(method_holder());
 677 }
 678 
 679 bool Method::is_vanilla_constructor() const {
 680   // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
 681   // which only calls the superclass vanilla constructor and possibly does stores of
 682   // zero constants to local fields:
 683   //
 684   //   aload_0, _fast_aload_0, or _nofast_aload_0
 685   //   invokespecial
 686   //   indexbyte1
 687   //   indexbyte2
 688   //
 689   // followed by an (optional) sequence of:
 690   //
 691   //   aload_0
 692   //   aconst_null / iconst_0 / fconst_0 / dconst_0
 693   //   putfield
 694   //   indexbyte1
 695   //   indexbyte2
 696   //
 697   // followed by:
 698   //
 699   //   return
 700 
 701   assert(name() == vmSymbols::object_initializer_name(),    "Should only be called for default constructors");
 702   assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
 703   int size = code_size();
 704   // Check if size match
 705   if (size == 0 || size % 5 != 0) return false;
 706   address cb = code_base();
 707   int last = size - 1;
 708   if ((cb[0] != Bytecodes::_aload_0 && cb[0] != Bytecodes::_fast_aload_0 && cb[0] != Bytecodes::_nofast_aload_0) ||
 709        cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
 710     // Does not call superclass default constructor
 711     return false;
 712   }
 713   // Check optional sequence
 714   for (int i = 4; i < last; i += 5) {
 715     if (cb[i] != Bytecodes::_aload_0) return false;
 716     if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
 717     if (cb[i+2] != Bytecodes::_putfield) return false;
 718   }
 719   return true;
 720 }
 721 
 722 
 723 bool Method::compute_has_loops_flag() {
 724   BytecodeStream bcs(methodHandle(Thread::current(), this));
 725   Bytecodes::Code bc;
 726 
 727   while ((bc = bcs.next()) >= 0) {
 728     switch (bc) {
 729       case Bytecodes::_ifeq:

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









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

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

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

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

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

2466     st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2467   }
2468 }
2469 
2470 void Method::print_linkage_flags(outputStream* st) {
2471   access_flags().print_on(st);
2472   if (is_default_method()) {
2473     st->print("default ");
2474   }
2475   if (is_overpass()) {
2476     st->print("overpass ");
2477   }
2478 }
2479 #endif //PRODUCT
2480 
2481 void Method::print_value_on(outputStream* st) const {
2482   assert(is_method(), "must be method");
2483   st->print("%s", internal_name());
2484   print_address_on(st);
2485   st->print(" ");
2486   if (WizardMode) access_flags().print_on(st);
2487   name()->print_value_on(st);
2488   st->print(" ");
2489   signature()->print_value_on(st);
2490   st->print(" in ");
2491   method_holder()->print_value_on(st);
2492   if (WizardMode) st->print("#%d", _vtable_index);
2493   if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2494   if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2495 }
2496 
2497 // Verification
2498 
2499 void Method::verify_on(outputStream* st) {
2500   guarantee(is_method(), "object must be method");
2501   guarantee(constants()->is_constantPool(), "should be constant pool");
2502   MethodData* md = method_data();
2503   guarantee(md == nullptr ||
2504       md->is_methodData(), "should be method data");
2505 }
< prev index next >