pawn-lang / compiler

Pawn compiler for SA-MP with bug fixes and new features - runs on Windows, Linux, macOS
Other
305 stars 72 forks source link

Union of variables in multi-state functions #428

Open Y-Less opened 5 years ago

Y-Less commented 5 years ago

Issue description:

If you have two variables in different states, then use them both in a function that is in both states, they may end up pointing to the same memory address.

Two variables in different states should never be called at the same time, so they can share memory. However, a function can be declared in multiple states at once using <state:a, state:b>, which means that a variable in state:a can be used, as can a variable in state:b. In this case, they should be marked as possibly co-existent, and thus should not be collocated.

Minimal complete verifiable example (MCVE):

new var1 <tt:aa>;
new var2 <tt:bb>;

Func() <tt:aa, tt:bb>
{
    var1 = 5;
    var2 = 6;
    #pragma unused var1, var2
}

main()
{
    Func();
}

Generated code:

CODE 0  ; 0
;program exit point
    halt 0

;exit point for functions called from the wrong state
l.0     ; 8
    halt d

DATA 0  ; 0
dump 0  ; automaton (anonymous)
dump 0  ; automaton tt

CODE 0  ; 10
    load.pri 4  ; Func
    switch 2
l.2     ; 20
    casetbl
    case 2 0
    case 1 1
    case 2 1

DATA 0  ; 8
dumpn 0 1

CODE 0  ; 3c
l.1     ; 3c
    proc    ; Func
    ; line c
    ; line d
    const.pri 5
    stor.pri 8
    ;$exp
    ; line e
    const.pri 6
    stor.pri 8
    ;$exp
    zero.pri
    retn

    proc    ; main
    ; line 13
    ; line 14
    push.c 0
    call .Func
    ;$exp
    zero.pri
    retn

STKSIZE 1000

The important part is here:

    ; line d
    const.pri 5
    stor.pri 8
    ; line e
    const.pri 6
    stor.pri 8

Where two different values are written to the same memory location.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity.