-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.c
More file actions
251 lines (201 loc) · 6.84 KB
/
Copy pathhook.c
File metadata and controls
251 lines (201 loc) · 6.84 KB
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
#include "hook.h"
#include "sandbox.h"
#include "common.h"
#include "assert.h"
#include "timer.h"
#include "chan.h"
#include "include/cart.h"
#include <stdint.h>
#include <inttypes.h>
enum {
ID_TIMER_NEW,
ID_TIMER_ADD,
ID_TIMER_SET_CB,
ID_TIMER_SET_REPEAT,
ID_TIMER_SET_INTERVAL,
ID_TIMER_PAUSE,
ID_TIMER_RESUME,
ID_TIMER_DEL,
ID_CHAN_NEW,
ID_CHAN_DEL,
ID_COUNT
};
/*****************************************************************************/
cart_timer_t cart_timer_new(void)
{
return *(const cart_timer_t *) sandbox_call_supervisor(sizeof(cart_timer_t), ID_TIMER_NEW, NULL, 0);
}
static void hook_timer_new(void *const data)
{
*((cart_timer_t *) data) = timer_new();
}
/*****************************************************************************/
struct cart_timer_add_args {
const cart_cb_t cb;
const cart_timer_repeat_t repeat;
const bool run;
const cart_timer_interval_t interval;
const cart_timer_unit_t unit;
const bool autodel;
};
cart_timer_t cart_timer_add(const cart_cb_t cb, const cart_timer_repeat_t repeat, const bool run,
const cart_timer_interval_t interval, const cart_timer_unit_t unit, const bool autodel)
{
return *(const cart_timer_t *) sandbox_call_supervisor(sizeof(cart_timer_t), ID_TIMER_ADD,
&(const struct cart_timer_add_args) { cb, repeat, run, interval, unit, autodel },
sizeof(struct cart_timer_add_args));
}
static void hook_timer_add(void *const data)
{
const struct cart_timer_add_args *args = data;
const cart_timer_t handle = timer_add(args->cb, args->repeat, args->run, args->interval, args->unit, args->autodel);
*((cart_timer_t *) data) = handle;
}
/*****************************************************************************/
struct cart_timer_set_cb_args {
const cart_timer_t tm;
const cart_cb_t cb;
};
bool cart_timer_set_cb(const cart_timer_t tm, const cart_cb_t cb)
{
return *(const bool *) sandbox_call_supervisor(sizeof(bool), ID_TIMER_SET_CB,
&(const struct cart_timer_set_cb_args) { tm, cb },
sizeof(struct cart_timer_set_cb_args));
}
static void hook_timer_set_cb(void *const data)
{
const struct cart_timer_set_cb_args *args = data;
const bool done = timer_set_cb(args->tm, args->cb);
*((bool *) data) = done;
}
/*****************************************************************************/
struct cart_timer_set_repeat_args {
const cart_timer_t tm;
const cart_timer_repeat_t repeat;
};
bool cart_timer_set_repeat(const cart_timer_t tm, const cart_timer_repeat_t repeat)
{
return *(const bool *) sandbox_call_supervisor(sizeof(bool), ID_TIMER_SET_REPEAT,
&(const struct cart_timer_set_repeat_args) { tm, repeat },
sizeof(struct cart_timer_set_repeat_args));
}
static void hook_timer_set_repeat(void *const data)
{
const struct cart_timer_set_repeat_args *args = data;
const bool done = timer_set_repeat(args->tm, args->repeat);
*((bool *) data) = done;
}
/*****************************************************************************/
struct cart_timer_set_interval_args {
const cart_timer_t tm;
const cart_timer_interval_t interval;
const cart_timer_unit_t unit;
};
bool cart_timer_set_interval(const cart_timer_t tm, const cart_timer_interval_t interval, const cart_timer_unit_t unit)
{
return *(const bool *) sandbox_call_supervisor(sizeof(bool), ID_TIMER_SET_INTERVAL,
&(const struct cart_timer_set_interval_args) { tm, interval, unit },
sizeof(struct cart_timer_set_interval_args));
}
static void hook_timer_set_interval(void *const data)
{
const struct cart_timer_set_interval_args *args = data;
const bool done = timer_set_interval(args->tm, args->interval, args->unit);
*((bool *) data) = done;
}
/*****************************************************************************/
struct cart_timer_pause_args {
const cart_timer_t tm;
};
bool cart_timer_pause(const cart_timer_t tm)
{
return *(const bool *) sandbox_call_supervisor(sizeof(bool), ID_TIMER_PAUSE,
&(const struct cart_timer_pause_args) { tm },
sizeof(struct cart_timer_pause_args));
}
static void hook_timer_pause(void *const data)
{
const struct cart_timer_pause_args *args = data;
const bool done = timer_pause(args->tm);
*((bool *) data) = done;
}
/*****************************************************************************/
struct cart_timer_resume_args {
const cart_timer_t tm;
};
bool cart_timer_resume(const cart_timer_t tm)
{
return *(const bool *) sandbox_call_supervisor(sizeof(bool), ID_TIMER_RESUME,
&(const struct cart_timer_resume_args) { tm },
sizeof(struct cart_timer_resume_args));
}
static void hook_timer_resume(void *const data)
{
const struct cart_timer_resume_args *args = data;
const bool done = timer_resume(args->tm);
*((bool *) data) = done;
}
/*****************************************************************************/
struct cart_timer_del_args {
const cart_timer_t tm;
};
bool cart_timer_del(const cart_timer_t tm)
{
return *(const bool *) sandbox_call_supervisor(sizeof(bool), ID_TIMER_DEL,
&(const struct cart_timer_del_args) { tm },
sizeof(struct cart_timer_del_args));
}
static void hook_timer_del(void *const data)
{
const struct cart_timer_del_args *args = data;
const bool done = timer_del(args->tm);
*((bool *) data) = done;
}
/*****************************************************************************/
cart_chan_t cart_chan_new(void)
{
return *(const cart_chan_t *) sandbox_call_supervisor(sizeof(cart_chan_t), ID_CHAN_NEW, NULL, 0);
}
static void hook_chan_new(void *const data)
{
*((cart_chan_t *) data) = chan_new();
}
/*****************************************************************************/
struct cart_chan_del_args {
const cart_chan_t ch;
};
bool cart_chan_del(const cart_chan_t ch)
{
return *(const bool *) sandbox_call_supervisor(sizeof(bool), ID_CHAN_DEL,
&(const struct cart_chan_del_args) { ch },
sizeof(struct cart_chan_del_args));
}
static void hook_chan_del(void *const data)
{
const struct cart_chan_del_args *args = data;
const bool done = chan_del(args->ch);
*((bool *) data) = done;
}
/*****************************************************************************/
static void (*const hooks[])(void *const) = {
hook_timer_new,
hook_timer_add,
hook_timer_set_cb,
hook_timer_set_repeat,
hook_timer_set_interval,
hook_timer_pause,
hook_timer_resume,
hook_timer_del,
hook_chan_new,
hook_chan_del,
};
static_assert(countof(hooks) == ID_COUNT, "Hooks are mismatched");
void hook_execute(void *const data)
{
const uint64_t call_id = *(const uint64_t *) data;
if (likely(call_id < ID_COUNT)) {
hooks[call_id]((uint8_t *) data + 16);
} else {
log_error("Cannot execute hook number %" PRIu64, call_id);
}
}