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

            Bug ID: 78189
           Summary: movaps generated for unaligned store in aligned
                    struct, when struct is referenced via unaligned
                    member.
           Product: gcc
           Version: 6.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jwyatt at feralinteractive dot com
  Target Milestone: ---

The code below, when compiled with -O2 -ftree-slp-vectorize -Wall -Wextra,
generates:

  pxor   %xmm0,%xmm0
  movaps %xmm0,0x10(%rdi)

for NullB. This causes a segfault, as rdi contains the address of the 8-byte
aligned "misaligned" member of struct B.

This does not happen if NullB uses reinterpret_cast<uintptr_t> instead of
uint8_t*, if "misaligned" and "pad" are swapped (so that "misaligned" is in
fact aligned). B::Null() is also fine.

// Impl.h

#include <cstddef>

struct A
{
        void * a;
        void * b;
};

struct alignas(16) B
{
        void * pad;
        void * misaligned;
        void * pad2;

        A a;

        void Null();
};

static_assert((offsetof(B, a) % 16) == 8, "A should be misaligned for this
test");

void NullB(void * misalignedPtr);

// Impl.cpp
#include <stdint.h>

#include "Impl.h"

void B::Null()
{
        a.a = nullptr;
        a.b = nullptr;
}

void NullB(void * misalignedPtr)
{
        B* b = reinterpret_cast<B*>(reinterpret_cast<uint8_t *>(misalignedPtr)
- offsetof(B, misaligned));
        b->Null();
}

// main.cpp
#include "Impl.h"

int main()
{
        B b;
        NullB(&b.misaligned);
        return 0;
}


gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/6.2.1/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,objc,obj-c++,fortran,ada,go,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-linker-hash-style=gnu --enable-plugin --enable-initfini-array
--disable-libgcj --with-isl --enable-libmpx --enable-gnu-indirect-function
--with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 6.2.1 20160916 (Red Hat 6.2.1-2) (GCC)

Reply via email to