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 *
23 */
24
25 #ifndef SHARE_RUNTIME_FRAME_HPP
26 #define SHARE_RUNTIME_FRAME_HPP
27
28 #include "code/vmregTypes.hpp"
29 #include "compiler/oopMap.hpp"
30 #include "oops/oopsHierarchy.hpp"
31 #include "runtime/basicLock.hpp"
32 #include "runtime/monitorChunk.hpp"
33 #include "utilities/checkedCast.hpp"
34 #include "utilities/growableArray.hpp"
35 #include "utilities/macros.hpp"
36 #ifdef ZERO
37 # include "stack_zero.hpp"
38 #endif
39
40 typedef class BytecodeInterpreter* interpreterState;
41
42 class CodeBlob;
43 class FrameValues;
44 class InterpreterOopMap;
45 class JavaCallWrapper;
46 class Method;
47 class methodHandle;
48 class RegisterMap;
49 class vframeArray;
50
51 enum class DerivedPointerIterationMode {
52 _with_table,
53 _directly,
54 _ignore
55 };
56
57 // A frame represents a physical stack frame (an activation). Frames
58 // can be C or Java frames, and the Java frames can be interpreted or
59 // compiled. In contrast, vframes represent source-level activations,
60 // so that one physical frame can correspond to multiple source level
61 // frames because of inlining.
62
63 class frame {
64 private:
65 // Instance variables:
66 union {
67 intptr_t* _sp; // stack pointer (from Thread::last_Java_sp)
68 int _offset_sp; // used by frames in stack chunks
69 };
70 address _pc; // program counter (the next instruction after the call)
71 mutable CodeBlob* _cb; // CodeBlob that "owns" pc
72 mutable const ImmutableOopMap* _oop_map; // oop map, for compiled/stubs frames only
73 enum deopt_state {
74 not_deoptimized,
75 is_deoptimized,
76 unknown
77 };
78
79 deopt_state _deopt_state;
80
81 // Do internal pointers in interpreter frames use absolute adddresses or relative (to fp)?
82 // Frames in stack chunks are on the Java heap and use relative addressing; on the stack
83 // they use absolute addressing
84 bool _on_heap; // This frame represents a frame on the heap.
85 DEBUG_ONLY(int _frame_index;) // the frame index in a stack chunk; -1 when on a thread stack
86
87 // We use different assertions to allow for intermediate states (e.g. during thawing or relativizing the frame)
88 void assert_on_heap() const { assert(is_heap_frame(), "Using offset with a non-chunk frame"); }
89 void assert_offset() const { assert(_frame_index >= 0, "Using offset with a non-chunk frame"); assert_on_heap(); }
90 void assert_absolute() const { assert(_frame_index == -1, "Using absolute addresses with a chunk frame"); }
91
92 const ImmutableOopMap* get_oop_map() const;
93
94 public:
95 // Constructors
96 frame();
97
98 explicit frame(bool dummy) {} // no initialization
99
100 explicit frame(intptr_t* sp);
101
102 #ifndef PRODUCT
103 // This is a generic constructor which is only used by pns() in debug.cpp.
104 // pns (i.e. print native stack) uses this constructor to create a starting
105 // frame for stack walking. The implementation of this constructor is platform
106 // dependent (i.e. SPARC doesn't need an 'fp' argument an will ignore it) but
107 // we want to keep the signature generic because pns() is shared code.
108 frame(void* sp, void* fp, void* pc);
109 #endif
110
111 // Accessors
112
113 // pc: Returns the pc at which this frame will continue normally.
114 // It must point at the beginning of the next instruction to execute.
115 address pc() const { return _pc; }
116
117 // This returns the pc that if you were in the debugger you'd see. Not
118 // the idealized value in the frame object. This undoes the magic conversion
119 // that happens for deoptimized frames. In addition it makes the value the
120 // hardware would want to see in the native frame. The only user (at this point)
121 // is deoptimization. It likely no one else should ever use it.
122 address raw_pc() const;
123
124 // Return the original PC for the given PC if:
125 // (a) the given PC belongs to an nmethod and
126 // (b) it is a deopt PC
127 address get_deopt_original_pc() const;
128
129 void set_pc(address newpc);
130 void adjust_pc(address newpc);
131
132 intptr_t* sp() const { assert_absolute(); return _sp; }
133 void set_sp( intptr_t* newsp ) { _sp = newsp; }
134
135 int offset_sp() const { assert_offset(); return _offset_sp; }
136 void set_offset_sp( int newsp ) { assert_on_heap(); _offset_sp = newsp; }
137
138 int frame_index() const {
139 #ifdef ASSERT
140 return _frame_index;
141 #else
142 return -1;
143 #endif
144 }
145 void set_frame_index( int index ) {
146 #ifdef ASSERT
147 _frame_index = index;
148 #endif
149 }
150
151 static int sender_sp_ret_address_offset();
152
153 CodeBlob* cb() const { return _cb; }
154 inline CodeBlob* get_cb() const;
155 // inline void set_cb(CodeBlob* cb);
156
157 const ImmutableOopMap* oop_map() const {
158 if (_oop_map == nullptr) {
159 _oop_map = get_oop_map();
160 }
161 return _oop_map;
162 }
163
164 // patching operations
165 void patch_pc(Thread* thread, address pc);
166
167 // Every frame needs to return a unique id which distinguishes it from all other frames.
168 // For sparc and ia32 use sp.
169 // No real frame should have an id() of null so it is a distinguishing value for an unmatchable frame.
170 // We also have relationals which allow comparing a frame to another frame's id() allowing
171 // us to distinguish younger (more recent activation) from older (less recent activations)
172 // A null id is only valid when comparing for equality.
173
174 intptr_t* id(void) const;
175 bool is_younger(intptr_t* id) const;
176 bool is_older(intptr_t* id) const;
177
178 // testers
179
180 // Compares for strict equality. Rarely used or needed.
181 // It can return a different result than f1.id() == f2.id()
182 bool equal(frame other) const;
183
184 // type testers
185 bool is_empty() const { return _pc == nullptr; }
186 bool is_interpreted_frame() const;
187 bool is_java_frame() const;
188 bool is_entry_frame() const; // Java frame called from C?
189 bool is_stub_frame() const;
190 bool is_ignored_frame() const;
191 bool is_native_frame() const;
192 bool is_runtime_frame() const;
193 bool is_compiled_frame() const;
194 bool is_safepoint_blob_frame() const;
195 bool is_deoptimized_frame() const;
196 bool is_upcall_stub_frame() const;
197 bool is_heap_frame() const { return _on_heap; }
198
199 // testers
200 bool is_first_frame() const; // oldest frame? (has no sender)
201 bool is_first_java_frame() const; // same for Java frame
202 bool is_first_vthread_frame(JavaThread* thread) const;
203
204 bool is_interpreted_frame_valid(JavaThread* thread) const; // performs sanity checks on interpreted frames.
205
206 // is this frame doing a call using the compiled calling convention?
207 bool is_compiled_caller() const {
208 return is_compiled_frame() || is_upcall_stub_frame();
209 }
210
211 // tells whether this frame is marked for deoptimization
212 bool should_be_deoptimized() const;
213
214 // tells whether this frame can be deoptimized
215 bool can_be_deoptimized() const;
216
217 // the frame size in machine words
218 inline int frame_size() const;
219
220 // the size, in words, of stack-passed arguments
221 inline int compiled_frame_stack_argsize() const;
222
223 inline void interpreted_frame_oop_map(InterpreterOopMap* mask) const;
224
225 // returns the sending frame
226 inline frame sender(RegisterMap* map) const;
227
228 bool safe_for_sender(JavaThread *thread);
229
230 // returns the sender, but skips conversion frames
231 frame real_sender(RegisterMap* map) const;
232
233 // returns the sending Java frame, skipping any intermediate C frames
234 // NB: receiver must not be first frame
235 frame java_sender() const;
236
237 private:
238 // Helper methods for better factored code in frame::sender
239 inline frame sender_for_compiled_frame(RegisterMap* map) const;
240 frame sender_for_entry_frame(RegisterMap* map) const;
241 frame sender_for_interpreter_frame(RegisterMap* map) const;
242 frame sender_for_upcall_stub_frame(RegisterMap* map) const;
243
244 bool is_entry_frame_valid(JavaThread* thread) const;
245
246 Method* safe_interpreter_frame_method() const;
247
248 // All frames:
249
250 // A low-level interface for vframes:
251
252 public:
253
254 intptr_t* addr_at(int index) const { return &fp()[index]; }
255 intptr_t at_absolute(int index) const { return *addr_at(index); }
256 // Interpreter frames in continuation stacks are on the heap, and internal addresses are relative to fp.
257 intptr_t at_relative(int index) const { return (intptr_t)(fp() + fp()[index]); }
258
259 intptr_t at_relative_or_null(int index) const {
260 return (fp()[index] != 0)
261 ? (intptr_t)(fp() + fp()[index])
262 : 0;
263 }
264
265 intptr_t at(int index) const {
266 return _on_heap ? at_relative(index) : at_absolute(index);
267 }
268
269 public:
270 // Link (i.e., the pointer to the previous frame)
271 // might crash if the frame has no parent
272 intptr_t* link() const;
273
274 // Link (i.e., the pointer to the previous frame) or null if the link cannot be accessed
275 intptr_t* link_or_null() const;
276
277 // Return address
278 address sender_pc() const;
279
280 // Support for deoptimization
281 void deoptimize(JavaThread* thread);
282
283 // The frame's original SP, before any extension by an interpreted callee;
284 // used for packing debug info into vframeArray objects and vframeArray lookup.
285 intptr_t* unextended_sp() const;
286 void set_unextended_sp(intptr_t* value);
287
288 int offset_unextended_sp() const;
289 void set_offset_unextended_sp(int value);
290
291 // returns the stack pointer of the calling frame
292 intptr_t* sender_sp() const;
293
294 // Returns the real 'frame pointer' for the current frame.
295 // This is the value expected by the platform ABI when it defines a
296 // frame pointer register. It may differ from the effective value of
297 // the FP register when that register is used in the JVM for other
298 // purposes (like compiled frames on some platforms).
299 // On other platforms, it is defined so that the stack area used by
300 // this frame goes from real_fp() to sp().
301 intptr_t* real_fp() const;
302
303 // Deoptimization info, if needed (platform dependent).
304 // Stored in the initial_info field of the unroll info, to be used by
305 // the platform dependent deoptimization blobs.
306 intptr_t *initial_deoptimization_info();
307
308 // Interpreter frames:
309
310 private:
311 intptr_t* interpreter_frame_locals() const;
312 intptr_t* interpreter_frame_bcp_addr() const;
313 intptr_t* interpreter_frame_mdp_addr() const;
314
315 public:
316 // Locals
317
318 // The _at version returns a pointer because the address is used for GC.
319 intptr_t* interpreter_frame_local_at(int index) const;
320
321 void interpreter_frame_set_locals(intptr_t* locs);
322
323 // byte code index
324 jint interpreter_frame_bci() const;
325
326 // byte code pointer
327 address interpreter_frame_bcp() const;
328 void interpreter_frame_set_bcp(address bcp);
329
330 // method data pointer
331 address interpreter_frame_mdp() const;
332 void interpreter_frame_set_mdp(address dp);
333
334 // Find receiver out of caller's (compiled) argument list
335 oop retrieve_receiver(RegisterMap *reg_map);
336
337 // Return the monitor owner and BasicLock for compiled synchronized
338 // native methods. Used by JVMTI's GetLocalInstance method
339 // (via VM_GetReceiver) to retrieve the receiver from a native wrapper frame.
340 BasicLock* get_native_monitor() const;
341 oop get_native_receiver() const;
342
343 // Find receiver for an invoke when arguments are just pushed on stack (i.e., callee stack-frame is
344 // not setup)
345 oop interpreter_callee_receiver(Symbol* signature);
346
347
348 oop* interpreter_callee_receiver_addr(Symbol* signature);
349
350
351 // expression stack (may go up or down, direction == 1 or -1)
352 public:
353 intptr_t* interpreter_frame_expression_stack() const;
354
355 // The _at version returns a pointer because the address is used for GC.
356 intptr_t* interpreter_frame_expression_stack_at(jint offset) const;
357
358 // top of expression stack
359 intptr_t* interpreter_frame_tos_at(jint offset) const;
360 intptr_t* interpreter_frame_tos_address() const;
361
362
363 jint interpreter_frame_expression_stack_size() const;
364
365 intptr_t* interpreter_frame_sender_sp() const;
366
367 // template based interpreter deoptimization support
368 void set_interpreter_frame_sender_sp(intptr_t* sender_sp);
369 void interpreter_frame_set_monitor_end(BasicObjectLock* value);
370
371 // Address of the temp oop in the frame. Needed as GC root.
372 oop* interpreter_frame_temp_oop_addr() const;
373
374 // BasicObjectLocks:
375 //
376 // interpreter_frame_monitor_begin is higher in memory than interpreter_frame_monitor_end
377 // Interpreter_frame_monitor_begin points to one element beyond the oldest one,
378 // interpreter_frame_monitor_end points to the youngest one, or if there are none,
379 // it points to one beyond where the first element will be.
380 // interpreter_frame_monitor_size reports the allocation size of a monitor in the interpreter stack.
381 // this value is >= BasicObjectLock::size(), and may be rounded up
382
383 BasicObjectLock* interpreter_frame_monitor_begin() const;
384 BasicObjectLock* interpreter_frame_monitor_end() const;
385 BasicObjectLock* next_monitor_in_interpreter_frame(BasicObjectLock* current) const;
386 BasicObjectLock* previous_monitor_in_interpreter_frame(BasicObjectLock* current) const;
387 static int interpreter_frame_monitor_size();
388 static int interpreter_frame_monitor_size_in_bytes();
389
390 #ifdef ASSERT
391 void interpreter_frame_verify_monitor(BasicObjectLock* value) const;
392 #endif
393
394 // Return/result value from this interpreter frame
395 // If the method return type is T_OBJECT or T_ARRAY populates oop_result
396 // For other (non-T_VOID) the appropriate field in the jvalue is populated
397 // with the result value.
398 // Should only be called when at method exit when the method is not
399 // exiting due to an exception.
400 BasicType interpreter_frame_result(oop* oop_result, jvalue* value_result);
401
402 public:
403 // Method & constant pool cache
404 Method* interpreter_frame_method() const;
405 void interpreter_frame_set_method(Method* method);
406 Method** interpreter_frame_method_addr() const;
407 ConstantPoolCache** interpreter_frame_cache_addr() const;
408 oop* interpreter_frame_mirror_addr() const;
409
410 void interpreter_frame_set_mirror(oop mirror);
411
412 public:
413 // Entry frames
414 JavaCallWrapper* entry_frame_call_wrapper() const { return *entry_frame_call_wrapper_addr(); }
415 JavaCallWrapper* entry_frame_call_wrapper_if_safe(JavaThread* thread) const;
416 JavaCallWrapper** entry_frame_call_wrapper_addr() const;
417 intptr_t* entry_frame_argument_at(int offset) const;
418
419 // tells whether there is another chunk of Delta stack above
420 bool entry_frame_is_first() const;
421 bool upcall_stub_frame_is_first() const;
422
423 // Safepoints
424
425 public:
426 oop saved_oop_result(RegisterMap* map) const;
427 void set_saved_oop_result(RegisterMap* map, oop obj);
428
429 static JavaThread** saved_thread_address(const frame& f);
430
431 // For debugging
432 private:
433 const char* print_name() const;
434
435 void describe_pd(FrameValues& values, int frame_no);
436
437 public:
438 void print_value() const { print_value_on(tty); }
439 void print_value_on(outputStream* st) const;
440 void print_on(outputStream* st) const;
441 void interpreter_frame_print_on(outputStream* st) const;
442 void print_on_error(outputStream* st, char* buf, int buflen, bool verbose = false) const;
443 static void print_C_frame(outputStream* st, char* buf, int buflen, address pc);
444 static frame next_frame(frame fr, Thread* t); // For native stack walking
445
446 #ifndef PRODUCT
447 // Add annotated descriptions of memory locations belonging to this frame to values
448 void describe(FrameValues& values, int frame_no, const RegisterMap* reg_map=nullptr, bool top = false);
449 #endif
450
451 // Conversion from a VMReg to physical stack location
452 template <typename RegisterMapT>
453 address oopmapreg_to_location(VMReg reg, const RegisterMapT* reg_map) const;
454 template <typename RegisterMapT>
455 oop* oopmapreg_to_oop_location(VMReg reg, const RegisterMapT* reg_map) const;
456
457 // Oops-do's
458 void oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, const RegisterMap* reg_map, OopClosure* f) const;
459 void oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache = true) const;
460
461 private:
462 void oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const;
463
464 // Iteration of oops
465 void oops_do_internal(OopClosure* f, NMethodClosure* cf,
466 DerivedOopClosure* df, DerivedPointerIterationMode derived_mode,
467 const RegisterMap* map, bool use_interpreter_oop_map_cache) const;
468
469 void oops_entry_do(OopClosure* f, const RegisterMap* map) const;
470 void oops_upcall_do(OopClosure* f, const RegisterMap* map) const;
471 void oops_nmethod_do(OopClosure* f, NMethodClosure* cf,
472 DerivedOopClosure* df, DerivedPointerIterationMode derived_mode,
473 const RegisterMap* map) const;
474 public:
475 // Memory management
476 void oops_do(OopClosure* f, NMethodClosure* cf, const RegisterMap* map) {
477 #if COMPILER2_OR_JVMCI
478 DerivedPointerIterationMode dpim = DerivedPointerTable::is_active() ?
479 DerivedPointerIterationMode::_with_table :
480 DerivedPointerIterationMode::_ignore;
481 #else
482 DerivedPointerIterationMode dpim = DerivedPointerIterationMode::_ignore;;
483 #endif
484 oops_do_internal(f, cf, nullptr, dpim, map, true);
485 }
486
487 void oops_do(OopClosure* f, NMethodClosure* cf, DerivedOopClosure* df, const RegisterMap* map) {
488 oops_do_internal(f, cf, df, DerivedPointerIterationMode::_ignore, map, true);
489 }
490
491 void oops_do(OopClosure* f, NMethodClosure* cf, const RegisterMap* map,
492 DerivedPointerIterationMode derived_mode) const {
493 oops_do_internal(f, cf, nullptr, derived_mode, map, true);
494 }
495
496 void nmethod_do(NMethodClosure* cf) const;
497
498 // RedefineClasses support for finding live interpreted methods on the stack
499 void metadata_do(MetadataClosure* f) const;
500
501 // Verification
502 void verify(const RegisterMap* map) const;
503 #ifdef ASSERT
504 static bool verify_return_pc(address x);
505 // Usage:
506 // assert(frame::verify_return_pc(return_address), "must be a return pc");
507 #endif
508
509 #if INCLUDE_JFR
510 // Static helper routines
511 static address interpreter_bcp(const intptr_t* fp);
512 static address interpreter_return_address(const intptr_t* fp);
513 static intptr_t* interpreter_sender_sp(const intptr_t* fp);
514 static bool is_interpreter_frame_setup_at(const intptr_t* fp, const void* sp);
515 static intptr_t* sender_sp(intptr_t* fp);
516 static intptr_t* link(const intptr_t* fp);
517 static address return_address(const intptr_t* sp);
518 static intptr_t* fp(const intptr_t* sp);
519 #endif
520
521 #include CPU_HEADER(frame)
522
523 };
524
525 #ifndef PRODUCT
526 // A simple class to describe a location on the stack
527 class FrameValue {
528 public:
529 intptr_t* location;
530 char* description;
531 int owner;
532 int priority;
533
534 FrameValue() {
535 location = nullptr;
536 description = nullptr;
537 owner = -1;
538 priority = 0;
539 }
540 };
541
542
543 // A collection of described stack values that can print a symbolic
544 // description of the stack memory. Interpreter frame values can be
545 // in the caller frames so all the values are collected first and then
546 // sorted before being printed.
547 class FrameValues {
548 private:
549 GrowableArray<FrameValue> _values;
550
551 static int compare(FrameValue* a, FrameValue* b) {
552 if (a->location == b->location) {
553 return a->priority - b->priority;
554 }
555 return checked_cast<int>(a->location - b->location);
556 }
557
558 void print_on(outputStream* out, int min_index, int max_index, intptr_t* v0, intptr_t* v1);
559
560 public:
561 // Used by frame functions to describe locations.
562 void describe(int owner, intptr_t* location, const char* description, int priority = 0);
563
564 #ifdef ASSERT
565 void validate();
566 #endif
567 void print(JavaThread* thread) { print_on(thread, tty); }
568 void print_on(JavaThread* thread, outputStream* out);
569 void print(stackChunkOop chunk) { print_on(chunk, tty); }
570 void print_on(stackChunkOop chunk, outputStream* out);
571 };
572
573 #endif
574
575
576 #endif // SHARE_RUNTIME_FRAME_HPP
--- EOF ---