bmx-ng / bcc

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

Crash when handover a variable by reference to a function #577

Closed MidimasterSoft closed 2 years ago

MidimasterSoft commented 2 years ago

I convert at the moment a GIF-loader code to class for BlitzMax NG. Now I observed a strange behavior:

When I handover a global INT variable to my function "by reference" and use this variable for a FOR/NEXT-loop inside the function the app crashes without a senseful Debug-message. It looks like the C-code crashes.

I get this bug in RELEASE and DEBUG mode. But i was only able to write a demonstration, that crashes in DEBUG mode:

SuperStrict
Framework Brl.Basic
Local v:Int = 20
ForLoop(v)

Function ForLoop(intVariable:Int Var)
    Local sum:Int = 0
    For Local i:Int = 0 Until intVariable
        sum :+ i
    Next
End Function

when we do it without VAR it works as expected:

SuperStrict
Framework Brl.Basic
Local v:Int = 20
ForLoop(v)

Function ForLoop(intVariable:Int)
    Local sum:Int = 0
    For Local i:Int = 0 Until intVariable
        sum :+ i
    Next
End Function

It also works perfect when I copy the variable to a local INT:

SuperStrict
Framework Brl.Basic
Local v:Int = 20
ForLoop(v)

Function ForLoop(intVariable:Int Var)
    Local sum:Int = 0
    Local maxi:Int =  intVariable
    For Local i:Int = 0 Until maxi
        sum :+ i
    Next
End Function

I tested it on Win-10

Derron already confirmed the same behavior on his computer.

GWRon commented 2 years ago
    bbOnDebugEnterStm(&__stmt_0);
    bbt_sum=0;
    struct BBDebugStm __stmt_1 = {0x558ac721d846ce0d, 10, 0};
    bbOnDebugEnterStm(&__stmt_1);
    {
        BBINT bbt_i=0;
        BBINT* bbt_=*bbt_intVariable;
        for(;(bbt_i<*bbt_);bbt_i=(bbt_i+1)){
            struct BBDebugScope_1 __scope = {
                BBDEBUGSCOPE_LOCALBLOCK,
                0,
                {
                    {
                        BBDEBUGDECL_LOCAL,
                        "i",
                        "i",
                        .var_address=&bbt_i
                    },
                    {
                        BBDEBUGDECL_END
                    }
                }
            };
            bbOnDebugEnterScope(&__scope);
            struct BBDebugStm __stmt_0 = {0x558ac721d846ce0d, 11, 0};
            bbOnDebugEnterStm(&__stmt_0);
            bbt_sum+=bbt_i;
            bbOnDebugLeaveScope();
        }
    }
    bbOnDebugLeaveScope();

this is the generated C-Debug-Build code of the loop portion (and a bit earlier) ... probably some pointer problem.

I just do not understand what exactly happens in your application code (as you reported to have segfaults in release build mode too)

davecamp commented 2 years ago

The error is being caused by a 'double dereference' of the parameter. This code in release build...

SuperStrict
Framework Brl.Basic
Local v:Int = 20
ForLoop(v)

Function ForLoop(intVariable:Int Var)
    Local sum:Int = 0
    For Local i:Int = 0 Until intVariable
        sum :+ i
        Print sum
    Next
End Function

produces this c code for the ForLoop function

void _m_untitled1_ForLoop(BBINT* bbt_intVariable){
    BBINT bbt_sum=0;
    {
        BBINT bbt_i=0;
        BBINT* bbt_=*bbt_intVariable;           <---------- This is incorrect
        for(;(bbt_i<*bbt_);bbt_i=(bbt_i+1)){    <---------- Which causes the fault here
            bbt_sum+=bbt_i;
            brl_standardio_Print(bbStringFromInt(bbt_sum));
        }
    }
}

The bbtintVariable is a pointer which is deferenced and assigned to bbt which is (incorrectly in this situation) also a pointer to a BBINT. Then in the for loop the bbt_ variable is dereferenced... which means trying to dereference the actual integer value as if it was a memory address... which is causing the fault/violation.

Hopefully this may help where to look in the compiler.

davecamp commented 2 years ago

Just thinking out loud here... In the c code would it be better to change a 'Max Var' to a reference type as opposed to a pointer type? Just food for thought.

MidimasterSoft commented 2 years ago

ff