Skip to contentSkip to conversation

Fix leak sanitizer warnings in perf top

PATCH New Ian Rogers 1 participant 3 messages Jun 7, 2023 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

cover Fix leak sanitizer warnings in perf top

With these two patches perf top/report/script will largely run without leak sanitizer warnings.

Ian Rogers (2):
perf annotate: Fix parse_objdump_line memory leak perf top: Add exit routine for main thread

 tools/perf/builtin-top.c   | 11 ++++++++++-
 tools/perf/util/annotate.c |  3 ++-
 2 files changed, 12 insertions(+), 2 deletions(-)
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
author reviewer
#1 lore raw

PATCH 1/2 perf annotate: Fix parse_objdump_line memory leak

fileloc is used to hold a previous line, before overwriting it ensure the previous contents is freed. Free the storage once done in symbol__disassemble.

---
 tools/perf/util/annotate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/annotate.c +2 -1
index b708bbc49c9e..fc5f44535ebe 100644
@@ -1524,6 +1524,7 @@ static int symbol__parse_objdump_line(struct symbol *sym,
 	/* /filename:linenr ? Save line number and ignore. */
 	if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
 		*line_nr = atoi(parsed_line + match[1].rm_so);
+		free(*fileloc);
 		*fileloc = strdup(parsed_line);
 		return 0;
 	}
@@ -1572,7 +1573,6 @@ static int symbol__parse_objdump_line(struct symbol *sym,
 	}
 
 	annotation_line__add(&dl->al, &notes->src->source);
-
 	return 0;
 }
 
@@ -2114,6 +2114,7 @@ static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
 		nline++;
 	}
 	free(line);
+	free(fileloc);
 
 	err = finish_command(&objdump_process);
 	if (err)
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
author reviewer
#2 lore raw

PATCH 2/2 perf top: Add exit routine for main thread

In reply to Ian Rogers

Add exit_process_thread that reverses init_process_thread. This avoids leak sanitizer reporting memory leaks.

---
 tools/perf/builtin-top.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
tools/perf/builtin-top.c +10 -1
index 99010dfa5760..c363c04e16df 100644
@@ -392,7 +392,7 @@ static void prompt_percent(int *target, const char *msg)
 
 static void perf_top__prompt_symbol(struct perf_top *top, const char *msg)
 {
-	char *buf = malloc(0), *p;
+	char *buf = NULL, *p;
 	struct hist_entry *syme = top->sym_filter_entry, *n, *found = NULL;
 	struct hists *hists = evsel__hists(top->sym_evsel);
 	struct rb_node *next;
@@ -1227,6 +1227,14 @@ static void init_process_thread(struct perf_top *top)
 	cond_init(&top->qe.cond);
 }
 
+static void exit_process_thread(struct perf_top *top)
+{
+	ordered_events__free(&top->qe.data[0]);
+	ordered_events__free(&top->qe.data[1]);
+	mutex_destroy(&top->qe.mutex);
+	cond_destroy(&top->qe.cond);
+}
+
 static int __cmd_top(struct perf_top *top)
 {
 	struct record_opts *opts = &top->record_opts;
@@ -1357,6 +1365,7 @@ static int __cmd_top(struct perf_top *top)
 	cond_signal(&top->qe.cond);
 	pthread_join(thread_process, NULL);
 	perf_set_singlethreaded();
+	exit_process_thread(top);
 	return ret;
 }
 
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog