We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2536 - SDL_memset crashes on unaligned destinations [PATCH]
Summary: SDL_memset crashes on unaligned destinations [PATCH]
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: *don't know* (show other bugs)
Version: HG 2.1
Hardware: x86_64 Windows 8
: P2 critical
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
: 2030 (view as bug list)
Depends on:
Blocks:
 
Reported: 2014-05-11 01:07 UTC by Yuri K. Schlesner
Modified: 2014-05-19 04:26 UTC (History)
1 user (show)

See Also:


Attachments
Proposed patch. (1.53 KB, text/plain)
2014-05-11 01:07 UTC, Yuri K. Schlesner
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Yuri K. Schlesner 2014-05-11 01:07:29 UTC
Created attachment 1644 [details]
Proposed patch.

SDL_FillRect was crashing for me and I tracked this down to a bug/bad interaction with code generation in SDL_memset. GCC generates SSE unaligned store instructions for the Uint32 loop, causing crashes if the destination buffer isn't aligned on a 4-byte boundary.

This affects the official 2.0.3 64-bit binaries for MinGW. (Note: if you compile your own SDL, it may have HAVE_MEMSET set, and will thus use the libc memset and you will not hit this bug. Manually comment out HAVE_MEMSET in SDL_config.h if that's the case.)

I've attached a patch that fixes this by manually copying the first bytes until the buffer is aligned, and then continuing with the old code.

However, since GCC is optimizing the rest of the copy to a SSE loop, I question the value of even having the manual Uint32 copy. When I changed the loop to a simple Uint8 copy loop GCC optimized it to a call to memset. Is there a platform without memset available?
Comment 1 Yuri K. Schlesner 2014-05-11 01:11:51 UTC
Looks like this is a bug of #2030. Sorry, didn't notice it while writing the report. I don't know if I should close this as a dupe, since I have a patch.
Comment 2 Ryan C. Gordon 2014-05-18 19:05:05 UTC
(In reply to Yuri K. Schlesner from comment #0)
> Is there a platform without memset available?

To answer this question: there aren't any platforms we care about without memset(), but on Windows, we don't have any dependency on a C runtime at all (since it generally causes conflicts with people that want to use any of the several that Microsoft offers, or Cygwin's, Borland's, etc), so for the real basic pieces of the C runtime everyone expects, like memset, we offer simple implementations of our own.

Checking patch now.

--ryan.
Comment 3 Ryan C. Gordon 2014-05-18 19:14:40 UTC
This patch is now https://hg.libsdl.org/SDL/rev/d21a3ff2b0d2 ... thanks!

--ryan.
Comment 4 Yuri K. Schlesner 2014-05-19 04:26:45 UTC
*** Bug 2030 has been marked as a duplicate of this bug. ***