5 * LTTng filter expression parser
7 * Copyright 2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * SPDX-License-Identifier: LGPL-2.1-only
11 * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html
20 #include "common/bytecode/bytecode.h"
21 #include "filter-ast.h"
22 #include "filter-parser.h"
23 #include "memstream.h"
25 #include <common/compat/errno.h>
26 #include <common/macros.h>
28 #define WIDTH_u64_SCANF_IS_A_BROKEN_API "20"
29 #define WIDTH_o64_SCANF_IS_A_BROKEN_API "22"
30 #define WIDTH_x64_SCANF_IS_A_BROKEN_API "17"
31 #define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */
34 static const int print_xml = 1;
35 #define dbg_printf(fmt, args...) \
36 printf("[debug filter_parser] " fmt, ## args)
38 static const int print_xml = 0;
39 #define dbg_printf(fmt, args...) \
41 /* do nothing but check printf format */ \
43 printf("[debug filter_parser] " fmt, ## args); \
50 int filter_parser_debug = 0;
53 int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner);
55 int yylex(union YYSTYPE *yyval, yyscan_t scanner);
57 int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
59 int yylex_destroy(yyscan_t yyparser_ctx);
61 void yyrestart(FILE * in_str, yyscan_t parser_ctx);
64 struct cds_list_head gc;
69 static const char *node_type_to_str[] = {
70 [ NODE_UNKNOWN ] = "NODE_UNKNOWN",
71 [ NODE_ROOT ] = "NODE_ROOT",
72 [ NODE_EXPRESSION ] = "NODE_EXPRESSION",
73 [ NODE_OP ] = "NODE_OP",
74 [ NODE_UNARY_OP ] = "NODE_UNARY_OP",
78 const char *node_type(struct filter_node *node)
80 if (node->type < NR_NODE_TYPES)
81 return node_type_to_str[node->type];
86 static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
89 struct gc_string *gstr;
92 /* TODO: could be faster with find first bit or glib Gstring */
93 /* sizeof long to account for malloc header (int or long ?) */
94 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
97 gstr = zmalloc(alloclen);
101 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
102 gstr->alloclen = alloclen;
108 * note: never use gc_string_append on a string that has external references.
109 * gsrc will be garbage collected immediately, and gstr might be.
110 * Should only be used to append characters to a string literal or constant.
113 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
114 struct gc_string *gstr,
115 struct gc_string *gsrc)
117 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
120 /* TODO: could be faster with find first bit or glib Gstring */
121 /* sizeof long to account for malloc header (int or long ?) */
122 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
125 if (alloclen > gstr->alloclen) {
126 struct gc_string *newgstr;
128 newgstr = gc_string_alloc(parser_ctx, newlen);
129 strcpy(newgstr->s, gstr->s);
130 strcat(newgstr->s, gsrc->s);
131 cds_list_del(&gstr->gc);
135 strcat(gstr->s, gsrc->s);
137 cds_list_del(&gsrc->gc);
143 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
145 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
146 strcpy(lvalp->gs->s, src);
149 static struct filter_node *make_node(struct filter_parser_ctx *scanner,
152 struct filter_ast *ast = filter_parser_get_ast(scanner);
153 struct filter_node *node;
155 node = zmalloc(sizeof(*node));
158 memset(node, 0, sizeof(*node));
160 cds_list_add(&node->gc, &ast->allocated_nodes);
164 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
167 case NODE_EXPRESSION:
176 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
184 static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
186 struct filter_node *lchild,
187 struct filter_node *rchild)
189 struct filter_ast *ast = filter_parser_get_ast(scanner);
190 struct filter_node *node;
192 node = zmalloc(sizeof(*node));
195 memset(node, 0, sizeof(*node));
196 node->type = NODE_OP;
197 cds_list_add(&node->gc, &ast->allocated_nodes);
198 node->u.op.type = type;
199 node->u.op.lchild = lchild;
200 node->u.op.rchild = rchild;
205 void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
207 fprintf(stderr, "error %s\n", str);
210 #define parse_error(parser_ctx, str) \
212 yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \
216 static void free_strings(struct cds_list_head *list)
218 struct gc_string *gstr, *tmp;
220 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
224 static struct filter_ast *filter_ast_alloc(void)
226 struct filter_ast *ast;
228 ast = zmalloc(sizeof(*ast));
231 memset(ast, 0, sizeof(*ast));
232 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
233 ast->root.type = NODE_ROOT;
237 static void filter_ast_free(struct filter_ast *ast)
239 struct filter_node *node, *tmp;
241 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
247 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
249 return yyparse(parser_ctx, parser_ctx->scanner);
253 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
255 struct filter_parser_ctx *parser_ctx;
258 yydebug = filter_parser_debug;
260 parser_ctx = zmalloc(sizeof(*parser_ctx));
263 memset(parser_ctx, 0, sizeof(*parser_ctx));
265 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
267 fprintf(stderr, "yylex_init error\n");
268 goto cleanup_parser_ctx;
270 /* Start processing new stream */
271 yyrestart(input, parser_ctx->scanner);
273 parser_ctx->ast = filter_ast_alloc();
274 if (!parser_ctx->ast)
276 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
279 fprintf(stdout, "parser_ctx input is a%s.\n",
280 isatty(fileno(input)) ? "n interactive tty" :
281 " noninteractive file");
286 ret = yylex_destroy(parser_ctx->scanner);
288 fprintf(stderr, "yylex_destroy error\n");
295 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
299 ret = yylex_destroy(parser_ctx->scanner);
301 fprintf(stderr, "yylex_destroy error\n");
303 filter_ast_free(parser_ctx->ast);
304 free_strings(&parser_ctx->allocated_strings);
305 filter_ir_free(parser_ctx);
306 free(parser_ctx->bytecode);
307 free(parser_ctx->bytecode_reloc);
313 int filter_parser_ctx_create_from_filter_expression(
314 const char *filter_expression, struct filter_parser_ctx **ctxp)
317 struct filter_parser_ctx *ctx = NULL;
320 assert(filter_expression);
324 * Casting const to non-const, as the underlying function will use it in
327 fmem = lttng_fmemopen((void *) filter_expression,
328 strlen(filter_expression), "r");
330 fprintf(stderr, "Error opening memory as stream\n");
331 ret = -LTTNG_ERR_FILTER_NOMEM;
334 ctx = filter_parser_ctx_alloc(fmem);
336 fprintf(stderr, "Error allocating parser\n");
337 ret = -LTTNG_ERR_FILTER_NOMEM;
338 goto filter_alloc_error;
340 ret = filter_parser_ctx_append_ast(ctx);
342 fprintf(stderr, "Parse error\n");
343 ret = -LTTNG_ERR_FILTER_INVAL;
347 ret = filter_visitor_print_xml(ctx, stdout, 0);
350 fprintf(stderr, "XML print error\n");
351 ret = -LTTNG_ERR_FILTER_INVAL;
356 dbg_printf("Generating IR... ");
358 ret = filter_visitor_ir_generate(ctx);
360 fprintf(stderr, "Generate IR error\n");
361 ret = -LTTNG_ERR_FILTER_INVAL;
364 dbg_printf("done\n");
366 dbg_printf("Validating IR... ");
368 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
370 ret = -LTTNG_ERR_FILTER_INVAL;
374 /* Normalize globbing patterns in the expression. */
375 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
377 ret = -LTTNG_ERR_FILTER_INVAL;
381 /* Validate strings used as literals in the expression. */
382 ret = filter_visitor_ir_validate_string(ctx);
384 ret = -LTTNG_ERR_FILTER_INVAL;
388 /* Validate globbing patterns in the expression. */
389 ret = filter_visitor_ir_validate_globbing(ctx);
391 ret = -LTTNG_ERR_FILTER_INVAL;
395 dbg_printf("done\n");
397 dbg_printf("Generating bytecode... ");
399 ret = filter_visitor_bytecode_generate(ctx);
401 fprintf(stderr, "Generate bytecode error\n");
402 ret = -LTTNG_ERR_FILTER_INVAL;
405 dbg_printf("done\n");
406 dbg_printf("Size of bytecode generated: %u bytes.\n",
407 bytecode_get_len(&ctx->bytecode->b));
409 /* No need to keep the memory stream. */
410 if (fclose(fmem) != 0) {
411 fprintf(stderr, "fclose (%d) \n", errno);
412 ret = -LTTNG_ERR_FILTER_INVAL;
420 filter_parser_ctx_free(ctx);
422 if (fclose(fmem) != 0) {
423 fprintf(stderr, "fclose (%d) \n", errno);
433 #include "common/macros.h"
436 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src);
441 %parse-param {struct filter_parser_ctx *parser_ctx}
442 %parse-param {yyscan_t scanner}
443 %lex-param {yyscan_t scanner}
444 %start translation_unit
445 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
446 %token ESCSEQ CHAR_STRING_TOKEN
447 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
448 %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
449 %token STAR PLUS MINUS
450 %token MOD_OP DIV_OP RIGHT_OP LEFT_OP
451 %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
452 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
453 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
455 %token <gs> IDENTIFIER GLOBAL_IDENTIFIER
461 struct gc_string *gs;
462 struct filter_node *n;
465 %type <gs> s_char s_char_sequence c_char c_char_sequence
467 %type <n> primary_expression
468 %type <n> prefix_expression
469 %type <n> prefix_expression_rec
470 %type <n> postfix_expression
471 %type <n> unary_expression
472 %type <n> unary_operator
473 %type <n> multiplicative_expression
474 %type <n> additive_expression
475 %type <n> shift_expression
476 %type <n> relational_expression
477 %type <n> equality_expression
478 %type <n> and_expression
479 %type <n> exclusive_or_expression
480 %type <n> inclusive_or_expression
481 %type <n> logical_and_expression
482 %type <n> logical_or_expression
484 %type <n> identifiers
494 | c_char_sequence c_char
495 { $$ = gc_string_append(parser_ctx, $1, $2); }
503 parse_error(parser_ctx, "escape sequences not supported yet");
507 /* 1.6 String literals */
512 | s_char_sequence s_char
513 { $$ = gc_string_append(parser_ctx, $1, $2); }
521 parse_error(parser_ctx, "escape sequences not supported yet");
528 $$ = make_node(parser_ctx, NODE_EXPRESSION);
529 $$->u.expression.type = AST_EXP_CONSTANT;
530 if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
531 &$$->u.expression.u.constant) != 1) {
532 parse_error(parser_ctx, "cannot scanf decimal constant");
537 $$ = make_node(parser_ctx, NODE_EXPRESSION);
538 $$->u.expression.type = AST_EXP_CONSTANT;
539 if (!strcmp(yylval.gs->s, "0")) {
540 $$->u.expression.u.constant = 0;
541 } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
542 &$$->u.expression.u.constant) != 1) {
543 parse_error(parser_ctx, "cannot scanf octal constant");
546 | HEXADECIMAL_CONSTANT
548 $$ = make_node(parser_ctx, NODE_EXPRESSION);
549 $$->u.expression.type = AST_EXP_CONSTANT;
550 if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
551 &$$->u.expression.u.constant) != 1) {
552 parse_error(parser_ctx, "cannot scanf hexadecimal constant");
557 $$ = make_node(parser_ctx, NODE_EXPRESSION);
558 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
559 if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
560 &$$->u.expression.u.float_constant) != 1) {
561 parse_error(parser_ctx, "cannot scanf float constant");
564 | STRING_LITERAL_START DQUOTE
566 $$ = make_node(parser_ctx, NODE_EXPRESSION);
567 $$->u.expression.type = AST_EXP_STRING;
568 $$->u.expression.u.string = "";
570 | STRING_LITERAL_START s_char_sequence DQUOTE
572 $$ = make_node(parser_ctx, NODE_EXPRESSION);
573 $$->u.expression.type = AST_EXP_STRING;
574 $$->u.expression.u.string = $2->s;
576 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
578 $$ = make_node(parser_ctx, NODE_EXPRESSION);
579 $$->u.expression.type = AST_EXP_STRING;
580 $$->u.expression.u.string = $2->s;
582 | LPAREN expression RPAREN
584 $$ = make_node(parser_ctx, NODE_EXPRESSION);
585 $$->u.expression.type = AST_EXP_NESTED;
586 $$->u.expression.u.child = $2;
593 $$ = make_node(parser_ctx, NODE_EXPRESSION);
594 $$->u.expression.type = AST_EXP_IDENTIFIER;
595 $$->u.expression.u.identifier = yylval.gs->s;
599 $$ = make_node(parser_ctx, NODE_EXPRESSION);
600 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
601 $$->u.expression.u.identifier = yylval.gs->s;
605 prefix_expression_rec
606 : LSBRAC unary_expression RSBRAC
610 | LSBRAC unary_expression RSBRAC prefix_expression_rec
613 $$->u.expression.pre_op = AST_LINK_BRACKET;
614 $$->u.expression.prev = $4;
623 | identifiers prefix_expression_rec
626 $$->u.expression.pre_op = AST_LINK_BRACKET;
627 $$->u.expression.next_bracket = $2;
636 | postfix_expression DOT prefix_expression
639 $$->u.expression.post_op = AST_LINK_DOT;
640 $$->u.expression.prev = $1;
642 | postfix_expression RARROW prefix_expression
645 $$->u.expression.post_op = AST_LINK_RARROW;
646 $$->u.expression.prev = $1;
655 | unary_operator unary_expression
658 $$->u.unary_op.child = $2;
665 $$ = make_node(parser_ctx, NODE_UNARY_OP);
666 $$->u.unary_op.type = AST_UNARY_PLUS;
670 $$ = make_node(parser_ctx, NODE_UNARY_OP);
671 $$->u.unary_op.type = AST_UNARY_MINUS;
675 $$ = make_node(parser_ctx, NODE_UNARY_OP);
676 $$->u.unary_op.type = AST_UNARY_NOT;
680 $$ = make_node(parser_ctx, NODE_UNARY_OP);
681 $$->u.unary_op.type = AST_UNARY_BIT_NOT;
685 multiplicative_expression
688 | multiplicative_expression STAR unary_expression
690 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
692 | multiplicative_expression DIV_OP unary_expression
694 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
696 | multiplicative_expression MOD_OP unary_expression
698 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
703 : multiplicative_expression
705 | additive_expression PLUS multiplicative_expression
707 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
709 | additive_expression MINUS multiplicative_expression
711 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
716 : additive_expression
718 | shift_expression LEFT_OP additive_expression
720 $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3);
722 | shift_expression RIGHT_OP additive_expression
724 $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3);
731 | and_expression AND_BIN shift_expression
733 $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3);
737 exclusive_or_expression
740 | exclusive_or_expression XOR_BIN and_expression
742 $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3);
746 inclusive_or_expression
747 : exclusive_or_expression
749 | inclusive_or_expression OR_BIN exclusive_or_expression
751 $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3);
755 relational_expression
756 : inclusive_or_expression
758 | relational_expression LT_OP inclusive_or_expression
760 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
762 | relational_expression GT_OP inclusive_or_expression
764 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
766 | relational_expression LE_OP inclusive_or_expression
768 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
770 | relational_expression GE_OP inclusive_or_expression
772 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
777 : relational_expression
779 | equality_expression EQ_OP relational_expression
781 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
783 | equality_expression NE_OP relational_expression
785 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
789 logical_and_expression
790 : equality_expression
792 | logical_and_expression AND_OP equality_expression
794 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
798 logical_or_expression
799 : logical_and_expression
801 | logical_or_expression OR_OP logical_and_expression
803 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
808 : logical_or_expression
815 parser_ctx->ast->root.u.root.child = $1;