| // Copyright 2026 The Fuchsia Authors | |
| // | |
| // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 | |
| // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT | |
| // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. | |
| // This file may not be copied, modified, or distributed except according to | |
| // those terms. | |
| //! Self-check AST-mutated program probes (`--self-check-oracle`). | |
| //! | |
| //! Each corpus-linked mutant rewrites a captured MIR body's AST (operator or | |
| //! local integer signedness) and evaluates the mutated program under the | |
| //! unmodified Lean `evalMirFuelBy` evaluator against real corpus Miri | |
| //! reference outcomes (using arguments derived directly from the manifest). | |
| //! A separate `synthetic_lean_probes` entry exercises a Lean-only `Use.move` | |
| //! deinitialization program without polluting corpus kill percentages. Guard | |
| //! mutations of the Lean TCB itself are checked separately by | |
| //! `ci/check_guard_mutation.py`. | |
| use std::{collections::BTreeMap, path::PathBuf}; | |
| use serde::{Deserialize, Serialize}; | |
| use crate::{ | |
| corpus::LitmusCase, | |
| lean_runner::{CapturedModule, LeanSession}, | |
| verdict::{Expectation, ObservedOutcome, evaluate_case}, | |
| }; | |
| mod probes; | |
| const MUTANT_PROBE_TIMEOUT_SECS: u64 = 180; | |
| /// Report covering a single corpus-linked AST-mutated oracle probe. | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| pub struct MutantResult { | |
| pub mutant_id: String, | |
| pub target_case_id: String, | |
| pub description: String, | |
| pub killed: bool, | |
| pub expected_kill_verdict: String, | |
| pub actual_verdict: String, | |
| } | |
| /// Report covering a synthetic Lean-only semantics self-probe (not counted in | |
| /// corpus mutant kill percentages). | |
| #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| pub struct SyntheticProbeResult { | |
| pub probe_id: String, | |
| pub description: String, | |
| pub passed: bool, | |
| pub expected_kill_verdict: String, | |
| pub actual_verdict: String, | |
| } | |
| /// Summary report across all Stage-1 oracle self-check mutants and synthetic | |
| /// probes. | |
| #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] | |
| pub struct MutantSuiteReport { | |
| pub total_mutants: usize, | |
| pub killed_mutants: usize, | |
| pub killed_pct: f64, | |
| pub results: Vec<MutantResult>, | |
| pub synthetic_lean_probes: Vec<SyntheticProbeResult>, | |
| } | |
| struct MutantSpec { | |
| id: &'static str, | |
| target_case_id: &'static str, | |
| description: &'static str, | |
| expected_kill: &'static str, | |
| } | |
| const MUTANT_SPECS: &[MutantSpec] = &[ | |
| MutantSpec { | |
| id: "mutant_unchecked_add_wraps", | |
| target_case_id: "ub_unchecked_add_u32_ovf", | |
| description: "AST rewrite replaces SysrootCall uncheckedAdd with wrappingAdd so u32::MAX + 1 returns 0 instead of faulting UB", | |
| expected_kill: "UnsoundMissedFault", | |
| }, | |
| MutantSpec { | |
| id: "mutant_unchecked_sub_wraps", | |
| target_case_id: "ub_unchecked_sub_i32_ovf", | |
| description: "AST rewrite replaces SysrootCall uncheckedSub with wrappingSub so i32::MIN - 1 returns i32::MAX instead of faulting UB", | |
| expected_kill: "UnsoundMissedFault", | |
| }, | |
| MutantSpec { | |
| id: "mutant_unchecked_mul_wraps", | |
| target_case_id: "ub_unchecked_mul_i8_ovf", | |
| description: "AST rewrite replaces SysrootCall uncheckedMul with wrappingMul so 64i8 * 4 returns 0 instead of faulting UB", | |
| expected_kill: "UnsoundMissedFault", | |
| }, | |
| MutantSpec { | |
| id: "mutant_shl_swapped_to_shr", | |
| target_case_id: "arith_u32_shl_over_width", | |
| description: "AST rewrite swaps BinaryOp::Shl to Shr so 1u32 << 35 evaluates 1u32 >> 35 (0 instead of 8)", | |
| expected_kill: "ValueMismatch", | |
| }, | |
| MutantSpec { | |
| id: "mutant_int_to_int_drops_sign_extend", | |
| target_case_id: "cast_char_bool_int_branch_false", | |
| description: "AST rewrite narrows i32 cast target/local to unsigned u16 so widening negative i16 zero-extends instead of sign-extending", | |
| expected_kill: "ValueMismatch", | |
| }, | |
| ]; | |
| /// Run all 5 manifest-derived AST-mutated probes against real Miri outcomes | |
| /// (plus the standalone `moveDeinitProg` Lean probe) and verify 100% kills. | |
| pub fn run_self_check_mutants( | |
| lean: &LeanSession, | |
| captured_by_source: &BTreeMap<PathBuf, CapturedModule>, | |
| cases: &[LitmusCase], | |
| miri_outcomes: &BTreeMap<String, ObservedOutcome>, | |
| ) -> Result<MutantSuiteReport, String> { | |
| let mut case_by_id = BTreeMap::new(); | |
| for c in cases { | |
| case_by_id.insert(c.id.as_str(), c); | |
| } | |
| let arith_cap = find_captured_suffix(captured_by_source, "arith_pass.rs")?; | |
| let cf_cap = find_captured_suffix(captured_by_source, "control_enum_pass.rs")?; | |
| let ub_cap = find_captured_suffix(captured_by_source, "ub_corpus.rs")?; | |
| let tmp = | |
| crate::mir_eval_runner::build_temp_dir("anneal-mutant-", lean.validated_tmpdir.as_deref())?; | |
| let probe_sources = probes::build_mutant_probe_sources(arith_cap, cf_cap, ub_cap, &case_by_id)?; | |
| let raw_outcomes = | |
| probes::run_mutant_probes(lean, tmp.path(), &probe_sources, arith_cap, cf_cap, ub_cap)?; | |
| let mut results = Vec::with_capacity(MUTANT_SPECS.len()); | |
| let mut killed = 0usize; | |
| for spec in MUTANT_SPECS { | |
| let mutant_lean = | |
| raw_outcomes.get(spec.id).cloned().unwrap_or_else(|| ObservedOutcome::ToolError { | |
| detail: format!("missing Lean mutant result for {}", spec.id), | |
| }); | |
| let case = case_by_id | |
| .get(spec.target_case_id) | |
| .ok_or_else(|| format!("missing corpus case {:?}", spec.target_case_id))?; | |
| let miri_out = miri_outcomes.get(spec.target_case_id).ok_or_else(|| { | |
| format!("missing Miri reference outcome for mutant target {:?}", spec.target_case_id) | |
| })?; | |
| let verdict = evaluate_case( | |
| case.expected, | |
| case.expected_return.as_ref(), | |
| case.miri_ub_contains.as_deref(), | |
| case.expected_miri, | |
| miri_out, | |
| &mutant_lean, | |
| None, | |
| ); | |
| let is_killed = !verdict.is_ok() && verdict.label() == spec.expected_kill; | |
| if is_killed { | |
| killed += 1; | |
| } | |
| results.push(MutantResult { | |
| mutant_id: spec.id.to_owned(), | |
| target_case_id: spec.target_case_id.to_owned(), | |
| description: spec.description.to_owned(), | |
| killed: is_killed, | |
| expected_kill_verdict: spec.expected_kill.to_owned(), | |
| actual_verdict: verdict.label().to_owned(), | |
| }); | |
| } | |
| let unmutated_move = raw_outcomes.get("ref_move_deinit").cloned().unwrap_or_else(|| { | |
| ObservedOutcome::ToolError { detail: "missing unmutated move-deinit reference".to_owned() } | |
| }); | |
| let mutated_move = | |
| raw_outcomes.get("probe_move_deinit_ignored").cloned().unwrap_or_else(|| { | |
| ObservedOutcome::ToolError { detail: "missing mutated move-deinit probe".to_owned() } | |
| }); | |
| let move_verdict = | |
| evaluate_case(Expectation::Ub, None, None, None, &unmutated_move, &mutated_move, None); | |
| let move_killed = !move_verdict.is_ok() && move_verdict.label() == "UnsoundMissedFault"; | |
| let synthetic_lean_probes = vec![SyntheticProbeResult { | |
| probe_id: "probe_move_deinit_ignored".to_owned(), | |
| description: "Synthetic Lean-only Use.move-to-Use.copy AST rewrite verifies move deinitialization fault detection".to_owned(), | |
| passed: move_killed, | |
| expected_kill_verdict: "UnsoundMissedFault".to_owned(), | |
| actual_verdict: move_verdict.label().to_owned(), | |
| }]; | |
| let total = MUTANT_SPECS.len(); | |
| let killed_pct = if total == 0 { 0.0 } else { (killed as f64) * 100.0 / (total as f64) }; | |
| Ok(MutantSuiteReport { | |
| total_mutants: total, | |
| killed_mutants: killed, | |
| killed_pct, | |
| results, | |
| synthetic_lean_probes, | |
| }) | |
| } | |
| fn find_captured_suffix<'a>( | |
| map: &'a BTreeMap<PathBuf, CapturedModule>, | |
| suffix: &str, | |
| ) -> Result<&'a CapturedModule, String> { | |
| map.iter() | |
| .find(|(p, _)| p.ends_with(suffix)) | |
| .map(|(_, c)| c) | |
| .ok_or_else(|| format!("missing captured module for {suffix} in mutant self-check")) | |
| } |