]> www.infradead.org Git - users/hch/misc.git/commitdiff
perf parse-events: Fix parsing of >30kb event strings
authorIan Rogers <irogers@google.com>
Fri, 5 Sep 2025 04:26:43 +0000 (21:26 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Fri, 3 Oct 2025 22:06:19 +0000 (19:06 -0300)
Metrics may generate many particularly uncore event references. The
resulting event string may then be >32kb. The parse events lex is
using "%option reject" which stores backtracking state in a buffer
sized at roughtly 30kb. If the event string is larger than this then a
buffer overflow and typically a crash happens.

The need for "%option reject" was for BPF events which were removed in
commit 3d6dfae88917 ("perf parse-events: Remove BPF event
support"). As "%option reject" is both a memory and performance cost
let's remove it and fix the parsing case for event strings being over
~30kb.

Whilst cleaning up "%option reject" make the header files accurately
reflect functions used in the code and tidy up not requiring yywrap.

Measuring on the "PMU JSON event tests" a modest reduction of 0.41%
user time and 0.27% max resident size was observed. More importantly
this change fixes parsing large metrics and event strings.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/parse-events.l

index c985aa8a7d563f0eebfbf781684779c690bd5c2a..d65eb32124c8e211388565f6cddd00cad309ac65 100644 (file)
@@ -5,16 +5,14 @@
 %option stack
 %option bison-locations
 %option yylineno
-%option reject
+%option noyywrap
 
 %{
 #include <errno.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
 #include "parse-events.h"
 #include "parse-events-bison.h"
-#include "evsel.h"
 
 char *parse_events_get_text(yyscan_t yyscanner);
 YYSTYPE *parse_events_get_lval(yyscan_t yyscanner);
@@ -223,10 +221,6 @@ do {                                                       \
        yycolumn += yyleng;                             \
 } while (0);
 
-#define USER_REJECT            \
-       yycolumn -= yyleng;     \
-       REJECT
-
 %}
 
 %x mem
@@ -425,8 +419,3 @@ r{num_raw_hex}              { return str(yyscanner, PE_RAW); }
 .                      { }
 
 %%
-
-int parse_events_wrap(void *scanner __maybe_unused)
-{
-       return 1;
-}