Skip to content

Commit 9a6450a

Browse files
committed
also accept odd number of hex digits; add README section on randomized alignment testing
1 parent 27d5b84 commit 9a6450a

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ getrandom = { version = "0.2", features = ["std"] }
2222
env_logger = "0.9"
2323
log = "0.4"
2424
shell-escape = "0.1.4"
25-
hex = "0.4.0"
2625
rand = "0.8"
2726
smallvec = "1.7"
2827

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,28 @@ Here is an example job for GitHub Actions:
175175
The explicit `cargo miri setup` helps to keep the output of the actual test step
176176
clean.
177177

178+
### Testing for alignment issues
179+
180+
Miri can sometimes miss misaligned accesses since allocations can "happen to be"
181+
aligned just right. You can use `-Zmiri-symbolic-alignment-check` to definitely
182+
catch all such issues, but that flag will also cause false positives when code
183+
does manual pointer arithmetic to account for alignment. Another alternative is
184+
to call Miri with various values for `-Zmiri-seed`; that will alter the
185+
randomness that is used to determine allocation base addresses. The following
186+
snippet calls Miri in a loop with different values for the seed:
187+
188+
```
189+
for seed in $({ echo obase=16; seq 255; } | bc); do
190+
MIRIFLAGS=-Zmiri-seed=$seed cargo miri test || { echo "Last seed: $seed"; break; };
191+
done
192+
```
193+
178194
### Common Problems
179195

180196
When using the above instructions, you may encounter a number of confusing compiler
181197
errors.
182198

183-
### "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
199+
#### "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
184200

185201
You may see this when trying to get Miri to display a backtrace. By default, Miri
186202
doesn't expose any environment to the program, so running

src/bin/miri.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::num::NonZeroU64;
1515
use std::path::PathBuf;
1616
use std::str::FromStr;
1717

18-
use hex::FromHexError;
1918
use log::debug;
2019

2120
use rustc_data_structures::sync::Lrc;
@@ -377,22 +376,11 @@ fn main() {
377376
if miri_config.seed.is_some() {
378377
panic!("Cannot specify -Zmiri-seed multiple times!");
379378
}
380-
let seed_raw = hex::decode(arg.strip_prefix("-Zmiri-seed=").unwrap())
381-
.unwrap_or_else(|err| match err {
382-
FromHexError::InvalidHexCharacter { .. } => panic!(
383-
"-Zmiri-seed should only contain valid hex digits [0-9a-fA-F]"
384-
),
385-
FromHexError::OddLength =>
386-
panic!("-Zmiri-seed should have an even number of digits"),
387-
err => panic!("unknown error decoding -Zmiri-seed as hex: {:?}", err),
388-
});
389-
if seed_raw.len() > 8 {
390-
panic!("-Zmiri-seed must be at most 8 bytes, was {}", seed_raw.len());
391-
}
392-
393-
let mut bytes = [0; 8];
394-
bytes[..seed_raw.len()].copy_from_slice(&seed_raw);
395-
miri_config.seed = Some(u64::from_be_bytes(bytes));
379+
let seed = u64::from_str_radix(arg.strip_prefix("-Zmiri-seed=").unwrap(), 16)
380+
.unwrap_or_else(|_| panic!(
381+
"-Zmiri-seed should only contain valid hex digits [0-9a-fA-F] and fit into a u64 (max 16 characters)"
382+
));
383+
miri_config.seed = Some(seed);
396384
}
397385
arg if arg.starts_with("-Zmiri-env-exclude=") => {
398386
miri_config

0 commit comments

Comments
 (0)