Skip to contentSkip to conversation

hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions

PATCH In review Николай Зорин 4 participants 5 messages Sep 15 – Sep 18

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions

SWSA#34e3af79-c6c2-4ad2-986f-313c1ce28e5a

In tcx_blit_writel(), memory copying occurs within the same video RAM buffers (vram and vram24) using source and destination offsets that can potentially overlap during blit operations.

Using memcpy() on overlapping memory regions triggers undefined behavior according to the C standard. Depending on compiler optimizations, this can lead to graphical artifacts, memory corruption, or unexpected behavior in the guest system.

Fix this by replacing memcpy() with memmove(), which explicitly guarantees safe and correct behavior when memory regions overlap.

---
 hw/display/tcx.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/tcx.c +0 -0
index 2cfc1e8f..fb48a199 100644
@@ -587,9 +587,9 @@ static void tcx_blit_writel(void *opaque, hwaddr addr,

                 }
             }
         } else {
-            memcpy(&s->vram[addr], &s->vram[adsr], len);
+            memmove(&s->vram[addr], &s->vram[adsr], len); if (s->depth == 24) {
-                memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4); +                memmove(&s->vram24[addr], &s->vram24[adsr], len * 4); }
         }
         tcx_set_dirty(s, addr, len);

Signature · 1 line
2.43.0
#1 lore raw
Quoted 1 line · Николай Зорин

SWSA#34e3af79-c6c2-4ad2-986f-313c1ce28e5a

^^^^^^^^^ what is this ?

I'm guessing it is a tag you're using for tracking issues, but if so, it should be removed before sending commits upstream.

Quoted 38 lines

In tcx_blit_writel(), memory copying occurs within the same video RAM buffers (vram and vram24) using source and destination offsets that can potentially overlap during blit operations.

Using memcpy() on overlapping memory regions triggers undefined behavior according to the C standard. Depending on compiler optimizations, this can lead to graphical artifacts, memory corruption, or unexpected behavior in the guest system.

Fix this by replacing memcpy() with memmove(), which explicitly guarantees safe and correct behavior when memory regions overlap.

---
 hw/display/tcx.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/tcx.c +2 -2
index 2cfc1e8f..fb48a199 100644
@@ -587,9 +587,9 @@ static void tcx_blit_writel(void *opaque, hwaddr addr,
                  }
              }
          } else {
-            memcpy(&s->vram[addr], &s->vram[adsr], len);
+            memmove(&s->vram[addr], &s->vram[adsr], len);
              if (s->depth == 24) {
-                memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4);
+                memmove(&s->vram24[addr], &s->vram24[adsr], len * 4);
              }
          }
          tcx_set_dirty(s, addr, len);
Signature · 1 line
2.43.0

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
#2 lore raw

This is our tag. Okay, in the future I'll remove the "garbage" (anything that doesn't relate to the original).

15.09.2026 19:31, Daniel P. Berrangé пишет:

Quoted 2 lines

I'm guessing it is a tag you're using for tracking issues, but if so, it should be removed before sending commits upstream.

#3 lore raw
In reply to Николай Зорин

thanks for the patch! some questions:

Quoted 1 line · Николай Зорин

SWSA#34e3af79-c6c2-4ad2-986f-313c1ce28e5a

what is this?

Quoted 8 lines

In tcx_blit_writel(), memory copying occurs within the same video RAM buffers (vram and vram24) using source and destination offsets that can potentially overlap during blit operations.

Using memcpy() on overlapping memory regions triggers undefined behavior according to the C standard. Depending on compiler optimizations, this can lead to graphical artifacts, memory corruption, or unexpected behavior in the guest system.

was this actually observed? in which configuration? which guest? why does the guest do such overlapping blit write?

it also seems unlikely to depend on optimizations - more likely on libc.

Quoted 26 lines

Fix this by replacing memcpy() with memmove(), which explicitly guarantees safe and correct behavior when memory regions overlap.

---
 hw/display/tcx.c | 4 ++--

 1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/tcx.c +2 -2
index 2cfc1e8f..fb48a199 100644
@@ -587,9 +587,9 @@ static void tcx_blit_writel(void *opaque, hwaddr addr,
                  }
              }
          } else {
-            memcpy(&s->vram[addr], &s->vram[adsr], len);
+            memmove(&s->vram[addr], &s->vram[adsr], len);
              if (s->depth == 24) {
-                memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4);
+                memmove(&s->vram24[addr], &s->vram24[adsr], len * 4);
              }
          }
          tcx_set_dirty(s, addr, len);
Signature · 1 line
2.43.0
#4 lore raw
In reply to Николай Зорин
Quoted 1 line · Николай Зорин

SWSA#34e3af79-c6c2-4ad2-986f-313c1ce28e5a

I guess this is from something else?

Quoted 8 lines

In tcx_blit_writel(), memory copying occurs within the same video RAM buffers (vram and vram24) using source and destination offsets that can potentially overlap during blit operations.

Using memcpy() on overlapping memory regions triggers undefined behavior according to the C standard. Depending on compiler optimizations, this can lead to graphical artifacts, memory corruption, or unexpected behavior in the guest system.

I would hope that this is just graphical artifacts given that this is just video RAM...

Quoted 2 lines

Fix this by replacing memcpy() with memmove(), which explicitly guarantees safe and correct behavior when memory regions overlap.

Do you have a specific test case? I can see that this would make sense in some cases, e.g. sideways scrolling.

Quoted 11 lines
---
  hw/display/tcx.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
hw/display/tcx.c +0 -0
index 2cfc1e8f..fb48a199 100644
@@ -587,9 +587,9 @@ static void tcx_blit_writel(void *opaque, hwaddr addr,
                  }

hw/display/tcx.c static void tcx_blit_writel(void *opaque, hwaddr addr, · line 595

Quoted 10 lines
hw/display/tcx.c +2 -2
              }
          } else {
-            memcpy(&s->vram[addr], &s->vram[adsr], len);
+            memmove(&s->vram[addr], &s->vram[adsr], len);
              if (s->depth == 24) {
-                memcpy(&s->vram24[addr], &s->vram24[adsr], len * 4);
+                memmove(&s->vram24[addr], &s->vram24[adsr], len * 4);
              }
          }
          tcx_set_dirty(s, addr, len);

Please can you can send a v2 with the commit message updated, then if it looks good I'll queue it in my qemu-sparc branch.

ATB,

Mark.