42 #include "interpreter/oopMapCache.hpp"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "logging/logTag.hpp"
46 #include "memory/allocation.inline.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/metaspaceClosure.hpp"
49 #include "memory/oopFactory.hpp"
50 #include "memory/resourceArea.hpp"
51 #include "memory/universe.hpp"
52 #include "nmt/memTracker.hpp"
53 #include "oops/constMethod.hpp"
54 #include "oops/constantPool.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/method.inline.hpp"
57 #include "oops/methodData.hpp"
58 #include "oops/objArrayKlass.hpp"
59 #include "oops/objArrayOop.inline.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "oops/symbol.hpp"
62 #include "prims/jvmtiExport.hpp"
63 #include "prims/methodHandles.hpp"
64 #include "runtime/atomic.hpp"
65 #include "runtime/continuationEntry.hpp"
66 #include "runtime/frame.inline.hpp"
67 #include "runtime/handles.inline.hpp"
68 #include "runtime/init.hpp"
69 #include "runtime/java.hpp"
70 #include "runtime/orderAccess.hpp"
71 #include "runtime/relocator.hpp"
72 #include "runtime/safepointVerifiers.hpp"
73 #include "runtime/sharedRuntime.hpp"
74 #include "runtime/signature.hpp"
75 #include "runtime/threads.hpp"
76 #include "runtime/vm_version.hpp"
77 #include "utilities/align.hpp"
78 #include "utilities/quickSort.hpp"
79 #include "utilities/vmError.hpp"
80 #include "utilities/xmlstream.hpp"
81
100 }
101
102 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
103 NoSafepointVerifier no_safepoint;
104 set_constMethod(xconst);
105 set_access_flags(access_flags);
106 set_intrinsic_id(vmIntrinsics::_none);
107 clear_method_data();
108 clear_method_counters();
109 set_vtable_index(Method::garbage_vtable_index);
110
111 // Fix and bury in Method*
112 set_interpreter_entry(nullptr); // sets i2i entry and from_int
113 set_adapter_entry(nullptr);
114 Method::clear_code(); // from_c/from_i get set to c2i/i2i
115
116 if (access_flags.is_native()) {
117 clear_native_function();
118 set_signature_handler(nullptr);
119 }
120
121 NOT_PRODUCT(set_compiled_invocation_count(0);)
122 // Name is very useful for debugging.
123 NOT_PRODUCT(_name = name;)
124 }
125
126 // Release Method*. The nmethod will be gone when we get here because
127 // we've walked the code cache.
128 void Method::deallocate_contents(ClassLoaderData* loader_data) {
129 MetadataFactory::free_metadata(loader_data, constMethod());
130 set_constMethod(nullptr);
131 MetadataFactory::free_metadata(loader_data, method_data());
132 clear_method_data();
133 MetadataFactory::free_metadata(loader_data, method_counters());
134 clear_method_counters();
135 // The nmethod will be gone when we get here.
136 if (code() != nullptr) _code = nullptr;
137 }
138
139 void Method::release_C_heap_structures() {
140 if (method_data()) {
141 method_data()->release_C_heap_structures();
142
143 // Destroy MethodData embedded lock
144 method_data()->~MethodData();
145 }
146 }
147
148 address Method::get_i2c_entry() {
149 assert(adapter() != nullptr, "must have");
150 return adapter()->get_i2c_entry();
151 }
152
153 address Method::get_c2i_entry() {
154 assert(adapter() != nullptr, "must have");
155 return adapter()->get_c2i_entry();
156 }
157
158 address Method::get_c2i_unverified_entry() {
159 assert(adapter() != nullptr, "must have");
160 return adapter()->get_c2i_unverified_entry();
161 }
162
163 address Method::get_c2i_no_clinit_check_entry() {
164 assert(VM_Version::supports_fast_class_init_checks(), "");
165 assert(adapter() != nullptr, "must have");
166 return adapter()->get_c2i_no_clinit_check_entry();
167 }
168
169 char* Method::name_and_sig_as_C_string() const {
170 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
171 }
172
173 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
174 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
175 }
176
177 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
178 const char* klass_name = klass->external_name();
179 int klass_name_len = (int)strlen(klass_name);
180 int method_name_len = method_name->utf8_length();
181 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
182 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
370 return code_base();
371 } else {
372 return bcp;
373 }
374 }
375
376 int Method::size(bool is_native) {
377 // If native, then include pointers for native_function and signature_handler
378 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
379 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
380 return align_metadata_size(header_size() + extra_words);
381 }
382
383 Symbol* Method::klass_name() const {
384 return method_holder()->name();
385 }
386
387 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
388 log_trace(cds)("Iter(Method): %p", this);
389
390 if (!method_holder()->is_rewritten()) {
391 it->push(&_constMethod, MetaspaceClosure::_writable);
392 } else {
393 it->push(&_constMethod);
394 }
395 it->push(&_method_data);
396 it->push(&_method_counters);
397 NOT_PRODUCT(it->push(&_name);)
398 }
399
400 #if INCLUDE_CDS
401 // Attempt to return method to original state. Clear any pointers
402 // (to objects outside the shared spaces). We won't be able to predict
403 // where they should point in a new JVM. Further initialize some
404 // entries now in order allow them to be write protected later.
405
406 void Method::remove_unshareable_info() {
407 unlink_method();
408 JFR_ONLY(REMOVE_METHOD_ID(this);)
409 }
410
647 bool Method::init_method_counters(MethodCounters* counters) {
648 // Try to install a pointer to MethodCounters, return true on success.
649 return Atomic::replace_if_null(&_method_counters, counters);
650 }
651
652 void Method::set_exception_handler_entered(int handler_bci) {
653 if (ProfileExceptionHandlers) {
654 MethodData* mdo = method_data();
655 if (mdo != nullptr) {
656 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
657 handler_data.set_exception_handler_entered();
658 }
659 }
660 }
661
662 int Method::extra_stack_words() {
663 // not an inline function, to avoid a header dependency on Interpreter
664 return extra_stack_entries() * Interpreter::stackElementSize;
665 }
666
667 bool Method::is_vanilla_constructor() const {
668 // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
669 // which only calls the superclass vanilla constructor and possibly does stores of
670 // zero constants to local fields:
671 //
672 // aload_0
673 // invokespecial
674 // indexbyte1
675 // indexbyte2
676 //
677 // followed by an (optional) sequence of:
678 //
679 // aload_0
680 // aconst_null / iconst_0 / fconst_0 / dconst_0
681 // putfield
682 // indexbyte1
683 // indexbyte2
684 //
685 // followed by:
686 //
687 // return
688
689 assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors");
690 assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
691 int size = code_size();
692 // Check if size match
693 if (size == 0 || size % 5 != 0) return false;
694 address cb = code_base();
695 int last = size - 1;
696 if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
697 // Does not call superclass default constructor
698 return false;
699 }
700 // Check optional sequence
701 for (int i = 4; i < last; i += 5) {
702 if (cb[i] != Bytecodes::_aload_0) return false;
703 if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
704 if (cb[i+2] != Bytecodes::_putfield) return false;
705 }
706 return true;
707 }
708
709
710 bool Method::compute_has_loops_flag() {
711 BytecodeStream bcs(methodHandle(Thread::current(), this));
712 Bytecodes::Code bc;
713
714 while ((bc = bcs.next()) >= 0) {
715 switch (bc) {
716 case Bytecodes::_ifeq:
838
839 bool Method::is_accessor() const {
840 return is_getter() || is_setter();
841 }
842
843 bool Method::is_getter() const {
844 if (code_size() != 5) return false;
845 if (size_of_parameters() != 1) return false;
846 if (java_code_at(0) != Bytecodes::_aload_0) return false;
847 if (java_code_at(1) != Bytecodes::_getfield) return false;
848 switch (java_code_at(4)) {
849 case Bytecodes::_ireturn:
850 case Bytecodes::_lreturn:
851 case Bytecodes::_freturn:
852 case Bytecodes::_dreturn:
853 case Bytecodes::_areturn:
854 break;
855 default:
856 return false;
857 }
858 return true;
859 }
860
861 bool Method::is_setter() const {
862 if (code_size() != 6) return false;
863 if (java_code_at(0) != Bytecodes::_aload_0) return false;
864 switch (java_code_at(1)) {
865 case Bytecodes::_iload_1:
866 case Bytecodes::_aload_1:
867 case Bytecodes::_fload_1:
868 if (size_of_parameters() != 2) return false;
869 break;
870 case Bytecodes::_dload_1:
871 case Bytecodes::_lload_1:
872 if (size_of_parameters() != 3) return false;
873 break;
874 default:
875 return false;
876 }
877 if (java_code_at(2) != Bytecodes::_putfield) return false;
878 if (java_code_at(5) != Bytecodes::_return) return false;
879 return true;
880 }
881
882 bool Method::is_constant_getter() const {
883 int last_index = code_size() - 1;
884 // Check if the first 1-3 bytecodes are a constant push
885 // and the last bytecode is a return.
886 return (2 <= code_size() && code_size() <= 4 &&
887 Bytecodes::is_const(java_code_at(0)) &&
888 Bytecodes::length_for(java_code_at(0)) == last_index &&
889 Bytecodes::is_return(java_code_at(last_index)));
890 }
891
892 bool Method::is_initializer() const {
893 return is_object_initializer() || is_static_initializer();
894 }
895
896 bool Method::has_valid_initializer_flags() const {
897 return (is_static() ||
898 method_holder()->major_version() < 51);
899 }
900
901 bool Method::is_static_initializer() const {
902 // For classfiles version 51 or greater, ensure that the clinit method is
903 // static. Non-static methods with the name "<clinit>" are not static
904 // initializers. (older classfiles exempted for backward compatibility)
905 return name() == vmSymbols::class_initializer_name() &&
906 has_valid_initializer_flags();
907 }
908
909 bool Method::is_object_initializer() 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 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
918 int length = method->checked_exceptions_length();
919 if (length == 0) { // common case
920 return objArrayHandle(THREAD, Universe::the_empty_class_array());
921 } else {
922 methodHandle h_this(THREAD, method);
923 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
924 objArrayHandle mirrors (THREAD, m_oop);
925 for (int i = 0; i < length; i++) {
926 CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
927 Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
928 if (log_is_enabled(Warning, exceptions) &&
929 !k->is_subclass_of(vmClasses::Throwable_klass())) {
930 ResourceMark rm(THREAD);
948 // Not necessarily sorted and not necessarily one-to-one.
949 CompressedLineNumberReadStream stream(compressed_linenumber_table());
950 while (stream.read_pair()) {
951 if (stream.bci() == bci) {
952 // perfect match
953 return stream.line();
954 } else {
955 // update best_bci/line
956 if (stream.bci() < bci && stream.bci() >= best_bci) {
957 best_bci = stream.bci();
958 best_line = stream.line();
959 }
960 }
961 }
962 }
963 return best_line;
964 }
965
966
967 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
968 if( constants()->tag_at(klass_index).is_unresolved_klass() ) {
969 Thread *thread = Thread::current();
970 Symbol* klass_name = constants()->klass_name_at(klass_index);
971 Handle loader(thread, method_holder()->class_loader());
972 Handle prot (thread, method_holder()->protection_domain());
973 return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != nullptr;
974 } else {
975 return true;
976 }
977 }
978
979
980 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
981 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
982 if (must_be_resolved) {
983 // Make sure klass is resolved in constantpool.
984 if (constants()->tag_at(klass_index).is_unresolved_klass()) return false;
985 }
986 return is_klass_loaded_by_klass_index(klass_index);
987 }
988
989
990 void Method::set_native_function(address function, bool post_event_flag) {
991 assert(function != nullptr, "use clear_native_function to unregister natives");
992 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
993 address* native_function = native_function_addr();
994
995 // We can see racers trying to place the same native function into place. Once
996 // is plenty.
997 address current = *native_function;
998 if (current == function) return;
999 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1000 function != nullptr) {
1001 // native_method_throw_unsatisfied_link_error_entry() should only
1002 // be passed when post_event_flag is false.
1003 assert(function !=
1004 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1133 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1134 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1135 if (comp_level == CompLevel_all) {
1136 set_is_not_c1_osr_compilable();
1137 set_is_not_c2_osr_compilable();
1138 } else {
1139 if (is_c1_compile(comp_level))
1140 set_is_not_c1_osr_compilable();
1141 if (is_c2_compile(comp_level))
1142 set_is_not_c2_osr_compilable();
1143 }
1144 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1145 }
1146
1147 // Revert to using the interpreter and clear out the nmethod
1148 void Method::clear_code() {
1149 // this may be null if c2i adapters have not been made yet
1150 // Only should happen at allocate time.
1151 if (adapter() == nullptr) {
1152 _from_compiled_entry = nullptr;
1153 } else {
1154 _from_compiled_entry = adapter()->get_c2i_entry();
1155 }
1156 OrderAccess::storestore();
1157 _from_interpreted_entry = _i2i_entry;
1158 OrderAccess::storestore();
1159 _code = nullptr;
1160 }
1161
1162 void Method::unlink_code(nmethod *compare) {
1163 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1164 // We need to check if either the _code or _from_compiled_code_entry_point
1165 // refer to this nmethod because there is a race in setting these two fields
1166 // in Method* as seen in bugid 4947125.
1167 if (code() == compare ||
1168 from_compiled_entry() == compare->verified_entry_point()) {
1169 clear_code();
1170 }
1171 }
1172
1173 void Method::unlink_code() {
1174 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1175 clear_code();
1176 }
1177
1178 #if INCLUDE_CDS
1179 // Called by class data sharing to remove any entry points (which are not shared)
1180 void Method::unlink_method() {
1181 assert(CDSConfig::is_dumping_archive(), "sanity");
1182 _code = nullptr;
1183 _adapter = nullptr;
1184 _i2i_entry = nullptr;
1185 _from_compiled_entry = nullptr;
1186 _from_interpreted_entry = nullptr;
1187
1188 if (is_native()) {
1189 *native_function_addr() = nullptr;
1190 set_signature_handler(nullptr);
1191 }
1192 NOT_PRODUCT(set_compiled_invocation_count(0);)
1193
1194 clear_method_data();
1195 clear_method_counters();
1196 remove_unshareable_flags();
1197 }
1198
1199 void Method::remove_unshareable_flags() {
1200 // clear all the flags that shouldn't be in the archived version
1201 assert(!is_old(), "must be");
1202 assert(!is_obsolete(), "must be");
1203 assert(!is_deleted(), "must be");
1204
1205 set_is_prefixed_native(false);
1206 set_queued_for_compilation(false);
1207 set_is_not_c2_compilable(false);
1208 set_is_not_c1_compilable(false);
1209 set_is_not_c2_osr_compilable(false);
1210 set_on_stack_flag(false);
1211 }
1212 #endif
1213
1214 // Called when the method_holder is getting linked. Setup entrypoints so the method
1215 // is ready to be called from interpreter, compiler, and vtables.
1216 void Method::link_method(const methodHandle& h_method, TRAPS) {
1217 // If the code cache is full, we may reenter this function for the
1218 // leftover methods that weren't linked.
1219 if (adapter() != nullptr) {
1220 return;
1221 }
1222 assert( _code == nullptr, "nothing compiled yet" );
1223
1224 // Setup interpreter entrypoint
1225 assert(this == h_method(), "wrong h_method()" );
1226
1227 assert(adapter() == nullptr, "init'd to null");
1228 address entry = Interpreter::entry_for_method(h_method);
1229 assert(entry != nullptr, "interpreter entry must be non-null");
1230 // Sets both _i2i_entry and _from_interpreted_entry
1231 set_interpreter_entry(entry);
1232
1233 // Don't overwrite already registered native entries.
1234 if (is_native() && !has_native_function()) {
1235 set_native_function(
1236 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1237 !native_bind_event_is_interesting);
1238 }
1239
1240 // Setup compiler entrypoint. This is made eagerly, so we do not need
1241 // special handling of vtables. An alternative is to make adapters more
1242 // lazily by calling make_adapter() from from_compiled_entry() for the
1243 // normal calls. For vtable calls life gets more complicated. When a
1244 // call-site goes mega-morphic we need adapters in all methods which can be
1245 // called from the vtable. We need adapters on such methods that get loaded
1246 // later. Ditto for mega-morphic itable calls. If this proves to be a
1247 // problem we'll make these lazily later.
1248 (void) make_adapters(h_method, CHECK);
1249
1250 // ONLY USE the h_method now as make_adapter may have blocked
1251
1252 if (h_method->is_continuation_native_intrinsic()) {
1253 _from_interpreted_entry = nullptr;
1254 _from_compiled_entry = nullptr;
1255 _i2i_entry = nullptr;
1256 if (Continuations::enabled()) {
1257 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1258 AdapterHandlerLibrary::create_native_wrapper(h_method);
1265 }
1266
1267 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1268 // Adapters for compiled code are made eagerly here. They are fairly
1269 // small (generally < 100 bytes) and quick to make (and cached and shared)
1270 // so making them eagerly shouldn't be too expensive.
1271 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1272 if (adapter == nullptr ) {
1273 if (!is_init_completed()) {
1274 // Don't throw exceptions during VM initialization because java.lang.* classes
1275 // might not have been initialized, causing problems when constructing the
1276 // Java exception object.
1277 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1278 } else {
1279 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1280 }
1281 }
1282
1283 mh->set_adapter_entry(adapter);
1284 mh->_from_compiled_entry = adapter->get_c2i_entry();
1285 return adapter->get_c2i_entry();
1286 }
1287
1288 // The verified_code_entry() must be called when a invoke is resolved
1289 // on this method.
1290
1291 // It returns the compiled code entry point, after asserting not null.
1292 // This function is called after potential safepoints so that nmethod
1293 // or adapter that it points to is still live and valid.
1294 // This function must not hit a safepoint!
1295 address Method::verified_code_entry() {
1296 debug_only(NoSafepointVerifier nsv;)
1297 assert(_from_compiled_entry != nullptr, "must be set");
1298 return _from_compiled_entry;
1299 }
1300
1301 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1302 // (could be racing a deopt).
1303 // Not inline to avoid circular ref.
1304 bool Method::check_code() const {
1305 // cached in a register or local. There's a race on the value of the field.
1306 nmethod *code = Atomic::load_acquire(&_code);
1307 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1308 }
1309
1310 // Install compiled code. Instantly it can execute.
1311 void Method::set_code(const methodHandle& mh, nmethod *code) {
1312 assert_lock_strong(NMethodState_lock);
1313 assert( code, "use clear_code to remove code" );
1314 assert( mh->check_code(), "" );
1315
1316 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1317
1318 // These writes must happen in this order, because the interpreter will
1319 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1320 // which jumps to _from_compiled_entry.
1321 mh->_code = code; // Assign before allowing compiled code to exec
1322
1323 int comp_level = code->comp_level();
1324 // In theory there could be a race here. In practice it is unlikely
1325 // and not worth worrying about.
1326 if (comp_level > mh->highest_comp_level()) {
1327 mh->set_highest_comp_level(comp_level);
1328 }
1329
1330 OrderAccess::storestore();
1331 mh->_from_compiled_entry = code->verified_entry_point();
1332 OrderAccess::storestore();
1333
1334 if (mh->is_continuation_native_intrinsic()) {
1335 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1336
1337 if (mh->is_continuation_enter_intrinsic()) {
1338 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1339 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1340 } else if (mh->is_continuation_yield_intrinsic()) {
1341 mh->_i2i_entry = mh->get_i2c_entry();
1342 } else {
1343 guarantee(false, "Unknown Continuation native intrinsic");
1344 }
1345 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1346 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1347 } else if (!mh->is_method_handle_intrinsic()) {
1348 // Instantly compiled code can execute.
1349 mh->_from_interpreted_entry = mh->get_i2c_entry();
1350 }
1351 }
2289 }
2290
2291 // Check that this pointer is valid by checking that the vtbl pointer matches
2292 bool Method::is_valid_method(const Method* m) {
2293 if (m == nullptr) {
2294 return false;
2295 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2296 // Quick sanity check on pointer.
2297 return false;
2298 } else if (!os::is_readable_range(m, m + 1)) {
2299 return false;
2300 } else if (m->is_shared()) {
2301 return CppVtables::is_valid_shared_method(m);
2302 } else if (Metaspace::contains_non_shared(m)) {
2303 return has_method_vptr((const void*)m);
2304 } else {
2305 return false;
2306 }
2307 }
2308
2309 #ifndef PRODUCT
2310 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2311 out->print("%d", loader_data->jmethod_ids()->count_methods());
2312 }
2313 #endif // PRODUCT
2314
2315
2316 // Printing
2317
2318 #ifndef PRODUCT
2319
2320 void Method::print_on(outputStream* st) const {
2321 ResourceMark rm;
2322 assert(is_method(), "must be method");
2323 st->print_cr("%s", internal_name());
2324 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2325 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2326 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2327 constants()->print_value_on(st); st->cr();
2328 st->print (" - access: 0x%x ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2329 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2330 st->print (" - name: "); name()->print_value_on(st); st->cr();
2331 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2332 st->print_cr(" - max stack: %d", max_stack());
2333 st->print_cr(" - max locals: %d", max_locals());
2334 st->print_cr(" - size of params: %d", size_of_parameters());
2335 st->print_cr(" - method size: %d", method_size());
2336 if (intrinsic_id() != vmIntrinsics::_none)
2337 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2338 if (highest_comp_level() != CompLevel_none)
2339 st->print_cr(" - highest level: %d", highest_comp_level());
2340 st->print_cr(" - vtable index: %d", _vtable_index);
2341 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2342 st->print( " - adapters: ");
2343 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2344 if (a == nullptr)
2345 st->print_cr(PTR_FORMAT, p2i(a));
2346 else
2347 a->print_adapter_on(st);
2348 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2349 st->print_cr(" - code size: %d", code_size());
2350 if (code_size() != 0) {
2351 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2352 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2353 }
2354 if (method_data() != nullptr) {
2355 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2356 }
2357 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2358 if (checked_exceptions_length() > 0) {
2359 CheckedExceptionElement* table = checked_exceptions_start();
2360 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2361 if (Verbose) {
2362 for (int i = 0; i < checked_exceptions_length(); i++) {
2363 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2364 }
2365 }
2366 }
2367 if (has_linenumber_table()) {
2368 u_char* table = compressed_linenumber_table();
2398 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2399 }
2400 }
2401
2402 void Method::print_linkage_flags(outputStream* st) {
2403 access_flags().print_on(st);
2404 if (is_default_method()) {
2405 st->print("default ");
2406 }
2407 if (is_overpass()) {
2408 st->print("overpass ");
2409 }
2410 }
2411 #endif //PRODUCT
2412
2413 void Method::print_value_on(outputStream* st) const {
2414 assert(is_method(), "must be method");
2415 st->print("%s", internal_name());
2416 print_address_on(st);
2417 st->print(" ");
2418 name()->print_value_on(st);
2419 st->print(" ");
2420 signature()->print_value_on(st);
2421 st->print(" in ");
2422 method_holder()->print_value_on(st);
2423 if (WizardMode) st->print("#%d", _vtable_index);
2424 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2425 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2426 }
2427
2428 // Verification
2429
2430 void Method::verify_on(outputStream* st) {
2431 guarantee(is_method(), "object must be method");
2432 guarantee(constants()->is_constantPool(), "should be constant pool");
2433 MethodData* md = method_data();
2434 guarantee(md == nullptr ||
2435 md->is_methodData(), "should be method data");
2436 }
|
42 #include "interpreter/oopMapCache.hpp"
43 #include "logging/log.hpp"
44 #include "logging/logStream.hpp"
45 #include "logging/logTag.hpp"
46 #include "memory/allocation.inline.hpp"
47 #include "memory/metadataFactory.hpp"
48 #include "memory/metaspaceClosure.hpp"
49 #include "memory/oopFactory.hpp"
50 #include "memory/resourceArea.hpp"
51 #include "memory/universe.hpp"
52 #include "nmt/memTracker.hpp"
53 #include "oops/constMethod.hpp"
54 #include "oops/constantPool.hpp"
55 #include "oops/klass.inline.hpp"
56 #include "oops/method.inline.hpp"
57 #include "oops/methodData.hpp"
58 #include "oops/objArrayKlass.hpp"
59 #include "oops/objArrayOop.inline.hpp"
60 #include "oops/oop.inline.hpp"
61 #include "oops/symbol.hpp"
62 #include "oops/inlineKlass.inline.hpp"
63 #include "prims/jvmtiExport.hpp"
64 #include "prims/methodHandles.hpp"
65 #include "runtime/atomic.hpp"
66 #include "runtime/continuationEntry.hpp"
67 #include "runtime/frame.inline.hpp"
68 #include "runtime/handles.inline.hpp"
69 #include "runtime/init.hpp"
70 #include "runtime/java.hpp"
71 #include "runtime/orderAccess.hpp"
72 #include "runtime/relocator.hpp"
73 #include "runtime/safepointVerifiers.hpp"
74 #include "runtime/sharedRuntime.hpp"
75 #include "runtime/signature.hpp"
76 #include "runtime/threads.hpp"
77 #include "runtime/vm_version.hpp"
78 #include "utilities/align.hpp"
79 #include "utilities/quickSort.hpp"
80 #include "utilities/vmError.hpp"
81 #include "utilities/xmlstream.hpp"
82
101 }
102
103 Method::Method(ConstMethod* xconst, AccessFlags access_flags, Symbol* name) {
104 NoSafepointVerifier no_safepoint;
105 set_constMethod(xconst);
106 set_access_flags(access_flags);
107 set_intrinsic_id(vmIntrinsics::_none);
108 clear_method_data();
109 clear_method_counters();
110 set_vtable_index(Method::garbage_vtable_index);
111
112 // Fix and bury in Method*
113 set_interpreter_entry(nullptr); // sets i2i entry and from_int
114 set_adapter_entry(nullptr);
115 Method::clear_code(); // from_c/from_i get set to c2i/i2i
116
117 if (access_flags.is_native()) {
118 clear_native_function();
119 set_signature_handler(nullptr);
120 }
121 NOT_PRODUCT(set_compiled_invocation_count(0);)
122 // Name is very useful for debugging.
123 NOT_PRODUCT(_name = name;)
124 }
125
126 // Release Method*. The nmethod will be gone when we get here because
127 // we've walked the code cache.
128 void Method::deallocate_contents(ClassLoaderData* loader_data) {
129 MetadataFactory::free_metadata(loader_data, constMethod());
130 set_constMethod(nullptr);
131 MetadataFactory::free_metadata(loader_data, method_data());
132 clear_method_data();
133 MetadataFactory::free_metadata(loader_data, method_counters());
134 clear_method_counters();
135 // The nmethod will be gone when we get here.
136 if (code() != nullptr) _code = nullptr;
137 }
138
139 void Method::release_C_heap_structures() {
140 if (method_data()) {
141 method_data()->release_C_heap_structures();
142
143 // Destroy MethodData embedded lock
144 method_data()->~MethodData();
145 }
146 }
147
148 address Method::get_i2c_entry() {
149 assert(adapter() != nullptr, "must have");
150 return adapter()->get_i2c_entry();
151 }
152
153 address Method::get_c2i_entry() {
154 assert(adapter() != nullptr, "must have");
155 return adapter()->get_c2i_entry();
156 }
157
158 address Method::get_c2i_inline_entry() {
159 assert(adapter() != nullptr, "must have");
160 return adapter()->get_c2i_inline_entry();
161 }
162
163 address Method::get_c2i_unverified_entry() {
164 assert(adapter() != nullptr, "must have");
165 return adapter()->get_c2i_unverified_entry();
166 }
167
168 address Method::get_c2i_unverified_inline_entry() {
169 assert(adapter() != nullptr, "must have");
170 return adapter()->get_c2i_unverified_inline_entry();
171 }
172
173 address Method::get_c2i_no_clinit_check_entry() {
174 assert(VM_Version::supports_fast_class_init_checks(), "");
175 assert(adapter() != nullptr, "must have");
176 return adapter()->get_c2i_no_clinit_check_entry();
177 }
178
179 char* Method::name_and_sig_as_C_string() const {
180 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
181 }
182
183 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
184 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
185 }
186
187 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
188 const char* klass_name = klass->external_name();
189 int klass_name_len = (int)strlen(klass_name);
190 int method_name_len = method_name->utf8_length();
191 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
192 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
380 return code_base();
381 } else {
382 return bcp;
383 }
384 }
385
386 int Method::size(bool is_native) {
387 // If native, then include pointers for native_function and signature_handler
388 int extra_bytes = (is_native) ? 2*sizeof(address*) : 0;
389 int extra_words = align_up(extra_bytes, BytesPerWord) / BytesPerWord;
390 return align_metadata_size(header_size() + extra_words);
391 }
392
393 Symbol* Method::klass_name() const {
394 return method_holder()->name();
395 }
396
397 void Method::metaspace_pointers_do(MetaspaceClosure* it) {
398 log_trace(cds)("Iter(Method): %p", this);
399
400 if (!method_holder()->is_rewritten() || CDSConfig::is_valhalla_preview()) {
401 it->push(&_constMethod, MetaspaceClosure::_writable);
402 } else {
403 it->push(&_constMethod);
404 }
405 it->push(&_method_data);
406 it->push(&_method_counters);
407 NOT_PRODUCT(it->push(&_name);)
408 }
409
410 #if INCLUDE_CDS
411 // Attempt to return method to original state. Clear any pointers
412 // (to objects outside the shared spaces). We won't be able to predict
413 // where they should point in a new JVM. Further initialize some
414 // entries now in order allow them to be write protected later.
415
416 void Method::remove_unshareable_info() {
417 unlink_method();
418 JFR_ONLY(REMOVE_METHOD_ID(this);)
419 }
420
657 bool Method::init_method_counters(MethodCounters* counters) {
658 // Try to install a pointer to MethodCounters, return true on success.
659 return Atomic::replace_if_null(&_method_counters, counters);
660 }
661
662 void Method::set_exception_handler_entered(int handler_bci) {
663 if (ProfileExceptionHandlers) {
664 MethodData* mdo = method_data();
665 if (mdo != nullptr) {
666 BitData handler_data = mdo->exception_handler_bci_to_data(handler_bci);
667 handler_data.set_exception_handler_entered();
668 }
669 }
670 }
671
672 int Method::extra_stack_words() {
673 // not an inline function, to avoid a header dependency on Interpreter
674 return extra_stack_entries() * Interpreter::stackElementSize;
675 }
676
677 // InlineKlass the method is declared to return. This must not
678 // safepoint as it is called with references live on the stack at
679 // locations the GC is unaware of.
680 InlineKlass* Method::returns_inline_type(Thread* thread) const {
681 assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
682 if (is_native()) {
683 return nullptr;
684 }
685 NoSafepointVerifier nsv;
686 SignatureStream ss(signature());
687 while (!ss.at_return_type()) {
688 ss.next();
689 }
690 return ss.as_inline_klass(method_holder());
691 }
692
693 bool Method::is_vanilla_constructor() const {
694 // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
695 // which only calls the superclass vanilla constructor and possibly does stores of
696 // zero constants to local fields:
697 //
698 // aload_0, _fast_aload_0, or _nofast_aload_0
699 // invokespecial
700 // indexbyte1
701 // indexbyte2
702 //
703 // followed by an (optional) sequence of:
704 //
705 // aload_0
706 // aconst_null / iconst_0 / fconst_0 / dconst_0
707 // putfield
708 // indexbyte1
709 // indexbyte2
710 //
711 // followed by:
712 //
713 // return
714
715 assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors");
716 assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
717 int size = code_size();
718 // Check if size match
719 if (size == 0 || size % 5 != 0) return false;
720 address cb = code_base();
721 int last = size - 1;
722 if ((cb[0] != Bytecodes::_aload_0 && cb[0] != Bytecodes::_fast_aload_0 && cb[0] != Bytecodes::_nofast_aload_0) ||
723 cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
724 // Does not call superclass default constructor
725 return false;
726 }
727 // Check optional sequence
728 for (int i = 4; i < last; i += 5) {
729 if (cb[i] != Bytecodes::_aload_0) return false;
730 if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
731 if (cb[i+2] != Bytecodes::_putfield) return false;
732 }
733 return true;
734 }
735
736
737 bool Method::compute_has_loops_flag() {
738 BytecodeStream bcs(methodHandle(Thread::current(), this));
739 Bytecodes::Code bc;
740
741 while ((bc = bcs.next()) >= 0) {
742 switch (bc) {
743 case Bytecodes::_ifeq:
865
866 bool Method::is_accessor() const {
867 return is_getter() || is_setter();
868 }
869
870 bool Method::is_getter() const {
871 if (code_size() != 5) return false;
872 if (size_of_parameters() != 1) return false;
873 if (java_code_at(0) != Bytecodes::_aload_0) return false;
874 if (java_code_at(1) != Bytecodes::_getfield) return false;
875 switch (java_code_at(4)) {
876 case Bytecodes::_ireturn:
877 case Bytecodes::_lreturn:
878 case Bytecodes::_freturn:
879 case Bytecodes::_dreturn:
880 case Bytecodes::_areturn:
881 break;
882 default:
883 return false;
884 }
885 if (has_scalarized_return()) {
886 // Don't treat this as (trivial) getter method because the
887 // inline type should be returned in a scalarized form.
888 return false;
889 }
890 return true;
891 }
892
893 bool Method::is_setter() const {
894 if (code_size() != 6) return false;
895 if (java_code_at(0) != Bytecodes::_aload_0) return false;
896 switch (java_code_at(1)) {
897 case Bytecodes::_iload_1:
898 case Bytecodes::_aload_1:
899 case Bytecodes::_fload_1:
900 if (size_of_parameters() != 2) return false;
901 break;
902 case Bytecodes::_dload_1:
903 case Bytecodes::_lload_1:
904 if (size_of_parameters() != 3) return false;
905 break;
906 default:
907 return false;
908 }
909 if (java_code_at(2) != Bytecodes::_putfield) return false;
910 if (java_code_at(5) != Bytecodes::_return) return false;
911 if (has_scalarized_args()) {
912 // Don't treat this as (trivial) setter method because the
913 // inline type argument should be passed in a scalarized form.
914 return false;
915 }
916 return true;
917 }
918
919 bool Method::is_constant_getter() const {
920 int last_index = code_size() - 1;
921 // Check if the first 1-3 bytecodes are a constant push
922 // and the last bytecode is a return.
923 return (2 <= code_size() && code_size() <= 4 &&
924 Bytecodes::is_const(java_code_at(0)) &&
925 Bytecodes::length_for(java_code_at(0)) == last_index &&
926 Bytecodes::is_return(java_code_at(last_index)) &&
927 !has_scalarized_args());
928 }
929
930 bool Method::is_class_initializer() const {
931 // For classfiles version 51 or greater, ensure that the clinit method is
932 // static. Non-static methods with the name "<clinit>" are not static
933 // initializers. (older classfiles exempted for backward compatibility)
934 return (name() == vmSymbols::class_initializer_name() &&
935 (is_static() ||
936 method_holder()->major_version() < 51));
937 }
938
939 // A method named <init>, is a classic object constructor.
940 bool Method::is_object_constructor() const {
941 return name() == vmSymbols::object_initializer_name();
942 }
943
944 bool Method::needs_clinit_barrier() const {
945 return is_static() && !method_holder()->is_initialized();
946 }
947
948 objArrayHandle Method::resolved_checked_exceptions_impl(Method* method, TRAPS) {
949 int length = method->checked_exceptions_length();
950 if (length == 0) { // common case
951 return objArrayHandle(THREAD, Universe::the_empty_class_array());
952 } else {
953 methodHandle h_this(THREAD, method);
954 objArrayOop m_oop = oopFactory::new_objArray(vmClasses::Class_klass(), length, CHECK_(objArrayHandle()));
955 objArrayHandle mirrors (THREAD, m_oop);
956 for (int i = 0; i < length; i++) {
957 CheckedExceptionElement* table = h_this->checked_exceptions_start(); // recompute on each iteration, not gc safe
958 Klass* k = h_this->constants()->klass_at(table[i].class_cp_index, CHECK_(objArrayHandle()));
959 if (log_is_enabled(Warning, exceptions) &&
960 !k->is_subclass_of(vmClasses::Throwable_klass())) {
961 ResourceMark rm(THREAD);
979 // Not necessarily sorted and not necessarily one-to-one.
980 CompressedLineNumberReadStream stream(compressed_linenumber_table());
981 while (stream.read_pair()) {
982 if (stream.bci() == bci) {
983 // perfect match
984 return stream.line();
985 } else {
986 // update best_bci/line
987 if (stream.bci() < bci && stream.bci() >= best_bci) {
988 best_bci = stream.bci();
989 best_line = stream.line();
990 }
991 }
992 }
993 }
994 return best_line;
995 }
996
997
998 bool Method::is_klass_loaded_by_klass_index(int klass_index) const {
999 if( constants()->tag_at(klass_index).is_unresolved_klass()) {
1000 Thread *thread = Thread::current();
1001 Symbol* klass_name = constants()->klass_name_at(klass_index);
1002 Handle loader(thread, method_holder()->class_loader());
1003 Handle prot (thread, method_holder()->protection_domain());
1004 return SystemDictionary::find_instance_klass(thread, klass_name, loader, prot) != nullptr;
1005 } else {
1006 return true;
1007 }
1008 }
1009
1010
1011 bool Method::is_klass_loaded(int refinfo_index, Bytecodes::Code bc, bool must_be_resolved) const {
1012 int klass_index = constants()->klass_ref_index_at(refinfo_index, bc);
1013 if (must_be_resolved) {
1014 // Make sure klass is resolved in constantpool.
1015 if (constants()->tag_at(klass_index).is_unresolved_klass()) {
1016 return false;
1017 }
1018 }
1019 return is_klass_loaded_by_klass_index(klass_index);
1020 }
1021
1022
1023 void Method::set_native_function(address function, bool post_event_flag) {
1024 assert(function != nullptr, "use clear_native_function to unregister natives");
1025 assert(!is_special_native_intrinsic() || function == SharedRuntime::native_method_throw_unsatisfied_link_error_entry(), "");
1026 address* native_function = native_function_addr();
1027
1028 // We can see racers trying to place the same native function into place. Once
1029 // is plenty.
1030 address current = *native_function;
1031 if (current == function) return;
1032 if (post_event_flag && JvmtiExport::should_post_native_method_bind() &&
1033 function != nullptr) {
1034 // native_method_throw_unsatisfied_link_error_entry() should only
1035 // be passed when post_event_flag is false.
1036 assert(function !=
1037 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1166 void Method::set_not_osr_compilable(const char* reason, int comp_level, bool report) {
1167 print_made_not_compilable(comp_level, /*is_osr*/ true, report, reason);
1168 if (comp_level == CompLevel_all) {
1169 set_is_not_c1_osr_compilable();
1170 set_is_not_c2_osr_compilable();
1171 } else {
1172 if (is_c1_compile(comp_level))
1173 set_is_not_c1_osr_compilable();
1174 if (is_c2_compile(comp_level))
1175 set_is_not_c2_osr_compilable();
1176 }
1177 assert(!CompilationPolicy::can_be_osr_compiled(methodHandle(Thread::current(), this), comp_level), "sanity check");
1178 }
1179
1180 // Revert to using the interpreter and clear out the nmethod
1181 void Method::clear_code() {
1182 // this may be null if c2i adapters have not been made yet
1183 // Only should happen at allocate time.
1184 if (adapter() == nullptr) {
1185 _from_compiled_entry = nullptr;
1186 _from_compiled_inline_entry = nullptr;
1187 _from_compiled_inline_ro_entry = nullptr;
1188 } else {
1189 _from_compiled_entry = adapter()->get_c2i_entry();
1190 _from_compiled_inline_entry = adapter()->get_c2i_inline_entry();
1191 _from_compiled_inline_ro_entry = adapter()->get_c2i_inline_ro_entry();
1192 }
1193 OrderAccess::storestore();
1194 _from_interpreted_entry = _i2i_entry;
1195 OrderAccess::storestore();
1196 _code = nullptr;
1197 }
1198
1199 void Method::unlink_code(nmethod *compare) {
1200 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1201 // We need to check if either the _code or _from_compiled_code_entry_point
1202 // refer to this nmethod because there is a race in setting these two fields
1203 // in Method* as seen in bugid 4947125.
1204 if (code() == compare ||
1205 from_compiled_entry() == compare->verified_entry_point()) {
1206 clear_code();
1207 }
1208 }
1209
1210 void Method::unlink_code() {
1211 ConditionalMutexLocker ml(NMethodState_lock, !NMethodState_lock->owned_by_self(), Mutex::_no_safepoint_check_flag);
1212 clear_code();
1213 }
1214
1215 #if INCLUDE_CDS
1216 // Called by class data sharing to remove any entry points (which are not shared)
1217 void Method::unlink_method() {
1218 assert(CDSConfig::is_dumping_archive(), "sanity");
1219 _code = nullptr;
1220 _adapter = nullptr;
1221 _i2i_entry = nullptr;
1222 _from_compiled_entry = nullptr;
1223 _from_compiled_inline_entry = nullptr;
1224 _from_compiled_inline_ro_entry = nullptr;
1225 _from_interpreted_entry = nullptr;
1226
1227 if (is_native()) {
1228 *native_function_addr() = nullptr;
1229 set_signature_handler(nullptr);
1230 }
1231 NOT_PRODUCT(set_compiled_invocation_count(0);)
1232
1233 clear_method_data();
1234 clear_method_counters();
1235 remove_unshareable_flags();
1236 }
1237
1238 void Method::remove_unshareable_flags() {
1239 // clear all the flags that shouldn't be in the archived version
1240 assert(!is_old(), "must be");
1241 assert(!is_obsolete(), "must be");
1242 assert(!is_deleted(), "must be");
1243
1244 set_is_prefixed_native(false);
1245 set_queued_for_compilation(false);
1246 set_is_not_c2_compilable(false);
1247 set_is_not_c1_compilable(false);
1248 set_is_not_c2_osr_compilable(false);
1249 set_on_stack_flag(false);
1250 set_has_scalarized_args(false);
1251 set_has_scalarized_return(false);
1252 }
1253 #endif
1254
1255 // Called when the method_holder is getting linked. Setup entrypoints so the method
1256 // is ready to be called from interpreter, compiler, and vtables.
1257 void Method::link_method(const methodHandle& h_method, TRAPS) {
1258 // If the code cache is full, we may reenter this function for the
1259 // leftover methods that weren't linked.
1260 if (adapter() != nullptr) {
1261 return;
1262 }
1263 assert( _code == nullptr, "nothing compiled yet" );
1264
1265 // Setup interpreter entrypoint
1266 assert(this == h_method(), "wrong h_method()" );
1267
1268 assert(adapter() == nullptr, "init'd to null");
1269 address entry = Interpreter::entry_for_method(h_method);
1270 assert(entry != nullptr, "interpreter entry must be non-null");
1271 // Sets both _i2i_entry and _from_interpreted_entry
1272 set_interpreter_entry(entry);
1273
1274 // Don't overwrite already registered native entries.
1275 if (is_native() && !has_native_function()) {
1276 set_native_function(
1277 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1278 !native_bind_event_is_interesting);
1279 }
1280 if (InlineTypeReturnedAsFields && returns_inline_type(THREAD) && !has_scalarized_return()) {
1281 set_has_scalarized_return();
1282 }
1283
1284 // Setup compiler entrypoint. This is made eagerly, so we do not need
1285 // special handling of vtables. An alternative is to make adapters more
1286 // lazily by calling make_adapter() from from_compiled_entry() for the
1287 // normal calls. For vtable calls life gets more complicated. When a
1288 // call-site goes mega-morphic we need adapters in all methods which can be
1289 // called from the vtable. We need adapters on such methods that get loaded
1290 // later. Ditto for mega-morphic itable calls. If this proves to be a
1291 // problem we'll make these lazily later.
1292 (void) make_adapters(h_method, CHECK);
1293
1294 // ONLY USE the h_method now as make_adapter may have blocked
1295
1296 if (h_method->is_continuation_native_intrinsic()) {
1297 _from_interpreted_entry = nullptr;
1298 _from_compiled_entry = nullptr;
1299 _i2i_entry = nullptr;
1300 if (Continuations::enabled()) {
1301 assert(!Threads::is_vm_complete(), "should only be called during vm init");
1302 AdapterHandlerLibrary::create_native_wrapper(h_method);
1309 }
1310
1311 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1312 // Adapters for compiled code are made eagerly here. They are fairly
1313 // small (generally < 100 bytes) and quick to make (and cached and shared)
1314 // so making them eagerly shouldn't be too expensive.
1315 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1316 if (adapter == nullptr ) {
1317 if (!is_init_completed()) {
1318 // Don't throw exceptions during VM initialization because java.lang.* classes
1319 // might not have been initialized, causing problems when constructing the
1320 // Java exception object.
1321 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1322 } else {
1323 THROW_MSG_NULL(vmSymbols::java_lang_OutOfMemoryError(), "Out of space in CodeCache for adapters");
1324 }
1325 }
1326
1327 mh->set_adapter_entry(adapter);
1328 mh->_from_compiled_entry = adapter->get_c2i_entry();
1329 mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1330 mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1331 return adapter->get_c2i_entry();
1332 }
1333
1334 // The verified_code_entry() must be called when a invoke is resolved
1335 // on this method.
1336
1337 // It returns the compiled code entry point, after asserting not null.
1338 // This function is called after potential safepoints so that nmethod
1339 // or adapter that it points to is still live and valid.
1340 // This function must not hit a safepoint!
1341 address Method::verified_code_entry() {
1342 debug_only(NoSafepointVerifier nsv;)
1343 assert(_from_compiled_entry != nullptr, "must be set");
1344 return _from_compiled_entry;
1345 }
1346
1347 address Method::verified_inline_code_entry() {
1348 debug_only(NoSafepointVerifier nsv;)
1349 assert(_from_compiled_inline_entry != nullptr, "must be set");
1350 return _from_compiled_inline_entry;
1351 }
1352
1353 address Method::verified_inline_ro_code_entry() {
1354 debug_only(NoSafepointVerifier nsv;)
1355 assert(_from_compiled_inline_ro_entry != nullptr, "must be set");
1356 return _from_compiled_inline_ro_entry;
1357 }
1358
1359 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1360 // (could be racing a deopt).
1361 // Not inline to avoid circular ref.
1362 bool Method::check_code() const {
1363 // cached in a register or local. There's a race on the value of the field.
1364 nmethod *code = Atomic::load_acquire(&_code);
1365 return code == nullptr || (code->method() == nullptr) || (code->method() == (Method*)this && !code->is_osr_method());
1366 }
1367
1368 // Install compiled code. Instantly it can execute.
1369 void Method::set_code(const methodHandle& mh, nmethod *code) {
1370 assert_lock_strong(NMethodState_lock);
1371 assert( code, "use clear_code to remove code" );
1372 assert( mh->check_code(), "" );
1373
1374 guarantee(mh->adapter() != nullptr, "Adapter blob must already exist!");
1375
1376 // These writes must happen in this order, because the interpreter will
1377 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1378 // which jumps to _from_compiled_entry.
1379 mh->_code = code; // Assign before allowing compiled code to exec
1380
1381 int comp_level = code->comp_level();
1382 // In theory there could be a race here. In practice it is unlikely
1383 // and not worth worrying about.
1384 if (comp_level > mh->highest_comp_level()) {
1385 mh->set_highest_comp_level(comp_level);
1386 }
1387
1388 OrderAccess::storestore();
1389 mh->_from_compiled_entry = code->verified_entry_point();
1390 mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1391 mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1392 OrderAccess::storestore();
1393
1394 if (mh->is_continuation_native_intrinsic()) {
1395 assert(mh->_from_interpreted_entry == nullptr, "initialized incorrectly"); // see link_method
1396
1397 if (mh->is_continuation_enter_intrinsic()) {
1398 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1399 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1400 } else if (mh->is_continuation_yield_intrinsic()) {
1401 mh->_i2i_entry = mh->get_i2c_entry();
1402 } else {
1403 guarantee(false, "Unknown Continuation native intrinsic");
1404 }
1405 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1406 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1407 } else if (!mh->is_method_handle_intrinsic()) {
1408 // Instantly compiled code can execute.
1409 mh->_from_interpreted_entry = mh->get_i2c_entry();
1410 }
1411 }
2349 }
2350
2351 // Check that this pointer is valid by checking that the vtbl pointer matches
2352 bool Method::is_valid_method(const Method* m) {
2353 if (m == nullptr) {
2354 return false;
2355 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2356 // Quick sanity check on pointer.
2357 return false;
2358 } else if (!os::is_readable_range(m, m + 1)) {
2359 return false;
2360 } else if (m->is_shared()) {
2361 return CppVtables::is_valid_shared_method(m);
2362 } else if (Metaspace::contains_non_shared(m)) {
2363 return has_method_vptr((const void*)m);
2364 } else {
2365 return false;
2366 }
2367 }
2368
2369 bool Method::is_scalarized_arg(int idx) const {
2370 if (!has_scalarized_args()) {
2371 return false;
2372 }
2373 // Search through signature and check if argument is wrapped in T_METADATA/T_VOID
2374 int depth = 0;
2375 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2376 for (int i = 0; i < sig->length(); i++) {
2377 BasicType bt = sig->at(i)._bt;
2378 if (bt == T_METADATA) {
2379 depth++;
2380 }
2381 if (idx == 0) {
2382 break; // Argument found
2383 }
2384 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2385 depth--;
2386 }
2387 if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2388 idx--; // Advance to next argument
2389 }
2390 }
2391 return depth != 0;
2392 }
2393
2394 #ifndef PRODUCT
2395 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2396 out->print("%d", loader_data->jmethod_ids()->count_methods());
2397 }
2398 #endif // PRODUCT
2399
2400
2401 // Printing
2402
2403 #ifndef PRODUCT
2404
2405 void Method::print_on(outputStream* st) const {
2406 ResourceMark rm;
2407 assert(is_method(), "must be method");
2408 st->print_cr("%s", internal_name());
2409 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2410 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2411 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2412 constants()->print_value_on(st); st->cr();
2413 st->print (" - access: 0x%x ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2414 st->print (" - flags: 0x%x ", _flags.as_int()); _flags.print_on(st); st->cr();
2415 st->print (" - name: "); name()->print_value_on(st); st->cr();
2416 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2417 st->print_cr(" - max stack: %d", max_stack());
2418 st->print_cr(" - max locals: %d", max_locals());
2419 st->print_cr(" - size of params: %d", size_of_parameters());
2420 st->print_cr(" - method size: %d", method_size());
2421 if (intrinsic_id() != vmIntrinsics::_none)
2422 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2423 if (highest_comp_level() != CompLevel_none)
2424 st->print_cr(" - highest level: %d", highest_comp_level());
2425 st->print_cr(" - vtable index: %d", _vtable_index);
2426 #ifdef ASSERT
2427 if (valid_itable_index())
2428 st->print_cr(" - itable index: %d", itable_index());
2429 #endif
2430 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2431 st->print( " - adapters: ");
2432 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2433 if (a == nullptr)
2434 st->print_cr(PTR_FORMAT, p2i(a));
2435 else
2436 a->print_adapter_on(st);
2437 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2438 st->print_cr(" - compiled inline entry " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2439 st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2440 st->print_cr(" - code size: %d", code_size());
2441 if (code_size() != 0) {
2442 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2443 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2444 }
2445 if (method_data() != nullptr) {
2446 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2447 }
2448 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2449 if (checked_exceptions_length() > 0) {
2450 CheckedExceptionElement* table = checked_exceptions_start();
2451 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2452 if (Verbose) {
2453 for (int i = 0; i < checked_exceptions_length(); i++) {
2454 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2455 }
2456 }
2457 }
2458 if (has_linenumber_table()) {
2459 u_char* table = compressed_linenumber_table();
2489 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2490 }
2491 }
2492
2493 void Method::print_linkage_flags(outputStream* st) {
2494 access_flags().print_on(st);
2495 if (is_default_method()) {
2496 st->print("default ");
2497 }
2498 if (is_overpass()) {
2499 st->print("overpass ");
2500 }
2501 }
2502 #endif //PRODUCT
2503
2504 void Method::print_value_on(outputStream* st) const {
2505 assert(is_method(), "must be method");
2506 st->print("%s", internal_name());
2507 print_address_on(st);
2508 st->print(" ");
2509 if (WizardMode) access_flags().print_on(st);
2510 name()->print_value_on(st);
2511 st->print(" ");
2512 signature()->print_value_on(st);
2513 st->print(" in ");
2514 method_holder()->print_value_on(st);
2515 if (WizardMode) st->print("#%d", _vtable_index);
2516 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2517 if (WizardMode && code() != nullptr) st->print(" ((nmethod*)%p)", code());
2518 }
2519
2520 // Verification
2521
2522 void Method::verify_on(outputStream* st) {
2523 guarantee(is_method(), "object must be method");
2524 guarantee(constants()->is_constantPool(), "should be constant pool");
2525 MethodData* md = method_data();
2526 guarantee(md == nullptr ||
2527 md->is_methodData(), "should be method data");
2528 }
|