< prev index next >

src/hotspot/share/oops/method.cpp

Print this page

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

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

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





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





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

 635     return nullptr;
 636   }
 637 
 638   if (!mh->init_method_counters(counters)) {
 639     MetadataFactory::free_metadata(mh->method_holder()->class_loader_data(), counters);
 640   }
 641 
 642   return mh->method_counters();
 643 }
 644 
 645 bool Method::init_method_counters(MethodCounters* counters) {
 646   // Try to install a pointer to MethodCounters, return true on success.
 647   return Atomic::replace_if_null(&_method_counters, counters);
 648 }
 649 
 650 int Method::extra_stack_words() {
 651   // not an inline function, to avoid a header dependency on Interpreter
 652   return extra_stack_entries() * Interpreter::stackElementSize;
 653 }
 654 













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

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

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





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





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

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

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






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

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


 973   }
 974   return is_klass_loaded_by_klass_index(klass_index);
 975 }
 976 
 977 
 978 void Method::set_native_function(address function, bool post_event_flag) {
 979   assert(function != nullptr, "use clear_native_function to unregister natives");
 980   assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
 981   address* native_function = native_function_addr();
 982 
 983   // We can see racers trying to place the same native function into place. Once
 984   // is plenty.
 985   address current = *native_function;
 986   if (current == function) return;
 987   if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
 988       function != nullptr) {
 989     // native_method_throw_unsatisfied_link_error_entry() should only
 990     // be passed when post_event_flag is false.
 991     assert(function !=
 992       SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),

1121 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1122   print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1123   if (comp_level == CompLevel_all) {
1124     set_is_not_c1_osr_compilable();
1125     set_is_not_c2_osr_compilable();
1126   } else {
1127     if (is_c1_compile(comp_level))
1128       set_is_not_c1_osr_compilable();
1129     if (is_c2_compile(comp_level))
1130       set_is_not_c2_osr_compilable();
1131   }
1132   assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1133 }
1134 
1135 // Revert to using the interpreter and clear out the nmethod
1136 void Method::clear_code() {
1137   // this may be null if c2i adapters have not been made yet
1138   // Only should happen at allocate time.
1139   if (adapter() == nullptr) {
1140     _from_compiled_entry    = nullptr;


1141   } else {
1142     _from_compiled_entry    = adapter()->get_c2i_entry();


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


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

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




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


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












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


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

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

























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




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


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

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

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

  40 #include "interpreter/interpreter.hpp"
  41 #include "interpreter/oopMapCache.hpp"
  42 #include "logging/log.hpp"
  43 #include "logging/logTag.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/allocation.inline.hpp"
  46 #include "memory/metadataFactory.hpp"
  47 #include "memory/metaspaceClosure.hpp"
  48 #include "memory/oopFactory.hpp"
  49 #include "memory/resourceArea.hpp"
  50 #include "memory/universe.hpp"
  51 #include "oops/constMethod.hpp"
  52 #include "oops/constantPool.hpp"
  53 #include "oops/klass.inline.hpp"
  54 #include "oops/method.inline.hpp"
  55 #include "oops/methodData.hpp"
  56 #include "oops/objArrayKlass.hpp"
  57 #include "oops/objArrayOop.inline.hpp"
  58 #include "oops/oop.inline.hpp"
  59 #include "oops/symbol.hpp"
  60 #include "oops/inlineKlass.inline.hpp"
  61 #include "prims/jvmtiExport.hpp"
  62 #include "prims/methodHandles.hpp"
  63 #include "runtime/arguments.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/orderAccess.hpp"
  70 #include "runtime/relocator.hpp"
  71 #include "runtime/safepointVerifiers.hpp"
  72 #include "runtime/sharedRuntime.hpp"
  73 #include "runtime/signature.hpp"
  74 #include "runtime/vm_version.hpp"
  75 #include "services/memTracker.hpp"
  76 #include "utilities/align.hpp"
  77 #include "utilities/quickSort.hpp"
  78 #include "utilities/vmError.hpp"
  79 #include "utilities/xmlstream.hpp"
  80 

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

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

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





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

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

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

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

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

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