All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions
@ 2026-09-15 16:20 Николай Зорин
  2026-09-15 16:31 ` Daniel P. Berrangé
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Николай Зорин @ 2026-09-15 16:20 UTC (permalink / raw)
  To: qemu-devel
  Cc: thuth, mst, stefanha, kwolf, marcandre.lureau, mark.cave-ayland

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.

Signed-off-by: Nikolay N Zorin <zorin@swemel.ru>
---
  hw/display/tcx.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/hw/display/tcx.c b/hw/display/tcx.c
index 2cfc1e8f..fb48a199 100644
--- a/hw/display/tcx.c
+++ b/hw/display/tcx.c
@@ -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);
-- 
2.43.0



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

* Re: [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions
  2026-09-15 16:20 [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions Николай Зорин
@ 2026-09-15 16:31 ` Daniel P. Berrangé
  2026-09-15 16:38   ` Николай Зорин
  2026-09-15 16:50 ` Michael S. Tsirkin
  2026-09-18 13:14 ` Mark Cave-Ayland
  2 siblings, 1 reply; 5+ messages in thread
From: Daniel P. Berrangé @ 2026-09-15 16:31 UTC (permalink / raw)
  To: Николай Зорин
  Cc: qemu-devel, thuth, mst, stefanha, kwolf, marcandre.lureau,
	mark.cave-ayland

On Tue, Sep 15, 2026 at 07:20:46PM +0300, Николай Зорин wrote:
> 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.

> 
> 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.
> 
> Signed-off-by: Nikolay N Zorin <zorin@swemel.ru>
> ---
>  hw/display/tcx.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/display/tcx.c b/hw/display/tcx.c
> index 2cfc1e8f..fb48a199 100644
> --- a/hw/display/tcx.c
> +++ b/hw/display/tcx.c
> @@ -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);
> -- 
> 2.43.0
> 
> 

With regards,
Daniel
-- 
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|



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

* Re: [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions
  2026-09-15 16:31 ` Daniel P. Berrangé
@ 2026-09-15 16:38   ` Николай Зорин
  0 siblings, 0 replies; 5+ messages in thread
From: Николай Зорин @ 2026-09-15 16:38 UTC (permalink / raw)
  To: Daniel P. Berrangé
  Cc: qemu-devel, thuth, mst, stefanha, kwolf, marcandre.lureau,
	mark.cave-ayland

[-- Attachment #1: Type: text/plain, Size: 291 bytes --]

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é пишет:
> I'm guessing it is a tag you're using for tracking issues, but
> if so, it should be removed before sending commits upstream.

[-- Attachment #2: Type: text/html, Size: 948 bytes --]

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

* Re: [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions
  2026-09-15 16:20 [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions Николай Зорин
  2026-09-15 16:31 ` Daniel P. Berrangé
@ 2026-09-15 16:50 ` Michael S. Tsirkin
  2026-09-18 13:14 ` Mark Cave-Ayland
  2 siblings, 0 replies; 5+ messages in thread
From: Michael S. Tsirkin @ 2026-09-15 16:50 UTC (permalink / raw)
  To: Николай Зорин
  Cc: qemu-devel, thuth, stefanha, kwolf, marcandre.lureau,
	mark.cave-ayland

thanks for the patch! some questions:

On Tue, Sep 15, 2026 at 07:20:46PM +0300, Николай Зорин wrote:
> SWSA#34e3af79-c6c2-4ad2-986f-313c1ce28e5a

what is this?

> 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.

> Fix this by replacing `memcpy()` with `memmove()`, which explicitly
> guarantees safe and correct behavior when memory regions overlap.
> 
> Signed-off-by: Nikolay N Zorin <zorin@swemel.ru>
> ---
>  hw/display/tcx.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/display/tcx.c b/hw/display/tcx.c
> index 2cfc1e8f..fb48a199 100644
> --- a/hw/display/tcx.c
> +++ b/hw/display/tcx.c
> @@ -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);
> -- 
> 2.43.0



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

* Re: [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions
  2026-09-15 16:20 [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions Николай Зорин
  2026-09-15 16:31 ` Daniel P. Berrangé
  2026-09-15 16:50 ` Michael S. Tsirkin
@ 2026-09-18 13:14 ` Mark Cave-Ayland
  2 siblings, 0 replies; 5+ messages in thread
From: Mark Cave-Ayland @ 2026-09-18 13:14 UTC (permalink / raw)
  To: Николай Зорин,
	qemu-devel
  Cc: thuth, mst, stefanha, kwolf, marcandre.lureau

On 15/09/2026 17:20, Николай Зорин wrote:

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

I guess this is from something else?

> 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...

> 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.

> Signed-off-by: Nikolay N Zorin <zorin@swemel.ru>
> ---
>   hw/display/tcx.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/hw/display/tcx.c b/hw/display/tcx.c
> index 2cfc1e8f..fb48a199 100644
> --- a/hw/display/tcx.c
> +++ b/hw/display/tcx.c
> @@ -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);

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.



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

end of thread, other threads:[~2026-09-18 13:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-15 16:20 [PATCH] hw/display/tcx: use memmove instead of memcpy for overlapping blit memory regions Николай Зорин
2026-09-15 16:31 ` Daniel P. Berrangé
2026-09-15 16:38   ` Николай Зорин
2026-09-15 16:50 ` Michael S. Tsirkin
2026-09-18 13:14 ` Mark Cave-Ayland

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.