[PATCH 1/2] lib: sbi: fix uninitialized return value on trap in sbi_get_insn()
Ilya Mamay
mmamayka01 at gmail.com
Tue Nov 24 04:48:59 PST 2026
In sbi_get_insn(), the `insn` variable is initialized to 0, but its
inline assembly output constraint is specified as write-only ("=&r").
If one of the `lhu` instructions causes an exception,
`sbi_hart_expected_trap` redirects execution directly to the `2:`
label, skipping the writes to `insn`. As a result, `sbi_get_insn()`
returns an uninitialized garbage value from a register or the stack.
Change the constraint for `insn` from "=&r" to "+&r" so the compiler
preserves the initial zero value across the assembly block.
Signed-off-by: Ilya Mamay <mmamayka01 at gmail.com>
---
lib/sbi/sbi_unpriv.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/sbi/sbi_unpriv.c b/lib/sbi/sbi_unpriv.c
index 1550d111..3fd9d9ff 100644
--- a/lib/sbi/sbi_unpriv.c
+++ b/lib/sbi/sbi_unpriv.c
@@ -227,7 +227,7 @@ ulong sbi_get_insn(ulong mepc, struct sbi_trap_info *trap)
"csrw " STR(CSR_MTVEC) ", %[mtvec]"
: [mstatus] "+&r"(mstatus), [mtvec] "+&r"(mtvec),
[tinfo] "+&r"(tinfo), [ttmp] "+&r"(ttmp),
- [insn] "=&r"(insn)
+ [insn] "+&r"(insn)
: [mprv] "r"(MSTATUS_MPRV | MSTATUS_MXR),
[taddr] "r"((ulong)trap), [addr] "r"(mepc)
: "memory");
--
2.43.0
More information about the opensbi
mailing list