Debian Codegraph @

src/resolver/resolver.c

raw
1 #include "resolver/resolver.h"
2 #include "lexical/parser.h"
3 #include "lexical/token.h"
4 #include "utils/error.h"
5 #include "utils/hash.h"
6 #include "utils/logger.h"
7 #include "utils/vector.h"
8 #include <assert.h>
9 #include <seccomp.h>
10 #include <stdbool.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 static bool has_error;
16 static statement_t *local;
17 static uint16_t *masks, mem_valid = 0;
18 static uint16_t bpf_len = 0;
19
20 #define REPORT_ERROR(error_msg) \
21 do \
22 { \
23 report_error (error_msg); \
24 return; \
25 } \
26 while (0)
27
28 #define SPRINTF_CAT(...) print += sprintf (print, __VA_ARGS__)
29 static void
30 report_error (const char *error_msg)
31 {
32 has_error = true;
33 char buf[0x400];
34 char *print = buf;
35
36 SPRINTF_CAT ("%hu:-: %s\n", local->text_nr, error_msg);
37 // During the resolution phase, errors do not have a clear starting point
38 // errors in this stage indicate a semantic error in an entire line of code.
39 SPRINTF_CAT ("%.*s\n", local->line_len, local->line_start);
40 // line_len shouldn't include a '\n'
41 memset (print, '~', local->line_len);
42 print[local->line_len] = '\0';
43 // warn will print a '\n'
44
45 warn ("%s", buf);
46 }
47
48 static bool
49 match_from_to (token_type type, token_type from, token_type to)
50 {
51 return type >= from && type <= to;
52 }
53
54 static void
55 error_line (void)
56 {
57 has_error = true;
58 char buf[0x400];
59 char *print = buf;
60
61 error_line_t *error_line = &local->error_line;
62
63 uint16_t err_len = error_line->error_start - local->line_start;
64 SPRINTF_CAT ("%hu:%hu: %s\n", local->text_nr, err_len + 1,
65 error_line->error_msg);
66 SPRINTF_CAT ("%.*s\n", local->line_len, local->line_start);
67 // line_len shouldn't include a '\n'
68 memset (print, ' ', err_len);
69 print[err_len] = '^';
70 print[err_len + 1] = '\0';
71 // warn will print a '\n'
72
73 warn ("%s", buf);
74 }
75 #undef SPRINTF_CAT
76
77 #define IS_ARG_OUT_RANGE(obj) is_out_range (obj, 5)
78 #define IS_MEM_OUT_RANGE(obj) is_out_range (obj, 15)
79
80 static bool
81 is_out_range (obj_t *obj, uint32_t max_idx)
82 {
83 return obj->data > max_idx;
84 }
85
86 static void
87 assign_A (assign_line_t *assign_line)
88 {
89 token_type operator = assign_line->operator;
90 obj_t *right = &assign_line->right_var;
91
92 if (operator == NEGATIVE)
93 {
94 if (right->type != A)
95 REPORT_ERROR (M_RIGHT_SHOULD_BE_A);
96 return;
97 }
98 else if (match_from_to (operator, ADD_TO, XOR_TO))
99 {
100 if (right->type != X && right->type != NUMBER)
101 REPORT_ERROR (M_RIGHT_SHOULD_BE_X_OR_NUM);
102
103 if (operator == DIVIDE_TO && right->type == NUMBER && right->data == 0)
104 REPORT_ERROR (M_ALU_DIV_BY_ZERO);
105
106 // clang-format off
107 if (match_from_to (operator, LSH_TO, RSH_TO) &&
108 right->type == NUMBER && right->data >= 32)
109 // clang-format on
110 REPORT_ERROR (M_ALU_SH_OUT_OF_RANGE);
111 return;
112 }
113
114 assert (operator == EQUAL);
115
116 if (right->type == A)
117 REPORT_ERROR (M_RIGHT_CAN_NOT_BE_A);
118 else if ((right->type == ATTR_LOWARG || right->type == ATTR_HIGHARG)
119 && IS_ARG_OUT_RANGE (right))
120 REPORT_ERROR (M_ARGS_IDX_OUT_OF_RANGE);
121 else if (right->type == MEM)
122 {
123 if (IS_MEM_OUT_RANGE (right))
124 REPORT_ERROR (M_MEM_IDX_OUT_OF_RANGE);
125 if (!(mem_valid & (1 << right->data)))
126 REPORT_ERROR (M_UNINITIALIZED_MEM);
127 }
128 }
129
130 static void
131 assign_X (assign_line_t *assign_line)
132 {
133 token_type *operator = &assign_line->operator;
134 obj_t *right = &assign_line->right_var;
135
136 if (*operator != EQUAL)
137 REPORT_ERROR (M_OPERATOR_SHOULD_BE_EQUAL);
138
139 if (match_from_to (right->type, ATTR_SYSCALL, ATTR_HIGHARG))
140 REPORT_ERROR (M_LEFT_SHOULD_BE_A);
141
142 else if (right->type == X)
143 REPORT_ERROR (M_RIGHT_CAN_NOT_BE_X);
144 else if ((right->type == ATTR_LOWARG || right->type == ATTR_HIGHARG)
145 && IS_ARG_OUT_RANGE (right))
146 REPORT_ERROR (M_ARGS_IDX_OUT_OF_RANGE);
147 else if (right->type == MEM)
148 {
149 if (IS_MEM_OUT_RANGE (right))
150 REPORT_ERROR (M_MEM_IDX_OUT_OF_RANGE);
151 if (!(mem_valid & (1 << right->data)))
152 REPORT_ERROR (M_UNINITIALIZED_MEM);
153 }
154 }
155
156 static void
157 assign_MEM (assign_line_t *assign_line)
158 {
159 obj_t *left = &assign_line->left_var;
160 token_type operator = assign_line->operator;
161 obj_t *right = &assign_line->right_var;
162
163 if (operator != EQUAL)
164 REPORT_ERROR (M_OPERATOR_SHOULD_BE_EQUAL);
165
166 if (IS_MEM_OUT_RANGE (left))
167 REPORT_ERROR (M_MEM_IDX_OUT_OF_RANGE);
168
169 if (right->type != A && right->type != X)
170 REPORT_ERROR (M_RIGHT_SHOULD_BE_A_OR_X);
171
172 mem_valid |= (1 << left->data);
173 }
174
175 static void
176 assign_line (void)
177 {
178 assign_line_t *assign_line = &local->assign_line;
179 if (assign_line->left_var.type == A)
180 assign_A (assign_line);
181 else if (assign_line->left_var.type == X)
182 assign_X (assign_line);
183 else if (assign_line->left_var.type == MEM)
184 assign_MEM (assign_line);
185 }
186
187 static void
188 set_jt_jf (label_t *label, uint32_t code_nr)
189 {
190 label->code_nr = code_nr - local->code_nr - 1;
191 }
192
193 static void
194 jump_line (void)
195 {
196 jump_line_t *jump_line = &local->jump_line;
197
198 if (!jump_line->if_condition)
199 {
200 uint32_t jt = find_key (&jump_line->jt.key);
201 if (jt == (uint16_t)-1)
202 REPORT_ERROR (M_CANNOT_FIND_LABEL);
203 else if (jt > bpf_len)
204 REPORT_ERROR (M_JA_OUT_OF_FILTERS);
205 set_jt_jf (&jump_line->jt, jt);
206
207 if ((int16_t)jump_line->jt.code_nr < 0)
208 REPORT_ERROR (M_JA_MUST_BE_POSITIVE);
209
210 masks[jt] &= mem_valid;
211 mem_valid = ~0;
212 return;
213 }
214
215 uint32_t jt = find_key (&jump_line->jt.key);
216 if (jt == (uint16_t)-1)
217 REPORT_ERROR (M_CANNOT_FIND_LABEL);
218 set_jt_jf (&jump_line->jt, jt);
219
220 if ((int16_t)jump_line->jt.code_nr < 0)
221 REPORT_ERROR (M_JT_MUST_BE_POSITIVE);
222 if (jump_line->jt.code_nr > UINT8_MAX)
223 REPORT_ERROR (M_JT_TOO_FAR);
224 if (jt > bpf_len)
225 REPORT_ERROR (M_JT_INVALID_TAG);
226
227 uint32_t jf;
228 if (jump_line->jf.key.start == NULL)
229 jf = local->code_nr + 1;
230 else
231 {
232 jf = find_key (&jump_line->jf.key);
233 if (jf == (uint16_t)-1)
234 REPORT_ERROR (M_CANNOT_FIND_LABEL);
235 }
236
237 set_jt_jf (&jump_line->jf, jf);
238
239 jf = local->code_nr + jump_line->jf.code_nr + 1;
240 if ((int16_t)jump_line->jf.code_nr < 0)
241 REPORT_ERROR (M_JF_MUST_BE_POSITIVE);
242 if (jump_line->jf.code_nr > UINT8_MAX)
243 REPORT_ERROR (M_JF_TOO_FAR);
244 if (jf > bpf_len)
245 REPORT_ERROR (M_JF_INVALID_TAG);
246
247 masks[jt] &= mem_valid;
248 masks[jf] &= mem_valid;
249 mem_valid = ~0;
250 }
251
252 static void
253 return_line (void)
254 {
255 return_line_t *return_line = &local->return_line;
256 token_type ret_type = return_line->ret_obj.type;
257 if (ret_type == A || ret_type == NUMBER)
258 return;
259
260 if ((ret_type == TRACE || ret_type == TRAP || ret_type == ERRNO)
261 && return_line->ret_obj.data > 0xffff)
262 REPORT_ERROR (M_RET_DATA_OVERFLOW);
263 // Don't assume the ret_obj.data == zero when not used
264 }
265
266 static void
267 resolve_statement (statement_t *statement)
268 {
269 local = statement;
270
271 switch (local->type)
272 {
273 case ERROR_LINE:
274 error_line ();
275 return;
276 case ASSIGN_LINE:
277 assign_line ();
278 return;
279 case JUMP_LINE:
280 jump_line ();
281 return;
282 case RETURN_LINE:
283 return_line ();
284 return;
285 // nothing need to be done for these line
286 case EMPTY_LINE:
287 case EOF_LINE:
288 assert (!"Resolving EMPTY_LINE/EOF_LINE??");
289 }
290 }
291
292 bool
293 resolver (vector_t *code_ptr_v)
294 {
295 has_error = false;
296
297 masks = reallocate (NULL, sizeof (*masks) * (code_ptr_v->count));
298 memset (masks, 0xff, sizeof (*masks) * (code_ptr_v->count));
299 mem_valid = 0;
300
301 bpf_len = code_ptr_v->count - 1;
302 if (bpf_len == 0)
303 error ("%s", M_NO_VALID_CODE);
304
305 for (uint32_t i = 1; i < code_ptr_v->count; i++)
306 {
307 mem_valid &= masks[i];
308 statement_t **ptr = get_vector (code_ptr_v, i);
309 resolve_statement (*ptr);
310 }
311
312 statement_t **last = get_vector (code_ptr_v, code_ptr_v->count - 1);
313 if ((*last)->type != RETURN_LINE)
314 report_error (M_MUST_END_WITH_RET);
315
316 reallocate (masks, 0x0);
317
318 return has_error;
319 }