Skip to contentSkip to conversation

perf parse-events: fix memory leaks found on parse_events

PATCH Superseded Ian Rogers 2 participants 4 messages Mar 16, 2020 – Mar 23, 2020 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf parse-events: fix memory leaks found on parse_events

Memory leaks found by applying LLVM's libfuzzer on the parse_events function.

---
 tools/lib/perf/evlist.c        | 2 ++
 tools/perf/util/parse-events.c | 2 ++
 tools/perf/util/parse-events.y | 3 ++-
 3 files changed, 6 insertions(+), 1 deletion(-)
tools/lib/perf/evlist.c +2 -0
index 5b9f2ca50591..6485d1438f75 100644
@@ -125,8 +125,10 @@ static void perf_evlist__purge(struct perf_evlist *evlist)
 void perf_evlist__exit(struct perf_evlist *evlist)
 {
 	perf_cpu_map__put(evlist->cpus);
+	perf_cpu_map__put(evlist->all_cpus);
 	perf_thread_map__put(evlist->threads);
 	evlist->cpus = NULL;
+	evlist->all_cpus = NULL;
 	evlist->threads = NULL;
 	fdarray__exit(&evlist->pollfd);
 }
tools/perf/util/parse-events.c +2 -0
index a14995835d85..997862224292 100644
@@ -1482,6 +1482,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
 
 		list_for_each_entry_safe(pos, tmp, &config_terms, list) {
 			list_del_init(&pos->list);
+			if (pos->free_str)
+				free(pos->val.str);
 			free(pos);
 		}
 		return -EINVAL;
tools/perf/util/parse-events.y +2 -1
index 94f8bcd83582..8212cc771667 100644
@@ -44,7 +44,7 @@ static void free_list_evsel(struct list_head* list_evsel)
 
 	list_for_each_entry_safe(evsel, tmp, list_evsel, core.node) {
 		list_del_init(&evsel->core.node);
-		perf_evsel__delete(evsel);
+		evsel__delete(evsel);
 	}
 	free(list_evsel);
 }
@@ -326,6 +326,7 @@ PE_NAME opt_pmu_config
 	}
 	parse_events_terms__delete($2);
 	parse_events_terms__delete(orig_terms);
+	free(pattern);
 	free($1);
 	$$ = list;
 #undef CLEANUP_YYABORT
Signature · 1 line
2.25.1.481.gfbce0eb801-goog
#1 lore raw
Quoted 15 lines · Ian Rogers

Memory leaks found by applying LLVM's libfuzzer on the parse_events function.

---
 tools/lib/perf/evlist.c        | 2 ++
 tools/perf/util/parse-events.c | 2 ++
 tools/perf/util/parse-events.y | 3 ++-
 3 files changed, 6 insertions(+), 1 deletion(-)
tools/lib/perf/evlist.c +0 -0
index 5b9f2ca50591..6485d1438f75 100644

tools/lib/perf/evlist.c static void perf_evlist__purge(struct perf_evlist *evlist) · line 128

Quoted 4 lines
tools/lib/perf/evlist.c +1 -0
@@ -125,8 +125,10 @@ static void perf_evlist__purge(struct perf_evlist *evlist)
 void perf_evlist__exit(struct perf_evlist *evlist)
 {
 	perf_cpu_map__put(evlist->cpus);
+	perf_cpu_map__put(evlist->all_cpus);

ugh, yes, could you please put it to separate libperf patch?

tools/lib/perf/evlist.c static void perf_evlist__purge(struct perf_evlist *evlist) · line 131

Quoted 3 lines
tools/lib/perf/evlist.c +1 -0
 	perf_thread_map__put(evlist->threads);
 	evlist->cpus = NULL;
+	evlist->all_cpus = NULL;

there's already change adding this waiting on the list:
https://lore.kernel.org/lkml/[email protected]/

tools/perf/util/parse-events.c int parse_events_add_pmu(struct parse_events_state *parse_state, · line 1486

Quoted 13 lines
tools/lib/perf/evlist.c +0 -0
 	evlist->threads = NULL;
 	fdarray__exit(&evlist->pollfd);
 }
tools/perf/util/parse-events.c +2 -0
index a14995835d85..997862224292 100644
@@ -1482,6 +1482,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
 
 		list_for_each_entry_safe(pos, tmp, &config_terms, list) {
 			list_del_init(&pos->list);
+			if (pos->free_str)
+				free(pos->val.str);

ack, would be nice to have perf_evsel__free_config_terms generalized to work directly over config terms list, so we'd have only single cleanup function

tools/perf/util/parse-events.y static void free_list_evsel(struct list_head* list_evsel) · line 47

Quoted 13 lines
tools/perf/util/parse-events.c +0 -0
 			free(pos);
 		}
 		return -EINVAL;
tools/perf/util/parse-events.y +1 -1
index 94f8bcd83582..8212cc771667 100644
@@ -44,7 +44,7 @@ static void free_list_evsel(struct list_head* list_evsel)
 
 	list_for_each_entry_safe(evsel, tmp, list_evsel, core.node) {
 		list_del_init(&evsel->core.node);
-		perf_evsel__delete(evsel);
+		evsel__delete(evsel);

ack

tools/perf/util/parse-events.y PE_NAME opt_pmu_config · line 329

Quoted 8 lines
tools/perf/util/parse-events.y +1 -0
 	}
 	free(list_evsel);
 }
@@ -326,6 +326,7 @@ PE_NAME opt_pmu_config
 	}
 	parse_events_terms__delete($2);
 	parse_events_terms__delete(orig_terms);
+	free(pattern);

ack

could you please send the separate change for libperf? and synchronize with that other patch mentioned above

thanks,
jirka

author reviewer
#2 lore raw
Quoted 12 lines · Jiri Olsa
Quoted 10 lines · Ian Rogers

Memory leaks found by applying LLVM's libfuzzer on the parse_events function.

---
 tools/lib/perf/evlist.c        | 2 ++
 tools/perf/util/parse-events.c | 2 ++
 tools/perf/util/parse-events.y | 3 ++-
 3 files changed, 6 insertions(+), 1 deletion(-)
Quoted 11 lines

tools/lib/perf/evlist.c static void perf_evlist__purge(struct perf_evlist *evlist) · line 128

Quoted 9 lines
tools/lib/perf/evlist.c +1 -0
index 5b9f2ca50591..6485d1438f75 100644
@@ -125,8 +125,10 @@ static void perf_evlist__purge(struct perf_evlist *evlist)
 void perf_evlist__exit(struct perf_evlist *evlist)
 {
      perf_cpu_map__put(evlist->cpus);
+     perf_cpu_map__put(evlist->all_cpus);

ugh, yes, could you please put it to separate libperf patch?

Done. https://lkml.org/lkml/2020/3/18/1318

Quoted 6 lines

tools/lib/perf/evlist.c static void perf_evlist__purge(struct perf_evlist *evlist) · line 131

Quoted 3 lines
tools/lib/perf/evlist.c +1 -0
      perf_thread_map__put(evlist->threads);
      evlist->cpus = NULL;
+     evlist->all_cpus = NULL;

there's already change adding this waiting on the list:
https://lore.kernel.org/lkml/[email protected]/

I'm not seeing this in perf/core on
git://git.kernel.org/pub/scm/linux/kernel/git/acme/linux.git
The previous thread mentions Arnaldo porting it. It is only 1 statement so I've left it in.

Quoted 45 lines

tools/perf/util/parse-events.c int parse_events_add_pmu(struct parse_events_state *parse_state, · line 1486

Quoted 13 lines
tools/lib/perf/evlist.c +0 -0
      evlist->threads = NULL;
      fdarray__exit(&evlist->pollfd);
 }
tools/perf/util/parse-events.c +2 -0
index a14995835d85..997862224292 100644
@@ -1482,6 +1482,8 @@ int parse_events_add_pmu(struct parse_events_state *parse_state,
 
              list_for_each_entry_safe(pos, tmp, &config_terms, list) {
                      list_del_init(&pos->list);
+                     if (pos->free_str)
+                             free(pos->val.str);

ack, would be nice to have perf_evsel__free_config_terms generalized to work directly over config terms list, so we'd have only single cleanup function

tools/perf/util/parse-events.y static void free_list_evsel(struct list_head* list_evsel) · line 47

Quoted 13 lines
tools/perf/util/parse-events.c +0 -0
                      free(pos);
              }
              return -EINVAL;
tools/perf/util/parse-events.y +1 -1
index 94f8bcd83582..8212cc771667 100644
@@ -44,7 +44,7 @@ static void free_list_evsel(struct list_head* list_evsel)
 
      list_for_each_entry_safe(evsel, tmp, list_evsel, core.node) {
              list_del_init(&evsel->core.node);
-             perf_evsel__delete(evsel);
+             evsel__delete(evsel);

ack

tools/perf/util/parse-events.y PE_NAME opt_pmu_config · line 329

Quoted 8 lines
tools/perf/util/parse-events.y +1 -0
      }
      free(list_evsel);
 }
@@ -326,6 +326,7 @@ PE_NAME opt_pmu_config
      }
      parse_events_terms__delete($2);
      parse_events_terms__delete(orig_terms);
+     free(pattern);

ack

Quoted 2 lines

could you please send the separate change for libperf? and synchronize with that other patch mentioned above

Done-ish. Thanks,
Ian

Quoted 7 lines

thanks,
jirka

--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20200318104011.GF821557%40krava.

#3 lore raw
Quoted 25 lines · Ian Rogers
Quoted 12 lines · Jiri Olsa
Quoted 10 lines · Ian Rogers

Memory leaks found by applying LLVM's libfuzzer on the parse_events function.

---
 tools/lib/perf/evlist.c        | 2 ++
 tools/perf/util/parse-events.c | 2 ++
 tools/perf/util/parse-events.y | 3 ++-
 3 files changed, 6 insertions(+), 1 deletion(-)
Quoted 11 lines

tools/lib/perf/evlist.c static void perf_evlist__purge(struct perf_evlist *evlist) · line 128

Quoted 9 lines
tools/lib/perf/evlist.c +1 -0
index 5b9f2ca50591..6485d1438f75 100644
@@ -125,8 +125,10 @@ static void perf_evlist__purge(struct perf_evlist *evlist)
 void perf_evlist__exit(struct perf_evlist *evlist)
 {
      perf_cpu_map__put(evlist->cpus);
+     perf_cpu_map__put(evlist->all_cpus);

ugh, yes, could you please put it to separate libperf patch?

Quoted 8 lines

Done. https://lkml.org/lkml/2020/3/18/1318

Quoted 6 lines

tools/lib/perf/evlist.c static void perf_evlist__purge(struct perf_evlist *evlist) · line 131

Quoted 3 lines
tools/lib/perf/evlist.c +1 -0
      perf_thread_map__put(evlist->threads);
      evlist->cpus = NULL;
+     evlist->all_cpus = NULL;

there's already change adding this waiting on the list:
https://lore.kernel.org/lkml/[email protected]/

Arnaldo, could you plz pull this one ^^^ ?

thanks,
jirka