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

    <tr>
        <th>Summary</th>
        <td>
            Incorrect signed load generated on armv8 under UBSAN
        </td>
    </tr>

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

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

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

<pre>
    Compiling the following on armv8 with `-fsanitize=undefined` results in a UBSAN complaint:
```
#include <cstdint>

int main(){
  uint8_t n = 128;
  void *ptrs[200];
  ptrs[(int)n];
  return 0;
}
```

Which reports:
```
test.cpp:6:3: runtime error: index -128 out of bounds for type 'void *[200]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior test.cpp:6:3 in
```

I believe there is a signed byte load being incorrectly generated instead of an unsigned load in this case, and the resulting sign extension results in an overflow that ubsan detects: https://godbolt.org/z/7Y3f7oxGs

Changing the cast from `(int)` to `(unsigned)` appears to hide the issue.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8VMGO4ygQ_RpyKSXCEMf2wYeks1nNYfawrdZqTitsKnGtMFhQTnfP16_sOOme0e5IFhbvAcXjPTAp0cUj1iI_iPy4MiN3IdYeydnOxFUT7Hv9FPqBHPkLcIdwDs6F16kXPJjYX0t4Je5A7OT6nIwnpu8o9HH0Fs_k0YqdhIhpdJyAPBh4OTzv_4A29IMz5FnovZBHIfdiJ5fv1lWafOtGiyD0U5vYzoN_W9i5Jc_QG_JClUJVojjcYICRPJd_M3gQ-giZKoV-cNdAFoTaDxyTyA9KSpEfP_ELLlQ5FVSV_5GOyGP0IB-QKI7_LWBu_-qo7SDiECKn_9PKmHjTDoPQ-53Qey30HuLomXoEjDHECSBv8Q3WmSohjAzhDE0YvU1wDhH4fUAQqriL-xCmiluN55evX_d_fptWerl7c8DOXCnE58W3uc7DuXWz0PDz_oD8LyR_gQYd4RWnwEQESmBgDpqF5p0RXDAWGpxCRL4NMWLL7h0u6DEaRgvkE6Oxk0bjYfTL5HkeeeCOErQmoVBPYLydg3nL2LTmNBrwjdEnCv6H8HkIV4xnF16BO8MwNsl4sMjYzu5AxzzMNqmTUKdLsE1wvAnxItTpu1Cn4ps-F-Ht9_RZ8FNn_OV-QVqTGM4x9NOVeIRougUcFuguaMHNMKCJaeI7svOpAaU04mZla20rXZkV1tmuyPKyqjK16uoql7pSuixNWaHSRpalarZ2W1WmyEyuV1QrqVSmVCaVznK1aa3cbsui3Va70kqrxVZib8htnLv2k8DVXLLOq52WK2cadOn-LMR6GrRuxksSW-kocfqYxsQO6y93H-GzVx-OPh6LKV3x9gasxujqnw6cuBubTRt6oU5TieW3HmL4B1sW6jRvMwl1mnf6bwAAAP__Vs55sQ">