| // 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. | |
| //! `anneal-cpp-diff`: C11/C++20 operational-semantics differential oracle | |
| //! comparing `Trusted.CppSemantics` against native compilers (`clang`/`clang++` | |
| //! with UBSan+ASan and optional `gcc`/`g++` with UBSan). | |
| #![forbid(unsafe_code)] | |
| pub mod compiler_leg; | |
| pub mod corpus; | |
| pub mod lean_driver; | |
| pub mod report; | |
| pub mod verdict; | |
| use std::{ | |
| collections::BTreeSet, | |
| env, fs, | |
| path::{Path, PathBuf}, | |
| sync::atomic::{AtomicU64, Ordering}, | |
| time::{SystemTime, UNIX_EPOCH}, | |
| }; | |
| use crate::{ | |
| compiler_leg::{run_compiler_case, CompilerLegConfig}, | |
| corpus::CorpusCase, | |
| lean_driver::{run_lean_leg, LeanLegConfig}, | |
| report::{CaseReport, DiffReport}, | |
| verdict::{classify_case, LegOutcome}, | |
| }; | |
| static TEMP_DIR_COUNTER: AtomicU64 = AtomicU64::new(0); | |
| /// End-to-end execution configuration for [`run_suite`]. | |
| #[derive(Clone, Debug)] | |
| pub struct SuiteConfig { | |
| pub lean: LeanLegConfig, | |
| pub primary_compiler: CompilerLegConfig, | |
| pub secondary_compiler: Option<CompilerLegConfig>, | |
| pub keep_temp: bool, | |
| } | |
| struct TempWorkDir { | |
| path: PathBuf, | |
| keep: bool, | |
| } | |
| impl TempWorkDir { | |
| fn create(keep: bool) -> Result<Self, String> { | |
| let nanos = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0); | |
| let seq = TEMP_DIR_COUNTER.fetch_add(1, Ordering::Relaxed); | |
| let pid = std::process::id(); | |
| let path = env::temp_dir().join(format!("anneal-cpp-diff-{pid}-{nanos}-{seq}")); | |
| fs::create_dir_all(&path) | |
| .map_err(|e| format!("cannot create temp work dir {}: {e}", path.display()))?; | |
| Ok(Self { path, keep }) | |
| } | |
| fn path(&self) -> &Path { | |
| &self.path | |
| } | |
| } | |
| impl Drop for TempWorkDir { | |
| fn drop(&mut self) { | |
| if !self.keep { | |
| let _ = fs::remove_dir_all(&self.path); | |
| } | |
| } | |
| } | |
| /// Run the full differential oracle over `cases` through both the Lean | |
| /// `Trusted.CppSemantics` leg and the native compiler/UBSan leg(s). | |
| pub fn run_suite(cfg: &SuiteConfig, cases: &[CorpusCase]) -> Result<DiffReport, String> { | |
| let mut seen_ids = BTreeSet::new(); | |
| for c in cases { | |
| c.validate()?; | |
| if !seen_ids.insert(c.id.as_str()) { | |
| return Err(format!("duplicate corpus case id {:?}", c.id)); | |
| } | |
| } | |
| let work = TempWorkDir::create(cfg.keep_temp)?; | |
| let lean_outcomes = run_lean_leg(&cfg.lean, cases, work.path())?; | |
| let mut reports = Vec::with_capacity(cases.len()); | |
| for case in cases { | |
| let lean_out = lean_outcomes.get(&case.id).cloned().unwrap_or_else(|| { | |
| LegOutcome::ToolError { detail: format!("missing Lean outcome for {}", case.id) } | |
| }); | |
| let primary_out = run_compiler_case(&cfg.primary_compiler, case, work.path()); | |
| let secondary_out = | |
| cfg.secondary_compiler.as_ref().map(|sec| run_compiler_case(sec, case, work.path())); | |
| let verdict = | |
| classify_case(&case.verdict_spec(), &lean_out, &primary_out, secondary_out.as_ref()); | |
| reports.push(CaseReport { | |
| id: case.id.clone(), | |
| category: case.category, | |
| language: case.language, | |
| expectation: case.expectation, | |
| verdict, | |
| lean_outcome: lean_out, | |
| primary_compiler_outcome: primary_out, | |
| secondary_compiler_outcome: secondary_out, | |
| }); | |
| } | |
| Ok(DiffReport::new( | |
| cfg.primary_compiler.label.clone(), | |
| cfg.secondary_compiler.as_ref().map(|c| c.label.clone()), | |
| reports, | |
| )) | |
| } | |
| #[cfg(test)] | |
| mod tests { | |
| use std::collections::BTreeSet; | |
| use anneal_lower_cpp::LowerError; | |
| use crate::{ | |
| compiler_leg::{ | |
| classify_exit, detect_sanitizer_ub, render_harness, COMPILER_RESULT_PREFIX, | |
| }, | |
| corpus::{default_corpus, Category, Language}, | |
| lean_driver::{ | |
| absolutize_executable, absolutize_path, classify_lower_error, | |
| compute_trusted_cache_key, parse_driver_output, parse_single_payload, | |
| render_batch_driver, trusted_cache_is_fresh, PreparedLeanCase, LEAN_RESULT_PREFIX, | |
| TRUSTED_CACHE_STAMP_FILE, TRUSTED_CPP_MODULES, | |
| }, | |
| report::{CaseReport, DiffReport, REPORT_SCHEMA}, | |
| verdict::{ | |
| classify_case, CaseExpectation, CppIntWidth, CppScalarValue, CppSign, LegOutcome, | |
| UnsupportedStage, Verdict, VerdictSpec, | |
| }, | |
| TempWorkDir, | |
| }; | |
| #[test] | |
| fn corpus_has_at_least_45_valid_unique_cases_covering_all_categories() { | |
| let corpus = default_corpus(); | |
| assert!(corpus.len() >= 45, "expected >= 45 corpus cases, got {}", corpus.len()); | |
| let mut ids = BTreeSet::new(); | |
| let mut categories = BTreeSet::new(); | |
| let mut defined_count = 0usize; | |
| let mut ub_count = 0usize; | |
| let mut unsup_count = 0usize; | |
| let mut c11_count = 0usize; | |
| let mut cpp20_count = 0usize; | |
| for case in &corpus { | |
| case.validate().unwrap_or_else(|e| panic!("invalid case {}: {e}", case.id)); | |
| assert!(ids.insert(case.id.clone()), "duplicate case id {}", case.id); | |
| categories.insert(format!("{:?}", case.category)); | |
| match case.expectation { | |
| CaseExpectation::Defined => defined_count += 1, | |
| CaseExpectation::StrictUb => ub_count += 1, | |
| CaseExpectation::ExpectedUnsupported => unsup_count += 1, | |
| } | |
| match case.language { | |
| Language::C11 => c11_count += 1, | |
| Language::Cpp20 => cpp20_count += 1, | |
| } | |
| let harness = render_harness(case) | |
| .unwrap_or_else(|e| panic!("harness failed for {}: {e}", case.id)); | |
| assert!(harness.contains(&case.entry)); | |
| assert!(harness.contains(COMPILER_RESULT_PREFIX)); | |
| } | |
| assert!(defined_count >= 20, "expected >= 20 defined cases, got {defined_count}"); | |
| assert!(ub_count >= 12, "expected >= 12 strict_ub cases, got {ub_count}"); | |
| assert!(unsup_count >= 8, "expected >= 8 expected_unsupported cases, got {unsup_count}"); | |
| assert!(c11_count >= 35, "expected >= 35 C11 cases, got {c11_count}"); | |
| assert!(cpp20_count >= 4, "expected >= 4 C++20 cases, got {cpp20_count}"); | |
| assert_eq!(categories.len(), 12, "all 12 semantic categories must be represented"); | |
| } | |
| #[test] | |
| fn scalar_value_round_trip_and_c_expr_edge_cases() { | |
| let samples = [ | |
| ("void", CppScalarValue::Void), | |
| ("bool:true", CppScalarValue::bool_val(true)), | |
| ("bool:false", CppScalarValue::bool_val(false)), | |
| ("int:w8:signed:255", CppScalarValue::i8(-1)), | |
| ("int:w8:unsigned:255", CppScalarValue::u8(255)), | |
| ("int:w16:unsigned:65535", CppScalarValue::u16(65535)), | |
| ("int:w32:signed:2147483648", CppScalarValue::i32(i32::MIN)), | |
| ("int:w32:unsigned:4294967295", CppScalarValue::u32(u32::MAX)), | |
| ("int:w64:signed:9223372036854775808", CppScalarValue::i64(i64::MIN)), | |
| ("int:w64:unsigned:18446744073709551615", CppScalarValue::u64(u64::MAX)), | |
| ]; | |
| for (tagged, expected) in samples { | |
| let parsed = CppScalarValue::parse_tagged(tagged).unwrap(); | |
| assert_eq!(parsed, expected); | |
| } | |
| assert_eq!(CppScalarValue::i32(i32::MIN).to_c_expr().unwrap(), "(-2147483647 - 1)"); | |
| assert_eq!( | |
| CppScalarValue::i64(i64::MIN).to_c_expr().unwrap(), | |
| "(-9223372036854775807LL - 1LL)" | |
| ); | |
| assert!(CppScalarValue::Void.to_c_expr().is_err()); | |
| assert!(CppScalarValue::parse_tagged("int:w8:unsigned:256").is_err()); | |
| assert_eq!( | |
| CppScalarValue::Int { | |
| width: CppIntWidth::W32, | |
| sign: CppSign::Signed, | |
| unsigned_bits: 42, | |
| } | |
| .to_lean_val(), | |
| ".int .w32 .signed 42" | |
| ); | |
| } | |
| #[test] | |
| fn verdict_classifies_defined_match_and_mismatches() { | |
| let forty_two = CppScalarValue::i32(42); | |
| let forty_three = CppScalarValue::i32(43); | |
| let spec = VerdictSpec { | |
| expectation: CaseExpectation::Defined, | |
| expected_return: Some(&forty_two), | |
| ubsan_detectable: false, | |
| expected_unsupported_stage: None, | |
| }; | |
| let ok = classify_case( | |
| &spec, | |
| &LegOutcome::Returned { value: forty_two.clone() }, | |
| &LegOutcome::Returned { value: forty_two.clone() }, | |
| Some(&LegOutcome::Returned { value: forty_two.clone() }), | |
| ); | |
| assert_eq!(ok, Verdict::DefinedMatch { value: forty_two.clone() }); | |
| assert!(ok.is_pass()); | |
| // Value mismatch between Lean and compiler fails as DefinedMismatch. | |
| let mismatch = classify_case( | |
| &spec, | |
| &LegOutcome::Returned { value: forty_three.clone() }, | |
| &LegOutcome::Returned { value: forty_two.clone() }, | |
| None, | |
| ); | |
| assert!(matches!(mismatch, Verdict::DefinedMismatch { .. })); | |
| assert!(!mismatch.is_pass()); | |
| // Compiler UBSan trap while Lean returned a value is unsound missed UB. | |
| let missed_ub = classify_case( | |
| &spec, | |
| &LegOutcome::Returned { value: forty_two.clone() }, | |
| &LegOutcome::UndefinedBehavior { detail: "signed integer overflow".into() }, | |
| None, | |
| ); | |
| assert!(matches!(missed_ub, Verdict::DefinedMismatch { .. })); | |
| // False-positive UB in Lean on a defined case fails as DefinedMismatch. | |
| let false_ub = classify_case( | |
| &spec, | |
| &LegOutcome::UndefinedBehavior { detail: "false positive".into() }, | |
| &LegOutcome::Returned { value: forty_two.clone() }, | |
| None, | |
| ); | |
| assert!(matches!(false_ub, Verdict::DefinedMismatch { .. })); | |
| // Compiler disagreement fails as DefinedMismatch. | |
| let cc_disagree = classify_case( | |
| &spec, | |
| &LegOutcome::Returned { value: forty_two.clone() }, | |
| &LegOutcome::Returned { value: forty_two.clone() }, | |
| Some(&LegOutcome::Returned { value: forty_three }), | |
| ); | |
| assert!(matches!(cc_disagree, Verdict::DefinedMismatch { .. })); | |
| } | |
| #[test] | |
| fn verdict_classifies_strict_ub_and_rejects_vacuous_or_missed_ub() { | |
| let ub_spec = VerdictSpec { | |
| expectation: CaseExpectation::StrictUb, | |
| expected_return: None, | |
| ubsan_detectable: true, | |
| expected_unsupported_stage: None, | |
| }; | |
| let confirmed = classify_case( | |
| &ub_spec, | |
| &LegOutcome::UndefinedBehavior { detail: "overflow".into() }, | |
| &LegOutcome::UndefinedBehavior { detail: "runtime error: signed overflow".into() }, | |
| None, | |
| ); | |
| assert!(matches!(confirmed, Verdict::StrictUbConfirmed { ubsan_confirmed: true, .. })); | |
| // If UBSan should have caught it but didn't, fail as UnexpectedError. | |
| let missed_ubsan = classify_case( | |
| &ub_spec, | |
| &LegOutcome::UndefinedBehavior { detail: "overflow".into() }, | |
| &LegOutcome::Returned { value: CppScalarValue::i32(0) }, | |
| None, | |
| ); | |
| assert!(matches!(missed_ubsan, Verdict::UnexpectedError { .. })); | |
| // When ubsan_detectable is false, Lean UB counts as StrictUbConfirmed. | |
| let beyond_spec = VerdictSpec { ubsan_detectable: false, ..ub_spec }; | |
| let lean_stricter = classify_case( | |
| &beyond_spec, | |
| &LegOutcome::UndefinedBehavior { detail: "uninit read".into() }, | |
| &LegOutcome::Returned { value: CppScalarValue::i32(0) }, | |
| None, | |
| ); | |
| assert!(matches!(lean_stricter, Verdict::StrictUbConfirmed { ubsan_confirmed: false, .. })); | |
| // Lean returning defined when UBSan trapped fails as DefinedMismatch. | |
| let unsound = classify_case( | |
| &ub_spec, | |
| &LegOutcome::Returned { value: CppScalarValue::i32(-2147483648) }, | |
| &LegOutcome::UndefinedBehavior { detail: "runtime error".into() }, | |
| None, | |
| ); | |
| assert!(matches!(unsound, Verdict::DefinedMismatch { .. })); | |
| } | |
| #[test] | |
| fn verdict_classifies_expected_unsupported_and_stage_enforcement() { | |
| let spec = VerdictSpec { | |
| expectation: CaseExpectation::ExpectedUnsupported, | |
| expected_return: None, | |
| ubsan_detectable: false, | |
| expected_unsupported_stage: Some(UnsupportedStage::Step), | |
| }; | |
| let ok = classify_case( | |
| &spec, | |
| &LegOutcome::Unsupported { | |
| stage: UnsupportedStage::Step, | |
| detail: "negative shift lhs".into(), | |
| }, | |
| &LegOutcome::UndefinedBehavior { detail: "shift of negative value".into() }, | |
| None, | |
| ); | |
| assert!(matches!(ok, Verdict::ExpectedUnsupported { stage: UnsupportedStage::Step, .. })); | |
| // Wrong unsupported stage fails closed. | |
| let wrong_stage = classify_case( | |
| &spec, | |
| &LegOutcome::Unsupported { | |
| stage: UnsupportedStage::Init, | |
| detail: "init rejected".into(), | |
| }, | |
| &LegOutcome::Returned { value: CppScalarValue::i32(0) }, | |
| None, | |
| ); | |
| assert!(matches!(wrong_stage, Verdict::UnexpectedError { .. })); | |
| // Lowering error classification distinguishes unsupported vs malformed. | |
| assert!(matches!( | |
| classify_lower_error(&LowerError::UnsequencedSideEffects { detail: "x".into() }), | |
| LegOutcome::Unsupported { stage: UnsupportedStage::Lowering, .. } | |
| )); | |
| assert!(matches!( | |
| classify_lower_error(&LowerError::Malformed { detail: "bug".into() }), | |
| LegOutcome::ToolError { .. } | |
| )); | |
| } | |
| #[test] | |
| fn lean_driver_generation_and_output_parsing() { | |
| let cases = vec![ | |
| PreparedLeanCase { | |
| case_id: "c11_add".into(), | |
| module_name: "CppDiffCase0".into(), | |
| args: vec![CppScalarValue::i32(19), CppScalarValue::i32(23)], | |
| }, | |
| PreparedLeanCase { | |
| case_id: "c11_ub".into(), | |
| module_name: "CppDiffCase1".into(), | |
| args: vec![CppScalarValue::i32(i32::MAX), CppScalarValue::i32(1)], | |
| }, | |
| ]; | |
| let driver = render_batch_driver(&cases, 500); | |
| assert!(driver.contains("import CppDiffCase0")); | |
| assert!(driver.contains("import CppDiffCase1")); | |
| assert!(driver.contains("runCppProgram ``CppDiffCase0.program")); | |
| assert!(driver.contains(".int .w32 .signed 19")); | |
| assert!(driver.contains("500")); | |
| let fake_stdout = format!( | |
| "noise line\n\ | |
| {LEAN_RESULT_PREFIX}c11_add:DONE:int:Trusted.CppAst.CppIntWidth.w32:Trusted.CppAst.Signedness.signed:42:steps=7\n\ | |
| {LEAN_RESULT_PREFIX}c11_ub:FAULT:undefined:steps=4\n\ | |
| {LEAN_RESULT_PREFIX}c11_unsup:FAULT:unsupported:steps=3\n\ | |
| {LEAN_RESULT_PREFIX}c11_init:INIT_UNSUPPORTED\n\ | |
| {LEAN_RESULT_PREFIX}c11_fuel:OUT_OF_FUEL:500\n" | |
| ); | |
| let parsed = parse_driver_output(&fake_stdout); | |
| assert_eq!( | |
| parsed.get("c11_add"), | |
| Some(&LegOutcome::Returned { value: CppScalarValue::i32(42) }) | |
| ); | |
| assert!(matches!(parsed.get("c11_ub"), Some(LegOutcome::UndefinedBehavior { .. }))); | |
| assert!(matches!( | |
| parsed.get("c11_unsup"), | |
| Some(LegOutcome::Unsupported { stage: UnsupportedStage::Step, .. }) | |
| )); | |
| assert!(matches!( | |
| parsed.get("c11_init"), | |
| Some(LegOutcome::Unsupported { stage: UnsupportedStage::Init, .. }) | |
| )); | |
| assert_eq!(parsed.get("c11_fuel"), Some(&LegOutcome::OutOfFuel { fuel: 500 })); | |
| assert!(matches!(parse_single_payload("STUCK:foo"), LegOutcome::ToolError { .. })); | |
| } | |
| #[test] | |
| fn json_report_serialization_and_sanitizer_detection() { | |
| assert!(detect_sanitizer_ub( | |
| "foo.c:2:10: runtime error: signed integer overflow: 2147483647 + 1" | |
| ) | |
| .is_some()); | |
| assert!(detect_sanitizer_ub( | |
| "==123==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x0" | |
| ) | |
| .is_some()); | |
| assert!(detect_sanitizer_ub("normal compiler stderr").is_none()); | |
| let report = DiffReport::new( | |
| "clang-19", | |
| Some("g++".into()), | |
| vec![ | |
| CaseReport { | |
| id: "c11_add_i32_defined".into(), | |
| category: Category::IntPromotionsAndWrap, | |
| language: Language::C11, | |
| expectation: CaseExpectation::Defined, | |
| verdict: Verdict::DefinedMatch { value: CppScalarValue::i32(42) }, | |
| lean_outcome: LegOutcome::Returned { value: CppScalarValue::i32(42) }, | |
| primary_compiler_outcome: LegOutcome::Returned { | |
| value: CppScalarValue::i32(42), | |
| }, | |
| secondary_compiler_outcome: Some(LegOutcome::Returned { | |
| value: CppScalarValue::i32(42), | |
| }), | |
| }, | |
| CaseReport { | |
| id: "c11_bad".into(), | |
| category: Category::Shifts, | |
| language: Language::C11, | |
| expectation: CaseExpectation::Defined, | |
| verdict: Verdict::DefinedMismatch { detail: "mismatch".into() }, | |
| lean_outcome: LegOutcome::Returned { value: CppScalarValue::i32(1) }, | |
| primary_compiler_outcome: LegOutcome::Returned { | |
| value: CppScalarValue::i32(2), | |
| }, | |
| secondary_compiler_outcome: None, | |
| }, | |
| ], | |
| ); | |
| assert_eq!(report.schema, REPORT_SCHEMA); | |
| assert_eq!(report.summary.total, 2); | |
| assert_eq!(report.summary.passed, 1); | |
| assert_eq!(report.summary.failed, 1); | |
| assert!(!report.summary.all_passed()); | |
| let json = report.to_json_pretty().unwrap(); | |
| let round_trip: DiffReport = serde_json::from_str(&json).unwrap(); | |
| assert_eq!(round_trip, report); | |
| let human = report.format_human(); | |
| assert!(human.contains("DEFINED_MISMATCH")); | |
| assert!(human.contains("c11_bad")); | |
| } | |
| #[test] | |
| fn trusted_olean_cache_key_invalidates_on_source_or_version_change() { | |
| let tmp = TempWorkDir::create(false).unwrap(); | |
| let repo = tmp.path().join("repo"); | |
| let cache = tmp.path().join("cache"); | |
| for module in TRUSTED_CPP_MODULES { | |
| let rel = module.replace('.', "/"); | |
| let src = repo.join("trusted/lean").join(format!("{rel}.lean")); | |
| std::fs::create_dir_all(src.parent().unwrap()).unwrap(); | |
| std::fs::write(&src, format!("-- {module}\n")).unwrap(); | |
| let olean = cache.join(format!("{rel}.olean")); | |
| std::fs::create_dir_all(olean.parent().unwrap()).unwrap(); | |
| std::fs::write(&olean, b"fake-olean").unwrap(); | |
| } | |
| std::fs::write(repo.join("trusted/lean/lean-toolchain"), "leanprover/lean4:v4.35.0\n") | |
| .unwrap(); | |
| let key1 = compute_trusted_cache_key(&repo, "Lean (version 4.35.0)").unwrap(); | |
| // Without a stamp file, bare `.olean` existence is never fresh. | |
| assert!(!trusted_cache_is_fresh(&cache, &key1)); | |
| std::fs::write(cache.join(TRUSTED_CACHE_STAMP_FILE), format!("{key1}\n")).unwrap(); | |
| assert!(trusted_cache_is_fresh(&cache, &key1)); | |
| // Changing Lean version invalidates the cache key. | |
| let key_ver = compute_trusted_cache_key(&repo, "Lean (version 4.36.0)").unwrap(); | |
| assert_ne!(key1, key_ver); | |
| assert!(!trusted_cache_is_fresh(&cache, &key_ver)); | |
| // Editing any Trusted.CppSemantics source file invalidates the cache. | |
| std::fs::write(repo.join("trusted/lean/Trusted/CppSemantics/Eval.lean"), "-- edited\n") | |
| .unwrap(); | |
| let key_edited = compute_trusted_cache_key(&repo, "Lean (version 4.35.0)").unwrap(); | |
| assert_ne!(key1, key_edited); | |
| assert!(!trusted_cache_is_fresh(&cache, &key_edited)); | |
| } | |
| #[test] | |
| fn compiler_compile_errors_fail_closed_for_strict_ub_and_unsupported() { | |
| let lean_ub = LegOutcome::UndefinedBehavior { detail: "const write".into() }; | |
| let cc_err = LegOutcome::CompileError { detail: "syntax error".into() }; | |
| let cc_ub = LegOutcome::UndefinedBehavior { detail: "runtime error: overflow".into() }; | |
| // ubsan_detectable=false must still reject compiler CompileErrors. | |
| let beyond_spec = VerdictSpec { | |
| expectation: CaseExpectation::StrictUb, | |
| expected_return: None, | |
| ubsan_detectable: false, | |
| expected_unsupported_stage: None, | |
| }; | |
| assert!(matches!( | |
| classify_case(&beyond_spec, &lean_ub, &cc_err, None), | |
| Verdict::UnexpectedError { .. } | |
| )); | |
| assert!(matches!( | |
| classify_case( | |
| &beyond_spec, | |
| &lean_ub, | |
| &LegOutcome::Returned { value: CppScalarValue::i32(0) }, | |
| Some(&cc_err), | |
| ), | |
| Verdict::UnexpectedError { .. } | |
| )); | |
| // ubsan_detectable=true must not mask secondary CompileError. | |
| let detect_spec = VerdictSpec { ubsan_detectable: true, ..beyond_spec }; | |
| assert!(matches!( | |
| classify_case(&detect_spec, &lean_ub, &cc_ub, Some(&cc_err)), | |
| Verdict::UnexpectedError { .. } | |
| )); | |
| // ExpectedUnsupported must reject secondary CompileError too. | |
| let unsup_spec = VerdictSpec { | |
| expectation: CaseExpectation::ExpectedUnsupported, | |
| expected_return: None, | |
| ubsan_detectable: false, | |
| expected_unsupported_stage: Some(UnsupportedStage::Step), | |
| }; | |
| let lean_unsup = | |
| LegOutcome::Unsupported { stage: UnsupportedStage::Step, detail: "neg shift".into() }; | |
| assert!(matches!( | |
| classify_case( | |
| &unsup_spec, | |
| &lean_unsup, | |
| &LegOutcome::Returned { value: CppScalarValue::i32(0) }, | |
| Some(&cc_err), | |
| ), | |
| Verdict::UnexpectedError { .. } | |
| )); | |
| } | |
| #[test] | |
| fn classify_exit_separates_infra_kills_and_asan_init_from_real_ub() { | |
| // External kills and bare SIGABRT without sanitizer diag are ToolError. | |
| for sig in [6, 9, 15, 24] { | |
| assert!( | |
| matches!(classify_exit(None, Some(sig), "", ""), LegOutcome::ToolError { .. }), | |
| "bare signal {sig} should be ToolError" | |
| ); | |
| } | |
| // ASan startup failures die via SIGABRT (6) under | |
| // ASAN_OPTIONS=abort_on_error=1 and exit 1 without it; both fail. | |
| let asan_shadow_err = | |
| "==123==ERROR: AddressSanitizer: Shadow memory range interleaves with an existing memory mapping.\n"; | |
| let asan_alloc_err = "==123==ERROR: AddressSanitizer failed to allocate 0x200000 bytes\n"; | |
| for err in [asan_shadow_err, asan_alloc_err] { | |
| assert!(detect_sanitizer_ub(err).is_none()); | |
| assert!(matches!(classify_exit(None, Some(6), "", err), LegOutcome::ToolError { .. })); | |
| assert!(matches!(classify_exit(Some(1), None, "", err), LegOutcome::ToolError { .. })); | |
| } | |
| // Generic "Sanitizer" stderr without a bug diagnostic is ToolError. | |
| assert!(matches!( | |
| classify_exit(None, Some(6), "", "AddressSanitizer: failed to initialize"), | |
| LegOutcome::ToolError { .. } | |
| )); | |
| // Hardware fault signals (SIGFPE=8, SIGSEGV=11) map to UB. | |
| assert!(matches!( | |
| classify_exit(None, Some(8), "", ""), | |
| LegOutcome::UndefinedBehavior { .. } | |
| )); | |
| assert!(matches!( | |
| classify_exit(None, Some(11), "", ""), | |
| LegOutcome::UndefinedBehavior { .. } | |
| )); | |
| // Real UBSan / ASan diagnostics (including SIGABRT exit) map to UB. | |
| assert!(matches!( | |
| classify_exit(Some(1), None, "", "test.c:3:10: runtime error: division by zero\n"), | |
| LegOutcome::UndefinedBehavior { .. } | |
| )); | |
| assert!(matches!( | |
| classify_exit( | |
| None, | |
| Some(6), | |
| "", | |
| "==123==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x0\n" | |
| ), | |
| LegOutcome::UndefinedBehavior { .. } | |
| )); | |
| } | |
| #[test] | |
| fn absolutize_paths_preserves_bare_commands_and_resolves_relative_paths() { | |
| let cwd = std::path::Path::new("/work/repo"); | |
| assert_eq!( | |
| absolutize_path(std::path::Path::new("./lean-cache"), cwd), | |
| std::path::PathBuf::from("/work/repo/./lean-cache") | |
| ); | |
| assert_eq!( | |
| absolutize_path(std::path::Path::new("/abs/cache"), cwd), | |
| std::path::PathBuf::from("/abs/cache") | |
| ); | |
| assert_eq!( | |
| absolutize_executable(std::path::Path::new("lean"), cwd), | |
| std::path::PathBuf::from("lean") | |
| ); | |
| assert_eq!( | |
| absolutize_executable(std::path::Path::new("./bin/lean"), cwd), | |
| std::path::PathBuf::from("/work/repo/./bin/lean") | |
| ); | |
| assert_eq!( | |
| absolutize_executable(std::path::Path::new("/usr/bin/lean"), cwd), | |
| std::path::PathBuf::from("/usr/bin/lean") | |
| ); | |
| } | |
| #[test] | |
| fn lean_driver_fails_closed_on_stuck_bool_return() { | |
| let driver = render_batch_driver( | |
| &[PreparedLeanCase { | |
| case_id: "c11_bool".into(), | |
| module_name: "CppDiffCase0".into(), | |
| args: vec![], | |
| }], | |
| 100, | |
| ); | |
| assert!(driver.contains("Bool.true")); | |
| assert!(driver.contains("Bool.false")); | |
| assert!(driver.contains("STUCK_BOOL:")); | |
| assert!(matches!(parse_single_payload("STUCK_BOOL:foo"), LegOutcome::ToolError { .. })); | |
| assert_eq!( | |
| parse_single_payload("DONE:bool:false:steps=3"), | |
| LegOutcome::Returned { value: CppScalarValue::bool_val(false) } | |
| ); | |
| } | |
| } |