bmx-ng / bcc

A next-generation bcc parser for BlitzMax
zlib License
33 stars 13 forks source link

Throw causes segfaults on x64 Windows #546

Closed HurryStarfish closed 3 years ago

HurryStarfish commented 3 years ago

I've experienced segfaults caused by using the Throw statement. These happen consistently on my machine when executing certain builds, but only under extremely specific conditions. For example, I've been able to consistenly produce crashes on my machine with the following code:

SuperStrict
Framework BRL.Threads

Local threads:TThread[2]
For Local i:Int = 0 Until threads.length
    threads[i] = TThread.Create(TestThread, New TTestThreadData)
Next
For Local t:TThread = EachIn threads
    t.Wait
Next

Type TTestThreadData
End Type

Function TestThread:Object(data:Object)
    Local testThreadData:TTestThreadData = TTestThreadData(data)
    Local asdf:String

    Try
        noop "bla"
        Throw "aaaaaaaa"
        If True Then noop ""
    Catch e:Object
    End Try
    noop "done"
End Function

Function noop(s:String)
End Function

gdb reports the segfault at this location:

#0  0x0000000076eba59b in ntdll!EtwEventSetInformation ()
   from C:\Windows\SYSTEM32\ntdll.dll
No symbol table info available.
#1  0x000007fefd40e5a3 in msvcrt!longjmp ()
   from C:\Windows\system32\msvcrt.dll
No symbol table info available.
#2  0x0000000000420d8d in bbExThrow (p=p@entry=0x43f130 <_s1>)
    at C:/Programmierung/BlitzMax-NG latest/mod/brl.mod/blitz.mod/blitz_ex.c:141

        st = <optimized out>
[...]

These segfaults only happen when building for x64/Windows/Release. They may not be reproducible with this exact code on other machines. They seem to be influenced by gcc/MinGW optimizations, because they stop happening when building without optimization level -O3, and they also stop happening when very minor changes to the code are made, such as

After spending quite a while trying to find the problem, I've come to believe it's a bug in the setjmp/longjmp implementation in MinGW that has existed for several years. Info on this can be found here: