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

    <tr>
        <th>Summary</th>
        <td>
            UBSan: Incorrect generated `__ubsan_handle_type_mismatch_v1`?
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    In apache/arrow we encountered a weird UBSan error (https://github.com/apache/arrow/pull/46124#issuecomment-2814416656) which seems to be a false alarm. A reduced case can be found [here](https://godbolt.org/z/GsYcn155q) .

To summarize, for function:
```
uint64_t read64(const uint64_t* src, size_t n) {
 uint64_t result = 0;
    std::memcpy(&result, src, n);
    return result;
}
```
A misaligned `src` shouldn't be considered UB because it is merely passed into `std::memcpy` as `void *` which requires no alignment. However the generated code jumps to `__ubsan_handle_type_mismatch_v1` once misaligned regardless of how it is used afterwards.

As a comparison, changing the pointer type from `uint64_t *` to `uint8_t *` gets the correct codegen - no alignment checking:
```
uint64_t read8(const uint8_t* src, size_t n) {
    uint64_t result = 0;
    std::memcpy(&result, src, n);
 return result;
}
```

The same behavior is observed on X86 as well, and as early as 18.1.0 (17.0.1 is fine) for both Arm and X86.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVEuP2zgT_DX0pTECRT0sH3TQZODvy3k3QPZktMi2xCwfDh82Zn79ghpnJ5MFNjksYMA2ya6uIqsLY9SLIxpZ98i6px3mtPowvqCzmHx9GNrd7NXz-NEBXlCuxMQRQ_A3uBGQkz67RIEUINxIBwWfHn9DBxSCD8DEsKZ0iayZmDgycVx0WvNcSW8LzDs8Jo6XbAwTx7avRctEo2PMJL215NKDGOq2rfu-65k4wG3VcoVIZCMkDzMBwhlNJECDwVYwQSCVJSmQGAkkunLo7LNTwLrHlQKx7umf_LyavUmVDwsTxxc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