From 197e5df9f0ef0a66ea7c60b171ce6fcde7656d3e Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 22 Feb 2022 09:24:00 +0100 Subject: [PATCH] Add environment randomization on Windows --- CHANGELOG.md | 2 ++ src/shell.rs | 78 +++++++++++++++++++++------------------------------- 2 files changed, 34 insertions(+), 46 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed80355..1a01a80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ - Breaking change: the `-s` short option for `--style` is now used for the new `--setup` option. +- The environment offset randomization is now also available on Windows, + see #484 ## Bugfixes diff --git a/src/shell.rs b/src/shell.rs index 30e1cfe..ed64e30 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -18,24 +18,8 @@ pub struct ExecuteResult { pub status: ExitStatus, } -/// Execute the given command and return a timing summary -#[cfg(windows)] -pub fn execute_and_time( - stdout: Stdio, - stderr: Stdio, - command: &str, - shell: &Shell, -) -> Result { - let mut child = run_shell_command(stdout, stderr, command, shell)?; - let cpu_timer = get_cpu_timer(&child); - let status = child.wait()?; - - let (user_time, system_time) = cpu_timer.stop(); - Ok(ExecuteResult { - user_time, - system_time, - status, - }) +fn randomized_environment_offset_value() -> String { + "X".repeat(rand::random::() % 4096usize) } /// Execute the given command and return a timing summary @@ -48,7 +32,19 @@ pub fn execute_and_time( ) -> Result { let cpu_timer = get_cpu_timer(); - let status = run_shell_command(stdout, stderr, command, shell)?; + let status = shell + .command() + .arg("-c") + .arg(command) + .env( + "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET", + randomized_environment_offset_value(), + ) + .stdin(Stdio::null()) + .stdout(stdout) + .stderr(stderr) + .status() + .with_context(|| format!("Failed to run command '{}'", command))?; let (user_time, system_time) = cpu_timer.stop(); @@ -59,44 +55,34 @@ pub fn execute_and_time( }) } -/// Run a standard shell command using `sh -c` -#[cfg(not(windows))] -fn run_shell_command( +/// Execute the given command and return a timing summary +#[cfg(windows)] +pub fn execute_and_time( stdout: Stdio, stderr: Stdio, command: &str, shell: &Shell, -) -> Result { - shell +) -> Result { + let mut child = shell .command() - .arg("-c") + .arg("/C") .arg(command) .env( "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET", - "X".repeat(rand::random::() % 4096usize), + randomized_environment_offset_value(), ) .stdin(Stdio::null()) .stdout(stdout) .stderr(stderr) - .status() - .with_context(|| format!("Failed to run command '{}'", command)) -} - -/// Run a Windows shell command using `cmd.exe /C` -#[cfg(windows)] -fn run_shell_command( - stdout: Stdio, - stderr: Stdio, - command: &str, - shell: &Shell, -) -> Result { - shell - .command() - .arg("/C") - .arg(command) - .stdin(Stdio::null()) - .stdout(stdout) - .stderr(stderr) .spawn() - .with_context(|| format!("Failed to run command '{}'", command)) + .with_context(|| format!("Failed to run command '{}'", command))?; + let cpu_timer = get_cpu_timer(&child); + let status = child.wait()?; + + let (user_time, system_time) = cpu_timer.stop(); + Ok(ExecuteResult { + user_time, + system_time, + status, + }) }