blob: 233e1c36a8a0ceebeeea9832e29a76885afe8677 [file] [log] [blame]
Elliott Hughes68e76522011-10-05 13:22:16 -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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_STACK_H_
18#define ART_RUNTIME_STACK_H_
Elliott Hughes68e76522011-10-05 13:22:16 -070019
Elliott Hughes68e76522011-10-05 13:22:16 -070020#include <stdint.h>
Ian Rogers40e3bac2012-11-20 00:09:14 -080021#include <string>
Elliott Hughes68e76522011-10-05 13:22:16 -070022
Ian Rogersd582fa42014-11-05 23:46:43 -080023#include "arch/instruction_set.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "dex_file.h"
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -080025#include "gc_root.h"
Ian Rogerse63db272014-07-15 15:36:11 -070026#include "mirror/object_reference.h"
27#include "throw_location.h"
28#include "utils.h"
29#include "verify_object.h"
30
Elliott Hughes68e76522011-10-05 13:22:16 -070031namespace art {
32
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033namespace mirror {
Brian Carlstromea46f952013-07-30 01:26:50 -070034 class ArtMethod;
35 class Object;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036} // namespace mirror
37
38class Context;
Ian Rogers0399dde2012-06-06 17:09:28 -070039class ShadowFrame;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070040class HandleScope;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070042class Thread;
43
Ian Rogers2bcb4a42012-11-08 10:39:18 -080044// The kind of vreg being accessed in calls to Set/GetVReg.
45enum VRegKind {
46 kReferenceVReg,
47 kIntVReg,
48 kFloatVReg,
49 kLongLoVReg,
50 kLongHiVReg,
51 kDoubleLoVReg,
52 kDoubleHiVReg,
53 kConstant,
54 kImpreciseConstant,
55 kUndefined,
56};
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070057std::ostream& operator<<(std::ostream& os, const VRegKind& rhs);
Ian Rogers2bcb4a42012-11-08 10:39:18 -080058
Ian Rogersef7d42f2014-01-06 12:55:46 -080059// A reference from the shadow stack to a MirrorType object within the Java heap.
60template<class MirrorType>
61class MANAGED StackReference : public mirror::ObjectReference<false, MirrorType> {
62 public:
63 StackReference<MirrorType>() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
64 : mirror::ObjectReference<false, MirrorType>(nullptr) {}
65
66 static StackReference<MirrorType> FromMirrorPtr(MirrorType* p)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
68 return StackReference<MirrorType>(p);
69 }
70
71 private:
72 StackReference<MirrorType>(MirrorType* p) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
73 : mirror::ObjectReference<false, MirrorType>(p) {}
74};
75
Elliott Hughes956af0f2014-12-11 14:34:28 -080076// ShadowFrame has 2 possible layouts:
Mathieu Chartier67022432012-11-29 18:04:50 -080077// - interpreter - separate VRegs and reference arrays. References are in the reference array.
78// - JNI - just VRegs, but where every VReg holds a reference.
Ian Rogers0399dde2012-06-06 17:09:28 -070079class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070080 public:
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 // Compute size of ShadowFrame in bytes assuming it has a reference array.
Jeff Hao66135192013-05-14 11:02:41 -070082 static size_t ComputeSize(uint32_t num_vregs) {
83 return sizeof(ShadowFrame) + (sizeof(uint32_t) * num_vregs) +
Ian Rogersef7d42f2014-01-06 12:55:46 -080084 (sizeof(StackReference<mirror::Object>) * num_vregs);
Jeff Hao66135192013-05-14 11:02:41 -070085 }
86
87 // Create ShadowFrame in heap for deoptimization.
TDYa127ce4cc0d2012-11-18 16:59:53 -080088 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Brian Carlstromea46f952013-07-30 01:26:50 -070089 mirror::ArtMethod* method, uint32_t dex_pc) {
Jeff Hao66135192013-05-14 11:02:41 -070090 uint8_t* memory = new uint8_t[ComputeSize(num_vregs)];
Sebastien Hertzc61124b2013-09-10 11:44:19 +020091 return Create(num_vregs, link, method, dex_pc, memory);
Jeff Hao66135192013-05-14 11:02:41 -070092 }
93
94 // Create ShadowFrame for interpreter using provided memory.
95 static ShadowFrame* Create(uint32_t num_vregs, ShadowFrame* link,
Brian Carlstromea46f952013-07-30 01:26:50 -070096 mirror::ArtMethod* method, uint32_t dex_pc, void* memory) {
TDYa127ce4cc0d2012-11-18 16:59:53 -080097 ShadowFrame* sf = new (memory) ShadowFrame(num_vregs, link, method, dex_pc, true);
98 return sf;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -070099 }
100 ~ShadowFrame() {}
101
TDYa127ce4cc0d2012-11-18 16:59:53 -0800102 bool HasReferenceArray() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700103 return true;
Ian Rogers0399dde2012-06-06 17:09:28 -0700104 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700105
TDYa127ce4cc0d2012-11-18 16:59:53 -0800106 uint32_t NumberOfVRegs() const {
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700107 return number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700108 }
109
Ian Rogers0399dde2012-06-06 17:09:28 -0700110 uint32_t GetDexPC() const {
111 return dex_pc_;
112 }
113
114 void SetDexPC(uint32_t dex_pc) {
115 dex_pc_ = dex_pc;
116 }
117
Ian Rogers0399dde2012-06-06 17:09:28 -0700118 ShadowFrame* GetLink() const {
119 return link_;
120 }
121
122 void SetLink(ShadowFrame* frame) {
123 DCHECK_NE(this, frame);
124 link_ = frame;
125 }
126
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700127 int32_t GetVReg(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800128 DCHECK_LT(i, NumberOfVRegs());
129 const uint32_t* vreg = &vregs_[i];
130 return *reinterpret_cast<const int32_t*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700131 }
132
133 float GetVRegFloat(size_t i) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800134 DCHECK_LT(i, NumberOfVRegs());
135 // NOTE: Strict-aliasing?
136 const uint32_t* vreg = &vregs_[i];
137 return *reinterpret_cast<const float*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700138 }
139
140 int64_t GetVRegLong(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200141 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800142 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700143 // Alignment attribute required for GCC 4.8
144 typedef const int64_t unaligned_int64 __attribute__ ((aligned (4)));
145 return *reinterpret_cast<unaligned_int64*>(vreg);
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700146 }
147
148 double GetVRegDouble(size_t i) const {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200149 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800150 const uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700151 // Alignment attribute required for GCC 4.8
152 typedef const double unaligned_double __attribute__ ((aligned (4)));
153 return *reinterpret_cast<unaligned_double*>(vreg);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800154 }
155
Mathieu Chartier4e305412014-02-19 10:54:44 -0800156 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 mirror::Object* GetVRegReference(size_t i) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800158 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800159 mirror::Object* ref;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800160 if (HasReferenceArray()) {
Mathieu Chartier4e305412014-02-19 10:54:44 -0800161 ref = References()[i].AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800162 } else {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800163 const uint32_t* vreg_ptr = &vregs_[i];
Mathieu Chartier4e305412014-02-19 10:54:44 -0800164 ref = reinterpret_cast<const StackReference<mirror::Object>*>(vreg_ptr)->AsMirrorPtr();
TDYa127ce4cc0d2012-11-18 16:59:53 -0800165 }
Mathieu Chartier4e305412014-02-19 10:54:44 -0800166 if (kVerifyFlags & kVerifyReads) {
167 VerifyObject(ref);
168 }
169 return ref;
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700170 }
171
Jeff Hao16743632013-05-08 10:59:04 -0700172 // Get view of vregs as range of consecutive arguments starting at i.
173 uint32_t* GetVRegArgs(size_t i) {
174 return &vregs_[i];
175 }
176
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700177 void SetVReg(size_t i, int32_t val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800178 DCHECK_LT(i, NumberOfVRegs());
179 uint32_t* vreg = &vregs_[i];
180 *reinterpret_cast<int32_t*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700181 // This is needed for moving collectors since these can update the vreg references if they
182 // happen to agree with references in the reference array.
183 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700185 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700186 }
187
188 void SetVRegFloat(size_t i, float val) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800189 DCHECK_LT(i, NumberOfVRegs());
190 uint32_t* vreg = &vregs_[i];
191 *reinterpret_cast<float*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700192 // This is needed for moving collectors since these can update the vreg references if they
193 // happen to agree with references in the reference array.
194 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800195 References()[i].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700196 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700197 }
198
199 void SetVRegLong(size_t i, int64_t val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200200 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800201 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700202 // Alignment attribute required for GCC 4.8
203 typedef int64_t unaligned_int64 __attribute__ ((aligned (4)));
204 *reinterpret_cast<unaligned_int64*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700205 // This is needed for moving collectors since these can update the vreg references if they
206 // happen to agree with references in the reference array.
207 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800208 References()[i].Clear();
209 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700210 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700211 }
212
213 void SetVRegDouble(size_t i, double val) {
Sebastien Hertz807a2562013-04-15 09:33:39 +0200214 DCHECK_LT(i, NumberOfVRegs());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800215 uint32_t* vreg = &vregs_[i];
Jeff Haoe47637c2013-09-19 15:13:16 -0700216 // Alignment attribute required for GCC 4.8
217 typedef double unaligned_double __attribute__ ((aligned (4)));
218 *reinterpret_cast<unaligned_double*>(vreg) = val;
Mathieu Chartier590fee92013-09-13 13:46:47 -0700219 // 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 if (kMovingCollector && HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800222 References()[i].Clear();
223 References()[i + 1].Clear();
Mathieu Chartier590fee92013-09-13 13:46:47 -0700224 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700225 }
226
Mathieu Chartier4e305412014-02-19 10:54:44 -0800227 template<VerifyObjectFlags kVerifyFlags = kDefaultVerifyFlags>
Ian Rogersef7d42f2014-01-06 12:55:46 -0800228 void SetVRegReference(size_t i, mirror::Object* val) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800229 DCHECK_LT(i, NumberOfVRegs());
Mathieu Chartier4e305412014-02-19 10:54:44 -0800230 if (kVerifyFlags & kVerifyWrites) {
231 VerifyObject(val);
232 }
TDYa127ce4cc0d2012-11-18 16:59:53 -0800233 uint32_t* vreg = &vregs_[i];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800234 reinterpret_cast<StackReference<mirror::Object>*>(vreg)->Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800235 if (HasReferenceArray()) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800236 References()[i].Assign(val);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800237 }
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700238 }
239
Ian Rogersef7d42f2014-01-06 12:55:46 -0800240 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
241 DCHECK(method_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700242 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -0700243 }
244
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700245 mirror::ArtMethod** GetMethodAddress() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
246 DCHECK(method_ != nullptr);
247 return &method_;
248 }
249
Ian Rogers62d6c772013-02-27 08:32:07 -0800250 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
251
Jeff Haoe701f482013-05-24 11:50:49 -0700252 mirror::Object* GetThisObject(uint16_t num_ins) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
253
Ian Rogers62d6c772013-02-27 08:32:07 -0800254 ThrowLocation GetCurrentLocationForThrow() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
255
Ian Rogersef7d42f2014-01-06 12:55:46 -0800256 bool Contains(StackReference<mirror::Object>* shadow_frame_entry_obj) const {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800257 if (HasReferenceArray()) {
258 return ((&References()[0] <= shadow_frame_entry_obj) &&
259 (shadow_frame_entry_obj <= (&References()[NumberOfVRegs() - 1])));
260 } else {
261 uint32_t* shadow_frame_entry = reinterpret_cast<uint32_t*>(shadow_frame_entry_obj);
262 return ((&vregs_[0] <= shadow_frame_entry) &&
263 (shadow_frame_entry <= (&vregs_[NumberOfVRegs() - 1])));
Ian Rogers0399dde2012-06-06 17:09:28 -0700264 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700265 }
266
Ian Rogers0399dde2012-06-06 17:09:28 -0700267 static size_t LinkOffset() {
268 return OFFSETOF_MEMBER(ShadowFrame, link_);
269 }
270
Ian Rogers0399dde2012-06-06 17:09:28 -0700271 static size_t MethodOffset() {
272 return OFFSETOF_MEMBER(ShadowFrame, method_);
273 }
274
Ian Rogers0399dde2012-06-06 17:09:28 -0700275 static size_t DexPCOffset() {
276 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
277 }
278
Ian Rogers5438ad82012-10-15 17:22:44 -0700279 static size_t NumberOfVRegsOffset() {
280 return OFFSETOF_MEMBER(ShadowFrame, number_of_vregs_);
281 }
282
TDYa127ce4cc0d2012-11-18 16:59:53 -0800283 static size_t VRegsOffset() {
284 return OFFSETOF_MEMBER(ShadowFrame, vregs_);
Ian Rogers5438ad82012-10-15 17:22:44 -0700285 }
286
Elliott Hughes68e76522011-10-05 13:22:16 -0700287 private:
Brian Carlstromea46f952013-07-30 01:26:50 -0700288 ShadowFrame(uint32_t num_vregs, ShadowFrame* link, mirror::ArtMethod* method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800289 uint32_t dex_pc, bool has_reference_array)
TDYa127ce4cc0d2012-11-18 16:59:53 -0800290 : number_of_vregs_(num_vregs), link_(link), method_(method), dex_pc_(dex_pc) {
291 if (has_reference_array) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800292 memset(vregs_, 0, num_vregs * (sizeof(uint32_t) + sizeof(StackReference<mirror::Object>)));
Mathieu Chartier67022432012-11-29 18:04:50 -0800293 } else {
Jeff Haoe701f482013-05-24 11:50:49 -0700294 memset(vregs_, 0, num_vregs * sizeof(uint32_t));
Ian Rogers2fa6b2e2012-10-17 00:10:17 -0700295 }
296 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700297
Ian Rogersef7d42f2014-01-06 12:55:46 -0800298 const StackReference<mirror::Object>* References() const {
Mathieu Chartier67022432012-11-29 18:04:50 -0800299 DCHECK(HasReferenceArray());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800300 const uint32_t* vreg_end = &vregs_[NumberOfVRegs()];
Ian Rogersef7d42f2014-01-06 12:55:46 -0800301 return reinterpret_cast<const StackReference<mirror::Object>*>(vreg_end);
TDYa127ce4cc0d2012-11-18 16:59:53 -0800302 }
303
Ian Rogersef7d42f2014-01-06 12:55:46 -0800304 StackReference<mirror::Object>* References() {
305 return const_cast<StackReference<mirror::Object>*>(const_cast<const ShadowFrame*>(this)->References());
TDYa127ce4cc0d2012-11-18 16:59:53 -0800306 }
307
Ian Rogers8a01a3a2013-05-06 13:25:44 -0700308 const uint32_t number_of_vregs_;
Ian Rogers5438ad82012-10-15 17:22:44 -0700309 // Link to previous shadow frame or NULL.
Ian Rogers0399dde2012-06-06 17:09:28 -0700310 ShadowFrame* link_;
Brian Carlstromea46f952013-07-30 01:26:50 -0700311 mirror::ArtMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700312 uint32_t dex_pc_;
TDYa127ce4cc0d2012-11-18 16:59:53 -0800313 uint32_t vregs_[0];
Elliott Hughes68e76522011-10-05 13:22:16 -0700314
Ian Rogers0399dde2012-06-06 17:09:28 -0700315 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700316};
317
Mathieu Chartiere34fa1d2015-01-14 14:55:47 -0800318class JavaFrameRootInfo : public RootInfo {
319 public:
320 JavaFrameRootInfo(uint32_t thread_id, const StackVisitor* stack_visitor, size_t vreg)
321 : RootInfo(kRootJavaFrame, thread_id), stack_visitor_(stack_visitor), vreg_(vreg) {
322 }
323 virtual void Describe(std::ostream& os) const OVERRIDE
324 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
325
326 private:
327 const StackVisitor* const stack_visitor_;
328 const size_t vreg_;
329};
330
Ian Rogers0399dde2012-06-06 17:09:28 -0700331// The managed stack is used to record fragments of managed code stacks. Managed code stacks
332// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
333// necessary for transitions between code using different frame layouts and transitions into native
334// code.
Ian Rogersdf1ce912012-11-27 17:07:11 -0800335class PACKED(4) ManagedStack {
Ian Rogers0399dde2012-06-06 17:09:28 -0700336 public:
Ian Rogersca190662012-06-26 15:45:57 -0700337 ManagedStack()
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700338 : top_quick_frame_(nullptr), link_(nullptr), top_shadow_frame_(nullptr) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700339
340 void PushManagedStackFragment(ManagedStack* fragment) {
341 // Copy this top fragment into given fragment.
342 memcpy(fragment, this, sizeof(ManagedStack));
343 // Clear this fragment, which has become the top.
344 memset(this, 0, sizeof(ManagedStack));
345 // Link our top fragment onto the given fragment.
346 link_ = fragment;
347 }
348
349 void PopManagedStackFragment(const ManagedStack& fragment) {
350 DCHECK(&fragment == link_);
351 // Copy this given fragment back to the top.
352 memcpy(this, &fragment, sizeof(ManagedStack));
353 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700354
355 ManagedStack* GetLink() const {
356 return link_;
357 }
358
Andreas Gampecf4035a2014-05-28 22:43:01 -0700359 StackReference<mirror::ArtMethod>* GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700360 return top_quick_frame_;
361 }
362
Andreas Gampecf4035a2014-05-28 22:43:01 -0700363 void SetTopQuickFrame(StackReference<mirror::ArtMethod>* top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700364 DCHECK(top_shadow_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700365 top_quick_frame_ = top;
366 }
367
Ian Rogers0399dde2012-06-06 17:09:28 -0700368 static size_t TopQuickFrameOffset() {
369 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
370 }
371
Ian Rogers0399dde2012-06-06 17:09:28 -0700372 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700373 DCHECK(top_quick_frame_ == nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700374 ShadowFrame* old_frame = top_shadow_frame_;
375 top_shadow_frame_ = new_top_frame;
376 new_top_frame->SetLink(old_frame);
377 return old_frame;
378 }
379
380 ShadowFrame* PopShadowFrame() {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700381 DCHECK(top_quick_frame_ == nullptr);
382 CHECK(top_shadow_frame_ != nullptr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700383 ShadowFrame* frame = top_shadow_frame_;
384 top_shadow_frame_ = frame->GetLink();
385 return frame;
386 }
387
388 ShadowFrame* GetTopShadowFrame() const {
389 return top_shadow_frame_;
390 }
391
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800392 void SetTopShadowFrame(ShadowFrame* top) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700393 DCHECK(top_quick_frame_ == nullptr);
Jeff Hao11ffc2d2013-02-01 11:52:17 -0800394 top_shadow_frame_ = top;
395 }
396
Ian Rogers0399dde2012-06-06 17:09:28 -0700397 static size_t TopShadowFrameOffset() {
398 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
399 }
400
Ian Rogersef7d42f2014-01-06 12:55:46 -0800401 size_t NumJniShadowFrameReferences() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700402
Ian Rogersef7d42f2014-01-06 12:55:46 -0800403 bool ShadowFramesContain(StackReference<mirror::Object>* shadow_frame_entry) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700404
405 private:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700406 StackReference<mirror::ArtMethod>* top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700407 ManagedStack* link_;
408 ShadowFrame* top_shadow_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700409};
410
411class StackVisitor {
412 protected:
Ian Rogers7a22fa62013-01-23 12:16:16 -0800413 StackVisitor(Thread* thread, Context* context) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700414
415 public:
416 virtual ~StackVisitor() {}
417
418 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700419 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700420
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700421 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700422 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700423
Ian Rogersef7d42f2014-01-06 12:55:46 -0800424 mirror::ArtMethod* GetMethod() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
425 if (cur_shadow_frame_ != nullptr) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700426 return cur_shadow_frame_->GetMethod();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800427 } else if (cur_quick_frame_ != nullptr) {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700428 return cur_quick_frame_->AsMirrorPtr();
Hiroshi Yamauchi92d1a662014-05-15 21:43:59 -0700429 } else {
430 return nullptr;
431 }
432 }
433
Ian Rogers0399dde2012-06-06 17:09:28 -0700434 bool IsShadowFrame() const {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800435 return cur_shadow_frame_ != nullptr;
Ian Rogers0399dde2012-06-06 17:09:28 -0700436 }
437
Dave Allisonb373e092014-02-20 16:06:36 -0800438 uint32_t GetDexPc(bool abort_on_failure = true) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0c7abda2012-09-19 13:33:42 -0700439
Ian Rogers62d6c772013-02-27 08:32:07 -0800440 mirror::Object* GetThisObject() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
441
Ian Rogers0c7abda2012-09-19 13:33:42 -0700442 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
443
Ian Rogersef7d42f2014-01-06 12:55:46 -0800444 uintptr_t* CalleeSaveAddress(int num, size_t frame_size) const
445 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700446 // Callee saves are held at the top of the frame
Ian Rogersef7d42f2014-01-06 12:55:46 -0800447 DCHECK(GetMethod() != nullptr);
Ian Rogers13735952014-10-08 12:43:28 -0700448 uint8_t* save_addr =
449 reinterpret_cast<uint8_t*>(cur_quick_frame_) + frame_size - ((num + 1) * sizeof(void*));
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800450#if defined(__i386__) || defined(__x86_64__)
Ian Rogers13735952014-10-08 12:43:28 -0700451 save_addr -= sizeof(void*); // account for return address
Ian Rogers0399dde2012-06-06 17:09:28 -0700452#endif
Mathieu Chartier67022432012-11-29 18:04:50 -0800453 return reinterpret_cast<uintptr_t*>(save_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700454 }
455
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700456 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700457 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800458 return GetNumFrames() - cur_depth_ - 1;
Ian Rogers0399dde2012-06-06 17:09:28 -0700459 }
460
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700461 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700462 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700463 return GetFrameHeight() + 1;
464 }
465
Ian Rogersb726dcb2012-09-05 08:57:23 -0700466 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700467 if (num_frames_ == 0) {
Ian Rogers7a22fa62013-01-23 12:16:16 -0800468 num_frames_ = ComputeNumFrames(thread_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700469 }
470 return num_frames_;
471 }
472
Hiroshi Yamauchi649278c2014-08-13 11:12:22 -0700473 size_t GetFrameDepth() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
474 return cur_depth_;
475 }
476
Ian Rogers5cf98192014-05-29 21:31:50 -0700477 // Get the method and dex pc immediately after the one that's currently being visited.
478 bool GetNextMethodAndDexPc(mirror::ArtMethod** next_method, uint32_t* next_dex_pc)
479 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
480
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200481 bool GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind, uint32_t* val) const
Ian Rogers2bcb4a42012-11-08 10:39:18 -0800482 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700483
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200484 uint32_t GetVReg(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind) const
485 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
486 uint32_t val;
487 bool success = GetVReg(m, vreg, kind, &val);
488 CHECK(success) << "Failed to read vreg " << vreg << " of kind " << kind;
489 return val;
490 }
491
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200492 bool GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo, VRegKind kind_hi,
493 uint64_t* val) const
494 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
495
496 uint64_t GetVRegPair(mirror::ArtMethod* m, uint16_t vreg, VRegKind kind_lo,
497 VRegKind kind_hi) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
498 uint64_t val;
499 bool success = GetVRegPair(m, vreg, kind_lo, kind_hi, &val);
500 CHECK(success) << "Failed to read vreg pair " << vreg
501 << " of kind [" << kind_lo << "," << kind_hi << "]";
502 return val;
503 }
504
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200505 bool SetVReg(mirror::ArtMethod* m, uint16_t vreg, uint32_t new_value, VRegKind kind)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700506 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700507
Sebastien Hertzc901dd72014-07-16 11:56:07 +0200508 bool SetVRegPair(mirror::ArtMethod* m, uint16_t vreg, uint64_t new_value,
509 VRegKind kind_lo, VRegKind kind_hi)
510 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
511
Mathieu Chartier815873e2014-02-13 18:02:13 -0800512 uintptr_t* GetGPRAddress(uint32_t reg) const;
Ian Rogers0399dde2012-06-06 17:09:28 -0700513
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700514 // This is a fast-path for getting/setting values in a quick frame.
Andreas Gampecf4035a2014-05-28 22:43:01 -0700515 uint32_t* GetVRegAddr(StackReference<mirror::ArtMethod>* cur_quick_frame,
516 const DexFile::CodeItem* code_item,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800517 uint32_t core_spills, uint32_t fp_spills, size_t frame_size,
518 uint16_t vreg) const {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000519 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg, kRuntimeISA);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700520 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
Ian Rogers13735952014-10-08 12:43:28 -0700521 uint8_t* vreg_addr = reinterpret_cast<uint8_t*>(cur_quick_frame) + offset;
Mathieu Chartier423d2a32013-09-12 17:33:56 -0700522 return reinterpret_cast<uint32_t*>(vreg_addr);
Ian Rogers0399dde2012-06-06 17:09:28 -0700523 }
524
Ian Rogersef7d42f2014-01-06 12:55:46 -0800525 uintptr_t GetReturnPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700526
Ian Rogersef7d42f2014-01-06 12:55:46 -0800527 void SetReturnPc(uintptr_t new_ret_pc) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700528
529 /*
530 * Return sp-relative offset for a Dalvik virtual register, compiler
531 * spill or Method* in bytes using Method*.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700532 * Note that (reg == -1) denotes an invalid Dalvik register. For the
533 * positive values, the Dalvik registers come first, followed by the
534 * Method*, followed by other special temporaries if any, followed by
535 * regular compiler temporary. As of now we only have the Method* as
536 * as a special compiler temporary.
537 * A compiler temporary can be thought of as a virtual register that
538 * does not exist in the dex but holds intermediate values to help
539 * optimizations and code generation. A special compiler temporary is
540 * one whose location in frame is well known while non-special ones
541 * do not have a requirement on location in frame as long as code
542 * generator itself knows how to access them.
Ian Rogers0399dde2012-06-06 17:09:28 -0700543 *
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700544 * +-------------------------------+
545 * | IN[ins-1] | {Note: resides in caller's frame}
546 * | . |
547 * | IN[0] |
548 * | caller's ArtMethod | ... StackReference<ArtMethod>
549 * +===============================+ {Note: start of callee's frame}
550 * | core callee-save spill | {variable sized}
551 * +-------------------------------+
552 * | fp callee-save spill |
553 * +-------------------------------+
554 * | filler word | {For compatibility, if V[locals-1] used as wide
555 * +-------------------------------+
556 * | V[locals-1] |
557 * | V[locals-2] |
558 * | . |
559 * | . | ... (reg == 2)
560 * | V[1] | ... (reg == 1)
561 * | V[0] | ... (reg == 0) <---- "locals_start"
562 * +-------------------------------+
563 * | stack alignment padding | {0 to (kStackAlignWords-1) of padding}
564 * +-------------------------------+
565 * | Compiler temp region | ... (reg >= max_num_special_temps)
566 * | . |
567 * | . |
Brian Carlstrom2cbaccb2014-09-14 20:34:17 -0700568 * | V[max_num_special_temps + 1] |
569 * | V[max_num_special_temps + 0] |
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700570 * +-------------------------------+
571 * | OUT[outs-1] |
572 * | OUT[outs-2] |
573 * | . |
574 * | OUT[0] |
575 * | StackReference<ArtMethod> | ... (reg == num_total_code_regs == special_temp_value) <<== sp, 16-byte aligned
576 * +===============================+
Ian Rogers0399dde2012-06-06 17:09:28 -0700577 */
578 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700579 uint32_t core_spills, uint32_t fp_spills,
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000580 size_t frame_size, int reg, InstructionSet isa) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700581 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700582 DCHECK_NE(reg, -1);
Vladimir Marko81949632014-05-02 11:53:22 +0100583 int spill_size = POPCOUNT(core_spills) * GetBytesPerGprSpillLocation(isa)
584 + POPCOUNT(fp_spills) * GetBytesPerFprSpillLocation(isa)
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000585 + sizeof(uint32_t); // Filler.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700586 int num_regs = code_item->registers_size_ - code_item->ins_size_;
587 int temp_threshold = code_item->registers_size_;
588 const int max_num_special_temps = 1;
589 if (reg == temp_threshold) {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800590 // The current method pointer corresponds to special location on stack.
591 return 0;
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700592 } else if (reg >= temp_threshold + max_num_special_temps) {
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800593 /*
594 * Special temporaries may have custom locations and the logic above deals with that.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700595 * However, non-special temporaries are placed relative to the outs.
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800596 */
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700597 int temps_start = sizeof(StackReference<mirror::ArtMethod>) + code_item->outs_size_ * sizeof(uint32_t);
598 int relative_offset = (reg - (temp_threshold + max_num_special_temps)) * sizeof(uint32_t);
599 return temps_start + relative_offset;
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800600 } else if (reg < num_regs) {
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700601 int locals_start = frame_size - spill_size - num_regs * sizeof(uint32_t);
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800602 return locals_start + (reg * sizeof(uint32_t));
Ian Rogers0399dde2012-06-06 17:09:28 -0700603 } else {
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800604 // Handle ins.
buzbee82818642014-06-04 15:35:41 -0700605 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) +
606 sizeof(StackReference<mirror::ArtMethod>);
Ian Rogers0399dde2012-06-06 17:09:28 -0700607 }
608 }
609
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000610 static int GetOutVROffset(uint16_t out_num, InstructionSet isa) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700611 UNUSED(isa);
buzbee82818642014-06-04 15:35:41 -0700612 // According to stack model, the first out is above the Method referernce.
613 return sizeof(StackReference<mirror::ArtMethod>) + (out_num * sizeof(uint32_t));
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800614 }
615
Ian Rogers0399dde2012-06-06 17:09:28 -0700616 uintptr_t GetCurrentQuickFramePc() const {
617 return cur_quick_frame_pc_;
618 }
619
Andreas Gampecf4035a2014-05-28 22:43:01 -0700620 StackReference<mirror::ArtMethod>* GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700621 return cur_quick_frame_;
622 }
623
624 ShadowFrame* GetCurrentShadowFrame() const {
625 return cur_shadow_frame_;
626 }
627
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700628 HandleScope* GetCurrentHandleScope() const {
Andreas Gampecf4035a2014-05-28 22:43:01 -0700629 StackReference<mirror::ArtMethod>* sp = GetCurrentQuickFrame();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700630 ++sp; // Skip Method*; handle scope comes next;
631 return reinterpret_cast<HandleScope*>(sp);
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700632 }
633
Ian Rogers40e3bac2012-11-20 00:09:14 -0800634 std::string DescribeLocation() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
635
Ian Rogers7a22fa62013-01-23 12:16:16 -0800636 static size_t ComputeNumFrames(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800637
Ian Rogers7a22fa62013-01-23 12:16:16 -0800638 static void DescribeStack(Thread* thread) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers306057f2012-11-26 12:45:53 -0800639
Ian Rogers0399dde2012-06-06 17:09:28 -0700640 private:
Ian Rogers5cf98192014-05-29 21:31:50 -0700641 // Private constructor known in the case that num_frames_ has already been computed.
642 StackVisitor(Thread* thread, Context* context, size_t num_frames)
643 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
644
Sebastien Hertz0bcb2902014-06-17 15:52:45 +0200645 bool GetGPR(uint32_t reg, uintptr_t* val) const;
646 bool SetGPR(uint32_t reg, uintptr_t value);
647 bool GetFPR(uint32_t reg, uintptr_t* val) const;
648 bool SetFPR(uint32_t reg, uintptr_t value);
649
Ian Rogersb726dcb2012-09-05 08:57:23 -0700650 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700651
Ian Rogers7a22fa62013-01-23 12:16:16 -0800652 Thread* const thread_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700653 ShadowFrame* cur_shadow_frame_;
Andreas Gampecf4035a2014-05-28 22:43:01 -0700654 StackReference<mirror::ArtMethod>* cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700655 uintptr_t cur_quick_frame_pc_;
656 // Lazily computed, number of frames in the stack.
657 size_t num_frames_;
658 // Depth of the frame we're currently at.
659 size_t cur_depth_;
Brian Carlstrom0cd7ec22013-07-17 23:40:20 -0700660
Ian Rogers0399dde2012-06-06 17:09:28 -0700661 protected:
662 Context* const context_;
663};
664
Elliott Hughes68e76522011-10-05 13:22:16 -0700665} // namespace art
666
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700667#endif // ART_RUNTIME_STACK_H_