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