publication_probe.rs

verification/proofs/hylix/kani/publication_probe.rs

publication_probe.rs38 lines← SDK documentation
1use super::*;2use cdk_rsx_platform::memory::{MemoryError, MemoryResult};34struct Image { bytes: [u8; 1536], writes: usize }5unsafe impl Memory for Image {6    fn len(&self) -> u64 { self.bytes.len() as u64 }7    fn grow_to(&mut self, len: u64) -> MemoryResult<()> {8        if len <= self.len() { Ok(()) } else { Err(MemoryError::Bounds) }9    }10    fn read(&self, at: u64, out: &mut [u8]) -> MemoryResult<()> {11        let start = usize::try_from(at).map_err(|_| MemoryError::Bounds)?;12        let end = start.checked_add(out.len()).ok_or(MemoryError::Bounds)?;13        out.copy_from_slice(self.bytes.get(start..end).ok_or(MemoryError::Bounds)?);14        Ok(())15    }16    fn write(&mut self, at: u64, bytes: &[u8]) -> MemoryResult<()> {17        let start = usize::try_from(at).map_err(|_| MemoryError::Bounds)?;18        let end = start.checked_add(bytes.len()).ok_or(MemoryError::Bounds)?;19        self.bytes.get_mut(start..end).ok_or(MemoryError::Bounds)?.copy_from_slice(bytes);20        self.writes += 1;21        Ok(())22    }23}2425#[kani::proof]26#[kani::unwind(20)]27fn actual_working_set_frontend_probe() {28    let mut ws = WorkingSet::new(Image { bytes: [0; 1536], writes: 0 },29        WorkingSetConfig { segment_bytes: 512, max_segments: 1, retain_clean: false }).unwrap();30    ws.begin(true).unwrap();31    ws.write(8, &[17]).unwrap();32    let mut got = [0; 1];33    ws.read(8, &mut got).unwrap();34    assert!(got[0] == 17);35    assert!(ws.memory.borrow().writes == 0);36    ws.finish().unwrap();37    assert!(ws.memory.borrow().bytes[8] == 17 && ws.memory.borrow().writes == 1);38}