Skip to content

Unexpected undefined behavior on IA32 ? #560

@boulme

Description

@boulme

On architecture IA32, ./ccomp -interp on the following program printfs ERROR: Initial state undefined
(just tested on commit ce0f6ca).

typedef struct {
     char is_float;
     union {
       long long i;
       double f;
     } u;
  } number;

number x={.is_float=0, .u={.i=42}};
number y;

int main(){
  y.u = x.u;
  return y.u.i;
}

I guess that there is no such UB in this program. For example, on x86_64, ./ccomp -interp prints Time 22: program terminated (exit code = 42).

I understand that this issue is related to Csem.assign_loc_copy for assignment y.u = x.u. Formal semantics requires offset to be aligned on Ctypes.alignof_blockcopy. But the trusted frontend (i.e. C2C) aligns global variables on Ctypes.alignof.

And on IA32 architecture (only), alignof_blockcopy may not divide Ctypes.alignof, even on naturally_aligned types, in particular for Tlong and for Tfloat F64.

Thus, a simple fix, would be to define

Fixpoint alignof_blockcopy (env: composite_env) (t: type) : Z :=
  match t with
  | Tlong _ _ => Archi.align_int64
  | Tfloat F64 _ => Archi.align_float64
  | ... => (* same as before *)
  end.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions