https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123765

            Bug ID: 123765
           Summary: bpf: incorrect code generation for overlapping
                    load/store
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kristerw at gcc dot gnu.org
            Blocks: 118443
  Target Milestone: ---
            Target: bpf-unknown-none

The following function is miscompiled with -fno-strict-aliasing -O1 or higher:

typedef int vec __attribute__((vector_size(4*sizeof(int))));
typedef unsigned uvec __attribute__((vector_size(4*sizeof(int))));
void h(vec*p,uvec*q){
    vec a = *p;
    *q = (uvec)a;
}

Compiling this as

 bpf-unknown-none-gcc -O1 -fno-strict-aliasing -mlittle-endian -mcpu=v4 -S
bug.c

produces the following assembly:

  h:
        r0 = *(u64 *) (r1+0)
        *(u64 *) (r2+0) = r0
        r1 = *(u64 *) (r1+8)
        *(u64 *) (r2+8) = r1
        exit

The problem is that *p and *q are only guaranteed to be 64-bit aligned
(according to the information in the GIMPLE IR), so overlap is possible. In
that case, the first store may overwrite bytes that have not yet been loaded.
Therefore, both loads must be executed before the first store, such as:

  h:
        r0 = *(u64 *) (r1+0)
        r1 = *(u64 *) (r1+8)
        *(u64 *) (r2+0) = r0
        *(u64 *) (r2+8) = r1
        exit


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118443
[Bug 118443] [Meta bug] Bugs triggered by and blocking more smtgcc testing

Reply via email to