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
Provenance
- Commit
197e5df- Tree
3582acb- Author
David Peter <mail@david-peter.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
cb6f2f7
2 files changed+33 added−45 removed
modified:src/shell.rs+31 added-45 removed
16 unmodified lines17 /// The exit status of the process18 pub status: ExitStatus,19 }20 21-/// Execute the given command and return a timing summary22-#[cfg(windows)]23-pub 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()?;32-33- let (user_time, system_time) = cpu_timer.stop();34- Ok(ExecuteResult {35- user_time,36- system_time,37- status,38- })21+fn randomized_environment_offset_value() -> String {22+ "X".repeat(rand::random::<usize>() % 4096usize)23 }24 25 /// Execute the given command and return a timing summary26 #[cfg(not(windows))]4 unmodified lines31 shell: &Shell,32 ) -> Result<ExecuteResult> {33 let cpu_timer = get_cpu_timer();34 51- let status = run_shell_command(stdout, stderr, command, shell)?;52-53- let (user_time, system_time) = cpu_timer.stop();54-55- Ok(ExecuteResult {56- user_time,57- system_time,58- status,59- })60-}61-62-/// Run a standard shell command using `sh -c`63-#[cfg(not(windows))]64-fn 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))?;48+49+ let (user_time, system_time) = cpu_timer.stop();50+51+ Ok(ExecuteResult {52+ user_time,53+ system_time,54+ status,55+ })56 }57 85-/// Run a Windows shell command using `cmd.exe /C`58+/// Execute the given command and return a timing summary59 #[cfg(windows)]87-fn run_shell_command(60+pub 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()?;81+82+ 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 lines8 ## Changes9 10 - 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 #48414 15 ## Bugfixes16 17 ## Other271 unmodified lines