Skip to content

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

Description

@saethlin

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.

Activity

  1. added
    A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.
    C-bugCategory: This is a bug.
    A-mir-optArea: MIR optimizations
    on Aug 22, 2023
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Aug 22, 2023
  3. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Aug 22, 2023
  4. added this to the 1.72.0 milestone on Aug 22, 2023
  5. cuviper commented on Aug 22, 2023

    @cuviper
    Member

    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.

  6. apiraino commented on Aug 23, 2023

    @apiraino
    Contributor

    WG-prioritization assigning priority (Zulip discussion).

    @rustbot label -I-prioritize +P-critical

  7. added and removed
    I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}
    on Aug 23, 2023
  8. cuviper commented on Aug 23, 2023

    @cuviper
    Member

    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.

  9. cuviper commented on Aug 23, 2023

    @cuviper
    Member

    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)
    
  10. 1 remaining item

  11. added a commit that references this issue on Aug 23, 2023
  12. cuviper commented on Aug 23, 2023

    @cuviper
    Member

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

  13. modified the milestones: 1.72.0, 1.73.0 on Aug 23, 2023
  14. added a commit that references this issue on Aug 27, 2023
  15. cuviper commented on Aug 27, 2023

    @cuviper
    Member

    Reopening until we solve 1.73-beta too.

  16. linked a pull request that will close this issue[beta] backports #115442on Sep 1, 2023
  17. cuviper commented on Sep 7, 2023

    @cuviper
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-debuginfoArea: Debugging information in compiled programs (DWARF, PDB, etc.)A-mir-optArea: MIR optimizationsC-bugCategory: This is a bug.P-criticalCritical priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions