All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name
@ 2026-12-02 17:20 Athira Rajeev
  2026-12-02 17:20 ` [PATCH V3 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines Athira Rajeev
  2026-12-02 17:37 ` [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name sashiko-bot
  0 siblings, 2 replies; 3+ messages in thread
From: Athira Rajeev @ 2026-12-02 17:20 UTC (permalink / raw)
  To: acme, jolsa, adrian.hunter, james.clark, tmricht, maddy, irogers,
	namhyung, bradley.morgan
  Cc: linux-perf-users, linuxppc-dev, atrajeev, hbathini, tejas05,
	tshah, sashiko-bot

When the disassembler isn't used, disasm_line__parse_powerpc() sets the
instruction name to the string literal "".  That name is owned by the
disasm_line: disasm_line__free() does zfree(&dl->ins.name), and
check_ppc_insn() replaces it with a strdup()ed name when the raw
instruction is a memory or arithmetic one.

For instructions that check_ppc_insn() doesn't name, the literal is
passed to free(), which makes glibc abort with "free(): invalid
pointer".

Use a heap allocated empty string instead, so the name is always
something that can be freed.  This also allows check_ppc_insn() to free
the previous name before replacing it, which is done in a follow up
patch to avoid leaking it.  Also free *namep if strndup() fails during
raw instruction parsing to avoid leaking it on error.

Fixes: 06dd4c5a561c ("perf annotate: Add disasm_line__parse() to parse raw instruction for powerpc")
Reported-by: sashiko-bot@kernel.org
Signed-off-by: Athira Rajeev <atrajeev@linux.ibm.com>
---
Changelog:
V3 : Free *namep (dl->ins.name) if strndup() fails to avoid leaking
     it on error (reported by Sashiko).
V2 : Use strdup("") instead of string literal "" in
     disasm_line__parse_powerpc() so disasm_line__free() does not
     trigger an invalid free() (reported by Sashiko).

 tools/perf/util/disasm.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 5b9bc94be55e..169fc5acbfc5 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -908,14 +908,23 @@ static int disasm_line__parse_powerpc(struct disasm_line *dl, struct annotate_ar
 	if (name_raw_insn[0] == '\0')
 		return -1;
 
-	if (disasm)
+	if (disasm) {
 		ret = disasm_line__parse(name, namep, rawp);
-	else
-		*namep = "";
+	} else {
+		/*
+		 * The name is freed in disasm_line__free() and may be replaced
+		 * by check_ppc_insn(), so it must be heap allocated.
+		 */
+		*namep = strdup("");
+		if (*namep == NULL)
+			return -1;
+	}
 
 	tmp_raw_insn = strndup(name_raw_insn, 11);
-	if (tmp_raw_insn == NULL)
+	if (tmp_raw_insn == NULL) {
+		zfree(namep);
 		return -1;
+	}
 
 	remove_spaces(tmp_raw_insn);
 
-- 
2.52.0



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH V3 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines
  2026-12-02 17:20 [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name Athira Rajeev
@ 2026-12-02 17:20 ` Athira Rajeev
  2026-12-02 17:37 ` [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: Athira Rajeev @ 2026-12-02 17:20 UTC (permalink / raw)
  To: acme, jolsa, adrian.hunter, james.clark, tmricht, maddy, irogers,
	namhyung, bradley.morgan
  Cc: linux-perf-users, linuxppc-dev, atrajeev, hbathini, tejas05,
	tshah, Narendra Nalli, Vijay Puliyala, sashiko-bot

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 <narnalli@in.ibm.com>
Reported-by: Vijay Puliyala <vpuliyal@in.ibm.com>
Reported-by: sashiko-bot@kernel.org
Signed-off-by: Athira Rajeev <atrajeev@linux.ibm.com>
---
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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name
  2026-12-02 17:20 [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name Athira Rajeev
  2026-12-02 17:20 ` [PATCH V3 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines Athira Rajeev
@ 2026-12-02 17:37 ` sashiko-bot
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-02 17:37 UTC (permalink / raw)
  To: Athira Rajeev; +Cc: linux-perf-users

Sashiko has reviewed this 2-patch series and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202172048.99786-1-atrajeev@linux.ibm.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-02 17:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 17:20 [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name Athira Rajeev
2026-12-02 17:20 ` [PATCH V3 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines Athira Rajeev
2026-12-02 17:37 ` [PATCH V3 1/2] perf disasm powerpc: Don't use a string literal as the instruction name sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.