Power10 introduces prefixed instructions that are 8 bytes wide.  objdump
emits these as two lines: the first carries both the raw bytes and the
mnemonic, while the second carries only the raw bytes of the second word
with no mnemonic:

  1020900c:  0b 00 10 06  pla  r31,757280  # 102c1e2c <func>
  10209010:  20 8e e0 3b

disasm_line__parse_powerpc() calls disasm_line__parse() on the mnemonic
portion of the line.  disasm_line__parse() returns -1 when the name
string is empty, which causes disasm_line__new() to return NULL, which
causes symbol__parse_objdump_line() to return -1, which immediately
breaks the objdump output parsing loop in symbol__disassemble_objdump().

The result is that annotation of any function containing a prefixed
instruction stops at the continuation word and all subsequent
instructions in the function are lost.

Fix this by treating an empty mnemonic (after the 11-byte raw hex prefix)
as a valid case in disasm_line__parse_powerpc(): skip the call to
disasm_line__parse(), set the instruction name to an empty heap-allocated
string, and point operands at the empty end of the line so printing
callbacks can safely render "%s".

In check_ppc_insn(), skip decoding raw_insn when dl->ins.name is empty
and dl->ops.raw is non-NULL (indicating an objdump continuation line
rather than a raw disassembly line), so the 32-bit suffix word is not
misclassified as a standalone instruction.  Also free the previous
empty dl->ins.name when check_ppc_insn() replaces it in raw mode.

Fixes: 4c3f09e35ca9 ("perf annotate: Return errors from 
disasm_line__parse_powerpc()")
Reported-by: Narendra Nalli <[email protected]>
Reported-by: Vijay Puliyala <[email protected]>
Reported-by: [email protected]
Signed-off-by: Athira Rajeev <[email protected]>
---
Changelog:
V2 -> V3:
Addressed review comments from Sashiko:
- Avoid out-of-bounds read past dl->al.line when strlen(name_raw_insn) < 
RAW_BYTES.
- Skip opcode classification in check_ppc_insn() for objdump continuation
  lines (where dl->ins.name is empty and dl->ops.raw != NULL).
- Include <stdlib.h> in annotate-powerpc.c, check strdup() return values,
  and free old dl->ins.name across all replacement branches.
- Add Fixes: tag.

V1 -> V2:
Addressed review comment from Sashiko.
Set the operand value and also free the instruction name
before picking from opcode value


 .../util/annotate-arch/annotate-powerpc.c     | 37 +++++++++++++++++--
 tools/perf/util/disasm.c                      | 26 ++++++++++++-
 2 files changed, 57 insertions(+), 6 deletions(-)

diff --git a/tools/perf/util/annotate-arch/annotate-powerpc.c 
b/tools/perf/util/annotate-arch/annotate-powerpc.c
index 00b93296fc94..c4cb19f99849 100644
--- a/tools/perf/util/annotate-arch/annotate-powerpc.c
+++ b/tools/perf/util/annotate-arch/annotate-powerpc.c
@@ -1,4 +1,5 @@
 // SPDX-License-Identifier: GPL-2.0
+#include <stdlib.h>
 #include <string.h>
 #include <linux/compiler.h>
 #include <linux/kernel.h>
@@ -289,6 +290,13 @@ const struct ins_ops *check_ppc_insn(struct disasm_line 
*dl)
        };
        char name_insn[32];
 
+       /*
+        * An empty instruction name with non-NULL raw operands comes from
+        * an objdump Power10 prefixed instruction continuation word; do not 
decode it.
+        */
+       if (!strlen(dl->ins.name) && dl->ops.raw)
+               return NULL;
+
        /*
         * Instructions with opcode 32 to 63 are memory
         * instructions in powerpc
@@ -299,16 +307,28 @@ const struct ins_ops *check_ppc_insn(struct disasm_line 
*dl)
                 * opcode to be used in insn-stat
                 */
                if (!strlen(dl->ins.name)) {
+                       char *name;
+
                        sprintf(name_insn, "%d", opcode);
-                       dl->ins.name = strdup(name_insn);
+                       name = strdup(name_insn);
+                       if (name) {
+                               free((void *)dl->ins.name);
+                               dl->ins.name = name;
+                       }
                }
                return &load_store_ops;
        } else if (opcode == 31) {
                /* Check for memory instructions with opcode 31 */
                ret = bsearch(&mem_insns_31_opcode, ins_array, 
ARRAY_SIZE(ins_array), sizeof(ins_array[0]), cmp_offset);
                if (ret) {
-                       if (!strlen(dl->ins.name))
-                               dl->ins.name = strdup(ret->name);
+                       if (!strlen(dl->ins.name)) {
+                               char *name = strdup(ret->name);
+
+                               if (name) {
+                                       free((void *)dl->ins.name);
+                                       dl->ins.name = name;
+                               }
+                       }
                        return &load_store_ops;
                } else {
                        mem_insns_31_opcode.value = PPC_22_30(raw_insn);
@@ -324,8 +344,17 @@ const struct ins_ops *check_ppc_insn(struct disasm_line 
*dl)
                mem_insns_31_opcode.value = opcode;
                ret = bsearch(&mem_insns_31_opcode, arithmetic_two_ops, 
ARRAY_SIZE(arithmetic_two_ops),
                                sizeof(arithmetic_two_ops[0]), cmp_offset);
-               if (ret != NULL)
+               if (ret != NULL) {
+                       if (!strlen(dl->ins.name)) {
+                               char *name = strdup(ret->name);
+
+                               if (name) {
+                                       free((void *)dl->ins.name);
+                                       dl->ins.name = name;
+                               }
+                       }
                        return &arithmetic_ops;
+               }
        }
 
        return NULL;
diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 169fc5acbfc5..724e819f9320 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -898,7 +898,7 @@ static int disasm_line__parse_powerpc(struct disasm_line 
*dl, struct annotate_ar
        const char **namep = &dl->ins.name;
        char **rawp = &dl->ops.raw;
        char *tmp_raw_insn, *name_raw_insn = skip_spaces(line);
-       char *name = skip_spaces(name_raw_insn + RAW_BYTES);
+       char *name;
        int disasm = 0;
        int ret = 0;
 
@@ -909,7 +909,29 @@ static int disasm_line__parse_powerpc(struct disasm_line 
*dl, struct annotate_ar
                return -1;
 
        if (disasm) {
-               ret = disasm_line__parse(name, namep, rawp);
+               if (strlen(name_raw_insn) < RAW_BYTES)
+                       return -1;
+
+               name = skip_spaces(name_raw_insn + RAW_BYTES);
+               if (name[0] != '\0') {
+                       ret = disasm_line__parse(name, namep, rawp);
+               } else {
+                       /*
+                        * Power10 prefixed instructions are 8 bytes and 
objdump emits
+                        * the second word on its own line with raw bytes but no
+                        * mnemonic, e.g.:
+                        *   1020900c:  0b 00 10 06  pla  r31,757280
+                        *   10209010:  20 8e e0 3b
+                        *
+                        * Treat a missing mnemonic as an empty instruction 
name with
+                        * empty operands rather than a parse failure, so that 
parsing
+                        * continues past it.
+                        */
+                       *namep = strdup("");
+                       if (*namep == NULL)
+                               return -1;
+                       *rawp = name;   /* the '\0' at the end of dl->al.line */
+               }
        } else {
                /*
                 * The name is freed in disasm_line__free() and may be replaced
-- 
2.52.0


Reply via email to