Skip to content

Commit

Permalink
use an allow list for allowed asm options in naked functions
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Jul 24, 2024
1 parent c31ff97 commit 4b7a87d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
21 changes: 9 additions & 12 deletions compiler/rustc_passes/src/naked_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,22 +244,19 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
self.tcx.dcx().emit_err(NakedFunctionsOperands { unsupported_operands });
}

let unsupported_options: Vec<&'static str> = [
(InlineAsmOptions::MAY_UNWIND, "`may_unwind`"),
(InlineAsmOptions::NOMEM, "`nomem`"),
(InlineAsmOptions::NOSTACK, "`nostack`"),
(InlineAsmOptions::PRESERVES_FLAGS, "`preserves_flags`"),
(InlineAsmOptions::PURE, "`pure`"),
(InlineAsmOptions::READONLY, "`readonly`"),
]
.iter()
.filter_map(|&(option, name)| if asm.options.contains(option) { Some(name) } else { None })
.collect();
let supported_options =
InlineAsmOptions::RAW | InlineAsmOptions::NORETURN | InlineAsmOptions::ATT_SYNTAX;
let unsupported_options = asm.options.difference(supported_options);

if !unsupported_options.is_empty() {
self.tcx.dcx().emit_err(NakedFunctionsAsmOptions {
span,
unsupported_options: unsupported_options.join(", "),
unsupported_options: unsupported_options
.human_readable_names()
.into_iter()
.map(|name| format!("`{name}`"))
.collect::<Vec<_>>()
.join(", "),
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/asm/naked-functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ unsafe extern "C" fn invalid_options() {
unsafe extern "C" fn invalid_options_continued() {
asm!("", options(readonly, nostack), options(pure));
//~^ ERROR asm with the `pure` option must have at least one output
//~| ERROR asm options unsupported in naked functions: `nostack`, `pure`, `readonly`
//~| ERROR asm options unsupported in naked functions: `pure`, `readonly`, `nostack`
//~| ERROR asm in naked functions must use `noreturn` option
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/asm/naked-functions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ error[E0787]: asm options unsupported in naked functions: `nomem`, `preserves_fl
LL | asm!("", options(nomem, preserves_flags, noreturn));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0787]: asm options unsupported in naked functions: `nostack`, `pure`, `readonly`
error[E0787]: asm options unsupported in naked functions: `pure`, `readonly`, `nostack`
--> $DIR/naked-functions.rs:112:5
|
LL | asm!("", options(readonly, nostack), options(pure));
Expand Down

0 comments on commit 4b7a87d

Please sign in to comment.