Commit dc67c783 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf jit: Fix a few memory leaks

As reported by leak sanitizer.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Brian Robbins <brianrob@linux.microsoft.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Yuan Can <yuancan@huawei.com>
Link: https://lore.kernel.org/r/20230403203545.1872196-1-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 3ad45105
...@@ -87,6 +87,12 @@ buffer_ext_init(struct buffer_ext *be) ...@@ -87,6 +87,12 @@ buffer_ext_init(struct buffer_ext *be)
be->max_sz = 0; be->max_sz = 0;
} }
static void
buffer_ext_exit(struct buffer_ext *be)
{
free(be->data);
}
static inline size_t static inline size_t
buffer_ext_size(struct buffer_ext *be) buffer_ext_size(struct buffer_ext *be)
{ {
...@@ -487,28 +493,28 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries ...@@ -487,28 +493,28 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
Elf_Scn *scn; Elf_Scn *scn;
Elf_Shdr *shdr; Elf_Shdr *shdr;
struct buffer_ext dl, di, da; struct buffer_ext dl, di, da;
int ret; int ret = -1;
buffer_ext_init(&dl); buffer_ext_init(&dl);
buffer_ext_init(&di); buffer_ext_init(&di);
buffer_ext_init(&da); buffer_ext_init(&da);
ret = jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di); if (jit_process_debug_info(code_addr, debug, nr_debug_entries, &dl, &da, &di))
if (ret) goto out;
return -1;
/* /*
* setup .debug_line section * setup .debug_line section
*/ */
scn = elf_newscn(e); scn = elf_newscn(e);
if (!scn) { if (!scn) {
warnx("cannot create section"); warnx("cannot create section");
return -1; goto out;
} }
d = elf_newdata(scn); d = elf_newdata(scn);
if (!d) { if (!d) {
warnx("cannot get new data"); warnx("cannot get new data");
return -1; goto out;
} }
d->d_align = 1; d->d_align = 1;
...@@ -521,7 +527,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries ...@@ -521,7 +527,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
shdr = elf_getshdr(scn); shdr = elf_getshdr(scn);
if (!shdr) { if (!shdr) {
warnx("cannot get section header"); warnx("cannot get section header");
return -1; goto out;
} }
shdr->sh_name = 52; /* .debug_line */ shdr->sh_name = 52; /* .debug_line */
...@@ -536,13 +542,13 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries ...@@ -536,13 +542,13 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
scn = elf_newscn(e); scn = elf_newscn(e);
if (!scn) { if (!scn) {
warnx("cannot create section"); warnx("cannot create section");
return -1; goto out;
} }
d = elf_newdata(scn); d = elf_newdata(scn);
if (!d) { if (!d) {
warnx("cannot get new data"); warnx("cannot get new data");
return -1; goto out;
} }
d->d_align = 1; d->d_align = 1;
...@@ -555,7 +561,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries ...@@ -555,7 +561,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
shdr = elf_getshdr(scn); shdr = elf_getshdr(scn);
if (!shdr) { if (!shdr) {
warnx("cannot get section header"); warnx("cannot get section header");
return -1; goto out;
} }
shdr->sh_name = 64; /* .debug_info */ shdr->sh_name = 64; /* .debug_info */
...@@ -570,13 +576,13 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries ...@@ -570,13 +576,13 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
scn = elf_newscn(e); scn = elf_newscn(e);
if (!scn) { if (!scn) {
warnx("cannot create section"); warnx("cannot create section");
return -1; goto out;
} }
d = elf_newdata(scn); d = elf_newdata(scn);
if (!d) { if (!d) {
warnx("cannot get new data"); warnx("cannot get new data");
return -1; goto out;
} }
d->d_align = 1; d->d_align = 1;
...@@ -589,7 +595,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries ...@@ -589,7 +595,7 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
shdr = elf_getshdr(scn); shdr = elf_getshdr(scn);
if (!shdr) { if (!shdr) {
warnx("cannot get section header"); warnx("cannot get section header");
return -1; goto out;
} }
shdr->sh_name = 76; /* .debug_info */ shdr->sh_name = 76; /* .debug_info */
...@@ -601,9 +607,14 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries ...@@ -601,9 +607,14 @@ jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_entries
/* /*
* now we update the ELF image with all the sections * now we update the ELF image with all the sections
*/ */
if (elf_update(e, ELF_C_WRITE) < 0) { if (elf_update(e, ELF_C_WRITE) < 0)
warnx("elf_update debug failed"); warnx("elf_update debug failed");
return -1; else
} ret = 0;
return 0;
out:
buffer_ext_exit(&dl);
buffer_ext_exit(&di);
buffer_ext_exit(&da);
return ret;
} }
...@@ -235,9 +235,11 @@ jit_open(struct jit_buf_desc *jd, const char *name) ...@@ -235,9 +235,11 @@ jit_open(struct jit_buf_desc *jd, const char *name)
*/ */
strcpy(jd->dir, name); strcpy(jd->dir, name);
dirname(jd->dir); dirname(jd->dir);
free(buf);
return 0; return 0;
error: error:
free(buf);
funlockfile(jd->in); funlockfile(jd->in);
fclose(jd->in); fclose(jd->in);
return retval; return retval;
...@@ -523,7 +525,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr) ...@@ -523,7 +525,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
ret = perf_event__process_mmap2(tool, event, &sample, jd->machine); ret = perf_event__process_mmap2(tool, event, &sample, jd->machine);
if (ret) if (ret)
return ret; goto out;
ret = jit_inject_event(jd, event); ret = jit_inject_event(jd, event);
/* /*
...@@ -532,6 +534,8 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr) ...@@ -532,6 +534,8 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
if (!ret) if (!ret)
build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine); build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
out:
free(event);
return ret; return ret;
} }
...@@ -874,6 +878,7 @@ jit_process(struct perf_session *session, ...@@ -874,6 +878,7 @@ jit_process(struct perf_session *session,
} }
nsinfo__put(jd.nsi); nsinfo__put(jd.nsi);
free(jd.buf);
return ret; return ret;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment