Skip to content

Commit 121e9f4

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 9649706 commit 121e9f4

File tree

8 files changed

+32
-0
lines changed

8 files changed

+32
-0
lines changed

config.example.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,9 @@
512512
# are disabled statically" because `max_level_info` is enabled, set this value to `true`.
513513
#debug-logging = rust.debug-assertions (boolean)
514514

515+
# Whether or not to build rustc, tools and the libraries with randomized type layout
516+
#randomize-layout = false
517+
515518
# Whether or not overflow checks are enabled for the compiler and standard
516519
# library.
517520
#

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,9 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
18101810
if builder.config.rust_optimize_tests {
18111811
cmd.arg("--optimize-tests");
18121812
}
1813+
if builder.config.rust_randomize_layout {
1814+
cmd.arg("--rust-randomized-layout");
1815+
}
18131816
if builder.config.cmd.only_modified() {
18141817
cmd.arg("--only-modified");
18151818
}

src/bootstrap/src/core/builder.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,10 @@ impl<'a> Builder<'a> {
16141614
rustflags.arg("-Csymbol-mangling-version=legacy");
16151615
}
16161616

1617+
if self.config.rust_randomize_layout {
1618+
rustflags.arg("-Zrandomize-layout");
1619+
}
1620+
16171621
// Enable compile-time checking of `cfg` names, values and Cargo `features`.
16181622
//
16191623
// Note: `std`, `alloc` and `core` imports some dependencies by #[path] (like

src/bootstrap/src/core/config/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ pub struct Config {
280280
pub rust_codegen_backends: Vec<String>,
281281
pub rust_verify_llvm_ir: bool,
282282
pub rust_thin_lto_import_instr_limit: Option<u32>,
283+
pub rust_randomize_layout: bool,
283284
pub rust_remap_debuginfo: bool,
284285
pub rust_new_symbol_mangling: Option<bool>,
285286
pub rust_profile_use: Option<String>,
@@ -1088,6 +1089,7 @@ define_config! {
10881089
codegen_units: Option<u32> = "codegen-units",
10891090
codegen_units_std: Option<u32> = "codegen-units-std",
10901091
debug_assertions: Option<bool> = "debug-assertions",
1092+
randomize_layout: Option<bool> = "randomize-layout",
10911093
debug_assertions_std: Option<bool> = "debug-assertions-std",
10921094
overflow_checks: Option<bool> = "overflow-checks",
10931095
overflow_checks_std: Option<bool> = "overflow-checks-std",
@@ -1179,6 +1181,7 @@ impl Config {
11791181
backtrace: true,
11801182
rust_optimize: RustOptimize::Bool(true),
11811183
rust_optimize_tests: true,
1184+
rust_randomize_layout: false,
11821185
submodules: None,
11831186
docs: true,
11841187
docs_minification: true,
@@ -1629,6 +1632,7 @@ impl Config {
16291632
backtrace,
16301633
incremental,
16311634
parallel_compiler,
1635+
randomize_layout,
16321636
default_linker,
16331637
channel,
16341638
description,
@@ -1718,6 +1722,7 @@ impl Config {
17181722
set(&mut config.lld_mode, lld_mode);
17191723
set(&mut config.llvm_bitcode_linker_enabled, llvm_bitcode_linker);
17201724

1725+
config.rust_randomize_layout = randomize_layout.unwrap_or_default();
17211726
config.llvm_tools_enabled = llvm_tools.unwrap_or(true);
17221727
config.rustc_parallel =
17231728
parallel_compiler.unwrap_or(config.channel == "dev" || config.channel == "nightly");
@@ -2889,6 +2894,7 @@ fn check_incompatible_options_for_ci_rustc(
28892894
let Rust {
28902895
// Following options are the CI rustc incompatible ones.
28912896
optimize,
2897+
randomize_layout,
28922898
debug_logging,
28932899
debuginfo_level_rustc,
28942900
llvm_tools,
@@ -2953,6 +2959,7 @@ fn check_incompatible_options_for_ci_rustc(
29532959
// otherwise, we just print a warning with `warn` macro.
29542960

29552961
err!(current_rust_config.optimize, optimize);
2962+
err!(current_rust_config.randomize_layout, randomize_layout);
29562963
err!(current_rust_config.debug_logging, debug_logging);
29572964
err!(current_rust_config.debuginfo_level_rustc, debuginfo_level_rustc);
29582965
err!(current_rust_config.rpath, rpath);

src/tools/compiletest/src/command-list.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ const KNOWN_DIRECTIVE_NAMES: &[&str] = &[
136136
"min-llvm-version",
137137
"min-system-llvm-version",
138138
"needs-asm-support",
139+
"needs-deterministic-layouts",
139140
"needs-dlltool",
140141
"needs-dynamic-linking",
141142
"needs-force-clang-based-tests",

src/tools/compiletest/src/common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ pub struct Config {
274274
/// Flags to pass to the compiler when building for the target
275275
pub target_rustcflags: Vec<String>,
276276

277+
/// Whether the compiler and stdlib has been built with randomized struct layouts
278+
pub rust_randomized_layout: bool,
279+
277280
/// Whether tests should be optimized by default. Individual test-suites and test files may
278281
/// override this setting.
279282
pub optimize_tests: bool,

src/tools/compiletest/src/header/needs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ pub(super) fn handle_needs(
134134
condition: config.target_cfg().relocation_model == "pic",
135135
ignore_reason: "ignored on targets without PIC relocation model",
136136
},
137+
Need {
138+
name: "needs-deterministic-layouts",
139+
condition: !config.rust_randomized_layout,
140+
ignore_reason: "ignored when randomizing layouts",
141+
},
137142
Need {
138143
name: "needs-wasmtime",
139144
condition: config.runner.as_ref().is_some_and(|r| r.contains("wasmtime")),

src/tools/compiletest/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ pub fn parse_config(args: Vec<String>) -> Config {
9999
)
100100
.optmulti("", "host-rustcflags", "flags to pass to rustc for host", "FLAGS")
101101
.optmulti("", "target-rustcflags", "flags to pass to rustc for target", "FLAGS")
102+
.optflag(
103+
"",
104+
"rust-randomized-layout",
105+
"set this when rustc/stdlib were compiled with randomized layouts",
106+
)
102107
.optflag("", "optimize-tests", "run tests with optimizations enabled")
103108
.optflag("", "verbose", "run tests verbosely, showing all output")
104109
.optflag(
@@ -286,6 +291,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
286291
host_rustcflags: matches.opt_strs("host-rustcflags"),
287292
target_rustcflags: matches.opt_strs("target-rustcflags"),
288293
optimize_tests: matches.opt_present("optimize-tests"),
294+
rust_randomized_layout: matches.opt_present("rust-randomized-layout"),
289295
target,
290296
host: opt_str2(matches.opt_str("host")),
291297
cdb,

0 commit comments

Comments
 (0)