Skip to content

Commit 6b62bf3

Browse files
authored
Rollup merge of #91932 - Kixiron:randomize-seed, r=nagisa
Add user seed to `-Z randomize-layout` Allows users of -`Z randomize-layout` to provide `-Z layout-seed=<seed>` in order to further randomizing type layout randomization. Extension of [compiler-team/#457](rust-lang/compiler-team#457), allows users to change struct layouts without changing code and hoping that item path hashes change, aiding in detecting layout errors
2 parents 8039087 + 2af02ab commit 6b62bf3

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

compiler/rustc_middle/src/ty/layout.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,6 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
347347

348348
let mut inverse_memory_index: Vec<u32> = (0..fields.len() as u32).collect();
349349

350-
// `ReprOptions.layout_seed` is a deterministic seed that we can use to
351-
// randomize field ordering with
352-
let mut rng = Xoshiro128StarStar::seed_from_u64(repr.field_shuffle_seed);
353-
354350
let optimize = !repr.inhibit_struct_field_reordering_opt();
355351
if optimize {
356352
let end =
@@ -364,6 +360,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
364360
// the field ordering to try and catch some code making assumptions about layouts
365361
// we don't guarantee
366362
if repr.can_randomize_type_layout() {
363+
// `ReprOptions.layout_seed` is a deterministic seed that we can use to
364+
// randomize field ordering with
365+
let mut rng = Xoshiro128StarStar::seed_from_u64(repr.field_shuffle_seed);
366+
367367
// Shuffle the ordering of the fields
368368
optimizing.shuffle(&mut rng);
369369

compiler/rustc_middle/src/ty/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -1608,9 +1608,9 @@ bitflags! {
16081608
// the seed stored in `ReprOptions.layout_seed`
16091609
const RANDOMIZE_LAYOUT = 1 << 5;
16101610
// Any of these flags being set prevent field reordering optimisation.
1611-
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits |
1612-
ReprFlags::IS_SIMD.bits |
1613-
ReprFlags::IS_LINEAR.bits;
1611+
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits
1612+
| ReprFlags::IS_SIMD.bits
1613+
| ReprFlags::IS_LINEAR.bits;
16141614
}
16151615
}
16161616

@@ -1640,7 +1640,14 @@ impl ReprOptions {
16401640

16411641
// Generate a deterministically-derived seed from the item's path hash
16421642
// to allow for cross-crate compilation to actually work
1643-
let field_shuffle_seed = tcx.def_path_hash(did).0.to_smaller_hash();
1643+
let mut field_shuffle_seed = tcx.def_path_hash(did).0.to_smaller_hash();
1644+
1645+
// If the user defined a custom seed for layout randomization, xor the item's
1646+
// path hash with the user defined seed, this will allowing determinism while
1647+
// still allowing users to further randomize layout generation for e.g. fuzzing
1648+
if let Some(user_seed) = tcx.sess.opts.debugging_opts.layout_seed {
1649+
field_shuffle_seed ^= user_seed;
1650+
}
16441651

16451652
for attr in tcx.get_attrs(did).iter() {
16461653
for r in attr::find_repr_attrs(&tcx.sess, attr) {

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,8 @@ options! {
13211321
"print some statistics about the query system (default: no)"),
13221322
randomize_layout: bool = (false, parse_bool, [TRACKED],
13231323
"randomize the layout of types (default: no)"),
1324+
layout_seed: Option<u64> = (None, parse_opt_number, [TRACKED],
1325+
"seed layout randomization"),
13241326
relax_elf_relocations: Option<bool> = (None, parse_opt_bool, [TRACKED],
13251327
"whether ELF relocations can be relaxed"),
13261328
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],

0 commit comments

Comments
 (0)