[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PULL 08/13] target/riscv: Avoid bad shift in riscv_cpu_do_interrupt
From: |
Alistair Francis |
Subject: |
Re: [PULL 08/13] target/riscv: Avoid bad shift in riscv_cpu_do_interrupt() |
Date: |
Tue, 3 Dec 2024 22:25:59 +0900 |
On Tue, Dec 3, 2024 at 8:34 PM Philippe Mathieu-Daudé <philmd@linaro.org> wrote:
>
> From: Peter Maydell <peter.maydell@linaro.org>
>
> In riscv_cpu_do_interrupt() we use the 'cause' value we got out of
> cs->exception as a shift value. However this value can be larger
> than 31, which means that "1 << cause" is undefined behaviour,
> because we do the shift on an 'int' type.
>
> This causes the undefined behaviour sanitizer to complain
> on one of the check-tcg tests:
>
> $ UBSAN_OPTIONS=print_stacktrace=1:abort_on_error=1:halt_on_error=1
> ./build/clang/qemu-system-riscv64 -M virt -semihosting -display none -device
> loader,file=build/clang/tests/tcg/riscv64-softmmu/issue1060
> ../../target/riscv/cpu_helper.c:1805:38: runtime error: shift exponent 63 is
> too large for 32-bit type 'int'
> #0 0x55f2dc026703 in riscv_cpu_do_interrupt
> /mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/clang/../../target/riscv/cpu_helper.c:1805:38
> #1 0x55f2dc3d170e in cpu_handle_exception
> /mnt/nvmedisk/linaro/qemu-from-laptop/qemu/build/clang/../../accel/tcg/cpu-exec.c:752:9
>
> In this case cause is RISCV_EXCP_SEMIHOST, which is 0x3f.
>
> Use 1ULL instead to ensure that the shift is in range.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> Fixes: 1697837ed9 ("target/riscv: Add M-mode virtual interrupt and IRQ
> filtering support.")
> Fixes: 40336d5b1d ("target/riscv: Add HS-mode virtual interrupt and IRQ
> filtering support.")
> Reviewed-by: Daniel Henrique Barboza <dbarboza@ventanamicro.com>
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
> Reviewed-by: Alistair Francis <alistair.francis@wdc.com>
> Message-ID: <20241128103831.3452572-1-peter.maydell@linaro.org>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Thanks! I was just about to prep this, thanks for beating me to it :)
qemu-stable@nongnu.org this should be backported where it applies
Alistair
> ---
> target/riscv/cpu_helper.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/target/riscv/cpu_helper.c b/target/riscv/cpu_helper.c
> index 0a3ead69eab..45806f5ab0f 100644
> --- a/target/riscv/cpu_helper.c
> +++ b/target/riscv/cpu_helper.c
> @@ -1802,10 +1802,10 @@ void riscv_cpu_do_interrupt(CPUState *cs)
> bool async = !!(cs->exception_index & RISCV_EXCP_INT_FLAG);
> target_ulong cause = cs->exception_index & RISCV_EXCP_INT_MASK;
> uint64_t deleg = async ? env->mideleg : env->medeleg;
> - bool s_injected = env->mvip & (1 << cause) & env->mvien &&
> - !(env->mip & (1 << cause));
> - bool vs_injected = env->hvip & (1 << cause) & env->hvien &&
> - !(env->mip & (1 << cause));
> + bool s_injected = env->mvip & (1ULL << cause) & env->mvien &&
> + !(env->mip & (1ULL << cause));
> + bool vs_injected = env->hvip & (1ULL << cause) & env->hvien &&
> + !(env->mip & (1ULL << cause));
> target_ulong tval = 0;
> target_ulong tinst = 0;
> target_ulong htval = 0;
> --
> 2.45.2
>
>
- [PULL 00/13] Misc fixes for 2024-12-03, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 01/13] hw/openrisc: Fixed undercounting of TTCR in continuous mode, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 02/13] hw/openrisc/openrisc_sim: keep serial@90000000 as default, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 03/13] ui/cocoa: Temporarily ignore annoying deprecated declaration warnings, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 04/13] MAINTAINERS: add myself as the maintainer for LoongArch VirtMachine, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 05/13] meson: Add missing SDL dependency to system/main.c, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 06/13] MAINTAINERS: update email addr for Brian Cain, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 07/13] hw/core/machine: diagnose wrapping of maxmem, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 08/13] target/riscv: Avoid bad shift in riscv_cpu_do_interrupt(), Philippe Mathieu-Daudé, 2024/12/03
- Re: [PULL 08/13] target/riscv: Avoid bad shift in riscv_cpu_do_interrupt(),
Alistair Francis <=
- [PULL 09/13] hw/display/vga: Do not reset 'big_endian_fb' in vga_common_reset(), Philippe Mathieu-Daudé, 2024/12/03
- [PULL 10/13] hw/virtio: fix crash in processing balloon stats, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 11/13] tests/qtest: drop 'fuzz-' prefix from virtio-balloon test, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 12/13] tests/qtest: add test for querying balloon guest stats, Philippe Mathieu-Daudé, 2024/12/03
- [PULL 13/13] system: Select HVF by default when no other accelerator is available, Philippe Mathieu-Daudé, 2024/12/03
- Re: [PULL 00/13] Misc fixes for 2024-12-03, Peter Maydell, 2024/12/03