]> git.lizzy.rs Git - rust.git/commitdiff
Added -Z layout_seed for allowing user-defined randomization seeds
authorChase Wilson <me@chasewilson.dev>
Tue, 14 Dec 2021 18:48:41 +0000 (12:48 -0600)
committerChase Wilson <me@chasewilson.dev>
Tue, 14 Dec 2021 18:48:41 +0000 (12:48 -0600)
compiler/rustc_middle/src/ty/mod.rs
compiler/rustc_session/src/options.rs

index d0d2a46fc4c1d6131378cb8a9b5091aa5847c351..529e8478371734d006f441e51307ff3290e86d7a 100644 (file)
@@ -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) {
index bd7b1639613eb7963a776fcb38b2e779cf95cb74..32fabb6be4bfda7225282023bf6f5f1740735056 100644 (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],