Skip to content

Commit

Permalink
Align the stack for callbacks into mingw code
Browse files Browse the repository at this point in the history
  • Loading branch information
slipher authored and slipher committed Jan 8, 2019
1 parent 1e9d58c commit a3b29a5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/common/Compiler.h
Expand Up @@ -176,6 +176,14 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define BREAKPOINT() #define BREAKPOINT()
#endif #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 // Compat macros
#define QDECL #define QDECL
#define INLINE inline #define INLINE inline
Expand Down
2 changes: 1 addition & 1 deletion src/common/System.cpp
Expand Up @@ -240,7 +240,7 @@ static const char *WindowsExceptionString(DWORD code)
return "Unknown exception"; 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 // Reset handler so that any future errors cause a crash
SetUnhandledExceptionFilter(nullptr); SetUnhandledExceptionFilter(nullptr);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/client/cl_irc.cpp
Expand Up @@ -2533,7 +2533,7 @@ static HANDLE IRC_ThreadHandle = nullptr;
IRC_SystemThreadProc IRC_SystemThreadProc
================== ==================
*/ */
static DWORD WINAPI IRC_SystemThreadProc( LPVOID ) ALIGN_STACK_FOR_MINGW static DWORD WINAPI IRC_SystemThreadProc( LPVOID )
{ {
IRC_Thread(); IRC_Thread();
return 0; return 0;
Expand Down
14 changes: 3 additions & 11 deletions src/engine/framework/System.cpp
Expand Up @@ -671,17 +671,9 @@ static void Init(int argc, char** argv)


} // namespace Sys } // namespace Sys


// GCC expects a 16-byte aligned stack but Windows only guarantees 4-byte alignment. // Program entry point. On Windows, main is #defined to SDL_main which is invoked by SDLmain.
// The MinGW startup code should be handling this but for some reason it isn't. // This is why ALIGN_STACK_FOR_MINGW is needed (normally gcc would generate alignment code in main()).
#if defined(_WIN32) && defined(__GNUC__) && defined(__i386__) ALIGN_STACK_FOR_MINGW int main(int argc, char** argv)
#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)
{ {
// Initialize the engine. Any errors here are fatal. // Initialize the engine. Any errors here are fatal.
try { try {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/sys/sdl_glimp.cpp
Expand Up @@ -75,7 +75,7 @@ static SDL_Thread *renderThread = nullptr;
GLimp_RenderThreadWrapper 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 // These printfs cause race conditions which mess up the console output
logger.Notice( "Render thread starting\n" ); logger.Notice( "Render thread starting\n" );
Expand Down
2 changes: 1 addition & 1 deletion src/shared/VMMain.cpp
Expand Up @@ -97,7 +97,7 @@ void Sys::Error(Str::StringRef message)
#ifdef BUILD_VM_IN_PROCESS #ifdef BUILD_VM_IN_PROCESS


// Entry point called in a new thread inside the existing 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 {
try { try {
Expand Down

0 comments on commit a3b29a5

Please sign in to comment.