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/macros.hpp>
27 #define WIDTH_u64_SCANF_IS_A_BROKEN_API "20"
28 #define WIDTH_o64_SCANF_IS_A_BROKEN_API "22"
29 #define WIDTH_x64_SCANF_IS_A_BROKEN_API "17"
30 #define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */
33 static const int print_xml = 1;
34 #define dbg_printf(fmt, args...) \
35 printf("[debug filter_parser] " fmt, ## args)
37 static const int print_xml = 0;
38 #define dbg_printf(fmt, args...) \
40 /* do nothing but check printf format */ \
42 printf("[debug filter_parser] " fmt, ## args); \
46 int filter_parser_debug = 0;
48 int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner);
49 int yylex(union YYSTYPE *yyval, yyscan_t scanner);
50 int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals);
51 int yylex_destroy(yyscan_t yyparser_ctx);
52 void yyrestart(FILE * in_str, yyscan_t parser_ctx);
55 struct cds_list_head gc;
61 const char *node_type_str(enum node_type type)
65 return "NODE_UNKNOWN";
69 return "NODE_EXPRESSION";
73 return "NODE_UNARY_OP";
81 const char *node_type(struct filter_node *node)
83 if (node->type < NR_NODE_TYPES)
84 return node_type_str(node->type);
89 static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx,
92 struct gc_string *gstr;
95 /* TODO: could be faster with find first bit or glib Gstring */
96 /* sizeof long to account for malloc header (int or long ?) */
97 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len;
100 gstr = zmalloc<gc_string>(alloclen);
104 cds_list_add(&gstr->gc, &parser_ctx->allocated_strings);
105 gstr->alloclen = alloclen;
111 * note: never use gc_string_append on a string that has external references.
112 * gsrc will be garbage collected immediately, and gstr might be.
113 * Should only be used to append characters to a string literal or constant.
116 struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx,
117 struct gc_string *gstr,
118 struct gc_string *gsrc)
120 size_t newlen = strlen(gsrc->s) + strlen(gstr->s) + 1;
123 /* TODO: could be faster with find first bit or glib Gstring */
124 /* sizeof long to account for malloc header (int or long ?) */
125 for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + newlen;
128 if (alloclen > gstr->alloclen) {
129 struct gc_string *newgstr;
131 newgstr = gc_string_alloc(parser_ctx, newlen);
132 strcpy(newgstr->s, gstr->s);
133 strcat(newgstr->s, gsrc->s);
134 cds_list_del(&gstr->gc);
138 strcat(gstr->s, gsrc->s);
140 cds_list_del(&gsrc->gc);
145 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src)
147 lvalp->gs = gc_string_alloc(parser_ctx, strlen(src) + 1);
148 strcpy(lvalp->gs->s, src);
151 static struct filter_node *make_node(struct filter_parser_ctx *scanner,
154 struct filter_ast *ast = filter_parser_get_ast(scanner);
155 struct filter_node *node;
157 node = zmalloc<filter_node>();
160 memset(node, 0, sizeof(*node));
162 cds_list_add(&node->gc, &ast->allocated_nodes);
166 fprintf(stderr, "[error] %s: trying to create root node\n", __func__);
169 case NODE_EXPRESSION:
178 fprintf(stderr, "[error] %s: unknown node type %d\n", __func__,
186 static struct filter_node *make_op_node(struct filter_parser_ctx *scanner,
188 struct filter_node *lchild,
189 struct filter_node *rchild)
191 struct filter_ast *ast = filter_parser_get_ast(scanner);
192 struct filter_node *node;
194 node = zmalloc<filter_node>();
197 memset(node, 0, sizeof(*node));
198 node->type = NODE_OP;
199 cds_list_add(&node->gc, &ast->allocated_nodes);
200 node->u.op.type = type;
201 node->u.op.lchild = lchild;
202 node->u.op.rchild = rchild;
207 void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str)
209 fprintf(stderr, "error %s\n", str);
212 #define parse_error(parser_ctx, str) \
214 yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \
218 static void free_strings(struct cds_list_head *list)
220 struct gc_string *gstr, *tmp;
222 cds_list_for_each_entry_safe(gstr, tmp, list, gc)
226 static struct filter_ast *filter_ast_alloc(void)
228 struct filter_ast *ast;
230 ast = zmalloc<filter_ast>();
233 memset(ast, 0, sizeof(*ast));
234 CDS_INIT_LIST_HEAD(&ast->allocated_nodes);
235 ast->root.type = NODE_ROOT;
239 static void filter_ast_free(struct filter_ast *ast)
241 struct filter_node *node, *tmp;
243 cds_list_for_each_entry_safe(node, tmp, &ast->allocated_nodes, gc)
248 int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx)
250 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<filter_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");
294 void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx)
298 ret = yylex_destroy(parser_ctx->scanner);
300 fprintf(stderr, "yylex_destroy error\n");
302 filter_ast_free(parser_ctx->ast);
303 free_strings(&parser_ctx->allocated_strings);
304 filter_ir_free(parser_ctx);
305 free(parser_ctx->bytecode);
306 free(parser_ctx->bytecode_reloc);
311 int filter_parser_ctx_create_from_filter_expression(
312 const char *filter_expression, struct filter_parser_ctx **ctxp)
315 struct filter_parser_ctx *ctx = NULL;
318 LTTNG_ASSERT(filter_expression);
322 * Casting const to non-const, as the underlying function will use it in
325 fmem = lttng_fmemopen((void *) filter_expression,
326 strlen(filter_expression), "r");
328 fprintf(stderr, "Error opening memory as stream\n");
329 ret = -LTTNG_ERR_FILTER_NOMEM;
332 ctx = filter_parser_ctx_alloc(fmem);
334 fprintf(stderr, "Error allocating parser\n");
335 ret = -LTTNG_ERR_FILTER_NOMEM;
336 goto filter_alloc_error;
338 ret = filter_parser_ctx_append_ast(ctx);
340 fprintf(stderr, "Parse error\n");
341 ret = -LTTNG_ERR_FILTER_INVAL;
345 ret = filter_visitor_print_xml(ctx, stdout, 0);
348 fprintf(stderr, "XML print error\n");
349 ret = -LTTNG_ERR_FILTER_INVAL;
354 dbg_printf("Generating IR... ");
356 ret = filter_visitor_ir_generate(ctx);
358 fprintf(stderr, "Generate IR error\n");
359 ret = -LTTNG_ERR_FILTER_INVAL;
362 dbg_printf("done\n");
364 dbg_printf("Validating IR... ");
366 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
368 ret = -LTTNG_ERR_FILTER_INVAL;
372 /* Normalize globbing patterns in the expression. */
373 ret = filter_visitor_ir_normalize_glob_patterns(ctx);
375 ret = -LTTNG_ERR_FILTER_INVAL;
379 /* Validate strings used as literals in the expression. */
380 ret = filter_visitor_ir_validate_string(ctx);
382 ret = -LTTNG_ERR_FILTER_INVAL;
386 /* Validate globbing patterns in the expression. */
387 ret = filter_visitor_ir_validate_globbing(ctx);
389 ret = -LTTNG_ERR_FILTER_INVAL;
393 dbg_printf("done\n");
395 dbg_printf("Generating bytecode... ");
397 ret = filter_visitor_bytecode_generate(ctx);
399 fprintf(stderr, "Generate bytecode error\n");
400 ret = -LTTNG_ERR_FILTER_INVAL;
403 dbg_printf("done\n");
404 dbg_printf("Size of bytecode generated: %u bytes.\n",
405 bytecode_get_len(&ctx->bytecode->b));
407 /* No need to keep the memory stream. */
408 if (fclose(fmem) != 0) {
409 fprintf(stderr, "fclose (%d) \n", errno);
410 ret = -LTTNG_ERR_FILTER_INVAL;
418 filter_parser_ctx_free(ctx);
420 if (fclose(fmem) != 0) {
421 fprintf(stderr, "fclose (%d) \n", errno);
431 #include "common/macros.hpp"
433 void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src);
438 %parse-param {struct filter_parser_ctx *parser_ctx}
439 %parse-param {yyscan_t scanner}
440 %lex-param {yyscan_t scanner}
441 %start translation_unit
442 %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE
443 %token ESCSEQ CHAR_STRING_TOKEN
444 %token DECIMAL_CONSTANT OCTAL_CONSTANT HEXADECIMAL_CONSTANT FLOAT_CONSTANT
445 %token LSBRAC RSBRAC LPAREN RPAREN LBRAC RBRAC RARROW
446 %token STAR PLUS MINUS
447 %token MOD_OP DIV_OP RIGHT_OP LEFT_OP
448 %token EQ_OP NE_OP LE_OP GE_OP LT_OP GT_OP AND_OP OR_OP NOT_OP
449 %token ASSIGN COLON SEMICOLON DOTDOTDOT DOT EQUAL COMMA
450 %token XOR_BIN AND_BIN OR_BIN NOT_BIN
452 %token <gs> IDENTIFIER GLOBAL_IDENTIFIER
458 struct gc_string *gs;
459 struct filter_node *n;
462 %type <gs> s_char s_char_sequence c_char c_char_sequence
464 %type <n> primary_expression
465 %type <n> prefix_expression
466 %type <n> prefix_expression_rec
467 %type <n> postfix_expression
468 %type <n> unary_expression
469 %type <n> unary_operator
470 %type <n> multiplicative_expression
471 %type <n> additive_expression
472 %type <n> shift_expression
473 %type <n> relational_expression
474 %type <n> equality_expression
475 %type <n> and_expression
476 %type <n> exclusive_or_expression
477 %type <n> inclusive_or_expression
478 %type <n> logical_and_expression
479 %type <n> logical_or_expression
481 %type <n> identifiers
491 | c_char_sequence c_char
492 { $$ = gc_string_append(parser_ctx, $1, $2); }
500 parse_error(parser_ctx, "escape sequences not supported yet");
504 /* 1.6 String literals */
509 | s_char_sequence s_char
510 { $$ = gc_string_append(parser_ctx, $1, $2); }
518 parse_error(parser_ctx, "escape sequences not supported yet");
525 $$ = make_node(parser_ctx, NODE_EXPRESSION);
526 $$->u.expression.type = AST_EXP_CONSTANT;
527 if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64,
528 &$$->u.expression.u.constant) != 1) {
529 parse_error(parser_ctx, "cannot scanf decimal constant");
534 $$ = make_node(parser_ctx, NODE_EXPRESSION);
535 $$->u.expression.type = AST_EXP_CONSTANT;
536 if (!strcmp(yylval.gs->s, "0")) {
537 $$->u.expression.u.constant = 0;
538 } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64,
539 &$$->u.expression.u.constant) != 1) {
540 parse_error(parser_ctx, "cannot scanf octal constant");
543 | HEXADECIMAL_CONSTANT
545 $$ = make_node(parser_ctx, NODE_EXPRESSION);
546 $$->u.expression.type = AST_EXP_CONSTANT;
547 if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64,
548 &$$->u.expression.u.constant) != 1) {
549 parse_error(parser_ctx, "cannot scanf hexadecimal constant");
554 $$ = make_node(parser_ctx, NODE_EXPRESSION);
555 $$->u.expression.type = AST_EXP_FLOAT_CONSTANT;
556 if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg",
557 &$$->u.expression.u.float_constant) != 1) {
558 parse_error(parser_ctx, "cannot scanf float constant");
561 | STRING_LITERAL_START DQUOTE
563 $$ = make_node(parser_ctx, NODE_EXPRESSION);
564 $$->u.expression.type = AST_EXP_STRING;
565 $$->u.expression.u.string = "";
567 | STRING_LITERAL_START s_char_sequence DQUOTE
569 $$ = make_node(parser_ctx, NODE_EXPRESSION);
570 $$->u.expression.type = AST_EXP_STRING;
571 $$->u.expression.u.string = $2->s;
573 | CHARACTER_CONSTANT_START c_char_sequence SQUOTE
575 $$ = make_node(parser_ctx, NODE_EXPRESSION);
576 $$->u.expression.type = AST_EXP_STRING;
577 $$->u.expression.u.string = $2->s;
579 | LPAREN expression RPAREN
581 $$ = make_node(parser_ctx, NODE_EXPRESSION);
582 $$->u.expression.type = AST_EXP_NESTED;
583 $$->u.expression.u.child = $2;
590 $$ = make_node(parser_ctx, NODE_EXPRESSION);
591 $$->u.expression.type = AST_EXP_IDENTIFIER;
592 $$->u.expression.u.identifier = yylval.gs->s;
596 $$ = make_node(parser_ctx, NODE_EXPRESSION);
597 $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER;
598 $$->u.expression.u.identifier = yylval.gs->s;
602 prefix_expression_rec
603 : LSBRAC unary_expression RSBRAC
607 | LSBRAC unary_expression RSBRAC prefix_expression_rec
610 $$->u.expression.pre_op = AST_LINK_BRACKET;
611 $$->u.expression.prev = $4;
620 | identifiers prefix_expression_rec
623 $$->u.expression.pre_op = AST_LINK_BRACKET;
624 $$->u.expression.next_bracket = $2;
633 | postfix_expression DOT prefix_expression
636 $$->u.expression.post_op = AST_LINK_DOT;
637 $$->u.expression.prev = $1;
639 | postfix_expression RARROW prefix_expression
642 $$->u.expression.post_op = AST_LINK_RARROW;
643 $$->u.expression.prev = $1;
652 | unary_operator unary_expression
655 $$->u.unary_op.child = $2;
662 $$ = make_node(parser_ctx, NODE_UNARY_OP);
663 $$->u.unary_op.type = AST_UNARY_PLUS;
667 $$ = make_node(parser_ctx, NODE_UNARY_OP);
668 $$->u.unary_op.type = AST_UNARY_MINUS;
672 $$ = make_node(parser_ctx, NODE_UNARY_OP);
673 $$->u.unary_op.type = AST_UNARY_NOT;
677 $$ = make_node(parser_ctx, NODE_UNARY_OP);
678 $$->u.unary_op.type = AST_UNARY_BIT_NOT;
682 multiplicative_expression
685 | multiplicative_expression STAR unary_expression
687 $$ = make_op_node(parser_ctx, AST_OP_MUL, $1, $3);
689 | multiplicative_expression DIV_OP unary_expression
691 $$ = make_op_node(parser_ctx, AST_OP_DIV, $1, $3);
693 | multiplicative_expression MOD_OP unary_expression
695 $$ = make_op_node(parser_ctx, AST_OP_MOD, $1, $3);
700 : multiplicative_expression
702 | additive_expression PLUS multiplicative_expression
704 $$ = make_op_node(parser_ctx, AST_OP_PLUS, $1, $3);
706 | additive_expression MINUS multiplicative_expression
708 $$ = make_op_node(parser_ctx, AST_OP_MINUS, $1, $3);
713 : additive_expression
715 | shift_expression LEFT_OP additive_expression
717 $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3);
719 | shift_expression RIGHT_OP additive_expression
721 $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3);
728 | and_expression AND_BIN shift_expression
730 $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3);
734 exclusive_or_expression
737 | exclusive_or_expression XOR_BIN and_expression
739 $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3);
743 inclusive_or_expression
744 : exclusive_or_expression
746 | inclusive_or_expression OR_BIN exclusive_or_expression
748 $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3);
752 relational_expression
753 : inclusive_or_expression
755 | relational_expression LT_OP inclusive_or_expression
757 $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3);
759 | relational_expression GT_OP inclusive_or_expression
761 $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3);
763 | relational_expression LE_OP inclusive_or_expression
765 $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3);
767 | relational_expression GE_OP inclusive_or_expression
769 $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3);
774 : relational_expression
776 | equality_expression EQ_OP relational_expression
778 $$ = make_op_node(parser_ctx, AST_OP_EQ, $1, $3);
780 | equality_expression NE_OP relational_expression
782 $$ = make_op_node(parser_ctx, AST_OP_NE, $1, $3);
786 logical_and_expression
787 : equality_expression
789 | logical_and_expression AND_OP equality_expression
791 $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3);
795 logical_or_expression
796 : logical_and_expression
798 | logical_or_expression OR_OP logical_and_expression
800 $$ = make_op_node(parser_ctx, AST_OP_OR, $1, $3);
805 : logical_or_expression
812 parser_ctx->ast->root.u.root.child = $1;