Skip to content

rustc segfault when using a function as input to asm! #68136

Closed

Description

@stevecheckoway

Using rustc nightly

rustc 1.42.0-nightly (fc5deca21 2019-12-21)

the following two code samples cause rustc to crash.

#![feature(asm)]

extern "C" fn foo() { }

fn main() {
    unsafe {
        asm!("callq $0" :: "s"(foo) :: "volatile");
    }
}
#![feature(asm)]

extern "C" fn foo() { }

fn main() {
    let x: usize;
    unsafe {
        asm!("movq $1, $0" : "=r"(x) : "r"(foo));
    }
    assert!(x != 0);
}

Here's a rustc backtrace for the first.

(lldb) bt
* thread #3, stop reason = EXC_BAD_ACCESS (code=1, address=0x18)
  * frame #0: 0x000000010153c761 librustc_driver-0c56519087c8b41a.dylib`llvm::TargetLowering::LowerAsmOperandForConstraint(llvm::SDValue, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::vector<llvm::SDValue, std::__1::allocator<llvm::SDValue> >&, llvm::SelectionDAG&) const + 129
    frame #1: 0x0000000100807d54 librustc_driver-0c56519087c8b41a.dylib`llvm::X86TargetLowering::LowerAsmOperandForConstraint(llvm::SDValue, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::vector<llvm::SDValue, std::__1::allocator<llvm::SDValue> >&, llvm::SelectionDAG&) const + 1636
    frame #2: 0x000000010149f2fa librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGBuilder::visitInlineAsm(llvm::ImmutableCallSite) + 8874
    frame #3: 0x0000000101480ee9 librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) + 105
    frame #4: 0x000000010150ecb0 librustc_driver-0c56519087c8b41a.dylib`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&) + 128
    frame #5: 0x000000010150e55b librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) + 5243
    frame #6: 0x000000010150c344 librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 1748
    frame #7: 0x000000010073274d librustc_driver-0c56519087c8b41a.dylib`(anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 141
    frame #8: 0x00000001016de44d librustc_driver-0c56519087c8b41a.dylib`llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 285
    frame #9: 0x0000000101f6fab1 librustc_driver-0c56519087c8b41a.dylib`llvm::FPPassManager::runOnFunction(llvm::Function&) + 1057
    frame #10: 0x0000000101f6fdb3 librustc_driver-0c56519087c8b41a.dylib`llvm::FPPassManager::runOnModule(llvm::Module&) + 131
    frame #11: 0x0000000101f70289 librustc_driver-0c56519087c8b41a.dylib`llvm::legacy::PassManagerImpl::run(llvm::Module&) + 969
    frame #12: 0x00000001006924d3 librustc_driver-0c56519087c8b41a.dylib`LLVMRustWriteOutputFile + 563
    frame #13: 0x00000001006112e6 librustc_driver-0c56519087c8b41a.dylib`rustc_codegen_llvm::back::write::write_output_file::h76d2b2ebfb7a8eed (.llvm.10576665092353784990) + 86
    frame #14: 0x00000001006662f8 librustc_driver-0c56519087c8b41a.dylib`rustc_codegen_llvm::back::write::codegen::_$u7b$$u7b$closure$u7d$$u7d$::h66912a0fe09f03df (.llvm.3263083813660944518) + 1560
    frame #15: 0x00000001006643b3 librustc_driver-0c56519087c8b41a.dylib`rustc::util::common::time_ext::haeef6b0b58ed606e + 163
    frame #16: 0x000000010061669b librustc_driver-0c56519087c8b41a.dylib`_$LT$rustc_codegen_llvm..LlvmCodegenBackend$u20$as$u20$rustc_codegen_ssa..traits..write..WriteBackendMethods$GT$::codegen::h0f4f9d6743018805 + 3723
    frame #17: 0x000000010053e7e7 librustc_driver-0c56519087c8b41a.dylib`rustc_codegen_ssa::back::write::execute_work_item::ha12f01ddc6c02062 + 4167
    frame #18: 0x000000010065d1cf librustc_driver-0c56519087c8b41a.dylib`std::sys_common::backtrace::__rust_begin_short_backtrace::hc4efc99b4fa2ef62 + 239
    frame #19: 0x000000010060cd0b librustc_driver-0c56519087c8b41a.dylib`std::panicking::try::do_call::h223b883ec45a8b2d (.llvm.10576665092353784990) + 43
    frame #20: 0x000000010615526f libstd-fb8a2cba31c5c8b1.dylib`__rust_maybe_catch_panic + 31
    frame #21: 0x000000010051ba96 librustc_driver-0c56519087c8b41a.dylib`core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h27c4ab159ffa15f6 + 134
    frame #22: 0x00000001061267fe libstd-fb8a2cba31c5c8b1.dylib`_$LT$alloc..boxed..Box$LT$F$GT$$u20$as$u20$core..ops..function..FnOnce$LT$A$GT$$GT$::call_once::ha165f2d8179450a9 + 62
    frame #23: 0x0000000106153fae libstd-fb8a2cba31c5c8b1.dylib`std::sys::unix::thread::Thread::new::thread_start::h6d30401d88e22320 + 142
    frame #24: 0x00007fff62cde2eb libsystem_pthread.dylib`_pthread_body + 126
    frame #25: 0x00007fff62ce1249 libsystem_pthread.dylib`_pthread_start + 66
    frame #26: 0x00007fff62cdd40d libsystem_pthread.dylib`thread_start + 13

Here's the backtrace for the second.

(lldb) bt
* thread #2, stop reason = EXC_BAD_ACCESS (code=1, address=0x28)
  * frame #0: 0x00000001007b5374 librustc_driver-0c56519087c8b41a.dylib`llvm::X86TargetLowering::isZExtFree(llvm::SDValue, llvm::EVT) const + 4
    frame #1: 0x000000010147e086 librustc_driver-0c56519087c8b41a.dylib`llvm::RegsForValue::getCopyToRegs(llvm::SDValue, llvm::SelectionDAG&, llvm::SDLoc const&, llvm::SDValue&, llvm::SDValue*, llvm::Value const*, llvm::ISD::NodeType) const + 566
    frame #2: 0x000000010149fe6d librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGBuilder::visitInlineAsm(llvm::ImmutableCallSite) + 11805
    frame #3: 0x0000000101480ee9 librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) + 105
    frame #4: 0x000000010150ecb0 librustc_driver-0c56519087c8b41a.dylib`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&) + 128
    frame #5: 0x000000010150e55b librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) + 5243
    frame #6: 0x000000010150c344 librustc_driver-0c56519087c8b41a.dylib`llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 1748
    frame #7: 0x000000010073274d librustc_driver-0c56519087c8b41a.dylib`(anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 141
    frame #8: 0x00000001016de44d librustc_driver-0c56519087c8b41a.dylib`llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 285
    frame #9: 0x0000000101f6fab1 librustc_driver-0c56519087c8b41a.dylib`llvm::FPPassManager::runOnFunction(llvm::Function&) + 1057
    frame #10: 0x0000000101f6fdb3 librustc_driver-0c56519087c8b41a.dylib`llvm::FPPassManager::runOnModule(llvm::Module&) + 131
    frame #11: 0x0000000101f70289 librustc_driver-0c56519087c8b41a.dylib`llvm::legacy::PassManagerImpl::run(llvm::Module&) + 969
    frame #12: 0x00000001006924d3 librustc_driver-0c56519087c8b41a.dylib`LLVMRustWriteOutputFile + 563
    frame #13: 0x00000001006112e6 librustc_driver-0c56519087c8b41a.dylib`rustc_codegen_llvm::back::write::write_output_file::h76d2b2ebfb7a8eed (.llvm.10576665092353784990) + 86
    frame #14: 0x00000001006662f8 librustc_driver-0c56519087c8b41a.dylib`rustc_codegen_llvm::back::write::codegen::_$u7b$$u7b$closure$u7d$$u7d$::h66912a0fe09f03df (.llvm.3263083813660944518) + 1560
    frame #15: 0x00000001006643b3 librustc_driver-0c56519087c8b41a.dylib`rustc::util::common::time_ext::haeef6b0b58ed606e + 163
    frame #16: 0x000000010061669b librustc_driver-0c56519087c8b41a.dylib`_$LT$rustc_codegen_llvm..LlvmCodegenBackend$u20$as$u20$rustc_codegen_ssa..traits..write..WriteBackendMethods$GT$::codegen::h0f4f9d6743018805 + 3723
    frame #17: 0x000000010053e7e7 librustc_driver-0c56519087c8b41a.dylib`rustc_codegen_ssa::back::write::execute_work_item::ha12f01ddc6c02062 + 4167
    frame #18: 0x000000010065d1cf librustc_driver-0c56519087c8b41a.dylib`std::sys_common::backtrace::__rust_begin_short_backtrace::hc4efc99b4fa2ef62 + 239
    frame #19: 0x000000010060cd0b librustc_driver-0c56519087c8b41a.dylib`std::panicking::try::do_call::h223b883ec45a8b2d (.llvm.10576665092353784990) + 43
    frame #20: 0x000000010615526f libstd-fb8a2cba31c5c8b1.dylib`__rust_maybe_catch_panic + 31
    frame #21: 0x000000010051ba96 librustc_driver-0c56519087c8b41a.dylib`core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::h27c4ab159ffa15f6 + 134
    frame #22: 0x00000001061267fe libstd-fb8a2cba31c5c8b1.dylib`_$LT$alloc..boxed..Box$LT$F$GT$$u20$as$u20$core..ops..function..FnOnce$LT$A$GT$$GT$::call_once::ha165f2d8179450a9 + 62
    frame #23: 0x0000000106153fae libstd-fb8a2cba31c5c8b1.dylib`std::sys::unix::thread::Thread::new::thread_start::h6d30401d88e22320 + 142
    frame #24: 0x00007fff62cde2eb libsystem_pthread.dylib`_pthread_body + 126
    frame #25: 0x00007fff62ce1249 libsystem_pthread.dylib`_pthread_start + 66
    frame #26: 0x00007fff62cdd40d libsystem_pthread.dylib`thread_start + 13

Activity

  1. added
    A-inline-assemblyArea: Inline assembly (`asm!(…)`)
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    C-bugCategory: This is a bug.
    I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.
    on Jan 11, 2020
  2. Amanieu commented on May 8, 2020

    @Amanieu
    Member

    As a workaround you can use foo as usize.

  3. Amanieu commented on May 22, 2020

    @Amanieu
    Member

    This issue does not apply to the new asm! (RFC 2850) which automatically coerces ZST fn types to function pointers.

    The legacy llvm_asm! is deprecated and is no longer maintained.

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-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.glacierICE tracked in rust-lang/glacier.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions