Skip to content

Commit c4d68c9

Browse files
committed
formatting + update to unsafe(naked)
1 parent 0f837d8 commit c4d68c9

File tree

3 files changed

+41
-41
lines changed

3 files changed

+41
-41
lines changed

Diff for: src/attributes.md

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ The following attributes are unsafe:
4545
* [`export_name`]
4646
* [`link_section`]
4747
* [`no_mangle`]
48+
* [`naked`]
4849

4950
r[attributes.kind]
5051
Attributes can be classified into the following kinds:

Diff for: src/attributes/codegen.md

+9-16
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,22 @@ r[attributes.codegen.naked]
5050
## The `naked` attribute
5151

5252
r[attributes.codegen.naked.intro]
53-
The *`naked` [attribute]* prevents the compiler from emitting a function prologue and
54-
epilogue for the attributed function.
53+
The *`naked` [attribute]* prevents the compiler from emitting a function prologue and epilogue for the attributed function.
5554

5655
r[attributes.codegen.naked.body]
57-
The [function body] must consist of exactly one [`naked_asm!`] macro invocation, which
58-
may be enclosed within an [unsafe block].
56+
The [function body] must consist of exactly one [`naked_asm!`] macro invocation.
5957

6058
r[attributes.codegen.naked.prologue-epilogue]
61-
No function prologue or epilogue are generated for the attributed function: the contents
62-
of the `naked_asm!` invocation make up the full body of a naked function.
59+
No function prologue or epilogue are generated for the attributed function: the contents of the `naked_asm!` invocation make up the full body of a naked function.
60+
61+
r[attributes.codegen.naked.unsafe-attribute]
62+
The `naked` attribute is an [unsafe attribute]. Annotating a function with `#[unsafe(naked)]` comes with the safety obligation that the body respects the function's calling convention, and that the body either returns or diverges.
6363

6464
r[attributes.codegen.naked.call-stack]
65-
The asm code will have a valid call stack and register state on entry as per the signature and calling convention of the function.
65+
The assembly code will have a valid call stack and register state on entry as per the signature and calling convention of the function.
6666

6767
r[attributes.codegen.naked.no-duplication]
68-
The asm code may not be duplicated by the compiler, except when monomorphizing polymorphic functions.
69-
This property is important for naked functions that define symbols in the assembly code.
70-
71-
r[attributes.codegen.naked.unsafe-function]
72-
A naked function that makes use of registers in a way that does not conform
73-
to the specified calling convention imposes additional safety invariants on its caller,
74-
and therefore must be marked as an [unsafe function].
68+
The assembly code may not be duplicated by the compiler, except when monomorphizing polymorphic functions. This property is important for naked functions that define symbols in the assembly code.
7569

7670
r[attributes.codegen.naked.unused-variables]
7771
The [`unused_variables`] lint is suppressed within naked functions.
@@ -552,8 +546,7 @@ trait object whose methods are attributed.
552546
[target architecture]: ../conditional-compilation.md#target_arch
553547
[trait]: ../items/traits.md
554548
[undefined behavior]: ../behavior-considered-undefined.md
555-
[unsafe block]: ../unsafe-blocks.md
556-
[unsafe function]: ../unsafe-functions.md
549+
[unsafe attribute]: ../attributes.md#r-attributes.safety
557550
[rust-abi]: ../items/external-blocks.md#abi
558551
[`Location`]: core::panic::Location
559552

Diff for: src/inline-assembly.md

+31-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ r[asm]
22
# Inline assembly
33

44
r[asm.intro]
5-
Support for inline assembly is provided via the [`asm!`], [`naked_asm!`] and [`global_asm!`] macros.
5+
Support for inline assembly is provided via the [`asm!`], [`naked_asm!`], and [`global_asm!`] macros.
66
It can be used to embed handwritten assembly in the assembly output generated by the compiler.
77

88
[`asm!`]: core::arch::asm
@@ -84,6 +84,15 @@ r[asm.scope.naked_asm]
8484
With the `naked_asm!` macro, the assembly code is emitted in a function scope and constitutes the full assembly code of a function.
8585
The `naked_asm!` macro is only allowed in [naked functions](attributes/codegen.md#the-naked-attribute).
8686

87+
```rust
88+
# #[cfg(target_arch = "x86_64")] {
89+
# #[unsafe(naked)]
90+
# extern "C" fn wrapper() {
91+
core::arch::naked_asm!("/* {} */", const 0);
92+
# }
93+
# }
94+
```
95+
8796
r[asm.scope.global_asm]
8897
With the `global_asm!` macro, the assembly code is emitted in a global scope, outside a function.
8998
This can be used to hand-write entire functions using assembly code, and generally provides much more freedom to use arbitrary registers and assembler directives.
@@ -391,7 +400,7 @@ assert_eq!(y, 1);
391400
```
392401

393402
r[asm.operand-type.naked_asm-restriction]
394-
Because `naked_asm!` defines a whole function body, it can only use `sym` and `const` operands.
403+
Because `naked_asm!` defines a whole function body and the compiler cannot emit any additional code to handle operands, it can only use `sym` and `const` operands.
395404

396405
r[asm.operand-type.global_asm-restriction]
397406
Because `global_asm!` exists outside a function, it can only use `sym` and `const` operands.
@@ -1392,12 +1401,12 @@ r[asm.naked-rules.reg-not-output]
13921401

13931402
r[asm.naked-rules.noreturn]
13941403
- Behavior is undefined if execution falls through to the end of the `naked_asm!` block.
1395-
- the assembly code is expected to contain a return instruction or to diverge
1404+
- every path through the assembly code is expected to terminate with a return instruction or to diverge
13961405

13971406
r[asm.naked-rules.mem-same-as-ffi]
13981407
- The set of memory locations that assembly code is allowed to read and write are the same as those allowed for an FFI function.
13991408
- Refer to the unsafe code guidelines for the exact rules.
1400-
- These rules do not apply to memory which is private to the asm code, such as stack space allocated within the `naked_asm!` block.
1409+
- These rules do not apply to memory which is private to the assembly code, such as stack space allocated within the `naked_asm!` block.
14011410

14021411
r[asm.naked-rules.black-box]
14031412
- The compiler cannot assume that the instructions in the `naked_asm!` block are the ones that will actually be executed.
@@ -1408,29 +1417,26 @@ r[asm.naked-rules.unwind]
14081417
- Unwinding out of a `naked_asm!` block is allowed.
14091418
- For correct behavior, the appropriate assembler directives that emit unwinding metadata must be used.
14101419

1411-
<!-- #[naked] is currently unstable, tests would fail if this were marked `rust` -->
1412-
```txt
1420+
```rust
14131421
# #[cfg(target_arch = "x86_64")] {
1414-
#[naked]
1422+
#[unsafe(naked)]
14151423
extern "C-unwind" fn naked_function() {
1416-
unsafe {
1417-
core::arch::naked_asm!(
1418-
".cfi_startproc",
1419-
"push rbp",
1420-
".cfi_def_cfa_offset 16",
1421-
".cfi_offset rbp, -16",
1422-
"mov rbp, rsp",
1423-
".cfi_def_cfa_register rbp",
1424-
"",
1425-
"call {function}",
1426-
"",
1427-
"pop rbp",
1428-
".cfi_def_cfa rsp, 8",
1429-
"ret",
1430-
".cfi_endproc",
1431-
function = sym function_that_panics,
1432-
)
1433-
}
1424+
core::arch::naked_asm!(
1425+
".cfi_startproc",
1426+
"push rbp",
1427+
".cfi_def_cfa_offset 16",
1428+
".cfi_offset rbp, -16",
1429+
"mov rbp, rsp",
1430+
".cfi_def_cfa_register rbp",
1431+
"",
1432+
"call {function}",
1433+
"",
1434+
"pop rbp",
1435+
".cfi_def_cfa rsp, 8",
1436+
"ret",
1437+
".cfi_endproc",
1438+
function = sym function_that_panics,
1439+
)
14341440
}
14351441

14361442
extern "C-unwind" fn function_that_panics() {

0 commit comments

Comments
 (0)