Skip to content

Commit 1f8dc72

Browse files
authored
Merge pull request #1344 from workingjubilee/patch-1
Explain typos in `asm!` can be unsound
2 parents 5c328ef + aba56b9 commit 1f8dc72

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/inline-assembly.md

+23-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ asm := "asm!(" format_string *("," format_string) *("," operand) [","] ")"
5252
global_asm := "global_asm!(" format_string *("," format_string) *("," operand) [","] ")"
5353
```
5454

55-
5655
## Scope
5756

5857
Inline assembly can be used in one of two ways.
@@ -485,6 +484,29 @@ To avoid undefined behavior, these rules must be followed when using function-sc
485484

486485
> **Note**: As a general rule, the flags covered by `preserves_flags` are those which are *not* preserved when performing a function call.
487486
487+
### Correctness and Validity
488+
489+
In addition to all of the previous rules, the string argument to `asm!` must ultimately become—
490+
after all other arguments are evaluated, formatting is performed, and operands are translated—
491+
assembly that is both syntactically correct and semantically valid for the target architecture.
492+
The formatting rules allow the compiler to generate assembly with correct syntax.
493+
Rules concerning operands permit valid translation of Rust operands into and out of `asm!`.
494+
Adherence to these rules is necessary, but not sufficient, for the final expanded assembly to be
495+
both correct and valid. For instance:
496+
497+
- arguments may be placed in positions which are syntactically incorrect after formatting
498+
- an instruction may be correctly written, but given architecturally invalid operands
499+
- an architecturally unspecified instruction may be assembled into unspecified code
500+
- a set of instructions, each correct and valid, may cause undefined behavior if placed in immediate succession
501+
502+
As a result, these rules are _non-exhaustive_. The compiler is not required to check the
503+
correctness and validity of the initial string nor the final assembly that is generated.
504+
The assembler may check for correctness and validity but is not required to do so.
505+
When using `asm!`, a typographical error may be sufficient to make a program unsound,
506+
and the rules for assembly may include thousands of pages of architectural reference manuals.
507+
Programmers should exercise appropriate care, as invoking this `unsafe` capability comes with
508+
assuming the responsibility of not violating rules of both the compiler or the architecture.
509+
488510
### Directives Support
489511

490512
Inline assembly supports a subset of the directives supported by both GNU AS and LLVM's internal assembler, given as follows.

0 commit comments

Comments
 (0)