blob: bd0aee6613fd697fe8e3cf9a825b9a5e81028dfe [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
31class Method;
Ian Rogers0399dde2012-06-06 17:09:28 -070032class Object;
33class ShadowFrame;
Elliott Hughes68e76522011-10-05 13:22:16 -070034class Thread;
35
Elliott Hughesbfe487b2011-10-26 15:48:55 -070036jobject GetThreadStack(JNIEnv*, 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
Elliott Hughes68e76522011-10-05 13:22:16 -070078 Method* 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
Ian Rogers0399dde2012-06-06 17:09:28 -070083 void SetMethod(Method* method) {
84 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
Ian Rogers0399dde2012-06-06 17:09:28 -070093 void VisitRoots(Heap::RootVisitor* visitor, void* arg) {
94 size_t num_refs = NumberOfReferences();
95 for (size_t j = 0; j < num_refs; j++) {
96 Object* object = GetReference(j);
97 if (object != NULL) {
98 visitor(object, arg);
99 }
100 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700101 }
102
Ian Rogers0399dde2012-06-06 17:09:28 -0700103 // Offset of link within shadow frame
104 static size_t LinkOffset() {
105 return OFFSETOF_MEMBER(ShadowFrame, link_);
106 }
107
108 // Offset of method within shadow frame
109 static size_t MethodOffset() {
110 return OFFSETOF_MEMBER(ShadowFrame, method_);
111 }
112
113 // Offset of dex pc within shadow frame
114 static size_t DexPCOffset() {
115 return OFFSETOF_MEMBER(ShadowFrame, dex_pc_);
116 }
117
118 // Offset of length within shadow frame
119 static size_t NumberOfReferencesOffset() {
120 return OFFSETOF_MEMBER(ShadowFrame, number_of_references_);
121 }
122
123 // Offset of references within shadow frame
124 static size_t ReferencesOffset() {
125 return OFFSETOF_MEMBER(ShadowFrame, references_);
126 }
Elliott Hughes68e76522011-10-05 13:22:16 -0700127
128 private:
Ian Rogers0399dde2012-06-06 17:09:28 -0700129 // ShadowFrame should be allocated by the generated code directly.
130 // We should not create new shadow stack in the runtime support function.
131 ~ShadowFrame() {}
Elliott Hughes68e76522011-10-05 13:22:16 -0700132
Ian Rogers0399dde2012-06-06 17:09:28 -0700133 uint32_t number_of_references_;
134 ShadowFrame* link_;
135 Method* method_;
136 uint32_t dex_pc_;
137 Object* references_[];
Elliott Hughes68e76522011-10-05 13:22:16 -0700138
Ian Rogers0399dde2012-06-06 17:09:28 -0700139 DISALLOW_IMPLICIT_CONSTRUCTORS(ShadowFrame);
Elliott Hughes68e76522011-10-05 13:22:16 -0700140};
141
Ian Rogers0399dde2012-06-06 17:09:28 -0700142// The managed stack is used to record fragments of managed code stacks. Managed code stacks
143// may either be shadow frames or lists of frames using fixed frame sizes. Transition records are
144// necessary for transitions between code using different frame layouts and transitions into native
145// code.
146class PACKED ManagedStack {
147 public:
148 ManagedStack() : link_(NULL), top_shadow_frame_(NULL),
149 top_quick_frame_(NULL), top_quick_frame_pc_(0) {}
150 void PushManagedStackFragment(ManagedStack* fragment);
151 void PopManagedStackFragment(const ManagedStack& record);
152
153 ManagedStack* GetLink() const {
154 return link_;
155 }
156
157 Method** GetTopQuickFrame() const {
158 return top_quick_frame_;
159 }
160
161 void SetTopQuickFrame(Method** top) {
162 top_quick_frame_ = top;
163 }
164
165 uintptr_t GetTopQuickFramePc() const {
166 return top_quick_frame_pc_;
167 }
168
169 void SetTopQuickFramePc(uintptr_t pc) {
170 top_quick_frame_pc_ = pc;
171 }
172
173 static size_t TopQuickFrameOffset() {
174 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_);
175 }
176
177 static size_t TopQuickFramePcOffset() {
178 return OFFSETOF_MEMBER(ManagedStack, top_quick_frame_pc_);
179 }
180
181 ShadowFrame* PushShadowFrame(ShadowFrame* new_top_frame) {
182 ShadowFrame* old_frame = top_shadow_frame_;
183 top_shadow_frame_ = new_top_frame;
184 new_top_frame->SetLink(old_frame);
185 return old_frame;
186 }
187
188 ShadowFrame* PopShadowFrame() {
189 CHECK(top_shadow_frame_ != NULL);
190 ShadowFrame* frame = top_shadow_frame_;
191 top_shadow_frame_ = frame->GetLink();
192 return frame;
193 }
194
195 ShadowFrame* GetTopShadowFrame() const {
196 return top_shadow_frame_;
197 }
198
199 static size_t TopShadowFrameOffset() {
200 return OFFSETOF_MEMBER(ManagedStack, top_shadow_frame_);
201 }
202
203 size_t NumShadowFrameReferences() const;
204
205 bool ShadowFramesContain(Object** shadow_frame_entry) const;
206
207 private:
208 ManagedStack* link_;
209 ShadowFrame* top_shadow_frame_;
210 Method** top_quick_frame_;
211 uintptr_t top_quick_frame_pc_;
212};
213
214class StackVisitor {
215 protected:
216 StackVisitor(const ManagedStack* stack, const std::vector<TraceStackFrame>* trace_stack,
217 Context* context = NULL) :
218 stack_start_(stack), trace_stack_(trace_stack), cur_shadow_frame_(NULL), cur_quick_frame_(NULL),
219 cur_quick_frame_pc_(0), num_frames_(0), cur_depth_(0), context_(context) {}
220
221 public:
222 virtual ~StackVisitor() {}
223
224 // Return 'true' if we should continue to visit more frames, 'false' to stop.
225 virtual bool VisitFrame() = 0;
226
227 void WalkStack(bool include_transitions = false);
228
229 Method* GetMethod() const {
230 if (cur_shadow_frame_ != NULL) {
231 return cur_shadow_frame_->GetMethod();
232 } else if (cur_quick_frame_ != NULL) {
233 return *cur_quick_frame_;
234 } else {
235 return NULL;
236 }
237 }
238
239 bool IsShadowFrame() const {
240 return cur_shadow_frame_ != NULL;
241 }
242
243 uintptr_t LoadCalleeSave(int num, size_t frame_size) const {
244 // Callee saves are held at the top of the frame
245 Method* method = GetMethod();
246 DCHECK(method != NULL);
247 byte* save_addr =
248 reinterpret_cast<byte*>(cur_quick_frame_) + frame_size - ((num + 1) * kPointerSize);
249#if defined(__i386__)
250 save_addr -= kPointerSize; // account for return address
251#endif
252 return *reinterpret_cast<uintptr_t*>(save_addr);
253 }
254
255 uint32_t GetDexPc() const;
256
257 // Gets the height of the stack in the managed stack frames, including transitions.
258 size_t GetFrameHeight() {
259 return GetNumFrames() - cur_depth_;
260 }
261
262 // Get a frame ID where 0 is a special value.
263 size_t GetFrameId() {
264 return GetFrameHeight() + 1;
265 }
266
267 size_t GetNumFrames() {
268 if (num_frames_ == 0) {
269 num_frames_ = ComputeNumFrames();
270 }
271 return num_frames_;
272 }
273
274 uint32_t GetVReg(Method* m, int vreg) const;
275
276 void SetVReg(Method* m, int vreg, uint32_t new_value);
277
278 uintptr_t GetGPR(uint32_t reg) const;
279
280 uint32_t GetVReg(const DexFile::CodeItem* code_item, uint32_t core_spills,
281 uint32_t fp_spills, size_t frame_size, int vreg) const {
282 int offset = GetVRegOffset(code_item, core_spills, fp_spills, frame_size, vreg);
283 byte* vreg_addr = reinterpret_cast<byte*>(GetCurrentQuickFrame()) + offset;
284 return *reinterpret_cast<uint32_t*>(vreg_addr);
285 }
286
287 uintptr_t GetReturnPc() const;
288
289 void SetReturnPc(uintptr_t new_ret_pc);
290
291 /*
292 * Return sp-relative offset for a Dalvik virtual register, compiler
293 * spill or Method* in bytes using Method*.
294 * Note that (reg >= 0) refers to a Dalvik register, (reg == -2)
295 * denotes Method* and (reg <= -3) denotes a compiler temp.
296 *
297 * +------------------------+
298 * | IN[ins-1] | {Note: resides in caller's frame}
299 * | . |
300 * | IN[0] |
301 * | caller's Method* |
302 * +========================+ {Note: start of callee's frame}
303 * | core callee-save spill | {variable sized}
304 * +------------------------+
305 * | fp callee-save spill |
306 * +------------------------+
307 * | filler word | {For compatibility, if V[locals-1] used as wide
308 * +------------------------+
309 * | V[locals-1] |
310 * | V[locals-2] |
311 * | . |
312 * | . | ... (reg == 2)
313 * | V[1] | ... (reg == 1)
314 * | V[0] | ... (reg == 0) <---- "locals_start"
315 * +------------------------+
316 * | Compiler temps | ... (reg == -2)
317 * | | ... (reg == -3)
318 * | | ... (reg == -4)
319 * +------------------------+
320 * | stack alignment padding| {0 to (kStackAlignWords-1) of padding}
321 * +------------------------+
322 * | OUT[outs-1] |
323 * | OUT[outs-2] |
324 * | . |
325 * | OUT[0] |
326 * | curMethod* | ... (reg == -1) <<== sp, 16-byte aligned
327 * +========================+
328 */
329 static int GetVRegOffset(const DexFile::CodeItem* code_item,
330 uint32_t core_spills, uint32_t fp_spills,
331 size_t frame_size, int reg) {
332 DCHECK_EQ(frame_size & (kStackAlignment - 1), 0U);
333 int num_spills = __builtin_popcount(core_spills) + __builtin_popcount(fp_spills) + 1; // Filler.
334 int num_ins = code_item->ins_size_;
335 int num_regs = code_item->registers_size_ - num_ins;
336 int locals_start = frame_size - ((num_spills + num_regs) * sizeof(uint32_t));
337 if (reg == -2) {
338 return 0; // Method*
339 } else if (reg <= -3) {
340 return locals_start - ((reg + 1) * sizeof(uint32_t)); // Compiler temp.
341 } else if (reg < num_regs) {
342 return locals_start + (reg * sizeof(uint32_t)); // Dalvik local reg.
343 } else {
344 return frame_size + ((reg - num_regs) * sizeof(uint32_t)) + sizeof(uint32_t); // Dalvik in.
345 }
346 }
347
348 uintptr_t GetCurrentQuickFramePc() const {
349 return cur_quick_frame_pc_;
350 }
351
352 Method** GetCurrentQuickFrame() const {
353 return cur_quick_frame_;
354 }
355
356 ShadowFrame* GetCurrentShadowFrame() const {
357 return cur_shadow_frame_;
358 }
359
360 private:
361 size_t ComputeNumFrames() const;
362
363 TraceStackFrame GetTraceStackFrame(uint32_t depth) const {
364 return trace_stack_->at(trace_stack_->size() - depth - 1);
365 }
366
367 void SanityCheckFrame();
368
369 const ManagedStack* const stack_start_;
370 const std::vector<TraceStackFrame>* const trace_stack_;
371 ShadowFrame* cur_shadow_frame_;
372 Method** cur_quick_frame_;
373 uintptr_t cur_quick_frame_pc_;
374 // Lazily computed, number of frames in the stack.
375 size_t num_frames_;
376 // Depth of the frame we're currently at.
377 size_t cur_depth_;
378 protected:
379 Context* const context_;
380};
381
382static inline uintptr_t AdjustQuickFramePcForDexPcComputation(uintptr_t pc) {
383 // Quick methods record a mapping from quick PCs to Dex PCs at the beginning of the code for
384 // each dex instruction. When walking the stack, the return PC will be set to the instruction
385 // following call which will likely be the start of the next dex instruction. Adjust the PC
386 // for these cases by 2 bytes in case the return PC also has the thumb bit set.
387 if (pc > 0) { pc -= 2; }
388 return pc;
389}
390
Elliott Hughes68e76522011-10-05 13:22:16 -0700391} // namespace art
392
393#endif // ART_SRC_STACK_H_