perf tools: Use zfree() where applicable

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>
This commit is contained in:
Arnaldo Carvalho de Melo
2019-07-04 12:06:20 -03:00
parent 7f7c536f23
commit d8f9da2404
33 changed files with 100 additions and 99 deletions
+2 -1
View File
@@ -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"
@@ -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();
+2 -1
View File
@@ -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"
@@ -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();
+2 -2
View File
@@ -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"
@@ -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;
@@ -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 {
+2 -2
View File
@@ -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[] = {
@@ -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);
+3 -2
View File
@@ -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"
@@ -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;
}
+2 -1
View File
@@ -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)
{
@@ -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;
+2 -1
View File
@@ -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"
@@ -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;
+2 -1
View File
@@ -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
@@ -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;
+3 -3
View File
@@ -1,6 +1,5 @@
// 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"
@@ -8,6 +7,7 @@
#include "config.h"
#include "time-utils.h"
#include <linux/time64.h>
#include <linux/zalloc.h>
static u64 context_len = 10 * NSEC_PER_MSEC;
@@ -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)
+2 -2
View File
@@ -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
@@ -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;
}
+1 -2
View File
@@ -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);
}
+2 -3
View File
@@ -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);
}
@@ -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)
+1 -1
View File
@@ -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;
+1 -1
View File
@@ -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);
}
+2 -3
View File
@@ -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)
@@ -2569,7 +2568,7 @@ err_free_etm:
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);
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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);
}
+4 -4
View File
@@ -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);
}
@@ -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);
}
/*
+1 -1
View File
@@ -856,7 +856,7 @@ free_threads:
free(synthesize_threads);
free_dirent:
for (i = 0; i < n; i++)
free(dirent[i]);
zfree(&dirent[i]);
free(dirent);
return err;
+3 -3
View File
@@ -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;
}
@@ -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;
}

Some files were not shown because too many files have changed in this diff Show More