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

    <tr>
        <th>Summary</th>
        <td>
            [libLLVM] Function scoped statics cause segfaults when main thread exits during compilation
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          patrick-rivos
      </td>
    </tr>
</table>

<pre>
    Reproducer: https://github.com/patrick-rivos/llvm-function-scoped-statics

https://reviews.llvm.org/D129120 (and many other commits) removed uses of ManagedStatic and replaced the uses with a function-scoped static. This introduced subtle issues for multithreaded programs that dlopen libLLVM (eg. users of the jit).

Since static objects are destructed in reverse order of registration (alongside atexit handlers), multithreaded programs can encounter a situation where the main thread calls exit() and starts executing function-scoped static destructors. This can result in failed assertions or segfaults when compiling as the function-scoped static objects have been pulled out from under libLLVM.

Moving function-scoped statics back to ManagedStatic would fix this issue. Not sure if there are complexities with that approach.

Some relevant commits: https://github.com/llvm/llvm-project/commit/ede600377cb6df1bef71f070130d8cfe734cc5b7 https://github.com/llvm/llvm-project/commit/c4ccf608c2380796d64db108eed3b24c1b0140ef https://github.com/llvm/llvm-project/commit/ebbbd93e3cd8c91b5ac01540c05c619236a90289

cc @nhaehnle 
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVE1v3DYQ_TXcy8ALipJW0kEHt8ae4h6aovchOZKYUKTAj3Xy7wtKa7cx4AJBroKG72PeG4zRzI5oZO1vrH06YU6LD-OGKRj19SGYm48n6fX38U_agtdZUWD1IywpbZHVj0xcmbjOJi1ZnpVfmbj-MMvE1drb-jBlp5Lx7iEqv5F-iAmTUZHxR8Yff3ws0M3QSzyXubMPMxPXp0oMleDARI9Ow4ruO_i0UADl19WkyMQAgVZ_Iw05UgQ_wTM6nEl_3pGgzAXaLCrSkBY6fnsxaQGEd-zgYH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