Skip to content

Code generation bug when using naked/inlineasm #73940

Closed
@npmccallum

Description

@npmccallum

I thought I'd do some exploration of the new Rust inline assembly support. Unfortunately, I think I've hit a bug.

Here's my code:

#![feature(naked_functions)]
#![feature(asm)]

#[naked]
#[must_use]
#[inline(never)]
pub unsafe extern "C" fn save(ctx: &mut [usize; 8]) -> bool {
    asm!(
        "pop    rax",
        "mov    [rdi + 0x00], r12",
        "mov    [rdi + 0x08], r13",
        "mov    [rdi + 0x10], r14",
        "mov    [rdi + 0x18], r15",
        "mov    [rdi + 0x20], rbx",
        "mov    [rdi + 0x28], rbp",
        "mov    [rdi + 0x30], rsp",
        "mov    [rdi + 0x38], rax",
        "jmp    rax",
        in("rdi") ctx,
        options(noreturn)
    )
}

When compiling (cargo rustc -- --emit asm), I get the following output:

        .section        .text._ZN6asmbug4save17h08c3eb7a61e00fbaE,"ax",@progbits
        .globl  _ZN6asmbug4save17h08c3eb7a61e00fbaE
        .p2align        4, 0x90
        .type   _ZN6asmbug4save17h08c3eb7a61e00fbaE,@function
_ZN6asmbug4save17h08c3eb7a61e00fbaE:
.Lfunc_begin0:
        .file   1 "asmbug/src/lib.rs"
        .loc    1 7 0
        .cfi_startproc
        movq    %rdi, (%rsp)		# This is definitely not correct...
.Ltmp0:
        .loc    1 8 5 prologue_end
        #APP

        popq    %rax
        movq    %r12, (%rdi)
        movq    %r13, 8(%rdi)
        movq    %r14, 16(%rdi)
        movq    %r15, 24(%rdi)
        movq    %rbx, 32(%rdi)
        movq    %rbp, 40(%rdi)
        movq    %rsp, 48(%rdi)
        movq    %rax, 56(%rdi)
        jmpq    *%rax

        #NO_APP
        ud2
.Ltmp1:
.Lfunc_end0:
        .size   _ZN6asmbug4save17h08c3eb7a61e00fbaE, .Lfunc_end0-_ZN6asmbug4save17h08c3eb7a61e00fbaE
        .cfi_endproc

However, if I compile a different way (rustc src/lib.rs --crate-type lib --emit asm), I don't get the offending line:

        .section        .text._ZN3lib4save17h41fda79ed270926dE,"ax",@progbits
        .globl  _ZN3lib4save17h41fda79ed270926dE
        .p2align        4, 0x90
        .type   _ZN3lib4save17h41fda79ed270926dE,@function
_ZN3lib4save17h41fda79ed270926dE:
        .cfi_startproc
        #APP

        popq    %rax
        movq    %r12, (%rdi)
        movq    %r13, 8(%rdi)
        movq    %r14, 16(%rdi)
        movq    %r15, 24(%rdi)
        movq    %rbx, 32(%rdi)
        movq    %rbp, 40(%rdi)
        movq    %rsp, 48(%rdi)
        movq    %rax, 56(%rdi)
        jmpq    *%rax

        #NO_APP
        ud2
.Lfunc_end0:
        .size   _ZN3lib4save17h41fda79ed270926dE, .Lfunc_end0-_ZN3lib4save17h41fda79ed270926dE
        .cfi_endproc

Likewise, if I compile for release (cargo rustc --release -- --emit asm), the offending line is missing:

        .section        .text._ZN6asmbug4save17h151c2e3546492694E,"ax",@progbits
        .globl  _ZN6asmbug4save17h151c2e3546492694E
        .p2align        4, 0x90
        .type   _ZN6asmbug4save17h151c2e3546492694E,@function
_ZN6asmbug4save17h151c2e3546492694E:
        .cfi_startproc
        #APP

        popq    %rax
        movq    %r12, (%rdi)
        movq    %r13, 8(%rdi)
        movq    %r14, 16(%rdi)
        movq    %r15, 24(%rdi)
        movq    %rbx, 32(%rdi)
        movq    %rbp, 40(%rdi)
        movq    %rsp, 48(%rdi)
        movq    %rax, 56(%rdi)
        jmpq    *%rax

        #NO_APP
        ud2
.Lfunc_end0:
        .size   _ZN6asmbug4save17h151c2e3546492694E, .Lfunc_end0-_ZN6asmbug4save17h151c2e3546492694E
        .cfi_endproc

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-nakedArea: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzSC-bugCategory: This is a bug.F-asm`#![feature(asm)]` (not `llvm_asm`)T-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