[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <ZvMHHKKZwab2IhlL@google.com>
Date: Tue, 24 Sep 2024 11:38:20 -0700
From: Namhyung Kim <namhyung@...nel.org>
To: Ian Rogers <irogers@...gle.com>
Cc: Peter Zijlstra <peterz@...radead.org>, Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Jiri Olsa <jolsa@...nel.org>,
Adrian Hunter <adrian.hunter@...el.com>,
Kan Liang <kan.liang@...ux.intel.com>,
Kajol Jain <kjain@...ux.ibm.com>,
Athira Rajeev <atrajeev@...ux.vnet.ibm.com>,
"Steinar H. Gunderson" <sesse@...gle.com>,
Masami Hiramatsu <mhiramat@...nel.org>,
"David S. Miller" <davem@...emloft.net>,
Przemek Kitszel <przemyslaw.kitszel@...el.com>,
Alexander Lobakin <aleksander.lobakin@...el.com>,
Hemant Kumar <hemant@...ux.vnet.ibm.com>,
linux-perf-users@...r.kernel.org, linux-kernel@...r.kernel.org,
Yang Jihong <yangjihong@...edance.com>, leo.yan@....com
Subject: Re: [PATCH v1 1/3] perf disasm: Fix capstone memory leak
On Mon, Sep 23, 2024 at 05:37:18PM -0700, Ian Rogers wrote:
> 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@...gle.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);
It seems cs_free() can handle NULL insn and 0 free_count (like regular free)
so we can call it unconditionally.
Thanks,
Namhyung
> + }
> free(buf);
> return count < 0 ? count : 0;
>
> --
> 2.46.0.792.g87dc391469-goog
>
Powered by blists - more mailing lists