Skip to content

Commit c58c49a

Browse files
WangNan0acmel
authored andcommitted
perf tools: Fix crash in build_id_cache__kallsyms_path()
build_id_cache__kallsyms_path() accepts a string buffer but also allocs a buffer using asnprintf. Unfortunately, the its only user passes it a stack-allocated buffer. Freeing it causes crashes like this: $ perf script *** Error in `/home/wangnan/perf': free(): invalid pointer: 0x00007fffffff9630 *** ======= Backtrace: ========= lib64/libc.so.6(+0x6eeef)[0x7ffff5dbaeef] lib64/libc.so.6(+0x78cae)[0x7ffff5dc4cae] lib64/libc.so.6(+0x79987)[0x7ffff5dc5987] /home/w00229757/perf(build_id_cache__kallsyms_path+0x6b)[0x49681b] /home/w00229757/perf[0x4bdd40] /home/w00229757/perf(dso__load+0xa3a)[0x4c048a] /home/w00229757/perf(map__load+0x6f)[0x4d561f] /home/w00229757/perf(thread__find_addr_map+0x235)[0x49e935] /home/w00229757/perf(machine__resolve+0x7d)[0x49ec6d] /home/w00229757/perf[0x4555a8] /home/w00229757/perf[0x4d9507] /home/w00229757/perf[0x4d9e80] /home/w00229757/perf(ordered_events__flush+0x354)[0x4dd444] /home/w00229757/perf(perf_session__process_events+0x3d0)[0x4dc140] /home/w00229757/perf(cmd_script+0x12b0)[0x4592e0] /home/w00229757/perf[0x4911f1] /home/w00229757/perf(main+0x68f)[0x4352ef] /lib64/libc.so.6(__libc_start_main+0xf5)[0x7ffff5d6dbd5] /home/w00229757/perf[0x435415] ======= Memory map: ======== This patch simplifies build_id_cache__kallsyms_path(), not even considering allocating a string buffer, so never frees anything. Its caller should manage memory allocation. Signed-off-by: Wang Nan <[email protected]> Cc: Masami Hiramatsu <[email protected]> Cc: Zefan Li <[email protected]> Cc: [email protected] Fixes: 0141226 ("perf buildid-cache: Use path/to/bin/buildid/elf instead of path/to/bin/buildid") Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent edb13ed commit c58c49a

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

tools/perf/util/build-id.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,17 @@ static int asnprintf(char **strp, size_t size, const char *fmt, ...)
147147
char *build_id_cache__kallsyms_path(const char *sbuild_id, char *bf,
148148
size_t size)
149149
{
150-
bool is_alloc = !!bf;
151150
bool retry_old = true;
152151

153-
asnprintf(&bf, size, "%s/%s/%s/kallsyms",
154-
buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
152+
snprintf(bf, size, "%s/%s/%s/kallsyms",
153+
buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
155154
retry:
156155
if (!access(bf, F_OK))
157156
return bf;
158-
if (is_alloc)
159-
free(bf);
160157
if (retry_old) {
161158
/* Try old style kallsyms cache */
162-
asnprintf(&bf, size, "%s/%s/%s",
163-
buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
159+
snprintf(bf, size, "%s/%s/%s",
160+
buildid_dir, DSO__NAME_KALLSYMS, sbuild_id);
164161
retry_old = false;
165162
goto retry;
166163
}

0 commit comments

Comments
 (0)