Skip to contentSkip to conversation

Fix 2 memory leaks

PATCH Applied Ian Rogers 2 participants 4 messages Oct 16, 2024 – Oct 18, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v2 cover Fix 2 memory leaks

Fix memory leaks with libcapstone and libdw that are detected by leak sanitizer as part of perf test.

Original v1 was here:
https://lore.kernel.org/lkml/[email protected]/

v2: Avoid broken free reported by Namhyung. Move build fix to
subsequent patch.

Ian Rogers (2):
perf disasm: Fix capstone memory leak
perf probe: Fix libdw memory leak

 tools/perf/util/disasm.c       | 11 +++++++----
 tools/perf/util/probe-finder.c |  4 ++++
 tools/perf/util/probe-finder.h |  4 ++--
 3 files changed, 13 insertions(+), 6 deletions(-)
Signature · 1 line
2.47.0.105.g07ac214952-goog
author reviewer
#1 lore raw

PATCH v2 1/2 perf disasm: Fix capstone memory leak

The insn argument passed to cs_disasm needs freeing. To support accurately having count, add an additional free_count variable.

Fixes c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
---
 tools/perf/util/disasm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/disasm.c +7 -4
index f05ba7739c1e..2c8063660f2e 100644
@@ -1627,12 +1627,12 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 	u64 start = map__rip_2objdump(map, sym->start);
 	u64 len;
 	u64 offset;
-	int i, count;
+	int i, count, free_count;
 	bool is_64bit = false;
 	bool needs_cs_close = false;
 	u8 *buf = NULL;
 	csh handle;
-	cs_insn *insn;
+	cs_insn *insn = NULL;
 	char disasm_buf[512];
 	struct disasm_line *dl;
 
@@ -1664,7 +1664,7 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 
 	needs_cs_close = true;
 
-	count = cs_disasm(handle, buf, len, start, len, &insn);
+	free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
 	for (i = 0, offset = 0; i < count; i++) {
 		int printed;
 
@@ -1702,8 +1702,11 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 	}
 
 out:
-	if (needs_cs_close)
+	if (needs_cs_close) {
 		cs_close(&handle);
+		if (free_count > 0)
+			cs_free(insn, free_count);
+	}
 	free(buf);
 	return count < 0 ? count : 0;
 
Signature · 1 line
2.47.0.105.g07ac214952-goog
author reviewer
#2 lore raw

PATCH v2 2/2 perf probe: Fix libdw memory leak

In reply to Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh which is allocated and owned via a call to dwarf_getcfi_elf. Confusingly cfi_dbg shouldn't be freed as its memory is owned by the passed in debuginfo struct. Add comments to highlight this.

This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
 tools/perf/util/probe-finder.c | 4 ++++
 tools/perf/util/probe-finder.h | 4 ++--
 2 files changed, 6 insertions(+), 2 deletions(-)
tools/perf/util/probe-finder.c +4 -0
index 630e16c54ed5..d6b902899940 100644
@@ -1379,6 +1379,10 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
 	if (ret >= 0 && tf.pf.skip_empty_arg)
 		ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
 
+#if _ELFUTILS_PREREQ(0, 142)
+	dwarf_cfi_end(tf.pf.cfi_eh);
+#endif
+
 	if (ret < 0 || tf.ntevs == 0) {
 		for (i = 0; i < tf.ntevs; i++)
 			clear_probe_trace_event(&tf.tevs[i]);
tools/perf/util/probe-finder.h +2 -2
index 3add5ff516e1..724db829b49e 100644
@@ -64,9 +64,9 @@ struct probe_finder {
 
 	/* For variable searching */
 #if _ELFUTILS_PREREQ(0, 142)
-	/* Call Frame Information from .eh_frame */
+	/* Call Frame Information from .eh_frame. Owned by this struct. */
 	Dwarf_CFI		*cfi_eh;
-	/* Call Frame Information from .debug_frame */
+	/* Call Frame Information from .debug_frame. Not owned. */
 	Dwarf_CFI		*cfi_dbg;
 #endif
 	Dwarf_Op		*fb_ops;	/* Frame base attribute */
Signature · 1 line
2.47.0.105.g07ac214952-goog