perf annotate: Fix memcpy size in arch__grow_instructions
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
@@ -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
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 copyarch->nr_instructions* sizeof(struct ins) bytes, not justarch->nr_instructionsbytes.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
Signed-off-by Suchit Karunakaran <[email protected]>--- tools/perf/util/disasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/disasm.c +0
-0
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
-1goto out_update_instructions; } --2.52.0
Arnaldo Carvalho de Melo
maintainerQuoted 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 copyarch->nr_instructions* sizeof(struct ins) bytes, not justarch->nr_instructionsbytes.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
Signed-off-by Suchit Karunakaran <[email protected]>--- tools/perf/util/disasm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/perf/util/disasm.c +0
-0tools/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
Reviewed-by Ian Rogers <[email protected]>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:
Its an old bug I introduced almost 10 years ago ;-\
- Arnaldo