[tip:perf/core] perf evsel: Don't rely on malloc working for sz 0

tip-bot for Vineet Gupta
2015-01-30T18:32:10Z

Commit-ID: 8d9cbd8f870e1aab00fb0f8a465887877a1d6f82
Gitweb: http://git.kernel.org/tip/8d9cbd8f870e1aab00fb0f8a465887877a1d6f82
Author: Vineet Gupta <Vineet.Gupta1@synopsys.com>
AuthorDate: Tue, 13 Jan 2015 19:13:23 +0530
Committer: Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 28 Jan 2015 12:43:32 -0300

perf evsel: Don't rely on malloc working for sz 0

When running perf on ARC (uClibc based userspace), ran into this issue
------------->8----------------
[ARCLinux]$ ./perf record ls
bin etc perf sys
debug init perf.data tmp
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.001 MB perf.data (~24 samples) ]

[ARCLinux]$ ./perf report
incompatible file format (rerun with -v to learn more)
------------->8----------------

The problem happens in the following call stack when zalloc is called
with size zero

glibc default / uClibc with MALLOC_GLIBC_COMPAT are OK, but not if that
config option is not enabled.

cmd_report
perf_session__new
perf_session__open
perf_session__read_header
read_attr(fd, header, &f_attr)
nr_ids = f_attr.ids.size / sizeof(u64); <-- 0
perf_evsel__alloc_id(vsel, 1, nr_ids)
zalloc(ncpus * nthreads * sizeof(u64)) <-- 0

header.c: read_attr()

(gdb) p *f_attr
$17 = {
attr = {
type = 0,
size = 96,
config = 0,
{
sample_period = 4000,
sample_freq = 4000
},
...
ids = {
offset = 104,
size = 0 <------
}
}

Signed-off-by: Vineet Gupta <vgupta@synopsys.com>
Suggested-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexey Brodkin <Alexey.Brodkin@synopsys.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/1421156604-30603-5-git-send-email-vgupta@synopsys.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
tools/perf/util/evsel.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 1e90c85..1d826d6 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -797,6 +797,9 @@ int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)

int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
{
+ if (ncpus == 0 || nthreads == 0)
+ return 0;
+
if (evsel->system_wide)
nthreads = 1;