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