Skip to content

Commit

Permalink
Add safe compilation options
Browse files Browse the repository at this point in the history
Add two options when building rust: strip and stack protector.
If set `strip = true`, symbols will be stripped using `-Cstrip=symbols`.
Also can set `stack-protector` and stack protectors will be used.
  • Loading branch information
lxy19980601 committed Nov 21, 2023
1 parent 3a85a5c commit 5f4c094
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,15 @@ change-id = 116881
# desired in distributions, for example.
#rpath = true

# Indicates whether symbols should be stripped using `-Cstrip=symbols`.
#strip = false

# Indicates whether stack protectors should be used
# via the unstable option `-Zstack-protector`.
# Valid options are : `none`(default),`basic`,`strong`, or `all`.
# But `strong` and `basic` options may be buggy and are not suggested here.(rust-lang/rust #114903)
#stack-protector = "none"

# Prints each test name as it is executed, to help debug issues in the test harness itself.
#verbose-tests = false

Expand Down
10 changes: 10 additions & 0 deletions src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,6 +1669,16 @@ impl<'a> Builder<'a> {
}
}

cargo.env(profile_var("STRIP"), self.config.rust_strip.to_string());

match self.config.rust_stack_protector.as_str() {
"none" => rustflags.arg("-Zstack-protector=none"),
"basic" => rustflags.arg("-Zstack-protector=basic"),
"strong" => rustflags.arg("-Zstack-protector=strong"),
"all" => rustflags.arg("-Zstack-protector=all"),
other => unreachable!("{:?} is not recognized as valid stack protectors", other),
};

if let Some(host_linker) = self.linker(compiler.host) {
hostflags.arg(format!("-Clinker={}", host_linker.display()));
}
Expand Down
8 changes: 8 additions & 0 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ pub struct Config {
pub rust_debuginfo_level_tests: DebuginfoLevel,
pub rust_split_debuginfo: SplitDebuginfo,
pub rust_rpath: bool,
pub rust_strip: bool,
pub rust_stack_protector: String,
pub rustc_parallel: bool,
pub rustc_default_linker: Option<String>,
pub rust_optimize_tests: bool,
Expand Down Expand Up @@ -1000,6 +1002,8 @@ define_config! {
description: Option<String> = "description",
musl_root: Option<String> = "musl-root",
rpath: Option<bool> = "rpath",
strip: Option<bool> = "strip",
stack_protector: Option<String> = "stack-protector",
verbose_tests: Option<bool> = "verbose-tests",
optimize_tests: Option<bool> = "optimize-tests",
codegen_tests: Option<bool> = "codegen-tests",
Expand Down Expand Up @@ -1067,6 +1071,8 @@ impl Config {
config.docs = true;
config.docs_minification = true;
config.rust_rpath = true;
config.rust_strip = false;
config.rust_stack_protector = "none".to_string();
config.channel = "dev".to_string();
config.codegen_tests = true;
config.rust_dist_src = true;
Expand Down Expand Up @@ -1420,6 +1426,8 @@ impl Config {
set(&mut config.rust_optimize_tests, rust.optimize_tests);
set(&mut config.codegen_tests, rust.codegen_tests);
set(&mut config.rust_rpath, rust.rpath);
set(&mut config.rust_strip, rust.strip);
set(&mut config.rust_stack_protector, rust.stack_protector);
set(&mut config.jemalloc, rust.jemalloc);
set(&mut config.test_compare_mode, rust.test_compare_mode);
set(&mut config.backtrace, rust.backtrace);
Expand Down

0 comments on commit 5f4c094

Please sign in to comment.