forked from mitchan0321/toy
/
cstack.c
301 lines (255 loc) · 7.69 KB
/
cstack.c
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* $Id: cstack.c,v 1.7 2012/06/10 07:00:23 mit-sato Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
#include <string.h>
#include <sys/mman.h>
#include "toy.h"
#define __CSTACK_DEF__
#include "cstack.h"
#include "types.h"
static void sig_cstack(int flag, siginfo_t* siginfo, void* ptr);
static void sig_cstack_running_handler(int flag, siginfo_t* siginfo, void* ptr);
static int alloc_slot(int slot);
static jmp_buf jmp_env;
void
init_cstack() {
int i, fin;
stack_t ss;
struct sigaction newsig, oldsig;
CStack.slot_size = STACK_SLOT_SIZE * sizeof(__PTRDIFF_TYPE__);
for (i = 0 ; i < STACK_SLOT_MAX ; i++) {
CStack.stack_slot[i].state = SS_INVAL;
CStack.stack_slot[i].start_addr = 0;
CStack.stack_slot[i].barrier_addr = 0;
CStack.stack_slot[i].safe_addr = 0;
CStack.stack_slot[i].end_addr = 0;
CStack.stack_slot[i].jmp_buff_enable = 0;
memset(CStack.stack_slot[i].jmp_buff, 0, sizeof(jmp_buf));
}
ss.ss_sp = GC_MALLOC(SIGASTKSZ);
if (NULL == ss.ss_sp) {
fprintf(stderr, "Can't alloc altinative stack(1).\n");
exit(1);
}
ss.ss_size = SIGASTKSZ;
ss.ss_flags = 0;
if (-1 == sigaltstack(&ss, NULL)) {
fprintf(stderr, "Can't set altinative stack(1).\n");
exit(1);
}
if (-1 == sigaction(SIGSEGV, NULL, &oldsig)) {
fprintf(stderr, "Can't save sigaction(1).\n");
exit(1);
}
sigemptyset(&newsig.sa_mask);
sigaddset(&newsig.sa_mask, SIGSEGV);
newsig.sa_sigaction = sig_cstack;
newsig.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
if (-1 == sigaction(SIGSEGV, &newsig, NULL)) {
fprintf(stderr, "Can't set sigaction(1).\n");
exit(1);
}
fin = alloc_slot(0);
CStack.number_of_slot = fin;
if (-1 == sigaction(SIGSEGV, &oldsig, NULL)) {
fprintf(stderr, "Can't restore sigaction.\n");
exit(1);
}
/* for main interp stack to use */
CStack.stack_slot[0].state = SS_USE;
/* init current coroutine */
Current_coroutine = 0;
/* running signal install */
ss.ss_sp = GC_MALLOC(SIGASTKSZ);
if (NULL == ss.ss_sp) {
fprintf(stderr, "Can't alloc altinative stack(2).\n");
exit(1);
}
ss.ss_size = SIGASTKSZ;
ss.ss_flags = 0;
if (-1 == sigaltstack(&ss, NULL)) {
fprintf(stderr, "Can't set altinative stack(2).\n");
exit(1);
}
if (-1 == sigaction(SIGSEGV, NULL, &oldsig)) {
fprintf(stderr, "Can't save sigaction(2).\n");
exit(1);
}
sigemptyset(&newsig.sa_mask);
sigaddset(&newsig.sa_mask, SIGSEGV);
newsig.sa_sigaction = sig_cstack_running_handler;
newsig.sa_flags = SA_SIGINFO | SA_RESTART | SA_ONSTACK;
if (-1 == sigaction(SIGSEGV, &newsig, NULL)) {
fprintf(stderr, "Can't set sigaction(2).\n");
exit(1);
}
}
static int
alloc_slot(int slot) {
volatile int i;
volatile __PTRDIFF_TYPE__ *stack_frame;
stack_frame = alloca(STACK_SLOT_SIZE * sizeof(__PTRDIFF_TYPE__));
if (0 == setjmp(jmp_env)) {
for (i = (STACK_SLOT_SIZE - 1) ; i >= 0 ; i--) {
stack_frame[i] = 0;
}
} else {
return slot;
}
CStack.stack_slot[slot].state = SS_FREE;
CStack.stack_slot[slot].start_addr = (__PTRDIFF_TYPE__*)stack_frame;
CStack.stack_slot[slot].end_addr = (__PTRDIFF_TYPE__*)
((void*)stack_frame + (STACK_SLOT_SIZE * sizeof(__PTRDIFF_TYPE__)));
/* set memory protect barrier */
if ((__PTRDIFF_TYPE__)(((void*)stack_frame + MP_SPARE)) & MP_ALIGN) {
CStack.stack_slot[slot].barrier_addr = (__PTRDIFF_TYPE__*)
(((__PTRDIFF_TYPE__)((void*)stack_frame
+ MP_SPARE + MP_PAGESIZE)) & ~MP_ALIGN);
} else {
CStack.stack_slot[slot].barrier_addr = (__PTRDIFF_TYPE__*)
((void*)(stack_frame) + MP_SPARE);
}
CStack.stack_slot[slot].safe_addr = (__PTRDIFF_TYPE__*)
((void*)(CStack.stack_slot[slot].barrier_addr) + MP_PAGESIZE);
mprotect((void*)CStack.stack_slot[slot].barrier_addr, MP_PAGESIZE, PROT_READ);
/* indicate garbage collection address block to BoehmGC */
GC_add_roots((void*)CStack.stack_slot[slot].safe_addr,
(void*)CStack.stack_slot[slot].end_addr);
slot ++;
if (slot > STACK_SLOT_MAX) return (slot-1);
return alloc_slot(slot);
}
static void
sig_cstack(int flag, siginfo_t* siginfo, void* ptr) {
longjmp(jmp_env, 1);
}
static void
sig_cstack_running_handler(int flag, siginfo_t* siginfo, void* ptr) {
if (CStack.stack_slot[Current_coroutine].jmp_buff_enable) {
longjmp(CStack.stack_slot[Current_coroutine].jmp_buff, 1);
}
fprintf(stderr, "Overflow handler is not installed, at %d.\n", Current_coroutine);
exit(1);
}
int
cstack_get() {
int i;
for (i = 0 ; i < CStack.number_of_slot ; i++) {
if (SS_FREE == CStack.stack_slot[i].state) {
CStack.stack_slot[i].state = SS_USE;
memset((void*)CStack.stack_slot[i].safe_addr, 0,
CStack.stack_slot[i].end_addr - CStack.stack_slot[i].safe_addr);
return i;
}
}
return -1;
}
void
cstack_release(int slot_id) {
if ((slot_id >= CStack.number_of_slot) ||
(slot_id < 1)) {
return;
}
CStack.stack_slot[slot_id].state = SS_FREE;
CStack.stack_slot[slot_id].jmp_buff_enable = 0;
}
Toy_Type*
cstack_list() {
int i;
Toy_Type *result, *slist, *elist;
result = new_list(NULL);
list_append(result, new_integer_si(CStack.number_of_slot));
slist = new_list(NULL);
for (i = 0 ; i < CStack.number_of_slot ; i++) {
elist = new_list(NULL);
switch (CStack.stack_slot[i].state) {
case SS_FREE:
list_append(elist, new_symbol("FREE"));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].start_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].barrier_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].safe_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].end_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].jmp_buff_enable));
break;
case SS_USE:
list_append(elist, new_symbol("USE"));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].start_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].barrier_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].safe_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].end_addr));
list_append(elist,
new_integer_si((__PTRDIFF_TYPE__)
(__PTRDIFF_TYPE__)
CStack.stack_slot[i].jmp_buff_enable));
break;
}
list_append(slist, elist);
}
list_append(result, slist);
return result;
}
void*
cstack_get_start_addr(int slot_id) {
if ((slot_id >= CStack.number_of_slot) ||
(slot_id < 0)) {
return 0;
}
return (void*)CStack.stack_slot[slot_id].start_addr;
}
void*
cstack_get_end_addr(int slot_id) {
if ((slot_id >= CStack.number_of_slot) ||
(slot_id < 0)) {
return 0;
}
return (void*)CStack.stack_slot[slot_id].end_addr;
}
int
cstack_get_size() {
return CStack.slot_size;
}
void
cstack_set_jmpbuff(int slot, jmp_buf *buff) {
memcpy(&(CStack.stack_slot[slot].jmp_buff), buff, sizeof(jmp_buf));
CStack.stack_slot[slot].jmp_buff_enable = 1;
}
int
cstack_enter(int new_slot) {
int old_slot;
old_slot = Current_coroutine;
Current_coroutine = new_slot;
return old_slot;
}
void
cstack_leave(int old_slot) {
Current_coroutine = old_slot;
}