I have discovered that any program written in either C or C++ that is compiled using the -fstack-check flag crashes on start up.  I am including a very simple Hello World application and the Make File I used to compile it as an example.


#include <stdlib.h>
#include <stdio.h>

int main(int argc, char * argv [], char * envp [])
    printf("Hello World!\n");
    return 0;


# -*- Makefile -*-

SRC = $(wildcard *.c)
OBJ = $(SRC:.c=.o)
TARGET = HelloWorld.exe

CFLAGS := -mthreads -Wall
CPPFLAGS := -D_MBCS -D_WIN32 -D_WIN32_IE=0x0700 -D_WINDOWS -D_X86_=1 -DWIN32 -DWINVER=0x0600
LDFLAGS := -Wl,--subsystem,console
LDLIBS := -lkernel32 -luser32

ifeq ($(DEBUG), 1)
# A crash occurs when the stack-check flag is specified as in the following line.
CFLAGS += -fexceptions -fstack-check -g3 -O0
# To avoid the crash, simply omit the stack-check flag as in the following line.
# CFLAGS += -fexceptions -g3 -O0
# not ifeq ($(DEBUG), 1)
# As a test, you can use the following value for CFLAGS for the release build to
# show that the stack-check crash will occur in a release build as well.
# CFLAGS += -fstack-check -O3
# end ifeq ($(DEBUG), 1)

%.o : %.c
    gcc $(CFLAGS) $(CPPFLAGS) -c -o $@ $<

$(TARGET) : $(OBJ)
    gcc $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) -o $@ $^ $(LDLIBS)

all : $(TARGET)

    -@rm $(OBJ)
    -@rm $(TARGET)

This program, when built using the command "make all DEBUG=1" crashes on start up.  I used gdb to get a back trace after the crash occurred.  The following is the result.

Program received signal SIGSEGV, Segmentation fault.
main (argc=1, argv=0x4f2f78, envp=0x4f1f08) at HelloWorld.c:7
7               return 0;
(gdb) backtrace
#0  main (argc=1, argv=0x4f2f78, envp=0x4f1f08) at HelloWorld.c:7

I realize that this back trace does not provide any really useful information.

The following call stack obtained using the Visual Studio 2005 debugger provides a little more information but still not enough to really track down the cause of the crash (I know that GCC debug information is not compatible with Visual Studio but I was hoping that it might help to shed a little more light on the problem).

>    HelloWorld.exe!004013c9()    
     [Frames below may be incorrect and/or missing, no symbols loaded for HelloWorld.exe]   
     msvcrt.dll!_RtlpImageNtHeader@4()  + 0x54 bytes   
     kernel32.dll!@BaseThreadInitThunk@12()  + 0x12 bytes   
     ntdll.dll!___RtlUserThreadStart@8()  + 0x27 bytes   
     ntdll.dll!__RtlUserThreadStart@8()  + 0x1b bytes   

Note that according to the Visual Studio debugger this crash occurs as a result of an "Unhandled exception at 0x004013c9 in HelloWorld.exe: 0xC0000005: Access violation writing location 0x0022cf14."

This program was built using GCC version  4.5.0 on Windows 7 32-bit.  I installed MinGW using mingw-get-inst-20101030.

As the comments in the Make File suggest, I tried doing a debug build without the
-fstack-check flag set and I did not see the crash.  I also tried doing a release build with the -fstack-check flagset and I did get the crash, so I am fairly certain that it is the -fstack-check flag that caused the crash.  The release build without the -fstack-check flag works perfectly.