Skip to content

Can bypass validation of asm! in a function that is never used via inline(always)  #36718

Closed
@japaric

Description

@japaric

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inline-assemblyArea: Inline assembly (`asm!(…)`)C-bugCategory: This is a bug.F-asm`#![feature(asm)]` (not `llvm_asm`)P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions