Skip to content

Commit 37d418a

Browse files
committed
Add rust.randomize-layout config to build artifacts with -Zrandomize-layout
Additionally teach compiletest to ignore tests that rely on deterministic layout. Tests themselves aren't built with randomization but they can still observe slight changes in std or rustc
1 parent 450e99f commit 37d418a

File tree

7 files changed

+24
-0
lines changed

7 files changed

+24
-0
lines changed

config.toml.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ changelog-seen = 2
434434
# set this value to `true`.
435435
#debug-logging = rust.debug-assertions (boolean)
436436

437+
# Whether or not to build rustc, tools and the libraries with randomized type layout
438+
#
439+
# Defaults to rust.debug value
440+
#randomize-layout = rust.debug (boolean)
441+
437442
# Whether or not overflow checks are enabled for the compiler and standard
438443
# library.
439444
#

src/bootstrap/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,10 @@ impl<'a> Builder<'a> {
14711471
rustflags.arg("-Zunstable-options");
14721472
}
14731473

1474+
if self.config.rust_randomize_layout {
1475+
rustflags.arg("-Zrandomize-layout");
1476+
}
1477+
14741478
// Enable cfg checking of cargo features for everything but std and also enable cfg
14751479
// checking of names and values.
14761480
//

src/bootstrap/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ pub struct Config {
136136
pub rust_codegen_units_std: Option<u32>,
137137
pub rust_debug_assertions: bool,
138138
pub rust_debug_assertions_std: bool,
139+
pub rust_randomize_layout: bool,
139140
pub rust_overflow_checks: bool,
140141
pub rust_overflow_checks_std: bool,
141142
pub rust_debug_logging: bool,
@@ -678,6 +679,7 @@ define_config! {
678679
codegen_units_std: Option<u32> = "codegen-units-std",
679680
debug_assertions: Option<bool> = "debug-assertions",
680681
debug_assertions_std: Option<bool> = "debug-assertions-std",
682+
randomize_layout: Option<bool> = "randomize-layout",
681683
overflow_checks: Option<bool> = "overflow-checks",
682684
overflow_checks_std: Option<bool> = "overflow-checks-std",
683685
debug_logging: Option<bool> = "debug-logging",
@@ -940,6 +942,7 @@ impl Config {
940942
let mut debug = None;
941943
let mut debug_assertions = None;
942944
let mut debug_assertions_std = None;
945+
let mut randomize_layout = None;
943946
let mut overflow_checks = None;
944947
let mut overflow_checks_std = None;
945948
let mut debug_logging = None;
@@ -1039,6 +1042,7 @@ impl Config {
10391042
debug = rust.debug;
10401043
debug_assertions = rust.debug_assertions;
10411044
debug_assertions_std = rust.debug_assertions_std;
1045+
randomize_layout = rust.randomize_layout;
10421046
overflow_checks = rust.overflow_checks;
10431047
overflow_checks_std = rust.overflow_checks_std;
10441048
debug_logging = rust.debug_logging;
@@ -1192,6 +1196,7 @@ impl Config {
11921196
config.rust_debug_assertions_std =
11931197
debug_assertions_std.unwrap_or(config.rust_debug_assertions);
11941198
config.rust_overflow_checks = overflow_checks.unwrap_or(default);
1199+
config.rust_randomize_layout = randomize_layout.unwrap_or(default);
11951200
config.rust_overflow_checks_std =
11961201
overflow_checks_std.unwrap_or(config.rust_overflow_checks);
11971202

src/bootstrap/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,9 @@ note: if you're sure you want to do this, please open an issue as to why. In the
14431443
if builder.config.rust_optimize_tests {
14441444
cmd.arg("--optimize-tests");
14451445
}
1446+
if builder.config.rust_randomize_layout {
1447+
cmd.arg("--rust-randomized-layout");
1448+
}
14461449
let mut flags = if is_rustdoc { Vec::new() } else { vec!["-Crpath".to_string()] };
14471450
flags.push(format!("-Cdebuginfo={}", builder.config.rust_debuginfo_level_tests));
14481451
flags.push(builder.config.cmd.rustc_args().join(" "));

src/tools/compiletest/src/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ pub struct Config {
271271
/// Flags to pass to the compiler when building for the target
272272
pub target_rustcflags: Option<String>,
273273

274+
/// Whether the compiler and stdlib has been built with randomized struct layouts
275+
pub rust_randomized_layout: bool,
276+
274277
/// Whether tests should be optimized by default. Individual test-suites and test files may
275278
/// override this setting.
276279
pub optimize_tests: bool,

src/tools/compiletest/src/header.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,8 @@ pub fn make_test_description<R: Read>(
935935
ignore |= !has_tsan && config.parse_name_directive(ln, "needs-sanitizer-thread");
936936
ignore |= !has_hwasan && config.parse_name_directive(ln, "needs-sanitizer-hwaddress");
937937
ignore |= !has_memtag && config.parse_name_directive(ln, "needs-sanitizer-memtag");
938+
ignore |= config.rust_randomized_layout
939+
&& config.parse_name_directive(ln, "needs-deterministic-layouts");
938940
ignore |= !has_shadow_call_stack
939941
&& config.parse_name_directive(ln, "needs-sanitizer-shadow-call-stack");
940942
ignore |= config.target_panic == PanicStrategy::Abort

src/tools/compiletest/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
103103
)
104104
.optmulti("", "host-rustcflags", "flags to pass to rustc for host", "FLAGS")
105105
.optmulti("", "target-rustcflags", "flags to pass to rustc for target", "FLAGS")
106+
.optflag("", "rust-randomized-layout", "set this when rustc/stdlib were compiled with randomized layouts")
106107
.optflag("", "optimize-tests", "run tests with optimizations enabled")
107108
.optopt("", "target-panic", "what panic strategy the target supports", "unwind | abort")
108109
.optflag("", "verbose", "run tests verbosely, showing all output")
@@ -255,6 +256,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
255256
runtool: matches.opt_str("runtool"),
256257
host_rustcflags: Some(matches.opt_strs("host-rustcflags").join(" ")),
257258
target_rustcflags: Some(matches.opt_strs("target-rustcflags").join(" ")),
259+
rust_randomized_layout: matches.opt_present("rust-randomized-layout"),
258260
optimize_tests: matches.opt_present("optimize-tests"),
259261
target_panic: match matches.opt_str("target-panic").as_deref() {
260262
Some("unwind") | None => PanicStrategy::Unwind,

0 commit comments

Comments
 (0)