* [PATCH v2 0/2] Fix 2 memory leaks
@ 2024-10-16 23:56 Ian Rogers
2024-10-16 23:56 ` [PATCH v2 1/2] perf disasm: Fix capstone memory leak Ian Rogers
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Ian Rogers @ 2024-10-16 23:56 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, Kan Liang, Kajol Jain, Athira Rajeev,
Steinar H. Gunderson, Masami Hiramatsu (Google),
Alexander Lobakin, David S. Miller, Hemant Kumar,
linux-perf-users, linux-kernel
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/20240924003720.617258-1-irogers@google.com/
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(-)
--
2.47.0.105.g07ac214952-goog
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 1/2] perf disasm: Fix capstone memory leak
2024-10-16 23:56 [PATCH v2 0/2] Fix 2 memory leaks Ian Rogers
@ 2024-10-16 23:56 ` Ian Rogers
2024-10-16 23:56 ` [PATCH v2 2/2] perf probe: Fix libdw " Ian Rogers
2024-10-18 15:20 ` [PATCH v2 0/2] Fix 2 memory leaks Namhyung Kim
2 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2024-10-16 23:56 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, Kan Liang, Kajol Jain, Athira Rajeev,
Steinar H. Gunderson, Masami Hiramatsu (Google),
Alexander Lobakin, David S. Miller, Hemant Kumar,
linux-perf-users, linux-kernel
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")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/disasm.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index f05ba7739c1e..2c8063660f2e 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -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;
--
2.47.0.105.g07ac214952-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] perf probe: Fix libdw memory leak
2024-10-16 23:56 [PATCH v2 0/2] Fix 2 memory leaks Ian Rogers
2024-10-16 23:56 ` [PATCH v2 1/2] perf disasm: Fix capstone memory leak Ian Rogers
@ 2024-10-16 23:56 ` Ian Rogers
2024-10-18 15:20 ` [PATCH v2 0/2] Fix 2 memory leaks Namhyung Kim
2 siblings, 0 replies; 4+ messages in thread
From: Ian Rogers @ 2024-10-16 23:56 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, Kan Liang, Kajol Jain, Athira Rajeev,
Steinar H. Gunderson, Masami Hiramatsu (Google),
Alexander Lobakin, David S. Miller, Hemant Kumar,
linux-perf-users, linux-kernel
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")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/probe-finder.c | 4 ++++
tools/perf/util/probe-finder.h | 4 ++--
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c
index 630e16c54ed5..d6b902899940 100644
--- a/tools/perf/util/probe-finder.c
+++ b/tools/perf/util/probe-finder.c
@@ -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]);
diff --git a/tools/perf/util/probe-finder.h b/tools/perf/util/probe-finder.h
index 3add5ff516e1..724db829b49e 100644
--- a/tools/perf/util/probe-finder.h
+++ b/tools/perf/util/probe-finder.h
@@ -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 */
--
2.47.0.105.g07ac214952-goog
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v2 0/2] Fix 2 memory leaks
2024-10-16 23:56 [PATCH v2 0/2] Fix 2 memory leaks Ian Rogers
2024-10-16 23:56 ` [PATCH v2 1/2] perf disasm: Fix capstone memory leak Ian Rogers
2024-10-16 23:56 ` [PATCH v2 2/2] perf probe: Fix libdw " Ian Rogers
@ 2024-10-18 15:20 ` Namhyung Kim
2 siblings, 0 replies; 4+ messages in thread
From: Namhyung Kim @ 2024-10-18 15:20 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Mark Rutland, Alexander Shishkin, Jiri Olsa, Adrian Hunter,
Kan Liang, Kajol Jain, Athira Rajeev, Steinar H. Gunderson,
Masami Hiramatsu (Google), Alexander Lobakin, David S. Miller,
Hemant Kumar, linux-perf-users, linux-kernel, Ian Rogers
On Wed, 16 Oct 2024 16:56:20 -0700, Ian Rogers wrote:
> 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/20240924003720.617258-1-irogers@google.com/
>
> v2: Avoid broken free reported by Namhyung. Move build fix to
> subsequent patch.
>
> [...]
Applied to perf-tools-next, thanks!
Best regards,
Namhyung
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2024-10-18 15:20 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-16 23:56 [PATCH v2 0/2] Fix 2 memory leaks Ian Rogers
2024-10-16 23:56 ` [PATCH v2 1/2] perf disasm: Fix capstone memory leak Ian Rogers
2024-10-16 23:56 ` [PATCH v2 2/2] perf probe: Fix libdw " Ian Rogers
2024-10-18 15:20 ` [PATCH v2 0/2] Fix 2 memory leaks Namhyung Kim
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).