Repository navigation
Commit 3f8a13b
Use byte-wise memchr under Kani to cut symbolic execution cost (#628)
Profiling `ffi::c_str::verify::check_to_bytes` (via CBMC
`--program-only` SSA attribution) showed that a third of all SSA steps
came from a single function: `slice::memchr::memchr_aligned`'s runtime
arm — the word-at-a-time scan behind slice `contains`, which `CStr`'s
safety invariant and contract clauses evaluate repeatedly. Symbolically,
the word-optimized scan is ~13× more expensive than the semantically
equivalent byte-wise `memchr_naive` loop while providing zero benefit:
there is no word-level parallelism in symbolic execution.
This PR routes `memchr` to `memchr_naive` under `cfg(kani)`. Measured
effect (Kani 152c6a8c + CBMC 6.10.0, `--jobs=4`): the full `ffi::c_str`
suite runs 12/12 in 2:13 wall; `check_to_bytes` alone drops from 3:28
wall / 170 s CBMC solve to 14.6 s solve, and from 606 s to 21 s solve
when dependency contracts are asserted. `check_from_bytes_until_nul`
needs its unwind bound raised from 32 to 33 (the byte-wise loop needs
one more iteration than the word scan); it now solves in 3.1 s where the
old bound's comment recorded 33.1 s.
Since the CStr harnesses were the only indirect verification coverage of
`memchr_aligned`'s unsafe word-sized reads, the PR adds an equivalence
harness checking `memchr_aligned` against `memchr_naive` on all inputs
up to 24 bytes (documenting the `len >= 2*USIZE_BYTES` precondition its
caller establishes), so the optimized implementation stays verified. All
`str::` and `slice::memchr` harnesses (15 + 13) pass with this change.
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: Kiro <kiro-agent@users.noreply.github.com>
Co-authored-by: Felipe R. Monteiro <rms.felipe@gmail.com>1 parent d106d2f commit 3f8a13b
2 files changed
Lines changed: 47 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
901 | 901 | | |
902 | 902 | | |
903 | 903 | | |
904 | | - | |
| 904 | + | |
905 | 905 | | |
906 | 906 | | |
907 | 907 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
23 | 25 | | |
24 | 26 | | |
25 | 27 | | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
30 | 41 | | |
31 | | - | |
| 42 | + | |
| 43 | + | |
32 | 44 | | |
33 | 45 | | |
34 | 46 | | |
| |||
159 | 171 | | |
160 | 172 | | |
161 | 173 | | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
0 commit comments