Skip to content

Commit 809dc73

Browse files
committed
Make asm label blocks safe context
`asm!()` is forced to be wrapped inside unsafe. If there's no special treatment, the label blocks would also always be unsafe with no way of opting out.
1 parent 4cc494b commit 809dc73

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed

Diff for: compiler/rustc_mir_build/src/check_unsafety.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,45 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
541541
self.requires_unsafe(expr.span, DerefOfRawPointer);
542542
}
543543
}
544-
ExprKind::InlineAsm { .. } => {
544+
ExprKind::InlineAsm(box InlineAsmExpr {
545+
asm_macro: _,
546+
ref operands,
547+
template: _,
548+
options: _,
549+
line_spans: _,
550+
}) => {
545551
self.requires_unsafe(expr.span, UseOfInlineAssembly);
552+
553+
// For inline asm, do not use `walk_expr`, since we want to handle the label block
554+
// specially.
555+
for op in &**operands {
556+
use rustc_middle::thir::InlineAsmOperand::*;
557+
match op {
558+
In { expr, reg: _ }
559+
| Out { expr: Some(expr), reg: _, late: _ }
560+
| InOut { expr, reg: _, late: _ } => self.visit_expr(&self.thir()[*expr]),
561+
SplitInOut { in_expr, out_expr, reg: _, late: _ } => {
562+
self.visit_expr(&self.thir()[*in_expr]);
563+
if let Some(out_expr) = out_expr {
564+
self.visit_expr(&self.thir()[*out_expr]);
565+
}
566+
}
567+
Out { expr: None, reg: _, late: _ }
568+
| Const { value: _, span: _ }
569+
| SymFn { value: _, span: _ }
570+
| SymStatic { def_id: _ } => {}
571+
Label { block } => {
572+
// Label blocks are safe context.
573+
// `asm!()` is forced to be wrapped inside unsafe. If there's no special
574+
// treatment, the label blocks would also always be unsafe with no way
575+
// of opting out.
576+
self.in_safety_context(SafetyContext::Safe, |this| {
577+
visit::walk_block(this, &this.thir()[*block])
578+
});
579+
}
580+
}
581+
}
582+
return;
546583
}
547584
ExprKind::Adt(box AdtExpr {
548585
adt_def,

Diff for: src/doc/unstable-book/src/language-features/asm-goto.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ unsafe {
2121
}
2222
```
2323

24-
The block must have unit type or diverge.
24+
The block must have unit type or diverge. The block starts a new safety context,
25+
so despite outer `unsafe`, you need extra unsafe to perform unsafe operations
26+
within `label <block>`.
2527

2628
When `label <block>` is used together with `noreturn` option, it means that the
2729
assembly will not fallthrough. It's allowed to jump to a label within the

Diff for: tests/ui/asm/x86_64/goto-block-safe.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ only-x86_64
2+
//@ needs-asm-support
3+
4+
#![deny(unreachable_code)]
5+
#![feature(asm_goto)]
6+
7+
use std::arch::asm;
8+
9+
fn goto_fallthough() {
10+
unsafe {
11+
asm!(
12+
"/* {} */",
13+
label {
14+
core::hint::unreachable_unchecked();
15+
//~^ ERROR [E0133]
16+
}
17+
)
18+
}
19+
}
20+
21+
fn main() {
22+
goto_fallthough();
23+
}

Diff for: tests/ui/asm/x86_64/goto-block-safe.stderr

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0133]: call to unsafe function `unreachable_unchecked` is unsafe and requires unsafe function or block
2+
--> $DIR/goto-block-safe.rs:14:17
3+
|
4+
LL | unsafe {
5+
| ------ items do not inherit unsafety from separate enclosing items
6+
...
7+
LL | core::hint::unreachable_unchecked();
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call to unsafe function
9+
|
10+
= note: consult the function's documentation for information on how to avoid undefined behavior
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)