1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
696
697 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
698 JavaThread* thread = reg_map->thread();
699 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
700 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
701 return;
702 }
703
704 if (!method()->is_native()) {
705 address pc = fr.pc();
706 bool has_receiver, has_appendix;
707 Symbol* signature;
708
709 // The method attached by JIT-compilers should be used, if present.
710 // Bytecode can be inaccurate in such case.
711 Method* callee = attached_method_before_pc(pc);
712 if (callee != nullptr) {
713 has_receiver = !(callee->access_flags().is_static());
714 has_appendix = false;
715 signature = callee->signature();
716 } else {
717 SimpleScopeDesc ssd(this, pc);
718
719 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
720 has_receiver = call.has_receiver();
721 has_appendix = call.has_appendix();
722 signature = call.signature();
723 }
724
725 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
726 } else if (method()->is_continuation_enter_intrinsic()) {
727 // This method only calls Continuation.enter()
728 Symbol* signature = vmSymbols::continuationEnter_signature();
729 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
730 }
731 }
732
733 Method* nmethod::attached_method(address call_instr) {
734 assert(code_contains(call_instr), "not part of the nmethod");
735 RelocIterator iter(this, call_instr, call_instr + 1);
1226
1227 _has_unsafe_access = 0;
1228 _has_wide_vectors = 0;
1229 _has_monitors = 0;
1230 _has_scoped_access = 0;
1231 _has_flushed_dependencies = 0;
1232 _is_unlinked = 0;
1233 _load_reported = 0; // jvmti state
1234
1235 _deoptimization_status = not_marked;
1236
1237 // SECT_CONSTS is first in code buffer so the offset should be 0.
1238 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1239 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1240
1241 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1242
1243 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1244 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1245
1246 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1247 }
1248
1249 // Post initialization
1250 void nmethod::post_init() {
1251 clear_unloading_state();
1252
1253 finalize_relocations();
1254
1255 Universe::heap()->register_nmethod(this);
1256 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1257
1258 CodeCache::commit(this);
1259 }
1260
1261 // For native wrappers
1262 nmethod::nmethod(
1263 Method* method,
1264 CompilerType type,
1265 int nmethod_size,
1266 int compile_id,
1267 CodeOffsets* offsets,
1268 CodeBuffer* code_buffer,
1269 int frame_size,
1270 ByteSize basic_lock_owner_sp_offset,
1271 ByteSize basic_lock_sp_offset,
1272 OopMapSet* oop_maps,
1273 int mutable_data_size)
1274 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1275 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1276 _deoptimization_generation(0),
1277 _gc_epoch(CodeCache::gc_epoch()),
1278 _method(method),
1279 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1280 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1281 {
1282 {
1283 DEBUG_ONLY(NoSafepointVerifier nsv;)
1284 assert_locked_or_safepoint(CodeCache_lock);
1285
1286 init_defaults(code_buffer, offsets);
1287
1288 _osr_entry_point = nullptr;
1289 _pc_desc_container = nullptr;
1290 _entry_bci = InvocationEntryBci;
1291 _compile_id = compile_id;
1292 _comp_level = CompLevel_none;
1293 _compiler_type = type;
1294 _orig_pc_offset = 0;
1295 _num_stack_arg_slots = 0;
1296
1297 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1298 // Continuation enter intrinsic
1299 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1300 } else {
1301 _exception_offset = 0;
1302 }
1303 // Native wrappers do not have deopt handlers. Make the values
1304 // something that will never match a pc like the nmethod vtable entry
1305 _deopt_handler_entry_offset = 0;
1411 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1412 if (_mutable_data == nullptr) {
1413 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1414 }
1415 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1416 } else {
1417 _mutable_data = nullptr;
1418 }
1419
1420 _deoptimization_generation = 0;
1421 _gc_epoch = CodeCache::gc_epoch();
1422 _method = nm._method;
1423 _osr_link = nullptr;
1424
1425 _exception_cache = nullptr;
1426 _gc_data = nullptr;
1427 _oops_do_mark_nmethods = nullptr;
1428 _oops_do_mark_link = nullptr;
1429 _compiled_ic_data = nullptr;
1430
1431 if (nm._osr_entry_point != nullptr) {
1432 _osr_entry_point = (nm._osr_entry_point - (address) &nm) + (address) this;
1433 } else {
1434 _osr_entry_point = nullptr;
1435 }
1436
1437 _entry_offset = nm._entry_offset;
1438 _verified_entry_offset = nm._verified_entry_offset;
1439 _entry_bci = nm._entry_bci;
1440 _immutable_data_size = nm._immutable_data_size;
1441
1442 _skipped_instructions_size = nm._skipped_instructions_size;
1443 _stub_offset = nm._stub_offset;
1444 _exception_offset = nm._exception_offset;
1445 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1446 _unwind_handler_offset = nm._unwind_handler_offset;
1447 _num_stack_arg_slots = nm._num_stack_arg_slots;
1448 _oops_size = nm._oops_size;
1449 #if INCLUDE_JVMCI
1450 _metadata_size = nm._metadata_size;
1451 #endif
1452 _nul_chk_table_offset = nm._nul_chk_table_offset;
1453 _handler_table_offset = nm._handler_table_offset;
1454 _scopes_pcs_offset = nm._scopes_pcs_offset;
1455 _scopes_data_offset = nm._scopes_data_offset;
1456 #if INCLUDE_JVMCI
1457 _speculations_offset = nm._speculations_offset;
1458 #endif
1736 _exception_offset = -1;
1737 }
1738
1739 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1740 }
1741 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1742 // C1 generates UnwindHandler at the end of instructions section.
1743 // Calculate positive offset as distance between the start of stubs section
1744 // (which is also the end of instructions section) and the start of the handler.
1745 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1746 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1747 } else {
1748 _unwind_handler_offset = -1;
1749 }
1750
1751 CHECKED_CAST(_oops_size, uint16_t, align_up(code_buffer->total_oop_size(), oopSize));
1752 uint16_t metadata_size;
1753 CHECKED_CAST(metadata_size, uint16_t, align_up(code_buffer->total_metadata_size(), wordSize));
1754 JVMCI_ONLY( _metadata_size = metadata_size; )
1755 int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));
1756 assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1757 "wrong mutable data size: %d != %d + %d + %d",
1758 _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1759 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1760 nmethod_size, (int)(code_end() - header_begin()));
1761
1762 _immutable_data_size = immutable_data_size;
1763 if (immutable_data_size > 0) {
1764 assert(immutable_data != nullptr, "required");
1765 _immutable_data = immutable_data;
1766 } else {
1767 // We need unique not null address
1768 _immutable_data = blob_end();
1769 }
1770 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1771 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1772 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1773 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1774
1775 #if INCLUDE_JVMCI
3189 }
3190
3191 bool nmethod::check_dependency_on(DepChange& changes) {
3192 // What has happened:
3193 // 1) a new class dependee has been added
3194 // 2) dependee and all its super classes have been marked
3195 bool found_check = false; // set true if we are upset
3196 for (Dependencies::DepStream deps(this); deps.next(); ) {
3197 // Evaluate only relevant dependencies.
3198 if (deps.spot_check_dependency_at(changes) != nullptr) {
3199 found_check = true;
3200 NOT_DEBUG(break);
3201 }
3202 }
3203 return found_check;
3204 }
3205
3206 // Called from mark_for_deoptimization, when dependee is invalidated.
3207 bool nmethod::is_dependent_on_method(Method* dependee) {
3208 for (Dependencies::DepStream deps(this); deps.next(); ) {
3209 if (deps.type() != Dependencies::evol_method)
3210 continue;
3211 Method* method = deps.method_argument(0);
3212 if (method == dependee) return true;
3213 }
3214 return false;
3215 }
3216
3217 void nmethod_init() {
3218 // make sure you didn't forget to adjust the filler fields
3219 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3220 }
3221
3222 // -----------------------------------------------------------------------------
3223 // Verification
3224
3225 class VerifyOopsClosure: public OopClosure {
3226 nmethod* _nm;
3227 bool _ok;
3228 public:
3229 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3230 bool ok() { return _ok; }
3231 virtual void do_oop(oop* p) {
3232 if (oopDesc::is_oop_or_null(*p)) return;
4030 return st.as_string();
4031 }
4032 }
4033 }
4034 return have_one ? "other" : nullptr;
4035 }
4036
4037 // Return the last scope in (begin..end]
4038 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4039 PcDesc* p = pc_desc_near(begin+1);
4040 if (p != nullptr && p->real_pc(this) <= end) {
4041 return new ScopeDesc(this, p);
4042 }
4043 return nullptr;
4044 }
4045
4046 const char* nmethod::nmethod_section_label(address pos) const {
4047 const char* label = nullptr;
4048 if (pos == code_begin()) label = "[Instructions begin]";
4049 if (pos == entry_point()) label = "[Entry Point]";
4050 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
4051 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
4052 // Check stub_code before checking exception_handler or deopt_handler.
4053 if (pos == this->stub_begin()) label = "[Stub Code]";
4054 if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin()) label = "[Exception Handler]";
4055 if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4056 return label;
4057 }
4058
4059 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4060 if (print_section_labels) {
4061 const char* label = nmethod_section_label(block_begin);
4062 if (label != nullptr) {
4063 stream->bol();
4064 stream->print_cr("%s", label);
4065 }
4066 }
4067
4068 if (block_begin == entry_point()) {
4069 Method* m = method();
4070 if (m != nullptr) {
4071 stream->print(" # ");
4072 m->print_value_on(stream);
4073 stream->cr();
4074 }
4075 if (m != nullptr && !is_osr_method()) {
4076 ResourceMark rm;
4077 int sizeargs = m->size_of_parameters();
4078 BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, sizeargs);
4079 VMRegPair* regs = NEW_RESOURCE_ARRAY(VMRegPair, sizeargs);
4080 {
4081 int sig_index = 0;
4082 if (!m->is_static())
4083 sig_bt[sig_index++] = T_OBJECT; // 'this'
4084 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ss.next()) {
4085 BasicType t = ss.type();
4086 sig_bt[sig_index++] = t;
4087 if (type2size[t] == 2) {
4088 sig_bt[sig_index++] = T_VOID;
4089 } else {
4090 assert(type2size[t] == 1, "size is 1 or 2");
4091 }
4092 }
4093 assert(sig_index == sizeargs, "");
4094 }
4095 const char* spname = "sp"; // make arch-specific?
4096 SharedRuntime::java_calling_convention(sig_bt, regs, sizeargs);
4097 int stack_slot_offset = this->frame_size() * wordSize;
4098 int tab1 = 14, tab2 = 24;
4099 int sig_index = 0;
4100 int arg_index = (m->is_static() ? 0 : -1);
4101 bool did_old_sp = false;
4102 for (SignatureStream ss(m->signature()); !ss.at_return_type(); ) {
4103 bool at_this = (arg_index == -1);
4104 bool at_old_sp = false;
4105 BasicType t = (at_this ? T_OBJECT : ss.type());
4106 assert(t == sig_bt[sig_index], "sigs in sync");
4107 if (at_this)
4108 stream->print(" # this: ");
4109 else
4110 stream->print(" # parm%d: ", arg_index);
4111 stream->move_to(tab1);
4112 VMReg fst = regs[sig_index].first();
4113 VMReg snd = regs[sig_index].second();
4114 if (fst->is_reg()) {
4115 stream->print("%s", fst->name());
4116 if (snd->is_valid()) {
4117 stream->print(":%s", snd->name());
4118 }
4119 } else if (fst->is_stack()) {
4120 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4121 if (offset == stack_slot_offset) at_old_sp = true;
4122 stream->print("[%s+0x%x]", spname, offset);
4123 } else {
4124 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4125 }
4126 stream->print(" ");
4127 stream->move_to(tab2);
4128 stream->print("= ");
4129 if (at_this) {
4130 m->method_holder()->print_value_on(stream);
4131 } else {
4132 bool did_name = false;
4133 if (!at_this && ss.is_reference()) {
4134 Symbol* name = ss.as_symbol();
4135 name->print_value_on(stream);
4136 did_name = true;
4137 }
4138 if (!did_name)
4139 stream->print("%s", type2name(t));
4140 }
4141 if (at_old_sp) {
4142 stream->print(" (%s of caller)", spname);
4143 did_old_sp = true;
4144 }
4145 stream->cr();
4146 sig_index += type2size[t];
4147 arg_index += 1;
4148 if (!at_this) ss.next();
4149 }
4150 if (!did_old_sp) {
4151 stream->print(" # ");
4152 stream->move_to(tab1);
4153 stream->print("[%s+0x%x]", spname, stack_slot_offset);
4154 stream->print(" (%s of caller)", spname);
4155 stream->cr();
4156 }
4157 }
4158 }
4159 }
4160
4161 // Returns whether this nmethod has code comments.
4162 bool nmethod::has_code_comment(address begin, address end) {
4163 // scopes?
4164 ScopeDesc* sd = scope_desc_in(begin, end);
4165 if (sd != nullptr) return true;
4166
4167 // relocations?
4168 const char* str = reloc_string_for(begin, end);
4169 if (str != nullptr) return true;
4170
4171 // implicit exceptions?
4172 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4173 if (cont_offset != 0) return true;
4174
4175 return false;
4176 }
4177
4261 else
4262 st->print("<UNKNOWN>");
4263 break;
4264 }
4265 case Bytecodes::_getfield:
4266 case Bytecodes::_putfield:
4267 case Bytecodes::_getstatic:
4268 case Bytecodes::_putstatic:
4269 {
4270 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4271 st->print(" ");
4272 if (field.name() != nullptr)
4273 field.name()->print_symbol_on(st);
4274 else
4275 st->print("<UNKNOWN>");
4276 }
4277 default:
4278 break;
4279 }
4280 }
4281 st->print(" {reexecute=%d rethrow=%d return_oop=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop());
4282 }
4283
4284 // Print all scopes
4285 for (;sd != nullptr; sd = sd->sender()) {
4286 st->move_to(column, 6, 0);
4287 st->print("; -");
4288 if (sd->should_reexecute()) {
4289 st->print(" (reexecute)");
4290 }
4291 if (sd->method() == nullptr) {
4292 st->print("method is nullptr");
4293 } else {
4294 sd->method()->print_short_name(st);
4295 }
4296 int lineno = sd->method()->line_number_from_bci(sd->bci());
4297 if (lineno != -1) {
4298 st->print("@%d (line %d)", sd->bci(), lineno);
4299 } else {
4300 st->print("@%d", sd->bci());
4301 }
|
1 /*
2 * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
696
697 // handle the case of an anchor explicitly set in continuation code that doesn't have a callee
698 JavaThread* thread = reg_map->thread();
699 if ((thread->has_last_Java_frame() && fr.sp() == thread->last_Java_sp())
700 JVMTI_ONLY(|| (method()->is_continuation_enter_intrinsic() && thread->on_monitor_waited_event()))) {
701 return;
702 }
703
704 if (!method()->is_native()) {
705 address pc = fr.pc();
706 bool has_receiver, has_appendix;
707 Symbol* signature;
708
709 // The method attached by JIT-compilers should be used, if present.
710 // Bytecode can be inaccurate in such case.
711 Method* callee = attached_method_before_pc(pc);
712 if (callee != nullptr) {
713 has_receiver = !(callee->access_flags().is_static());
714 has_appendix = false;
715 signature = callee->signature();
716
717 // If inline types are passed as fields, use the extended signature
718 // which contains the types of all (oop) fields of the inline type.
719 if (is_compiled_by_c2() && callee->has_scalarized_args()) {
720 const GrowableArray<SigEntry>* sig = callee->adapter()->get_sig_cc();
721 assert(sig != nullptr, "sig should never be null");
722 TempNewSymbol tmp_sig = SigEntry::create_symbol(sig);
723 has_receiver = false; // The extended signature contains the receiver type
724 fr.oops_compiled_arguments_do(tmp_sig, has_receiver, has_appendix, reg_map, f);
725 return;
726 }
727 } else {
728 SimpleScopeDesc ssd(this, pc);
729
730 Bytecode_invoke call(methodHandle(Thread::current(), ssd.method()), ssd.bci());
731 has_receiver = call.has_receiver();
732 has_appendix = call.has_appendix();
733 signature = call.signature();
734 }
735
736 fr.oops_compiled_arguments_do(signature, has_receiver, has_appendix, reg_map, f);
737 } else if (method()->is_continuation_enter_intrinsic()) {
738 // This method only calls Continuation.enter()
739 Symbol* signature = vmSymbols::continuationEnter_signature();
740 fr.oops_compiled_arguments_do(signature, false, false, reg_map, f);
741 }
742 }
743
744 Method* nmethod::attached_method(address call_instr) {
745 assert(code_contains(call_instr), "not part of the nmethod");
746 RelocIterator iter(this, call_instr, call_instr + 1);
1237
1238 _has_unsafe_access = 0;
1239 _has_wide_vectors = 0;
1240 _has_monitors = 0;
1241 _has_scoped_access = 0;
1242 _has_flushed_dependencies = 0;
1243 _is_unlinked = 0;
1244 _load_reported = 0; // jvmti state
1245
1246 _deoptimization_status = not_marked;
1247
1248 // SECT_CONSTS is first in code buffer so the offset should be 0.
1249 int consts_offset = code_buffer->total_offset_of(code_buffer->consts());
1250 assert(consts_offset == 0, "const_offset: %d", consts_offset);
1251
1252 _stub_offset = content_offset() + code_buffer->total_offset_of(code_buffer->stubs());
1253
1254 CHECKED_CAST(_entry_offset, uint16_t, (offsets->value(CodeOffsets::Entry)));
1255 CHECKED_CAST(_verified_entry_offset, uint16_t, (offsets->value(CodeOffsets::Verified_Entry)));
1256
1257 _inline_entry_offset = _entry_offset;
1258 _verified_inline_entry_offset = _verified_entry_offset;
1259 _verified_inline_ro_entry_offset = _verified_entry_offset;
1260
1261 _skipped_instructions_size = code_buffer->total_skipped_instructions_size();
1262 }
1263
1264 // Post initialization
1265 void nmethod::post_init() {
1266 clear_unloading_state();
1267
1268 finalize_relocations();
1269
1270 Universe::heap()->register_nmethod(this);
1271 DEBUG_ONLY(Universe::heap()->verify_nmethod(this));
1272
1273 CodeCache::commit(this);
1274 }
1275
1276 // For native wrappers
1277 nmethod::nmethod(
1278 Method* method,
1279 CompilerType type,
1280 int nmethod_size,
1281 int compile_id,
1282 CodeOffsets* offsets,
1283 CodeBuffer* code_buffer,
1284 int frame_size,
1285 ByteSize basic_lock_owner_sp_offset,
1286 ByteSize basic_lock_sp_offset,
1287 OopMapSet* oop_maps,
1288 int mutable_data_size)
1289 : CodeBlob("native nmethod", CodeBlobKind::Nmethod, code_buffer, nmethod_size, sizeof(nmethod),
1290 offsets->value(CodeOffsets::Frame_Complete), frame_size, oop_maps, false, mutable_data_size),
1291 _deoptimization_generation(0),
1292 _gc_epoch(CodeCache::gc_epoch()),
1293 _method(method),
1294 _native_receiver_sp_offset(basic_lock_owner_sp_offset),
1295 _native_basic_lock_sp_offset(basic_lock_sp_offset)
1296 {
1297 {
1298 DEBUG_ONLY(NoSafepointVerifier nsv;)
1299 assert_locked_or_safepoint(CodeCache_lock);
1300 assert(!method->has_scalarized_args(), "scalarized native wrappers not supported yet");
1301 init_defaults(code_buffer, offsets);
1302
1303 _osr_entry_point = nullptr;
1304 _pc_desc_container = nullptr;
1305 _entry_bci = InvocationEntryBci;
1306 _compile_id = compile_id;
1307 _comp_level = CompLevel_none;
1308 _compiler_type = type;
1309 _orig_pc_offset = 0;
1310 _num_stack_arg_slots = 0;
1311
1312 if (offsets->value(CodeOffsets::Exceptions) != -1) {
1313 // Continuation enter intrinsic
1314 _exception_offset = code_offset() + offsets->value(CodeOffsets::Exceptions);
1315 } else {
1316 _exception_offset = 0;
1317 }
1318 // Native wrappers do not have deopt handlers. Make the values
1319 // something that will never match a pc like the nmethod vtable entry
1320 _deopt_handler_entry_offset = 0;
1426 _mutable_data = (address)os::malloc(_mutable_data_size, mtCode);
1427 if (_mutable_data == nullptr) {
1428 vm_exit_out_of_memory(_mutable_data_size, OOM_MALLOC_ERROR, "nmethod: no space for mutable data");
1429 }
1430 memcpy(mutable_data_begin(), nm.mutable_data_begin(), nm.mutable_data_size());
1431 } else {
1432 _mutable_data = nullptr;
1433 }
1434
1435 _deoptimization_generation = 0;
1436 _gc_epoch = CodeCache::gc_epoch();
1437 _method = nm._method;
1438 _osr_link = nullptr;
1439
1440 _exception_cache = nullptr;
1441 _gc_data = nullptr;
1442 _oops_do_mark_nmethods = nullptr;
1443 _oops_do_mark_link = nullptr;
1444 _compiled_ic_data = nullptr;
1445
1446 // Relocate the OSR entry point from nm to the new nmethod.
1447 if (nm._osr_entry_point == nullptr) {
1448 _osr_entry_point = nullptr;
1449 } else {
1450 address new_addr = nm._osr_entry_point - (address) &nm + (address) this;
1451 assert(new_addr >= code_begin() && new_addr < code_end(),
1452 "relocated address must be within code bounds");
1453 _osr_entry_point = new_addr;
1454 }
1455 _entry_offset = nm._entry_offset;
1456 _verified_entry_offset = nm._verified_entry_offset;
1457 _inline_entry_offset = nm._inline_entry_offset;
1458 _verified_inline_entry_offset = nm._verified_inline_entry_offset;
1459 _verified_inline_ro_entry_offset = nm._verified_inline_ro_entry_offset;
1460
1461 _entry_bci = nm._entry_bci;
1462 _immutable_data_size = nm._immutable_data_size;
1463
1464 _skipped_instructions_size = nm._skipped_instructions_size;
1465 _stub_offset = nm._stub_offset;
1466 _exception_offset = nm._exception_offset;
1467 _deopt_handler_entry_offset = nm._deopt_handler_entry_offset;
1468 _unwind_handler_offset = nm._unwind_handler_offset;
1469 _num_stack_arg_slots = nm._num_stack_arg_slots;
1470 _oops_size = nm._oops_size;
1471 #if INCLUDE_JVMCI
1472 _metadata_size = nm._metadata_size;
1473 #endif
1474 _nul_chk_table_offset = nm._nul_chk_table_offset;
1475 _handler_table_offset = nm._handler_table_offset;
1476 _scopes_pcs_offset = nm._scopes_pcs_offset;
1477 _scopes_data_offset = nm._scopes_data_offset;
1478 #if INCLUDE_JVMCI
1479 _speculations_offset = nm._speculations_offset;
1480 #endif
1758 _exception_offset = -1;
1759 }
1760
1761 _deopt_handler_entry_offset = _stub_offset + offsets->value(CodeOffsets::Deopt);
1762 }
1763 if (offsets->value(CodeOffsets::UnwindHandler) != -1) {
1764 // C1 generates UnwindHandler at the end of instructions section.
1765 // Calculate positive offset as distance between the start of stubs section
1766 // (which is also the end of instructions section) and the start of the handler.
1767 int unwind_handler_offset = code_offset() + offsets->value(CodeOffsets::UnwindHandler);
1768 CHECKED_CAST(_unwind_handler_offset, int16_t, (_stub_offset - unwind_handler_offset));
1769 } else {
1770 _unwind_handler_offset = -1;
1771 }
1772
1773 CHECKED_CAST(_oops_size, uint16_t, align_up(code_buffer->total_oop_size(), oopSize));
1774 uint16_t metadata_size;
1775 CHECKED_CAST(metadata_size, uint16_t, align_up(code_buffer->total_metadata_size(), wordSize));
1776 JVMCI_ONLY( _metadata_size = metadata_size; )
1777 int jvmci_data_size = 0 JVMCI_ONLY( + align_up(compiler->is_jvmci() ? jvmci_data->size() : 0, oopSize));
1778 if (offsets->value(CodeOffsets::Inline_Entry) != CodeOffsets::no_such_entry_point) {
1779 CHECKED_CAST(_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Inline_Entry));
1780 }
1781 if (offsets->value(CodeOffsets::Verified_Inline_Entry) != CodeOffsets::no_such_entry_point) {
1782 CHECKED_CAST(_verified_inline_entry_offset , uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry));
1783 }
1784 if (offsets->value(CodeOffsets::Verified_Inline_Entry_RO) != CodeOffsets::no_such_entry_point) {
1785 CHECKED_CAST(_verified_inline_ro_entry_offset, uint16_t, offsets->value(CodeOffsets::Verified_Inline_Entry_RO));
1786 }
1787
1788 assert(_mutable_data_size == _relocation_size + metadata_size + jvmci_data_size,
1789 "wrong mutable data size: %d != %d + %d + %d",
1790 _mutable_data_size, _relocation_size, metadata_size, jvmci_data_size);
1791 assert(nmethod_size == data_end() - header_begin(), "wrong nmethod size: %d != %d",
1792 nmethod_size, (int)(code_end() - header_begin()));
1793
1794 _immutable_data_size = immutable_data_size;
1795 if (immutable_data_size > 0) {
1796 assert(immutable_data != nullptr, "required");
1797 _immutable_data = immutable_data;
1798 } else {
1799 // We need unique not null address
1800 _immutable_data = blob_end();
1801 }
1802 CHECKED_CAST(_nul_chk_table_offset, uint16_t, (align_up((int)dependencies->size_in_bytes(), oopSize)));
1803 CHECKED_CAST(_handler_table_offset, uint16_t, (_nul_chk_table_offset + align_up(nul_chk_table->size_in_bytes(), oopSize)));
1804 _scopes_pcs_offset = _handler_table_offset + align_up(handler_table->size_in_bytes(), oopSize);
1805 _scopes_data_offset = _scopes_pcs_offset + adjust_pcs_size(debug_info->pcs_size());
1806
1807 #if INCLUDE_JVMCI
3221 }
3222
3223 bool nmethod::check_dependency_on(DepChange& changes) {
3224 // What has happened:
3225 // 1) a new class dependee has been added
3226 // 2) dependee and all its super classes have been marked
3227 bool found_check = false; // set true if we are upset
3228 for (Dependencies::DepStream deps(this); deps.next(); ) {
3229 // Evaluate only relevant dependencies.
3230 if (deps.spot_check_dependency_at(changes) != nullptr) {
3231 found_check = true;
3232 NOT_DEBUG(break);
3233 }
3234 }
3235 return found_check;
3236 }
3237
3238 // Called from mark_for_deoptimization, when dependee is invalidated.
3239 bool nmethod::is_dependent_on_method(Method* dependee) {
3240 for (Dependencies::DepStream deps(this); deps.next(); ) {
3241 if (Dependencies::has_method_dep(deps.type())) {
3242 Method* method = deps.method_argument(0);
3243 if (method == dependee) return true;
3244 }
3245 }
3246 return false;
3247 }
3248
3249 void nmethod_init() {
3250 // make sure you didn't forget to adjust the filler fields
3251 assert(sizeof(nmethod) % oopSize == 0, "nmethod size must be multiple of a word");
3252 }
3253
3254 // -----------------------------------------------------------------------------
3255 // Verification
3256
3257 class VerifyOopsClosure: public OopClosure {
3258 nmethod* _nm;
3259 bool _ok;
3260 public:
3261 VerifyOopsClosure(nmethod* nm) : _nm(nm), _ok(true) { }
3262 bool ok() { return _ok; }
3263 virtual void do_oop(oop* p) {
3264 if (oopDesc::is_oop_or_null(*p)) return;
4062 return st.as_string();
4063 }
4064 }
4065 }
4066 return have_one ? "other" : nullptr;
4067 }
4068
4069 // Return the last scope in (begin..end]
4070 ScopeDesc* nmethod::scope_desc_in(address begin, address end) {
4071 PcDesc* p = pc_desc_near(begin+1);
4072 if (p != nullptr && p->real_pc(this) <= end) {
4073 return new ScopeDesc(this, p);
4074 }
4075 return nullptr;
4076 }
4077
4078 const char* nmethod::nmethod_section_label(address pos) const {
4079 const char* label = nullptr;
4080 if (pos == code_begin()) label = "[Instructions begin]";
4081 if (pos == entry_point()) label = "[Entry Point]";
4082 if (pos == inline_entry_point()) label = "[Inline Entry Point]";
4083 if (pos == verified_entry_point()) label = "[Verified Entry Point]";
4084 if (pos == verified_inline_entry_point()) label = "[Verified Inline Entry Point]";
4085 if (pos == verified_inline_ro_entry_point()) label = "[Verified Inline Entry Point (RO)]";
4086 if (pos == consts_begin() && pos != insts_begin()) label = "[Constants]";
4087 // Check stub_code before checking exception_handler or deopt_handler.
4088 if (pos == this->stub_begin()) label = "[Stub Code]";
4089 if (JVMCI_ONLY(_exception_offset >= 0 &&) pos == exception_begin()) label = "[Exception Handler]";
4090 if (JVMCI_ONLY(_deopt_handler_entry_offset != -1 &&) pos == deopt_handler_entry()) label = "[Deopt Handler Entry Point]";
4091 return label;
4092 }
4093
4094 static int maybe_print_entry_label(outputStream* stream, address pos, address entry, const char* label) {
4095 if (pos == entry) {
4096 stream->bol();
4097 stream->print_cr("%s", label);
4098 return 1;
4099 } else {
4100 return 0;
4101 }
4102 }
4103
4104 void nmethod::print_nmethod_labels(outputStream* stream, address block_begin, bool print_section_labels) const {
4105 if (print_section_labels) {
4106 int n = 0;
4107 // Multiple entry points may be at the same position. Print them all.
4108 n += maybe_print_entry_label(stream, block_begin, entry_point(), "[Entry Point]");
4109 n += maybe_print_entry_label(stream, block_begin, inline_entry_point(), "[Inline Entry Point]");
4110 n += maybe_print_entry_label(stream, block_begin, verified_entry_point(), "[Verified Entry Point]");
4111 n += maybe_print_entry_label(stream, block_begin, verified_inline_entry_point(), "[Verified Inline Entry Point]");
4112 n += maybe_print_entry_label(stream, block_begin, verified_inline_ro_entry_point(), "[Verified Inline Entry Point (RO)]");
4113 if (n == 0) {
4114 const char* label = nmethod_section_label(block_begin);
4115 if (label != nullptr) {
4116 stream->bol();
4117 stream->print_cr("%s", label);
4118 }
4119 }
4120 }
4121
4122 Method* m = method();
4123 if (m == nullptr || is_osr_method()) {
4124 return;
4125 }
4126
4127 // Print the name of the method (only once)
4128 address low = MIN3(entry_point(),
4129 verified_entry_point(),
4130 inline_entry_point());
4131 // The verified inline entry point and verified inline RO entry point are not always
4132 // used. When they are unused. CodeOffsets::Verified_Inline_Entry(_RO) is -1. Hence,
4133 // the calculated entry point is smaller than the block they are offsetting into.
4134 if (verified_inline_entry_point() >= block_begin) {
4135 low = MIN2(low, verified_inline_entry_point());
4136 }
4137 if (verified_inline_ro_entry_point() >= block_begin) {
4138 low = MIN2(low, verified_inline_ro_entry_point());
4139 }
4140 assert(low != nullptr, "sanity");
4141 if (block_begin == low) {
4142 stream->print(" # ");
4143 m->print_value_on(stream);
4144 stream->cr();
4145 }
4146
4147 // Print the arguments for the 3 types of verified entry points
4148 CompiledEntrySignature ces(m);
4149 ces.compute_calling_conventions(false);
4150 const GrowableArray<SigEntry>* sig_cc;
4151 const VMRegPair* regs;
4152 if (block_begin == verified_entry_point()) {
4153 sig_cc = ces.sig_cc();
4154 regs = ces.regs_cc();
4155 } else if (block_begin == verified_inline_entry_point()) {
4156 sig_cc = ces.sig();
4157 regs = ces.regs();
4158 } else if (block_begin == verified_inline_ro_entry_point()) {
4159 sig_cc = ces.sig_cc_ro();
4160 regs = ces.regs_cc_ro();
4161 } else {
4162 return;
4163 }
4164
4165 bool has_this = !m->is_static();
4166 if (ces.has_inline_recv() && block_begin == verified_entry_point()) {
4167 // <this> argument is scalarized for verified_entry_point()
4168 has_this = false;
4169 }
4170 const char* spname = "sp"; // make arch-specific?
4171 int stack_slot_offset = this->frame_size() * wordSize;
4172 int tab1 = 14, tab2 = 24;
4173 int sig_index = 0;
4174 int arg_index = has_this ? -1 : 0;
4175 bool did_old_sp = false;
4176 for (ExtendedSignature sig = ExtendedSignature(sig_cc, SigEntryFilter()); !sig.at_end(); ++sig) {
4177 bool at_this = (arg_index == -1);
4178 bool at_old_sp = false;
4179 BasicType t = (*sig)._bt;
4180 if (at_this) {
4181 stream->print(" # this: ");
4182 } else {
4183 stream->print(" # parm%d: ", arg_index);
4184 }
4185 stream->move_to(tab1);
4186 VMReg fst = regs[sig_index].first();
4187 VMReg snd = regs[sig_index].second();
4188 if (fst->is_reg()) {
4189 stream->print("%s", fst->name());
4190 if (snd->is_valid()) {
4191 stream->print(":%s", snd->name());
4192 }
4193 } else if (fst->is_stack()) {
4194 int offset = fst->reg2stack() * VMRegImpl::stack_slot_size + stack_slot_offset;
4195 if (offset == stack_slot_offset) at_old_sp = true;
4196 stream->print("[%s+0x%x]", spname, offset);
4197 } else {
4198 stream->print("reg%d:%d??", (int)(intptr_t)fst, (int)(intptr_t)snd);
4199 }
4200 stream->print(" ");
4201 stream->move_to(tab2);
4202 stream->print("= ");
4203 if (at_this) {
4204 m->method_holder()->print_value_on(stream);
4205 } else {
4206 bool did_name = false;
4207 if (is_reference_type(t)) {
4208 Symbol* name = (*sig)._name;
4209 name->print_value_on(stream);
4210 did_name = true;
4211 }
4212 if (!did_name)
4213 stream->print("%s", type2name(t));
4214 if ((*sig)._null_marker) {
4215 stream->print(" (null marker)");
4216 }
4217 }
4218 if (at_old_sp) {
4219 stream->print(" (%s of caller)", spname);
4220 did_old_sp = true;
4221 }
4222 stream->cr();
4223 sig_index += type2size[t];
4224 arg_index += 1;
4225 }
4226 if (!did_old_sp) {
4227 stream->print(" # ");
4228 stream->move_to(tab1);
4229 stream->print("[%s+0x%x]", spname, stack_slot_offset);
4230 stream->print(" (%s of caller)", spname);
4231 stream->cr();
4232 }
4233 }
4234
4235 // Returns whether this nmethod has code comments.
4236 bool nmethod::has_code_comment(address begin, address end) {
4237 // scopes?
4238 ScopeDesc* sd = scope_desc_in(begin, end);
4239 if (sd != nullptr) return true;
4240
4241 // relocations?
4242 const char* str = reloc_string_for(begin, end);
4243 if (str != nullptr) return true;
4244
4245 // implicit exceptions?
4246 int cont_offset = ImplicitExceptionTable(this).continuation_offset((uint)(begin - code_begin()));
4247 if (cont_offset != 0) return true;
4248
4249 return false;
4250 }
4251
4335 else
4336 st->print("<UNKNOWN>");
4337 break;
4338 }
4339 case Bytecodes::_getfield:
4340 case Bytecodes::_putfield:
4341 case Bytecodes::_getstatic:
4342 case Bytecodes::_putstatic:
4343 {
4344 Bytecode_field field(methodHandle(thread, sd->method()), sd->bci());
4345 st->print(" ");
4346 if (field.name() != nullptr)
4347 field.name()->print_symbol_on(st);
4348 else
4349 st->print("<UNKNOWN>");
4350 }
4351 default:
4352 break;
4353 }
4354 }
4355 st->print(" {reexecute=%d rethrow=%d return_oop=%d return_scalarized=%d}", sd->should_reexecute(), sd->rethrow_exception(), sd->return_oop(), sd->return_scalarized());
4356 }
4357
4358 // Print all scopes
4359 for (;sd != nullptr; sd = sd->sender()) {
4360 st->move_to(column, 6, 0);
4361 st->print("; -");
4362 if (sd->should_reexecute()) {
4363 st->print(" (reexecute)");
4364 }
4365 if (sd->method() == nullptr) {
4366 st->print("method is nullptr");
4367 } else {
4368 sd->method()->print_short_name(st);
4369 }
4370 int lineno = sd->method()->line_number_from_bci(sd->bci());
4371 if (lineno != -1) {
4372 st->print("@%d (line %d)", sd->bci(), lineno);
4373 } else {
4374 st->print("@%d", sd->bci());
4375 }
|