blob: bc97ace880ee4b92e54ee3b966cd6488547669d1 [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,
David Srbecky8d335b62019-06-06 14:25:42 +010063 // Used to suppress exception events caused by other instrumentation events.
64 kSkipNextExceptionEvent = 1 << 4,
Alex Light0aa7a5a2018-10-10 15:58:14 +000065 };
66
Andreas Gampe36a296f2017-06-13 14:11:11 -070067 public:
68 // Compute size of ShadowFrame in bytes assuming it has a reference array.
69 static size_t ComputeSize(uint32_t num_vregs) {
70 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
71 (sizeof(StackReference<mirror::Object>) * num_vregs);
72 }
73
74 // Create ShadowFrame in heap for deoptimization.
75 static ShadowFrame* CreateDeoptimizedFrame(uint32_t num_vregs, ShadowFrame* link,
76 ArtMethod* method, uint32_t dex_pc) {
77 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
78 return CreateShadowFrameImpl(num_vregs, link, method, dex_pc, memory);
79 }
80
81 // Delete a ShadowFrame allocated on the heap for deoptimization.
82 static void DeleteDeoptimizedFrame(ShadowFrame* sf) {
83 sf->~ShadowFrame(); // Explicitly destruct.
84 uint8_t* memory = reinterpret_cast<uint8_t*>(sf);
85 delete[] memory;
86 }
87
88 // Create a shadow frame in a fresh alloca. This needs to be in the context of the caller.
89 // Inlining doesn't work, the compiler will still undo the alloca. So this needs to be a macro.
90#define CREATE_SHADOW_FRAME(num_vregs, link, method, dex_pc) ({ \
91 size_t frame_size = ShadowFrame::ComputeSize(num_vregs); \
92 void* alloca_mem = alloca(frame_size); \
93 ShadowFrameAllocaUniquePtr( \
94 ShadowFrame::CreateShadowFrameImpl((num_vregs), (link), (method), (dex_pc), \
95 (alloca_mem))); \
96 })
97
98 ~ShadowFrame() {}
99
100 // TODO(iam): Clean references array up since they're always there,
101 // we don't need to do conditionals.
102 bool HasReferenceArray() const {
103 return true;
104 }
105
106 uint32_t NumberOfVRegs() const {
107 return number_of_vregs_;
108 }
109
110 uint32_t GetDexPC() const {
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800111 return (dex_pc_ptr_ == nullptr) ? dex_pc_ : dex_pc_ptr_ - dex_instructions_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700112 }
113
114 int16_t GetCachedHotnessCountdown() const {
115 return cached_hotness_countdown_;
116 }
117
118 void SetCachedHotnessCountdown(int16_t cached_hotness_countdown) {
119 cached_hotness_countdown_ = cached_hotness_countdown;
120 }
121
122 int16_t GetHotnessCountdown() const {
123 return hotness_countdown_;
124 }
125
126 void SetHotnessCountdown(int16_t hotness_countdown) {
127 hotness_countdown_ = hotness_countdown;
128 }
129
130 void SetDexPC(uint32_t dex_pc) {
131 dex_pc_ = dex_pc;
132 dex_pc_ptr_ = nullptr;
133 }
134
135 ShadowFrame* GetLink() const {
136 return link_;
137 }
138
139 void SetLink(ShadowFrame* frame) {
140 DCHECK_NE(this, frame);
141 link_ = frame;
142 }
143
144 int32_t GetVReg(size_t i) const {
145 DCHECK_LT(i, NumberOfVRegs());
146 const uint32_t* vreg = &vregs_[i];
147 return *reinterpret_cast<const int32_t*>(vreg);
148 }
149
150 // Shorts are extended to Ints in VRegs. Interpreter intrinsics needs them as shorts.
151 int16_t GetVRegShort(size_t i) const {
152 return static_cast<int16_t>(GetVReg(i));
153 }
154
155 uint32_t* GetVRegAddr(size_t i) {
156 return &vregs_[i];
157 }
158
159 uint32_t* GetShadowRefAddr(size_t i) {
160 DCHECK(HasReferenceArray());
161 DCHECK_LT(i, NumberOfVRegs());
162 return &vregs_[i + NumberOfVRegs()];
163 }
164
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800165 const uint16_t* GetDexInstructions() const {
166 return dex_instructions_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700167 }
168
169 float GetVRegFloat(size_t i) const {
170 DCHECK_LT(i, NumberOfVRegs());
171 // NOTE: Strict-aliasing?
172 const uint32_t* vreg = &vregs_[i];
173 return *reinterpret_cast<const float*>(vreg);
174 }
175
176 int64_t GetVRegLong(size_t i) const {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100177 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700178 const uint32_t* vreg = &vregs_[i];
179 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
180 return *reinterpret_cast<unaligned_int64*>(vreg);
181 }
182
183 double GetVRegDouble(size_t i) const {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100184 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700185 const uint32_t* vreg = &vregs_[i];
186 typedef const double unaligned_double __attribute__ ((aligned (4)));
187 return *reinterpret_cast<unaligned_double*>(vreg);
188 }
189
190 // Look up the reference given its virtual register number.
191 // If this returns non-null then this does not mean the vreg is currently a reference
192 // on non-moving collectors. Check that the raw reg with GetVReg is equal to this if not certain.
193 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
194 mirror::Object* GetVRegReference(size_t i) const REQUIRES_SHARED(Locks::mutator_lock_) {
195 DCHECK_LT(i, NumberOfVRegs());
196 mirror::Object* ref;
Nicolas Geoffray4cbfadc2018-10-10 16:09:43 +0100197 DCHECK(HasReferenceArray());
198 ref = References()[i].AsMirrorPtr();
Roland Levillaina78f5b62017-09-29 13:50:44 +0100199 ReadBarrier::MaybeAssertToSpaceInvariant(ref);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700200 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.
207 uint32_t* GetVRegArgs(size_t i) {
208 return &vregs_[i];
209 }
210
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 if (kMovingCollector && HasReferenceArray()) {
218 References()[i].Clear();
219 }
220 }
221
222 void SetVRegFloat(size_t i, float val) {
223 DCHECK_LT(i, NumberOfVRegs());
224 uint32_t* vreg = &vregs_[i];
225 *reinterpret_cast<float*>(vreg) = val;
226 // This is needed for moving collectors since these can update the vreg references if they
227 // happen to agree with references in the reference array.
228 if (kMovingCollector && HasReferenceArray()) {
229 References()[i].Clear();
230 }
231 }
232
233 void SetVRegLong(size_t i, int64_t val) {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100234 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700235 uint32_t* vreg = &vregs_[i];
236 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
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 if (kMovingCollector && HasReferenceArray()) {
241 References()[i].Clear();
242 References()[i + 1].Clear();
243 }
244 }
245
246 void SetVRegDouble(size_t i, double val) {
David Srbeckyb8e5ad12018-08-31 07:02:02 +0100247 DCHECK_LT(i + 1, NumberOfVRegs());
Andreas Gampe36a296f2017-06-13 14:11:11 -0700248 uint32_t* vreg = &vregs_[i];
249 typedef double unaligned_double __attribute__ ((aligned (4)));
250 *reinterpret_cast<unaligned_double*>(vreg) = val;
251 // This is needed for moving collectors since these can update the vreg references if they
252 // happen to agree with references in the reference array.
253 if (kMovingCollector && HasReferenceArray()) {
254 References()[i].Clear();
255 References()[i + 1].Clear();
256 }
257 }
258
259 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100260 void SetVRegReference(size_t i, ObjPtr<mirror::Object> val)
261 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700262
263 void SetMethod(ArtMethod* method) REQUIRES(Locks::mutator_lock_) {
264 DCHECK(method != nullptr);
265 DCHECK(method_ != nullptr);
266 method_ = method;
267 }
268
269 ArtMethod* GetMethod() const REQUIRES_SHARED(Locks::mutator_lock_) {
270 DCHECK(method_ != nullptr);
271 return method_;
272 }
273
274 mirror::Object* GetThisObject() const REQUIRES_SHARED(Locks::mutator_lock_);
275
276 mirror::Object* GetThisObject(uint16_t num_ins) const REQUIRES_SHARED(Locks::mutator_lock_);
277
278 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
279 if (HasReferenceArray()) {
280 return ((&References()[0] <= shadow_frame_entry_obj) &&
281 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
282 } else {
283 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
284 return ((&vregs_[0] <= shadow_frame_entry) &&
285 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
286 }
287 }
288
289 LockCountData& GetLockCountData() {
290 return lock_count_data_;
291 }
292
David Srbecky56de89a2018-10-01 15:32:20 +0100293 static constexpr size_t LockCountDataOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700294 return OFFSETOF_MEMBER(ShadowFrame, lock_count_data_);
295 }
296
David Srbecky56de89a2018-10-01 15:32:20 +0100297 static constexpr size_t LinkOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700298 return OFFSETOF_MEMBER(ShadowFrame, link_);
299 }
300
David Srbecky56de89a2018-10-01 15:32:20 +0100301 static constexpr size_t MethodOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700302 return OFFSETOF_MEMBER(ShadowFrame, method_);
303 }
304
David Srbecky56de89a2018-10-01 15:32:20 +0100305 static constexpr size_t DexPCOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700306 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
307 }
308
David Srbecky56de89a2018-10-01 15:32:20 +0100309 static constexpr size_t NumberOfVRegsOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700310 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
311 }
312
David Srbecky56de89a2018-10-01 15:32:20 +0100313 static constexpr size_t VRegsOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700314 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
315 }
316
David Srbecky56de89a2018-10-01 15:32:20 +0100317 static constexpr size_t ResultRegisterOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700318 return OFFSETOF_MEMBER(ShadowFrame, result_register_);
319 }
320
David Srbecky56de89a2018-10-01 15:32:20 +0100321 static constexpr size_t DexPCPtrOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700322 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_ptr_);
323 }
324
David Srbecky56de89a2018-10-01 15:32:20 +0100325 static constexpr size_t DexInstructionsOffset() {
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800326 return OFFSETOF_MEMBER(ShadowFrame, dex_instructions_);
Andreas Gampe36a296f2017-06-13 14:11:11 -0700327 }
328
David Srbecky56de89a2018-10-01 15:32:20 +0100329 static constexpr size_t CachedHotnessCountdownOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700330 return OFFSETOF_MEMBER(ShadowFrame, cached_hotness_countdown_);
331 }
332
David Srbecky56de89a2018-10-01 15:32:20 +0100333 static constexpr size_t HotnessCountdownOffset() {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700334 return OFFSETOF_MEMBER(ShadowFrame, hotness_countdown_);
335 }
336
337 // Create ShadowFrame for interpreter using provided memory.
338 static ShadowFrame* CreateShadowFrameImpl(uint32_t num_vregs,
339 ShadowFrame* link,
340 ArtMethod* method,
341 uint32_t dex_pc,
342 void* memory) {
343 return new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
344 }
345
346 const uint16_t* GetDexPCPtr() {
347 return dex_pc_ptr_;
348 }
349
350 void SetDexPCPtr(uint16_t* dex_pc_ptr) {
351 dex_pc_ptr_ = dex_pc_ptr;
352 }
353
354 JValue* GetResultRegister() {
355 return result_register_;
356 }
357
Alex Lighte814f9d2017-07-31 16:14:39 -0700358 bool NeedsNotifyPop() const {
Alex Light0aa7a5a2018-10-10 15:58:14 +0000359 return GetFrameFlag(FrameFlags::kNotifyFramePop);
Alex Lighte814f9d2017-07-31 16:14:39 -0700360 }
361
362 void SetNotifyPop(bool notify) {
Alex Light0aa7a5a2018-10-10 15:58:14 +0000363 UpdateFrameFlag(notify, FrameFlags::kNotifyFramePop);
364 }
365
366 bool GetForcePopFrame() const {
367 return GetFrameFlag(FrameFlags::kForcePopFrame);
368 }
369
370 void SetForcePopFrame(bool enable) {
371 UpdateFrameFlag(enable, FrameFlags::kForcePopFrame);
372 }
373
374 bool GetForceRetryInstruction() const {
375 return GetFrameFlag(FrameFlags::kForceRetryInst);
376 }
377
378 void SetForceRetryInstruction(bool enable) {
379 UpdateFrameFlag(enable, FrameFlags::kForceRetryInst);
Alex Lighte814f9d2017-07-31 16:14:39 -0700380 }
381
Alex Lightb7c640d2019-03-20 15:52:13 -0700382 bool GetSkipMethodExitEvents() const {
383 return GetFrameFlag(FrameFlags::kSkipMethodExitEvents);
384 }
385
386 void SetSkipMethodExitEvents(bool enable) {
387 UpdateFrameFlag(enable, FrameFlags::kSkipMethodExitEvents);
388 }
389
David Srbecky8d335b62019-06-06 14:25:42 +0100390 bool GetSkipNextExceptionEvent() const {
391 return GetFrameFlag(FrameFlags::kSkipNextExceptionEvent);
392 }
393
394 void SetSkipNextExceptionEvent(bool enable) {
395 UpdateFrameFlag(enable, FrameFlags::kSkipNextExceptionEvent);
396 }
397
Nicolas Geoffray893147c2018-10-29 14:49:30 +0000398 void CheckConsistentVRegs() const {
399 if (kIsDebugBuild) {
400 // A shadow frame visible to GC requires the following rule: for a given vreg,
401 // its vreg reference equivalent should be the same, or null.
402 for (uint32_t i = 0; i < NumberOfVRegs(); ++i) {
403 int32_t reference_value = References()[i].AsVRegValue();
404 CHECK((GetVReg(i) == reference_value) || (reference_value == 0));
405 }
406 }
407 }
408
Andreas Gampe36a296f2017-06-13 14:11:11 -0700409 private:
410 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, ArtMethod* method,
411 uint32_t dex_pc, bool has_reference_array)
412 : link_(link),
413 method_(method),
414 result_register_(nullptr),
415 dex_pc_ptr_(nullptr),
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800416 dex_instructions_(nullptr),
Andreas Gampe36a296f2017-06-13 14:11:11 -0700417 number_of_vregs_(num_vregs),
418 dex_pc_(dex_pc),
419 cached_hotness_countdown_(0),
Alex Lighte814f9d2017-07-31 16:14:39 -0700420 hotness_countdown_(0),
Alex Light0aa7a5a2018-10-10 15:58:14 +0000421 frame_flags_(0) {
Andreas Gampe36a296f2017-06-13 14:11:11 -0700422 // TODO(iam): Remove this parameter, it's an an artifact of portable removal
423 DCHECK(has_reference_array);
424 if (has_reference_array) {
425 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
426 } else {
427 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
428 }
429 }
430
Alex Light0aa7a5a2018-10-10 15:58:14 +0000431 void UpdateFrameFlag(bool enable, FrameFlags flag) {
432 if (enable) {
433 frame_flags_ |= static_cast<uint32_t>(flag);
434 } else {
435 frame_flags_ &= ~static_cast<uint32_t>(flag);
436 }
437 }
438
439 bool GetFrameFlag(FrameFlags flag) const {
440 return (frame_flags_ & static_cast<uint32_t>(flag)) != 0;
441 }
442
Andreas Gampe36a296f2017-06-13 14:11:11 -0700443 const StackReference<mirror::Object>* References() const {
444 DCHECK(HasReferenceArray());
445 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
446 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
447 }
448
449 StackReference<mirror::Object>* References() {
450 return const_cast<StackReference<mirror::Object>*>(
451 const_cast<const ShadowFrame*>(this)->References());
452 }
453
454 // Link to previous shadow frame or null.
455 ShadowFrame* link_;
456 ArtMethod* method_;
457 JValue* result_register_;
458 const uint16_t* dex_pc_ptr_;
Mathieu Chartierfc9555d2017-11-05 16:32:19 -0800459 // Dex instruction base of the code item.
460 const uint16_t* dex_instructions_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700461 LockCountData lock_count_data_; // This may contain GC roots when lock counting is active.
462 const uint32_t number_of_vregs_;
463 uint32_t dex_pc_;
464 int16_t cached_hotness_countdown_;
465 int16_t hotness_countdown_;
Alex Light0aa7a5a2018-10-10 15:58:14 +0000466
467 // This is a set of ShadowFrame::FrameFlags which denote special states this frame is in.
468 // NB alignment requires that this field takes 4 bytes no matter its size. Only 3 bits are
469 // currently used.
470 uint32_t frame_flags_;
Andreas Gampe36a296f2017-06-13 14:11:11 -0700471
472 // This is a two-part array:
473 // - [0..number_of_vregs) holds the raw virtual registers, and each element here is always 4
474 // bytes.
475 // - [number_of_vregs..number_of_vregs*2) holds only reference registers. Each element here is
476 // ptr-sized.
477 // In other words when a primitive is stored in vX, the second (reference) part of the array will
478 // be null. When a reference is stored in vX, the second (reference) part of the array will be a
479 // copy of vX.
480 uint32_t vregs_[0];
481
482 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
483};
484
485struct ShadowFrameDeleter {
486 inline void operator()(ShadowFrame* frame) {
487 if (frame != nullptr) {
488 frame->~ShadowFrame();
489 }
490 }
491};
492
493} // namespace art
494
495#endif // ART_RUNTIME_INTERPRETER_SHADOW_FRAME_H_