From: Marcos Oduardo <marcos.oduardo@gmail.com>
To: opensbi@lists.infradead.org
Cc: Marcos Oduardo <marcos.oduardo@gmail.com>
Subject: [PATCH] lib: sbi: fix undefined behavior in signed shifts in sbi_hart.c, sbi_ecall_interface.h
Date: Mon, 23 Feb 2026 01:11:59 +0100 [thread overview]
Message-ID: <20260223001202.284612-1-marcos.oduardo@gmail.com> (raw)
UBSan detected undefined behavior in sbi_hart.c and sbi_fwft.c (in the case
of sbi_fwft.c, the bug comes from a macro call defined at sbi_ecall_interface.h)
caused by shifting a signed integer into the sign bit (1 << 31)
This can be fixed by using the 1UL literal, ensuring defined arithmetic.
Please let me know if there’s any other most suitable solution for this bug.
Signed-off-by: Marcos Oduardo <marcos.oduardo@gmail.com>
---
include/sbi/sbi_ecall_interface.h | 2 +-
lib/sbi/sbi_hart.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/sbi/sbi_ecall_interface.h b/include/sbi/sbi_ecall_interface.h
index 76624e3f..9a776f79 100644
--- a/include/sbi/sbi_ecall_interface.h
+++ b/include/sbi/sbi_ecall_interface.h
@@ -144,7 +144,7 @@ enum sbi_fwft_feature_t {
SBI_FWFT_GLOBAL_PLATFORM_END = 0xffffffff,
};
-#define SBI_FWFT_GLOBAL_FEATURE_BIT (1 << 31)
+#define SBI_FWFT_GLOBAL_FEATURE_BIT (1UL << 31)
#define SBI_FWFT_PLATFORM_FEATURE_BIT (1 << 30)
#define SBI_FWFT_SET_FLAG_LOCK (1 << 0)
diff --git a/lib/sbi/sbi_hart.c b/lib/sbi/sbi_hart.c
index 60e95bca..99e13990 100644
--- a/lib/sbi/sbi_hart.c
+++ b/lib/sbi/sbi_hart.c
@@ -70,7 +70,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
*/
mhpmevent_init_val |= (MHPMEVENT_OF | MHPMEVENT_MINH);
for (cidx = 0; cidx <= 28; cidx++) {
- if (!(mhpm_mask & 1 << (cidx + 3)))
+ if (!(mhpm_mask & 1UL << (cidx + 3)))
continue;
#if __riscv_xlen == 32
csr_write_num(CSR_MHPMEVENT3 + cidx,
--
2.53.0
--
opensbi mailing list
opensbi@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
next reply other threads:[~2026-02-23 0:11 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-02-23 0:11 Marcos Oduardo [this message]
2026-03-09 8:59 ` [PATCH] lib: sbi: fix undefined behavior in signed shifts in sbi_hart.c, sbi_ecall_interface.h Anup Patel
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260223001202.284612-1-marcos.oduardo@gmail.com \
--to=marcos.oduardo@gmail.com \
--cc=opensbi@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox