From 7c649bc74ecd425bdf15f5376979eeac973821c0 Mon Sep 17 00:00:00 2001 From: nicoo Date: Mon, 3 May 2021 16:14:38 +0200 Subject: [PATCH] factor::benches: Add check against ASLR --- src/uu/factor/BENCHMARKING.md | 1 - src/uu/factor/benches/table.rs | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/uu/factor/BENCHMARKING.md b/src/uu/factor/BENCHMARKING.md index 3ad038c15..cf3bb35d0 100644 --- a/src/uu/factor/BENCHMARKING.md +++ b/src/uu/factor/BENCHMARKING.md @@ -25,7 +25,6 @@ as possible: to it, so it won't be preempted in the middle of a measurement ; - disable ASLR by running `setarch -R cargo bench`, so we can compare results across multiple executions. - **TODO**: check this propagates to the benchmark process [`criterion`]: https://bheisler.github.io/criterion.rs/book/index.html diff --git a/src/uu/factor/benches/table.rs b/src/uu/factor/benches/table.rs index 44ea1c863..232e59053 100644 --- a/src/uu/factor/benches/table.rs +++ b/src/uu/factor/benches/table.rs @@ -4,6 +4,9 @@ use std::convert::TryInto; use uu_factor::{table::*, Factors}; fn table(c: &mut Criterion) { + #[cfg(target_os = "linux")] + check_personality(); + const INPUT_SIZE: usize = 128; assert!( INPUT_SIZE % CHUNK_SIZE == 0, @@ -55,5 +58,29 @@ fn table(c: &mut Criterion) { group.finish() } +#[cfg(target_os = "linux")] +fn check_personality() { + use std::fs; + const ADDR_NO_RANDOMIZE: u64 = 0x0040000; + const PERSONALITY_PATH: &'static str = "/proc/self/personality"; + + let p_string = fs::read_to_string(PERSONALITY_PATH) + .expect(&format!("Couldn't read '{}'", PERSONALITY_PATH)) + .strip_suffix("\n") + .unwrap() + .to_owned(); + + let personality = u64::from_str_radix(&p_string, 16).expect(&format!( + "Expected a hex value for personality, got '{:?}'", + p_string + )); + if personality & ADDR_NO_RANDOMIZE == 0 { + eprintln!( + "WARNING: Benchmarking with ASLR enabled (personality is {:x}), results might not be reproducible.", + personality + ); + } +} + criterion_group!(benches, table); criterion_main!(benches);