clang-tools support in tools
Subsystems (4)
Ian Rogers
reviewerv3 cover clang-tools support in tools
Allow the clang-tools scripts to work with builds in tools such as tools/perf and tools/lib/perf. An example use looks like:
```
$ cd tools/perf
$ make CC=clang CXX=clang++
$ ../../scripts/clang-tools/gen_compile_commands.py
$ ../../scripts/clang-tools/run-clang-tools.py clang-tidy compile_commands.json -checks=-*,readability-named-parameter
Skipping non-C file: 'tools/perf/bench/mem-memcpy-x86-64-asm.S'
Skipping non-C file: 'tools/perf/bench/mem-memset-x86-64-asm.S'
Skipping non-C file: 'tools/perf/arch/x86/tests/regs_load.S'
8 warnings generated.
Suppressed 8 warnings (8 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
2 warnings generated.
4 warnings generated.
Suppressed 4 warnings (4 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
2 warnings generated.
4 warnings generated.
Suppressed 4 warnings (4 in non-user code).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.
3 warnings generated.
tools/perf/util/parse-events-flex.c:546:27: warning: all parameters should be named in a function [readability-named-parameter]
void *yyalloc ( yy_size_t , yyscan_t yyscanner );
^
/*size*/
...
```Fix a number of the more serious low-hanging issues in perf found by clang-tidy.
This support isn't complete, in particular it doesn't support output directories properly and so fails for tools/lib/bpf, tools/bpf/bpftool and if an output directory is used.
v3. Add Nick Desaulniers reviewed-by to patch 3. For Namhyung, drop
"perf hisi-ptt: Fix potential memory leak", split lock change out of "perf svghelper: Avoid memory leak" and address comments in "perf header: Fix various error path memory leaks".
v2: Address comments by Nick Desaulniers in patch 3, and add their
Reviewed-by to patches 1 and 2.
Ian Rogers (18):
gen_compile_commands: Allow the line prefix to still be cmd_
gen_compile_commands: Sort output compile commands by file name
run-clang-tools: Add pass through checks and header-filter arguments perf bench uprobe: Fix potential use of memory after free perf buildid-cache: Fix use of uninitialized value
perf env: Remove unnecessary NULL tests
perf jitdump: Avoid memory leak
perf mem-events: Avoid uninitialized read
perf dlfilter: Be defensive against potential NULL dereference perf hists browser: Reorder variables to reduce padding perf hists browser: Avoid potential NULL dereference
perf svghelper: Avoid memory leak
perf lock: Fix a memory leak on an error path
perf parse-events: Fix unlikely memory leak when cloning terms tools api: Avoid potential double free
perf trace-event-info: Avoid passing NULL value to closedir perf header: Fix various error path memory leaks
perf bpf_counter: Fix a few memory leaks
scripts/clang-tools/gen_compile_commands.py | 8 +-- scripts/clang-tools/run-clang-tools.py | 32 ++++++++--- tools/lib/api/io.h | 1 + tools/perf/bench/uprobe.c | 1 + tools/perf/builtin-buildid-cache.c | 6 ++- tools/perf/builtin-lock.c | 1 + tools/perf/ui/browsers/hists.c | 6 +-- tools/perf/util/bpf_counter.c | 5 +- tools/perf/util/dlfilter.c | 4 +- tools/perf/util/env.c | 6 +-- tools/perf/util/header.c | 60 ++++++++++++--------- tools/perf/util/jitdump.c | 1 + tools/perf/util/mem-events.c | 3 +- tools/perf/util/parse-events.c | 4 +- tools/perf/util/svghelper.c | 5 +- tools/perf/util/trace-event-info.c | 3 +- 16 files changed, 94 insertions(+), 52 deletions(-)
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 1/18 gen_compile_commands: Allow the line prefix to still be cmd_
Builds in tools still use the cmd_ prefix in .cmd files, so don't require the saved part. Name the groups in the line pattern match so that changing the regular expression is more robust and works with the addition of a new match group.
--- scripts/clang-tools/gen_compile_commands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
scripts/clang-tools/gen_compile_commands.py +3 -3
@@ -19,7 +19,7 @@ _DEFAULT_OUTPUT = 'compile_commands.json' _DEFAULT_LOG_LEVEL = 'WARNING' _FILENAME_PATTERN = r'^\..*\.cmd$' -_LINE_PATTERN = r'^savedcmd_[^ ]*\.o := (.* )([^ ]*\.[cS]) *(;|$)' +_LINE_PATTERN = r'^(saved)?cmd_[^ ]*\.o := (?P<command_prefix>.* )(?P<file_path>[^ ]*\.[cS]) *(;|$)' _VALID_LOG_LEVELS = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] # The tools/ directory adopts a different build system, and produces .cmd # files in a different format. Do not support it.
@@ -213,8 +213,8 @@ def main(): result = line_matcher.match(f.readline()) if result: try: - entry = process_line(directory, result.group(1), - result.group(2)) + entry = process_line(directory, result.group('command_prefix'), + result.group('file_path')) compile_commands.append(entry) except ValueError as err: logging.info('Could not add line from %s: %s',
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 2/18 gen_compile_commands: Sort output compile commands by file name
In reply to Ian RogersMake the output more stable and deterministic.
--- scripts/clang-tools/gen_compile_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/clang-tools/gen_compile_commands.py +1 -1
@@ -221,7 +221,7 @@ def main(): cmdfile, err) with open(output, 'wt') as f: - json.dump(compile_commands, f, indent=2, sort_keys=True) + json.dump(sorted(compile_commands, key=lambda x: x["file"]), f, indent=2, sort_keys=True) if __name__ == '__main__':
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 3/18 run-clang-tools: Add pass through checks and and header-filter arguments
In reply to Ian RogersAdd a -checks argument to allow the checks passed to the clang-tool to be set on the command line.
Add a pass through -header-filter option.
Don't run analysis on non-C or CPP files.
--- scripts/clang-tools/run-clang-tools.py | 32 ++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-)
scripts/clang-tools/run-clang-tools.py +25 -7
@@ -33,6 +33,11 @@ def parse_arguments(): path_help = "Path to the compilation database to parse" parser.add_argument("path", type=str, help=path_help) + checks_help = "Checks to pass to the analysis" + parser.add_argument("-checks", type=str, default=None, help=checks_help) + header_filter_help = "Pass the -header-filter value to the tool" + parser.add_argument("-header-filter", type=str, default=None, help=header_filter_help) + return parser.parse_args()
@@ -45,14 +50,27 @@ def init(l, a): def run_analysis(entry): # Disable all checks, then re-enable the ones we want - checks = [] - checks.append("-checks=-*") - if args.type == "clang-tidy": - checks.append("linuxkernel-*") + global args + checks = None + if args.checks: + checks = args.checks.split(',') else: - checks.append("clang-analyzer-*") - checks.append("-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling") - p = subprocess.run(["clang-tidy", "-p", args.path, ",".join(checks), entry["file"]], + checks = ["-*"] + if args.type == "clang-tidy": + checks.append("linuxkernel-*") + else: + checks.append("clang-analyzer-*") + checks.append("-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling") + file = entry["file"] + if not file.endswith(".c") and not file.endswith(".cpp"): + with lock: + print(f"Skipping non-C file: '{file}'", file=sys.stderr) + return + pargs = ["clang-tidy", "-p", args.path, "-checks=" + ",".join(checks)] + if args.header_filter: + pargs.append("-header-filter=" + args.header_filter) + pargs.append(file) + p = subprocess.run(pargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=entry["directory"])
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 3/18 run-clang-tools: Add pass through checks and header-filter arguments
In reply to Ian RogersAdd a -checks argument to allow the checks passed to the clang-tool to be set on the command line.
Add a pass through -header-filter option.
Don't run analysis on non-C or CPP files.
--- scripts/clang-tools/run-clang-tools.py | 32 ++++++++++++++++++++------ 1 file changed, 25 insertions(+), 7 deletions(-)
scripts/clang-tools/run-clang-tools.py +25 -7
@@ -33,6 +33,11 @@ def parse_arguments(): path_help = "Path to the compilation database to parse" parser.add_argument("path", type=str, help=path_help) + checks_help = "Checks to pass to the analysis" + parser.add_argument("-checks", type=str, default=None, help=checks_help) + header_filter_help = "Pass the -header-filter value to the tool" + parser.add_argument("-header-filter", type=str, default=None, help=header_filter_help) + return parser.parse_args()
@@ -45,14 +50,27 @@ def init(l, a): def run_analysis(entry): # Disable all checks, then re-enable the ones we want - checks = [] - checks.append("-checks=-*") - if args.type == "clang-tidy": - checks.append("linuxkernel-*") + global args + checks = None + if args.checks: + checks = args.checks.split(',') else: - checks.append("clang-analyzer-*") - checks.append("-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling") - p = subprocess.run(["clang-tidy", "-p", args.path, ",".join(checks), entry["file"]], + checks = ["-*"] + if args.type == "clang-tidy": + checks.append("linuxkernel-*") + else: + checks.append("clang-analyzer-*") + checks.append("-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling") + file = entry["file"] + if not file.endswith(".c") and not file.endswith(".cpp"): + with lock: + print(f"Skipping non-C file: '{file}'", file=sys.stderr) + return + pargs = ["clang-tidy", "-p", args.path, "-checks=" + ",".join(checks)] + if args.header_filter: + pargs.append("-header-filter=" + args.header_filter) + pargs.append(file) + p = subprocess.run(pargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=entry["directory"])
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 4/18 perf bench uprobe: Fix potential use of memory after free
In reply to Ian RogersFound by clang-tidy:
```
bench/uprobe.c:98:3: warning: Use of memory after it is freed [clang-analyzer-unix.Malloc]
bench_uprobe_bpf__destroy(skel);
```--- tools/perf/bench/uprobe.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/bench/uprobe.c +1 -0
@@ -89,6 +89,7 @@ static int bench_uprobe__setup_bpf_skel(enum bench_uprobe bench)return err; cleanup:bench_uprobe_bpf__destroy (skel); + skel =NULL ;return err; }
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 5/18 perf buildid-cache: Fix use of uninitialized value
In reply to Ian RogersThe buildid filename is first determined and then from this the buildid read. If getting the filename fails then the buildid will be used for a later memcmp uninitialized. Detected by clang-tidy.
--- tools/perf/builtin-buildid-cache.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/builtin-buildid-cache.c +4 -2
@@ -277,8 +277,10 @@ static bool dso__missing_buildid_cache(struct dso *dso, int parm __maybe_unused)char filename[PATH_MAX ];struct build_id bid; -if (dso__build_id_filename (dso, filename,sizeof (filename), false) && -filename__read_build_id (filename, &bid) ==-1 ) { +if (!dso__build_id_filename (dso, filename,sizeof (filename), false)) +return true; + +if (filename__read_build_id (filename, &bid) ==-1 ) {if (errno ==ENOENT )return false;
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 6/18 perf env: Remove unnecessary NULL tests
In reply to Ian Rogersclang-tidy was warning:
```
util/env.c:334:23: warning: Access to field 'nr_pmu_mappings' results in a dereference of a null pointer (loaded from variable 'env') [clang-analyzer-core.NullDereference]
env->nr_pmu_mappings = pmu_num;
```As functions are called potentially when !env was true. This condition could never be true as it would produce a segv, so remove the unnecessary NULL tests and silence clang-tidy.
--- tools/perf/util/env.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/perf/util/env.c +3 -3
@@ -457,7 +457,7 @@ const char *perf_env__cpuid(struct perf_env *env) {int status; -if (!env || !env->cpuid) {/* Assume local operation */ +if (!env->cpuid) {/* Assume local operation */ status =perf_env__read_cpuid (env);if (status)return NULL ;
@@ -470,7 +470,7 @@ int perf_env__nr_pmu_mappings(struct perf_env *env) {int status; -if (!env || !env->nr_pmu_mappings) {/* Assume local operation */ +if (!env->nr_pmu_mappings) {/* Assume local operation */ status =perf_env__read_pmu_mappings (env);if (status)return 0 ;
@@ -483,7 +483,7 @@ const char *perf_env__pmu_mappings(struct perf_env *env) {int status; -if (!env || !env->pmu_mappings) {/* Assume local operation */ +if (!env->pmu_mappings) {/* Assume local operation */ status =perf_env__read_pmu_mappings (env);if (status)return NULL ;
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 7/18 perf jitdump: Avoid memory leak
In reply to Ian Rogersjit_repipe_unwinding_info is called in a loop by jit_process_dump, avoid leaking unwinding_data by free-ing before overwriting. Error detected by clang-tidy.
--- tools/perf/util/jitdump.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/util/jitdump.c +1 -0
@@ -675,6 +675,7 @@ jit_repipe_unwinding_info(struct jit_buf_desc *jd, union jr_entry *jr) jd->eh_frame_hdr_size = jr->unwinding.eh_frame_hdr_size; jd->unwinding_size = jr->unwinding.unwinding_size; jd->unwinding_mapped_size = jr->unwinding.mapped_size; +free (jd->unwinding_data); jd->unwinding_data = unwinding_data;return 0 ;
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 8/18 perf mem-events: Avoid uninitialized read
In reply to Ian Rogerspmu should be initialized to NULL before perf_pmus__scan loop. Fix and shrink the scope of pmu at the same time. Issue detected by clang-tidy.
--- tools/perf/util/mem-events.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/mem-events.c +2 -1
@@ -185,7 +185,6 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr, {int i = *argv_nr, k =0 ;struct perf_mem_event *e; -struct perf_pmu *pmu;for (int j =0 ; j <PERF_MEM_EVENTS__MAX ; j++) { e =perf_mem_events__ptr (j);
@@ -202,6 +201,8 @@ int perf_mem_events__record_args(const char **rec_argv, int *argv_nr, rec_argv[i++] ="-e" ; rec_argv[i++] =perf_mem_events__name (j,NULL ); }else { +struct perf_pmu *pmu =NULL ; +if (!e->supported) {perf_mem_events__print_unsupport_hybrid (e, j);return -1 ;
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 9/18 perf dlfilter: Be defensive against potential NULL dereference
In reply to Ian RogersIn the unlikely case of having a symbol without a mapping, avoid a NULL dereference that clang-tidy warns about.
--- tools/perf/util/dlfilter.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/dlfilter.c +3 -1
@@ -52,8 +52,10 @@ static void al_to_d_al(struct addr_location *al, struct perf_dlfilter_al *d_al) d_al->sym_end = sym->end;if (al->addr < sym->end) d_al->symoff = al->addr - sym->start; -else +else if (al->map) d_al->symoff = al->addr -map__start (al->map) - sym->start; +else + d_al->symoff =0 ; d_al->sym_binding = sym->binding; }else { d_al->sym =NULL ;
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 10/18 perf hists browser: Reorder variables to reduce padding
In reply to Ian RogersAddress clang-tidy warning:
``` tools/perf/ui/browsers/hists.c:2416:8: warning: Excessive padding in 'struct popup_action' (8 padding bytes, where 0 is optimal). Optimal fields order: time, thread, evsel, fn, ms, socket, rstype, ```
--- tools/perf/ui/browsers/hists.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/ui/browsers/hists.c +2 -2
@@ -2416,12 +2416,12 @@ static int switch_data_file(void)struct popup_action {unsigned long time;struct thread *thread; +struct evsel *evsel; +int (*fn)(struct hist_browser *browser,struct popup_action *act);struct map_symbol ms;int socket; -struct evsel *evsel;enum rstype rstype; -int (*fn)(struct hist_browser *browser,struct popup_action *act); }; static int
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 11/18 perf hists browser: Avoid potential NULL dereference
In reply to Ian RogersOn other code paths browser->he_selection is NULL checked, add a missing case reported by clang-tidy.
--- tools/perf/ui/browsers/hists.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/ui/browsers/hists.c +1 -1
@@ -3302,7 +3302,7 @@ static int evsel__hists_browse(struct evsel *evsel, int nr_events, const char *h &options[nr_options], &bi->to.ms, bi->to.al_addr); - }else { + }else if (browser->he_selection) { nr_options += add_annotate_opt(browser, &actions[nr_options], &options[nr_options],
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 12/18 perf svghelper: Avoid memory leak
In reply to Ian RogersOn success path the sib_core and sib_thr values weren't being freed. Detected by clang-tidy.
--- tools/perf/util/svghelper.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/svghelper.c +3 -2
@@ -754,6 +754,7 @@ int svg_build_topology_map(struct perf_env *env)int i, nr_cpus;struct topology t;char *sib_core, *sib_thr; +int ret =-1 ; nr_cpus =min (env->nr_cpus_online,MAX_NR_CPUS );
@@ -799,11 +800,11 @@ int svg_build_topology_map(struct perf_env *env)scan_core_topology (topology_map, &t, nr_cpus); -return 0 ; + ret =0 ; exit:zfree (&t.sib_core);zfree (&t.sib_thr); -return -1 ; +return ret; }
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 13/18 perf lock: Fix a memory leak on an error path
In reply to Ian RogersIf a memory allocation fails then the strdup-ed string needs freeing. Detected by clang-tidy.
--- tools/perf/builtin-lock.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/builtin-lock.c +1 -0
@@ -2478,6 +2478,7 @@ static int parse_call_stack(const struct option *opt __maybe_unused, const char entry =malloc (sizeof (*entry) +strlen (tok) +1 );if (entry ==NULL ) {pr_err ("Memory allocation failure\n" ); +free (s);return -1 ; }
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 14/18 perf parse-events: Fix unlikely memory leak when cloning terms
In reply to Ian RogersAdd missing free on an error path as detected by clang-tidy.
--- tools/perf/util/parse-events.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/parse-events.c +3 -1
@@ -2549,8 +2549,10 @@ int parse_events_term__clone(struct parse_events_term **new,return new_term (new, &temp,/*str=*/ NULL , term->val.num); str =strdup (term->val.str); -if (!str) +if (!str) { +zfree (&temp.config);return -ENOMEM ; + }return new_term (new, &temp, str,/*num=*/ 0 ); }
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 15/18 tools api: Avoid potential double free
In reply to Ian Rogersio__getline will free the line on error but it doesn't clear the out argument. This may lead to the line being freed twice, like in tools/perf/util/srcline.c as detected by clang-tidy.
--- tools/lib/api/io.h | 1 + 1 file changed, 1 insertion(+)
tools/lib/api/io.h +1 -0
@@ -180,6 +180,7 @@ static inline ssize_t io__getline(struct io *io, char **line_out, size_t *line_lreturn line_len; err_out:free (line); + *line_out =NULL ;return -ENOMEM ; }
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 16/18 perf trace-event-info: Avoid passing NULL value to closedir
In reply to Ian RogersIf opendir failed then closedir was passed NULL which is erroneous. Caught by clang-tidy.
--- tools/perf/util/trace-event-info.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/trace-event-info.c +2 -1
@@ -313,7 +313,8 @@ static int record_event_files(struct tracepoint_path *tps) } err =0 ; out: -closedir (dir); +if (dir) +closedir (dir);put_tracing_file (path);return err;
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 17/18 perf header: Fix various error path memory leaks
In reply to Ian RogersMemory leaks were detected by clang-tidy.
--- tools/perf/util/header.c | 60 +++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 26 deletions(-)
tools/perf/util/header.c +34 -26
@@ -2573,7 +2573,7 @@ static int process_cmdline(struct feat_fd *ff, void *data __maybe_unused)static int process_cpu_topology (struct feat_fd *ff,void *data__maybe_unused ) {u32 nr, i; -char *str; +char *str =NULL ;struct strbuf sb;int cpu_nr = ff->ph->env.nr_cpus_avail;u64 size =0 ;
@@ -2601,7 +2601,7 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)if (strbuf_add (&sb, str,strlen (str) +1 ) <0 ) goto error; size +=string_size (str); -free (str); +zfree (&str); } ph->env.sibling_cores =strbuf_detach (&sb,NULL );
@@ -2620,7 +2620,7 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)if (strbuf_add (&sb, str,strlen (str) +1 ) <0 ) goto error; size +=string_size (str); -free (str); +zfree (&str); } ph->env.sibling_threads =strbuf_detach (&sb,NULL );
@@ -2684,7 +2684,7 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused)if (strbuf_add (&sb, str,strlen (str) +1 ) <0 ) goto error; size +=string_size (str); -free (str); +zfree (&str); } ph->env.sibling_dies =strbuf_detach (&sb,NULL );
@@ -2699,6 +2699,7 @@ static int process_cpu_topology(struct feat_fd *ff, void *data __maybe_unused) error:strbuf_release (&sb); +zfree (&str); free_cpu:zfree (&ph->env.cpu);return -1 ;
@@ -2736,10 +2737,9 @@ static int process_numa_topology(struct feat_fd *ff, void *data __maybe_unused) goto error; n->map =perf_cpu_map__new (str); +free (str);if (!n->map) goto error; - -free (str); } ff->ph->env.nr_numa_nodes = nr; ff->ph->env.numa_nodes = nodes;
@@ -2913,10 +2913,10 @@ static int process_cache(struct feat_fd *ff, void *data __maybe_unused)return -1 ;for (i =0 ; i < cnt; i++) { -struct cpu_cache_level c; +struct cpu_cache_level *c = &caches[i];#define _R (v) \ - if (do_read_u32(ff, &c.v))\ + if (do_read_u32(ff, &c->v)) \ goto out_free_caches; \_R (level)
@@ -2926,22 +2926,25 @@ static int process_cache(struct feat_fd *ff, void *data __maybe_unused)#undef _R#define _R (v) \ - c.v = do_read_string(ff); \ - if (!c.v) \ - goto out_free_caches; + c->v = do_read_string(ff); \ + if (!c->v) \ + goto out_free_caches; \ _R(type) _R(size) _R(map)#undef _R - - caches[i] = c; } ff->ph->env.caches = caches; ff->ph->env.caches_cnt = cnt;return 0 ; out_free_caches: +for (i =0 ; i < cnt; i++) { +free (caches[i].type); +free (caches[i].size); +free (caches[i].map); + }free (caches);return -1 ; }
@@ -3585,18 +3588,16 @@ static int perf_header__adds_write(struct perf_header *header, struct feat_copier *fc) {int nr_sections; -struct feat_fd ff; +struct feat_fd ff = { + .fd = fd, + .ph = header, + };struct perf_file_section *feat_sec, *p;int sec_size;u64 sec_start;int feat;int err; - ff = (struct feat_fd ){ - .fd = fd, - .ph = header, - }; - nr_sections =bitmap_weight (header->adds_features,HEADER_FEAT_BITS );if (!nr_sections)return 0 ;
@@ -3623,6 +3624,7 @@ static int perf_header__adds_write(struct perf_header *header, err =do_write (&ff, feat_sec, sec_size);if (err <0 )pr_debug ("failed to write feature section\n" ); +free (ff.buf);/* TODO: added to silence clang-tidy. */ free (feat_sec);return err; }
@@ -3630,11 +3632,11 @@ static int perf_header__adds_write(struct perf_header *header,int perf_header__write_pipe (int fd) {struct perf_pipe_file_header f_header; -struct feat_fd ff; +struct feat_fd ff = { + .fd = fd, + };int err; - ff = (struct feat_fd ){ .fd = fd }; - f_header = (struct perf_pipe_file_header ){ .magic =PERF_MAGIC , .size =sizeof (f_header),
@@ -3645,7 +3647,7 @@ int perf_header__write_pipe(int fd)pr_debug ("failed to write perf pipe header\n" );return err; } - +free (ff.buf);return 0 ; }
@@ -3658,11 +3660,12 @@ static int perf_session__do_write_header(struct perf_session *session,struct perf_file_attr f_attr;struct perf_header *header = &session->header;struct evsel *evsel; -struct feat_fd ff; +struct feat_fd ff = { + .fd = fd, + };u64 attr_offset;int err; - ff = (struct feat_fd ){ .fd = fd};lseek (fd,sizeof (f_header),SEEK_SET );evlist__for_each_entry (session->evlist, evsel) {
@@ -3670,6 +3673,7 @@ static int perf_session__do_write_header(struct perf_session *session, err =do_write (&ff, evsel->core.id, evsel->core.ids *sizeof (u64));if (err <0 ) {pr_debug ("failed to write perf header\n" ); +free (ff.buf);return err; } }
@@ -3695,6 +3699,7 @@ static int perf_session__do_write_header(struct perf_session *session, err =do_write (&ff, &f_attr,sizeof (f_attr));if (err <0 ) {pr_debug ("failed to write perf header attribute\n" ); +free (ff.buf);return err; } }
@@ -3705,8 +3710,10 @@ static int perf_session__do_write_header(struct perf_session *session,if (at_exit) { err =perf_header__adds_write (header, evlist, fd, fc); -if (err <0 ) +if (err <0 ) { +free (ff.buf);return err; + } } f_header = (struct perf_file_header ){
@@ -3728,6 +3735,7 @@ static int perf_session__do_write_header(struct perf_session *session,lseek (fd,0 ,SEEK_SET ); err =do_write (&ff, &f_header,sizeof (f_header)); +free (ff.buf);if (err <0 ) {pr_debug ("failed to write perf header\n" );return err;
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Ian Rogers
reviewerPATCH v3 18/18 perf bpf_counter: Fix a few memory leaks
In reply to Ian RogersMemory leaks were detected by clang-tidy.
--- tools/perf/util/bpf_counter.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/perf/util/bpf_counter.c +4 -1
@@ -104,7 +104,7 @@ static int bpf_program_profiler_load_one(struct evsel *evsel, u32 prog_id)struct bpf_prog_profiler_bpf *skel;struct bpf_counter *counter;struct bpf_program *prog; -char *prog_name; +char *prog_name =NULL ;int prog_fd;int err;
@@ -155,10 +155,12 @@ static int bpf_program_profiler_load_one(struct evsel *evsel, u32 prog_id)assert (skel !=NULL ); counter->skel = skel;list_add (&counter->list, &evsel->bpf_counter_list); +free (prog_name);close (prog_fd);return 0 ; err_out:bpf_prog_profiler_bpf__destroy (skel); +free (prog_name);free (counter);close (prog_fd);return -1 ;
@@ -180,6 +182,7 @@ static int bpf_program_profiler__load(struct evsel *evsel, struct target *target (*p != '\0' && *p != ',')) {pr_err ("Failed to parse bpf prog ids %s\n" , target->bpf_str); +free (bpf_str_);return -1 ; }
Signature · 1 line
2.42.0.609.gbb76f46606-goog
Hi Ian,
Quoted 56 lines · Ian Rogers
Allow the clang-tools scripts to work with builds in tools such as
tools/perfandtools/lib/perf. An example use looks like:``` $ cd tools/perf $ make CC=clang CXX=clang++ $ ../../scripts/clang-tools/gen_compile_commands.py $ ../../scripts/clang-tools/run-clang-tools.py clang-tidy compile_commands.json -checks=-*,readability-named-parameter Skipping non-C file: 'tools/perf/bench/mem-memcpy-x86-64-asm.S' Skipping non-C file: 'tools/perf/bench/mem-memset-x86-64-asm.S' Skipping non-C file: 'tools/perf/arch/x86/tests/regs_load.S' 8 warnings generated. Suppressed 8 warnings (8 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 2 warnings generated. 4 warnings generated. Suppressed 4 warnings (4 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 2 warnings generated. 4 warnings generated. Suppressed 4 warnings (4 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 3 warnings generated. tools/perf/util/parse-events-flex.c:546:27: warning: all parameters should be named in a function [readability-named-parameter] void *yyalloc ( yy_size_t , yyscan_t yyscanner ); ^ /*size*/ ... ```Fix a number of the more serious low-hanging issues in perf found by clang-tidy.
This support isn't complete, in particular it doesn't support output directories properly and so fails for
tools/lib/bpf,tools/bpf/bpftooland if an output directory is used.v3. Add Nick Desaulniers reviewed-by to patch 3. For Namhyung, drop
"perf hisi-ptt: Fix potential memory leak", split lock change out of "perf svghelper: Avoid memory leak" and address comments in "perf header: Fix various error path memory leaks".
v2: Address comments by Nick Desaulniers in patch 3, and add their
Reviewed-by to patches 1 and 2.Ian Rogers (18):
gen_compile_commands: Allow the line prefix to still becmd_
gen_compile_commands: Sort output compile commands by file name
run-clang-tools: Add pass through checks and header-filter arguments perf bench uprobe: Fix potential use of memory after free perf buildid-cache: Fix use of uninitialized value
perf env: Remove unnecessary NULL tests
perf jitdump: Avoid memory leak
perf mem-events: Avoid uninitialized read
perf dlfilter: Be defensive against potential NULL dereference perf hists browser: Reorder variables to reduce padding
Quoted 8 lines
perf hists browser: Avoid potential NULL dereference perf svghelper: Avoid memory leak
perf lock: Fix a memory leak on an error path
perf parse-events: Fix unlikely memory leak when cloning terms tools api: Avoid potential double free
perf trace-event-info: Avoid passing NULL value to closedir perf header: Fix various error path memory leaks perfbpf_counter: Fix a few memory leaks
I agree with your comment on v2 that it needs more work to clean the code up. Anyway I'm ok with v3 now.
For ther perf part (patch 4 to 18),
Thanks,
Namhyung
Quoted 22 lines
scripts/clang-tools/gen_compile_commands.py | 8 +-- scripts/clang-tools/run-clang-tools.py | 32 ++++++++--- tools/lib/api/io.h | 1 + tools/perf/bench/uprobe.c | 1 + tools/perf/builtin-buildid-cache.c | 6 ++- tools/perf/builtin-lock.c | 1 + tools/perf/ui/browsers/hists.c | 6 +-- tools/perf/util/bpf_counter.c | 5 +- tools/perf/util/dlfilter.c | 4 +- tools/perf/util/env.c | 6 +-- tools/perf/util/header.c | 60 ++++++++++++--------- tools/perf/util/jitdump.c | 1 + tools/perf/util/mem-events.c | 3 +- tools/perf/util/parse-events.c | 4 +- tools/perf/util/svghelper.c | 5 +- tools/perf/util/trace-event-info.c | 3 +- 16 files changed, 94 insertions(+), 52 deletions(-)--
2.42.0.609.gbb76f46606-goog
Quoted 69 lines · Namhyung Kim
Hi Ian,
Quoted 56 lines · Ian Rogers
Allow the clang-tools scripts to work with builds in tools such as
tools/perfandtools/lib/perf. An example use looks like:``` $ cd tools/perf $ make CC=clang CXX=clang++ $ ../../scripts/clang-tools/gen_compile_commands.py $ ../../scripts/clang-tools/run-clang-tools.py clang-tidy compile_commands.json -checks=-*,readability-named-parameter Skipping non-C file: 'tools/perf/bench/mem-memcpy-x86-64-asm.S' Skipping non-C file: 'tools/perf/bench/mem-memset-x86-64-asm.S' Skipping non-C file: 'tools/perf/arch/x86/tests/regs_load.S' 8 warnings generated. Suppressed 8 warnings (8 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 2 warnings generated. 4 warnings generated. Suppressed 4 warnings (4 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 2 warnings generated. 4 warnings generated. Suppressed 4 warnings (4 in non-user code). Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well. 3 warnings generated. tools/perf/util/parse-events-flex.c:546:27: warning: all parameters should be named in a function [readability-named-parameter] void *yyalloc ( yy_size_t , yyscan_t yyscanner ); ^ /*size*/ ... ```Fix a number of the more serious low-hanging issues in perf found by clang-tidy.
This support isn't complete, in particular it doesn't support output directories properly and so fails for
tools/lib/bpf,tools/bpf/bpftooland if an output directory is used.v3. Add Nick Desaulniers reviewed-by to patch 3. For Namhyung, drop
"perf hisi-ptt: Fix potential memory leak", split lock change out of "perf svghelper: Avoid memory leak" and address comments in "perf header: Fix various error path memory leaks".
v2: Address comments by Nick Desaulniers in patch 3, and add their
Reviewed-by to patches 1 and 2.Ian Rogers (18):
gen_compile_commands: Allow the line prefix to still becmd_
gen_compile_commands: Sort output compile commands by file name
run-clang-tools: Add pass through checks and header-filter arguments perf bench uprobe: Fix potential use of memory after free perf buildid-cache: Fix use of uninitialized value
perf env: Remove unnecessary NULL tests
perf jitdump: Avoid memory leak
perf mem-events: Avoid uninitialized read
perf dlfilter: Be defensive against potential NULL dereference perf hists browser: Reorder variables to reduce paddingQuoted 8 lines
perf hists browser: Avoid potential NULL dereference perf svghelper: Avoid memory leak
perf lock: Fix a memory leak on an error path
perf parse-events: Fix unlikely memory leak when cloning terms tools api: Avoid potential double free
perf trace-event-info: Avoid passing NULL value to closedir perf header: Fix various error path memory leaks perfbpf_counter: Fix a few memory leaks
Quoted 5 lines
I agree with your comment on v2 that it needs more work to clean the code up. Anyway I'm ok with v3 now.
For ther perf part (patch 4 to 18),
Acked-by Namhyung Kim <[email protected]>
Applied to perf-tools-next, thanks!