[1/1] perf probe: Free string returned by synthesize_perf_probe_point() on failure to add a probe

Message ID ZM0l1Oxamr4SVjfY@kernel.org
State New
Headers
Series [1/1] perf probe: Free string returned by synthesize_perf_probe_point() on failure to add a probe |

Commit Message

Arnaldo Carvalho de Melo Aug. 4, 2023, 4:22 p.m. UTC
  Building perf with EXTRA_CFLAGS="-fsanitize=address" a leak is detect
when trying to add a probe to a non-existent function:

  # perf probe -x ~/bin/perf dso__neW
  Probe point 'dso__neW' not found.
    Error: Failed to add events.

  =================================================================
  ==296634==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 128 byte(s) in 1 object(s) allocated from:
      #0 0x7f67642ba097 in calloc (/lib64/libasan.so.8+0xba097)
      #1 0x7f67641a76f1 in allocate_cfi (/lib64/libdw.so.1+0x3f6f1)

  Direct leak of 65 byte(s) in 1 object(s) allocated from:
      #0 0x7f67642b95b5 in __interceptor_realloc.part.0 (/lib64/libasan.so.8+0xb95b5)
      #1 0x6cac75 in strbuf_grow util/strbuf.c:64
      #2 0x6ca934 in strbuf_init util/strbuf.c:25
      #3 0x9337d2 in synthesize_perf_probe_point util/probe-event.c:2018
      #4 0x92be51 in try_to_find_probe_trace_events util/probe-event.c:964
      #5 0x93d5c6 in convert_to_probe_trace_events util/probe-event.c:3512
      #6 0x93d6d5 in convert_perf_probe_events util/probe-event.c:3529
      #7 0x56f37f in perf_add_probe_events /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:354
      #8 0x572fbc in __cmd_probe /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:738
      #9 0x5730f2 in cmd_probe /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:766
      #10 0x635d81 in run_builtin /var/home/acme/git/perf-tools-next/tools/perf/perf.c:323
      #11 0x6362c1 in handle_internal_command /var/home/acme/git/perf-tools-next/tools/perf/perf.c:377
      #12 0x63667a in run_argv /var/home/acme/git/perf-tools-next/tools/perf/perf.c:421
      #13 0x636b8d in main /var/home/acme/git/perf-tools-next/tools/perf/perf.c:537
      #14 0x7f676302950f in __libc_start_call_main (/lib64/libc.so.6+0x2950f)

  SUMMARY: AddressSanitizer: 193 byte(s) leaked in 2 allocation(s).
  #

synthesize_perf_probe_point() returns a "detached" strbuf, i.e. a
malloc'ed string that needs to be free'd.

An audit will be performed to find other such cases.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/lkml/
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/probe-event.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Masami Hiramatsu (Google) Aug. 5, 2023, 2:01 p.m. UTC | #1
On Fri, 4 Aug 2023 13:22:44 -0300
Arnaldo Carvalho de Melo <acme@kernel.org> wrote:

> Building perf with EXTRA_CFLAGS="-fsanitize=address" a leak is detect
> when trying to add a probe to a non-existent function:
> 
>   # perf probe -x ~/bin/perf dso__neW
>   Probe point 'dso__neW' not found.
>     Error: Failed to add events.
> 
>   =================================================================
>   ==296634==ERROR: LeakSanitizer: detected memory leaks
> 
>   Direct leak of 128 byte(s) in 1 object(s) allocated from:
>       #0 0x7f67642ba097 in calloc (/lib64/libasan.so.8+0xba097)
>       #1 0x7f67641a76f1 in allocate_cfi (/lib64/libdw.so.1+0x3f6f1)
> 
>   Direct leak of 65 byte(s) in 1 object(s) allocated from:
>       #0 0x7f67642b95b5 in __interceptor_realloc.part.0 (/lib64/libasan.so.8+0xb95b5)
>       #1 0x6cac75 in strbuf_grow util/strbuf.c:64
>       #2 0x6ca934 in strbuf_init util/strbuf.c:25
>       #3 0x9337d2 in synthesize_perf_probe_point util/probe-event.c:2018
>       #4 0x92be51 in try_to_find_probe_trace_events util/probe-event.c:964
>       #5 0x93d5c6 in convert_to_probe_trace_events util/probe-event.c:3512
>       #6 0x93d6d5 in convert_perf_probe_events util/probe-event.c:3529
>       #7 0x56f37f in perf_add_probe_events /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:354
>       #8 0x572fbc in __cmd_probe /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:738
>       #9 0x5730f2 in cmd_probe /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:766
>       #10 0x635d81 in run_builtin /var/home/acme/git/perf-tools-next/tools/perf/perf.c:323
>       #11 0x6362c1 in handle_internal_command /var/home/acme/git/perf-tools-next/tools/perf/perf.c:377
>       #12 0x63667a in run_argv /var/home/acme/git/perf-tools-next/tools/perf/perf.c:421
>       #13 0x636b8d in main /var/home/acme/git/perf-tools-next/tools/perf/perf.c:537
>       #14 0x7f676302950f in __libc_start_call_main (/lib64/libc.so.6+0x2950f)
> 
>   SUMMARY: AddressSanitizer: 193 byte(s) leaked in 2 allocation(s).
>   #
> 
> synthesize_perf_probe_point() returns a "detached" strbuf, i.e. a
> malloc'ed string that needs to be free'd.
> 
> An audit will be performed to find other such cases.

Thanks for finding it!

Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

> 
> Cc: Adrian Hunter <adrian.hunter@intel.com>
> Cc: Ian Rogers <irogers@google.com>
> Cc: Jiri Olsa <jolsa@kernel.org>
> Cc: Masami Hiramatsu <mhiramat@kernel.org>
> Cc: Namhyung Kim <namhyung@kernel.org>
> Link: https://lore.kernel.org/lkml/
> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> ---
>  tools/perf/util/probe-event.c | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
> index 2d056f02ae408a64..c7bfeab610a3679a 100644
> --- a/tools/perf/util/probe-event.c
> +++ b/tools/perf/util/probe-event.c
> @@ -961,8 +961,9 @@ static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
>  	debuginfo__delete(dinfo);
>  
>  	if (ntevs == 0)	{	/* No error but failed to find probe point. */
> -		pr_warning("Probe point '%s' not found.\n",
> -			   synthesize_perf_probe_point(&pev->point));
> +		char *probe_point = synthesize_perf_probe_point(&pev->point);
> +		pr_warning("Probe point '%s' not found.\n", probe_point);
> +		free(probe_point);
>  		return -ENODEV;
>  	} else if (ntevs < 0) {
>  		/* Error path : ntevs < 0 */
> -- 
> 2.37.1
>
  
Arnaldo Carvalho de Melo Aug. 7, 2023, 7:40 p.m. UTC | #2
Em Sat, Aug 05, 2023 at 11:01:48PM +0900, Masami Hiramatsu escreveu:
> On Fri, 4 Aug 2023 13:22:44 -0300
> Arnaldo Carvalho de Melo <acme@kernel.org> wrote:
> 
> > Building perf with EXTRA_CFLAGS="-fsanitize=address" a leak is detect
> > when trying to add a probe to a non-existent function:
> > 
> >   # perf probe -x ~/bin/perf dso__neW
> >   Probe point 'dso__neW' not found.
> >     Error: Failed to add events.
> > 
> >   =================================================================
> >   ==296634==ERROR: LeakSanitizer: detected memory leaks
> > 
> >   Direct leak of 128 byte(s) in 1 object(s) allocated from:
> >       #0 0x7f67642ba097 in calloc (/lib64/libasan.so.8+0xba097)
> >       #1 0x7f67641a76f1 in allocate_cfi (/lib64/libdw.so.1+0x3f6f1)
> > 
> >   Direct leak of 65 byte(s) in 1 object(s) allocated from:
> >       #0 0x7f67642b95b5 in __interceptor_realloc.part.0 (/lib64/libasan.so.8+0xb95b5)
> >       #1 0x6cac75 in strbuf_grow util/strbuf.c:64
> >       #2 0x6ca934 in strbuf_init util/strbuf.c:25
> >       #3 0x9337d2 in synthesize_perf_probe_point util/probe-event.c:2018
> >       #4 0x92be51 in try_to_find_probe_trace_events util/probe-event.c:964
> >       #5 0x93d5c6 in convert_to_probe_trace_events util/probe-event.c:3512
> >       #6 0x93d6d5 in convert_perf_probe_events util/probe-event.c:3529
> >       #7 0x56f37f in perf_add_probe_events /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:354
> >       #8 0x572fbc in __cmd_probe /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:738
> >       #9 0x5730f2 in cmd_probe /var/home/acme/git/perf-tools-next/tools/perf/builtin-probe.c:766
> >       #10 0x635d81 in run_builtin /var/home/acme/git/perf-tools-next/tools/perf/perf.c:323
> >       #11 0x6362c1 in handle_internal_command /var/home/acme/git/perf-tools-next/tools/perf/perf.c:377
> >       #12 0x63667a in run_argv /var/home/acme/git/perf-tools-next/tools/perf/perf.c:421
> >       #13 0x636b8d in main /var/home/acme/git/perf-tools-next/tools/perf/perf.c:537
> >       #14 0x7f676302950f in __libc_start_call_main (/lib64/libc.so.6+0x2950f)
> > 
> >   SUMMARY: AddressSanitizer: 193 byte(s) leaked in 2 allocation(s).
> >   #
> > 
> > synthesize_perf_probe_point() returns a "detached" strbuf, i.e. a
> > malloc'ed string that needs to be free'd.
> > 
> > An audit will be performed to find other such cases.
> 
> Thanks for finding it!
> 
> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>

Thanks, added the tag.

- Arnaldo
 
> > 
> > Cc: Adrian Hunter <adrian.hunter@intel.com>
> > Cc: Ian Rogers <irogers@google.com>
> > Cc: Jiri Olsa <jolsa@kernel.org>
> > Cc: Masami Hiramatsu <mhiramat@kernel.org>
> > Cc: Namhyung Kim <namhyung@kernel.org>
> > Link: https://lore.kernel.org/lkml/
> > Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
> > ---
> >  tools/perf/util/probe-event.c | 5 +++--
> >  1 file changed, 3 insertions(+), 2 deletions(-)
> > 
> > diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
> > index 2d056f02ae408a64..c7bfeab610a3679a 100644
> > --- a/tools/perf/util/probe-event.c
> > +++ b/tools/perf/util/probe-event.c
> > @@ -961,8 +961,9 @@ static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
> >  	debuginfo__delete(dinfo);
> >  
> >  	if (ntevs == 0)	{	/* No error but failed to find probe point. */
> > -		pr_warning("Probe point '%s' not found.\n",
> > -			   synthesize_perf_probe_point(&pev->point));
> > +		char *probe_point = synthesize_perf_probe_point(&pev->point);
> > +		pr_warning("Probe point '%s' not found.\n", probe_point);
> > +		free(probe_point);
> >  		return -ENODEV;
> >  	} else if (ntevs < 0) {
> >  		/* Error path : ntevs < 0 */
> > -- 
> > 2.37.1
> > 
> 
> 
> -- 
> Masami Hiramatsu (Google) <mhiramat@kernel.org>
  

Patch

diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index 2d056f02ae408a64..c7bfeab610a3679a 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -961,8 +961,9 @@  static int try_to_find_probe_trace_events(struct perf_probe_event *pev,
 	debuginfo__delete(dinfo);
 
 	if (ntevs == 0)	{	/* No error but failed to find probe point. */
-		pr_warning("Probe point '%s' not found.\n",
-			   synthesize_perf_probe_point(&pev->point));
+		char *probe_point = synthesize_perf_probe_point(&pev->point);
+		pr_warning("Probe point '%s' not found.\n", probe_point);
+		free(probe_point);
 		return -ENODEV;
 	} else if (ntevs < 0) {
 		/* Error path : ntevs < 0 */