X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Flib%2Flttng-ctl%2Ffilter%2Ffilter-parser.y;h=ba2d3007b932754ea080d3f88ed4ff030aeda8d6;hp=3f301340dbb4ccff7150d9ce79d4d86046d07563;hb=52dc69b2dc349a43f108a9926b0794ee6b6ec4c3;hpb=586dc72f727912b4aa381789c418e062bce89d08 diff --git a/src/lib/lttng-ctl/filter/filter-parser.y b/src/lib/lttng-ctl/filter/filter-parser.y index 3f301340d..ba2d3007b 100644 --- a/src/lib/lttng-ctl/filter/filter-parser.y +++ b/src/lib/lttng-ctl/filter/filter-parser.y @@ -4,20 +4,9 @@ * * LTTng filter expression parser * - * Copyright 2012 - Mathieu Desnoyers + * Copyright 2012 Mathieu Desnoyers * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License, version 2.1 only, - * as published by the Free Software Foundation. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * SPDX-License-Identifier: LGPL-2.1-only * * Grammar inspired from http://www.quut.com/c/ANSI-C-grammar-y.html */ @@ -34,15 +23,20 @@ #include +#define WIDTH_u64_SCANF_IS_A_BROKEN_API "20" +#define WIDTH_o64_SCANF_IS_A_BROKEN_API "22" +#define WIDTH_x64_SCANF_IS_A_BROKEN_API "17" +#define WIDTH_lg_SCANF_IS_A_BROKEN_API "4096" /* Hugely optimistic approximation */ + LTTNG_HIDDEN int yydebug; LTTNG_HIDDEN int filter_parser_debug = 0; LTTNG_HIDDEN -int yyparse(struct filter_parser_ctx *parser_ctx); +int yyparse(struct filter_parser_ctx *parser_ctx, yyscan_t scanner); LTTNG_HIDDEN -int yylex(union YYSTYPE *yyval, struct filter_parser_ctx *parser_ctx); +int yylex(union YYSTYPE *yyval, yyscan_t scanner); LTTNG_HIDDEN int yylex_init_extra(struct filter_parser_ctx *parser_ctx, yyscan_t * ptr_yy_globals); LTTNG_HIDDEN @@ -84,9 +78,13 @@ static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx, for (alloclen = 8; alloclen < sizeof(long) + sizeof(*gstr) + len; alloclen *= 2); - gstr = malloc(alloclen); + gstr = zmalloc(alloclen); + if (!gstr) { + goto end; + } cds_list_add(&gstr->gc, &parser_ctx->allocated_strings); gstr->alloclen = alloclen; +end: return gstr; } @@ -95,7 +93,7 @@ static struct gc_string *gc_string_alloc(struct filter_parser_ctx *parser_ctx, * gsrc will be garbage collected immediately, and gstr might be. * Should only be used to append characters to a string literal or constant. */ -LTTNG_HIDDEN +static struct gc_string *gc_string_append(struct filter_parser_ctx *parser_ctx, struct gc_string *gstr, struct gc_string *gsrc) @@ -138,7 +136,7 @@ static struct filter_node *make_node(struct filter_parser_ctx *scanner, struct filter_ast *ast = filter_parser_get_ast(scanner); struct filter_node *node; - node = malloc(sizeof(*node)); + node = zmalloc(sizeof(*node)); if (!node) return NULL; memset(node, 0, sizeof(*node)); @@ -175,7 +173,7 @@ static struct filter_node *make_op_node(struct filter_parser_ctx *scanner, struct filter_ast *ast = filter_parser_get_ast(scanner); struct filter_node *node; - node = malloc(sizeof(*node)); + node = zmalloc(sizeof(*node)); if (!node) return NULL; memset(node, 0, sizeof(*node)); @@ -187,21 +185,15 @@ static struct filter_node *make_op_node(struct filter_parser_ctx *scanner, return node; } -LTTNG_HIDDEN -void yyerror(struct filter_parser_ctx *parser_ctx, const char *str) +static +void yyerror(struct filter_parser_ctx *parser_ctx, yyscan_t scanner, const char *str) { fprintf(stderr, "error %s\n", str); } - -LTTNG_HIDDEN -int yywrap(void) -{ - return 1; -} #define parse_error(parser_ctx, str) \ do { \ - yyerror(parser_ctx, YY_("parse error: " str "\n")); \ + yyerror(parser_ctx, parser_ctx->scanner, YY_("parse error: " str "\n")); \ YYERROR; \ } while (0) @@ -217,7 +209,7 @@ static struct filter_ast *filter_ast_alloc(void) { struct filter_ast *ast; - ast = malloc(sizeof(*ast)); + ast = zmalloc(sizeof(*ast)); if (!ast) return NULL; memset(ast, 0, sizeof(*ast)); @@ -238,7 +230,7 @@ static void filter_ast_free(struct filter_ast *ast) LTTNG_HIDDEN int filter_parser_ctx_append_ast(struct filter_parser_ctx *parser_ctx) { - return yyparse(parser_ctx); + return yyparse(parser_ctx, parser_ctx->scanner); } LTTNG_HIDDEN @@ -249,7 +241,7 @@ struct filter_parser_ctx *filter_parser_ctx_alloc(FILE *input) yydebug = filter_parser_debug; - parser_ctx = malloc(sizeof(*parser_ctx)); + parser_ctx = zmalloc(sizeof(*parser_ctx)); if (!parser_ctx) return NULL; memset(parser_ctx, 0, sizeof(*parser_ctx)); @@ -298,10 +290,19 @@ void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx) %} +%code provides +{ +#include "common/macros.h" + +LTTNG_HIDDEN +void setstring(struct filter_parser_ctx *parser_ctx, YYSTYPE *lvalp, const char *src); +} + %define api.pure /* %locations */ %parse-param {struct filter_parser_ctx *parser_ctx} -%lex-param {struct filter_parser_ctx *parser_ctx} +%parse-param {yyscan_t scanner} +%lex-param {yyscan_t scanner} %start translation_unit %token CHARACTER_CONSTANT_START SQUOTE STRING_LITERAL_START DQUOTE %token ESCSEQ CHAR_STRING_TOKEN @@ -326,6 +327,8 @@ void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx) %type s_char s_char_sequence c_char c_char_sequence %type primary_expression +%type prefix_expression +%type prefix_expression_rec %type postfix_expression %type unary_expression %type unary_operator @@ -340,6 +343,7 @@ void filter_parser_ctx_free(struct filter_parser_ctx *parser_ctx) %type logical_and_expression %type logical_or_expression %type expression +%type identifiers %% @@ -380,47 +384,44 @@ s_char: } ; -primary_expression - : IDENTIFIER - { - $$ = make_node(parser_ctx, NODE_EXPRESSION); - $$->u.expression.type = AST_EXP_IDENTIFIER; - $$->u.expression.u.identifier = yylval.gs->s; - } - | GLOBAL_IDENTIFIER - { - $$ = make_node(parser_ctx, NODE_EXPRESSION); - $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER; - $$->u.expression.u.identifier = yylval.gs->s; - } - - | DECIMAL_CONSTANT +primary_expression: + DECIMAL_CONSTANT { $$ = make_node(parser_ctx, NODE_EXPRESSION); $$->u.expression.type = AST_EXP_CONSTANT; - sscanf(yylval.gs->s, "%" PRIu64, - &$$->u.expression.u.constant); + if (sscanf(yylval.gs->s, "%" WIDTH_u64_SCANF_IS_A_BROKEN_API SCNu64, + &$$->u.expression.u.constant) != 1) { + parse_error(parser_ctx, "cannot scanf decimal constant"); + } } | OCTAL_CONSTANT { $$ = make_node(parser_ctx, NODE_EXPRESSION); $$->u.expression.type = AST_EXP_CONSTANT; - sscanf(yylval.gs->s, "0%" PRIo64, - &$$->u.expression.u.constant); + if (!strcmp(yylval.gs->s, "0")) { + $$->u.expression.u.constant = 0; + } else if (sscanf(yylval.gs->s, "0%" WIDTH_o64_SCANF_IS_A_BROKEN_API SCNo64, + &$$->u.expression.u.constant) != 1) { + parse_error(parser_ctx, "cannot scanf octal constant"); + } } | HEXADECIMAL_CONSTANT { $$ = make_node(parser_ctx, NODE_EXPRESSION); $$->u.expression.type = AST_EXP_CONSTANT; - sscanf(yylval.gs->s, "0x%" PRIx64, - &$$->u.expression.u.constant); + if (sscanf(yylval.gs->s, "0x%" WIDTH_x64_SCANF_IS_A_BROKEN_API SCNx64, + &$$->u.expression.u.constant) != 1) { + parse_error(parser_ctx, "cannot scanf hexadecimal constant"); + } } | FLOAT_CONSTANT { $$ = make_node(parser_ctx, NODE_EXPRESSION); $$->u.expression.type = AST_EXP_FLOAT_CONSTANT; - sscanf(yylval.gs->s, "%lg", - &$$->u.expression.u.float_constant); + if (sscanf(yylval.gs->s, "%" WIDTH_lg_SCANF_IS_A_BROKEN_API "lg", + &$$->u.expression.u.float_constant) != 1) { + parse_error(parser_ctx, "cannot scanf float constant"); + } } | STRING_LITERAL_START DQUOTE { @@ -448,23 +449,62 @@ primary_expression } ; -postfix_expression - : primary_expression - { $$ = $1; } - | postfix_expression DOT IDENTIFIER +identifiers + : IDENTIFIER { $$ = make_node(parser_ctx, NODE_EXPRESSION); $$->u.expression.type = AST_EXP_IDENTIFIER; + $$->u.expression.u.identifier = yylval.gs->s; + } + | GLOBAL_IDENTIFIER + { + $$ = make_node(parser_ctx, NODE_EXPRESSION); + $$->u.expression.type = AST_EXP_GLOBAL_IDENTIFIER; + $$->u.expression.u.identifier = yylval.gs->s; + } + ; + +prefix_expression_rec + : LSBRAC unary_expression RSBRAC + { + $$ = $2; + } + | LSBRAC unary_expression RSBRAC prefix_expression_rec + { + $$ = $2; + $$->u.expression.pre_op = AST_LINK_BRACKET; + $$->u.expression.prev = $4; + } + ; + +prefix_expression + : identifiers + { + $$ = $1; + } + | identifiers prefix_expression_rec + { + $$ = $1; + $$->u.expression.pre_op = AST_LINK_BRACKET; + $$->u.expression.next_bracket = $2; + } + ; + +postfix_expression + : prefix_expression + { + $$ = $1; + } + | postfix_expression DOT prefix_expression + { + $$ = $3; $$->u.expression.post_op = AST_LINK_DOT; - $$->u.expression.u.identifier = $3->s; $$->u.expression.prev = $1; } - | postfix_expression RARROW IDENTIFIER + | postfix_expression RARROW prefix_expression { - $$ = make_node(parser_ctx, NODE_EXPRESSION); - $$->u.expression.type = AST_EXP_IDENTIFIER; + $$ = $3; $$->u.expression.post_op = AST_LINK_RARROW; - $$->u.expression.u.identifier = $3->s; $$->u.expression.prev = $1; } ; @@ -472,6 +512,8 @@ postfix_expression unary_expression : postfix_expression { $$ = $1; } + | primary_expression + { $$ = $1; } | unary_operator unary_expression { $$ = $1; @@ -498,7 +540,7 @@ unary_operator | NOT_BIN { $$ = make_node(parser_ctx, NODE_UNARY_OP); - $$->u.unary_op.type = AST_UNARY_BIN_NOT; + $$->u.unary_op.type = AST_UNARY_BIT_NOT; } ; @@ -537,30 +579,57 @@ shift_expression { $$ = $1; } | shift_expression LEFT_OP additive_expression { - $$ = make_op_node(parser_ctx, AST_OP_LSHIFT, $1, $3); + $$ = make_op_node(parser_ctx, AST_OP_BIT_LSHIFT, $1, $3); } | shift_expression RIGHT_OP additive_expression { - $$ = make_op_node(parser_ctx, AST_OP_RSHIFT, $1, $3); + $$ = make_op_node(parser_ctx, AST_OP_BIT_RSHIFT, $1, $3); } ; -relational_expression +and_expression : shift_expression { $$ = $1; } - | relational_expression LT_OP shift_expression + | and_expression AND_BIN shift_expression + { + $$ = make_op_node(parser_ctx, AST_OP_BIT_AND, $1, $3); + } + ; + +exclusive_or_expression + : and_expression + { $$ = $1; } + | exclusive_or_expression XOR_BIN and_expression + { + $$ = make_op_node(parser_ctx, AST_OP_BIT_XOR, $1, $3); + } + ; + +inclusive_or_expression + : exclusive_or_expression + { $$ = $1; } + | inclusive_or_expression OR_BIN exclusive_or_expression + { + $$ = make_op_node(parser_ctx, AST_OP_BIT_OR, $1, $3); + } + ; + +relational_expression + : inclusive_or_expression + { $$ = $1; } + | relational_expression LT_OP inclusive_or_expression { $$ = make_op_node(parser_ctx, AST_OP_LT, $1, $3); } - | relational_expression GT_OP shift_expression + | relational_expression GT_OP inclusive_or_expression { $$ = make_op_node(parser_ctx, AST_OP_GT, $1, $3); } - | relational_expression LE_OP shift_expression + | relational_expression LE_OP inclusive_or_expression { $$ = make_op_node(parser_ctx, AST_OP_LE, $1, $3); } - | relational_expression GE_OP shift_expression + | relational_expression GE_OP inclusive_or_expression { $$ = make_op_node(parser_ctx, AST_OP_GE, $1, $3); } @@ -579,37 +648,10 @@ equality_expression } ; -and_expression - : equality_expression - { $$ = $1; } - | and_expression AND_BIN equality_expression - { - $$ = make_op_node(parser_ctx, AST_OP_BIN_AND, $1, $3); - } - ; - -exclusive_or_expression - : and_expression - { $$ = $1; } - | exclusive_or_expression XOR_BIN and_expression - { - $$ = make_op_node(parser_ctx, AST_OP_BIN_XOR, $1, $3); - } - ; - -inclusive_or_expression - : exclusive_or_expression - { $$ = $1; } - | inclusive_or_expression OR_BIN exclusive_or_expression - { - $$ = make_op_node(parser_ctx, AST_OP_BIN_OR, $1, $3); - } - ; - logical_and_expression - : inclusive_or_expression + : equality_expression { $$ = $1; } - | logical_and_expression AND_OP inclusive_or_expression + | logical_and_expression AND_OP equality_expression { $$ = make_op_node(parser_ctx, AST_OP_AND, $1, $3); }