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

    <tr>
        <th>Summary</th>
        <td>
            Hwasan-instrumented code not propagating tag on global
        </td>
    </tr>

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

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

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

<pre>
    Clang seems to be generating problematic assembly with hwasan for the following code:

```
#pragma clang optimize off

struct trace_string_ref {
  unsigned int encoded_value;
  const char* inline_string;
};

void func(void) {
  __attribute__((unused)) struct trace_string_ref __trace_category_ref;
}
```

When compiled with `clang -target aarch64-unknown-linux-gnu -fsanitize=hwaddress -ftrivial-auto-var-init=pattern -std=c11 -c no_optimization.c -S -o - -O1` this assembly is produced in `func`:

```
        adrp    x8, :pg_hi21_nc:.L__const.func.__trace_category_ref
        add     x1, x8, :lo12:.L__const.func.__trace_category_ref
        mov     x0, sp
        mov     x2, #16
        bl      __hwasan_memcpy
```

The issue here is that `x1` will not contain the tag of `.L__const.func.__trace_category_ref` so the call to `__hwasan_memcpy` will result in a tag mismatch comparing the untagged `x1` to the actual tag for `.L__const.func.__trace_category_ref` stored in shadow. What should be the correct assembly is something like:

```
        adrp    x1, :pg_hi21_nc:.L__const.func.__trace_category_ref
        movk    x1, #:prel_g3:.L__const.func.__trace_category_ref+4294967296
        add     x1, x1, :lo12:.L__const.func.__trace_category_ref
        mov     w8, #16
        mov     w2, w8
        bl      __hwasan_memcpy
```

where the `movk + prel_g3` propagates the tag. I'm able to produce the correct assembly if `-O1` is omitted. If I keep the `-O1` and omit the `#pragma clang optimize off`, this snippet gets optimized out. Seems like there's maybe something in the `-O1` that conflicts with `#pragma clang optimize off` that leads to this.

A couple other things:
- `.L__const.func.__trace_category_ref` is definitely tagged (`.set .L__const.func.__trace_category_ref, .L__const.func.__trace_category_ref.hwasan+4899916394579099648`)
- The final executable this is a part of does contain the corresponding descriptor in `hwasan_globals`, so shadow for this global is properly setup.

I produced this with clang revision `cdb30f7a26354b000310ebe30a6874f7737675ed`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytVl1vpDYU_TXMiwXiYwaGh3nIJqqaqlIrbaV9RAYbcGMwss1M0l_fYwOT7Eey2XZHaMBwfX1877nnulbs6XQr6dgRw_lgiFWk5qTjI9fUCryetKolHzBoCDWGD7V8Ihdhe9JfqKEjaZUmtue4S6kubkqjGA-ymyC-C-LtP4_Xaxmm2aRpN1DS-LXVZMUg_uFEte3LacbqubHEatrwCgN4rzRvSVB8WAwImUcjupEzIkZL-OjWZtWZyhkQrkaNGo0lTU91kN7AUopx83e1Coq752f_f1aCkXYemyA9uucgLV-uXFXUwkU9W15VMME1j7Phzs6Zvga-qpZXDbW8U_rJvfwMxTcj5v8_9XzEZoZJSOzYZwEWSwxDS3XHLaFUN32-D-fxYVSXMcRe58ewG2cStsiXsIhzkN0he4xpbgxeA9tZUBnS2arwTHUoYAabCRvkeiShsQzDJklI2JBRVWu-QAo1Rg0JP5JQkZCEfySAAzYI88wVPINDbG58jhxeH1Js7E2KkPVHmZ7c_RHxvSWYM3VVL9Kkgo_sJvq9qnxyI-c0-mZov3DH_P0xce6uTqVK0v_iblDnxV3sHJnpla-pXybNkvxzg1ou96paiqka-NBMT28w4C9UmjBm5qTn2j0i2tS6qD762F-ElMiQdZS3FPF2pWkpSqx1Ru_ZILwY5ec1FM6gCHj1JcJtKTBoltYllvplBmGgFU3vWUod572nGWC6Dgy4ArXLErSxM5V-qhOSH4BolV4YZXrK1CUin1wgTK9myZyG-Q0orTlq8CUZjRo4GApgUjx8R6e-ImHyU0gIXjy8cJdmzqPmsuqyd7pLP-zTcl_mRVrmbxI8-UkEvxxfofDVwHMcZv-T4BfPa5c8fPCBwmbJFh0kHmIy0Q6AzcbtiNwHaTEQikbleLXKzSsM8HWwKhXooAYBkWPw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