Linux Perf Users
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH] perf util: asprintf helper for leak sanitizer
Date: Tue, 13 Jun 2023 12:16:38 -0700	[thread overview]
Message-ID: <20230613191639.1547925-1-irogers@google.com> (raw)

asprintf is a source of memory leaks but produces bad stack traces on
my Debian linux. This patch adds a simple asprintf implementation to
util.c that works around it.

Before output:
```
==1541752==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10 byte(s) in 1 object(s) allocated from:
    #0 0x7f90c76b89cf in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:69
    #1 0x7f90c649d2c7 in __vasprintf_internal libio/vasprintf.c:71
    #2 0x55ad9b79afbf  (/tmp/perf/perf+0x850fbf)

SUMMARY: AddressSanitizer: 10 byte(s) leaked in 1 allocation(s).
```

After output:
```
==1545918==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10 byte(s) in 1 object(s) allocated from:
    #0 0x7f2755a7077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439
    #1 0x564986a8df31 in asprintf util/util.c:566
    #2 0x5649869b5901 in metricgroup__lookup_default_metricgroup util/metricgroup.c:1520
    #3 0x5649869b5e57 in metricgroup__lookup_create util/metricgroup.c:1579
    #4 0x5649869b6ddc in parse_groups util/metricgroup.c:1698
    #5 0x5649869b7714 in metricgroup__parse_groups util/metricgroup.c:1771
    #6 0x5649867da9d5 in add_default_attributes tools/perf/builtin-stat.c:2164
    #7 0x5649867ddbfb in cmd_stat tools/perf/builtin-stat.c:2707
    #8 0x5649868fa5a2 in run_builtin tools/perf/perf.c:323
    #9 0x5649868fab13 in handle_internal_command tools/perf/perf.c:377
    #10 0x5649868faedb in run_argv tools/perf/perf.c:421
    #11 0x5649868fb443 in main tools/perf/perf.c:537
    #12 0x7f2754846189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: 10 byte(s) leaked in 1 allocation(s).
```

RFC: is this useful for others? Should we have a build flag for it?
---
 tools/perf/util/util.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index c1fd9ba6d697..57eb528c5fed 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -552,3 +552,22 @@ int sched_getcpu(void)
 	return -1;
 }
 #endif
+
+int asprintf(char **restrict strp, const char *restrict fmt, ...)
+{
+	char buf[1024];
+	va_list ap;
+	int size;
+	char *result;
+
+	va_start(ap, fmt);
+	size = vsnprintf(buf, sizeof(buf), fmt, ap);
+	if (size < (int)sizeof(buf))
+		result = strdup(buf);
+	else
+		size = vasprintf(&result, fmt, ap);
+
+	*strp = result;
+	va_end(ap);
+	return size;
+}
-- 
2.41.0.162.gfafddb0af9-goog


             reply	other threads:[~2023-06-13 19:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-13 19:16 Ian Rogers [this message]
2023-06-13 19:50 ` [RFC PATCH] perf util: asprintf helper for leak sanitizer Arnaldo Carvalho de Melo
2023-06-13 19:54   ` Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230613191639.1547925-1-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox