public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/64516] New: arm: wrong unaligned load generated
@ 2015-01-07  2:08 markus at oberhumer dot com
  2015-01-16 14:24 ` [Bug target/64516] " ramana at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: markus at oberhumer dot com @ 2015-01-07  2:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 64516
           Summary: arm: wrong unaligned load generated
           Product: gcc
           Version: 4.9.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: markus at oberhumer dot com

Created attachment 34391
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=34391&action=edit
unaligned_load_bug.c to reproduce the problem.

arm: bad code generated => run time crash

- gcc 4.9.2 seems to forget the attribute(packed) in some cases.

- please see the disassembly for get16_unaligned().

- put16_unaligned() is correct !

- FWIW, gcc 4.3.5 and clang 3.5 work

~Markus


$ cat unaligned_load_bug.c
typedef struct { char a[2]; } __attribute__((__packed__)) TU2;

// OK: correct (but poor) code generated for the store
void put16_unaligned(void *p, unsigned short v) {
    if (sizeof(TU2) == sizeof(v) && __alignof__(TU2) == 1) {
        *(TU2 *)p = *(const TU2 *)(const void *)(&v);
    }
}

// BUG: incorrect transformation into an aligned load => run time crash !!
unsigned short get16_unaligned(const void *p) {
    unsigned short v;
    if (sizeof(TU2) == sizeof(v) && __alignof__(TU2) == 1) {
        *(TU2 *)(void *)(&v) = *(const TU2 *)p;
    }
    return v;
}

// aligned versions - just for comparison
void put16_aligned(void *p, unsigned short v) {
    *(unsigned short *)p = *(&v);
}
unsigned short get16_aligned(const void *p) {
    unsigned short v;
    *(&v) = *(const unsigned short *)p;
    return v;
}

// EOF

$ arm-linux-gnueabi-gcc-4.9 -v
gcc version 4.9.2 (Ubuntu/Linaro 4.9.2-7ubuntu3)

$ arm-linux-gnueabi-gcc-4.9 -march=armv4 -marm -O2 -Wall -W -Wcast-align -c
unaligned_load_bug.c

$ arm-linux-gnueabi-objdump -d unaligned_load_bug.o

unaligned_load_bug.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <put16_unaligned>:
   0:   e52de004        push    {lr}            ; (str lr, [sp, #-4]!)
   4:   e24dd00c        sub     sp, sp, #12
   8:   e28d3008        add     r3, sp, #8
   c:   e16310b2        strh    r1, [r3, #-2]!
  10:   e3a02002        mov     r2, #2
  14:   e1a01003        mov     r1, r3
  18:   ebfffffe        bl      0 <memcpy>
  1c:   e28dd00c        add     sp, sp, #12
  20:   e49de004        pop     {lr}            ; (ldr lr, [sp], #4)
  24:   e12fff1e        bx      lr

00000028 <get16_unaligned>:
  28:   e1d000b0        ldrh    r0, [r0]
  2c:   e12fff1e        bx      lr

00000030 <put16_aligned>:
  30:   e1c010b0        strh    r1, [r0]
  34:   e12fff1e        bx      lr

00000038 <get16_aligned>:
  38:   e1d000b0        ldrh    r0, [r0]
  3c:   e12fff1e        bx      lr


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug target/64516] arm: wrong unaligned load generated
  2015-01-07  2:08 [Bug target/64516] New: arm: wrong unaligned load generated markus at oberhumer dot com
@ 2015-01-16 14:24 ` ramana at gcc dot gnu.org
  2015-02-05 12:11 ` [Bug target/64516] [4.x Regression] " markus at oberhumer dot com
  2015-02-05 12:34 ` markus at oberhumer dot com
  2 siblings, 0 replies; 4+ messages in thread
From: ramana at gcc dot gnu.org @ 2015-01-16 14:24 UTC (permalink / raw)
  To: gcc-bugs

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

Ramana Radhakrishnan <ramana at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-01-16
                 CC|                            |ramana at gcc dot gnu.org
     Ever confirmed|0                           |1
      Known to fail|                            |4.9.0, 4.9.1, 4.9.2, 5.0

--- Comment #1 from Ramana Radhakrishnan <ramana at gcc dot gnu.org> ---

Hmmm, I'm not sure if such type punning pushes the attributes through. We seem
to think that the alignment will be 16 bits and not 8 bits at expand time.

(insn 6 3 7 2 (set (reg:SI 115)
        (zero_extend:SI (mem:HI (reg/v/f:SI 112 [ p ]) [1 MEM[(const struct TU2
*)p_2(D)]+0 S2 A16]))) /tmp/al.c:17 -1
     (nil))


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug target/64516] [4.x Regression] arm: wrong unaligned load generated
  2015-01-07  2:08 [Bug target/64516] New: arm: wrong unaligned load generated markus at oberhumer dot com
  2015-01-16 14:24 ` [Bug target/64516] " ramana at gcc dot gnu.org
@ 2015-02-05 12:11 ` markus at oberhumer dot com
  2015-02-05 12:34 ` markus at oberhumer dot com
  2 siblings, 0 replies; 4+ messages in thread
From: markus at oberhumer dot com @ 2015-02-05 12:11 UTC (permalink / raw)
  To: gcc-bugs

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

Markus F.X.J. Oberhumer <markus at oberhumer dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to work|                            |4.3.3

--- Comment #2 from Markus F.X.J. Oberhumer <markus at oberhumer dot com> ---
This is a wrong code regression against gcc 4.3.3.

I've booted an older ARM Ubuntu box just for testing:

$ gcc -v
[...]
gcc version 4.3.3 (Ubuntu 4.3.3-5ubuntu4)

$ gcc -O3 -Wall -W -Wcast-align -c unaligned_load_bug.c

$ objdump -d unaligned_load_bug.o

unaligned_load_bug.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <put16_unaligned>:
   0:   e52de004        push    {lr}            ; (str lr, [sp, #-4]!)
   4:   e24dd00c        sub     sp, sp, #12     ; 0xc
   8:   e28d3008        add     r3, sp, #8      ; 0x8
   c:   e3a02002        mov     r2, #2  ; 0x2
  10:   e16310b2        strh    r1, [r3, #-2]!
  14:   e1a01003        mov     r1, r3
  18:   ebfffffe        bl      0 <memcpy>
  1c:   e28dd00c        add     sp, sp, #12     ; 0xc
  20:   e8bd8000        pop     {pc}

00000024 <get16_unaligned>:
  24:   e52de004        push    {lr}            ; (str lr, [sp, #-4]!)
  28:   e24dd00c        sub     sp, sp, #12     ; 0xc
  2c:   e1a01000        mov     r1, r0
  30:   e3a02002        mov     r2, #2  ; 0x2
  34:   e28d0006        add     r0, sp, #6      ; 0x6
  38:   ebfffffe        bl      0 <memcpy>
  3c:   e1dd00b6        ldrh    r0, [sp, #6]
  40:   e28dd00c        add     sp, sp, #12     ; 0xc
  44:   e8bd8000        pop     {pc}

00000048 <put16_aligned>:
  48:   e1c010b0        strh    r1, [r0]
  4c:   e12fff1e        bx      lr

00000050 <get16_aligned>:
  50:   e1d000b0        ldrh    r0, [r0]
  54:   e12fff1e        bx      lr


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug target/64516] [4.x Regression] arm: wrong unaligned load generated
  2015-01-07  2:08 [Bug target/64516] New: arm: wrong unaligned load generated markus at oberhumer dot com
  2015-01-16 14:24 ` [Bug target/64516] " ramana at gcc dot gnu.org
  2015-02-05 12:11 ` [Bug target/64516] [4.x Regression] " markus at oberhumer dot com
@ 2015-02-05 12:34 ` markus at oberhumer dot com
  2 siblings, 0 replies; 4+ messages in thread
From: markus at oberhumer dot com @ 2015-02-05 12:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Markus F.X.J. Oberhumer <markus at oberhumer dot com> ---
Just for reference, here is the expected result:

$ clang -target armv5-unknown-linux-gnu -marm -mfloat-abi=soft --version
clang version 3.5.1 (tags/RELEASE_351/final)
Target: armv5-unknown-linux-gnu
Thread model: posix

$ clang -target armv5-unknown-linux-gnu -marm -mfloat-abi=soft -O2 -Wall -W
-Wcast-align -c unaligned_load_bug.c

$ arm-linux-gnueabihf-objdump -d unaligned_load_bug.o

unaligned_load_bug.o:     file format elf32-littlearm

Disassembly of section .text:

00000000 <put16_unaligned>:
   0:   e5c01000        strb    r1, [r0]
   4:   e1a01421        lsr     r1, r1, #8
   8:   e5c01001        strb    r1, [r0, #1]
   c:   e12fff1e        bx      lr

00000010 <get16_unaligned>:
  10:   e5d01000        ldrb    r1, [r0]
  14:   e5d00001        ldrb    r0, [r0, #1]
  18:   e1810400        orr     r0, r1, r0, lsl #8
  1c:   e12fff1e        bx      lr

00000020 <put16_aligned>:
  20:   e1c010b0        strh    r1, [r0]
  24:   e12fff1e        bx      lr

00000028 <get16_aligned>:
  28:   e1d000b0        ldrh    r0, [r0]
  2c:   e12fff1e        bx      lr


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-02-05 12:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-07  2:08 [Bug target/64516] New: arm: wrong unaligned load generated markus at oberhumer dot com
2015-01-16 14:24 ` [Bug target/64516] " ramana at gcc dot gnu.org
2015-02-05 12:11 ` [Bug target/64516] [4.x Regression] " markus at oberhumer dot com
2015-02-05 12:34 ` markus at oberhumer dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).