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
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
use rustc_target::abi::Align;

/// Returns `true` if this place is allowed to be less aligned
/// than its containing struct (because it is within a packed
/// struct).
pub fn is_disaligned<'tcx, L>(
    tcx: TyCtxt<'tcx>,
    local_decls: &L,
    param_env: ty::ParamEnv<'tcx>,
    place: Place<'tcx>,
) -> bool
where
    L: HasLocalDecls<'tcx>,
{
    debug!("is_disaligned({:?})", place);
    let pack = match is_within_packed(tcx, local_decls, place) {
        None => {
            debug!("is_disaligned({:?}) - not within packed", place);
            return false;
        }
        Some(pack) => pack,
    };

    let ty = place.ty(local_decls, tcx).ty;
    match tcx.layout_of(param_env.and(ty)) {
        Ok(layout) if layout.align.abi <= pack => {
            // If the packed alignment is greater or equal to the field alignment, the type won't be
            // further disaligned.
            debug!(
                "is_disaligned({:?}) - align = {}, packed = {}; not disaligned",
                place,
                layout.align.abi.bytes(),
                pack.bytes()
            );
            false
        }
        _ => {
            debug!("is_disaligned({:?}) - true", place);
            true
        }
    }
}

fn is_within_packed<'tcx, L>(
    tcx: TyCtxt<'tcx>,
    local_decls: &L,
    place: Place<'tcx>,
) -> Option<Align>
where
    L: HasLocalDecls<'tcx>,
{
    for (place_base, elem) in place.iter_projections().rev() {
        match elem {
            // encountered a Deref, which is ABI-aligned
            ProjectionElem::Deref => break,
            ProjectionElem::Field(..) => {
                let ty = place_base.ty(local_decls, tcx).ty;
                match ty.kind() {
                    ty::Adt(def, _) => return def.repr.pack,
                    _ => {}
                }
            }
            _ => {}
        }
    }

    None
}