use an allow list for allowed asm options in naked functions

This commit is contained in:
Folkert 2024-07-24 15:27:56 +02:00
parent c31ff97bf1
commit 4b7a87de10
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C
3 changed files with 11 additions and 14 deletions

View File

@ -244,22 +244,19 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
self.tcx.dcx().emit_err(NakedFunctionsOperands { unsupported_operands }); self.tcx.dcx().emit_err(NakedFunctionsOperands { unsupported_operands });
} }
let unsupported_options: Vec<&'static str> = [ let supported_options =
(InlineAsmOptions::MAY_UNWIND, "`may_unwind`"), InlineAsmOptions::RAW | InlineAsmOptions::NORETURN | InlineAsmOptions::ATT_SYNTAX;
(InlineAsmOptions::NOMEM, "`nomem`"), let unsupported_options = asm.options.difference(supported_options);
(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();
if !unsupported_options.is_empty() { if !unsupported_options.is_empty() {
self.tcx.dcx().emit_err(NakedFunctionsAsmOptions { self.tcx.dcx().emit_err(NakedFunctionsAsmOptions {
span, span,
unsupported_options: unsupported_options.join(", "), unsupported_options: unsupported_options
.human_readable_names()
.into_iter()
.map(|name| format!("`{name}`"))
.collect::<Vec<_>>()
.join(", "),
}); });
} }

View File

@ -111,7 +111,7 @@ unsafe extern "C" fn invalid_options() {
unsafe extern "C" fn invalid_options_continued() { unsafe extern "C" fn invalid_options_continued() {
asm!("", options(readonly, nostack), options(pure)); asm!("", options(readonly, nostack), options(pure));
//~^ ERROR asm with the `pure` option must have at least one output //~^ 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 //~| ERROR asm in naked functions must use `noreturn` option
} }

View File

@ -212,7 +212,7 @@ error[E0787]: asm options unsupported in naked functions: `nomem`, `preserves_fl
LL | asm!("", options(nomem, preserves_flags, noreturn)); 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 --> $DIR/naked-functions.rs:112:5
| |
LL | asm!("", options(readonly, nostack), options(pure)); LL | asm!("", options(readonly, nostack), options(pure));