Closed
Description
STR
NOTE target = x86_64-unknown-linux-gnu in all cases
This is rejected:
#![feature(asm)]
unsafe fn bkpt() {
// ARM specific instruction
asm!("bkpt");
}
$ rustc asm.rs
error: <inline asm>:1:2: error: invalid instruction mnemonic 'bkpt'
bkpt
^
But this is accepted:
#![feature(asm)]
#[inline(always)]
unsafe fn bkpt() {
asm!("bkpt");
}
$ rustc asm.rs && echo OK
OK
Don't worry though, because you can't actually use the ARCH specific instruction:
#![feature(asm)]
fn main() {
unsafe {
bkpt();
}
}
#[inline(always)]
unsafe fn bkpt() {
asm!("bkpt");
}
$ rustc asm.rs
error: <inline asm>:1:2: error: invalid instruction mnemonic 'bkpt'
bkpt
^
Additional comments
This is a pretty useful bug/feature! You can use it cargo test
the arch-agnostic parts of a crate
designed for ARM on x86 without adding a bunch of cfg
attributes:
#![feature(asm)]
// ARM only
#[inline(always)]
pub unsafe fn control() -> u32 {
let r: u32;
asm!("msr CONTROL, $0" : "=r"(r));
r
}
// arch-agnostic stuff
pub fn foo() -> bool {
true
}
#[test]
fn bar() {
assert!(foo());
}
$ cargo test
running 1 test
test bar ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
Without the inline(always)
trick, you'll have to add a cfg(target_arch = "arm")
to the bkpt
function to get this to compile.
I don't think we want people to rely on this though. Specially, if the inline(always)
trick stops
working in the future.
Meta
$ rustc -V
rustc 1.13.0-nightly (4f9812a59 2016-09-21)
cc @brson I'll let you decide how bad this is.