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