Debian Codegraph @

src/lexical/parser.c

raw
1 #include "lexical/parser.h"
2 #include "lexical/scanner.h"
3 #include "lexical/token.h"
4 #include "main.h"
5 #include "utils/arch_trans.h"
6 #include "utils/error.h"
7 #include "utils/hash.h"
8 #include "utils/logger.h"
9 #include "utils/vector.h"
10 #include <seccomp.h>
11 #include <setjmp.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <string.h>
15
16 typedef struct
17 {
18 uint16_t text_nr;
19 uint16_t code_nr;
20
21 token_t previous;
22 token_t current;
23 token_t next;
24 } parse_t;
25
26 static parse_t parse = { .text_nr = 0, .code_nr = 0 };
27 static statement_t *local;
28 static uint32_t local_arch;
29 static jmp_buf g_env;
30
31 static void error_at (token_t token, const char *err_msg);
32
33 static void
34 advance (void)
35 {
36 parse.previous = parse.current;
37 parse.current = parse.next;
38 scan_token (&parse.next);
39 }
40
41 static bool
42 peek (token_type expected)
43 {
44 return expected == parse.next.type;
45 }
46
47 static bool
48 peek_from_to (token_type from, token_type to)
49 {
50 return parse.next.type >= from && parse.next.type <= to;
51 }
52
53 static bool
54 match (token_type expected)
55 {
56 if (!peek (expected))
57 return false;
58
59 advance ();
60 return true;
61 }
62
63 static bool
64 match_number (void)
65 {
66 if (LIKELY (peek (NUMBER)))
67 return advance (), true;
68 if (peek (OVERFLOW_NUMBER))
69 error_at (parse.next, M_NUMBER_OVERFLOW);
70 return false;
71 }
72
73 static bool
74 match_from_to (token_type expected_start, token_type expected_end)
75 {
76 if (!peek_from_to (expected_start, expected_end))
77 return false;
78
79 advance ();
80 return true;
81 }
82
83 static void
84 error_at (token_t token, const char *err_msg)
85 {
86 // sync to the nextline
87 while (!(match (EOL) || peek (TOKEN_EOF)))
88 advance ();
89
90 local->type = ERROR_LINE;
91 local->line_len = parse.current.token_start - local->line_start;
92 local->error_line.error_start = token.token_start;
93 local->error_line.error_msg = err_msg;
94 longjmp (g_env, 1);
95 }
96
97 // paren_num can be ignored, default as 0
98 static uint32_t
99 paren_num (void)
100 {
101 if (!match (LEFT_PAREN))
102 return 0;
103 if (!match_number ())
104 error_at (parse.next, M_EXPECT_NUMBER);
105 if (!match (RIGHT_PAREN))
106 error_at (parse.next, M_EXPECT_PAREN);
107 return parse.previous.data;
108 }
109
110 // but bracket_num can not be ignored
111 static uint32_t
112 bracket_num (void)
113 {
114 if (!match (LEFT_BRACKET))
115 error_at (parse.next, M_EXPECT_BRACKET);
116 if (!match_number ())
117 error_at (parse.next, M_EXPECT_NUMBER);
118 if (!match (RIGHT_BRACKET))
119 error_at (parse.next, M_EXPECT_BRACKET);
120 return parse.previous.data;
121 }
122
123 static void
124 empty_line (void)
125 {
126 local->type = EMPTY_LINE;
127 parse.code_nr--;
128 // empty_line doesn't count
129 return;
130 }
131
132 static void
133 eof_line (void)
134 {
135 local->type = EOF_LINE;
136 return;
137 }
138
139 static void
140 return_line (void)
141 {
142 local->type = RETURN_LINE;
143
144 obj_t *obj = &local->return_line.ret_obj;
145 obj->literal.start = parse.next.token_start;
146
147 if (match_from_to (TRACE, ERRNO))
148 {
149 obj->type = parse.current.type;
150 obj->data = paren_num ();
151 }
152 else if (match_number ())
153 {
154 obj->type = NUMBER;
155 obj->data = parse.current.data;
156 }
157 else if (match_from_to (KILL_PROC, LOG) || match (A))
158 obj->type = parse.current.type;
159 else
160 error_at (parse.next, M_EXPECT_RETURN_VAL);
161
162 obj->literal.len = parse.current.token_start + parse.current.token_len
163 - obj->literal.start;
164 // some obj consume more than one token like TRACE(0xf)
165 }
166
167 static void
168 label (label_t *label)
169 {
170 if (!match (IDENTIFIER))
171 error_at (parse.next, M_EXPECT_LABEL);
172
173 label->key.start = parse.current.token_start;
174 label->key.len = parse.current.token_len;
175 }
176
177 static uint32_t
178 resolve_name_arch (uint32_t arch_token, token_t *sys_token)
179 {
180 char *sysname = sys_token->token_start;
181 char *strend = sys_token->token_start + sys_token->token_len;
182 char save = *strend;
183 *strend = '\0';
184 uint32_t sys_nr = seccomp_syscall_resolve_name_arch (arch_token, sysname);
185 *strend = save;
186 return sys_nr;
187 }
188
189 static void
190 compare_obj (obj_t *obj)
191 {
192 obj->literal.start = parse.next.token_start;
193 obj->literal.len = parse.next.token_len;
194
195 if (match (X))
196 {
197 obj->type = parse.current.type;
198 return;
199 }
200
201 obj->type = NUMBER;
202
203 if (match_number ())
204 {
205 obj->data = parse.current.data;
206 return;
207 }
208
209 if (match (IDENTIFIER))
210 {
211 obj->data = resolve_name_arch (local_arch, &parse.current);
212 if (obj->data == (uint32_t)-1)
213 error_at (parse.current, M_EXPECT_SYSCALL);
214 return;
215 }
216 // read
217
218 if (!match_from_to (ARCH_X86, ARCH_RISCV64))
219 error_at (parse.next, M_UNEXPECT_TOKEN);
220
221 if (!match (DOT))
222 {
223 obj->data = internal_arch_to_scmp_arch (parse.current.type);
224 if (UNLIKELY (obj->data == (uint32_t)-1))
225 // libseccomp does not support input arch??
226 error_at (parse.current, M_EXPECT_ARCH);
227 return;
228 }
229 // i386
230
231 if (!peek (IDENTIFIER))
232 error_at (parse.next, M_EXPECT_SYSCALL);
233
234 uint32_t scmp_arch = internal_arch_to_scmp_arch (parse.previous.type);
235 if (UNLIKELY (scmp_arch == (uint32_t)-1))
236 // libseccomp does not support input arch??
237 error_at (parse.previous, M_EXPECT_ARCH);
238 obj->data = resolve_name_arch (scmp_arch, &parse.next);
239 if (obj->data == (uint32_t)-1)
240 error_at (parse.next, M_EXPECT_SYSCALL);
241
242 obj->literal.len += parse.next.token_len + 1;
243 // +1 is for dot
244 advance ();
245 // i386.read
246 }
247
248 static void
249 condition (jump_line_t *jump_line)
250 {
251 jump_line->if_condition = true;
252
253 if (match (BANG))
254 jump_line->if_bang = true;
255
256 if (!match (LEFT_PAREN))
257 error_at (parse.next, M_EXPECT_PAREN);
258 if (!match (A))
259 error_at (parse.next, M_EXPECT_A);
260 if (!match_from_to (EQUAL_EQUAL, AND))
261 error_at (parse.next, M_EXPECT_COMPARTOR);
262 jump_line->comparator = parse.current.type;
263
264 compare_obj (&jump_line->cmpobj);
265
266 if (!match (RIGHT_PAREN))
267 error_at (parse.next, M_EXPECT_PAREN);
268 }
269
270 static void
271 ja_line (void)
272 {
273 local->type = JUMP_LINE;
274 jump_line_t *jump_line = &local->jump_line;
275 jump_line->if_condition = false;
276
277 label (&jump_line->jt);
278 }
279
280 static void
281 jump_line (void)
282 {
283 local->type = JUMP_LINE;
284 jump_line_t *jump_line = &local->jump_line;
285
286 condition (jump_line);
287
288 if (!match (GOTO))
289 error_at (parse.next, M_EXPECT_GOTO);
290
291 label (&jump_line->jt);
292 jump_line->jf.key.start = NULL;
293 // jf default as zero
294
295 if (!match (COMMA))
296 return;
297 // probably this jump_line has no jf
298
299 if (!match (ELSE))
300 error_at (parse.next, M_EXPECT_ELSE);
301 if (!match (GOTO))
302 error_at (parse.next, M_EXPECT_GOTO);
303
304 label (&jump_line->jf);
305 }
306
307 static void
308 left (obj_t *obj)
309 {
310 obj->type = parse.current.type;
311 // expression checked for us, it must be A X MEM here
312 obj->literal.start = parse.current.token_start;
313
314 if (parse.current.type == MEM)
315 obj->data = bracket_num ();
316
317 obj->literal.len = parse.current.token_start + parse.current.token_len
318 - obj->literal.start;
319 // some obj consume more than one token like mem[0xf]
320 }
321
322 static void
323 right (obj_t *obj)
324 {
325 obj->literal.start = parse.next.token_start;
326
327 if (match (A) || match (X) || match_from_to (ATTR_LEN, ATTR_HIGHPC))
328 obj->type = parse.current.type;
329 else if (match (MEM) || match (ATTR_LOWARG) || match (ATTR_HIGHARG))
330 {
331 obj->type = parse.current.type;
332 obj->data = bracket_num ();
333 }
334 else if (match_number ())
335 {
336 obj->type = parse.current.type;
337 obj->data = parse.current.data;
338 }
339 else
340 error_at (parse.next, M_EXPECT_RIGHT_VAR);
341
342 obj->literal.len = parse.current.token_start + parse.current.token_len
343 - obj->literal.start;
344 // some obj consume more than one token like mem[0xf]
345 }
346
347 static void
348 assign_line (void)
349 {
350 local->type = ASSIGN_LINE;
351 assign_line_t *assign_line = &local->assign_line;
352
353 left (&assign_line->left_var);
354
355 if (match_from_to (ADD_TO, XOR_TO))
356 assign_line->operator = parse.current.type;
357 else if (match (EQUAL))
358 {
359 // if we match NEGATIVE, use NEGATIVE to overwrite operator
360 // else use EQUAL as operator
361 match (NEGATIVE);
362 assign_line->operator = parse.current.type;
363 }
364 else
365 error_at (parse.next, M_EXPECT_OPERATOR);
366
367 right (&assign_line->right_var);
368 }
369
370 static void
371 expression (void)
372 {
373 if (peek (EOL) || peek (COMMENT))
374 empty_line ();
375 else if (peek (TOKEN_EOF))
376 eof_line ();
377 else if (UNLIKELY (parse.code_nr > BPF_MAXINSNS))
378 error ("%s", M_STATEMENT_OVERFLOW);
379 else if (match (RETURN))
380 return_line ();
381 else if (match (IF))
382 jump_line ();
383 else if (match (GOTO))
384 ja_line ();
385 else if (match_from_to (A, MEM))
386 assign_line ();
387 else
388 error_at (parse.next, M_UNEXPECT_TOKEN);
389
390 if (match (COMMENT))
391 local->comment = parse.current.token_start - local->line_start;
392 else
393 local->comment = -1;
394
395 if (match (EOL))
396 local->line_len = parse.current.token_start - local->line_start;
397 else if (peek (TOKEN_EOF))
398 local->line_len = parse.next.token_start - local->line_start;
399 else
400 error_at (parse.next, M_UNEXPECT_TOKEN);
401 }
402
403 static void
404 label_decl (string_t *label_decl)
405 {
406 if (!match (LABEL_DECL))
407 return;
408
409 label_decl->start = parse.current.token_start;
410 label_decl->len = parse.current.token_len - 1;
411 // ignore the ':' character
412 if (insert_key (label_decl, parse.code_nr) == 1)
413 error_at (parse.current, M_DUPLICATED_LABEL);
414
415 // ignore our disasm useless output
416 uint32_t count = 0;
417 while (match_number () && count < 4)
418 count++;
419 }
420
421 static void
422 parse_line (statement_t *statement)
423 {
424 local = statement;
425 memset (local, 0, sizeof (statement_t));
426
427 parse.text_nr++;
428 parse.code_nr++;
429 // if statement turns out to be empty_line, code_line--;
430 local->code_nr = parse.code_nr;
431 local->text_nr = parse.text_nr;
432 local->label_decl.start = NULL;
433 // label_decl.string default as NULL
434
435 local->line_start = parse.next.token_start;
436
437 if (setjmp (g_env) == 1)
438 return;
439
440 label_decl (&statement->label_decl);
441 expression ();
442
443 return;
444 }
445
446 void
447 init_parser (uint32_t scmp_arch)
448 {
449 advance ();
450 local_arch = scmp_arch;
451 }
452
453 void
454 parser (vector_t *text_v, vector_t *code_ptr_v)
455 {
456 statement_t statement = { 0 };
457 statement_t *ptr = &statement;
458 push_vector (text_v, &statement);
459 push_vector (code_ptr_v, &ptr);
460 while (true)
461 {
462 parse_line (&statement);
463
464 if (statement.type == EOF_LINE)
465 break;
466 push_vector (text_v, &statement);
467 }
468
469 // we have to do it after text vector finish
470 // because text vector might reallocate
471 for (uint32_t i = 1; i < text_v->count; i++)
472 {
473 ptr = get_vector (text_v, i);
474 if (ptr->type == EMPTY_LINE)
475 continue;
476 push_vector (code_ptr_v, &ptr);
477 }
478 }