Skip to content

naked + variadic args partially ignores the naked #99858

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

Closed
aviramha opened this issue Jul 28, 2022 · 1 comment · Fixed by #123249
Closed

naked + variadic args partially ignores the naked #99858

aviramha opened this issue Jul 28, 2022 · 1 comment · Fixed by #123249
Labels
A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS C-bug Category: This is a bug. F-naked_functions `#![feature(naked_functions)]`

Comments

@aviramha
Copy link
Contributor

I found out that if I define a #[naked] function that has variadic args it still emits a function prolog.

I tried this code:

#![feature(c_variadic)]
#![feature(naked_functions)]
use std::{arch::asm};
#[no_mangle]
#[cfg(target_os = "linux")]
#[cfg(target_arch = "x86_64")]
#[naked]
unsafe extern "C" fn rsyscall(syscall: i64, args: ...) -> i64 {
    asm!(
        "mov    rax, rdi",
        "mov    rdi, rsi",
        "mov    rsi, rdx",
        "mov    rdx, rcx",
        "mov    r10, r8",
        "mov    r8, r9",
        "mov    r9, qword ptr[rsp]",
        "syscall",
        "ret",
        options(noreturn)
    )
}

I expected to see this happen: asm output:

        mov     rax, rdi
        mov     rdi, rsi
        mov     rsi, rdx
        mov     rdx, rcx
        mov     r10, r8
        mov     r8, r9
        mov     r9, qword ptr [rsp]
        syscall
        ret

Instead, this happened: asm output

rsyscall:
        mov     rax, rsp
        mov     qword ptr [rsp + 16], rax
        mov     qword ptr [rsp + 8], rax
        mov     qword ptr [rsp], 0

        mov     rax, rdi
        mov     rdi, rsi
        mov     rsi, rdx
        mov     rdx, rcx
        mov     r10, r8
        mov     r8, r9
        mov     r9, qword ptr [rsp]
        syscall
        ret

Meta

rustc --version --verbose:

rustc 1.64.0-nightly (2643b1646 2022-07-27)
binary: rustc
commit-hash: 2643b16468fda787470340890212591d8bc832b7
commit-date: 2022-07-27
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6
@aviramha aviramha added the C-bug Category: This is a bug. label Jul 28, 2022
@bjorn3 bjorn3 added A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS F-naked_functions `#![feature(naked_functions)]` labels Jul 28, 2022
@bjorn3
Copy link
Member

bjorn3 commented Jul 28, 2022

As workaround you could use global_asm!.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 12, 2024
do not add prolog for variadic naked functions

fixes rust-lang#99858
@bors bors closed this as completed in 9139d72 Apr 12, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 12, 2024
Rollup merge of rust-lang#123249 - goolmoos:naked_variadics, r=pnkfelix

do not add prolog for variadic naked functions

fixes rust-lang#99858
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS C-bug Category: This is a bug. F-naked_functions `#![feature(naked_functions)]`
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants