Updated Miri details

This commit is contained in:
Maurus Decimus
2026-09-29 10:58:56 +02:00
parent 096b8b01e2
commit c22d5fe970
4 changed files with 34 additions and 12 deletions
+4 -2
View File
@@ -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:
+2 -2
View File
@@ -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()
+12 -7
View File
@@ -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<Vec<u8>> {
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<Vec<u8>> {
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<u8> {
@@ -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,
+16 -1
View File
@@ -174,7 +174,9 @@ fn owned_str(bytes: Vec<u8>) -> 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();