perf annotate: Fix NULL pointer dereference in loongarch_call__parse
PATCH 1/1 perf annotate: Fix NULL pointer dereference in loongarch_call__parse
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.
Add a NULL check for the return value of strchr() before dereferencing it, consistent with s390_call__parse() which handles the same case correctly.
--- tools/perf/util/annotate-arch/annotate-loongarch.c | 3 +++ 1 file changed, 3 insertions(+)
tools/perf/util/annotate-arch/annotate-loongarch.c +3 -0
@@ -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 ) +return -1 ; + name++; if (arch->objdump.skip_functions_char &&
Signature · 1 line
2.43.7
[email protected]
AI reviewSashiko AI review found 1 potential issue(s): - [Medium] The fix correctly prevents a NULL pointer dereference, but improperly aborts instruction parsing by returning `-1`. This strips the `is_call`…
Sashiko AI review found 1 potential issue(s):
- [Medium] The fix correctly prevents a NULL pointer dereference, but improperly aborts instruction parsing by returning -1. This strips the is_call attribute and degrades perf annotate functionality, whereas it should bypass name extraction and resolve the target using the already extracted address. --
commit 807b803f1f78132524dc6117283161821fab508a
Author: Jianping Liu <[email protected]>
perf annotate: Fix NULL pointer dereference in loongarch_call__parse
This commit prevents a segmentation fault in loongarch_call__parse() by adding a NULL check for the return value of strchr() when parsing objdump output. It handles cases where a branch-and-link instruction lacks a symbol name.
Quoted 3 lines
Add a NULL check for the return value of
strchr()before dereferencing it, consistent withs390_call__parse()which handles the same case correctly.
Does s390_call__parse() actually handle this correctly without regressions?
Returning -1 causes disasm_line__init_ins() to receive a negative return value and set dl->ins.ops = NULL. This makes ins__is_call() evaluate to false, meaning the instruction completely loses its call semantics in the TUI.
tools/perf/util/annotate-arch/annotate-loongarch.c static int loongarch_call__parse(const struct arch *arch, struct ins_operands *o · line 35
Quoted 11 lines
tools/perf/util/annotate-arch/annotate-loongarch.c +3
-0@@ -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 ) +return -1 ; + name++;
Could this code bypass the name extraction and proceed directly to symbol resolution using the already extracted target address?
Since ops->target.addr is successfully parsed right above this check, skipping the name parsing and returning 0 instead of aborting with -1 would preserve the is_call attribute. This would allow the TUI to still draw branch arrows and navigate the call.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1