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

Code generation bug when using naked/inlineasm #73940

Closed
npmccallum opened this issue Jul 1, 2020 · 4 comments
Closed

Code generation bug when using naked/inlineasm #73940

npmccallum opened this issue Jul 1, 2020 · 4 comments
Labels
A-codegen Area: Code generation A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@npmccallum
Copy link
Contributor

npmccallum commented Jul 1, 2020

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
@npmccallum npmccallum added the C-bug Category: This is a bug. label Jul 1, 2020
@npmccallum
Copy link
Contributor Author

@joshtriplett FYI

@jonas-schievink jonas-schievink added A-codegen Area: Code generation A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS F-asm `#![feature(asm)]` (not `llvm_asm`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. labels Jul 1, 2020
@nbdd0121
Copy link
Contributor

nbdd0121 commented Jul 1, 2020

LLVM IR suggests that it is related to debugging support

; playground::save
; Function Attrs: naked noinline nounwind nonlazybind uwtable
define zeroext i1 @_ZN10playground4save17h7b21f7512f77098aE([8 x i64]* align 8 dereferenceable(64) %ctx) unnamed_addr #0 !dbg !6 {
start:
  %ctx.dbg.spill = alloca [8 x i64]*, align 8
  store [8 x i64]* %ctx, [8 x i64]** %ctx.dbg.spill, align 8
  call void @llvm.dbg.declare(metadata [8 x i64]** %ctx.dbg.spill, metadata !18, metadata !DIExpression()), !dbg !19
  call void asm sideeffect alignstack inteldialect "pop    rax\0Amov    [rdi + 0x00], r12\0Amov    [rdi + 0x08], r13\0Amov    [rdi + 0x10], r14\0Amov    [rdi + 0x18], r15\0Amov    [rdi + 0x20], rbx\0Amov    [rdi + 0x28], rbp\0Amov    [rdi + 0x30], rsp\0Amov    [rdi + 0x38], rax\0Ajmp    rax", "{di},~{dirflag},~{fpsr},~{flags},~{memory}"([8 x i64]* %ctx), !dbg !20, !srcloc !21
  unreachable, !dbg !20
}

@npmccallum
Copy link
Contributor Author

@haraldh FYI

@nagisa
Copy link
Member

nagisa commented Jul 4, 2020

Duplicate of #42779

@nagisa nagisa marked this as a duplicate of #42779 Jul 4, 2020
@nagisa nagisa closed this as completed Jul 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-codegen Area: Code generation A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants