2020-12-07 00:00:00 +00:00
|
|
|
//! Checks validity of naked functions.
|
|
|
|
|
2020-11-25 00:00:00 +00:00
|
|
|
use rustc_hir as hir;
|
2022-06-25 14:18:44 +00:00
|
|
|
use rustc_hir::def::DefKind;
|
2023-04-26 18:53:51 +00:00
|
|
|
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
|
2022-06-25 14:18:44 +00:00
|
|
|
use rustc_hir::intravisit::Visitor;
|
2024-09-05 16:56:38 +00:00
|
|
|
use rustc_hir::{ExprKind, HirIdSet, StmtKind};
|
2024-09-10 12:42:17 +00:00
|
|
|
use rustc_middle::hir::nested_filter::OnlyBodies;
|
2023-05-15 04:24:45 +00:00
|
|
|
use rustc_middle::query::Providers;
|
2024-09-05 17:45:40 +00:00
|
|
|
use rustc_middle::span_bug;
|
2020-11-25 00:00:00 +00:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-08-04 19:00:49 +00:00
|
|
|
use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI;
|
2024-12-12 23:29:23 +00:00
|
|
|
use rustc_span::{Span, sym};
|
2020-12-07 00:00:00 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2020-11-25 00:00:00 +00:00
|
|
|
|
2022-09-27 15:44:56 +00:00
|
|
|
use crate::errors::{
|
2024-09-05 16:56:38 +00:00
|
|
|
NakedAsmOutsideNakedFn, NakedFunctionsAsmBlock, NakedFunctionsMustNakedAsm, NoPatterns,
|
|
|
|
ParamsNotAllowed, UndefinedNakedFunctionAbi,
|
2022-09-27 15:44:56 +00:00
|
|
|
};
|
|
|
|
|
2022-05-20 23:51:09 +00:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2020-11-25 00:00:00 +00:00
|
|
|
*providers = Providers { check_mod_naked_functions, ..*providers };
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:53:51 +00:00
|
|
|
fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
|
2022-06-25 14:18:44 +00:00
|
|
|
let items = tcx.hir_module_items(module_def_id);
|
|
|
|
for def_id in items.definitions() {
|
|
|
|
if !matches!(tcx.def_kind(def_id), DefKind::Fn | DefKind::AssocFn) {
|
|
|
|
continue;
|
2020-11-25 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 13:28:34 +00:00
|
|
|
let (fn_header, body_id) = match tcx.hir_node_by_def_id(def_id) {
|
2022-06-25 14:18:44 +00:00
|
|
|
hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, _, body_id), .. })
|
|
|
|
| hir::Node::TraitItem(hir::TraitItem {
|
|
|
|
kind: hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)),
|
|
|
|
..
|
|
|
|
})
|
|
|
|
| hir::Node::ImplItem(hir::ImplItem {
|
|
|
|
kind: hir::ImplItemKind::Fn(sig, body_id),
|
|
|
|
..
|
|
|
|
}) => (sig.header, *body_id),
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let body = tcx.hir().body(body_id);
|
2024-09-10 12:42:17 +00:00
|
|
|
|
|
|
|
if tcx.has_attr(def_id, sym::naked) {
|
|
|
|
check_abi(tcx, def_id, fn_header.abi);
|
|
|
|
check_no_patterns(tcx, body.params);
|
|
|
|
check_no_parameters_use(tcx, body);
|
|
|
|
check_asm(tcx, def_id, body);
|
|
|
|
} else {
|
|
|
|
// `naked_asm!` is not allowed outside of functions marked as `#[naked]`
|
|
|
|
let mut visitor = CheckNakedAsmInNakedFn { tcx };
|
|
|
|
visitor.visit_body(body);
|
|
|
|
}
|
2021-08-02 19:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 00:00:00 +00:00
|
|
|
/// Checks that function uses non-Rust ABI.
|
2022-06-25 14:18:44 +00:00
|
|
|
fn check_abi(tcx: TyCtxt<'_>, def_id: LocalDefId, abi: Abi) {
|
2020-12-07 00:00:00 +00:00
|
|
|
if abi == Abi::Rust {
|
2023-11-24 16:28:19 +00:00
|
|
|
let hir_id = tcx.local_def_id_to_hir_id(def_id);
|
2022-06-25 14:18:44 +00:00
|
|
|
let span = tcx.def_span(def_id);
|
2024-01-16 05:27:02 +00:00
|
|
|
tcx.emit_node_span_lint(
|
2022-09-16 07:01:02 +00:00
|
|
|
UNDEFINED_NAKED_FUNCTION_ABI,
|
|
|
|
hir_id,
|
|
|
|
span,
|
2022-09-27 15:44:56 +00:00
|
|
|
UndefinedNakedFunctionAbi,
|
2022-09-16 07:01:02 +00:00
|
|
|
);
|
2020-12-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 00:00:00 +00:00
|
|
|
/// Checks that parameters don't use patterns. Mirrors the checks for function declarations.
|
2020-12-07 00:00:00 +00:00
|
|
|
fn check_no_patterns(tcx: TyCtxt<'_>, params: &[hir::Param<'_>]) {
|
|
|
|
for param in params {
|
2020-11-25 00:00:00 +00:00
|
|
|
match param.pat.kind {
|
2024-04-16 23:23:30 +00:00
|
|
|
hir::PatKind::Wild | hir::PatKind::Binding(hir::BindingMode::NONE, _, _, None) => {}
|
2020-11-25 00:00:00 +00:00
|
|
|
_ => {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(NoPatterns { span: param.pat.span });
|
2020-11-25 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-07 00:00:00 +00:00
|
|
|
/// Checks that function parameters aren't used in the function body.
|
|
|
|
fn check_no_parameters_use<'tcx>(tcx: TyCtxt<'tcx>, body: &'tcx hir::Body<'tcx>) {
|
2024-03-06 06:24:13 +00:00
|
|
|
let mut params = HirIdSet::default();
|
2020-11-25 00:00:00 +00:00
|
|
|
for param in body.params {
|
|
|
|
param.pat.each_binding(|_binding_mode, hir_id, _span, _ident| {
|
|
|
|
params.insert(hir_id);
|
|
|
|
});
|
|
|
|
}
|
2020-12-07 00:00:00 +00:00
|
|
|
CheckParameters { tcx, params }.visit_body(body);
|
2020-11-25 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 00:00:00 +00:00
|
|
|
struct CheckParameters<'tcx> {
|
2020-11-25 00:00:00 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2024-03-06 06:24:13 +00:00
|
|
|
params: HirIdSet,
|
2020-11-25 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-07 00:00:00 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> {
|
2020-11-25 00:00:00 +00:00
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
|
|
|
if let hir::ExprKind::Path(hir::QPath::Resolved(
|
|
|
|
_,
|
|
|
|
hir::Path { res: hir::def::Res::Local(var_hir_id), .. },
|
|
|
|
)) = expr.kind
|
|
|
|
{
|
|
|
|
if self.params.contains(var_hir_id) {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.tcx.dcx().emit_err(ParamsNotAllowed { span: expr.span });
|
2020-12-07 00:00:00 +00:00
|
|
|
return;
|
2020-11-25 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::intravisit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
2020-12-07 00:00:00 +00:00
|
|
|
|
|
|
|
/// Checks that function body contains a single inline assembly block.
|
2022-06-25 14:18:44 +00:00
|
|
|
fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir::Body<'tcx>) {
|
2024-09-05 16:56:38 +00:00
|
|
|
let mut this = CheckInlineAssembly { items: Vec::new() };
|
2020-12-07 00:00:00 +00:00
|
|
|
this.visit_body(body);
|
2024-09-05 11:45:26 +00:00
|
|
|
if let [(ItemKind::NakedAsm | ItemKind::Err, _)] = this.items[..] {
|
2020-12-07 00:00:00 +00:00
|
|
|
// Ok.
|
|
|
|
} else {
|
2022-04-01 05:29:38 +00:00
|
|
|
let mut must_show_error = false;
|
2024-09-05 11:45:26 +00:00
|
|
|
let mut has_naked_asm = false;
|
2022-04-01 05:29:38 +00:00
|
|
|
let mut has_err = false;
|
2022-09-27 15:44:56 +00:00
|
|
|
let mut multiple_asms = vec![];
|
|
|
|
let mut non_asms = vec![];
|
2022-01-21 00:00:00 +00:00
|
|
|
for &(kind, span) in &this.items {
|
|
|
|
match kind {
|
2024-09-05 11:45:26 +00:00
|
|
|
ItemKind::NakedAsm if has_naked_asm => {
|
2022-04-01 05:29:38 +00:00
|
|
|
must_show_error = true;
|
2022-09-27 15:44:56 +00:00
|
|
|
multiple_asms.push(span);
|
2022-01-21 00:00:00 +00:00
|
|
|
}
|
2024-09-05 11:45:26 +00:00
|
|
|
ItemKind::NakedAsm => has_naked_asm = true,
|
|
|
|
ItemKind::InlineAsm => {
|
|
|
|
has_err = true;
|
|
|
|
|
2024-09-05 17:45:40 +00:00
|
|
|
tcx.dcx().emit_err(NakedFunctionsMustNakedAsm { span });
|
2024-09-05 11:45:26 +00:00
|
|
|
}
|
2022-01-21 00:00:00 +00:00
|
|
|
ItemKind::NonAsm => {
|
2022-04-01 05:29:38 +00:00
|
|
|
must_show_error = true;
|
2022-09-27 15:44:56 +00:00
|
|
|
non_asms.push(span);
|
2020-12-07 00:00:00 +00:00
|
|
|
}
|
2022-04-01 05:29:38 +00:00
|
|
|
ItemKind::Err => has_err = true,
|
2020-12-07 00:00:00 +00:00
|
|
|
}
|
2022-01-21 00:00:00 +00:00
|
|
|
}
|
2022-04-01 05:29:38 +00:00
|
|
|
|
|
|
|
// If the naked function only contains a single asm block and a non-zero number of
|
|
|
|
// errors, then don't show an additional error. This allows for appending/prepending
|
|
|
|
// `compile_error!("...")` statements and reduces error noise.
|
|
|
|
if must_show_error || !has_err {
|
2023-12-18 11:21:37 +00:00
|
|
|
tcx.dcx().emit_err(NakedFunctionsAsmBlock {
|
2022-09-27 15:44:56 +00:00
|
|
|
span: tcx.def_span(def_id),
|
|
|
|
multiple_asms,
|
|
|
|
non_asms,
|
|
|
|
});
|
2022-04-01 05:29:38 +00:00
|
|
|
}
|
2020-12-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 16:56:38 +00:00
|
|
|
struct CheckInlineAssembly {
|
2020-12-07 00:00:00 +00:00
|
|
|
items: Vec<(ItemKind, Span)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum ItemKind {
|
2024-09-05 11:45:26 +00:00
|
|
|
NakedAsm,
|
|
|
|
InlineAsm,
|
2020-12-07 00:00:00 +00:00
|
|
|
NonAsm,
|
2022-04-01 05:29:38 +00:00
|
|
|
Err,
|
2020-12-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2024-09-05 16:56:38 +00:00
|
|
|
impl CheckInlineAssembly {
|
|
|
|
fn check_expr<'tcx>(&mut self, expr: &'tcx hir::Expr<'tcx>, span: Span) {
|
2020-12-07 00:00:00 +00:00
|
|
|
match expr.kind {
|
2023-03-14 17:18:26 +00:00
|
|
|
ExprKind::ConstBlock(..)
|
2020-12-07 00:00:00 +00:00
|
|
|
| ExprKind::Array(..)
|
|
|
|
| ExprKind::Call(..)
|
|
|
|
| ExprKind::MethodCall(..)
|
|
|
|
| ExprKind::Tup(..)
|
|
|
|
| ExprKind::Binary(..)
|
|
|
|
| ExprKind::Unary(..)
|
|
|
|
| ExprKind::Lit(..)
|
|
|
|
| ExprKind::Cast(..)
|
|
|
|
| ExprKind::Type(..)
|
2024-09-13 18:00:10 +00:00
|
|
|
| ExprKind::UnsafeBinderCast(..)
|
2020-12-07 00:00:00 +00:00
|
|
|
| ExprKind::Loop(..)
|
|
|
|
| ExprKind::Match(..)
|
2021-01-01 18:38:11 +00:00
|
|
|
| ExprKind::If(..)
|
2022-06-11 19:25:25 +00:00
|
|
|
| ExprKind::Closure { .. }
|
2020-12-07 00:00:00 +00:00
|
|
|
| ExprKind::Assign(..)
|
|
|
|
| ExprKind::AssignOp(..)
|
|
|
|
| ExprKind::Field(..)
|
|
|
|
| ExprKind::Index(..)
|
|
|
|
| ExprKind::Path(..)
|
|
|
|
| ExprKind::AddrOf(..)
|
2021-08-08 14:49:13 +00:00
|
|
|
| ExprKind::Let(..)
|
2020-12-07 00:00:00 +00:00
|
|
|
| ExprKind::Break(..)
|
|
|
|
| ExprKind::Continue(..)
|
|
|
|
| ExprKind::Ret(..)
|
2022-09-11 07:37:49 +00:00
|
|
|
| ExprKind::OffsetOf(..)
|
2022-11-21 12:40:27 +00:00
|
|
|
| ExprKind::Become(..)
|
2020-12-07 00:00:00 +00:00
|
|
|
| ExprKind::Struct(..)
|
|
|
|
| ExprKind::Repeat(..)
|
|
|
|
| ExprKind::Yield(..) => {
|
|
|
|
self.items.push((ItemKind::NonAsm, span));
|
|
|
|
}
|
|
|
|
|
2024-09-05 17:45:40 +00:00
|
|
|
ExprKind::InlineAsm(asm) => match asm.asm_macro {
|
|
|
|
rustc_ast::AsmMacro::Asm => {
|
|
|
|
self.items.push((ItemKind::InlineAsm, span));
|
2024-09-05 11:45:26 +00:00
|
|
|
}
|
2024-09-05 17:45:40 +00:00
|
|
|
rustc_ast::AsmMacro::NakedAsm => {
|
|
|
|
self.items.push((ItemKind::NakedAsm, span));
|
|
|
|
}
|
|
|
|
rustc_ast::AsmMacro::GlobalAsm => {
|
|
|
|
span_bug!(span, "`global_asm!` is not allowed in this position")
|
|
|
|
}
|
|
|
|
},
|
2020-12-07 00:00:00 +00:00
|
|
|
|
2022-04-01 05:29:38 +00:00
|
|
|
ExprKind::DropTemps(..) | ExprKind::Block(..) => {
|
2020-12-07 00:00:00 +00:00
|
|
|
hir::intravisit::walk_expr(self, expr);
|
|
|
|
}
|
2022-04-01 05:29:38 +00:00
|
|
|
|
2023-02-22 22:40:06 +00:00
|
|
|
ExprKind::Err(_) => {
|
2022-04-01 05:29:38 +00:00
|
|
|
self.items.push((ItemKind::Err, span));
|
|
|
|
}
|
2020-12-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 16:56:38 +00:00
|
|
|
impl<'tcx> Visitor<'tcx> for CheckInlineAssembly {
|
2020-12-07 00:00:00 +00:00
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
|
|
|
|
match stmt.kind {
|
|
|
|
StmtKind::Item(..) => {}
|
2024-03-14 10:53:38 +00:00
|
|
|
StmtKind::Let(..) => {
|
2020-12-07 00:00:00 +00:00
|
|
|
self.items.push((ItemKind::NonAsm, stmt.span));
|
|
|
|
}
|
2023-11-21 19:07:32 +00:00
|
|
|
StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
|
2020-12-07 00:00:00 +00:00
|
|
|
self.check_expr(expr, stmt.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
2023-11-21 19:07:32 +00:00
|
|
|
self.check_expr(expr, expr.span);
|
2020-12-07 00:00:00 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-10 12:42:17 +00:00
|
|
|
|
|
|
|
struct CheckNakedAsmInNakedFn<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for CheckNakedAsmInNakedFn<'tcx> {
|
|
|
|
type NestedFilter = OnlyBodies;
|
|
|
|
|
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.tcx.hir()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
|
|
|
if let ExprKind::InlineAsm(inline_asm) = expr.kind {
|
|
|
|
if let rustc_ast::AsmMacro::NakedAsm = inline_asm.asm_macro {
|
|
|
|
self.tcx.dcx().emit_err(NakedAsmOutsideNakedFn { span: expr.span });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hir::intravisit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|