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