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:
Matthias Krüger 2021-12-19 00:38:41 +01:00 committed by GitHub
commit 6b62bf3814
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 8 deletions

View File

@ -347,10 +347,6 @@ fn univariant_uninterned(
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();
if optimize {
let end =
@ -364,6 +360,10 @@ fn univariant_uninterned(
// the field ordering to try and catch some code making assumptions about layouts
// we don't guarantee
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
optimizing.shuffle(&mut rng);

View File

@ -1608,9 +1608,9 @@ pub struct ReprFlags: u8 {
// the seed stored in `ReprOptions.layout_seed`
const RANDOMIZE_LAYOUT = 1 << 5;
// Any of these flags being set prevent field reordering optimisation.
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits |
ReprFlags::IS_SIMD.bits |
ReprFlags::IS_LINEAR.bits;
const IS_UNOPTIMISABLE = ReprFlags::IS_C.bits
| ReprFlags::IS_SIMD.bits
| ReprFlags::IS_LINEAR.bits;
}
}
@ -1640,7 +1640,14 @@ pub fn new(tcx: TyCtxt<'_>, did: DefId) -> ReprOptions {
// Generate a deterministically-derived seed from the item's path hash
// 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 r in attr::find_repr_attrs(&tcx.sess, attr) {

View File

@ -1321,6 +1321,8 @@ mod parse {
"print some statistics about the query system (default: no)"),
randomize_layout: bool = (false, parse_bool, [TRACKED],
"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],
"whether ELF relocations can be relaxed"),
relro_level: Option<RelroLevel> = (None, parse_relro_level, [TRACKED],