provider.rs

verification/proofs/page-pool/creusot/provider.rs

provider.rs360 lines← SDK documentation
1//! Explicit conditional boundary for a provider with a pinned control page.2//! The physical implementation of these primitive contracts is not proved.3use super::*;4#[path="resource_admission.rs"] mod resource_admission;5#[path="borrowed_zero.rs"] mod borrowed_zero;67// Models for the two Backing observations used by the physical bridges.8// An implementation must qualify its concrete control_ptr/pages bodies against9// these functions; their names alone supply no ownership or observation theorem.10// The borrowed-image implementation is checked by strict source erasure.11pub unsafe trait ResourceBacking: Backing {12    #[logic]13    fn resource_base(self) -> *mut u8;14    #[logic]15    fn resource_pages(self) -> usize;16    #[logic]17    fn resource_payload(self) -> Seq<u8>;18    #[logic(prophetic)]19    fn resource_end(self) -> Seq<u8>;20}2122#[logic(open(super))]23pub(super) fn backing_key<B: ResourceBacking>(backing: B) -> *mut u8 {24    backing.resource_base()25}2627#[logic(open(super))]28pub(super) fn backing_pages<B: ResourceBacking>(backing: B) -> usize {29    backing.resource_pages()30}3132#[logic(open(super))]33pub(super) fn backing_payload<B: ResourceBacking>(backing: B) -> Seq<u8> {34    backing.resource_payload()35}3637#[logic(prophetic, open(super))]38pub(super) fn backing_end<B: ResourceBacking>(backing: B) -> Seq<u8> {39    backing.resource_end()40}4142// Borrow an existing real token; page observation is bound by the caller.43#[check(ghost)]44#[ensures(physical_byte_key(*result) == *control.ward())]45#[ensures(physical_bytes(*result) == (*control).val_unsized()@)]46#[ensures(page_count(*result) == pages)]47#[ensures(end_control(*result) == ^*control)]48#[ensures(clean_pages(*result) == Seq::create(pages@, |_| false))]49#[ensures(zero_requests(*result) == Seq::empty())]50pub(super) fn borrow_control<'p>(control: Ghost<&'p mut Perm<*const [u8]>>, pages: usize) -> Ghost<PhysicalState<'p>> {51    ghost! {52        let clean = snapshot!(Seq::create(pages@, |_| false));53        let zeroes = snapshot!(Seq::empty());54        PhysicalState { control: control.into_inner(), pages,55            clean: clean.into_ghost().into_inner(), zeroes: zeroes.into_ghost().into_inner() }56    }57}5859#[logic(open(super))]60pub(super) fn page_count(state: PhysicalState) -> usize { state.pages }6162#[logic(open(super))]63pub(super) fn physical_bytes(state: PhysicalState) -> Seq<u8> {64    pearlite! { state.control.val_unsized()@ }65}6667#[logic(open(super))]68pub(super) fn clean_pages(state: PhysicalState) -> Seq<bool> { state.clean }6970#[logic(open(super))]71pub(super) fn zero_requests(state: PhysicalState) -> Seq<(usize, usize)> { state.zeroes }7273#[logic(open(super))]74pub(super) fn initialized_run(state: PhysicalState, run: Run) -> bool {75    pearlite! { 1usize <= run.start && 0usize < run.count76        && run.start@ + run.count@ <= page_count(state)@77        && forall<p: usize> run.start <= p && p@ < run.start@ + run.count@ ==> clean_pages(state)[p@] }78}7980#[logic(open(super))]81pub(super) fn physical_words(state: PhysicalState) -> Seq<u64> {82    control_words(*state.control)83}8485#[logic(open(super))]86pub(super) fn physical_key(state: PhysicalState) -> *mut u8 {87    pearlite! { state.control.ward().thin().offset_logic(-ROOT_BYTES@) as *mut u8 }88}8990#[logic(prophetic, open(super))]91pub(super) fn end_control(state: PhysicalState) -> Perm<*const [u8]> {92    pearlite! { ^state.control }93}9495#[logic(open(super))]96pub(super) fn physical_byte_key(state: PhysicalState) -> *const [u8] {97    pearlite! { *state.control.ward() }98}99100// Keep the byte-content quantifier inside the provider proof boundary. Sibling101// algorithms use the checked preservation/extraction lemmas below; identity,102// page count and extents remain transparent in physical_bound.103#[logic(open(self))]104pub(super) fn clean_payload(payload: Seq<u8>, clean: Seq<bool>, pages: usize) -> bool {105    pearlite! { forall<p: usize, byte> 1usize <= p && p < pages106        && clean[p@] && 0 <= byte && byte < PAGE_BYTES@ ==>107        payload[(p@ - 1) * PAGE_BYTES@ + byte] == 0u8 }108}109110#[logic(open(super))]111pub(super) fn physical_bound<B: ResourceBacking>(backing: B, state: PhysicalState) -> bool {112    pearlite! { physical_bytes(state).len() == (PAGE_BYTES - ROOT_BYTES)@113        && physical_key(state) == backing_key(backing) && page_count(state) == backing_pages(backing)114        && clean_pages(state).len() == page_count(state)@115        && backing_payload(backing).len() == if page_count(state) == 0usize { 0 }116            else { (page_count(state)@ - 1) * PAGE_BYTES@ }117        && clean_payload(backing_payload(backing), clean_pages(state), page_count(state)) }118}119120#[logic(open(super))]121pub(super) fn payload_zero_run<B: ResourceBacking>(backing: B, run: Run) -> bool {122    pearlite! { 1usize <= run.start && 0usize < run.count123        && run.start@ + run.count@ <= backing_pages(backing)@124        && (run.start@ + run.count@ - 1) * PAGE_BYTES@ <= backing_payload(backing).len()125        && forall<p: usize, byte> run.start <= p && p@ < run.start@ + run.count@126            && 0 <= byte && byte < PAGE_BYTES@ ==>127            backing_payload(backing)[(p@ - 1) * PAGE_BYTES@ + byte] == 0u8 }128}129130#[check(ghost)]131#[requires(physical_bound(*backing, **state) && initialized_run(**state, run))]132#[ensures(payload_zero_run(*backing, run))]133pub(super) fn initialized_bytes<B: ResourceBacking>(backing: Snapshot<B>, state: Ghost<&PhysicalState>, run: Run) {}134135#[check(ghost)]136#[requires(payload_zero_run(*backing, run))]137#[requires(run.start <= page && page@ < run.start@ + run.count@ && byte < PAGE_BYTES)]138#[ensures(0 <= (page@ - 1) * PAGE_BYTES@ + byte@)]139#[ensures((page@ - 1) * PAGE_BYTES@ + byte@ < backing_payload(*backing).len())]140#[ensures(backing_payload(*backing)[(page@ - 1) * PAGE_BYTES@ + byte@] == 0u8)]141pub(super) fn initialized_byte<B: ResourceBacking>(backing: Snapshot<B>, run: Run, page: usize, byte: usize) {}142143// Rebuild the physical binding after a metadata-only operation. The operation144// may change metadata bytes, but cannot change their extent or the payload.145#[check(ghost)]146#[requires(physical_bound(*before_backing, *before) && physical_ready(*before))]147#[requires(backing_key(*after_backing) == backing_key(*before_backing))]148#[requires(backing_pages(*after_backing) == backing_pages(*before_backing))]149#[requires(backing_payload(*after_backing) == backing_payload(*before_backing))]150#[requires(page_count(*after) == page_count(*before) && clean_pages(*after) == clean_pages(*before))]151#[requires(physical_byte_key(*after) == physical_byte_key(*before))]152#[requires(physical_bytes(*after).len() == physical_bytes(*before).len())]153#[ensures(physical_bound(*after_backing, *after) && physical_ready(*after))]154pub(super) fn metadata_frame<B: ResourceBacking>(before_backing: Snapshot<B>, after_backing: Snapshot<B>,155    before: Snapshot<PhysicalState>, after: Snapshot<PhysicalState>) {}156157// Forgetting cleanliness only weakens a bound; the forget operation itself158// needs no payload owner or ownership premise.159#[check(ghost)]160#[requires(physical_bound(*backing, *before))]161#[requires(physical_ready(*after))]162#[requires(page_count(*after) == page_count(*before))]163#[requires(physical_byte_key(*after) == physical_byte_key(*before))]164#[requires(forall<p: usize> p < page_count(*before) && clean_pages(*after)[p@] ==>165    clean_pages(*before)[p@])]166#[ensures(physical_bound(*backing, *after))]167pub(super) fn forget_bound<B: ResourceBacking>(backing: Snapshot<B>,168    before: Snapshot<PhysicalState>, after: Snapshot<PhysicalState>) {}169170impl<'p> PhysicalState<'p> {171    #[check(ghost)]172    #[ensures(result.val_unsized()@ == physical_bytes(*self))]173    #[ensures(control_words(*result) == physical_words(*self))]174    #[ensures(result.ward().thin().offset_logic(-ROOT_BYTES@) as *mut u8 == physical_key(*self))]175    pub(super) fn control(&self) -> &Perm<*const [u8]> { &*self.control }176177    #[check(ghost)]178    #[ensures(result.val_unsized()@ == physical_bytes(*self))]179    #[ensures((^result).val_unsized()@ == physical_bytes(^self))]180    #[ensures(control_words(*result) == physical_words(*self))]181    #[ensures(control_words(^result) == physical_words(^self))]182    #[ensures(result.ward().thin().offset_logic(-ROOT_BYTES@) as *mut u8 == physical_key(*self))]183    #[ensures((^result).ward().thin().offset_logic(-ROOT_BYTES@) as *mut u8 == physical_key(^self))]184    #[ensures(page_count(^self) == page_count(*self))]185    #[ensures(clean_pages(^self) == clean_pages(*self))]186    #[ensures(zero_requests(^self) == zero_requests(*self))]187    #[ensures(end_control(^self) == end_control(*self))]188    #[ensures(*result.ward() == physical_byte_key(*self))]189    #[ensures(*(^result).ward() == physical_byte_key(^self))]190    pub(super) fn control_mut(&mut self) -> &mut Perm<*const [u8]> { &mut *self.control }191192}193194// Dropping an allocation into external ownership forfeits any continuing195// zero-content claim. This ghost transition precedes arbitrary payload writes.196#[check(ghost)]197#[requires(physical_ready(**state))]198#[requires(run.start@ + run.count@ <= page_count(**state)@)]199#[ensures(physical_ready(^*state))]200#[ensures(physical_words(^*state) == physical_words(**state))]201#[ensures(physical_key(^*state) == physical_key(**state))]202#[ensures(physical_byte_key(^*state) == physical_byte_key(**state))]203#[ensures(end_control(^*state) == end_control(**state))]204#[ensures(page_count(^*state) == page_count(**state))]205#[ensures(zero_requests(^*state) == zero_requests(**state))]206#[ensures(forall<p: usize> p < page_count(**state) ==> clean_pages(^*state)[p@] ==207    if run.start <= p && p@ < run.start@ + run.count@ { false } else { clean_pages(**state)[p@] })]208pub(super) fn handoff_payload(mut state: Ghost<&mut PhysicalState>, run: Run) {209    ghost! {210        let clean = snapshot!(Seq::create((**state).clean.len(), |p|211            if run.start@ <= p && p < run.start@ + run.count@ { false } else { (**state).clean[p] }));212        (**state).clean = clean.into_ghost().into_inner();213    };214}215216#[check(ghost)]217#[requires(physical_ready(**state))]218#[requires(1usize <= run.start && 0usize < run.count)]219#[requires(run.start@ + run.count@ <= page_count(**state)@)]220pub(super) fn handed_out_payload_is_dirty(mut state: Ghost<&mut PhysicalState>, run: Run) {221    handoff_payload(ghost!(&mut **state), run);222    proof_assert!(!clean_pages(**state)[run.start@]);223    proof_assert!(!initialized_run(**state, run));224}225226// Physical TCB: this observation reads the count represented by the exclusive227// provider resource. It does not assume saved-header agreement or any contents.228#[trusted]229#[check(terminates)]230#[erasure(<B as Backing>::pages)]231#[requires(physical_bound(*backing, **state))]232#[ensures(result == page_count(**state))]233pub(super) fn observed_pages<B: Backing + provider::ResourceBacking>(backing: &B, state: Ghost<&PhysicalState>) -> usize {234    backing.pages()235}236237238#[logic(open(super))]239pub(super) fn physical_ready(state: PhysicalState) -> bool {240    pearlite! {241        1usize <= page_count(state)242        && physical_bytes(state).len() == (PAGE_BYTES - ROOT_BYTES)@243        && well_sized(physical_words(state))244        && clean_pages(state).len() == page_count(state)@245        && (physical_key(state)).addr_logic()@ + page_count(state)@ * PAGE_BYTES@ <= usize::MAX@246    }247}248249// Physical TCB, pinned-control scope: growth preserves the existing control250// allocation and its words. This is the exact, failure-atomic count contract251// required by unsafe Backing::grow_to; implementations are still physical TCB.252#[trusted]253#[check(terminates)]254#[erasure(<B as Backing>::grow_to)]255#[requires(physical_bound(*backing, **state) && physical_ready(**state))]256#[ensures(physical_bound(^backing, ^*state) && physical_ready(^*state))]257#[ensures(backing_key(^backing) == backing_key(*backing))]258#[ensures(physical_key(^*state) == physical_key(**state))]259#[ensures(physical_byte_key(^*state) == physical_byte_key(**state))]260#[ensures(end_control(^*state) == end_control(**state))]261#[ensures(physical_words(^*state) == physical_words(**state))]262#[ensures(page_count(**state) <= page_count(^*state))]263#[ensures(page_count(^*state) == if result { if page_count(**state) < pages { pages } else { page_count(**state) } }264    else { page_count(**state) })]265#[ensures(zero_requests(^*state) == zero_requests(**state))]266#[ensures(!result ==> clean_pages(^*state) == clean_pages(**state))]267#[ensures(forall<p: usize> p < page_count(^*state) ==> clean_pages(^*state)[p@] ==268    if p < page_count(**state) { clean_pages(**state)[p@] } else { true })]269#[ensures(backing_end(^backing) == backing_end(*backing))]270#[ensures(forall<i> 0 <= i && i < backing_payload(*backing).len() ==>271    backing_payload(^backing)[i] == backing_payload(*backing)[i])]272#[ensures(forall<i> backing_payload(*backing).len() <= i && i < backing_payload(^backing).len() ==>273    backing_payload(^backing)[i] == 0u8)]274pub(super) fn grow_pages<B: Backing + provider::ResourceBacking>(275    backing: &mut B, pages: usize, state: Ghost<&mut PhysicalState>,276) -> bool {277    backing.grow_to(pages)278}279280// The raw pointer names the exact allocation held by the actual Perm resource.281#[trusted]282#[check(terminates)]283#[erasure(<B as Backing>::control_ptr)]284#[requires(physical_bound(*backing, **state) && physical_ready(**state))]285#[ensures(result == physical_key(**state))]286pub(super) fn observed_control_ptr<B: Backing + provider::ResourceBacking>(287    backing: &B, state: Ghost<&PhysicalState>,288) -> *mut u8 {289    backing.control_ptr()290}291292// A narrow permission-aware raw add, including the actual pointer's provenance.293#[trusted]294#[check(terminates)]295#[erasure(<*mut u8>::add)]296#[requires(physical_ready(**state))]297#[requires(pointer == physical_key(**state) && offset <= PAGE_BYTES)]298#[ensures(result == pointer.offset_logic(offset@))]299pub(super) unsafe fn control_add(300    pointer: *mut u8, offset: usize, state: Ghost<&PhysicalState>,301) -> *mut u8 {302    unsafe { pointer.add(offset) }303}304305// Physical TCB: the single production memset, with exact word effect and frame.306// This contract says nothing about headers, partitions or summary consistency.307#[trusted]308#[check(terminates)]309#[erasure(core::ptr::write_bytes::<u8>)]310#[requires(physical_ready(**state))]311#[requires(pointer == (physical_key(**state)).offset_logic(HEADER_OFFSET@))]312#[requires(value == 0u8 && count == METADATA_END - HEADER_OFFSET)]313#[ensures(physical_ready(^*state))]314#[ensures(physical_key(^*state) == physical_key(**state))]315#[ensures(physical_byte_key(^*state) == physical_byte_key(**state))]316#[ensures(end_control(^*state) == end_control(**state))]317#[ensures(page_count(^*state) == page_count(**state))]318#[ensures(clean_pages(^*state) == clean_pages(**state))]319#[ensures(zero_requests(^*state) == zero_requests(**state))]320#[ensures(forall<i> 0 <= i && i < physical_words(**state).len() ==>321    physical_words(^*state)[i] == if HEADER_OFFSET@ / 8 <= i && i < METADATA_END@ / 8 {322        0u64323    } else { physical_words(**state)[i] })]324pub(super) unsafe fn zero_metadata(325    pointer: *mut u8, value: u8, count: usize, state: Ghost<&mut PhysicalState>,326) {327    unsafe { ptr::write_bytes(pointer, value, count) }328}329330// Physical TCB: the actual Backing::zero supplies zero content for this exact331// whole-page request. The log records the call, not a persistent promise about332// later app writes. The backing implementation remains a separate producer.333#[trusted]334#[check(terminates)]335#[erasure(<B as Backing>::zero)]336#[requires(physical_bound(*backing, **state) && physical_ready(**state))]337#[requires(PAGE_BYTES <= bytes.start && bytes.start <= bytes.end)]338#[requires(bytes.end@ <= page_count(**state)@ * PAGE_BYTES@)]339#[requires(bytes.start@ % PAGE_BYTES@ == 0 && bytes.end@ % PAGE_BYTES@ == 0)]340#[ensures(physical_bound(^backing, ^*state) && physical_ready(^*state))]341#[ensures(backing_key(^backing) == backing_key(*backing))]342#[ensures(physical_key(^*state) == physical_key(**state))]343#[ensures(physical_byte_key(^*state) == physical_byte_key(**state))]344#[ensures(end_control(^*state) == end_control(**state))]345#[ensures(physical_words(^*state) == physical_words(**state))]346#[ensures(page_count(^*state) == page_count(**state))]347#[ensures(zero_requests(^*state) == zero_requests(**state).push_back((bytes.start, bytes.end)))]348#[ensures(backing_end(^backing) == backing_end(*backing))]349#[ensures(forall<i> 0 <= i && i < backing_payload(*backing).len() ==>350    backing_payload(^backing)[i] == if bytes.start@ - PAGE_BYTES@ <= i && i < bytes.end@ - PAGE_BYTES@ {351        0u8352    } else { backing_payload(*backing)[i] })]353#[ensures(forall<p: usize> p < page_count(**state) ==> clean_pages(^*state)[p@] ==354    if bytes.start@ / PAGE_BYTES@ <= p@ && p@ < bytes.end@ / PAGE_BYTES@ { true }355    else { clean_pages(**state)[p@] })]356pub(super) unsafe fn zero_payload<B: Backing + provider::ResourceBacking>(357    backing: &mut B, bytes: Range<usize>, state: Ghost<&mut PhysicalState>,358) {359    unsafe { backing.zero(bytes) }360}