Skip to contentSkip to conversation

perf disasm: Fix memory leak for locked operations

PATCH Applied Ian Rogers 3 participants 9 messages Aug 13, 2024 – Aug 14, 2024 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/2 perf disasm: Fix memory leak for locked operations

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Found with lock/leak sanitizer.

---
 tools/perf/util/disasm.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/disasm.c +1 -0
index 22289003e16d..226d2181f694 100644
@@ -566,6 +566,7 @@ static void lock__delete(struct ins_operands *ops)
 		ins_ops__delete(ops->locked.ops);
 
 	zfree(&ops->locked.ops);
+	zfree(&ops->locked.ins.name);
 	zfree(&ops->target.raw);
 	zfree(&ops->target.name);
 }
Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
author reviewer
#1 lore raw

PATCH 2/2 perf test annotate: Dump trapping test in trap handler

Help to better identify the location of test failures but dumping the failing test in the trap handler.

---
 tools/perf/tests/shell/annotate.sh | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/tests/shell/annotate.sh +2 -1
index b072d9b97387..2ccf4f1d46b6 100755
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 # perf annotate basic tests
 # SPDX-License-Identifier: GPL-2.0
 
@@ -28,6 +28,7 @@ cleanup() {
 }
 
 trap_cleanup() {
+  echo "Unexpected signal in ${FUNCNAME[1]}"
   cleanup
   exit 1
 }
Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
maintainer Applied
#2 lore raw
In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Found with lock/leak sanitizer.

Applied both patches to perf-tools-next.

Thanks,

- Arnaldo

Quoted 19 lines
---
 tools/perf/util/disasm.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/disasm.c +1 -0
index 22289003e16d..226d2181f694 100644
@@ -566,6 +566,7 @@ static void lock__delete(struct ins_operands *ops)
 		ins_ops__delete(ops->locked.ops);
 
 	zfree(&ops->locked.ops);
+	zfree(&ops->locked.ins.name);
 	zfree(&ops->target.raw);
 	zfree(&ops->target.name);
 }
Signature · 1 line
2.46.0.76.ge559c4bf1a-goog
author reviewer
#3 lore raw
Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Ian Rogers

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Found with lock/leak sanitizer.

Ooops, I meant address/leak sanitizer.

Quoted 1 line

Applied both patches to perf-tools-next.

Thanks, could you fix the commit message.

Thanks,
Ian

Quoted 23 lines

Thanks,

- Arnaldo

Quoted 19 lines
---
 tools/perf/util/disasm.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/disasm.c +1 -1
index 22289003e16d..226d2181f694 100644
@@ -566,6 +566,7 @@ static void lock__delete(struct ins_operands *ops)
              ins_ops__delete(ops->locked.ops);
 
      zfree(&ops->locked.ops);
+     zfree(&ops->locked.ins.name);
      zfree(&ops->target.raw);
      zfree(&ops->target.name);
 }
--

2.46.0.76.ge559c4bf1a-goog

#4 lore raw
Quoted 8 lines · Ian Rogers
Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 4 lines · Ian Rogers

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

Found with lock/leak sanitizer.

Ooops, I meant address/leak sanitizer.

Quoted 1 line

Applied both patches to perf-tools-next.

Thanks, could you fix the commit message.

Sure,

- Arnaldo

author reviewer
#5 lore raw
Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Ian Rogers
Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Ian Rogers

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Found with lock/leak sanitizer.

Quoted 7 lines
Quoted 5 lines

Ooops, I meant address/leak sanitizer.

Quoted 1 line

Applied both patches to perf-tools-next.

Thanks, could you fix the commit message.

Sure,

Also, it'd be good if maybe Namhyung could take a look. I did things this way as it made sense to me, but we have nested things going on and potentially the free would be more natural in ins_ops__delete.

Thanks,
Ian

#6 lore raw
Quoted 14 lines · Ian Rogers
Quoted 12 lines · Arnaldo Carvalho de Melo
Quoted 11 lines · Ian Rogers
Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Ian Rogers

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Found with lock/leak sanitizer.

Ooops, I meant address/leak sanitizer.

Quoted 1 line

Applied both patches to perf-tools-next.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Thanks, could you fix the commit message.

Sure,

Also, it'd be good if maybe Namhyung could take a look. I did things this way as it made sense to me, but we have nested things going on and potentially the free would be more natural in ins_ops__delete.

ins_ops__delete() would have to look it is operating on a lock operation to access the right member of the unnamed union (locked) to get that string pointer to free.

lock__delete() is the 'lock' destructor, that knows these details, so its the right place to do the freeing.

I.e.:

static struct ins_ops lock_ops = {
.free = lock__delete,
.parse = lock__parse,
.scnprintf = lock__scnprintf,
};

When it was introduced:

commit c46219ac34f0f365bac700ca6a10ef979c643233

Author: Arnaldo Carvalho de Melo <[email protected]>
Date:   Sat May 12 13:26:20 2012 -0300

perf annotate: Introduce ->free() method in ins_ops

So that we don't special case disasm_line__free, allowing each instruction class to provide a specialized destructor, like is needed for 'lock'.

- Arnaldo

maintainer Acked-by
#7 lore raw
In reply to Ian Rogers

Hi Ian,

Quoted 19 lines · Ian Rogers
Quoted 16 lines · Arnaldo Carvalho de Melo
Quoted 14 lines · Ian Rogers
Quoted 7 lines · Arnaldo Carvalho de Melo
Quoted 5 lines · Ian Rogers

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Found with lock/leak sanitizer.

Ooops, I meant address/leak sanitizer.

Quoted 1 line

Applied both patches to perf-tools-next.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Thanks, could you fix the commit message.

Sure,

Also, it'd be good if maybe Namhyung could take a look. I did things this way as it made sense to me, but we have nested things going on and potentially the free would be more natural in ins_ops__delete.

Looks good to me.

Thanks,
Namhyung

author reviewer
#8 lore raw
Quoted 27 lines · Namhyung Kim

Hi Ian,

Quoted 23 lines · Ian Rogers
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 8 lines · Ian Rogers
Quoted 6 lines · Arnaldo Carvalho de Melo
Quoted 4 lines · Ian Rogers

lock__parse calls disasm_line__parse passing &ops->locked.ins.name. Ensure ops->locked.ins.name is freed in lock__delete.

Quoted 7 lines
Quoted 1 line
Quoted 1 line

Found with lock/leak sanitizer.

Ooops, I meant address/leak sanitizer.

Quoted 1 line

Applied both patches to perf-tools-next.

Thanks, could you fix the commit message.

Sure,

Quoted 7 lines
Quoted 3 lines

Also, it'd be good if maybe Namhyung could take a look. I did things this way as it made sense to me, but we have nested things going on and potentially the free would be more natural in ins_ops__delete.

Looks good to me.

Thanks Namhyung, thanks Arnaldo. I think git blame said Namhyung because of a refactoring. I'm glad the free is in the correct location.

Thanks,
Ian