2023-05-14 14:20:32 +00:00
|
|
|
use rustc_ast::Mutability;
|
2023-07-14 20:10:14 +00:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
|
|
|
use rustc_hir::{def::Res, Expr, ExprKind, HirId, Local, QPath, StmtKind, UnOp};
|
|
|
|
use rustc_middle::ty::{self, TypeAndMut};
|
|
|
|
use rustc_span::{sym, Span};
|
2023-05-14 14:20:32 +00:00
|
|
|
|
2023-07-06 19:45:24 +00:00
|
|
|
use crate::{lints::InvalidReferenceCastingDiag, LateContext, LateLintPass, LintContext};
|
2023-05-14 14:20:32 +00:00
|
|
|
|
|
|
|
declare_lint! {
|
2023-07-06 19:45:24 +00:00
|
|
|
/// The `invalid_reference_casting` lint checks for casts of `&T` to `&mut T`
|
2023-05-14 14:20:32 +00:00
|
|
|
/// without using interior mutability.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```rust,compile_fail
|
|
|
|
/// fn x(r: &i32) {
|
|
|
|
/// unsafe {
|
|
|
|
/// *(r as *const i32 as *mut i32) += 1;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// {{produces}}
|
|
|
|
///
|
|
|
|
/// ### Explanation
|
|
|
|
///
|
|
|
|
/// Casting `&T` to `&mut T` without using interior mutability is undefined behavior,
|
|
|
|
/// as it's a violation of Rust reference aliasing requirements.
|
|
|
|
///
|
|
|
|
/// `UnsafeCell` is the only way to obtain aliasable data that is considered
|
|
|
|
/// mutable.
|
2023-07-06 19:45:24 +00:00
|
|
|
INVALID_REFERENCE_CASTING,
|
2023-07-14 19:54:27 +00:00
|
|
|
Deny,
|
2023-05-14 14:20:32 +00:00
|
|
|
"casts of `&T` to `&mut T` without interior mutability"
|
|
|
|
}
|
|
|
|
|
2023-07-14 20:10:14 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct InvalidReferenceCasting {
|
|
|
|
casted: FxHashMap<HirId, Span>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(InvalidReferenceCasting => [INVALID_REFERENCE_CASTING]);
|
2023-05-14 14:20:32 +00:00
|
|
|
|
2023-07-06 19:45:24 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
|
2023-07-14 20:10:14 +00:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx rustc_hir::Stmt<'tcx>) {
|
|
|
|
let StmtKind::Local(local) = stmt.kind else {
|
2023-05-14 14:20:32 +00:00
|
|
|
return;
|
|
|
|
};
|
2023-07-14 20:10:14 +00:00
|
|
|
let Local { init: Some(init), els: None, .. } = local else {
|
2023-05-14 14:20:32 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2023-07-14 20:10:14 +00:00
|
|
|
if is_cast_from_const_to_mut(cx, init) {
|
|
|
|
self.casted.insert(local.pat.hir_id, init.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
2023-08-22 13:41:07 +00:00
|
|
|
let Some((is_assignment, e)) = is_operation_we_care_about(cx, expr) else {
|
2023-05-14 14:20:32 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2023-07-14 20:25:47 +00:00
|
|
|
let orig_cast = if is_cast_from_const_to_mut(cx, e) {
|
|
|
|
None
|
2023-07-14 20:10:14 +00:00
|
|
|
} else if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind
|
|
|
|
&& let Res::Local(hir_id) = &path.res
|
|
|
|
&& let Some(orig_cast) = self.casted.get(hir_id) {
|
2023-07-14 20:25:47 +00:00
|
|
|
Some(*orig_cast)
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
|
|
|
cx.emit_spanned_lint(
|
|
|
|
INVALID_REFERENCE_CASTING,
|
|
|
|
expr.span,
|
2023-08-22 13:41:07 +00:00
|
|
|
if is_assignment {
|
2023-07-14 20:25:47 +00:00
|
|
|
InvalidReferenceCastingDiag::AssignToRef { orig_cast }
|
2023-08-22 13:41:07 +00:00
|
|
|
} else {
|
|
|
|
InvalidReferenceCastingDiag::BorrowAsMut { orig_cast }
|
2023-07-14 20:25:47 +00:00
|
|
|
},
|
|
|
|
);
|
2023-05-14 14:20:32 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-14 20:10:14 +00:00
|
|
|
|
2023-08-22 13:41:07 +00:00
|
|
|
fn is_operation_we_care_about<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
e: &'tcx Expr<'tcx>,
|
|
|
|
) -> Option<(bool, &'tcx Expr<'tcx>)> {
|
|
|
|
fn deref_assign_or_addr_of<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<(bool, &'tcx Expr<'tcx>)> {
|
|
|
|
// &mut <expr>
|
|
|
|
let inner = if let ExprKind::AddrOf(_, Mutability::Mut, expr) = expr.kind {
|
|
|
|
expr
|
|
|
|
// <expr> = ...
|
|
|
|
} else if let ExprKind::Assign(expr, _, _) = expr.kind {
|
|
|
|
expr
|
|
|
|
// <expr> += ...
|
|
|
|
} else if let ExprKind::AssignOp(_, expr, _) = expr.kind {
|
|
|
|
expr
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
if let ExprKind::Unary(UnOp::Deref, e) = &inner.kind {
|
|
|
|
Some((!matches!(expr.kind, ExprKind::AddrOf(..)), e))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ptr_write<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
e: &'tcx Expr<'tcx>,
|
|
|
|
) -> Option<(bool, &'tcx Expr<'tcx>)> {
|
|
|
|
if let ExprKind::Call(path, [arg_ptr, _arg_val]) = e.kind
|
|
|
|
&& let ExprKind::Path(ref qpath) = path.kind
|
|
|
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
|
|
|
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_write | sym::ptr_write_volatile | sym::ptr_write_unaligned))
|
|
|
|
{
|
|
|
|
Some((true, arg_ptr))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deref_assign_or_addr_of(e).or_else(|| ptr_write(cx, e))
|
|
|
|
}
|
|
|
|
|
2023-07-14 20:10:14 +00:00
|
|
|
fn is_cast_from_const_to_mut<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
|
|
|
|
let e = e.peel_blocks();
|
|
|
|
|
2023-08-12 10:40:26 +00:00
|
|
|
fn from_casts<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> Option<&'tcx Expr<'tcx>> {
|
|
|
|
// <expr> as *mut ...
|
2023-08-13 15:08:33 +00:00
|
|
|
let mut e = if let ExprKind::Cast(e, t) = e.kind
|
2023-08-12 10:40:26 +00:00
|
|
|
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(t.hir_id).kind() {
|
|
|
|
e
|
|
|
|
// <expr>.cast_mut()
|
|
|
|
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
|
|
|
|
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::ptr_cast_mut, def_id) {
|
|
|
|
expr
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
2023-07-14 20:10:14 +00:00
|
|
|
|
2023-08-13 15:08:33 +00:00
|
|
|
let mut had_at_least_one_cast = false;
|
|
|
|
loop {
|
|
|
|
e = e.peel_blocks();
|
|
|
|
// <expr> as *mut/const ... or <expr> as <uint>
|
|
|
|
e = if let ExprKind::Cast(expr, t) = e.kind
|
|
|
|
&& matches!(cx.typeck_results().node_type(t.hir_id).kind(), ty::RawPtr(_) | ty::Uint(_)) {
|
|
|
|
had_at_least_one_cast = true;
|
|
|
|
expr
|
|
|
|
// <expr>.cast(), <expr>.cast_mut() or <expr>.cast_const()
|
|
|
|
} else if let ExprKind::MethodCall(_, expr, [], _) = e.kind
|
|
|
|
&& let Some(def_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
|
|
|
&& matches!(
|
|
|
|
cx.tcx.get_diagnostic_name(def_id),
|
|
|
|
Some(sym::ptr_cast | sym::const_ptr_cast | sym::ptr_cast_mut | sym::ptr_cast_const)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
had_at_least_one_cast = true;
|
|
|
|
expr
|
|
|
|
// ptr::from_ref(<expr>)
|
|
|
|
} else if let ExprKind::Call(path, [arg]) = e.kind
|
|
|
|
&& let ExprKind::Path(ref qpath) = path.kind
|
|
|
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::ptr_from_ref, def_id) {
|
|
|
|
return Some(arg);
|
|
|
|
} else if had_at_least_one_cast {
|
|
|
|
return Some(e);
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
}
|
2023-08-12 10:40:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_transmute<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
e: &'tcx Expr<'tcx>,
|
|
|
|
) -> Option<&'tcx Expr<'tcx>> {
|
|
|
|
// mem::transmute::<_, *mut _>(<expr>)
|
|
|
|
if let ExprKind::Call(path, [arg]) = e.kind
|
|
|
|
&& let ExprKind::Path(ref qpath) = path.kind
|
|
|
|
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
|
|
|
|
&& cx.tcx.is_diagnostic_item(sym::transmute, def_id)
|
|
|
|
&& let ty::RawPtr(TypeAndMut { mutbl: Mutability::Mut, .. }) = cx.typeck_results().node_type(e.hir_id).kind() {
|
|
|
|
Some(arg)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2023-07-14 20:10:14 +00:00
|
|
|
|
2023-08-12 10:40:26 +00:00
|
|
|
let Some(e) = from_casts(cx, e).or_else(|| from_transmute(cx, e)) else {
|
2023-07-14 20:10:14 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
let e = e.peel_blocks();
|
2023-07-14 20:15:28 +00:00
|
|
|
matches!(cx.typeck_results().node_type(e.hir_id).kind(), ty::Ref(_, _, Mutability::Not))
|
2023-07-14 20:10:14 +00:00
|
|
|
}
|