Permalink
Browse files

Align the stack for callbacks into mingw code

  • Loading branch information...
slipher committed Jan 5, 2019
1 parent 1e9d58c commit a3b29a5eceed7056712659ed4d286504aad26cc0
@@ -176,6 +176,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BREAKPOINT()
#endif

#if defined(__MINGW32__) && defined(__i386__)
// On x86, GCC expects 16-byte stack alignment (used for SSE instructions), but MSVC only uses 4-byte alignment.
// Therefore the stack needs to be adjusted whenever MSVC code calls into GCC code.
# define ALIGN_STACK_FOR_MINGW __attribute__((force_align_arg_pointer))
#else
# define ALIGN_STACK_FOR_MINGW
#endif

// Compat macros
#define QDECL
#define INLINE inline
@@ -240,7 +240,7 @@ static const char *WindowsExceptionString(DWORD code)
return "Unknown exception";
}
}
static LONG WINAPI CrashHandler(PEXCEPTION_POINTERS ExceptionInfo)
ALIGN_STACK_FOR_MINGW static LONG WINAPI CrashHandler(PEXCEPTION_POINTERS ExceptionInfo)
{
// Reset handler so that any future errors cause a crash
SetUnhandledExceptionFilter(nullptr);
@@ -2533,7 +2533,7 @@ static HANDLE IRC_ThreadHandle = nullptr;
IRC_SystemThreadProc
==================
*/
static DWORD WINAPI IRC_SystemThreadProc( LPVOID )
ALIGN_STACK_FOR_MINGW static DWORD WINAPI IRC_SystemThreadProc( LPVOID )
{
IRC_Thread();
return 0;
@@ -671,17 +671,9 @@ static void Init(int argc, char** argv)

} // namespace Sys

// GCC expects a 16-byte aligned stack but Windows only guarantees 4-byte alignment.
// The MinGW startup code should be handling this but for some reason it isn't.
#if defined(_WIN32) && defined(__GNUC__) && defined(__i386__)
#define ALIGN_STACK __attribute__((force_align_arg_pointer))
#else
#define ALIGN_STACK
#endif

// Program entry point. On Windows, main is #defined to SDL_main which is
// invoked by SDLmain.
ALIGN_STACK int main(int argc, char** argv)
// Program entry point. On Windows, main is #defined to SDL_main which is invoked by SDLmain.
// This is why ALIGN_STACK_FOR_MINGW is needed (normally gcc would generate alignment code in main()).
ALIGN_STACK_FOR_MINGW int main(int argc, char** argv)
{
// Initialize the engine. Any errors here are fatal.
try {
@@ -75,7 +75,7 @@ static SDL_Thread *renderThread = nullptr;
GLimp_RenderThreadWrapper
===============
*/
static int GLimp_RenderThreadWrapper( void* )
ALIGN_STACK_FOR_MINGW static int GLimp_RenderThreadWrapper( void* )
{
// These printfs cause race conditions which mess up the console output
logger.Notice( "Render thread starting\n" );
@@ -97,7 +97,7 @@ void Sys::Error(Str::StringRef message)
#ifdef BUILD_VM_IN_PROCESS

// Entry point called in a new thread inside the existing process
extern "C" DLLEXPORT void vmMain(Sys::OSHandle rootSocket)
extern "C" DLLEXPORT ALIGN_STACK_FOR_MINGW void vmMain(Sys::OSHandle rootSocket)
{
try {
try {

0 comments on commit a3b29a5

Please sign in to comment.