43 #include "interpreter/oopMapCache.hpp"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "logging/logTag.hpp"
47 #include "memory/allocation.inline.hpp"
48 #include "memory/metadataFactory.hpp"
49 #include "memory/metaspaceClosure.hpp"
50 #include "memory/oopFactory.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "nmt/memTracker.hpp"
54 #include "oops/constMethod.hpp"
55 #include "oops/constantPool.hpp"
56 #include "oops/klass.inline.hpp"
57 #include "oops/method.inline.hpp"
58 #include "oops/methodData.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/symbol.hpp"
63 #include "prims/jvmtiExport.hpp"
64 #include "prims/methodHandles.hpp"
65 #include "runtime/atomic.hpp"
66 #include "runtime/arguments.hpp"
67 #include "runtime/continuationEntry.hpp"
68 #include "runtime/frame.inline.hpp"
69 #include "runtime/handles.inline.hpp"
70 #include "runtime/init.hpp"
71 #include "runtime/java.hpp"
72 #include "runtime/orderAccess.hpp"
73 #include "runtime/perfData.hpp"
74 #include "runtime/relocator.hpp"
75 #include "runtime/safepointVerifiers.hpp"
76 #include "runtime/sharedRuntime.hpp"
77 #include "runtime/signature.hpp"
78 #include "runtime/threads.hpp"
79 #include "runtime/vm_version.hpp"
80 #include "utilities/align.hpp"
81 #include "utilities/quickSort.hpp"
82 #include "utilities/vmError.hpp"
103 }
104
105 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
106 NoSafepointVerifier no_safepoint;
107 set_constMethod(xconst);
108 set_access_flags(access_flags);
109 set_intrinsic_id(vmIntrinsics::_none);
110 clear_method_data();
111 clear_method_counters();
112 set_vtable_index(Method::garbage_vtable_index);
113
114 // Fix and bury in Method*
115 set_interpreter_entry(nullptr); // sets i2i entry and from_int
116 set_adapter_entry(nullptr);
117 Method::clear_code(); // from_c/from_i get set to c2i/i2i
118
119 if (access_flags.is_native()) {
120 clear_native_function();
121 set_signature_handler(nullptr);
122 }
123
124 NOT_PRODUCT(set_compiled_invocation_count(0);)
125 // Name is very useful for debugging.
126 NOT_PRODUCT(_name = name;)
127 }
128
129 // Release Method*. The nmethod will be gone when we get here because
130 // we've walked the code cache.
131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
132 MetadataFactory::free_metadata(loader_data, constMethod());
133 set_constMethod(nullptr);
134 MetadataFactory::free_metadata(loader_data, method_data());
135 clear_method_data();
136 MetadataFactory::free_metadata(loader_data, method_counters());
137 clear_method_counters();
138 set_adapter_entry(nullptr);
139 // The nmethod will be gone when we get here.
140 if (code() != nullptr) _code = nullptr;
141 }
142
143 void Method::release_C_heap_structures() {
144 if (method_data()) {
145 method_data()->release_C_heap_structures();
146
147 // Destroy MethodData embedded lock
148 method_data()->~MethodData();
149 }
150 }
151
152 address Method::get_i2c_entry() {
153 assert(adapter() != nullptr, "must have");
154 return adapter()->get_i2c_entry();
155 }
156
157 address Method::get_c2i_entry() {
158 assert(adapter() != nullptr, "must have");
159 return adapter()->get_c2i_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_no_clinit_check_entry() {
168 assert(VM_Version::supports_fast_class_init_checks(), "");
169 assert(adapter() != nullptr, "must have");
170 return adapter()->get_c2i_no_clinit_check_entry();
171 }
172
173 char* Method::name_and_sig_as_C_string() const {
174 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
175 }
176
177 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
178 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
179 }
180
181 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
182 const char* klass_name = klass->external_name();
183 int klass_name_len = (int)strlen(klass_name);
184 int method_name_len = method_name->utf8_length();
185 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
186 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
372 return code_base();
373 } else {
374 return bcp;
375 }
376 }
377
378 int Method::size(bool is_native) {
379 // If native, then include pointers for native_function and signature_handler
380 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
381 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
382 return align_metadata_size(header_size() + extra_words);
383 }
384
385 Symbol* Method::klass_name() const {
386 return method_holder()->name();
387 }
388
389 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
390 log_trace(aot)("Iter(Method): %p", this);
391
392 if (!method_holder()->is_rewritten()) {
393 it->push(&_constMethod, MetaspaceClosure::_writable);
394 } else {
395 it->push(&_constMethod);
396 }
397 it->push(&_adapter);
398 it->push(&_method_data);
399 it->push(&_method_counters);
400 NOT_PRODUCT(it->push(&_name);)
401 }
402
403 #if INCLUDE_CDS
404 // Attempt to return method to original state. Clear any pointers
405 // (to objects outside the shared spaces). We won't be able to predict
406 // where they should point in a new JVM. Further initialize some
407 // entries now in order allow them to be write protected later.
408
409 void Method::remove_unshareable_info() {
410 unlink_method();
411 if (CDSConfig::is_dumping_adapters() && _adapter != nullptr) {
412 _adapter->remove_unshareable_info();
413 _adapter = nullptr;
414 }
415 JFR_ONLY(REMOVE_METHOD_ID(this);)
416 }
417
418 void Method::restore_unshareable_info(TRAPS) {
419 assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
420 if (_adapter != nullptr) {
421 assert(_adapter->is_linked(), "must be");
422 _from_compiled_entry = _adapter->get_c2i_entry();
423 }
424 assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
425 }
426 #endif
427
428 void Method::set_vtable_index(int index) {
429 if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
430 // At runtime initialize_vtable is rerun as part of link_class_impl()
431 // for a shared class loaded by the non-boot loader to obtain the loader
432 // constraints based on the runtime classloaders' context.
433 return; // don't write into the shared class
434 } else {
435 _vtable_index = index;
436 }
437 }
438
439 void Method::set_itable_index(int index) {
440 if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
441 // At runtime initialize_itable is rerun as part of link_class_impl()
442 // for a shared class loaded by the non-boot loader to obtain the loader
658 bool Method::init_method_counters(MethodCounters* counters) {
659 // Try to install a pointer to MethodCounters, return true on success.
660 return Atomic::replace_if_null(&_method_counters, counters);
661 }
662
663 void Method::set_exception_handler_entered(int handler_bci) {
664 if (ProfileExceptionHandlers) {
665 MethodData* mdo = method_data();
666 if (mdo != nullptr) {
667 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
668 handler_data.set_exception_handler_entered();
669 }
670 }
671 }
672
673 int Method::extra_stack_words() {
674 // not an inline function, to avoid a header dependency on Interpreter
675 return extra_stack_entries() * Interpreter::stackElementSize;
676 }
677
678 bool Method::compute_has_loops_flag() {
679 BytecodeStream bcs(methodHandle(Thread::current(), this));
680 Bytecodes::Code bc;
681
682 while ((bc = bcs.next()) >= 0) {
683 switch (bc) {
684 case Bytecodes::_ifeq:
685 case Bytecodes::_ifnull:
686 case Bytecodes::_iflt:
687 case Bytecodes::_ifle:
688 case Bytecodes::_ifne:
689 case Bytecodes::_ifnonnull:
690 case Bytecodes::_ifgt:
691 case Bytecodes::_ifge:
692 case Bytecodes::_if_icmpeq:
693 case Bytecodes::_if_icmpne:
694 case Bytecodes::_if_icmplt:
695 case Bytecodes::_if_icmpgt:
696 case Bytecodes::_if_icmple:
697 case Bytecodes::_if_icmpge:
806
807 bool Method::is_accessor() const {
808 return is_getter() || is_setter();
809 }
810
811 bool Method::is_getter() const {
812 if (code_size() != 5) return false;
813 if (size_of_parameters() != 1) return false;
814 if (java_code_at(0) != Bytecodes::_aload_0) return false;
815 if (java_code_at(1) != Bytecodes::_getfield) return false;
816 switch (java_code_at(4)) {
817 case Bytecodes::_ireturn:
818 case Bytecodes::_lreturn:
819 case Bytecodes::_freturn:
820 case Bytecodes::_dreturn:
821 case Bytecodes::_areturn:
822 break;
823 default:
824 return false;
825 }
826 return true;
827 }
828
829 bool Method::is_setter() const {
830 if (code_size() != 6) return false;
831 if (java_code_at(0) != Bytecodes::_aload_0) return false;
832 switch (java_code_at(1)) {
833 case Bytecodes::_iload_1:
834 case Bytecodes::_aload_1:
835 case Bytecodes::_fload_1:
836 if (size_of_parameters() != 2) return false;
837 break;
838 case Bytecodes::_dload_1:
839 case Bytecodes::_lload_1:
840 if (size_of_parameters() != 3) return false;
841 break;
842 default:
843 return false;
844 }
845 if (java_code_at(2) != Bytecodes::_putfield) return false;
846 if (java_code_at(5) != Bytecodes::_return) return false;
847 return true;
848 }
849
850 bool Method::is_constant_getter() const {
851 int last_index = code_size() - 1;
852 // Check if the first 1-3 bytecodes are a constant push
853 // and the last bytecode is a return.
854 return (2 <= code_size() && code_size() <= 4 &&
855 Bytecodes::is_const(java_code_at(0)) &&
856 Bytecodes::length_for(java_code_at(0)) == last_index &&
857 Bytecodes::is_return(java_code_at(last_index)));
858 }
859
860 bool Method::has_valid_initializer_flags() const {
861 return (is_static() ||
862 method_holder()->major_version() < 51);
863 }
864
865 bool Method::is_static_initializer() const {
866 // For classfiles version 51 or greater, ensure that the clinit method is
867 // static. Non-static methods with the name "<clinit>" are not static
868 // initializers. (older classfiles exempted for backward compatibility)
869 return name() == vmSymbols::class_initializer_name() &&
870 has_valid_initializer_flags();
871 }
872
873 bool Method::is_object_initializer() const {
874 return name() == vmSymbols::object_initializer_name();
875 }
876
877 bool Method::needs_clinit_barrier() const {
878 return is_static() && !method_holder()->is_initialized();
879 }
880
881 bool Method::is_object_wait0() const {
882 return klass_name() == vmSymbols::java_lang_Object()
883 && name() == vmSymbols::wait_name();
884 }
885
886 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
887 int length = method->checked_exceptions_length();
888 if (length == 0) { // common case
889 return objArrayHandle(THREAD, Universe::the_empty_class_array());
890 } else {
891 methodHandle h_this(THREAD, method);
892 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
893 objArrayHandle mirrors (THREAD, m_oop);
894 for (int i = 0; i < length; i++) {
917 // Not necessarily sorted and not necessarily one-to-one.
918 CompressedLineNumberReadStream stream(compressed_linenumber_table());
919 while (stream.read_pair()) {
920 if (stream.bci() == bci) {
921 // perfect match
922 return stream.line();
923 } else {
924 // update best_bci/line
925 if (stream.bci() < bci && stream.bci() >= best_bci) {
926 best_bci = stream.bci();
927 best_line = stream.line();
928 }
929 }
930 }
931 }
932 return best_line;
933 }
934
935
936 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
937 if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
938 Thread *thread = Thread::current();
939 Symbol* klass_name = constants()->klass_name_at(klass_index);
940 Handle loader(thread, method_holder()->class_loader());
941 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
942 } else {
943 return true;
944 }
945 }
946
947
948 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
949 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
950 if (must_be_resolved) {
951 // Make sure klass is resolved in constantpool.
952 if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;
953 }
954 return is_klass_loaded_by_klass_index(klass_index);
955 }
956
957
958 void Method::set_native_function(address function, bool post_event_flag) {
959 assert(function != nullptr, "use clear_native_function to unregister natives");
960 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
961 address* native_function = native_function_addr();
962
963 // We can see racers trying to place the same native function into place. Once
964 // is plenty.
965 address current = *native_function;
966 if (current == function) return;
967 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
968 function != nullptr) {
969 // native_method_throw_unsatisfied_link_error_entry() should only
970 // be passed when post_event_flag is false.
971 assert(function !=
972 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1101 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1102 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1103 if (comp_level == CompLevel_all) {
1104 set_is_not_c1_osr_compilable();
1105 set_is_not_c2_osr_compilable();
1106 } else {
1107 if (is_c1_compile(comp_level))
1108 set_is_not_c1_osr_compilable();
1109 if (is_c2_compile(comp_level))
1110 set_is_not_c2_osr_compilable();
1111 }
1112 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1113 }
1114
1115 // Revert to using the interpreter and clear out the nmethod
1116 void Method::clear_code() {
1117 // this may be null if c2i adapters have not been made yet
1118 // Only should happen at allocate time.
1119 if (adapter() == nullptr) {
1120 _from_compiled_entry = nullptr;
1121 } else {
1122 _from_compiled_entry = adapter()->get_c2i_entry();
1123 }
1124 OrderAccess::storestore();
1125 _from_interpreted_entry = _i2i_entry;
1126 OrderAccess::storestore();
1127 _code = nullptr;
1128 }
1129
1130 void Method::unlink_code(nmethod *compare) {
1131 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1132 // We need to check if either the _code or _from_compiled_code_entry_point
1133 // refer to this nmethod because there is a race in setting these two fields
1134 // in Method* as seen in bugid 4947125.
1135 if (code() == compare ||
1136 from_compiled_entry() == compare->verified_entry_point()) {
1137 clear_code();
1138 }
1139 }
1140
1141 void Method::unlink_code() {
1142 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1143 clear_code();
1144 }
1145
1146 #if INCLUDE_CDS
1147 // Called by class data sharing to remove any entry points (which are not shared)
1148 void Method::unlink_method() {
1149 assert(CDSConfig::is_dumping_archive(), "sanity");
1150 _code = nullptr;
1151 if (!CDSConfig::is_dumping_adapters() || AdapterHandlerLibrary::is_abstract_method_adapter(_adapter)) {
1152 _adapter = nullptr;
1153 }
1154 _i2i_entry = nullptr;
1155 _from_compiled_entry = nullptr;
1156 _from_interpreted_entry = nullptr;
1157
1158 if (is_native()) {
1159 *native_function_addr() = nullptr;
1160 set_signature_handler(nullptr);
1161 }
1162 NOT_PRODUCT(set_compiled_invocation_count(0);)
1163
1164 clear_method_data();
1165 clear_method_counters();
1166 remove_unshareable_flags();
1167 }
1168
1169 void Method::remove_unshareable_flags() {
1170 // clear all the flags that shouldn't be in the archived version
1171 assert(!is_old(), "must be");
1172 assert(!is_obsolete(), "must be");
1173 assert(!is_deleted(), "must be");
1174
1175 set_is_prefixed_native(false);
1176 set_queued_for_compilation(false);
1177 set_is_not_c2_compilable(false);
1178 set_is_not_c1_compilable(false);
1179 set_is_not_c2_osr_compilable(false);
1180 set_on_stack_flag(false);
1181 }
1182 #endif
1183
1184 // Called when the method_holder is getting linked. Setup entrypoints so the method
1185 // is ready to be called from interpreter, compiler, and vtables.
1186 void Method::link_method(const methodHandle& h_method, TRAPS) {
1187 if (log_is_enabled(Info, perf, class, link)) {
1188 ClassLoader::perf_ik_link_methods_count()->inc();
1189 }
1190
1191 // If the code cache is full, we may reenter this function for the
1192 // leftover methods that weren't linked.
1193 if (adapter() != nullptr) {
1194 if (adapter()->is_shared()) {
1195 assert(adapter()->is_linked(), "Adapter is shared but not linked");
1196 } else {
1197 return;
1198 }
1199 }
1200 assert( _code == nullptr, "nothing compiled yet" );
1201
1202 // Setup interpreter entrypoint
1203 assert(this == h_method(), "wrong h_method()" );
1204
1205 assert(adapter() == nullptr || adapter()->is_linked(), "init'd to null or restored from cache");
1206 address entry = Interpreter::entry_for_method(h_method);
1207 assert(entry != nullptr, "interpreter entry must be non-null");
1208 // Sets both _i2i_entry and _from_interpreted_entry
1209 set_interpreter_entry(entry);
1210
1211 // Don't overwrite already registered native entries.
1212 if (is_native() && !has_native_function()) {
1213 set_native_function(
1214 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1215 !native_bind_event_is_interesting);
1216 }
1217
1218 // Setup compiler entrypoint. This is made eagerly, so we do not need
1219 // special handling of vtables. An alternative is to make adapters more
1220 // lazily by calling make_adapter() from from_compiled_entry() for the
1221 // normal calls. For vtable calls life gets more complicated. When a
1222 // call-site goes mega-morphic we need adapters in all methods which can be
1223 // called from the vtable. We need adapters on such methods that get loaded
1224 // later. Ditto for mega-morphic itable calls. If this proves to be a
1225 // problem we'll make these lazily later.
1226 if (_adapter == nullptr) {
1227 (void) make_adapters(h_method, CHECK);
1228 assert(adapter()->is_linked(), "Adapter must have been linked");
1229 }
1230
1231 // ONLY USE the h_method now as make_adapter may have blocked
1232
1233 if (h_method->is_continuation_native_intrinsic()) {
1234 _from_interpreted_entry = nullptr;
1235 _from_compiled_entry = nullptr;
1236 _i2i_entry = nullptr;
1248 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1249 PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1250
1251 // Adapters for compiled code are made eagerly here. They are fairly
1252 // small (generally < 100 bytes) and quick to make (and cached and shared)
1253 // so making them eagerly shouldn't be too expensive.
1254 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1255 if (adapter == nullptr ) {
1256 if (!is_init_completed()) {
1257 // Don't throw exceptions during VM initialization because java.lang.* classes
1258 // might not have been initialized, causing problems when constructing the
1259 // Java exception object.
1260 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1261 } else {
1262 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1263 }
1264 }
1265
1266 mh->set_adapter_entry(adapter);
1267 mh->_from_compiled_entry = adapter->get_c2i_entry();
1268 return adapter->get_c2i_entry();
1269 }
1270
1271 // The verified_code_entry() must be called when a invoke is resolved
1272 // on this method.
1273
1274 // It returns the compiled code entry point, after asserting not null.
1275 // This function is called after potential safepoints so that nmethod
1276 // or adapter that it points to is still live and valid.
1277 // This function must not hit a safepoint!
1278 address Method::verified_code_entry() {
1279 DEBUG_ONLY(NoSafepointVerifier nsv;)
1280 assert(_from_compiled_entry != nullptr, "must be set");
1281 return _from_compiled_entry;
1282 }
1283
1284 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1285 // (could be racing a deopt).
1286 // Not inline to avoid circular ref.
1287 bool Method::check_code() const {
1288 // cached in a register or local. There's a race on the value of the field.
1289 nmethod *code = Atomic::load_acquire(&_code);
1290 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1291 }
1292
1293 // Install compiled code. Instantly it can execute.
1294 void Method::set_code(const methodHandle& mh, nmethod *code) {
1295 assert_lock_strong(NMethodState_lock);
1296 assert( code, "use clear_code to remove code" );
1297 assert( mh->check_code(), "" );
1298
1299 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1300
1301 // These writes must happen in this order, because the interpreter will
1302 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1303 // which jumps to _from_compiled_entry.
1304 mh->_code = code; // Assign before allowing compiled code to exec
1305
1306 int comp_level = code->comp_level();
1307 // In theory there could be a race here. In practice it is unlikely
1308 // and not worth worrying about.
1309 if (comp_level > mh->highest_comp_level()) {
1310 mh->set_highest_comp_level(comp_level);
1311 }
1312
1313 OrderAccess::storestore();
1314 mh->_from_compiled_entry = code->verified_entry_point();
1315 OrderAccess::storestore();
1316
1317 if (mh->is_continuation_native_intrinsic()) {
1318 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1319
1320 if (mh->is_continuation_enter_intrinsic()) {
1321 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1322 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1323 } else if (mh->is_continuation_yield_intrinsic()) {
1324 mh->_i2i_entry = mh->get_i2c_entry();
1325 } else {
1326 guarantee(false, "Unknown Continuation native intrinsic");
1327 }
1328 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1329 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1330 } else if (!mh->is_method_handle_intrinsic()) {
1331 // Instantly compiled code can execute.
1332 mh->_from_interpreted_entry = mh->get_i2c_entry();
1333 }
1334 }
1487 assert(m->can_be_statically_bound(), "");
1488 m->set_vtable_index(Method::nonvirtual_vtable_index);
1489 m->link_method(m, CHECK_(empty));
1490
1491 if (iid == vmIntrinsics::_linkToNative) {
1492 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1493 }
1494 if (log_is_enabled(Debug, methodhandles)) {
1495 LogTarget(Debug, methodhandles) lt;
1496 LogStream ls(lt);
1497 m->print_on(&ls);
1498 }
1499
1500 return m;
1501 }
1502
1503 #if INCLUDE_CDS
1504 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1505 if (m->adapter() != nullptr) {
1506 m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1507 }
1508 m->link_method(m, CHECK);
1509
1510 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1511 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1512 }
1513 }
1514 #endif
1515
1516 Klass* Method::check_non_bcp_klass(Klass* klass) {
1517 if (klass != nullptr && klass->class_loader() != nullptr) {
1518 if (klass->is_objArray_klass())
1519 klass = ObjArrayKlass::cast(klass)->bottom_klass();
1520 return klass;
1521 }
1522 return nullptr;
1523 }
1524
1525
1526 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
2253 }
2254
2255 // Check that this pointer is valid by checking that the vtbl pointer matches
2256 bool Method::is_valid_method(const Method* m) {
2257 if (m == nullptr) {
2258 return false;
2259 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2260 // Quick sanity check on pointer.
2261 return false;
2262 } else if (!os::is_readable_range(m, m + 1)) {
2263 return false;
2264 } else if (m->is_shared()) {
2265 return CppVtables::is_valid_shared_method(m);
2266 } else if (Metaspace::contains_non_shared(m)) {
2267 return has_method_vptr((const void*)m);
2268 } else {
2269 return false;
2270 }
2271 }
2272
2273 #ifndef PRODUCT
2274 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2275 out->print("%d", loader_data->jmethod_ids()->count_methods());
2276 }
2277 #endif // PRODUCT
2278
2279
2280 // Printing
2281
2282 #ifndef PRODUCT
2283
2284 void Method::print_on(outputStream* st) const {
2285 ResourceMark rm;
2286 assert(is_method(), "must be method");
2287 st->print_cr("%s", internal_name());
2288 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2289 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2290 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2291 constants()->print_value_on(st); st->cr();
2292 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2293 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2294 st->print (" - name: "); name()->print_value_on(st); st->cr();
2295 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2296 st->print_cr(" - max stack: %d", max_stack());
2297 st->print_cr(" - max locals: %d", max_locals());
2298 st->print_cr(" - size of params: %d", size_of_parameters());
2299 st->print_cr(" - method size: %d", method_size());
2300 if (intrinsic_id() != vmIntrinsics::_none)
2301 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2302 if (highest_comp_level() != CompLevel_none)
2303 st->print_cr(" - highest level: %d", highest_comp_level());
2304 st->print_cr(" - vtable index: %d", _vtable_index);
2305 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2306 st->print( " - adapters: ");
2307 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2308 if (a == nullptr)
2309 st->print_cr(PTR_FORMAT, p2i(a));
2310 else
2311 a->print_adapter_on(st);
2312 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2313 st->print_cr(" - code size: %d", code_size());
2314 if (code_size() != 0) {
2315 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2316 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2317 }
2318 if (method_data() != nullptr) {
2319 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2320 }
2321 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2322 if (checked_exceptions_length() > 0) {
2323 CheckedExceptionElement* table = checked_exceptions_start();
2324 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2325 if (Verbose) {
2326 for (int i = 0; i < checked_exceptions_length(); i++) {
2327 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2328 }
2329 }
2330 }
2331 if (has_linenumber_table()) {
2332 u_char* table = compressed_linenumber_table();
2362 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2363 }
2364 }
2365
2366 void Method::print_linkage_flags(outputStream* st) {
2367 access_flags().print_on(st);
2368 if (is_default_method()) {
2369 st->print("default ");
2370 }
2371 if (is_overpass()) {
2372 st->print("overpass ");
2373 }
2374 }
2375 #endif //PRODUCT
2376
2377 void Method::print_value_on(outputStream* st) const {
2378 assert(is_method(), "must be method");
2379 st->print("%s", internal_name());
2380 print_address_on(st);
2381 st->print(" ");
2382 name()->print_value_on(st);
2383 st->print(" ");
2384 signature()->print_value_on(st);
2385 st->print(" in ");
2386 method_holder()->print_value_on(st);
2387 if (WizardMode) st->print("#%d", _vtable_index);
2388 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2389 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2390 }
2391
2392 // Verification
2393
2394 void Method::verify_on(outputStream* st) {
2395 guarantee(is_method(), "object must be method");
2396 guarantee(constants()->is_constantPool(), "should be constant pool");
2397 MethodData* md = method_data();
2398 guarantee(md == nullptr ||
2399 md->is_methodData(), "should be method data");
2400 }
|
43 #include "interpreter/oopMapCache.hpp"
44 #include "logging/log.hpp"
45 #include "logging/logStream.hpp"
46 #include "logging/logTag.hpp"
47 #include "memory/allocation.inline.hpp"
48 #include "memory/metadataFactory.hpp"
49 #include "memory/metaspaceClosure.hpp"
50 #include "memory/oopFactory.hpp"
51 #include "memory/resourceArea.hpp"
52 #include "memory/universe.hpp"
53 #include "nmt/memTracker.hpp"
54 #include "oops/constMethod.hpp"
55 #include "oops/constantPool.hpp"
56 #include "oops/klass.inline.hpp"
57 #include "oops/method.inline.hpp"
58 #include "oops/methodData.hpp"
59 #include "oops/objArrayKlass.hpp"
60 #include "oops/objArrayOop.inline.hpp"
61 #include "oops/oop.inline.hpp"
62 #include "oops/symbol.hpp"
63 #include "oops/inlineKlass.inline.hpp"
64 #include "prims/jvmtiExport.hpp"
65 #include "prims/methodHandles.hpp"
66 #include "runtime/atomic.hpp"
67 #include "runtime/arguments.hpp"
68 #include "runtime/continuationEntry.hpp"
69 #include "runtime/frame.inline.hpp"
70 #include "runtime/handles.inline.hpp"
71 #include "runtime/init.hpp"
72 #include "runtime/java.hpp"
73 #include "runtime/orderAccess.hpp"
74 #include "runtime/perfData.hpp"
75 #include "runtime/relocator.hpp"
76 #include "runtime/safepointVerifiers.hpp"
77 #include "runtime/sharedRuntime.hpp"
78 #include "runtime/signature.hpp"
79 #include "runtime/threads.hpp"
80 #include "runtime/vm_version.hpp"
81 #include "utilities/align.hpp"
82 #include "utilities/quickSort.hpp"
83 #include "utilities/vmError.hpp"
104 }
105
106 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
107 NoSafepointVerifier no_safepoint;
108 set_constMethod(xconst);
109 set_access_flags(access_flags);
110 set_intrinsic_id(vmIntrinsics::_none);
111 clear_method_data();
112 clear_method_counters();
113 set_vtable_index(Method::garbage_vtable_index);
114
115 // Fix and bury in Method*
116 set_interpreter_entry(nullptr); // sets i2i entry and from_int
117 set_adapter_entry(nullptr);
118 Method::clear_code(); // from_c/from_i get set to c2i/i2i
119
120 if (access_flags.is_native()) {
121 clear_native_function();
122 set_signature_handler(nullptr);
123 }
124 NOT_PRODUCT(set_compiled_invocation_count(0);)
125 // Name is very useful for debugging.
126 NOT_PRODUCT(_name = name;)
127 }
128
129 // Release Method*. The nmethod will be gone when we get here because
130 // we've walked the code cache.
131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
132 MetadataFactory::free_metadata(loader_data, constMethod());
133 set_constMethod(nullptr);
134 MetadataFactory::free_metadata(loader_data, method_data());
135 clear_method_data();
136 MetadataFactory::free_metadata(loader_data, method_counters());
137 clear_method_counters();
138 set_adapter_entry(nullptr);
139 // The nmethod will be gone when we get here.
140 if (code() != nullptr) _code = nullptr;
141 }
142
143 void Method::release_C_heap_structures() {
144 if (method_data()) {
145 method_data()->release_C_heap_structures();
146
147 // Destroy MethodData embedded lock
148 method_data()->~MethodData();
149 }
150 }
151
152 address Method::get_i2c_entry() {
153 assert(adapter() != nullptr, "must have");
154 return adapter()->get_i2c_entry();
155 }
156
157 address Method::get_c2i_entry() {
158 assert(adapter() != nullptr, "must have");
159 return adapter()->get_c2i_entry();
160 }
161
162 address Method::get_c2i_inline_entry() {
163 assert(adapter() != nullptr, "must have");
164 return adapter()->get_c2i_inline_entry();
165 }
166
167 address Method::get_c2i_unverified_entry() {
168 assert(adapter() != nullptr, "must have");
169 return adapter()->get_c2i_unverified_entry();
170 }
171
172 address Method::get_c2i_unverified_inline_entry() {
173 assert(adapter() != nullptr, "must have");
174 return adapter()->get_c2i_unverified_inline_entry();
175 }
176
177 address Method::get_c2i_no_clinit_check_entry() {
178 assert(VM_Version::supports_fast_class_init_checks(), "");
179 assert(adapter() != nullptr, "must have");
180 return adapter()->get_c2i_no_clinit_check_entry();
181 }
182
183 char* Method::name_and_sig_as_C_string() const {
184 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
185 }
186
187 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
188 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
189 }
190
191 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
192 const char* klass_name = klass->external_name();
193 int klass_name_len = (int)strlen(klass_name);
194 int method_name_len = method_name->utf8_length();
195 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
196 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
382 return code_base();
383 } else {
384 return bcp;
385 }
386 }
387
388 int Method::size(bool is_native) {
389 // If native, then include pointers for native_function and signature_handler
390 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
391 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
392 return align_metadata_size(header_size() + extra_words);
393 }
394
395 Symbol* Method::klass_name() const {
396 return method_holder()->name();
397 }
398
399 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
400 log_trace(aot)("Iter(Method): %p", this);
401
402 if (!method_holder()->is_rewritten() || CDSConfig::is_valhalla_preview()) {
403 it->push(&_constMethod, MetaspaceClosure::_writable);
404 } else {
405 it->push(&_constMethod);
406 }
407 it->push(&_adapter);
408 it->push(&_method_data);
409 it->push(&_method_counters);
410 NOT_PRODUCT(it->push(&_name);)
411 }
412
413 #if INCLUDE_CDS
414 // Attempt to return method to original state. Clear any pointers
415 // (to objects outside the shared spaces). We won't be able to predict
416 // where they should point in a new JVM. Further initialize some
417 // entries now in order allow them to be write protected later.
418
419 void Method::remove_unshareable_info() {
420 unlink_method();
421 if (CDSConfig::is_dumping_adapters() && _adapter != nullptr) {
422 _adapter->remove_unshareable_info();
423 _adapter = nullptr;
424 }
425 JFR_ONLY(REMOVE_METHOD_ID(this);)
426 }
427
428 void Method::restore_unshareable_info(TRAPS) {
429 assert(is_method() && is_valid_method(this), "ensure C++ vtable is restored");
430 if (_adapter != nullptr) {
431 assert(_adapter->is_linked(), "must be");
432 _from_compiled_entry = _adapter->get_c2i_entry();
433 _from_compiled_inline_entry = _adapter->get_c2i_inline_entry();
434 _from_compiled_inline_ro_entry = _adapter->get_c2i_inline_ro_entry();
435 }
436 assert(!queued_for_compilation(), "method's queued_for_compilation flag should not be set");
437 }
438 #endif
439
440 void Method::set_vtable_index(int index) {
441 if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
442 // At runtime initialize_vtable is rerun as part of link_class_impl()
443 // for a shared class loaded by the non-boot loader to obtain the loader
444 // constraints based on the runtime classloaders' context.
445 return; // don't write into the shared class
446 } else {
447 _vtable_index = index;
448 }
449 }
450
451 void Method::set_itable_index(int index) {
452 if (is_shared() && !MetaspaceShared::remapped_readwrite() && method_holder()->verified_at_dump_time()) {
453 // At runtime initialize_itable is rerun as part of link_class_impl()
454 // for a shared class loaded by the non-boot loader to obtain the loader
670 bool Method::init_method_counters(MethodCounters* counters) {
671 // Try to install a pointer to MethodCounters, return true on success.
672 return Atomic::replace_if_null(&_method_counters, counters);
673 }
674
675 void Method::set_exception_handler_entered(int handler_bci) {
676 if (ProfileExceptionHandlers) {
677 MethodData* mdo = method_data();
678 if (mdo != nullptr) {
679 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
680 handler_data.set_exception_handler_entered();
681 }
682 }
683 }
684
685 int Method::extra_stack_words() {
686 // not an inline function, to avoid a header dependency on Interpreter
687 return extra_stack_entries() * Interpreter::stackElementSize;
688 }
689
690 // InlineKlass the method is declared to return. This must not
691 // safepoint as it is called with references live on the stack at
692 // locations the GC is unaware of.
693 InlineKlass* Method::returns_inline_type(Thread* thread) const {
694 assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
695 if (is_native()) {
696 return nullptr;
697 }
698 NoSafepointVerifier nsv;
699 SignatureStream ss(signature());
700 while (!ss.at_return_type()) {
701 ss.next();
702 }
703 return ss.as_inline_klass(method_holder());
704 }
705
706 bool Method::compute_has_loops_flag() {
707 BytecodeStream bcs(methodHandle(Thread::current(), this));
708 Bytecodes::Code bc;
709
710 while ((bc = bcs.next()) >= 0) {
711 switch (bc) {
712 case Bytecodes::_ifeq:
713 case Bytecodes::_ifnull:
714 case Bytecodes::_iflt:
715 case Bytecodes::_ifle:
716 case Bytecodes::_ifne:
717 case Bytecodes::_ifnonnull:
718 case Bytecodes::_ifgt:
719 case Bytecodes::_ifge:
720 case Bytecodes::_if_icmpeq:
721 case Bytecodes::_if_icmpne:
722 case Bytecodes::_if_icmplt:
723 case Bytecodes::_if_icmpgt:
724 case Bytecodes::_if_icmple:
725 case Bytecodes::_if_icmpge:
834
835 bool Method::is_accessor() const {
836 return is_getter() || is_setter();
837 }
838
839 bool Method::is_getter() const {
840 if (code_size() != 5) return false;
841 if (size_of_parameters() != 1) return false;
842 if (java_code_at(0) != Bytecodes::_aload_0) return false;
843 if (java_code_at(1) != Bytecodes::_getfield) return false;
844 switch (java_code_at(4)) {
845 case Bytecodes::_ireturn:
846 case Bytecodes::_lreturn:
847 case Bytecodes::_freturn:
848 case Bytecodes::_dreturn:
849 case Bytecodes::_areturn:
850 break;
851 default:
852 return false;
853 }
854 if (has_scalarized_return()) {
855 // Don't treat this as (trivial) getter method because the
856 // inline type should be returned in a scalarized form.
857 return false;
858 }
859 return true;
860 }
861
862 bool Method::is_setter() const {
863 if (code_size() != 6) return false;
864 if (java_code_at(0) != Bytecodes::_aload_0) return false;
865 switch (java_code_at(1)) {
866 case Bytecodes::_iload_1:
867 case Bytecodes::_aload_1:
868 case Bytecodes::_fload_1:
869 if (size_of_parameters() != 2) return false;
870 break;
871 case Bytecodes::_dload_1:
872 case Bytecodes::_lload_1:
873 if (size_of_parameters() != 3) return false;
874 break;
875 default:
876 return false;
877 }
878 if (java_code_at(2) != Bytecodes::_putfield) return false;
879 if (java_code_at(5) != Bytecodes::_return) return false;
880 if (has_scalarized_args()) {
881 // Don't treat this as (trivial) setter method because the
882 // inline type argument should be passed in a scalarized form.
883 return false;
884 }
885 return true;
886 }
887
888 bool Method::is_constant_getter() const {
889 int last_index = code_size() - 1;
890 // Check if the first 1-3 bytecodes are a constant push
891 // and the last bytecode is a return.
892 return (2 <= code_size() && code_size() <= 4 &&
893 Bytecodes::is_const(java_code_at(0)) &&
894 Bytecodes::length_for(java_code_at(0)) == last_index &&
895 Bytecodes::is_return(java_code_at(last_index)) &&
896 !has_scalarized_args());
897 }
898
899 bool Method::is_class_initializer() const {
900 // For classfiles version 51 or greater, ensure that the clinit method is
901 // static. Non-static methods with the name "<clinit>" are not static
902 // initializers. (older classfiles exempted for backward compatibility)
903 return (name() == vmSymbols::class_initializer_name() &&
904 (is_static() ||
905 method_holder()->major_version() < 51));
906 }
907
908 // A method named <init>, is a classic object constructor.
909 bool Method::is_object_constructor() const {
910 return name() == vmSymbols::object_initializer_name();
911 }
912
913 bool Method::needs_clinit_barrier() const {
914 return is_static() && !method_holder()->is_initialized();
915 }
916
917 bool Method::is_object_wait0() const {
918 return klass_name() == vmSymbols::java_lang_Object()
919 && name() == vmSymbols::wait_name();
920 }
921
922 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
923 int length = method->checked_exceptions_length();
924 if (length == 0) { // common case
925 return objArrayHandle(THREAD, Universe::the_empty_class_array());
926 } else {
927 methodHandle h_this(THREAD, method);
928 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
929 objArrayHandle mirrors (THREAD, m_oop);
930 for (int i = 0; i < length; i++) {
953 // Not necessarily sorted and not necessarily one-to-one.
954 CompressedLineNumberReadStream stream(compressed_linenumber_table());
955 while (stream.read_pair()) {
956 if (stream.bci() == bci) {
957 // perfect match
958 return stream.line();
959 } else {
960 // update best_bci/line
961 if (stream.bci() < bci && stream.bci() >= best_bci) {
962 best_bci = stream.bci();
963 best_line = stream.line();
964 }
965 }
966 }
967 }
968 return best_line;
969 }
970
971
972 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
973 if( constants()->tag_at(klass_index).is_unresolved_klass()) {
974 Thread *thread = Thread::current();
975 Symbol* klass_name = constants()->klass_name_at(klass_index);
976 Handle loader(thread, method_holder()->class_loader());
977 return SystemDictionary::find_instance_klass(thread, klass_name, loader) != nullptr;
978 } else {
979 return true;
980 }
981 }
982
983
984 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
985 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
986 if (must_be_resolved) {
987 // Make sure klass is resolved in constantpool.
988 if (constants()->tag_at(klass_index).is_unresolved_klass()) {
989 return false;
990 }
991 }
992 return is_klass_loaded_by_klass_index(klass_index);
993 }
994
995
996 void Method::set_native_function(address function, bool post_event_flag) {
997 assert(function != nullptr, "use clear_native_function to unregister natives");
998 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
999 address* native_function = native_function_addr();
1000
1001 // We can see racers trying to place the same native function into place. Once
1002 // is plenty.
1003 address current = *native_function;
1004 if (current == function) return;
1005 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1006 function != nullptr) {
1007 // native_method_throw_unsatisfied_link_error_entry() should only
1008 // be passed when post_event_flag is false.
1009 assert(function !=
1010 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1139 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1140 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1141 if (comp_level == CompLevel_all) {
1142 set_is_not_c1_osr_compilable();
1143 set_is_not_c2_osr_compilable();
1144 } else {
1145 if (is_c1_compile(comp_level))
1146 set_is_not_c1_osr_compilable();
1147 if (is_c2_compile(comp_level))
1148 set_is_not_c2_osr_compilable();
1149 }
1150 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1151 }
1152
1153 // Revert to using the interpreter and clear out the nmethod
1154 void Method::clear_code() {
1155 // this may be null if c2i adapters have not been made yet
1156 // Only should happen at allocate time.
1157 if (adapter() == nullptr) {
1158 _from_compiled_entry = nullptr;
1159 _from_compiled_inline_entry = nullptr;
1160 _from_compiled_inline_ro_entry = nullptr;
1161 } else {
1162 _from_compiled_entry = adapter()->get_c2i_entry();
1163 _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1164 _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1165 }
1166 OrderAccess::storestore();
1167 _from_interpreted_entry = _i2i_entry;
1168 OrderAccess::storestore();
1169 _code = nullptr;
1170 }
1171
1172 void Method::unlink_code(nmethod *compare) {
1173 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1174 // We need to check if either the _code or _from_compiled_code_entry_point
1175 // refer to this nmethod because there is a race in setting these two fields
1176 // in Method* as seen in bugid 4947125.
1177 if (code() == compare ||
1178 from_compiled_entry() == compare->verified_entry_point()) {
1179 clear_code();
1180 }
1181 }
1182
1183 void Method::unlink_code() {
1184 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1185 clear_code();
1186 }
1187
1188 #if INCLUDE_CDS
1189 // Called by class data sharing to remove any entry points (which are not shared)
1190 void Method::unlink_method() {
1191 assert(CDSConfig::is_dumping_archive(), "sanity");
1192 _code = nullptr;
1193 if (!CDSConfig::is_dumping_adapters() || AdapterHandlerLibrary::is_abstract_method_adapter(_adapter)) {
1194 _adapter = nullptr;
1195 }
1196 _i2i_entry = nullptr;
1197 _from_compiled_entry = nullptr;
1198 _from_compiled_inline_entry = nullptr;
1199 _from_compiled_inline_ro_entry = nullptr;
1200 _from_interpreted_entry = nullptr;
1201
1202 if (is_native()) {
1203 *native_function_addr() = nullptr;
1204 set_signature_handler(nullptr);
1205 }
1206 NOT_PRODUCT(set_compiled_invocation_count(0);)
1207
1208 clear_method_data();
1209 clear_method_counters();
1210 remove_unshareable_flags();
1211 }
1212
1213 void Method::remove_unshareable_flags() {
1214 // clear all the flags that shouldn't be in the archived version
1215 assert(!is_old(), "must be");
1216 assert(!is_obsolete(), "must be");
1217 assert(!is_deleted(), "must be");
1218
1219 set_is_prefixed_native(false);
1220 set_queued_for_compilation(false);
1221 set_is_not_c2_compilable(false);
1222 set_is_not_c1_compilable(false);
1223 set_is_not_c2_osr_compilable(false);
1224 set_on_stack_flag(false);
1225 set_has_scalarized_args(false);
1226 set_has_scalarized_return(false);
1227 }
1228 #endif
1229
1230 // Called when the method_holder is getting linked. Setup entrypoints so the method
1231 // is ready to be called from interpreter, compiler, and vtables.
1232 void Method::link_method(const methodHandle& h_method, TRAPS) {
1233 if (log_is_enabled(Info, perf, class, link)) {
1234 ClassLoader::perf_ik_link_methods_count()->inc();
1235 }
1236
1237 // If the code cache is full, we may reenter this function for the
1238 // leftover methods that weren't linked.
1239 if (adapter() != nullptr) {
1240 if (adapter()->is_shared()) {
1241 assert(adapter()->is_linked(), "Adapter is shared but not linked");
1242 } else {
1243 return;
1244 }
1245 }
1246 assert( _code == nullptr, "nothing compiled yet" );
1247
1248 // Setup interpreter entrypoint
1249 assert(this == h_method(), "wrong h_method()" );
1250
1251 assert(adapter() == nullptr || adapter()->is_linked(), "init'd to null or restored from cache");
1252 address entry = Interpreter::entry_for_method(h_method);
1253 assert(entry != nullptr, "interpreter entry must be non-null");
1254 // Sets both _i2i_entry and _from_interpreted_entry
1255 set_interpreter_entry(entry);
1256
1257 // Don't overwrite already registered native entries.
1258 if (is_native() && !has_native_function()) {
1259 set_native_function(
1260 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1261 !native_bind_event_is_interesting);
1262 }
1263 if (InlineTypeReturnedAsFields && returns_inline_type(THREAD) && !has_scalarized_return()) {
1264 set_has_scalarized_return();
1265 }
1266
1267 // Setup compiler entrypoint. This is made eagerly, so we do not need
1268 // special handling of vtables. An alternative is to make adapters more
1269 // lazily by calling make_adapter() from from_compiled_entry() for the
1270 // normal calls. For vtable calls life gets more complicated. When a
1271 // call-site goes mega-morphic we need adapters in all methods which can be
1272 // called from the vtable. We need adapters on such methods that get loaded
1273 // later. Ditto for mega-morphic itable calls. If this proves to be a
1274 // problem we'll make these lazily later.
1275 if (_adapter == nullptr) {
1276 (void) make_adapters(h_method, CHECK);
1277 assert(adapter()->is_linked(), "Adapter must have been linked");
1278 }
1279
1280 // ONLY USE the h_method now as make_adapter may have blocked
1281
1282 if (h_method->is_continuation_native_intrinsic()) {
1283 _from_interpreted_entry = nullptr;
1284 _from_compiled_entry = nullptr;
1285 _i2i_entry = nullptr;
1297 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1298 PerfTraceTime timer(ClassLoader::perf_method_adapters_time());
1299
1300 // Adapters for compiled code are made eagerly here. They are fairly
1301 // small (generally < 100 bytes) and quick to make (and cached and shared)
1302 // so making them eagerly shouldn't be too expensive.
1303 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1304 if (adapter == nullptr ) {
1305 if (!is_init_completed()) {
1306 // Don't throw exceptions during VM initialization because java.lang.* classes
1307 // might not have been initialized, causing problems when constructing the
1308 // Java exception object.
1309 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1310 } else {
1311 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1312 }
1313 }
1314
1315 mh->set_adapter_entry(adapter);
1316 mh->_from_compiled_entry = adapter->get_c2i_entry();
1317 mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1318 mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1319 return adapter->get_c2i_entry();
1320 }
1321
1322 // The verified_code_entry() must be called when a invoke is resolved
1323 // on this method.
1324
1325 // It returns the compiled code entry point, after asserting not null.
1326 // This function is called after potential safepoints so that nmethod
1327 // or adapter that it points to is still live and valid.
1328 // This function must not hit a safepoint!
1329 address Method::verified_code_entry() {
1330 DEBUG_ONLY(NoSafepointVerifier nsv;)
1331 assert(_from_compiled_entry != nullptr, "must be set");
1332 return _from_compiled_entry;
1333 }
1334
1335 address Method::verified_inline_code_entry() {
1336 DEBUG_ONLY(NoSafepointVerifier nsv;)
1337 assert(_from_compiled_inline_entry != nullptr, "must be set");
1338 return _from_compiled_inline_entry;
1339 }
1340
1341 address Method::verified_inline_ro_code_entry() {
1342 DEBUG_ONLY(NoSafepointVerifier nsv;)
1343 assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1344 return _from_compiled_inline_ro_entry;
1345 }
1346
1347 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1348 // (could be racing a deopt).
1349 // Not inline to avoid circular ref.
1350 bool Method::check_code() const {
1351 // cached in a register or local. There's a race on the value of the field.
1352 nmethod *code = Atomic::load_acquire(&_code);
1353 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1354 }
1355
1356 // Install compiled code. Instantly it can execute.
1357 void Method::set_code(const methodHandle& mh, nmethod *code) {
1358 assert_lock_strong(NMethodState_lock);
1359 assert( code, "use clear_code to remove code" );
1360 assert( mh->check_code(), "" );
1361
1362 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1363
1364 // These writes must happen in this order, because the interpreter will
1365 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1366 // which jumps to _from_compiled_entry.
1367 mh->_code = code; // Assign before allowing compiled code to exec
1368
1369 int comp_level = code->comp_level();
1370 // In theory there could be a race here. In practice it is unlikely
1371 // and not worth worrying about.
1372 if (comp_level > mh->highest_comp_level()) {
1373 mh->set_highest_comp_level(comp_level);
1374 }
1375
1376 OrderAccess::storestore();
1377 mh->_from_compiled_entry = code->verified_entry_point();
1378 mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1379 mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1380 OrderAccess::storestore();
1381
1382 if (mh->is_continuation_native_intrinsic()) {
1383 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1384
1385 if (mh->is_continuation_enter_intrinsic()) {
1386 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1387 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1388 } else if (mh->is_continuation_yield_intrinsic()) {
1389 mh->_i2i_entry = mh->get_i2c_entry();
1390 } else {
1391 guarantee(false, "Unknown Continuation native intrinsic");
1392 }
1393 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1394 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1395 } else if (!mh->is_method_handle_intrinsic()) {
1396 // Instantly compiled code can execute.
1397 mh->_from_interpreted_entry = mh->get_i2c_entry();
1398 }
1399 }
1552 assert(m->can_be_statically_bound(), "");
1553 m->set_vtable_index(Method::nonvirtual_vtable_index);
1554 m->link_method(m, CHECK_(empty));
1555
1556 if (iid == vmIntrinsics::_linkToNative) {
1557 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1558 }
1559 if (log_is_enabled(Debug, methodhandles)) {
1560 LogTarget(Debug, methodhandles) lt;
1561 LogStream ls(lt);
1562 m->print_on(&ls);
1563 }
1564
1565 return m;
1566 }
1567
1568 #if INCLUDE_CDS
1569 void Method::restore_archived_method_handle_intrinsic(methodHandle m, TRAPS) {
1570 if (m->adapter() != nullptr) {
1571 m->set_from_compiled_entry(m->adapter()->get_c2i_entry());
1572 m->set_from_compiled_inline_entry(m->adapter()->get_c2i_inline_entry());
1573 m->set_from_compiled_inline_ro_entry(m->adapter()->get_c2i_inline_ro_entry());
1574 }
1575 m->link_method(m, CHECK);
1576
1577 if (m->intrinsic_id() == vmIntrinsics::_linkToNative) {
1578 m->set_interpreter_entry(m->adapter()->get_i2c_entry());
1579 }
1580 }
1581 #endif
1582
1583 Klass* Method::check_non_bcp_klass(Klass* klass) {
1584 if (klass != nullptr && klass->class_loader() != nullptr) {
1585 if (klass->is_objArray_klass())
1586 klass = ObjArrayKlass::cast(klass)->bottom_klass();
1587 return klass;
1588 }
1589 return nullptr;
1590 }
1591
1592
1593 methodHandle Method::clone_with_new_data(const methodHandle& m, u_char* new_code, int new_code_length,
2320 }
2321
2322 // Check that this pointer is valid by checking that the vtbl pointer matches
2323 bool Method::is_valid_method(const Method* m) {
2324 if (m == nullptr) {
2325 return false;
2326 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2327 // Quick sanity check on pointer.
2328 return false;
2329 } else if (!os::is_readable_range(m, m + 1)) {
2330 return false;
2331 } else if (m->is_shared()) {
2332 return CppVtables::is_valid_shared_method(m);
2333 } else if (Metaspace::contains_non_shared(m)) {
2334 return has_method_vptr((const void*)m);
2335 } else {
2336 return false;
2337 }
2338 }
2339
2340 bool Method::is_scalarized_arg(int idx) const {
2341 if (!has_scalarized_args()) {
2342 return false;
2343 }
2344 // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2345 int depth = 0;
2346 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2347 for (int i = 0; i < sig->length(); i++) {
2348 BasicType bt = sig->at(i)._bt;
2349 if (bt == T_METADATA) {
2350 depth++;
2351 }
2352 if (idx == 0) {
2353 break; // Argument found
2354 }
2355 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2356 depth--;
2357 }
2358 if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2359 idx--; // Advance to next argument
2360 }
2361 }
2362 return depth != 0;
2363 }
2364
2365 #ifndef PRODUCT
2366 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2367 out->print("%d", loader_data->jmethod_ids()->count_methods());
2368 }
2369 #endif // PRODUCT
2370
2371
2372 // Printing
2373
2374 #ifndef PRODUCT
2375
2376 void Method::print_on(outputStream* st) const {
2377 ResourceMark rm;
2378 assert(is_method(), "must be method");
2379 st->print_cr("%s", internal_name());
2380 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2381 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2382 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2383 constants()->print_value_on(st); st->cr();
2384 st->print (" - access: 0x%x ", access_flags().as_method_flags()); access_flags().print_on(st); st->cr();
2385 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2386 st->print (" - name: "); name()->print_value_on(st); st->cr();
2387 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2388 st->print_cr(" - max stack: %d", max_stack());
2389 st->print_cr(" - max locals: %d", max_locals());
2390 st->print_cr(" - size of params: %d", size_of_parameters());
2391 st->print_cr(" - method size: %d", method_size());
2392 if (intrinsic_id() != vmIntrinsics::_none)
2393 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2394 if (highest_comp_level() != CompLevel_none)
2395 st->print_cr(" - highest level: %d", highest_comp_level());
2396 st->print_cr(" - vtable index: %d", _vtable_index);
2397 #ifdef ASSERT
2398 if (valid_itable_index())
2399 st->print_cr(" - itable index: %d", itable_index());
2400 #endif
2401 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2402 st->print( " - adapters: ");
2403 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2404 if (a == nullptr)
2405 st->print_cr(PTR_FORMAT, p2i(a));
2406 else
2407 a->print_adapter_on(st);
2408 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2409 st->print_cr(" - compiled inline entry " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2410 st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2411 st->print_cr(" - code size: %d", code_size());
2412 if (code_size() != 0) {
2413 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2414 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2415 }
2416 if (method_data() != nullptr) {
2417 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2418 }
2419 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2420 if (checked_exceptions_length() > 0) {
2421 CheckedExceptionElement* table = checked_exceptions_start();
2422 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2423 if (Verbose) {
2424 for (int i = 0; i < checked_exceptions_length(); i++) {
2425 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2426 }
2427 }
2428 }
2429 if (has_linenumber_table()) {
2430 u_char* table = compressed_linenumber_table();
2460 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2461 }
2462 }
2463
2464 void Method::print_linkage_flags(outputStream* st) {
2465 access_flags().print_on(st);
2466 if (is_default_method()) {
2467 st->print("default ");
2468 }
2469 if (is_overpass()) {
2470 st->print("overpass ");
2471 }
2472 }
2473 #endif //PRODUCT
2474
2475 void Method::print_value_on(outputStream* st) const {
2476 assert(is_method(), "must be method");
2477 st->print("%s", internal_name());
2478 print_address_on(st);
2479 st->print(" ");
2480 if (WizardMode) access_flags().print_on(st);
2481 name()->print_value_on(st);
2482 st->print(" ");
2483 signature()->print_value_on(st);
2484 st->print(" in ");
2485 method_holder()->print_value_on(st);
2486 if (WizardMode) st->print("#%d", _vtable_index);
2487 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2488 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2489 }
2490
2491 // Verification
2492
2493 void Method::verify_on(outputStream* st) {
2494 guarantee(is_method(), "object must be method");
2495 guarantee(constants()->is_constantPool(), "should be constant pool");
2496 MethodData* md = method_data();
2497 guarantee(md == nullptr ||
2498 md->is_methodData(), "should be method data");
2499 }
|