Skip to content

Commit 4ee56d4

Browse files
bstriefolkertdev
authored andcommittedApr 19, 2025
Add documentation for the naked function attribute
1 parent 3bf3402 commit 4ee56d4

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
 

‎src/attributes/codegen.md

+86
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,81 @@ r[attributes.codegen.cold]
4646
The *`cold` [attribute]* suggests that the attributed function is unlikely to
4747
be called.
4848

49+
r[attributes.codegen.naked]
50+
## The `naked` attribute
51+
52+
The *`naked` [attribute]* may be applied to a function in order to prevent the compiler
53+
from emitting a function prologue.
54+
55+
### Requirements
56+
57+
Any function marked with the `naked` attribute must meet the following requirements;
58+
failure to do so will result in a compiler error.
59+
60+
* The [function body] must consist of exactly one [`asm!`] macro invocation,
61+
which may be enclosed within an [unsafe block].
62+
* This `asm!` invocation must not contain any [operands]
63+
except for `const` and `sym` operands.
64+
* This `asm!` invocation must specify the `noreturn` [option],
65+
and must not specify any other options except for `att_syntax`.
66+
* The function must not be marked with the [`inline`] attribute.
67+
68+
### Recommendations
69+
70+
Any function marked with the `naked` attribute should adhere to the following recommendations;
71+
failure to do so will result in a compiler warning.
72+
73+
* The function should feature an [extern function qualifier] that is not `extern "Rust"`.
74+
* All arguments and return types of the function should be [FFI-safe].
75+
76+
### Effects
77+
78+
Marking a function with the `naked` attribute has the following effects:
79+
80+
* The compiler will not generate a prologue for this function.
81+
Within the function, all registers will remain precisely as they were set up
82+
by its caller.
83+
* The compiler will suppress the [`unused_variables`] lint for this function.
84+
85+
### Notes
86+
87+
* The [rules for inline assembly] ordinarily consider it undefined behavior to
88+
refer to registers not specified as input operands, or to modify
89+
registers not specified as output operands.
90+
The reason for this is because ordinarily an `asm!` invocation cannot guarantee
91+
the state of the registers surrounding the assembly block.
92+
However, in naked functions the state of the registers is guaranteed
93+
by adherence to the specified calling convention.
94+
Therefore, it is not undefined behavior for the `asm!` invocation in a naked function
95+
to refer to registers without specifying them as operands.
96+
* A naked function that makes use of registers in a way that does not conform
97+
to the specified calling convention imposes additional safety invariants on its caller,
98+
and therefore must be marked as an [unsafe function].
99+
* Implementations may assume that naked functions never unwind.
100+
Unwinding through a naked function is undefined behavior.
101+
* The semantics of naked functions require implementations to set up the call stack
102+
according to the specified calling convention before executing a naked function,
103+
even in contexts where setting up the call stack would ordinarily be unnecessary,
104+
such as when the function is inlined.
105+
An implementation can fulfill this requirement by guaranteeing that naked functions
106+
are never inlined.
107+
However, implementations are not currently required to guarantee that naked functions
108+
are never inlined.
109+
In the future it may become a requirement for implementations to guarantee that
110+
naked functions are never inlined;
111+
users must not rely on any observable behavior that may result from inlining.
112+
* Although implementations are prohibited from generating code for a naked function that
113+
contains any instructions that precede the naked function's `asm!` block,
114+
under some circumstances, implementations may generate code that contains instructions
115+
*after* a naked function's `asm!` block.
116+
In the future it may become a requirement for implementations to guarantee
117+
the absence of any instructions following a naked function's `asm!` block;
118+
users must not rely on the presence of any trailing instructions.
119+
If a user of the `naked` attribute relies on the absence of trailing instructions
120+
for correctness, for the time being it is the user's responsibility to ensure that
121+
the instructions truly are absent,
122+
for example by passing any necessary code generation flags to the compiler.
123+
49124
r[attributes.codegen.no_builtins]
50125
## The `no_builtins` attribute
51126

@@ -499,13 +574,24 @@ trait object whose methods are attributed.
499574
[`-C target-feature`]: ../../rustc/codegen-options/index.html#target-feature
500575
[`is_x86_feature_detected`]: ../../std/arch/macro.is_x86_feature_detected.html
501576
[`is_aarch64_feature_detected`]: ../../std/arch/macro.is_aarch64_feature_detected.html
577+
[`naked_asm!`]: ../inline-assembly.md
578+
[`inline`]: #the-inline-attribute
502579
[`target_feature` conditional compilation option]: ../conditional-compilation.md#target_feature
580+
[`unused_variables`]: ../../rustc/lints/listing/warn-by-default.html#unused-variables
503581
[attribute]: ../attributes.md
504582
[attributes]: ../attributes.md
583+
[extern function qualifier]: ../items/functions.md#extern-function-qualifier
584+
[FFI-safe]: ../../rustc/lints/listing/warn-by-default.html#improper-ctypes-definitions
585+
[function body]: ../items/functions.md#function-body
505586
[functions]: ../items/functions.md
587+
[operands]: ../inline-assembly.md#operand-type
588+
[option]: ../inline-assembly.md#options
589+
[rules for inline assembly]: ../inline-assembly.md#rules-for-inline-assembly
506590
[target architecture]: ../conditional-compilation.md#target_arch
507591
[trait]: ../items/traits.md
508592
[undefined behavior]: ../behavior-considered-undefined.md
593+
[unsafe block]: ../unsafe-blocks.md
594+
[unsafe function]: ../unsafe-functions.md
509595
[rust-abi]: ../items/external-blocks.md#abi
510596
[`Location`]: core::panic::Location
511597

0 commit comments

Comments
 (0)
Please sign in to comment.