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 });
}
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(", "),
});
}

View File

@ -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
}

View File

@ -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));