2
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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/frame.inline.hpp"
35
#include "runtime/handles.inline.hpp"
36
#include "runtime/javaCalls.hpp"
37
#include "runtime/monitorChunk.hpp"
38
#include "runtime/os.inline.hpp"
39
#include "runtime/signature.hpp"
40
#include "runtime/stubCodeGenerator.hpp"
41
#include "runtime/stubRoutines.hpp"
42
#include "vmreg_arm.inline.hpp"
44
#include "c1/c1_Runtime1.hpp"
45
#include "runtime/vframeArray.hpp"
49
void RegisterMap::check_location_valid() {
54
// Profiling/safepoint support
56
bool frame::safe_for_sender(JavaThread *thread) {
57
address sp = (address)_sp;
58
address fp = (address)_fp;
59
address unextended_sp = (address)_unextended_sp;
61
// consider stack guards when trying to determine "safe" stack pointers
62
// sp must be within the usable part of the stack (not in guards)
63
if (!thread->is_in_usable_stack(sp)) {
67
if (!thread->is_in_stack_range_incl(unextended_sp, sp)) {
71
// We know sp/unextended_sp are safe. Only fp is questionable here.
73
bool fp_safe = thread->is_in_stack_range_incl(fp, sp);
75
if (_cb != nullptr ) {
77
// First check if frame is complete and tester is reliable
78
// Unfortunately we can only check frame complete for runtime stubs and nmethod
79
// other generic buffer blobs are more problematic so we just assume they are
80
// ok. adapter blobs never have a frame complete and are never ok.
82
if (!_cb->is_frame_complete_at(_pc)) {
83
if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
88
// Could just be some random pointer within the codeBlob
89
if (!_cb->code_contains(_pc)) {
94
if (is_entry_frame()) {
95
// an entry frame must have a valid fp.
96
return fp_safe && is_entry_frame_valid(thread);
99
intptr_t* sender_sp = nullptr;
100
address sender_pc = nullptr;
102
if (is_interpreted_frame()) {
108
sender_pc = (address) this->fp()[return_addr_offset];
109
sender_sp = (intptr_t*) addr_at(sender_sp_offset);
112
// must be some sort of compiled/runtime frame
113
// fp does not have to be safe (although it could be check for c1?)
115
sender_sp = _unextended_sp + _cb->frame_size();
116
// Is sender_sp safe?
117
if (!thread->is_in_full_stack_checked((address)sender_sp)) {
120
// With our calling conventions, the return_address should
121
// end up being the word on the stack
122
sender_pc = (address) *(sender_sp - sender_sp_offset + return_addr_offset);
125
// We must always be able to find a recognizable pc
126
CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
127
if (sender_pc == nullptr || sender_blob == nullptr) {
132
// If the potential sender is the interpreter then we can do some more checking
133
if (Interpreter::contains(sender_pc)) {
135
// FP is always saved in a recognizable place in any code we generate. However
136
// only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved FP
137
// is really a frame pointer.
139
intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);
140
if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
144
// construct the potential sender
146
frame sender(sender_sp, saved_fp, sender_pc);
148
return sender.is_interpreted_frame_valid(thread);
151
// Could just be some random pointer within the codeBlob
152
if (!sender_blob->code_contains(sender_pc)) {
156
// We should never be able to see an adapter if the current frame is something from code cache
157
if (sender_blob->is_adapter_blob()) {
161
// Could be the call_stub
162
if (StubRoutines::returns_to_call_stub(sender_pc)) {
163
intptr_t *saved_fp = (intptr_t*)*(sender_sp - frame::sender_sp_offset + link_offset);
164
if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
168
// construct the potential sender
170
frame sender(sender_sp, saved_fp, sender_pc);
172
// Validate the JavaCallWrapper an entry frame must have
173
address jcw = (address)sender.entry_frame_call_wrapper();
175
return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
178
// If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
179
// because the return address counts against the callee's frame.
181
if (sender_blob->frame_size() <= 0) {
182
assert(!sender_blob->is_nmethod(), "should count return address at least");
186
// We should never be able to see anything here except an nmethod. If something in the
187
// code cache (current frame) is called by an entity within the code cache that entity
188
// should not be anything but the call stub (already covered), the interpreter (already covered)
191
if (!sender_blob->is_nmethod()) {
195
// Could put some more validation for the potential non-interpreted sender
196
// frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
198
// One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
200
// We've validated the potential sender that would be created
204
// Must be native-compiled frame. Since sender will try and use fp to find
205
// linkages it must be safe
211
// Will the pc we fetch be non-zero (which we'll find at the oldest frame)
213
if ((address) this->fp()[return_addr_offset] == nullptr) return false;
216
// could try and do some more potential verification of native frame if we could think of some...
222
void frame::patch_pc(Thread* thread, address pc) {
223
assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
224
address* pc_addr = &((address *)sp())[-sender_sp_offset+return_addr_offset];
225
if (TracePcPatching) {
226
tty->print_cr("patch_pc at address" INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "] ",
227
p2i(pc_addr), p2i(*pc_addr), p2i(pc));
229
DEBUG_ONLY(address old_pc = _pc;)
231
_pc = pc; // must be set before call to get_deopt_original_pc
232
address original_pc = get_deopt_original_pc();
233
if (original_pc != nullptr) {
234
assert(original_pc == old_pc, "expected original PC to be stored before patching");
235
_deopt_state = is_deoptimized;
238
_deopt_state = not_deoptimized;
243
bool frame::is_interpreted_frame() const {
244
return Interpreter::contains(pc());
247
intptr_t* frame::entry_frame_argument_at(int offset) const {
248
assert(is_entry_frame(), "entry frame expected");
249
// convert offset to index to deal with tsi
250
int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
251
// Entry frame's arguments are always in relation to unextended_sp()
252
return &unextended_sp()[index];
257
void frame::interpreter_frame_set_locals(intptr_t* locs) {
258
assert(is_interpreted_frame(), "interpreted frame expected");
259
// set relativized locals
260
ptr_at_put(interpreter_frame_locals_offset, (intptr_t) (locs - fp()));
265
intptr_t* frame::interpreter_frame_sender_sp() const {
266
assert(is_interpreted_frame(), "interpreted frame expected");
267
return (intptr_t*) at(interpreter_frame_sender_sp_offset);
270
void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
271
assert(is_interpreted_frame(), "interpreted frame expected");
272
ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
278
BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
279
return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
282
BasicObjectLock* frame::interpreter_frame_monitor_end() const {
283
BasicObjectLock* result = (BasicObjectLock*) *addr_at(interpreter_frame_monitor_block_top_offset);
284
// make sure the pointer points inside the frame
285
assert((intptr_t) fp() > (intptr_t) result, "result must < than frame pointer");
286
assert((intptr_t) sp() <= (intptr_t) result, "result must >= than stack pointer");
290
void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
291
*((BasicObjectLock**)addr_at(interpreter_frame_monitor_block_top_offset)) = value;
295
// Used by template based interpreter deoptimization
296
void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
297
*((intptr_t**)addr_at(interpreter_frame_last_sp_offset)) = sp;
301
frame frame::sender_for_entry_frame(RegisterMap* map) const {
302
assert(map != nullptr, "map must be set");
303
// Java frame called from C; skip all C frames and return top C
304
// frame of that chunk as the sender
305
JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
306
assert(!entry_frame_is_first(), "next Java fp must be non zero");
307
assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
309
assert(map->include_argument_oops(), "should be set by clear");
310
if (jfa->last_Java_pc() != nullptr) {
311
frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
314
frame fr(jfa->last_Java_sp(), jfa->last_Java_fp());
318
UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
323
bool frame::upcall_stub_frame_is_first() const {
328
//------------------------------------------------------------------------------
329
// frame::verify_deopt_original_pc
331
// Verifies the calculated original PC of a deoptimization PC for the
332
// given unextended SP. The unextended SP might also be the saved SP
333
// for MethodHandle call sites.
335
void frame::verify_deopt_original_pc(nmethod* nm, intptr_t* unextended_sp, bool is_method_handle_return) {
338
// This is ugly but it's better than to change {get,set}_original_pc
339
// to take an SP value as argument. And it's only a debugging
341
fr._unextended_sp = unextended_sp;
343
address original_pc = nm->get_original_pc(&fr);
344
assert(nm->insts_contains_inclusive(original_pc),
345
"original PC must be in the main code section of the compiled method (or must be immediately following it)");
346
assert(nm->is_method_handle_return(original_pc) == is_method_handle_return, "must be");
350
//------------------------------------------------------------------------------
351
// frame::adjust_unextended_sp
352
void frame::adjust_unextended_sp() {
355
// If we are returning to a compiled MethodHandle call site, the
356
// saved_fp will in fact be a saved value of the unextended SP. The
357
// simplest way to tell whether we are returning to such a call site
360
nmethod* sender_nm = (_cb == nullptr) ? nullptr : _cb->as_nmethod_or_null();
361
if (sender_nm != nullptr) {
362
// If the sender PC is a deoptimization point, get the original
363
// PC. For MethodHandle call site the unextended_sp is stored in
365
if (sender_nm->is_deopt_mh_entry(_pc)) {
366
DEBUG_ONLY(verify_deopt_mh_original_pc(sender_nm, _fp));
367
_unextended_sp = _fp;
369
else if (sender_nm->is_deopt_entry(_pc)) {
370
DEBUG_ONLY(verify_deopt_original_pc(sender_nm, _unextended_sp));
372
else if (sender_nm->is_method_handle_return(_pc)) {
373
_unextended_sp = _fp;
378
//------------------------------------------------------------------------------
379
// frame::update_map_with_saved_link
380
void frame::update_map_with_saved_link(RegisterMap* map, intptr_t** link_addr) {
381
// see x86 for comments
382
map->set_location(FP->as_VMReg(), (address) link_addr);
385
frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
386
// SP is the raw SP from the sender after adapter or interpreter
388
intptr_t* sender_sp = this->sender_sp();
390
// This is the sp before any possible extension (adapter/locals).
391
intptr_t* unextended_sp = interpreter_frame_sender_sp();
394
if (map->update_map()) {
395
update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
399
return frame(sender_sp, unextended_sp, link(), sender_pc());
402
bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
403
assert(is_interpreted_frame(), "Not an interpreted frame");
404
// These are reasonable sanity checks
405
if (fp() == 0 || (intptr_t(fp()) & (wordSize-1)) != 0) {
408
if (sp() == 0 || (intptr_t(sp()) & (wordSize-1)) != 0) {
411
if (fp() + interpreter_frame_initial_sp_offset < sp()) {
414
// These are hacks to keep us out of trouble.
415
// The problem with these is that they mask other problems
416
if (fp() <= sp()) { // this attempts to deal with unsigned comparison above
419
// do some validation of frame elements
423
Method* m = safe_interpreter_frame_method();
425
// validate the method we'd find in this potential sender
426
if (!Method::is_valid_method(m)) return false;
428
// stack frames shouldn't be much larger than max_stack elements
430
if (fp() - sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
436
address bcp = interpreter_frame_bcp();
437
if (m->validate_bci_from_bcp(bcp) < 0) {
441
// validate ConstantPoolCache*
442
ConstantPoolCache* cp = *interpreter_frame_cache_addr();
443
if (MetaspaceObj::is_valid(cp) == false) return false;
447
address locals = (address)interpreter_frame_locals();
448
return thread->is_in_stack_range_incl(locals, (address)fp());
451
BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
452
assert(is_interpreted_frame(), "interpreted frame expected");
453
Method* method = interpreter_frame_method();
454
BasicType type = method->result_type();
457
if (method->is_native()) {
458
// Prior to calling into the runtime to report the method_exit both of
459
// the possible return value registers are saved.
460
// Return value registers are pushed to the native stack
461
res_addr = (intptr_t*)sp();
463
// FP result is pushed onto a stack along with integer result registers
464
if (type == T_FLOAT || type == T_DOUBLE) {
467
#endif // __ABI_HARD__
469
res_addr = (intptr_t*)interpreter_frame_tos_address();
476
if (method->is_native()) {
477
obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
479
obj = *(oop*)res_addr;
481
assert(Universe::is_in_heap_or_null(obj), "sanity check");
485
case T_BOOLEAN : value_result->z = *(jboolean*)res_addr; break;
486
case T_BYTE : value_result->b = *(jbyte*)res_addr; break;
487
case T_CHAR : value_result->c = *(jchar*)res_addr; break;
488
case T_SHORT : value_result->s = *(jshort*)res_addr; break;
489
case T_INT : value_result->i = *(jint*)res_addr; break;
490
case T_LONG : value_result->j = *(jlong*)res_addr; break;
491
case T_FLOAT : value_result->f = *(jfloat*)res_addr; break;
492
case T_DOUBLE : value_result->d = *(jdouble*)res_addr; break;
493
case T_VOID : /* Nothing to do */ break;
494
default : ShouldNotReachHere();
500
intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
501
int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
502
return &interpreter_frame_tos_address()[index];
507
#define DESCRIBE_FP_OFFSET(name) \
508
values.describe(frame_no, fp() + frame::name##_offset, #name)
510
void frame::describe_pd(FrameValues& values, int frame_no) {
511
if (is_interpreted_frame()) {
512
DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
513
DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
514
DESCRIBE_FP_OFFSET(interpreter_frame_method);
515
DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
516
DESCRIBE_FP_OFFSET(interpreter_frame_cache);
517
DESCRIBE_FP_OFFSET(interpreter_frame_locals);
518
DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
519
DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
523
// This is a generic constructor which is only used by pns() in debug.cpp.
524
frame::frame(void* sp, void* fp, void* pc) {
525
init((intptr_t*)sp, (intptr_t*)sp, (intptr_t*)fp, (address)pc);
530
intptr_t *frame::initial_deoptimization_info() {
531
// used to reset the saved FP
535
intptr_t* frame::real_fp() const {
536
if (is_entry_frame()) {
537
// Work-around: FP (currently) does not conform to the ABI for entry
538
// frames (see generate_call_stub). Might be worth fixing as another CR.
539
// Following code assumes (and asserts) this has not yet been fixed.
540
assert(frame::entry_frame_call_wrapper_offset == 0, "adjust this code");
541
intptr_t* new_fp = fp();
542
new_fp += 5; // saved R0,R1,R2,R4,R10
544
new_fp += 8*2; // saved D8..D15
548
if (_cb != nullptr) {
549
// use the frame size if valid
550
int size = _cb->frame_size();
552
return unextended_sp() + size;
556
assert(! is_compiled_frame(), "unknown compiled frame size");