Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalarReplacementOfAggregates causes rustc to emit invalid LLVM debuginfo (and sometimes rustc segfaults) #115113

Closed
saethlin opened this issue Aug 22, 2023 · 7 comments · Fixed by #115139 or #115442
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. P-critical Critical priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Milestone

Comments

@saethlin
Copy link
Member

https://rust.godbolt.org/z/9YvvvcMKj

pub struct Endian;

#[allow(dead_code)]
pub struct EndianSlice<'input> {
    slice: &'input [u8],
    endian: Endian,
}

pub fn test(s: &[u8]) {
    let slice = EndianSlice { slice: s, endian: Endian };
}

Compiled with

--emit=llvm-ir -C debuginfo=2 -Zmir-opt-level=0 -Zmir-enable-passes= ScalarReplacementOfAggregates

Becomes this:

define void @example::test(ptr align 1 %s.0, i64 %s.1) unnamed_addr #0 !dbg !7 {
start:
  %slice.dbg.spill1 = alloca { ptr, i64 }, align 8
  %slice.dbg.spill = alloca %Endian, align 1
  %s.dbg.spill = alloca { ptr, i64 }, align 8
  %0 = getelementptr inbounds { ptr, i64 }, ptr %s.dbg.spill, i32 0, i32 0
  store ptr %s.0, ptr %0, align 8
  %1 = getelementptr inbounds { ptr, i64 }, ptr %s.dbg.spill, i32 0, i32 1
  store i64 %s.1, ptr %1, align 8
  call void @llvm.dbg.declare(metadata ptr %s.dbg.spill, metadata !22, metadata !DIExpression()), !dbg !30
  call void @llvm.dbg.declare(metadata ptr %slice.dbg.spill, metadata !23, metadata !DIExpression()), !dbg !31
  %2 = getelementptr inbounds { ptr, i64 }, ptr %slice.dbg.spill1, i32 0, i32 0, !dbg !32
  store ptr %s.0, ptr %2, align 8, !dbg !32
  %3 = getelementptr inbounds { ptr, i64 }, ptr %slice.dbg.spill1, i32 0, i32 1, !dbg !32
  store i64 %s.1, ptr %3, align 8, !dbg !32
  call void @llvm.dbg.declare(metadata ptr %slice.dbg.spill1, metadata !23, metadata !DIExpression()), !dbg !31
  ret void, !dbg !33
}
<snip>
!22 = !DILocalVariable(name: "s", arg: 1, scope: !7, file: !8, line: 9, type: !12)
!23 = !DILocalVariable(name: "slice", scope: !24, file: !8, line: 10, type: !25, align: 8)

ScalarReplacementOfAggregates is on by default in the stable toolchain that is currently being released, and it was turned on in #112002. There is an LLVM assertion which can catch some manifestations of this problem, but in the wild it seems rather hard to actually hit the assertion.

To the best of my knowledge https://bugzilla.redhat.com/show_bug.cgi?id=2226564 is the closest we have to someone hitting this in the wild. I hit it on my own by using -Cdebuginfo=2 -Zinline-mir-threshold=1000 -Zinline-mir-hint-threshold=1000 with -Zbuild-std.

If this becomes a problem when 1.72 goes out, an emergency fix would be just reverting the change to is_enabled in the linked PR.

@saethlin saethlin added A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. C-bug Category: This is a bug. A-mir-opt Area: MIR optimizations labels Aug 22, 2023
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Aug 22, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 22, 2023
@cuviper cuviper added this to the 1.72.0 milestone Aug 22, 2023
@cuviper
Copy link
Member

cuviper commented Aug 22, 2023

If this becomes a problem when 1.72 goes out, an emergency fix would be just reverting the change to is_enabled in the linked PR.

Since I'm the one with the "wild" bugzilla, I do plan to apply such a patch when I update to 1.72 in Fedora.

@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize P-critical

@rustbot rustbot added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Aug 23, 2023
@cuviper
Copy link
Member

cuviper commented Aug 23, 2023

I can reproduce this building release cargo with debuginfo:

~/rust/cargo$ RUSTFLAGS=-Cdebuginfo=2 cargo  1.72.0 build --release
[...]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/librustc_driver-bcfc773c3b04a145.so( 0x3261f03)[0x7f0b73e61f03]
/lib64/libc.so.6( 0x3db70)[0x7f0b708c4b70]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so( 0x651af8d)[0x7f0b6dd1af8d]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so( 0x653f8d6)[0x7f0b6dd3f8d6]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DwarfCompileUnit24constructVariableDIEImplERKNS_11DbgVariableEb 0x1e10)[0x7f0b6d47bd10]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE 0x517)[0x7f0b6d50c457]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE 0xae5)[0x7f0b6d50ca25]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE 0xae5)[0x7f0b6d50ca25]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE 0xae5)[0x7f0b6d50ca25]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DwarfCompileUnit25createAndAddScopeChildrenEPNS_12LexicalScopeERNS_3DIEE 0x7d6)[0x7f0b6d50c716]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DwarfCompileUnit27constructSubprogramScopeDIEEPKNS_12DISubprogramEPNS_12LexicalScopeE 0xe9)[0x7f0b6d6c90a9]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm10DwarfDebug15endFunctionImplEPKNS_15MachineFunctionE 0x242)[0x7f0b6d50a882]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm16DebugHandlerBase11endFunctionEPKNS_15MachineFunctionE 0x5e)[0x7f0b6d674bb6]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm10AsmPrinter16emitFunctionBodyEv 0x2d9a)[0x7f0b6d5c5dae]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so( 0x5dc28ae)[0x7f0b6d5c28ae]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm13FPPassManager11runOnModuleERNS_6ModuleE 0xb91)[0x7f0b6d55aeeb]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libLLVM-16-rust-1.72.0-stable.so(_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE 0x26d)[0x7f0b6d5ea839]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/librustc_driver-bcfc773c3b04a145.so( 0x28049b6)[0x7f0b734049b6]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/librustc_driver-bcfc773c3b04a145.so( 0x2803efd)[0x7f0b73403efd]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/librustc_driver-bcfc773c3b04a145.so( 0x280209f)[0x7f0b7340209f]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/librustc_driver-bcfc773c3b04a145.so( 0x27200e3)[0x7f0b733200e3]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/librustc_driver-bcfc773c3b04a145.so( 0x271da70)[0x7f0b7331da70]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/librustc_driver-bcfc773c3b04a145.so( 0x2728fa6)[0x7f0b73328fa6]
/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/lib/libstd-ae1700406ab17f0e.so(rust_metadata_std_ce96d94f7d822b06 0x10b275)[0x7f0b70b70275]
/lib64/libc.so.6( 0x8c907)[0x7f0b70913907]
/lib64/libc.so.6( 0x112870)[0x7f0b70999870]
error: could not compile `cargo` (lib)

Caused by:
  process didn't exit successfully: `/home/jistone/.rustup/toolchains/1.72.0-x86_64-unknown-linux-gnu/bin/rustc --crate-name cargo [...] (signal: 11, SIGSEGV: invalid memory reference)

That's 1.72.0 from the dev-static pre-release build. It also reproduces with rustc 1.73.0-beta.1 (680cdf816 2023-08-21) and rustc 1.74.0-nightly (c469197b1 2023-08-22), which are both on LLVM 17.

@cuviper
Copy link
Member

cuviper commented Aug 23, 2023

That also does trip with LLVM assertions:

rustc: /checkout/src/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp:323: void llvm::DbgVariable::addMMIEntry(const DbgVariable &): Assertion `(FrameIndexExprs.size() == 1 || llvm::all_of(FrameIndexExprs, [](FrameIndexExpr &FIE) { return FIE.Expr && FIE.Expr->isFragment(); })) && "conflicting locations for variable"' failed.
error: could not compile `cargo` (lib)

wesleywiser added a commit to wesleywiser/rust that referenced this issue Aug 23, 2023
Turn off the MIR SROA optimization by default to prevent incorrect
debuginfo generation and rustc ICEs caused by invalid LLVM IR being
created.

Related to rust-lang#115113
bors added a commit to rust-lang-ci/rust that referenced this issue Aug 23, 2023
…iper

Disable MIR SROA optimization by default

Turn off the MIR SROA optimization by default to prevent incorrect debuginfo generation and rustc ICEs caused by invalid LLVM IR being created.

Related to rust-lang#115113

r? `@cuviper`
cc `@saethlin`
@cuviper
Copy link
Member

cuviper commented Aug 23, 2023

Since #115140 just landed on stable for 1.72, I'm moving this to the 1.73 milestone.

@cuviper
Copy link
Member

cuviper commented Aug 27, 2023

Reopening until we solve 1.73-beta too.

@cuviper cuviper reopened this Aug 27, 2023
@cuviper cuviper reopened this Aug 29, 2023
@cuviper cuviper linked a pull request Sep 1, 2023 that will close this issue
@cuviper
Copy link
Member

cuviper commented Sep 7, 2023

Hmm, the backport did merge, but I guess this didn't auto-close because the PR wasn't to master?

@cuviper cuviper closed this as completed Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-debuginfo Area: Debugging information in compiled programs (DWARF, PDB, etc.) A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. P-critical Critical priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
4 participants