Skip to content

Commit 87dd3a0

Browse files
committed
Make #[naked] an unsafe attribute
1 parent 191df20 commit 87dd3a0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+375
-403
lines changed

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ builtin_macros_multiple_defaults = multiple declared defaults
247247
.suggestion = make `{$ident}` default
248248
249249
builtin_macros_naked_functions_testing_attribute =
250-
cannot use `#[naked]` with testing attributes
250+
cannot use `#[unsafe(naked)]` with testing attributes
251251
.label = function marked with testing attribute here
252-
.naked_attribute = `#[naked]` is incompatible with testing attributes
252+
.naked_attribute = `#[unsafe(naked)]` is incompatible with testing attributes
253253
254254
builtin_macros_no_default_variant = `#[derive(Default)]` on enum with no `#[default]`
255255
.label = this enum needs a unit variant marked with `#[default]`

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,9 @@ global_asm! {
387387
}
388388

389389
#[cfg(all(not(jit), target_arch = "x86_64"))]
390-
#[naked]
390+
#[unsafe(naked)]
391391
extern "C" fn naked_test() {
392-
unsafe {
393-
naked_asm!("ret");
394-
}
392+
naked_asm!("ret")
395393
}
396394

397395
#[repr(C)]

compiler/rustc_error_codes/src/error_codes/E0736.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Erroneous code example:
1111

1212
```compile_fail,E0736
1313
#[inline]
14-
#[naked]
14+
#[unsafe(naked)]
1515
fn foo() {}
1616
```
1717

compiler/rustc_error_codes/src/error_codes/E0787.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Erroneous code example:
55
```compile_fail,E0787
66
#![feature(naked_functions)]
77
8-
#[naked]
8+
#[unsafe(naked)]
99
pub extern "C" fn f() -> u32 {
1010
42
1111
}

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
517517

518518
// Linking:
519519
gated!(
520-
naked, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
520+
unsafe naked, Normal, template!(Word), WarnFollowing, EncodeCrossCrate::No,
521521
naked_functions, experimental!(naked)
522522
),
523523

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,17 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
564564
}
565565
}
566566
ExprKind::InlineAsm(box InlineAsmExpr {
567-
asm_macro: AsmMacro::Asm | AsmMacro::NakedAsm,
567+
asm_macro: asm_macro @ (AsmMacro::Asm | AsmMacro::NakedAsm),
568568
ref operands,
569569
template: _,
570570
options: _,
571571
line_spans: _,
572572
}) => {
573-
self.requires_unsafe(expr.span, UseOfInlineAssembly);
573+
// The `naked` attribute and the `naked_asm!` block form one atomic unit of
574+
// unsafety, and `naked_asm!` does not itself need to be wrapped in an unsafe block.
575+
if let AsmMacro::Asm = asm_macro {
576+
self.requires_unsafe(expr.span, UseOfInlineAssembly);
577+
}
574578

575579
// For inline asm, do not use `walk_expr`, since we want to handle the label block
576580
// specially.

compiler/rustc_parse/src/validate_attr.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,12 +194,6 @@ pub fn check_attribute_safety(psess: &ParseSess, safety: AttributeSafety, attr:
194194
}
195195
}
196196
} else if let Safety::Unsafe(unsafe_span) = attr_item.unsafety {
197-
// Allow (but don't require) `#[unsafe(naked)]` so that compiler-builtins can upgrade to it.
198-
// FIXME(#139797): remove this special case when compiler-builtins has upgraded.
199-
if attr.has_name(sym::naked) {
200-
return;
201-
}
202-
203197
psess.dcx().emit_err(errors::InvalidAttrUnsafe {
204198
span: unsafe_span,
205199
name: attr_item.path.clone(),

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,17 +508,17 @@ passes_must_use_no_effect =
508508
`#[must_use]` has no effect when applied to {$article} {$target}
509509
510510
passes_naked_asm_outside_naked_fn =
511-
the `naked_asm!` macro can only be used in functions marked with `#[naked]`
511+
the `naked_asm!` macro can only be used in functions marked with `#[unsafe(naked)]`
512512
513513
passes_naked_functions_asm_block =
514514
naked functions must contain a single `naked_asm!` invocation
515515
.label_multiple_asm = multiple `naked_asm!` invocations are not allowed in naked functions
516516
.label_non_asm = not allowed in naked functions
517517
518518
passes_naked_functions_incompatible_attribute =
519-
attribute incompatible with `#[naked]`
520-
.label = the `{$attr}` attribute is incompatible with `#[naked]`
521-
.naked_attribute = function marked with `#[naked]` here
519+
attribute incompatible with `#[unsafe(naked)]`
520+
.label = the `{$attr}` attribute is incompatible with `#[unafe(naked)]`
521+
.naked_attribute = function marked with `#[unsafe(naked)]` here
522522
523523
passes_naked_functions_must_naked_asm =
524524
the `asm!` macro is not allowed in naked functions

src/doc/unstable-book/src/compiler-flags/sanitizer.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -247,34 +247,31 @@ See the [Clang ControlFlowIntegrity documentation][clang-cfi] for more details.
247247
```rust,ignore (making doc tests pass cross-platform is hard)
248248
#![feature(naked_functions)]
249249

250-
use std::arch::asm;
250+
use std::arch::naked_asm;
251251
use std::mem;
252252

253253
fn add_one(x: i32) -> i32 {
254254
x + 1
255255
}
256256

257-
#[naked]
257+
#[unsafe(naked)]
258258
pub extern "C" fn add_two(x: i32) {
259259
// x + 2 preceded by a landing pad/nop block
260-
unsafe {
261-
asm!(
262-
"
263-
nop
264-
nop
265-
nop
266-
nop
267-
nop
268-
nop
269-
nop
270-
nop
271-
nop
272-
lea eax, [rdi+2]
273-
ret
274-
",
275-
options(noreturn)
276-
);
277-
}
260+
naked_asm!(
261+
"
262+
nop
263+
nop
264+
nop
265+
nop
266+
nop
267+
nop
268+
nop
269+
nop
270+
nop
271+
lea eax, [rdi+2]
272+
ret
273+
"
274+
);
278275
}
279276

280277
fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {

tests/assembly/naked-functions/aarch64-naked-fn-no-bti-prolog.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::arch::naked_asm;
1313
// LLVM implements this via making sure of that, even for functions with the naked attribute.
1414
// So, we must emit an appropriate instruction instead!
1515
#[no_mangle]
16-
#[naked]
17-
pub unsafe extern "C" fn _hlt() -> ! {
16+
#[unsafe(naked)]
17+
pub extern "C" fn _hlt() -> ! {
1818
// CHECK-NOT: hint #34
1919
// CHECK: hlt #0x1
2020
naked_asm!("hlt #1")

0 commit comments

Comments
 (0)