public inbox for linux-perf-users@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] perf annotate: Fix NULL pointer dereference in loongarch_call__parse
@ 2026-04-23 10:35 Jianping Liu
  2026-04-28  3:50 ` WANG Rui
  0 siblings, 1 reply; 2+ messages in thread
From: Jianping Liu @ 2026-04-23 10:35 UTC (permalink / raw)
  To: peterz, mingo, acme, namhyung, wangming01, wangrui, frankljpliu
  Cc: linux-perf-users, linux-kernel, Jianping Liu

From: Jianping Liu <frankjpliu@tencent.com>

loongarch_call__parse() dereferences the return value of strchr()
without NULL check:

  name = strchr(endptr, '<');
  name++;

When objdump output for a 'bl' (branch-and-link) instruction does not
contain a symbol name delimited by '<' and '>' (e.g., when the call
target has no associated symbol), strchr() returns NULL, and the
subsequent name++ produces a wild pointer (0x1). This leads to a
segmentation fault when strchr() is later called with this invalid
pointer:

  #0  __strchr_lasx () from /lib64/libc.so.6
  #1  loongarch_call__parse () at arch/loongarch/annotate/instructions.c:29
  #2  disasm_line__init_ins () at util/annotate.c:1205
  ...

The objdump line triggering the crash looks like:

  bl              98824   # 0x9000000000641ea0

Note the absence of "<function_name>" after the address.

Rather than aborting parse with -1 (which would clear dl->ins.ops and
lose the is_call attribute, preventing the TUI from drawing branch
arrows and navigating the call), follow the same pattern as the generic
call__parse() in util/disasm.c: when the symbol name is absent, jump to
the address-based symbol lookup so that ops->target.sym can still be
resolved via maps__find_ams() using the already-parsed target address.
This preserves is_call semantics and keeps TUI navigation working.

---
Changes from v1:
- Instead of returning -1 when the '<sym>' token is absent jump to the
  address-based symbol lookup so that maps__find_ams() can still resolve
  ops->target.sym from the already parsed target address.

- Link to v1: https://lore.kernel.org/all/20260423053102.3717015-1-frankljpliu@gmail.com/
---

Fixes: 4ca0d340ce20 ("perf annotate: Fix instruction association and parsing for LoongArch")
Signed-off-by: Jianping Liu <frankjpliu@tencent.com>
Reviewed-by: Ming Wang <wangming01@loongson.cn>
---
 tools/perf/util/annotate-arch/annotate-loongarch.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/perf/util/annotate-arch/annotate-loongarch.c b/tools/perf/util/annotate-arch/annotate-loongarch.c
index c2addca77320..a949b4392dd4 100644
--- a/tools/perf/util/annotate-arch/annotate-loongarch.c
+++ b/tools/perf/util/annotate-arch/annotate-loongarch.c
@@ -29,6 +29,9 @@ static int loongarch_call__parse(const struct arch *arch, struct ins_operands *o
 	ops->target.addr = strtoull(c, &endptr, 16);
 
 	name = strchr(endptr, '<');
+	if (name == NULL)
+		goto find_target;
+
 	name++;
 
 	if (arch->objdump.skip_functions_char &&
@@ -46,6 +49,7 @@ static int loongarch_call__parse(const struct arch *arch, struct ins_operands *o
 	if (ops->target.name == NULL)
 		return -1;
 
+find_target:
 	target = (struct addr_map_symbol) {
 		.ms = { .map = map__get(map), },
 		.addr = map__objdump_2mem(map, ops->target.addr),
-- 
2.43.7


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

* Re: [PATCH v2] perf annotate: Fix NULL pointer dereference in loongarch_call__parse
  2026-04-23 10:35 [PATCH v2] perf annotate: Fix NULL pointer dereference in loongarch_call__parse Jianping Liu
@ 2026-04-28  3:50 ` WANG Rui
  0 siblings, 0 replies; 2+ messages in thread
From: WANG Rui @ 2026-04-28  3:50 UTC (permalink / raw)
  To: Jianping Liu
  Cc: peterz, mingo, acme, namhyung, wangming01, linux-perf-users,
	linux-kernel, Jianping Liu

On Thu, Apr 23, 2026 at 6:35 PM Jianping Liu <frankljpliu@gmail.com> wrote:
>
> From: Jianping Liu <frankjpliu@tencent.com>
>
> loongarch_call__parse() dereferences the return value of strchr()
> without NULL check:
>
>   name = strchr(endptr, '<');
>   name++;
>
> When objdump output for a 'bl' (branch-and-link) instruction does not
> contain a symbol name delimited by '<' and '>' (e.g., when the call
> target has no associated symbol), strchr() returns NULL, and the
> subsequent name++ produces a wild pointer (0x1). This leads to a
> segmentation fault when strchr() is later called with this invalid
> pointer:
>
>   #0  __strchr_lasx () from /lib64/libc.so.6
>   #1  loongarch_call__parse () at arch/loongarch/annotate/instructions.c:29
>   #2  disasm_line__init_ins () at util/annotate.c:1205
>   ...
>
> The objdump line triggering the crash looks like:
>
>   bl              98824   # 0x9000000000641ea0
>
> Note the absence of "<function_name>" after the address.
>
> Rather than aborting parse with -1 (which would clear dl->ins.ops and
> lose the is_call attribute, preventing the TUI from drawing branch
> arrows and navigating the call), follow the same pattern as the generic
> call__parse() in util/disasm.c: when the symbol name is absent, jump to
> the address-based symbol lookup so that ops->target.sym can still be
> resolved via maps__find_ams() using the already-parsed target address.
> This preserves is_call semantics and keeps TUI navigation working.
>
> ---
> Changes from v1:
> - Instead of returning -1 when the '<sym>' token is absent jump to the
>   address-based symbol lookup so that maps__find_ams() can still resolve
>   ops->target.sym from the already parsed target address.
>
> - Link to v1: https://lore.kernel.org/all/20260423053102.3717015-1-frankljpliu@gmail.com/
> ---
>
> Fixes: 4ca0d340ce20 ("perf annotate: Fix instruction association and parsing for LoongArch")
> Signed-off-by: Jianping Liu <frankjpliu@tencent.com>
> Reviewed-by: Ming Wang <wangming01@loongson.cn>

Tested-by: WANG Rui <wangrui@loongson.cn>

Thanks,
Rui

> ---
>  tools/perf/util/annotate-arch/annotate-loongarch.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/tools/perf/util/annotate-arch/annotate-loongarch.c b/tools/perf/util/annotate-arch/annotate-loongarch.c
> index c2addca77320..a949b4392dd4 100644
> --- a/tools/perf/util/annotate-arch/annotate-loongarch.c
> +++ b/tools/perf/util/annotate-arch/annotate-loongarch.c
> @@ -29,6 +29,9 @@ static int loongarch_call__parse(const struct arch *arch, struct ins_operands *o
>         ops->target.addr = strtoull(c, &endptr, 16);
>
>         name = strchr(endptr, '<');
> +       if (name == NULL)
> +               goto find_target;
> +
>         name++;
>
>         if (arch->objdump.skip_functions_char &&
> @@ -46,6 +49,7 @@ static int loongarch_call__parse(const struct arch *arch, struct ins_operands *o
>         if (ops->target.name == NULL)
>                 return -1;
>
> +find_target:
>         target = (struct addr_map_symbol) {
>                 .ms = { .map = map__get(map), },
>                 .addr = map__objdump_2mem(map, ops->target.addr),
> --
> 2.43.7


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

end of thread, other threads:[~2026-04-28  3:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-23 10:35 [PATCH v2] perf annotate: Fix NULL pointer dereference in loongarch_call__parse Jianping Liu
2026-04-28  3:50 ` WANG Rui

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