Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/runner-shared/src/runtime_env/go.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const GODEBUG: &str = "GODEBUG";
/// Starts the global `math/rand` source from `Seed(1)` instead of a random seed.
const RANDAUTOSEED_OFF: &str = "randautoseed=0";

/// Go env:
/// - `GODEBUG=randautoseed=0`: deterministic global `math/rand` in every mode.
pub(super) fn env() -> Vec<(&'static str, String)> {
let user_godebug = std::env::var(GODEBUG).ok();
vec![(GODEBUG, godebug(user_godebug.as_deref()))]
}

/// Prepends our settings to the user's `GODEBUG`. Go uses the last occurrence
/// of a setting, so an explicit `randautoseed` from the user still wins.
fn godebug(user_godebug: Option<&str>) -> String {
match user_godebug {
Some(user) if !user.is_empty() => format!("{RANDAUTOSEED_OFF},{user}"),
_ => RANDAUTOSEED_OFF.to_owned(),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn keeps_user_settings_after_ours() {
assert_eq!(godebug(None), "randautoseed=0");
assert_eq!(godebug(Some("")), "randautoseed=0");
assert_eq!(
godebug(Some("gctrace=1,randautoseed=1")),
"randautoseed=0,gctrace=1,randautoseed=1"
);
}
}
2 changes: 2 additions & 0 deletions crates/runner-shared/src/runtime_env/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::measurement_mode::MeasurementMode;

mod go;
mod java;
mod python;

Expand All @@ -10,5 +11,6 @@ pub fn env(mode: MeasurementMode) -> Vec<(&'static str, String)> {
let mut env = vec![(RUNNER_MODE_ENV, mode.runner_mode_env_value().to_owned())];
env.extend(python::env(mode));
env.extend(java::env(mode));
env.extend(go::env());
env
}
61 changes: 61 additions & 0 deletions src/executor/helpers/aslr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! Disables ASLR for benchmark processes, so the memory layout and address-seeded
//! hashes (e.g. abseil's `absl::Hash`) stay the same across runs.

use crate::executor::helpers::command::CommandBuilder;
use crate::prelude::*;
use std::env::consts::ARCH;
use std::process::{Command, Stdio};
use std::sync::LazyLock;

pub const SETARCH: &str = "setarch";
pub const ADDR_NO_RANDOMIZE_FLAG: &str = "--addr-no-randomize";

/// Wraps `cmd` in `setarch <arch> --addr-no-randomize`.
///
/// The kernel clears the flag when exec'ing setuid or file-capability binaries
/// (`sudo`, `memtrack`), so this must wrap the innermost benchmark command.
///
/// Leaves `cmd` unchanged off Linux, or when `setarch` fails, e.g. because
/// Docker's default seccomp profile blocks `personality(ADDR_NO_RANDOMIZE)`.
pub fn wrap_with_aslr_disabled(cmd: &mut CommandBuilder) {
if !can_disable_aslr() {
return;
}
cmd.wrap(SETARCH, [ARCH, ADDR_NO_RANDOMIZE_FLAG]);
}

/// [`wrap_with_aslr_disabled`] for a shell command string, for callers that run
/// their command through a shell. Returns `command` unchanged on the same
/// conditions, so no extra shell is spawned when ASLR stays on.
pub fn shell_command_with_aslr_disabled(command: String) -> String {
if !can_disable_aslr() {
return command;
}
shell_words::join([
SETARCH,
ARCH,
ADDR_NO_RANDOMIZE_FLAG,
"bash",
"-c",
&command,
])
}

fn can_disable_aslr() -> bool {
cfg!(target_os = "linux") && *CAN_DISABLE_ASLR
}

static CAN_DISABLE_ASLR: LazyLock<bool> = LazyLock::new(|| {
let can_disable = Command::new(SETARCH)
.args([ARCH, ADDR_NO_RANDOMIZE_FLAG, "true"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|status| status.success());
if !can_disable {
warn!(
"Failed to disable ASLR with `{SETARCH} {ARCH} {ADDR_NO_RANDOMIZE_FLAG}`, memory addresses will vary between runs"
);
}
can_disable
});
1 change: 1 addition & 0 deletions src/executor/helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod apt;
pub mod aslr;
#[cfg(target_os = "linux")]
pub mod capabilities;
pub mod command;
Expand Down
9 changes: 6 additions & 3 deletions src/executor/memory/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::executor::ExecutorName;
use crate::executor::ExecutorSupport;
use crate::executor::PrivilegeStatus;
use crate::executor::ToolStatus;
use crate::executor::helpers::aslr::shell_command_with_aslr_disabled;
use crate::executor::helpers::command::CommandBuilder;
use crate::executor::helpers::env::{build_path_env, get_base_injected_env};
use crate::executor::helpers::get_bench_command::get_bench_command;
Expand Down Expand Up @@ -58,9 +59,11 @@ impl MemoryExecutor {
let (ipc_server, server_name) = ipc::IpcOneShotServer::new()?;

// A file-capability binary runs in glibc secure-execution mode, which strips
// LD_* variables. The forwarded environment has to be re-sourced below that
// boundary, inside the benchmark, to survive.
let bench_command = get_bench_command(&execution_context.config)?;
// LD_* variables and clears the ASLR personality flag. The forwarded
// environment and `setarch` have to sit below that boundary, inside the
// benchmark, to survive.
let bench_command =
shell_command_with_aslr_disabled(get_bench_command(&execution_context.config)?);
let (bench_command, env_file) = prefix_command_with_env(&bench_command, &extra_env)?;

// Build the memtrack command
Expand Down
5 changes: 3 additions & 2 deletions src/executor/valgrind/measure.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::executor::ExecutorConfig;
use crate::executor::RunnerMode;
use crate::executor::config::SimulationTool;
use crate::executor::helpers::aslr::{ADDR_NO_RANDOMIZE_FLAG, SETARCH};
use crate::executor::helpers::env::{build_path_env, get_base_injected_env};
use crate::executor::helpers::get_bench_command::get_bench_command;
use crate::executor::helpers::run_command_with_log_pipe::run_command_with_log_pipe;
Expand Down Expand Up @@ -130,8 +131,8 @@ pub async fn measure(
}

// Create the command
let mut cmd = Command::new("setarch");
cmd.arg(ARCH).arg("--addr-no-randomize");
let mut cmd = Command::new(SETARCH);
cmd.arg(ARCH).arg(ADDR_NO_RANDOMIZE_FLAG);
// Configure the environment
cmd.envs(get_base_injected_env(
RunnerMode::Simulation,
Expand Down
2 changes: 2 additions & 0 deletions src/executor/wall_time/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::executor::Executor;
use crate::executor::ExecutorConfig;
use crate::executor::ToolStatus;
use crate::executor::config::WalltimeProfiler;
use crate::executor::helpers::aslr::wrap_with_aslr_disabled;
use crate::executor::helpers::command::CommandBuilder;
use crate::executor::helpers::env::{build_path_env, get_base_injected_env};
use crate::executor::helpers::get_bench_command::get_bench_command;
Expand Down Expand Up @@ -81,6 +82,7 @@ impl WallTimeExecutor {

let mut bench_cmd = CommandBuilder::new("bash");
bench_cmd.arg(script_file.path());
wrap_with_aslr_disabled(&mut bench_cmd);
let (mut bench_cmd, env_file) = wrap_with_env(bench_cmd, &extra_env)?;

if let Some(cwd) = &config.working_directory {
Expand Down
Loading