Skip to content

Commit 71cbe74

Browse files
Strip the crate prefix from scanner names so contract metrics match again (#696)
All `*_under_contract` metrics have read 0 since #685: core dropped from 313 to 0 unsafe functions under contract in one run while its contract count didn't change. Since the nightly-2026-02-05 update (#613), the scanner reports crate-prefixed names (`core::ptr::align_offset`) but `kani list` reports `ptr::align_offset`, so nothing matches. Kani itself strips the prefix (`strip_local_crate_prefix`); the scanner doesn't. This applies the same stripping in `kani_std_analysis.py`, so the metrics are correct regardless of Kani version. Core now reads 307 / 281 / 323 (unsafe / safe-abstraction / safe under contract) instead of 0. Fixing it in Kani's scanner is another option, happy to do that instead if preferred. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 29a6bf7 commit 71cbe74

3 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎.github/workflows/kani.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,11 @@ jobs:
256256
with:
257257
python-version: '3.x'
258258

259+
- name: Test the metrics script
260+
run: |
261+
pip install -r scripts/kani-std-analysis/requirements.txt
262+
python3 -m unittest discover -s scripts/kani-std-analysis
263+
259264
# Step 2: Run list on the std library
260265
- name: Run Kani Metrics
261266
run: |

‎scripts/kani-std-analysis/kani_std_analysis.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@ def str_to_bool(string: str):
3838
sys.exit(1)
3939

4040

41+
# Scanner names are crate-prefixed (`core::ptr::align_offset`) since rust-lang/rust#149401;
42+
# `kani list` names are not (`ptr::align_offset`). Strip the prefix so they match again, but
43+
# only where `<crate>::` is a path qualifier, never a continuation segment (like Kani does).
44+
def strip_crate_prefix(name: str, crate: str) -> str:
45+
needle = f"{crate}::"
46+
if needle not in name:
47+
return name
48+
out = []
49+
rest = name
50+
prev = None
51+
while True:
52+
at_qualifier = prev is None or (not prev.isalnum() and prev not in "_:")
53+
if at_qualifier and rest.startswith(needle):
54+
rest = rest[len(needle):]
55+
prev = ":"
56+
continue
57+
if not rest:
58+
break
59+
ch = rest[0]
60+
out.append(ch)
61+
prev = ch
62+
rest = rest[1:]
63+
return "".join(out)
64+
65+
4166
# Process the results from Kani's std-analysis.sh script for each crate.
4267
class GenericSTDMetrics():
4368
def __init__(self, results_dir, crate):
@@ -89,6 +114,7 @@ def read_scan_functions(self):
89114
if len(row) >= 5:
90115
name, is_unsafe, has_unsafe_ops = row[0], row[1], row[2]
91116
has_unsupported_input, has_loop = row[3], row[4]
117+
name = strip_crate_prefix(name, self.crate)
92118
# An unsafe function is a function for which is_unsafe=true
93119
if str_to_bool(is_unsafe):
94120
self.unsafe_fns.append(name)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import unittest
2+
3+
from kani_std_analysis import strip_crate_prefix
4+
5+
6+
class StripCratePrefixTest(unittest.TestCase):
7+
# Pinned to Kani's `strip_local_crate_prefix`: only a crate-root qualifier
8+
# is dropped, a path segment that merely shares the crate's name is kept.
9+
def test_matches_kani(self):
10+
cases = [
11+
# crate, scanner name, expected `kani list` name
12+
("core", "<char as core::ascii::AsciiExt>::is_ascii",
13+
"<char as ascii::AsciiExt>::is_ascii"),
14+
("core", "core::core_simd::vector::Simd", "core_simd::vector::Simd"),
15+
("core", "ptr::align_offset", "ptr::align_offset"),
16+
("core", "alloc::vec::Vec", "alloc::vec::Vec"),
17+
# Not idempotent, like Kani's copy: apply exactly once.
18+
("main", "main::main::{closure#0}", "main::{closure#0}"),
19+
("core", "core::core::foo", "core::foo"),
20+
]
21+
for crate, name, expected in cases:
22+
with self.subTest(name=name):
23+
self.assertEqual(strip_crate_prefix(name, crate), expected)
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main()

0 commit comments

Comments
 (0)