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