2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2021-03-28 10:50:20 +00:00
|
|
|
use rustc_target::abi::Align;
|
2017-10-03 14:01:01 +00:00
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` if this place is allowed to be less aligned
|
2017-10-03 14:01:01 +00:00
|
|
|
/// than its containing struct (because it is within a packed
|
|
|
|
/// struct).
|
2019-06-11 21:11:55 +00:00
|
|
|
pub fn is_disaligned<'tcx, L>(
|
2019-06-13 21:48:52 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-11 21:11:55 +00:00
|
|
|
local_decls: &L,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2020-03-31 15:19:29 +00:00
|
|
|
place: Place<'tcx>,
|
2019-06-11 21:11:55 +00:00
|
|
|
) -> bool
|
|
|
|
where
|
|
|
|
L: HasLocalDecls<'tcx>,
|
2017-10-03 14:01:01 +00:00
|
|
|
{
|
2017-12-01 12:39:51 +00:00
|
|
|
debug!("is_disaligned({:?})", place);
|
2022-02-18 23:47:43 +00:00
|
|
|
let Some(pack) = is_within_packed(tcx, local_decls, place) else {
|
|
|
|
debug!("is_disaligned({:?}) - not within packed", place);
|
|
|
|
return false;
|
2021-03-28 10:50:20 +00:00
|
|
|
};
|
2017-10-03 14:01:01 +00:00
|
|
|
|
2019-03-29 02:08:31 +00:00
|
|
|
let ty = place.ty(local_decls, tcx).ty;
|
2021-08-24 18:29:30 +00:00
|
|
|
match tcx.layout_of(param_env.and(ty)) {
|
2021-03-28 10:50:20 +00:00
|
|
|
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()
|
|
|
|
);
|
2017-10-03 14:01:01 +00:00
|
|
|
false
|
|
|
|
}
|
|
|
|
_ => {
|
2017-12-01 12:39:51 +00:00
|
|
|
debug!("is_disaligned({:?}) - true", place);
|
2017-10-03 14:01:01 +00:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 10:50:20 +00:00
|
|
|
fn is_within_packed<'tcx, L>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
local_decls: &L,
|
|
|
|
place: Place<'tcx>,
|
|
|
|
) -> Option<Align>
|
2019-06-11 21:11:55 +00:00
|
|
|
where
|
|
|
|
L: HasLocalDecls<'tcx>,
|
2017-10-03 14:01:01 +00:00
|
|
|
{
|
2021-01-10 05:33:38 +00:00
|
|
|
for (place_base, elem) in place.iter_projections().rev() {
|
2019-07-29 22:07:28 +00:00
|
|
|
match elem {
|
2017-10-03 14:01:01 +00:00
|
|
|
// encountered a Deref, which is ABI-aligned
|
|
|
|
ProjectionElem::Deref => break,
|
|
|
|
ProjectionElem::Field(..) => {
|
2021-01-10 05:33:38 +00:00
|
|
|
let ty = place_base.ty(local_decls, tcx).ty;
|
2020-08-02 22:49:11 +00:00
|
|
|
match ty.kind() {
|
2022-03-04 20:28:41 +00:00
|
|
|
ty::Adt(def, _) => return def.repr().pack,
|
2017-10-03 14:01:01 +00:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 10:50:20 +00:00
|
|
|
None
|
2017-10-03 14:01:01 +00:00
|
|
|
}
|