|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use rustc::hir::def_id::DefId; |
| 12 | +use rustc::mir::*; |
| 13 | +use rustc::ty::TyCtxt; |
| 14 | + |
| 15 | +use transform::{MirPass, MirSource}; |
| 16 | +use util::patch::MirPatch; |
| 17 | +use util; |
| 18 | + |
| 19 | +// This pass moves values being dropped that are within a packed |
| 20 | +// struct to a separate local before dropping them, to ensure that |
| 21 | +// they are dropped from an aligned address. |
| 22 | +// |
| 23 | +// For example, if we have something like |
| 24 | +// ```Rust |
| 25 | +// #[repr(packed)] |
| 26 | +// struct Foo { |
| 27 | +// dealign: u8, |
| 28 | +// data: Vec<u8> |
| 29 | +// } |
| 30 | +// |
| 31 | +// let foo = ...; |
| 32 | +// ``` |
| 33 | +// |
| 34 | +// We want to call `drop_in_place::<Vec<u8>>` on `data` from an aligned |
| 35 | +// address. This means we can't simply drop `foo.data` directly, because |
| 36 | +// its address is not aligned. |
| 37 | +// |
| 38 | +// Instead, we move `foo.data` to a local and drop that: |
| 39 | +// ``` |
| 40 | +// storage.live(drop_temp) |
| 41 | +// drop_temp = foo.data; |
| 42 | +// drop(drop_temp) -> next |
| 43 | +// next: |
| 44 | +// storage.dead(drop_temp) |
| 45 | +// ``` |
| 46 | +// |
| 47 | +// The storage instructions are required to avoid stack space |
| 48 | +// blowup. |
| 49 | + |
| 50 | +pub struct AddMovesForPackedDrops; |
| 51 | + |
| 52 | +impl MirPass for AddMovesForPackedDrops { |
| 53 | + fn run_pass<'a, 'tcx>(&self, |
| 54 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 55 | + src: MirSource, |
| 56 | + mir: &mut Mir<'tcx>) |
| 57 | + { |
| 58 | + debug!("add_moves_for_packed_drops({:?} @ {:?})", src, mir.span); |
| 59 | + add_moves_for_packed_drops(tcx, mir, src.def_id); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +pub fn add_moves_for_packed_drops<'a, 'tcx>( |
| 64 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 65 | + mir: &mut Mir<'tcx>, |
| 66 | + def_id: DefId) |
| 67 | +{ |
| 68 | + let patch = add_moves_for_packed_drops_patch(tcx, mir, def_id); |
| 69 | + patch.apply(mir); |
| 70 | +} |
| 71 | + |
| 72 | +fn add_moves_for_packed_drops_patch<'a, 'tcx>( |
| 73 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 74 | + mir: &Mir<'tcx>, |
| 75 | + def_id: DefId) |
| 76 | + -> MirPatch<'tcx> |
| 77 | +{ |
| 78 | + let mut patch = MirPatch::new(mir); |
| 79 | + let param_env = tcx.param_env(def_id); |
| 80 | + |
| 81 | + for (bb, data) in mir.basic_blocks().iter_enumerated() { |
| 82 | + let loc = Location { block: bb, statement_index: data.statements.len() }; |
| 83 | + let terminator = data.terminator(); |
| 84 | + |
| 85 | + match terminator.kind { |
| 86 | + TerminatorKind::Drop { ref location, .. } |
| 87 | + if util::is_disaligned(tcx, mir, param_env, location) => |
| 88 | + { |
| 89 | + add_move_for_packed_drop(tcx, mir, &mut patch, terminator, |
| 90 | + loc, data.is_cleanup); |
| 91 | + } |
| 92 | + TerminatorKind::DropAndReplace { .. } => { |
| 93 | + span_bug!(terminator.source_info.span, |
| 94 | + "replace in AddMovesForPackedDrops"); |
| 95 | + } |
| 96 | + _ => {} |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + patch |
| 101 | +} |
| 102 | + |
| 103 | +fn add_move_for_packed_drop<'a, 'tcx>( |
| 104 | + tcx: TyCtxt<'a, 'tcx, 'tcx>, |
| 105 | + mir: &Mir<'tcx>, |
| 106 | + patch: &mut MirPatch<'tcx>, |
| 107 | + terminator: &Terminator<'tcx>, |
| 108 | + loc: Location, |
| 109 | + is_cleanup: bool) |
| 110 | +{ |
| 111 | + debug!("add_move_for_packed_drop({:?} @ {:?})", terminator, loc); |
| 112 | + let (location, target, unwind) = match terminator.kind { |
| 113 | + TerminatorKind::Drop { ref location, target, unwind } => |
| 114 | + (location, target, unwind), |
| 115 | + _ => unreachable!() |
| 116 | + }; |
| 117 | + |
| 118 | + let source_info = terminator.source_info; |
| 119 | + let ty = location.ty(mir, tcx).to_ty(tcx); |
| 120 | + let temp = patch.new_temp(ty, terminator.source_info.span); |
| 121 | + |
| 122 | + let storage_dead_block = patch.new_block(BasicBlockData { |
| 123 | + statements: vec![Statement { |
| 124 | + source_info, kind: StatementKind::StorageDead(temp) |
| 125 | + }], |
| 126 | + terminator: Some(Terminator { |
| 127 | + source_info, kind: TerminatorKind::Goto { target } |
| 128 | + }), |
| 129 | + is_cleanup |
| 130 | + }); |
| 131 | + |
| 132 | + patch.add_statement( |
| 133 | + loc, StatementKind::StorageLive(temp)); |
| 134 | + patch.add_assign(loc, Lvalue::Local(temp), |
| 135 | + Rvalue::Use(Operand::Consume(location.clone()))); |
| 136 | + patch.patch_terminator(loc.block, TerminatorKind::Drop { |
| 137 | + location: Lvalue::Local(temp), |
| 138 | + target: storage_dead_block, |
| 139 | + unwind |
| 140 | + }); |
| 141 | +} |
0 commit comments