After several days searching why my code refactoring to remove globals was crashing,

I found the problem it was because CValue stack variables have rubish as it inital values
and assigning to a member that is smaller than the big union item and trying to
recover it later as a different member gives bak garbage.

ST_FUNC void vset(TCCState* tcc_state, CType *type, int r, int v)
{
    CValue cval;
    memset(&cval, 0, sizeof(CValue));

    cval.i = v; //,<<<<<<<<<<< here is the main bug that mix with garbage
    vsetc(tcc_state, type, r, &cval);
}

/* store a value or an expression directly in global data or in local array */
static void init_putv(TCCState* tcc_state, CType *type, Section *sec, unsigned long c,
                      int v, int expr_type)
{
...
        case VT_PTR:
            if (tcc_state->tccgen_vtop->r & VT_SYM) {
                greloc(tcc_state, sec, tcc_state->tccgen_vtop->sym, c, R_DATA_PTR);
            }

//<<< on the next line is where we try to get the assigned value to cvalue.i as cvalue.ull

            *(addr_t *)ptr |= (tcc_state->tccgen_vtop->c.ull & bit_mask) << bit_pos;
            break;

Also this patch makes vla tests pass on linux 32 bits
This commit is contained in:
mingodad
2014-03-26 20:14:39 +00:00
parent aa561d7011
commit 4bc83ac393
2 changed files with 20 additions and 5 deletions

View File

@ -329,6 +329,7 @@ static void vsetc(CType *type, int r, CValue *vc)
void vpush(CType *type)
{
CValue cval;
memset(&cval, 0, sizeof(CValue));
vsetc(type, VT_CONST, &cval);
}
@ -336,6 +337,7 @@ void vpush(CType *type)
ST_FUNC void vpushi(int v)
{
CValue cval;
memset(&cval, 0, sizeof(CValue));
cval.i = v;
vsetc(&int_type, VT_CONST, &cval);
}
@ -344,6 +346,7 @@ ST_FUNC void vpushi(int v)
static void vpushs(long long v)
{
CValue cval;
memset(&cval, 0, sizeof(CValue));
if (PTR_SIZE == 4)
cval.i = (int)v;
else
@ -354,8 +357,9 @@ static void vpushs(long long v)
/* push arbitrary 64bit constant */
void vpush64(int ty, unsigned long long v)
{
CValue cval;
CType ctype;
CValue cval;
memset(&cval, 0, sizeof(CValue));
ctype.t = ty;
ctype.ref = NULL;
cval.ull = v;
@ -372,6 +376,7 @@ static inline void vpushll(long long v)
static inline void vpushsym(CType *type, Sym *sym)
{
CValue cval;
memset(&cval, 0, sizeof(CValue));
cval.ull = 0;
vsetc(type, VT_CONST | VT_SYM, &cval);
@ -446,6 +451,7 @@ ST_FUNC void vpush_global_sym(CType *type, int v)
ST_FUNC void vset(CType *type, int r, int v)
{
CValue cval;
memset(&cval, 0, sizeof(CValue));
cval.i = v;
vsetc(type, r, &cval);
@ -731,6 +737,7 @@ ST_FUNC int gv(int rc)
unsigned long offset;
#if defined(TCC_TARGET_ARM) && !defined(TCC_ARM_VFP)
CValue check;
memset(&check, 0, sizeof(CValue));
#endif
/* XXX: unify with initializers handling ? */