crates

clock_contract

check proofs license minimum Rust version

The wall clock as a contract between a clock service and everything that reads or sets the time. It holds the layout of a shared clock page, the small protocol for proposing a new time, and the policy a proposal must meet. no_std, no dependencies outside an optional model-checking build. A clock service, its readers and a standard library's time support share this one definition, so they cannot drift. On its own the crate maps nothing and sets nothing; it is useful with a clock service that maps the page and a loader that grants it.

Reading the clock is a read-only mapping of the page. Setting it is a read/write mapping of the same page. Proposing a time is a message to an endpoint, which the service answers by applying the policy here. Wall-clock time is the monotonic counter plus an offset, and only the offset is ever written, so stepping the clock cannot disturb a monotonic Instant. Times are nanoseconds since the Unix epoch in a u64, which runs out in 2554.

The contract

EXAMPLES

use clock_contract::{NANOSECONDS_PER_SECOND, offset_for, policy, state, status, wall_nanoseconds};

// The machine has been up for 5 seconds and the service learns the wall time.
let monotonic = 5 * NANOSECONDS_PER_SECOND;
let now = 1_800_000_000 * NANOSECONDS_PER_SECOND;
assert_eq!(policy::decide(state::UNKNOWN, 0, now), status::ACCEPTED);
let offset = offset_for(now, monotonic);
assert_eq!(wall_nanoseconds(offset, monotonic), now);

// A proposal a day ahead of a synchronized clock is refused.
assert_eq!(
    policy::decide(state::SYNCHRONIZED, now, now + 86_400 * NANOSECONDS_PER_SECOND),
    status::REFUSED_TOO_FAR_FORWARD,
);

What is proven

Two Kani harnesses cover every input:

Each has a patch under falsifications/ that makes it fail.

The loom run checks the clock page against the C11 model, not against the Arm or RISC-V memory models. Its models hold three claims. A reader never returns a torn pair, including a reader that sleeps across two publishes and one that reads while the page is initialized. A writer stopped at any point inside a publish never makes a reader wait, and a restart that clears its claim keeps the last publish. Two writers never interleave. Weakening any ordering on the page fails one of them, except the release that gives the claim back, which loom passes and the argument in the source carries.

Building and testing

The crate needs 64-bit atomics (target_has_atomic = "64"): every word of the page is an AtomicU64, so a target without them fails to build with a message that says so.

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
cargo build --lib --target aarch64-unknown-none-softfloat
cargo build --lib --target riscv64imac-unknown-none-elf

Run rustup target add once, before the first bare-metal build. cargo test runs the unit tests and the doctests in a few seconds.

Running the loom models

RUSTFLAGS="--cfg loom" cargo test --release

This runs the loom models in place of the unit tests, so it reports fewer tests than cargo test, each a model rather than a unit test. It takes under half a minute, most of it compiling loom.

Running the proofs

Install Kani 0.68.0, the version CI uses, with cargo install --locked kani-verifier --version 0.68.0, then cargo kani setup, which downloads a model checker. Plain cargo kani runs both harnesses in a few seconds and ends Complete - 2 successfully verified harnesses, 0 failures, 2 total. To run one:

cargo kani --harness verification::a_proposal_is_accepted_only_inside_the_policy_bounds --exact

Running one falsification patch

git apply falsifications/verification.a_proposal_is_accepted_only_inside_the_policy_bounds.patch
cargo kani --harness verification::a_proposal_is_accepted_only_inside_the_policy_bounds --exact   # ends VERIFICATION:- FAILED
git checkout -- src

The harness name is the part of the patch's file name after verification., and each patch turns its own harness red.

BUGS

Minimum Rust version

Rust 1.88, for the host and the bare-metal builds alike. To check 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.