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

    <tr>
        <th>Summary</th>
        <td>
            -fsanitize=cfi-icall leads to eliminated icalls with optimization levels above -O0
        </td>
    </tr>

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

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

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

<pre>
    `-fsanitize=cfi-call` does not emit CFI statements and eliminates the icall during code elimination, leading to broken binaries.

We have created a minimal example highlighting the issue. The following code gets compiled using clang 19.1.0 x86_64 and `-O3 -fsanitize=cfi-icall -flto`
```c
void (*pFunc)();

int main(int argc, char** argv) {
    pFunc = argv[1];
 pFunc();
}
```
to

```
main:
 mov    rax,QWORD PTR [rsi+0x8]
 mov    QWORD PTR [rip+0x2edd],rax        # 4018 <pFunc>
 ud1 eax,DWORD PTR [eax+0x2]
```


When setting the `-O0`, it does emit the icall as expected. We created a compiler explorer example: https://godbolt.org/z/P71hx6xqs

However, we have also found that initializing the `pFunc` seems to stop the code elimination:

```c
void test() {
    test();
}

//void (*pFunc)(); // Bad
void (*pFunc)() = &test; // Good

int main(int argc, char** argv) {
    pFunc = (void (*)())**argv;
 pFunc();
}
```
In that case the icall exists. Compiler explorer: https://godbolt.org/z/36hYWb7YW

We have also tested the previous clang version but cannot find a version where this compiles as expected.


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVVuvozYQ_jXOyygRmEDggYdcmnafdrta6WifKmMPwVtjp7Yh6fn1lQ3NSbKnF1WNEMGe2zfjb8bMOXnSiDXJdyQ_LNjgO2PrbxepVGOUWDRG_F6TIlm2jmnp5SuS7MBbueRMKVIkIAw60MYD9tLD_vgBnGcee9TeAdMCUMleaubRge8QZDAEMVipT8CNwJuCNJrQPShkIsi8gcaaX1FDIzWzEt2KJAeSbKf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