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

ICE while using inline assembly #36907

Open
SethDusek opened this Issue Oct 2, 2016 · 8 comments

Comments

Projects
None yet
7 participants
@SethDusek
Copy link

SethDusek commented Oct 2, 2016

So I was messing around with the inline assembler, when I ran into an ICE

Code: https://gist.github.com/9dfeeb4d7cda8aeb1d25c7ea89ee277e

   Compiling asmswitch v0.1.0 (file:///home/kamal/rust/asmswitch)
     Running `rustc src/main.rs --crate-name asmswitch --crate-type bin -g -C metadata=71a43ba090afc09b --out-dir /home/kamal/rust/asmswitch/target/debug --emit=dep-info,link -L dependency=/home/kamal/rust/asmswitch/target/debug/deps`
error: Could not compile `asmswitch`.

Caused by:
  process didn't exit successfully: `rustc src/main.rs --crate-name asmswitch --crate-type bin -g -C metadata=71a43ba090afc09b --out-dir /home/kamal/rust/asmswitch/target/debug --emit=dep-info,link -L dependency=/home/kamal/rust/asmswitch/target/debug/deps` (exit code: 1)

@TimNN

This comment has been minimized.

Copy link
Contributor

TimNN commented Oct 2, 2016

(I'm not getting an ICE but a crash), backtrace on OS X / nightly-2016-09-29:

* thread #2: tid = 0xd98a10, 0x0000000101d9fed4 librustc_llvm-6eb85298.dylib`llvm::X86TargetLowering::isZExtFree(llvm::SDValue, llvm::EVT) const + 4, name = 'rustc', stop reason = EXC_BAD_ACCESS (code=1, address=0x3a)
  * frame #0: 0x0000000101d9fed4 librustc_llvm-6eb85298.dylib`llvm::X86TargetLowering::isZExtFree(llvm::SDValue, llvm::EVT) const + 4
    frame #1: 0x0000000101f900d1 librustc_llvm-6eb85298.dylib`llvm::RegsForValue::getCopyToRegs(llvm::SDValue, llvm::SelectionDAG&, llvm::SDLoc const&, llvm::SDValue&, llvm::SDValue*, llvm::Value const*, llvm::ISD::NodeType) const + 689
    frame #2: 0x0000000101fb0fed librustc_llvm-6eb85298.dylib`llvm::SelectionDAGBuilder::visitInlineAsm(llvm::ImmutableCallSite) + 8605
    frame #3: 0x0000000101f93d48 librustc_llvm-6eb85298.dylib`llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) + 1144
    frame #4: 0x0000000102025248 librustc_llvm-6eb85298.dylib`llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::Instruction const>, llvm::ilist_iterator<llvm::Instruction const>, bool&) + 40
    frame #5: 0x00000001020249e2 librustc_llvm-6eb85298.dylib`llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) + 10146
    frame #6: 0x0000000102021174 librustc_llvm-6eb85298.dylib`llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 1684
    frame #7: 0x0000000101d26134 librustc_llvm-6eb85298.dylib`(anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) + 20
    frame #8: 0x000000010221f7e5 librustc_llvm-6eb85298.dylib`llvm::MachineFunctionPass::runOnFunction(llvm::Function&) + 293
    frame #9: 0x0000000102b189d3 librustc_llvm-6eb85298.dylib`llvm::FPPassManager::runOnFunction(llvm::Function&) + 515
    frame #10: 0x0000000102b18c2b librustc_llvm-6eb85298.dylib`llvm::FPPassManager::runOnModule(llvm::Module&) + 43
    frame #11: 0x0000000102b191b3 librustc_llvm-6eb85298.dylib`llvm::legacy::PassManagerImpl::run(llvm::Module&) + 1075
    frame #12: 0x000000010151b240 librustc_llvm-6eb85298.dylib`LLVMRustWriteOutputFile + 368
    frame #13: 0x00000001002164ef librustc_trans-6eb85298.dylib`rustc_trans::back::write::write_output_file::h7e8cf29735c33835 + 95
    frame #14: 0x00000001002be680 librustc_trans-6eb85298.dylib`rustc_trans::back::write::optimize_and_codegen::_$u7b$$u7b$closure$u7d$$u7d$::h368ad232bd936447 + 672
    frame #15: 0x000000010021de92 librustc_trans-6eb85298.dylib`rustc_trans::back::write::execute_work_item::h11850bd68a6e29be + 6434
    frame #16: 0x0000000100218c7b librustc_trans-6eb85298.dylib`rustc_trans::back::write::run_passes::h17498b78d8227324 + 3659
    frame #17: 0x00000001000ba57a librustc_driver-6eb85298.dylib`rustc_driver::driver::phase_5_run_llvm_passes::h11938d38ef5db27c + 298
    frame #18: 0x00000001000a54c3 librustc_driver-6eb85298.dylib`rustc_driver::driver::compile_input::h5b63ccd49eeeb98b + 7699
    frame #19: 0x00000001000cd179 librustc_driver-6eb85298.dylib`rustc_driver::run_compiler::h5441e14d75f46685 + 2041
    frame #20: 0x000000010000dba1 librustc_driver-6eb85298.dylib`std::panicking::try::do_call::h75a39c92a39808cb + 161
    frame #21: 0x00000001041bbfbb libstd-6eb85298.dylib`__rust_maybe_catch_panic + 27
    frame #22: 0x000000010002d585 librustc_driver-6eb85298.dylib`_$LT$F$u20$as$u20$alloc..boxed
@TimNN

This comment has been minimized.

Copy link
Contributor

TimNN commented Oct 2, 2016

This was introduced between nightly-2016-03-09 and nightly-2016-03-11 (Changes).

I suspect this was an effect of #31710 - I would assume that the use of a function name in inline assembly would now require some special handling to turn it into the function address / pointer.

cc @eddyb.

@TimNN

This comment has been minimized.

Copy link
Contributor

TimNN commented Oct 2, 2016

Indeed, it appears as if any ZST in the position of foo in the provided example triggers a crash.

@eddyb

This comment has been minimized.

Copy link
Member

eddyb commented Oct 2, 2016

Indeed, need to cast foo to unsafe fn() -> usize now as per #19925.
transmute got a warning cycle but inline assembly is unstable anyway.

The best we can do is some sanity checking for asm! similar to variadic C functions.
cc @nikomatsakis

@TimNN

This comment has been minimized.

Copy link
Contributor

TimNN commented Oct 2, 2016

(I wanted to add nightly-to-nightly, which we didn't have, but I hit enter too soon and accidentally tagged this as stable-to-nightly (which I then removed))

@nagisa

This comment has been minimized.

Copy link
Contributor

nagisa commented Oct 2, 2016

Should be A-inline-assembly.

@tari

This comment has been minimized.

Copy link
Contributor

tari commented Oct 2, 2016

I get no crash (or abort) when using LLVM compiled in Debug mode with assertions enabled, rustc 7b33876 on x86_64 Linux. The generated code looks correct.

I assume the ZST issue is the cause, but it sounds like there's an LLVM bug involved as well.

@aidanhs

This comment has been minimized.

Copy link
Member

aidanhs commented Dec 7, 2016

Copying my example from the duplicate issue I raised (which doesn't really add anything except rust playground links):


This is fine:

#![feature(asm)]
fn abcd() {
    println!("lo");
}
fn main() {
    unsafe{asm!(".global abc;.set abc, ${0:P}" : : "i"(abcd as fn()))};
    println!("hi");
    unsafe { abc() };
}
extern {
    fn abc();
}

https://is.gd/2hOCz4
Output when running compiled binary:

hi
lo

This is not (removed as fn()):

#![feature(asm)]
fn abcd() {
    println!("lo");
}
fn main() {
    unsafe{asm!(".global abc;.set abc, ${0:P}" : : "i"(abcd))};
    println!("hi");
    unsafe { abc() };
}
extern {
    fn abc();
}

https://is.gd/09bozZ
Output when compiling:

rustc 1.15.0-nightly (2217bd771 2016-11-25)
rustc: /buildslave/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/llvm/include/llvm/Support/Casting.h:81: static bool llvm::isa_impl_cl<To, From*>::doit(const From*) [with To = llvm::ConstantSDNode; From = llvm::SDNode]: Assertion `Val && "isa<> used on a null pointer"' failed.
Aborted (core dumped)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.