SDL icon indicating copy to clipboard operation
SDL copied to clipboard

SDL_blit_copy.c:66:16: runtime error: store to misaligned address

Open 1bsyl opened this issue 1 year ago • 2 comments

I seeing this error on x86_64 machine not sure how to reproduce yet...


src/video/SDL_blit_copy.c:66:16: runtime error: store to misaligned address 0x7f0ba9c5fc8c for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fc8c: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 

/src/video/SDL_blit_copy.c:67:16: runtime error: store to misaligned address 0x7f0ba9c5fc94 for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fc94: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 
src/video/SDL_blit_copy.c:68:16: runtime error: store to misaligned address 0x7f0ba9c5fc9c for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fc9c: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 
src/video/SDL_blit_copy.c:69:16: runtime error: store to misaligned address 0x7f0ba9c5fca4 for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fca4: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 
src/video/SDL_blit_copy.c:70:16: runtime error: store to misaligned address 0x7f0ba9c5fcac for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fcac: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 
src/video/SDL_blit_copy.c:71:16: runtime error: store to misaligned address 0x7f0ba9c5fcb4 for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fcb4: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 
src/video/SDL_blit_copy.c:72:16: runtime error: store to misaligned address 0x7f0ba9c5fcbc for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fcbc: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 
src/video/SDL_blit_copy.c:73:16: runtime error: store to misaligned address 0x7f0ba9c5fcc4 for type '__m64', which requires 8 byte alignment
0x7f0ba9c5fcc4: note: pointer points here
  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff  00 00 00 ff 00 00 00 ff
              ^ 



1bsyl avatar Jan 12 '24 14:01 1bsyl

I assume this is with the undefined behaviour sanitizer (-fsanitize=undefined) or similar?

It looks as though SDL_memcpyMMX() assumes that src and dst are both 8-byte-aligned, but someone or something has called it with a dst that is misaligned.

I notice that for SSE, we check that src and dst are nicely 16-byte-aligned, and we don't use SSE if they are not:

    if (SDL_HasSSE() &&
        !((uintptr_t)src & 15) && !(srcskip & 15) &&
        !((uintptr_t)dst & 15) && !(dstskip & 15)) {
        /* ... use SSE */

but for MMX, the only thing we check is that dstskip and srcskip are nicely aligned, and we don't check the alignment of src and dst themselves.

This would suggest that the call into the MMX implementation should perhaps look more like the SSE one:

-    if (SDL_HasMMX() && !(srcskip & 7) && !(dstskip & 7)) {
+    if (SDL_HasMMX() &&
+        !((uintptr_t)src & 7) && !(srcskip & 7) &&
+        !((uintptr_t)dst & 7) && !(dstskip & 7)) {
        /* ... use MMX */

and fall back to using plain memcpy() in the misaligned case?

A high-quality implementation of __builtin_memcpy() or memcpy() or bcopy() like the one supplied by gcc will already do all of these tricks (and more!) itself, so we are probably not gaining much from SDL having its own versions, and it's entirely possible that SDL's versions are strictly worse than the versions provided by some compilers and/or C standard libraries - but I realise some of the platforms that SDL supports probably have a crap memcpy() in their standard library, or none at all.

smcv avatar Jan 15 '24 17:01 smcv

yes, with: cmake -DCMAKE_C_FLAGS="-fsanitize=address -fsanitize=undefined" -DCMAKE_CXX_FLAGS="-fsanitize=address -fsanitize=undefined"

here's a test-case:

       SDL_Surface *s1, *s2;
       SDL_Rect r;
       s1 = SDL_CreateSurface(100, 100, SDL_PIXELFORMAT_XBGR8888);
       s2 = SDL_CreateSurface(100, 100, SDL_PIXELFORMAT_XBGR8888);
       r.x = 51;
       r.y = 30;
       r.w = 30;
       r.h = 30;
       SDL_BlitSurface(s1, &r, s2, &r);

1bsyl avatar Jan 16 '24 12:01 1bsyl

We removed this MMX code in SDL3, so this can be closed.

icculus avatar May 22 '24 17:05 icculus