From b005c00e701715925118e5d3e2aab2036cdccf1d Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 3 Jun 2025 09:34:54 -0700 Subject: [PATCH 1/5] Add naked example --- src/attributes/codegen.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index cf9837d9e..c725cb2c7 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -52,6 +52,25 @@ r[attributes.codegen.naked] r[attributes.codegen.naked.intro] The *`naked` [attribute]* prevents the compiler from emitting a function prologue and epilogue for the attributed function. +> [!EXAMPLE] +> ```rust +> # #[cfg(target_arch = "x86_64")] { +> /// Adds 3 to the given number. +> /// +> /// SAFETY: The validity of the used registers +> /// is guaranteed according to the "sysv64" ABI. +> #[unsafe(naked)] +> pub extern "sysv64" fn add_n(number: usize) -> usize { +> core::arch::naked_asm!( +> "add rdi, {}", +> "mov rax, rdi", +> "ret", +> const 3, +> ) +> } +> # } +> ``` + r[attributes.codegen.naked.body] The [function body] must consist of exactly one [`naked_asm!`] macro invocation. From 5300cfcb67cab1567a01cc8d0eccbc7d2c6e7541 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 3 Jun 2025 09:35:10 -0700 Subject: [PATCH 2/5] Add naked attribute template rules --- src/attributes/codegen.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index c725cb2c7..442113147 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -71,6 +71,23 @@ The *`naked` [attribute]* prevents the compiler from emitting a function prologu > # } > ``` +r[attributes.codegen.naked.syntax] +The `naked` attribute uses the [MetaWord] syntax and thus does not take any inputs. + +r[attributes.codegen.naked.allowed-positions] +The `naked` attribute may only be applied to: + +- [Free functions][items.fn] +- [Inherent associated functions][items.associated.fn] +- [Trait impl functions][items.impl.trait] +- [Trait definition functions][items.traits] with a body + +r[attributes.codegen.naked.duplicates] +Duplicate instances of the `naked` attribute have no effect. + +> [!NOTE] +> `rustc` currently warns on subsequent duplicate `naked` attributes. + r[attributes.codegen.naked.body] The [function body] must consist of exactly one [`naked_asm!`] macro invocation. From f3df38805e4f66fcf7f70e1977336c3e21ba9f39 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 3 Jun 2025 09:36:57 -0700 Subject: [PATCH 3/5] Move and reword attributes.codegen.naked.unsafe To follow the attribute template, and to simplify the wording a little. --- src/attributes/codegen.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 442113147..040b8e64b 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -88,15 +88,15 @@ Duplicate instances of the `naked` attribute have no effect. > [!NOTE] > `rustc` currently warns on subsequent duplicate `naked` attributes. +r[attributes.codegen.naked.unsafe] +The `naked` attribute must be marked with [`unsafe`][attributes.safety] because the body must respect the function's calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code). + r[attributes.codegen.naked.body] The [function body] must consist of exactly one [`naked_asm!`] macro invocation. r[attributes.codegen.naked.prologue-epilogue] No function prologue or epilogue is generated for the attributed function. The assembly code in the `naked_asm!` block constitutes the full body of a naked function. -r[attributes.codegen.naked.unsafe-attribute] -The `naked` attribute is an [unsafe attribute]. Annotating a function with `#[unsafe(naked)]` comes with the safety obligation that the body must respect the function's calling convention, uphold its signature, and either return or diverge (i.e., not fall through past the end of the assembly code). - r[attributes.codegen.naked.call-stack] The assembly code may assume that the call stack and register state are valid on entry as per the signature and calling convention of the function. From f232a270010296c5db059f670360dfbfa2035f56 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 3 Jun 2025 09:37:45 -0700 Subject: [PATCH 4/5] Add attributes.codegen.naked.target_feature naked cannot be used with target_feature. https://github.com/rust-lang/rust/blob/aae43c4532690153af7465227816c93036bb1604/compiler/rustc_passes/src/check_attr.rs#L672-L685 --- src/attributes/codegen.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 040b8e64b..3ae791dbf 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -118,6 +118,11 @@ The [`track_caller`](#the-track_caller-attribute) attribute cannot be applied to r[attributes.codegen.naked.testing] The [testing attributes](testing.md) cannot be applied to a naked function. +r[attributes.codegen.naked.target_feature] +The [`target_feature`][attributes.codegen.target_feature] attribute cannot be applied to a naked function. + + + r[attributes.codegen.no_builtins] ## The `no_builtins` attribute From 36d52fc2eda0a79714a38d51c0cda3286f6cbd9d Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Tue, 3 Jun 2025 09:38:21 -0700 Subject: [PATCH 5/5] Add attributes.codegen.naked.abi Naked attributes cannot have the "Rust" ABI. https://github.com/rust-lang/rust/blob/aae43c4532690153af7465227816c93036bb1604/compiler/rustc_passes/src/check_attr.rs#L641-L652 --- src/attributes/codegen.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/attributes/codegen.md b/src/attributes/codegen.md index 3ae791dbf..c136c150f 100644 --- a/src/attributes/codegen.md +++ b/src/attributes/codegen.md @@ -123,6 +123,9 @@ The [`target_feature`][attributes.codegen.target_feature] attribute cannot be ap +r[attributes.codegen.naked.abi] +The function cannot have the ["Rust" ABI][items.extern.abi.rust]. + r[attributes.codegen.no_builtins] ## The `no_builtins` attribute