blob: 89818164328aa2906a3ddbfb55dd7169be124a04 [file] [log] [blame]
Andreas Gampe36a296f2017-06-13 14:11:11 -07001/*
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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070020#include <cstdint>
Andreas Gampe36a296f2017-06-13 14:11:11 -070021#include <cstring>
Andreas Gampe36a296f2017-06-13 14:11:11 -070022#include <string>
23
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080024#include "base/locks.h"
Andreas Gampe36a296f2017-06-13 14:11:11 -070025#include "base/macros.h"
Andreas Gampe36a296f2017-06-13 14:11:11 -070026#include "lock_count_data.h"
27#include "read_barrier.h"
28#include "stack_reference.h"
29#include "verify_object.h"
30
31namespace art {
32
33namespace mirror {
Igor Murashkin2ffb7032017-11-08 13:35:21 -080034class Object;
Andreas Gampe36a296f2017-06-13 14:11:11 -070035} // namespace mirror
36
37class ArtMethod;
38class ShadowFrame;
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +010039template<class MirrorType> class ObjPtr;
Andreas Gampe36a296f2017-06-13 14:11:11 -070040class Thread;
41union JValue;
42
43// Forward declaration. Just calls the destructor.
44struct ShadowFrameDeleter;
45using 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.
50class ShadowFrame {
Alex Light0aa7a5a2018-10-10 15:58:14 +000051 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,
Alex Lightb7c640d2019-03-20 15:52:13 -070060 // 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,
Alex Light0aa7a5a2018-10-10 15:58:14 +000063 };
64
Andreas Gampe36a296f2017-06-13 14:11:11 -070065 public:
66 // Compute size of ShadowFrame in bytes assuming it has a reference array.
67 static size_t ComputeSize(uint32_t num_vregs) {
68 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
69 (sizeof(StackReference<mirror::Object>) * num_vregs);
70 }
71
72 // Create ShadowFrame in heap for deoptimization.
73 static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs, ShadowFrame* link,
74 ArtMethod* method, uint32_t dex_pc) {
75 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
76 return CreateShadowFrameImpl(num_vregs, link, method, dex_pc, memory);
77 }
78
79 // Delete a ShadowFrame allocated on the heap for deoptimization.
80 static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
81 sf->~ShadowFrame(); // Explicitly destruct.
82 uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
83 delete[] memory;
84 }
85
86 // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
87 // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
88#define CREATE_SHADOW_FRAME(num_vregs, link, method, dex_pc) ({ \
89 size_t frame_size = ShadowFrame::ComputeSize(num_vregs); \
90 void* alloca_mem = alloca(frame_size); \
91 ShadowFrameAllocaUniquePtr( \
92 ShadowFrame::CreateShadowFrameImpl((num_vregs), (link), (method), (dex_pc), \
93 (alloca_mem))); \
94 })
95
96 ~ShadowFrame() {}
97
98 // TODO(iam): Clean references array up since they're always there,
99 // we don't need to do conditionals.
100 bool HasReferenceArray() const {
101 return true;
102 }
103
104 uint32_t NumberOfVRegs() const {
105 return number_of_vregs_;
106 }
107
108 uint32_t GetDexPC() const {
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800109 return (dex_pc_ptr_ == nullptr) ? dex_pc_ : dex_pc_ptr_ - dex_instructions_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700110 }
111
112 int16_t GetCachedHotnessCountdown() const {
113 return cached_hotness_countdown_;
114 }
115
116 void SetCachedHotnessCountdown(int16_t cached_hotness_countdown) {
117 cached_hotness_countdown_ = cached_hotness_countdown;
118 }
119
120 int16_t GetHotnessCountdown() const {
121 return hotness_countdown_;
122 }
123
124 void SetHotnessCountdown(int16_t hotness_countdown) {
125 hotness_countdown_ = hotness_countdown;
126 }
127
128 void SetDexPC(uint32_t dex_pc) {
129 dex_pc_ = dex_pc;
130 dex_pc_ptr_ = nullptr;
131 }
132
133 ShadowFrame* GetLink() const {
134 return link_;
135 }
136
137 void SetLink(ShadowFrame* frame) {
138 DCHECK_NE(this, frame);
139 link_ = frame;
140 }
141
142 int32_t GetVReg(size_t i) const {
143 DCHECK_LT(i, NumberOfVRegs());
144 const uint32_t* vreg = &vregs_[i];
145 return *reinterpret_cast<const int32_t*>(vreg);
146 }
147
148 // Shorts are extended to Ints in VRegs. Interpreter intrinsics needs them as shorts.
149 int16_t GetVRegShort(size_t i) const {
150 return static_cast<int16_t>(GetVReg(i));
151 }
152
153 uint32_t* GetVRegAddr(size_t i) {
154 return &vregs_[i];
155 }
156
157 uint32_t* GetShadowRefAddr(size_t i) {
158 DCHECK(HasReferenceArray());
159 DCHECK_LT(i, NumberOfVRegs());
160 return &vregs_[i + NumberOfVRegs()];
161 }
162
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800163 const uint16_t* GetDexInstructions() const {
164 return dex_instructions_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700165 }
166
167 float GetVRegFloat(size_t i) const {
168 DCHECK_LT(i, NumberOfVRegs());
169 // NOTE: Strict-aliasing?
170 const uint32_t* vreg = &vregs_[i];
171 return *reinterpret_cast<const float*>(vreg);
172 }
173
174 int64_t GetVRegLong(size_t i) const {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100175 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700176 const uint32_t* vreg = &vregs_[i];
177 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
178 return *reinterpret_cast<unaligned_int64*>(vreg);
179 }
180
181 double GetVRegDouble(size_t i) const {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100182 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700183 const uint32_t* vreg = &vregs_[i];
184 typedef const double unaligned_double __attribute__ ((aligned (4)));
185 return *reinterpret_cast<unaligned_double*>(vreg);
186 }
187
188 // Look up the reference given its virtual register number.
189 // If this returns non-null then this does not mean the vreg is currently a reference
190 // on non-moving collectors. Check that the raw reg with GetVReg is equal to this if not certain.
191 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
192 mirror::Object* GetVRegReference(size_t i) const REQUIRES_SHARED(Locks::mutator_lock_) {
193 DCHECK_LT(i, NumberOfVRegs());
194 mirror::Object* ref;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100195 DCHECK(HasReferenceArray());
196 ref = References()[i].AsMirrorPtr();
Roland Levillaina78f5b62017-09-29 13:50:44 +0100197 ReadBarrier::MaybeAssertToSpaceInvariant(ref);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700198 if (kVerifyFlags & kVerifyReads) {
199 VerifyObject(ref);
200 }
201 return ref;
202 }
203
204 // Get view of vregs as range of consecutive arguments starting at i.
205 uint32_t* GetVRegArgs(size_t i) {
206 return &vregs_[i];
207 }
208
209 void SetVReg(size_t i, int32_t val) {
210 DCHECK_LT(i, NumberOfVRegs());
211 uint32_t* vreg = &vregs_[i];
212 *reinterpret_cast<int32_t*>(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 if (kMovingCollector && HasReferenceArray()) {
216 References()[i].Clear();
217 }
218 }
219
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 if (kMovingCollector && HasReferenceArray()) {
227 References()[i].Clear();
228 }
229 }
230
231 void SetVRegLong(size_t i, int64_t val) {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100232 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700233 uint32_t* vreg = &vregs_[i];
234 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
235 *reinterpret_cast<unaligned_int64*>(vreg) = val;
236 // This is needed for moving collectors since these can update the vreg references if they
237 // happen to agree with references in the reference array.
238 if (kMovingCollector && HasReferenceArray()) {
239 References()[i].Clear();
240 References()[i + 1].Clear();
241 }
242 }
243
244 void SetVRegDouble(size_t i, double val) {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100245 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700246 uint32_t* vreg = &vregs_[i];
247 typedef double unaligned_double __attribute__ ((aligned (4)));
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 if (kMovingCollector && HasReferenceArray()) {
252 References()[i].Clear();
253 References()[i + 1].Clear();
254 }
255 }
256
257 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100258 void SetVRegReference(size_t i, ObjPtr<mirror::Object> val)
259 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700260
261 void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_) {
262 DCHECK(method != nullptr);
263 DCHECK(method_ != nullptr);
264 method_ = method;
265 }
266
267 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_) {
268 DCHECK(method_ != nullptr);
269 return method_;
270 }
271
272 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
273
274 mirror::Object* GetThisObject(uint16_t num_ins) const REQUIRES_SHARED(Locks::mutator_lock_);
275
276 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
277 if (HasReferenceArray()) {
278 return ((&References()[0] <= shadow_frame_entry_obj) &&
279 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
280 } else {
281 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
282 return ((&vregs_[0] <= shadow_frame_entry) &&
283 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
284 }
285 }
286
287 LockCountData& GetLockCountData() {
288 return lock_count_data_;
289 }
290
David Srbecky56de89a2018-10-01 15:32:20 +0100291 static constexpr size_t LockCountDataOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700292 return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_);
293 }
294
David Srbecky56de89a2018-10-01 15:32:20 +0100295 static constexpr size_t LinkOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700296 return OFFSETOF_MEMBER(ShadowFrame, link_);
297 }
298
David Srbecky56de89a2018-10-01 15:32:20 +0100299 static constexpr size_t MethodOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700300 return OFFSETOF_MEMBER(ShadowFrame, method_);
301 }
302
David Srbecky56de89a2018-10-01 15:32:20 +0100303 static constexpr size_t DexPCOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700304 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
305 }
306
David Srbecky56de89a2018-10-01 15:32:20 +0100307 static constexpr size_t NumberOfVRegsOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700308 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
309 }
310
David Srbecky56de89a2018-10-01 15:32:20 +0100311 static constexpr size_t VRegsOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700312 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
313 }
314
David Srbecky56de89a2018-10-01 15:32:20 +0100315 static constexpr size_t ResultRegisterOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700316 return OFFSETOF_MEMBER(ShadowFrame, result_register_);
317 }
318
David Srbecky56de89a2018-10-01 15:32:20 +0100319 static constexpr size_t DexPCPtrOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700320 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_ptr_);
321 }
322
David Srbecky56de89a2018-10-01 15:32:20 +0100323 static constexpr size_t DexInstructionsOffset() {
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800324 return OFFSETOF_MEMBER(ShadowFrame, dex_instructions_);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700325 }
326
David Srbecky56de89a2018-10-01 15:32:20 +0100327 static constexpr size_t CachedHotnessCountdownOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700328 return OFFSETOF_MEMBER(ShadowFrame, cached_hotness_countdown_);
329 }
330
David Srbecky56de89a2018-10-01 15:32:20 +0100331 static constexpr size_t HotnessCountdownOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700332 return OFFSETOF_MEMBER(ShadowFrame, hotness_countdown_);
333 }
334
335 // Create ShadowFrame for interpreter using provided memory.
336 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
337 ShadowFrame* link,
338 ArtMethod* method,
339 uint32_t dex_pc,
340 void* memory) {
341 return new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
342 }
343
344 const uint16_t* GetDexPCPtr() {
345 return dex_pc_ptr_;
346 }
347
348 void SetDexPCPtr(uint16_t* dex_pc_ptr) {
349 dex_pc_ptr_ = dex_pc_ptr;
350 }
351
352 JValue* GetResultRegister() {
353 return result_register_;
354 }
355
Alex Lighte814f9d2017-07-31 16:14:39 -0700356 bool NeedsNotifyPop() const {
Alex Light0aa7a5a2018-10-10 15:58:14 +0000357 return GetFrameFlag(FrameFlags::kNotifyFramePop);
Alex Lighte814f9d2017-07-31 16:14:39 -0700358 }
359
360 void SetNotifyPop(bool notify) {
Alex Light0aa7a5a2018-10-10 15:58:14 +0000361 UpdateFrameFlag(notify, FrameFlags::kNotifyFramePop);
362 }
363
364 bool GetForcePopFrame() const {
365 return GetFrameFlag(FrameFlags::kForcePopFrame);
366 }
367
368 void SetForcePopFrame(bool enable) {
369 UpdateFrameFlag(enable, FrameFlags::kForcePopFrame);
370 }
371
372 bool GetForceRetryInstruction() const {
373 return GetFrameFlag(FrameFlags::kForceRetryInst);
374 }
375
376 void SetForceRetryInstruction(bool enable) {
377 UpdateFrameFlag(enable, FrameFlags::kForceRetryInst);
Alex Lighte814f9d2017-07-31 16:14:39 -0700378 }
379
Alex Lightb7c640d2019-03-20 15:52:13 -0700380 bool GetSkipMethodExitEvents() const {
381 return GetFrameFlag(FrameFlags::kSkipMethodExitEvents);
382 }
383
384 void SetSkipMethodExitEvents(bool enable) {
385 UpdateFrameFlag(enable, FrameFlags::kSkipMethodExitEvents);
386 }
387
Nicolas Geoffray893147c2018-10-29 14:49:30 +0000388 void CheckConsistentVRegs() const {
389 if (kIsDebugBuild) {
390 // A shadow frame visible to GC requires the following rule: for a given vreg,
391 // its vreg reference equivalent should be the same, or null.
392 for (uint32_t i = 0; i < NumberOfVRegs(); ++i) {
393 int32_t reference_value = References()[i].AsVRegValue();
394 CHECK((GetVReg(i) == reference_value) || (reference_value == 0));
395 }
396 }
397 }
398
Andreas Gampe36a296f2017-06-13 14:11:11 -0700399 private:
400 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, ArtMethod* method,
401 uint32_t dex_pc, bool has_reference_array)
402 : link_(link),
403 method_(method),
404 result_register_(nullptr),
405 dex_pc_ptr_(nullptr),
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800406 dex_instructions_(nullptr),
Andreas Gampe36a296f2017-06-13 14:11:11 -0700407 number_of_vregs_(num_vregs),
408 dex_pc_(dex_pc),
409 cached_hotness_countdown_(0),
Alex Lighte814f9d2017-07-31 16:14:39 -0700410 hotness_countdown_(0),
Alex Light0aa7a5a2018-10-10 15:58:14 +0000411 frame_flags_(0) {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700412 // TODO(iam): Remove this parameter, it's an an artifact of portable removal
413 DCHECK(has_reference_array);
414 if (has_reference_array) {
415 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
416 } else {
417 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
418 }
419 }
420
Alex Light0aa7a5a2018-10-10 15:58:14 +0000421 void UpdateFrameFlag(bool enable, FrameFlags flag) {
422 if (enable) {
423 frame_flags_ |= static_cast<uint32_t>(flag);
424 } else {
425 frame_flags_ &= ~static_cast<uint32_t>(flag);
426 }
427 }
428
429 bool GetFrameFlag(FrameFlags flag) const {
430 return (frame_flags_ & static_cast<uint32_t>(flag)) != 0;
431 }
432
Andreas Gampe36a296f2017-06-13 14:11:11 -0700433 const StackReference<mirror::Object>* References() const {
434 DCHECK(HasReferenceArray());
435 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
436 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
437 }
438
439 StackReference<mirror::Object>* References() {
440 return const_cast<StackReference<mirror::Object>*>(
441 const_cast<const ShadowFrame*>(this)->References());
442 }
443
444 // Link to previous shadow frame or null.
445 ShadowFrame* link_;
446 ArtMethod* method_;
447 JValue* result_register_;
448 const uint16_t* dex_pc_ptr_;
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800449 // Dex instruction base of the code item.
450 const uint16_t* dex_instructions_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700451 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active.
452 const uint32_t number_of_vregs_;
453 uint32_t dex_pc_;
454 int16_t cached_hotness_countdown_;
455 int16_t hotness_countdown_;
Alex Light0aa7a5a2018-10-10 15:58:14 +0000456
457 // This is a set of ShadowFrame::FrameFlags which denote special states this frame is in.
458 // NB alignment requires that this field takes 4 bytes no matter its size. Only 3 bits are
459 // currently used.
460 uint32_t frame_flags_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700461
462 // This is a two-part array:
463 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
464 // bytes.
465 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
466 // ptr-sized.
467 // In other words when a primitive is stored in vX, the second (reference) part of the array will
468 // be null. When a reference is stored in vX, the second (reference) part of the array will be a
469 // copy of vX.
470 uint32_t vregs_[0];
471
472 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
473};
474
475struct ShadowFrameDeleter {
476 inline void operator()(ShadowFrame* frame) {
477 if (frame != nullptr) {
478 frame->~ShadowFrame();
479 }
480 }
481};
482
483} // namespace art
484
485#endif // ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_