Hello Sergei,

2013/11/11 Sergei Antonov <[email protected]>:
> Hello!
> I'm using Mingw-w64 3.0 release compiled with gcc-4.7.2. This is my test 
> pogram:
>
> =======================================
> #include <windows.h>
> #include <tchar.h>
>
> struct S {
>   S() {
>     OutputDebugString("test ctor");
>   }
>   ~S() {
>     OutputDebugString("test dtor");
>   }
> };
>
> int APIENTRY _tWinMain(HINSTANCE hInstance,
>                      HINSTANCE hPrevInstance,
>                      LPTSTR    lpCmdLine,
>                      int       nCmdShow) {
>   static S s; // <=== this will have to be destroyed on program exit
>   return 0;
> }
> =======================================
>
> Compilation command:
> x86_64-w64-mingw32-g++ -m32 -static test.cpp -o test.exe
>
> I test the resultant .exe using Dr. Memory - an analog of valgrind for
> Windows ( http://drmemory.org/ ). It finds an error:
>
> D:\>Programs\DrMemory-Windows-1.6.0-2\bin\drmemory.exe -- D:\test.exe
> ~~Dr.M~~ Dr. Memory version 1.6.0
> ~~Dr.M~~ Running "D:\test.exe"
> ~~Dr.M~~
> ~~Dr.M~~ Error #1: UNADDRESSABLE ACCESS: reading 0x7efdde1c-0x7efdde20 4 
> byte(s)
> ~~Dr.M~~ # 0 KERNELBASE.dll!TlsGetValue                        +0x17
>   (0x75d53c24 <KERNELBASE.dll+0x13c24>)
> ~~Dr.M~~ # 1 __mingwthr_run_key_dtors.part.0
> [../crt/tlsthrd.c:108]
> ~~Dr.M~~ # 2 __mingw_TLScallback
> [../crt/tlsthrd.c:129]
> ~~Dr.M~~ # 3 ntdll.dll!RtlUlonglongByteSwap                    +0x7a8
>   (0x77b4dc49 <ntdll.dll+0x7dc49>)
> ~~Dr.M~~ # 4 ntdll.dll!wcscspn                                 +0x251
>   (0x77b40a91 <ntdll.dll+0x70a91>)
> ~~Dr.M~~ # 5 ntdll.dll!RtlExitUserProcess                      +0x73
>   (0x77b1d5a4 <ntdll.dll+0x4d5a4>)
> ~~Dr.M~~ # 6 KERNEL32.dll!ExitProcess                          +0x14
>   (0x75a179c5 <KERNEL32.dll+0x179c5>)
> ~~Dr.M~~ # 7 msvcrt.dll!__crtExitProcess
> ~~Dr.M~~ # 8 msvcrt.dll!_cinit
> ~~Dr.M~~ # 9 msvcrt.dll!exit
> ~~Dr.M~~ #10 _fu0___set_invalid_parameter_handler
> [../crt/crtexe.c:335]
> ~~Dr.M~~ #11 KERNEL32.dll!BaseThreadInitThunk                  +0x11
>   (0x75a1336a <KERNEL32.dll+0x1336a>)
> ~~Dr.M~~ Note: @0:00:01.194 in thread 4132
> ~~Dr.M~~ Note: instruction: mov    0x00000e10(%eax,%ecx,4) -> %eax
> ~~Dr.M~~
> ~~Dr.M~~ ERRORS FOUND:
> ~~Dr.M~~       1 unique,     1 total unaddressable access(es)
>
> This error only happens for .exe compiled with -static.
> The reason is that TlsGetValue is called for an index *after* it has
> already been freed with TlsFree. I verified this by patching out a
> call to TlsFree in the binary - no error happened for the patched
> binary.
>
> So this -static mode reverse sequence of operation has to be fixed.
> Unfortunately, I cannot provide a patch since I don't understand the
> code well enough.

Hmm, this is indeed interesting.  The underlying issue is here that on
program-shutdown the emulTLS-destruction code in libgcc is called
before TLS-hooks are ...
In our startup-code/runtime library-code we don't call TlsFree
ourself.  So within the routine __mingwthr_run_key_dtors we try to get
the key-value of the key_dtor_list for calling the destructor.

Of interest is here one point.  Are we already asserting in call of
mingwthr_run_key_dtors in __mingw_TLScallback by the reason-value of
DLL_PROCESS_DETACH, or by DLL_THREAD_DETACH?

It might be that the call for DLL_PROCESS_DETACH we shouldn't do, as
before the thread should have already be detached ...  at least worth
a try.

Regards,
Kai

------------------------------------------------------------------------------
November Webinars for C, C++, Fortran Developers
Accelerate application performance with scalable programming models. Explore
techniques for threading, error checking, porting, and tuning. Get the most 
from the latest Intel processors and coprocessors. See abstracts and register
http://pubads.g.doubleclick.net/gampad/clk?id=60136231&iu=/4140/ostg.clktrk
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to