From c93c2080445324b69750867266f8aec2ab82b10c Mon Sep 17 00:00:00 2001 From: Srivatsan Samraj <321934658+srivatsansamraj@users.noreply.github.com> Date: Mon, 5 Oct 2026 15:25:23 -0700 Subject: [PATCH 1/2] Fail an autoharness harness that no generated input reaches Under --constructor-args, autoharness generates a private-field type through one of its constructors, turning an assert-guarded constructor's assertions and a checked constructor's None or Err return into assumptions, and it assumes the type's mined invariant conditions for each generated value. When no input satisfies these assumptions, every path ends before the function under test is called, and the harness reported Success without checking anything. Each harness under --constructor-args now covers the call to its function. When that cover is unsatisfiable or unreachable, the driver fails the harness with a line naming the function, and the SARIF output carries an error result for it. A harness that reaches the function with some of its inputs, such as with None for an Option argument, is verified as before, and a harness without the flag gets no new property. --constructor-args is rejected with --output-format=old, which reads only CBMC's exit status and cannot see the cover. The autoharness_constructor_vacuity test covers both constructor kinds, mined conditions that contradict a constructor or each other, a formatting-trait harness, an Option argument and a satisfiable control, with and without reachability checks, the SARIF output, and a run without the flag. --- .../src/reference/experimental/autoharness.md | 16 +- .../src/kani_middle/transform/automatic.rs | 47 +++++- kani-driver/src/args/autoharness_args.rs | 51 +++++- kani-driver/src/call_cbmc.rs | 9 +- kani-driver/src/cbmc_property_renderer.rs | 23 ++- kani-driver/src/sarif.rs | 54 ++++--- .../autoharness_constructor_vacuity/check.sh | 37 +++++ .../config.yml | 5 + .../autoharness_constructor_vacuity/expected | 35 ++++ .../vacuous.rs | 150 ++++++++++++++++++ 10 files changed, 395 insertions(+), 32 deletions(-) create mode 100755 tests/script-based-pre/autoharness_constructor_vacuity/check.sh create mode 100644 tests/script-based-pre/autoharness_constructor_vacuity/config.yml create mode 100644 tests/script-based-pre/autoharness_constructor_vacuity/expected create mode 100644 tests/script-based-pre/autoharness_constructor_vacuity/vacuous.rs diff --git a/docs/src/reference/experimental/autoharness.md b/docs/src/reference/experimental/autoharness.md index 9ba3bbc75ba5..5ad4ed2e782e 100644 --- a/docs/src/reference/experimental/autoharness.md +++ b/docs/src/reference/experimental/autoharness.md @@ -136,7 +136,7 @@ works in three phases; click a box to jump to its section: │ • one monomorphization only │ │ codegen → CBMC → summary │ │ • no aliasing between args (#4750) │ │ (see the architecture diagram below) │ │ • bounded args only up to bound │ └──────────────────────────────────────────┘ -│ • ctor may be vacuous (#4757) │ +│ • ctor may reach only some inputs │ │ • mined-invariant heuristic (#4763) │ └───────────────────────────────────────┘ @@ -737,9 +737,17 @@ This applies to harnesses marked "(ctor)", i.e. runs with `--constructor-args` ( [Constructor-based generation](#constructor-based-generation)). If the chosen constructor is *unsatisfiable* for the generated type — an assert-guarded constructor every argument of which trips an assertion, or a checked constructor that always returns -`None`/`Err` — the generated body assumes `false` on all paths and the harness becomes -**vacuous**, reporting `Success` without checking anything. Kani does not yet detect this case; -see [#4757](https://github.com/model-checking/kani/issues/4757). +`None`/`Err` — the generated body assumes `false` on all paths. Mined conditions that no +generated value satisfies have the same effect. If no generated input then reaches the function +under test, the harness is **vacuous**: none of its checks can fail. Kani covers the call to that +function (`` autoharness: a generated input reaches `...` ``) and reports such a harness as a +failure, with the line `` Vacuous harness: no generated input reaches `...` ``. For a function +with a contract, its `requires` clause is assumed inside the call, after the cover, so a +precondition that excludes every generated input is not reported as vacuous. A harness that +reaches the function with only some of its inputs, for example with `None` for an `Option` +argument whose payload cannot be generated, is reported as usual; the excluded inputs, such as +the `Some` path, are never checked. `--constructor-args` cannot be combined with +`--output-format=old`, which cannot detect a vacuous harness. ### Mined-invariant heuristic diff --git a/kani-compiler/src/kani_middle/transform/automatic.rs b/kani-compiler/src/kani_middle/transform/automatic.rs index bddad3937965..26e91348f79b 100644 --- a/kani-compiler/src/kani_middle/transform/automatic.rs +++ b/kani-compiler/src/kani_middle/transform/automatic.rs @@ -1659,11 +1659,6 @@ impl AutomaticArbitraryPass { /// arguments and panic paths converted into assumptions /// (c.f. [find_unchecked_constructor][crate::kani_middle::find_unchecked_constructor]). /// Returns None if the constructor body contains constructs the inliner does not support. - /// - /// Soundness caveat: if the constructor is unsatisfiable for `ty` (every argument trips an - /// assertion), the generated body assumes `false` on all paths and the harness becomes - /// vacuous, reporting Success without checking anything. Not yet detected; tracked in - /// . fn generate_unchecked_ctor_body( &self, tcx: TyCtxt, @@ -2024,6 +2019,8 @@ pub struct AutomaticHarnessPass { init_contracts_hook: Instance, reset_clause_depth: Instance, kani_autoharness_intrinsic: FnDef, + /// The FnDef of KaniHook::Cover, c.f. `cover_function_reached`. + kani_cover: FnDef, /// Whether --check-invariants is enabled: check values returned by verified functions /// against the type's mined invariant conjuncts. check_invariants: bool, @@ -2044,6 +2041,7 @@ impl AutomaticHarnessPass { *kani_fns.get(&KaniModel::ResetContractClauseDepth.into()).unwrap(); let reset_clause_depth = Instance::resolve(reset_clause_depth, &GenericArgs(vec![])).unwrap(); + let kani_cover = *kani_fns.get(&KaniHook::Cover.into()).unwrap(); let check_invariants = query_db.args().autoharness_check_invariants; Self { models: AnyModels::new(query_db), @@ -2051,9 +2049,45 @@ impl AutomaticHarnessPass { init_contracts_hook, reset_clause_depth, kani_autoharness_intrinsic, + kani_cover, check_invariants, } } + + /// Under --constructor-args, emit `kani::cover(true, msg)` at `source`, where the harness is + /// about to call `function` (directly, or through a formatting model). The assumptions made + /// while generating the arguments (a constructor that never returns, mined conditions that + /// contradict it or each other) can exclude every input, so that the harness checks nothing; + /// the driver then reports the harness as vacuous + /// (). + fn cover_function_reached( + &self, + body: &mut MutableBody, + source: &mut SourceInstruction, + function: Instance, + ) { + if !self.models.constructor_args { + return; + } + let cover_inst = Instance::resolve(self.kani_cover, &GenericArgs(vec![])).unwrap(); + let span = source.span(body.blocks()); + let true_op = Operand::Constant(ConstOperand { + span, + user_ty: None, + const_: MirConst::from_bool(true), + }); + // kani-driver recognizes this cover by its description (`VACUITY_COVER_DESC`). + let msg = format!("autoharness: a generated input reaches `{}`", function.name()); + let msg_op = body.new_str_operand(&msg, span); + let unit_lcl = body.new_local(Ty::new_tuple(&[]), span, Mutability::Not); + body.insert_call( + &cover_inst, + source, + InsertPosition::Before, + vec![true_op, msg_op], + Place::from(unit_lcl), + ); + } } impl TransformPass for AutomaticHarnessPass { @@ -2121,6 +2155,7 @@ impl TransformPass for AutomaticHarnessPass { source.span(harness_body.blocks()), Mutability::Not, ); + self.cover_function_reached(&mut harness_body, &mut source, fn_to_verify); harness_body.insert_call( &model_inst, &mut source, @@ -2202,6 +2237,8 @@ impl TransformPass for AutomaticHarnessPass { ); let ret_place = Place::from(ret_lcl); + self.cover_function_reached(&mut harness_body, &mut source, fn_to_verify); + // Call `fn_to_verify` on the nondeterministic arguments generated above. harness_body.insert_call( &fn_to_verify, diff --git a/kani-driver/src/args/autoharness_args.rs b/kani-driver/src/args/autoharness_args.rs index d981638bd73f..8aa27fbeeabf 100644 --- a/kani-driver/src/args/autoharness_args.rs +++ b/kani-driver/src/args/autoharness_args.rs @@ -5,7 +5,7 @@ use std::path::PathBuf; use crate::args::list_args::Format; -use crate::args::{ValidateArgs, VerificationArgs, validate_std_path}; +use crate::args::{OutputFormat, ValidateArgs, VerificationArgs, validate_std_path}; use crate::util::warning; use clap::{Error, Parser, error::ErrorKind}; use kani_metadata::{ @@ -244,6 +244,16 @@ impl ValidateArgs for CargoAutoharnessArgs { )); } + if self.common_autoharness_args.constructor_args + && self.verify_opts.output_format() == OutputFormat::Old + { + return Err(Error::raw( + ErrorKind::ArgumentConflict, + "Conflicting options: --constructor-args isn't compatible with \ + --output-format=old, since `old` cannot detect a vacuous harness.", + )); + } + Ok(()) } } @@ -296,6 +306,16 @@ impl ValidateArgs for StandaloneAutoharnessArgs { )); } + if self.common_autoharness_args.constructor_args + && self.verify_opts.output_format() == OutputFormat::Old + { + return Err(Error::raw( + ErrorKind::ArgumentConflict, + "Conflicting options: --constructor-args isn't compatible with \ + --output-format=old, since `old` cannot detect a vacuous harness.", + )); + } + Ok(()) } } @@ -352,4 +372,33 @@ mod tests { assert_eq!(args.string_bound, AUTOHARNESS_STR_BOUND); assert_eq!(args.bounded_arbitrary_bound, AUTOHARNESS_BOUNDED_ARBITRARY_BOUND); } + + /// Both subcommand forms reject `--constructor-args` with `--output-format=old`, and accept + /// either option on its own. + #[test] + fn constructor_args_reject_old_output_format() { + // `validate` also checks the standalone input is a regular file, so use a real one. + let dir = tempfile::tempdir().unwrap(); + let input = dir.path().join("input.rs"); + std::fs::write(&input, "").unwrap(); + let validate = |options: &[&str]| { + let mut args = vec!["autoharness", "-Z", "autoharness"]; + args.extend_from_slice(options); + let cargo = CargoAutoharnessArgs::try_parse_from(&args).unwrap().validate(); + args.push(input.to_str().unwrap()); + let standalone = StandaloneAutoharnessArgs::try_parse_from(&args).unwrap().validate(); + [cargo, standalone] + }; + + for result in validate(&["--constructor-args", "--output-format=old"]) { + let err = result.expect_err("expected --constructor-args to reject old output"); + assert_eq!(err.kind(), ErrorKind::ArgumentConflict); + assert!(err.to_string().contains("--constructor-args"), "{err}"); + } + for options in [["--constructor-args"], ["--output-format=old"]] { + for result in validate(&options) { + assert!(result.is_ok(), "{options:?}: {:?}", result.err()); + } + } + } } diff --git a/kani-driver/src/call_cbmc.rs b/kani-driver/src/call_cbmc.rs index eb2213fdddd8..d89471545d8d 100644 --- a/kani-driver/src/call_cbmc.rs +++ b/kani-driver/src/call_cbmc.rs @@ -21,7 +21,9 @@ use crate::args::{OutputFormat, VerificationArgs}; use crate::cbmc_output_parser::{ CheckStatus, ParserItem, Property, VerificationOutput, extract_results, process_cbmc_output, }; -use crate::cbmc_property_renderer::{format_coverage, format_result, kani_cbmc_output_filter}; +use crate::cbmc_property_renderer::{ + format_coverage, format_result, is_vacuous_harness_cover, kani_cbmc_output_filter, +}; use crate::coverage::cov_results::{CoverageCheck, CoverageResults}; use crate::coverage::cov_results::{CoverageRegion, CoverageTerm}; use crate::session::KaniSession; @@ -498,6 +500,11 @@ impl VerificationResult { { let (mut status, mut failed_properties) = verification_outcome_from_properties(&results, should_panic); + // `format_result` prints the explanation. + if results.iter().any(is_vacuous_harness_cover) { + status = VerificationStatus::Failure; + failed_properties = FailedProperties::Other; + } // A dropped quantifier makes the analysis unsound: a `kani::assume` containing one is // silently not enforced, so a "successful" result may be vacuous. Kani must never // report success in that case -- force a failure and (via the rendered error) direct diff --git a/kani-driver/src/cbmc_property_renderer.rs b/kani-driver/src/cbmc_property_renderer.rs index fc572fadab88..743df79a98f8 100644 --- a/kani-driver/src/cbmc_property_renderer.rs +++ b/kani-driver/src/cbmc_property_renderer.rs @@ -154,6 +154,7 @@ const UNSUPPORTED_CONSTRUCT_DESC: &str = "is not currently supported by Kani"; const UNWINDING_ASSERT_DESC: &str = "unwinding assertion loop"; const UNWINDING_ASSERT_REC_DESC: &str = "recursion unwinding assertion"; const UNDEFINED_FUNCTION_DESC: &str = "undefined function should be unreachable"; +const VACUITY_COVER_DESC: &str = "autoharness: a generated input reaches "; impl ParserItem { /// Determines if an item must be skipped or not. @@ -249,7 +250,9 @@ fn format_item_terse(_item: &ParserItem) -> Option { /// This function reports the results of normal checks (e.g. assertions and /// arithmetic overflow checks) and cover properties (specified using the /// `kani::cover` macro) separately. Cover properties currently do not impact -/// the overall verification success or failure. +/// the overall verification success or failure, except that a harness fails +/// when no input reaches its autoharness vacuity cover (see +/// `is_vacuous_harness_cover`). /// /// TODO: We could `write!` to `result_str` instead /// @@ -389,6 +392,10 @@ pub fn format_result( let failure_message = build_failure_message(prop.description.clone(), &prop.trace.clone()); result_str.push_str(&failure_message); } + for prop in properties.iter().filter(|prop| is_vacuous_harness_cover(prop)) { + let vacuity_line = format!("{}\n", vacuity_message(prop)); + result_str.push_str(&vacuity_line); + } let verification_result = if status == VerificationStatus::Success { style("SUCCESSFUL").green() @@ -550,6 +557,20 @@ pub fn postprocess_result(properties: Vec, extra_ptr_checks: bool) -> update_results_of_cover_checks(results_after_code_coverage) } +/// Whether `prop` is a `cover_function_reached` cover (kani-compiler, `automatic.rs`) that no +/// input can satisfy, which makes the harness vacuous. +pub fn is_vacuous_harness_cover(prop: &Property) -> bool { + prop.is_cover_property() + && prop.description.starts_with(VACUITY_COVER_DESC) + && matches!(prop.status, CheckStatus::Unsatisfiable | CheckStatus::Unreachable) +} + +/// The line reporting a vacuous harness, for a `prop` that `is_vacuous_harness_cover` accepts. +pub fn vacuity_message(prop: &Property) -> String { + let function = &prop.description[VACUITY_COVER_DESC.len()..]; + format!("Vacuous harness: no generated input reaches {function}") +} + /// Determines if there is property with status `FAILURE` and the given description fn has_check_failure(properties: &Vec, description: &str) -> bool { for prop in properties { diff --git a/kani-driver/src/sarif.rs b/kani-driver/src/sarif.rs index 2a9f73d56151..ab2bfd44078e 100644 --- a/kani-driver/src/sarif.rs +++ b/kani-driver/src/sarif.rs @@ -3,9 +3,11 @@ use crate::call_cbmc::ExitStatus; use crate::cbmc_output_parser::{CheckStatus, Property, SourceLocation, TraceItem}; +use crate::cbmc_property_renderer::{is_vacuous_harness_cover, vacuity_message}; use crate::harness_runner::HarnessResult; use crate::session::KaniSession; use anyhow::{Context, Result}; +use kani_metadata::HarnessMetadata; use pathdiff::diff_paths; use serde::Serialize; use std::collections::BTreeMap; @@ -180,6 +182,16 @@ impl SarifLog { }, }); } + for prop in properties.iter().filter(|prop| is_vacuous_harness_cover(prop)) { + let rule_id = "kani.autoharness.vacuous"; + let desc = "No generated input reaches the function under test"; + rules.entry(rule_id.to_string()).or_insert_with(|| ReportingDescriptor { + id: rule_id.to_string(), + short_description: Message { text: desc.to_string() }, + }); + let text = vacuity_message(prop); + sarif_results.push(harness_level_result(harness, rule_id, &text)); + } } Err(exit_status) => { let (rule_id, desc) = exit_status_rule(exit_status); @@ -188,26 +200,7 @@ impl SarifLog { short_description: Message { text: desc.to_string() }, }); - sarif_results.push(SarifResult { - rule_id: rule_id.to_string(), - level: "error", - message: Message { text: format!("[{}] {desc}", harness.pretty_name) }, - locations: vec![Location { - physical_location: PhysicalLocation { - artifact_location: ArtifactLocation { - uri: relativize_path(&harness.original_file), - }, - region: Region { - start_line: harness.original_start_line as u32, - start_column: None, - }, - }, - }], - properties: ResultProperties { - harness: harness.pretty_name.clone(), - property_name: None, - }, - }); + sarif_results.push(harness_level_result(harness, rule_id, desc)); } } } @@ -230,6 +223,27 @@ impl SarifLog { } } +/// An error result about `harness` as a whole, located at the harness's own source. +fn harness_level_result(harness: &HarnessMetadata, rule_id: &str, text: &str) -> SarifResult { + SarifResult { + rule_id: rule_id.to_string(), + level: "error", + message: Message { text: format!("[{}] {text}", harness.pretty_name) }, + locations: vec![Location { + physical_location: PhysicalLocation { + artifact_location: ArtifactLocation { + uri: relativize_path(&harness.original_file), + }, + region: Region { + start_line: harness.original_start_line as u32, + start_column: None, + }, + }, + }], + properties: ResultProperties { harness: harness.pretty_name.clone(), property_name: None }, + } +} + fn sarif_level(status: &CheckStatus) -> Option<&'static str> { match status { CheckStatus::Failure => Some("error"), diff --git a/tests/script-based-pre/autoharness_constructor_vacuity/check.sh b/tests/script-based-pre/autoharness_constructor_vacuity/check.sh new file mode 100755 index 000000000000..b2fea61d12a1 --- /dev/null +++ b/tests/script-based-pre/autoharness_constructor_vacuity/check.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash +# Copyright Kani Contributors +# SPDX-License-Identifier: Apache-2.0 OR MIT + +set -eu + +# The logs go outside the source tree, so a failing run leaves nothing behind. +log_dir=$(mktemp -d) +trap 'rm -rf "$log_dir"' EXIT + +# Without --constructor-args, a harness gets no new property. +kani autoharness -Z autoharness --include-pattern boom_q --output-format=regular vacuous.rs \ + > "$log_dir/no_flag.log" 2>&1 || true +grep -q '| m::Q::boom_q *| #\[kani::proof\] *| Failure' "$log_dir/no_flag.log" +if grep -q 'autoharness: a generated input reaches' "$log_dir/no_flag.log"; then exit 2; fi + +# Without assertion reachability checks, the cover before a call that no input reaches is +# UNSATISFIABLE rather than UNREACHABLE; the harness must be reported as vacuous all the same. +kani autoharness -Z autoharness --constructor-args --no-assertion-reach-checks \ + --include-pattern boom_q --output-format=terse vacuous.rs > "$log_dir/no_reach.log" 2>&1 || true +grep -q 'Vacuous harness: no generated input reaches `vacuous::m::Q::boom_q`' \ + "$log_dir/no_reach.log" +grep -q '| m::Q::boom_q *| #\[kani::proof\] (ctor) *| Failure' "$log_dir/no_reach.log" + +# SARIF has one error result for each vacuous harness and none for a harness that reaches its +# function. +kani autoharness -Z autoharness --constructor-args --sarif "$log_dir/out.sarif" \ + --include-pattern boom --include-pattern R::get --include-pattern opt vacuous.rs \ + > "$log_dir/sarif.log" 2>&1 || true +[ "$(grep -c '"level": "error"' "$log_dir/out.sarif")" = 2 ] +[ "$(grep -c '"harness": ' "$log_dir/out.sarif")" = 2 ] +for harness in P::boom_p Q::boom_q; do + grep -qF "[m::$harness] Vacuous harness: no generated input reaches \`vacuous::m::$harness\`" \ + "$log_dir/out.sarif" +done + +kani autoharness -Z autoharness --constructor-args --output-format=regular vacuous.rs diff --git a/tests/script-based-pre/autoharness_constructor_vacuity/config.yml b/tests/script-based-pre/autoharness_constructor_vacuity/config.yml new file mode 100644 index 000000000000..c831fd7ddef0 --- /dev/null +++ b/tests/script-based-pre/autoharness_constructor_vacuity/config.yml @@ -0,0 +1,5 @@ +# Copyright Kani Contributors +# SPDX-License-Identifier: Apache-2.0 OR MIT +script: check.sh +expected: expected +exit_code: 1 diff --git a/tests/script-based-pre/autoharness_constructor_vacuity/expected b/tests/script-based-pre/autoharness_constructor_vacuity/expected new file mode 100644 index 000000000000..f697404bf6a4 --- /dev/null +++ b/tests/script-based-pre/autoharness_constructor_vacuity/expected @@ -0,0 +1,35 @@ + - Status: UNREACHABLE\ + - Description: "autoharness: a generated input reaches `vacuous::m::P::boom_p`" + - Status: UNREACHABLE\ + - Description: "autoharness: a generated input reaches `vacuous::m::Q::boom_q`" + - Status: SATISFIED\ + - Description: "autoharness: a generated input reaches `vacuous::m::R::get`" + - Status: SATISFIED\ + - Description: "autoharness: a generated input reaches `vacuous::m::opt`" + - Status: SATISFIED\ + - Description: "autoharness: a generated input reaches `vacuous::m::P::new_unchecked`" +Vacuous harness: no generated input reaches `vacuous::m::P::boom_p` +Vacuous harness: no generated input reaches `vacuous::m::Q::boom_q` +Vacuous harness: no generated input reaches `::fmt` +Vacuous harness: no generated input reaches `vacuous::m::S::a` +Vacuous harness: no generated input reaches `vacuous::m::S::b` +Vacuous harness: no generated input reaches `vacuous::m::T::c` +Vacuous harness: no generated input reaches `vacuous::m::T::d` +Vacuous harness: no generated input reaches `vacuous::m::V::a` +Vacuous harness: no generated input reaches `vacuous::m::V::b` +Vacuous harness: no generated input reaches `vacuous::m::V::c` +Vacuous harness: no generated input reaches `vacuous::m::V::d` +| vacuous | m::P::boom_p | #[kani::proof] (ctor) | Failure | +| vacuous | m::Q::boom_q | #[kani::proof] (ctor) | Failure | +| vacuous | ::fmt | #[kani::proof] (ctor) | Failure | +| vacuous | m::S::a | #[kani::proof] (ctor) | Failure | +| vacuous | m::S::b | #[kani::proof] (ctor) | Failure | +| vacuous | m::T::c | #[kani::proof] (ctor) | Failure | +| vacuous | m::T::d | #[kani::proof] (ctor) | Failure | +| vacuous | m::V::a | #[kani::proof] (ctor) | Failure | +| vacuous | m::V::b | #[kani::proof] (ctor) | Failure | +| vacuous | m::V::c | #[kani::proof] (ctor) | Failure | +| vacuous | m::V::d | #[kani::proof] (ctor) | Failure | +| vacuous | m::opt | #[kani::proof] (ctor) | Success | +| vacuous | m::R::get | #[kani::proof] (ctor) | Success | +Complete - 5 successfully verified functions, 13 failures, 18 total. diff --git a/tests/script-based-pre/autoharness_constructor_vacuity/vacuous.rs b/tests/script-based-pre/autoharness_constructor_vacuity/vacuous.rs new file mode 100644 index 000000000000..5db38c2ade9b --- /dev/null +++ b/tests/script-based-pre/autoharness_constructor_vacuity/vacuous.rs @@ -0,0 +1,150 @@ +// Copyright Kani Contributors +// SPDX-License-Identifier: Apache-2.0 OR MIT + +//! Regression test for https://github.com/model-checking/kani/issues/4757. +//! +//! Under --constructor-args, the assumptions made while generating a harness's inputs can exclude +//! every input: a constructor that never returns a value, or mined conditions that contradict the +//! constructor or each other. Such a harness never reaches the function it verifies and must be +//! reported as a vacuous failure that names the function. + +pub mod m { + // Assert-guarded representation constructor (inlined, with its assertions turned into + // assumptions) whose assertions no argument satisfies. + pub struct P { + v: u8, + } + + impl P { + pub unsafe fn new_unchecked(v: u8) -> Self { + assert!(v > 10); + assert!(v < 5); + P { v } + } + + pub fn boom_p(&self) -> u8 { + assert!(self.v > 10 && self.v < 5, "always fails"); + self.v + } + } + + // Checked public constructor that always returns `None`. + pub struct Q { + v: u8, + } + + impl Q { + pub fn new(_v: u8) -> Option { + None + } + + pub fn boom_q(&self) -> u8 { + assert!(self.v > 10 && self.v < 5, "always fails"); + self.v + } + } + + // A formatting-trait harness reaches `fmt` through Kani's formatting model, not a direct call. + impl core::fmt::Display for Q { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + assert!(self.v > 10 && self.v < 5, "always fails"); + f.write_str("Q") + } + } + + // Not vacuous: no `Q` can be generated, but the `None` path reaches the function. + pub fn opt(q: Option, d: u8) -> u8 { + match q { + Some(q) => q.boom_q(), + None => d, + } + } + + // Control: a checked constructor that returns a value for some inputs. + pub struct R { + v: u8, + } + + impl R { + pub fn new(v: u8) -> Option { + if v > 0 { Some(R { v }) } else { None } + } + + pub fn get(&self) -> u8 { + assert!(self.v > 0, "R::new rejects zero"); + self.v + } + } + + // Satisfiable checked constructor, but the condition mined from `a` and `b` (`v < 5`), + // assumed once the constructor returns, excludes every value it returns. + pub struct S { + v: u8, + } + + impl S { + pub fn new(v: u8) -> Option { + if v > 10 { Some(S { v }) } else { None } + } + + pub fn a(&self) -> u8 { + assert!(self.v < 5, "a needs v < 5"); + self.v + } + + pub fn b(&self) -> u8 { + assert!(self.v < 5, "b needs v < 5"); + self.v / 2 + } + } + + // The same with a satisfiable assert-guarded constructor. + pub struct T { + v: u8, + } + + impl T { + pub unsafe fn new_unchecked(v: u8) -> Self { + assert!(v > 10); + T { v } + } + + pub fn c(&self) -> u8 { + assert!(self.v < 5, "c needs v < 5"); + self.v + } + + pub fn d(&self) -> u8 { + assert!(self.v < 5, "d needs v < 5"); + self.v / 2 + } + } + + // No constructor, and the conditions mined from `a` and `b` (`v < 5`) and from `c` and `d` + // (`v > 10`) contradict each other. + pub struct V { + v: u8, + } + + impl V { + pub fn a(&self) -> u8 { + assert!(self.v < 5, "a"); + self.v + } + + pub fn b(&self) -> u8 { + assert!(self.v < 5, "b"); + self.v / 2 + } + + pub fn c(&self) -> u8 { + assert!(self.v > 10, "c"); + self.v + } + + pub fn d(&self) -> u8 { + assert!(self.v > 10, "d"); + self.v / 3 + } + } +} From 7a55085d59fd0401844c046a1cc9c6a76e3f2937 Mon Sep 17 00:00:00 2001 From: Srivatsan Samraj <321934658+srivatsansamraj@users.noreply.github.com> Date: Fri, 6 Nov 2026 13:39:11 -0800 Subject: [PATCH 2/2] Share the vacuity cover prefix through kani_metadata The compiler writes the cover's description and the driver recognizes it by the same prefix, so both now take it from one constant. `vacuity_message` returns `None` for any other property instead of slicing its description, so it cannot panic, and the lookup of the cover hook says why it cannot fail. --- .../src/kani_middle/transform/automatic.rs | 8 +++++--- kani-driver/src/cbmc_property_renderer.rs | 15 ++++++++------- kani-driver/src/sarif.rs | 5 ++--- kani_metadata/src/lib.rs | 4 ++++ 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/kani-compiler/src/kani_middle/transform/automatic.rs b/kani-compiler/src/kani_middle/transform/automatic.rs index 26e91348f79b..13cfb146b6ca 100644 --- a/kani-compiler/src/kani_middle/transform/automatic.rs +++ b/kani-compiler/src/kani_middle/transform/automatic.rs @@ -2069,15 +2069,17 @@ impl AutomaticHarnessPass { if !self.models.constructor_args { return; } - let cover_inst = Instance::resolve(self.kani_cover, &GenericArgs(vec![])).unwrap(); + let cover_inst = Instance::resolve(self.kani_cover, &GenericArgs(vec![])) + .expect("the cover hook is always registered"); let span = source.span(body.blocks()); let true_op = Operand::Constant(ConstOperand { span, user_ty: None, const_: MirConst::from_bool(true), }); - // kani-driver recognizes this cover by its description (`VACUITY_COVER_DESC`). - let msg = format!("autoharness: a generated input reaches `{}`", function.name()); + // kani-driver recognizes this cover by the start of its description. + let msg = + format!("{}`{}`", kani_metadata::AUTOHARNESS_VACUITY_COVER_PREFIX, function.name()); let msg_op = body.new_str_operand(&msg, span); let unit_lcl = body.new_local(Ty::new_tuple(&[]), span, Mutability::Not); body.insert_call( diff --git a/kani-driver/src/cbmc_property_renderer.rs b/kani-driver/src/cbmc_property_renderer.rs index 743df79a98f8..3702a5945200 100644 --- a/kani-driver/src/cbmc_property_renderer.rs +++ b/kani-driver/src/cbmc_property_renderer.rs @@ -154,7 +154,7 @@ const UNSUPPORTED_CONSTRUCT_DESC: &str = "is not currently supported by Kani"; const UNWINDING_ASSERT_DESC: &str = "unwinding assertion loop"; const UNWINDING_ASSERT_REC_DESC: &str = "recursion unwinding assertion"; const UNDEFINED_FUNCTION_DESC: &str = "undefined function should be unreachable"; -const VACUITY_COVER_DESC: &str = "autoharness: a generated input reaches "; +const VACUITY_COVER_DESC: &str = kani_metadata::AUTOHARNESS_VACUITY_COVER_PREFIX; impl ParserItem { /// Determines if an item must be skipped or not. @@ -392,8 +392,8 @@ pub fn format_result( let failure_message = build_failure_message(prop.description.clone(), &prop.trace.clone()); result_str.push_str(&failure_message); } - for prop in properties.iter().filter(|prop| is_vacuous_harness_cover(prop)) { - let vacuity_line = format!("{}\n", vacuity_message(prop)); + for message in properties.iter().filter_map(vacuity_message) { + let vacuity_line = format!("{message}\n"); result_str.push_str(&vacuity_line); } @@ -565,10 +565,11 @@ pub fn is_vacuous_harness_cover(prop: &Property) -> bool { && matches!(prop.status, CheckStatus::Unsatisfiable | CheckStatus::Unreachable) } -/// The line reporting a vacuous harness, for a `prop` that `is_vacuous_harness_cover` accepts. -pub fn vacuity_message(prop: &Property) -> String { - let function = &prop.description[VACUITY_COVER_DESC.len()..]; - format!("Vacuous harness: no generated input reaches {function}") +/// The line reporting a vacuous harness, if `prop` is a cover that makes it vacuous. +pub fn vacuity_message(prop: &Property) -> Option { + let function = prop.description.strip_prefix(VACUITY_COVER_DESC)?; + is_vacuous_harness_cover(prop) + .then(|| format!("Vacuous harness: no generated input reaches {function}")) } /// Determines if there is property with status `FAILURE` and the given description diff --git a/kani-driver/src/sarif.rs b/kani-driver/src/sarif.rs index ab2bfd44078e..0a624be64983 100644 --- a/kani-driver/src/sarif.rs +++ b/kani-driver/src/sarif.rs @@ -3,7 +3,7 @@ use crate::call_cbmc::ExitStatus; use crate::cbmc_output_parser::{CheckStatus, Property, SourceLocation, TraceItem}; -use crate::cbmc_property_renderer::{is_vacuous_harness_cover, vacuity_message}; +use crate::cbmc_property_renderer::vacuity_message; use crate::harness_runner::HarnessResult; use crate::session::KaniSession; use anyhow::{Context, Result}; @@ -182,14 +182,13 @@ impl SarifLog { }, }); } - for prop in properties.iter().filter(|prop| is_vacuous_harness_cover(prop)) { + for text in properties.iter().filter_map(vacuity_message) { let rule_id = "kani.autoharness.vacuous"; let desc = "No generated input reaches the function under test"; rules.entry(rule_id.to_string()).or_insert_with(|| ReportingDescriptor { id: rule_id.to_string(), short_description: Message { text: desc.to_string() }, }); - let text = vacuity_message(prop); sarif_results.push(harness_level_result(harness, rule_id, &text)); } } diff --git a/kani_metadata/src/lib.rs b/kani_metadata/src/lib.rs index 3e7490b37bd7..ef884ddf9dac 100644 --- a/kani_metadata/src/lib.rs +++ b/kani_metadata/src/lib.rs @@ -48,6 +48,10 @@ pub const AUTOHARNESS_STR_BOUND: u64 = 4; /// makes simple `String` harnesses exceed Kani's default 60s harness timeout. pub const AUTOHARNESS_BOUNDED_ARBITRARY_BOUND: u64 = 4; +/// The start of the description of the cover that automatic harnesses place before the call to +/// the function under test. kani-driver reports a harness whose cover no input reaches as vacuous. +pub const AUTOHARNESS_VACUITY_COVER_PREFIX: &str = "autoharness: a generated input reaches "; + /// The structure of `.kani-metadata.json` files, which are emitted for each crate #[derive(Debug, Clone, Serialize, Deserialize)] pub struct KaniMetadata {