bmx-ng / bcc

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

GC issue / Structs issue? #532

Open GWRon opened 4 years ago

GWRon commented 4 years ago

I have a flaw somewhere in my code - so I tried to run it as "debug build" in MaxIDE. It runs there until I interacted the way it makes it crash (whatever makes it crash ...). I see the debug tree for a very short amount end then the output lists "segmentation fault" - am pretty sure it has to with structs.

Then I tried to execute the exact same binary in gdb (as I built with GDB information) - there it does not even do a proper startup...

(gdb) r
Starting program: /home/ronny/Arbeit/Share/Dig-master/samples/guisamples/textarea.debug 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0x00000000007a1b8b in GC_find_limit_with_bound (bound=0x0, up=0, 
    p=<optimized out>)
    at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/os_dep.c:1008
1008                    GC_noop1((word)(*result));
(gdb) bt
#0  0x00000000007a1b8b in GC_find_limit_with_bound (bound=0x0, up=0, p=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/os_dep.c:1008
#1  0x00000000007a1dd9 in GC_find_limit (up=0, p=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/os_dep.c:1020
#2  GC_init_linux_data_start () at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/os_dep.c:459
#3  0x000000000079caf4 in GC_init () at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/misc.c:1202
#4  0x000000000079d2a5 in GC_init () at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/misc.c:1370
#5  0x0000000000799306 in bbGCStartup (spTop=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/blitz_gc.c:76
#6  0x0000000000782d69 in bbStartup (argc=1, argv=0x7fffffffe198, dummy1=<optimized out>, dummy2=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/blitz_app.c:494
#7  0x00000000004078b8 in main ()

Any idea what to do? how to narrow it down ?

GWRon commented 4 years ago

I entered "continue" ...so it does continue to run (and executes) ... but soon after this I run into the next issue:

(gdb) continue
Continuing.
[New Thread 0x7ffff49fd700 (LWP 30716)]
[New Thread 0x7ffff41fc700 (LWP 30717)]
[New Thread 0x7ffff39fb700 (LWP 30718)]
reset text cache
reset text cache
reset text cache
reset text cache
reset text cache

Thread 1 "textarea.debug" received signal SIGSEGV, Segmentation fault.
GC_malloc_kind_global (k=0, lb=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/malloc.c:311
311                 *opp = obj_link(op);
(gdb) bt
#0  GC_malloc_kind_global (k=0, lb=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/malloc.c:311
#1  GC_malloc_kind (k=0, lb=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/malloc.c:336
#2  GC_malloc_atomic (lb=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/bdwgc/malloc.c:343
#3  0x00000000007993ea in bbGCAllocObject (sz=<optimized out>, clas=clas@entry=0xa8ad40 <bbArrayClass>, flags=1) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/blitz_gc.c:102
#4  0x0000000000796f9e in allocateArray (dims=<optimized out>, data_size=<optimized out>, lens=<optimized out>, type=<optimized out>) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/blitz_array.c:103
#5  bbArraySliceStruct (data_size=0, structInit=0x0, end=85, beg=0, inarr=0x7ffff2ff0a80, type=0x7bb5b1 "i") at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/blitz_array.c:234
#6  bbArraySlice (type=type@entry=0x7bb5b1 "i", inarr=0x7ffff2ff0a80, beg=beg@entry=0, end=85) at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/blitz.mod/blitz_array.c:222
#7  0x00000000004b5d46 in __m_base_gfx_bitmapfont_STextParseInfo_EnsureDynamicArraySize_v_i (o=<optimized out>, bbt_size=<optimized out>) at /home/ronny/Arbeit/Share/Dig-master/base.gfx.bitmapfont.bmx:2602
#8  0x00000000004d9670 in __m_base_gfx_bitmapfont_STextParseInfo_CalculateDimensions_v_vSffTTBitmapFontvTSDrawTextSettings (o=<optimized out>, bbt_txt=<optimized out>, bbt_txt@entry=0x7ffff2fccea0, bbt_limitWidth=<optimized out>, 
    bbt_limitWidth@entry=600, bbt_limitHeight=<optimized out>, bbt_limitHeight@entry=-1, bbt_font=<optimized out>, bbt_font@entry=0x7ffff2f72f20, bbt_settings=<optimized out>, bbt_settings@entry=0x7ffff3141c48)
    at /home/ronny/Arbeit/Share/Dig-master/base.gfx.bitmapfont.bmx:2764
#9  0x00000000004f5c4d in __m_base_gfx_gui_textarea_TGUITextArea_DrawContent (o=<optimized out>) at /home/ronny/Arbeit/Share/Dig-master/base.gfx.gui.textarea.bmx:668
#10 0x000000000053603f in __m_base_gfx_gui_TGUIobject_Draw (o=<optimized out>) at /home/ronny/Arbeit/Share/Dig-master/base.gfx.gui.bmx:1794
#11 0x000000000052f54c in __m_base_gfx_gui_TGUIManager_Draw_TTLowerStringiii (o=<optimized out>, bbt_State=<optimized out>, bbt_fromZ=<optimized out>, bbt_toZ=<optimized out>, bbt_drawTypes=<optimized out>)
    at /home/ronny/Arbeit/Share/Dig-master/base.gfx.gui.bmx:614
#12 0x00000000004081b3 in _bb_main () at /home/ronny/Arbeit/Share/Dig-master/samples/guisamples/textarea.bmx:47
#13 0x000000000070c133 in __bb_brl_appstub_appstub () at /home/ronny/Arbeit/Tools/BlitzMaxNG/mod/brl.mod/appstub.mod/appstub.bmx:79
#14 0x00000000004078bf in main ()

the last call in this stack done by "me" is the method "EnsureDynamicArraySize" of a struct.

Struct STextParseInfo
    'storage of current font styles
    Field stylesB:Int
    Field stylesI:Int
    Field stylesInvisible:Int
    Field StaticArray stylesColors:SColor8[10]
    Field stylesColorsDynamic:SColor8[]
    Field stylesColorsIndex:Int

    Field baseColor:SColor8
    Field hasCurrentColor:Int

    'helper to read from a given text without extracting strings first
    Field command:SSubString
    Field payload:SSubString

    ' word wrap, alignment and dimensions storage
    Private
    Field StaticArray lineinfo_widths:Short[10]
    Field StaticArray lineinfo_boxHeights:Short[10]
    Field StaticArray lineinfo_contentHeights:Short[10]
    Field StaticArray lineinfo_maxFontHeights:Short[10]
    Field StaticArray lineinfo_lineBreakIndices:Int[10]
    Field StaticArray lineinfo_lineBreakOptions:Byte[10]
    Field StaticArray lineinfo_fontDisplaceYs:Int[10]
    Field lineinfo_widthsDynamic:Short[] = Null
    Field lineinfo_boxHeightsDynamic:Short[] = Null
    Field lineinfo_contentHeightsDynamic:Short[] = Null
    Field lineinfo_maxFontHeightsDynamic:Short[] = Null
    Field lineinfo_lineBreakIndicesDynamic:Int[] = Null
    Field lineinfo_lineBreakOptionsDynamic:Byte[] = Null
    Field lineinfo_fontDisplaceYsDynamic:Int[] = Null

    Public
    Field lineFontDisplaceYMax:Int
...
    Method EnsureDynamicArraySize(size:Int)

This struct passed "by reference" (to avoid copying it over and over) and is optionally transported via a wrapper in varying overloaded methods ("external API"):

Type TTextParseInfo
    'private
    Field data:STextParseInfo

    Method New()
        data = new STextParseInfo(5, 10)
    End Method

    Method New(estimatedLineCount:Short, estimatedNestedColorStyles:Int)
        data = new STextParseInfo(estimatedLineCount, estimatedNestedColorStyles)
    End Method
End Type

Am using the struct here to make it as light as possible (like a group of variables) when used internally.

GWRon commented 4 years ago

Did a rebuild of the app, with latest bcc etc - the "continue" error did not happen on the next run but the initial "gdb stop" (noop1) still happened - I assume it is something which I should configure GDB to "ignore" (like we did in the past with some other stuff) ?

also MaxIDE is still not showing "where" it segfaults - I had 1 out of 5 segfaults in which It has shown "where" the error happened and even had a debug tree. It is surely failing at the same spot each time...

GWRon commented 4 years ago

Here is a "video" (gif) of this maxide failure: maxideerror

GWRon commented 4 years ago

Tried to check if MaxIDE can show the error when replacing "struct" with "type" for this particular struct ... but then it does no longer crash.

This means that either the struct stuff is bugged - or the code I wrote. I assume it is my flaw but maybe ... it's not.

As said ... I am passing the struct as "var" here and there (but NOT assigning a "new" struct to the variable in the methods) ... I am just "reading" or calling a method of the struct (so it "internally" changes private values).

Can the "dynamic" (normal) arrays be the issue here? something like a "structs have a fixed/predefined size"? Dunno how the arrays are stored (with a reference - like types?)? initially my arrays are "Null" (unused). If the arrays were the issue it would explain why it works with "Type" instead of "Struct".

GWRon commented 4 years ago

Commit https://github.com/bmx-ng/brl.mod/commit/c407fab2ce5143e96bcf0298cb07e0e98f021116 tried to close this issue - but it still happens.

GWRon commented 4 years ago

I assume it has a null access in this "mymethod()" call but fails to jump "into" it ...

maxide_structs

As you see - the struct which has the method is "valid", and same for the param struct whose property is accessed.

Edit: replacing "struct" with "type" again - and it works without any "flaws" (at least for these regards here :D)

GWRon commented 4 years ago

OK ... so why does it fail there?

I have a method - the only one "tackling" my (non static) arrays


    Method EnsureDynamicArraySize(size:Int)
        If lineinfo_boxHeightsDynamic.length <= size
'ddd
'print "ensure size: " + size + "+ 10"  
            lineinfo_boxHeightsDynamic = lineinfo_boxHeightsDynamic[.. size + 10]
            lineinfo_contentHeightsDynamic = lineinfo_contentHeightsDynamic[.. size + 10]
            lineinfo_maxFontHeightsDynamic = lineinfo_maxFontHeightsDynamic[.. size + 10]
            lineinfo_widthsDynamic = lineinfo_widthsDynamic[.. size + 10]
            lineinfo_lineBreakIndicesDynamic = lineinfo_lineBreakIndicesDynamic[.. size + 10]
            lineinfo_lineBreakOptionsDynamic = lineinfo_lineBreakOptionsDynamic[.. size + 10]
            lineinfo_fontDisplaceYsDynamic = lineinfo_fontDisplaceYsDynamic[.. size + 10]
        EndIf
    End Method

It is called before I access the arrays. So normally they all should be of the same size - right?

Nope - I get some null arrays, and others have wrong types when "unfolding" in the debugger... And -- even the static arrays are Null ... some of them!

Remember: this is how I defined them in the struct:

    Field StaticArray lineinfo_widths:Short[10]
    Field StaticArray lineinfo_boxHeights:Short[10]
    Field StaticArray lineinfo_contentHeights:Short[10]
    Field StaticArray lineinfo_maxFontHeights:Short[10]
    Field StaticArray lineinfo_lineBreakIndices:Int[10]
    Field StaticArray lineinfo_lineBreakOptions:Byte[10]
    Field StaticArray lineinfo_fontDisplaceYs:Int[10]
    Field lineinfo_widthsDynamic:Short[] = Null
    Field lineinfo_boxHeightsDynamic:Short[] = Null
    Field lineinfo_contentHeightsDynamic:Short[] = Null
    Field lineinfo_maxFontHeightsDynamic:Short[] = Null
    Field lineinfo_lineBreakIndicesDynamic:Int[] = Null
    Field lineinfo_lineBreakOptionsDynamic:Byte[] = Null
    Field lineinfo_fontDisplaceYsDynamic:Int[] = Null

And this is what the debugger spits out: debugger

I got a TPixmap in one of my arrays ??

GWRon commented 4 years ago

Another reminder: I pass many structs as VAR ! maybe this creates the issue ?

Edit: attached the "sample" code: File to compile: Dig-master/samples/guisamples/textarea.bmx Dig-mastersegfault2.zip

When it starts up (if it does without segfault) then scroll the text up and down a bit (mouse wheel or scrollbar) until it segfaults or debugger kicks in.