diff --git a/verification/squabble-creusot/Cargo.lock b/verification/squabble-creusot/Cargo.lock new file mode 100644 index 0000000..90205f7 --- /dev/null +++ b/verification/squabble-creusot/Cargo.lock @@ -0,0 +1,324 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "bumpalo" +version = "3.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87a0cf66fd1eb2fb7b822cb78370d82303750809bc6ba855f3f58e517d3238db" + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "creusot-std" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b019630da7e585ab18eff475d7ea820474c3fc0cd05a57706facf37f67ab2705" +dependencies = [ + "creusot-std-proc", +] + +[[package]] +name = "creusot-std-proc" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a21564d3da8f97ce00e03125d0c3f7c9faedfa68e5d182d88beb25c82c12d25" +dependencies = [ + "pearlite-syn", + "proc-macro2", + "quote", + "syn 2.0.119", + "uuid", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-task", + "pin-project-lite", + "slab", +] + +[[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 = "itoa" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bd1574e617a9a6c1f8f8181bd7e841e1f20e4277607ef28945765188d6284d4" + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "pearlite-syn" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0cada22bee420c97f91bd98f0e1effdd6ef5ab219df0561f35144dcb504ebfd" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752" + +[[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 = "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 = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[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 = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "squabble-core" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", + "thiserror", +] + +[[package]] +name = "squabble-creusot" +version = "0.1.0" +dependencies = [ + "creusot-std", + "serde_json", + "squabble-core", +] + +[[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 = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.10", +] + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "uuid" +version = "1.29.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c14b15dffa2f4bd712103e857140aa04a4f2ff6e955c77d7eb8e53ee02a3b64" +dependencies = [ + "getrandom", + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.10", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/verification/squabble-creusot/Cargo.toml b/verification/squabble-creusot/Cargo.toml index a7fc4a5..c70cb4f 100644 --- a/verification/squabble-creusot/Cargo.toml +++ b/verification/squabble-creusot/Cargo.toml @@ -54,3 +54,4 @@ creusot-std = { version = "0.14", optional = true } # Only for the stable-Rust mirror tests, which check that the model in # `src/model.rs` agrees with the real implementation in `squabble-core`. squabble-core = { path = "../../crates/squabble-core" } +serde_json = "1" diff --git a/verification/squabble-creusot/src/contracts.rs b/verification/squabble-creusot/src/contracts.rs index b169be1..32f0483 100644 --- a/verification/squabble-creusot/src/contracts.rs +++ b/verification/squabble-creusot/src/contracts.rs @@ -69,3 +69,63 @@ pub fn after_admission(passed: u32, skipped: u32, total: u32, admitted: bool) -> let _ = admitted; passed as u64 + skipped as u64 == total as u64 } + +// ---- PR verdict (S4; design 3.6 properties 3 and 5) --------------------------- +// +// These state to the prover the per-check facts the derivation in +// `squabble_core::verdict` conditions on. They are a *model* of that function +// (see `crate::model`), not a proof of it: the mirror tests in `model.rs` are +// what tie the model to the real code, and docs/CREUSOT.adoc records what has +// and has not been discharged. + +/// Property 3 (no blame for the base), per check: a failure the base already +/// carries -- inherited at the base tip, or cured on it -- is never *introduced* +/// by the PR. +#[requires(!(tip_pass && tip_fail))] +#[requires(head_fail && (cured || (tip_fail && inherits)))] +#[ensures(!result)] +pub fn no_blame_for_the_base( + head_fail: bool, + cured: bool, + tip_pass: bool, + tip_fail: bool, + inherits: bool, +) -> bool { + head_fail && !cured && (tip_pass || (tip_fail && !inherits)) +} + +/// The same statement from the other side: a check is introduced exactly when +/// it fails on the head, is not cured, and the base tip either passes or fails +/// with a signature the head does not inherit. +#[ensures(result == (head_fail && !cured && (tip_pass || (tip_fail && !inherits))))] +pub fn introduced( + head_fail: bool, + cured: bool, + tip_pass: bool, + tip_fail: bool, + inherits: bool, +) -> bool { + head_fail && !cured && (tip_pass || (tip_fail && !inherits)) +} + +/// Property 5 (infra is never PR-BROKEN, except the ruled start failure): the +/// result can only be true for a start failure the PR's own workflow edit caused. +#[requires(kind <= 4u8)] +#[ensures(result ==> kind == 0u8)] +#[ensures(result ==> edits_workflow && starts_on_base)] +#[ensures(result == (kind == 0u8 && edits_workflow && starts_on_base))] +pub fn pr_broken_by_infra(kind: u8, edits_workflow: bool, starts_on_base: bool) -> bool { + kind == 0u8 && edits_workflow && starts_on_base +} + +/// Billing, runner, rate-limit and timeout failures are never the author's, +/// whatever the diff touches. +#[requires(1u8 <= kind && kind <= 4u8)] +#[ensures(!result)] +pub fn other_infra_is_never_pr_broken( + kind: u8, + edits_workflow: bool, + starts_on_base: bool, +) -> bool { + kind == 0u8 && edits_workflow && starts_on_base +} diff --git a/verification/squabble-creusot/src/model.rs b/verification/squabble-creusot/src/model.rs index c48cbb1..71f9994 100644 --- a/verification/squabble-creusot/src/model.rs +++ b/verification/squabble-creusot/src/model.rs @@ -62,6 +62,57 @@ pub fn requires_admission(covered: bool, github_owned: bool) -> bool { !admits(covered, github_owned) } +// ---- PR verdict (S4; design 3.6 properties 3 and 5) ----------------------- + +/// A check is *introduced* by the PR (design 3.3 step 3): it fails on the head, +/// is not `cured` on the base, and either passes on the base tip or fails there +/// with a signature the head does not inherit. `tip_pass` and `tip_fail` are +/// both false when the base has no completed observation (NeverOnBase), which +/// is CI-BROKEN, never the author's. +pub fn introduced( + head_fail: bool, + cured: bool, + tip_pass: bool, + tip_fail: bool, + inherits: bool, +) -> bool { + head_fail && !cured && (tip_pass || (tip_fail && !inherits)) +} + +/// `cured`: the head carries a failure the base has since fixed (red at the +/// merge base with an inheriting signature, green at the base tip). +pub fn cured(head_fail: bool, mb_fail_inherits: bool, tip_pass: bool) -> bool { + head_fail && mb_fail_inherits && tip_pass +} + +/// Property 3, per check: a failure the base already carries (inherited at the +/// tip, or cured) never makes that check the author's. The verdict-level +/// statement needs the carve-out in `pr_broken_by_infra` below: a start failure +/// the PR itself caused is PR-BROKEN without any `Fail` observation. +pub fn no_blame_for_the_base( + head_fail: bool, + cured: bool, + tip_pass: bool, + tip_fail: bool, + inherits: bool, +) -> bool { + introduced(head_fail, cured, tip_pass, tip_fail, inherits) +} + +/// Infra kind codes, in design order. Only `STARTUP_FAILURE` can ever be the +/// author's, and only under the 3.5 ruling. +pub const STARTUP_FAILURE: u8 = 0; +pub const BILLING: u8 = 1; +pub const RUNNER_UNAVAILABLE: u8 = 2; +pub const RATE_LIMITED: u8 = 3; +pub const TIMEOUT: u8 = 4; + +/// Property 5: an infra observation yields PR-BROKEN only for a start failure +/// on a workflow the PR's diff touches that still starts on the base tip. +pub fn pr_broken_by_infra(kind: u8, edits_workflow: bool, starts_on_base: bool) -> bool { + kind == STARTUP_FAILURE && edits_workflow && starts_on_base +} + /// Repointing a coordinate carries the pin **verbatim**: a SHA stays that SHA, /// a tag stays that tag. Repointing is not re-pinning. pub fn repointed_pin(pin: &str) -> &str { @@ -214,7 +265,11 @@ mod tests { RequiredCheck::new("a", CheckRun::Passed), RequiredCheck::new("b", CheckRun::Missing).with_cause(cause), ]); - assert_ne!(g.evaluate(), GateState::Green, "{cause:?} must not be green"); + assert_ne!( + g.evaluate(), + GateState::Green, + "{cause:?} must not be green" + ); // ...and the model says the same thing about the same shape. assert!(!accepted(1, 0, 2), "{cause:?}"); } @@ -227,6 +282,7 @@ mod tests { let allow = AllowList { patterns: vec!["owner/repo@*".into(), "wide/*".into(), "actions/*".into()], github_owned_allowed: true, + verified_allowed: false, }; for coord in [ "owner/repo", @@ -269,6 +325,7 @@ mod tests { Some(&AllowList { patterns: Vec::new(), github_owned_allowed: true, + verified_allowed: false, }), ); let unknown = ZeroJobAttribution { @@ -310,6 +367,165 @@ mod tests { ); // A tag pin is carried just as literally. let tag = Coordinate::parse("old/stds/.github/workflows/gov.yml@v2").unwrap(); - assert_eq!(tag.rewritten("new/stds"), "new/stds/.github/workflows/gov.yml@v2"); + assert_eq!( + tag.rewritten("new/stds"), + "new/stds/.github/workflows/gov.yml@v2" + ); + } +} + +/// Mirror tests for the PR-verdict model: every combination of the per-check +/// facts is built into a real evidence record and run through +/// `squabble_core::verdict::derive`; the model must agree. This is what makes +/// the contracts in `crate::contracts` a statement about the real function and +/// not about a toy. +#[cfg(test)] +mod verdict_mirror { + use super::*; + use serde_json::{json, Value}; + use squabble_core::verdict::{derive, Derived, Evidence, Verdict}; + use std::path::PathBuf; + + fn fixture(name: &str) -> Value { + let p = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("../../docs/upstream/proven-cicd-w49/pr-verdict/fixtures") + .join(format!("{name}.json")); + serde_json::from_str::(&std::fs::read_to_string(p).unwrap()).unwrap()["evidence"] + .clone() + } + + fn pass() -> Value { + json!({"state":"Pass","source":"check_run","conclusions":["success"]}) + } + + fn fail(tests: &[&str]) -> Value { + json!({"state":"Fail","source":"check_run","check_run_id":1, + "signature":{"failed_count":tests.len(),"strength":"strong","steps":["cargo test"],"messages":[],"tests":tests}}) + } + + /// Does the real function put `test` in PR-BROKEN's `introduced` list? + fn real_blames(ev: &Value) -> bool { + let ev: Evidence = serde_json::from_value(ev.clone()).unwrap(); + match derive(&ev) { + Derived::Settled(Verdict::PrBroken { introduced, .. }) => { + introduced.iter().any(|c| c == "test") + } + _ => false, + } + } + + /// head in {pass, fail[t1]}; tip in {pass, fail[t1] (inherits), fail[t2] + /// (does not), no observation}; merge base in {pass, fail[t1] (same), + /// fail[t2] (other)}. Every combination, against the model. + #[test] + fn introduced_and_cured_match_the_real_derivation() { + let heads = [None, Some(vec!["t1"])]; + let tips: [Option>; 4] = [None, Some(vec![]), Some(vec!["t1"]), Some(vec!["t2"])]; + // tip: None = no observation, Some([]) = Pass, Some(ids) = Fail(ids) + let mbs: [Option>; 3] = [Some(vec![]), Some(vec!["t1"]), Some(vec!["t2"])]; + let mut n = 0; + for head in &heads { + for tip in &tips { + for mb in &mbs { + let mut ev = fixture("pr-broken-new-failure"); + let obs = |ids: &Option>| match ids { + None => json!({"state":"Absent"}), + Some(v) if v.is_empty() => pass(), + Some(v) => fail(v), + }; + ev["obs"]["head"]["test"] = match head { + None => pass(), + Some(v) => fail(v), + }; + ev["obs"]["base_tip"]["test"] = obs(tip); + ev["obs"]["merge_base"]["test"] = obs(&mb.clone()); + ev["obs"]["base_nearest"]["test"] = match tip { + None => Value::Null, + Some(_) => json!({"sha": "b".repeat(40), "obs": obs(tip)}), + }; + + let head_fail = head.is_some(); + let tip_pass = matches!(tip, Some(v) if v.is_empty()); + let tip_fail = matches!(tip, Some(v) if !v.is_empty()); + let inherits = matches!(tip, Some(v) if v == &vec!["t1"]); + let mb_inherits = matches!(mb, Some(v) if v == &vec!["t1"]); + let cured_m = cured(head_fail, mb_inherits, tip_pass); + let model = introduced(head_fail, cured_m, tip_pass, tip_fail, inherits); + assert_eq!( + model, + real_blames(&ev), + "model vs derive: head={head:?} tip={tip:?} mb={mb:?}" + ); + if head_fail && (cured_m || (tip_fail && inherits)) { + assert!(!no_blame_for_the_base( + head_fail, cured_m, tip_pass, tip_fail, inherits + )); + assert!( + !real_blames(&ev), + "property 3 violated by the real derivation: {tip:?} {mb:?}" + ); + } + n += 1; + } + } + } + assert_eq!(n, 24); + } + + /// Property 5: for each infra kind, whether the PR edits the producing + /// workflow and whether that workflow starts on the base tip. + #[test] + fn infra_blame_matches_the_real_derivation() { + let kinds = [ + "StartupFailure", + "Billing", + "RunnerUnavailable", + "RateLimited", + "Timeout", + ]; + for (code, kind) in kinds.iter().enumerate() { + for edits in [false, true] { + for starts in [false, true] { + let mut ev = fixture("pr-broken-workflow-cannot-start"); + ev["obs"]["head"]["test"] = json!({"state":"Infra","infra_kind":kind}); + if !edits { + ev["pull"]["files"] = + json!([{"filename":"src/lib.rs","status":"modified"}]); + } + ev["workflow_runs"]["base_tip"][0]["startup_failure"] = json!(!starts); + let model = pr_broken_by_infra(code as u8, edits, starts); + assert_eq!( + model, + real_blames(&ev), + "kind={kind} edits={edits} starts={starts}" + ); + if code as u8 != STARTUP_FAILURE { + assert!(!real_blames(&ev), "{kind} must never be PR-BROKEN"); + } + } + } + } + let _ = (BILLING, RUNNER_UNAVAILABLE, RATE_LIMITED, TIMEOUT); + } + + /// Property 4 (replayable): the derivation is a function of the record + /// alone. Two reads of the same bytes give the same verdict, byte for byte. + #[test] + fn replaying_a_serialised_record_gives_the_same_verdict() { + for name in [ + "ci-broken-inherited", + "pr-broken-new-failure", + "ready-clean", + "unsettled-pending-within-deadline", + ] { + let ev = fixture(name); + let a: Evidence = serde_json::from_str(&ev.to_string()).unwrap(); + let b: Evidence = serde_json::from_str(&ev.to_string()).unwrap(); + assert_eq!( + serde_json::to_string(&derive(&a)).unwrap(), + serde_json::to_string(&derive(&b)).unwrap(), + "{name}" + ); + } } }