Skip to contentSkip to conversation

perf annotate: Fix memcpy size in arch__grow_instructions

PATCH In review Suchit Karunakaran 3 participants 3 messages Jan 22 – Jan 23 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf annotate: Fix memcpy size in arch__grow_instructions

The memcpy in arch__grow_instructions() is copying the wrong number of bytes when growing from a non-allocated table. It should copy arch->nr_instructions * sizeof(struct ins) bytes, not just arch->nr_instructions bytes.

This bug causes data corruption as only a partial copy of the instruction table is made, leading to garbage data in most entries and potential crashes

---
 tools/perf/util/disasm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/disasm.c +1 -1
index 50b9433f3f8e..6faa9df8e373 100644
@@ -81,7 +81,7 @@ static int arch__grow_instructions(struct arch *arch)
 	if (new_instructions == NULL)
 		return -1;
 
-	memcpy(new_instructions, arch->instructions, arch->nr_instructions);
+	memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(struct ins));
 	goto out_update_instructions;
 }
 
Signature · 1 line
2.52.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 20 lines · Suchit Karunakaran

The memcpy in arch__grow_instructions() is copying the wrong number of bytes when growing from a non-allocated table. It should copy arch->nr_instructions * sizeof(struct ins) bytes, not just arch->nr_instructions bytes.

This bug causes data corruption as only a partial copy of the instruction table is made, leading to garbage data in most entries and potential crashes

---
 tools/perf/util/disasm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/disasm.c +0 -0
index 50b9433f3f8e..6faa9df8e373 100644

tools/perf/util/disasm.c static int arch__grow_instructions(struct arch *arch) · line 84

Quoted 5 lines
tools/perf/util/disasm.c +1 -1
@@ -81,7 +81,7 @@ static int arch__grow_instructions(struct arch *arch)
        if (new_instructions == NULL)
                return -1;
 
-       memcpy(new_instructions, arch->instructions, arch->nr_instructions);
+       memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(struct ins));

Nice catch! I think we've been trying to make code like this more like:
memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(*new_instructions));
but the immediately preceding calloc isn't doing this, so your patch is consistent. There is also a clean up in this code to switch to using reallocarray rather than realloc.

Thanks,
Ian

Quoted 6 lines
tools/perf/util/disasm.c +0 -1
        goto out_update_instructions;
 }
 
--

2.52.0

#2 lore raw
Quoted 27 lines · Ian Rogers
Quoted 19 lines · Suchit Karunakaran

The memcpy in arch__grow_instructions() is copying the wrong number of bytes when growing from a non-allocated table. It should copy arch->nr_instructions * sizeof(struct ins) bytes, not just arch->nr_instructions bytes.

This bug causes data corruption as only a partial copy of the instruction table is made, leading to garbage data in most entries and potential crashes

---
 tools/perf/util/disasm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/perf/util/disasm.c +0 -0
index 50b9433f3f8e..6faa9df8e373 100644

tools/perf/util/disasm.c static int arch__grow_instructions(struct arch *arch) · line 84

Quoted 5 lines
tools/perf/util/disasm.c +1 -1
@@ -81,7 +81,7 @@ static int arch__grow_instructions(struct arch *arch)
        if (new_instructions == NULL)
                return -1;
 
-       memcpy(new_instructions, arch->instructions, arch->nr_instructions);
+       memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(struct ins));
Quoted 8 lines

Nice catch! I think we've been trying to make code like this more like:
memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(*new_instructions));
but the immediately preceding calloc isn't doing this, so your patch is consistent. There is also a clean up in this code to switch to using reallocarray rather than realloc.

Right, that would be better, but the fix is good as is and we can cleanup it later.

I added the missing:

Fixes 2a1ff812c40be982 ("perf annotate: Introduce alternative method of keeping instructions table")

Its an old bug I introduced almost 10 years ago ;-\

- Arnaldo