From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 23570 invoked by alias); 17 Apr 2012 07:08:05 -0000 Received: (qmail 23543 invoked by uid 22791); 17 Apr 2012 07:08:03 -0000 X-SWARE-Spam-Status: No, hits=-3.5 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00,TW_CP X-Spam-Check-By: sourceware.org Received: from localhost (HELO gcc.gnu.org) (127.0.0.1) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 17 Apr 2012 07:07:16 +0000 From: "jquesnelle at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/53016] New: memcpy optimization can cause unaligned access on ARM Date: Tue, 17 Apr 2012 07:08:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jquesnelle at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Changed-Fields: Message-ID: X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated Content-Type: text/plain; charset="UTF-8" MIME-Version: 1.0 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2012-04/txt/msg01360.txt.bz2 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53016 Bug #: 53016 Summary: memcpy optimization can cause unaligned access on ARM Classification: Unclassified Product: gcc Version: 4.4.3 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned@gcc.gnu.org ReportedBy: jquesnelle@gmail.com Created attachment 27174 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=27174 reproduction files The built-in memcpy that -O2 substitutes in seems to cause an unaligned memory access on ARMv5TE when structs are stacked in a certain way. I originally discovered this when a release build of native code for inclusion in an Android program caused a SIGBUS. Attached is a simple test case that replicates this on Android. There is no main() function but it should be trivial to substitute in (sorry, I don't have access to a regular ARM Linux box). It appears to involve over-aggressive use of ldm/stm (possibly ignoring padding?). Works fine (-O0): memcpy((void*)&parent.children[2],(const void*)child3,size); 24: 4b0a ldr r3, [pc, #40] 26: 447b add r3, pc 28: 1c19 adds r1, r3, #0 2a: 3138 adds r1, #56 2c: 4b09 ldr r3, [pc, #36] 2e: 447b add r3, pc 30: 681b ldr r3, [r3, #0] 32: 9a03 ldr r2, [sp, #12] 34: 1c08 adds r0, r1, #0 36: 1c11 adds r1, r2, #0 38: 1c1a adds r2, r3, #0 3a: f7ff fffe bl 0 Gives SIGBUS (-O2): memcpy((void*)&parent.children[2],(const void*)child3,size); 2: 4b07 ldr r3, [pc, #28] 4: 4907 ldr r1, [pc, #28] 6: 447b add r3, pc 8: 681a ldr r2, [r3, #0] a: 4479 add r1, pc c: 3138 adds r1, #56 e: 1c0b adds r3, r1, #0 10: 323c adds r2, #60 12: ca31 ldmia r2!, {r0, r4, r5} <--- Unaligned access 14: c331 stmia r3!, {r0, r4, r5} 16: ca13 ldmia r2!, {r0, r1, r4} 18: c313 stmia r3!, {r0, r1, r4} 1a: 6812 ldr r2, [r2, #0] 1c: 601a str r2, [r3, #0] I have confirmed this both on a TI OMAP 3530 (BeagleBoard) and Samsung Exynos 3110 (Samsung Epic 4G). I'm not sure if this is the same as bug #47754.