[PATCH 2/3] objtool/LoongArch: Move the cfi->regs access after the NULL check
From: Leizhen Zhang
Date: Mon Nov 09 2026 - 19:15:03 EST
init_orc_entry() takes &cfi->regs[CFI_FP] and &cfi->regs[CFI_RA] before
it checks whether cfi is NULL, although a NULL cfi is an expected case
(unreachable nops/traps and STACK_FRAME_NON_STANDARD functions, as the
comment right below says).
Forming a member address through a NULL pointer is undefined behaviour,
and it lets the compiler assume cfi != NULL and drop the check that
follows. The x86 copy of this function has the same problem, and UBSan
reports it there on every defconfig build.
Take the addresses only after the NULL check.
The report was found with UBSan, and the analysis and fix were
prepared with the help of an AI coding assistant; I reviewed and
tested the result.
Fixes: 3c7266cd7bc5 ("objtool/LoongArch: Enable orc to be built")
Signed-off-by: Leizhen Zhang <lzsx618@xxxxxxxxx>
---
tools/objtool/arch/loongarch/orc.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/tools/objtool/arch/loongarch/orc.c b/tools/objtool/arch/loongarch/orc.c
index ffd3a3c858ae..960b59d5e566 100644
--- a/tools/objtool/arch/loongarch/orc.c
+++ b/tools/objtool/arch/loongarch/orc.c
@@ -8,8 +8,7 @@
int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi, struct instruction *insn)
{
- struct cfi_reg *fp = &cfi->regs[CFI_FP];
- struct cfi_reg *ra = &cfi->regs[CFI_RA];
+ struct cfi_reg *fp, *ra;
memset(orc, 0, sizeof(*orc));
@@ -23,6 +22,9 @@ int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi, struct instruct
return 0;
}
+ fp = &cfi->regs[CFI_FP];
+ ra = &cfi->regs[CFI_RA];
+
switch (cfi->type) {
case UNWIND_HINT_TYPE_UNDEFINED:
orc->type = ORC_TYPE_UNDEFINED;
--
2.34.1