• 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_INTERPRETER_SHADOW_FRAME_H_
18 #define ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_
19 
20 #include <cstdint>
21 #include <cstring>
22 #include <string>
23 
24 #include "base/locks.h"
25 #include "base/macros.h"
26 #include "lock_count_data.h"
27 #include "read_barrier.h"
28 #include "stack_reference.h"
29 #include "verify_object.h"
30 
31 namespace art HIDDEN {
32 
33 namespace mirror {
34 class Object;
35 }  // namespace mirror
36 
37 class ArtMethod;
38 class ShadowFrame;
39 template<class MirrorType> class ObjPtr;
40 class Thread;
41 union JValue;
42 
43 // Forward declaration. Just calls the destructor.
44 struct ShadowFrameDeleter;
45 using ShadowFrameAllocaUniquePtr = std::unique_ptr<ShadowFrame, ShadowFrameDeleter>;
46 
47 // ShadowFrame has 2 possible layouts:
48 //  - interpreter - separate VRegs and reference arrays. References are in the reference array.
49 //  - JNI - just VRegs, but where every VReg holds a reference.
50 class ShadowFrame {
51  private:
52   // Used to keep track of extra state the shadowframe has.
53   enum class FrameFlags : uint32_t {
54     // We have been requested to notify when this frame gets popped.
55     kNotifyFramePop = 1 << 0,
56     // We have been asked to pop this frame off the stack as soon as possible.
57     kForcePopFrame = 1 << 1,
58     // We have been asked to re-execute the last instruction.
59     kForceRetryInst = 1 << 2,
60     // Mark that we expect the next frame to retry the last instruction (used by instrumentation and
61     // debuggers to keep track of required events)
62     kSkipMethodExitEvents = 1 << 3,
63     // Used to suppress exception events caused by other instrumentation events.
64     kSkipNextExceptionEvent = 1 << 4,
65     // Used to specify if DexPCMoveEvents have to be reported. These events will
66     // only be reported if the method has a breakpoint set.
67     kNotifyDexPcMoveEvents = 1 << 5,
68     // Used to specify if ExceptionHandledEvent has to be reported. When enabled these events are
69     // reported when we reach the catch block after an exception was thrown. These events have to
70     // be reported after the DexPCMoveEvent if enabled.
71     kNotifyExceptionHandledEvent = 1 << 6,
72   };
73 
74  public:
75   // Compute size of ShadowFrame in bytes assuming it has a reference array.
ComputeSize(uint32_t num_vregs)76   static size_t ComputeSize(uint32_t num_vregs) {
77     return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
78            (sizeof(StackReference<mirror::Object>) * num_vregs);
79   }
80 
81   // Create ShadowFrame in heap for deoptimization.
CreateDeoptimizedFrame(uint32_t num_vregs,ArtMethod * method,uint32_t dex_pc)82   static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs,
83                                              ArtMethod* method,
84                                              uint32_t dex_pc) {
85     uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
86     return CreateShadowFrameImpl(num_vregs, method, dex_pc, memory);
87   }
88 
89   // Delete a ShadowFrame allocated on the heap for deoptimization.
DeleteDeoptimizedFrame(ShadowFrame * sf)90   static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
91     sf->~ShadowFrame();  // Explicitly destruct.
92     uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
93     delete[] memory;
94   }
95 
96   // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
97   // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
98 #define CREATE_SHADOW_FRAME(num_vregs, method, dex_pc) ({                                    \
99     size_t frame_size = ShadowFrame::ComputeSize(num_vregs);                                 \
100     void* alloca_mem = alloca(frame_size);                                                   \
101     ShadowFrameAllocaUniquePtr(                                                              \
102         ShadowFrame::CreateShadowFrameImpl((num_vregs), (method), (dex_pc), (alloca_mem)));  \
103     })
104 
~ShadowFrame()105   ~ShadowFrame() {}
106 
NumberOfVRegs()107   uint32_t NumberOfVRegs() const {
108     return number_of_vregs_;
109   }
110 
GetDexPC()111   uint32_t GetDexPC() const { return dex_pc_; }
112 
SetDexPC(uint32_t dex_pc)113   void SetDexPC(uint32_t dex_pc) { dex_pc_ = dex_pc; }
114 
GetLink()115   ShadowFrame* GetLink() const {
116     return link_;
117   }
118 
SetLink(ShadowFrame * frame)119   void SetLink(ShadowFrame* frame) {
120     DCHECK_NE(this, frame);
121     DCHECK_EQ(link_, nullptr);
122     link_ = frame;
123   }
124 
ClearLink()125   void ClearLink() {
126     link_ = nullptr;
127   }
128 
GetVReg(size_t i)129   int32_t GetVReg(size_t i) const {
130     DCHECK_LT(i, NumberOfVRegs());
131     const uint32_t* vreg = &vregs_[i];
132     return *reinterpret_cast<const int32_t*>(vreg);
133   }
134 
135   // Shorts are extended to Ints in VRegs.  Interpreter intrinsics needs them as shorts.
GetVRegShort(size_t i)136   int16_t GetVRegShort(size_t i) const {
137     return static_cast<int16_t>(GetVReg(i));
138   }
139 
GetVRegAddr(size_t i)140   uint32_t* GetVRegAddr(size_t i) {
141     return &vregs_[i];
142   }
143 
GetShadowRefAddr(size_t i)144   uint32_t* GetShadowRefAddr(size_t i) {
145     DCHECK_LT(i, NumberOfVRegs());
146     return &vregs_[i + NumberOfVRegs()];
147   }
148 
GetVRegFloat(size_t i)149   float GetVRegFloat(size_t i) const {
150     DCHECK_LT(i, NumberOfVRegs());
151     // NOTE: Strict-aliasing?
152     const uint32_t* vreg = &vregs_[i];
153     return *reinterpret_cast<const float*>(vreg);
154   }
155 
GetVRegLong(size_t i)156   int64_t GetVRegLong(size_t i) const {
157     DCHECK_LT(i + 1, NumberOfVRegs());
158     const uint32_t* vreg = &vregs_[i];
159     using unaligned_int64 __attribute__((aligned(4))) = const int64_t;
160     return *reinterpret_cast<unaligned_int64*>(vreg);
161   }
162 
GetVRegDouble(size_t i)163   double GetVRegDouble(size_t i) const {
164     DCHECK_LT(i + 1, NumberOfVRegs());
165     const uint32_t* vreg = &vregs_[i];
166     using unaligned_double __attribute__((aligned(4))) = const double;
167     return *reinterpret_cast<unaligned_double*>(vreg);
168   }
169 
170   // Look up the reference given its virtual register number.
171   // If this returns non-null then this does not mean the vreg is currently a reference
172   // on non-moving collectors. Check that the raw reg with GetVReg is equal to this if not certain.
173   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
GetVRegReference(size_t i)174   mirror::Object* GetVRegReference(size_t i) const REQUIRES_SHARED(Locks::mutator_lock_) {
175     DCHECK_LT(i, NumberOfVRegs());
176     mirror::Object* ref;
177     ref = References()[i].AsMirrorPtr();
178     ReadBarrier::MaybeAssertToSpaceInvariant(ref);
179     if (kVerifyFlags & kVerifyReads) {
180       VerifyObject(ref);
181     }
182     return ref;
183   }
184 
185   // Get view of vregs as range of consecutive arguments starting at i.
GetVRegArgs(size_t i)186   uint32_t* GetVRegArgs(size_t i) {
187     return &vregs_[i];
188   }
189 
SetVReg(size_t i,int32_t val)190   void SetVReg(size_t i, int32_t val) {
191     DCHECK_LT(i, NumberOfVRegs());
192     uint32_t* vreg = &vregs_[i];
193     *reinterpret_cast<int32_t*>(vreg) = val;
194     // This is needed for moving collectors since these can update the vreg references if they
195     // happen to agree with references in the reference array.
196     References()[i].Clear();
197   }
198 
SetVRegFloat(size_t i,float val)199   void SetVRegFloat(size_t i, float val) {
200     DCHECK_LT(i, NumberOfVRegs());
201     uint32_t* vreg = &vregs_[i];
202     *reinterpret_cast<float*>(vreg) = val;
203     // This is needed for moving collectors since these can update the vreg references if they
204     // happen to agree with references in the reference array.
205     References()[i].Clear();
206   }
207 
SetVRegLong(size_t i,int64_t val)208   void SetVRegLong(size_t i, int64_t val) {
209     DCHECK_LT(i + 1, NumberOfVRegs());
210     uint32_t* vreg = &vregs_[i];
211     using unaligned_int64 __attribute__((aligned(4))) = int64_t;
212     *reinterpret_cast<unaligned_int64*>(vreg) = val;
213     // This is needed for moving collectors since these can update the vreg references if they
214     // happen to agree with references in the reference array.
215     References()[i].Clear();
216     References()[i + 1].Clear();
217   }
218 
SetVRegDouble(size_t i,double val)219   void SetVRegDouble(size_t i, double val) {
220     DCHECK_LT(i + 1, NumberOfVRegs());
221     uint32_t* vreg = &vregs_[i];
222     using unaligned_double __attribute__((aligned(4))) = double;
223     *reinterpret_cast<unaligned_double*>(vreg) = val;
224     // This is needed for moving collectors since these can update the vreg references if they
225     // happen to agree with references in the reference array.
226     References()[i].Clear();
227     References()[i + 1].Clear();
228   }
229 
230   template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
231   void SetVRegReference(size_t i, ObjPtr<mirror::Object> val)
232       REQUIRES_SHARED(Locks::mutator_lock_);
233 
SetMethod(ArtMethod * method)234   void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_) {
235     DCHECK(method != nullptr);
236     DCHECK(method_ != nullptr);
237     method_ = method;
238   }
239 
GetMethod()240   ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_) {
241     DCHECK(method_ != nullptr);
242     return method_;
243   }
244 
245   mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
246 
247   mirror::Object* GetThisObject(uint16_t num_ins) const REQUIRES_SHARED(Locks::mutator_lock_);
248 
Contains(StackReference<mirror::Object> * shadow_frame_entry_obj)249   bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
250     return ((&References()[0] <= shadow_frame_entry_obj) &&
251             (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
252   }
253 
GetLockCountData()254   LockCountData& GetLockCountData() {
255     return lock_count_data_;
256   }
257 
LockCountDataOffset()258   static constexpr size_t LockCountDataOffset() {
259     return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_);
260   }
261 
LinkOffset()262   static constexpr size_t LinkOffset() {
263     return OFFSETOF_MEMBER(ShadowFrame, link_);
264   }
265 
MethodOffset()266   static constexpr size_t MethodOffset() {
267     return OFFSETOF_MEMBER(ShadowFrame, method_);
268   }
269 
DexPCOffset()270   static constexpr size_t DexPCOffset() {
271     return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
272   }
273 
NumberOfVRegsOffset()274   static constexpr size_t NumberOfVRegsOffset() {
275     return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
276   }
277 
VRegsOffset()278   static constexpr size_t VRegsOffset() {
279     return OFFSETOF_MEMBER(ShadowFrame, vregs_);
280   }
281 
282   // Create ShadowFrame for interpreter using provided memory.
CreateShadowFrameImpl(uint32_t num_vregs,ArtMethod * method,uint32_t dex_pc,void * memory)283   static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
284                                             ArtMethod* method,
285                                             uint32_t dex_pc,
286                                             void* memory) {
287     return new (memory) ShadowFrame(num_vregs, method, dex_pc);
288   }
289 
NeedsNotifyPop()290   bool NeedsNotifyPop() const {
291     return GetFrameFlag(FrameFlags::kNotifyFramePop);
292   }
293 
SetNotifyPop(bool notify)294   void SetNotifyPop(bool notify) {
295     UpdateFrameFlag(notify, FrameFlags::kNotifyFramePop);
296   }
297 
GetForcePopFrame()298   bool GetForcePopFrame() const {
299     return GetFrameFlag(FrameFlags::kForcePopFrame);
300   }
301 
SetForcePopFrame(bool enable)302   void SetForcePopFrame(bool enable) {
303     UpdateFrameFlag(enable, FrameFlags::kForcePopFrame);
304   }
305 
GetForceRetryInstruction()306   bool GetForceRetryInstruction() const {
307     return GetFrameFlag(FrameFlags::kForceRetryInst);
308   }
309 
SetForceRetryInstruction(bool enable)310   void SetForceRetryInstruction(bool enable) {
311     UpdateFrameFlag(enable, FrameFlags::kForceRetryInst);
312   }
313 
GetSkipMethodExitEvents()314   bool GetSkipMethodExitEvents() const {
315     return GetFrameFlag(FrameFlags::kSkipMethodExitEvents);
316   }
317 
SetSkipMethodExitEvents(bool enable)318   void SetSkipMethodExitEvents(bool enable) {
319     UpdateFrameFlag(enable, FrameFlags::kSkipMethodExitEvents);
320   }
321 
GetSkipNextExceptionEvent()322   bool GetSkipNextExceptionEvent() const {
323     return GetFrameFlag(FrameFlags::kSkipNextExceptionEvent);
324   }
325 
SetSkipNextExceptionEvent(bool enable)326   void SetSkipNextExceptionEvent(bool enable) {
327     UpdateFrameFlag(enable, FrameFlags::kSkipNextExceptionEvent);
328   }
329 
GetNotifyDexPcMoveEvents()330   bool GetNotifyDexPcMoveEvents() const {
331     return GetFrameFlag(FrameFlags::kNotifyDexPcMoveEvents);
332   }
333 
SetNotifyDexPcMoveEvents(bool enable)334   void SetNotifyDexPcMoveEvents(bool enable) {
335     UpdateFrameFlag(enable, FrameFlags::kNotifyDexPcMoveEvents);
336   }
337 
GetNotifyExceptionHandledEvent()338   bool GetNotifyExceptionHandledEvent() const {
339     return GetFrameFlag(FrameFlags::kNotifyExceptionHandledEvent);
340   }
341 
SetNotifyExceptionHandledEvent(bool enable)342   void SetNotifyExceptionHandledEvent(bool enable) {
343     UpdateFrameFlag(enable, FrameFlags::kNotifyExceptionHandledEvent);
344   }
345 
CheckConsistentVRegs()346   void CheckConsistentVRegs() const {
347     if (kIsDebugBuild) {
348       // A shadow frame visible to GC requires the following rule: for a given vreg,
349       // its vreg reference equivalent should be the same, or null.
350       for (uint32_t i = 0; i < NumberOfVRegs(); ++i) {
351         int32_t reference_value = References()[i].AsVRegValue();
352         CHECK((GetVReg(i) == reference_value) || (reference_value == 0));
353       }
354     }
355   }
356 
357  private:
ShadowFrame(uint32_t num_vregs,ArtMethod * method,uint32_t dex_pc)358   ShadowFrame(uint32_t num_vregs, ArtMethod* method, uint32_t dex_pc)
359       : link_(nullptr),
360         method_(method),
361         number_of_vregs_(num_vregs),
362         dex_pc_(dex_pc),
363         frame_flags_(0) {
364     memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
365   }
366 
UpdateFrameFlag(bool enable,FrameFlags flag)367   void UpdateFrameFlag(bool enable, FrameFlags flag) {
368     if (enable) {
369       frame_flags_ |= static_cast<uint32_t>(flag);
370     } else {
371       frame_flags_ &= ~static_cast<uint32_t>(flag);
372     }
373   }
374 
GetFrameFlag(FrameFlags flag)375   bool GetFrameFlag(FrameFlags flag) const {
376     return (frame_flags_ & static_cast<uint32_t>(flag)) != 0;
377   }
378 
References()379   const StackReference<mirror::Object>* References() const {
380     const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
381     return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
382   }
383 
References()384   StackReference<mirror::Object>* References() {
385     return const_cast<StackReference<mirror::Object>*>(
386         const_cast<const ShadowFrame*>(this)->References());
387   }
388 
389   // Link to previous shadow frame or null.
390   ShadowFrame* link_;
391   ArtMethod* method_;
392   LockCountData lock_count_data_;  // This may contain GC roots when lock counting is active.
393   const uint32_t number_of_vregs_;
394   uint32_t dex_pc_;
395 
396   // This is a set of ShadowFrame::FrameFlags which denote special states this frame is in.
397   // NB alignment requires that this field takes 4 bytes no matter its size. Only 7 bits are
398   // currently used.
399   uint32_t frame_flags_;
400 
401   // This is a two-part array:
402   //  - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
403   //    bytes.
404   //  - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
405   //    ptr-sized.
406   // In other words when a primitive is stored in vX, the second (reference) part of the array will
407   // be null. When a reference is stored in vX, the second (reference) part of the array will be a
408   // copy of vX.
409   uint32_t vregs_[0];
410 
411   DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
412 };
413 
414 struct ShadowFrameDeleter {
operatorShadowFrameDeleter415   inline void operator()(ShadowFrame* frame) {
416     if (frame != nullptr) {
417       frame->~ShadowFrame();
418     }
419   }
420 };
421 
422 }  // namespace art
423 
424 #endif  // ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_
425