-
Notifications
You must be signed in to change notification settings - Fork 392
/
aco.h
227 lines (182 loc) Β· 5.55 KB
/
aco.h
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
// Copyright 2018 Sen Han <00hnes@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ACO_H
#define ACO_H
#include <string.h>
#include <stdint.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/mman.h>
#ifdef ACO_USE_VALGRIND
#include <valgrind/valgrind.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
#define ACO_VERSION_MAJOR 1
#define ACO_VERSION_MINOR 2
#define ACO_VERSION_PATCH 4
#ifdef __i386__
#define ACO_REG_IDX_RETADDR 0
#define ACO_REG_IDX_SP 1
#define ACO_REG_IDX_BP 2
#define ACO_REG_IDX_FPU 6
#elif __x86_64__
#define ACO_REG_IDX_RETADDR 4
#define ACO_REG_IDX_SP 5
#define ACO_REG_IDX_BP 7
#define ACO_REG_IDX_FPU 8
#else
#error "platform no support yet"
#endif
typedef struct {
void* ptr;
size_t sz;
size_t valid_sz;
// max copy size in bytes
size_t max_cpsz;
// copy from share stack to this save stack
size_t ct_save;
// copy from this save stack to share stack
size_t ct_restore;
} aco_save_stack_t;
struct aco_s;
typedef struct aco_s aco_t;
typedef struct {
void* ptr;
size_t sz;
void* align_highptr;
void* align_retptr;
size_t align_validsz;
size_t align_limit;
aco_t* owner;
char guard_page_enabled;
void* real_ptr;
size_t real_sz;
#ifdef ACO_USE_VALGRIND
unsigned long valgrind_stk_id;
#endif
} aco_share_stack_t;
typedef void (*aco_cofuncp_t)(void);
struct aco_s{
// cpu registers' state
#ifdef __i386__
#ifdef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
void* reg[6];
#else
void* reg[8];
#endif
#elif __x86_64__
#ifdef ACO_CONFIG_SHARE_FPU_MXCSR_ENV
void* reg[8];
#else
void* reg[9];
#endif
#else
#error "platform no support yet"
#endif
aco_t* main_co;
void* arg;
char is_end;
aco_cofuncp_t fp;
aco_save_stack_t save_stack;
aco_share_stack_t* share_stack;
};
#define aco_likely(x) (__builtin_expect(!!(x), 1))
#define aco_unlikely(x) (__builtin_expect(!!(x), 0))
#define aco_assert(EX) ((aco_likely(EX))?((void)0):(abort()))
#define aco_assertptr(ptr) ((aco_likely((ptr) != NULL))?((void)0):(abort()))
#define aco_assertalloc_bool(b) do { \
if(aco_unlikely(!(b))){ \
fprintf(stderr, "Aborting: failed to allocate memory: %s:%d:%s\n", \
__FILE__, __LINE__, __PRETTY_FUNCTION__); \
abort(); \
} \
} while(0)
#define aco_assertalloc_ptr(ptr) do { \
if(aco_unlikely((ptr) == NULL)){ \
fprintf(stderr, "Aborting: failed to allocate memory: %s:%d:%s\n", \
__FILE__, __LINE__, __PRETTY_FUNCTION__); \
abort(); \
} \
} while(0)
#if defined(aco_attr_no_asan)
#error "aco_attr_no_asan already defined"
#endif
#if defined(ACO_USE_ASAN)
#if defined(__has_feature)
#if __has_feature(__address_sanitizer__)
#define aco_attr_no_asan \
__attribute__((__no_sanitize_address__))
#endif
#endif
#if defined(__SANITIZE_ADDRESS__) && !defined(aco_attr_no_asan)
#define aco_attr_no_asan \
__attribute__((__no_sanitize_address__))
#endif
#endif
#ifndef aco_attr_no_asan
#define aco_attr_no_asan
#endif
extern void aco_runtime_test(void);
extern void aco_thread_init(aco_cofuncp_t last_word_co_fp);
extern void* acosw(aco_t* from_co, aco_t* to_co) __asm__("acosw"); // asm
extern void aco_save_fpucw_mxcsr(void* p) __asm__("aco_save_fpucw_mxcsr"); // asm
extern void aco_funcp_protector_asm(void) __asm__("aco_funcp_protector_asm"); // asm
extern void aco_funcp_protector(void);
extern aco_share_stack_t* aco_share_stack_new(size_t sz);
aco_share_stack_t* aco_share_stack_new2(size_t sz, char guard_page_enabled);
extern void aco_share_stack_destroy(aco_share_stack_t* sstk);
extern aco_t* aco_create(
aco_t* main_co,
aco_share_stack_t* share_stack,
size_t save_stack_sz,
aco_cofuncp_t fp, void* arg
);
// aco's Global Thread Local Storage variable `co`
extern __thread aco_t* aco_gtls_co;
aco_attr_no_asan
extern void aco_resume(aco_t* resume_co);
//extern void aco_yield1(aco_t* yield_co);
#define aco_yield1(yield_co) do { \
aco_assertptr((yield_co)); \
aco_assertptr((yield_co)->main_co); \
acosw((yield_co), (yield_co)->main_co); \
} while(0)
#define aco_yield() do { \
aco_yield1(aco_gtls_co); \
} while(0)
#define aco_get_arg() (aco_gtls_co->arg)
#define aco_get_co() ({(void)0; aco_gtls_co;})
#define aco_co() ({(void)0; aco_gtls_co;})
extern void aco_destroy(aco_t* co);
#define aco_is_main_co(co) ({((co)->main_co) == NULL;})
#define aco_exit1(co) do { \
(co)->is_end = 1; \
aco_assert((co)->share_stack->owner == (co)); \
(co)->share_stack->owner = NULL; \
(co)->share_stack->align_validsz = 0; \
aco_yield1((co)); \
aco_assert(0); \
} while(0)
#define aco_exit() do { \
aco_exit1(aco_gtls_co); \
} while(0)
#ifdef __cplusplus
}
#endif
#endif