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
19 #include "common/bytecode/bytecode.hpp"
20 #include "filter-ast.hpp"
21 #include "filter-parser.hpp"
22 #include "memstream.hpp"
24 #include <common/compat/errno.hpp>
25 #include <common/compiler.hpp>
26 #include <common/macros.hpp>
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 * bison 3.8.2 produces a parser with unused, but set, which results in warnings
38 #pragma clang diagnostic ignored "-Wunused-but-set-variable"
39 #endif /* __clang__ */
42 static const int print_xml = 1;
43 #define dbg_printf(fmt, args...) \
44 printf("[debug filter_parser] " fmt, ## args)
46 static const int print_xml = 0;
47 #define dbg_printf(fmt, args...) \
49 /* do nothing but check printf format */ \
51 printf("[debug filter_parser] " fmt, ## args); \
55 int filter_parser_debug = 0;
57 int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner);
58 int yylex(union YYSTYPE *yyval, yyscan_t scanner);
59 int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
60 int yylex_destroy(yyscan_t yyparser_ctx);
61 void yyrestart(FILE * in_str, yyscan_t parser_ctx);
64 struct cds_list_head gc;
66 char s[LTTNG_FLEXIBLE_ARRAY_MEMBER_LENGTH];
70 const char *node_type_str(enum node_type type)
74 return "NODE_UNKNOWN";
78 return "NODE_EXPRESSION";
82 return "NODE_UNARY_OP";
90 const char *node_type(struct filter_node *node)
92 if (node->type < NR_NODE_TYPES)
93 return node_type_str(node->type);
98 static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
101 struct gc_string *gstr;
104 /* TODO: could be faster with find first bit or glib Gstring */
105 /* sizeof long to account for malloc header (int or long ?) */
106 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
109 gstr = zmalloc<gc_string>(alloclen);
113 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
114 gstr->alloclen = alloclen;
120 * note: never use gc_string_append on a string that has external references.
121 * gsrc will be garbage collected immediately, and gstr might be.
122 * Should only be used to append characters to a string literal or constant.
125 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
126 struct gc_string *gstr,
127 struct gc_string *gsrc)
129 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
132 /* TODO: could be faster with find first bit or glib Gstring */
133 /* sizeof long to account for malloc header (int or long ?) */
134 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
137 if (alloclen > gstr->alloclen) {
138 struct gc_string *newgstr;
140 newgstr = gc_string_alloc(parser_ctx, newlen);
141 strcpy(newgstr->s, gstr->s);
142 strcat(newgstr->s, gsrc->s);
143 cds_list_del(&gstr->gc);
147 strcat(gstr->s, gsrc->s);
149 cds_list_del(&gsrc->gc);
154 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
156 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
157 strcpy(lvalp->gs->s, src);
160 static struct filter_node *make_node(struct filter_parser_ctx *scanner,
163 struct filter_ast *ast = filter_parser_get_ast(scanner);
164 struct filter_node *node;
166 node = zmalloc<filter_node>();
169 memset(node, 0, sizeof(*node));
171 cds_list_add(&node->gc, &ast->allocated_nodes);
175 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
178 case NODE_EXPRESSION:
187 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
195 static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
197 struct filter_node *lchild,
198 struct filter_node *rchild)
200 struct filter_ast *ast = filter_parser_get_ast(scanner);
201 struct filter_node *node;
203 node = zmalloc<filter_node>();
206 memset(node, 0, sizeof(*node));
207 node->type = NODE_OP;
208 cds_list_add(&node->gc, &ast->allocated_nodes);
209 node->u.op.type = type;
210 node->u.op.lchild = lchild;
211 node->u.op.rchild = rchild;
216 void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
218 fprintf(stderr, "error %s\n", str);
221 #define parse_error(parser_ctx, str) \
223 yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \
227 static void free_strings(struct cds_list_head *list)
229 struct gc_string *gstr, *tmp;
231 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
235 static struct filter_ast *filter_ast_alloc(void)
237 struct filter_ast *ast;
239 ast = zmalloc<filter_ast>();
242 memset(ast, 0, sizeof(*ast));
243 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
244 ast->root.type = NODE_ROOT;
248 static void filter_ast_free(struct filter_ast *ast)
250 struct filter_node *node, *tmp;
252 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
257 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
259 return yyparse(parser_ctx, parser_ctx->scanner);
262 struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input)
264 struct filter_parser_ctx *parser_ctx;
267 yydebug = filter_parser_debug;
269 parser_ctx = zmalloc<filter_parser_ctx>();
272 memset(parser_ctx, 0, sizeof(*parser_ctx));
274 ret = yylex_init_extra(parser_ctx, &parser_ctx->scanner);
276 fprintf(stderr, "yylex_init error\n");
277 goto cleanup_parser_ctx;
279 /* Start processing new stream */
280 yyrestart(input, parser_ctx->scanner);
282 parser_ctx->ast = filter_ast_alloc();
283 if (!parser_ctx->ast)
285 CDS_INIT_LIST_HEAD(&parser_ctx->allocated_strings);
288 fprintf(stdout, "parser_ctx input is a%s.\n",
289 isatty(fileno(input)) ? "n interactive tty" :
290 " noninteractive file");
295 ret = yylex_destroy(parser_ctx->scanner);
297 fprintf(stderr, "yylex_destroy error\n");
303 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
307 ret = yylex_destroy(parser_ctx->scanner);
309 fprintf(stderr, "yylex_destroy error\n");
311 filter_ast_free(parser_ctx->ast);
312 free_strings(&parser_ctx->allocated_strings);
313 filter_ir_free(parser_ctx);
314 free(parser_ctx->bytecode);
315 free(parser_ctx->bytecode_reloc);
320 int filter_parser_ctx_create_from_filter_expression(
321 const char *filter_expression, struct filter_parser_ctx **ctxp)
324 struct filter_parser_ctx *ctx = NULL;
327 LTTNG_ASSERT(filter_expression);
331 * Casting const to non-const, as the underlying function will use it in
334 fmem = lttng_fmemopen((void *) filter_expression,
335 strlen(filter_expression), "r");
337 fprintf(stderr, "Error opening memory as stream\n");
338 ret = -LTTNG_ERR_FILTER_NOMEM;
341 ctx = filter_parser_ctx_alloc(fmem);
343 fprintf(stderr, "Error allocating parser\n");
344 ret = -LTTNG_ERR_FILTER_NOMEM;
345 goto filter_alloc_error;
347 ret = filter_parser_ctx_append_ast(ctx);
349 fprintf(stderr, "Parse error\n");
350 ret = -LTTNG_ERR_FILTER_INVAL;
354 ret = filter_visitor_print_xml(ctx, stdout, 0);
357 fprintf(stderr, "XML print error\n");
358 ret = -LTTNG_ERR_FILTER_INVAL;
363 dbg_printf("Generating IR... ");
365 ret = filter_visitor_ir_generate(ctx);
367 fprintf(stderr, "Generate IR error\n");
368 ret = -LTTNG_ERR_FILTER_INVAL;
371 dbg_printf("done\n");
373 dbg_printf("Validating IR... ");
375 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
377 ret = -LTTNG_ERR_FILTER_INVAL;
381 /* Normalize globbing patterns in the expression. */
382 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
384 ret = -LTTNG_ERR_FILTER_INVAL;
388 /* Validate strings used as literals in the expression. */
389 ret = filter_visitor_ir_validate_string(ctx);
391 ret = -LTTNG_ERR_FILTER_INVAL;
395 /* Validate globbing patterns in the expression. */
396 ret = filter_visitor_ir_validate_globbing(ctx);
398 ret = -LTTNG_ERR_FILTER_INVAL;
402 dbg_printf("done\n");
404 dbg_printf("Generating bytecode... ");
406 ret = filter_visitor_bytecode_generate(ctx);
408 fprintf(stderr, "Generate bytecode error\n");
409 ret = -LTTNG_ERR_FILTER_INVAL;
412 dbg_printf("done\n");
413 dbg_printf("Size of bytecode generated: %u bytes.\n",
414 bytecode_get_len(&ctx->bytecode->b));
416 /* No need to keep the memory stream. */
417 if (fclose(fmem) != 0) {
418 fprintf(stderr, "fclose (%d) \n", errno);
419 ret = -LTTNG_ERR_FILTER_INVAL;
427 filter_parser_ctx_free(ctx);
429 if (fclose(fmem) != 0) {
430 fprintf(stderr, "fclose (%d) \n", errno);
440 #include "common/macros.hpp"
442 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src);
447 %parse-param {struct filter_parser_ctx *parser_ctx}
448 %parse-param {yyscan_t scanner}
449 %lex-param {yyscan_t scanner}
450 %start translation_unit
451 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
452 %token ESCSEQ CHAR_STRING_TOKEN
453 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
454 %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
455 %token STAR PLUS MINUS
456 %token MOD_OP DIV_OP RIGHT_OP LEFT_OP
457 %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
458 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
459 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
461 %token <gs> IDENTIFIER GLOBAL_IDENTIFIER
467 struct gc_string *gs;
468 struct filter_node *n;
471 %type <gs> s_char s_char_sequence c_char c_char_sequence
473 %type <n> primary_expression
474 %type <n> prefix_expression
475 %type <n> prefix_expression_rec
476 %type <n> postfix_expression
477 %type <n> unary_expression
478 %type <n> unary_operator
479 %type <n> multiplicative_expression
480 %type <n> additive_expression
481 %type <n> shift_expression
482 %type <n> relational_expression
483 %type <n> equality_expression
484 %type <n> and_expression
485 %type <n> exclusive_or_expression
486 %type <n> inclusive_or_expression
487 %type <n> logical_and_expression
488 %type <n> logical_or_expression
490 %type <n> identifiers
500 | c_char_sequence c_char
501 { $$ = gc_string_append(parser_ctx, $1, $2); }
509 parse_error(parser_ctx, "escape sequences not supported yet");
513 /* 1.6 String literals */
518 | s_char_sequence s_char
519 { $$ = gc_string_append(parser_ctx, $1, $2); }
527 parse_error(parser_ctx, "escape sequences not supported yet");
534 $$ = make_node(parser_ctx, NODE_EXPRESSION);
535 $$->u.expression.type = AST_EXP_CONSTANT;
536 if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
537 &$$->u.expression.u.constant) != 1) {
538 parse_error(parser_ctx, "cannot scanf decimal constant");
543 $$ = make_node(parser_ctx, NODE_EXPRESSION);
544 $$->u.expression.type = AST_EXP_CONSTANT;
545 if (!strcmp(yylval.gs->s, "0")) {
546 $$->u.expression.u.constant = 0;
547 } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
548 &$$->u.expression.u.constant) != 1) {
549 parse_error(parser_ctx, "cannot scanf octal constant");
552 | HEXADECIMAL_CONSTANT
554 $$ = make_node(parser_ctx, NODE_EXPRESSION);
555 $$->u.expression.type = AST_EXP_CONSTANT;
556 if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
557 &$$->u.expression.u.constant) != 1) {
558 parse_error(parser_ctx, "cannot scanf hexadecimal constant");
563 $$ = make_node(parser_ctx, NODE_EXPRESSION);
564 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
565 if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
566 &$$->u.expression.u.float_constant) != 1) {
567 parse_error(parser_ctx, "cannot scanf float constant");
570 | STRING_LITERAL_START DQUOTE
572 $$ = make_node(parser_ctx, NODE_EXPRESSION);
573 $$->u.expression.type = AST_EXP_STRING;
574 $$->u.expression.u.string = "";
576 | STRING_LITERAL_START s_char_sequence DQUOTE
578 $$ = make_node(parser_ctx, NODE_EXPRESSION);
579 $$->u.expression.type = AST_EXP_STRING;
580 $$->u.expression.u.string = $2->s;
582 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
584 $$ = make_node(parser_ctx, NODE_EXPRESSION);
585 $$->u.expression.type = AST_EXP_STRING;
586 $$->u.expression.u.string = $2->s;
588 | LPAREN expression RPAREN
590 $$ = make_node(parser_ctx, NODE_EXPRESSION);
591 $$->u.expression.type = AST_EXP_NESTED;
592 $$->u.expression.u.child = $2;
599 $$ = make_node(parser_ctx, NODE_EXPRESSION);
600 $$->u.expression.type = AST_EXP_IDENTIFIER;
601 $$->u.expression.u.identifier = yylval.gs->s;
605 $$ = make_node(parser_ctx, NODE_EXPRESSION);
606 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
607 $$->u.expression.u.identifier = yylval.gs->s;
611 prefix_expression_rec
612 : LSBRAC unary_expression RSBRAC
616 | LSBRAC unary_expression RSBRAC prefix_expression_rec
619 $$->u.expression.pre_op = AST_LINK_BRACKET;
620 $$->u.expression.prev = $4;
629 | identifiers prefix_expression_rec
632 $$->u.expression.pre_op = AST_LINK_BRACKET;
633 $$->u.expression.next_bracket = $2;
642 | postfix_expression DOT prefix_expression
645 $$->u.expression.post_op = AST_LINK_DOT;
646 $$->u.expression.prev = $1;
648 | postfix_expression RARROW prefix_expression
651 $$->u.expression.post_op = AST_LINK_RARROW;
652 $$->u.expression.prev = $1;
661 | unary_operator unary_expression
664 $$->u.unary_op.child = $2;
671 $$ = make_node(parser_ctx, NODE_UNARY_OP);
672 $$->u.unary_op.type = AST_UNARY_PLUS;
676 $$ = make_node(parser_ctx, NODE_UNARY_OP);
677 $$->u.unary_op.type = AST_UNARY_MINUS;
681 $$ = make_node(parser_ctx, NODE_UNARY_OP);
682 $$->u.unary_op.type = AST_UNARY_NOT;
686 $$ = make_node(parser_ctx, NODE_UNARY_OP);
687 $$->u.unary_op.type = AST_UNARY_BIT_NOT;
691 multiplicative_expression
694 | multiplicative_expression STAR unary_expression
696 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
698 | multiplicative_expression DIV_OP unary_expression
700 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
702 | multiplicative_expression MOD_OP unary_expression
704 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
709 : multiplicative_expression
711 | additive_expression PLUS multiplicative_expression
713 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
715 | additive_expression MINUS multiplicative_expression
717 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
722 : additive_expression
724 | shift_expression LEFT_OP additive_expression
726 $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3);
728 | shift_expression RIGHT_OP additive_expression
730 $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3);
737 | and_expression AND_BIN shift_expression
739 $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3);
743 exclusive_or_expression
746 | exclusive_or_expression XOR_BIN and_expression
748 $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3);
752 inclusive_or_expression
753 : exclusive_or_expression
755 | inclusive_or_expression OR_BIN exclusive_or_expression
757 $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3);
761 relational_expression
762 : inclusive_or_expression
764 | relational_expression LT_OP inclusive_or_expression
766 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
768 | relational_expression GT_OP inclusive_or_expression
770 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
772 | relational_expression LE_OP inclusive_or_expression
774 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
776 | relational_expression GE_OP inclusive_or_expression
778 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
783 : relational_expression
785 | equality_expression EQ_OP relational_expression
787 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
789 | equality_expression NE_OP relational_expression
791 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
795 logical_and_expression
796 : equality_expression
798 | logical_and_expression AND_OP equality_expression
800 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
804 logical_or_expression
805 : logical_and_expression
807 | logical_or_expression OR_OP logical_and_expression
809 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
814 : logical_or_expression
821 parser_ctx->ast->root.u.root.child = $1;