Skip to contentSkip to conversation

system/physmem: Silence warning from ubsan

PATCH In review Thomas Huth 4 participants 4 messages Jul 28, 2025 – Jul 29, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 system/physmem: Silence warning from ubsan

From: Thomas Huth <[email protected]>

When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running the bios-tables-test for example:

.../system/physmem.c:3243:13: runtime error: applying non-zero offset 262144 to null pointer

    #0 0x55ac1df5fbc4 in address_space_write_rom_internal .../system/physmem.c:3243:13

The problem is that buf is indeed NULL if the function is e.g. called with type == FLUSH_CACHE. Add a check to fix the issue.

---
 system/physmem.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
system/physmem.c +3 -1
index 130c148ffb5..00333ffa7f7 100644
@@ -3240,8 +3240,10 @@ static inline MemTxResult address_space_write_rom_internal(AddressSpace *as,
             }
         }
         len -= l;
-        buf += l;
         addr += l;
+        if (buf) {
+            buf += l;
+        }
     }
     return MEMTX_OK;
 }
Signature · 1 line
2.50.1
#1 lore raw

Cc'ing Patrick & Peter for similar patch:
https://lore.kernel.org/qemu-devel/[email protected]/

Quoted 32 lines · Thomas Huth

From: Thomas Huth <[email protected]>

When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running the bios-tables-test for example:

.../system/physmem.c:3243:13: runtime error: applying non-zero offset 262144 to null pointer

     #0 0x55ac1df5fbc4 in address_space_write_rom_internal .../system/physmem.c:3243:13

The problem is that buf is indeed NULL if the function is e.g. called with type == FLUSH_CACHE. Add a check to fix the issue.

---
  system/physmem.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
system/physmem.c +3 -1
index 130c148ffb5..00333ffa7f7 100644
@@ -3240,8 +3240,10 @@ static inline MemTxResult address_space_write_rom_internal(AddressSpace *as,
              }
          }
          len -= l;
-        buf += l;
          addr += l;
+        if (buf) {
+            buf += l;
+        }
      }
      return MEMTX_OK;
  }
#2 lore raw
Quoted 2 lines · Philippe Mathieu-Daudé

Cc'ing Patrick & Peter for similar patch:
https://lore.kernel.org/qemu-devel/[email protected]/

This version seems to work.

Quoted 9 lines · Thomas Huth
Quoted 7 lines

From: Thomas Huth <[email protected]>

When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running the bios-tables-test for example:

.../system/physmem.c:3243:13: runtime error: applying non-zero offset 262144 to null pointer

     #0 0x55ac1df5fbc4 in address_space_write_rom_internal .../system/physmem.c:3243:13

I can trigger this by as simple as "./qemu-system-x86_64 -accel kvm". I queued it, thanks.

Signature · 1 line
Peter Xu
Reviewed-by on v1 1/1
#3 lore raw
In reply to Thomas Huth
Quoted 5 lines · Thomas Huth

From: Thomas Huth <[email protected]>

When compiling QEMU with --enable-ubsan there is a undefined behavior warning when running the bios-tables-test for example:

Quoted 7 lines

.../system/physmem.c:3243:13: runtime error: applying non-zero offset 262144 to null pointer

     #0 0x55ac1df5fbc4 in address_space_write_rom_internal .../system/physmem.c:3243:13

The problem is that buf is indeed NULL if the function is e.g. called with type == FLUSH_CACHE. Add a check to fix the issue.

Signature · 3 lines
Cheers,

David / dhildenb