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
21 #include "filter-ast.h"
22 #include "filter-parser.h"
23 #include "filter-bytecode.h"
24 #include "memstream.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 free_strings(&parser_ctx->allocated_strings);
300 filter_ast_free(parser_ctx->ast);
301 ret = yylex_destroy(parser_ctx->scanner);
303 fprintf(stderr, "yylex_destroy error\n");
308 int filter_parser_ctx_create_from_filter_expression(
309 const char *filter_expression, struct filter_parser_ctx **ctxp)
312 struct filter_parser_ctx *ctx = NULL;
315 assert(filter_expression);
319 * Casting const to non-const, as the underlying function will use it in
322 fmem = lttng_fmemopen((void *) filter_expression,
323 strlen(filter_expression), "r");
325 fprintf(stderr, "Error opening memory as stream\n");
326 ret = -LTTNG_ERR_FILTER_NOMEM;
329 ctx = filter_parser_ctx_alloc(fmem);
331 fprintf(stderr, "Error allocating parser\n");
332 ret = -LTTNG_ERR_FILTER_NOMEM;
333 goto filter_alloc_error;
335 ret = filter_parser_ctx_append_ast(ctx);
337 fprintf(stderr, "Parse error\n");
338 ret = -LTTNG_ERR_FILTER_INVAL;
342 ret = filter_visitor_print_xml(ctx, stdout, 0);
345 fprintf(stderr, "XML print error\n");
346 ret = -LTTNG_ERR_FILTER_INVAL;
351 dbg_printf("Generating IR... ");
353 ret = filter_visitor_ir_generate(ctx);
355 fprintf(stderr, "Generate IR error\n");
356 ret = -LTTNG_ERR_FILTER_INVAL;
359 dbg_printf("done\n");
361 dbg_printf("Validating IR... ");
363 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
365 ret = -LTTNG_ERR_FILTER_INVAL;
369 /* Normalize globbing patterns in the expression. */
370 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
372 ret = -LTTNG_ERR_FILTER_INVAL;
376 /* Validate strings used as literals in the expression. */
377 ret = filter_visitor_ir_validate_string(ctx);
379 ret = -LTTNG_ERR_FILTER_INVAL;
383 /* Validate globbing patterns in the expression. */
384 ret = filter_visitor_ir_validate_globbing(ctx);
386 ret = -LTTNG_ERR_FILTER_INVAL;
390 dbg_printf("done\n");
392 dbg_printf("Generating bytecode... ");
394 ret = filter_visitor_bytecode_generate(ctx);
396 fprintf(stderr, "Generate bytecode error\n");
397 ret = -LTTNG_ERR_FILTER_INVAL;
400 dbg_printf("done\n");
401 dbg_printf("Size of bytecode generated: %u bytes.\n",
402 bytecode_get_len(&ctx->bytecode->b));
404 /* No need to keep the memory stream. */
405 if (fclose(fmem) != 0) {
406 fprintf(stderr, "fclose (%d) \n", errno);
407 ret = -LTTNG_ERR_FILTER_INVAL;
415 filter_parser_ctx_free(ctx);
417 if (fclose(fmem) != 0) {
418 fprintf(stderr, "fclose (%d) \n", errno);
428 #include "common/macros.h"
431 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src);
436 %parse-param {struct filter_parser_ctx *parser_ctx}
437 %parse-param {yyscan_t scanner}
438 %lex-param {yyscan_t scanner}
439 %start translation_unit
440 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
441 %token ESCSEQ CHAR_STRING_TOKEN
442 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
443 %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
444 %token STAR PLUS MINUS
445 %token MOD_OP DIV_OP RIGHT_OP LEFT_OP
446 %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
447 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
448 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
450 %token <gs> IDENTIFIER GLOBAL_IDENTIFIER
456 struct gc_string *gs;
457 struct filter_node *n;
460 %type <gs> s_char s_char_sequence c_char c_char_sequence
462 %type <n> primary_expression
463 %type <n> prefix_expression
464 %type <n> prefix_expression_rec
465 %type <n> postfix_expression
466 %type <n> unary_expression
467 %type <n> unary_operator
468 %type <n> multiplicative_expression
469 %type <n> additive_expression
470 %type <n> shift_expression
471 %type <n> relational_expression
472 %type <n> equality_expression
473 %type <n> and_expression
474 %type <n> exclusive_or_expression
475 %type <n> inclusive_or_expression
476 %type <n> logical_and_expression
477 %type <n> logical_or_expression
479 %type <n> identifiers
489 | c_char_sequence c_char
490 { $$ = gc_string_append(parser_ctx, $1, $2); }
498 parse_error(parser_ctx, "escape sequences not supported yet");
502 /* 1.6 String literals */
507 | s_char_sequence s_char
508 { $$ = gc_string_append(parser_ctx, $1, $2); }
516 parse_error(parser_ctx, "escape sequences not supported yet");
523 $$ = make_node(parser_ctx, NODE_EXPRESSION);
524 $$->u.expression.type = AST_EXP_CONSTANT;
525 if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
526 &$$->u.expression.u.constant) != 1) {
527 parse_error(parser_ctx, "cannot scanf decimal constant");
532 $$ = make_node(parser_ctx, NODE_EXPRESSION);
533 $$->u.expression.type = AST_EXP_CONSTANT;
534 if (!strcmp(yylval.gs->s, "0")) {
535 $$->u.expression.u.constant = 0;
536 } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
537 &$$->u.expression.u.constant) != 1) {
538 parse_error(parser_ctx, "cannot scanf octal constant");
541 | HEXADECIMAL_CONSTANT
543 $$ = make_node(parser_ctx, NODE_EXPRESSION);
544 $$->u.expression.type = AST_EXP_CONSTANT;
545 if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
546 &$$->u.expression.u.constant) != 1) {
547 parse_error(parser_ctx, "cannot scanf hexadecimal constant");
552 $$ = make_node(parser_ctx, NODE_EXPRESSION);
553 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
554 if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
555 &$$->u.expression.u.float_constant) != 1) {
556 parse_error(parser_ctx, "cannot scanf float constant");
559 | STRING_LITERAL_START DQUOTE
561 $$ = make_node(parser_ctx, NODE_EXPRESSION);
562 $$->u.expression.type = AST_EXP_STRING;
563 $$->u.expression.u.string = "";
565 | STRING_LITERAL_START s_char_sequence DQUOTE
567 $$ = make_node(parser_ctx, NODE_EXPRESSION);
568 $$->u.expression.type = AST_EXP_STRING;
569 $$->u.expression.u.string = $2->s;
571 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
573 $$ = make_node(parser_ctx, NODE_EXPRESSION);
574 $$->u.expression.type = AST_EXP_STRING;
575 $$->u.expression.u.string = $2->s;
577 | LPAREN expression RPAREN
579 $$ = make_node(parser_ctx, NODE_EXPRESSION);
580 $$->u.expression.type = AST_EXP_NESTED;
581 $$->u.expression.u.child = $2;
588 $$ = make_node(parser_ctx, NODE_EXPRESSION);
589 $$->u.expression.type = AST_EXP_IDENTIFIER;
590 $$->u.expression.u.identifier = yylval.gs->s;
594 $$ = make_node(parser_ctx, NODE_EXPRESSION);
595 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
596 $$->u.expression.u.identifier = yylval.gs->s;
600 prefix_expression_rec
601 : LSBRAC unary_expression RSBRAC
605 | LSBRAC unary_expression RSBRAC prefix_expression_rec
608 $$->u.expression.pre_op = AST_LINK_BRACKET;
609 $$->u.expression.prev = $4;
618 | identifiers prefix_expression_rec
621 $$->u.expression.pre_op = AST_LINK_BRACKET;
622 $$->u.expression.next_bracket = $2;
631 | postfix_expression DOT prefix_expression
634 $$->u.expression.post_op = AST_LINK_DOT;
635 $$->u.expression.prev = $1;
637 | postfix_expression RARROW prefix_expression
640 $$->u.expression.post_op = AST_LINK_RARROW;
641 $$->u.expression.prev = $1;
650 | unary_operator unary_expression
653 $$->u.unary_op.child = $2;
660 $$ = make_node(parser_ctx, NODE_UNARY_OP);
661 $$->u.unary_op.type = AST_UNARY_PLUS;
665 $$ = make_node(parser_ctx, NODE_UNARY_OP);
666 $$->u.unary_op.type = AST_UNARY_MINUS;
670 $$ = make_node(parser_ctx, NODE_UNARY_OP);
671 $$->u.unary_op.type = AST_UNARY_NOT;
675 $$ = make_node(parser_ctx, NODE_UNARY_OP);
676 $$->u.unary_op.type = AST_UNARY_BIT_NOT;
680 multiplicative_expression
683 | multiplicative_expression STAR unary_expression
685 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
687 | multiplicative_expression DIV_OP unary_expression
689 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
691 | multiplicative_expression MOD_OP unary_expression
693 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
698 : multiplicative_expression
700 | additive_expression PLUS multiplicative_expression
702 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
704 | additive_expression MINUS multiplicative_expression
706 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
711 : additive_expression
713 | shift_expression LEFT_OP additive_expression
715 $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3);
717 | shift_expression RIGHT_OP additive_expression
719 $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3);
726 | and_expression AND_BIN shift_expression
728 $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3);
732 exclusive_or_expression
735 | exclusive_or_expression XOR_BIN and_expression
737 $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3);
741 inclusive_or_expression
742 : exclusive_or_expression
744 | inclusive_or_expression OR_BIN exclusive_or_expression
746 $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3);
750 relational_expression
751 : inclusive_or_expression
753 | relational_expression LT_OP inclusive_or_expression
755 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
757 | relational_expression GT_OP inclusive_or_expression
759 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
761 | relational_expression LE_OP inclusive_or_expression
763 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
765 | relational_expression GE_OP inclusive_or_expression
767 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
772 : relational_expression
774 | equality_expression EQ_OP relational_expression
776 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
778 | equality_expression NE_OP relational_expression
780 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
784 logical_and_expression
785 : equality_expression
787 | logical_and_expression AND_OP equality_expression
789 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
793 logical_or_expression
794 : logical_and_expression
796 | logical_or_expression OR_OP logical_and_expression
798 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
803 : logical_or_expression
810 parser_ctx->ast->root.u.root.child = $1;