Debuggin MinGW32 crashes under Windows XP

% Remco Bloemen % 2014-02-27

Summary: You must pass the compiler flag -mincoming-stack-boundary=2 to gcc under mingw32.

I spent most of last week debugging a rather difficult problem under Windows XP. A multithreaded program would work properly under Windows 7, but would randomly crash under Windows XP.

My first thought went to a race condition. A difference in scheduling between XP and 7 might explain why it hapes in the former and not in the later.

So I put in more and more locks, but that did not help. I then tries hellgrind, but it flooded me with false positives from OpenSSL (well, I hope they are false).

I then spent a lot of time using gdb to step trough the program. This was very difficult since Windows has some sort of own debugger. This debugger takes over control as soon as the crash occurs, hiding it from gdb and in turn crashes itself in an unrelated place. After many hours I still couldn’t find the location of the crash.

After much googling and trying I finally found this page. The problem is that 32bit windows has 4 byte stack allignment and GCC assumes 16 byte, which is required for SSE instructions. GCC corrects for this by aligning the stack before calling main. However, if a windows library uses a callback or creates a new thread than the stack allignment might be lost. And this will make any SSE instruction after it crash. In my case I think the SSE instructions came from Qt’s unicode converters.

The solution is simple: teach GCC not to assume only 4 byte stack allignment with -mincoming-stack-boundary=2.

Remco Bloemen
Math & Engineering
https://2π.com