crates

block_roster_contract

check proofs license minimum Rust version

The page that answers "what block devices are attached": a small, fixed-width, pointer-free layout that one party writes once, at wiring time, and that other programs then map read-only and read. no_std, no dependencies. The writer and the readers share this one definition, so they cannot drift.

This is a shared page, not a message protocol. There is no request, no reply and no server. Holding a roster tells a program that a device exists; it does not let the program touch it. Something else decides which program gets which device's endpoint, so a program without the page has nowhere to look.

How a program comes to hold the page, and to whom it declares INTERFACE_ID, are the business of the system around this crate; the crate fixes the bytes and nothing else.

The contract

The page is little-endian:

bytes field
0..8 MAGIC, the ASCII text blkrosv1 read as a little-endian u64
8..12 count: how many entries follow
12..16 capacity: how many entries this page has room for; the reader refuses a count past it
16.. entries, ENTRY_BYTES (8) each

An entry is a u32 ordinal (which device of this transport, counting from 0) and a u32 transport, one of TRANSPORT_VIRTIO_MEMORY_MAPPED_IO (0), TRANSPORT_VIRTIO_PCI (1) and TRANSPORT_NON_VOLATILE_MEMORY_EXPRESS (2).

EXAMPLES

use block_roster_contract::{Device, Roster, TRANSPORT_VIRTIO_PCI};

// The writer lays the roster into a page it owns.
let mut page = [0u8; 4096];
block_roster_contract::write(&mut page, &[Device { ordinal: 0, transport: TRANSPORT_VIRTIO_PCI }]).unwrap();

// A reader holds the mapped page and borrows from it.
let roster = Roster::read(&page).unwrap();
assert_eq!(roster.len(), 1);
assert_eq!(roster.get(0).unwrap().transport_name(), Some("virtio-pci"));
assert_eq!(roster.count_by_transport(TRANSPORT_VIRTIO_PCI), 1);

What is proven

Five Kani harnesses in src/lib.rs prove five things. Roster::read is total on any bytes, up to three entries (40 bytes). read is also total on a whole 4096-byte page with any header, and every entry it counts there can be fetched. entry_capacity never claims more room than a page of any length has. write is total on any page length up to 40 bytes and refuses a page too short for its header. write then read returns the same devices in order. Install Kani and run them as "Building and testing" below describes.

Building and testing

cargo build
cargo test
rustup target add x86_64-unknown-none aarch64-unknown-none-softfloat riscv64imac-unknown-none-elf
cargo build --lib --target x86_64-unknown-none   # also aarch64-unknown-none-softfloat, riscv64imac-unknown-none-elf

Run rustup target add once, before the first bare-metal build. Without it, that build stops with can't find crate for core. cargo test runs on the host and covers the unit tests and the documentation tests, one of which is the example above.

Running the proofs

The proofs need Kani, which rustup does not install. Run cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup. The setup step downloads a model checker, so it needs network access. CI uses version 0.68.0, and another version may verify differently.

Then cargo kani, from the crate's directory, runs all five harnesses. On an x86_64 Linux desktop that takes one to two minutes and peaks near 2.5 gigabytes of memory. CI runs plain cargo kani, with no extra flags. To run one harness, give its full path and --exact:

cargo kani --harness verification::write_is_total_on_any_page_length --exact

A passing run prints several lines that look alarming. All of them are expected:

Trust the last line of the run: Complete - 5 successfully verified harnesses, 0 failures, 5 total.

Running one falsification patch

patch -p1 --no-backup-if-mismatch < falsifications/verification.write_is_total_on_any_page_length.patch
cargo kani --harness verification::write_is_total_on_any_page_length --exact   # ends VERIFICATION:- FAILED
git checkout -- src

The harness name is the part of the patch's file name after verification.. Every patch turns its harness red: the run ends with VERIFICATION:- FAILED and Complete - 0 successfully verified harnesses, 1 failures, 1 total., and git checkout -- src reverts the patch. What the failed check says differs by patch, because Kani reports a failed assert_eq!, or an index out of bounds, as a placeholder message from inside the standard library:

Each patch's header says which assertion or index it breaks. A reader compares the kind of failure (an index out of bounds, or a failed assert_eq!) with that header, since the standard library's file and line say nothing about this crate.

BUGS

Minimum Rust version

Rust 1.88, the first release with slice::as_chunks, which the reader uses. To check it, install that toolchain and a bare-metal target, then test and build with it:

rustup toolchain install 1.88 --profile minimal --target x86_64-unknown-none
cargo +1.88 test
cargo +1.88 build --lib --target x86_64-unknown-none

License

Licensed under either of MIT (LICENSE-MIT) or Apache-2.0 (LICENSE-APACHE), at your option.