Skip to content
Myr

Add environment randomization on Windows

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Peter · committed by David Peter · parent cb6f2f7 · Browse files · 2 files+33 added−45 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+33 added−45 removed

  • other:+33 added−45 removed

2 files+33 added−45 removed

modified:src/shell.rs+31 added-45 removed
16 unmodified lines
17 /// The exit status of the process18 pub status: ExitStatus,19}2021/// Execute the given command and return a timing summary22#[cfg(windows)]23pub fn execute_and_time(24 stdout: Stdio,25 stderr: Stdio,26 command: &str,27 shell: &Shell,28) -> Result<ExecuteResult> {29 let mut child = run_shell_command(stdout, stderr, command, shell)?;30 let cpu_timer = get_cpu_timer(&child);31 let status = child.wait()?;3233 let (user_time, system_time) = cpu_timer.stop();34 Ok(ExecuteResult {35 user_time,36 system_time,37 status,38 })21fn randomized_environment_offset_value() -> String {22 "X".repeat(rand::random::<usize>() % 4096usize)23}2425/// Execute the given command and return a timing summary26#[cfg(not(windows))]
4 unmodified lines
31 shell: &Shell,32) -> Result<ExecuteResult> {33 let cpu_timer = get_cpu_timer();3451 let status = run_shell_command(stdout, stderr, command, shell)?;5253 let (user_time, system_time) = cpu_timer.stop();5455 Ok(ExecuteResult {56 user_time,57 system_time,58 status,59 })60}6162/// Run a standard shell command using `sh -c`63#[cfg(not(windows))]64fn run_shell_command(65 stdout: Stdio,66 stderr: Stdio,67 command: &str,68 shell: &Shell,69) -> Result<std::process::ExitStatus> {70 shell35 let status = shell36 .command()37 .arg("-c")38 .arg(command)39 .env(40 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",76 "X".repeat(rand::random::<usize>() % 4096usize),41 randomized_environment_offset_value(),42 )43 .stdin(Stdio::null())44 .stdout(stdout)45 .stderr(stderr)46 .status()82 .with_context(|| format!("Failed to run command '{}'", command))47 .with_context(|| format!("Failed to run command '{}'", command))?;4849 let (user_time, system_time) = cpu_timer.stop();5051 Ok(ExecuteResult {52 user_time,53 system_time,54 status,55 })56}5785/// Run a Windows shell command using `cmd.exe /C`58/// Execute the given command and return a timing summary59#[cfg(windows)]87fn run_shell_command(60pub fn execute_and_time(61 stdout: Stdio,62 stderr: Stdio,63 command: &str,64 shell: &Shell,92) -> Result<std::process::Child> {93 shell65) -> Result<ExecuteResult> {66 let mut child = shell67 .command()68 .arg("/C")69 .arg(command)70 .env(71 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",72 randomized_environment_offset_value(),73 )74 .stdin(Stdio::null())75 .stdout(stdout)76 .stderr(stderr)77 .spawn()101 .with_context(|| format!("Failed to run command '{}'", command))78 .with_context(|| format!("Failed to run command '{}'", command))?;79 let cpu_timer = get_cpu_timer(&child);80 let status = child.wait()?;8182 let (user_time, system_time) = cpu_timer.stop();83 Ok(ExecuteResult {84 user_time,85 system_time,86 status,87 })88}
modified:CHANGELOG.md+2 added
7 unmodified lines
8## Changes910- Breaking change: the `-s` short option for `--style` is now used for11 the new `--setup` option.12- The environment offset randomization is now also available on Windows,13 see #4841415## Bugfixes1617## Other
271 unmodified lines