lib: sbi: fix undefined behavior in signed shifts in sbi_hart.c, sbi_ecall_interface.h
PATCH 1/1 lib: sbi: fix undefined behavior in signed shifts in sbi_hart.c, sbi_ecall_interface.h
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.
--- include/sbi/sbi_ecall_interface.h | 2 +- lib/sbi/sbi_hart.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/sbi/sbi_ecall_interface.h +1 -1
@@ -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)
lib/sbi/sbi_hart.c +1 -1
@@ -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,
Signature · 7 lines
2.53.0 -- opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi
Quoted 10 lines · Marcos Oduardo
UBSan detected undefined behavior in
sbi_hart.candsbi_fwft.c(in the case ofsbi_fwft.c, the bug comes from a macro call defined atsbi_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 <[email protected]>
The patch subject can be a bit shorter. I have taken care at the time of merging.
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 39 lines
--- include/sbi/sbi_ecall_interface.h | 2 +- lib/sbi/sbi_hart.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)include/sbi/sbi_ecall_interface.h +1
-1@@ -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)lib/sbi/sbi_hart.c +1
-2@@ -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
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Signature · 3 lines
opensbi mailing list [email protected] http://lists.infradead.org/mailman/listinfo/opensbi