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

    <tr>
        <th>Summary</th>
        <td>
            strange C++ unwind behavior on ppc64le with LTO program and -ftrivial-auto-var-init=zero libunwind
        </td>
    </tr>

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

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

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

<pre>
    Clang 17.0.6, ppc64le, musl libc, compiler-rt/libunwind/libc++, Chimera Linux.

Given the following program C++:

```
#include <cstdio>
#include <stdexcept>
#include <string>

using str = std::string;

int main(void) {
    printf("iter 1\n");
    if (true) {
        str foo1{"some random string"};
        try {
            throw std::runtime_error{"some random string"};
        } catch (std::runtime_error const &e) {
            if (foo1 != e.what()) {
 printf("BOO! '%s' '%s' %p\n", e.what(), foo1.data(), foo1.data());
            }
            printf("OK %p\n", foo1.data());
        }
    }
    printf("iter 2\n");
    if (true) {
        str foo2{"some random string"};
        try {
 throw std::runtime_error{"some random string"};
        } catch (std::runtime_error const &e) {
            if (foo2 != e.what()) {
 printf("BOO! '%s' '%s' %p\n", e.what(), foo2.data(), foo2.data());
            }
            printf("OK %p\n", foo2.data());
        }
    }
}
```

I get the following output:

```
iter 1
OK 0x3fffc2943891
iter 2
BOO! 'some random string' 'dom strisome ranng' 0x3fffc2943891
OK 0x3fffc2943891
```

When compiled with the following flags: `-O2 -flto=thin`, but particularly only when libunwind is compiled with `-ftrivial-auto-var-init=zero`. The prerequisite is always this combo; if I either compile the test program without LTO, or if I compile libunwind without any special flags, the test program works. It's always the second iteration that breaks. I am not sure whether this is a miscompilation problem of the test program, or of libunwind itself.

This was originally found by running the test suite of the `fmt` library, where the `ostream` tests fail like this. It's always every second throw, i.e. in a program with 4 iterations, two of them fail.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/e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