25
#include "precompiled.hpp"
26
#include "compiler/oopMap.hpp"
27
#include "interpreter/interpreter.hpp"
28
#include "memory/resourceArea.hpp"
29
#include "memory/universe.hpp"
30
#include "oops/markWord.hpp"
31
#include "oops/method.hpp"
32
#include "oops/oop.inline.hpp"
33
#include "prims/methodHandles.hpp"
34
#include "runtime/continuation.hpp"
35
#include "runtime/frame.inline.hpp"
36
#include "runtime/handles.inline.hpp"
37
#include "runtime/javaCalls.hpp"
38
#include "runtime/monitorChunk.hpp"
39
#include "runtime/signature.hpp"
40
#include "runtime/stackWatermarkSet.hpp"
41
#include "runtime/stubCodeGenerator.hpp"
42
#include "runtime/stubRoutines.hpp"
43
#include "vmreg_x86.inline.hpp"
44
#include "utilities/formatBuffer.hpp"
46
#include "c1/c1_Runtime1.hpp"
47
#include "runtime/vframeArray.hpp"
51
void RegisterMap::check_location_valid() {
57
bool frame::safe_for_sender(JavaThread *thread) {
58
if (is_heap_frame()) {
61
address sp = (address)_sp;
62
address fp = (address)_fp;
63
address unextended_sp = (address)_unextended_sp;
67
if (!thread->is_in_usable_stack(sp)) {
75
if (!thread->is_in_stack_range_incl(unextended_sp, sp - Interpreter::stackElementSize)) {
81
bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
82
thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
90
if (_cb != nullptr ) {
97
if (!_cb->is_frame_complete_at(_pc)) {
98
if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
104
if (!_cb->code_contains(_pc)) {
109
if (is_entry_frame()) {
111
return fp_safe && is_entry_frame_valid(thread);
112
} else if (is_upcall_stub_frame()) {
116
intptr_t* sender_sp = nullptr;
117
intptr_t* sender_unextended_sp = nullptr;
118
address sender_pc = nullptr;
119
intptr_t* saved_fp = nullptr;
121
if (is_interpreted_frame()) {
127
sender_pc = (address) this->fp()[return_addr_offset];
131
sender_sp = (intptr_t*) addr_at(sender_sp_offset);
132
sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
133
saved_fp = (intptr_t*) this->fp()[link_offset];
140
if (_cb->frame_size() <= 0) {
144
sender_sp = _unextended_sp + _cb->frame_size();
146
if (!thread->is_in_full_stack_checked((address)sender_sp)) {
149
sender_unextended_sp = sender_sp;
151
sender_pc = (address) *(sender_sp-1);
153
saved_fp = (intptr_t*) *(sender_sp - frame::sender_sp_offset);
156
if (Continuation::is_return_barrier_entry(sender_pc)) {
158
frame s = Continuation::continuation_bottom_sender(thread, *this, sender_sp);
164
if (Interpreter::contains(sender_pc)) {
170
if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
176
frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
178
return sender.is_interpreted_frame_valid(thread);
183
CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
184
if (sender_pc == nullptr || sender_blob == nullptr) {
189
if (!sender_blob->code_contains(sender_pc)) {
194
if (sender_blob->is_adapter_blob()) {
199
if (StubRoutines::returns_to_call_stub(sender_pc)) {
200
if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
206
frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
209
address jcw = (address)sender.entry_frame_call_wrapper();
211
return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
212
} else if (sender_blob->is_upcall_stub()) {
216
nmethod* nm = sender_blob->as_nmethod_or_null();
218
if (nm->is_deopt_mh_entry(sender_pc) || nm->is_deopt_entry(sender_pc) ||
219
nm->method()->is_method_handle_intrinsic()) {
227
if (sender_blob->frame_size() <= 0) {
228
assert(!sender_blob->is_nmethod(), "should count return address at least");
237
if (!sender_blob->is_nmethod()) {
259
if ( (address) this->fp()[return_addr_offset] == nullptr) return false;
269
void frame::patch_pc(Thread* thread, address pc) {
270
assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
271
address* pc_addr = &(((address*) sp())[-1]);
273
if (TracePcPatching) {
274
tty->print_cr("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
275
p2i(pc_addr), p2i(*pc_addr), p2i(pc));
280
assert(!Continuation::is_return_barrier_entry(*pc_addr), "return barrier");
282
assert(_pc == *pc_addr || pc == *pc_addr || *pc_addr == 0, "");
283
DEBUG_ONLY(address old_pc = _pc;)
286
address original_pc = get_deopt_original_pc();
287
if (original_pc != nullptr) {
288
assert(original_pc == old_pc, "expected original PC to be stored before patching");
289
_deopt_state = is_deoptimized;
292
_deopt_state = not_deoptimized;
294
assert(!is_compiled_frame() || !_cb->as_nmethod()->is_deopt_entry(_pc), "must be");
298
frame f(this->sp(), this->unextended_sp(), this->fp(), pc);
299
assert(f.is_deoptimized_frame() == this->is_deoptimized_frame() && f.pc() == this->pc() && f.raw_pc() == this->raw_pc(),
300
"must be (f.is_deoptimized_frame(): %d this->is_deoptimized_frame(): %d "
301
"f.pc(): " INTPTR_FORMAT " this->pc(): " INTPTR_FORMAT " f.raw_pc(): " INTPTR_FORMAT " this->raw_pc(): " INTPTR_FORMAT ")",
302
f.is_deoptimized_frame(), this->is_deoptimized_frame(), p2i(f.pc()), p2i(this->pc()), p2i(f.raw_pc()), p2i(this->raw_pc()));
307
intptr_t* frame::entry_frame_argument_at(int offset) const {
309
int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
311
return &unextended_sp()[index];
316
void frame::interpreter_frame_set_locals(intptr_t* locs) {
317
assert(is_interpreted_frame(), "interpreted frame expected");
319
ptr_at_put(interpreter_frame_locals_offset, (intptr_t) (locs - fp()));
324
intptr_t* frame::interpreter_frame_sender_sp() const {
325
assert(is_interpreted_frame(), "interpreted frame expected");
326
return (intptr_t*) at(interpreter_frame_sender_sp_offset);
329
void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
330
assert(is_interpreted_frame(), "interpreted frame expected");
331
ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
337
BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
338
return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
341
BasicObjectLock* frame::interpreter_frame_monitor_end() const {
342
BasicObjectLock* result = (BasicObjectLock*) at_relative(interpreter_frame_monitor_block_top_offset);
344
assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
345
assert((intptr_t*) result < fp(), "monitor end should be strictly below the frame pointer: result: " INTPTR_FORMAT " fp: " INTPTR_FORMAT, p2i(result), p2i(fp()));
349
void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
350
assert(is_interpreted_frame(), "interpreted frame expected");
352
ptr_at_put(interpreter_frame_monitor_block_top_offset, (intptr_t*)value - fp());
353
assert(at_absolute(interpreter_frame_monitor_block_top_offset) <= interpreter_frame_monitor_block_top_offset, "");
357
void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
358
assert(is_interpreted_frame(), "interpreted frame expected");
360
ptr_at_put(interpreter_frame_last_sp_offset, sp != nullptr ? (sp - fp()) : 0);
363
frame frame::sender_for_entry_frame(RegisterMap* map) const {
364
assert(map != nullptr, "map must be set");
367
JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
368
assert(!entry_frame_is_first(), "next Java fp must be non zero");
369
assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
372
jfa->make_walkable();
374
assert(map->include_argument_oops(), "should be set by clear");
375
frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
380
UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
381
assert(frame.is_upcall_stub_frame(), "wrong frame");
383
return reinterpret_cast<UpcallStub::FrameData*>(
384
reinterpret_cast<address>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
387
bool frame::upcall_stub_frame_is_first() const {
388
assert(is_upcall_stub_frame(), "must be optimzed entry frame");
389
UpcallStub* blob = _cb->as_upcall_stub();
390
JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
391
return jfa->last_Java_sp() == nullptr;
394
frame frame::sender_for_upcall_stub_frame(RegisterMap* map) const {
395
assert(map != nullptr, "map must be set");
396
UpcallStub* blob = _cb->as_upcall_stub();
399
JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
400
assert(!upcall_stub_frame_is_first(), "must have a frame anchor to go back to");
401
assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
404
jfa->make_walkable();
406
assert(map->include_argument_oops(), "should be set by clear");
407
frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
418
void frame::verify_deopt_original_pc(nmethod* nm, intptr_t* unextended_sp) {
424
fr._unextended_sp = unextended_sp;
426
address original_pc = nm->get_original_pc(&fr);
427
assert(nm->insts_contains_inclusive(original_pc),
428
"original PC must be in the main code section of the compiled method (or must be immediately following it) original_pc: " INTPTR_FORMAT " unextended_sp: " INTPTR_FORMAT " name: %s", p2i(original_pc), p2i(unextended_sp), nm->name());
435
void frame::adjust_unextended_sp() {
440
if (_cb != nullptr) {
441
nmethod* sender_nm = _cb->as_nmethod_or_null();
442
if (sender_nm != nullptr) {
444
if (sender_nm->is_deopt_entry(_pc) ||
445
sender_nm->is_deopt_mh_entry(_pc)) {
446
verify_deopt_original_pc(sender_nm, _unextended_sp);
455
frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
458
intptr_t* sender_sp = this->sender_sp();
461
intptr_t* unextended_sp = interpreter_frame_sender_sp();
462
intptr_t* sender_fp = link();
464
#if COMPILER2_OR_JVMCI
465
if (map->update_map()) {
466
update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
470
address sender_pc = this->sender_pc();
472
if (Continuation::is_return_barrier_entry(sender_pc)) {
473
if (map->walk_cont()) {
474
return Continuation::top_frame(*this, map);
476
return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
480
return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
483
bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
484
assert(is_interpreted_frame(), "Not an interpreted frame");
486
if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
489
if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
492
if (fp() + interpreter_frame_initial_sp_offset < sp()) {
504
Method* m = safe_interpreter_frame_method();
507
if (!Method::is_valid_method(m)) return false;
514
if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
520
address bcp = interpreter_frame_bcp();
521
if (m->validate_bci_from_bcp(bcp) < 0) {
526
ConstantPoolCache* cp = *interpreter_frame_cache_addr();
527
if (MetaspaceObj::is_valid(cp) == false) return false;
531
address locals = (address)interpreter_frame_locals();
532
return thread->is_in_stack_range_incl(locals, (address)fp());
535
BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
536
assert(is_interpreted_frame(), "interpreted frame expected");
537
Method* method = interpreter_frame_method();
538
BasicType type = method->result_type();
541
if (method->is_native()) {
545
tos_addr = (intptr_t*)sp();
546
if (type == T_FLOAT || type == T_DOUBLE) {
551
tos_addr += 2 * Interpreter::stackElementWords;
557
tos_addr = (intptr_t*)interpreter_frame_tos_address();
564
if (method->is_native()) {
565
obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
567
oop* obj_p = (oop*)tos_addr;
568
obj = (obj_p == nullptr) ? (oop)nullptr : *obj_p;
570
assert(Universe::is_in_heap_or_null(obj), "sanity check");
574
case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
575
case T_BYTE : value_result->b = *(jbyte*)tos_addr; break;
576
case T_CHAR : value_result->c = *(jchar*)tos_addr; break;
577
case T_SHORT : value_result->s = *(jshort*)tos_addr; break;
578
case T_INT : value_result->i = *(jint*)tos_addr; break;
579
case T_LONG : value_result->j = *(jlong*)tos_addr; break;
582
value_result->f = *(jfloat*)tos_addr;
584
if (method->is_native()) {
585
jdouble d = *(jdouble*)tos_addr;
586
value_result->f = (jfloat)d;
588
value_result->f = *(jfloat*)tos_addr;
593
case T_DOUBLE : value_result->d = *(jdouble*)tos_addr; break;
595
default : ShouldNotReachHere();
601
intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
602
int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
603
return &interpreter_frame_tos_address()[index];
608
#define DESCRIBE_FP_OFFSET(name) \
609
values.describe(frame_no, fp() + frame::name##_offset, #name, 1)
611
void frame::describe_pd(FrameValues& values, int frame_no) {
612
if (is_interpreted_frame()) {
613
DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
614
DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
615
DESCRIBE_FP_OFFSET(interpreter_frame_method);
616
DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
617
DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
618
DESCRIBE_FP_OFFSET(interpreter_frame_cache);
619
DESCRIBE_FP_OFFSET(interpreter_frame_locals);
620
DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
621
DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
623
} else if (is_entry_frame()) {
627
for (int i = 0; i < entry_frame_after_call_words; i++) {
628
values.describe(frame_no, fp() - i, err_msg("call_stub word fp - %d", i));
633
if (is_java_frame() || Continuation::is_continuation_enterSpecial(*this)) {
634
intptr_t* ret_pc_loc;
636
if (is_interpreted_frame()) {
637
ret_pc_loc = fp() + return_addr_offset;
640
ret_pc_loc = real_fp() - return_addr_offset;
641
fp_loc = real_fp() - sender_sp_offset;
643
address ret_pc = *(address*)ret_pc_loc;
644
values.describe(frame_no, ret_pc_loc,
645
Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
646
values.describe(-1, fp_loc, "saved fp", 0);
652
intptr_t *frame::initial_deoptimization_info() {
659
frame::frame(void* sp, void* fp, void* pc) {
660
init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
665
void JavaFrameAnchor::make_walkable() {
667
if (last_Java_sp() == nullptr) return;
669
if (walkable()) return;
670
vmassert(last_Java_pc() == nullptr, "already walkable");
671
_last_Java_pc = (address)_last_Java_sp[-1];
672
vmassert(walkable(), "something went wrong");