[PATCH 00/20] perf: fix several memory leaks reported by ASan on perf-test

From: Riccardo Mancini
Date: Thu Jul 15 2021 - 12:07:31 EST


This patchset fixes several memory leaks found using ASan on perf-test.
After this patch, all memory leaks in perf-test that were found on my machine
are fixed.
This does not mean that there are no longer memory leaks in perf (just running
perf report or perf top raises a few), but that now it is possible to run the
perf tests with ASan to catch future memory errors.

While many of these leaks have little to no effect on the daily use of perf
(some were inside the test itself, other were just a missing cleanup on exit),
other were (slightly) more serious, namely:
- nsinfo refcounting was broken since it was not decreased when a reference was
replaced, causing many nsinfos to never being deallocated.
- dso__new_map refcounting was wrong, missing a __put
- lzma stream was never closed

Below are the results of perf-test before and after this patchset. Only failed
and skipped tests are reported for brevity.

The perf binary has been compiled using
make DEBUG=1 EXTRA_CFLAGS='-fno-omit-frame-pointer -fsanitize=address'
compilation using clang shows same results.

Tests are run on my laptop:
Intel(R) Core(TM) i7-5500U CPU @ 2.40GHz (2 cores + hyperthreading)
Fedora 34 w/ kernel 5.12.14-300.fc34.x86_64

Before this patchset:
1: vmlinux symtab matches kallsyms : FAILED!
19: 'import perf' in python : FAILED!
23.1: Read Only Watchpoint : Skip (missing hardware support)
31: Lookup mmap thread : FAILED!
41: Session topology : FAILED!
42.1: Basic BPF filtering : FAILED!
42.2: BPF pinning : Skip
42.3: BPF prologue generation : Skip
49: Synthesize attr update : FAILED!
58: builtin clang support : Skip (not compiled in)
63: Test libpfm4 support : Skip (not compiled in)
65: maps__merge_in : FAILED!
73: DWARF unwind : FAILED!
78: build id cache operations : FAILED!
82: Use vfs_getname probe to get syscall args filenames : FAILED!
83: Zstd perf.data compression/decompression : FAILED!
86: perf stat --bpf-counters test : Skip
87: Check Arm CoreSight trace data recording and synthesized samples: Skip
88: Check open filename arg using perf trace + vfs_getname : FAILED!

After this patchset:
19: 'import perf' in python : FAILED!
23.1: Read Only Watchpoint : Skip (missing hardware support)
58: builtin clang support : Skip (not compiled in)
63: Test libpfm4 support : Skip (not compiled in)
73: DWARF unwind : FAILED!
86: perf stat --bpf-counters test : Skip
87: Check Arm CoreSight trace data recording and synthesized samples: Skip

After this patchset, without ASan:
23.1: Read Only Watchpoint : Skip (missing hardware support)
58: builtin clang support : Skip (not compiled in)
63: Test libpfm4 support : Skip (not compiled in)
86: perf stat --bpf-counters test : Skip
87: Check Arm CoreSight trace data recording and synthesized samples: Skip

The two failing tests are false positives and can be skipped:
- "19: 'import perf' in python" fails since there are memory leaks inside the
python library.
- "73: DWARF unwind" fails due to stack buffer underflow, since it's unwinding
the stack.

Riccardo

Riccardo Mancini (20):
perf nsinfo: fix refcounting
perf env: fix sibling_dies memory leak
perf test: session_topology: delete session->evlist
perf test: event_update: fix memory leak of evlist
perf test: event_update: fix memory leak of unit
perf dso: fix memory leak in dso__new_map
perf test: maps__merge_in: fix memory leak of maps
perf env: fix memory leak of cpu_pmu_caps
perf report: free generated help strings for sort option
perf inject: close inject.output
perf session: cleanup trace_event
perf script: release zstd data
perf script: fix memory leaks in perf_script
perf util/lzma: close lzma stream
perf trace: free malloc'd trace fields on exit
perf trace: free syscall->arg_fmt
perf trace: free syscall tp fields in evsel->priv
perf trace: free strings in trace__parse_events_option
perf test: bpf: free obj_buf
perf util/probe-file: delete namelist on error in del_events

tools/perf/builtin-inject.c | 13 ++++++---
tools/perf/builtin-report.c | 33 ++++++++++++++-------
tools/perf/builtin-script.c | 8 ++++++
tools/perf/builtin-trace.c | 51 +++++++++++++++++++++++++++++++--
tools/perf/tests/bpf.c | 1 +
tools/perf/tests/event_update.c | 6 ++--
tools/perf/tests/maps.c | 2 ++
tools/perf/tests/topology.c | 1 +
tools/perf/util/dso.c | 4 ++-
tools/perf/util/env.c | 2 ++
tools/perf/util/lzma.c | 8 ++++--
tools/perf/util/map.c | 2 ++
tools/perf/util/probe-event.c | 4 ++-
tools/perf/util/probe-file.c | 4 +--
tools/perf/util/session.c | 1 +
tools/perf/util/sort.c | 2 +-
tools/perf/util/sort.h | 2 +-
17 files changed, 116 insertions(+), 28 deletions(-)

--
2.31.1