context_support.rs
verification/proofs/hylix/kani/context_support.rs
1use super::*;2use crate::Block;3use core::cell::Cell;45// A conditional Storage contract fixture: one live initialized 16-byte block,6// exact bounded writes/reads, read-only discard, successful begin/finish. It is7// not an Arena allocator or a producer of IC-wide physical stable authority.8struct TraceStorage { bytes: [u8; 16], events: Cell<u64> }9impl TraceStorage {10 fn new(value: u8) -> Self { Self { bytes: [value; 16], events: Cell::new(0) } }11 fn event(&self, n: u64) { self.events.set((self.events.get() << 4) | n); }12}13impl Storage for TraceStorage {14 fn frame_begin(&self, writable: bool) -> Result<()> { self.event(if writable { 1 } else { 6 }); Ok(()) }15 fn frame_finish(&mut self) -> Result<()> { self.event(3); Ok(()) }16 fn frame_discard(&self) { self.event(5); }17 fn allocate(&mut self, _: u64) -> Result<Block> { Err(Error::Bounds) }18 fn deallocate(&mut self, _: Block) -> Result<()> { Err(Error::Bounds) }19 fn resize(&mut self, _: Block, _: u64) -> Result<Block> { Err(Error::Bounds) }20 fn len(&self, block: Block) -> Result<u64> {21 if block == crate::storage::fixture_block() { Ok(16) } else { Err(Error::Bounds) }22 }23 fn read(&self, block: Block, at: u64, out: &mut [u8]) -> Result<()> {24 self.len(block)?;25 let start = usize::try_from(at).map_err(|_| Error::Bounds)?;26 let end = start.checked_add(out.len()).ok_or(Error::Bounds)?;27 out.copy_from_slice(self.bytes.get(start..end).ok_or(Error::Bounds)?);28 self.event(4); Ok(())29 }30 fn write(&mut self, block: Block, at: u64, bytes: &[u8]) -> Result<()> {31 self.len(block)?;32 let start = usize::try_from(at).map_err(|_| Error::Bounds)?;33 let end = start.checked_add(bytes.len()).ok_or(Error::Bounds)?;34 self.bytes.get_mut(start..end).ok_or(Error::Bounds)?.copy_from_slice(bytes);35 self.event(2); Ok(())36 }37}38fn outcome(result: Result<usize>, fail: bool, value: usize) -> bool {39 match result { Ok(n) => !fail && n == value, Err(Error::Bounds) => fail, Err(_) => false }40}