Repository: rust
Issue: rustc segfault with struct containing large array of fat pointers + a bool
Labels: I-crash,A-LLVM,T-compiler,C-bug

I'm not sure of the full cause to be quite honest. Some debugging notes from other Discord users that helped me reproduce and verify the issue:

* for one user, 4095 [for the array size] works fine, 4096 segfaults. 
* On i686, the size can go up to 8191 for another user before crashing
* Removing the `dirty` bool causes the error to go away for me
* Replacing the microbench call with `std::hint::black_box(DataBlock::make())` also causes the error to go away for me

### Code

Please see https://gist.github.com/drey7925/53e36a957ca54991e693b67098d51d12 for a full code listing

```Rust
use std::{any::Any, sync::Arc};

pub struct DataBlock {
    extended_data: [Option<Box<dyn Any>>; 4096],
    dirty: bool,
}
impl DataBlock {
    fn make() -> Result<DataBlock, ()> {
        Ok(DataBlock {
            extended_data: std::array::from_fn(|_| None),
            dirty: false,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use microbench::{self, Options};

    #[test]
    fn test_chunk_roundtrip() {
        let options = Options::default();
        microbench::bench(&options, "deserialize_chunk", || DataBlock::make());
    }
}
```

Cargo.toml:
```
[package]
name = "rustc-crash-repro"
version = "0.1.0"
edition = "2021"
[dependencies]
microbench = "0.5.0"
```

Cargo.lock:
```
version = 3

[[package]]
name = "microbench"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4c44e40aee4e6fd2f4257bb91e5948ce79285aeb949129448889cf2fbf6da0b"

[[package]]
name = "rustc-crash-repro"
version = "0.1.0"
dependencies = [
 "microbench",
]
```

To run: `cargo +stable test --release`

### 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.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-unknown-linux-gnu
release: 1.68.2
LLVM version: 15.0.6

Also reproduces on

rustc 1.68.2 (9eb3afe9e 2023-03-27)
binary: rustc
commit-hash: 9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0
commit-date: 2023-03-27
host: x86_64-pc-windows-msvc
release: 1.68.2
LLVM version: 15.0.6

and

rustc 1.66.0 (69f9c33d7 2022-12-12)
binary: rustc
commit-hash: 69f9c33d71c871fc16ac445211281c6e7a340943
commit-date: 2022-12-12
host: x86_64-unknown-linux-gnu
release: 1.66.0
LLVM version: 15.0.2
```


### Error output

```
process didn't exit successfully: `rustc --crate-name rustc_crash_repro --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=252 --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no --test -C metadata=d0f935590eb979e4 -C extra-filename=-d0f935590eb979e4 --out-dir [path]/target/release/deps -L dependency=[path]/target/release/deps --extern microbench=[path]/target/release/deps/libmicrobench-8b09c09bbd603738.rlib` (signal: 11, SIGSEGV: invalid memory reference)
```


<details><summary><strong>Backtrace</strong></summary>
<p>

```
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x32ae373)[0x7eff6797f373]
/lib/x86_64-linux-gnu/libc.so.6(+0x42520)[0x7eff64335520]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm12SelectionDAG18ReplaceAllUsesWithEPNS_6SDNodeEPKNS_7SDValueE+0x17c)[0x7eff6290f298]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(+0x5672916)[0x7eff62a72916]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm12SelectionDAG7CombineENS_12CombineLevelEPNS_9AAResultsENS_10CodeGenOpt5LevelE+0x17f1)[0x7eff62dba8f1]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm16SelectionDAGISel17CodeGenAndEmitDAGEv+0x6e)[0x7eff62ad65ee]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm16SelectionDAGISel20SelectAllBasicBlocksERKNS_8FunctionE+0x12c5)[0x7eff62dd93c7]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm16SelectionDAGISel20runOnMachineFunctionERNS_15MachineFunctionE+0x410)[0x7eff62e34f90]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(+0x5a34b53)[0x7eff62e34b53]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm13FPPassManager13runOnFunctionERNS_8FunctionE+0xafd)[0x7eff62aa70bd]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE+0x2f)[0x7eff62aa6589]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libLLVM-15-rust-1.68.2-stable.so(_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE+0x21a)[0x7eff62aebd1a]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x27dd132)[0x7eff66eae132]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x27dcad5)[0x7eff66eadad5]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x27daa0a)[0x7eff66eaba0a]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x27d786d)[0x7eff66ea886d]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x27d6834)[0x7eff66ea7834]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x27d56a3)[0x7eff66ea66a3]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-93862cade34bf92c.so(+0x27d5406)[0x7eff66ea6406]
/home/drey/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libstd-bc6b80525d6b1f3b.so(rust_metadata_std_f8b79e573431a86c+0x10e823)[0x7eff64644823]
/lib/x86_64-linux-gnu/libc.so.6(+0x94b43)[0x7eff64387b43]
/lib/x86_64-linux-gnu/libc.so.6(+0x126a00)[0x7eff64419a00]

or on Windows:

 # Child-SP          RetAddr               Call Site
00 00000077`430fba10 00007ffe`c0bd8fc9     rustc_driver_4de585d93dcd8eb1!llvm::SelectionDAG::ReplaceAllUsesWith+0x1a0
01 00000077`430fbb90 00007ffe`c0bd1dc3     rustc_driver_4de585d93dcd8eb1!llvm::DenseMapBase<llvm::SmallDenseMap<llvm::SDNode * __ptr64,llvm::detail::DenseSetEmpty,16,llvm::DenseMapInfo<llvm::SDNode * __ptr64,void>,llvm::detail::DenseSetPair<llvm::SDNode * __ptr64> >,llvm::SDNode * __ptr64,llvm::detail::DenseSetEmpty,llvm::DenseMapInfo<llvm::SDNode * __ptr64,void>,llvm::detail::DenseSetPair<llvm::SDNode * __ptr64> >::moveFromOldBuckets+0x4109
02 00000077`430fc660 00007ffe`c0ad16c8     rustc_driver_4de585d93dcd8eb1!llvm::SelectionDAG::Combine+0x17b3
03 00000077`430fcf70 00007ffe`c0ad0540     rustc_driver_4de585d93dcd8eb1!llvm::SelectionDAGISel::CodeGenAndEmitDAG+0xd8
04 00000077`430fd390 00007ffe`c0ace9e7     rustc_driver_4de585d93dcd8eb1!llvm::SelectionDAGISel::SelectAllBasicBlocks+0xdb0
05 00000077`430fd760 00007ffe`bf5bd3d9     rustc_driver_4de585d93dcd8eb1!llvm::SelectionDAGISel::runOnMachineFunction+0x3b7
06 00000077`430fd890 00007ffe`c0580888     rustc_driver_4de585d93dcd8eb1!llvm::AVRDAGToDAGISel::`scalar deleting destructor'+0xb9
07 00000077`430fd8e0 00007ffe`c0409c61     rustc_driver_4de585d93dcd8eb1!llvm::MachineFunctionPass::runOnFunction+0x118
08 00000077`430fde70 00007ffe`c0411ae1     rustc_driver_4de585d93dcd8eb1!llvm::FPPassManager::runOnFunction+0x571
09 00000077`430fe170 00007ffe`bf4e20ad     rustc_driver_4de585d93dcd8eb1!llvm::FPPassManager::runOnModule+0x31
0a 00000077`430fe1c0 00007ffe`bf48e82c     rustc_driver_4de585d93dcd8eb1!llvm::legacy::PassManagerImpl::run+0x27d
0b 00000077`430fe300 00007ffe`bf458b97     rustc_driver_4de585d93dcd8eb1!LLVMRustWriteOutputFile+0x3dc
0c 00000077`430fe5a0 00007ffe`bf45c1bb     rustc_driver_4de585d93dcd8eb1!RNvYNtNtNtNtCs2vRy9Nn98DZ_12rustc_middle2ty6consts4kind9ConstKindNtNtB8_5visit13TypeVisitable23has_escaping_bound_varsCs753WWl4Jsqx_16rustc_hir_typeck+0xf47
0d 00000077`430fe6d0 00007ffe`bf472d80     rustc_driver_4de585d93dcd8eb1!RNvNtNtCse1ZkExa1DRd_18rustc_codegen_llvm4back5write7codegen+0x40b
0e 00000077`430fe9d0 00007ffe`bf466d5e     rustc_driver_4de585d93dcd8eb1!RINvNtNtCsf7Ykt4ZYILz_17rustc_codegen_ssa4back5write19start_async_codegenNtCse1ZkExa1DRd_18rustc_codegen_llvm18LlvmCodegenBackendEB1b_+0xa8c0
0f 00000077`430feca0 00007ffe`bf408a59     rustc_driver_4de585d93dcd8eb1!RINvNtNtCsf7Ykt4ZYILz_17rustc_codegen_ssa4back5write17execute_work_itemNtCse1ZkExa1DRd_18rustc_codegen_llvm18LlvmCodegenBackendEB19_+0x35e
10 00000077`430ff080 00007ffe`bf438377     rustc_driver_4de585d93dcd8eb1!RINvNtNtCs4shZNHyK4px_3std10sys_common9backtrace28___rust_begin_short_backtraceNCINvXs0_Cse1ZkExa1DRd_18rustc_codegen_llvmNtB1o_18LlvmCodegenBackendNtNtNtCsf7Ykt4ZYILz_17rustc_codegen_ssa6traits7backend19ExtraBackendMethods18spawn_named_threadNCINvNtNtB2s_4back5write10spawn_workB1W_E0uE0uEB1o_+0xf9
11 00000077`430ff590 00007fff`acf0021c     rustc_driver_4de585d93dcd8eb1!RINvXs1C_NtCs4hXP7hK1Gxt_10rustc_span6def_idNtB7_10LocalDefIdNtNtCs1RoSZkFyxqL_4core4hash4Hash4hashNtCscaonAXChtWT_10rustc_hash8FxHasherECskWiUba3cX2K_18rustc_monomorphize+0x6d7
12 (Inline Function) --------`--------     std_9f65829977a28b3f!alloc::boxed::impl$45::call_once+0xb [/rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library\alloc\src\boxed.rs @ 1988] 
13 (Inline Function) --------`--------     std_9f65829977a28b3f!alloc::boxed::impl$45::call_once+0x16 [/rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library\alloc\src\boxed.rs @ 1988] 
14 00000077`430ff920 00007fff`c4c5269d     std_9f65829977a28b3f!std::sys::windows::thread::impl$0::new::thread_start+0x4c [/rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library\std\src\sys\windows\thread.rs @ 56] 
15 00000077`430ff9b0 00007fff`c662a9f8     KERNEL32!BaseThreadInitThunk+0x1d
16 00000077`430ff9e0 00000000`00000000     ntdll!RtlUserThreadStart+0x28

```

</p>
</details>