2020-12-06 18:00:00 -06:00
|
|
|
//! Checks validity of naked functions.
|
|
|
|
|
2022-06-25 09:18:44 -05:00
|
|
|
use rustc_ast::InlineAsmOptions;
|
2022-03-31 17:14:01 -05:00
|
|
|
use rustc_errors::{struct_span_err, Applicability};
|
2020-11-24 18:00:00 -06:00
|
|
|
use rustc_hir as hir;
|
2022-06-25 09:18:44 -05:00
|
|
|
use rustc_hir::def::DefKind;
|
2020-11-24 18:00:00 -06:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2022-06-25 09:18:44 -05:00
|
|
|
use rustc_hir::intravisit::Visitor;
|
|
|
|
use rustc_hir::{ExprKind, InlineAsmOperand, StmtKind};
|
2020-11-24 18:00:00 -06:00
|
|
|
use rustc_middle::ty::query::Providers;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2021-08-04 14:00:49 -05:00
|
|
|
use rustc_session::lint::builtin::UNDEFINED_NAKED_FUNCTION_ABI;
|
2020-11-24 18:00:00 -06:00
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
use rustc_span::Span;
|
2020-12-06 18:00:00 -06:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2020-11-24 18:00:00 -06:00
|
|
|
|
2022-05-20 18:51:09 -05:00
|
|
|
pub(crate) fn provide(providers: &mut Providers) {
|
2020-11-24 18:00:00 -06:00
|
|
|
*providers = Providers { check_mod_naked_functions, ..*providers };
|
|
|
|
}
|
|
|
|
|
2022-06-25 09:18:44 -05:00
|
|
|
fn check_mod_naked_functions(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
|
|
|
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-24 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
2022-06-25 09:18:44 -05:00
|
|
|
let naked = tcx.has_attr(def_id.to_def_id(), sym::naked);
|
|
|
|
if !naked {
|
|
|
|
continue;
|
2020-11-24 18:00:00 -06:00
|
|
|
}
|
2022-06-25 09:18:44 -05:00
|
|
|
|
|
|
|
let (fn_header, body_id) = match tcx.hir().get_by_def_id(def_id) {
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
check_inline(tcx, def_id);
|
2020-11-24 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-02 14:03:43 -05:00
|
|
|
/// Check that the function isn't inlined.
|
2022-06-25 09:18:44 -05:00
|
|
|
fn check_inline(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
|
|
|
let attrs = tcx.get_attrs(def_id.to_def_id(), sym::inline);
|
|
|
|
for attr in attrs {
|
2022-01-20 18:00:00 -06:00
|
|
|
tcx.sess.struct_span_err(attr.span, "naked functions cannot be inlined").emit();
|
2021-08-02 14:03:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 18:00:00 -06:00
|
|
|
/// Checks that function uses non-Rust ABI.
|
2022-06-25 09:18:44 -05:00
|
|
|
fn check_abi(tcx: TyCtxt<'_>, def_id: LocalDefId, abi: Abi) {
|
2020-12-06 18:00:00 -06:00
|
|
|
if abi == Abi::Rust {
|
2022-06-25 09:18:44 -05:00
|
|
|
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
|
|
|
|
let span = tcx.def_span(def_id);
|
2022-09-16 02:01:02 -05:00
|
|
|
tcx.struct_span_lint_hir(
|
|
|
|
UNDEFINED_NAKED_FUNCTION_ABI,
|
|
|
|
hir_id,
|
|
|
|
span,
|
|
|
|
"Rust ABI is unsupported in naked functions",
|
|
|
|
|lint| lint,
|
|
|
|
);
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:00:00 -06:00
|
|
|
/// Checks that parameters don't use patterns. Mirrors the checks for function declarations.
|
2020-12-06 18:00:00 -06:00
|
|
|
fn check_no_patterns(tcx: TyCtxt<'_>, params: &[hir::Param<'_>]) {
|
|
|
|
for param in params {
|
2020-11-24 18:00:00 -06:00
|
|
|
match param.pat.kind {
|
|
|
|
hir::PatKind::Wild
|
2022-08-30 17:34:35 -05:00
|
|
|
| hir::PatKind::Binding(hir::BindingAnnotation::NONE, _, _, None) => {}
|
2020-11-24 18:00:00 -06:00
|
|
|
_ => {
|
|
|
|
tcx.sess
|
|
|
|
.struct_span_err(
|
|
|
|
param.pat.span,
|
|
|
|
"patterns not allowed in naked function parameters",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-06 18:00:00 -06: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>) {
|
2020-11-24 18:00:00 -06:00
|
|
|
let mut params = hir::HirIdSet::default();
|
|
|
|
for param in body.params {
|
|
|
|
param.pat.each_binding(|_binding_mode, hir_id, _span, _ident| {
|
|
|
|
params.insert(hir_id);
|
|
|
|
});
|
|
|
|
}
|
2020-12-06 18:00:00 -06:00
|
|
|
CheckParameters { tcx, params }.visit_body(body);
|
2020-11-24 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
2020-12-06 18:00:00 -06:00
|
|
|
struct CheckParameters<'tcx> {
|
2020-11-24 18:00:00 -06:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
params: hir::HirIdSet,
|
|
|
|
}
|
|
|
|
|
2020-12-06 18:00:00 -06:00
|
|
|
impl<'tcx> Visitor<'tcx> for CheckParameters<'tcx> {
|
2020-11-24 18:00:00 -06: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) {
|
|
|
|
self.tcx
|
|
|
|
.sess
|
|
|
|
.struct_span_err(
|
|
|
|
expr.span,
|
2020-12-06 18:00:00 -06:00
|
|
|
"referencing function parameters is not allowed in naked functions",
|
2020-11-24 18:00:00 -06:00
|
|
|
)
|
2020-12-06 18:00:00 -06:00
|
|
|
.help("follow the calling convention in asm block to use parameters")
|
2020-11-24 18:00:00 -06:00
|
|
|
.emit();
|
2020-12-06 18:00:00 -06:00
|
|
|
return;
|
2020-11-24 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::intravisit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
2020-12-06 18:00:00 -06:00
|
|
|
|
|
|
|
/// Checks that function body contains a single inline assembly block.
|
2022-06-25 09:18:44 -05:00
|
|
|
fn check_asm<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'tcx hir::Body<'tcx>) {
|
2020-12-06 18:00:00 -06:00
|
|
|
let mut this = CheckInlineAssembly { tcx, items: Vec::new() };
|
|
|
|
this.visit_body(body);
|
2022-04-01 00:29:38 -05:00
|
|
|
if let [(ItemKind::Asm | ItemKind::Err, _)] = this.items[..] {
|
2020-12-06 18:00:00 -06:00
|
|
|
// Ok.
|
|
|
|
} else {
|
2022-01-20 18:00:00 -06:00
|
|
|
let mut diag = struct_span_err!(
|
|
|
|
tcx.sess,
|
2022-06-25 09:18:44 -05:00
|
|
|
tcx.def_span(def_id),
|
2022-01-20 18:00:00 -06:00
|
|
|
E0787,
|
|
|
|
"naked functions must contain a single asm block"
|
|
|
|
);
|
2022-04-01 00:29:38 -05:00
|
|
|
|
|
|
|
let mut must_show_error = false;
|
2022-01-20 18:00:00 -06:00
|
|
|
let mut has_asm = false;
|
2022-04-01 00:29:38 -05:00
|
|
|
let mut has_err = false;
|
2022-01-20 18:00:00 -06:00
|
|
|
for &(kind, span) in &this.items {
|
|
|
|
match kind {
|
|
|
|
ItemKind::Asm if has_asm => {
|
2022-04-01 00:29:38 -05:00
|
|
|
must_show_error = true;
|
2022-01-20 18:00:00 -06:00
|
|
|
diag.span_label(span, "multiple asm blocks are unsupported in naked functions");
|
|
|
|
}
|
|
|
|
ItemKind::Asm => has_asm = true,
|
|
|
|
ItemKind::NonAsm => {
|
2022-04-01 00:29:38 -05:00
|
|
|
must_show_error = true;
|
2022-01-20 18:00:00 -06:00
|
|
|
diag.span_label(span, "non-asm is unsupported in naked functions");
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
2022-04-01 00:29:38 -05:00
|
|
|
ItemKind::Err => has_err = true,
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
2022-01-20 18:00:00 -06:00
|
|
|
}
|
2022-04-01 00:29:38 -05: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 {
|
|
|
|
diag.emit();
|
|
|
|
} else {
|
|
|
|
diag.cancel();
|
|
|
|
}
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CheckInlineAssembly<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
items: Vec<(ItemKind, Span)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
enum ItemKind {
|
|
|
|
Asm,
|
|
|
|
NonAsm,
|
2022-04-01 00:29:38 -05:00
|
|
|
Err,
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CheckInlineAssembly<'tcx> {
|
|
|
|
fn check_expr(&mut self, expr: &'tcx hir::Expr<'tcx>, span: Span) {
|
|
|
|
match expr.kind {
|
|
|
|
ExprKind::Box(..)
|
|
|
|
| ExprKind::ConstBlock(..)
|
|
|
|
| ExprKind::Array(..)
|
|
|
|
| ExprKind::Call(..)
|
|
|
|
| ExprKind::MethodCall(..)
|
|
|
|
| ExprKind::Tup(..)
|
|
|
|
| ExprKind::Binary(..)
|
|
|
|
| ExprKind::Unary(..)
|
|
|
|
| ExprKind::Lit(..)
|
|
|
|
| ExprKind::Cast(..)
|
|
|
|
| ExprKind::Type(..)
|
|
|
|
| ExprKind::Loop(..)
|
|
|
|
| ExprKind::Match(..)
|
2021-01-01 12:38:11 -06:00
|
|
|
| ExprKind::If(..)
|
2022-06-11 14:25:25 -05:00
|
|
|
| ExprKind::Closure { .. }
|
2020-12-06 18:00:00 -06:00
|
|
|
| ExprKind::Assign(..)
|
|
|
|
| ExprKind::AssignOp(..)
|
|
|
|
| ExprKind::Field(..)
|
|
|
|
| ExprKind::Index(..)
|
|
|
|
| ExprKind::Path(..)
|
|
|
|
| ExprKind::AddrOf(..)
|
2021-08-08 09:49:13 -05:00
|
|
|
| ExprKind::Let(..)
|
2020-12-06 18:00:00 -06:00
|
|
|
| ExprKind::Break(..)
|
|
|
|
| ExprKind::Continue(..)
|
|
|
|
| ExprKind::Ret(..)
|
|
|
|
| ExprKind::Struct(..)
|
|
|
|
| ExprKind::Repeat(..)
|
|
|
|
| ExprKind::Yield(..) => {
|
|
|
|
self.items.push((ItemKind::NonAsm, span));
|
|
|
|
}
|
|
|
|
|
|
|
|
ExprKind::InlineAsm(ref asm) => {
|
|
|
|
self.items.push((ItemKind::Asm, span));
|
2022-01-20 18:00:00 -06:00
|
|
|
self.check_inline_asm(asm, span);
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
2022-04-01 00:29:38 -05:00
|
|
|
ExprKind::DropTemps(..) | ExprKind::Block(..) => {
|
2020-12-06 18:00:00 -06:00
|
|
|
hir::intravisit::walk_expr(self, expr);
|
|
|
|
}
|
2022-04-01 00:29:38 -05:00
|
|
|
|
|
|
|
ExprKind::Err => {
|
|
|
|
self.items.push((ItemKind::Err, span));
|
|
|
|
}
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 18:00:00 -06:00
|
|
|
fn check_inline_asm(&self, asm: &'tcx hir::InlineAsm<'tcx>, span: Span) {
|
2020-12-06 18:00:00 -06:00
|
|
|
let unsupported_operands: Vec<Span> = asm
|
|
|
|
.operands
|
|
|
|
.iter()
|
|
|
|
.filter_map(|&(ref op, op_sp)| match op {
|
2022-02-28 18:50:56 -06:00
|
|
|
InlineAsmOperand::Const { .. }
|
|
|
|
| InlineAsmOperand::SymFn { .. }
|
|
|
|
| InlineAsmOperand::SymStatic { .. } => None,
|
2020-12-06 18:00:00 -06:00
|
|
|
InlineAsmOperand::In { .. }
|
|
|
|
| InlineAsmOperand::Out { .. }
|
|
|
|
| InlineAsmOperand::InOut { .. }
|
|
|
|
| InlineAsmOperand::SplitInOut { .. } => Some(op_sp),
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
if !unsupported_operands.is_empty() {
|
2022-01-20 18:00:00 -06:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
2020-12-06 18:00:00 -06:00
|
|
|
unsupported_operands,
|
2022-01-20 18:00:00 -06:00
|
|
|
E0787,
|
|
|
|
"only `const` and `sym` operands are supported in naked functions",
|
|
|
|
)
|
|
|
|
.emit();
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
let unsupported_options: Vec<&'static str> = [
|
2022-01-20 18:00:00 -06:00
|
|
|
(InlineAsmOptions::MAY_UNWIND, "`may_unwind`"),
|
2020-12-06 18:00:00 -06:00
|
|
|
(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();
|
|
|
|
|
|
|
|
if !unsupported_options.is_empty() {
|
2022-01-20 18:00:00 -06:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
span,
|
|
|
|
E0787,
|
|
|
|
"asm options unsupported in naked functions: {}",
|
|
|
|
unsupported_options.join(", ")
|
|
|
|
)
|
|
|
|
.emit();
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if !asm.options.contains(InlineAsmOptions::NORETURN) {
|
2022-03-31 17:14:01 -05:00
|
|
|
let last_span = asm
|
|
|
|
.operands
|
|
|
|
.last()
|
|
|
|
.map_or_else(|| asm.template_strs.last().unwrap().2, |op| op.1)
|
|
|
|
.shrink_to_hi();
|
|
|
|
|
2022-01-20 18:00:00 -06:00
|
|
|
struct_span_err!(
|
|
|
|
self.tcx.sess,
|
|
|
|
span,
|
|
|
|
E0787,
|
|
|
|
"asm in naked functions must use `noreturn` option"
|
|
|
|
)
|
2022-03-31 17:14:01 -05:00
|
|
|
.span_suggestion(
|
|
|
|
last_span,
|
|
|
|
"consider specifying that the asm block is responsible \
|
2022-04-01 10:28:45 -05:00
|
|
|
for returning from the function",
|
2022-06-13 01:48:40 -05:00
|
|
|
", options(noreturn)",
|
2022-03-31 17:14:01 -05:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
)
|
2022-01-20 18:00:00 -06:00
|
|
|
.emit();
|
2020-12-06 18:00:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for CheckInlineAssembly<'tcx> {
|
|
|
|
fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
|
|
|
|
match stmt.kind {
|
|
|
|
StmtKind::Item(..) => {}
|
|
|
|
StmtKind::Local(..) => {
|
|
|
|
self.items.push((ItemKind::NonAsm, stmt.span));
|
|
|
|
}
|
|
|
|
StmtKind::Expr(ref expr) | StmtKind::Semi(ref expr) => {
|
|
|
|
self.check_expr(expr, stmt.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
|
|
|
self.check_expr(&expr, expr.span);
|
|
|
|
}
|
|
|
|
}
|