diff --git a/.gitignore b/.gitignore index 8e0a292..f1d5341 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ # Fuzzing and proptest artefacts /fuzz/target/ +/fuzz/target-none/ /fuzz/corpus/ /fuzz/artifacts/ **/proptest-regressions/*.tmp diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock new file mode 100644 index 0000000..489b091 --- /dev/null +++ b/fuzz/Cargo.lock @@ -0,0 +1,424 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cc" +version = "1.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02f0e023adb368345ce0ca6cdfc0f24a06554468d0e8503e14a0841de79bfa69" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "ciborium" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" +dependencies = [ + "ciborium-io", + "ciborium-ll", + "serde", +] + +[[package]] +name = "ciborium-io" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" + +[[package]] +name = "ciborium-ll" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" +dependencies = [ + "ciborium-io", + "half", +] + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + +[[package]] +name = "crunchy" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" + +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi", +] + +[[package]] +name = "half" +version = "2.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" +dependencies = [ + "cfg-if", + "crunchy", + "zerocopy", +] + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "jiff" +version = "0.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175dc66334db3b9ab9985bf8b61cfed50dc16d20964bb47f9e479adcae984a7f" +dependencies = [ + "jiff-core", + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", +] + +[[package]] +name = "jiff-core" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e599b80292e597019b8b3e392de3f4bea2959ddfb8c7dff60e035e9e311a2c1b" +dependencies = [ + "log", +] + +[[package]] +name = "jiff-static" +version = "0.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7b1af7547d47354bc438d797e3854601bde8f8d185780c5ba4ab27afa54f1ba" +dependencies = [ + "jiff-core", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom", + "libc", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libfuzzer-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9fd2f41a1cba099f79a0b6b6c35656cf7c03351a7bae8ff0f28f25270f929d2" +dependencies = [ + "arbitrary", + "cc", +] + +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "portable-atomic-util" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10ab3eb7f3becc3a1cbc4f2c6f20267996cfc1a6467a873763411b136a122715" +dependencies = [ + "portable-atomic", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "proto" +version = "0.1.0" +dependencies = [ + "ciborium", + "roads", + "serde", + "sha2", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "roads" +version = "0.1.0" +dependencies = [ + "base64", + "ipnet", + "jiff", + "serde", + "serde_json", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + +[[package]] +name = "via-core-fuzz" +version = "0.0.0" +dependencies = [ + "cc", + "jiff", + "libfuzzer-sys", + "proto", + "roads", +] + +[[package]] +name = "zerocopy" +version = "0.8.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32bc8a75f879cd6fe53a6bedf3c28f0e1b914c77424fc2be9e2ec85d7d25aff5" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8bfd2644ea1681f60cf95029ff18aa8ad1cc3f7b8fe37286f8e473bb29f816b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 0000000..0542b14 --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "via-core-fuzz" +version = "0.0.0" +edition = "2024" +publish = false +license = "CPAL-1.0" + +[package.metadata] +cargo-fuzz = true + +[build-dependencies] +cc = "1" + +[dependencies] +libfuzzer-sys = "0.4" +roads = { path = "../crates/roads" } +proto = { path = "../crates/proto" } +jiff = { version = "0.2", default-features = false, features = ["std"] } + +# Its own workspace, so the fuzz build never joins the main one (cargo fuzz's layout). +[workspace] +members = ["."] + +[[bin]] +name = "road_file" +path = "fuzz_targets/road_file.rs" +test = false +doc = false +bench = false + +[[bin]] +name = "envelope" +path = "fuzz_targets/envelope.rs" +test = false +doc = false +bench = false diff --git a/fuzz/build.rs b/fuzz/build.rs new file mode 100644 index 0000000..7658367 --- /dev/null +++ b/fuzz/build.rs @@ -0,0 +1,7 @@ +// Windows only: give the coverage counters their section bounds (see win_sections.c). +fn main() { + println!("cargo:rerun-if-changed=win_sections.c"); + if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") { + cc::Build::new().file("win_sections.c").compile("win_sections"); + } +} diff --git a/fuzz/fuzz_targets/envelope.rs b/fuzz/fuzz_targets/envelope.rs new file mode 100644 index 0000000..13712a4 --- /dev/null +++ b/fuzz/fuzz_targets/envelope.rs @@ -0,0 +1,18 @@ +//! Fuzzes the control-protocol decoder (QA review of PR #1): `Envelope::decode` never panics, and anything it +//! accepts encodes to bytes that decode to the same envelope, and encoding is stable. +//! +//! Run: RUSTC_BOOTSTRAP=1 cargo fuzz run envelope -s none -- -max_total_time=300 + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use proto::Envelope; + +fuzz_target!(|data: &[u8]| { + if let Ok(env) = Envelope::decode(data) { + let bytes = env.encode(); + let again = Envelope::decode(&bytes).expect("an encoded envelope decodes"); + assert_eq!(again, env); + assert_eq!(again.encode(), bytes, "encoding is not stable"); + } +}); diff --git a/fuzz/fuzz_targets/road_file.rs b/fuzz/fuzz_targets/road_file.rs new file mode 100644 index 0000000..727e0f1 --- /dev/null +++ b/fuzz/fuzz_targets/road_file.rs @@ -0,0 +1,118 @@ +//! Fuzzes the road-file parser and the compiler behind it (QA review of PR #1). +//! +//! For any bytes as one road file, added to the home store: +//! 1. `Road::parse` never panics; +//! 2. a road that parses renders to canonical text that parses back to the same road, and rendering is a fixed +//! point (`render(parse(render(r))) == render(r)`); +//! 3. `compile` never panics (its `expect`/`unreachable!` paths assume the parser's checks); +//! 4. deny by default: a file that does not parse, or a road the compiler reports an error for, leaves every +//! plan byte-identical to the home store without it. +//! +//! Run on Linux with nightly: `cargo fuzz run road_file -- -max_total_time=600`. +//! +//! Run on Windows with the stable MSVC toolchain and no sanitizer (`cargo fuzz -s none` adds the PC table, which +//! does not link on COFF without a sanitizer runtime; `build.rs` + `win_sections.c` supply the counter bounds): +//! cd fuzz +//! RUSTFLAGS="-Cpasses=sancov-module -Cllvm-args=-sanitizer-coverage-level=4 +//! -Cllvm-args=-sanitizer-coverage-inline-8bit-counters -Cllvm-args=-sanitizer-coverage-trace-compares +//! -Cllvm-args=-simplifycfg-branch-fold-threshold=0 -Ccodegen-units=1 --cfg fuzzing -Cdebug-assertions +//! -Clink-arg=/include:main" \ +//! cargo build --release --target x86_64-pc-windows-msvc --target-dir target-none --bin road_file +//! target-none/x86_64-pc-windows-msvc/release/road_file.exe -max_total_time=600 corpus/road_file +//! Seed the corpus with `crates/roads/tests/fixtures/home/roads/*` and `fixtures/invalid/roads/*`. + +#![no_main] + +use libfuzzer_sys::fuzz_target; +use roads::{Road, RoadId, Store, compile}; +use std::sync::OnceLock; + +macro_rules! home { + ($f:literal) => { + ( + $f, + include_bytes!(concat!("../../crates/roads/tests/fixtures/home/roads/", $f)).as_slice(), + ) + }; +} + +const DIRECTORY: &[u8] = include_bytes!("../../crates/roads/tests/fixtures/home/directory.json"); +const DEVICES: &[u8] = include_bytes!("../../crates/roads/tests/fixtures/home/devices.json"); +const HOME: [(&str, &[u8]); 10] = [ + home!("7tq0mz-jana-photos-until-sunday.md"), + home!("c0z5wn-petr-exits-friend.md"), + home!("e5yj1s-steam-goes-direct.md"), + home!("h2x8vd-mark-home-lan.md"), + home!("k3f9q2-household-home-server.md"), + home!("m6v0hp-petr-git-expired.md"), + home!("p4n6rb-mark-pc-exits-home.md"), + home!("r8m2tg-router-home-server.md"), + home!("w9c3ke-mark-pc-kill-switch.md"), + home!("x1d7qa-router-imported.md"), +]; + +fn now() -> jiff::Timestamp { + "2026-11-25T12:00:00+01:00".parse().unwrap() +} + +fn files(extra: Option<(&str, &[u8])>) -> Vec<(String, Vec)> { + HOME.iter() + .copied() + .chain(extra) + .map(|(n, b)| (n.to_string(), b.to_vec())) + .collect() +} + +fn plans_json(store: &Store) -> (Vec, roads::Compiled) { + let c = compile(store, now()); + (c.plans.values().map(|p| p.to_json()).collect(), c) +} + +fn baseline() -> &'static Vec { + static B: OnceLock> = OnceLock::new(); + B.get_or_init(|| plans_json(&Store::from_parts(&files(None), Some(DIRECTORY), Some(DEVICES))).0) +} + +/// The id the file claims, so the file name can match it and the fuzzer reaches past `id.file-name`. +fn claimed_id(data: &[u8]) -> String { + std::str::from_utf8(data) + .ok() + .and_then(|t| { + t.lines() + .find_map(|l| l.strip_prefix("id: ").map(|v| v.trim_end_matches('\r'))) + }) + .filter(|v| RoadId::new(v).is_some()) + .unwrap_or("zz9zz9") + .to_string() +} + +fuzz_target!(|data: &[u8]| { + let id = claimed_id(data); + let name = format!("{id}-fuzz.md"); + let path = format!("roads/{name}"); + + // 1 and 2: the parser, and the canonical round trip. + let parsed = Road::parse(&path, data); + if let Ok(road) = &parsed { + let text = road.render(); + let again = match Road::parse(&path, text.as_bytes()) { + Ok(r) => r, + Err(p) => panic!("a rendered road does not parse: {p:?}\n--- rendered ---\n{text}"), + }; + assert_eq!(again.render(), text, "render is not a fixed point"); + assert_eq!( + (&again.id, &again.subject, again.verb, &again.object, &again.until, &again.ports, &again.via, again.classic, &again.created, &again.body), + (&road.id, &road.subject, road.verb, &road.object, &road.until, &road.ports, &road.via, road.classic, &road.created, &road.body), + "the canonical text means something else:\n{text}" + ); + } + + // 3 and 4: the compiler behind it, with the home store. + let store = Store::from_parts(&files(Some((&name, data))), Some(DIRECTORY), Some(DEVICES)); + let (plans, c) = plans_json(&store); + let collides = HOME.iter().any(|(n, _)| n.starts_with(&format!("{id}-"))); + let refused = parsed.is_err() || c.errors().any(|p| p.path == path); + if refused && !collides { + assert_eq!(&plans, baseline(), "a refused road changed a plan"); + } +}); diff --git a/fuzz/win_sections.c b/fuzz/win_sections.c new file mode 100644 index 0000000..ebd4ab9 --- /dev/null +++ b/fuzz/win_sections.c @@ -0,0 +1,9 @@ +/* Section bounds for SanitizerCoverage's inline 8-bit counters on Windows (COFF), so the fuzz targets link + * without a sanitizer runtime. LLVM puts the counters in ".SCOV$CM"; the linker sorts ".SCOV$CA" before it and + * ".SCOV$CZ" after it, and libFuzzer reads everything in between. compiler-rt does the same in + * sanitizer_coverage_win_sections.cpp, which only ships inside a sanitizer runtime. Built only for MSVC targets + * and only for the fuzz crate, which is never part of a release. */ +#pragma section(".SCOV$CA", read, write) +__declspec(allocate(".SCOV$CA")) unsigned char __start___sancov_cntrs[1] = {0}; +#pragma section(".SCOV$CZ", read, write) +__declspec(allocate(".SCOV$CZ")) unsigned char __stop___sancov_cntrs[1] = {0};