1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * Copyright (c) 2014, 2020, Red Hat Inc. All rights reserved.
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
6 * This code is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 only, as
8 * published by the Free Software Foundation.
9 *
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
15 *
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
22 * questions.
23 *
24 */
25
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/stackWatermarkSet.hpp"
41 #include "runtime/stubCodeGenerator.hpp"
42 #include "runtime/stubRoutines.hpp"
43 #include "vmreg_aarch64.inline.hpp"
44 #ifdef COMPILER1
45 #include "c1/c1_Runtime1.hpp"
46 #include "runtime/vframeArray.hpp"
47 #endif
48
49 #ifdef ASSERT
50 void RegisterMap::check_location_valid() {
51 }
52 #endif
53
54
55 // Profiling/safepoint support
56
57 bool frame::safe_for_sender(JavaThread *thread) {
58 if (is_heap_frame()) {
59 return true;
60 }
61 address sp = (address)_sp;
62 address fp = (address)_fp;
63 address unextended_sp = (address)_unextended_sp;
64
65 // consider stack guards when trying to determine "safe" stack pointers
66 // sp must be within the usable part of the stack (not in guards)
67 if (!thread->is_in_usable_stack(sp)) {
68 return false;
69 }
70
71 // When we are running interpreted code the machine stack pointer, SP, is
72 // set low enough so that the Java expression stack can grow and shrink
73 // without ever exceeding the machine stack bounds. So, ESP >= SP.
74
75 // When we call out of an interpreted method, SP is incremented so that
76 // the space between SP and ESP is removed. The SP saved in the callee's
77 // frame is the SP *before* this increment. So, when we walk a stack of
78 // interpreter frames the sender's SP saved in a frame might be less than
79 // the SP at the point of call.
80
81 // So unextended sp must be within the stack but we need not to check
82 // that unextended sp >= sp
83 if (!thread->is_in_full_stack_checked(unextended_sp)) {
84 return false;
85 }
86
87 // an fp must be within the stack and above (but not equal) sp
88 // second evaluation on fp+ is added to handle situation where fp is -1
89 bool fp_safe = thread->is_in_stack_range_excl(fp, sp) &&
90 thread->is_in_full_stack_checked(fp + (return_addr_offset * sizeof(void*)));
91
92 // We know sp/unextended_sp are safe only fp is questionable here
93
94 // If the current frame is known to the code cache then we can attempt to
95 // to construct the sender and do some validation of it. This goes a long way
96 // toward eliminating issues when we get in frame construction code
97
98 if (_cb != nullptr ) {
99
100 // First check if frame is complete and tester is reliable
101 // Unfortunately we can only check frame complete for runtime stubs and nmethod
102 // other generic buffer blobs are more problematic so we just assume they are
103 // ok. adapter blobs never have a frame complete and are never ok.
104
105 if (!_cb->is_frame_complete_at(_pc)) {
106 if (_cb->is_nmethod() || _cb->is_adapter_blob() || _cb->is_runtime_stub()) {
107 return false;
108 }
109 }
110
111 // Could just be some random pointer within the codeBlob
112 if (!_cb->code_contains(_pc)) {
113 return false;
114 }
115
116 // Entry frame checks
117 if (is_entry_frame()) {
118 // an entry frame must have a valid fp.
119 return fp_safe && is_entry_frame_valid(thread);
120 } else if (is_upcall_stub_frame()) {
121 return fp_safe;
122 }
123
124 intptr_t* sender_sp = nullptr;
125 intptr_t* sender_unextended_sp = nullptr;
126 address sender_pc = nullptr;
127 intptr_t* saved_fp = nullptr;
128
129 if (is_interpreted_frame()) {
130 // fp must be safe
131 if (!fp_safe) {
132 return false;
133 }
134
135 // for interpreted frames, the value below is the sender "raw" sp,
136 // which can be different from the sender unextended sp (the sp seen
137 // by the sender) because of current frame local variables
138 sender_sp = (intptr_t*) addr_at(sender_sp_offset);
139 sender_unextended_sp = (intptr_t*) this->fp()[interpreter_frame_sender_sp_offset];
140 saved_fp = (intptr_t*) this->fp()[link_offset];
141 sender_pc = pauth_strip_verifiable((address) this->fp()[return_addr_offset]);
142 } else {
143 // must be some sort of compiled/runtime frame
144 // fp does not have to be safe (although it could be check for c1?)
145
146 // check for a valid frame_size, otherwise we are unlikely to get a valid sender_pc
147 if (_cb->frame_size() <= 0) {
148 return false;
149 }
150
151 sender_sp = _unextended_sp + _cb->frame_size();
152 // Is sender_sp safe?
153 if (!thread->is_in_full_stack_checked((address)sender_sp)) {
154 return false;
155 }
156 // Note: frame::sender_sp_offset is only valid for compiled frame
157 intptr_t **saved_fp_addr = (intptr_t**) (sender_sp - frame::sender_sp_offset);
158 saved_fp = *saved_fp_addr;
159 // Note: PAC authentication may fail in case broken frame is passed in.
160 // Just strip it for now.
161 sender_pc = pauth_strip_pointer((address) *(sender_sp - 1));
162
163 // Repair the sender sp if this is a method with scalarized inline type args
164 sender_sp = repair_sender_sp(sender_sp, saved_fp_addr);
165 sender_unextended_sp = sender_sp;
166 }
167 if (Continuation::is_return_barrier_entry(sender_pc)) {
168 // sender_pc might be invalid so check that the frame
169 // actually belongs to a Continuation.
170 if (!Continuation::is_frame_in_continuation(thread, *this)) {
171 return false;
172 }
173 // If our sender_pc is the return barrier, then our "real" sender is the continuation entry
174 frame s = Continuation::continuation_bottom_sender(thread, *this, sender_sp);
175 sender_sp = s.sp();
176 sender_pc = s.pc();
177 }
178
179 // If the potential sender is the interpreter then we can do some more checking
180 if (Interpreter::contains(sender_pc)) {
181
182 // fp is always saved in a recognizable place in any code we generate. However
183 // only if the sender is interpreted/call_stub (c1 too?) are we certain that the saved fp
184 // is really a frame pointer.
185
186 if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
187 return false;
188 }
189
190 // construct the potential sender
191
192 frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
193
194 return sender.is_interpreted_frame_valid(thread);
195
196 }
197
198 // We must always be able to find a recognizable pc
199 CodeBlob* sender_blob = CodeCache::find_blob(sender_pc);
200 if (sender_pc == nullptr || sender_blob == nullptr) {
201 return false;
202 }
203
204 // Could just be some random pointer within the codeBlob
205 if (!sender_blob->code_contains(sender_pc)) {
206 return false;
207 }
208
209 // We should never be able to see an adapter if the current frame is something from code cache
210 if (sender_blob->is_adapter_blob()) {
211 return false;
212 }
213
214 // Could be the call_stub
215 if (StubRoutines::returns_to_call_stub(sender_pc)) {
216 if (!thread->is_in_stack_range_excl((address)saved_fp, (address)sender_sp)) {
217 return false;
218 }
219
220 // construct the potential sender
221
222 frame sender(sender_sp, sender_unextended_sp, saved_fp, sender_pc);
223
224 // Validate the JavaCallWrapper an entry frame must have
225 address jcw = (address)sender.entry_frame_call_wrapper();
226
227 return thread->is_in_stack_range_excl(jcw, (address)sender.fp());
228 } else if (sender_blob->is_upcall_stub()) {
229 return false;
230 }
231
232 nmethod* nm = sender_blob->as_nmethod_or_null();
233 if (nm != nullptr) {
234 if (nm->is_deopt_entry(sender_pc) || nm->method()->is_method_handle_intrinsic()) {
235 return false;
236 }
237 }
238
239 // If the frame size is 0 something (or less) is bad because every nmethod has a non-zero frame size
240 // because the return address counts against the callee's frame.
241
242 if (sender_blob->frame_size() <= 0) {
243 assert(!sender_blob->is_nmethod(), "should count return address at least");
244 return false;
245 }
246
247 // We should never be able to see anything here except an nmethod. If something in the
248 // code cache (current frame) is called by an entity within the code cache that entity
249 // should not be anything but the call stub (already covered), the interpreter (already covered)
250 // or an nmethod.
251
252 if (!sender_blob->is_nmethod()) {
253 return false;
254 }
255
256 // Could put some more validation for the potential non-interpreted sender
257 // frame we'd create by calling sender if I could think of any. Wait for next crash in forte...
258
259 // One idea is seeing if the sender_pc we have is one that we'd expect to call to current cb
260
261 // We've validated the potential sender that would be created
262 return true;
263 }
264
265 // Must be native-compiled frame. Since sender will try and use fp to find
266 // linkages it must be safe
267
268 if (!fp_safe) {
269 return false;
270 }
271
272 // Will the pc we fetch be non-zero (which we'll find at the oldest frame)
273
274 if ( (address) this->fp()[return_addr_offset] == nullptr) return false;
275
276
277 // could try and do some more potential verification of native frame if we could think of some...
278
279 return true;
280
281 }
282
283 void frame::patch_pc(Thread* thread, address pc) {
284 assert(_cb == CodeCache::find_blob(pc), "unexpected pc");
285 address* pc_addr = &(((address*) sp())[-1]);
286 address signed_pc = pauth_sign_return_address(pc);
287 address pc_old = pauth_strip_verifiable(*pc_addr);
288
289 if (TracePcPatching) {
290 tty->print("patch_pc at address " INTPTR_FORMAT " [" INTPTR_FORMAT " -> " INTPTR_FORMAT "]",
291 p2i(pc_addr), p2i(pc_old), p2i(pc));
292 if (VM_Version::use_rop_protection()) {
293 tty->print(" [signed " INTPTR_FORMAT " -> " INTPTR_FORMAT "]", p2i(*pc_addr), p2i(signed_pc));
294 }
295 tty->print_cr("");
296 }
297
298 assert(!Continuation::is_return_barrier_entry(pc_old), "return barrier");
299
300 // Either the return address is the original one or we are going to
301 // patch in the same address that's already there.
302 assert(_pc == pc_old || pc == pc_old || pc_old == nullptr, "");
303 DEBUG_ONLY(address old_pc = _pc;)
304 *pc_addr = signed_pc;
305 _pc = pc; // must be set before call to get_deopt_original_pc
306 address original_pc = get_deopt_original_pc();
307 if (original_pc != nullptr) {
308 assert(original_pc == old_pc, "expected original PC to be stored before patching");
309 _deopt_state = is_deoptimized;
310 _pc = original_pc;
311 } else {
312 _deopt_state = not_deoptimized;
313 }
314 }
315
316 intptr_t* frame::entry_frame_argument_at(int offset) const {
317 // convert offset to index to deal with tsi
318 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
319 // Entry frame's arguments are always in relation to unextended_sp()
320 return &unextended_sp()[index];
321 }
322
323 // locals
324
325 void frame::interpreter_frame_set_locals(intptr_t* locs) {
326 assert(is_interpreted_frame(), "interpreted frame expected");
327 // set relativized locals
328 ptr_at_put(interpreter_frame_locals_offset, (intptr_t) (locs - fp()));
329 }
330
331 // sender_sp
332
333 intptr_t* frame::interpreter_frame_sender_sp() const {
334 assert(is_interpreted_frame(), "interpreted frame expected");
335 return (intptr_t*) at(interpreter_frame_sender_sp_offset);
336 }
337
338 void frame::set_interpreter_frame_sender_sp(intptr_t* sender_sp) {
339 assert(is_interpreted_frame(), "interpreted frame expected");
340 ptr_at_put(interpreter_frame_sender_sp_offset, (intptr_t) sender_sp);
341 }
342
343
344 // monitor elements
345
346 BasicObjectLock* frame::interpreter_frame_monitor_begin() const {
347 return (BasicObjectLock*) addr_at(interpreter_frame_monitor_block_bottom_offset);
348 }
349
350 BasicObjectLock* frame::interpreter_frame_monitor_end() const {
351 BasicObjectLock* result = (BasicObjectLock*) at_relative(interpreter_frame_monitor_block_top_offset);
352 // make sure the pointer points inside the frame
353 assert(sp() <= (intptr_t*) result, "monitor end should be above the stack pointer");
354 assert((intptr_t*) result < fp(), "monitor end should be strictly below the frame pointer");
355 return result;
356 }
357
358 void frame::interpreter_frame_set_monitor_end(BasicObjectLock* value) {
359 assert(is_interpreted_frame(), "interpreted frame expected");
360 // set relativized monitor_block_top
361 ptr_at_put(interpreter_frame_monitor_block_top_offset, (intptr_t*)value - fp());
362 assert(at_absolute(interpreter_frame_monitor_block_top_offset) <= interpreter_frame_monitor_block_top_offset, "");
363 }
364
365 // Used by template based interpreter deoptimization
366 void frame::interpreter_frame_set_last_sp(intptr_t* sp) {
367 assert(is_interpreted_frame(), "interpreted frame expected");
368 // set relativized last_sp
369 ptr_at_put(interpreter_frame_last_sp_offset, sp != nullptr ? (sp - fp()) : 0);
370 }
371
372 // Used by template based interpreter deoptimization
373 void frame::interpreter_frame_set_extended_sp(intptr_t* sp) {
374 assert(is_interpreted_frame(), "interpreted frame expected");
375 // set relativized extended_sp
376 ptr_at_put(interpreter_frame_extended_sp_offset, (sp - fp()));
377 }
378
379 frame frame::sender_for_entry_frame(RegisterMap* map) const {
380 assert(map != nullptr, "map must be set");
381 // Java frame called from C; skip all C frames and return top C
382 // frame of that chunk as the sender
383 JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
384 assert(!entry_frame_is_first(), "next Java fp must be non zero");
385 assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
386 // Since we are walking the stack now this nested anchor is obviously walkable
387 // even if it wasn't when it was stacked.
388 jfa->make_walkable();
389 map->clear();
390 assert(map->include_argument_oops(), "should be set by clear");
391 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
392 fr.set_sp_is_trusted();
393
394 return fr;
395 }
396
397 UpcallStub::FrameData* UpcallStub::frame_data_for_frame(const frame& frame) const {
398 assert(frame.is_upcall_stub_frame(), "wrong frame");
399 // need unextended_sp here, since normal sp is wrong for interpreter callees
400 return reinterpret_cast<UpcallStub::FrameData*>(
401 reinterpret_cast<address>(frame.unextended_sp()) + in_bytes(_frame_data_offset));
402 }
403
404 bool frame::upcall_stub_frame_is_first() const {
405 assert(is_upcall_stub_frame(), "must be optimzed entry frame");
406 UpcallStub* blob = _cb->as_upcall_stub();
407 JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
408 return jfa->last_Java_sp() == nullptr;
409 }
410
411 frame frame::sender_for_upcall_stub_frame(RegisterMap* map) const {
412 assert(map != nullptr, "map must be set");
413 UpcallStub* blob = _cb->as_upcall_stub();
414 // Java frame called from C; skip all C frames and return top C
415 // frame of that chunk as the sender
416 JavaFrameAnchor* jfa = blob->jfa_for_frame(*this);
417 assert(!upcall_stub_frame_is_first(), "must have a frame anchor to go back to");
418 assert(jfa->last_Java_sp() > sp(), "must be above this frame on stack");
419 // Since we are walking the stack now this nested anchor is obviously walkable
420 // even if it wasn't when it was stacked.
421 jfa->make_walkable();
422 map->clear();
423 assert(map->include_argument_oops(), "should be set by clear");
424 frame fr(jfa->last_Java_sp(), jfa->last_Java_fp(), jfa->last_Java_pc());
425
426 return fr;
427 }
428
429 #if defined(ASSERT)
430 static address get_register_address_in_stub(const frame& stub_fr, VMReg reg) {
431 RegisterMap map(nullptr,
432 RegisterMap::UpdateMap::include,
433 RegisterMap::ProcessFrames::skip,
434 RegisterMap::WalkContinuation::skip);
435 stub_fr.oop_map()->update_register_map(&stub_fr, &map);
436 return map.location(reg, stub_fr.sp());
437 }
438 #endif
439
440 JavaThread** frame::saved_thread_address(const frame& f) {
441 CodeBlob* cb = f.cb();
442 assert(cb != nullptr && cb->is_runtime_stub(), "invalid frame");
443
444 JavaThread** thread_addr;
445 #ifdef COMPILER1
446 if (cb == Runtime1::blob_for(StubId::c1_monitorenter_id) ||
447 cb == Runtime1::blob_for(StubId::c1_monitorenter_nofpu_id)) {
448 thread_addr = (JavaThread**)(f.sp() + Runtime1::runtime_blob_current_thread_offset(f));
449 } else
450 #endif
451 {
452 // c2 only saves rbp in the stub frame so nothing to do.
453 thread_addr = nullptr;
454 }
455 assert(get_register_address_in_stub(f, SharedRuntime::thread_register()) == (address)thread_addr, "wrong thread address");
456 return thread_addr;
457 }
458
459 //------------------------------------------------------------------------------
460 // frame::sender_for_interpreter_frame
461 frame frame::sender_for_interpreter_frame(RegisterMap* map) const {
462 // SP is the raw SP from the sender after adapter or interpreter
463 // extension.
464 intptr_t* sender_sp = this->sender_sp();
465
466 // This is the sp before any possible extension (adapter/locals).
467 intptr_t* unextended_sp = interpreter_frame_sender_sp();
468 intptr_t* sender_fp = link();
469
470 #if defined(COMPILER1) || COMPILER2_OR_JVMCI
471 if (map->update_map()) {
472 update_map_with_saved_link(map, (intptr_t**) addr_at(link_offset));
473 }
474 #endif // defined(COMPILER1) || COMPILER1_OR_COMPILER2
475
476 // For ROP protection, Interpreter will have signed the sender_pc,
477 // but there is no requirement to authenticate it here.
478 address sender_pc = pauth_strip_verifiable(sender_pc_maybe_signed());
479
480 if (Continuation::is_return_barrier_entry(sender_pc)) {
481 if (map->walk_cont()) { // about to walk into an h-stack
482 return Continuation::top_frame(*this, map);
483 } else {
484 return Continuation::continuation_bottom_sender(map->thread(), *this, sender_sp);
485 }
486 }
487
488 return frame(sender_sp, unextended_sp, sender_fp, sender_pc);
489 }
490
491 bool frame::is_interpreted_frame_valid(JavaThread* thread) const {
492 assert(is_interpreted_frame(), "Not an interpreted frame");
493 // These are reasonable sanity checks
494 if (fp() == nullptr || (intptr_t(fp()) & (wordSize-1)) != 0) {
495 return false;
496 }
497 if (sp() == nullptr || (intptr_t(sp()) & (wordSize-1)) != 0) {
498 return false;
499 }
500 if (fp() + interpreter_frame_initial_sp_offset < sp()) {
501 return false;
502 }
503 // These are hacks to keep us out of trouble.
504 // The problem with these is that they mask other problems
505 if (fp() <= sp()) { // this attempts to deal with unsigned comparison above
506 return false;
507 }
508
509 // do some validation of frame elements
510
511 // first the method
512
513 Method* m = safe_interpreter_frame_method();
514
515 // validate the method we'd find in this potential sender
516 if (!Method::is_valid_method(m)) return false;
517
518 // stack frames shouldn't be much larger than max_stack elements
519 // this test requires the use of unextended_sp which is the sp as seen by
520 // the current frame, and not sp which is the "raw" pc which could point
521 // further because of local variables of the callee method inserted after
522 // method arguments
523 if (fp() - unextended_sp() > 1024 + m->max_stack()*Interpreter::stackElementSize) {
524 return false;
525 }
526
527 // validate bci/bcx
528
529 address bcp = interpreter_frame_bcp();
530 if (m->validate_bci_from_bcp(bcp) < 0) {
531 return false;
532 }
533
534 // validate constantPoolCache*
535 ConstantPoolCache* cp = *interpreter_frame_cache_addr();
536 if (MetaspaceObj::is_valid(cp) == false) return false;
537
538 // validate locals
539
540 address locals = (address)interpreter_frame_locals();
541 return thread->is_in_stack_range_incl(locals, (address)fp());
542 }
543
544 BasicType frame::interpreter_frame_result(oop* oop_result, jvalue* value_result) {
545 assert(is_interpreted_frame(), "interpreted frame expected");
546 Method* method = interpreter_frame_method();
547 BasicType type = method->result_type();
548
549 intptr_t* tos_addr;
550 if (method->is_native()) {
551 // TODO : ensure AARCH64 does the same as Intel here i.e. push v0 then r0
552 // Prior to calling into the runtime to report the method_exit the possible
553 // return value is pushed to the native stack. If the result is a jfloat/jdouble
554 // then ST0 is saved before EAX/EDX. See the note in generate_native_result
555 tos_addr = (intptr_t*)sp();
556 if (type == T_FLOAT || type == T_DOUBLE) {
557 // This is times two because we do a push(ltos) after pushing XMM0
558 // and that takes two interpreter stack slots.
559 tos_addr += 2 * Interpreter::stackElementWords;
560 }
561 } else {
562 tos_addr = (intptr_t*)interpreter_frame_tos_address();
563 }
564
565 switch (type) {
566 case T_OBJECT :
567 case T_ARRAY : {
568 oop obj;
569 if (method->is_native()) {
570 obj = cast_to_oop(at(interpreter_frame_oop_temp_offset));
571 } else {
572 oop* obj_p = (oop*)tos_addr;
573 obj = (obj_p == nullptr) ? (oop)nullptr : *obj_p;
574 }
575 assert(Universe::is_in_heap_or_null(obj), "sanity check");
576 *oop_result = obj;
577 break;
578 }
579 case T_BOOLEAN : value_result->z = *(jboolean*)tos_addr; break;
580 case T_BYTE : value_result->b = *(jbyte*)tos_addr; break;
581 case T_CHAR : value_result->c = *(jchar*)tos_addr; break;
582 case T_SHORT : value_result->s = *(jshort*)tos_addr; break;
583 case T_INT : value_result->i = *(jint*)tos_addr; break;
584 case T_LONG : value_result->j = *(jlong*)tos_addr; break;
585 case T_FLOAT : {
586 value_result->f = *(jfloat*)tos_addr;
587 break;
588 }
589 case T_DOUBLE : value_result->d = *(jdouble*)tos_addr; break;
590 case T_VOID : /* Nothing to do */ break;
591 default : ShouldNotReachHere();
592 }
593
594 return type;
595 }
596
597 intptr_t* frame::interpreter_frame_tos_at(jint offset) const {
598 int index = (Interpreter::expr_offset_in_bytes(offset)/wordSize);
599 return &interpreter_frame_tos_address()[index];
600 }
601
602 #ifndef PRODUCT
603
604 #define DESCRIBE_FP_OFFSET(name) \
605 values.describe(frame_no, fp() + frame::name##_offset, #name)
606
607 void frame::describe_pd(FrameValues& values, int frame_no) {
608 if (is_interpreted_frame()) {
609 DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
610 DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
611 DESCRIBE_FP_OFFSET(interpreter_frame_method);
612 DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
613 DESCRIBE_FP_OFFSET(interpreter_frame_extended_sp);
614 DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
615 DESCRIBE_FP_OFFSET(interpreter_frame_cache);
616 DESCRIBE_FP_OFFSET(interpreter_frame_locals);
617 DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
618 DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
619 }
620
621 if (is_java_frame() || Continuation::is_continuation_enterSpecial(*this)) {
622 intptr_t* ret_pc_loc;
623 intptr_t* fp_loc;
624 if (is_interpreted_frame()) {
625 ret_pc_loc = fp() + return_addr_offset;
626 fp_loc = fp();
627 } else {
628 ret_pc_loc = real_fp() - return_addr_offset;
629 fp_loc = real_fp() - sender_sp_offset;
630 if (cb()->is_nmethod() && cb()->as_nmethod_or_null()->needs_stack_repair()) {
631 values.describe(frame_no, fp_loc - 1, err_msg("fsize for #%d", frame_no), 1);
632 }
633 }
634 address ret_pc = *(address*)ret_pc_loc;
635 values.describe(frame_no, ret_pc_loc,
636 Continuation::is_return_barrier_entry(ret_pc) ? "return address (return barrier)" : "return address");
637 values.describe(-1, fp_loc, "saved fp", 0); // "unowned" as value belongs to sender
638 }
639 }
640 #endif
641
642 intptr_t *frame::initial_deoptimization_info() {
643 // Not used on aarch64, but we must return something.
644 return nullptr;
645 }
646
647 #undef DESCRIBE_FP_OFFSET
648
649 #define DESCRIBE_FP_OFFSET(name) \
650 { \
651 uintptr_t *p = (uintptr_t *)fp; \
652 printf(INTPTR_FORMAT " " INTPTR_FORMAT " %s\n", \
653 (uintptr_t)(p + frame::name##_offset), \
654 p[frame::name##_offset], #name); \
655 }
656
657 static THREAD_LOCAL uintptr_t nextfp;
658 static THREAD_LOCAL uintptr_t nextpc;
659 static THREAD_LOCAL uintptr_t nextsp;
660 static THREAD_LOCAL RegisterMap *reg_map;
661
662 static void printbc(Method *m, intptr_t bcx) {
663 const char *name;
664 char buf[16];
665 if (m->validate_bci_from_bcp((address)bcx) < 0
666 || !m->contains((address)bcx)) {
667 name = "???";
668 os::snprintf_checked(buf, sizeof buf, "(bad)");
669 } else {
670 int bci = m->bci_from((address)bcx);
671 os::snprintf_checked(buf, sizeof buf, "%d", bci);
672 name = Bytecodes::name(m->code_at(bci));
673 }
674 ResourceMark rm;
675 printf("%s : %s ==> %s\n", m->name_and_sig_as_C_string(), buf, name);
676 }
677
678 static void internal_pf(uintptr_t sp, uintptr_t fp, uintptr_t pc, uintptr_t bcx) {
679 if (! fp)
680 return;
681
682 DESCRIBE_FP_OFFSET(return_addr);
683 DESCRIBE_FP_OFFSET(link);
684 DESCRIBE_FP_OFFSET(interpreter_frame_sender_sp);
685 DESCRIBE_FP_OFFSET(interpreter_frame_last_sp);
686 DESCRIBE_FP_OFFSET(interpreter_frame_method);
687 DESCRIBE_FP_OFFSET(interpreter_frame_mdp);
688 DESCRIBE_FP_OFFSET(interpreter_frame_extended_sp);
689 DESCRIBE_FP_OFFSET(interpreter_frame_mirror);
690 DESCRIBE_FP_OFFSET(interpreter_frame_cache);
691 DESCRIBE_FP_OFFSET(interpreter_frame_locals);
692 DESCRIBE_FP_OFFSET(interpreter_frame_bcp);
693 DESCRIBE_FP_OFFSET(interpreter_frame_initial_sp);
694 uintptr_t *p = (uintptr_t *)fp;
695
696 // We want to see all frames, native and Java. For compiled and
697 // interpreted frames we have special information that allows us to
698 // unwind them; for everything else we assume that the native frame
699 // pointer chain is intact.
700 frame this_frame((intptr_t*)sp, (intptr_t*)fp, (address)pc);
701 if (this_frame.is_compiled_frame() ||
702 this_frame.is_interpreted_frame()) {
703 frame sender = this_frame.sender(reg_map);
704 nextfp = (uintptr_t)sender.fp();
705 nextpc = (uintptr_t)sender.pc();
706 nextsp = (uintptr_t)sender.unextended_sp();
707 } else {
708 nextfp = p[frame::link_offset];
709 nextpc = p[frame::return_addr_offset];
710 nextsp = (uintptr_t)&p[frame::sender_sp_offset];
711 }
712
713 if (bcx == -1ULL)
714 bcx = p[frame::interpreter_frame_bcp_offset];
715
716 if (Interpreter::contains((address)pc)) {
717 Method* m = (Method*)p[frame::interpreter_frame_method_offset];
718 if(m && m->is_method()) {
719 printbc(m, bcx);
720 } else
721 printf("not a Method\n");
722 } else {
723 CodeBlob *cb = CodeCache::find_blob((address)pc);
724 if (cb != nullptr) {
725 if (cb->is_nmethod()) {
726 ResourceMark rm;
727 nmethod* nm = (nmethod*)cb;
728 printf("nmethod %s\n", nm->method()->name_and_sig_as_C_string());
729 } else if (cb->name()) {
730 printf("CodeBlob %s\n", cb->name());
731 }
732 }
733 }
734 }
735
736 extern "C" void npf() {
737 CodeBlob *cb = CodeCache::find_blob((address)nextpc);
738 // C2 does not always chain the frame pointers when it can, instead
739 // preferring to use fixed offsets from SP, so a simple leave() does
740 // not work. Instead, it adds the frame size to SP then pops FP and
741 // LR. We have to do the same thing to get a good call chain.
742 if (cb && cb->frame_size())
743 nextfp = nextsp + wordSize * (cb->frame_size() - 2);
744 internal_pf (nextsp, nextfp, nextpc, -1);
745 }
746
747 extern "C" void pf(uintptr_t sp, uintptr_t fp, uintptr_t pc,
748 uintptr_t bcx, uintptr_t thread) {
749 if (!reg_map) {
750 reg_map = NEW_C_HEAP_OBJ(RegisterMap, mtInternal);
751 ::new (reg_map) RegisterMap(reinterpret_cast<JavaThread*>(thread),
752 RegisterMap::UpdateMap::skip,
753 RegisterMap::ProcessFrames::include,
754 RegisterMap::WalkContinuation::skip);
755 } else {
756 *reg_map = RegisterMap(reinterpret_cast<JavaThread*>(thread),
757 RegisterMap::UpdateMap::skip,
758 RegisterMap::ProcessFrames::include,
759 RegisterMap::WalkContinuation::skip);
760 }
761
762 {
763 CodeBlob *cb = CodeCache::find_blob((address)pc);
764 if (cb && cb->frame_size())
765 fp = sp + wordSize * (cb->frame_size() - 2);
766 }
767 internal_pf(sp, fp, pc, bcx);
768 }
769
770 // support for printing out where we are in a Java method
771 // needs to be passed current fp and bcp register values
772 // prints method name, bc index and bytecode name
773 extern "C" void pm(uintptr_t fp, uintptr_t bcx) {
774 DESCRIBE_FP_OFFSET(interpreter_frame_method);
775 uintptr_t *p = (uintptr_t *)fp;
776 Method* m = (Method*)p[frame::interpreter_frame_method_offset];
777 printbc(m, bcx);
778 }
779
780 #ifndef PRODUCT
781 // This is a generic constructor which is only used by pns() in debug.cpp.
782 frame::frame(void* sp, void* fp, void* pc) {
783 init((intptr_t*)sp, (intptr_t*)fp, (address)pc);
784 }
785
786 #endif
787
788 // Check for a method with scalarized inline type arguments that needs
789 // a stack repair and return the repaired sender stack pointer.
790 intptr_t* frame::repair_sender_sp(intptr_t* sender_sp, intptr_t** saved_fp_addr) const {
791 nmethod* nm = _cb->as_nmethod_or_null();
792 if (nm != nullptr && nm->needs_stack_repair()) {
793 // The stack increment resides just below the saved FP on the stack and
794 // records the total frame size excluding the two words for saving FP and LR.
795 intptr_t* sp_inc_addr = (intptr_t*) (saved_fp_addr - 1);
796 assert(*sp_inc_addr % StackAlignmentInBytes == 0, "sp_inc not aligned");
797 int real_frame_size = (*sp_inc_addr / wordSize) + 2;
798 assert(real_frame_size >= _cb->frame_size() && real_frame_size <= 1000000, "invalid frame size");
799 sender_sp = unextended_sp() + real_frame_size;
800 }
801 return sender_sp;
802 }
803
804 intptr_t* frame::repair_sender_sp(nmethod* nm, intptr_t* sp, intptr_t** saved_fp_addr) {
805 assert(nm != nullptr && nm->needs_stack_repair(), "");
806 // The stack increment resides just below the saved FP on the stack and
807 // records the total frame size excluding the two words for saving FP and LR.
808 intptr_t* real_frame_size_addr = (intptr_t*) (saved_fp_addr - 1);
809 int real_frame_size = (*real_frame_size_addr / wordSize) + 2;
810 assert(real_frame_size >= nm->frame_size() && real_frame_size <= 1000000, "invalid frame size");
811 return sp + real_frame_size;
812 }
813
814 bool frame::was_augmented_on_entry(int& real_size) const {
815 assert(is_compiled_frame(), "");
816 if (_cb->as_nmethod_or_null()->needs_stack_repair()) {
817 intptr_t* real_frame_size_addr = unextended_sp() + _cb->frame_size() - sender_sp_offset - 1;
818 log_trace(continuations)("real_frame_size is addr is " INTPTR_FORMAT, p2i(real_frame_size_addr));
819 real_size = (*real_frame_size_addr / wordSize) + 2;
820 return real_size != _cb->frame_size();
821 }
822 real_size = _cb->frame_size();
823 return false;
824 }
825
826 void JavaFrameAnchor::make_walkable() {
827 // last frame set?
828 if (last_Java_sp() == nullptr) return;
829 // already walkable?
830 if (walkable()) return;
831 vmassert(last_Java_sp() != nullptr, "not called from Java code?");
832 _last_Java_pc = (address)_last_Java_sp[-1];
833 vmassert(walkable(), "something went wrong");
834 }