native_callbacks_dense.rs

verification/proofs/provider/kani/native_callbacks_dense.rs

native_callbacks_dense.rs113 lines← SDK documentation
1// Actual dense VecMemory branch: this is separate from sparse Store admission.2#[kani::proof]3fn actual_dense_callback_reads_owned_windows_and_returns_result() {4    let original: [u8; 32] = kani::any();5    let image = VecMemory::from_bytes(original.to_vec());6    let base = image.bytes.as_ptr();7    let start: usize = kani::any();8    let end: usize = kani::any();9    let expected = start as u128 <= end as u128 && end as u128 <= 32;10    let mut calls = 0usize;11    let returned = image.with_retained_region(|cap| {12        calls += 1;13        assert!(cap.as_ptr() == base && cap.len() == 32);14        let window = cap.get(start..end);15        assert!(window.is_some() == expected);16        if let Some(window) = window {17            assert!(window.len() == end - start);18            assert!(window.as_ptr() == base.wrapping_add(start));19            if !window.is_empty() {20                let query: u8 = kani::any();21                kani::assume((query as usize) < window.len());22                assert!(window[query as usize] == original[start + query as usize]);23            }24        }25        kani::cover!(start == 0 && end == 32 && window.is_some(), "actual dense callback lends its whole owner");26        kani::cover!(start == 32 && end == 32 && window.is_some(), "actual dense callback lends its empty endpoint");27        kani::cover!(start == 8 && end == 16 && original[8] != original[0] && window.is_some(),28                     "nonuniform owner distinguishes the admitted window from the buffer origin");29        kani::cover!(start == usize::MAX && end == 0 && window.is_none(), "actual dense callback rejects reversed range");30        Ok(17usize)31    }).unwrap();32    assert!(calls == 1 && returned == 17);33    assert!(image.bytes.len() == 32 && image.bytes.as_ptr() == base);34}3536#[kani::proof]37fn actual_dense_callback_frames_loans_rejoins_owner_and_returns_error() {38    let original: [u8; 32] = kani::any();39    let value: u8 = kani::any();40    let fail: bool = kani::any();41    let index: u8 = kani::any();42    kani::assume(index < 8);43    let index = index as usize;44    let mut ultimate = VecMemory::from_bytes(original.to_vec());45    let base = ultimate.bytes.as_ptr();46    let mut calls = 0usize;47    {48        let current = &mut ultimate;49        let returned = current.with_retained_region_mut(|mut cap| {50            calls += 1;51            assert!(cap.as_ptr() == base && cap.len() == 32);52            {53                // Derive every loan from the admitted capability. There are no54                // preexisting references across whole-buffer mutable admission.55                let whole = cap.get_mut(0..32).unwrap();56                let (before, remaining) = whole.split_at_mut(8);57                let (middle, after) = remaining.split_at_mut(16);58                let before = &*before;59                let after = &*after;60                middle[index] = value;61                assert!(middle[index] == value);62                assert!(before[index] == original[index] && after[index] == original[24 + index]);63            }64            // All split loans end before this fresh capability reborrow.65            assert!(cap.as_region().get(8..16).unwrap()[index] == value);66            if fail { Err(Error::Bounds) } else { Ok(23usize) }67        });68        assert!(calls == 1);69        let correct = match returned {70            Ok(number) => !fail && number == 23,71            Err(Error::Bounds) => fail,72            Err(_) => false,73        };74        assert!(correct);75        assert!(current.bytes[8 + index] == value);76        assert!(current.bytes[index] == original[index] && current.bytes[24 + index] == original[24 + index]);77        // The callback and every subloan end before the next owner operation.78        current.write(16, &[0x71]).unwrap();79    }80    assert!(ultimate.bytes.as_ptr() == base && ultimate.bytes.len() == 32);81    let bytes = ultimate.into_bytes();82    let query: u8 = kani::any();83    kani::assume(query < 32);84    let query = query as usize;85    assert!(bytes[query] == if query == 16 { 0x71 } else if query == 8 + index { value } else { original[query] });86    kani::cover!(fail && original[8 + index] == 0xa5 && value == 0x31 && index == 7,87                 "actual callback returns error after its exact native byte effect and owner rejoin");88    kani::cover!(!fail && query == 31 && original[31] == 0xa5 && original[8 + index] != value && value == 0x31,89                 "actual callback normal return preserves a distant neighbor byte");90}9192#[kani::proof]93fn negative_actual_dense_callback_changes_neighbor() {94    let mut image = VecMemory::from_bytes(vec![0xa5; 32]);95    image.with_retained_region_mut(|mut cap| {96        cap.get_mut(8..16).unwrap()[0] = 0x17;97        Ok(())98    }).unwrap();99    assert!(image.bytes[0] != 0xa5, "NEGATIVE_CONTROL_ACTUAL_DENSE_CALLBACK_NEIGHBOR_CHANGED");100}101102#[kani::proof]103fn negative_actual_dense_callback_uses_buffer_origin_for_window() {104    let mut original = [0; 32];105    original[0] = 0x11;106    original[8] = 0x22;107    let image = VecMemory::from_bytes(original.to_vec());108    image.with_retained_region(|cap| {109        let window = cap.get(8..16).unwrap();110        assert!(window[0] == original[0], "NEGATIVE_CONTROL_ACTUAL_DENSE_CALLBACK_WRONG_WINDOW_ORIGIN");111        Ok(())112    }).unwrap();113}