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

Naked functions contain a trailing ret insn #32487

Closed
nagisa opened this issue Mar 25, 2016 · 5 comments
Closed

Naked functions contain a trailing ret insn #32487

nagisa opened this issue Mar 25, 2016 · 5 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented Mar 25, 2016

#[naked]
fn naked(){
    unsafe {
    asm!("ret");
    }
}

results in

; Function Attrs: naked uwtable
define internal void @_ZN5naked20hb8640283bf863d54eaaE() unnamed_addr #0 {
entry-block:
  call void asm "ret", "~{dirflag},~{fpsr},~{flags}"(), !srcloc !0
  ret void
}
    .section    .text._ZN5naked20hb8640283bf863d54eaaE,"ax",@progbits
    .align  16, 0x90
    .type   _ZN5naked20hb8640283bf863d54eaaE,@function
_ZN5naked20hb8640283bf863d54eaaE:
    .cfi_startproc
    #APP
    retq
    #NO_APP
    retq
.Lfunc_end0:
    .size   _ZN5naked20hb8640283bf863d54eaaE, .Lfunc_end0-_ZN5naked20hb8640283bf863d54eaaE
    .cfi_endproc

note the trailing retq instruction that shouldn’t otherwise be here.

An equivalent function compiled by clang looks like this:

; Function Attrs: naked noinline nounwind uwtable
define void @naked() #0 {
  call void asm sideeffect "ret", "~{dirflag},~{fpsr},~{flags}"() #1, !srcloc !1
  unreachable
}
    .globl  naked
    .align  16, 0x90
    .type   naked,@function
naked:                                  # @naked
    .cfi_startproc
# BB#0:
    #APP
    retq
    #NO_APP
.Lfunc_end0:
    .size   naked, .Lfunc_end0-naked
    .cfi_endproc
@nagisa nagisa added the A-codegen Area: Code generation label Mar 25, 2016
@nagisa
Copy link
Member Author

nagisa commented Mar 25, 2016

cc @ticki

@nagisa nagisa changed the title Naked functions: contain a trailing ret insn Naked functions contain a trailing ret insn Mar 25, 2016
@ticki
Copy link
Contributor

ticki commented Mar 25, 2016

This is weird...

@nagisa
Copy link
Member Author

nagisa commented Mar 25, 2016

cc #32408

@nagisa nagisa added the A-naked Area: `#[naked]`, prologue and epilogue-free, functions, https://git.io/vAzzS label Jun 2, 2016
edef1c pushed a commit to edef1c/libfringe that referenced this issue Aug 13, 2016
The core::intrinsics::unreachable() we used at the end of every naked
function is essentially pointless, as #[naked] implies that intrinsic
at the end of the function. rustc currently does not implement that
behavior (rust-lang/rust#32487), but it is a bug. On top of that,
anything except a single asm!() in naked functions is likely to be
disallowed in the future (rust-lang/rust#32490).

A nice side effect is that we avoid the core_intrinsics feature,
which will be never stabilized, though neither asm nor
naked_functions are likely to be stabilized soon.
edef1c pushed a commit to edef1c/libfringe that referenced this issue Feb 25, 2017
The core::intrinsics::unreachable() we used at the end of every naked
function is essentially pointless, as #[naked] implies that intrinsic
at the end of the function. rustc currently does not implement that
behavior (rust-lang/rust#32487), but it is a bug. On top of that,
anything except a single asm!() in naked functions is likely to be
disallowed in the future (rust-lang/rust#32490).

A nice side effect is that we avoid the core_intrinsics feature,
which will be never stabilized, though neither asm nor
naked_functions are likely to be stabilized soon.
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 24, 2017
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 25, 2019
@npmccallum
Copy link
Contributor

@nagisa This is not a bug. You need to use the noreturn option. See: https://github.com/Amanieu/rfcs/blob/inline-asm/text/0000-inline-asm.md#options-1

@steveklabnik @Mark-Simulacrum This can be closed.

@steveklabnik
Copy link
Member

Well, please note that when @nagisa wrote this, there was an entirely different asm implementation.

Checking today's nightly,

#![feature(naked_functions, asm)]

#[naked]
pub fn naked(){
    unsafe {
        asm!("ret");
    }
}

gives

playground::naked: # @playground::naked
# %bb.0:
	#APP
	retq
	#NO_APP
	retq

while

#![feature(naked_functions, asm)]

#[naked]
pub fn naked(){
    unsafe {
        asm!("ret", options(noreturn));
    }
}

gives

playground::naked: # @playground::naked
# %bb.0:
	#APP
	retq
	#NO_APP
	ud2

It does seem like the new impl has these semantics, and so yes, this isn't a bug. Thanks all!

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. 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

5 participants