Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gcc: backport some upstream fixes #8052

Merged
merged 1 commit into from
Mar 3, 2021
Merged

Conversation

lazka
Copy link
Member

@lazka lazka commented Mar 2, 2021

Fixes #8046

@jeremyd2019
Copy link
Member

In order for GCC to use its implementation of __cxa_thread_atexit instead of mingw-w64's it is necessary to export ac_cv_func___cxa_thread_atexit=no for configure. @mstorsjo talked about adding an option for that in https://sourceforge.net/p/mingw-w64/mailman/message/37199203/ but to my knowledge that has not happened yet.

@lazka
Copy link
Member Author

lazka commented Mar 2, 2021

Should I add it? Are there any other packages affected by this change?

@jeremyd2019
Copy link
Member

jeremyd2019 commented Mar 2, 2021

Any C++ that uses thread_local objects would be affected.

The current state of affairs with mingw-w64's implementation, paired with a dynamically linked libwinpthread (as is default with the toolchain distributed here) results in a use-after-free during destruction of such objects defined in the EXE. GCC's implementation, while having its own issues, at least is capable of avoiding that situation, so seemed to be the least-bad option.
https://sourceforge.net/p/mingw-w64/mailman/message/37196899/

@jeremyd2019
Copy link
Member

See also #7096

@jeremyd2019
Copy link
Member

D'oh, doesn't seem to have helped the use-after-free situation, as I expected it to. In fact, it seems to have moved to the .dll instead...

Starting program: tlstest-main.exe
[New Thread 7216.0x2058]
[New Thread 7216.0x2530]
[New Thread 7216.0x1728]
main global ctor on thread 8576
main
main, starting thread1
[New Thread 7216.0x2494]
threadfunc thread 9364
main local tls ctor on thread 9364
main, thread1 started
LoadLibrary tlstest-lib.dll
lib global ctor on thread 8576
LoadLibrary tlstest-lib.dll ret 5bb50000
main, got func address, calling it
func
lib local tls ctor on thread 8576
lib global tls ctor on thread 8576
func end, thread 8576
main, starting thread2
[New Thread 7216.0x13b0]
threadfunc thread 5040
main local tls ctor on thread 5040
main, thread2 started
thread 9364 calling func
func
lib local tls ctor on thread 9364
lib global tls ctor on thread 9364
func end, thread 9364
main, thread1 work done
thread 5040 calling func
func
lib local tls ctor on thread 5040
lib global tls ctor on thread 5040
func end, thread 5040
main, thread2 work done
thread 9364 finishing

Thread 5 received signal SIGSEGV, Segmentation fault.
[Switching to Thread 7216.0x2494]
0x762d97fc in strlen () from C:\WINDOWS\System32\msvcrt.dll
=> 0x762d97fc <strlen+12>:      8a 01   mov    al,BYTE PTR [ecx]
(gdb) bt
#0  0x762d97fc in strlen () from C:\WINDOWS\System32\msvcrt.dll
#1  0x5bb52828 in __pformat_puts (
    s=0xfeeefeee <error: Cannot access memory at address 0xfeeefeee>,
    stream=stream@entry=0x1def880)
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/stdio/mingw_pformat.c:558
#2  0x5bb544fe in __mingw_pformat (flags=<optimized out>,
    dest=<optimized out>, max=<optimized out>,
    fmt=0x5bb5a05e " dtor from thread %d, now on %d\n", argv=<optimized out>)
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/stdio/mingw_pformat.c:2482
#3  0x5bb523c9 in __mingw_vfprintf (stream=0x76304640 <msvcrt!_iob+64>,
    fmt=0x5bb5a05c "%s dtor from thread %d, now on %d\n",
    argv=0x1def934 "î_î_î_î_"$")
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/stdio/mingw_vfprintf.c:54
#4  0x5bb57f05 in fprintf(_iobuf*, char const*, ...) ()
   from ...\tlstest-lib.dll
#5  0x5bb57fba in Hello::~Hello() ()
   from ...\tlstest-lib.dll
#6  0x7a69a2b7 in ?? () from ...\mingw32\bin\libstdc++-6.dll
#7  0x6c054e42 in ?? () from ...\mingw32\bin\libwinpthread-1.dll
#8  0x6c0550d3 in ?? () from ...\mingw32\bin\libwinpthread-1.dll
#9  0x770ed656 in ntdll!RtlFreeOemString () from C:\WINDOWS\SYSTEM32\ntdll.dll
#10 0x770922bd in ntdll!RtlActivateActivationContextUnsafeFast ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll
#11 0x770949ba in ntdll!RtlInitUnicodeStringEx ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll
#12 0x77090f43 in ntdll!LdrShutdownThread ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll

@jeremyd2019
Copy link
Member

Compared with current version of gcc in repo:

Starting program: ...\tlstest-main.exe
[New Thread 556.0x12ec]
[New Thread 556.0x1c14]
[New Thread 556.0x17c]
main global ctor on thread 6780
main
main, starting thread1
[New Thread 556.0x27a8]
threadfunc thread 10152
main local tls ctor on thread 10152
main, thread1 started
LoadLibrary tlstest-lib.dll
lib global ctor on thread 6780
LoadLibrary tlstest-lib.dll ret 5bb50000
main, got func address, calling it
func
lib local tls ctor on thread 6780
lib global tls ctor on thread 6780
func end, thread 6780
main, starting thread2
[New Thread 556.0xedc]
threadfunc thread 3804
main local tls ctor on thread 3804
main, thread2 started
thread 10152 calling func
func
lib local tls ctor on thread 10152
lib global tls ctor on thread 10152
func end, thread 10152
main, thread1 work done
thread 3804 calling func
func
lib local tls ctor on thread 3804
lib global tls ctor on thread 3804
func end, thread 3804
main, thread2 work done
thread 10152 finishing
lib global tls dtor from thread 10152, now on 10152
lib local tls dtor from thread 10152, now on 10152

Thread 5 received signal SIGSEGV, Segmentation fault.
[Switching to Thread 556.0x27a8]
0x762d97fc in strlen () from C:\WINDOWS\System32\msvcrt.dll
=> 0x762d97fc <strlen+12>:      8a 01   mov    al,BYTE PTR [ecx]
(gdb) bt
#0  0x762d97fc in strlen () from C:\WINDOWS\System32\msvcrt.dll
#1  0x00633458 in __pformat_puts (
    s=0xfeeefeee <error: Cannot access memory at address 0xfeeefeee>,
    stream=stream@entry=0x1d1f9d8)
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/stdio/mingw_pformat.c:558
#2  0x0063512e in __mingw_pformat (flags=<optimized out>, flags@entry=24576,
    dest=<optimized out>, dest@entry=0x76304640 <msvcrt!_iob+64>,
    max=<optimized out>, max@entry=0,
    fmt=0x63a05e " dtor from thread %d, now on %d\n",
    fmt@entry=0x63a05c "%s dtor from thread %d, now on %d\n",
    argv=<optimized out>, argv@entry=0x1d1fa8c "î_î_î_î_"'")
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/stdio/mingw_pformat.c:2482
#3  0x00632ff9 in __mingw_vfprintf (stream=0x76304640 <msvcrt!_iob+64>,
    fmt=0x63a05c "%s dtor from thread %d, now on %d\n",
    argv=0x1d1fa8c "î_î_î_î_"'")
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/stdio/mingw_vfprintf.c:54
#4  0x00638abd in fprintf(_iobuf*, char const*, ...) ()
#5  0x00638b72 in Hello::~Hello() ()
#6  0x00632915 in run_dtor_list (ptr=<synthetic pointer>)
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/tls_atexit.c:59
#7  tls_callback (hDllHandle=0x630000, dwReason=3, lpReserved=0x0)
    at C:/_/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/tls_atexit.c:155
#8  0x770ed656 in ntdll!RtlFreeOemString () from C:\WINDOWS\SYSTEM32\ntdll.dll
#9  0x770922bd in ntdll!RtlActivateActivationContextUnsafeFast ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll
#10 0x770949ba in ntdll!RtlInitUnicodeStringEx ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll
#11 0x77091014 in ntdll!LdrShutdownThread ()
   from C:\WINDOWS\SYSTEM32\ntdll.dll

@jeremyd2019
Copy link
Member

Sorry, I guess this patch/change turns out to be a wild goose chase, thought I had confirmed that this worked back in like October 😕

@lazka lazka merged commit cb77097 into msys2:master Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

gcc backports
2 participants