1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
3 use rustc_data_structures::graph::dominators::Dominators;
4 use rustc_middle::mir::visit::Visitor;
5 use rustc_middle::mir::{self, BasicBlock, Body, Location, NonDivergingIntrinsic, Place, Rvalue};
6 use rustc_middle::mir::{BorrowKind, Mutability, Operand};
7 use rustc_middle::mir::{InlineAsmOperand, Terminator, TerminatorKind};
8 use rustc_middle::mir::{Statement, StatementKind};
9 use rustc_middle::ty::TyCtxt;
10
11 use crate::{
12 borrow_set::BorrowSet, facts::AllFacts, location::LocationTable, path_utils::*, AccessDepth,
13 Activation, ArtificialField, BorrowIndex, Deep, LocalMutationIsAllowed, Read, ReadKind,
14 ReadOrWrite, Reservation, Shallow, Write, WriteKind,
15 };
16
generate_invalidates<'tcx>( tcx: TyCtxt<'tcx>, all_facts: &mut Option<AllFacts>, location_table: &LocationTable, body: &Body<'tcx>, borrow_set: &BorrowSet<'tcx>, )17 pub(super) fn generate_invalidates<'tcx>(
18 tcx: TyCtxt<'tcx>,
19 all_facts: &mut Option<AllFacts>,
20 location_table: &LocationTable,
21 body: &Body<'tcx>,
22 borrow_set: &BorrowSet<'tcx>,
23 ) {
24 if all_facts.is_none() {
25 // Nothing to do if we don't have any facts
26 return;
27 }
28
29 if let Some(all_facts) = all_facts {
30 let _prof_timer = tcx.prof.generic_activity("polonius_fact_generation");
31 let dominators = body.basic_blocks.dominators();
32 let mut ig = InvalidationGenerator {
33 all_facts,
34 borrow_set,
35 tcx,
36 location_table,
37 body: &body,
38 dominators,
39 };
40 ig.visit_body(body);
41 }
42 }
43
44 struct InvalidationGenerator<'cx, 'tcx> {
45 tcx: TyCtxt<'tcx>,
46 all_facts: &'cx mut AllFacts,
47 location_table: &'cx LocationTable,
48 body: &'cx Body<'tcx>,
49 dominators: &'cx Dominators<BasicBlock>,
50 borrow_set: &'cx BorrowSet<'tcx>,
51 }
52
53 /// Visits the whole MIR and generates `invalidates()` facts.
54 /// Most of the code implementing this was stolen from `borrow_check/mod.rs`.
55 impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
visit_statement(&mut self, statement: &Statement<'tcx>, location: Location)56 fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
57 self.check_activations(location);
58
59 match &statement.kind {
60 StatementKind::Assign(box (lhs, rhs)) => {
61 self.consume_rvalue(location, rhs);
62
63 self.mutate_place(location, *lhs, Shallow(None));
64 }
65 StatementKind::FakeRead(box (_, _)) => {
66 // Only relevant for initialized/liveness/safety checks.
67 }
68 StatementKind::Intrinsic(box NonDivergingIntrinsic::Assume(op)) => {
69 self.consume_operand(location, op);
70 }
71 StatementKind::Intrinsic(box NonDivergingIntrinsic::CopyNonOverlapping(mir::CopyNonOverlapping {
72 src,
73 dst,
74 count,
75 })) => {
76 self.consume_operand(location, src);
77 self.consume_operand(location, dst);
78 self.consume_operand(location, count);
79 }
80 // Only relevant for mir typeck
81 StatementKind::AscribeUserType(..)
82 // Only relevant for liveness and unsafeck
83 | StatementKind::PlaceMention(..)
84 // Doesn't have any language semantics
85 | StatementKind::Coverage(..)
86 // Does not actually affect borrowck
87 | StatementKind::StorageLive(..) => {}
88 StatementKind::StorageDead(local) => {
89 self.access_place(
90 location,
91 Place::from(*local),
92 (Shallow(None), Write(WriteKind::StorageDeadOrDrop)),
93 LocalMutationIsAllowed::Yes,
94 );
95 }
96 StatementKind::ConstEvalCounter
97 | StatementKind::Nop
98 | StatementKind::Retag { .. }
99 | StatementKind::Deinit(..)
100 | StatementKind::SetDiscriminant { .. } => {
101 bug!("Statement not allowed in this MIR phase")
102 }
103 }
104
105 self.super_statement(statement, location);
106 }
107
visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location)108 fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) {
109 self.check_activations(location);
110
111 match &terminator.kind {
112 TerminatorKind::SwitchInt { discr, targets: _ } => {
113 self.consume_operand(location, discr);
114 }
115 TerminatorKind::Drop { place: drop_place, target: _, unwind: _, replace } => {
116 let write_kind =
117 if *replace { WriteKind::Replace } else { WriteKind::StorageDeadOrDrop };
118 self.access_place(
119 location,
120 *drop_place,
121 (AccessDepth::Drop, Write(write_kind)),
122 LocalMutationIsAllowed::Yes,
123 );
124 }
125 TerminatorKind::Call {
126 func,
127 args,
128 destination,
129 target: _,
130 unwind: _,
131 call_source: _,
132 fn_span: _,
133 } => {
134 self.consume_operand(location, func);
135 for arg in args {
136 self.consume_operand(location, arg);
137 }
138 self.mutate_place(location, *destination, Deep);
139 }
140 TerminatorKind::Assert { cond, expected: _, msg, target: _, unwind: _ } => {
141 self.consume_operand(location, cond);
142 use rustc_middle::mir::AssertKind;
143 if let AssertKind::BoundsCheck { len, index } = &**msg {
144 self.consume_operand(location, len);
145 self.consume_operand(location, index);
146 }
147 }
148 TerminatorKind::Yield { value, resume, resume_arg, drop: _ } => {
149 self.consume_operand(location, value);
150
151 // Invalidate all borrows of local places
152 let borrow_set = self.borrow_set;
153 let resume = self.location_table.start_index(resume.start_location());
154 for (i, data) in borrow_set.iter_enumerated() {
155 if borrow_of_local_data(data.borrowed_place) {
156 self.all_facts.loan_invalidated_at.push((resume, i));
157 }
158 }
159
160 self.mutate_place(location, *resume_arg, Deep);
161 }
162 TerminatorKind::Resume | TerminatorKind::Return | TerminatorKind::GeneratorDrop => {
163 // Invalidate all borrows of local places
164 let borrow_set = self.borrow_set;
165 let start = self.location_table.start_index(location);
166 for (i, data) in borrow_set.iter_enumerated() {
167 if borrow_of_local_data(data.borrowed_place) {
168 self.all_facts.loan_invalidated_at.push((start, i));
169 }
170 }
171 }
172 TerminatorKind::InlineAsm {
173 template: _,
174 operands,
175 options: _,
176 line_spans: _,
177 destination: _,
178 unwind: _,
179 } => {
180 for op in operands {
181 match op {
182 InlineAsmOperand::In { reg: _, value } => {
183 self.consume_operand(location, value);
184 }
185 InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
186 if let &Some(place) = place {
187 self.mutate_place(location, place, Shallow(None));
188 }
189 }
190 InlineAsmOperand::InOut { reg: _, late: _, in_value, out_place } => {
191 self.consume_operand(location, in_value);
192 if let &Some(out_place) = out_place {
193 self.mutate_place(location, out_place, Shallow(None));
194 }
195 }
196 InlineAsmOperand::Const { value: _ }
197 | InlineAsmOperand::SymFn { value: _ }
198 | InlineAsmOperand::SymStatic { def_id: _ } => {}
199 }
200 }
201 }
202 TerminatorKind::Goto { target: _ }
203 | TerminatorKind::Terminate
204 | TerminatorKind::Unreachable
205 | TerminatorKind::FalseEdge { real_target: _, imaginary_target: _ }
206 | TerminatorKind::FalseUnwind { real_target: _, unwind: _ } => {
207 // no data used, thus irrelevant to borrowck
208 }
209 }
210
211 self.super_terminator(terminator, location);
212 }
213 }
214
215 impl<'cx, 'tcx> InvalidationGenerator<'cx, 'tcx> {
216 /// Simulates mutation of a place.
mutate_place(&mut self, location: Location, place: Place<'tcx>, kind: AccessDepth)217 fn mutate_place(&mut self, location: Location, place: Place<'tcx>, kind: AccessDepth) {
218 self.access_place(
219 location,
220 place,
221 (kind, Write(WriteKind::Mutate)),
222 LocalMutationIsAllowed::ExceptUpvars,
223 );
224 }
225
226 /// Simulates consumption of an operand.
consume_operand(&mut self, location: Location, operand: &Operand<'tcx>)227 fn consume_operand(&mut self, location: Location, operand: &Operand<'tcx>) {
228 match *operand {
229 Operand::Copy(place) => {
230 self.access_place(
231 location,
232 place,
233 (Deep, Read(ReadKind::Copy)),
234 LocalMutationIsAllowed::No,
235 );
236 }
237 Operand::Move(place) => {
238 self.access_place(
239 location,
240 place,
241 (Deep, Write(WriteKind::Move)),
242 LocalMutationIsAllowed::Yes,
243 );
244 }
245 Operand::Constant(_) => {}
246 }
247 }
248
249 // Simulates consumption of an rvalue
consume_rvalue(&mut self, location: Location, rvalue: &Rvalue<'tcx>)250 fn consume_rvalue(&mut self, location: Location, rvalue: &Rvalue<'tcx>) {
251 match rvalue {
252 &Rvalue::Ref(_ /*rgn*/, bk, place) => {
253 let access_kind = match bk {
254 BorrowKind::Shallow => {
255 (Shallow(Some(ArtificialField::ShallowBorrow)), Read(ReadKind::Borrow(bk)))
256 }
257 BorrowKind::Shared => (Deep, Read(ReadKind::Borrow(bk))),
258 BorrowKind::Mut { .. } => {
259 let wk = WriteKind::MutableBorrow(bk);
260 if allow_two_phase_borrow(bk) {
261 (Deep, Reservation(wk))
262 } else {
263 (Deep, Write(wk))
264 }
265 }
266 };
267
268 self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
269 }
270
271 &Rvalue::AddressOf(mutability, place) => {
272 let access_kind = match mutability {
273 Mutability::Mut => (
274 Deep,
275 Write(WriteKind::MutableBorrow(BorrowKind::Mut {
276 kind: mir::MutBorrowKind::Default,
277 })),
278 ),
279 Mutability::Not => (Deep, Read(ReadKind::Borrow(BorrowKind::Shared))),
280 };
281
282 self.access_place(location, place, access_kind, LocalMutationIsAllowed::No);
283 }
284
285 Rvalue::ThreadLocalRef(_) => {}
286
287 Rvalue::Use(operand)
288 | Rvalue::Repeat(operand, _)
289 | Rvalue::UnaryOp(_ /*un_op*/, operand)
290 | Rvalue::Cast(_ /*cast_kind*/, operand, _ /*ty*/)
291 | Rvalue::ShallowInitBox(operand, _ /*ty*/) => self.consume_operand(location, operand),
292
293 &Rvalue::CopyForDeref(place) => {
294 let op = &Operand::Copy(place);
295 self.consume_operand(location, op);
296 }
297
298 &(Rvalue::Len(place) | Rvalue::Discriminant(place)) => {
299 let af = match rvalue {
300 Rvalue::Len(..) => Some(ArtificialField::ArrayLength),
301 Rvalue::Discriminant(..) => None,
302 _ => unreachable!(),
303 };
304 self.access_place(
305 location,
306 place,
307 (Shallow(af), Read(ReadKind::Copy)),
308 LocalMutationIsAllowed::No,
309 );
310 }
311
312 Rvalue::BinaryOp(_bin_op, box (operand1, operand2))
313 | Rvalue::CheckedBinaryOp(_bin_op, box (operand1, operand2)) => {
314 self.consume_operand(location, operand1);
315 self.consume_operand(location, operand2);
316 }
317
318 Rvalue::NullaryOp(_op, _ty) => {}
319
320 Rvalue::Aggregate(_, operands) => {
321 for operand in operands {
322 self.consume_operand(location, operand);
323 }
324 }
325 }
326 }
327
328 /// Simulates an access to a place.
access_place( &mut self, location: Location, place: Place<'tcx>, kind: (AccessDepth, ReadOrWrite), _is_local_mutation_allowed: LocalMutationIsAllowed, )329 fn access_place(
330 &mut self,
331 location: Location,
332 place: Place<'tcx>,
333 kind: (AccessDepth, ReadOrWrite),
334 _is_local_mutation_allowed: LocalMutationIsAllowed,
335 ) {
336 let (sd, rw) = kind;
337 // note: not doing check_access_permissions checks because they don't generate invalidates
338 self.check_access_for_conflict(location, place, sd, rw);
339 }
340
check_access_for_conflict( &mut self, location: Location, place: Place<'tcx>, sd: AccessDepth, rw: ReadOrWrite, )341 fn check_access_for_conflict(
342 &mut self,
343 location: Location,
344 place: Place<'tcx>,
345 sd: AccessDepth,
346 rw: ReadOrWrite,
347 ) {
348 debug!(
349 "invalidation::check_access_for_conflict(location={:?}, place={:?}, sd={:?}, \
350 rw={:?})",
351 location, place, sd, rw,
352 );
353 let tcx = self.tcx;
354 let body = self.body;
355 let borrow_set = self.borrow_set;
356 let indices = self.borrow_set.indices();
357 each_borrow_involving_path(
358 self,
359 tcx,
360 body,
361 location,
362 (sd, place),
363 borrow_set,
364 indices,
365 |this, borrow_index, borrow| {
366 match (rw, borrow.kind) {
367 // Obviously an activation is compatible with its own
368 // reservation (or even prior activating uses of same
369 // borrow); so don't check if they interfere.
370 //
371 // NOTE: *reservations* do conflict with themselves;
372 // thus aren't injecting unsoundness w/ this check.)
373 (Activation(_, activating), _) if activating == borrow_index => {
374 // Activating a borrow doesn't generate any invalidations, since we
375 // have already taken the reservation
376 }
377
378 (Read(_), BorrowKind::Shallow | BorrowKind::Shared)
379 | (Read(ReadKind::Borrow(BorrowKind::Shallow)), BorrowKind::Mut { .. }) => {
380 // Reads don't invalidate shared or shallow borrows
381 }
382
383 (Read(_), BorrowKind::Mut { .. }) => {
384 // Reading from mere reservations of mutable-borrows is OK.
385 if !is_active(&this.dominators, borrow, location) {
386 // If the borrow isn't active yet, reads don't invalidate it
387 assert!(allow_two_phase_borrow(borrow.kind));
388 return Control::Continue;
389 }
390
391 // Unique and mutable borrows are invalidated by reads from any
392 // involved path
393 this.emit_loan_invalidated_at(borrow_index, location);
394 }
395
396 (Reservation(_) | Activation(_, _) | Write(_), _) => {
397 // unique or mutable borrows are invalidated by writes.
398 // Reservations count as writes since we need to check
399 // that activating the borrow will be OK
400 // FIXME(bob_twinkles) is this actually the right thing to do?
401 this.emit_loan_invalidated_at(borrow_index, location);
402 }
403 }
404 Control::Continue
405 },
406 );
407 }
408
409 /// Generates a new `loan_invalidated_at(L, B)` fact.
emit_loan_invalidated_at(&mut self, b: BorrowIndex, l: Location)410 fn emit_loan_invalidated_at(&mut self, b: BorrowIndex, l: Location) {
411 let lidx = self.location_table.start_index(l);
412 self.all_facts.loan_invalidated_at.push((lidx, b));
413 }
414
check_activations(&mut self, location: Location)415 fn check_activations(&mut self, location: Location) {
416 // Two-phase borrow support: For each activation that is newly
417 // generated at this statement, check if it interferes with
418 // another borrow.
419 for &borrow_index in self.borrow_set.activations_at_location(location) {
420 let borrow = &self.borrow_set[borrow_index];
421
422 // only mutable borrows should be 2-phase
423 assert!(match borrow.kind {
424 BorrowKind::Shared | BorrowKind::Shallow => false,
425 BorrowKind::Mut { .. } => true,
426 });
427
428 self.access_place(
429 location,
430 borrow.borrowed_place,
431 (Deep, Activation(WriteKind::MutableBorrow(borrow.kind), borrow_index)),
432 LocalMutationIsAllowed::No,
433 );
434
435 // We do not need to call `check_if_path_or_subpath_is_moved`
436 // again, as we already called it when we made the
437 // initial reservation.
438 }
439 }
440 }
441