From 9a6450af95dd63c6e4f5ca43feff49313692885c Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 12 Mar 2022 10:09:15 -0500 Subject: [PATCH] also accept odd number of hex digits; add README section on randomized alignment testing --- Cargo.lock | 7 ------- Cargo.toml | 1 - README.md | 18 +++++++++++++++++- src/bin/miri.rs | 22 +++++----------------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 674ce8a2b1e..35e45800c0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,12 +155,6 @@ dependencies = [ "libc", ] -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" - [[package]] name = "humantime" version = "2.1.0" @@ -258,7 +252,6 @@ dependencies = [ "compiletest_rs", "env_logger", "getrandom", - "hex", "libc", "log", "measureme", diff --git a/Cargo.toml b/Cargo.toml index a4d85e39e65..356f6822fa1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,6 @@ getrandom = { version = "0.2", features = ["std"] } env_logger = "0.9" log = "0.4" shell-escape = "0.1.4" -hex = "0.4.0" rand = "0.8" smallvec = "1.7" diff --git a/README.md b/README.md index 55154356c9d..8e8670f3775 100644 --- a/README.md +++ b/README.md @@ -175,12 +175,28 @@ Here is an example job for GitHub Actions: The explicit `cargo miri setup` helps to keep the output of the actual test step clean. +### Testing for alignment issues + +Miri can sometimes miss misaligned accesses since allocations can "happen to be" +aligned just right. You can use `-Zmiri-symbolic-alignment-check` to definitely +catch all such issues, but that flag will also cause false positives when code +does manual pointer arithmetic to account for alignment. Another alternative is +to call Miri with various values for `-Zmiri-seed`; that will alter the +randomness that is used to determine allocation base addresses. The following +snippet calls Miri in a loop with different values for the seed: + +``` +for seed in $({ echo obase=16; seq 255; } | bc); do + MIRIFLAGS=-Zmiri-seed=$seed cargo miri test || { echo "Last seed: $seed"; break; }; +done +``` + ### Common Problems When using the above instructions, you may encounter a number of confusing compiler errors. -### "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace" +#### "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace" You may see this when trying to get Miri to display a backtrace. By default, Miri doesn't expose any environment to the program, so running diff --git a/src/bin/miri.rs b/src/bin/miri.rs index dd7b0b54f48..a1f7c617f0a 100644 --- a/src/bin/miri.rs +++ b/src/bin/miri.rs @@ -15,7 +15,6 @@ use std::num::NonZeroU64; use std::path::PathBuf; use std::str::FromStr; -use hex::FromHexError; use log::debug; use rustc_data_structures::sync::Lrc; @@ -377,22 +376,11 @@ fn main() { if miri_config.seed.is_some() { panic!("Cannot specify -Zmiri-seed multiple times!"); } - let seed_raw = hex::decode(arg.strip_prefix("-Zmiri-seed=").unwrap()) - .unwrap_or_else(|err| match err { - FromHexError::InvalidHexCharacter { .. } => panic!( - "-Zmiri-seed should only contain valid hex digits [0-9a-fA-F]" - ), - FromHexError::OddLength => - panic!("-Zmiri-seed should have an even number of digits"), - err => panic!("unknown error decoding -Zmiri-seed as hex: {:?}", err), - }); - if seed_raw.len() > 8 { - panic!("-Zmiri-seed must be at most 8 bytes, was {}", seed_raw.len()); - } - - let mut bytes = [0; 8]; - bytes[..seed_raw.len()].copy_from_slice(&seed_raw); - miri_config.seed = Some(u64::from_be_bytes(bytes)); + let seed = u64::from_str_radix(arg.strip_prefix("-Zmiri-seed=").unwrap(), 16) + .unwrap_or_else(|_| panic!( + "-Zmiri-seed should only contain valid hex digits [0-9a-fA-F] and fit into a u64 (max 16 characters)" + )); + miri_config.seed = Some(seed); } arg if arg.starts_with("-Zmiri-env-exclude=") => { miri_config