<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63258>63258</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [AArch64] AddressSanitizer may generate unaligned access with -mstrict-align
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          vit9696
      </td>
    </tr>
</table>

<pre>
    Consider the following code snippet compiled with the latest `clang -target=aarch64-gnu-linux-eabi -O3 -mstrict-align -fsanitize=address`:

```c
extern unsigned long test[3];
unsigned long test[3];

void test_access(unsigned long *ptr);

__attribute__((noinline))
void test_access(unsigned long *ptr)
{
  ptr[0] = 3;
  ptr[1] = 3;
}

int main(void)
{
  test_access(&test[1]);

  while(1) {}
}
```

The produced assembly will look something like this:

```cpp
test_access:                            // @test_access
        sub     sp, sp, #32
        stp     x29, x30, [sp, #16]             // 16-byte Folded Spill
        add     x29, sp, #16
 mov     x8, #0x1000000000
        lsr     x9, x0, #3
        ldrh w8, [x9, x8]
        cbnz    w8, .LBB0_2
        mov     w8, #3
 dup     v0.2d, x8
        str     q0, [sp]
        ldr     x8, [sp, #8]
        str     x8, [x0, #8]
        ldr     x8, [sp]
 str     x8, [x0]
        ldp     x29, x30, [sp, #16]             // 16-byte Folded Reload
        add     sp, sp, #32
 ret
.LBB0_2:
        bl      __asan_report_store16
```

Line `ldrh w8, [x9, x8]` will cause unaligned access and crash on configurations where unaligned access checking is enabled.

This happens because `test` is given 16-byte alignment, yet `&test[1]` will thus be only 8-byte aligned. `8 >> 3` gives `1`, and thus we get odd shadow address, which is passed to `ldrh` causing alignment data abort.

One can play with the snippet on https://godbolt.org/z/7jc584efs.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVsGO4jgQ_RpzKYGMTUI4cIBmOI000s7ekWMXiWccO2s70MzXr5wQBmh6V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