hw/intc/sifive_clint: Fix overflow in sifive_clint_write_timecmp()
PATCH v2 1/1 hw/intc/sifive_clint: Fix overflow in sifive_clint_write_timecmp()
next is an uint64_t value, but timer_mod takes an int64_t. This resulted in high values such as UINT64_MAX being converted to -1, which caused an immediate timer interrupt.
By limiting next to INT64_MAX no overflow will happen while the timer will still be effectively set to "infinitely" far in the future.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/493
---
I wrongly used MAX instead of MIN. I've amended the patch.
hw/intc/sifive_clint.c | 2 ++ 1 file changed, 2 insertions(+)
hw/intc/sifive_clint.c +2 -0
@@ -61,6 +61,8 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value,/* back to ns (note args switched in muldiv64) */ next =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) +muldiv64 (diff,NANOSECONDS_PER_SECOND , timebase_freq); +/* ensure next does not overflow, as timer_mod takes a signed value */ + next =MIN (next,INT64_MAX );timer_mod (cpu->env.timer, next); }
Signature · 1 line
2.20.1
Quoted 10 lines · David Hoppenbrouwers
nextis anuint64_tvalue, buttimer_modtakes anint64_t. This resulted in high values such asUINT64_MAXbeing converted to-1, which caused an immediate timer interrupt.By limiting
nexttoINT64_MAXno overflow will happen while the timer will still be effectively set to "infinitely" far in the future.Resolves: https://gitlab.com/qemu-project/qemu/-/issues/493
Signed-off-by David Hoppenbrouwers <[email protected]>
Alistair
Quoted 23 lines
---
I wrongly usedMAXinstead ofMIN. I've amended the patch.hw/intc/sifive_clint.c | 2 ++ 1 file changed, 2 insertions(+)hw/intc/sifive_clint.c +2
-1@@ -61,6 +61,8 @@ static void sifive_clint_write_timecmp(RISCVCPU *cpu, uint64_t value,/* back to ns (note args switched in muldiv64) */ next =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ) +muldiv64 (diff,NANOSECONDS_PER_SECOND , timebase_freq); +/* ensure next does not overflow, as timer_mod takes a signed value */ + next =MIN (next,INT64_MAX );timer_mod (cpu->env.timer, next); } --2.20.1
Quoted 8 lines · David Hoppenbrouwers
nextis anuint64_tvalue, buttimer_modtakes anint64_t. This resulted in high values such asUINT64_MAXbeing converted to-1, which caused an immediate timer interrupt.By limiting
nexttoINT64_MAXno overflow will happen while the timer will still be effectively set to "infinitely" far in the future.
Quoted 8 lines
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/493
Signed-off-by David Hoppenbrouwers <[email protected]>---
I wrongly usedMAXinstead ofMIN. I've amended the patch.hw/intc/sifive_clint.c | 2 ++ 1 file changed, 2 insertions(+)
Alistair Francis
AppliedQuoted 15 lines · Bin Meng
Quoted 13 lines · David Hoppenbrouwers
nextis anuint64_tvalue, buttimer_modtakes anint64_t. This resulted in high values such asUINT64_MAXbeing converted to-1, which caused an immediate timer interrupt.By limiting
nexttoINT64_MAXno overflow will happen while the timer will still be effectively set to "infinitely" far in the future.Resolves: https://gitlab.com/qemu-project/qemu/-/issues/493
Signed-off-by David Hoppenbrouwers <[email protected]>---
I wrongly usedMAXinstead ofMIN. I've amended the patch.
Quoted 5 lines
Quoted 3 lines
hw/intc/sifive_clint.c | 2 ++ 1 file changed, 2 insertions(+)Reviewed-by Bin Meng <[email protected]>
Thanks!
Applied to riscv-to-apply.next
Alistair