model-checking / verify-rust-std

Verifying the Rust standard library
https://model-checking.github.io/verify-rust-std/
Other
110 stars 25 forks source link

Challenge 5: Verify functions iterating over inductive data type: `linked_list` #29

Open qinheping opened 4 months ago

qinheping commented 4 months ago

Link to PR: #30 Link to challenge: 0005-linked-list.md

bp7968h commented 11 hours ago

Hi @qinheping and @feliperodri , can I please give this a try, thank you.

bp7968h commented 2 hours ago

Hi @carolynzech, @celinval, @qinheping, @feliperodri; I've been working on the challenge to verify the memory safety for this challenge. Below is the code I have written so far to prove that undefined behavior doesn't exists for clear function in LinkedList:

use kani;

#[cfg(kani)]
#[kani::proof]
fn unbounded_check_for_clear() {
    let size: i8 = kani::any();
    let mut size_copy: i8 = size.clone();
    let mut list: LinkedList<i8> = LinkedList::new();

    // create a arbitraty size linkedlist
    loop {
        if size_copy == 0 {
            break;
        }

        list.push_back(size_copy);

        match size_copy.is_positive() {
            true => size_copy -= 1,
            false => size_copy += 1,
        }
    }
    // check if the original kani produced size aligns with the linked list
    assert_eq!(list.contains(&size), true);
    assert_eq!(list.len(), size.abs().try_into().unwrap());

    // clear all the items
    list.clear();

    //check again
    assert_eq!(list.contains(&size), false);
    assert_eq!(list.len(), 0);
    assert!(list.is_empty());
}

I have some confusions:

thank you for your time and help.