Skip to content

asm! macro compiles fine on debug build but not on release build #61429

Closed
@cfsamson

Description

@cfsamson

The following code compiles fine on Mac debug build, but fails on release build. The same happens on rust playground. This is just part of some code I use for debugging.

The remarkable thing is that in the larger example I have I use the same type of inline assembly and it compiles fine on --release, so I don't know what to conclude from this:

I tried this code (minimal example):

#![feature(asm)]

const SSIZE: usize = 64;
fn main() {
    let mut ctx = ThreadContext::default();
    let mut n = ThreadContext::default();

    let mut stack = vec![0_u8; SSIZE];
    let stack_ptr = stack.as_mut_ptr();

    unsafe {
        std::ptr::write(
            stack_ptr.offset((SSIZE - 8) as isize) as *mut u64,
            test as u64,
        );
        ctx.rsp = stack_ptr.offset((SSIZE - 8) as isize) as u64;

    }
    switch(&mut ctx, &mut n);
}

fn test() -> ! {
    println!("WORKS!");

    loop {}
}


fn switch(old: *mut ThreadContext, new: *mut ThreadContext) {
    unsafe {
        asm!("
        mov     %rsp, 0x00($0)
        "
        : "=*m"(old)
        : "rm"(new)
        : "rsp", "r15", "r14", "r13", "r12", "rbx", "rbp", "memory"
        );

        dbg!(&*old);
    };
}

#[derive(Debug, Default)]
#[repr(C)]
struct ThreadContext {
    rsp: u64,
    r15: u64,
    r14: u64,
    r13: u64,
    r12: u64,
    rbx: u64,
    rbp: u64,
}

I expected it to compile on both release and debug build. Instead, i got this error when compiling for release:

error: <inline asm>:2:22: error: expected register here
        mov     0x00(-48(%rbp)), %rsp
                     ^

  --> src/main.rs:9:9
   |
9  | /         asm!("
10 | |         mov     0x00($0), %rsp
11 | |         ret
12 | |        "
...  |
15 | |         : "rsp", "r15", "r14", "r13", "r12", "rbx", "rbp", "memory"
16 | |         );
   | |__________^

error: aborting due to previous error

error: Could not compile `playground`.

Easiest steps to reproduce. See this playground link:
https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=23908e070df55393682c9a922c80085b

You will need to choose "run" or "show assembly", somehow if you choose build it builds without errors in the playground.

Meta

rustc 1.37.0-nightly (37d001e4d 2019-05-29)
binary: rustc
commit-hash: 37d001e4deb206ed954fde5d91690221e8306fc3
commit-date: 2019-05-29
host: x86_64-apple-darwin
release: 1.37.0-nightly
LLVM version: 8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inline-assemblyArea: Inline assembly (`asm!(…)`)O-macosOperating system: macOST-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