Repository: rust
Issue: capturing stack backtrace becomes slower and sometimes segfaults on Apple Silicon
Labels: A-runtime,I-crash,A-LLVM,O-macos,T-compiler,C-bug,O-AArch64

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

Sorry for not having a MCVE for this issue. I'm still constructing it. For now I have to refer to a large codebase to reproduce the issue.

Basically, after we upgrade from nightly-2022-07-29 to nightly-2022-10-16, we found two things:

* Capturing stack backtrace is much slower than before. A single capture can take as long as 300 ms. Previously the Backtrace::capture() at the same code location only takes 1ms. Most of the time are spent on libunwind's CFI_Parser. https://github.com/risingwavelabs/risingwave/issues/6131
* Sometimes capturing a stack backtrace will segfault. https://github.com/risingwavelabs/risingwave/issues/6205#issuecomment-1313050193

This issue is stably reproducible on some specific commits of our project with some specific way of compiling, so I guess it's probably not related to incremental compile. I guess it would be more likely to be a problem with the LLVM 15 upgrade in August or the std::backtrace::Backtrace stabilization.

**Reproduce 1**

On this commit: https://github.com/risingwavelabs/risingwave/commit/227e9e5acd37073e4c2afed09294f509d371fc52

```
RUST_BACKTRACE=1 cargo run --bin risingwave -- playground
```

In another terminal, use psql (Postgres's client) to connect to the program:

```
psql -h localhost -p 4566 -d dev -U root
CREATE TABLE t(a int, b int);
CREATE VIEW v AS SELECT * FROM t;
DROP TABLE t;
```

The program will immediately segfault in Backtrace::capture.

Interestingly, if we use `cargo build -p risingwave_cmd_all && ./target/debug/risingwave playground`, it works. The commit following the buggy commit https://github.com/risingwavelabs/risingwave/commit/604a0a5f38ac1bdaaa5ab77b9eade74af65f202c also magically resolves the issue with some random code change.

**Reproduce 2**

On this commit: https://github.com/risingwavelabs/risingwave/commit/484b9ab27d7fd5612e78f02af70cd5aca0a29daf

```
cargo build -p risingwave_cmd
RUST_BACKTRACE=1 ./target/debug/meta-node # in terminal 1
RUST_BACKTRACE=1 ./target/debug/compute-node # in terminal 2
RUST_BACKTRACE=1 ./target/debug/frontend # in terminal 3
```

In another terminal:

```
psql -h localhost -p 4566 -d dev -U root
CREATE TABLE BOOLTBL2 (f1 bool); INSERT INTO BOOLTBL2 (f1) VALUES (bool 'XXX');
```

compute-node will also immediately segfault when capturing a backtrace.

Thanks for investigating into this!

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.66.0-nightly (b8c35ca26 2022-10-15)
binary: rustc
commit-hash: b8c35ca26b191bb9a9ac669a4b3f4d3d52d97fb1
commit-date: 2022-10-15
host: aarch64-apple-darwin
release: 1.66.0-nightly
LLVM version: 15.0.2
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>

```
<backtrace>
```

</p>
</details>