qemu-trivial.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hw/mips/malta: Silence warning from ubsan
@ 2025-07-28 11:51 Thomas Huth
  2025-07-28 13:25 ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Huth @ 2025-07-28 11:51 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé, qemu-devel
  Cc: Aurelien Jarno, Jiaxun Yang, qemu-trivial

From: Thomas Huth <thuth@redhat.com>

When compiling QEMU with --enable-ubsan there is a undefined behavior
warning when using the malta machine:

 hw/mips/malta.c:1200:32: runtime error: addition of unsigned offset
  to 0x7fb620600000 overflowed to 0x7fb6205fffff
 SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior hw/mips/malta.c:1200:32

To fix the issue, check the bios_size whether we really loaded the
firmware before trying to byte-swap the instructions here.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 hw/mips/malta.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/mips/malta.c b/hw/mips/malta.c
index cbdbb210568..47dd4016cfd 100644
--- a/hw/mips/malta.c
+++ b/hw/mips/malta.c
@@ -1190,7 +1190,7 @@ void mips_malta_init(MachineState *machine)
          * In little endian mode the 32bit words in the bios are swapped,
          * a neat trick which allows bi-endian firmware.
          */
-        if (!TARGET_BIG_ENDIAN) {
+        if (!TARGET_BIG_ENDIAN && bios_size > 0) {
             uint32_t *end, *addr;
             const size_t swapsize = MIN(bios_size, 0x3e0000);
             addr = rom_ptr(FLASH_ADDRESS, swapsize);
-- 
2.50.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] hw/mips/malta: Silence warning from ubsan
  2025-07-28 11:51 [PATCH] hw/mips/malta: Silence warning from ubsan Thomas Huth
@ 2025-07-28 13:25 ` Philippe Mathieu-Daudé
  2025-07-28 15:40   ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 3+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-07-28 13:25 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel; +Cc: Aurelien Jarno, Jiaxun Yang, qemu-trivial

Hi Thomas,

On 28/7/25 13:51, Thomas Huth wrote:
> From: Thomas Huth <thuth@redhat.com>
> 
> When compiling QEMU with --enable-ubsan there is a undefined behavior
> warning when using the malta machine:
> 
>   hw/mips/malta.c:1200:32: runtime error: addition of unsigned offset
>    to 0x7fb620600000 overflowed to 0x7fb6205fffff
>   SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior hw/mips/malta.c:1200:32
> 
> To fix the issue, check the bios_size whether we really loaded the
> firmware before trying to byte-swap the instructions here.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---
>   hw/mips/malta.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/hw/mips/malta.c b/hw/mips/malta.c
> index cbdbb210568..47dd4016cfd 100644
> --- a/hw/mips/malta.c
> +++ b/hw/mips/malta.c
> @@ -1190,7 +1190,7 @@ void mips_malta_init(MachineState *machine)
>            * In little endian mode the 32bit words in the bios are swapped,
>            * a neat trick which allows bi-endian firmware.
>            */
> -        if (!TARGET_BIG_ENDIAN) {
> +        if (!TARGET_BIG_ENDIAN && bios_size > 0) {
>               uint32_t *end, *addr;
>               const size_t swapsize = MIN(bios_size, 0x3e0000);
>               addr = rom_ptr(FLASH_ADDRESS, swapsize);

What about:

-- >8 --
@@ -1180,7 +1180,7 @@ void mips_malta_init(MachineState *machine)
              } else {
                  bios_size = -1;
              }
-            if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
+            if ((bios_size <= 0 || bios_size > BIOS_SIZE) &&
                  machine->firmware && !qtest_enabled()) {
                  error_report("Could not load MIPS bios '%s'", 
machine->firmware);
                  exit(1);
---

?


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] hw/mips/malta: Silence warning from ubsan
  2025-07-28 13:25 ` Philippe Mathieu-Daudé
@ 2025-07-28 15:40   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 3+ messages in thread
From: Philippe Mathieu-Daudé @ 2025-07-28 15:40 UTC (permalink / raw)
  To: Thomas Huth, qemu-devel; +Cc: Aurelien Jarno, Jiaxun Yang, qemu-trivial

On 28/7/25 15:25, Philippe Mathieu-Daudé wrote:
> Hi Thomas,
> 
> On 28/7/25 13:51, Thomas Huth wrote:
>> From: Thomas Huth <thuth@redhat.com>
>>
>> When compiling QEMU with --enable-ubsan there is a undefined behavior
>> warning when using the malta machine:
>>
>>   hw/mips/malta.c:1200:32: runtime error: addition of unsigned offset
>>    to 0x7fb620600000 overflowed to 0x7fb6205fffff
>>   SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior hw/mips/ 
>> malta.c:1200:32
>>
>> To fix the issue, check the bios_size whether we really loaded the
>> firmware before trying to byte-swap the instructions here.
>>
>> Signed-off-by: Thomas Huth <thuth@redhat.com>
>> ---
>>   hw/mips/malta.c | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/hw/mips/malta.c b/hw/mips/malta.c
>> index cbdbb210568..47dd4016cfd 100644
>> --- a/hw/mips/malta.c
>> +++ b/hw/mips/malta.c
>> @@ -1190,7 +1190,7 @@ void mips_malta_init(MachineState *machine)
>>            * In little endian mode the 32bit words in the bios are 
>> swapped,
>>            * a neat trick which allows bi-endian firmware.
>>            */
>> -        if (!TARGET_BIG_ENDIAN) {
>> +        if (!TARGET_BIG_ENDIAN && bios_size > 0) {
>>               uint32_t *end, *addr;
>>               const size_t swapsize = MIN(bios_size, 0x3e0000);
>>               addr = rom_ptr(FLASH_ADDRESS, swapsize);

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

and queued, thanks!

> 
> What about:
> 
> -- >8 --
> @@ -1180,7 +1180,7 @@ void mips_malta_init(MachineState *machine)
>               } else {
>                   bios_size = -1;
>               }
> -            if ((bios_size < 0 || bios_size > BIOS_SIZE) &&
> +            if ((bios_size <= 0 || bios_size > BIOS_SIZE) &&
>                   machine->firmware && !qtest_enabled()) {
>                   error_report("Could not load MIPS bios '%s'", machine- 
>  >firmware);
>                   exit(1);
> ---
> 
> ?



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-07-28 15:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-28 11:51 [PATCH] hw/mips/malta: Silence warning from ubsan Thomas Huth
2025-07-28 13:25 ` Philippe Mathieu-Daudé
2025-07-28 15:40   ` Philippe Mathieu-Daudé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).