]>
Commit | Line | Data |
---|---|---|
fc512014 XL |
1 | //! Checks validity of naked functions. |
2 | ||
94222f64 | 3 | use rustc_ast::{Attribute, InlineAsmOptions}; |
5099ac24 | 4 | use rustc_errors::struct_span_err; |
fc512014 XL |
5 | use rustc_hir as hir; |
6 | use rustc_hir::def_id::LocalDefId; | |
5099ac24 | 7 | use rustc_hir::intravisit::{FnKind, Visitor}; |
fc512014 XL |
8 | use rustc_hir::{ExprKind, HirId, InlineAsmOperand, StmtKind}; |
9 | use rustc_middle::ty::query::Providers; | |
10 | use rustc_middle::ty::TyCtxt; | |
94222f64 | 11 | use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI; |
fc512014 XL |
12 | use rustc_span::symbol::sym; |
13 | use rustc_span::Span; | |
14 | use rustc_target::spec::abi::Abi; | |
15 | ||
16 | fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) { | |
17 | tcx.hir().visit_item_likes_in_module( | |
18 | module_def_id, | |
19 | &mut CheckNakedFunctions { tcx }.as_deep_visitor(), | |
20 | ); | |
21 | } | |
22 | ||
23 | crate fn provide(providers: &mut Providers) { | |
24 | *providers = Providers { check_mod_naked_functions, ..*providers }; | |
25 | } | |
26 | ||
27 | struct CheckNakedFunctions<'tcx> { | |
28 | tcx: TyCtxt<'tcx>, | |
29 | } | |
30 | ||
31 | impl<'tcx> Visitor<'tcx> for CheckNakedFunctions<'tcx> { | |
fc512014 XL |
32 | fn visit_fn( |
33 | &mut self, | |
a2a8927a | 34 | fk: FnKind<'_>, |
fc512014 XL |
35 | _fd: &'tcx hir::FnDecl<'tcx>, |
36 | body_id: hir::BodyId, | |
37 | span: Span, | |
38 | hir_id: HirId, | |
39 | ) { | |
40 | let ident_span; | |
41 | let fn_header; | |
42 | ||
43 | match fk { | |
6a06907d | 44 | FnKind::Closure => { |
fc512014 XL |
45 | // Closures with a naked attribute are rejected during attribute |
46 | // check. Don't validate them any further. | |
47 | return; | |
48 | } | |
49 | FnKind::ItemFn(ident, _, ref header, ..) => { | |
50 | ident_span = ident.span; | |
51 | fn_header = header; | |
52 | } | |
53 | ||
54 | FnKind::Method(ident, ref sig, ..) => { | |
55 | ident_span = ident.span; | |
56 | fn_header = &sig.header; | |
57 | } | |
58 | } | |
59 | ||
6a06907d XL |
60 | let attrs = self.tcx.hir().attrs(hir_id); |
61 | let naked = attrs.iter().any(|attr| attr.has_name(sym::naked)); | |
fc512014 XL |
62 | if naked { |
63 | let body = self.tcx.hir().body(body_id); | |
64 | check_abi(self.tcx, hir_id, fn_header.abi, ident_span); | |
65 | check_no_patterns(self.tcx, body.params); | |
66 | check_no_parameters_use(self.tcx, body); | |
5099ac24 FG |
67 | check_asm(self.tcx, body, span); |
68 | check_inline(self.tcx, attrs); | |
fc512014 XL |
69 | } |
70 | } | |
71 | } | |
72 | ||
94222f64 | 73 | /// Check that the function isn't inlined. |
5099ac24 | 74 | fn check_inline(tcx: TyCtxt<'_>, attrs: &[Attribute]) { |
94222f64 | 75 | for attr in attrs.iter().filter(|attr| attr.has_name(sym::inline)) { |
5099ac24 | 76 | tcx.sess.struct_span_err(attr.span, "naked functions cannot be inlined").emit(); |
94222f64 XL |
77 | } |
78 | } | |
79 | ||
fc512014 XL |
80 | /// Checks that function uses non-Rust ABI. |
81 | fn check_abi(tcx: TyCtxt<'_>, hir_id: HirId, abi: Abi, fn_ident_span: Span) { | |
82 | if abi == Abi::Rust { | |
94222f64 | 83 | tcx.struct_span_lint_hir(UNDEFINED_NAKED_FUNCTION_ABI, hir_id, fn_ident_span, |lint| { |
fc512014 XL |
84 | lint.build("Rust ABI is unsupported in naked functions").emit(); |
85 | }); | |
86 | } | |
87 | } | |
88 | ||
89 | /// Checks that parameters don't use patterns. Mirrors the checks for function declarations. | |
90 | fn check_no_patterns(tcx: TyCtxt<'_>, params: &[hir::Param<'_>]) { | |
91 | for param in params { | |
92 | match param.pat.kind { | |
93 | hir::PatKind::Wild | |
94 | | hir::PatKind::Binding(hir::BindingAnnotation::Unannotated, _, _, None) => {} | |
95 | _ => { | |
96 | tcx.sess | |
97 | .struct_span_err( | |
98 | param.pat.span, | |
99 | "patterns not allowed in naked function parameters", | |
100 | ) | |
101 | .emit(); | |
102 | } | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | /// Checks that function parameters aren't used in the function body. | |
108 | fn check_no_parameters_use<'tcx>(tcx: TyCtxt<'tcx>, body: &'tcx hir::Body<'tcx>) { | |
109 | let mut params = hir::HirIdSet::default(); | |
110 | for param in body.params { | |
111 | param.pat.each_binding(|_binding_mode, hir_id, _span, _ident| { | |
112 | params.insert(hir_id); | |
113 | }); | |
114 | } | |
115 | CheckParameters { tcx, params }.visit_body(body); | |
116 | } | |
117 | ||
118 | struct CheckParameters<'tcx> { | |
119 | tcx: TyCtxt<'tcx>, | |
120 | params: hir::HirIdSet, | |
121 | } | |
122 | ||
123 | impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> { | |
fc512014 XL |
124 | fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { |
125 | if let hir::ExprKind::Path(hir::QPath::Resolved( | |
126 | _, | |
127 | hir::Path { res: hir::def::Res::Local(var_hir_id), .. }, | |
128 | )) = expr.kind | |
129 | { | |
130 | if self.params.contains(var_hir_id) { | |
131 | self.tcx | |
132 | .sess | |
133 | .struct_span_err( | |
134 | expr.span, | |
135 | "referencing function parameters is not allowed in naked functions", | |
136 | ) | |
137 | .help("follow the calling convention in asm block to use parameters") | |
138 | .emit(); | |
139 | return; | |
140 | } | |
141 | } | |
142 | hir::intravisit::walk_expr(self, expr); | |
143 | } | |
144 | } | |
145 | ||
146 | /// Checks that function body contains a single inline assembly block. | |
5099ac24 | 147 | fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, body: &'tcx hir::Body<'tcx>, fn_span: Span) { |
fc512014 XL |
148 | let mut this = CheckInlineAssembly { tcx, items: Vec::new() }; |
149 | this.visit_body(body); | |
5869c6ff | 150 | if let [(ItemKind::Asm, _)] = this.items[..] { |
fc512014 XL |
151 | // Ok. |
152 | } else { | |
5099ac24 FG |
153 | let mut diag = struct_span_err!( |
154 | tcx.sess, | |
155 | fn_span, | |
156 | E0787, | |
157 | "naked functions must contain a single asm block" | |
158 | ); | |
159 | let mut has_asm = false; | |
160 | for &(kind, span) in &this.items { | |
161 | match kind { | |
162 | ItemKind::Asm if has_asm => { | |
163 | diag.span_label(span, "multiple asm blocks are unsupported in naked functions"); | |
164 | } | |
165 | ItemKind::Asm => has_asm = true, | |
166 | ItemKind::NonAsm => { | |
167 | diag.span_label(span, "non-asm is unsupported in naked functions"); | |
fc512014 XL |
168 | } |
169 | } | |
5099ac24 FG |
170 | } |
171 | diag.emit(); | |
fc512014 XL |
172 | } |
173 | } | |
174 | ||
175 | struct CheckInlineAssembly<'tcx> { | |
176 | tcx: TyCtxt<'tcx>, | |
177 | items: Vec<(ItemKind, Span)>, | |
178 | } | |
179 | ||
180 | #[derive(Copy, Clone)] | |
181 | enum ItemKind { | |
182 | Asm, | |
183 | NonAsm, | |
184 | } | |
185 | ||
186 | impl<'tcx> CheckInlineAssembly<'tcx> { | |
187 | fn check_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, span: Span) { | |
188 | match expr.kind { | |
189 | ExprKind::Box(..) | |
190 | | ExprKind::ConstBlock(..) | |
191 | | ExprKind::Array(..) | |
192 | | ExprKind::Call(..) | |
193 | | ExprKind::MethodCall(..) | |
194 | | ExprKind::Tup(..) | |
195 | | ExprKind::Binary(..) | |
196 | | ExprKind::Unary(..) | |
197 | | ExprKind::Lit(..) | |
198 | | ExprKind::Cast(..) | |
199 | | ExprKind::Type(..) | |
200 | | ExprKind::Loop(..) | |
201 | | ExprKind::Match(..) | |
5869c6ff | 202 | | ExprKind::If(..) |
fc512014 XL |
203 | | ExprKind::Closure(..) |
204 | | ExprKind::Assign(..) | |
205 | | ExprKind::AssignOp(..) | |
206 | | ExprKind::Field(..) | |
207 | | ExprKind::Index(..) | |
208 | | ExprKind::Path(..) | |
209 | | ExprKind::AddrOf(..) | |
94222f64 | 210 | | ExprKind::Let(..) |
fc512014 XL |
211 | | ExprKind::Break(..) |
212 | | ExprKind::Continue(..) | |
213 | | ExprKind::Ret(..) | |
214 | | ExprKind::Struct(..) | |
215 | | ExprKind::Repeat(..) | |
216 | | ExprKind::Yield(..) => { | |
217 | self.items.push((ItemKind::NonAsm, span)); | |
218 | } | |
219 | ||
220 | ExprKind::InlineAsm(ref asm) => { | |
221 | self.items.push((ItemKind::Asm, span)); | |
5099ac24 | 222 | self.check_inline_asm(asm, span); |
fc512014 XL |
223 | } |
224 | ||
225 | ExprKind::DropTemps(..) | ExprKind::Block(..) | ExprKind::Err => { | |
226 | hir::intravisit::walk_expr(self, expr); | |
227 | } | |
228 | } | |
229 | } | |
230 | ||
5099ac24 | 231 | fn check_inline_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) { |
fc512014 XL |
232 | let unsupported_operands: Vec<Span> = asm |
233 | .operands | |
234 | .iter() | |
235 | .filter_map(|&(ref op, op_sp)| match op { | |
236 | InlineAsmOperand::Const { .. } | InlineAsmOperand::Sym { .. } => None, | |
237 | InlineAsmOperand::In { .. } | |
238 | | InlineAsmOperand::Out { .. } | |
239 | | InlineAsmOperand::InOut { .. } | |
240 | | InlineAsmOperand::SplitInOut { .. } => Some(op_sp), | |
241 | }) | |
242 | .collect(); | |
243 | if !unsupported_operands.is_empty() { | |
5099ac24 FG |
244 | struct_span_err!( |
245 | self.tcx.sess, | |
fc512014 | 246 | unsupported_operands, |
5099ac24 FG |
247 | E0787, |
248 | "only `const` and `sym` operands are supported in naked functions", | |
249 | ) | |
250 | .emit(); | |
fc512014 XL |
251 | } |
252 | ||
253 | let unsupported_options: Vec<&'static str> = [ | |
5099ac24 | 254 | (InlineAsmOptions::MAY_UNWIND, "`may_unwind`"), |
fc512014 XL |
255 | (InlineAsmOptions::NOMEM, "`nomem`"), |
256 | (InlineAsmOptions::NOSTACK, "`nostack`"), | |
257 | (InlineAsmOptions::PRESERVES_FLAGS, "`preserves_flags`"), | |
258 | (InlineAsmOptions::PURE, "`pure`"), | |
259 | (InlineAsmOptions::READONLY, "`readonly`"), | |
260 | ] | |
261 | .iter() | |
262 | .filter_map(|&(option, name)| if asm.options.contains(option) { Some(name) } else { None }) | |
263 | .collect(); | |
264 | ||
265 | if !unsupported_options.is_empty() { | |
5099ac24 FG |
266 | struct_span_err!( |
267 | self.tcx.sess, | |
268 | span, | |
269 | E0787, | |
270 | "asm options unsupported in naked functions: {}", | |
271 | unsupported_options.join(", ") | |
272 | ) | |
273 | .emit(); | |
fc512014 XL |
274 | } |
275 | ||
276 | if !asm.options.contains(InlineAsmOptions::NORETURN) { | |
5099ac24 FG |
277 | struct_span_err!( |
278 | self.tcx.sess, | |
279 | span, | |
280 | E0787, | |
281 | "asm in naked functions must use `noreturn` option" | |
282 | ) | |
283 | .emit(); | |
fc512014 XL |
284 | } |
285 | } | |
286 | } | |
287 | ||
288 | impl<'tcx> Visitor<'tcx> for CheckInlineAssembly<'tcx> { | |
fc512014 XL |
289 | fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) { |
290 | match stmt.kind { | |
291 | StmtKind::Item(..) => {} | |
292 | StmtKind::Local(..) => { | |
293 | self.items.push((ItemKind::NonAsm, stmt.span)); | |
294 | } | |
295 | StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => { | |
296 | self.check_expr(expr, stmt.span); | |
297 | } | |
298 | } | |
299 | } | |
300 | ||
301 | fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) { | |
302 | self.check_expr(&expr, expr.span); | |
303 | } | |
304 | } |