2024-11-02 19:32:52 -07:00
|
|
|
use rustc_abi::Align;
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2024-05-22 14:20:23 +10:00
|
|
|
use tracing::debug;
|
2017-10-03 16:01:01 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Returns `true` if this place is allowed to be less aligned
|
2017-10-03 16:01:01 +02:00
|
|
|
/// than its containing struct (because it is within a packed
|
|
|
|
|
/// struct).
|
2019-06-12 00:11:55 +03:00
|
|
|
pub fn is_disaligned<'tcx, L>(
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
local_decls: &L,
|
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
2020-03-31 12:19:29 -03:00
|
|
|
place: Place<'tcx>,
|
2019-06-12 00:11:55 +03:00
|
|
|
) -> bool
|
|
|
|
|
where
|
|
|
|
|
L: HasLocalDecls<'tcx>,
|
2017-10-03 16:01:01 +02:00
|
|
|
{
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("is_disaligned({:?})", place);
|
2022-02-19 00:47:43 +01:00
|
|
|
let Some(pack) = is_within_packed(tcx, local_decls, place) else {
|
|
|
|
|
debug!("is_disaligned({:?}) - not within packed", place);
|
|
|
|
|
return false;
|
2021-03-28 12:50:20 +02:00
|
|
|
};
|
2017-10-03 16:01:01 +02:00
|
|
|
|
2019-03-28 19:08:31 -07:00
|
|
|
let ty = place.ty(local_decls, tcx).ty;
|
2024-08-08 11:56:12 -04:00
|
|
|
let unsized_tail = || tcx.struct_tail_for_codegen(ty, param_env);
|
2021-08-24 21:29:30 +03:00
|
|
|
match tcx.layout_of(param_env.and(ty)) {
|
2023-09-05 23:06:35 +02:00
|
|
|
Ok(layout)
|
|
|
|
|
if layout.align.abi <= pack
|
|
|
|
|
&& (layout.is_sized()
|
|
|
|
|
|| matches!(unsized_tail().kind(), ty::Slice(..) | ty::Str)) =>
|
|
|
|
|
{
|
2021-03-28 12:50:20 +02:00
|
|
|
// If the packed alignment is greater or equal to the field alignment, the type won't be
|
|
|
|
|
// further disaligned.
|
2023-09-05 22:29:51 +02:00
|
|
|
// However we need to ensure the field is sized; for unsized fields, `layout.align` is
|
2023-09-05 23:06:35 +02:00
|
|
|
// just an approximation -- except when the unsized tail is a slice, where the alignment
|
|
|
|
|
// is fully determined by the type.
|
2021-03-28 12:50:20 +02:00
|
|
|
debug!(
|
|
|
|
|
"is_disaligned({:?}) - align = {}, packed = {}; not disaligned",
|
|
|
|
|
place,
|
|
|
|
|
layout.align.abi.bytes(),
|
|
|
|
|
pack.bytes()
|
|
|
|
|
);
|
2017-10-03 16:01:01 +02:00
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
2023-08-24 11:34:23 +02:00
|
|
|
// We cannot figure out the layout. Conservatively assume that this is disaligned.
|
2017-12-01 14:39:51 +02:00
|
|
|
debug!("is_disaligned({:?}) - true", place);
|
2017-10-03 16:01:01 +02:00
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-24 11:34:23 +02:00
|
|
|
pub fn is_within_packed<'tcx, L>(
|
2021-03-28 12:50:20 +02:00
|
|
|
tcx: TyCtxt<'tcx>,
|
|
|
|
|
local_decls: &L,
|
|
|
|
|
place: Place<'tcx>,
|
|
|
|
|
) -> Option<Align>
|
2019-06-12 00:11:55 +03:00
|
|
|
where
|
|
|
|
|
L: HasLocalDecls<'tcx>,
|
2017-10-03 16:01:01 +02:00
|
|
|
{
|
2022-08-02 12:21:38 -04:00
|
|
|
place
|
|
|
|
|
.iter_projections()
|
|
|
|
|
.rev()
|
|
|
|
|
// Stop at `Deref`; standard ABI alignment applies there.
|
|
|
|
|
.take_while(|(_base, elem)| !matches!(elem, ProjectionElem::Deref))
|
|
|
|
|
// Consider the packed alignments at play here...
|
|
|
|
|
.filter_map(|(base, _elem)| {
|
|
|
|
|
base.ty(local_decls, tcx).ty.ty_adt_def().and_then(|adt| adt.repr().pack)
|
|
|
|
|
})
|
|
|
|
|
// ... and compute their minimum.
|
|
|
|
|
// The overall smallest alignment is what matters.
|
|
|
|
|
.min()
|
2017-10-03 16:01:01 +02:00
|
|
|
}
|