rust icon indicating copy to clipboard operation
rust copied to clipboard

rustc 1.78.0 segfault on MacOS / M1

Open dwrensha opened this issue 1 year ago • 5 comments

I get a segfault when I try to run cargo test on the following code:

# Cargo.toml

[package]

name = "rustc-segfault"
edition = "2021"
// src/lib.rs

#[repr(C, align(8))]
pub struct ConstantReader<T> {
    pub phantom: core::marker::PhantomData<T>,
    pub words: &'static [u64],
}

pub mod test_constants {
  pub static VOID_LIST_CONST: crate::ConstantReader<usize> = {
    pub static WORDS: [u64; 1] = [0];
    crate::ConstantReader {
      phantom: ::std::marker::PhantomData,
      words: &WORDS,
    }
  };
}

#[test]
fn test_constants() {
    let _ = test_constants::VOID_LIST_CONST.words.as_ptr();
}

 % rustc -vV
rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: aarch64-apple-darwin
release: 1.78.0
LLVM version: 18.1.2

 % cargo test
   Compiling rustc-segfault v0.0.0 (/Users/dwrensha/Desktop/rustc-segfault)
error: could not compile `rustc-segfault` (lib test)

Caused by:
  process didn't exit successfully: `/Users/dwrensha/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc --crate-name rustc_segfault --edition=2021 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=179 --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --test -C metadata=47a41d92fd841df5 -C extra-filename=-47a41d92fd841df5 --out-dir /Users/dwrensha/Desktop/rustc-segfault/target/debug/deps -C incremental=/Users/dwrensha/Desktop/rustc-segfault/target/debug/incremental -L dependency=/Users/dwrensha/Desktop/rustc-segfault/target/debug/deps` (signal: 11, SIGSEGV: invalid memory reference)

I see the same behavior with the latest nightly rustc. I see no error with rustc 1.77.0 or earlier. It also works fine on Linux, and on an older Intel Macbook.

I also updated to the latest XCode to make sure all parts of my toolchain were up to date. Doing so did not make the segfault go away.

dwrensha avatar May 12 '24 21:05 dwrensha

The problem disappears if I turn off incremental compilation by adding this to the Cargo.toml:

[profile.dev]
incremental = false

dwrensha avatar May 12 '24 21:05 dwrensha

I believe this bisects to LLVM 18 from #120055 cc @nikic

lqd avatar May 13 '24 15:05 lqd

We are hitting an LLVM assertion:

Assertion failed: (N < NumContainedTys && "Element number out of range!"), function getElementType, file DerivedTypes.h, line 343.

saethlin avatar May 13 '24 20:05 saethlin

Could someone please run cargo -v and then repeat the rustc command with -C save-temps and share the bitcode files?

nikic avatar May 14 '24 02:05 nikic

Could someone please run cargo -v and then repeat the rustc command with -C save-temps and share the bitcode files?

I can reproduce it, and I will try to reduce it.

dianqk avatar May 14 '24 02:05 dianqk

Upstream issue: https://github.com/llvm/llvm-project/issues/92062 Reduced: https://llvm.godbolt.org/z/a4h1abfKc.

target triple = "arm64"

@p = external global { {}, { ptr } }

define void @foo() {
bb:
  %i1 = load ptr, ptr @p, align 8
  store ptr %i1, ptr null, align 8
  ret void
}

dianqk avatar May 14 '24 04:05 dianqk

It has already been backported to LLVM 18: https://github.com/llvm/llvm-project/pull/92129. @rustbot label +llvm-fixed-upstream

dianqk avatar May 16 '24 03:05 dianqk