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

SIGSEGV in the compiler #82065

Closed
pitust opened this issue Feb 13, 2021 · 6 comments · Fixed by #92816
Closed

SIGSEGV in the compiler #82065

pitust opened this issue Feb 13, 2021 · 6 comments · Fixed by #92816
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. 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

@pitust
Copy link

pitust commented Feb 13, 2021

Code

#![feature(llvm_asm)]
fn main() {
    unsafe { llvm_asm!("mov $0, %rax" :: "i"(main)); }
}

Meta

rustc --version --verbose:

rustc 1.50.0-nightly (f74583445 2020-12-18)
binary: rustc
commit-hash: f74583445702e2e27ec4415376f2c540a83d7ded
commit-date: 2020-12-18
host: x86_64-unknown-linux-gnu
release: 1.50.0-nightly

rustc +stable --version --verbose:

rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: x86_64-unknown-linux-gnu
release: 1.50.0

Note that on stable RUSTC_BOOTSTRAP=1 is needed to trigger the bug

Error output

Hmm. What should go here?

Segmentation fault (core dumped)
GDB Backtrace

0x00007ffff29005a9 in llvm::X86TargetLowering::LowerAsmOperandForConstraint(llvm::SDValue, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::vector<llvm::SDValue, std::allocator<llvm::SDValue> >&, llvm::SelectionDAG&) const () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
(gdb) bt
#0  0x00007ffff29005a9 in llvm::X86TargetLowering::LowerAsmOperandForConstraint(llvm::SDValue, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::vector<llvm::SDValue, std::allocator<llvm::SDValue> >&, llvm::SelectionDAG&) const () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#1  0x00007ffff0a2241a in llvm::SelectionDAGBuilder::visitInlineAsm(llvm::CallBase const&) ()
   from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#2  0x00007ffff09ff867 in llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#3  0x00007ffff0abf4fe 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/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#4  0x00007ffff0abe793 in llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) ()
   from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#5  0x00007ffff0abb926 in llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()
   from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#6  0x00007ffff27dacd7 in (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()
--Type <RET> for more, q to quit, c to continue without paging--
   from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#7  0x00007ffff062478e in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#8  0x00007ffff03fefd2 in llvm::FPPassManager::runOnFunction(llvm::Function&) () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#9  0x00007ffff04059e3 in llvm::FPPassManager::runOnModule(llvm::Module&) () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#10 0x00007ffff03ffa2a in llvm::legacy::PassManagerImpl::run(llvm::Module&) () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-11-rust-1.50.0-nightly.so
#11 0x00007ffff40240ab in LLVMRustWriteOutputFile () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-54c3daf069bcd3f6.so
#12 0x00007ffff3f2656a in rustc_codegen_llvm::back::write::write_output_file () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-54c3daf069bcd3f6.so
#13 0x00007ffff3f2b604 in rustc_codegen_llvm::back::write::codegen () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-54c3daf069bcd3f6.so
#14 0x00007ffff3f4997b in rustc_codegen_ssa::back::write::finish_intra_module_work () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-54c3daf069bcd3f6.so
#15 0x00007ffff3f43cd7 in rustc_codegen_ssa::back::write::execute_work_item () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-54c3daf069bcd3f6.so
#16 0x00007ffff3e64d3a in std::sys_common::backtrace::__rust_begin_short_backtrace () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-54c3daf069bcd3f6.so
#17 0x00007ffff3e6ac6d in core::ops::function::FnOnce::call_once{{vtable-shim}} () from /home/pitust/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-54c3daf069bcd3f6.so
--Type <RET> for more, q to quit, c to continue without paging--
#18 0x00007ffff32717ba in alloc::boxed::{{impl}}::call_once<(),FnOnce<()>,alloc::alloc::Global> () at /rustc/f74583445702e2e27ec4415376f2c540a83d7ded/library/alloc/src/boxed.rs:1328
#19 alloc::boxed::{{impl}}::call_once<(),alloc::boxed::Box<FnOnce<()>, alloc::alloc::Global>,alloc::alloc::Global> () at /rustc/f74583445702e2e27ec4415376f2c540a83d7ded/library/alloc/src/boxed.rs:1328
#20 std::sys::unix::thread::{{impl}}::new::thread_start () at /rustc/f74583445702e2e27ec4415376f2c540a83d7ded//library/std/src/sys/unix/thread.rs:71
#21 0x00007ffff317a299 in start_thread () from /usr/lib/libpthread.so.0
#22 0x00007ffff3091153 in clone () from /usr/lib/libc.so.6

I have no clue why this happens, but i guess writing a JIT in rust like this wasn't the smartest idea...

@pitust pitust 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 Feb 13, 2021
@SNCPlay42
Copy link
Contributor

SNCPlay42 commented Feb 13, 2021

llvm_asm! is deprecated in favor of asm! (I think? I'm pretty sure I've seen llvm_asm issues closed before because asm should be used instead); perhaps you could try using that instead.

@rustbot label requires-nightly A-LLVM F-llvm_asm

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. requires-nightly This issue requires a nightly compiler in some way. labels Feb 13, 2021
@SNCPlay42
Copy link
Contributor

@rustbot label -I-ICE +I-crash

Looks like there isn't even a feature label for llvm_asm.

@rustbot rustbot added 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 Feb 13, 2021
@nagisa
Copy link
Member

nagisa commented Feb 13, 2021

Yes, llvm_asm is not supported and issues arise from use of it will not be fixed.

That said this looks like a potentially genuine LLVM bug, so reporting it upstream may make sense still.

define internal void @banana() unnamed_addr {
start:
  tail call void asm sideeffect "mov $0, %rax", "i,~{dirflag},~{fpsr},~{flags}"({} undef)
  ret void
}

is the LLVM-IR reproducer.

@pitust
Copy link
Author

pitust commented Feb 13, 2021

I couldn't repro it in C, so i assumed it's a rust bug. I guess i was wrong.
What's that {} undef? I don't really know a lot of LLVM IR.

@nagisa
Copy link
Member

nagisa commented Feb 13, 2021

Its a unspecified bit pattern of type {} (empty structure). In that respect it is a bug in Rust that it a) for some reason ends up generating a type of {} rather than a type of a function pointer; and b) it passes in an undef value rather than a @symbol. But, regardless, with llvm_asm being slated for removal, this will not be fixed.

@SNCPlay42
Copy link
Contributor

for some reason ends up generating a type of {} rather than a type of a function pointer;

This is probably because it considers main to have its unique function type fn() {main} (which is a ZST) rather than coercing it to a function pointer.

Which suggests this workaround:

#![feature(llvm_asm)]
fn main() {
    unsafe { llvm_asm!("mov $0, %rax" :: "i"(main as fn())); }
}

Which indeed doesn't segfault (the linker complains about not not being able to relocate this in position-independent code instead).

@bors bors closed this as completed in a34c079 Jan 17, 2022
flip1995 pushed a commit to flip1995/rust that referenced this issue Jan 27, 2022
Remove deprecated LLVM-style inline assembly

The `llvm_asm!` was deprecated back in rust-lang#87590 1.56.0, with intention to remove
it once `asm!` was stabilized, which already happened in rust-lang#91728 1.59.0. Now it
is time to remove `llvm_asm!` to avoid continued maintenance cost.

Closes rust-lang#70173.
Closes rust-lang#92794.
Closes rust-lang#87612.
Closes rust-lang#82065.

cc `@rust-lang/wg-inline-asm`

r? `@Amanieu`
bjorn3 pushed a commit to bjorn3/rust that referenced this issue Feb 23, 2022
Remove deprecated LLVM-style inline assembly

The `llvm_asm!` was deprecated back in rust-lang#87590 1.56.0, with intention to remove
it once `asm!` was stabilized, which already happened in rust-lang#91728 1.59.0. Now it
is time to remove `llvm_asm!` to avoid continued maintenance cost.

Closes rust-lang#70173.
Closes rust-lang#92794.
Closes rust-lang#87612.
Closes rust-lang#82065.

cc `@rust-lang/wg-inline-asm`

r? `@Amanieu`
calebcartwright pushed a commit to calebcartwright/rust that referenced this issue Mar 30, 2022
Remove deprecated LLVM-style inline assembly

The `llvm_asm!` was deprecated back in rust-lang#87590 1.56.0, with intention to remove
it once `asm!` was stabilized, which already happened in rust-lang#91728 1.59.0. Now it
is time to remove `llvm_asm!` to avoid continued maintenance cost.

Closes rust-lang#70173.
Closes rust-lang#92794.
Closes rust-lang#87612.
Closes rust-lang#82065.

cc `@rust-lang/wg-inline-asm`

r? `@Amanieu`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. 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.

4 participants