rustc_mir_transform/
check_alignment.rs1use rustc_abi::Align;
2use rustc_attr_ir::lang_items::LangItem;
3use rustc_index::IndexVec;
4use rustc_middle::mir::interpret::Scalar;
5use rustc_middle::mir::visit::PlaceContext;
6use rustc_middle::mir::*;
7use rustc_middle::ty::{Ty, TyCtxt};
8
9use crate::PassPolicy;
10use crate::check_pointers::{BorrowedFieldProjectionMode, PointerCheck, check_pointers};
11
12pub(super) struct CheckAlignment;
13
14impl<'tcx> crate::MirPass<'tcx> for CheckAlignment {
15 fn policy(&self, ctx: &crate::PassCtx<'_>) -> PassPolicy {
16 PassPolicy::optional(ctx.ub_checks())
18 }
19
20 fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
21 let excluded_pointees = [tcx.types.bool, tcx.types.i8, tcx.types.u8];
23
24 check_pointers(
28 tcx,
29 body,
30 &excluded_pointees,
31 insert_alignment_check,
32 BorrowedFieldProjectionMode::FollowProjections,
33 );
34 }
35}
36
37fn insert_alignment_check<'tcx>(
40 tcx: TyCtxt<'tcx>,
41 pointer: Place<'tcx>,
42 pointee_ty: Ty<'tcx>,
43 _context: PlaceContext,
44 local_decls: &mut IndexVec<Local, LocalDecl<'tcx>>,
45 stmts: &mut Vec<Statement<'tcx>>,
46 source_info: SourceInfo,
47) -> PointerCheck<'tcx> {
48 let const_raw_ptr = Ty::new_imm_ptr(tcx, tcx.types.unit);
50 let rvalue = Rvalue::Cast(CastKind::PtrToPtr, Operand::Copy(pointer), const_raw_ptr);
51 let thin_ptr = local_decls.push(LocalDecl::with_source_info(const_raw_ptr, source_info)).into();
52 stmts.push(Statement::new(source_info, StatementKind::Assign(Box::new((thin_ptr, rvalue)))));
53
54 let rvalue = Rvalue::Cast(CastKind::Transmute, Operand::Copy(thin_ptr), tcx.types.usize);
56 let addr = local_decls.push(LocalDecl::with_source_info(tcx.types.usize, source_info)).into();
57 stmts.push(Statement::new(source_info, StatementKind::Assign(Box::new((addr, rvalue)))));
58
59 let align_def_id = tcx.require_lang_item(LangItem::AlignOf, source_info.span);
61 let alignment = Operand::unevaluated_constant(
62 tcx,
63 align_def_id,
64 tcx.mk_args(&[pointee_ty.into()]),
65 source_info.span,
66 );
67
68 let alignment_mask =
70 local_decls.push(LocalDecl::with_source_info(tcx.types.usize, source_info)).into();
71 let one = Operand::Constant(Box::new(ConstOperand {
72 span: source_info.span,
73 user_ty: None,
74 const_: Const::Val(ConstValue::Scalar(Scalar::from_target_usize(1, &tcx)), tcx.types.usize),
75 }));
76 stmts.push(Statement::new(
77 source_info,
78 StatementKind::Assign(Box::new((
79 alignment_mask,
80 Rvalue::BinaryOp(BinOp::Sub, Box::new((alignment.clone(), one))),
81 ))),
82 ));
83
84 if let max_align = tcx.sess.target.max_reliable_alignment()
87 && max_align < Align::MAX
88 {
89 let max_mask = max_align.bytes() - 1;
90 let max_mask = Operand::Constant(Box::new(ConstOperand {
91 span: source_info.span,
92 user_ty: None,
93 const_: Const::Val(
94 ConstValue::Scalar(Scalar::from_target_usize(max_mask, &tcx)),
95 tcx.types.usize,
96 ),
97 }));
98 stmts.push(Statement::new(
99 source_info,
100 StatementKind::Assign(Box::new((
101 alignment_mask,
102 Rvalue::BinaryOp(
103 BinOp::BitAnd,
104 Box::new((Operand::Copy(alignment_mask), max_mask)),
105 ),
106 ))),
107 ));
108 }
109
110 let alignment_bits =
112 local_decls.push(LocalDecl::with_source_info(tcx.types.usize, source_info)).into();
113 stmts.push(Statement::new(
114 source_info,
115 StatementKind::Assign(Box::new((
116 alignment_bits,
117 Rvalue::BinaryOp(
118 BinOp::BitAnd,
119 Box::new((Operand::Copy(addr), Operand::Copy(alignment_mask))),
120 ),
121 ))),
122 ));
123
124 let is_ok = local_decls.push(LocalDecl::with_source_info(tcx.types.bool, source_info)).into();
126 let zero = Operand::Constant(Box::new(ConstOperand {
127 span: source_info.span,
128 user_ty: None,
129 const_: Const::Val(ConstValue::Scalar(Scalar::from_target_usize(0, &tcx)), tcx.types.usize),
130 }));
131 stmts.push(Statement::new(
132 source_info,
133 StatementKind::Assign(Box::new((
134 is_ok,
135 Rvalue::BinaryOp(BinOp::Eq, Box::new((Operand::Copy(alignment_bits), zero.clone()))),
136 ))),
137 ));
138
139 PointerCheck {
142 cond: Operand::Copy(is_ok),
143 assert_kind: Box::new(AssertKind::MisalignedPointerDereference {
144 required: alignment,
145 found: Operand::Copy(addr),
146 }),
147 }
148}