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

rustc segfaults while translating inline assembly to write memory location #31437

Closed
Burgos opened this issue Feb 5, 2016 · 19 comments
Closed
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way.

Comments

@Burgos
Copy link

Burgos commented Feb 5, 2016

#![feature(asm)]

fn test() -> () {
    unsafe {
        let test: i32;
        // using `str r0, [%0]` or any other instruction, it still crashes.
        asm!("mov %0, r0" : "=m"(test) ::);
    }   
}

Results in:

test2.rs:3:1: 8:2 warning: function is never used: `test`, #[warn(dead_code)] on by default
test2.rs:3 fn test() -> () {
test2.rs:4     unsafe {
test2.rs:5         let test: i32;
test2.rs:6         asm!("ldr %0, r0" : "=m"(test) ::);
test2.rs:7     }
test2.rs:8 }
test2.rs:5:13: 5:17 warning: variable `test` is assigned to, but never used, #[warn(unused_variables)] on by default
test2.rs:5         let test: i32;
                       ^~~~
test2.rs:6:34: 6:38 warning: value assigned to `test` is never read, #[warn(unused_assignments)] on by default
test2.rs:6         asm!("ldr %0, r0" : "=m"(test) ::);
                                            ^~~~
test2.rs:6:9: 6:44 note: in this expansion of asm!
Segmentation fault

Backtrace:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb33ffb40 (LWP 8295)]
0xb573ad67 in llvm::SelectionDAGBuilder::visitInlineAsm(llvm::ImmutableCallSite) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
(gdb) bt
#0  0xb573ad67 in llvm::SelectionDAGBuilder::visitInlineAsm(llvm::ImmutableCallSite) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#1  0xb574e899 in llvm::SelectionDAGBuilder::visitCall(llvm::CallInst const&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#2  0xb5726cf4 in llvm::SelectionDAGBuilder::visit(unsigned int, llvm::User const&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#3  0xb5750ed5 in llvm::SelectionDAGBuilder::visit(llvm::Instruction const&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#4  0xb575dd4d in llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::Instruction const>, llvm::ilist_iterator<llvm::Instruction const>, bool&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#5  0xb576144f in llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#6  0xb57623db in llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#7  0xb54ffae9 in (anonymous namespace)::X86DAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#8  0xb58ae64e in llvm::MachineFunctionPass::runOnFunction(llvm::Function&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#9  0xb5ff6fec in llvm::FPPassManager::runOnFunction(llvm::Function&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#10 0xb5ff7378 in llvm::FPPassManager::runOnModule(llvm::Module&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#11 0xb5ff7695 in llvm::legacy::PassManagerImpl::run(llvm::Module&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#12 0xb5ff78a6 in llvm::legacy::PassManager::run(llvm::Module&) ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_llvm-71b07a99.so
#13 0xb4ea06b3 in LLVMRustWriteOutputFile (Target=0xb3d4b370, PMR=0xb3d21870, M=0xb3d0dc88, 
    path=0xb34219c0 "test2.0.o", FileType=llvm::TargetMachine::CGFT_ObjectFile)
    at /home/burgos/arm/rustc-1.6.0/src/rustllvm/PassWrapper.cpp:252
#14 0xb73d1b07 in back::write::write_output_file::h989c25c3cd2a3d4cnPc ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_trans-71b07a99.so
#15 0xb73d3f2c in back::write::optimize_and_codegen::_$LT$closure$GT$::closure.49106 ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_trans-71b07a99.so
#16 0xb73dcf8d in back::write::execute_work_item::hba4e87c8ecf4087c6Ed ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_trans-71b07a99.so
#17 0xb73d5625 in back::write::run_passes::hb688c8c39e303091Wqd ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_trans-71b07a99.so
#18 0xb7e4d890 in driver::phase_5_run_llvm_passes::h2c8f91b2f1b966e8aUa ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_driver-71b07a99.so
#19 0xb7df9d35 in driver::compile_input::h9c0ab200de97eadbhca ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_driver-71b07a99.so
#20 0xb7deb795 in run_compiler::h7e44862e31a6afdaOwc ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_driver-71b07a99.so
#21 0xb7de7e7b in sys_common::unwind::try::try_fn::try_fn::h8832516399314236240 ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_driver-71b07a99.so
#22 0xb7b36a85 in __rust_try () from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/libstd-71b07a99.so
#23 0xb7b2dd58 in sys_common::unwind::try::inner_try::ha18b63b3bff0cd283Ps ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/libstd-71b07a99.so
#24 0xb7de8200 in boxed::_$LT$impl$GT$::call_box::call_box::h2869009502916178978 ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/librustc_driver-71b07a99.so
#25 0xb7b3edca in sys::thread::_$LT$impl$GT$::new::thread_start::h1c9cba6d903a4e18oax ()
   from /home/burgos/rust/arm-unknown-linux-gnueabi/lib/libstd-71b07a99.so
#26 0xb41f9f70 in start_thread () from /lib/i386-linux-gnu/libpthread.so.0
---Type <return> to continue, or q <return> to quit--- 
#27 0xb79cfbee in clone () from /lib/i386-linux-gnu/libc.so.6

rustc:

~/rust/arm-unknown-linux-gnueabi/bin/rustc --version
rustc 1.6.0-dev
@Burgos Burgos changed the title ICE while translating inline assembly to write to struct field rustc segfaults while translating inline assembly to write to struct field Feb 5, 2016
@Burgos Burgos changed the title rustc segfaults while translating inline assembly to write to struct field rustc segfaults while translating inline assembly to write memory location Feb 5, 2016
@Burgos
Copy link
Author

Burgos commented Feb 5, 2016

I've changed the title, as I've managed to reduce test case.

@Burgos
Copy link
Author

Burgos commented Feb 5, 2016

rustc was built with just --target=arm-unknown-linux-gnueabi

@steveklabnik steveklabnik added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Feb 5, 2016
@Burgos
Copy link
Author

Burgos commented Feb 5, 2016

I'm compiling beta 1.7, will report if it is resolved.

@Burgos
Copy link
Author

Burgos commented Feb 5, 2016

Can confirm that this happens with 1.7.0-dev as well.

@Burgos
Copy link
Author

Burgos commented Feb 6, 2016

Debug build shows segfault as a result of failed assertion:

rustc: /home/burgos/rustc-beta/src/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp:6012: void llvm::SelectionDAGBuilder::visitInlineAsm(llvm::ImmutableCallSite): Assertion `(OpInfo.isMultipleAlternative || (OpInfo.Type == InlineAsm::isInput)) && "Can only indirectify direct input operands!"' failed.

Which leads to the following issues:

#29382
#16337
#16383
#836

@Burgos
Copy link
Author

Burgos commented Feb 6, 2016

This one looks promising, I will try it out: effcd29

@MagaTailor
Copy link

On my ARM machine there's no problem using a nightly. You were using an i386 host cross-compiler, right?

It duly segfaults under 1.5 though.

@Burgos
Copy link
Author

Burgos commented Feb 6, 2016

On my ARM machine there's no problem using a nightly. You were using an i386 host cross-compiler, right?

Yeah, I haven't tried with nightly, just with v1.6 and v1.7-beta. I will try the nightly as well, perhaps tomorrow.

You were using an i386 host cross-compiler, right?

Correct.

Thanks!

@Burgos
Copy link
Author

Burgos commented Feb 6, 2016

This one looks promising, I will try it out: effcd29

Yes, moving m with =* to in set workaround the assertion. Still, the original example should not assert. I will try it on nightly, it the assertion is fixed as @petevine reports, I will close this issue.

@Burgos
Copy link
Author

Burgos commented Feb 7, 2016

Still segfaults rustc 1.8.0-dev :-(.

@MagaTailor
Copy link

Getting interesting! The rustc I was using was built Feb, 4th from a source snapshot.

Other differences between how yours and mine were built (apart from native vs cross) are:
--disable-jemalloc and this ARM specific llvm patch.

@Burgos
Copy link
Author

Burgos commented Feb 7, 2016

Interesting, thanks! I will try first with the llvm's patch!

@MagaTailor
Copy link

No, wait - theses details are completely irrelevant.

I get the segfault too - the reason I was able to compile was using rustc -O transparently.

@sanxiyn sanxiyn added the A-inline-assembly Area: Inline assembly (`asm!(…)`) label May 10, 2016
@pinkisemils
Copy link

Doesn't even compile on a nightly for 1.13.

@aidanhs
Copy link
Member

aidanhs commented Dec 8, 2016

Probably a similar issue so I'll report here:

#![feature(asm)]
fn main() {
    unsafe { asm!("" : : "X"(main)) };
}

https://is.gd/wn6Vnv

Dec  8 02:31:56 a-3-linux kernel: [136862.765624] rustc[30254]: segfault at 18 ip 00007f421469f1b9 sp 00007f42105f1870 error 4 in librustc_llvm-1357b93f.so[7f421364c000+2e4d000]

@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 22, 2017
@istankovic
Copy link
Contributor

Still segfaults with rustc 1.26.0-nightly (adf2135ad 2018-03-17).

@oberien
Copy link
Contributor

oberien commented Jun 12, 2018

Still segfaults on rustc 1.28.0-nightly (1d4dbf4 2018-06-11).

Regarding @aidanhs example: If you change "X"(main) to "X"(main as usize), it doesn't segfault.

@dancrossnyc
Copy link

dancrossnyc commented Feb 12, 2019

Adding another voice to the chorus pointing out that this is still broken. My example code:

#![feature(asm)]

pub fn xswap(word: &mut u64, value: u64) -> u64 {
    let result: u64;
    let wp = word as *mut u64;
    unsafe {
        asm!("lock; xchgq %0, %1"
             : "=r"(result), "=m"(*wp)
             : "0"(value), "m"(*wp)
             :: "volatile");
    }
    result
}

fn main() {
    let mut a = 0u64;
    let b = xswap(&mut a, 1);
    println!("a = {}, b = {}", a, b);
}

This makes it difficult to give a pedagogical examples of writing interrupt-safe locks in a no_std environment (the spin crate, for example, does not disable interrupts when holding a lock). Replacing the body of the function with an intrinsic call works, but is not great as an example for students.

Of course, it could be that my assembler is messed up, in which case I'd welcome a correction. :-)

@Centril Centril added the requires-nightly This issue requires a nightly compiler in some way. label Oct 25, 2019
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Apr 29, 2020
@Amanieu
Copy link
Member

Amanieu commented May 22, 2020

This issue does not apply to the new asm! (RFC 2850) which doesn't support memory operands.

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
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

No branches or pull requests