-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.cpp
205 lines (162 loc) · 4.5 KB
/
frame.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "std.h"
#include "rope.h"
#include "thread.h"
#include "SpmtThread.h"
#include "Loggers.h"
#include "Helper.h"
using namespace std;
/* The function getCallerFrame() is used in the code that does
security related stack-walking. It guards against invocation
via reflection. These frames must be skipped, else it will
appear that the caller was loaded by the boot loader. */
Frame *getCallerFrame(Frame *last)
{
loop:
/* Skip the top frame, and check if the
previous is a dummy frame */
if((last = last->prev)->mb == NULL) {
/* Skip the dummy frame, and check if
* we're at the top of the stack */
if((last = last->prev)->prev == NULL)
return NULL;
/* Check if we were invoked via reflection */
if(last->mb->classobj == getReflectMethodClass()) {
/* There will be two frames for invoke. Skip
the first, and jump back. This also handles
recursive invocation via reflection. */
last = last->prev;
goto loop;
}
}
return last;
}
Class *getCallerCallerClass()
{
//Frame *last = getExecEnv()->last_frame->prev;
//assert(getExecEnv()->last_frame->prev == threadSelf()->current_core()->get_current_mode()->frame->prev);
Frame* last = threadSelf()->get_current_spmt_thread()->get_current_mode()->frame->prev;
if((last->mb == NULL && (last = last->prev)->prev == NULL) ||
(last = getCallerFrame(last)) == NULL)
return NULL;
return last->mb->classobj;
}
unsigned long long call_count = 0; // just for debug
Frame::Frame(int lvars_size, int ostack_size)
:
last_pc(nullptr),
mb(nullptr),
object(nullptr),
owner(nullptr),
caller(nullptr),
caller_pc(nullptr),
prev(nullptr),
caller_sp(nullptr),
pinned(false),
is_top(false),
is_iter_in_current_effect(false)
{
//assert(lvars_size);
//assert(ostack_size);
lvars = new uintptr_t[lvars_size];
ostack_base = new uintptr_t[ostack_size];
_name_ = 0;
lrefs = 0;
//{{{ just for debug
call_count++;
c = call_count;
magic = 1978;
if (call_count == 23808) {
int x = 0;
x++;
}
//}}} just for debug
xxx = 0;
frame_no = 0;
}
Frame::~Frame()
{
// if (xxx == 999) {
// cout << "#" << g_current_core()->id() << " free xxx999 " << *this << endl;
// }
delete[] lvars;
delete[] ostack_base;
delete lrefs;
//{{{ just for debug
lvars = 0;
ostack_base = 0;
mb = 0;
prev = 0;
caller_pc = 0;
caller_sp = 0;
caller = 0;
magic = 2009;
object = 0;
//}}} just for debug
xxx = 0;
}
Object*
Frame::get_object()
{
return object;
}
bool
Frame::is_top_frame()
{
return is_top;
}
void copy_args_to_params(uintptr_t* arg, uintptr_t* param, int count)
{
std::copy(arg, arg + count, param);
}
DummyFrame::DummyFrame()
:
Frame(5, 20)
{
_name_ = "dummy frame";
}
Frame*
create_dummy_frame(Frame* caller_frame)
{
Frame* dummy_frame = new Frame(5, 20);
dummy_frame->prev = caller_frame;
dummy_frame->_name_ = "dummy frame";
return dummy_frame;
}
Frame*
g_create_frame(SpmtThread* owner, Object* object, MethodBlock* new_mb, uintptr_t* args,
SpmtThread* caller, CodePntr caller_pc, Frame* caller_frame, uintptr_t* caller_sp,
bool is_top)
{
assert(new_mb);
u2 lvars_size = new_mb->max_locals;
u2 ostack_size = new_mb->max_stack;
if (new_mb->max_stack == 0 && new_mb->is_native()) {
ostack_size = std::max(new_mb->max_locals, (u2)4);
}
lvars_size = lvars_size != 0 ? lvars_size : 4;
ostack_size = ostack_size != 0 ? ostack_size : 4;
Frame* new_frame = new Frame(lvars_size, ostack_size);
frame_no++;
new_frame->frame_no = frame_no;
new_frame->is_top = is_top;
new_frame->owner = owner;
new_frame->object = object;
new_frame->mb = new_mb;
new_frame->prev = caller_frame;
new_frame->caller = caller;
new_frame->caller_sp = caller_sp;
new_frame->caller_pc = caller_pc;
if (args)
copy_args_to_params(args, new_frame->lvars, new_mb->args_count);
return new_frame;
}
void
g_destroy_frame(Frame* frame)
{
MINILOG_IF((is_client_code and is_app_obj(frame->mb->classobj)),
free_frames_logger,
"#" << g_get_current_spmt_thread()->id()
<< " delete frame=" << (void*)frame);
delete frame;
//frame->magic = 2009; // only mark dead, do not delete for debug purpose
}