From c22d5fe970c411c31c8acc494ef09e97f8fee52b Mon Sep 17 00:00:00 2001 From: Maurus Decimus <11444311+mdecimus@users.noreply.github.com> Date: Tue, 29 Sep 2026 10:58:56 +0200 Subject: [PATCH] Updated Miri details --- README.md | 6 ++++-- src/parser/tests.rs | 4 ++-- src/scan/tests.rs | 19 ++++++++++++------- src/store/str.rs | 17 ++++++++++++++++- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 21eae33..5990261 100644 --- a/README.md +++ b/README.md @@ -409,15 +409,17 @@ To fuzz the parser (requires [cargo-fuzz](https://crates.io/crates/cargo-fuzz) a The other targets are `limits` (parser limits), `fields` (header field parsers), `charsets` (charset and HTML decoders), `kernels` (every SIMD kernel against the scalar one) and `mbox` (mbox reader); `kernels`, `fields` and `charsets` use `-max_len=4096`, `limits` uses `-max_len=16384` and `mbox` uses `-max_len=65536`. -To run the unit tests that exercise `unsafe` code under [Miri](https://github.com/rust-lang/miri) (the tests use fewer samples under Miri), natively and on x86-64 with the AVX2 kernels: +To run the unit tests under [Miri](https://github.com/rust-lang/miri) (the tests use fewer samples under Miri; each line takes about an hour): the span resolver and the charset decoders, the SIMD kernels natively and on x86-64 (SSE2 only, then AVX2), and the parser. Miri does not implement every NEON instruction used by encodify, so the parser tests use its portable code (`--cfg encodify_scalar`, encodify 1.0.1 or later): ```bash $ MIRIFLAGS=-Zmiri-disable-isolation cargo +nightly miri test --lib -- store:: decoders::charsets:: $ MIRIFLAGS=-Zmiri-disable-isolation cargo +nightly miri test --lib -- scan:: + $ MIRIFLAGS=-Zmiri-disable-isolation cargo +nightly miri test --lib --target x86_64-unknown-linux-gnu -- scan:: $ MIRIFLAGS=-Zmiri-disable-isolation RUSTFLAGS="-Ctarget-feature=+avx2" cargo +nightly miri test --lib --target x86_64-unknown-linux-gnu -- scan:: + $ MIRIFLAGS=-Zmiri-disable-isolation RUSTFLAGS="--cfg encodify_scalar" cargo +nightly miri test --lib -- parser:: --skip truncated_inputs_never_panic --skip mutated_inputs_never_panic ``` -On macOS, the x86-64 run also needs `CC_x86_64_unknown_linux_gnu=clang AR_x86_64_unknown_linux_gnu=ar CFLAGS_x86_64_unknown_linux_gnu=-ffreestanding`. Miri does not implement every NEON instruction used by encodify, so the parser tests run under Miri with its portable code: add `RUSTFLAGS="--cfg encodify_scalar"` and `-- parser::`. +On macOS, the x86-64 runs also need `CC_x86_64_unknown_linux_gnu=clang AR_x86_64_unknown_linux_gnu=ar CFLAGS_x86_64_unknown_linux_gnu=-ffreestanding`. The benchmarks read the Enron maildir and the Stalwart SMTP test messages, which `scripts/fetch-corpora.sh` downloads into `target/corpora` (or the directory named by `MAIL_PARSER_CORPORA`) and checks against `scripts/corpora.sha256`; a missing corpus is skipped with a warning. To download them and run the micro-benchmarks of the SIMD kernels and of string resolution: diff --git a/src/parser/tests.rs b/src/parser/tests.rs index ee96ee4..6323778 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -9,7 +9,7 @@ use crate::{ MessageParser, PartFlags, PartKind, Source, scan::{ Kernel, - tests::{fixture_files, with_crlf, with_lf}, + tests::{FIXTURE_STEP, fixture_files, with_crlf, with_lf}, }, }; use encodify::base64; @@ -578,7 +578,7 @@ fn message_is_send_sync_and_covariant() { #[test] fn fixtures_parse_with_every_kernel() { let files = fixture_files(); - assert!(files.len() > 100); + assert!(files.len() > 100 / FIXTURE_STEP); for file in files { for raw in [with_lf(&file), with_crlf(&file)] { let reference = MessageParser::new() diff --git a/src/scan/tests.rs b/src/scan/tests.rs index 78b0f49..65f386f 100644 --- a/src/scan/tests.rs +++ b/src/scan/tests.rs @@ -11,6 +11,8 @@ const ALPHABET: &[u8] = b"\n\n\n\r\r---- \ta"; const ITERATIONS: usize = if cfg!(miri) { 20 } else { 3_000 }; const SET_ITERATIONS: usize = if cfg!(miri) { 4 } else { 400 }; const SAMPLE_STEP: usize = if cfg!(miri) { 9 } else { 1 }; +const FROM_STEP: usize = if cfg!(miri) { 5 } else { 1 }; +pub(crate) const FIXTURE_STEP: usize = if cfg!(miri) { 8 } else { 1 }; const MAX_LEN: usize = 300; pub(crate) struct Rng(pub(crate) u64); @@ -71,7 +73,7 @@ fn agree_on_sample(kernel: Kernel, hay: &[u8]) { } pub(crate) fn fixture_files() -> Vec> { - let mut files = Vec::new(); + let mut paths = Vec::new(); let mut dirs = vec![PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("resources/eml")]; while let Some(dir) = dirs.pop() { let Ok(entries) = std::fs::read_dir(&dir) else { @@ -80,14 +82,17 @@ pub(crate) fn fixture_files() -> Vec> { for path in entries.flatten().map(|entry| entry.path()) { if path.is_dir() { dirs.push(path); - } else if path.extension().is_some_and(|ext| ext == "eml") - && let Ok(bytes) = std::fs::read(&path) - { - files.push(bytes); + } else if path.extension().is_some_and(|ext| ext == "eml") { + paths.push(path); } } } - files + paths.sort_unstable(); + paths + .iter() + .step_by(FIXTURE_STEP) + .filter_map(|path| std::fs::read(path).ok()) + .collect() } pub(crate) fn with_crlf(bytes: &[u8]) -> Vec { @@ -192,7 +197,7 @@ const SETS: [ByteSet; 7] = [ fn agree_on_sets(hay: &[u8]) { for set in &SETS { - for from in 0..=hay.len() + 1 { + for from in (0..=hay.len() + 1).step_by(FROM_STEP) { for end in [ from, from + 1, diff --git a/src/store/str.rs b/src/store/str.rs index 1bce9ab..3a67c5c 100644 --- a/src/store/str.rs +++ b/src/store/str.rs @@ -174,7 +174,9 @@ fn owned_str(bytes: Vec) -> String { #[cfg(test)] mod tests { - use super::Str; + use super::{Str, validated_string}; + + const LONG_NON_ASCII_CHARS: usize = 100; #[test] fn borrow_validates_utf8() { @@ -186,6 +188,19 @@ mod tests { assert!(Str::borrow(b"\xff\xfe", 0..2).is_none()); } + #[test] + fn long_non_ascii_input_validates() { + let text = "\u{e9}".repeat(LONG_NON_ASCII_CHARS); + let source = text.as_bytes(); + let span = Str::borrow(source, 0..source.len()).expect("valid"); + assert_eq!(span.resolve(source, ""), text); + assert!(Str::borrow(source, 1..source.len()).is_none()); + assert_eq!( + validated_string(source.to_vec()).as_deref(), + Ok(text.as_str()) + ); + } + #[test] fn pool_strings() { let mut pool = String::new();