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
103 set_access_flags(access_flags);
104 set_intrinsic_id(vmIntrinsics::_none);
105 set_force_inline(false);
106 set_hidden(false);
107 set_dont_inline(false);
108 set_changes_current_thread(false);
109 set_has_injected_profile(false);
110 set_method_data(NULL);
111 clear_method_counters();
112 set_vtable_index(Method::garbage_vtable_index);
113
114 // Fix and bury in Method*
115 set_interpreter_entry(NULL); // sets i2i entry and from_int
116 set_adapter_entry(NULL);
117 Method::clear_code(); // from_c/from_i get set to c2i/i2i
118
119 if (access_flags.is_native()) {
120 clear_native_function();
121 set_signature_handler(NULL);
122 }
123
124 NOT_PRODUCT(set_compiled_invocation_count(0);)
125 // Name is very useful for debugging.
126 NOT_PRODUCT(_name = name;)
127 }
128
129 // Release Method*. The nmethod will be gone when we get here because
130 // we've walked the code cache.
131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
132 MetadataFactory::free_metadata(loader_data, constMethod());
133 set_constMethod(NULL);
134 MetadataFactory::free_metadata(loader_data, method_data());
135 set_method_data(NULL);
136 MetadataFactory::free_metadata(loader_data, method_counters());
137 clear_method_counters();
138 // The nmethod will be gone when we get here.
139 if (code() != NULL) _code = NULL;
140 }
141
142 void Method::release_C_heap_structures() {
143 if (method_data()) {
144 method_data()->release_C_heap_structures();
145
146 // Destroy MethodData embedded lock
147 method_data()->~MethodData();
148 }
149 }
150
151 address Method::get_i2c_entry() {
152 assert(adapter() != NULL, "must have");
153 return adapter()->get_i2c_entry();
154 }
155
156 address Method::get_c2i_entry() {
157 assert(adapter() != NULL, "must have");
158 return adapter()->get_c2i_entry();
159 }
160
161 address Method::get_c2i_unverified_entry() {
162 assert(adapter() != NULL, "must have");
163 return adapter()->get_c2i_unverified_entry();
164 }
165
166 address Method::get_c2i_no_clinit_check_entry() {
167 assert(VM_Version::supports_fast_class_init_checks(), "");
168 assert(adapter() != NULL, "must have");
169 return adapter()->get_c2i_no_clinit_check_entry();
170 }
171
172 char* Method::name_and_sig_as_C_string() const {
173 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
174 }
175
176 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
177 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
178 }
179
180 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
181 const char* klass_name = klass->external_name();
182 int klass_name_len = (int)strlen(klass_name);
183 int method_name_len = method_name->utf8_length();
184 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
185 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
652 }
653
654 int Method::extra_stack_words() {
655 // not an inline function, to avoid a header dependency on Interpreter
656 return extra_stack_entries() * Interpreter::stackElementSize;
657 }
658
659 // Derive size of parameters, return type, and fingerprint,
660 // all in one pass, which is run at load time.
661 // We need the first two, and might as well grab the third.
662 void Method::compute_from_signature(Symbol* sig) {
663 // At this point, since we are scanning the signature,
664 // we might as well compute the whole fingerprint.
665 Fingerprinter fp(sig, is_static());
666 set_size_of_parameters(fp.size_of_parameters());
667 set_num_stack_arg_slots(fp.num_stack_arg_slots());
668 constMethod()->set_result_type(fp.return_type());
669 constMethod()->set_fingerprint(fp.fingerprint());
670 }
671
672 bool Method::is_vanilla_constructor() const {
673 // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
674 // which only calls the superclass vanilla constructor and possibly does stores of
675 // zero constants to local fields:
676 //
677 // aload_0
678 // invokespecial
679 // indexbyte1
680 // indexbyte2
681 //
682 // followed by an (optional) sequence of:
683 //
684 // aload_0
685 // aconst_null / iconst_0 / fconst_0 / dconst_0
686 // putfield
687 // indexbyte1
688 // indexbyte2
689 //
690 // followed by:
691 //
692 // return
693
694 assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors");
695 assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
696 int size = code_size();
697 // Check if size match
698 if (size == 0 || size % 5 != 0) return false;
699 address cb = code_base();
700 int last = size - 1;
701 if (cb[0] != Bytecodes::_aload_0 || cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
702 // Does not call superclass default constructor
703 return false;
704 }
705 // Check optional sequence
706 for (int i = 4; i < last; i += 5) {
707 if (cb[i] != Bytecodes::_aload_0) return false;
708 if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
709 if (cb[i+2] != Bytecodes::_putfield) return false;
710 }
711 return true;
712 }
713
714
715 bool Method::compute_has_loops_flag() {
716 BytecodeStream bcs(methodHandle(Thread::current(), this));
717 Bytecodes::Code bc;
718
719 while ((bc = bcs.next()) >= 0) {
720 switch (bc) {
721 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) != NULL;
975 } else {
976 return true;
977 }
978 }
979
980
981 bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
982 int klass_index = constants()->klass_ref_index_at(refinfo_index);
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 != NULL, "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 != NULL) {
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_not_c1_osr_compilable();
1138 set_not_c2_osr_compilable();
1139 } else {
1140 if (is_c1_compile(comp_level))
1141 set_not_c1_osr_compilable();
1142 if (is_c2_compile(comp_level))
1143 set_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() == NULL) {
1153 _from_compiled_entry = NULL;
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 = NULL;
1161 }
1162
1163 void Method::unlink_code(CompiledMethod *compare) {
1164 MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : 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() ? NULL : 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 = NULL;
1184 _adapter = NULL;
1185 _i2i_entry = NULL;
1186 _from_compiled_entry = NULL;
1187 _from_interpreted_entry = NULL;
1188
1189 if (is_native()) {
1190 *native_function_addr() = NULL;
1191 set_signature_handler(NULL);
1192 }
1193 NOT_PRODUCT(set_compiled_invocation_count(0);)
1194
1195 set_method_data(NULL);
1196 clear_method_counters();
1197 }
1198 #endif
1199
1200 // Called when the method_holder is getting linked. Setup entrypoints so the method
1201 // is ready to be called from interpreter, compiler, and vtables.
1202 void Method::link_method(const methodHandle& h_method, TRAPS) {
1203 // If the code cache is full, we may reenter this function for the
1204 // leftover methods that weren't linked.
1205 if (adapter() != NULL) {
1206 return;
1207 }
1208 assert( _code == NULL, "nothing compiled yet" );
1209
1210 // Setup interpreter entrypoint
1211 assert(this == h_method(), "wrong h_method()" );
1212
1213 assert(adapter() == NULL, "init'd to NULL");
1214 address entry = Interpreter::entry_for_method(h_method);
1215 assert(entry != NULL, "interpreter entry must be non-null");
1216 // Sets both _i2i_entry and _from_interpreted_entry
1217 set_interpreter_entry(entry);
1218
1219 // Don't overwrite already registered native entries.
1220 if (is_native() && !has_native_function()) {
1221 set_native_function(
1222 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1223 !native_bind_event_is_interesting);
1224 }
1225
1226 // Setup compiler entrypoint. This is made eagerly, so we do not need
1227 // special handling of vtables. An alternative is to make adapters more
1228 // lazily by calling make_adapter() from from_compiled_entry() for the
1229 // normal calls. For vtable calls life gets more complicated. When a
1230 // call-site goes mega-morphic we need adapters in all methods which can be
1231 // called from the vtable. We need adapters on such methods that get loaded
1232 // later. Ditto for mega-morphic itable calls. If this proves to be a
1233 // problem we'll make these lazily later.
1234 (void) make_adapters(h_method, CHECK);
1235
1236 // ONLY USE the h_method now as make_adapter may have blocked
1237
1238 if (h_method->is_continuation_native_intrinsic()) {
1239 // the entry points to this method will be set in set_code, called when first resolving this method
1240 _from_interpreted_entry = NULL;
1241 _from_compiled_entry = NULL;
1242 _i2i_entry = NULL;
1243 }
1244 }
1245
1246 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1247 // Adapters for compiled code are made eagerly here. They are fairly
1248 // small (generally < 100 bytes) and quick to make (and cached and shared)
1249 // so making them eagerly shouldn't be too expensive.
1250 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1251 if (adapter == NULL ) {
1252 if (!is_init_completed()) {
1253 // Don't throw exceptions during VM initialization because java.lang.* classes
1254 // might not have been initialized, causing problems when constructing the
1255 // Java exception object.
1256 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1257 } else {
1258 THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
1259 }
1260 }
1261
1262 mh->set_adapter_entry(adapter);
1263 mh->_from_compiled_entry = adapter->get_c2i_entry();
1264 return adapter->get_c2i_entry();
1265 }
1266
1267 // The verified_code_entry() must be called when a invoke is resolved
1268 // on this method.
1269
1270 // It returns the compiled code entry point, after asserting not null.
1271 // This function is called after potential safepoints so that nmethod
1272 // or adapter that it points to is still live and valid.
1273 // This function must not hit a safepoint!
1274 address Method::verified_code_entry() {
1275 debug_only(NoSafepointVerifier nsv;)
1276 assert(_from_compiled_entry != NULL, "must be set");
1277 return _from_compiled_entry;
1278 }
1279
1280 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1281 // (could be racing a deopt).
1282 // Not inline to avoid circular ref.
1283 bool Method::check_code() const {
1284 // cached in a register or local. There's a race on the value of the field.
1285 CompiledMethod *code = Atomic::load_acquire(&_code);
1286 return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
1287 }
1288
1289 // Install compiled code. Instantly it can execute.
1290 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1291 assert_lock_strong(CompiledMethod_lock);
1292 assert( code, "use clear_code to remove code" );
1293 assert( mh->check_code(), "" );
1294
1295 guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
1296
1297 // These writes must happen in this order, because the interpreter will
1298 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1299 // which jumps to _from_compiled_entry.
1300 mh->_code = code; // Assign before allowing compiled code to exec
1301
1302 int comp_level = code->comp_level();
1303 // In theory there could be a race here. In practice it is unlikely
1304 // and not worth worrying about.
1305 if (comp_level > mh->highest_comp_level()) {
1306 mh->set_highest_comp_level(comp_level);
1307 }
1308
1309 OrderAccess::storestore();
1310 mh->_from_compiled_entry = code->verified_entry_point();
1311 OrderAccess::storestore();
1312
1313 if (mh->is_continuation_native_intrinsic()) {
1314 assert(mh->_from_interpreted_entry == NULL, "initialized incorrectly"); // see link_method
1315
1316 if (mh->is_continuation_enter_intrinsic()) {
1317 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1318 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1319 } else if (mh->is_continuation_yield_intrinsic()) {
1320 mh->_i2i_entry = mh->get_i2c_entry();
1321 } else {
1322 guarantee(false, "Unknown Continuation native intrinsic");
1323 }
1324 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1325 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1326 } else if (!mh->is_method_handle_intrinsic()) {
1327 // Instantly compiled code can execute.
1328 mh->_from_interpreted_entry = mh->get_i2c_entry();
1329 }
1330 }
2267 }
2268
2269 // Check that this pointer is valid by checking that the vtbl pointer matches
2270 bool Method::is_valid_method(const Method* m) {
2271 if (m == NULL) {
2272 return false;
2273 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2274 // Quick sanity check on pointer.
2275 return false;
2276 } else if (!os::is_readable_range(m, m + 1)) {
2277 return false;
2278 } else if (m->is_shared()) {
2279 return CppVtables::is_valid_shared_method(m);
2280 } else if (Metaspace::contains_non_shared(m)) {
2281 return has_method_vptr((const void*)m);
2282 } else {
2283 return false;
2284 }
2285 }
2286
2287 #ifndef PRODUCT
2288 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2289 out->print("%d", loader_data->jmethod_ids()->count_methods());
2290 }
2291 #endif // PRODUCT
2292
2293
2294 // Printing
2295
2296 #ifndef PRODUCT
2297
2298 void Method::print_on(outputStream* st) const {
2299 ResourceMark rm;
2300 assert(is_method(), "must be method");
2301 st->print_cr("%s", internal_name());
2302 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2303 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2304 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2305 constants()->print_value_on(st); st->cr();
2306 st->print (" - access: 0x%x ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2307 st->print (" - name: "); name()->print_value_on(st); st->cr();
2308 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2309 st->print_cr(" - max stack: %d", max_stack());
2310 st->print_cr(" - max locals: %d", max_locals());
2311 st->print_cr(" - size of params: %d", size_of_parameters());
2312 st->print_cr(" - method size: %d", method_size());
2313 if (intrinsic_id() != vmIntrinsics::_none)
2314 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2315 if (highest_comp_level() != CompLevel_none)
2316 st->print_cr(" - highest level: %d", highest_comp_level());
2317 st->print_cr(" - vtable index: %d", _vtable_index);
2318 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2319 st->print( " - adapters: ");
2320 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2321 if (a == NULL)
2322 st->print_cr(PTR_FORMAT, p2i(a));
2323 else
2324 a->print_adapter_on(st);
2325 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2326 st->print_cr(" - code size: %d", code_size());
2327 if (code_size() != 0) {
2328 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2329 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2330 }
2331 if (method_data() != NULL) {
2332 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2333 }
2334 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2335 if (checked_exceptions_length() > 0) {
2336 CheckedExceptionElement* table = checked_exceptions_start();
2337 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2338 if (Verbose) {
2339 for (int i = 0; i < checked_exceptions_length(); i++) {
2340 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2341 }
2342 }
2343 }
2344 if (has_linenumber_table()) {
2345 u_char* table = compressed_linenumber_table();
2375 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2376 }
2377 }
2378
2379 void Method::print_linkage_flags(outputStream* st) {
2380 access_flags().print_on(st);
2381 if (is_default_method()) {
2382 st->print("default ");
2383 }
2384 if (is_overpass()) {
2385 st->print("overpass ");
2386 }
2387 }
2388 #endif //PRODUCT
2389
2390 void Method::print_value_on(outputStream* st) const {
2391 assert(is_method(), "must be method");
2392 st->print("%s", internal_name());
2393 print_address_on(st);
2394 st->print(" ");
2395 name()->print_value_on(st);
2396 st->print(" ");
2397 signature()->print_value_on(st);
2398 st->print(" in ");
2399 method_holder()->print_value_on(st);
2400 if (WizardMode) st->print("#%d", _vtable_index);
2401 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2402 if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code());
2403 }
2404
2405 // Verification
2406
2407 void Method::verify_on(outputStream* st) {
2408 guarantee(is_method(), "object must be method");
2409 guarantee(constants()->is_constantPool(), "should be constant pool");
2410 MethodData* md = method_data();
2411 guarantee(md == NULL ||
2412 md->is_methodData(), "should be method data");
2413 }
|
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
104 set_access_flags(access_flags);
105 set_intrinsic_id(vmIntrinsics::_none);
106 set_force_inline(false);
107 set_hidden(false);
108 set_dont_inline(false);
109 set_changes_current_thread(false);
110 set_has_injected_profile(false);
111 set_method_data(NULL);
112 clear_method_counters();
113 set_vtable_index(Method::garbage_vtable_index);
114
115 // Fix and bury in Method*
116 set_interpreter_entry(NULL); // sets i2i entry and from_int
117 set_adapter_entry(NULL);
118 Method::clear_code(); // from_c/from_i get set to c2i/i2i
119
120 if (access_flags.is_native()) {
121 clear_native_function();
122 set_signature_handler(NULL);
123 }
124 NOT_PRODUCT(set_compiled_invocation_count(0);)
125 // Name is very useful for debugging.
126 NOT_PRODUCT(_name = name;)
127 }
128
129 // Release Method*. The nmethod will be gone when we get here because
130 // we've walked the code cache.
131 void Method::deallocate_contents(ClassLoaderData* loader_data) {
132 MetadataFactory::free_metadata(loader_data, constMethod());
133 set_constMethod(NULL);
134 MetadataFactory::free_metadata(loader_data, method_data());
135 set_method_data(NULL);
136 MetadataFactory::free_metadata(loader_data, method_counters());
137 clear_method_counters();
138 // The nmethod will be gone when we get here.
139 if (code() != NULL) _code = NULL;
140 }
141
142 void Method::release_C_heap_structures() {
143 if (method_data()) {
144 method_data()->release_C_heap_structures();
145
146 // Destroy MethodData embedded lock
147 method_data()->~MethodData();
148 }
149 }
150
151 address Method::get_i2c_entry() {
152 assert(adapter() != NULL, "must have");
153 return adapter()->get_i2c_entry();
154 }
155
156 address Method::get_c2i_entry() {
157 assert(adapter() != NULL, "must have");
158 return adapter()->get_c2i_entry();
159 }
160
161 address Method::get_c2i_inline_entry() {
162 assert(adapter() != NULL, "must have");
163 return adapter()->get_c2i_inline_entry();
164 }
165
166 address Method::get_c2i_unverified_entry() {
167 assert(adapter() != NULL, "must have");
168 return adapter()->get_c2i_unverified_entry();
169 }
170
171 address Method::get_c2i_unverified_inline_entry() {
172 assert(adapter() != NULL, "must have");
173 return adapter()->get_c2i_unverified_inline_entry();
174 }
175
176 address Method::get_c2i_no_clinit_check_entry() {
177 assert(VM_Version::supports_fast_class_init_checks(), "");
178 assert(adapter() != NULL, "must have");
179 return adapter()->get_c2i_no_clinit_check_entry();
180 }
181
182 char* Method::name_and_sig_as_C_string() const {
183 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature());
184 }
185
186 char* Method::name_and_sig_as_C_string(char* buf, int size) const {
187 return name_and_sig_as_C_string(constants()->pool_holder(), name(), signature(), buf, size);
188 }
189
190 char* Method::name_and_sig_as_C_string(Klass* klass, Symbol* method_name, Symbol* signature) {
191 const char* klass_name = klass->external_name();
192 int klass_name_len = (int)strlen(klass_name);
193 int method_name_len = method_name->utf8_length();
194 int len = klass_name_len + 1 + method_name_len + signature->utf8_length();
195 char* dest = NEW_RESOURCE_ARRAY(char, len + 1);
662 }
663
664 int Method::extra_stack_words() {
665 // not an inline function, to avoid a header dependency on Interpreter
666 return extra_stack_entries() * Interpreter::stackElementSize;
667 }
668
669 // Derive size of parameters, return type, and fingerprint,
670 // all in one pass, which is run at load time.
671 // We need the first two, and might as well grab the third.
672 void Method::compute_from_signature(Symbol* sig) {
673 // At this point, since we are scanning the signature,
674 // we might as well compute the whole fingerprint.
675 Fingerprinter fp(sig, is_static());
676 set_size_of_parameters(fp.size_of_parameters());
677 set_num_stack_arg_slots(fp.num_stack_arg_slots());
678 constMethod()->set_result_type(fp.return_type());
679 constMethod()->set_fingerprint(fp.fingerprint());
680 }
681
682 // InlineKlass the method is declared to return. This must not
683 // safepoint as it is called with references live on the stack at
684 // locations the GC is unaware of.
685 InlineKlass* Method::returns_inline_type(Thread* thread) const {
686 assert(InlineTypeReturnedAsFields, "Inline types should never be returned as fields");
687 NoSafepointVerifier nsv;
688 SignatureStream ss(signature());
689 while (!ss.at_return_type()) {
690 ss.next();
691 }
692 return ss.as_inline_klass(method_holder());
693 }
694
695 bool Method::is_vanilla_constructor() const {
696 // Returns true if this method is a vanilla constructor, i.e. an "<init>" "()V" method
697 // which only calls the superclass vanilla constructor and possibly does stores of
698 // zero constants to local fields:
699 //
700 // aload_0, _fast_aload_0, or _nofast_aload_0
701 // invokespecial
702 // indexbyte1
703 // indexbyte2
704 //
705 // followed by an (optional) sequence of:
706 //
707 // aload_0
708 // aconst_null / iconst_0 / fconst_0 / dconst_0
709 // putfield
710 // indexbyte1
711 // indexbyte2
712 //
713 // followed by:
714 //
715 // return
716
717 assert(name() == vmSymbols::object_initializer_name(), "Should only be called for default constructors");
718 assert(signature() == vmSymbols::void_method_signature(), "Should only be called for default constructors");
719 int size = code_size();
720 // Check if size match
721 if (size == 0 || size % 5 != 0) return false;
722 address cb = code_base();
723 int last = size - 1;
724 if ((cb[0] != Bytecodes::_aload_0 && cb[0] != Bytecodes::_fast_aload_0 && cb[0] != Bytecodes::_nofast_aload_0) ||
725 cb[1] != Bytecodes::_invokespecial || cb[last] != Bytecodes::_return) {
726 // Does not call superclass default constructor
727 return false;
728 }
729 // Check optional sequence
730 for (int i = 4; i < last; i += 5) {
731 if (cb[i] != Bytecodes::_aload_0) return false;
732 if (!Bytecodes::is_zero_const(Bytecodes::cast(cb[i+1]))) return false;
733 if (cb[i+2] != Bytecodes::_putfield) return false;
734 }
735 return true;
736 }
737
738
739 bool Method::compute_has_loops_flag() {
740 BytecodeStream bcs(methodHandle(Thread::current(), this));
741 Bytecodes::Code bc;
742
743 while ((bc = bcs.next()) >= 0) {
744 switch (bc) {
745 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) != NULL;
1012 } else {
1013 return true;
1014 }
1015 }
1016
1017
1018 bool Method::is_klass_loaded(int refinfo_index, bool must_be_resolved) const {
1019 int klass_index = constants()->klass_ref_index_at(refinfo_index);
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 != NULL, "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 != NULL) {
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_not_c1_osr_compilable();
1177 set_not_c2_osr_compilable();
1178 } else {
1179 if (is_c1_compile(comp_level))
1180 set_not_c1_osr_compilable();
1181 if (is_c2_compile(comp_level))
1182 set_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() == NULL) {
1192 _from_compiled_entry = NULL;
1193 _from_compiled_inline_entry = NULL;
1194 _from_compiled_inline_ro_entry = NULL;
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 = NULL;
1204 }
1205
1206 void Method::unlink_code(CompiledMethod *compare) {
1207 MutexLocker ml(CompiledMethod_lock->owned_by_self() ? NULL : 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() ? NULL : 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 = NULL;
1227 _adapter = NULL;
1228 _i2i_entry = NULL;
1229 _from_compiled_entry = NULL;
1230 _from_compiled_inline_entry = NULL;
1231 _from_compiled_inline_ro_entry = NULL;
1232 _from_interpreted_entry = NULL;
1233
1234 if (is_native()) {
1235 *native_function_addr() = NULL;
1236 set_signature_handler(NULL);
1237 }
1238 NOT_PRODUCT(set_compiled_invocation_count(0);)
1239
1240 set_method_data(NULL);
1241 clear_method_counters();
1242 }
1243 #endif
1244
1245 // Called when the method_holder is getting linked. Setup entrypoints so the method
1246 // is ready to be called from interpreter, compiler, and vtables.
1247 void Method::link_method(const methodHandle& h_method, TRAPS) {
1248 // If the code cache is full, we may reenter this function for the
1249 // leftover methods that weren't linked.
1250 if (adapter() != NULL) {
1251 return;
1252 }
1253 assert( _code == NULL, "nothing compiled yet" );
1254
1255 // Setup interpreter entrypoint
1256 assert(this == h_method(), "wrong h_method()" );
1257
1258 assert(adapter() == NULL, "init'd to NULL");
1259 address entry = Interpreter::entry_for_method(h_method);
1260 assert(entry != NULL, "interpreter entry must be non-null");
1261 // Sets both _i2i_entry and _from_interpreted_entry
1262 set_interpreter_entry(entry);
1263
1264 // Don't overwrite already registered native entries.
1265 if (is_native() && !has_native_function()) {
1266 set_native_function(
1267 SharedRuntime::native_method_throw_unsatisfied_link_error_entry(),
1268 !native_bind_event_is_interesting);
1269 }
1270 if (InlineTypeReturnedAsFields && returns_inline_type(THREAD)) {
1271 set_has_scalarized_return(true);
1272 }
1273
1274 // Setup compiler entrypoint. This is made eagerly, so we do not need
1275 // special handling of vtables. An alternative is to make adapters more
1276 // lazily by calling make_adapter() from from_compiled_entry() for the
1277 // normal calls. For vtable calls life gets more complicated. When a
1278 // call-site goes mega-morphic we need adapters in all methods which can be
1279 // called from the vtable. We need adapters on such methods that get loaded
1280 // later. Ditto for mega-morphic itable calls. If this proves to be a
1281 // problem we'll make these lazily later.
1282 (void) make_adapters(h_method, CHECK);
1283
1284 // ONLY USE the h_method now as make_adapter may have blocked
1285
1286 if (h_method->is_continuation_native_intrinsic()) {
1287 // the entry points to this method will be set in set_code, called when first resolving this method
1288 _from_interpreted_entry = NULL;
1289 _from_compiled_entry = NULL;
1290 _i2i_entry = NULL;
1291 }
1292 }
1293
1294 address Method::make_adapters(const methodHandle& mh, TRAPS) {
1295 // Adapters for compiled code are made eagerly here. They are fairly
1296 // small (generally < 100 bytes) and quick to make (and cached and shared)
1297 // so making them eagerly shouldn't be too expensive.
1298 AdapterHandlerEntry* adapter = AdapterHandlerLibrary::get_adapter(mh);
1299 if (adapter == NULL ) {
1300 if (!is_init_completed()) {
1301 // Don't throw exceptions during VM initialization because java.lang.* classes
1302 // might not have been initialized, causing problems when constructing the
1303 // Java exception object.
1304 vm_exit_during_initialization("Out of space in CodeCache for adapters");
1305 } else {
1306 THROW_MSG_NULL(vmSymbols::java_lang_VirtualMachineError(), "Out of space in CodeCache for adapters");
1307 }
1308 }
1309
1310 mh->set_adapter_entry(adapter);
1311 mh->_from_compiled_entry = adapter->get_c2i_entry();
1312 mh->_from_compiled_inline_entry = adapter->get_c2i_inline_entry();
1313 mh->_from_compiled_inline_ro_entry = adapter->get_c2i_inline_ro_entry();
1314 return adapter->get_c2i_entry();
1315 }
1316
1317 // The verified_code_entry() must be called when a invoke is resolved
1318 // on this method.
1319
1320 // It returns the compiled code entry point, after asserting not null.
1321 // This function is called after potential safepoints so that nmethod
1322 // or adapter that it points to is still live and valid.
1323 // This function must not hit a safepoint!
1324 address Method::verified_code_entry() {
1325 debug_only(NoSafepointVerifier nsv;)
1326 assert(_from_compiled_entry != NULL, "must be set");
1327 return _from_compiled_entry;
1328 }
1329
1330 address Method::verified_inline_code_entry() {
1331 debug_only(NoSafepointVerifier nsv;)
1332 assert(_from_compiled_inline_entry != NULL, "must be set");
1333 return _from_compiled_inline_entry;
1334 }
1335
1336 address Method::verified_inline_ro_code_entry() {
1337 debug_only(NoSafepointVerifier nsv;)
1338 assert(_from_compiled_inline_ro_entry != NULL, "must be set");
1339 return _from_compiled_inline_ro_entry;
1340 }
1341
1342 // Check that if an nmethod ref exists, it has a backlink to this or no backlink at all
1343 // (could be racing a deopt).
1344 // Not inline to avoid circular ref.
1345 bool Method::check_code() const {
1346 // cached in a register or local. There's a race on the value of the field.
1347 CompiledMethod *code = Atomic::load_acquire(&_code);
1348 return code == NULL || (code->method() == NULL) || (code->method() == (Method*)this && !code->is_osr_method());
1349 }
1350
1351 // Install compiled code. Instantly it can execute.
1352 void Method::set_code(const methodHandle& mh, CompiledMethod *code) {
1353 assert_lock_strong(CompiledMethod_lock);
1354 assert( code, "use clear_code to remove code" );
1355 assert( mh->check_code(), "" );
1356
1357 guarantee(mh->adapter() != NULL, "Adapter blob must already exist!");
1358
1359 // These writes must happen in this order, because the interpreter will
1360 // directly jump to from_interpreted_entry which jumps to an i2c adapter
1361 // which jumps to _from_compiled_entry.
1362 mh->_code = code; // Assign before allowing compiled code to exec
1363
1364 int comp_level = code->comp_level();
1365 // In theory there could be a race here. In practice it is unlikely
1366 // and not worth worrying about.
1367 if (comp_level > mh->highest_comp_level()) {
1368 mh->set_highest_comp_level(comp_level);
1369 }
1370
1371 OrderAccess::storestore();
1372 mh->_from_compiled_entry = code->verified_entry_point();
1373 mh->_from_compiled_inline_entry = code->verified_inline_entry_point();
1374 mh->_from_compiled_inline_ro_entry = code->verified_inline_ro_entry_point();
1375 OrderAccess::storestore();
1376
1377 if (mh->is_continuation_native_intrinsic()) {
1378 assert(mh->_from_interpreted_entry == NULL, "initialized incorrectly"); // see link_method
1379
1380 if (mh->is_continuation_enter_intrinsic()) {
1381 // This is the entry used when we're in interpreter-only mode; see InterpreterMacroAssembler::jump_from_interpreted
1382 mh->_i2i_entry = ContinuationEntry::interpreted_entry();
1383 } else if (mh->is_continuation_yield_intrinsic()) {
1384 mh->_i2i_entry = mh->get_i2c_entry();
1385 } else {
1386 guarantee(false, "Unknown Continuation native intrinsic");
1387 }
1388 // This must come last, as it is what's tested in LinkResolver::resolve_static_call
1389 Atomic::release_store(&mh->_from_interpreted_entry , mh->get_i2c_entry());
1390 } else if (!mh->is_method_handle_intrinsic()) {
1391 // Instantly compiled code can execute.
1392 mh->_from_interpreted_entry = mh->get_i2c_entry();
1393 }
1394 }
2331 }
2332
2333 // Check that this pointer is valid by checking that the vtbl pointer matches
2334 bool Method::is_valid_method(const Method* m) {
2335 if (m == NULL) {
2336 return false;
2337 } else if ((intptr_t(m) & (wordSize-1)) != 0) {
2338 // Quick sanity check on pointer.
2339 return false;
2340 } else if (!os::is_readable_range(m, m + 1)) {
2341 return false;
2342 } else if (m->is_shared()) {
2343 return CppVtables::is_valid_shared_method(m);
2344 } else if (Metaspace::contains_non_shared(m)) {
2345 return has_method_vptr((const void*)m);
2346 } else {
2347 return false;
2348 }
2349 }
2350
2351 bool Method::is_scalarized_arg(int idx) const {
2352 if (!has_scalarized_args()) {
2353 return false;
2354 }
2355 // Search through signature and check if argument is wrapped in T_PRIMITIVE_OBJECT/T_VOID
2356 int depth = 0;
2357 const GrowableArray<SigEntry>* sig = adapter()->get_sig_cc();
2358 for (int i = 0; i < sig->length(); i++) {
2359 BasicType bt = sig->at(i)._bt;
2360 if (bt == T_PRIMITIVE_OBJECT) {
2361 depth++;
2362 }
2363 if (idx == 0) {
2364 break; // Argument found
2365 }
2366 if (bt == T_VOID && (sig->at(i-1)._bt != T_LONG && sig->at(i-1)._bt != T_DOUBLE)) {
2367 depth--;
2368 }
2369 if (depth == 0 && bt != T_LONG && bt != T_DOUBLE) {
2370 idx--; // Advance to next argument
2371 }
2372 }
2373 return depth != 0;
2374 }
2375
2376 #ifndef PRODUCT
2377 void Method::print_jmethod_ids_count(const ClassLoaderData* loader_data, outputStream* out) {
2378 out->print("%d", loader_data->jmethod_ids()->count_methods());
2379 }
2380 #endif // PRODUCT
2381
2382
2383 // Printing
2384
2385 #ifndef PRODUCT
2386
2387 void Method::print_on(outputStream* st) const {
2388 ResourceMark rm;
2389 assert(is_method(), "must be method");
2390 st->print_cr("%s", internal_name());
2391 st->print_cr(" - this oop: " PTR_FORMAT, p2i(this));
2392 st->print (" - method holder: "); method_holder()->print_value_on(st); st->cr();
2393 st->print (" - constants: " PTR_FORMAT " ", p2i(constants()));
2394 constants()->print_value_on(st); st->cr();
2395 st->print (" - access: 0x%x ", access_flags().as_int()); access_flags().print_on(st); st->cr();
2396 st->print (" - name: "); name()->print_value_on(st); st->cr();
2397 st->print (" - signature: "); signature()->print_value_on(st); st->cr();
2398 st->print_cr(" - max stack: %d", max_stack());
2399 st->print_cr(" - max locals: %d", max_locals());
2400 st->print_cr(" - size of params: %d", size_of_parameters());
2401 st->print_cr(" - method size: %d", method_size());
2402 if (intrinsic_id() != vmIntrinsics::_none)
2403 st->print_cr(" - intrinsic id: %d %s", vmIntrinsics::as_int(intrinsic_id()), vmIntrinsics::name_at(intrinsic_id()));
2404 if (highest_comp_level() != CompLevel_none)
2405 st->print_cr(" - highest level: %d", highest_comp_level());
2406 st->print_cr(" - vtable index: %d", _vtable_index);
2407 #ifdef ASSERT
2408 if (valid_itable_index())
2409 st->print_cr(" - itable index: %d", itable_index());
2410 #endif
2411 st->print_cr(" - i2i entry: " PTR_FORMAT, p2i(interpreter_entry()));
2412 st->print( " - adapters: ");
2413 AdapterHandlerEntry* a = ((Method*)this)->adapter();
2414 if (a == NULL)
2415 st->print_cr(PTR_FORMAT, p2i(a));
2416 else
2417 a->print_adapter_on(st);
2418 st->print_cr(" - compiled entry " PTR_FORMAT, p2i(from_compiled_entry()));
2419 st->print_cr(" - compiled inline entry " PTR_FORMAT, p2i(from_compiled_inline_entry()));
2420 st->print_cr(" - compiled inline ro entry " PTR_FORMAT, p2i(from_compiled_inline_ro_entry()));
2421 st->print_cr(" - code size: %d", code_size());
2422 if (code_size() != 0) {
2423 st->print_cr(" - code start: " PTR_FORMAT, p2i(code_base()));
2424 st->print_cr(" - code end (excl): " PTR_FORMAT, p2i(code_base() + code_size()));
2425 }
2426 if (method_data() != NULL) {
2427 st->print_cr(" - method data: " PTR_FORMAT, p2i(method_data()));
2428 }
2429 st->print_cr(" - checked ex length: %d", checked_exceptions_length());
2430 if (checked_exceptions_length() > 0) {
2431 CheckedExceptionElement* table = checked_exceptions_start();
2432 st->print_cr(" - checked ex start: " PTR_FORMAT, p2i(table));
2433 if (Verbose) {
2434 for (int i = 0; i < checked_exceptions_length(); i++) {
2435 st->print_cr(" - throws %s", constants()->printable_name_at(table[i].class_cp_index));
2436 }
2437 }
2438 }
2439 if (has_linenumber_table()) {
2440 u_char* table = compressed_linenumber_table();
2470 st->print_cr(" - signature handler: " PTR_FORMAT, p2i(signature_handler()));
2471 }
2472 }
2473
2474 void Method::print_linkage_flags(outputStream* st) {
2475 access_flags().print_on(st);
2476 if (is_default_method()) {
2477 st->print("default ");
2478 }
2479 if (is_overpass()) {
2480 st->print("overpass ");
2481 }
2482 }
2483 #endif //PRODUCT
2484
2485 void Method::print_value_on(outputStream* st) const {
2486 assert(is_method(), "must be method");
2487 st->print("%s", internal_name());
2488 print_address_on(st);
2489 st->print(" ");
2490 if (WizardMode) access_flags().print_on(st);
2491 name()->print_value_on(st);
2492 st->print(" ");
2493 signature()->print_value_on(st);
2494 st->print(" in ");
2495 method_holder()->print_value_on(st);
2496 if (WizardMode) st->print("#%d", _vtable_index);
2497 if (WizardMode) st->print("[%d,%d]", size_of_parameters(), max_locals());
2498 if (WizardMode && code() != NULL) st->print(" ((nmethod*)%p)", code());
2499 }
2500
2501 // Verification
2502
2503 void Method::verify_on(outputStream* st) {
2504 guarantee(is_method(), "object must be method");
2505 guarantee(constants()->is_constantPool(), "should be constant pool");
2506 MethodData* md = method_data();
2507 guarantee(md == NULL ||
2508 md->is_methodData(), "should be method data");
2509 }
|