forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpointer_alignment_check.rs
162 lines (144 loc) · 4.9 KB
/
pointer_alignment_check.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use rustc_hir::LangItem;
use rustc_middle::mir;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext};
use rustc_span::Span;
use super::FunctionCx;
use crate::base;
use crate::common;
use crate::mir::OperandValue;
use crate::traits::*;
pub fn pointers_to_check<F>(
statement: &mir::Statement<'_>,
required_align_of: F,
) -> Vec<(mir::Local, u64)>
where
F: Fn(mir::Local) -> Option<u64>,
{
let mut finder = PointerFinder { required_align_of, pointers: Vec::new() };
finder.visit_statement(statement, rustc_middle::mir::Location::START);
finder.pointers
}
struct PointerFinder<F> {
pointers: Vec<(mir::Local, u64)>,
required_align_of: F,
}
impl<'tcx, F> Visitor<'tcx> for PointerFinder<F>
where
F: Fn(mir::Local) -> Option<u64>,
{
fn visit_place(
&mut self,
place: &mir::Place<'tcx>,
context: PlaceContext,
location: mir::Location,
) {
// We want to only check reads and writes to Places, so we specifically exclude
// Borrows and AddressOf.
match context {
PlaceContext::MutatingUse(
MutatingUseContext::Store
| MutatingUseContext::AsmOutput
| MutatingUseContext::Call
| MutatingUseContext::Yield
| MutatingUseContext::Drop,
) => {}
PlaceContext::NonMutatingUse(
NonMutatingUseContext::Copy | NonMutatingUseContext::Move,
) => {}
_ => {
return;
}
}
if !place.is_indirect() {
return;
}
let pointer = place.local;
let Some(required_alignment) = (self.required_align_of)(pointer) else {
return;
};
if required_alignment == 1 {
return;
}
// Ensure that this place is based on an aligned pointer.
self.pointers.push((pointer, required_alignment));
self.super_place(place, context, location);
}
}
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
#[instrument(level = "debug", skip(self, bx))]
pub fn codegen_alignment_check(
&mut self,
bx: &mut Bx,
pointer: mir::Operand<'tcx>,
required_alignment: u64,
source_info: mir::SourceInfo,
) {
// Compute the alignment mask
let mask = bx.const_usize(required_alignment - 1);
let zero = bx.const_usize(0);
let required_alignment = bx.const_usize(required_alignment);
// And the pointer with the mask
let pointer = match self.codegen_operand(bx, &pointer).val {
OperandValue::Immediate(imm) => imm,
OperandValue::Pair(ptr, _) => ptr,
_ => {
unreachable!("{pointer:?}");
}
};
let addr = bx.ptrtoint(pointer, bx.cx().type_isize());
let masked = bx.and(addr, mask);
// Branch on whether the masked value is zero
let is_zero = bx.icmp(
base::bin_op_to_icmp_predicate(mir::BinOp::Eq.to_hir_binop(), false),
masked,
zero,
);
// Create destination blocks, branching on is_zero
let panic = bx.append_sibling_block("panic");
let success = bx.append_sibling_block("success");
bx.cond_br(is_zero, success, panic);
// Switch to the failure block and codegen a call to the panic intrinsic
bx.switch_to_block(panic);
self.set_debug_loc(bx, source_info);
let location = self.get_caller_location(bx, source_info).immediate();
self.codegen_nounwind_panic(
bx,
LangItem::PanicMisalignedPointerDereference,
&[required_alignment, addr, location],
source_info.span,
);
// Continue codegen in the success block.
bx.switch_to_block(success);
self.set_debug_loc(bx, source_info);
}
/// Emit a call to a diverging and `rustc_nounwind` panic helper.
#[instrument(level = "debug", skip(self, bx))]
fn codegen_nounwind_panic(
&mut self,
bx: &mut Bx,
lang_item: LangItem,
args: &[Bx::Value],
span: Span,
) {
let (fn_abi, fn_ptr, instance) = common::build_langcall(bx, Some(span), lang_item);
let fn_ty = bx.fn_decl_backend_type(&fn_abi);
let fn_attrs = if bx.tcx().def_kind(self.instance.def_id()).has_codegen_attrs() {
Some(bx.tcx().codegen_fn_attrs(self.instance.def_id()))
} else {
None
};
// bx.call requires that the call not unwind. Double-check that this LangItem can't unwind.
assert!(!fn_abi.can_unwind);
bx.call(
fn_ty,
fn_attrs,
Some(&fn_abi),
fn_ptr,
args,
None, /* funclet */
Some(instance),
);
bx.unreachable();
}
}