Skip to content

Commit 119ccc4

Browse files
committed
exclude unsizable tail from randomization seed calculation
1 parent 74a1582 commit 119ccc4

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

compiler/rustc_abi/src/layout.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -1050,15 +1050,15 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
10501050
let mut align = if pack.is_some() { dl.i8_align } else { dl.aggregate_align };
10511051
let mut max_repr_align = repr.align;
10521052
let mut inverse_memory_index: IndexVec<u32, FieldIdx> = fields.indices().collect();
1053-
let field_seed =
1054-
fields.raw.iter().fold(0u64, |acc, f| acc.wrapping_add(f.randomization_seed));
10551053
let optimize_field_order = !repr.inhibit_struct_field_reordering();
1056-
if optimize_field_order && fields.len() > 1 {
1057-
let end =
1058-
if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
1059-
let optimizing = &mut inverse_memory_index.raw[..end];
1060-
let fields_excluding_tail = &fields.raw[..end];
1054+
let end = if let StructKind::MaybeUnsized = kind { fields.len() - 1 } else { fields.len() };
1055+
let optimizing = &mut inverse_memory_index.raw[..end];
1056+
let fields_excluding_tail = &fields.raw[..end];
1057+
let field_seed = fields_excluding_tail
1058+
.iter()
1059+
.fold(0u64, |acc, f| acc.wrapping_add(f.randomization_seed));
10611060

1061+
if optimize_field_order && fields.len() > 1 {
10621062
// If `-Z randomize-layout` was enabled for the type definition we can shuffle
10631063
// the field ordering to try and catch some code making assumptions about layouts
10641064
// we don't guarantee.
@@ -1370,6 +1370,7 @@ impl<Cx: HasDataLayout> LayoutCalculator<Cx> {
13701370
};
13711371

13721372
// a transparent struct only has a single field, so its seed should be the same as the one we pass forward
1373+
// if the field is also unsizable then we pass zero, which is the identity-element wrapping-add used for seed mixing
13731374
let seed = if repr.transparent() {
13741375
field_seed
13751376
} else {

0 commit comments

Comments
 (0)