Skip to content

Commit

Permalink
perf tools: Use zfree() where applicable
Browse files Browse the repository at this point in the history
In places where the equivalent was already being done, i.e.:

   free(a);
   a = NULL;

And in placs where struct members are being freed so that if we have
some erroneous reference to its struct, then accesses to freed members
will result in segfaults, which we can detect faster than use after free
to areas that may still have something seemingly valid.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Link: https://lkml.kernel.org/n/tip-jatyoofo5boc1bsvoig6bb6i@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Arnaldo Carvalho de Melo committed Jul 9, 2019
1 parent 7f7c536 commit d8f9da2
Show file tree
Hide file tree
Showing 33 changed files with 100 additions and 99 deletions.
3 changes: 2 additions & 1 deletion tools/perf/bench/futex-hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <stdlib.h>
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/zalloc.h>
#include <sys/time.h>

#include "../util/stat.h"
Expand Down Expand Up @@ -214,7 +215,7 @@ int bench_futex_hash(int argc, const char **argv)
&worker[i].futex[nfutexes-1], t);
}

free(worker[i].futex);
zfree(&worker[i].futex);
}

print_summary();
Expand Down
3 changes: 2 additions & 1 deletion tools/perf/bench/futex-lock-pi.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <subcmd/parse-options.h>
#include <linux/compiler.h>
#include <linux/kernel.h>
#include <linux/zalloc.h>
#include <errno.h>
#include "bench.h"
#include "futex.h"
Expand Down Expand Up @@ -217,7 +218,7 @@ int bench_futex_lock_pi(int argc, const char **argv)
worker[i].tid, worker[i].futex, t);

if (multi)
free(worker[i].futex);
zfree(&worker[i].futex);
}

print_summary();
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/builtin-record.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "perf.h"

#include "util/build-id.h"
#include "util/util.h"
#include <subcmd/parse-options.h>
#include "util/parse-events.h"
#include "util/config.h"
Expand Down Expand Up @@ -54,6 +53,7 @@
#include <sys/mman.h>
#include <sys/wait.h>
#include <linux/time64.h>
#include <linux/zalloc.h>

struct switch_output {
bool enabled;
Expand Down Expand Up @@ -1110,7 +1110,7 @@ record__switch_output(struct record *rec, bool at_exit)
rec->switch_output.cur_file = n;
if (rec->switch_output.filenames[n]) {
remove(rec->switch_output.filenames[n]);
free(rec->switch_output.filenames[n]);
zfree(&rec->switch_output.filenames[n]);
}
rec->switch_output.filenames[n] = new_filename;
} else {
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/builtin-stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1586,7 +1586,7 @@ static void runtime_stat_delete(struct perf_stat_config *config)
for (i = 0; i < config->stats_num; i++)
runtime_stat__exit(&config->stats[i]);

free(config->stats);
zfree(&config->stats);
}

static const char * const stat_report_usage[] = {
Expand Down Expand Up @@ -2003,7 +2003,7 @@ int cmd_stat(int argc, const char **argv)
perf_stat__exit_aggr_mode();
perf_evlist__free_stats(evsel_list);
out:
free(stat_config.walltime_run);
zfree(&stat_config.walltime_run);

if (smi_cost && smi_reset)
sysfs__write_int(FREEZE_ON_SMI_PATH, 0);
Expand Down
5 changes: 3 additions & 2 deletions tools/perf/tests/dwarf-unwind.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/compiler.h>
#include <linux/types.h>
#include <linux/zalloc.h>
#include <inttypes.h>
#include <unistd.h>
#include "tests.h"
Expand Down Expand Up @@ -115,8 +116,8 @@ noinline int test_dwarf_unwind__thread(struct thread *thread)
}

out:
free(sample.user_stack.data);
free(sample.user_regs.regs);
zfree(&sample.user_stack.data);
zfree(&sample.user_regs.regs);
return err;
}

Expand Down
3 changes: 2 additions & 1 deletion tools/perf/tests/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "util/expr.h"
#include "tests.h"
#include <stdlib.h>
#include <linux/zalloc.h>

static int test(struct parse_ctx *ctx, const char *e, double val2)
{
Expand Down Expand Up @@ -58,7 +59,7 @@ int test__expr(struct test *t __maybe_unused, int subtest __maybe_unused)
TEST_ASSERT_VAL("find other", other[3] == NULL);

for (i = 0; i < num_other; i++)
free((void *)other[i]);
zfree(&other[i]);
free((void *)other);

return 0;
Expand Down
3 changes: 2 additions & 1 deletion tools/perf/tests/mem2node.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/compiler.h>
#include <linux/bitmap.h>
#include <linux/zalloc.h>
#include "cpumap.h"
#include "mem2node.h"
#include "tests.h"
Expand Down Expand Up @@ -67,7 +68,7 @@ int test__mem2node(struct test *t __maybe_unused, int subtest __maybe_unused)
T("failed: mem2node__node", -1 == mem2node__node(&map, 0x1050));

for (i = 0; i < ARRAY_SIZE(nodes); i++)
free(nodes[i].set);
zfree(&nodes[i].set);

mem2node__exit(&map);
return 0;
Expand Down
3 changes: 2 additions & 1 deletion tools/perf/tests/thread-map.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "tests.h"
#include "thread_map.h"
#include "debug.h"
#include <linux/zalloc.h>

#define NAME (const char *) "perf"
#define NAMEUL (unsigned long) NAME
Expand Down Expand Up @@ -133,7 +134,7 @@ int test__thread_map_remove(struct test *test __maybe_unused, int subtest __mayb
thread_map__remove(threads, 0));

for (i = 0; i < threads->nr; i++)
free(threads->map[i].comm);
zfree(&threads->map[i].comm);

free(threads);
return 0;
Expand Down
6 changes: 3 additions & 3 deletions tools/perf/ui/browsers/res_sample.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// SPDX-License-Identifier: GPL-2.0
/* Display a menu with individual samples to browse with perf script */
#include "util.h"
#include "hist.h"
#include "evsel.h"
#include "hists.h"
#include "sort.h"
#include "config.h"
#include "time-utils.h"
#include <linux/time64.h>
#include <linux/zalloc.h>

static u64 context_len = 10 * NSEC_PER_MSEC;

Expand Down Expand Up @@ -46,14 +46,14 @@ int res_sample_browse(struct res_sample *res_samples, int num_res,
if (asprintf(&names[i], "%s: CPU %d tid %d", tbuf,
res_samples[i].cpu, res_samples[i].tid) < 0) {
while (--i >= 0)
free(names[i]);
zfree(&names[i]);
free(names);
return -1;
}
}
choice = ui__popup_menu(num_res, names);
for (i = 0; i < num_res; i++)
free(names[i]);
zfree(&names[i]);
free(names);

if (choice < 0 || choice >= num_res)
Expand Down
4 changes: 2 additions & 2 deletions tools/perf/ui/browsers/scripts.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: GPL-2.0
#include "../../util/sort.h"
#include "../../util/util.h"
#include "../../util/hist.h"
#include "../../util/debug.h"
#include "../../util/symbol.h"
#include "../browser.h"
#include "../libslang.h"
#include "config.h"
#include <linux/zalloc.h>

#define SCRIPT_NAMELEN 128
#define SCRIPT_MAX_NO 64
Expand Down Expand Up @@ -142,7 +142,7 @@ static int list_scripts(char *script_name, bool *custom,
out:
free(buf);
for (i = 0; i < max_std; i++)
free(paths[i]);
zfree(&paths[i]);
return ret;
}

Expand Down
3 changes: 1 addition & 2 deletions tools/perf/util/annotate.c
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,7 @@ void disasm_line__free(struct disasm_line *dl)
dl->ins.ops->free(&dl->ops);
else
ins__delete(&dl->ops);
free((void *)dl->ins.name);
dl->ins.name = NULL;
zfree(&dl->ins.name);
annotation_line__delete(&dl->al);
}

Expand Down
5 changes: 2 additions & 3 deletions tools/perf/util/auxtrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ void auxtrace_cache__free(struct auxtrace_cache *c)
return;

auxtrace_cache__drop(c);
free(c->hashtable);
zfree(&c->hashtable);
free(c);
}

Expand Down Expand Up @@ -1459,12 +1459,11 @@ void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)

static void addr_filter__free_str(struct addr_filter *filt)
{
free(filt->str);
zfree(&filt->str);
filt->action = NULL;
filt->sym_from = NULL;
filt->sym_to = NULL;
filt->filename = NULL;
filt->str = NULL;
}

static struct addr_filter *addr_filter__new(void)
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static struct cgroup *cgroup__new(const char *name)
return cgroup;

out_free_name:
free(cgroup->name);
zfree(&cgroup->name);
out_err:
free(cgroup);
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/cputopo.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ void numa_topology__delete(struct numa_topology *tp)
u32 i;

for (i = 0; i < tp->nr; i++)
free(tp->nodes[i].cpus);
zfree(&tp->nodes[i].cpus);

free(tp);
}
5 changes: 2 additions & 3 deletions tools/perf/util/cs-etm.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,7 @@ static void cs_etm__free_traceid_queues(struct cs_etm_queue *etmq)
etmq->traceid_queues_list = NULL;

/* finally free the traceid_queues array */
free(etmq->traceid_queues);
etmq->traceid_queues = NULL;
zfree(&etmq->traceid_queues);
}

static void cs_etm__free_queue(void *priv)
Expand Down Expand Up @@ -2569,7 +2568,7 @@ int cs_etm__process_auxtrace_info(union perf_event *event,
err_free_metadata:
/* No need to check @metadata[j], free(NULL) is supported */
for (j = 0; j < num_cpu; j++)
free(metadata[j]);
zfree(&metadata[j]);
zfree(&metadata);
err_free_traceid_list:
intlist__delete(traceid_list);
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/data-convert-bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,7 @@ static void free_streams(struct ctf_writer *cw)
for (cpu = 0; cpu < cw->stream_cnt; cpu++)
ctf_stream__delete(cw->stream[cpu]);

free(cw->stream);
zfree(&cw->stream);
}

static int ctf_writer__setup_env(struct ctf_writer *cw,
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void close_dir(struct perf_data_file *files, int nr)
{
while (--nr >= 1) {
close(files[nr].fd);
free(files[nr].path);
zfree(&files[nr].path);
}
free(files);
}
Expand Down
8 changes: 4 additions & 4 deletions tools/perf/util/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void perf_env__exit(struct perf_env *env)
zfree(&env->caches);

for (i = 0; i < env->nr_memory_nodes; i++)
free(env->memory_nodes[i].set);
zfree(&env->memory_nodes[i].set);
zfree(&env->memory_nodes);
}

Expand Down Expand Up @@ -287,9 +287,9 @@ int perf_env__nr_cpus_avail(struct perf_env *env)

void cpu_cache_level__free(struct cpu_cache_level *cache)
{
free(cache->type);
free(cache->map);
free(cache->size);
zfree(&cache->type);
zfree(&cache->map);
zfree(&cache->size);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion tools/perf/util/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ int perf_event__synthesize_threads(struct perf_tool *tool,
free(synthesize_threads);
free_dirent:
for (i = 0; i < n; i++)
free(dirent[i]);
zfree(&dirent[i]);
free(dirent);

return err;
Expand Down
6 changes: 3 additions & 3 deletions tools/perf/util/header.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 lev

scnprintf(file, PATH_MAX, "%s/size", path);
if (sysfs__read_str(file, &cache->size, &len)) {
free(cache->type);
zfree(&cache->type);
return -1;
}

Expand All @@ -1061,8 +1061,8 @@ static int cpu_cache_level__read(struct cpu_cache_level *cache, u32 cpu, u16 lev

scnprintf(file, PATH_MAX, "%s/shared_cpu_list", path);
if (sysfs__read_str(file, &cache->map, &len)) {
free(cache->map);
free(cache->type);
zfree(&cache->map);
zfree(&cache->type);
return -1;
}

Expand Down
14 changes: 7 additions & 7 deletions tools/perf/util/hist.c
Original file line number Diff line number Diff line change
Expand Up @@ -472,24 +472,24 @@ static int hist_entry__init(struct hist_entry *he,
return 0;

err_srcline:
free(he->srcline);
zfree(&he->srcline);

err_rawdata:
free(he->raw_data);
zfree(&he->raw_data);

err_infos:
if (he->branch_info) {
map__put(he->branch_info->from.map);
map__put(he->branch_info->to.map);
free(he->branch_info);
zfree(&he->branch_info);
}
if (he->mem_info) {
map__put(he->mem_info->iaddr.map);
map__put(he->mem_info->daddr.map);
}
err:
map__zput(he->ms.map);
free(he->stat_acc);
zfree(&he->stat_acc);
return -ENOMEM;
}

Expand Down Expand Up @@ -1254,10 +1254,10 @@ void hist_entry__delete(struct hist_entry *he)
zfree(&he->stat_acc);
free_srcline(he->srcline);
if (he->srcfile && he->srcfile[0])
free(he->srcfile);
zfree(&he->srcfile);
free_callchain(he->callchain);
free(he->trace_output);
free(he->raw_data);
zfree(&he->trace_output);
zfree(&he->raw_data);
ops->free(he);
}

Expand Down
Loading

0 comments on commit d8f9da2

Please sign in to comment.