Skip to content

Commit 52f5065

Browse files
authored
Unrolled build for rust-lang#128138
Rollup merge of rust-lang#128138 - folkertdev:asm-option-allowlist, r=lcnr `#[naked]`: use an allowlist for allowed options on `asm!` in naked functions tracking issue: rust-lang#90957 this is mostly just a refactor, but using an allowlist (rather than a denylist) for which asm options are allowed in naked functions is a little safer. These options are disallowed because naked functions are effectively global asm, but defined using inline asm.
2 parents 54be9ad + 4b7a87d commit 52f5065

File tree

6 files changed

+49
-72
lines changed

6 files changed

+49
-72
lines changed

compiler/rustc_ast/src/ast.rs

+36
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,42 @@ bitflags::bitflags! {
22642264
}
22652265
}
22662266

2267+
impl InlineAsmOptions {
2268+
pub fn human_readable_names(&self) -> Vec<&'static str> {
2269+
let mut options = vec![];
2270+
2271+
if self.contains(InlineAsmOptions::PURE) {
2272+
options.push("pure");
2273+
}
2274+
if self.contains(InlineAsmOptions::NOMEM) {
2275+
options.push("nomem");
2276+
}
2277+
if self.contains(InlineAsmOptions::READONLY) {
2278+
options.push("readonly");
2279+
}
2280+
if self.contains(InlineAsmOptions::PRESERVES_FLAGS) {
2281+
options.push("preserves_flags");
2282+
}
2283+
if self.contains(InlineAsmOptions::NORETURN) {
2284+
options.push("noreturn");
2285+
}
2286+
if self.contains(InlineAsmOptions::NOSTACK) {
2287+
options.push("nostack");
2288+
}
2289+
if self.contains(InlineAsmOptions::ATT_SYNTAX) {
2290+
options.push("att_syntax");
2291+
}
2292+
if self.contains(InlineAsmOptions::RAW) {
2293+
options.push("raw");
2294+
}
2295+
if self.contains(InlineAsmOptions::MAY_UNWIND) {
2296+
options.push("may_unwind");
2297+
}
2298+
2299+
options
2300+
}
2301+
}
2302+
22672303
impl std::fmt::Debug for InlineAsmOptions {
22682304
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22692305
bitflags::parser::to_writer(self, f)

compiler/rustc_ast_pretty/src/pprust/state.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1505,35 +1505,7 @@ impl<'a> State<'a> {
15051505
AsmArg::Options(opts) => {
15061506
s.word("options");
15071507
s.popen();
1508-
let mut options = vec![];
1509-
if opts.contains(InlineAsmOptions::PURE) {
1510-
options.push("pure");
1511-
}
1512-
if opts.contains(InlineAsmOptions::NOMEM) {
1513-
options.push("nomem");
1514-
}
1515-
if opts.contains(InlineAsmOptions::READONLY) {
1516-
options.push("readonly");
1517-
}
1518-
if opts.contains(InlineAsmOptions::PRESERVES_FLAGS) {
1519-
options.push("preserves_flags");
1520-
}
1521-
if opts.contains(InlineAsmOptions::NORETURN) {
1522-
options.push("noreturn");
1523-
}
1524-
if opts.contains(InlineAsmOptions::NOSTACK) {
1525-
options.push("nostack");
1526-
}
1527-
if opts.contains(InlineAsmOptions::ATT_SYNTAX) {
1528-
options.push("att_syntax");
1529-
}
1530-
if opts.contains(InlineAsmOptions::RAW) {
1531-
options.push("raw");
1532-
}
1533-
if opts.contains(InlineAsmOptions::MAY_UNWIND) {
1534-
options.push("may_unwind");
1535-
}
1536-
s.commasep(Inconsistent, &options, |s, &opt| {
1508+
s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
15371509
s.word(opt);
15381510
});
15391511
s.pclose();

compiler/rustc_hir_pretty/src/lib.rs

+1-29
Original file line numberDiff line numberDiff line change
@@ -1298,35 +1298,7 @@ impl<'a> State<'a> {
12981298
AsmArg::Options(opts) => {
12991299
s.word("options");
13001300
s.popen();
1301-
let mut options = vec![];
1302-
if opts.contains(ast::InlineAsmOptions::PURE) {
1303-
options.push("pure");
1304-
}
1305-
if opts.contains(ast::InlineAsmOptions::NOMEM) {
1306-
options.push("nomem");
1307-
}
1308-
if opts.contains(ast::InlineAsmOptions::READONLY) {
1309-
options.push("readonly");
1310-
}
1311-
if opts.contains(ast::InlineAsmOptions::PRESERVES_FLAGS) {
1312-
options.push("preserves_flags");
1313-
}
1314-
if opts.contains(ast::InlineAsmOptions::NORETURN) {
1315-
options.push("noreturn");
1316-
}
1317-
if opts.contains(ast::InlineAsmOptions::NOSTACK) {
1318-
options.push("nostack");
1319-
}
1320-
if opts.contains(ast::InlineAsmOptions::ATT_SYNTAX) {
1321-
options.push("att_syntax");
1322-
}
1323-
if opts.contains(ast::InlineAsmOptions::RAW) {
1324-
options.push("raw");
1325-
}
1326-
if opts.contains(ast::InlineAsmOptions::MAY_UNWIND) {
1327-
options.push("may_unwind");
1328-
}
1329-
s.commasep(Inconsistent, &options, |s, &opt| {
1301+
s.commasep(Inconsistent, &opts.human_readable_names(), |s, &opt| {
13301302
s.word(opt);
13311303
});
13321304
s.pclose();

compiler/rustc_passes/src/naked_functions.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,19 @@ impl<'tcx> CheckInlineAssembly<'tcx> {
244244
self.tcx.dcx().emit_err(NakedFunctionsOperands { unsupported_operands });
245245
}
246246

247-
let unsupported_options: Vec<&'static str> = [
248-
(InlineAsmOptions::MAY_UNWIND, "`may_unwind`"),
249-
(InlineAsmOptions::NOMEM, "`nomem`"),
250-
(InlineAsmOptions::NOSTACK, "`nostack`"),
251-
(InlineAsmOptions::PRESERVES_FLAGS, "`preserves_flags`"),
252-
(InlineAsmOptions::PURE, "`pure`"),
253-
(InlineAsmOptions::READONLY, "`readonly`"),
254-
]
255-
.iter()
256-
.filter_map(|&(option, name)| if asm.options.contains(option) { Some(name) } else { None })
257-
.collect();
247+
let supported_options =
248+
InlineAsmOptions::RAW | InlineAsmOptions::NORETURN | InlineAsmOptions::ATT_SYNTAX;
249+
let unsupported_options = asm.options.difference(supported_options);
258250

259251
if !unsupported_options.is_empty() {
260252
self.tcx.dcx().emit_err(NakedFunctionsAsmOptions {
261253
span,
262-
unsupported_options: unsupported_options.join(", "),
254+
unsupported_options: unsupported_options
255+
.human_readable_names()
256+
.into_iter()
257+
.map(|name| format!("`{name}`"))
258+
.collect::<Vec<_>>()
259+
.join(", "),
263260
});
264261
}
265262

tests/ui/asm/naked-functions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ unsafe extern "C" fn invalid_options() {
111111
unsafe extern "C" fn invalid_options_continued() {
112112
asm!("", options(readonly, nostack), options(pure));
113113
//~^ ERROR asm with the `pure` option must have at least one output
114-
//~| ERROR asm options unsupported in naked functions: `nostack`, `pure`, `readonly`
114+
//~| ERROR asm options unsupported in naked functions: `pure`, `readonly`, `nostack`
115115
//~| ERROR asm in naked functions must use `noreturn` option
116116
}
117117

tests/ui/asm/naked-functions.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ error[E0787]: asm options unsupported in naked functions: `nomem`, `preserves_fl
212212
LL | asm!("", options(nomem, preserves_flags, noreturn));
213213
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
214214

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

0 commit comments

Comments
 (0)