Skip to content

perf: fix several memory leaks reported by ASan on perf-test

Riccardo Mancini linux-perf-users lkml 1 version Applied

Status from this message: Thanks, applied.

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest20 patches 29 repliesApplied

Patches in v1 Applied

#Subject and filesSizeRepliesApplied
1/20perf nsinfo: fix refcountingtools/perf/builtin-inject.c tools/perf/util/map.c tools/perf/util/probe-event.c+8 -37 replies
2/20perf env: fix sibling_dies memory leaktools/perf/util/env.c+1 -02 replies
3/20perf test: session_topology: delete session->evlisttools/perf/tests/topology.c+1 -01 reply
4/20perf test: event_update: fix memory leak of evlisttools/perf/tests/event_update.c+1 -11 reply
5/20perf test: event_update: fix memory leak of unittools/perf/tests/event_update.c+3 -11 reply
6/20perf dso: fix memory leak in dso__new_maptools/perf/util/dso.c+3 -12 replies
7/20perf test: maps__merge_in: fix memory leak of mapstools/perf/tests/maps.c+2 -01 reply
8/20perf env: fix memory leak of cpu_pmu_capstools/perf/util/env.c+1 -01 reply
9/20perf report: free generated help strings for sort optiontools/perf/builtin-report.c tools/perf/util/sort.c tools/perf/util/sort.h+24 -131 reply
10/20perf inject: close inject.outputtools/perf/builtin-inject.c+6 -21 reply
11/20perf session: cleanup trace_eventtools/perf/util/session.c+1 -01 reply
12/20perf script: release zstd datatools/perf/builtin-script.c+1 -01 reply
13/20perf script: fix memory leaks in perf_scripttools/perf/builtin-script.c+7 -01 reply
14/20perf util/lzma: close lzma streamtools/perf/util/lzma.c+5 -31 reply
15/20perf trace: free malloc'd trace fields on exittools/perf/builtin-trace.c+16 -11 reply
16/20perf trace: free syscall->arg_fmttools/perf/builtin-trace.c+15 -11 reply
17/20perf trace: free syscall tp fields in evsel->privtools/perf/builtin-trace.c+16 -11 reply
18/20perf trace: free strings in trace__parse_events_optiontools/perf/builtin-trace.c+3 -01 reply
19/20perf test: bpf: free obj_buftools/perf/tests/bpf.c+1 -02 replies
20/20perf util/probe-file: delete namelist on error in del_eventstools/perf/util/probe-file.c+2 -21 reply

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

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

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(-)
Signature · 1 line
2.31.1