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](https://github.com/rust-lang/compiler-team/issues/457), allows users to change struct layouts without changing code and hoping that item path hashes change, aiding in detecting layout errors
This commit is contained in:
commit
6b62bf3814
3 changed files with 17 additions and 8 deletions
|
@ -347,10 +347,6 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
|
||||||
|
|
||||||
let mut inverse_memory_index: Vec<u32> = (0..fields.len() as u32).collect();
|
let mut inverse_memory_index: Vec<u32> = (0..fields.len() as u32).collect();
|
||||||
|
|
||||||
// `ReprOptions.layout_seed` is a deterministic seed that we can use to
|
|
||||||
// randomize field ordering with
|
|
||||||
let mut rng = Xoshiro128StarStar::seed_from_u64(repr.field_shuffle_seed);
|
|
||||||
|
|
||||||
let optimize = !repr.inhibit_struct_field_reordering_opt();
|
let optimize = !repr.inhibit_struct_field_reordering_opt();
|
||||||
if optimize {
|
if optimize {
|
||||||
let end =
|
let end =
|
||||||
|
@ -364,6 +360,10 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
|
||||||
// the field ordering to try and catch some code making assumptions about layouts
|
// the field ordering to try and catch some code making assumptions about layouts
|
||||||
// we don't guarantee
|
// we don't guarantee
|
||||||
if repr.can_randomize_type_layout() {
|
if repr.can_randomize_type_layout() {
|
||||||
|
// `ReprOptions.layout_seed` is a deterministic seed that we can use to
|
||||||
|
// randomize field ordering with
|
||||||
|
let mut rng = Xoshiro128StarStar::seed_from_u64(repr.field_shuffle_seed);
|
||||||
|
|
||||||
// Shuffle the ordering of the fields
|
// Shuffle the ordering of the fields
|
||||||
optimizing.shuffle(&mut rng);
|
optimizing.shuffle(&mut rng);
|
||||||
|
|
||||||
|
|
|
@ -1608,9 +1608,9 @@ bitflags! {
|
||||||
// the seed stored in `ReprOptions.layout_seed`
|
// the seed stored in `ReprOptions.layout_seed`
|
||||||
const RANDOMIZE_LAYOUT = 1 << 5;
|
const RANDOMIZE_LAYOUT = 1 << 5;
|
||||||
// Any of these flags being set prevent field reordering optimisation.
|
// Any of these flags being set prevent field reordering optimisation.
|
||||||
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits |
|
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits
|
||||||
ReprFlags::IS_SIMD.bits |
|
| ReprFlags::IS_SIMD.bits
|
||||||
ReprFlags::IS_LINEAR.bits;
|
| ReprFlags::IS_LINEAR.bits;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1640,7 +1640,14 @@ impl ReprOptions {
|
||||||
|
|
||||||
// Generate a deterministically-derived seed from the item's path hash
|
// Generate a deterministically-derived seed from the item's path hash
|
||||||
// to allow for cross-crate compilation to actually work
|
// to allow for cross-crate compilation to actually work
|
||||||
let field_shuffle_seed = tcx.def_path_hash(did).0.to_smaller_hash();
|
let mut field_shuffle_seed = tcx.def_path_hash(did).0.to_smaller_hash();
|
||||||
|
|
||||||
|
// If the user defined a custom seed for layout randomization, xor the item's
|
||||||
|
// path hash with the user defined seed, this will allowing determinism while
|
||||||
|
// still allowing users to further randomize layout generation for e.g. fuzzing
|
||||||
|
if let Some(user_seed) = tcx.sess.opts.debugging_opts.layout_seed {
|
||||||
|
field_shuffle_seed ^= user_seed;
|
||||||
|
}
|
||||||
|
|
||||||
for attr in tcx.get_attrs(did).iter() {
|
for attr in tcx.get_attrs(did).iter() {
|
||||||
for r in attr::find_repr_attrs(&tcx.sess, attr) {
|
for r in attr::find_repr_attrs(&tcx.sess, attr) {
|
||||||
|
|
|
@ -1321,6 +1321,8 @@ options! {
|
||||||
"print some statistics about the query system (default: no)"),
|
"print some statistics about the query system (default: no)"),
|
||||||
randomize_layout: bool = (false, parse_bool, [TRACKED],
|
randomize_layout: bool = (false, parse_bool, [TRACKED],
|
||||||
"randomize the layout of types (default: no)"),
|
"randomize the layout of types (default: no)"),
|
||||||
|
layout_seed: Option<u64> = (None, parse_opt_number, [TRACKED],
|
||||||
|
"seed layout randomization"),
|
||||||
relax_elf_relocations: Option<bool> = (None, parse_opt_bool, [TRACKED],
|
relax_elf_relocations: Option<bool> = (None, parse_opt_bool, [TRACKED],
|
||||||
"whether ELF relocations can be relaxed"),
|
"whether ELF relocations can be relaxed"),
|
||||||
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
|
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue