Skip to content

Commit df20808

Browse files
committed
inhibit layout randomization for Box
1 parent 1e377c5 commit df20808

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

compiler/rustc_abi/src/layout.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -968,8 +968,8 @@ fn univariant<
968968
let mut align = if pack.is_some() { dl.i8_align } else { dl.aggregate_align };
969969
let mut max_repr_align = repr.align;
970970
let mut inverse_memory_index: IndexVec<u32, FieldIdx> = fields.indices().collect();
971-
let optimize = !repr.inhibit_struct_field_reordering();
972-
if optimize && fields.len() > 1 {
971+
let optimize_field_order = !repr.inhibit_struct_field_reordering();
972+
if optimize_field_order && fields.len() > 1 {
973973
let end = if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
974974
let optimizing = &mut inverse_memory_index.raw[..end];
975975
let fields_excluding_tail = &fields.raw[..end];
@@ -1176,7 +1176,7 @@ fn univariant<
11761176
// If field 5 has offset 0, offsets[0] is 5, and memory_index[5] should be 0.
11771177
// Field 5 would be the first element, so memory_index is i:
11781178
// Note: if we didn't optimize, it's already right.
1179-
let memory_index = if optimize {
1179+
let memory_index = if optimize_field_order {
11801180
inverse_memory_index.invert_bijective_mapping()
11811181
} else {
11821182
debug_assert!(inverse_memory_index.iter().copied().eq(fields.indices()));
@@ -1189,6 +1189,9 @@ fn univariant<
11891189
}
11901190
let mut layout_of_single_non_zst_field = None;
11911191
let mut abi = Abi::Aggregate { sized };
1192+
1193+
let optimize_abi = !repr.inhibit_newtype_abi_optimization();
1194+
11921195
// Try to make this a Scalar/ScalarPair.
11931196
if sized && size.bytes() > 0 {
11941197
// We skip *all* ZST here and later check if we are good in terms of alignment.
@@ -1205,7 +1208,7 @@ fn univariant<
12051208
match field.abi {
12061209
// For plain scalars, or vectors of them, we can't unpack
12071210
// newtypes for `#[repr(C)]`, as that affects C ABIs.
1208-
Abi::Scalar(_) | Abi::Vector { .. } if optimize => {
1211+
Abi::Scalar(_) | Abi::Vector { .. } if optimize_abi => {
12091212
abi = field.abi;
12101213
}
12111214
// But scalar pairs are Rust-specific and get

compiler/rustc_abi/src/lib.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,17 @@ bitflags! {
4343
const IS_SIMD = 1 << 1;
4444
const IS_TRANSPARENT = 1 << 2;
4545
// Internal only for now. If true, don't reorder fields.
46+
// On its own it does not prevent ABI optimizations.
4647
const IS_LINEAR = 1 << 3;
47-
// If true, the type's layout can be randomized using
48-
// the seed stored in `ReprOptions.field_shuffle_seed`
48+
// If true, the type's crate has opted into layout randomization.
49+
// Other flags can still inhibit reordering and thus randomization.
50+
// The seed stored in `ReprOptions.field_shuffle_seed`.
4951
const RANDOMIZE_LAYOUT = 1 << 4;
5052
// Any of these flags being set prevent field reordering optimisation.
51-
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits()
53+
const FIELD_ORDER_UNOPTIMIZABLE = ReprFlags::IS_C.bits()
5254
| ReprFlags::IS_SIMD.bits()
5355
| ReprFlags::IS_LINEAR.bits();
56+
const ABI_UNOPTIMIZABLE = ReprFlags::IS_C.bits() | ReprFlags::IS_SIMD.bits();
5457
}
5558
}
5659

@@ -139,10 +142,14 @@ impl ReprOptions {
139142
self.c() || self.int.is_some()
140143
}
141144

145+
pub fn inhibit_newtype_abi_optimization(&self) -> bool {
146+
self.flags.intersects(ReprFlags::ABI_UNOPTIMIZABLE)
147+
}
148+
142149
/// Returns `true` if this `#[repr()]` guarantees a fixed field order,
143150
/// e.g. `repr(C)` or `repr(<int>)`.
144151
pub fn inhibit_struct_field_reordering(&self) -> bool {
145-
self.flags.intersects(ReprFlags::IS_UNOPTIMISABLE) || self.int.is_some()
152+
self.flags.intersects(ReprFlags::FIELD_ORDER_UNOPTIMIZABLE) || self.int.is_some()
146153
}
147154

148155
/// Returns `true` if this type is valid for reordering and `-Z randomize-layout`

compiler/rustc_middle/src/ty/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
3535
use rustc_errors::{Diag, ErrorGuaranteed, StashKey};
3636
use rustc_hir::def::{CtorKind, CtorOf, DefKind, DocLinkResMap, LifetimeRes, Res};
3737
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LocalDefIdMap};
38+
use rustc_hir::LangItem;
3839
use rustc_index::IndexVec;
3940
use rustc_macros::{
4041
extension, Decodable, Encodable, HashStable, TyDecodable, TyEncodable, TypeFoldable,
@@ -1570,8 +1571,15 @@ impl<'tcx> TyCtxt<'tcx> {
15701571
flags.insert(ReprFlags::RANDOMIZE_LAYOUT);
15711572
}
15721573

1574+
// box is special, on the one hand the compiler assumes an ordered layout, with the pointer
1575+
// always at offset zero. On the other hand we want scalar abi optimizations.
1576+
let is_box = self.is_lang_item(did.to_def_id(), LangItem::OwnedBox);
1577+
15731578
// This is here instead of layout because the choice must make it into metadata.
1574-
if !self.consider_optimizing(|| format!("Reorder fields of {:?}", self.def_path_str(did))) {
1579+
if is_box
1580+
|| !self
1581+
.consider_optimizing(|| format!("Reorder fields of {:?}", self.def_path_str(did)))
1582+
{
15751583
flags.insert(ReprFlags::IS_LINEAR);
15761584
}
15771585

0 commit comments

Comments
 (0)