• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_RUNTIME_STACK_H_
18 #define ART_RUNTIME_STACK_H_
19 
20 #include <stdint.h>
21 
22 #include <optional>
23 #include <string>
24 
25 #include "base/locks.h"
26 #include "base/macros.h"
27 #include "deoptimization_kind.h"
28 #include "obj_ptr.h"
29 #include "quick/quick_method_frame_info.h"
30 #include "stack_map.h"
31 
32 namespace art {
33 
34 namespace mirror {
35 class Object;
36 }  // namespace mirror
37 
38 class ArtMethod;
39 class Context;
40 class HandleScope;
41 class OatQuickMethodHeader;
42 class ShadowFrame;
43 class Thread;
44 union JValue;
45 
46 // The kind of vreg being accessed in calls to Set/GetVReg.
47 enum VRegKind {
48   kReferenceVReg,
49   kIntVReg,
50   kFloatVReg,
51   kLongLoVReg,
52   kLongHiVReg,
53   kDoubleLoVReg,
54   kDoubleHiVReg,
55   kConstant,
56   kImpreciseConstant,
57   kUndefined,
58 };
59 std::ostream& operator<<(std::ostream& os, VRegKind rhs);
60 
61 /*
62  * Our current stack layout.
63  * The Dalvik registers come first, followed by the
64  * Method*, followed by other special temporaries if any, followed by
65  * regular compiler temporary. As of now we only have the Method* as
66  * as a special compiler temporary.
67  * A compiler temporary can be thought of as a virtual register that
68  * does not exist in the dex but holds intermediate values to help
69  * optimizations and code generation. A special compiler temporary is
70  * one whose location in frame is well known while non-special ones
71  * do not have a requirement on location in frame as long as code
72  * generator itself knows how to access them.
73  *
74  * TODO: Update this documentation?
75  *
76  *     +-------------------------------+
77  *     | IN[ins-1]                     |  {Note: resides in caller's frame}
78  *     |       .                       |
79  *     | IN[0]                         |
80  *     | caller's ArtMethod            |  ... ArtMethod*
81  *     +===============================+  {Note: start of callee's frame}
82  *     | core callee-save spill        |  {variable sized}
83  *     +-------------------------------+
84  *     | fp callee-save spill          |
85  *     +-------------------------------+
86  *     | filler word                   |  {For compatibility, if V[locals-1] used as wide
87  *     +-------------------------------+
88  *     | V[locals-1]                   |
89  *     | V[locals-2]                   |
90  *     |      .                        |
91  *     |      .                        |  ... (reg == 2)
92  *     | V[1]                          |  ... (reg == 1)
93  *     | V[0]                          |  ... (reg == 0) <---- "locals_start"
94  *     +-------------------------------+
95  *     | stack alignment padding       |  {0 to (kStackAlignWords-1) of padding}
96  *     +-------------------------------+
97  *     | Compiler temp region          |  ... (reg >= max_num_special_temps)
98  *     |      .                        |
99  *     |      .                        |
100  *     | V[max_num_special_temps + 1]  |
101  *     | V[max_num_special_temps + 0]  |
102  *     +-------------------------------+
103  *     | OUT[outs-1]                   |
104  *     | OUT[outs-2]                   |
105  *     |       .                       |
106  *     | OUT[0]                        |
107  *     | ArtMethod*                    |  ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned
108  *     +===============================+
109  */
110 
111 class StackVisitor {
112  public:
113   // This enum defines a flag to control whether inlined frames are included
114   // when walking the stack.
115   enum class StackWalkKind {
116     kIncludeInlinedFrames,
117     kSkipInlinedFrames,
118   };
119 
120  protected:
121   StackVisitor(Thread* thread,
122                Context* context,
123                StackWalkKind walk_kind,
124                bool check_suspended = true);
125 
126   bool GetRegisterIfAccessible(uint32_t reg, DexRegisterLocation::Kind kind, uint32_t* val) const
127       REQUIRES_SHARED(Locks::mutator_lock_);
128 
129  public:
~StackVisitor()130   virtual ~StackVisitor() {}
131   StackVisitor(const StackVisitor&) = default;
132   StackVisitor(StackVisitor&&) = default;
133 
134   // Return 'true' if we should continue to visit more frames, 'false' to stop.
135   virtual bool VisitFrame() REQUIRES_SHARED(Locks::mutator_lock_) = 0;
136 
137   enum class CountTransitions {
138     kYes,
139     kNo,
140   };
141 
142   template <CountTransitions kCount = CountTransitions::kYes>
143   void WalkStack(bool include_transitions = false) REQUIRES_SHARED(Locks::mutator_lock_);
144 
145   // Convenience helper function to walk the stack with a lambda as a visitor.
146   template <CountTransitions kCountTransitions = CountTransitions::kYes,
147             typename T>
148   ALWAYS_INLINE static void WalkStack(const T& fn,
149                                       Thread* thread,
150                                       Context* context,
151                                       StackWalkKind walk_kind,
152                                       bool check_suspended = true,
153                                       bool include_transitions = false)
REQUIRES_SHARED(Locks::mutator_lock_)154       REQUIRES_SHARED(Locks::mutator_lock_) {
155     class LambdaStackVisitor : public StackVisitor {
156      public:
157       LambdaStackVisitor(const T& fn,
158                          Thread* thread,
159                          Context* context,
160                          StackWalkKind walk_kind,
161                          bool check_suspended = true)
162           : StackVisitor(thread, context, walk_kind, check_suspended), fn_(fn) {}
163 
164       bool VisitFrame() override REQUIRES_SHARED(Locks::mutator_lock_) {
165         return fn_(this);
166       }
167 
168      private:
169       T fn_;
170     };
171     LambdaStackVisitor visitor(fn, thread, context, walk_kind, check_suspended);
172     visitor.template WalkStack<kCountTransitions>(include_transitions);
173   }
174 
GetThread()175   Thread* GetThread() const {
176     return thread_;
177   }
178 
179   ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_);
180 
181   // Sets this stack frame's method pointer. This requires a full lock of the MutatorLock. This
182   // doesn't work with inlined methods.
183   void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_);
184 
GetOuterMethod()185   ArtMethod* GetOuterMethod() const {
186     return *GetCurrentQuickFrame();
187   }
188 
IsShadowFrame()189   bool IsShadowFrame() const {
190     return cur_shadow_frame_ != nullptr;
191   }
192 
193   uint32_t GetDexPc(bool abort_on_failure = true) const REQUIRES_SHARED(Locks::mutator_lock_);
194 
195   // Returns a vector of the inlined dex pcs, in order from outermost to innermost but it replaces
196   // the innermost one with `handler_dex_pc`. In essence, (outermost dex pc, mid dex pc #1, ..., mid
197   // dex pc #n-1, `handler_dex_pc`).
198   std::vector<uint32_t> ComputeDexPcList(uint32_t handler_dex_pc) const
199       REQUIRES_SHARED(Locks::mutator_lock_);
200 
201   ObjPtr<mirror::Object> GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
202 
203   size_t GetNativePcOffset() const REQUIRES_SHARED(Locks::mutator_lock_);
204 
205   // Returns the height of the stack in the managed stack frames, including transitions.
GetFrameHeight()206   size_t GetFrameHeight() REQUIRES_SHARED(Locks::mutator_lock_) {
207     return GetNumFrames() - cur_depth_ - 1;
208   }
209 
210   // Returns a frame ID for JDWP use, starting from 1.
GetFrameId()211   size_t GetFrameId() REQUIRES_SHARED(Locks::mutator_lock_) {
212     return GetFrameHeight() + 1;
213   }
214 
GetNumFrames()215   size_t GetNumFrames() REQUIRES_SHARED(Locks::mutator_lock_) {
216     if (num_frames_ == 0) {
217       num_frames_ = ComputeNumFrames(thread_, walk_kind_);
218     }
219     return num_frames_;
220   }
221 
GetFrameDepth()222   size_t GetFrameDepth() const REQUIRES_SHARED(Locks::mutator_lock_) {
223     return cur_depth_;
224   }
225 
226   // Get the method and dex pc immediately after the one that's currently being visited.
227   bool GetNextMethodAndDexPc(ArtMethod** next_method, uint32_t* next_dex_pc)
228       REQUIRES_SHARED(Locks::mutator_lock_);
229 
230   bool GetVReg(ArtMethod* m,
231                uint16_t vreg,
232                VRegKind kind,
233                uint32_t* val,
234                std::optional<DexRegisterLocation> location = std::optional<DexRegisterLocation>(),
235                bool need_full_register_list = false) const REQUIRES_SHARED(Locks::mutator_lock_);
236 
237   bool GetVRegPair(ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
238                    uint64_t* val) const
239       REQUIRES_SHARED(Locks::mutator_lock_);
240 
241   // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
242   // is triggered to make the values effective.
243   bool SetVReg(ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
244       REQUIRES_SHARED(Locks::mutator_lock_);
245 
246   // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
247   // is triggered to make the values effective.
248   bool SetVRegReference(ArtMethod* m, uint16_t vreg, ObjPtr<mirror::Object> new_value)
249       REQUIRES_SHARED(Locks::mutator_lock_);
250 
251   // Values will be set in debugger shadow frames. Debugger will make sure deoptimization
252   // is triggered to make the values effective.
253   bool SetVRegPair(ArtMethod* m,
254                    uint16_t vreg,
255                    uint64_t new_value,
256                    VRegKind kind_lo,
257                    VRegKind kind_hi)
258       REQUIRES_SHARED(Locks::mutator_lock_);
259 
260   uintptr_t* GetGPRAddress(uint32_t reg) const;
261 
262   uintptr_t GetReturnPc() const REQUIRES_SHARED(Locks::mutator_lock_);
263   uintptr_t GetReturnPcAddr() const REQUIRES_SHARED(Locks::mutator_lock_);
264 
265   void SetReturnPc(uintptr_t new_ret_pc) REQUIRES_SHARED(Locks::mutator_lock_);
266 
IsInInlinedFrame()267   bool IsInInlinedFrame() const {
268     return !current_inline_frames_.empty();
269   }
270 
InlineDepth()271   size_t InlineDepth() const { return current_inline_frames_.size(); }
272 
GetCurrentInlinedFrame()273   InlineInfo GetCurrentInlinedFrame() const {
274     return current_inline_frames_.back();
275   }
276 
GetCurrentInlinedFrames()277   const BitTableRange<InlineInfo>& GetCurrentInlinedFrames() const {
278     return current_inline_frames_;
279   }
280 
GetCurrentQuickFramePc()281   uintptr_t GetCurrentQuickFramePc() const {
282     return cur_quick_frame_pc_;
283   }
284 
GetCurrentQuickFrame()285   ArtMethod** GetCurrentQuickFrame() const {
286     return cur_quick_frame_;
287   }
288 
GetCurrentShadowFrame()289   ShadowFrame* GetCurrentShadowFrame() const {
290     return cur_shadow_frame_;
291   }
292 
293   std::string DescribeLocation() const REQUIRES_SHARED(Locks::mutator_lock_);
294 
295   static size_t ComputeNumFrames(Thread* thread, StackWalkKind walk_kind)
296       REQUIRES_SHARED(Locks::mutator_lock_);
297 
298   static void DescribeStack(Thread* thread) REQUIRES_SHARED(Locks::mutator_lock_);
299 
GetCurrentOatQuickMethodHeader()300   const OatQuickMethodHeader* GetCurrentOatQuickMethodHeader() const {
301     return cur_oat_quick_method_header_;
302   }
303 
304   QuickMethodFrameInfo GetCurrentQuickFrameInfo() const REQUIRES_SHARED(Locks::mutator_lock_);
305 
SetShouldDeoptimizeFlag(DeoptimizeFlagValue value)306   void SetShouldDeoptimizeFlag(DeoptimizeFlagValue value) REQUIRES_SHARED(Locks::mutator_lock_) {
307     uint8_t* should_deoptimize_addr = GetShouldDeoptimizeFlagAddr();
308     *should_deoptimize_addr = *should_deoptimize_addr | static_cast<uint8_t>(value);
309   };
310 
UnsetShouldDeoptimizeFlag(DeoptimizeFlagValue value)311   void UnsetShouldDeoptimizeFlag(DeoptimizeFlagValue value) REQUIRES_SHARED(Locks::mutator_lock_) {
312     uint8_t* should_deoptimize_addr = GetShouldDeoptimizeFlagAddr();
313     *should_deoptimize_addr = *should_deoptimize_addr & ~static_cast<uint8_t>(value);
314   };
315 
GetShouldDeoptimizeFlag()316   uint8_t GetShouldDeoptimizeFlag() const REQUIRES_SHARED(Locks::mutator_lock_) {
317     return *GetShouldDeoptimizeFlagAddr();
318   }
319 
ShouldForceDeoptForRedefinition()320   bool ShouldForceDeoptForRedefinition() const REQUIRES_SHARED(Locks::mutator_lock_) {
321     uint8_t should_deopt_flag = GetShouldDeoptimizeFlag();
322     return (should_deopt_flag &
323             static_cast<uint8_t>(DeoptimizeFlagValue::kForceDeoptForRedefinition)) != 0;
324   }
325 
326   // Return the number of dex register in the map from the outermost frame to the number of inlined
327   // frames indicated by `depth`. If `depth` is 0, grab just the registers from the outermost level.
328   // If it is greater than 0, grab as many inline frames as `depth` indicates.
329   size_t GetNumberOfRegisters(CodeInfo* code_info, int depth) const;
330 
331  private:
332   // Private constructor known in the case that num_frames_ has already been computed.
333   StackVisitor(Thread* thread,
334                Context* context,
335                StackWalkKind walk_kind,
336                size_t num_frames,
337                bool check_suspended = true)
338       REQUIRES_SHARED(Locks::mutator_lock_);
339 
IsAccessibleRegister(uint32_t reg,bool is_float)340   bool IsAccessibleRegister(uint32_t reg, bool is_float) const {
341     return is_float ? IsAccessibleFPR(reg) : IsAccessibleGPR(reg);
342   }
GetRegister(uint32_t reg,bool is_float)343   uintptr_t GetRegister(uint32_t reg, bool is_float) const {
344     DCHECK(IsAccessibleRegister(reg, is_float));
345     return is_float ? GetFPR(reg) : GetGPR(reg);
346   }
347 
348   bool IsAccessibleGPR(uint32_t reg) const;
349   uintptr_t GetGPR(uint32_t reg) const;
350 
351   bool IsAccessibleFPR(uint32_t reg) const;
352   uintptr_t GetFPR(uint32_t reg) const;
353 
354   bool GetVRegFromDebuggerShadowFrame(uint16_t vreg, VRegKind kind, uint32_t* val) const
355       REQUIRES_SHARED(Locks::mutator_lock_);
356   bool GetVRegFromOptimizedCode(ArtMethod* m,
357                                 uint16_t vreg,
358                                 VRegKind kind,
359                                 uint32_t* val,
360                                 bool need_full_register_list = false) const
361       REQUIRES_SHARED(Locks::mutator_lock_);
362 
363   bool GetVRegPairFromDebuggerShadowFrame(uint16_t vreg,
364                                           VRegKind kind_lo,
365                                           VRegKind kind_hi,
366                                           uint64_t* val) const
367       REQUIRES_SHARED(Locks::mutator_lock_);
368   bool GetVRegPairFromOptimizedCode(ArtMethod* m,
369                                     uint16_t vreg,
370                                     VRegKind kind_lo,
371                                     VRegKind kind_hi,
372                                     uint64_t* val) const
373       REQUIRES_SHARED(Locks::mutator_lock_);
374   bool GetVRegFromOptimizedCode(DexRegisterLocation location, uint32_t* val) const
375       REQUIRES_SHARED(Locks::mutator_lock_);
376 
377   ShadowFrame* PrepareSetVReg(ArtMethod* m, uint16_t vreg, bool wide)
378       REQUIRES_SHARED(Locks::mutator_lock_);
379 
380   void ValidateFrame() const REQUIRES_SHARED(Locks::mutator_lock_);
381 
382   ALWAYS_INLINE CodeInfo* GetCurrentInlineInfo() const;
383   ALWAYS_INLINE StackMap* GetCurrentStackMap() const;
384 
385   Thread* const thread_;
386   const StackWalkKind walk_kind_;
387   ShadowFrame* cur_shadow_frame_;
388   ArtMethod** cur_quick_frame_;
389   uintptr_t cur_quick_frame_pc_;
390   const OatQuickMethodHeader* cur_oat_quick_method_header_;
391   // Lazily computed, number of frames in the stack.
392   size_t num_frames_;
393   // Depth of the frame we're currently at.
394   size_t cur_depth_;
395   // Current inlined frames of the method we are currently at.
396   // We keep poping frames from the end as we visit the frames.
397   BitTableRange<InlineInfo> current_inline_frames_;
398 
399   // Cache the most recently decoded inline info data.
400   // The 'current_inline_frames_' refers to this data, so we need to keep it alive anyway.
401   // Marked mutable since the cache fields are updated from const getters.
402   mutable std::pair<const OatQuickMethodHeader*, CodeInfo> cur_inline_info_;
403   mutable std::pair<uintptr_t, StackMap> cur_stack_map_;
404 
405   uint8_t* GetShouldDeoptimizeFlagAddr() const REQUIRES_SHARED(Locks::mutator_lock_);
406 
407  protected:
408   Context* const context_;
409   const bool check_suspended_;
410 };
411 
412 }  // namespace art
413 
414 #endif  // ART_RUNTIME_STACK_H_
415