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 { 112 return (dex_pc_ptr_ == nullptr) ? dex_pc_ : dex_pc_ptr_ - dex_instructions_; 113 } 114 GetCachedHotnessCountdown()115 int16_t GetCachedHotnessCountdown() const { 116 return cached_hotness_countdown_; 117 } 118 SetCachedHotnessCountdown(int16_t cached_hotness_countdown)119 void SetCachedHotnessCountdown(int16_t cached_hotness_countdown) { 120 cached_hotness_countdown_ = cached_hotness_countdown; 121 } 122 GetHotnessCountdown()123 int16_t GetHotnessCountdown() const { 124 return hotness_countdown_; 125 } 126 SetHotnessCountdown(int16_t hotness_countdown)127 void SetHotnessCountdown(int16_t hotness_countdown) { 128 hotness_countdown_ = hotness_countdown; 129 } 130 SetDexPC(uint32_t dex_pc)131 void SetDexPC(uint32_t dex_pc) { 132 dex_pc_ = dex_pc; 133 dex_pc_ptr_ = nullptr; 134 } 135 GetLink()136 ShadowFrame* GetLink() const { 137 return link_; 138 } 139 SetLink(ShadowFrame * frame)140 void SetLink(ShadowFrame* frame) { 141 DCHECK_NE(this, frame); 142 DCHECK_EQ(link_, nullptr); 143 link_ = frame; 144 } 145 ClearLink()146 void ClearLink() { 147 link_ = nullptr; 148 } 149 GetVReg(size_t i)150 int32_t GetVReg(size_t i) const { 151 DCHECK_LT(i, NumberOfVRegs()); 152 const uint32_t* vreg = &vregs_[i]; 153 return *reinterpret_cast<const int32_t*>(vreg); 154 } 155 156 // Shorts are extended to Ints in VRegs. Interpreter intrinsics needs them as shorts. GetVRegShort(size_t i)157 int16_t GetVRegShort(size_t i) const { 158 return static_cast<int16_t>(GetVReg(i)); 159 } 160 GetVRegAddr(size_t i)161 uint32_t* GetVRegAddr(size_t i) { 162 return &vregs_[i]; 163 } 164 GetShadowRefAddr(size_t i)165 uint32_t* GetShadowRefAddr(size_t i) { 166 DCHECK_LT(i, NumberOfVRegs()); 167 return &vregs_[i + NumberOfVRegs()]; 168 } 169 GetDexInstructions()170 const uint16_t* GetDexInstructions() const { 171 return dex_instructions_; 172 } 173 GetVRegFloat(size_t i)174 float GetVRegFloat(size_t i) const { 175 DCHECK_LT(i, NumberOfVRegs()); 176 // NOTE: Strict-aliasing? 177 const uint32_t* vreg = &vregs_[i]; 178 return *reinterpret_cast<const float*>(vreg); 179 } 180 GetVRegLong(size_t i)181 int64_t GetVRegLong(size_t i) const { 182 DCHECK_LT(i + 1, NumberOfVRegs()); 183 const uint32_t* vreg = &vregs_[i]; 184 using unaligned_int64 __attribute__((aligned(4))) = const int64_t; 185 return *reinterpret_cast<unaligned_int64*>(vreg); 186 } 187 GetVRegDouble(size_t i)188 double GetVRegDouble(size_t i) const { 189 DCHECK_LT(i + 1, NumberOfVRegs()); 190 const uint32_t* vreg = &vregs_[i]; 191 using unaligned_double __attribute__((aligned(4))) = const double; 192 return *reinterpret_cast<unaligned_double*>(vreg); 193 } 194 195 // Look up the reference given its virtual register number. 196 // If this returns non-null then this does not mean the vreg is currently a reference 197 // on non-moving collectors. Check that the raw reg with GetVReg is equal to this if not certain. 198 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> GetVRegReference(size_t i)199 mirror::Object* GetVRegReference(size_t i) const REQUIRES_SHARED(Locks::mutator_lock_) { 200 DCHECK_LT(i, NumberOfVRegs()); 201 mirror::Object* ref; 202 ref = References()[i].AsMirrorPtr(); 203 ReadBarrier::MaybeAssertToSpaceInvariant(ref); 204 if (kVerifyFlags & kVerifyReads) { 205 VerifyObject(ref); 206 } 207 return ref; 208 } 209 210 // Get view of vregs as range of consecutive arguments starting at i. GetVRegArgs(size_t i)211 uint32_t* GetVRegArgs(size_t i) { 212 return &vregs_[i]; 213 } 214 SetVReg(size_t i,int32_t val)215 void SetVReg(size_t i, int32_t val) { 216 DCHECK_LT(i, NumberOfVRegs()); 217 uint32_t* vreg = &vregs_[i]; 218 *reinterpret_cast<int32_t*>(vreg) = val; 219 // This is needed for moving collectors since these can update the vreg references if they 220 // happen to agree with references in the reference array. 221 References()[i].Clear(); 222 } 223 SetVRegFloat(size_t i,float val)224 void SetVRegFloat(size_t i, float val) { 225 DCHECK_LT(i, NumberOfVRegs()); 226 uint32_t* vreg = &vregs_[i]; 227 *reinterpret_cast<float*>(vreg) = val; 228 // This is needed for moving collectors since these can update the vreg references if they 229 // happen to agree with references in the reference array. 230 References()[i].Clear(); 231 } 232 SetVRegLong(size_t i,int64_t val)233 void SetVRegLong(size_t i, int64_t val) { 234 DCHECK_LT(i + 1, NumberOfVRegs()); 235 uint32_t* vreg = &vregs_[i]; 236 using unaligned_int64 __attribute__((aligned(4))) = int64_t; 237 *reinterpret_cast<unaligned_int64*>(vreg) = val; 238 // This is needed for moving collectors since these can update the vreg references if they 239 // happen to agree with references in the reference array. 240 References()[i].Clear(); 241 References()[i + 1].Clear(); 242 } 243 SetVRegDouble(size_t i,double val)244 void SetVRegDouble(size_t i, double val) { 245 DCHECK_LT(i + 1, NumberOfVRegs()); 246 uint32_t* vreg = &vregs_[i]; 247 using unaligned_double __attribute__((aligned(4))) = double; 248 *reinterpret_cast<unaligned_double*>(vreg) = val; 249 // This is needed for moving collectors since these can update the vreg references if they 250 // happen to agree with references in the reference array. 251 References()[i].Clear(); 252 References()[i + 1].Clear(); 253 } 254 255 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags> 256 void SetVRegReference(size_t i, ObjPtr<mirror::Object> val) 257 REQUIRES_SHARED(Locks::mutator_lock_); 258 SetMethod(ArtMethod * method)259 void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_) { 260 DCHECK(method != nullptr); 261 DCHECK(method_ != nullptr); 262 method_ = method; 263 } 264 GetMethod()265 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_) { 266 DCHECK(method_ != nullptr); 267 return method_; 268 } 269 270 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_); 271 272 mirror::Object* GetThisObject(uint16_t num_ins) const REQUIRES_SHARED(Locks::mutator_lock_); 273 Contains(StackReference<mirror::Object> * shadow_frame_entry_obj)274 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const { 275 return ((&References()[0] <= shadow_frame_entry_obj) && 276 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1]))); 277 } 278 GetLockCountData()279 LockCountData& GetLockCountData() { 280 return lock_count_data_; 281 } 282 LockCountDataOffset()283 static constexpr size_t LockCountDataOffset() { 284 return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_); 285 } 286 LinkOffset()287 static constexpr size_t LinkOffset() { 288 return OFFSETOF_MEMBER(ShadowFrame, link_); 289 } 290 MethodOffset()291 static constexpr size_t MethodOffset() { 292 return OFFSETOF_MEMBER(ShadowFrame, method_); 293 } 294 DexPCOffset()295 static constexpr size_t DexPCOffset() { 296 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_); 297 } 298 NumberOfVRegsOffset()299 static constexpr size_t NumberOfVRegsOffset() { 300 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_); 301 } 302 VRegsOffset()303 static constexpr size_t VRegsOffset() { 304 return OFFSETOF_MEMBER(ShadowFrame, vregs_); 305 } 306 DexPCPtrOffset()307 static constexpr size_t DexPCPtrOffset() { 308 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_ptr_); 309 } 310 DexInstructionsOffset()311 static constexpr size_t DexInstructionsOffset() { 312 return OFFSETOF_MEMBER(ShadowFrame, dex_instructions_); 313 } 314 CachedHotnessCountdownOffset()315 static constexpr size_t CachedHotnessCountdownOffset() { 316 return OFFSETOF_MEMBER(ShadowFrame, cached_hotness_countdown_); 317 } 318 HotnessCountdownOffset()319 static constexpr size_t HotnessCountdownOffset() { 320 return OFFSETOF_MEMBER(ShadowFrame, hotness_countdown_); 321 } 322 323 // Create ShadowFrame for interpreter using provided memory. CreateShadowFrameImpl(uint32_t num_vregs,ArtMethod * method,uint32_t dex_pc,void * memory)324 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs, 325 ArtMethod* method, 326 uint32_t dex_pc, 327 void* memory) { 328 return new (memory) ShadowFrame(num_vregs, method, dex_pc); 329 } 330 GetDexPCPtr()331 const uint16_t* GetDexPCPtr() { 332 return dex_pc_ptr_; 333 } 334 SetDexPCPtr(uint16_t * dex_pc_ptr)335 void SetDexPCPtr(uint16_t* dex_pc_ptr) { 336 dex_pc_ptr_ = dex_pc_ptr; 337 } 338 NeedsNotifyPop()339 bool NeedsNotifyPop() const { 340 return GetFrameFlag(FrameFlags::kNotifyFramePop); 341 } 342 SetNotifyPop(bool notify)343 void SetNotifyPop(bool notify) { 344 UpdateFrameFlag(notify, FrameFlags::kNotifyFramePop); 345 } 346 GetForcePopFrame()347 bool GetForcePopFrame() const { 348 return GetFrameFlag(FrameFlags::kForcePopFrame); 349 } 350 SetForcePopFrame(bool enable)351 void SetForcePopFrame(bool enable) { 352 UpdateFrameFlag(enable, FrameFlags::kForcePopFrame); 353 } 354 GetForceRetryInstruction()355 bool GetForceRetryInstruction() const { 356 return GetFrameFlag(FrameFlags::kForceRetryInst); 357 } 358 SetForceRetryInstruction(bool enable)359 void SetForceRetryInstruction(bool enable) { 360 UpdateFrameFlag(enable, FrameFlags::kForceRetryInst); 361 } 362 GetSkipMethodExitEvents()363 bool GetSkipMethodExitEvents() const { 364 return GetFrameFlag(FrameFlags::kSkipMethodExitEvents); 365 } 366 SetSkipMethodExitEvents(bool enable)367 void SetSkipMethodExitEvents(bool enable) { 368 UpdateFrameFlag(enable, FrameFlags::kSkipMethodExitEvents); 369 } 370 GetSkipNextExceptionEvent()371 bool GetSkipNextExceptionEvent() const { 372 return GetFrameFlag(FrameFlags::kSkipNextExceptionEvent); 373 } 374 SetSkipNextExceptionEvent(bool enable)375 void SetSkipNextExceptionEvent(bool enable) { 376 UpdateFrameFlag(enable, FrameFlags::kSkipNextExceptionEvent); 377 } 378 GetNotifyDexPcMoveEvents()379 bool GetNotifyDexPcMoveEvents() const { 380 return GetFrameFlag(FrameFlags::kNotifyDexPcMoveEvents); 381 } 382 SetNotifyDexPcMoveEvents(bool enable)383 void SetNotifyDexPcMoveEvents(bool enable) { 384 UpdateFrameFlag(enable, FrameFlags::kNotifyDexPcMoveEvents); 385 } 386 GetNotifyExceptionHandledEvent()387 bool GetNotifyExceptionHandledEvent() const { 388 return GetFrameFlag(FrameFlags::kNotifyExceptionHandledEvent); 389 } 390 SetNotifyExceptionHandledEvent(bool enable)391 void SetNotifyExceptionHandledEvent(bool enable) { 392 UpdateFrameFlag(enable, FrameFlags::kNotifyExceptionHandledEvent); 393 } 394 CheckConsistentVRegs()395 void CheckConsistentVRegs() const { 396 if (kIsDebugBuild) { 397 // A shadow frame visible to GC requires the following rule: for a given vreg, 398 // its vreg reference equivalent should be the same, or null. 399 for (uint32_t i = 0; i < NumberOfVRegs(); ++i) { 400 int32_t reference_value = References()[i].AsVRegValue(); 401 CHECK((GetVReg(i) == reference_value) || (reference_value == 0)); 402 } 403 } 404 } 405 406 private: ShadowFrame(uint32_t num_vregs,ArtMethod * method,uint32_t dex_pc)407 ShadowFrame(uint32_t num_vregs, ArtMethod* method, uint32_t dex_pc) 408 : link_(nullptr), 409 method_(method), 410 dex_pc_ptr_(nullptr), 411 dex_instructions_(nullptr), 412 number_of_vregs_(num_vregs), 413 dex_pc_(dex_pc), 414 cached_hotness_countdown_(0), 415 hotness_countdown_(0), 416 frame_flags_(0) { 417 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>))); 418 } 419 UpdateFrameFlag(bool enable,FrameFlags flag)420 void UpdateFrameFlag(bool enable, FrameFlags flag) { 421 if (enable) { 422 frame_flags_ |= static_cast<uint32_t>(flag); 423 } else { 424 frame_flags_ &= ~static_cast<uint32_t>(flag); 425 } 426 } 427 GetFrameFlag(FrameFlags flag)428 bool GetFrameFlag(FrameFlags flag) const { 429 return (frame_flags_ & static_cast<uint32_t>(flag)) != 0; 430 } 431 References()432 const StackReference<mirror::Object>* References() const { 433 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()]; 434 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end); 435 } 436 References()437 StackReference<mirror::Object>* References() { 438 return const_cast<StackReference<mirror::Object>*>( 439 const_cast<const ShadowFrame*>(this)->References()); 440 } 441 442 // Link to previous shadow frame or null. 443 ShadowFrame* link_; 444 ArtMethod* method_; 445 const uint16_t* dex_pc_ptr_; 446 // Dex instruction base of the code item. 447 const uint16_t* dex_instructions_; 448 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active. 449 const uint32_t number_of_vregs_; 450 uint32_t dex_pc_; 451 int16_t cached_hotness_countdown_; 452 int16_t hotness_countdown_; 453 454 // This is a set of ShadowFrame::FrameFlags which denote special states this frame is in. 455 // NB alignment requires that this field takes 4 bytes no matter its size. Only 7 bits are 456 // currently used. 457 uint32_t frame_flags_; 458 459 // This is a two-part array: 460 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4 461 // bytes. 462 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is 463 // ptr-sized. 464 // In other words when a primitive is stored in vX, the second (reference) part of the array will 465 // be null. When a reference is stored in vX, the second (reference) part of the array will be a 466 // copy of vX. 467 uint32_t vregs_[0]; 468 469 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame); 470 }; 471 472 struct ShadowFrameDeleter { operatorShadowFrameDeleter473 inline void operator()(ShadowFrame* frame) { 474 if (frame != nullptr) { 475 frame->~ShadowFrame(); 476 } 477 } 478 }; 479 480 } // namespace art 481 482 #endif // ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_ 483