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

            Bug ID: 87581
           Summary: Misaligned 16-bit read trap on x86 platform should be
                    either fixed or documented.
           Product: gcc
           Version: 4.9.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: svoboda at cert dot org
  Target Milestone: ---

Created attachment 44825
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=44825&action=edit
Crashing program

The attached code crashes with a SIGSEGV on GCC 4.9.4 on x86-64:

To crash it, compile with: gcc -O3 -fPIC

The same program does not crash if:
 * GCC 4.8.5
 * -fPIC is omitted
 * -mno-sse2 is provided
 * -O2
 * the compute() function is prepended with:
   __attribute__ ((target("no-sse")))

The crash occurs when the program reads and writes mis-aligned 16-bit values.
This is undefined behavior according to C11 s6.3.2.3p7, however it is widely
believed that x86 and x86-64 support unaligned memory reads and writes.

If GCC still assumes that unaligned memory read/write is safe on x86 & x86-64
they should change this optimization behavior.
But if GCC does NOT assume this, they (and others) need to be more vocal about
this. It needs to be in documentation...anyone who uses -O3 should know about
it.
(An alternative is to take SSE2 alignment requirements out of -O3 and put it
somewhere like -Ofast).

Reply via email to