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

nightly: segfault with asm!() / #[naked] (possible LLVM 11 bug) #75922

Closed
jgouly opened this issue Aug 25, 2020 · 2 comments · Fixed by #79411
Closed

nightly: segfault with asm!() / #[naked] (possible LLVM 11 bug) #75922

jgouly opened this issue Aug 25, 2020 · 2 comments · Fixed by #79411
Labels
A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jgouly
Copy link

jgouly commented Aug 25, 2020

Compiling the following code with cargo build --release causes a (signal: 11, SIGSEGV: invalid memory reference) error.

Code

#![feature(asm)]
#![feature(naked_functions)]

#[inline(never)]
// Removing #[naked] allows this to compile.
#[naked]
fn bar(ptr: *mut u64) -> u64 {
  let v;
  unsafe {
    asm!(
     "mov {bar}, {foo}",
     foo = in(reg) *ptr,
     bar = out(reg) v,
    )
  }
  v
}

fn main() {
  bar(&mut 0);
}

It was introduced by:

$ rustc --version
rustc 1.47.0-nightly (5180f3da5 2020-08-23)

Using the previous nightly works:

$ rustc --version
rustc 1.47.0-nightly (663d2f5cd 2020-08-22)

Looking at 663d2f5...5180f3d, the biggest change I see is the upgrade to LLVM 11.

Backtrace

(gdb) bt
#0  0x00007ffff0ccb4ed in llvm::SelectionDAGBuilder::visitLoad(llvm::LoadInst const&) ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
#1  0x00007ffff0cc3637 in llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
#2  0x00007ffff0d8404e in llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, false, false, void>, false, true>, bool&) ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
#3  0x00007ffff0d83667 in llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
#4  0x00007ffff0d80342 in llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
#5  0x00007ffff2b1de87 in (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&)
    ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
#6  0x00007ffff08db6ae in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
#7  0x00007ffff06b1002 in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
   from /home/build/rust/rustup/toolchains/nightly-2020-08-24-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.47.0-nightly.so
@jgouly jgouly added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 25, 2020
@jonas-schievink jonas-schievink added A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS F-asm `#![feature(asm)]` (not `llvm_asm`) I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. and removed I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ labels Aug 25, 2020
@JamieCunliffe
Copy link
Contributor

I believe this is actually undefined behaviour, naked functions are only supposed to contain inline assembly (https://internals.rust-lang.org/t/idea-naked-functions-2-0/11637/25)... I guess something went into LLVM that causes this to be enforced a little more than previously.

@jgouly
Copy link
Author

jgouly commented Aug 26, 2020

The following also crashes:

#![feature(asm)]
#![feature(naked_functions)]

#[inine(never)]
// Removing #[naked] allows this to compile.
#[naked]
fn bar(ptr: *mut u64) {
  unsafe {
    asm!(
     "mov {foo}, {foo}",
     foo = inout(reg) *ptr,
    )
  }
}

fn main() {
  bar(&mut 0);
}

EDIT: I just remembered that @Amanieu said that in/out aren't allowed in naked functions and will eventually error.

@jonas-schievink jonas-schievink added the requires-nightly This issue requires a nightly compiler in some way. label Aug 29, 2020
stlankes added a commit to hermit-os/kernel that referenced this issue Aug 30, 2020
- naked functions does currently not work (rust-lang/rust#75922)
- we replaced naked functions by assembly code
- using global_asm to include the assembly code
zhaofengli added a commit to mars-research/redleaf that referenced this issue Sep 22, 2020
@bors bors closed this as completed in b48cafd Nov 25, 2020
simonschoening pushed a commit to simonschoening/libhermit-rs that referenced this issue Aug 26, 2021
- naked functions does currently not work (rust-lang/rust#75922)
- we replaced naked functions by assembly code
- using global_asm to include the assembly code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants