Skip to content

Commit 4da67dd

Browse files
Update autoharness_analyzer for current AutoHarnessSkipReason variants (#680)
The analyzer keeps its own copy of `AutoHarnessSkipReason` and fails to deserialize metadata from current Kani, which changed `GenericFn` to `GenericFn(String)` (model-checking/kani#4679) and added `RequiresBoundedArguments` (model-checking/kani#4691). - Bring the enum in line with `kani_metadata`, keeping the `strum` strings identical so table text matches `kani autoharness --list`. - Normalize a bare `"GenericFn"` to `{"GenericFn": ""}` before `from_value`, since CI builds the pinned Kani, which still writes the old form. - Report `RequiresBoundedArguments` in its own type-category table instead of folding it into the missing-implementation one; the two need different remediation. - Note `--bounded-arguments` in the README. Verified with three hand-built `kani-metadata.json` fixtures (old format, `GenericFn` with payload, `RequiresBoundedArguments`): before the change they behave as described in #679, after it all three pass. `cargo test` covers the same three cases inline. Not run against a full std sweep. Resolves #679 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses. Co-authored-by: Srivatsan Samraj <321934658+srivatsansamraj@users.noreply.github.com>
1 parent 39ec052 commit 4da67dd

3 files changed

Lines changed: 103 additions & 11 deletions

File tree

‎scripts/autoharness_analyzer/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ kani autoharness --std ./library -Z function-contracts -Z mem-predicates -Z floa
1111

1212
on the standard library. The scanner_results/ directory contains the CSV files that the [scanner tool in Kani](https://github.com/model-checking/kani/tree/main/tools/scanner) produces.
1313

14+
Without `--bounded-arguments`, functions whose arguments are only supported with bounded values are skipped with `Requires --bounded-arguments for argument(s)` and summarized in their own type-category table.
15+
1416
The output is `autoharness_data.md`, which contains Markdown tables summarizing the autoharness application across all the crates in the standard library.
1517

1618
One of the tables has a column for "Skipped Type Categories." Generally speaking, "precise types" are what we think of as actual Rust types, and "type categories" are my subjective sense of how to group those types further. For example, `&mut i32` and `&mut u32` are two precise types, but they're in the same type category `&mut`. See the code for exact details on how we create type categories; the TL;DR is that we have a few hardcoded ones for raw pointers and references, and the rest we create using a fully-qualified path splitting heuristic.

‎scripts/autoharness_analyzer/src/main.rs‎

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ pub struct AutoHarnessMetadata {
5252
/// Reasons that Kani does not generate an automatic harness for a function.
5353
#[derive(Debug, Clone, Serialize, Deserialize, Display, EnumString)]
5454
pub enum AutoHarnessSkipReason {
55-
/// The function is generic.
55+
/// The function is generic and autoharness could not find a monomorphic instantiation to
56+
/// verify. The payload gives the specific reason (e.g. const generic parameters, or trait
57+
/// bounds that no candidate type satisfies).
5658
#[strum(serialize = "Generic Function")]
57-
GenericFn,
59+
GenericFn(String),
5860
/// A Kani-internal function: already a harness, implementation of a Kani associated item or Kani contract instrumentation functions).
5961
#[strum(serialize = "Kani implementation")]
6062
KaniImpl,
@@ -65,6 +67,11 @@ pub enum AutoHarnessSkipReason {
6567
/// The function does not have a body.
6668
#[strum(serialize = "The function does not have a body")]
6769
NoBody,
70+
/// The function's arguments are only supported with bounded nondeterministic values, and
71+
/// the user did not pass --bounded-arguments.
72+
/// (The Vec<(String, String)> contains the list of (name, type) tuples for each such argument.)
73+
#[strum(serialize = "Requires --bounded-arguments for argument(s)")]
74+
RequiresBoundedArguments(Vec<(String, String)>),
6875
/// The function doesn't match the user's provided filters.
6976
#[strum(serialize = "Did not match provided filters")]
7077
UserFilter,
@@ -90,6 +97,23 @@ impl AutoHarnessMetadata {
9097
}
9198
}
9299

100+
/// Kani at the commit in `tool_config/kani-version.toml` predates model-checking/kani#4679 and
101+
/// writes `GenericFn` as a bare string, while current Kani writes `{"GenericFn": "<reason>"}`.
102+
/// Rewrite the old form so both deserialize into `GenericFn(String)`.
103+
fn normalize_skip_reasons(mut autoharness_md: Value) -> Value {
104+
if let Some(skipped) = autoharness_md
105+
.get_mut("skipped")
106+
.and_then(Value::as_object_mut)
107+
{
108+
for reason in skipped.values_mut() {
109+
if reason.as_str() == Some("GenericFn") {
110+
*reason = serde_json::json!({ "GenericFn": "" });
111+
}
112+
}
113+
}
114+
autoharness_md
115+
}
116+
93117
fn main() -> Result<()> {
94118
let args = Args::parse();
95119

@@ -127,7 +151,7 @@ fn main() -> Result<()> {
127151
let fn_to_row_data = process_scan_fns(scanner_fn_csv_path)?;
128152

129153
let autoharness_md: AutoHarnessMetadata =
130-
serde_json::from_value(v["autoharness_md"].clone())?;
154+
serde_json::from_value(normalize_skip_reasons(v["autoharness_md"].clone()))?;
131155

132156
if args.per_crate {
133157
// Process each crate separately
@@ -165,3 +189,40 @@ fn main() -> Result<()> {
165189

166190
Ok(())
167191
}
192+
193+
#[cfg(test)]
194+
mod tests {
195+
use super::*;
196+
197+
fn parse(skipped: &str) -> AutoHarnessMetadata {
198+
let v: Value = serde_json::from_str(&format!(
199+
r#"{{"chosen": [], "skipped": {{"f": {skipped}}}}}"#
200+
))
201+
.unwrap();
202+
serde_json::from_value(normalize_skip_reasons(v)).unwrap()
203+
}
204+
205+
#[test]
206+
fn generic_fn_old_form() {
207+
let md = parse(r#""GenericFn""#);
208+
assert!(matches!(md.skipped["f"], AutoHarnessSkipReason::GenericFn(ref s) if s.is_empty()));
209+
}
210+
211+
#[test]
212+
fn generic_fn_new_form() {
213+
let md = parse(r#"{"GenericFn": "no candidate instantiation"}"#);
214+
assert!(matches!(
215+
md.skipped["f"],
216+
AutoHarnessSkipReason::GenericFn(ref s) if s == "no candidate instantiation"
217+
));
218+
}
219+
220+
#[test]
221+
fn requires_bounded_arguments() {
222+
let md = parse(r#"{"RequiresBoundedArguments": [["s", "&[u8]"]]}"#);
223+
let AutoHarnessSkipReason::RequiresBoundedArguments(args) = &md.skipped["f"] else {
224+
panic!("wrong variant");
225+
};
226+
assert_eq!(args, &[("s".to_string(), "&[u8]".to_string())]);
227+
}
228+
}

‎scripts/autoharness_analyzer/src/make_tables.rs‎

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,28 @@ fn skipped_overview_table(autoharness_md: &AutoHarnessMetadata) -> Result<Markdo
8585
)?)
8686
}
8787

88+
/// Return the (name, type) pairs behind a `MissingArbitraryImpl` skip, if that is the reason.
89+
fn missing_arbitrary_args(reason: &AutoHarnessSkipReason) -> Option<&Vec<(String, String)>> {
90+
match reason {
91+
AutoHarnessSkipReason::MissingArbitraryImpl(args) => Some(args),
92+
_ => None,
93+
}
94+
}
95+
96+
/// Return the (name, type) pairs behind a `RequiresBoundedArguments` skip, if that is the reason.
97+
fn bounded_arguments_args(reason: &AutoHarnessSkipReason) -> Option<&Vec<(String, String)>> {
98+
match reason {
99+
AutoHarnessSkipReason::RequiresBoundedArguments(args) => Some(args),
100+
_ => None,
101+
}
102+
}
103+
104+
/// Count the argument types behind every skip reason that `args_of` selects, grouped into
105+
/// type categories. `category_header` names the first column.
88106
fn skipped_breakdown_table(
89107
autoharness_md: &AutoHarnessMetadata,
108+
category_header: &str,
109+
args_of: fn(&AutoHarnessSkipReason) -> Option<&Vec<(String, String)>>,
90110
show_precise_types: bool,
91111
) -> Result<MarkdownTable> {
92112
// Rust type -- &mut i32, &mut u32, bool, etc.
@@ -114,7 +134,7 @@ fn skipped_breakdown_table(
114134
};
115135

116136
for reason in autoharness_md.skipped.values() {
117-
if let AutoHarnessSkipReason::MissingArbitraryImpl(args) = reason {
137+
if let Some(args) = args_of(reason) {
118138
for (_, arg_type) in args {
119139
let mut is_categorized = false;
120140
for category in &type_categories {
@@ -151,15 +171,12 @@ fn skipped_breakdown_table(
151171
Ok(MarkdownTable::new(
152172
Some(if show_precise_types {
153173
vec![
154-
"Unsupported Type Category".to_string(),
174+
category_header.to_string(),
155175
"# of occurences".to_string(),
156176
"Precise Types".to_string(),
157177
]
158178
} else {
159-
vec![
160-
"Unsupported Type Category".to_string(),
161-
"# of occurences".to_string(),
162-
]
179+
vec![category_header.to_string(), "# of occurences".to_string()]
163180
}),
164181
sorted_by_count
165182
.into_iter()
@@ -235,7 +252,18 @@ pub fn compute_metrics(
235252

236253
let chosen_overview_table = chosen_overview_table(&unsafe_metadata, fn_to_row_data)?;
237254
let skipped_overview_table = skipped_overview_table(&unsafe_metadata)?;
238-
let skipped_breakdown_table = skipped_breakdown_table(&unsafe_metadata, show_precise_types)?;
255+
let missing_arbitrary_table = skipped_breakdown_table(
256+
&unsafe_metadata,
257+
"Unsupported Type Category",
258+
missing_arbitrary_args,
259+
show_precise_types,
260+
)?;
261+
let bounded_arguments_table = skipped_breakdown_table(
262+
&unsafe_metadata,
263+
"Type Category Requiring --bounded-arguments",
264+
bounded_arguments_args,
265+
show_precise_types,
266+
)?;
239267

240268
let out_path = Path::new(&format!(
241269
"{}{}_autoharness_data",
@@ -247,7 +275,8 @@ pub fn compute_metrics(
247275

248276
write_table_to_file(&mut out_file, &chosen_overview_table)?;
249277
write_table_to_file(&mut out_file, &skipped_overview_table)?;
250-
write_table_to_file(&mut out_file, &skipped_breakdown_table)?;
278+
write_table_to_file(&mut out_file, &missing_arbitrary_table)?;
279+
write_table_to_file(&mut out_file, &bounded_arguments_table)?;
251280

252281
println!("Wrote results to {}", out_path.to_string_lossy());
253282

0 commit comments

Comments
 (0)