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

    <tr>
        <th>Summary</th>
        <td>
            Out of bounds CFI instruction access in ASMPrinter
        </td>
    </tr>

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

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

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

<pre>
    We've observed an LTO crash when building OpenCV as part of an internal codebase, resulting from an out-of-bounds vector access at:

https://github.com/llvm/llvm-project/blob/dbead2388b48c1730f54f9aef074a81254573797/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp#L1272

The crash bisects to https://reviews.llvm.org/D114547, but it's not clear if that diff is responsible or just exposing an existing issue. The unchecked out-of-bounds access was resulting in non-deterministic errors, so you'll need to either add bounds checking to make it deterministic (such as the assertion in https://reviews.llvm.org/D126919), or build with bounds checking enabled in your STL (e.g. `-D_GLIBCXX_ASSERTIONS`).

I've reduced it down to the [attached IR file](https://github.com/llvm/llvm-project/files/8827828/reduced.ll.txt); the following should reproduce the issue with either the above assertion added or bounds checking enabled in the STL (it might be named `reduced.ll.txt` instead of `reduced.ll` because of silly Github issue attachment file extension limitations, sorry):

```
llc -homogeneous-prolog-epilog reduced.ll
```

Unfortunately that reduction is incredibly finicky; innocuous things like removing redundant basic blocks or stripping debug info will cause the issue to no longer reproduce. I assume that what we really want here is a MIR test case, and that reducing the MIR would be far less finicky. I don't have any experience with MIR though; I tried following the steps in https://llvm.org/docs/MIRLangRef.html#testing-individual-code-generation-passes but wasn't able to create an MIR repro.

I'd really appreciate help with debugging and fixing the issue here and getting a better repro. CC @momchil-velikov, author of https://reviews.llvm.org/D114547, @efriedma-quic, reviewer of that diff, and @kyulee-com, author of the homogeneous prolog epilog pass.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVlFv2zYQ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