Skip to content

Performance drastically differs across two string indexing operations #3328

Closed

Description

@zhassan-aws

For the following program:

#[kani::proof]
#[kani::unwind(5)]
fn main() {
    let buf: [u8; 4] = kani::any();
    if let Ok(s) = std::str::from_utf8(&buf) {
        let c: char = kani::any();
        if let Some(index) = s.find(c) {
            let slice = &s[index..];
            //let slice = s.get(index..).unwrap();
            assert!(slice.chars().next() == Some(c));
        }
    }
}

CBMC's memory usage exceeds 24 GB after running for 2 minutes. These are the last few lines it prints before running out of memory:

Runtime Symex: 44.498s
size of program expression: 889131 steps
slicing removed 550833 assignments
Generated 63496 VCC(s), 27179 remaining after simplification
Runtime Postprocess Equation: 0.942692s
Passing problem to propositional reduction
converting SSA
Runtime Convert SSA: 22.1197s

For the commented-out version of the slice indexing operation (which is supposedly equivalent), verification is successful in ~26 seconds with less than 1 GB of memory usage:

$ kani str_slice_perf.rs
...
Runtime Symex: 2.71987s
size of program expression: 73171 steps
slicing removed 55203 assignments
Generated 3677 VCC(s), 2301 remaining after simplification
Runtime Postprocess Equation: 0.0965727s
Passing problem to propositional reduction
converting SSA
Runtime Convert SSA: 0.708747s
...
SUMMARY:
 ** 0 of 581 failed (16 unreachable)

VERIFICATION:- SUCCESSFUL
Verification Time: 25.568632s

Activity

  1. added
    [E] Performance
    [C] Feature / Enhancement
    on Jul 5, 2024
  2. zhassan-aws commented on Jul 6, 2024

    @zhassan-aws
    ContributorAuthor

    To double check that the two versions are equivalent, I wrote this harness:

    #[kani::proof]
    #[kani::unwind(4)]
    fn main() {
        let buf: [u8; 3] = kani::any();
        if let Ok(s) = std::str::from_utf8(&buf) {
            let index = kani::any_where(|i| *i <= s.len() && s.is_char_boundary(*i));
            let slice1 = &s[index..];
            let slice2 = s.get(index..).unwrap();
            assert!(slice1 == slice2);
        }
    }

    which successfully verifies (up to 3 bytes). Beyond 3 bytes, it runs out of memory.

  3. added
    T-CBMC
    on Jul 24, 2024
  4. tautschnig commented on Aug 5, 2024

    @tautschnig
    Member

    The performance problem is solely caused by the error-handling path: Quoting from
    https://github.com/rust-lang/rust/blob/4d48a6be74d46304cacb93cb8eb60d70d733b862/library/core/src/str/traits.rs#L537-L543:

        fn index(self, slice: &str) -> &Self::Output {
            let (start, end) = (self.start, slice.len());
            match self.get(slice) {
                Some(s) => s,
                None => super::slice_error_fail(slice, start, end),
            }
        }

    where slice_error_fail resolves to slice_error_fail_rt at runtime, see https://github.com/rust-lang/rust/blob/4d48a6be74d46304cacb93cb8eb60d70d733b862/library/core/src/str/mod.rs#L82-L115. Notably, this includes

    let s_trunc = &s[..trunc_len];
    ...
    let ch = s[char_start..].chars().next().unwrap();

    which means that Kani will need to recurse as it can't establish that either indexing won't panic. (I'm not even convinced slice_error_fail_rt is safe (what if end == s.len()?), but that's a matter for our standard library verification effort.)

    I believe we should look at stubbing out slice_error_fail_rt (and possibly other verbose error handling in the standard library) as, for Kani's purpose, it should be no different than directly using panic!.

  5. removed
    T-CBMC
    on Aug 5, 2024
  6. zhassan-aws commented on Aug 5, 2024

    @zhassan-aws
    ContributorAuthor

    Thanks for investigating this @tautschnig. This sounds similar to #3030 (comment). Seems we need a general solution for error-handling paths.

  7. zhassan-aws commented on Aug 26, 2025

    @zhassan-aws
    ContributorAuthor

    As of 8b8b897, memory usage for the original program remains under ~5 GB and verification succeeds in ~4 minutes.

  8. added
    Z-Kani Compiler
    on Nov 5, 2026
  9. feliperodri commented on Nov 5, 2026

    @feliperodri
    Member

    Fixed in 8b1c1ec: core::str::slice_error_fail is now hooked as a panic (hooks.rs:431). Verified on main: the original harness passes (repro 3328-1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Z-Kani Compiler[C] Feature / Enhancement[E] Performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions