Linux Perf Users
 help / color / mirror / Atom feed
* [PATCH V2 1/2] perf disasm powerpc: Don't use a string literal as the instruction name
@ 2026-12-02 12:43 Athira Rajeev
  2026-12-02 12:43 ` [PATCH V2 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines Athira Rajeev
  2026-12-02 12:55 ` [PATCH V2 1/2] perf disasm powerpc: Don't use a string literal as the instruction name sashiko-bot
  0 siblings, 2 replies; 4+ messages in thread
From: Athira Rajeev @ 2026-12-02 12:43 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 will make 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.

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:
V2 : This is a new update in V2 to address issue
reported by Sashiko during review. disasm_line__parse_powerpc()
sets the instruction name to string literal "" which could trigger
invalid free() at later point

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

diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index 5b9bc94be55e..a1787a9c4bfd 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -908,10 +908,17 @@ 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)
-- 
2.52.0


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

* [PATCH V2 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines
  2026-12-02 12:43 [PATCH V2 1/2] perf disasm powerpc: Don't use a string literal as the instruction name Athira Rajeev
@ 2026-12-02 12:43 ` Athira Rajeev
  2026-12-02 12:59   ` sashiko-bot
  2026-12-02 12:55 ` [PATCH V2 1/2] perf disasm powerpc: Don't use a string literal as the instruction name sashiko-bot
  1 sibling, 1 reply; 4+ messages in thread
From: Athira Rajeev @ 2026-12-02 12:43 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

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 as a valid case in
disasm_line__parse_powerpc(): skip the call to disasm_line__parse() and
set the instruction name to an empty heap-allocated string instead,
allowing parsing to continue to the next line.

Also point the operands at the (empty) end of the line, since
disasm_line__parse() is what normally sets them and the instruction
printing callbacks print them with "%s".

check_ppc_insn() names raw instructions from their opcode when the name
is empty, and so replaces the empty string allocated above.  Free it
first, to not leak it.

Reported-by: Narendra Nalli <narnalli@in.ibm.com>
Reported-by: Vijay Puliyala <vpuliyal@in.ibm.com>
Signed-off-by: Athira Rajeev <atrajeev@linux.ibm.com>
---
Changelog:
V1 -> V2:
 Addressed review comment from Sashiko.
 Set the operand value and also free the instruction name
 before picking from opcode value

 tools/perf/util/annotate-arch/annotate-powerpc.c |  5 ++++-
 tools/perf/util/disasm.c                         | 14 +++++++++++++-
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/annotate-arch/annotate-powerpc.c b/tools/perf/util/annotate-arch/annotate-powerpc.c
index 00b93296fc94..896c74a349f0 100644
--- a/tools/perf/util/annotate-arch/annotate-powerpc.c
+++ b/tools/perf/util/annotate-arch/annotate-powerpc.c
@@ -300,6 +300,7 @@ const struct ins_ops *check_ppc_insn(struct disasm_line *dl)
 		 */
 		if (!strlen(dl->ins.name)) {
 			sprintf(name_insn, "%d", opcode);
+			free((void *)dl->ins.name);
 			dl->ins.name = strdup(name_insn);
 		}
 		return &load_store_ops;
@@ -307,8 +308,10 @@ const struct ins_ops *check_ppc_insn(struct disasm_line *dl)
 		/* 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))
+			if (!strlen(dl->ins.name)) {
+				free((void *)dl->ins.name);
 				dl->ins.name = strdup(ret->name);
+			}
 			return &load_store_ops;
 		} else {
 			mem_insns_31_opcode.value = PPC_22_30(raw_insn);
diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
index a1787a9c4bfd..32e91b95ee72 100644
--- a/tools/perf/util/disasm.c
+++ b/tools/perf/util/disasm.c
@@ -908,16 +908,28 @@ 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 && name[0] != '\0') {
 		ret = disasm_line__parse(name, namep, rawp);
 	} else {
 		/*
 		 * The name is freed in disasm_line__free() and may be replaced
 		 * by check_ppc_insn(), so it must be heap allocated.
+		 *
+		 * 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;
+		if (disasm)
+			*rawp = name;	/* the '\0' at the end of dl->al.line */
 	}
 
 	tmp_raw_insn = strndup(name_raw_insn, 11);
-- 
2.52.0


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

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

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] perf disasm: memory leak of dl->ins.name in
  disasm_line__parse_powerpc()
--

commit 672ddb2e1e6cdd2923c6ba2964448e4efdf6e08b
Author: Athira Rajeev <atrajeev@linux.ibm.com>

perf disasm powerpc: Don't use a string literal as the instruction name

This commit allocates an empty string with strdup("") in
disasm_line__parse_powerpc() when the disassembler is not used, avoiding an
invalid free() of a string literal in disasm_line__free().

> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index 7c289b67b5bde..ac69ce93b0486 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -901,10 +901,17 @@ 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)

[Severity: Low]
Does this leak *namep (dl->ins.name) if strndup() fails right after
strdup("")?  Returning -1 sends the caller disasm_line__new() to
out_free_line, which frees dl->al and dl without freeing dl->ins.name.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202124337.10434-1-atrajeev@linux.ibm.com?part=1

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

* Re: [PATCH V2 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines
  2026-12-02 12:43 ` [PATCH V2 2/2] perf disasm powerpc: handle Power10 prefixed instruction continuation lines Athira Rajeev
@ 2026-12-02 12:59   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-02 12:59 UTC (permalink / raw)
  To: Athira Rajeev; +Cc: linux-perf-users

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] perf disasm powerpc: out-of-bounds read in
  disasm_line__parse_powerpc()
- [Medium] perf annotate powerpc: check_ppc_insn() misclassifies Power10
  continuation lines
- [Low] perf annotate powerpc: missing stdlib.h for free() and unchecked
  strdup() in check_ppc_insn()
- [Low] perf disasm powerpc: missing Fixes: trailer on bug-fix commit
--

commit 71958fae4473f320e48604fe9bb71c4d558f1d24
Author: Athira Rajeev <atrajeev@linux.ibm.com>

> perf disasm powerpc: handle Power10 prefixed instruction continuation lines
>
> 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 as a valid case in
> disasm_line__parse_powerpc(): skip the call to disasm_line__parse() and
> set the instruction name to an empty heap-allocated string instead,
> allowing parsing to continue to the next line.
>
> Also point the operands at the (empty) end of the line, since
> disasm_line__parse() is what normally sets them and the instruction
> printing callbacks print them with "%s".
>
> check_ppc_insn() names raw instructions from their opcode when the name
> is empty, and so replaces the empty string allocated above.  Free it
> first, to not leak it.
>
> Reported-by: Narendra Nalli <narnalli@in.ibm.com>
> Reported-by: Vijay Puliyala <vpuliyal@in.ibm.com>
> Signed-off-by: Athira Rajeev <atrajeev@linux.ibm.com>

[Severity: Low]
Should this bug fix include a Fixes: trailer for the commit that started
propagating the disasm_line__parse() error on empty continuation lines:

  Fixes: 4c3f09e35ca9 ("perf annotate: Return errors from disasm_line__parse_powerpc()")

> diff --git a/tools/perf/util/annotate-arch/annotate-powerpc.c b/tools/perf/util/annotate-arch/annotate-powerpc.c
> index 00b93296fc945..896c74a349f06 100644
> --- a/tools/perf/util/annotate-arch/annotate-powerpc.c
> +++ b/tools/perf/util/annotate-arch/annotate-powerpc.c
> @@ -300,6 +300,7 @@ const struct ins_ops *check_ppc_insn(struct disasm_line *dl)
>  		 */
>  		if (!strlen(dl->ins.name)) {
>  			sprintf(name_insn, "%d", opcode);
> +			free((void *)dl->ins.name);
>  			dl->ins.name = strdup(name_insn);
>  		}
>  		return &load_store_ops;
> @@ -307,8 +308,10 @@ const struct ins_ops *check_ppc_insn(struct disasm_line *dl)
>  		/* 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))
> +			if (!strlen(dl->ins.name)) {
> +				free((void *)dl->ins.name);
>  				dl->ins.name = strdup(ret->name);
> +			}
>  			return &load_store_ops;
>  		} else {
>  			mem_insns_31_opcode.value = PPC_22_30(raw_insn);

[Severity: Medium]
Does check_ppc_insn() misclassify Power10 prefixed continuation lines as
standalone instructions and overwrite their empty dl->ins.name?

After disasm_line__parse_powerpc() sets dl->ins.name = strdup("") and parses
the 32-bit suffix word into dl->raw.raw_insn, disasm_line__new() calls
disasm_line__init_ins() -> check_ppc_insn(dl).  Because check_ppc_insn()
cannot tell an objdump continuation line apart from a raw binary instruction
in symbol__disassemble_raw(), any suffix word whose primary opcode is 32..63
(such as pld or pstd) has its empty dl->ins.name replaced with the decimal
opcode string and gets load_store_ops attached.  Suffix words matching
opcode 31 or arithmetic_two_ops (such as the pla example in the commit
message, whose suffix word 0x3be08e20 has primary opcode 14 for addi) also
get load_store_ops or arithmetic_ops attached, which corrupts both the
rendered annotation output and update_insn_state_powerpc() register tracking.

[Severity: Low]
Does annotate-powerpc.c need to include <stdlib.h> for free() and check the
strdup() return values here?

Perf builds against both glibc and musl, and annotate-powerpc.c only includes
<string.h> at the top of the file without <stdlib.h>.  Also, if strdup()
returns NULL after dl->ins.name is freed, later callers such as
annotation__max_ins_name() and annotate_data_stat() pass dl->ins.name to
strlen() or strcmp() without a NULL check.

> diff --git a/tools/perf/util/disasm.c b/tools/perf/util/disasm.c
> index ac69ce93b0486..233683513699e 100644
> --- a/tools/perf/util/disasm.c
> +++ b/tools/perf/util/disasm.c
> @@ -901,16 +901,28 @@ 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 && name[0] != '\0') {
>  		ret = disasm_line__parse(name, namep, rawp);
>  	} else {
>  		/*
>  		 * The name is freed in disasm_line__free() and may be replaced
>  		 * by check_ppc_insn(), so it must be heap allocated.
> +		 *
> +		 * 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;
> +		if (disasm)
> +			*rawp = name;	/* the '\0' at the end of dl->al.line */
>  	}
>  
>  	tmp_raw_insn = strndup(name_raw_insn, 11);

[Severity: Medium]
Can evaluating name[0] and storing *rawp = name read and point past the end
of dl->al.line when the input line is shorter than RAW_BYTES (11)?

At the top of disasm_line__parse_powerpc():

tools/perf/util/disasm.c:disasm_line__parse_powerpc() {
	char *tmp_raw_insn, *name_raw_insn = skip_spaces(line);
	char *name = skip_spaces(name_raw_insn + RAW_BYTES);

dl->al.line is allocated with strdup(args->line).  In
symbol__disassemble_raw() and symbol__disassemble_capstone_powerpc()
(disasm == 0), args->line is formatted with sprintf(args->line, "%x",
line[i]), which is at most 8 hex characters, so skip_spaces(name_raw_insn +
RAW_BYTES) reads past the heap buffer on every instruction.

When disasm == 1, symbol__disassemble() tries LLVM and Capstone before
objdump, and neither emits an 11-byte raw hex prefix in args->line.  For a
short instruction line such as "blr" where strlen(name_raw_insn) < RAW_BYTES,
name_raw_insn + RAW_BYTES points past the end of dl->al.line, and if that
out-of-bounds byte happens to be '\0' (or for an 11-character line where
name[0] == '\0'), this new else branch accepts the line as a continuation
line instead of failing and falling back to objdump, and stores the
out-of-bounds pointer in *rawp.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202124337.10434-1-atrajeev@linux.ibm.com?part=2

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

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

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox