blob: bceadc3cd2e55477231ae2ff699aa9a992fbc3b5 [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
17#ifndef ART_SRC_STACK_H_
18#define ART_SRC_STACK_H_
19
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080020#include "dex_file.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070021#include "heap.h"
Elliott Hughesbfe487b2011-10-26 15:48:55 -070022#include "jni.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070023#include "macros.h"
Ian Rogers0399dde2012-06-06 17:09:28 -070024#include "oat/runtime/context.h"
25#include "trace.h"
Elliott Hughes68e76522011-10-05 13:22:16 -070026
27#include <stdint.h>
28
29namespace art {
30
Mathieu Chartier66f19252012-09-18 08:57:04 -070031class AbstractMethod;
Ian Rogers0399dde2012-06-06 17:09:28 -070032class Object;
33class ShadowFrame;
Elliott Hughes08fc03a2012-06-26 17:34:00 -070034class StackIndirectReferenceTable;
Ian Rogers00f7d0e2012-07-19 15:28:27 -070035class ScopedObjectAccess;
Elliott Hughes68e76522011-10-05 13:22:16 -070036class Thread;
37
Ian Rogers0399dde2012-06-06 17:09:28 -070038class ShadowFrame {
Elliott Hughes68e76522011-10-05 13:22:16 -070039 public:
Ian Rogers0399dde2012-06-06 17:09:28 -070040 // Number of references contained within this shadow frame
41 uint32_t NumberOfReferences() const {
42 return number_of_references_;
43 }
Elliott Hughes68e76522011-10-05 13:22:16 -070044
Ian Rogers0399dde2012-06-06 17:09:28 -070045 void SetNumberOfReferences(uint32_t number_of_references) {
46 number_of_references_ = number_of_references;
47 }
48
49 // Caller dex pc
50 uint32_t GetDexPC() const {
51 return dex_pc_;
52 }
53
54 void SetDexPC(uint32_t dex_pc) {
55 dex_pc_ = dex_pc;
56 }
57
58 // Link to previous shadow frame or NULL
59 ShadowFrame* GetLink() const {
60 return link_;
61 }
62
63 void SetLink(ShadowFrame* frame) {
64 DCHECK_NE(this, frame);
65 link_ = frame;
66 }
67
68 Object* GetReference(size_t i) const {
69 DCHECK_LT(i, number_of_references_);
70 return references_[i];
71 }
72
73 void SetReference(size_t i, Object* object) {
74 DCHECK_LT(i, number_of_references_);
75 references_[i] = object;
76 }
Elliott Hughes91bf6cd2012-02-14 17:27:48 -080077
Mathieu Chartier66f19252012-09-18 08:57:04 -070078 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -070079 DCHECK_NE(method_, static_cast<void*>(NULL));
80 return method_;
Elliott Hughes68e76522011-10-05 13:22:16 -070081 }
82
Mathieu Chartier66f19252012-09-18 08:57:04 -070083 void SetMethod(AbstractMethod* method) {
Ian Rogers0399dde2012-06-06 17:09:28 -070084 DCHECK_NE(method, static_cast<void*>(NULL));
85 method_ = method;
Elliott Hughes68e76522011-10-05 13:22:16 -070086 }
87
Ian Rogers0399dde2012-06-06 17:09:28 -070088 bool Contains(Object** shadow_frame_entry) const {
89 return ((&references_[0] <= shadow_frame_entry) &&
90 (shadow_frame_entry <= (&references_[number_of_references_ - 1])));
Elliott Hughes68e76522011-10-05 13:22:16 -070091 }
92
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070093 template <typename Visitor>
94 void VisitRoots(const Visitor& visitor) {
Ian Rogers0399dde2012-06-06 17:09:28 -070095 size_t num_refs = NumberOfReferences();
96 for (size_t j = 0; j < num_refs; j++) {
97 Object* object = GetReference(j);
98 if (object != NULL) {
Mathieu Chartier6f1c9492012-10-15 12:08:41 -070099 visitor(object, j);
Ian Rogers0399dde2012-06-06 17:09:28 -0700100 }
101 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700102 }
103
Ian Rogers0399dde2012-06-06 17:09:28 -0700104 // Offset of link within shadow frame
105 static size_t LinkOffset() {
106 return OFFSETOF_MEMBER(ShadowFrame, link_);
107 }
108
109 // Offset of method within shadow frame
110 static size_t MethodOffset() {
111 return OFFSETOF_MEMBER(ShadowFrame, method_);
112 }
113
114 // Offset of dex pc within shadow frame
115 static size_t DexPCOffset() {
116 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
117 }
118
119 // Offset of length within shadow frame
120 static size_t NumberOfReferencesOffset() {
121 return OFFSETOF_MEMBER(ShadowFrame, number_of_references_);
122 }
123
124 // Offset of references within shadow frame
125 static size_t ReferencesOffset() {
126 return OFFSETOF_MEMBER(ShadowFrame, references_);
127 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700128
129 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700130 // ShadowFrame should be allocated by the generated code directly.
131 // We should not create new shadow stack in the runtime support function.
132 ~ShadowFrame() {}
Elliott Hughes68e76522011-10-05 13:22:16 -0700133
Ian Rogers0399dde2012-06-06 17:09:28 -0700134 uint32_t number_of_references_;
135 ShadowFrame* link_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700136 AbstractMethod* method_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700137 uint32_t dex_pc_;
138 Object* references_[];
Elliott Hughes68e76522011-10-05 13:22:16 -0700139
Ian Rogers0399dde2012-06-06 17:09:28 -0700140 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700141};
142
Ian Rogers0399dde2012-06-06 17:09:28 -0700143// The managed stack is used to record fragments of managed code stacks. Managed code stacks
144// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
145// necessary for transitions between code using different frame layouts and transitions into native
146// code.
147class PACKED ManagedStack {
148 public:
Ian Rogersca190662012-06-26 15:45:57 -0700149 ManagedStack()
150 : link_(NULL), top_shadow_frame_(NULL), top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
Ian Rogers81d425b2012-09-27 16:03:43 -0700151
152 void PushManagedStackFragment(ManagedStack* fragment) {
153 // Copy this top fragment into given fragment.
154 memcpy(fragment, this, sizeof(ManagedStack));
155 // Clear this fragment, which has become the top.
156 memset(this, 0, sizeof(ManagedStack));
157 // Link our top fragment onto the given fragment.
158 link_ = fragment;
159 }
160
161 void PopManagedStackFragment(const ManagedStack& fragment) {
162 DCHECK(&fragment == link_);
163 // Copy this given fragment back to the top.
164 memcpy(this, &fragment, sizeof(ManagedStack));
165 }
Ian Rogers0399dde2012-06-06 17:09:28 -0700166
167 ManagedStack* GetLink() const {
168 return link_;
169 }
170
Mathieu Chartier66f19252012-09-18 08:57:04 -0700171 AbstractMethod** GetTopQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700172 return top_quick_frame_;
173 }
174
Mathieu Chartier66f19252012-09-18 08:57:04 -0700175 void SetTopQuickFrame(AbstractMethod** top) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700176 top_quick_frame_ = top;
177 }
178
179 uintptr_t GetTopQuickFramePc() const {
180 return top_quick_frame_pc_;
181 }
182
183 void SetTopQuickFramePc(uintptr_t pc) {
184 top_quick_frame_pc_ = pc;
185 }
186
187 static size_t TopQuickFrameOffset() {
188 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
189 }
190
191 static size_t TopQuickFramePcOffset() {
192 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
193 }
194
195 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
196 ShadowFrame* old_frame = top_shadow_frame_;
197 top_shadow_frame_ = new_top_frame;
198 new_top_frame->SetLink(old_frame);
199 return old_frame;
200 }
201
202 ShadowFrame* PopShadowFrame() {
203 CHECK(top_shadow_frame_ != NULL);
204 ShadowFrame* frame = top_shadow_frame_;
205 top_shadow_frame_ = frame->GetLink();
206 return frame;
207 }
208
209 ShadowFrame* GetTopShadowFrame() const {
210 return top_shadow_frame_;
211 }
212
213 static size_t TopShadowFrameOffset() {
214 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
215 }
216
217 size_t NumShadowFrameReferences() const;
218
219 bool ShadowFramesContain(Object** shadow_frame_entry) const;
220
221 private:
222 ManagedStack* link_;
223 ShadowFrame* top_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700224 AbstractMethod** top_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700225 uintptr_t top_quick_frame_pc_;
226};
227
228class StackVisitor {
229 protected:
230 StackVisitor(const ManagedStack* stack, const std::vector<TraceStackFrame>* trace_stack,
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700231 Context* context)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
Ian Rogersca190662012-06-26 15:45:57 -0700233 : stack_start_(stack), trace_stack_(trace_stack), cur_shadow_frame_(NULL),
234 cur_quick_frame_(NULL), cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0),
235 context_(context) {}
Ian Rogers0399dde2012-06-06 17:09:28 -0700236
237 public:
238 virtual ~StackVisitor() {}
239
240 // Return 'true' if we should continue to visit more frames, 'false' to stop.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700241 virtual bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Ian Rogers0399dde2012-06-06 17:09:28 -0700242
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 void WalkStack(bool include_transitions = false)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700244 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700245
Mathieu Chartier66f19252012-09-18 08:57:04 -0700246 AbstractMethod* GetMethod() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700247 if (cur_shadow_frame_ != NULL) {
248 return cur_shadow_frame_->GetMethod();
249 } else if (cur_quick_frame_ != NULL) {
250 return *cur_quick_frame_;
251 } else {
252 return NULL;
253 }
254 }
255
256 bool IsShadowFrame() const {
257 return cur_shadow_frame_ != NULL;
258 }
259
Ian Rogers0c7abda2012-09-19 13:33:42 -0700260 uint32_t GetDexPc() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
261
262 size_t GetNativePcOffset() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
263
Ian Rogers0399dde2012-06-06 17:09:28 -0700264 uintptr_t LoadCalleeSave(int num, size_t frame_size) const {
265 // Callee saves are held at the top of the frame
Mathieu Chartier66f19252012-09-18 08:57:04 -0700266 AbstractMethod* method = GetMethod();
Ian Rogers0399dde2012-06-06 17:09:28 -0700267 DCHECK(method != NULL);
268 byte* save_addr =
269 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
270#if defined(__i386__)
271 save_addr -= kPointerSize; // account for return address
272#endif
273 return *reinterpret_cast<uintptr_t*>(save_addr);
274 }
275
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700276 // Returns the height of the stack in the managed stack frames, including transitions.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700277 size_t GetFrameHeight() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700278 return GetNumFrames() - cur_depth_;
279 }
280
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700281 // Returns a frame ID for JDWP use, starting from 1.
Ian Rogersb726dcb2012-09-05 08:57:23 -0700282 size_t GetFrameId() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700283 return GetFrameHeight() + 1;
284 }
285
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 size_t GetNumFrames() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700287 if (num_frames_ == 0) {
288 num_frames_ = ComputeNumFrames();
289 }
290 return num_frames_;
291 }
292
Mathieu Chartier66f19252012-09-18 08:57:04 -0700293 uint32_t GetVReg(AbstractMethod* m, int vreg) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700294
Mathieu Chartier66f19252012-09-18 08:57:04 -0700295 void SetVReg(AbstractMethod* m, int vreg, uint32_t new_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700297
298 uintptr_t GetGPR(uint32_t reg) const;
299
Mathieu Chartier66f19252012-09-18 08:57:04 -0700300 uint32_t GetVReg(AbstractMethod** cur_quick_frame, const DexFile::CodeItem* code_item,
Ian Rogers0ec569a2012-07-01 16:43:46 -0700301 uint32_t core_spills, uint32_t fp_spills, size_t frame_size, int vreg) const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700302 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
Ian Rogers0ec569a2012-07-01 16:43:46 -0700303 DCHECK_EQ(cur_quick_frame, GetCurrentQuickFrame());
304 byte* vreg_addr = reinterpret_cast<byte*>(cur_quick_frame) + offset;
Ian Rogers0399dde2012-06-06 17:09:28 -0700305 return *reinterpret_cast<uint32_t*>(vreg_addr);
306 }
307
308 uintptr_t GetReturnPc() const;
309
310 void SetReturnPc(uintptr_t new_ret_pc);
311
312 /*
313 * Return sp-relative offset for a Dalvik virtual register, compiler
314 * spill or Method* in bytes using Method*.
315 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
316 * denotes Method* and (reg <= -3) denotes a compiler temp.
317 *
318 * +------------------------+
319 * | IN[ins-1] | {Note: resides in caller's frame}
320 * | . |
321 * | IN[0] |
322 * | caller's Method* |
323 * +========================+ {Note: start of callee's frame}
324 * | core callee-save spill | {variable sized}
325 * +------------------------+
326 * | fp callee-save spill |
327 * +------------------------+
328 * | filler word | {For compatibility, if V[locals-1] used as wide
329 * +------------------------+
330 * | V[locals-1] |
331 * | V[locals-2] |
332 * | . |
333 * | . | ... (reg == 2)
334 * | V[1] | ... (reg == 1)
335 * | V[0] | ... (reg == 0) <---- "locals_start"
336 * +------------------------+
337 * | Compiler temps | ... (reg == -2)
338 * | | ... (reg == -3)
339 * | | ... (reg == -4)
340 * +------------------------+
341 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
342 * +------------------------+
343 * | OUT[outs-1] |
344 * | OUT[outs-2] |
345 * | . |
346 * | OUT[0] |
347 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
348 * +========================+
349 */
350 static int GetVRegOffset(const DexFile::CodeItem* code_item,
Ian Rogersb23a7722012-10-09 16:54:26 -0700351 uint32_t core_spills, uint32_t fp_spills,
352 size_t frame_size, int reg) {
Ian Rogers0399dde2012-06-06 17:09:28 -0700353 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
354 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
355 int num_ins = code_item->ins_size_;
356 int num_regs = code_item->registers_size_ - num_ins;
357 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
358 if (reg == -2) {
359 return 0; // Method*
360 } else if (reg <= -3) {
361 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
362 } else if (reg < num_regs) {
363 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
364 } else {
365 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
366 }
367 }
368
369 uintptr_t GetCurrentQuickFramePc() const {
370 return cur_quick_frame_pc_;
371 }
372
Mathieu Chartier66f19252012-09-18 08:57:04 -0700373 AbstractMethod** GetCurrentQuickFrame() const {
Ian Rogers0399dde2012-06-06 17:09:28 -0700374 return cur_quick_frame_;
375 }
376
377 ShadowFrame* GetCurrentShadowFrame() const {
378 return cur_shadow_frame_;
379 }
380
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700381 StackIndirectReferenceTable* GetCurrentSirt() const {
Mathieu Chartier66f19252012-09-18 08:57:04 -0700382 AbstractMethod** sp = GetCurrentQuickFrame();
Elliott Hughes08fc03a2012-06-26 17:34:00 -0700383 ++sp; // Skip Method*; SIRT comes next;
384 return reinterpret_cast<StackIndirectReferenceTable*>(sp);
385 }
386
Ian Rogers0399dde2012-06-06 17:09:28 -0700387 private:
Ian Rogersb726dcb2012-09-05 08:57:23 -0700388 size_t ComputeNumFrames() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700389
390 TraceStackFrame GetTraceStackFrame(uint32_t depth) const {
391 return trace_stack_->at(trace_stack_->size() - depth - 1);
392 }
393
Ian Rogersb726dcb2012-09-05 08:57:23 -0700394 void SanityCheckFrame() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0399dde2012-06-06 17:09:28 -0700395
396 const ManagedStack* const stack_start_;
397 const std::vector<TraceStackFrame>* const trace_stack_;
398 ShadowFrame* cur_shadow_frame_;
Mathieu Chartier66f19252012-09-18 08:57:04 -0700399 AbstractMethod** cur_quick_frame_;
Ian Rogers0399dde2012-06-06 17:09:28 -0700400 uintptr_t cur_quick_frame_pc_;
401 // Lazily computed, number of frames in the stack.
402 size_t num_frames_;
403 // Depth of the frame we're currently at.
404 size_t cur_depth_;
405 protected:
406 Context* const context_;
407};
408
Elliott Hughes68e76522011-10-05 13:22:16 -0700409} // namespace art
410
411#endif // ART_SRC_STACK_H_