crates

byte_sink_contract

check proofs license minimum Rust version

One framing for "write these bytes there".

Up to sixteen bytes ride in three 64-bit message registers, with an opcode and a count in the first word, so a terminal, a file and a pipe can sit behind one capability slot.

A writer packs its bytes with pack, sends the three words, and ends the stream with one end-of-stream message. A sink decodes each message with unpack, which refuses anything the framing does not define. The crate is no_std, has no dependencies and is dual licensed MIT OR Apache-2.0. Kani proves it: every property it claims is a harness in the tree, and every harness carries a falsification patch that shows what turns it red.

EXAMPLES

Pack a line, decode each message as a sink would, and end the stream.

use byte_sink_contract::{MAXIMUM_INLINE_BYTES, Message, MessageWords, end_of_stream, pack, unpack};

let line = b"the quick brown fox\n"; // 20 bytes: two messages and an end of stream
let mut received = [0u8; 32];
let mut got = 0;

let mut rest = &line[..];
while !rest.is_empty() {
    let (words, taken) = pack(rest);
    rest = &rest[taken..]; // the count says how much `pack` took, so no tail is lost silently

    let mut buffer = [0u8; MAXIMUM_INLINE_BYTES];
    match unpack(words, &mut buffer) {
        Message::Bytes(count) => {
            received[got..got + count].copy_from_slice(&buffer[..count]);
            got += count;
        }
        Message::EndOfStream | Message::Malformed => unreachable!(),
    }
}

let end = MessageWords::new(end_of_stream(), 0, 0);
assert_eq!(unpack(end, &mut [0u8; MAXIMUM_INLINE_BYTES]), Message::EndOfStream);
assert_eq!(&received[..got], line);

Tell a delivered message from a missing sink and from a destroyed one by the return of the send.

use byte_sink_contract::{GONE, Sent, classify};

assert_eq!(classify(0), Sent::Ok);
assert_eq!(classify(GONE), Sent::Gone); // the sink was destroyed: the writer must end

What is proven

Six Kani harnesses state the framing for every input:

Each harness has a falsification record at falsifications/<module>.<harness>.patch. Applying it with git apply -p1 from the crate root makes that harness fail at its own assertion, which shows the proof checks something. The patches need a git checkout, and you revert each one with git checkout -- src before the next.

Not proven: that a particular sink acts on end of stream, or that a writer sends zero in the reserved words. The decoder refuses a non-zero reserved word; the writer's side is a convention.

Build, test and prove

You need a Rust toolchain at or above the minimum version at the foot of this page (rustup is the usual way to get one) and a git checkout. From the crate root:

Running one falsification patch

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

The failing run names the assertion that a return of zero is Ok.

BUGS

Minimum Rust version

Rust 1.88. Check it with rustup toolchain install 1.88 --profile minimal, then cargo +1.88 test, and the bare-metal build with the same +1.88.

License

MIT OR Apache-2.0, at your option: see LICENSE-MIT and LICENSE-APACHE.