<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Hi Jerome,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<font size="2"><span style="font-size:11pt">> Note the 'ldr x9, [x0]'. At this point there is no guarantee that x0 is a multiple of 8, so why is Clang generating this code?<br>
</span></font></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I think the point is that it can assume it is 8 byte aligned, so the question is why it isn't.  I guess that requires looking into how memory is allocated to what C is pointing to, or if some type punning caused passing an address that is not properly aligned.</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Cheers,</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Sjoerd.<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> llvm-dev <llvm-dev-bounces@lists.llvm.org> on behalf of Jerome Forissier via llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Sent:</b> 01 June 2020 15:26<br>
<b>To:</b> llvm-dev@lists.llvm.org <llvm-dev@lists.llvm.org><br>
<b>Cc:</b> op-tee@lists.trustedfirmware.org <op-tee@lists.trustedfirmware.org><br>
<b>Subject:</b> [llvm-dev] Aarch64: unaligned access despite -mstrict-align</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Hi,<br>
<br>
I experienced a crash in code compiled with Clang 10.0.0 due to a<br>
misaligned 64-bit data access. The (ARMv8) CPU is configured with SCTL.A<br>
== 1 (alignment check enable). With SCTLR.A == 0 the code runs as expected.<br>
<br>
After some investigation I came up with the following reproducer:<br>
<br>
---8<-------8<-------8<-------8<-------8<-------8<-------8<-------<br>
$ cat test.c<br>
extern char *g;<br>
int memcmp(const void *s1, const void *s2, unsigned long n);<br>
<br>
int f(void *c)<br>
{<br>
        return memcmp(g, c, 16);<br>
}<br>
$ clang --target=aarch64-linux-gnu -Os -mstrict-align -S test.c<br>
$ cat test.s<br>
        .text<br>
        .file   "test.c"<br>
        .globl  f                       // -- Begin function f<br>
        .p2align        2<br>
        .type   f,@function<br>
f:                                      // @f<br>
// %bb.0:<br>
        adrp    x8, g<br>
        ldr     x10, [x8, :lo12:g]<br>
        ldr     x9, [x0]<br>
        ldr     x8, [x10]<br>
        rev     x9, x9<br>
        rev     x8, x8<br>
        cmp     x8, x9<br>
        b.ne    .LBB0_3<br>
// %bb.1:<br>
        ldr     x8, [x10, #8]<br>
        ldr     x9, [x0, #8]<br>
        rev     x8, x8<br>
        rev     x9, x9<br>
        cmp     x8, x9<br>
        b.ne    .LBB0_3<br>
// %bb.2:<br>
        mov     w0, wzr<br>
        ret<br>
.LBB0_3:<br>
        cmp     x8, x9<br>
        mov     w8, #-1<br>
        cneg    w0, w8, hs<br>
        ret<br>
.Lfunc_end0:<br>
        .size   f, .Lfunc_end0-f<br>
                                        // -- End function<br>
        .ident  "clang version 10.0.0-4ubuntu1 "<br>
        .section        ".note.GNU-stack","",@progbits<br>
        .addrsig<br>
---8<-------8<-------8<-------8<-------8<-------8<-------8<-------<br>
<br>
Note the 'ldr x9, [x0]'. At this point there is no guarantee that x0 is<br>
a multiple of 8, so why is Clang generating this code?<br>
<br>
Thanks,<br>
-- <br>
Jerome<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
llvm-dev@lists.llvm.org<br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</div>
</span></font></div>
</body>
</html>