byte_support.rs

verification/proofs/stable/kani/byte_support.rs

byte_support.rs79 lines← SDK documentation
1use crate::error::{MemoryError as Error, MemoryResult as Result};2use crate::stable_capacity::{CapacityError, StableCapacity};34const MAX_PAGES: u64 = u64::MAX / 65_536;5const WINDOW: usize = 16;6const BUFFER: usize = 8;78#[derive(Clone, Copy)]9struct Primitive {10    pages: u64,11    base: u64,12    bytes: [u8; WINDOW],13    reads: u8,14    writes: u8,15    pointer: u64,16    offset: u64,17    size: u64,18}1920static mut PRIMITIVE: Primitive = Primitive {21    pages: 0, base: 0, bytes: [0; WINDOW], reads: 0, writes: 0,22    pointer: 0, offset: 0, size: 0,23};2425// Explicit System API assumption: one exclusive stable domain and one borrowed26// 16-byte image window. These functions model the VM; they do not verify it.27unsafe fn stable64_size() -> i64 {28    let state = unsafe { PRIMITIVE };29    assert!(state.pages <= MAX_PAGES);30    state.pages as i6431}3233unsafe fn stable64_grow(_: i64) -> i64 { panic!("growth excluded from byte I/O proof") }3435unsafe fn stable64_read(destination: i64, offset: i64, size: i64) {36    let mut state = unsafe { PRIMITIVE };37    assert!(size >= 0 && size as u64 <= BUFFER as u64);38    assert!(offset as u64 >= state.base);39    let start = (offset as u64 - state.base) as usize;40    assert!(start <= WINDOW && size as usize <= WINDOW - start);41    assert!((offset as u64 as u128) + size as u128 <= state.pages as u128 * 65_536);42    state.reads += 1;43    state.pointer = destination as u64;44    state.offset = offset as u64;45    state.size = size as u64;46    for index in 0..size as usize {47        unsafe { (destination as *mut u8).add(index).write(state.bytes[start + index]); }48    }49    unsafe { PRIMITIVE = state; }50}5152unsafe fn stable64_write(offset: i64, source: i64, size: i64) {53    let mut state = unsafe { PRIMITIVE };54    assert!(size >= 0 && size as u64 <= BUFFER as u64);55    assert!(offset as u64 >= state.base);56    let start = (offset as u64 - state.base) as usize;57    assert!(start <= WINDOW && size as usize <= WINDOW - start);58    assert!((offset as u64 as u128) + size as u128 <= state.pages as u128 * 65_536);59    state.writes += 1;60    state.pointer = source as u64;61    state.offset = offset as u64;62    state.size = size as u64;63    for index in 0..size as usize {64        state.bytes[start + index] = unsafe { (source as *const u8).add(index).read() };65    }66    unsafe { PRIMITIVE = state; }67}6869fn abort_storage(_: Error) -> ! { panic!("byte adapter abort") }7071// Signature-only shell. Every implementation body and the range checker are72// copied unchanged from wasm.rs; only external System API imports are modeled.73unsafe trait Memory {74    fn abort(&self, error: Error) -> !;75    fn len(&self) -> u64;76    fn grow_to(&mut self, bytes: u64) -> Result<()>;77    fn read(&self, offset: u64, out: &mut [u8]) -> Result<()>;78    fn write(&mut self, offset: u64, bytes: &[u8]) -> Result<()>;79}