context_publication.rs

verification/proofs/hylix/kani/context_publication.rs

context_publication.rs103 lines← SDK documentation
1#[kani::proof]2#[kani::unwind(20)]3fn actual_update_finishes_after_normal_and_returned_error() {4    let original: [u8; 16] = kani::any();5    let initial = original[3];6    let value: u8 = kani::any();7    let fail: bool = kani::any();8    let mut storage = TraceStorage { bytes: original, events: Cell::new(0) };9    let returned = using(&mut storage, || {10        with_storage(|s| s.write(crate::storage::fixture_block(), 3, &[value]))?;11        if fail { Err(Error::Bounds) } else { Ok(17usize) }12    });13    assert!(outcome(returned, fail, 17));14    // Begin(write), operation, finish; there is no discard on either return.15    assert!(storage.events.get() == 0x123);16    let query: u8 = kani::any();17    kani::assume(query < 16);18    assert!(storage.bytes[query as usize] == if query == 3 { value } else { original[query as usize] });19    assert!(EPOCH.get() == 0 && LOANS.get() == 0);20    assert!(ACTIVE.with(|slot| slot.borrow().is_none()));21    kani::cover!(fail && value != initial && query == 3,22                 "ordinary Err finalizes after a real byte effect");23    kani::cover!(!fail && value != initial && query == 15,24                 "normal completion frames an untouched neighbor");25}2627#[kani::proof]28#[kani::unwind(20)]29fn actual_query_discards_and_cannot_obtain_mutable_storage() {30    let original: [u8; 16] = kani::any();31    let initial = original[3];32    let fail: bool = kani::any();33    let storage = TraceStorage { bytes: original, events: Cell::new(0) };34    let called = Cell::new(false);35    let returned = using_read(&storage, || {36        let denied = with_storage(|_| { called.set(true); Ok(()) });37        assert!(matches!(denied, Err(Error::InvalidInput(_))));38        let mut got = [0; 1];39        with_storage_read(|s| s.read(crate::storage::fixture_block(), 3, &mut got))?;40        assert!(got[0] == initial);41        if fail { Err(Error::Bounds) } else { Ok(23usize) }42    });43    assert!(outcome(returned, fail, 23));44    assert!(!called.get());45    // Begin(read), read, discard. In particular frame_finish was never called.46    assert!(storage.events.get() == 0x645);47    let query: u8 = kani::any();48    kani::assume(query < 16);49    assert!(storage.bytes[query as usize] == original[query as usize]);50    assert!(EPOCH.get() == 0 && LOANS.get() == 0);51    assert!(ACTIVE.with(|slot| slot.borrow().is_none()));52    kani::cover!(fail && initial == 0x37, "query Err still discards without a write or finish");53    kani::cover!(!fail && initial == 0xa5, "normal query discards without a write or finish");54}5556#[kani::proof]57#[kani::unwind(20)]58fn actual_constructed_loan_ends_before_finalization_and_next_epoch() {59    let mut storage = TraceStorage::new(0);60    let first = using(&mut storage, || {61        let guard = ContextLoan::new()?;62        let epoch = guard.context_id();63        assert!(epoch != 0 && epoch == context_id()?);64        assert!(LOANS.get() == 1 && check_no_loans().is_err());65        assert!(guard.check().is_ok());66        drop(guard);67        assert!(LOANS.get() == 0 && check_no_loans().is_ok());68        Ok(epoch)69    }).unwrap();70    using(&mut storage, || {71        let guard = ContextLoan::new()?;72        assert!(guard.context_id() != first && guard.check().is_ok());73        drop(guard);74        Ok(())75    }).unwrap();76    assert!(storage.events.get() == 0x1313 && EPOCH.get() == 0 && LOANS.get() == 0);77    kani::cover!(first == 1, "two actual context entries construct different live guard epochs");78}7980#[kani::proof]81#[kani::unwind(20)]82fn actual_epoch_check_and_drop_have_exact_conditional_scalar_effect() {83    // Method-local predecessor state, not a constructor of an active provider,84    // a stale owning guard or a physical pin. A matching live guard requires a85    // positive counter. The reachable normal constructor is checked above.86    let current: u64 = kani::any();87    let issued: u64 = kani::any();88    let count: usize = kani::any();89    kani::assume(issued != 0 && (current != issued || count > 0));90    EPOCH.set(current);91    LOANS.set(count);92    let guard = ContextLoan { epoch: issued, _local: core::marker::PhantomData };93    assert!(guard.check().is_ok() == (current == issued));94    drop(guard);95    assert!(LOANS.get() == if current == issued { count - 1 } else { count });96    assert!(EPOCH.get() == current);97    kani::cover!(current == 2 && issued == 1 && count == 1,98                 "stale guard check rejects and Drop preserves the later frame counter");99    kani::cover!(current == issued && current == u64::MAX && count == 1,100                 "largest matching epoch drops exactly one scalar loan");101    EPOCH.set(0);102    LOANS.set(0);103}